1 // SPDX-License-Identifier: GPL-2.0+
4 * Denis Peter, MPL AG Switzerland
12 #include <dm/device-internal.h>
13 #include <dm/uclass-internal.h>
15 #if !defined(CONFIG_DM_SCSI)
16 # ifdef CONFIG_SCSI_DEV_LIST
17 # define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
19 # ifdef CONFIG_SATA_ULI5288
21 # define SCSI_VEND_ID 0x10b9
22 # define SCSI_DEV_ID 0x5288
24 # elif !defined(CONFIG_SCSI_AHCI_PLAT)
25 # error no scsi device defined
27 # define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
31 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
32 !defined(CONFIG_DM_SCSI)
33 const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
35 static struct scsi_cmd tempccb; /* temporary scsi command buffer */
37 static unsigned char tempbuff[512]; /* temporary data buffer */
39 #if !defined(CONFIG_DM_SCSI)
40 static int scsi_max_devs; /* number of highest available scsi device */
42 static int scsi_curr_dev; /* current device */
44 static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
47 /* almost the maximum amount of the scsi_ext command.. */
48 #define SCSI_MAX_READ_BLK 0xFFFF
49 #define SCSI_LBA48_READ 0xFFFFFFF
51 static void scsi_print_error(struct scsi_cmd *pccb)
53 /* Dummy function that could print an error for debugging */
56 #ifdef CONFIG_SYS_64BIT_LBA
57 void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start,
60 pccb->cmd[0] = SCSI_READ16;
61 pccb->cmd[1] = pccb->lun << 5;
62 pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff;
63 pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff;
64 pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff;
65 pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff;
66 pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff;
67 pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff;
68 pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff;
69 pccb->cmd[9] = (unsigned char)start & 0xff;
71 pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff;
72 pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff;
73 pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff;
74 pccb->cmd[14] = (unsigned char)blocks & 0xff;
77 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
78 debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n",
79 pccb->cmd[0], pccb->cmd[1],
80 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
81 pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
82 pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
86 static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
87 unsigned short blocks)
89 pccb->cmd[0] = SCSI_READ10;
90 pccb->cmd[1] = pccb->lun << 5;
91 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
92 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
93 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
94 pccb->cmd[5] = (unsigned char)start & 0xff;
96 pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
97 pccb->cmd[8] = (unsigned char)blocks & 0xff;
100 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
101 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
102 pccb->cmd[0], pccb->cmd[1],
103 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
104 pccb->cmd[7], pccb->cmd[8]);
107 static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
108 unsigned short blocks)
110 pccb->cmd[0] = SCSI_WRITE10;
111 pccb->cmd[1] = pccb->lun << 5;
112 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
113 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
114 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
115 pccb->cmd[5] = (unsigned char)start & 0xff;
117 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
118 pccb->cmd[8] = (unsigned char)blocks & 0xff;
121 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
122 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
124 pccb->cmd[0], pccb->cmd[1],
125 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
126 pccb->cmd[7], pccb->cmd[8]);
129 static void scsi_setup_inquiry(struct scsi_cmd *pccb)
131 pccb->cmd[0] = SCSI_INQUIRY;
132 pccb->cmd[1] = pccb->lun << 5;
135 if (pccb->datalen > 255)
138 pccb->cmd[4] = (unsigned char)pccb->datalen;
141 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
145 static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
148 static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
149 lbaint_t blkcnt, void *buffer)
153 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
154 struct udevice *bdev = dev->parent;
156 struct udevice *bdev = NULL;
158 lbaint_t start, blks;
160 unsigned short smallblks = 0;
161 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
164 pccb->target = block_dev->target;
165 pccb->lun = block_dev->lun;
166 buf_addr = (unsigned long)buffer;
169 debug("\nscsi_read: dev %d startblk " LBAF
170 ", blccnt " LBAF " buffer %lx\n",
171 block_dev->devnum, start, blks, (unsigned long)buffer);
173 pccb->pdata = (unsigned char *)buf_addr;
174 #ifdef CONFIG_SYS_64BIT_LBA
175 if (start > SCSI_LBA48_READ) {
176 unsigned long blocks;
177 blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK);
178 pccb->datalen = block_dev->blksz * blocks;
179 scsi_setup_read16(pccb, start, blocks);
184 if (blks > SCSI_MAX_READ_BLK) {
185 pccb->datalen = block_dev->blksz *
187 smallblks = SCSI_MAX_READ_BLK;
188 scsi_setup_read_ext(pccb, start, smallblks);
189 start += SCSI_MAX_READ_BLK;
190 blks -= SCSI_MAX_READ_BLK;
192 pccb->datalen = block_dev->blksz * blks;
193 smallblks = (unsigned short)blks;
194 scsi_setup_read_ext(pccb, start, smallblks);
198 debug("scsi_read_ext: startblk " LBAF
199 ", blccnt %x buffer %" PRIXPTR "\n",
200 start, smallblks, buf_addr);
201 if (scsi_exec(bdev, pccb)) {
202 scsi_print_error(pccb);
206 buf_addr += pccb->datalen;
208 debug("scsi_read_ext: end startblk " LBAF
209 ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr);
213 /*******************************************************************************
217 /* Almost the maximum amount of the scsi_ext command.. */
218 #define SCSI_MAX_WRITE_BLK 0xFFFF
221 static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
224 static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
225 lbaint_t blkcnt, const void *buffer)
229 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
230 struct udevice *bdev = dev->parent;
232 struct udevice *bdev = NULL;
234 lbaint_t start, blks;
236 unsigned short smallblks;
237 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
240 pccb->target = block_dev->target;
241 pccb->lun = block_dev->lun;
242 buf_addr = (unsigned long)buffer;
245 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
246 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
248 pccb->pdata = (unsigned char *)buf_addr;
249 if (blks > SCSI_MAX_WRITE_BLK) {
250 pccb->datalen = (block_dev->blksz *
252 smallblks = SCSI_MAX_WRITE_BLK;
253 scsi_setup_write_ext(pccb, start, smallblks);
254 start += SCSI_MAX_WRITE_BLK;
255 blks -= SCSI_MAX_WRITE_BLK;
257 pccb->datalen = block_dev->blksz * blks;
258 smallblks = (unsigned short)blks;
259 scsi_setup_write_ext(pccb, start, smallblks);
263 debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
264 __func__, start, smallblks, buf_addr);
265 if (scsi_exec(bdev, pccb)) {
266 scsi_print_error(pccb);
270 buf_addr += pccb->datalen;
272 debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
273 __func__, start, smallblks, buf_addr);
277 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
278 !defined(CONFIG_DM_SCSI)
284 * Find a device from the list, this driver will support a single
287 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
288 /* get PCI Device ID */
293 ret = dm_pci_find_device(scsi_device_list[i].vendor,
294 scsi_device_list[i].device, 0, &dev);
296 busdevfunc = dm_pci_get_bdf(dev);
300 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
301 scsi_device_list[i].device,
304 if (busdevfunc != -1)
308 if (busdevfunc == -1) {
309 printf("Error: SCSI Controller(s) ");
310 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
312 scsi_device_list[i].vendor,
313 scsi_device_list[i].device);
315 printf("not found\n");
320 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
321 scsi_device_list[i].vendor,
322 scsi_device_list[i].device,
323 (busdevfunc >> 16) & 0xFF,
324 (busdevfunc >> 11) & 0x1F,
325 (busdevfunc >> 8) & 0x7);
328 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
329 scsi_low_level_init(busdevfunc);
331 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
335 /* copy src to dest, skipping leading and trailing blanks
336 * and null terminate the string
338 static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
344 while (start < len) {
345 if (src[start] != ' ')
350 while (end > start) {
355 for (; start <= end; start++)
356 *dest ++= src[start];
360 static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb,
361 lbaint_t *capacity, unsigned long *blksz)
365 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
366 pccb->cmd[0] = SCSI_RD_CAPAC10;
367 pccb->cmd[1] = pccb->lun << 5;
369 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
372 if (scsi_exec(dev, pccb))
375 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
376 ((lbaint_t)pccb->pdata[1] << 16) |
377 ((lbaint_t)pccb->pdata[2] << 8) |
378 ((lbaint_t)pccb->pdata[3]);
380 if (*capacity != 0xffffffff) {
381 /* Read capacity (10) was sufficient for this drive. */
382 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
383 ((unsigned long)pccb->pdata[5] << 16) |
384 ((unsigned long)pccb->pdata[6] << 8) |
385 ((unsigned long)pccb->pdata[7]);
389 /* Read capacity (10) was insufficient. Use read capacity (16). */
390 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
391 pccb->cmd[0] = SCSI_RD_CAPAC16;
394 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
397 if (scsi_exec(dev, pccb))
400 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
401 ((uint64_t)pccb->pdata[1] << 48) |
402 ((uint64_t)pccb->pdata[2] << 40) |
403 ((uint64_t)pccb->pdata[3] << 32) |
404 ((uint64_t)pccb->pdata[4] << 24) |
405 ((uint64_t)pccb->pdata[5] << 16) |
406 ((uint64_t)pccb->pdata[6] << 8) |
407 ((uint64_t)pccb->pdata[7]);
409 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
410 ((uint64_t)pccb->pdata[9] << 48) |
411 ((uint64_t)pccb->pdata[10] << 40) |
412 ((uint64_t)pccb->pdata[11] << 32) |
413 ((uint64_t)pccb->pdata[12] << 24) |
414 ((uint64_t)pccb->pdata[13] << 16) |
415 ((uint64_t)pccb->pdata[14] << 8) |
416 ((uint64_t)pccb->pdata[15]);
423 * Some setup (fill-in) routines
425 static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
427 pccb->cmd[0] = SCSI_TST_U_RDY;
428 pccb->cmd[1] = pccb->lun << 5;
434 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
438 * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
440 * @dev_desc: Block device description pointer
442 static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
444 dev_desc->target = 0xff;
445 dev_desc->lun = 0xff;
446 dev_desc->log2blksz =
447 LOG2_INVALID(typeof(dev_desc->log2blksz));
448 dev_desc->type = DEV_TYPE_UNKNOWN;
449 dev_desc->vendor[0] = 0;
450 dev_desc->product[0] = 0;
451 dev_desc->revision[0] = 0;
452 dev_desc->removable = false;
453 #if !CONFIG_IS_ENABLED(BLK)
454 dev_desc->block_read = scsi_read;
455 dev_desc->block_write = scsi_write;
459 #if !defined(CONFIG_DM_SCSI)
461 * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties
463 * @dev_desc: Block device description pointer
464 * @devnum: Device number
466 static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
470 dev_desc->if_type = IF_TYPE_SCSI;
471 dev_desc->devnum = devnum;
472 dev_desc->part_type = PART_TYPE_UNKNOWN;
474 scsi_init_dev_desc_priv(dev_desc);
479 * scsi_detect_dev - Detect scsi device
483 * @dev_desc: block device description
485 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
488 * Return: 0 on success, error value otherwise
490 static int scsi_detect_dev(struct udevice *dev, int target, int lun,
491 struct blk_desc *dev_desc)
493 unsigned char perq, modi;
496 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
498 pccb->target = target;
500 pccb->pdata = (unsigned char *)&tempbuff;
502 scsi_setup_inquiry(pccb);
503 if (scsi_exec(dev, pccb)) {
504 if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
506 * selection timeout => assuming no
509 debug("Selection timeout ID %d\n",
513 scsi_print_error(pccb);
518 if ((perq & 0x1f) == 0x1f)
519 return -ENODEV; /* skip unknown devices */
520 if ((modi & 0x80) == 0x80) /* drive is removable */
521 dev_desc->removable = true;
522 /* get info for this device */
523 scsi_ident_cpy((unsigned char *)dev_desc->vendor,
525 scsi_ident_cpy((unsigned char *)dev_desc->product,
527 scsi_ident_cpy((unsigned char *)dev_desc->revision,
529 dev_desc->target = pccb->target;
530 dev_desc->lun = pccb->lun;
533 scsi_setup_test_unit_ready(pccb);
534 if (scsi_exec(dev, pccb)) {
535 if (dev_desc->removable) {
536 dev_desc->type = perq;
539 scsi_print_error(pccb);
542 if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) {
543 scsi_print_error(pccb);
546 dev_desc->lba = capacity;
547 dev_desc->blksz = blksz;
548 dev_desc->log2blksz = LOG2(dev_desc->blksz);
549 dev_desc->type = perq;
555 * (re)-scan the scsi bus and reports scsi device info
556 * to the user if mode = 1
558 #if defined(CONFIG_DM_SCSI)
559 static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
562 struct udevice *bdev;
564 struct blk_desc *bdesc;
568 * detect the scsi driver to get information about its geometry (block
569 * size, number of blocks) and other parameters (ids, type, ...)
571 scsi_init_dev_desc_priv(&bd);
572 if (scsi_detect_dev(dev, id, lun, &bd))
576 * Create only one block device and do detection
577 * to make sure that there won't be a lot of
578 * block devices created
580 snprintf(str, sizeof(str), "id%dlun%d", id, lun);
581 ret = blk_create_devicef(dev, "scsi_blk", str, IF_TYPE_SCSI, -1,
582 bd.blksz, bd.lba, &bdev);
584 debug("Can't create device\n");
588 bdesc = dev_get_uclass_platdata(bdev);
591 bdesc->removable = bd.removable;
592 bdesc->type = bd.type;
593 memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
594 memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
595 memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision));
599 printf(" Device %d: ", 0);
605 int scsi_scan_dev(struct udevice *dev, bool verbose)
607 struct scsi_platdata *uc_plat; /* scsi controller platdata */
612 /* probe SCSI controller driver */
613 ret = device_probe(dev);
617 /* Get controller platdata */
618 uc_plat = dev_get_uclass_platdata(dev);
620 for (i = 0; i < uc_plat->max_id; i++)
621 for (lun = 0; lun < uc_plat->max_lun; lun++)
622 do_scsi_scan_one(dev, i, lun, verbose);
627 int scsi_scan(bool verbose)
630 struct udevice *dev; /* SCSI controller */
634 printf("scanning bus for devices...\n");
636 blk_unbind_all(IF_TYPE_SCSI);
638 ret = uclass_get(UCLASS_SCSI, &uc);
642 uclass_foreach_dev(dev, uc) {
643 ret = scsi_scan_dev(dev, verbose);
651 int scsi_scan(bool verbose)
653 unsigned char i, lun;
657 printf("scanning bus for devices...\n");
658 for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++)
659 scsi_init_dev_desc(&scsi_dev_desc[i], i);
662 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
663 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
664 ret = scsi_detect_dev(NULL, i, lun,
665 &scsi_dev_desc[scsi_max_devs]);
668 part_init(&scsi_dev_desc[scsi_max_devs]);
671 printf(" Device %d: ", 0);
672 dev_print(&scsi_dev_desc[scsi_max_devs]);
677 if (scsi_max_devs > 0)
682 printf("Found %d device(s).\n", scsi_max_devs);
683 #ifndef CONFIG_SPL_BUILD
684 env_set_ulong("scsidevs", scsi_max_devs);
691 static const struct blk_ops scsi_blk_ops = {
696 U_BOOT_DRIVER(scsi_blk) = {
699 .ops = &scsi_blk_ops,
702 U_BOOT_LEGACY_BLK(scsi) = {
703 .if_typename = "scsi",
704 .if_type = IF_TYPE_SCSI,
705 .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE,
706 .desc = scsi_dev_desc,