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