3 * Denis Peter, MPL AG Switzerland
5 * SPDX-License-Identifier: GPL-2.0+
14 #ifdef CONFIG_SCSI_DEV_LIST
15 #define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
17 #ifdef CONFIG_SCSI_SYM53C8XX
18 #define SCSI_VEND_ID 0x1000
19 #ifndef CONFIG_SCSI_DEV_ID
20 #define SCSI_DEV_ID 0x0001
22 #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
24 #elif defined CONFIG_SATA_ULI5288
26 #define SCSI_VEND_ID 0x10b9
27 #define SCSI_DEV_ID 0x5288
29 #elif !defined(CONFIG_SCSI_AHCI_PLAT)
30 #error no scsi device defined
32 #define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
35 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
36 const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
38 static ccb tempccb; /* temporary scsi command buffer */
40 static unsigned char tempbuff[512]; /* temporary data buffer */
42 static int scsi_max_devs; /* number of highest available scsi device */
44 static int scsi_curr_dev; /* current device */
46 static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
48 /* almost the maximum amount of the scsi_ext command.. */
49 #define SCSI_MAX_READ_BLK 0xFFFF
50 #define SCSI_LBA48_READ 0xFFFFFFF
52 #ifdef CONFIG_SYS_64BIT_LBA
53 void scsi_setup_read16(ccb *pccb, lbaint_t start, unsigned long blocks)
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;
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;
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]);
81 void scsi_setup_read_ext(ccb *pccb, lbaint_t start, unsigned short blocks)
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;
90 pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
91 pccb->cmd[8] = (unsigned char)blocks & 0xff;
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]);
101 void scsi_setup_write_ext(ccb *pccb, lbaint_t start, unsigned short blocks)
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;
110 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
111 pccb->cmd[8] = (unsigned char)blocks & 0xff;
114 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
115 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
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]);
122 void scsi_setup_read6(ccb *pccb, lbaint_t start, unsigned short blocks)
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;
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]);
138 void scsi_setup_inquiry(ccb *pccb)
140 pccb->cmd[0] = SCSI_INQUIRY;
141 pccb->cmd[1] = pccb->lun << 5;
144 if (pccb->datalen > 255)
147 pccb->cmd[4] = (unsigned char)pccb->datalen;
150 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
154 static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
157 static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
158 lbaint_t blkcnt, void *buffer)
162 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
164 int device = block_dev->devnum;
165 lbaint_t start, blks;
167 unsigned short smallblks = 0;
168 ccb *pccb = (ccb *)&tempccb;
172 pccb->target = scsi_dev_desc[device].target;
173 pccb->lun = scsi_dev_desc[device].lun;
174 buf_addr = (unsigned long)buffer;
177 debug("\nscsi_read: dev %d startblk " LBAF
178 ", blccnt " LBAF " buffer %lx\n",
179 device, start, blks, (unsigned long)buffer);
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);
192 if (blks > SCSI_MAX_READ_BLK) {
193 pccb->datalen = scsi_dev_desc[device].blksz *
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;
200 pccb->datalen = scsi_dev_desc[device].blksz * blks;
201 smallblks = (unsigned short)blks;
202 scsi_setup_read_ext(pccb, start, smallblks);
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);
214 buf_addr += pccb->datalen;
216 debug("scsi_read_ext: end startblk " LBAF
217 ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr);
221 /*******************************************************************************
225 /* Almost the maximum amount of the scsi_ext command.. */
226 #define SCSI_MAX_WRITE_BLK 0xFFFF
229 static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
232 static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
233 lbaint_t blkcnt, const void *buffer)
237 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
239 int device = block_dev->devnum;
240 lbaint_t start, blks;
242 unsigned short smallblks;
243 ccb *pccb = (ccb *)&tempccb;
248 pccb->target = scsi_dev_desc[device].target;
249 pccb->lun = scsi_dev_desc[device].lun;
250 buf_addr = (unsigned long)buffer;
253 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
254 __func__, device, start, blks, (unsigned long)buffer);
256 pccb->pdata = (unsigned char *)buf_addr;
257 if (blks > SCSI_MAX_WRITE_BLK) {
258 pccb->datalen = (scsi_dev_desc[device].blksz *
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;
265 pccb->datalen = scsi_dev_desc[device].blksz * blks;
266 smallblks = (unsigned short)blks;
267 scsi_setup_write_ext(pccb, start, smallblks);
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);
278 buf_addr += pccb->datalen;
280 debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
281 __func__, start, smallblks, buf_addr);
285 int scsi_get_disk_count(void)
287 return scsi_max_devs;
290 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
296 * Find a device from the list, this driver will support a single
299 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
300 /* get PCI Device ID */
305 ret = dm_pci_find_device(scsi_device_list[i].vendor,
306 scsi_device_list[i].device, 0, &dev);
308 busdevfunc = dm_pci_get_bdf(dev);
312 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
313 scsi_device_list[i].device,
316 if (busdevfunc != -1)
320 if (busdevfunc == -1) {
321 printf("Error: SCSI Controller(s) ");
322 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
324 scsi_device_list[i].vendor,
325 scsi_device_list[i].device);
327 printf("not found\n");
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);
340 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
341 scsi_low_level_init(busdevfunc);
343 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
347 /* copy src to dest, skipping leading and trailing blanks
348 * and null terminate the string
350 void scsi_ident_cpy(unsigned char *dest, unsigned char *src, unsigned int len)
355 while (start < len) {
356 if (src[start] != ' ')
361 while (end > start) {
366 for (; start <= end; start++)
367 *dest ++= src[start];
372 /* Trim trailing blanks, and NUL-terminate string
374 void scsi_trim_trail(unsigned char *str, unsigned int len)
376 unsigned char *p = str + len - 1;
385 int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, unsigned long *blksz)
389 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
390 pccb->cmd[0] = SCSI_RD_CAPAC10;
391 pccb->cmd[1] = pccb->lun << 5;
393 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
396 if (scsi_exec(pccb) != true)
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]);
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]);
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;
418 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
421 if (scsi_exec(pccb) != true)
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]);
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]);
447 * Some setup (fill-in) routines
449 void scsi_setup_test_unit_ready(ccb *pccb)
451 pccb->cmd[0] = SCSI_TST_U_RDY;
452 pccb->cmd[1] = pccb->lun << 5;
458 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
462 * (re)-scan the scsi bus and reports scsi device info
463 * to the user if mode = 1
465 void scsi_scan(int mode)
467 unsigned char i, perq, modi, lun;
470 ccb *pccb = (ccb *)&tempccb;
473 printf("scanning bus for devices...\n");
474 for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++) {
475 scsi_dev_desc[i].target = 0xff;
476 scsi_dev_desc[i].lun = 0xff;
477 scsi_dev_desc[i].lba = 0;
478 scsi_dev_desc[i].blksz = 0;
479 scsi_dev_desc[i].log2blksz =
480 LOG2_INVALID(typeof(scsi_dev_desc[i].log2blksz));
481 scsi_dev_desc[i].type = DEV_TYPE_UNKNOWN;
482 scsi_dev_desc[i].vendor[0] = 0;
483 scsi_dev_desc[i].product[0] = 0;
484 scsi_dev_desc[i].revision[0] = 0;
485 scsi_dev_desc[i].removable = false;
486 scsi_dev_desc[i].if_type = IF_TYPE_SCSI;
487 scsi_dev_desc[i].devnum = i;
488 scsi_dev_desc[i].part_type = PART_TYPE_UNKNOWN;
490 scsi_dev_desc[i].block_read = scsi_read;
491 scsi_dev_desc[i].block_write = scsi_write;
495 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
497 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
499 pccb->pdata = (unsigned char *)&tempbuff;
501 scsi_setup_inquiry(pccb);
502 if (scsi_exec(pccb) != true) {
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 continue; /* skip unknown devices */
519 if ((modi & 0x80) == 0x80) /* drive is removable */
520 scsi_dev_desc[scsi_max_devs].removable = true;
521 /* get info for this device */
522 scsi_ident_cpy((unsigned char *)&scsi_dev_desc
523 [scsi_max_devs].vendor[0],
525 scsi_ident_cpy((unsigned char *)&scsi_dev_desc
526 [scsi_max_devs].product[0],
528 scsi_ident_cpy((unsigned char *)&scsi_dev_desc
529 [scsi_max_devs].revision[0],
531 scsi_dev_desc[scsi_max_devs].target = pccb->target;
532 scsi_dev_desc[scsi_max_devs].lun = pccb->lun;
535 scsi_setup_test_unit_ready(pccb);
536 if (scsi_exec(pccb) != true) {
537 if (scsi_dev_desc[scsi_max_devs].removable) {
538 scsi_dev_desc[scsi_max_devs].type =
542 scsi_print_error(pccb);
545 if (scsi_read_capacity(pccb, &capacity, &blksz)) {
546 scsi_print_error(pccb);
549 scsi_dev_desc[scsi_max_devs].lba = capacity;
550 scsi_dev_desc[scsi_max_devs].blksz = blksz;
551 scsi_dev_desc[scsi_max_devs].log2blksz =
552 LOG2(scsi_dev_desc[scsi_max_devs].blksz);
553 scsi_dev_desc[scsi_max_devs].type = perq;
554 part_init(&scsi_dev_desc[scsi_max_devs]);
557 printf(" Device %d: ", scsi_max_devs);
558 dev_print(&scsi_dev_desc[scsi_max_devs]);
563 if (scsi_max_devs > 0)
568 printf("Found %d device(s).\n", scsi_max_devs);
569 #ifndef CONFIG_SPL_BUILD
570 setenv_ulong("scsidevs", scsi_max_devs);
575 static const struct blk_ops scsi_blk_ops = {
580 U_BOOT_DRIVER(scsi_blk) = {
583 .ops = &scsi_blk_ops,
586 U_BOOT_LEGACY_BLK(scsi) = {
587 .if_typename = "scsi",
588 .if_type = IF_TYPE_SCSI,
589 .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE,
590 .desc = scsi_dev_desc,