Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 1 | /* |
| 2 | * PS3 Storage Library |
| 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/dma-mapping.h> |
| 22 | |
| 23 | #include <asm/lv1call.h> |
| 24 | #include <asm/ps3stor.h> |
| 25 | |
Geoff Levand | bc00351 | 2009-09-09 13:28:05 +0000 | [diff] [blame] | 26 | /* |
| 27 | * A workaround for flash memory I/O errors when the internal hard disk |
| 28 | * has not been formatted for OtherOS use. Delay disk close until flash |
| 29 | * memory is closed. |
| 30 | */ |
| 31 | |
| 32 | static struct ps3_flash_workaround { |
| 33 | int flash_open; |
| 34 | int disk_open; |
| 35 | struct ps3_system_bus_device *disk_sbd; |
| 36 | } ps3_flash_workaround; |
| 37 | |
| 38 | static int ps3stor_open_hv_device(struct ps3_system_bus_device *sbd) |
| 39 | { |
| 40 | int error = ps3_open_hv_device(sbd); |
| 41 | |
| 42 | if (error) |
| 43 | return error; |
| 44 | |
| 45 | if (sbd->match_id == PS3_MATCH_ID_STOR_FLASH) |
| 46 | ps3_flash_workaround.flash_open = 1; |
| 47 | |
| 48 | if (sbd->match_id == PS3_MATCH_ID_STOR_DISK) |
| 49 | ps3_flash_workaround.disk_open = 1; |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | static int ps3stor_close_hv_device(struct ps3_system_bus_device *sbd) |
| 55 | { |
| 56 | int error; |
| 57 | |
| 58 | if (sbd->match_id == PS3_MATCH_ID_STOR_DISK |
| 59 | && ps3_flash_workaround.disk_open |
| 60 | && ps3_flash_workaround.flash_open) { |
| 61 | ps3_flash_workaround.disk_sbd = sbd; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | error = ps3_close_hv_device(sbd); |
| 66 | |
| 67 | if (error) |
| 68 | return error; |
| 69 | |
| 70 | if (sbd->match_id == PS3_MATCH_ID_STOR_DISK) |
| 71 | ps3_flash_workaround.disk_open = 0; |
| 72 | |
| 73 | if (sbd->match_id == PS3_MATCH_ID_STOR_FLASH) { |
| 74 | ps3_flash_workaround.flash_open = 0; |
| 75 | |
| 76 | if (ps3_flash_workaround.disk_sbd) { |
| 77 | ps3_close_hv_device(ps3_flash_workaround.disk_sbd); |
| 78 | ps3_flash_workaround.disk_open = 0; |
| 79 | ps3_flash_workaround.disk_sbd = NULL; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 85 | |
| 86 | static int ps3stor_probe_access(struct ps3_storage_device *dev) |
| 87 | { |
| 88 | int res, error; |
| 89 | unsigned int i; |
| 90 | unsigned long n; |
| 91 | |
| 92 | if (dev->sbd.match_id == PS3_MATCH_ID_STOR_ROM) { |
| 93 | /* special case: CD-ROM is assumed always accessible */ |
| 94 | dev->accessible_regions = 1; |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | error = -EPERM; |
| 99 | for (i = 0; i < dev->num_regions; i++) { |
| 100 | dev_dbg(&dev->sbd.core, |
| 101 | "%s:%u: checking accessibility of region %u\n", |
| 102 | __func__, __LINE__, i); |
| 103 | |
| 104 | dev->region_idx = i; |
| 105 | res = ps3stor_read_write_sectors(dev, dev->bounce_lpar, 0, 1, |
| 106 | 0); |
| 107 | if (res) { |
| 108 | dev_dbg(&dev->sbd.core, "%s:%u: read failed, " |
| 109 | "region %u is not accessible\n", __func__, |
| 110 | __LINE__, i); |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | dev_dbg(&dev->sbd.core, "%s:%u: region %u is accessible\n", |
| 115 | __func__, __LINE__, i); |
| 116 | set_bit(i, &dev->accessible_regions); |
| 117 | |
| 118 | /* We can access at least one region */ |
| 119 | error = 0; |
| 120 | } |
| 121 | if (error) |
| 122 | return error; |
| 123 | |
| 124 | n = hweight_long(dev->accessible_regions); |
| 125 | if (n > 1) |
| 126 | dev_info(&dev->sbd.core, |
| 127 | "%s:%u: %lu accessible regions found. Only the first " |
Joe Perches | 898eb71 | 2007-10-18 03:06:30 -0700 | [diff] [blame] | 128 | "one will be used\n", |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 129 | __func__, __LINE__, n); |
| 130 | dev->region_idx = __ffs(dev->accessible_regions); |
| 131 | dev_info(&dev->sbd.core, |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 132 | "First accessible region has index %u start %llu size %llu\n", |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 133 | dev->region_idx, dev->regions[dev->region_idx].start, |
| 134 | dev->regions[dev->region_idx].size); |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | /** |
| 141 | * ps3stor_setup - Setup a storage device before use |
| 142 | * @dev: Pointer to a struct ps3_storage_device |
| 143 | * @handler: Pointer to an interrupt handler |
| 144 | * |
| 145 | * Returns 0 for success, or an error code |
| 146 | */ |
| 147 | int ps3stor_setup(struct ps3_storage_device *dev, irq_handler_t handler) |
| 148 | { |
| 149 | int error, res, alignment; |
| 150 | enum ps3_dma_page_size page_size; |
| 151 | |
Geoff Levand | bc00351 | 2009-09-09 13:28:05 +0000 | [diff] [blame] | 152 | error = ps3stor_open_hv_device(&dev->sbd); |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 153 | if (error) { |
| 154 | dev_err(&dev->sbd.core, |
| 155 | "%s:%u: ps3_open_hv_device failed %d\n", __func__, |
| 156 | __LINE__, error); |
| 157 | goto fail; |
| 158 | } |
| 159 | |
| 160 | error = ps3_sb_event_receive_port_setup(&dev->sbd, PS3_BINDING_CPU_ANY, |
| 161 | &dev->irq); |
| 162 | if (error) { |
| 163 | dev_err(&dev->sbd.core, |
| 164 | "%s:%u: ps3_sb_event_receive_port_setup failed %d\n", |
| 165 | __func__, __LINE__, error); |
| 166 | goto fail_close_device; |
| 167 | } |
| 168 | |
| 169 | error = request_irq(dev->irq, handler, IRQF_DISABLED, |
| 170 | dev->sbd.core.driver->name, dev); |
| 171 | if (error) { |
| 172 | dev_err(&dev->sbd.core, "%s:%u: request_irq failed %d\n", |
| 173 | __func__, __LINE__, error); |
| 174 | goto fail_sb_event_receive_port_destroy; |
| 175 | } |
| 176 | |
| 177 | alignment = min(__ffs(dev->bounce_size), |
| 178 | __ffs((unsigned long)dev->bounce_buf)); |
| 179 | if (alignment < 12) { |
| 180 | dev_err(&dev->sbd.core, |
| 181 | "%s:%u: bounce buffer not aligned (%lx at 0x%p)\n", |
| 182 | __func__, __LINE__, dev->bounce_size, dev->bounce_buf); |
| 183 | error = -EINVAL; |
| 184 | goto fail_free_irq; |
| 185 | } else if (alignment < 16) |
| 186 | page_size = PS3_DMA_4K; |
| 187 | else |
| 188 | page_size = PS3_DMA_64K; |
| 189 | dev->sbd.d_region = &dev->dma_region; |
| 190 | ps3_dma_region_init(&dev->sbd, &dev->dma_region, page_size, |
| 191 | PS3_DMA_OTHER, dev->bounce_buf, dev->bounce_size); |
| 192 | res = ps3_dma_region_create(&dev->dma_region); |
| 193 | if (res) { |
| 194 | dev_err(&dev->sbd.core, "%s:%u: cannot create DMA region\n", |
| 195 | __func__, __LINE__); |
| 196 | error = -ENOMEM; |
| 197 | goto fail_free_irq; |
| 198 | } |
| 199 | |
| 200 | dev->bounce_lpar = ps3_mm_phys_to_lpar(__pa(dev->bounce_buf)); |
| 201 | dev->bounce_dma = dma_map_single(&dev->sbd.core, dev->bounce_buf, |
| 202 | dev->bounce_size, DMA_BIDIRECTIONAL); |
| 203 | if (!dev->bounce_dma) { |
| 204 | dev_err(&dev->sbd.core, "%s:%u: map DMA region failed\n", |
| 205 | __func__, __LINE__); |
| 206 | error = -ENODEV; |
| 207 | goto fail_free_dma; |
| 208 | } |
| 209 | |
| 210 | error = ps3stor_probe_access(dev); |
| 211 | if (error) { |
| 212 | dev_err(&dev->sbd.core, "%s:%u: No accessible regions found\n", |
| 213 | __func__, __LINE__); |
| 214 | goto fail_unmap_dma; |
| 215 | } |
| 216 | return 0; |
| 217 | |
| 218 | fail_unmap_dma: |
| 219 | dma_unmap_single(&dev->sbd.core, dev->bounce_dma, dev->bounce_size, |
| 220 | DMA_BIDIRECTIONAL); |
| 221 | fail_free_dma: |
| 222 | ps3_dma_region_free(&dev->dma_region); |
| 223 | fail_free_irq: |
| 224 | free_irq(dev->irq, dev); |
| 225 | fail_sb_event_receive_port_destroy: |
| 226 | ps3_sb_event_receive_port_destroy(&dev->sbd, dev->irq); |
| 227 | fail_close_device: |
Geoff Levand | bc00351 | 2009-09-09 13:28:05 +0000 | [diff] [blame] | 228 | ps3stor_close_hv_device(&dev->sbd); |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 229 | fail: |
| 230 | return error; |
| 231 | } |
| 232 | EXPORT_SYMBOL_GPL(ps3stor_setup); |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * ps3stor_teardown - Tear down a storage device after use |
| 237 | * @dev: Pointer to a struct ps3_storage_device |
| 238 | */ |
| 239 | void ps3stor_teardown(struct ps3_storage_device *dev) |
| 240 | { |
| 241 | int error; |
| 242 | |
| 243 | dma_unmap_single(&dev->sbd.core, dev->bounce_dma, dev->bounce_size, |
| 244 | DMA_BIDIRECTIONAL); |
| 245 | ps3_dma_region_free(&dev->dma_region); |
| 246 | |
| 247 | free_irq(dev->irq, dev); |
| 248 | |
| 249 | error = ps3_sb_event_receive_port_destroy(&dev->sbd, dev->irq); |
| 250 | if (error) |
| 251 | dev_err(&dev->sbd.core, |
| 252 | "%s:%u: destroy event receive port failed %d\n", |
| 253 | __func__, __LINE__, error); |
| 254 | |
Geoff Levand | bc00351 | 2009-09-09 13:28:05 +0000 | [diff] [blame] | 255 | error = ps3stor_close_hv_device(&dev->sbd); |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 256 | if (error) |
| 257 | dev_err(&dev->sbd.core, |
| 258 | "%s:%u: ps3_close_hv_device failed %d\n", __func__, |
| 259 | __LINE__, error); |
| 260 | } |
| 261 | EXPORT_SYMBOL_GPL(ps3stor_teardown); |
| 262 | |
| 263 | |
| 264 | /** |
| 265 | * ps3stor_read_write_sectors - read/write from/to a storage device |
| 266 | * @dev: Pointer to a struct ps3_storage_device |
| 267 | * @lpar: HV logical partition address |
| 268 | * @start_sector: First sector to read/write |
| 269 | * @sectors: Number of sectors to read/write |
| 270 | * @write: Flag indicating write (non-zero) or read (zero) |
| 271 | * |
| 272 | * Returns 0 for success, -1 in case of failure to submit the command, or |
| 273 | * an LV1 status value in case of other errors |
| 274 | */ |
| 275 | u64 ps3stor_read_write_sectors(struct ps3_storage_device *dev, u64 lpar, |
| 276 | u64 start_sector, u64 sectors, int write) |
| 277 | { |
| 278 | unsigned int region_id = dev->regions[dev->region_idx].id; |
| 279 | const char *op = write ? "write" : "read"; |
| 280 | int res; |
| 281 | |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 282 | dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n", |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 283 | __func__, __LINE__, op, sectors, start_sector); |
| 284 | |
| 285 | init_completion(&dev->done); |
| 286 | res = write ? lv1_storage_write(dev->sbd.dev_id, region_id, |
| 287 | start_sector, sectors, 0, lpar, |
| 288 | &dev->tag) |
| 289 | : lv1_storage_read(dev->sbd.dev_id, region_id, |
| 290 | start_sector, sectors, 0, lpar, |
| 291 | &dev->tag); |
| 292 | if (res) { |
| 293 | dev_dbg(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__, |
| 294 | __LINE__, op, res); |
| 295 | return -1; |
| 296 | } |
| 297 | |
| 298 | wait_for_completion(&dev->done); |
| 299 | if (dev->lv1_status) { |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 300 | dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__, |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 301 | __LINE__, op, dev->lv1_status); |
| 302 | return dev->lv1_status; |
| 303 | } |
| 304 | |
| 305 | dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__, __LINE__, |
| 306 | op); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | EXPORT_SYMBOL_GPL(ps3stor_read_write_sectors); |
| 311 | |
| 312 | |
| 313 | /** |
| 314 | * ps3stor_send_command - send a device command to a storage device |
| 315 | * @dev: Pointer to a struct ps3_storage_device |
| 316 | * @cmd: Command number |
| 317 | * @arg1: First command argument |
| 318 | * @arg2: Second command argument |
| 319 | * @arg3: Third command argument |
| 320 | * @arg4: Fourth command argument |
| 321 | * |
| 322 | * Returns 0 for success, -1 in case of failure to submit the command, or |
| 323 | * an LV1 status value in case of other errors |
| 324 | */ |
| 325 | u64 ps3stor_send_command(struct ps3_storage_device *dev, u64 cmd, u64 arg1, |
| 326 | u64 arg2, u64 arg3, u64 arg4) |
| 327 | { |
| 328 | int res; |
| 329 | |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 330 | dev_dbg(&dev->sbd.core, "%s:%u: send device command 0x%llx\n", __func__, |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 331 | __LINE__, cmd); |
| 332 | |
| 333 | init_completion(&dev->done); |
| 334 | |
| 335 | res = lv1_storage_send_device_command(dev->sbd.dev_id, cmd, arg1, |
| 336 | arg2, arg3, arg4, &dev->tag); |
| 337 | if (res) { |
| 338 | dev_err(&dev->sbd.core, |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 339 | "%s:%u: send_device_command 0x%llx failed %d\n", |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 340 | __func__, __LINE__, cmd, res); |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | wait_for_completion(&dev->done); |
| 345 | if (dev->lv1_status) { |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 346 | dev_dbg(&dev->sbd.core, "%s:%u: command 0x%llx failed 0x%llx\n", |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 347 | __func__, __LINE__, cmd, dev->lv1_status); |
| 348 | return dev->lv1_status; |
| 349 | } |
| 350 | |
Stephen Rothwell | a9dad6e | 2009-01-13 20:10:06 +0000 | [diff] [blame] | 351 | dev_dbg(&dev->sbd.core, "%s:%u: command 0x%llx completed\n", __func__, |
Geert Uytterhoeven | 8007180 | 2007-06-22 00:14:21 +1000 | [diff] [blame] | 352 | __LINE__, cmd); |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | EXPORT_SYMBOL_GPL(ps3stor_send_command); |
| 357 | |
| 358 | |
| 359 | MODULE_LICENSE("GPL"); |
| 360 | MODULE_DESCRIPTION("PS3 Storage Bus Library"); |
| 361 | MODULE_AUTHOR("Sony Corporation"); |