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