^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * PS3 FLASH ROM Storage Driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2007 Sony Computer Entertainment Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright 2007 Sony Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/lv1call.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <asm/ps3stor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define DEVICE_NAME "ps3flash"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define FLASH_BLOCK_SIZE (256*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct ps3flash_private {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct mutex mutex; /* Bounce buffer mutex */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u64 chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int tag; /* Start sector of buffer, -1 if invalid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) bool dirty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static struct ps3_storage_device *ps3flash_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int ps3flash_read_write_sectors(struct ps3_storage_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u64 start_sector, int write)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u64 res = ps3stor_read_write_sectors(dev, dev->bounce_lpar,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) start_sector, priv->chunk_sectors,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) dev_err(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) __LINE__, write ? "write" : "read", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int ps3flash_writeback(struct ps3_storage_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (!priv->dirty || priv->tag < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) res = ps3flash_read_write_sectors(dev, priv->tag, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) priv->dirty = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static int ps3flash_fetch(struct ps3_storage_device *dev, u64 start_sector)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) if (start_sector == priv->tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) res = ps3flash_writeback(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) priv->tag = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) res = ps3flash_read_write_sectors(dev, start_sector, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) priv->tag = start_sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static loff_t ps3flash_llseek(struct file *file, loff_t offset, int origin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) struct ps3_storage_device *dev = ps3flash_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return generic_file_llseek_size(file, offset, origin, MAX_LFS_FILESIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) dev->regions[dev->region_idx].size*dev->blk_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static ssize_t ps3flash_read(char __user *userbuf, void *kernelbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) size_t count, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct ps3_storage_device *dev = ps3flash_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) u64 size, sector, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) size_t remaining, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) const void *src;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) dev_dbg(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) "%s:%u: Reading %zu bytes at position %lld to U0x%p/K0x%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) __func__, __LINE__, count, *pos, userbuf, kernelbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) size = dev->regions[dev->region_idx].size*dev->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (*pos >= size || !count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (*pos + count > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dev_dbg(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) "%s:%u Truncating count from %zu to %llu\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) __LINE__, count, size - *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) count = size - *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) sector = *pos / dev->bounce_size * priv->chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) offset = *pos % dev->bounce_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) remaining = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) n = min_t(u64, remaining, dev->bounce_size - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) src = dev->bounce_buf + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) mutex_lock(&priv->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) res = ps3flash_fetch(dev, sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dev_dbg(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) "%s:%u: copy %lu bytes from 0x%p to U0x%p/K0x%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) __func__, __LINE__, n, src, userbuf, kernelbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) if (userbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (copy_to_user(userbuf, src, n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) res = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) userbuf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (kernelbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) memcpy(kernelbuf, src, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) kernelbuf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) mutex_unlock(&priv->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) *pos += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) remaining -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) sector += priv->chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) } while (remaining > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) mutex_unlock(&priv->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static ssize_t ps3flash_write(const char __user *userbuf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) const void *kernelbuf, size_t count, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) struct ps3_storage_device *dev = ps3flash_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) struct ps3flash_private *priv = ps3_system_bus_get_drvdata(&dev->sbd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) u64 size, sector, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) size_t remaining, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) void *dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) dev_dbg(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) "%s:%u: Writing %zu bytes at position %lld from U0x%p/K0x%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) __func__, __LINE__, count, *pos, userbuf, kernelbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) size = dev->regions[dev->region_idx].size*dev->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (*pos >= size || !count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (*pos + count > size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) dev_dbg(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) "%s:%u Truncating count from %zu to %llu\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) __LINE__, count, size - *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) count = size - *pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) sector = *pos / dev->bounce_size * priv->chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) offset = *pos % dev->bounce_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) remaining = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) n = min_t(u64, remaining, dev->bounce_size - offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) dst = dev->bounce_buf + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) mutex_lock(&priv->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (n != dev->bounce_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) res = ps3flash_fetch(dev, sector);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) else if (sector != priv->tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) res = ps3flash_writeback(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) dev_dbg(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) "%s:%u: copy %lu bytes from U0x%p/K0x%p to 0x%p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) __func__, __LINE__, n, userbuf, kernelbuf, dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (userbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (copy_from_user(dst, userbuf, n)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) res = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) userbuf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (kernelbuf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) memcpy(dst, kernelbuf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) kernelbuf += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) priv->tag = sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) priv->dirty = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) mutex_unlock(&priv->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) *pos += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) remaining -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) sector += priv->chunk_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) } while (remaining > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) mutex_unlock(&priv->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static ssize_t ps3flash_user_read(struct file *file, char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) size_t count, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return ps3flash_read(buf, NULL, count, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) static ssize_t ps3flash_user_write(struct file *file, const char __user *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) size_t count, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return ps3flash_write(buf, NULL, count, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static ssize_t ps3flash_kernel_read(void *buf, size_t count, loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return ps3flash_read(NULL, buf, count, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static ssize_t ps3flash_kernel_write(const void *buf, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) loff_t pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int wb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) res = ps3flash_write(NULL, buf, count, &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* Make kernel writes synchronous */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) wb = ps3flash_writeback(ps3flash_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) if (wb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return wb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) static int ps3flash_flush(struct file *file, fl_owner_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) return ps3flash_writeback(ps3flash_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static int ps3flash_fsync(struct file *file, loff_t start, loff_t end, int datasync)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) inode_lock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) err = ps3flash_writeback(ps3flash_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) inode_unlock(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) static irqreturn_t ps3flash_interrupt(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) struct ps3_storage_device *dev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) u64 tag, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) res = lv1_storage_get_async_status(dev->sbd.dev_id, &tag, &status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (tag != dev->tag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) dev_err(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) "%s:%u: tag mismatch, got %llx, expected %llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) __func__, __LINE__, tag, dev->tag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) dev_err(&dev->sbd.core, "%s:%u: res=%d status=0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) __func__, __LINE__, res, status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) dev->lv1_status = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) complete(&dev->done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static const struct file_operations ps3flash_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) .llseek = ps3flash_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) .read = ps3flash_user_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) .write = ps3flash_user_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) .flush = ps3flash_flush,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) .fsync = ps3flash_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static const struct ps3_os_area_flash_ops ps3flash_kernel_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) .read = ps3flash_kernel_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) .write = ps3flash_kernel_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static struct miscdevice ps3flash_misc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) .minor = MISC_DYNAMIC_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) .name = DEVICE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) .fops = &ps3flash_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static int ps3flash_probe(struct ps3_system_bus_device *_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct ps3flash_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) unsigned long tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) tmp = dev->regions[dev->region_idx].start*dev->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (tmp % FLASH_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) dev_err(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) "%s:%u region start %lu is not aligned\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) __LINE__, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) tmp = dev->regions[dev->region_idx].size*dev->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (tmp % FLASH_BLOCK_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) dev_err(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) "%s:%u region size %lu is not aligned\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) __LINE__, tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* use static buffer, kmalloc cannot allocate 256 KiB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) if (!ps3flash_bounce_buffer.address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) if (ps3flash_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) dev_err(&dev->sbd.core,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) "Only one FLASH device is supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ps3flash_dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) priv = kzalloc(sizeof(*priv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (!priv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) ps3_system_bus_set_drvdata(&dev->sbd, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) mutex_init(&priv->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) priv->tag = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) dev->bounce_size = ps3flash_bounce_buffer.size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) dev->bounce_buf = ps3flash_bounce_buffer.address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) priv->chunk_sectors = dev->bounce_size / dev->blk_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) error = ps3stor_setup(dev, ps3flash_interrupt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) goto fail_free_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) ps3flash_misc.parent = &dev->sbd.core;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) error = misc_register(&ps3flash_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) dev_err(&dev->sbd.core, "%s:%u: misc_register failed %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) __func__, __LINE__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) goto fail_teardown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) dev_info(&dev->sbd.core, "%s:%u: registered misc device %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) __func__, __LINE__, ps3flash_misc.minor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) ps3_os_area_flash_register(&ps3flash_kernel_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) fail_teardown:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) ps3stor_teardown(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) fail_free_priv:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) ps3_system_bus_set_drvdata(&dev->sbd, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) ps3flash_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) static int ps3flash_remove(struct ps3_system_bus_device *_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) struct ps3_storage_device *dev = to_ps3_storage_device(&_dev->core);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) ps3_os_area_flash_register(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) misc_deregister(&ps3flash_misc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ps3stor_teardown(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) kfree(ps3_system_bus_get_drvdata(&dev->sbd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) ps3_system_bus_set_drvdata(&dev->sbd, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) ps3flash_dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) static struct ps3_system_bus_driver ps3flash = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) .match_id = PS3_MATCH_ID_STOR_FLASH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) .core.name = DEVICE_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) .core.owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) .probe = ps3flash_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) .remove = ps3flash_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) .shutdown = ps3flash_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static int __init ps3flash_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) return ps3_system_bus_driver_register(&ps3flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) static void __exit ps3flash_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) ps3_system_bus_driver_unregister(&ps3flash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) module_init(ps3flash_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) module_exit(ps3flash_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) MODULE_DESCRIPTION("PS3 FLASH ROM Storage Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) MODULE_AUTHOR("Sony Corporation");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) MODULE_ALIAS(PS3_MODULE_ALIAS_STOR_FLASH);