1 // SPDX-License-Identifier: GPL-2.0+
4 * Denis Peter, MPL AG Switzerland
11 #include <dm/device-internal.h>
12 #include <dm/uclass-internal.h>
14 #if !defined(CONFIG_DM_SCSI)
15 # ifdef CONFIG_SCSI_DEV_LIST
16 # define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
18 # ifdef CONFIG_SATA_ULI5288
20 # define SCSI_VEND_ID 0x10b9
21 # define SCSI_DEV_ID 0x5288
23 # elif !defined(CONFIG_SCSI_AHCI_PLAT)
24 # error no scsi device defined
26 # define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
30 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
31 !defined(CONFIG_DM_SCSI)
32 const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
34 static struct scsi_cmd tempccb; /* temporary scsi command buffer */
36 static unsigned char tempbuff[512]; /* temporary data buffer */
38 #if !defined(CONFIG_DM_SCSI)
39 static int scsi_max_devs; /* number of highest available scsi device */
41 static int scsi_curr_dev; /* current device */
43 static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
46 /* almost the maximum amount of the scsi_ext command.. */
47 #define SCSI_MAX_READ_BLK 0xFFFF
48 #define SCSI_LBA48_READ 0xFFFFFFF
50 static void scsi_print_error(struct scsi_cmd *pccb)
52 /* Dummy function that could print an error for debugging */
55 #ifdef CONFIG_SYS_64BIT_LBA
56 void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start,
59 pccb->cmd[0] = SCSI_READ16;
60 pccb->cmd[1] = pccb->lun << 5;
61 pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff;
62 pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff;
63 pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff;
64 pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff;
65 pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff;
66 pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff;
67 pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff;
68 pccb->cmd[9] = (unsigned char)start & 0xff;
70 pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff;
71 pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff;
72 pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff;
73 pccb->cmd[14] = (unsigned char)blocks & 0xff;
76 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
77 debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n",
78 pccb->cmd[0], pccb->cmd[1],
79 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
80 pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
81 pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
85 static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
86 unsigned short blocks)
88 pccb->cmd[0] = SCSI_READ10;
89 pccb->cmd[1] = pccb->lun << 5;
90 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
91 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
92 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
93 pccb->cmd[5] = (unsigned char)start & 0xff;
95 pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
96 pccb->cmd[8] = (unsigned char)blocks & 0xff;
99 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
100 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
101 pccb->cmd[0], pccb->cmd[1],
102 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
103 pccb->cmd[7], pccb->cmd[8]);
106 static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
107 unsigned short blocks)
109 pccb->cmd[0] = SCSI_WRITE10;
110 pccb->cmd[1] = pccb->lun << 5;
111 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
112 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
113 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
114 pccb->cmd[5] = (unsigned char)start & 0xff;
116 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
117 pccb->cmd[8] = (unsigned char)blocks & 0xff;
120 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
121 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
123 pccb->cmd[0], pccb->cmd[1],
124 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
125 pccb->cmd[7], pccb->cmd[8]);
128 static void scsi_setup_inquiry(struct scsi_cmd *pccb)
130 pccb->cmd[0] = SCSI_INQUIRY;
131 pccb->cmd[1] = pccb->lun << 5;
134 if (pccb->datalen > 255)
137 pccb->cmd[4] = (unsigned char)pccb->datalen;
140 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
144 static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
147 static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
148 lbaint_t blkcnt, void *buffer)
152 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
153 struct udevice *bdev = dev->parent;
155 struct udevice *bdev = NULL;
157 lbaint_t start, blks;
159 unsigned short smallblks = 0;
160 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
163 pccb->target = block_dev->target;
164 pccb->lun = block_dev->lun;
165 buf_addr = (unsigned long)buffer;
168 debug("\nscsi_read: dev %d startblk " LBAF
169 ", blccnt " LBAF " buffer %lx\n",
170 block_dev->devnum, start, blks, (unsigned long)buffer);
172 pccb->pdata = (unsigned char *)buf_addr;
173 #ifdef CONFIG_SYS_64BIT_LBA
174 if (start > SCSI_LBA48_READ) {
175 unsigned long blocks;
176 blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK);
177 pccb->datalen = block_dev->blksz * blocks;
178 scsi_setup_read16(pccb, start, blocks);
183 if (blks > SCSI_MAX_READ_BLK) {
184 pccb->datalen = block_dev->blksz *
186 smallblks = SCSI_MAX_READ_BLK;
187 scsi_setup_read_ext(pccb, start, smallblks);
188 start += SCSI_MAX_READ_BLK;
189 blks -= SCSI_MAX_READ_BLK;
191 pccb->datalen = block_dev->blksz * blks;
192 smallblks = (unsigned short)blks;
193 scsi_setup_read_ext(pccb, start, smallblks);
197 debug("scsi_read_ext: startblk " LBAF
198 ", blccnt %x buffer %lX\n",
199 start, smallblks, buf_addr);
200 if (scsi_exec(bdev, pccb)) {
201 scsi_print_error(pccb);
205 buf_addr += pccb->datalen;
207 debug("scsi_read_ext: end startblk " LBAF
208 ", blccnt %x buffer %lX\n", start, smallblks, buf_addr);
212 /*******************************************************************************
216 /* Almost the maximum amount of the scsi_ext command.. */
217 #define SCSI_MAX_WRITE_BLK 0xFFFF
220 static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
223 static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
224 lbaint_t blkcnt, const void *buffer)
228 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
229 struct udevice *bdev = dev->parent;
231 struct udevice *bdev = NULL;
233 lbaint_t start, blks;
235 unsigned short smallblks;
236 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
239 pccb->target = block_dev->target;
240 pccb->lun = block_dev->lun;
241 buf_addr = (unsigned long)buffer;
244 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
245 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
247 pccb->pdata = (unsigned char *)buf_addr;
248 if (blks > SCSI_MAX_WRITE_BLK) {
249 pccb->datalen = (block_dev->blksz *
251 smallblks = SCSI_MAX_WRITE_BLK;
252 scsi_setup_write_ext(pccb, start, smallblks);
253 start += SCSI_MAX_WRITE_BLK;
254 blks -= SCSI_MAX_WRITE_BLK;
256 pccb->datalen = block_dev->blksz * blks;
257 smallblks = (unsigned short)blks;
258 scsi_setup_write_ext(pccb, start, smallblks);
262 debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
263 __func__, start, smallblks, buf_addr);
264 if (scsi_exec(bdev, pccb)) {
265 scsi_print_error(pccb);
269 buf_addr += pccb->datalen;
271 debug("%s: end startblk " LBAF ", blccnt %x buffer %lX\n",
272 __func__, start, smallblks, buf_addr);
276 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
277 !defined(CONFIG_DM_SCSI)
283 * Find a device from the list, this driver will support a single
286 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
287 /* get PCI Device ID */
292 ret = dm_pci_find_device(scsi_device_list[i].vendor,
293 scsi_device_list[i].device, 0, &dev);
295 busdevfunc = dm_pci_get_bdf(dev);
299 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
300 scsi_device_list[i].device,
303 if (busdevfunc != -1)
307 if (busdevfunc == -1) {
308 printf("Error: SCSI Controller(s) ");
309 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
311 scsi_device_list[i].vendor,
312 scsi_device_list[i].device);
314 printf("not found\n");
319 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
320 scsi_device_list[i].vendor,
321 scsi_device_list[i].device,
322 (busdevfunc >> 16) & 0xFF,
323 (busdevfunc >> 11) & 0x1F,
324 (busdevfunc >> 8) & 0x7);
327 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
328 scsi_low_level_init(busdevfunc);
330 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
334 /* copy src to dest, skipping leading and trailing blanks
335 * and null terminate the string
337 static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
343 while (start < len) {
344 if (src[start] != ' ')
349 while (end > start) {
354 for (; start <= end; start++)
355 *dest ++= src[start];
359 static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb,
360 lbaint_t *capacity, unsigned long *blksz)
364 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
365 pccb->cmd[0] = SCSI_RD_CAPAC10;
366 pccb->cmd[1] = pccb->lun << 5;
368 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
371 if (scsi_exec(dev, pccb))
374 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
375 ((lbaint_t)pccb->pdata[1] << 16) |
376 ((lbaint_t)pccb->pdata[2] << 8) |
377 ((lbaint_t)pccb->pdata[3]);
379 if (*capacity != 0xffffffff) {
380 /* Read capacity (10) was sufficient for this drive. */
381 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
382 ((unsigned long)pccb->pdata[5] << 16) |
383 ((unsigned long)pccb->pdata[6] << 8) |
384 ((unsigned long)pccb->pdata[7]);
388 /* Read capacity (10) was insufficient. Use read capacity (16). */
389 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
390 pccb->cmd[0] = SCSI_RD_CAPAC16;
393 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
396 if (scsi_exec(dev, pccb))
399 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
400 ((uint64_t)pccb->pdata[1] << 48) |
401 ((uint64_t)pccb->pdata[2] << 40) |
402 ((uint64_t)pccb->pdata[3] << 32) |
403 ((uint64_t)pccb->pdata[4] << 24) |
404 ((uint64_t)pccb->pdata[5] << 16) |
405 ((uint64_t)pccb->pdata[6] << 8) |
406 ((uint64_t)pccb->pdata[7]);
408 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
409 ((uint64_t)pccb->pdata[9] << 48) |
410 ((uint64_t)pccb->pdata[10] << 40) |
411 ((uint64_t)pccb->pdata[11] << 32) |
412 ((uint64_t)pccb->pdata[12] << 24) |
413 ((uint64_t)pccb->pdata[13] << 16) |
414 ((uint64_t)pccb->pdata[14] << 8) |
415 ((uint64_t)pccb->pdata[15]);
422 * Some setup (fill-in) routines
424 static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
426 pccb->cmd[0] = SCSI_TST_U_RDY;
427 pccb->cmd[1] = pccb->lun << 5;
433 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
437 * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
439 * @dev_desc: Block device description pointer
441 static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
443 dev_desc->target = 0xff;
444 dev_desc->lun = 0xff;
445 dev_desc->log2blksz =
446 LOG2_INVALID(typeof(dev_desc->log2blksz));
447 dev_desc->type = DEV_TYPE_UNKNOWN;
448 dev_desc->vendor[0] = 0;
449 dev_desc->product[0] = 0;
450 dev_desc->revision[0] = 0;
451 dev_desc->removable = false;
452 #if !CONFIG_IS_ENABLED(BLK)
453 dev_desc->block_read = scsi_read;
454 dev_desc->block_write = scsi_write;
458 #if !defined(CONFIG_DM_SCSI)
460 * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties
462 * @dev_desc: Block device description pointer
463 * @devnum: Device number
465 static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
469 dev_desc->if_type = IF_TYPE_SCSI;
470 dev_desc->devnum = devnum;
471 dev_desc->part_type = PART_TYPE_UNKNOWN;
473 scsi_init_dev_desc_priv(dev_desc);
478 * scsi_detect_dev - Detect scsi device
482 * @dev_desc: block device description
484 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
487 * Return: 0 on success, error value otherwise
489 static int scsi_detect_dev(struct udevice *dev, int target, int lun,
490 struct blk_desc *dev_desc)
492 unsigned char perq, modi;
495 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
497 pccb->target = target;
499 pccb->pdata = (unsigned char *)&tempbuff;
501 scsi_setup_inquiry(pccb);
502 if (scsi_exec(dev, pccb)) {
503 if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
505 * selection timeout => assuming no
508 debug("Selection timeout ID %d\n",
512 scsi_print_error(pccb);
517 if ((perq & 0x1f) == 0x1f)
518 return -ENODEV; /* skip unknown devices */
519 if ((modi & 0x80) == 0x80) /* drive is removable */
520 dev_desc->removable = true;
521 /* get info for this device */
522 scsi_ident_cpy((unsigned char *)dev_desc->vendor,
524 scsi_ident_cpy((unsigned char *)dev_desc->product,
526 scsi_ident_cpy((unsigned char *)dev_desc->revision,
528 dev_desc->target = pccb->target;
529 dev_desc->lun = pccb->lun;
532 scsi_setup_test_unit_ready(pccb);
533 if (scsi_exec(dev, pccb)) {
534 if (dev_desc->removable) {
535 dev_desc->type = perq;
538 scsi_print_error(pccb);
541 if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) {
542 scsi_print_error(pccb);
545 dev_desc->lba = capacity;
546 dev_desc->blksz = blksz;
547 dev_desc->log2blksz = LOG2(dev_desc->blksz);
548 dev_desc->type = perq;
554 * (re)-scan the scsi bus and reports scsi device info
555 * to the user if mode = 1
557 #if defined(CONFIG_DM_SCSI)
558 static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
561 struct udevice *bdev;
563 struct blk_desc *bdesc;
567 * detect the scsi driver to get information about its geometry (block
568 * size, number of blocks) and other parameters (ids, type, ...)
570 scsi_init_dev_desc_priv(&bd);
571 if (scsi_detect_dev(dev, id, lun, &bd))
575 * Create only one block device and do detection
576 * to make sure that there won't be a lot of
577 * block devices created
579 snprintf(str, sizeof(str), "id%dlun%d", id, lun);
580 ret = blk_create_devicef(dev, "scsi_blk", str, IF_TYPE_SCSI, -1,
581 bd.blksz, bd.lba, &bdev);
583 debug("Can't create device\n");
587 bdesc = dev_get_uclass_platdata(bdev);
590 bdesc->removable = bd.removable;
591 bdesc->type = bd.type;
592 memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
593 memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
594 memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision));
597 printf(" Device %d: ", bdesc->devnum);
603 int scsi_scan_dev(struct udevice *dev, bool verbose)
605 struct scsi_platdata *uc_plat; /* scsi controller platdata */
610 /* probe SCSI controller driver */
611 ret = device_probe(dev);
615 /* Get controller platdata */
616 uc_plat = dev_get_uclass_platdata(dev);
618 for (i = 0; i < uc_plat->max_id; i++)
619 for (lun = 0; lun < uc_plat->max_lun; lun++)
620 do_scsi_scan_one(dev, i, lun, verbose);
625 int scsi_scan(bool verbose)
628 struct udevice *dev; /* SCSI controller */
632 printf("scanning bus for devices...\n");
634 blk_unbind_all(IF_TYPE_SCSI);
636 ret = uclass_get(UCLASS_SCSI, &uc);
640 uclass_foreach_dev(dev, uc) {
641 ret = scsi_scan_dev(dev, verbose);
649 int scsi_scan(bool verbose)
651 unsigned char i, lun;
655 printf("scanning bus for devices...\n");
656 for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++)
657 scsi_init_dev_desc(&scsi_dev_desc[i], i);
660 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
661 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
662 struct blk_desc *bdesc = &scsi_dev_desc[scsi_max_devs];
664 ret = scsi_detect_dev(NULL, i, lun, bdesc);
670 printf(" Device %d: ", bdesc->devnum);
676 if (scsi_max_devs > 0)
681 printf("Found %d device(s).\n", scsi_max_devs);
682 #ifndef CONFIG_SPL_BUILD
683 env_set_ulong("scsidevs", scsi_max_devs);
690 static const struct blk_ops scsi_blk_ops = {
695 U_BOOT_DRIVER(scsi_blk) = {
698 .ops = &scsi_blk_ops,
701 U_BOOT_LEGACY_BLK(scsi) = {
702 .if_typename = "scsi",
703 .if_type = IF_TYPE_SCSI,
704 .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE,
705 .desc = scsi_dev_desc,