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