1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2007 Sony Computer Entertainment Inc.
6 * Copyright 2007 Sony Corp.
9 #include <linux/dma-mapping.h>
10 #include <linux/module.h>
12 #include <asm/lv1call.h>
13 #include <asm/ps3stor.h>
16 * A workaround for flash memory I/O errors when the internal hard disk
17 * has not been formatted for OtherOS use. Delay disk close until flash
21 static struct ps3_flash_workaround {
24 struct ps3_system_bus_device *disk_sbd;
25 } ps3_flash_workaround;
27 static int ps3stor_open_hv_device(struct ps3_system_bus_device *sbd)
29 int error = ps3_open_hv_device(sbd);
34 if (sbd->match_id == PS3_MATCH_ID_STOR_FLASH)
35 ps3_flash_workaround.flash_open = 1;
37 if (sbd->match_id == PS3_MATCH_ID_STOR_DISK)
38 ps3_flash_workaround.disk_open = 1;
43 static int ps3stor_close_hv_device(struct ps3_system_bus_device *sbd)
47 if (sbd->match_id == PS3_MATCH_ID_STOR_DISK
48 && ps3_flash_workaround.disk_open
49 && ps3_flash_workaround.flash_open) {
50 ps3_flash_workaround.disk_sbd = sbd;
54 error = ps3_close_hv_device(sbd);
59 if (sbd->match_id == PS3_MATCH_ID_STOR_DISK)
60 ps3_flash_workaround.disk_open = 0;
62 if (sbd->match_id == PS3_MATCH_ID_STOR_FLASH) {
63 ps3_flash_workaround.flash_open = 0;
65 if (ps3_flash_workaround.disk_sbd) {
66 ps3_close_hv_device(ps3_flash_workaround.disk_sbd);
67 ps3_flash_workaround.disk_open = 0;
68 ps3_flash_workaround.disk_sbd = NULL;
75 static int ps3stor_probe_access(struct ps3_storage_device *dev)
81 if (dev->sbd.match_id == PS3_MATCH_ID_STOR_ROM) {
82 /* special case: CD-ROM is assumed always accessible */
83 dev->accessible_regions = 1;
88 for (i = 0; i < dev->num_regions; i++) {
89 dev_dbg(&dev->sbd.core,
90 "%s:%u: checking accessibility of region %u\n",
91 __func__, __LINE__, i);
94 res = ps3stor_read_write_sectors(dev, dev->bounce_lpar, 0, 1,
97 dev_dbg(&dev->sbd.core, "%s:%u: read failed, "
98 "region %u is not accessible\n", __func__,
103 dev_dbg(&dev->sbd.core, "%s:%u: region %u is accessible\n",
104 __func__, __LINE__, i);
105 set_bit(i, &dev->accessible_regions);
107 /* We can access at least one region */
113 n = hweight_long(dev->accessible_regions);
115 dev_info(&dev->sbd.core,
116 "%s:%u: %lu accessible regions found. Only the first "
117 "one will be used\n",
118 __func__, __LINE__, n);
119 dev->region_idx = __ffs(dev->accessible_regions);
120 dev_info(&dev->sbd.core,
121 "First accessible region has index %u start %llu size %llu\n",
122 dev->region_idx, dev->regions[dev->region_idx].start,
123 dev->regions[dev->region_idx].size);
130 * ps3stor_setup - Setup a storage device before use
131 * @dev: Pointer to a struct ps3_storage_device
132 * @handler: Pointer to an interrupt handler
134 * Returns 0 for success, or an error code
136 int ps3stor_setup(struct ps3_storage_device *dev, irq_handler_t handler)
138 int error, res, alignment;
139 enum ps3_dma_page_size page_size;
141 error = ps3stor_open_hv_device(&dev->sbd);
143 dev_err(&dev->sbd.core,
144 "%s:%u: ps3_open_hv_device failed %d\n", __func__,
149 error = ps3_sb_event_receive_port_setup(&dev->sbd, PS3_BINDING_CPU_ANY,
152 dev_err(&dev->sbd.core,
153 "%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
154 __func__, __LINE__, error);
155 goto fail_close_device;
158 error = request_irq(dev->irq, handler, 0,
159 dev->sbd.core.driver->name, dev);
161 dev_err(&dev->sbd.core, "%s:%u: request_irq failed %d\n",
162 __func__, __LINE__, error);
163 goto fail_sb_event_receive_port_destroy;
166 alignment = min(__ffs(dev->bounce_size),
167 __ffs((unsigned long)dev->bounce_buf));
168 if (alignment < 12) {
169 dev_err(&dev->sbd.core,
170 "%s:%u: bounce buffer not aligned (%lx at 0x%p)\n",
171 __func__, __LINE__, dev->bounce_size, dev->bounce_buf);
174 } else if (alignment < 16)
175 page_size = PS3_DMA_4K;
177 page_size = PS3_DMA_64K;
178 dev->sbd.d_region = &dev->dma_region;
179 ps3_dma_region_init(&dev->sbd, &dev->dma_region, page_size,
180 PS3_DMA_OTHER, dev->bounce_buf, dev->bounce_size);
181 res = ps3_dma_region_create(&dev->dma_region);
183 dev_err(&dev->sbd.core, "%s:%u: cannot create DMA region\n",
189 dev->bounce_lpar = ps3_mm_phys_to_lpar(__pa(dev->bounce_buf));
190 dev->bounce_dma = dma_map_single(&dev->sbd.core, dev->bounce_buf,
191 dev->bounce_size, DMA_BIDIRECTIONAL);
192 if (dma_mapping_error(&dev->sbd.core, dev->bounce_dma)) {
193 dev_err(&dev->sbd.core, "%s:%u: map DMA region failed\n",
199 error = ps3stor_probe_access(dev);
201 dev_err(&dev->sbd.core, "%s:%u: No accessible regions found\n",
208 dma_unmap_single(&dev->sbd.core, dev->bounce_dma, dev->bounce_size,
211 ps3_dma_region_free(&dev->dma_region);
213 free_irq(dev->irq, dev);
214 fail_sb_event_receive_port_destroy:
215 ps3_sb_event_receive_port_destroy(&dev->sbd, dev->irq);
217 ps3stor_close_hv_device(&dev->sbd);
221 EXPORT_SYMBOL_GPL(ps3stor_setup);
225 * ps3stor_teardown - Tear down a storage device after use
226 * @dev: Pointer to a struct ps3_storage_device
228 void ps3stor_teardown(struct ps3_storage_device *dev)
232 dma_unmap_single(&dev->sbd.core, dev->bounce_dma, dev->bounce_size,
234 ps3_dma_region_free(&dev->dma_region);
236 free_irq(dev->irq, dev);
238 error = ps3_sb_event_receive_port_destroy(&dev->sbd, dev->irq);
240 dev_err(&dev->sbd.core,
241 "%s:%u: destroy event receive port failed %d\n",
242 __func__, __LINE__, error);
244 error = ps3stor_close_hv_device(&dev->sbd);
246 dev_err(&dev->sbd.core,
247 "%s:%u: ps3_close_hv_device failed %d\n", __func__,
250 EXPORT_SYMBOL_GPL(ps3stor_teardown);
254 * ps3stor_read_write_sectors - read/write from/to a storage device
255 * @dev: Pointer to a struct ps3_storage_device
256 * @lpar: HV logical partition address
257 * @start_sector: First sector to read/write
258 * @sectors: Number of sectors to read/write
259 * @write: Flag indicating write (non-zero) or read (zero)
261 * Returns 0 for success, -1 in case of failure to submit the command, or
262 * an LV1 status value in case of other errors
264 u64 ps3stor_read_write_sectors(struct ps3_storage_device *dev, u64 lpar,
265 u64 start_sector, u64 sectors, int write)
267 unsigned int region_id = dev->regions[dev->region_idx].id;
268 const char *op = write ? "write" : "read";
271 dev_dbg(&dev->sbd.core, "%s:%u: %s %llu sectors starting at %llu\n",
272 __func__, __LINE__, op, sectors, start_sector);
274 init_completion(&dev->done);
275 res = write ? lv1_storage_write(dev->sbd.dev_id, region_id,
276 start_sector, sectors, 0, lpar,
278 : lv1_storage_read(dev->sbd.dev_id, region_id,
279 start_sector, sectors, 0, lpar,
282 dev_dbg(&dev->sbd.core, "%s:%u: %s failed %d\n", __func__,
287 wait_for_completion(&dev->done);
288 if (dev->lv1_status) {
289 dev_dbg(&dev->sbd.core, "%s:%u: %s failed 0x%llx\n", __func__,
290 __LINE__, op, dev->lv1_status);
291 return dev->lv1_status;
294 dev_dbg(&dev->sbd.core, "%s:%u: %s completed\n", __func__, __LINE__,
299 EXPORT_SYMBOL_GPL(ps3stor_read_write_sectors);
303 * ps3stor_send_command - send a device command to a storage device
304 * @dev: Pointer to a struct ps3_storage_device
305 * @cmd: Command number
306 * @arg1: First command argument
307 * @arg2: Second command argument
308 * @arg3: Third command argument
309 * @arg4: Fourth command argument
311 * Returns 0 for success, -1 in case of failure to submit the command, or
312 * an LV1 status value in case of other errors
314 u64 ps3stor_send_command(struct ps3_storage_device *dev, u64 cmd, u64 arg1,
315 u64 arg2, u64 arg3, u64 arg4)
319 dev_dbg(&dev->sbd.core, "%s:%u: send device command 0x%llx\n", __func__,
322 init_completion(&dev->done);
324 res = lv1_storage_send_device_command(dev->sbd.dev_id, cmd, arg1,
325 arg2, arg3, arg4, &dev->tag);
327 dev_err(&dev->sbd.core,
328 "%s:%u: send_device_command 0x%llx failed %d\n",
329 __func__, __LINE__, cmd, res);
333 wait_for_completion(&dev->done);
334 if (dev->lv1_status) {
335 dev_dbg(&dev->sbd.core, "%s:%u: command 0x%llx failed 0x%llx\n",
336 __func__, __LINE__, cmd, dev->lv1_status);
337 return dev->lv1_status;
340 dev_dbg(&dev->sbd.core, "%s:%u: command 0x%llx completed\n", __func__,
345 EXPORT_SYMBOL_GPL(ps3stor_send_command);
348 MODULE_LICENSE("GPL");
349 MODULE_DESCRIPTION("PS3 Storage Bus Library");
350 MODULE_AUTHOR("Sony Corporation");