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 static void scsi_setup_read_ext(ccb *pccb, lbaint_t start,
82 unsigned short blocks)
84 pccb->cmd[0] = SCSI_READ10;
85 pccb->cmd[1] = pccb->lun << 5;
86 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
87 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
88 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
89 pccb->cmd[5] = (unsigned char)start & 0xff;
91 pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
92 pccb->cmd[8] = (unsigned char)blocks & 0xff;
95 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
96 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
97 pccb->cmd[0], pccb->cmd[1],
98 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
99 pccb->cmd[7], pccb->cmd[8]);
102 static void scsi_setup_write_ext(ccb *pccb, lbaint_t start,
103 unsigned short blocks)
105 pccb->cmd[0] = SCSI_WRITE10;
106 pccb->cmd[1] = pccb->lun << 5;
107 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
108 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
109 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
110 pccb->cmd[5] = (unsigned char)start & 0xff;
112 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
113 pccb->cmd[8] = (unsigned char)blocks & 0xff;
116 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
117 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
119 pccb->cmd[0], pccb->cmd[1],
120 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
121 pccb->cmd[7], pccb->cmd[8]);
124 static void scsi_setup_inquiry(ccb *pccb)
126 pccb->cmd[0] = SCSI_INQUIRY;
127 pccb->cmd[1] = pccb->lun << 5;
130 if (pccb->datalen > 255)
133 pccb->cmd[4] = (unsigned char)pccb->datalen;
136 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
140 static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
143 static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
144 lbaint_t blkcnt, void *buffer)
148 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
150 lbaint_t start, blks;
152 unsigned short smallblks = 0;
153 ccb *pccb = (ccb *)&tempccb;
156 pccb->target = block_dev->target;
157 pccb->lun = block_dev->lun;
158 buf_addr = (unsigned long)buffer;
161 debug("\nscsi_read: dev %d startblk " LBAF
162 ", blccnt " LBAF " buffer %lx\n",
163 block_dev->devnum, start, blks, (unsigned long)buffer);
165 pccb->pdata = (unsigned char *)buf_addr;
166 #ifdef CONFIG_SYS_64BIT_LBA
167 if (start > SCSI_LBA48_READ) {
168 unsigned long blocks;
169 blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK);
170 pccb->datalen = block_dev->blksz * blocks;
171 scsi_setup_read16(pccb, start, blocks);
176 if (blks > SCSI_MAX_READ_BLK) {
177 pccb->datalen = block_dev->blksz *
179 smallblks = SCSI_MAX_READ_BLK;
180 scsi_setup_read_ext(pccb, start, smallblks);
181 start += SCSI_MAX_READ_BLK;
182 blks -= SCSI_MAX_READ_BLK;
184 pccb->datalen = block_dev->blksz * blks;
185 smallblks = (unsigned short)blks;
186 scsi_setup_read_ext(pccb, start, smallblks);
190 debug("scsi_read_ext: startblk " LBAF
191 ", blccnt %x buffer %" PRIXPTR "\n",
192 start, smallblks, buf_addr);
193 if (scsi_exec(pccb) != true) {
194 scsi_print_error(pccb);
198 buf_addr += pccb->datalen;
200 debug("scsi_read_ext: end startblk " LBAF
201 ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr);
205 /*******************************************************************************
209 /* Almost the maximum amount of the scsi_ext command.. */
210 #define SCSI_MAX_WRITE_BLK 0xFFFF
213 static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
216 static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
217 lbaint_t blkcnt, const void *buffer)
221 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
223 lbaint_t start, blks;
225 unsigned short smallblks;
226 ccb *pccb = (ccb *)&tempccb;
229 pccb->target = block_dev->target;
230 pccb->lun = block_dev->lun;
231 buf_addr = (unsigned long)buffer;
234 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
235 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
237 pccb->pdata = (unsigned char *)buf_addr;
238 if (blks > SCSI_MAX_WRITE_BLK) {
239 pccb->datalen = (block_dev->blksz *
241 smallblks = SCSI_MAX_WRITE_BLK;
242 scsi_setup_write_ext(pccb, start, smallblks);
243 start += SCSI_MAX_WRITE_BLK;
244 blks -= SCSI_MAX_WRITE_BLK;
246 pccb->datalen = block_dev->blksz * blks;
247 smallblks = (unsigned short)blks;
248 scsi_setup_write_ext(pccb, start, smallblks);
252 debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
253 __func__, start, smallblks, buf_addr);
254 if (scsi_exec(pccb) != true) {
255 scsi_print_error(pccb);
259 buf_addr += pccb->datalen;
261 debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
262 __func__, start, smallblks, buf_addr);
266 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
272 * Find a device from the list, this driver will support a single
275 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
276 /* get PCI Device ID */
281 ret = dm_pci_find_device(scsi_device_list[i].vendor,
282 scsi_device_list[i].device, 0, &dev);
284 busdevfunc = dm_pci_get_bdf(dev);
288 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
289 scsi_device_list[i].device,
292 if (busdevfunc != -1)
296 if (busdevfunc == -1) {
297 printf("Error: SCSI Controller(s) ");
298 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
300 scsi_device_list[i].vendor,
301 scsi_device_list[i].device);
303 printf("not found\n");
308 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
309 scsi_device_list[i].vendor,
310 scsi_device_list[i].device,
311 (busdevfunc >> 16) & 0xFF,
312 (busdevfunc >> 11) & 0x1F,
313 (busdevfunc >> 8) & 0x7);
316 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
317 scsi_low_level_init(busdevfunc);
319 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
323 /* copy src to dest, skipping leading and trailing blanks
324 * and null terminate the string
326 static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
332 while (start < len) {
333 if (src[start] != ' ')
338 while (end > start) {
343 for (; start <= end; start++)
344 *dest ++= src[start];
348 static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity,
349 unsigned long *blksz)
353 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
354 pccb->cmd[0] = SCSI_RD_CAPAC10;
355 pccb->cmd[1] = pccb->lun << 5;
357 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
360 if (scsi_exec(pccb) != true)
363 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
364 ((lbaint_t)pccb->pdata[1] << 16) |
365 ((lbaint_t)pccb->pdata[2] << 8) |
366 ((lbaint_t)pccb->pdata[3]);
368 if (*capacity != 0xffffffff) {
369 /* Read capacity (10) was sufficient for this drive. */
370 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
371 ((unsigned long)pccb->pdata[5] << 16) |
372 ((unsigned long)pccb->pdata[6] << 8) |
373 ((unsigned long)pccb->pdata[7]);
377 /* Read capacity (10) was insufficient. Use read capacity (16). */
378 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
379 pccb->cmd[0] = SCSI_RD_CAPAC16;
382 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
385 if (scsi_exec(pccb) != true)
388 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
389 ((uint64_t)pccb->pdata[1] << 48) |
390 ((uint64_t)pccb->pdata[2] << 40) |
391 ((uint64_t)pccb->pdata[3] << 32) |
392 ((uint64_t)pccb->pdata[4] << 24) |
393 ((uint64_t)pccb->pdata[5] << 16) |
394 ((uint64_t)pccb->pdata[6] << 8) |
395 ((uint64_t)pccb->pdata[7]);
397 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
398 ((uint64_t)pccb->pdata[9] << 48) |
399 ((uint64_t)pccb->pdata[10] << 40) |
400 ((uint64_t)pccb->pdata[11] << 32) |
401 ((uint64_t)pccb->pdata[12] << 24) |
402 ((uint64_t)pccb->pdata[13] << 16) |
403 ((uint64_t)pccb->pdata[14] << 8) |
404 ((uint64_t)pccb->pdata[15]);
411 * Some setup (fill-in) routines
413 static void scsi_setup_test_unit_ready(ccb *pccb)
415 pccb->cmd[0] = SCSI_TST_U_RDY;
416 pccb->cmd[1] = pccb->lun << 5;
422 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
426 * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
428 * @dev_desc: Block device description pointer
430 static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
432 dev_desc->target = 0xff;
433 dev_desc->lun = 0xff;
434 dev_desc->log2blksz =
435 LOG2_INVALID(typeof(dev_desc->log2blksz));
436 dev_desc->type = DEV_TYPE_UNKNOWN;
437 dev_desc->vendor[0] = 0;
438 dev_desc->product[0] = 0;
439 dev_desc->revision[0] = 0;
440 dev_desc->removable = false;
442 dev_desc->block_read = scsi_read;
443 dev_desc->block_write = scsi_write;
448 * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties
450 * @dev_desc: Block device description pointer
451 * @devnum: Device number
453 static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
457 dev_desc->if_type = IF_TYPE_SCSI;
458 dev_desc->devnum = devnum;
459 dev_desc->part_type = PART_TYPE_UNKNOWN;
461 scsi_init_dev_desc_priv(dev_desc);
465 * scsi_detect_dev - Detect scsi device
468 * @dev_desc: block device description
470 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
471 * detected. The LUN number is taken from the struct blk_desc *dev_desc.
473 * Return: 0 on success, error value otherwise
475 static int scsi_detect_dev(int target, struct blk_desc *dev_desc)
477 unsigned char perq, modi;
480 ccb *pccb = (ccb *)&tempccb;
482 pccb->target = target;
483 pccb->lun = dev_desc->lun;
484 pccb->pdata = (unsigned char *)&tempbuff;
486 scsi_setup_inquiry(pccb);
487 if (scsi_exec(pccb) != true) {
488 if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
490 * selection timeout => assuming no
493 debug("Selection timeout ID %d\n",
497 scsi_print_error(pccb);
502 if ((perq & 0x1f) == 0x1f)
503 return -ENODEV; /* skip unknown devices */
504 if ((modi & 0x80) == 0x80) /* drive is removable */
505 dev_desc->removable = true;
506 /* get info for this device */
507 scsi_ident_cpy((unsigned char *)dev_desc->vendor,
509 scsi_ident_cpy((unsigned char *)dev_desc->product,
511 scsi_ident_cpy((unsigned char *)dev_desc->revision,
513 dev_desc->target = pccb->target;
514 dev_desc->lun = pccb->lun;
517 scsi_setup_test_unit_ready(pccb);
518 if (scsi_exec(pccb) != true) {
519 if (dev_desc->removable) {
520 dev_desc->type = perq;
523 scsi_print_error(pccb);
526 if (scsi_read_capacity(pccb, &capacity, &blksz)) {
527 scsi_print_error(pccb);
530 dev_desc->lba = capacity;
531 dev_desc->blksz = blksz;
532 dev_desc->log2blksz = LOG2(dev_desc->blksz);
533 dev_desc->type = perq;
534 part_init(&dev_desc[0]);
540 * (re)-scan the scsi bus and reports scsi device info
541 * to the user if mode = 1
543 int scsi_scan(int mode)
545 unsigned char i, lun;
549 printf("scanning bus for devices...\n");
550 for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++)
551 scsi_init_dev_desc(&scsi_dev_desc[i], i);
554 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
555 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
556 scsi_dev_desc[scsi_max_devs].lun = lun;
557 ret = scsi_detect_dev(i, &scsi_dev_desc[scsi_max_devs]);
562 printf(" Device %d: ", 0);
563 dev_print(&scsi_dev_desc[scsi_max_devs]);
568 if (scsi_max_devs > 0)
573 printf("Found %d device(s).\n", scsi_max_devs);
574 #ifndef CONFIG_SPL_BUILD
575 setenv_ulong("scsidevs", scsi_max_devs);
581 static const struct blk_ops scsi_blk_ops = {
586 U_BOOT_DRIVER(scsi_blk) = {
589 .ops = &scsi_blk_ops,
592 U_BOOT_LEGACY_BLK(scsi) = {
593 .if_typename = "scsi",
594 .if_type = IF_TYPE_SCSI,
595 .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE,
596 .desc = scsi_dev_desc,