]> git.ipfire.org Git - people/ms/u-boot.git/blob - common/cmd_scsi.c
Merge git://www.denx.de/git/u-boot
[people/ms/u-boot.git] / common / cmd_scsi.c
1 /*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 *
23 *
24 *
25 */
26
27 /*
28 * SCSI support.
29 */
30 #include <common.h>
31 #include <command.h>
32 #include <asm/processor.h>
33 #include <scsi.h>
34 #include <image.h>
35 #include <pci.h>
36
37 #if defined(CONFIG_CMD_SCSI)
38
39 #ifdef CONFIG_SCSI_SYM53C8XX
40 #define SCSI_VEND_ID 0x1000
41 #ifndef CONFIG_SCSI_DEV_ID
42 #define SCSI_DEV_ID 0x0001
43 #else
44 #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
45 #endif
46 #elif defined CONFIG_SATA_ULI5288
47
48 #define SCSI_VEND_ID 0x10b9
49 #define SCSI_DEV_ID 0x5288
50
51 #else
52 #error no scsi device defined
53 #endif
54
55
56 static ccb tempccb; /* temporary scsi command buffer */
57
58 static unsigned char tempbuff[512]; /* temporary data buffer */
59
60 static int scsi_max_devs; /* number of highest available scsi device */
61
62 static int scsi_curr_dev; /* current device */
63
64 static block_dev_desc_t scsi_dev_desc[CFG_SCSI_MAX_DEVICE];
65
66 /********************************************************************************
67 * forward declerations of some Setup Routines
68 */
69 void scsi_setup_test_unit_ready(ccb * pccb);
70 void scsi_setup_read_capacity(ccb * pccb);
71 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
72 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
73 void scsi_setup_inquiry(ccb * pccb);
74 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
75
76
77 ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer);
78
79
80 /*********************************************************************************
81 * (re)-scan the scsi bus and reports scsi device info
82 * to the user if mode = 1
83 */
84 void scsi_scan(int mode)
85 {
86 unsigned char i,perq,modi,lun;
87 unsigned long capacity,blksz;
88 ccb* pccb=(ccb *)&tempccb;
89
90 if(mode==1) {
91 printf("scanning bus for devices...\n");
92 }
93 for(i=0;i<CFG_SCSI_MAX_DEVICE;i++) {
94 scsi_dev_desc[i].target=0xff;
95 scsi_dev_desc[i].lun=0xff;
96 scsi_dev_desc[i].lba=0;
97 scsi_dev_desc[i].blksz=0;
98 scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN;
99 scsi_dev_desc[i].vendor[0]=0;
100 scsi_dev_desc[i].product[0]=0;
101 scsi_dev_desc[i].revision[0]=0;
102 scsi_dev_desc[i].removable=FALSE;
103 scsi_dev_desc[i].if_type=IF_TYPE_SCSI;
104 scsi_dev_desc[i].dev=i;
105 scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
106 scsi_dev_desc[i].block_read=scsi_read;
107 }
108 scsi_max_devs=0;
109 for(i=0;i<CFG_SCSI_MAX_SCSI_ID;i++) {
110 pccb->target=i;
111 for(lun=0;lun<CFG_SCSI_MAX_LUN;lun++) {
112 pccb->lun=lun;
113 pccb->pdata=(unsigned char *)&tempbuff;
114 pccb->datalen=512;
115 scsi_setup_inquiry(pccb);
116 if(scsi_exec(pccb)!=TRUE) {
117 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
118 debug ("Selection timeout ID %d\n",pccb->target);
119 continue; /* selection timeout => assuming no device present */
120 }
121 scsi_print_error(pccb);
122 continue;
123 }
124 perq=tempbuff[0];
125 modi=tempbuff[1];
126 if((perq & 0x1f)==0x1f) {
127 continue; /* skip unknown devices */
128 }
129 if((modi&0x80)==0x80) /* drive is removable */
130 scsi_dev_desc[scsi_max_devs].removable=TRUE;
131 /* get info for this device */
132 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
133 &tempbuff[8], 8);
134 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
135 &tempbuff[16], 16);
136 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
137 &tempbuff[32], 4);
138 scsi_dev_desc[scsi_max_devs].target=pccb->target;
139 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
140
141 pccb->datalen=0;
142 scsi_setup_test_unit_ready(pccb);
143 if(scsi_exec(pccb)!=TRUE) {
144 if(scsi_dev_desc[scsi_max_devs].removable==TRUE) {
145 scsi_dev_desc[scsi_max_devs].type=perq;
146 goto removable;
147 }
148 scsi_print_error(pccb);
149 continue;
150 }
151 pccb->datalen=8;
152 scsi_setup_read_capacity(pccb);
153 if(scsi_exec(pccb)!=TRUE) {
154 scsi_print_error(pccb);
155 continue;
156 }
157 capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)|
158 ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]);
159 blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)|
160 ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]);
161 scsi_dev_desc[scsi_max_devs].lba=capacity;
162 scsi_dev_desc[scsi_max_devs].blksz=blksz;
163 scsi_dev_desc[scsi_max_devs].type=perq;
164 init_part(&scsi_dev_desc[scsi_max_devs]);
165 removable:
166 if(mode==1) {
167 printf (" Device %d: ", scsi_max_devs);
168 dev_print(&scsi_dev_desc[scsi_max_devs]);
169 } /* if mode */
170 scsi_max_devs++;
171 } /* next LUN */
172 }
173 if(scsi_max_devs>0)
174 scsi_curr_dev=0;
175 else
176 scsi_curr_dev=-1;
177 }
178
179
180 void scsi_init(void)
181 {
182 int busdevfunc;
183
184 busdevfunc=pci_find_device(SCSI_VEND_ID,SCSI_DEV_ID,0); /* get PCI Device ID */
185 if(busdevfunc==-1) {
186 printf("Error SCSI Controller (%04X,%04X) not found\n",SCSI_VEND_ID,SCSI_DEV_ID);
187 return;
188 }
189 #ifdef DEBUG
190 else {
191 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",SCSI_VEND_ID,SCSI_DEV_ID,(busdevfunc>>16)&0xFF,(busdevfunc>>11)&0x1F,(busdevfunc>>8)&0x7);
192 }
193 #endif
194 scsi_low_level_init(busdevfunc);
195 scsi_scan(1);
196 }
197
198 block_dev_desc_t * scsi_get_dev(int dev)
199 {
200 return (dev < CFG_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
201 }
202
203
204 /******************************************************************************
205 * scsi boot command intepreter. Derived from diskboot
206 */
207 int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
208 {
209 char *boot_device = NULL;
210 char *ep;
211 int dev, part = 0;
212 ulong addr, cnt, checksum;
213 disk_partition_t info;
214 image_header_t *hdr;
215 int rcode = 0;
216
217 switch (argc) {
218 case 1:
219 addr = CFG_LOAD_ADDR;
220 boot_device = getenv ("bootdevice");
221 break;
222 case 2:
223 addr = simple_strtoul(argv[1], NULL, 16);
224 boot_device = getenv ("bootdevice");
225 break;
226 case 3:
227 addr = simple_strtoul(argv[1], NULL, 16);
228 boot_device = argv[2];
229 break;
230 default:
231 printf ("Usage:\n%s\n", cmdtp->usage);
232 return 1;
233 }
234
235 if (!boot_device) {
236 puts ("\n** No boot device **\n");
237 return 1;
238 }
239
240 dev = simple_strtoul(boot_device, &ep, 16);
241 printf("booting from dev %d\n",dev);
242 if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
243 printf ("\n** Device %d not available\n", dev);
244 return 1;
245 }
246
247 if (*ep) {
248 if (*ep != ':') {
249 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
250 return 1;
251 }
252 part = simple_strtoul(++ep, NULL, 16);
253 }
254 if (get_partition_info (&scsi_dev_desc[dev], part, &info)) {
255 printf("error reading partinfo\n");
256 return 1;
257 }
258 if ((strncmp((char *)(info.type), BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
259 (strncmp((char *)(info.type), BOOT_PART_COMP, sizeof(info.type)) != 0)) {
260 printf ("\n** Invalid partition type \"%.32s\""
261 " (expect \"" BOOT_PART_TYPE "\")\n",
262 info.type);
263 return 1;
264 }
265
266 printf ("\nLoading from SCSI device %d, partition %d: "
267 "Name: %.32s Type: %.32s\n",
268 dev, part, info.name, info.type);
269
270 debug ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
271 info.start, info.size, info.blksz);
272
273 if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
274 printf ("** Read error on %d:%d\n", dev, part);
275 return 1;
276 }
277
278 hdr = (image_header_t *)addr;
279
280 if (ntohl(hdr->ih_magic) == IH_MAGIC) {
281 printf("\n** Bad Magic Number **\n");
282 return 1;
283 }
284
285 checksum = ntohl(hdr->ih_hcrc);
286 hdr->ih_hcrc = 0;
287
288 if (crc32 (0, (uchar *)hdr, sizeof(image_header_t)) != checksum) {
289 puts ("\n** Bad Header Checksum **\n");
290 return 1;
291 }
292 hdr->ih_hcrc = htonl(checksum); /* restore checksum for later use */
293
294 print_image_hdr (hdr);
295 cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
296 cnt += info.blksz - 1;
297 cnt /= info.blksz;
298 cnt -= 1;
299
300 if (scsi_read (dev, info.start+1, cnt,
301 (ulong *)(addr+info.blksz)) != cnt) {
302 printf ("** Read error on %d:%d\n", dev, part);
303 return 1;
304 }
305 /* Loading ok, update default load address */
306 load_addr = addr;
307
308 flush_cache (addr, (cnt+1)*info.blksz);
309
310 /* Check if we should attempt an auto-start */
311 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
312 char *local_args[2];
313 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
314 local_args[0] = argv[0];
315 local_args[1] = NULL;
316 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
317 rcode = do_bootm (cmdtp, 0, 1, local_args);
318 }
319 return rcode;
320 }
321
322 /*********************************************************************************
323 * scsi command intepreter
324 */
325 int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
326 {
327 switch (argc) {
328 case 0:
329 case 1: printf ("Usage:\n%s\n", cmdtp->usage); return 1;
330 case 2:
331 if (strncmp(argv[1],"res",3) == 0) {
332 printf("\nReset SCSI\n");
333 scsi_bus_reset();
334 scsi_scan(1);
335 return 0;
336 }
337 if (strncmp(argv[1],"inf",3) == 0) {
338 int i;
339 for (i=0; i<CFG_SCSI_MAX_DEVICE; ++i) {
340 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
341 continue; /* list only known devices */
342 printf ("SCSI dev. %d: ", i);
343 dev_print(&scsi_dev_desc[i]);
344 }
345 return 0;
346 }
347 if (strncmp(argv[1],"dev",3) == 0) {
348 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CFG_SCSI_MAX_DEVICE)) {
349 printf("\nno SCSI devices available\n");
350 return 1;
351 }
352 printf ("\n Device %d: ", scsi_curr_dev);
353 dev_print(&scsi_dev_desc[scsi_curr_dev]);
354 return 0;
355 }
356 if (strncmp(argv[1],"scan",4) == 0) {
357 scsi_scan(1);
358 return 0;
359 }
360 if (strncmp(argv[1],"part",4) == 0) {
361 int dev, ok;
362 for (ok=0, dev=0; dev<CFG_SCSI_MAX_DEVICE; ++dev) {
363 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
364 ok++;
365 if (dev)
366 printf("\n");
367 debug ("print_part of %x\n",dev);
368 print_part(&scsi_dev_desc[dev]);
369 }
370 }
371 if (!ok)
372 printf("\nno SCSI devices available\n");
373 return 1;
374 }
375 printf ("Usage:\n%s\n", cmdtp->usage);
376 return 1;
377 case 3:
378 if (strncmp(argv[1],"dev",3) == 0) {
379 int dev = (int)simple_strtoul(argv[2], NULL, 10);
380 printf ("\nSCSI device %d: ", dev);
381 if (dev >= CFG_SCSI_MAX_DEVICE) {
382 printf("unknown device\n");
383 return 1;
384 }
385 printf ("\n Device %d: ", dev);
386 dev_print(&scsi_dev_desc[dev]);
387 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
388 return 1;
389 }
390 scsi_curr_dev = dev;
391 printf("... is now current device\n");
392 return 0;
393 }
394 if (strncmp(argv[1],"part",4) == 0) {
395 int dev = (int)simple_strtoul(argv[2], NULL, 10);
396 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
397 print_part(&scsi_dev_desc[dev]);
398 }
399 else {
400 printf ("\nSCSI device %d not available\n", dev);
401 }
402 return 1;
403 }
404 printf ("Usage:\n%s\n", cmdtp->usage);
405 return 1;
406 default:
407 /* at least 4 args */
408 if (strcmp(argv[1],"read") == 0) {
409 ulong addr = simple_strtoul(argv[2], NULL, 16);
410 ulong blk = simple_strtoul(argv[3], NULL, 16);
411 ulong cnt = simple_strtoul(argv[4], NULL, 16);
412 ulong n;
413 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
414 scsi_curr_dev, blk, cnt);
415 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
416 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
417 return 0;
418 }
419 } /* switch */
420 printf ("Usage:\n%s\n", cmdtp->usage);
421 return 1;
422 }
423
424 /****************************************************************************************
425 * scsi_read
426 */
427
428 #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
429
430 ulong scsi_read(int device, ulong blknr, ulong blkcnt, void *buffer)
431 {
432 ulong start,blks, buf_addr;
433 unsigned short smallblks;
434 ccb* pccb=(ccb *)&tempccb;
435 device&=0xff;
436 /* Setup device
437 */
438 pccb->target=scsi_dev_desc[device].target;
439 pccb->lun=scsi_dev_desc[device].lun;
440 buf_addr=(unsigned long)buffer;
441 start=blknr;
442 blks=blkcnt;
443 debug ("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
444 do {
445 pccb->pdata=(unsigned char *)buf_addr;
446 if(blks>SCSI_MAX_READ_BLK) {
447 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
448 smallblks=SCSI_MAX_READ_BLK;
449 scsi_setup_read_ext(pccb,start,smallblks);
450 start+=SCSI_MAX_READ_BLK;
451 blks-=SCSI_MAX_READ_BLK;
452 }
453 else {
454 pccb->datalen=scsi_dev_desc[device].blksz * blks;
455 smallblks=(unsigned short) blks;
456 scsi_setup_read_ext(pccb,start,smallblks);
457 start+=blks;
458 blks=0;
459 }
460 debug ("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
461 if(scsi_exec(pccb)!=TRUE) {
462 scsi_print_error(pccb);
463 blkcnt-=blks;
464 break;
465 }
466 buf_addr+=pccb->datalen;
467 } while(blks!=0);
468 debug ("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
469 return(blkcnt);
470 }
471
472 /* copy src to dest, skipping leading and trailing blanks
473 * and null terminate the string
474 */
475 void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
476 {
477 int start,end;
478
479 start=0;
480 while(start<len) {
481 if(src[start]!=' ')
482 break;
483 start++;
484 }
485 end=len-1;
486 while(end>start) {
487 if(src[end]!=' ')
488 break;
489 end--;
490 }
491 for( ; start<=end; start++) {
492 *dest++=src[start];
493 }
494 *dest='\0';
495 }
496
497
498 /* Trim trailing blanks, and NUL-terminate string
499 */
500 void scsi_trim_trail (unsigned char *str, unsigned int len)
501 {
502 unsigned char *p = str + len - 1;
503
504 while (len-- > 0) {
505 *p-- = '\0';
506 if (*p != ' ') {
507 return;
508 }
509 }
510 }
511
512
513 /************************************************************************************
514 * Some setup (fill-in) routines
515 */
516 void scsi_setup_test_unit_ready(ccb * pccb)
517 {
518 pccb->cmd[0]=SCSI_TST_U_RDY;
519 pccb->cmd[1]=pccb->lun<<5;
520 pccb->cmd[2]=0;
521 pccb->cmd[3]=0;
522 pccb->cmd[4]=0;
523 pccb->cmd[5]=0;
524 pccb->cmdlen=6;
525 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
526 }
527
528 void scsi_setup_read_capacity(ccb * pccb)
529 {
530 pccb->cmd[0]=SCSI_RD_CAPAC;
531 pccb->cmd[1]=pccb->lun<<5;
532 pccb->cmd[2]=0;
533 pccb->cmd[3]=0;
534 pccb->cmd[4]=0;
535 pccb->cmd[5]=0;
536 pccb->cmd[6]=0;
537 pccb->cmd[7]=0;
538 pccb->cmd[8]=0;
539 pccb->cmd[9]=0;
540 pccb->cmdlen=10;
541 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
542
543 }
544
545 void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
546 {
547 pccb->cmd[0]=SCSI_READ10;
548 pccb->cmd[1]=pccb->lun<<5;
549 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
550 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
551 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
552 pccb->cmd[5]=((unsigned char) (start))&0xff;
553 pccb->cmd[6]=0;
554 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
555 pccb->cmd[8]=(unsigned char) blocks & 0xff;
556 pccb->cmd[6]=0;
557 pccb->cmdlen=10;
558 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
559 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
560 pccb->cmd[0],pccb->cmd[1],
561 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
562 pccb->cmd[7],pccb->cmd[8]);
563 }
564
565 void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
566 {
567 pccb->cmd[0]=SCSI_READ6;
568 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
569 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
570 pccb->cmd[3]=((unsigned char) (start))&0xff;
571 pccb->cmd[4]=(unsigned char) blocks & 0xff;
572 pccb->cmd[5]=0;
573 pccb->cmdlen=6;
574 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
575 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
576 pccb->cmd[0],pccb->cmd[1],
577 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
578 }
579
580
581 void scsi_setup_inquiry(ccb * pccb)
582 {
583 pccb->cmd[0]=SCSI_INQUIRY;
584 pccb->cmd[1]=pccb->lun<<5;
585 pccb->cmd[2]=0;
586 pccb->cmd[3]=0;
587 if(pccb->datalen>255)
588 pccb->cmd[4]=255;
589 else
590 pccb->cmd[4]=(unsigned char)pccb->datalen;
591 pccb->cmd[5]=0;
592 pccb->cmdlen=6;
593 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
594 }
595
596
597 U_BOOT_CMD(
598 scsi, 5, 1, do_scsi,
599 "scsi - SCSI sub-system\n",
600 "reset - reset SCSI controller\n"
601 "scsi info - show available SCSI devices\n"
602 "scsi scan - (re-)scan SCSI bus\n"
603 "scsi device [dev] - show or set current device\n"
604 "scsi part [dev] - print partition table of one or all SCSI devices\n"
605 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
606 " to memory address `addr'\n"
607 );
608
609 U_BOOT_CMD(
610 scsiboot, 3, 1, do_scsiboot,
611 "scsiboot- boot from SCSI device\n",
612 "loadAddr dev:part\n"
613 );
614
615 #endif