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