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