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