blob: 7b97dc9332b4d275ee05eb97690102a1a95a4e3f [file] [log] [blame]
wdenke887afc2002-08-27 09:44:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenke887afc2002-08-27 09:44:07 +00006 */
7
8/*
9 * SCSI support.
10 */
wdenke887afc2002-08-27 09:44:07 +000011#include <common.h>
12#include <command.h>
wdenke887afc2002-08-27 09:44:07 +000013#include <asm/processor.h>
14#include <scsi.h>
15#include <image.h>
wdenke887afc2002-08-27 09:44:07 +000016#include <pci.h>
17
Vadim Bendebury4ae5eb72012-10-29 05:23:45 +000018#ifdef CONFIG_SCSI_DEV_LIST
19#define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
20#else
wdenke887afc2002-08-27 09:44:07 +000021#ifdef CONFIG_SCSI_SYM53C8XX
22#define SCSI_VEND_ID 0x1000
23#ifndef CONFIG_SCSI_DEV_ID
24#define SCSI_DEV_ID 0x0001
25#else
26#define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
27#endif
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080028#elif defined CONFIG_SATA_ULI5288
29
30#define SCSI_VEND_ID 0x10b9
31#define SCSI_DEV_ID 0x5288
32
Rob Herring942e3142011-07-06 16:13:36 +000033#elif !defined(CONFIG_SCSI_AHCI_PLAT)
Jin Zhengxiong4782ac82006-08-23 19:10:44 +080034#error no scsi device defined
wdenke887afc2002-08-27 09:44:07 +000035#endif
Vadim Bendebury4ae5eb72012-10-29 05:23:45 +000036#define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
37#endif
wdenke887afc2002-08-27 09:44:07 +000038
Vadim Bendebury4ae5eb72012-10-29 05:23:45 +000039#ifdef CONFIG_PCI
40const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
41#endif
wdenke887afc2002-08-27 09:44:07 +000042static ccb tempccb; /* temporary scsi command buffer */
43
44static unsigned char tempbuff[512]; /* temporary data buffer */
45
46static int scsi_max_devs; /* number of highest available scsi device */
47
48static int scsi_curr_dev; /* current device */
49
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020050static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
wdenke887afc2002-08-27 09:44:07 +000051
52/********************************************************************************
53 * forward declerations of some Setup Routines
54 */
55void scsi_setup_test_unit_ready(ccb * pccb);
wdenke887afc2002-08-27 09:44:07 +000056void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks);
57void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks);
Hung-Te Lin758c9e62012-10-29 05:23:46 +000058static void scsi_setup_write_ext(ccb *pccb, unsigned long start,
59 unsigned short blocks);
wdenke887afc2002-08-27 09:44:07 +000060void scsi_setup_inquiry(ccb * pccb);
61void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
62
63
Gabe Blackb4c5bbc2012-10-29 05:23:57 +000064static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity,
65 unsigned long *blksz);
Simon Glass4f6aa342013-07-03 07:11:41 -070066static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
67 void *buffer);
68static ulong scsi_write(int device, lbaint_t blknr,
Hung-Te Lin758c9e62012-10-29 05:23:46 +000069 lbaint_t blkcnt, const void *buffer);
wdenke887afc2002-08-27 09:44:07 +000070
71
72/*********************************************************************************
73 * (re)-scan the scsi bus and reports scsi device info
74 * to the user if mode = 1
75 */
76void scsi_scan(int mode)
77{
78 unsigned char i,perq,modi,lun;
Gabe Blackb4c5bbc2012-10-29 05:23:57 +000079 lbaint_t capacity;
80 unsigned long blksz;
wdenke887afc2002-08-27 09:44:07 +000081 ccb* pccb=(ccb *)&tempccb;
82
83 if(mode==1) {
84 printf("scanning bus for devices...\n");
85 }
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020086 for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) {
wdenke887afc2002-08-27 09:44:07 +000087 scsi_dev_desc[i].target=0xff;
88 scsi_dev_desc[i].lun=0xff;
89 scsi_dev_desc[i].lba=0;
90 scsi_dev_desc[i].blksz=0;
Egbert Eich0472fbf2013-04-09 21:11:56 +000091 scsi_dev_desc[i].log2blksz =
92 LOG2_INVALID(typeof(scsi_dev_desc[i].log2blksz));
wdenke887afc2002-08-27 09:44:07 +000093 scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN;
94 scsi_dev_desc[i].vendor[0]=0;
95 scsi_dev_desc[i].product[0]=0;
96 scsi_dev_desc[i].revision[0]=0;
York Sun472d5462013-04-01 11:29:11 -070097 scsi_dev_desc[i].removable = false;
wdenke887afc2002-08-27 09:44:07 +000098 scsi_dev_desc[i].if_type=IF_TYPE_SCSI;
99 scsi_dev_desc[i].dev=i;
100 scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN;
101 scsi_dev_desc[i].block_read=scsi_read;
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000102 scsi_dev_desc[i].block_write = scsi_write;
wdenke887afc2002-08-27 09:44:07 +0000103 }
104 scsi_max_devs=0;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200105 for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) {
wdenke887afc2002-08-27 09:44:07 +0000106 pccb->target=i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200107 for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) {
wdenke887afc2002-08-27 09:44:07 +0000108 pccb->lun=lun;
109 pccb->pdata=(unsigned char *)&tempbuff;
110 pccb->datalen=512;
111 scsi_setup_inquiry(pccb);
York Sun472d5462013-04-01 11:29:11 -0700112 if (scsi_exec(pccb) != true) {
wdenke887afc2002-08-27 09:44:07 +0000113 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
wdenk1a344f22005-02-03 23:00:49 +0000114 debug ("Selection timeout ID %d\n",pccb->target);
wdenke887afc2002-08-27 09:44:07 +0000115 continue; /* selection timeout => assuming no device present */
116 }
117 scsi_print_error(pccb);
118 continue;
119 }
120 perq=tempbuff[0];
121 modi=tempbuff[1];
122 if((perq & 0x1f)==0x1f) {
123 continue; /* skip unknown devices */
124 }
125 if((modi&0x80)==0x80) /* drive is removable */
York Sun472d5462013-04-01 11:29:11 -0700126 scsi_dev_desc[scsi_max_devs].removable=true;
wdenke887afc2002-08-27 09:44:07 +0000127 /* get info for this device */
Stefan Roese2309c132007-11-17 07:58:25 +0100128 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
129 &tempbuff[8], 8);
130 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
131 &tempbuff[16], 16);
132 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
133 &tempbuff[32], 4);
wdenke887afc2002-08-27 09:44:07 +0000134 scsi_dev_desc[scsi_max_devs].target=pccb->target;
135 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
136
137 pccb->datalen=0;
138 scsi_setup_test_unit_ready(pccb);
York Sun472d5462013-04-01 11:29:11 -0700139 if (scsi_exec(pccb) != true) {
140 if (scsi_dev_desc[scsi_max_devs].removable == true) {
wdenke887afc2002-08-27 09:44:07 +0000141 scsi_dev_desc[scsi_max_devs].type=perq;
142 goto removable;
143 }
144 scsi_print_error(pccb);
145 continue;
146 }
Gabe Blackb4c5bbc2012-10-29 05:23:57 +0000147 if (scsi_read_capacity(pccb, &capacity, &blksz)) {
wdenke887afc2002-08-27 09:44:07 +0000148 scsi_print_error(pccb);
149 continue;
150 }
wdenke887afc2002-08-27 09:44:07 +0000151 scsi_dev_desc[scsi_max_devs].lba=capacity;
152 scsi_dev_desc[scsi_max_devs].blksz=blksz;
Egbert Eich0472fbf2013-04-09 21:11:56 +0000153 scsi_dev_desc[scsi_max_devs].log2blksz =
154 LOG2(scsi_dev_desc[scsi_max_devs].blksz);
wdenke887afc2002-08-27 09:44:07 +0000155 scsi_dev_desc[scsi_max_devs].type=perq;
156 init_part(&scsi_dev_desc[scsi_max_devs]);
157removable:
158 if(mode==1) {
159 printf (" Device %d: ", scsi_max_devs);
160 dev_print(&scsi_dev_desc[scsi_max_devs]);
161 } /* if mode */
162 scsi_max_devs++;
163 } /* next LUN */
164 }
165 if(scsi_max_devs>0)
166 scsi_curr_dev=0;
167 else
Wolfgang Denkd0ff51b2008-07-14 15:19:07 +0200168 scsi_curr_dev = -1;
Stefan Reinauer447c0312012-10-29 05:23:48 +0000169
170 printf("Found %d device(s).\n", scsi_max_devs);
171 setenv_ulong("scsidevs", scsi_max_devs);
172}
173
174int scsi_get_disk_count(void)
175{
176 return scsi_max_devs;
wdenke887afc2002-08-27 09:44:07 +0000177}
178
Rob Herring942e3142011-07-06 16:13:36 +0000179#ifdef CONFIG_PCI
wdenke887afc2002-08-27 09:44:07 +0000180void scsi_init(void)
181{
182 int busdevfunc;
Vadim Bendebury4ae5eb72012-10-29 05:23:45 +0000183 int i;
184 /*
185 * Find a device from the list, this driver will support a single
186 * controller.
187 */
188 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
189 /* get PCI Device ID */
190 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
191 scsi_device_list[i].device,
192 0);
193 if (busdevfunc != -1)
194 break;
195 }
wdenke887afc2002-08-27 09:44:07 +0000196
Vadim Bendebury4ae5eb72012-10-29 05:23:45 +0000197 if (busdevfunc == -1) {
198 printf("Error: SCSI Controller(s) ");
199 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
200 printf("%04X:%04X ",
201 scsi_device_list[i].vendor,
202 scsi_device_list[i].device);
203 }
204 printf("not found\n");
wdenke887afc2002-08-27 09:44:07 +0000205 return;
206 }
207#ifdef DEBUG
208 else {
Vadim Bendebury4ae5eb72012-10-29 05:23:45 +0000209 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
210 scsi_device_list[i].vendor,
211 scsi_device_list[i].device,
212 (busdevfunc >> 16) & 0xFF,
213 (busdevfunc >> 11) & 0x1F,
214 (busdevfunc >> 8) & 0x7);
wdenke887afc2002-08-27 09:44:07 +0000215 }
216#endif
217 scsi_low_level_init(busdevfunc);
218 scsi_scan(1);
219}
Rob Herring942e3142011-07-06 16:13:36 +0000220#endif
wdenke887afc2002-08-27 09:44:07 +0000221
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000222#ifdef CONFIG_PARTITIONS
wdenke887afc2002-08-27 09:44:07 +0000223block_dev_desc_t * scsi_get_dev(int dev)
224{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200225 return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
wdenke887afc2002-08-27 09:44:07 +0000226}
Matthew McClintockdf3fc522011-05-24 05:31:19 +0000227#endif
wdenke887afc2002-08-27 09:44:07 +0000228
wdenke887afc2002-08-27 09:44:07 +0000229/******************************************************************************
230 * scsi boot command intepreter. Derived from diskboot
231 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200232int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000233{
Rob Herring7405a132012-09-21 04:02:30 +0000234 return common_diskboot(cmdtp, "scsi", argc, argv);
wdenke887afc2002-08-27 09:44:07 +0000235}
236
237/*********************************************************************************
238 * scsi command intepreter
239 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200240int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000241{
242 switch (argc) {
Simon Glass4c12eeb2011-12-10 08:44:01 +0000243 case 0:
244 case 1:
245 return CMD_RET_USAGE;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200246
Simon Glass4c12eeb2011-12-10 08:44:01 +0000247 case 2:
wdenke887afc2002-08-27 09:44:07 +0000248 if (strncmp(argv[1],"res",3) == 0) {
249 printf("\nReset SCSI\n");
250 scsi_bus_reset();
251 scsi_scan(1);
252 return 0;
253 }
254 if (strncmp(argv[1],"inf",3) == 0) {
255 int i;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200256 for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) {
wdenke887afc2002-08-27 09:44:07 +0000257 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
258 continue; /* list only known devices */
259 printf ("SCSI dev. %d: ", i);
260 dev_print(&scsi_dev_desc[i]);
261 }
262 return 0;
263 }
264 if (strncmp(argv[1],"dev",3) == 0) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200265 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) {
wdenke887afc2002-08-27 09:44:07 +0000266 printf("\nno SCSI devices available\n");
267 return 1;
268 }
269 printf ("\n Device %d: ", scsi_curr_dev);
270 dev_print(&scsi_dev_desc[scsi_curr_dev]);
271 return 0;
272 }
273 if (strncmp(argv[1],"scan",4) == 0) {
274 scsi_scan(1);
275 return 0;
276 }
277 if (strncmp(argv[1],"part",4) == 0) {
278 int dev, ok;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200279 for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) {
wdenke887afc2002-08-27 09:44:07 +0000280 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
281 ok++;
282 if (dev)
283 printf("\n");
wdenk1a344f22005-02-03 23:00:49 +0000284 debug ("print_part of %x\n",dev);
wdenke887afc2002-08-27 09:44:07 +0000285 print_part(&scsi_dev_desc[dev]);
286 }
287 }
288 if (!ok)
289 printf("\nno SCSI devices available\n");
290 return 1;
291 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000292 return CMD_RET_USAGE;
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200293 case 3:
wdenke887afc2002-08-27 09:44:07 +0000294 if (strncmp(argv[1],"dev",3) == 0) {
295 int dev = (int)simple_strtoul(argv[2], NULL, 10);
296 printf ("\nSCSI device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200297 if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) {
wdenke887afc2002-08-27 09:44:07 +0000298 printf("unknown device\n");
299 return 1;
300 }
301 printf ("\n Device %d: ", dev);
302 dev_print(&scsi_dev_desc[dev]);
303 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
304 return 1;
305 }
306 scsi_curr_dev = dev;
307 printf("... is now current device\n");
308 return 0;
309 }
310 if (strncmp(argv[1],"part",4) == 0) {
311 int dev = (int)simple_strtoul(argv[2], NULL, 10);
312 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
313 print_part(&scsi_dev_desc[dev]);
314 }
315 else {
316 printf ("\nSCSI device %d not available\n", dev);
317 }
318 return 1;
319 }
Simon Glass4c12eeb2011-12-10 08:44:01 +0000320 return CMD_RET_USAGE;
wdenke887afc2002-08-27 09:44:07 +0000321 default:
322 /* at least 4 args */
323 if (strcmp(argv[1],"read") == 0) {
324 ulong addr = simple_strtoul(argv[2], NULL, 16);
325 ulong blk = simple_strtoul(argv[3], NULL, 16);
326 ulong cnt = simple_strtoul(argv[4], NULL, 16);
327 ulong n;
328 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
329 scsi_curr_dev, blk, cnt);
330 n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr);
331 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
332 return 0;
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000333 } else if (strcmp(argv[1], "write") == 0) {
334 ulong addr = simple_strtoul(argv[2], NULL, 16);
335 ulong blk = simple_strtoul(argv[3], NULL, 16);
336 ulong cnt = simple_strtoul(argv[4], NULL, 16);
337 ulong n;
338 printf("\nSCSI write: device %d block # %ld, "
339 "count %ld ... ",
340 scsi_curr_dev, blk, cnt);
341 n = scsi_write(scsi_curr_dev, blk, cnt,
342 (ulong *)addr);
343 printf("%ld blocks written: %s\n", n,
344 (n == cnt) ? "OK" : "ERROR");
345 return 0;
wdenke887afc2002-08-27 09:44:07 +0000346 }
347 } /* switch */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000348 return CMD_RET_USAGE;
wdenke887afc2002-08-27 09:44:07 +0000349}
350
351/****************************************************************************************
352 * scsi_read
353 */
354
355#define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */
356
Simon Glass4f6aa342013-07-03 07:11:41 -0700357static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt,
358 void *buffer)
wdenke887afc2002-08-27 09:44:07 +0000359{
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000360 lbaint_t start, blks;
361 uintptr_t buf_addr;
wdenke887afc2002-08-27 09:44:07 +0000362 unsigned short smallblks;
363 ccb* pccb=(ccb *)&tempccb;
364 device&=0xff;
365 /* Setup device
366 */
367 pccb->target=scsi_dev_desc[device].target;
368 pccb->lun=scsi_dev_desc[device].lun;
369 buf_addr=(unsigned long)buffer;
370 start=blknr;
371 blks=blkcnt;
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000372 debug("\nscsi_read: dev %d startblk " LBAF
373 ", blccnt " LBAF " buffer %lx\n",
374 device, start, blks, (unsigned long)buffer);
wdenke887afc2002-08-27 09:44:07 +0000375 do {
376 pccb->pdata=(unsigned char *)buf_addr;
377 if(blks>SCSI_MAX_READ_BLK) {
378 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
379 smallblks=SCSI_MAX_READ_BLK;
380 scsi_setup_read_ext(pccb,start,smallblks);
381 start+=SCSI_MAX_READ_BLK;
382 blks-=SCSI_MAX_READ_BLK;
383 }
384 else {
385 pccb->datalen=scsi_dev_desc[device].blksz * blks;
386 smallblks=(unsigned short) blks;
387 scsi_setup_read_ext(pccb,start,smallblks);
388 start+=blks;
389 blks=0;
390 }
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000391 debug("scsi_read_ext: startblk " LBAF
392 ", blccnt %x buffer %lx\n",
393 start, smallblks, buf_addr);
York Sun472d5462013-04-01 11:29:11 -0700394 if (scsi_exec(pccb) != true) {
wdenke887afc2002-08-27 09:44:07 +0000395 scsi_print_error(pccb);
396 blkcnt-=blks;
397 break;
398 }
399 buf_addr+=pccb->datalen;
400 } while(blks!=0);
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000401 debug("scsi_read_ext: end startblk " LBAF
402 ", blccnt %x buffer %lx\n", start, smallblks, buf_addr);
wdenke887afc2002-08-27 09:44:07 +0000403 return(blkcnt);
404}
405
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000406/*******************************************************************************
407 * scsi_write
408 */
409
410/* Almost the maximum amount of the scsi_ext command.. */
411#define SCSI_MAX_WRITE_BLK 0xFFFF
412
Simon Glass4f6aa342013-07-03 07:11:41 -0700413static ulong scsi_write(int device, lbaint_t blknr,
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000414 lbaint_t blkcnt, const void *buffer)
415{
416 lbaint_t start, blks;
417 uintptr_t buf_addr;
418 unsigned short smallblks;
419 ccb* pccb = (ccb *)&tempccb;
420 device &= 0xff;
421 /* Setup device
422 */
423 pccb->target = scsi_dev_desc[device].target;
424 pccb->lun = scsi_dev_desc[device].lun;
425 buf_addr = (unsigned long)buffer;
426 start = blknr;
427 blks = blkcnt;
428 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
429 __func__, device, start, blks, (unsigned long)buffer);
430 do {
431 pccb->pdata = (unsigned char *)buf_addr;
432 if (blks > SCSI_MAX_WRITE_BLK) {
433 pccb->datalen = (scsi_dev_desc[device].blksz *
434 SCSI_MAX_WRITE_BLK);
435 smallblks = SCSI_MAX_WRITE_BLK;
436 scsi_setup_write_ext(pccb, start, smallblks);
437 start += SCSI_MAX_WRITE_BLK;
438 blks -= SCSI_MAX_WRITE_BLK;
439 } else {
440 pccb->datalen = scsi_dev_desc[device].blksz * blks;
441 smallblks = (unsigned short)blks;
442 scsi_setup_write_ext(pccb, start, smallblks);
443 start += blks;
444 blks = 0;
445 }
446 debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
447 __func__, start, smallblks, buf_addr);
York Sun472d5462013-04-01 11:29:11 -0700448 if (scsi_exec(pccb) != true) {
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000449 scsi_print_error(pccb);
450 blkcnt -= blks;
451 break;
452 }
453 buf_addr += pccb->datalen;
454 } while (blks != 0);
455 debug("%s: end startblk " LBAF ", blccnt %x buffer %lx\n",
456 __func__, start, smallblks, buf_addr);
457 return blkcnt;
458}
459
wdenke887afc2002-08-27 09:44:07 +0000460/* copy src to dest, skipping leading and trailing blanks
461 * and null terminate the string
462 */
463void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
464{
465 int start,end;
466
467 start=0;
468 while(start<len) {
469 if(src[start]!=' ')
470 break;
471 start++;
472 }
473 end=len-1;
474 while(end>start) {
475 if(src[end]!=' ')
476 break;
477 end--;
478 }
479 for( ; start<=end; start++) {
480 *dest++=src[start];
481 }
482 *dest='\0';
483}
484
485
wdenke887afc2002-08-27 09:44:07 +0000486/* Trim trailing blanks, and NUL-terminate string
487 */
488void scsi_trim_trail (unsigned char *str, unsigned int len)
489{
490 unsigned char *p = str + len - 1;
491
492 while (len-- > 0) {
493 *p-- = '\0';
494 if (*p != ' ') {
495 return;
496 }
497 }
498}
499
Gabe Blackb4c5bbc2012-10-29 05:23:57 +0000500int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, unsigned long *blksz)
501{
502 *capacity = 0;
503
504 memset(pccb->cmd, 0, sizeof(pccb->cmd));
505 pccb->cmd[0] = SCSI_RD_CAPAC10;
506 pccb->cmd[1] = pccb->lun << 5;
507 pccb->cmdlen = 10;
508 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
509
510 pccb->datalen = 8;
York Sun472d5462013-04-01 11:29:11 -0700511 if (scsi_exec(pccb) != true)
Gabe Blackb4c5bbc2012-10-29 05:23:57 +0000512 return 1;
513
514 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
515 ((lbaint_t)pccb->pdata[1] << 16) |
516 ((lbaint_t)pccb->pdata[2] << 8) |
517 ((lbaint_t)pccb->pdata[3]);
518
519 if (*capacity != 0xffffffff) {
520 /* Read capacity (10) was sufficient for this drive. */
521 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
522 ((unsigned long)pccb->pdata[5] << 16) |
523 ((unsigned long)pccb->pdata[6] << 8) |
524 ((unsigned long)pccb->pdata[7]);
525 return 0;
526 }
527
528 /* Read capacity (10) was insufficient. Use read capacity (16). */
529
530 memset(pccb->cmd, 0, sizeof(pccb->cmd));
531 pccb->cmd[0] = SCSI_RD_CAPAC16;
532 pccb->cmd[1] = 0x10;
533 pccb->cmdlen = 16;
534 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
535
536 pccb->datalen = 16;
York Sun472d5462013-04-01 11:29:11 -0700537 if (scsi_exec(pccb) != true)
Gabe Blackb4c5bbc2012-10-29 05:23:57 +0000538 return 1;
539
540 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
541 ((uint64_t)pccb->pdata[1] << 48) |
542 ((uint64_t)pccb->pdata[2] << 40) |
543 ((uint64_t)pccb->pdata[3] << 32) |
544 ((uint64_t)pccb->pdata[4] << 24) |
545 ((uint64_t)pccb->pdata[5] << 16) |
546 ((uint64_t)pccb->pdata[6] << 8) |
547 ((uint64_t)pccb->pdata[7]);
548
549 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
550 ((uint64_t)pccb->pdata[9] << 48) |
551 ((uint64_t)pccb->pdata[10] << 40) |
552 ((uint64_t)pccb->pdata[11] << 32) |
553 ((uint64_t)pccb->pdata[12] << 24) |
554 ((uint64_t)pccb->pdata[13] << 16) |
555 ((uint64_t)pccb->pdata[14] << 8) |
556 ((uint64_t)pccb->pdata[15]);
557
558 return 0;
559}
560
wdenke887afc2002-08-27 09:44:07 +0000561
562/************************************************************************************
563 * Some setup (fill-in) routines
564 */
565void scsi_setup_test_unit_ready(ccb * pccb)
566{
567 pccb->cmd[0]=SCSI_TST_U_RDY;
568 pccb->cmd[1]=pccb->lun<<5;
569 pccb->cmd[2]=0;
570 pccb->cmd[3]=0;
571 pccb->cmd[4]=0;
572 pccb->cmd[5]=0;
573 pccb->cmdlen=6;
574 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
575}
576
wdenke887afc2002-08-27 09:44:07 +0000577void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks)
578{
579 pccb->cmd[0]=SCSI_READ10;
580 pccb->cmd[1]=pccb->lun<<5;
581 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
582 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
583 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
584 pccb->cmd[5]=((unsigned char) (start))&0xff;
585 pccb->cmd[6]=0;
586 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
587 pccb->cmd[8]=(unsigned char) blocks & 0xff;
588 pccb->cmd[6]=0;
589 pccb->cmdlen=10;
590 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
wdenk1a344f22005-02-03 23:00:49 +0000591 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
wdenke887afc2002-08-27 09:44:07 +0000592 pccb->cmd[0],pccb->cmd[1],
593 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
594 pccb->cmd[7],pccb->cmd[8]);
595}
596
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000597void scsi_setup_write_ext(ccb *pccb, unsigned long start, unsigned short blocks)
598{
599 pccb->cmd[0] = SCSI_WRITE10;
600 pccb->cmd[1] = pccb->lun << 5;
601 pccb->cmd[2] = ((unsigned char) (start>>24)) & 0xff;
602 pccb->cmd[3] = ((unsigned char) (start>>16)) & 0xff;
603 pccb->cmd[4] = ((unsigned char) (start>>8)) & 0xff;
604 pccb->cmd[5] = ((unsigned char) (start)) & 0xff;
605 pccb->cmd[6] = 0;
606 pccb->cmd[7] = ((unsigned char) (blocks>>8)) & 0xff;
607 pccb->cmd[8] = (unsigned char)blocks & 0xff;
608 pccb->cmd[9] = 0;
609 pccb->cmdlen = 10;
610 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
611 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
612 __func__,
613 pccb->cmd[0], pccb->cmd[1],
614 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
615 pccb->cmd[7], pccb->cmd[8]);
616}
617
wdenke887afc2002-08-27 09:44:07 +0000618void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks)
619{
620 pccb->cmd[0]=SCSI_READ6;
621 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
622 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
623 pccb->cmd[3]=((unsigned char) (start))&0xff;
624 pccb->cmd[4]=(unsigned char) blocks & 0xff;
625 pccb->cmd[5]=0;
626 pccb->cmdlen=6;
627 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
wdenk1a344f22005-02-03 23:00:49 +0000628 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
wdenke887afc2002-08-27 09:44:07 +0000629 pccb->cmd[0],pccb->cmd[1],
630 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
631}
632
633
634void scsi_setup_inquiry(ccb * pccb)
635{
636 pccb->cmd[0]=SCSI_INQUIRY;
637 pccb->cmd[1]=pccb->lun<<5;
638 pccb->cmd[2]=0;
639 pccb->cmd[3]=0;
640 if(pccb->datalen>255)
641 pccb->cmd[4]=255;
642 else
643 pccb->cmd[4]=(unsigned char)pccb->datalen;
644 pccb->cmd[5]=0;
645 pccb->cmdlen=6;
646 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
647}
648
Wolfgang Denk460c3222005-08-04 01:14:12 +0200649
650U_BOOT_CMD(
651 scsi, 5, 1, do_scsi,
Peter Tyser2fb26042009-01-27 18:03:12 -0600652 "SCSI sub-system",
Wolfgang Denk460c3222005-08-04 01:14:12 +0200653 "reset - reset SCSI controller\n"
654 "scsi info - show available SCSI devices\n"
655 "scsi scan - (re-)scan SCSI bus\n"
656 "scsi device [dev] - show or set current device\n"
657 "scsi part [dev] - print partition table of one or all SCSI devices\n"
658 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
Hung-Te Lin758c9e62012-10-29 05:23:46 +0000659 " to memory address `addr'\n"
660 "scsi write addr blk# cnt - write `cnt' blocks starting at block\n"
661 " `blk#' from memory address `addr'"
Wolfgang Denk460c3222005-08-04 01:14:12 +0200662);
663
664U_BOOT_CMD(
665 scsiboot, 3, 1, do_scsiboot,
Peter Tyser2fb26042009-01-27 18:03:12 -0600666 "boot from SCSI device",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200667 "loadAddr dev:part"
Wolfgang Denk460c3222005-08-04 01:14:12 +0200668);