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