]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_scsi.c
* LWMON extensions:
[people/ms/u-boot.git] / common / cmd_scsi.c
CommitLineData
e887afc9
WD
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
31#include <common.h>
32#include <command.h>
33#include <cmd_boot.h>
34#include <asm/processor.h>
35#include <scsi.h>
36#include <image.h>
37#include <cmd_disk.h>
38#include <pci.h>
39
40
41#undef SCSI_DEBUG
42
43#ifdef SCSI_DEBUG
44#define PRINTF(fmt,args...) printf (fmt ,##args)
45#else
46#define PRINTF(fmt,args...)
47#endif
48
49#if (CONFIG_COMMANDS & CFG_CMD_SCSI)
50
51#ifdef CONFIG_SCSI_SYM53C8XX
52#define SCSI_VEND_ID 0x1000
53#ifndef CONFIG_SCSI_DEV_ID
54#define SCSI_DEV_ID 0x0001
55#else
56#define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
57#endif
58#else
59#error CONFIG_SCSI_SYM53C8XX must be defined
60#endif
61
62
63static ccb tempccb; /* temporary scsi command buffer */
64
65static unsigned char tempbuff[512]; /* temporary data buffer */
66
67static int scsi_max_devs; /* number of highest available scsi device */
68
69static int scsi_curr_dev; /* current device */
70
71static block_dev_desc_t scsi_dev_desc[CFG_SCSI_MAX_DEVICE];
72
73/********************************************************************************
74 * forward declerations of some Setup Routines
75 */
76void scsi_setup_test_unit_ready(ccb * pccb);
77void scsi_setup_read_capacity(ccb * pccb);
78void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
79void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
80void scsi_setup_inquiry(ccb * pccb);
81void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
82
83
84ulong scsi_read(int device, ulong blknr, ulong blkcnt, ulong *buffer);
85
86
87/*********************************************************************************
88 * (re)-scan the scsi bus and reports scsi device info
89 * to the user if mode = 1
90 */
91void scsi_scan(int mode)
92{
93 unsigned char i,perq,modi,lun;
94 unsigned long capacity,blksz;
95 ccb* pccb=(ccb *)&tempccb;
96
97 if(mode==1) {
98 printf("scanning bus for devices...\n");
99 }
100 for(i=0;i<CFG_SCSI_MAX_DEVICE;i++) {
101 scsi_dev_desc[i].target=0xff;
102 scsi_dev_desc[i].lun=0xff;
103 scsi_dev_desc[i].lba=0;
104 scsi_dev_desc[i].blksz=0;
105 scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN;
106 scsi_dev_desc[i].vendor[0]=0;
107 scsi_dev_desc[i].product[0]=0;
108 scsi_dev_desc[i].revision[0]=0;
109 scsi_dev_desc[i].removable=FALSE;
110 scsi_dev_desc[i].if_type=IF_TYPE_SCSI;
111 scsi_dev_desc[i].dev=i;
112 scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
113 scsi_dev_desc[i].block_read=scsi_read;
114 }
115 scsi_max_devs=0;
116 for(i=0;i<CFG_SCSI_MAX_SCSI_ID;i++) {
117 pccb->target=i;
118 for(lun=0;lun<CFG_SCSI_MAX_LUN;lun++) {
119 pccb->lun=lun;
120 pccb->pdata=(unsigned char *)&tempbuff;
121 pccb->datalen=512;
122 scsi_setup_inquiry(pccb);
123 if(scsi_exec(pccb)!=TRUE) {
124 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
125 PRINTF("Selection timeout ID %d\n",pccb->target);
126 continue; /* selection timeout => assuming no device present */
127 }
128 scsi_print_error(pccb);
129 continue;
130 }
131 perq=tempbuff[0];
132 modi=tempbuff[1];
133 if((perq & 0x1f)==0x1f) {
134 continue; /* skip unknown devices */
135 }
136 if((modi&0x80)==0x80) /* drive is removable */
137 scsi_dev_desc[scsi_max_devs].removable=TRUE;
138 /* get info for this device */
139 scsi_ident_cpy(&scsi_dev_desc[scsi_max_devs].vendor[0],&tempbuff[8],8);
140 scsi_ident_cpy(&scsi_dev_desc[scsi_max_devs].product[0],&tempbuff[16],16);
141 scsi_ident_cpy(&scsi_dev_desc[scsi_max_devs].revision[0],&tempbuff[32],4);
142 scsi_dev_desc[scsi_max_devs].target=pccb->target;
143 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
144
145 pccb->datalen=0;
146 scsi_setup_test_unit_ready(pccb);
147 if(scsi_exec(pccb)!=TRUE) {
148 if(scsi_dev_desc[scsi_max_devs].removable==TRUE) {
149 scsi_dev_desc[scsi_max_devs].type=perq;
150 goto removable;
151 }
152 scsi_print_error(pccb);
153 continue;
154 }
155 pccb->datalen=8;
156 scsi_setup_read_capacity(pccb);
157 if(scsi_exec(pccb)!=TRUE) {
158 scsi_print_error(pccb);
159 continue;
160 }
161 capacity=((unsigned long)tempbuff[0]<<24)|((unsigned long)tempbuff[1]<<16)|
162 ((unsigned long)tempbuff[2]<<8)|((unsigned long)tempbuff[3]);
163 blksz=((unsigned long)tempbuff[4]<<24)|((unsigned long)tempbuff[5]<<16)|
164 ((unsigned long)tempbuff[6]<<8)|((unsigned long)tempbuff[7]);
165 scsi_dev_desc[scsi_max_devs].lba=capacity;
166 scsi_dev_desc[scsi_max_devs].blksz=blksz;
167 scsi_dev_desc[scsi_max_devs].type=perq;
168 init_part(&scsi_dev_desc[scsi_max_devs]);
169removable:
170 if(mode==1) {
171 printf (" Device %d: ", scsi_max_devs);
172 dev_print(&scsi_dev_desc[scsi_max_devs]);
173 } /* if mode */
174 scsi_max_devs++;
175 } /* next LUN */
176 }
177 if(scsi_max_devs>0)
178 scsi_curr_dev=0;
179 else
180 scsi_curr_dev=-1;
181}
182
183
184
185void scsi_init(void)
186{
187 int busdevfunc;
188
189 busdevfunc=pci_find_device(SCSI_VEND_ID,SCSI_DEV_ID,0); /* get PCI Device ID */
190 if(busdevfunc==-1) {
191 printf("Error SCSI Controller (%04X,%04X) not found\n",SCSI_VEND_ID,SCSI_DEV_ID);
192 return;
193 }
194#ifdef DEBUG
195 else {
196 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);
197 }
198#endif
199 scsi_low_level_init(busdevfunc);
200 scsi_scan(1);
201}
202
203block_dev_desc_t * scsi_get_dev(int dev)
204{
205 return((block_dev_desc_t *)&scsi_dev_desc[dev]);
206}
207
208
209
210/******************************************************************************
211 * scsi boot command intepreter. Derived from diskboot
212 */
213int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
214{
215 char *boot_device = NULL;
216 char *ep;
217 int dev, part = 0;
218 ulong cnt;
219 ulong addr;
220 disk_partition_t info;
221 image_header_t *hdr;
222 int rcode = 0;
223
224 switch (argc) {
225 case 1:
226 addr = CFG_LOAD_ADDR;
227 boot_device = getenv ("bootdevice");
228 break;
229 case 2:
230 addr = simple_strtoul(argv[1], NULL, 16);
231 boot_device = getenv ("bootdevice");
232 break;
233 case 3:
234 addr = simple_strtoul(argv[1], NULL, 16);
235 boot_device = argv[2];
236 break;
237 default:
238 printf ("Usage:\n%s\n", cmdtp->usage);
239 return 1;
240 }
241
242 if (!boot_device) {
243 puts ("\n** No boot device **\n");
244 return 1;
245 }
246
247 dev = simple_strtoul(boot_device, &ep, 16);
248 printf("booting from dev %d\n",dev);
249 if (scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
250 printf ("\n** Device %d not available\n", dev);
251 return 1;
252 }
253
254 if (*ep) {
255 if (*ep != ':') {
256 puts ("\n** Invalid boot device, use `dev[:part]' **\n");
257 return 1;
258 }
259 part = simple_strtoul(++ep, NULL, 16);
260 }
261 if (get_partition_info (&scsi_dev_desc[dev], part, &info)) {
262 printf("error reading partinfo\n");
263 return 1;
264 }
4532cb69
WD
265 if ((strncmp(info.type, BOOT_PART_TYPE, sizeof(info.type)) != 0) &&
266 (strncmp(info.type, BOOT_PART_COMP, sizeof(info.type)) != 0)) {
e887afc9
WD
267 printf ("\n** Invalid partition type \"%.32s\""
268 " (expect \"" BOOT_PART_TYPE "\")\n",
269 info.type);
270 return 1;
271 }
272
273 printf ("\nLoading from SCSI device %d, partition %d: "
274 "Name: %.32s Type: %.32s\n",
275 dev, part, info.name, info.type);
276
277 PRINTF ("First Block: %ld, # of blocks: %ld, Block Size: %ld\n",
278 info.start, info.size, info.blksz);
279
280 if (scsi_read (dev, info.start, 1, (ulong *)addr) != 1) {
281 printf ("** Read error on %d:%d\n", dev, part);
282 return 1;
283 }
284
285 hdr = (image_header_t *)addr;
286
287 if (hdr->ih_magic == IH_MAGIC) {
288
289 print_image_hdr (hdr);
290 cnt = (hdr->ih_size + sizeof(image_header_t));
291 cnt += info.blksz - 1;
292 cnt /= info.blksz;
293 cnt -= 1;
294 } else {
295 printf("\n** Bad Magic Number **\n");
296 return 1;
297 }
298
299 if (scsi_read (dev, info.start+1, cnt,
300 (ulong *)(addr+info.blksz)) != cnt) {
301 printf ("** Read error on %d:%d\n", dev, part);
302 return 1;
303 }
304 /* Loading ok, update default load address */
305 load_addr = addr;
306
307 flush_cache (addr, (cnt+1)*info.blksz);
308
309 /* Check if we should attempt an auto-start */
310 if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
311 char *local_args[2];
312 extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
313 local_args[0] = argv[0];
314 local_args[1] = NULL;
315 printf ("Automatic boot of image at addr 0x%08lX ...\n", addr);
316 rcode = do_bootm (cmdtp, 0, 1, local_args);
317 }
318 return rcode;
319}
320
321/*********************************************************************************
322 * scsi command intepreter
323 */
324int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
325{
326 switch (argc) {
327 case 0:
328 case 1: printf ("Usage:\n%s\n", cmdtp->usage); return 1;
329 case 2:
330 if (strncmp(argv[1],"res",3) == 0) {
331 printf("\nReset SCSI\n");
332 scsi_bus_reset();
333 scsi_scan(1);
334 return 0;
335 }
336 if (strncmp(argv[1],"inf",3) == 0) {
337 int i;
338 for (i=0; i<CFG_SCSI_MAX_DEVICE; ++i) {
339 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
340 continue; /* list only known devices */
341 printf ("SCSI dev. %d: ", i);
342 dev_print(&scsi_dev_desc[i]);
343 }
344 return 0;
345 }
346 if (strncmp(argv[1],"dev",3) == 0) {
347 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CFG_SCSI_MAX_DEVICE)) {
348 printf("\nno SCSI devices available\n");
349 return 1;
350 }
351 printf ("\n Device %d: ", scsi_curr_dev);
352 dev_print(&scsi_dev_desc[scsi_curr_dev]);
353 return 0;
354 }
355 if (strncmp(argv[1],"scan",4) == 0) {
356 scsi_scan(1);
357 return 0;
358 }
359 if (strncmp(argv[1],"part",4) == 0) {
360 int dev, ok;
361 for (ok=0, dev=0; dev<CFG_SCSI_MAX_DEVICE; ++dev) {
362 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
363 ok++;
364 if (dev)
365 printf("\n");
366 PRINTF("print_part of %x\n",dev);
367 print_part(&scsi_dev_desc[dev]);
368 }
369 }
370 if (!ok)
371 printf("\nno SCSI devices available\n");
372 return 1;
373 }
374 printf ("Usage:\n%s\n", cmdtp->usage);
375 return 1;
376 case 3:
377 if (strncmp(argv[1],"dev",3) == 0) {
378 int dev = (int)simple_strtoul(argv[2], NULL, 10);
379 printf ("\nSCSI device %d: ", dev);
380 if (dev >= CFG_SCSI_MAX_DEVICE) {
381 printf("unknown device\n");
382 return 1;
383 }
384 printf ("\n Device %d: ", dev);
385 dev_print(&scsi_dev_desc[dev]);
386 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
387 return 1;
388 }
389 scsi_curr_dev = dev;
390 printf("... is now current device\n");
391 return 0;
392 }
393 if (strncmp(argv[1],"part",4) == 0) {
394 int dev = (int)simple_strtoul(argv[2], NULL, 10);
395 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
396 print_part(&scsi_dev_desc[dev]);
397 }
398 else {
399 printf ("\nSCSI device %d not available\n", dev);
400 }
401 return 1;
402 }
403 printf ("Usage:\n%s\n", cmdtp->usage);
404 return 1;
405 default:
406 /* at least 4 args */
407 if (strcmp(argv[1],"read") == 0) {
408 ulong addr = simple_strtoul(argv[2], NULL, 16);
409 ulong blk = simple_strtoul(argv[3], NULL, 16);
410 ulong cnt = simple_strtoul(argv[4], NULL, 16);
411 ulong n;
412 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
413 scsi_curr_dev, blk, cnt);
414 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
415 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
416 return 0;
417 }
418 } /* switch */
419 printf ("Usage:\n%s\n", cmdtp->usage);
420 return 1;
421}
422
423/****************************************************************************************
424 * scsi_read
425 */
426
427#define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
428
429ulong scsi_read(int device, ulong blknr, ulong blkcnt, ulong *buffer)
430{
431 ulong start,blks, buf_addr;
432 unsigned short smallblks;
433 ccb* pccb=(ccb *)&tempccb;
434 device&=0xff;
435 /* Setup device
436 */
437 pccb->target=scsi_dev_desc[device].target;
438 pccb->lun=scsi_dev_desc[device].lun;
439 buf_addr=(unsigned long)buffer;
440 start=blknr;
441 blks=blkcnt;
442 PRINTF("\nscsi_read: dev %d startblk %lx, blccnt %lx buffer %lx\n",device,start,blks,(unsigned long)buffer);
443 do {
444 pccb->pdata=(unsigned char *)buf_addr;
445 if(blks>SCSI_MAX_READ_BLK) {
446 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
447 smallblks=SCSI_MAX_READ_BLK;
448 scsi_setup_read_ext(pccb,start,smallblks);
449 start+=SCSI_MAX_READ_BLK;
450 blks-=SCSI_MAX_READ_BLK;
451 }
452 else {
453 pccb->datalen=scsi_dev_desc[device].blksz * blks;
454 smallblks=(unsigned short) blks;
455 scsi_setup_read_ext(pccb,start,smallblks);
456 start+=blks;
457 blks=0;
458 }
459 PRINTF("scsi_read_ext: startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
460 if(scsi_exec(pccb)!=TRUE) {
461 scsi_print_error(pccb);
462 blkcnt-=blks;
463 break;
464 }
465 buf_addr+=pccb->datalen;
466 } while(blks!=0);
467 PRINTF("scsi_read_ext: end startblk %lx, blccnt %x buffer %lx\n",start,smallblks,buf_addr);
468 return(blkcnt);
469}
470
471/* copy src to dest, skipping leading and trailing blanks
472 * and null terminate the string
473 */
474void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
475{
476 int start,end;
477
478 start=0;
479 while(start<len) {
480 if(src[start]!=' ')
481 break;
482 start++;
483 }
484 end=len-1;
485 while(end>start) {
486 if(src[end]!=' ')
487 break;
488 end--;
489 }
490 for( ; start<=end; start++) {
491 *dest++=src[start];
492 }
493 *dest='\0';
494}
495
496
497
498/* Trim trailing blanks, and NUL-terminate string
499 */
500void 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 */
516void 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
528void 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
545void 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 PRINTF("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
565void 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 PRINTF("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
581void 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#endif /* #if (CONFIG_COMMANDS & CFG_CMD_SCSI) */
597
598