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