Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter, MPL AG Switzerland |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 10 | #include <inttypes.h> |
| 11 | #include <pci.h> |
| 12 | #include <scsi.h> |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/uclass-internal.h> |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 15 | |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 16 | #if !defined(CONFIG_DM_SCSI) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 17 | #ifdef CONFIG_SCSI_DEV_LIST |
| 18 | #define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST |
| 19 | #else |
Simon Glass | b8beb6b | 2017-06-14 21:28:22 -0600 | [diff] [blame^] | 20 | #ifdef CONFIG_SATA_ULI5288 |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 21 | |
| 22 | #define SCSI_VEND_ID 0x10b9 |
| 23 | #define SCSI_DEV_ID 0x5288 |
| 24 | |
| 25 | #elif !defined(CONFIG_SCSI_AHCI_PLAT) |
| 26 | #error no scsi device defined |
| 27 | #endif |
| 28 | #define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID} |
| 29 | #endif |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 30 | #endif |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 31 | |
| 32 | #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) |
| 33 | const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST }; |
| 34 | #endif |
| 35 | static ccb tempccb; /* temporary scsi command buffer */ |
| 36 | |
| 37 | static unsigned char tempbuff[512]; /* temporary data buffer */ |
| 38 | |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 39 | #if !defined(CONFIG_DM_SCSI) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 40 | static int scsi_max_devs; /* number of highest available scsi device */ |
| 41 | |
| 42 | static int scsi_curr_dev; /* current device */ |
| 43 | |
| 44 | static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE]; |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 45 | #endif |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 46 | |
| 47 | /* almost the maximum amount of the scsi_ext command.. */ |
| 48 | #define SCSI_MAX_READ_BLK 0xFFFF |
| 49 | #define SCSI_LBA48_READ 0xFFFFFFF |
| 50 | |
| 51 | #ifdef CONFIG_SYS_64BIT_LBA |
| 52 | void scsi_setup_read16(ccb *pccb, lbaint_t start, unsigned long blocks) |
| 53 | { |
| 54 | pccb->cmd[0] = SCSI_READ16; |
| 55 | pccb->cmd[1] = pccb->lun << 5; |
| 56 | pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff; |
| 57 | pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff; |
| 58 | pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff; |
| 59 | pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff; |
| 60 | pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff; |
| 61 | pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff; |
| 62 | pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff; |
| 63 | pccb->cmd[9] = (unsigned char)start & 0xff; |
| 64 | pccb->cmd[10] = 0; |
| 65 | pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff; |
| 66 | pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff; |
| 67 | pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff; |
| 68 | pccb->cmd[14] = (unsigned char)blocks & 0xff; |
| 69 | pccb->cmd[15] = 0; |
| 70 | pccb->cmdlen = 16; |
| 71 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 72 | debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n", |
| 73 | pccb->cmd[0], pccb->cmd[1], |
| 74 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 75 | pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9], |
| 76 | pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]); |
| 77 | } |
| 78 | #endif |
| 79 | |
Michal Simek | 545a284 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 80 | static void scsi_setup_read_ext(ccb *pccb, lbaint_t start, |
| 81 | unsigned short blocks) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 82 | { |
| 83 | pccb->cmd[0] = SCSI_READ10; |
| 84 | pccb->cmd[1] = pccb->lun << 5; |
| 85 | pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff; |
| 86 | pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff; |
| 87 | pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff; |
| 88 | pccb->cmd[5] = (unsigned char)start & 0xff; |
| 89 | pccb->cmd[6] = 0; |
| 90 | pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff; |
| 91 | pccb->cmd[8] = (unsigned char)blocks & 0xff; |
| 92 | pccb->cmd[6] = 0; |
| 93 | pccb->cmdlen = 10; |
| 94 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 95 | debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
| 96 | pccb->cmd[0], pccb->cmd[1], |
| 97 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 98 | pccb->cmd[7], pccb->cmd[8]); |
| 99 | } |
| 100 | |
Michal Simek | 545a284 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 101 | static void scsi_setup_write_ext(ccb *pccb, lbaint_t start, |
| 102 | unsigned short blocks) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 103 | { |
| 104 | pccb->cmd[0] = SCSI_WRITE10; |
| 105 | pccb->cmd[1] = pccb->lun << 5; |
| 106 | pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff; |
| 107 | pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff; |
| 108 | pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff; |
| 109 | pccb->cmd[5] = (unsigned char)start & 0xff; |
| 110 | pccb->cmd[6] = 0; |
| 111 | pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff; |
| 112 | pccb->cmd[8] = (unsigned char)blocks & 0xff; |
| 113 | pccb->cmd[9] = 0; |
| 114 | pccb->cmdlen = 10; |
| 115 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 116 | debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
| 117 | __func__, |
| 118 | pccb->cmd[0], pccb->cmd[1], |
| 119 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 120 | pccb->cmd[7], pccb->cmd[8]); |
| 121 | } |
| 122 | |
Michal Simek | 545a284 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 123 | static void scsi_setup_inquiry(ccb *pccb) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 124 | { |
| 125 | pccb->cmd[0] = SCSI_INQUIRY; |
| 126 | pccb->cmd[1] = pccb->lun << 5; |
| 127 | pccb->cmd[2] = 0; |
| 128 | pccb->cmd[3] = 0; |
| 129 | if (pccb->datalen > 255) |
| 130 | pccb->cmd[4] = 255; |
| 131 | else |
| 132 | pccb->cmd[4] = (unsigned char)pccb->datalen; |
| 133 | pccb->cmd[5] = 0; |
| 134 | pccb->cmdlen = 6; |
| 135 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 136 | } |
| 137 | |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 138 | #ifdef CONFIG_BLK |
| 139 | static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 140 | void *buffer) |
| 141 | #else |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 142 | static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr, |
| 143 | lbaint_t blkcnt, void *buffer) |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 144 | #endif |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 145 | { |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 146 | #ifdef CONFIG_BLK |
| 147 | struct blk_desc *block_dev = dev_get_uclass_platdata(dev); |
| 148 | #endif |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 149 | lbaint_t start, blks; |
| 150 | uintptr_t buf_addr; |
| 151 | unsigned short smallblks = 0; |
| 152 | ccb *pccb = (ccb *)&tempccb; |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 153 | |
| 154 | /* Setup device */ |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 155 | pccb->target = block_dev->target; |
| 156 | pccb->lun = block_dev->lun; |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 157 | buf_addr = (unsigned long)buffer; |
| 158 | start = blknr; |
| 159 | blks = blkcnt; |
| 160 | debug("\nscsi_read: dev %d startblk " LBAF |
| 161 | ", blccnt " LBAF " buffer %lx\n", |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 162 | block_dev->devnum, start, blks, (unsigned long)buffer); |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 163 | do { |
| 164 | pccb->pdata = (unsigned char *)buf_addr; |
| 165 | #ifdef CONFIG_SYS_64BIT_LBA |
| 166 | if (start > SCSI_LBA48_READ) { |
| 167 | unsigned long blocks; |
| 168 | blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK); |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 169 | pccb->datalen = block_dev->blksz * blocks; |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 170 | scsi_setup_read16(pccb, start, blocks); |
| 171 | start += blocks; |
| 172 | blks -= blocks; |
| 173 | } else |
| 174 | #endif |
| 175 | if (blks > SCSI_MAX_READ_BLK) { |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 176 | pccb->datalen = block_dev->blksz * |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 177 | SCSI_MAX_READ_BLK; |
| 178 | smallblks = SCSI_MAX_READ_BLK; |
| 179 | scsi_setup_read_ext(pccb, start, smallblks); |
| 180 | start += SCSI_MAX_READ_BLK; |
| 181 | blks -= SCSI_MAX_READ_BLK; |
| 182 | } else { |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 183 | pccb->datalen = block_dev->blksz * blks; |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 184 | smallblks = (unsigned short)blks; |
| 185 | scsi_setup_read_ext(pccb, start, smallblks); |
| 186 | start += blks; |
| 187 | blks = 0; |
| 188 | } |
| 189 | debug("scsi_read_ext: startblk " LBAF |
| 190 | ", blccnt %x buffer %" PRIXPTR "\n", |
| 191 | start, smallblks, buf_addr); |
| 192 | if (scsi_exec(pccb) != true) { |
| 193 | scsi_print_error(pccb); |
| 194 | blkcnt -= blks; |
| 195 | break; |
| 196 | } |
| 197 | buf_addr += pccb->datalen; |
| 198 | } while (blks != 0); |
| 199 | debug("scsi_read_ext: end startblk " LBAF |
| 200 | ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr); |
| 201 | return blkcnt; |
| 202 | } |
| 203 | |
| 204 | /******************************************************************************* |
| 205 | * scsi_write |
| 206 | */ |
| 207 | |
| 208 | /* Almost the maximum amount of the scsi_ext command.. */ |
| 209 | #define SCSI_MAX_WRITE_BLK 0xFFFF |
| 210 | |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 211 | #ifdef CONFIG_BLK |
| 212 | static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 213 | const void *buffer) |
| 214 | #else |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 215 | static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr, |
| 216 | lbaint_t blkcnt, const void *buffer) |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 217 | #endif |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 218 | { |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 219 | #ifdef CONFIG_BLK |
| 220 | struct blk_desc *block_dev = dev_get_uclass_platdata(dev); |
| 221 | #endif |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 222 | lbaint_t start, blks; |
| 223 | uintptr_t buf_addr; |
| 224 | unsigned short smallblks; |
| 225 | ccb *pccb = (ccb *)&tempccb; |
| 226 | |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 227 | /* Setup device */ |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 228 | pccb->target = block_dev->target; |
| 229 | pccb->lun = block_dev->lun; |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 230 | buf_addr = (unsigned long)buffer; |
| 231 | start = blknr; |
| 232 | blks = blkcnt; |
| 233 | debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 234 | __func__, block_dev->devnum, start, blks, (unsigned long)buffer); |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 235 | do { |
| 236 | pccb->pdata = (unsigned char *)buf_addr; |
| 237 | if (blks > SCSI_MAX_WRITE_BLK) { |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 238 | pccb->datalen = (block_dev->blksz * |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 239 | SCSI_MAX_WRITE_BLK); |
| 240 | smallblks = SCSI_MAX_WRITE_BLK; |
| 241 | scsi_setup_write_ext(pccb, start, smallblks); |
| 242 | start += SCSI_MAX_WRITE_BLK; |
| 243 | blks -= SCSI_MAX_WRITE_BLK; |
| 244 | } else { |
Michal Simek | cdb93b2 | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 245 | pccb->datalen = block_dev->blksz * blks; |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 246 | smallblks = (unsigned short)blks; |
| 247 | scsi_setup_write_ext(pccb, start, smallblks); |
| 248 | start += blks; |
| 249 | blks = 0; |
| 250 | } |
| 251 | debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", |
| 252 | __func__, start, smallblks, buf_addr); |
| 253 | if (scsi_exec(pccb) != true) { |
| 254 | scsi_print_error(pccb); |
| 255 | blkcnt -= blks; |
| 256 | break; |
| 257 | } |
| 258 | buf_addr += pccb->datalen; |
| 259 | } while (blks != 0); |
| 260 | debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", |
| 261 | __func__, start, smallblks, buf_addr); |
| 262 | return blkcnt; |
| 263 | } |
| 264 | |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 265 | #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) |
| 266 | void scsi_init(void) |
| 267 | { |
| 268 | int busdevfunc = -1; |
| 269 | int i; |
| 270 | /* |
| 271 | * Find a device from the list, this driver will support a single |
| 272 | * controller. |
| 273 | */ |
| 274 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 275 | /* get PCI Device ID */ |
| 276 | #ifdef CONFIG_DM_PCI |
| 277 | struct udevice *dev; |
| 278 | int ret; |
| 279 | |
| 280 | ret = dm_pci_find_device(scsi_device_list[i].vendor, |
| 281 | scsi_device_list[i].device, 0, &dev); |
| 282 | if (!ret) { |
| 283 | busdevfunc = dm_pci_get_bdf(dev); |
| 284 | break; |
| 285 | } |
| 286 | #else |
| 287 | busdevfunc = pci_find_device(scsi_device_list[i].vendor, |
| 288 | scsi_device_list[i].device, |
| 289 | 0); |
| 290 | #endif |
| 291 | if (busdevfunc != -1) |
| 292 | break; |
| 293 | } |
| 294 | |
| 295 | if (busdevfunc == -1) { |
| 296 | printf("Error: SCSI Controller(s) "); |
| 297 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 298 | printf("%04X:%04X ", |
| 299 | scsi_device_list[i].vendor, |
| 300 | scsi_device_list[i].device); |
| 301 | } |
| 302 | printf("not found\n"); |
| 303 | return; |
| 304 | } |
| 305 | #ifdef DEBUG |
| 306 | else { |
| 307 | printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n", |
| 308 | scsi_device_list[i].vendor, |
| 309 | scsi_device_list[i].device, |
| 310 | (busdevfunc >> 16) & 0xFF, |
| 311 | (busdevfunc >> 11) & 0x1F, |
| 312 | (busdevfunc >> 8) & 0x7); |
| 313 | } |
| 314 | #endif |
| 315 | bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci"); |
| 316 | scsi_low_level_init(busdevfunc); |
| 317 | scsi_scan(1); |
| 318 | bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI); |
| 319 | } |
| 320 | #endif |
| 321 | |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 322 | /* copy src to dest, skipping leading and trailing blanks |
| 323 | * and null terminate the string |
| 324 | */ |
Michal Simek | 545a284 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 325 | static void scsi_ident_cpy(unsigned char *dest, unsigned char *src, |
| 326 | unsigned int len) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 327 | { |
| 328 | int start, end; |
| 329 | |
| 330 | start = 0; |
| 331 | while (start < len) { |
| 332 | if (src[start] != ' ') |
| 333 | break; |
| 334 | start++; |
| 335 | } |
| 336 | end = len-1; |
| 337 | while (end > start) { |
| 338 | if (src[end] != ' ') |
| 339 | break; |
| 340 | end--; |
| 341 | } |
| 342 | for (; start <= end; start++) |
| 343 | *dest ++= src[start]; |
| 344 | *dest = '\0'; |
| 345 | } |
| 346 | |
Michal Simek | 545a284 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 347 | static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, |
| 348 | unsigned long *blksz) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 349 | { |
| 350 | *capacity = 0; |
| 351 | |
| 352 | memset(pccb->cmd, '\0', sizeof(pccb->cmd)); |
| 353 | pccb->cmd[0] = SCSI_RD_CAPAC10; |
| 354 | pccb->cmd[1] = pccb->lun << 5; |
| 355 | pccb->cmdlen = 10; |
| 356 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 357 | |
| 358 | pccb->datalen = 8; |
| 359 | if (scsi_exec(pccb) != true) |
| 360 | return 1; |
| 361 | |
| 362 | *capacity = ((lbaint_t)pccb->pdata[0] << 24) | |
| 363 | ((lbaint_t)pccb->pdata[1] << 16) | |
| 364 | ((lbaint_t)pccb->pdata[2] << 8) | |
| 365 | ((lbaint_t)pccb->pdata[3]); |
| 366 | |
| 367 | if (*capacity != 0xffffffff) { |
| 368 | /* Read capacity (10) was sufficient for this drive. */ |
| 369 | *blksz = ((unsigned long)pccb->pdata[4] << 24) | |
| 370 | ((unsigned long)pccb->pdata[5] << 16) | |
| 371 | ((unsigned long)pccb->pdata[6] << 8) | |
| 372 | ((unsigned long)pccb->pdata[7]); |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* Read capacity (10) was insufficient. Use read capacity (16). */ |
| 377 | memset(pccb->cmd, '\0', sizeof(pccb->cmd)); |
| 378 | pccb->cmd[0] = SCSI_RD_CAPAC16; |
| 379 | pccb->cmd[1] = 0x10; |
| 380 | pccb->cmdlen = 16; |
| 381 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 382 | |
| 383 | pccb->datalen = 16; |
| 384 | if (scsi_exec(pccb) != true) |
| 385 | return 1; |
| 386 | |
| 387 | *capacity = ((uint64_t)pccb->pdata[0] << 56) | |
| 388 | ((uint64_t)pccb->pdata[1] << 48) | |
| 389 | ((uint64_t)pccb->pdata[2] << 40) | |
| 390 | ((uint64_t)pccb->pdata[3] << 32) | |
| 391 | ((uint64_t)pccb->pdata[4] << 24) | |
| 392 | ((uint64_t)pccb->pdata[5] << 16) | |
| 393 | ((uint64_t)pccb->pdata[6] << 8) | |
| 394 | ((uint64_t)pccb->pdata[7]); |
| 395 | |
| 396 | *blksz = ((uint64_t)pccb->pdata[8] << 56) | |
| 397 | ((uint64_t)pccb->pdata[9] << 48) | |
| 398 | ((uint64_t)pccb->pdata[10] << 40) | |
| 399 | ((uint64_t)pccb->pdata[11] << 32) | |
| 400 | ((uint64_t)pccb->pdata[12] << 24) | |
| 401 | ((uint64_t)pccb->pdata[13] << 16) | |
| 402 | ((uint64_t)pccb->pdata[14] << 8) | |
| 403 | ((uint64_t)pccb->pdata[15]); |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | /* |
| 410 | * Some setup (fill-in) routines |
| 411 | */ |
Michal Simek | 545a284 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 412 | static void scsi_setup_test_unit_ready(ccb *pccb) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 413 | { |
| 414 | pccb->cmd[0] = SCSI_TST_U_RDY; |
| 415 | pccb->cmd[1] = pccb->lun << 5; |
| 416 | pccb->cmd[2] = 0; |
| 417 | pccb->cmd[3] = 0; |
| 418 | pccb->cmd[4] = 0; |
| 419 | pccb->cmd[5] = 0; |
| 420 | pccb->cmdlen = 6; |
| 421 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 422 | } |
| 423 | |
Michal Simek | 0b3a58e | 2016-11-30 12:50:58 +0100 | [diff] [blame] | 424 | /** |
| 425 | * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties |
| 426 | * |
| 427 | * @dev_desc: Block device description pointer |
| 428 | */ |
| 429 | static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc) |
Michal Simek | 92ca476 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 430 | { |
| 431 | dev_desc->target = 0xff; |
| 432 | dev_desc->lun = 0xff; |
Michal Simek | 92ca476 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 433 | dev_desc->log2blksz = |
| 434 | LOG2_INVALID(typeof(dev_desc->log2blksz)); |
| 435 | dev_desc->type = DEV_TYPE_UNKNOWN; |
| 436 | dev_desc->vendor[0] = 0; |
| 437 | dev_desc->product[0] = 0; |
| 438 | dev_desc->revision[0] = 0; |
| 439 | dev_desc->removable = false; |
Michal Simek | 92ca476 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 440 | #ifndef CONFIG_BLK |
| 441 | dev_desc->block_read = scsi_read; |
| 442 | dev_desc->block_write = scsi_write; |
| 443 | #endif |
| 444 | } |
| 445 | |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 446 | #if !defined(CONFIG_DM_SCSI) |
Michal Simek | 0b3a58e | 2016-11-30 12:50:58 +0100 | [diff] [blame] | 447 | /** |
| 448 | * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties |
| 449 | * |
| 450 | * @dev_desc: Block device description pointer |
| 451 | * @devnum: Device number |
| 452 | */ |
| 453 | static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum) |
| 454 | { |
| 455 | dev_desc->lba = 0; |
| 456 | dev_desc->blksz = 0; |
| 457 | dev_desc->if_type = IF_TYPE_SCSI; |
| 458 | dev_desc->devnum = devnum; |
| 459 | dev_desc->part_type = PART_TYPE_UNKNOWN; |
| 460 | |
| 461 | scsi_init_dev_desc_priv(dev_desc); |
| 462 | } |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 463 | #endif |
Michal Simek | bccfd9e | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 464 | |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 465 | /** |
| 466 | * scsi_detect_dev - Detect scsi device |
| 467 | * |
Michal Simek | bccfd9e | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 468 | * @target: target id |
Jean-Jacques Hiblot | e39cecf | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 469 | * @lun: target lun |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 470 | * @dev_desc: block device description |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 471 | * |
| 472 | * The scsi_detect_dev detects and fills a dev_desc structure when the device is |
Jean-Jacques Hiblot | e39cecf | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 473 | * detected. |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 474 | * |
| 475 | * Return: 0 on success, error value otherwise |
| 476 | */ |
Jean-Jacques Hiblot | e39cecf | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 477 | static int scsi_detect_dev(int target, int lun, struct blk_desc *dev_desc) |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 478 | { |
| 479 | unsigned char perq, modi; |
| 480 | lbaint_t capacity; |
| 481 | unsigned long blksz; |
Michal Simek | bccfd9e | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 482 | ccb *pccb = (ccb *)&tempccb; |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 483 | |
Michal Simek | bccfd9e | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 484 | pccb->target = target; |
Jean-Jacques Hiblot | e39cecf | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 485 | pccb->lun = lun; |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 486 | pccb->pdata = (unsigned char *)&tempbuff; |
| 487 | pccb->datalen = 512; |
| 488 | scsi_setup_inquiry(pccb); |
| 489 | if (scsi_exec(pccb) != true) { |
| 490 | if (pccb->contr_stat == SCSI_SEL_TIME_OUT) { |
| 491 | /* |
| 492 | * selection timeout => assuming no |
| 493 | * device present |
| 494 | */ |
| 495 | debug("Selection timeout ID %d\n", |
| 496 | pccb->target); |
| 497 | return -ETIMEDOUT; |
| 498 | } |
| 499 | scsi_print_error(pccb); |
| 500 | return -ENODEV; |
| 501 | } |
| 502 | perq = tempbuff[0]; |
| 503 | modi = tempbuff[1]; |
| 504 | if ((perq & 0x1f) == 0x1f) |
| 505 | return -ENODEV; /* skip unknown devices */ |
| 506 | if ((modi & 0x80) == 0x80) /* drive is removable */ |
| 507 | dev_desc->removable = true; |
| 508 | /* get info for this device */ |
| 509 | scsi_ident_cpy((unsigned char *)dev_desc->vendor, |
| 510 | &tempbuff[8], 8); |
| 511 | scsi_ident_cpy((unsigned char *)dev_desc->product, |
| 512 | &tempbuff[16], 16); |
| 513 | scsi_ident_cpy((unsigned char *)dev_desc->revision, |
| 514 | &tempbuff[32], 4); |
| 515 | dev_desc->target = pccb->target; |
| 516 | dev_desc->lun = pccb->lun; |
| 517 | |
| 518 | pccb->datalen = 0; |
| 519 | scsi_setup_test_unit_ready(pccb); |
| 520 | if (scsi_exec(pccb) != true) { |
| 521 | if (dev_desc->removable) { |
| 522 | dev_desc->type = perq; |
| 523 | goto removable; |
| 524 | } |
| 525 | scsi_print_error(pccb); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | if (scsi_read_capacity(pccb, &capacity, &blksz)) { |
| 529 | scsi_print_error(pccb); |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | dev_desc->lba = capacity; |
| 533 | dev_desc->blksz = blksz; |
| 534 | dev_desc->log2blksz = LOG2(dev_desc->blksz); |
| 535 | dev_desc->type = perq; |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 536 | removable: |
| 537 | return 0; |
| 538 | } |
| 539 | |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 540 | /* |
| 541 | * (re)-scan the scsi bus and reports scsi device info |
| 542 | * to the user if mode = 1 |
| 543 | */ |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 544 | #if defined(CONFIG_DM_SCSI) |
Jean-Jacques Hiblot | d52063b | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 545 | static int do_scsi_scan_one(struct udevice *dev, int id, int lun, int mode) |
| 546 | { |
| 547 | int ret; |
| 548 | struct udevice *bdev; |
| 549 | struct blk_desc bd; |
| 550 | struct blk_desc *bdesc; |
| 551 | char str[10]; |
| 552 | |
| 553 | /* |
| 554 | * detect the scsi driver to get information about its geometry (block |
| 555 | * size, number of blocks) and other parameters (ids, type, ...) |
| 556 | */ |
| 557 | scsi_init_dev_desc_priv(&bd); |
| 558 | if (scsi_detect_dev(id, lun, &bd)) |
| 559 | return -ENODEV; |
| 560 | |
| 561 | /* |
| 562 | * Create only one block device and do detection |
| 563 | * to make sure that there won't be a lot of |
| 564 | * block devices created |
| 565 | */ |
| 566 | snprintf(str, sizeof(str), "id%dlun%d", id, lun); |
| 567 | ret = blk_create_devicef(dev, "scsi_blk", str, IF_TYPE_SCSI, -1, |
| 568 | bd.blksz, bd.blksz * bd.lba, &bdev); |
| 569 | if (ret) { |
| 570 | debug("Can't create device\n"); |
| 571 | return ret; |
| 572 | } |
| 573 | |
| 574 | bdesc = dev_get_uclass_platdata(bdev); |
| 575 | bdesc->target = id; |
| 576 | bdesc->lun = lun; |
| 577 | bdesc->removable = bd.removable; |
| 578 | bdesc->type = bd.type; |
| 579 | memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor)); |
| 580 | memcpy(&bdesc->product, &bd.product, sizeof(bd.product)); |
| 581 | memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision)); |
| 582 | part_init(bdesc); |
| 583 | |
| 584 | if (mode == 1) { |
| 585 | printf(" Device %d: ", 0); |
| 586 | dev_print(bdesc); |
| 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 591 | int scsi_scan(int mode) |
| 592 | { |
| 593 | unsigned char i, lun; |
| 594 | struct uclass *uc; |
| 595 | struct udevice *dev; /* SCSI controller */ |
| 596 | int ret; |
| 597 | |
| 598 | if (mode == 1) |
| 599 | printf("scanning bus for devices...\n"); |
| 600 | |
Michal Simek | f8f41ae | 2017-01-02 09:40:09 +0100 | [diff] [blame] | 601 | blk_unbind_all(IF_TYPE_SCSI); |
| 602 | |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 603 | ret = uclass_get(UCLASS_SCSI, &uc); |
| 604 | if (ret) |
| 605 | return ret; |
| 606 | |
| 607 | uclass_foreach_dev(dev, uc) { |
| 608 | struct scsi_platdata *plat; /* scsi controller platdata */ |
| 609 | |
| 610 | /* probe SCSI controller driver */ |
| 611 | ret = device_probe(dev); |
| 612 | if (ret) |
| 613 | return ret; |
| 614 | |
| 615 | /* Get controller platdata */ |
| 616 | plat = dev_get_platdata(dev); |
| 617 | |
Jean-Jacques Hiblot | d52063b | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 618 | for (i = 0; i < plat->max_id; i++) |
| 619 | for (lun = 0; lun < plat->max_lun; lun++) |
| 620 | do_scsi_scan_one(dev, i, lun, mode); |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | #else |
Michal Simek | c002e39 | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 626 | int scsi_scan(int mode) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 627 | { |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 628 | unsigned char i, lun; |
| 629 | int ret; |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 630 | |
| 631 | if (mode == 1) |
| 632 | printf("scanning bus for devices...\n"); |
Michal Simek | 92ca476 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 633 | for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++) |
| 634 | scsi_init_dev_desc(&scsi_dev_desc[i], i); |
| 635 | |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 636 | scsi_max_devs = 0; |
| 637 | for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) { |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 638 | for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) { |
Jean-Jacques Hiblot | e39cecf | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 639 | ret = scsi_detect_dev(i, lun, |
| 640 | &scsi_dev_desc[scsi_max_devs]); |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 641 | if (ret) |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 642 | continue; |
Jean-Jacques Hiblot | 1330a72 | 2017-04-07 13:42:07 +0200 | [diff] [blame] | 643 | part_init(&scsi_dev_desc[scsi_max_devs]); |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 644 | |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 645 | if (mode == 1) { |
Michal Simek | 570712f | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 646 | printf(" Device %d: ", 0); |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 647 | dev_print(&scsi_dev_desc[scsi_max_devs]); |
| 648 | } /* if mode */ |
| 649 | scsi_max_devs++; |
| 650 | } /* next LUN */ |
| 651 | } |
| 652 | if (scsi_max_devs > 0) |
| 653 | scsi_curr_dev = 0; |
| 654 | else |
| 655 | scsi_curr_dev = -1; |
| 656 | |
| 657 | printf("Found %d device(s).\n", scsi_max_devs); |
| 658 | #ifndef CONFIG_SPL_BUILD |
| 659 | setenv_ulong("scsidevs", scsi_max_devs); |
| 660 | #endif |
Michal Simek | c002e39 | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 661 | return 0; |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 662 | } |
Michal Simek | e8a016b | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 663 | #endif |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 664 | |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 665 | #ifdef CONFIG_BLK |
| 666 | static const struct blk_ops scsi_blk_ops = { |
| 667 | .read = scsi_read, |
| 668 | .write = scsi_write, |
| 669 | }; |
| 670 | |
| 671 | U_BOOT_DRIVER(scsi_blk) = { |
| 672 | .name = "scsi_blk", |
| 673 | .id = UCLASS_BLK, |
| 674 | .ops = &scsi_blk_ops, |
| 675 | }; |
| 676 | #else |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 677 | U_BOOT_LEGACY_BLK(scsi) = { |
Ed Swarthout | 69c125f | 2016-06-01 08:11:24 -0500 | [diff] [blame] | 678 | .if_typename = "scsi", |
Simon Glass | 11f610e | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 679 | .if_type = IF_TYPE_SCSI, |
| 680 | .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE, |
| 681 | .desc = scsi_dev_desc, |
| 682 | }; |
Simon Glass | 535556b | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 683 | #endif |