blob: dff811bb9617ac5f730c0e879098427ff7d05e81 [file] [log] [blame]
wdenke887afc2002-08-27 09:44:07 +00001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
Wolfgang Denkd79de1d2013-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>
Simon Glassc82961d2014-10-15 04:38:37 -060013#include <inttypes.h>
wdenke887afc2002-08-27 09:44:07 +000014#include <asm/processor.h>
15#include <scsi.h>
16#include <image.h>
wdenke887afc2002-08-27 09:44:07 +000017#include <pci.h>
18
Vadim Bendebury3e9fa302012-10-29 05:23:45 +000019#ifdef CONFIG_SCSI_DEV_LIST
20#define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
21#else
wdenke887afc2002-08-27 09:44:07 +000022#ifdef CONFIG_SCSI_SYM53C8XX
23#define SCSI_VEND_ID 0x1000
24#ifndef CONFIG_SCSI_DEV_ID
25#define SCSI_DEV_ID 0x0001
26#else
27#define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
28#endif
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080029#elif defined CONFIG_SATA_ULI5288
30
31#define SCSI_VEND_ID 0x10b9
32#define SCSI_DEV_ID 0x5288
33
Rob Herringc2829ff2011-07-06 16:13:36 +000034#elif !defined(CONFIG_SCSI_AHCI_PLAT)
Jin Zhengxiongae180dc2006-08-23 19:10:44 +080035#error no scsi device defined
wdenke887afc2002-08-27 09:44:07 +000036#endif
Vadim Bendebury3e9fa302012-10-29 05:23:45 +000037#define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
38#endif
wdenke887afc2002-08-27 09:44:07 +000039
tang yuantiane36994b2015-03-20 10:27:54 +080040#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
Vadim Bendebury3e9fa302012-10-29 05:23:45 +000041const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
42#endif
wdenke887afc2002-08-27 09:44:07 +000043static ccb tempccb; /* temporary scsi command buffer */
44
45static unsigned char tempbuff[512]; /* temporary data buffer */
46
47static int scsi_max_devs; /* number of highest available scsi device */
48
49static int scsi_curr_dev; /* current device */
50
Simon Glasse3394752016-02-29 15:25:34 -070051static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
wdenke887afc2002-08-27 09:44:07 +000052
53/********************************************************************************
54 * forward declerations of some Setup Routines
55 */
56void scsi_setup_test_unit_ready(ccb * pccb);
Mark Langsdorf911b9592015-06-05 00:58:44 +010057void scsi_setup_read6(ccb * pccb, lbaint_t start, unsigned short blocks);
58void scsi_setup_read_ext(ccb * pccb, lbaint_t start, unsigned short blocks);
Mark Langsdorf5ed06fc2015-06-05 00:58:45 +010059void scsi_setup_read16(ccb * pccb, lbaint_t start, unsigned long blocks);
60
Mark Langsdorf911b9592015-06-05 00:58:44 +010061static void scsi_setup_write_ext(ccb *pccb, lbaint_t start,
62 unsigned short blocks);
wdenke887afc2002-08-27 09:44:07 +000063void scsi_setup_inquiry(ccb * pccb);
64void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len);
65
66
Gabe Blackbcc35392012-10-29 05:23:57 +000067static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity,
68 unsigned long *blksz);
Simon Glasse3394752016-02-29 15:25:34 -070069static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
Stephen Warrene73f2962015-12-07 11:38:48 -070070 lbaint_t blkcnt, void *buffer);
Simon Glasse3394752016-02-29 15:25:34 -070071static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +000072 lbaint_t blkcnt, const void *buffer);
wdenke887afc2002-08-27 09:44:07 +000073
74
75/*********************************************************************************
76 * (re)-scan the scsi bus and reports scsi device info
77 * to the user if mode = 1
78 */
79void scsi_scan(int mode)
80{
81 unsigned char i,perq,modi,lun;
Gabe Blackbcc35392012-10-29 05:23:57 +000082 lbaint_t capacity;
83 unsigned long blksz;
wdenke887afc2002-08-27 09:44:07 +000084 ccb* pccb=(ccb *)&tempccb;
85
86 if(mode==1) {
87 printf("scanning bus for devices...\n");
88 }
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020089 for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) {
wdenke887afc2002-08-27 09:44:07 +000090 scsi_dev_desc[i].target=0xff;
91 scsi_dev_desc[i].lun=0xff;
92 scsi_dev_desc[i].lba=0;
93 scsi_dev_desc[i].blksz=0;
Egbert Eich2eec2ab2013-04-09 21:11:56 +000094 scsi_dev_desc[i].log2blksz =
95 LOG2_INVALID(typeof(scsi_dev_desc[i].log2blksz));
wdenke887afc2002-08-27 09:44:07 +000096 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;
York Sun4a598092013-04-01 11:29:11 -0700100 scsi_dev_desc[i].removable = false;
wdenke887afc2002-08-27 09:44:07 +0000101 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;
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000105 scsi_dev_desc[i].block_write = scsi_write;
wdenke887afc2002-08-27 09:44:07 +0000106 }
107 scsi_max_devs=0;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200108 for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) {
wdenke887afc2002-08-27 09:44:07 +0000109 pccb->target=i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200110 for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) {
wdenke887afc2002-08-27 09:44:07 +0000111 pccb->lun=lun;
112 pccb->pdata=(unsigned char *)&tempbuff;
113 pccb->datalen=512;
114 scsi_setup_inquiry(pccb);
York Sun4a598092013-04-01 11:29:11 -0700115 if (scsi_exec(pccb) != true) {
wdenke887afc2002-08-27 09:44:07 +0000116 if(pccb->contr_stat==SCSI_SEL_TIME_OUT) {
wdenkf1276d42005-02-03 23:00:49 +0000117 debug ("Selection timeout ID %d\n",pccb->target);
wdenke887afc2002-08-27 09:44:07 +0000118 continue; /* selection timeout => assuming no device present */
119 }
120 scsi_print_error(pccb);
121 continue;
122 }
123 perq=tempbuff[0];
124 modi=tempbuff[1];
125 if((perq & 0x1f)==0x1f) {
126 continue; /* skip unknown devices */
127 }
128 if((modi&0x80)==0x80) /* drive is removable */
York Sun4a598092013-04-01 11:29:11 -0700129 scsi_dev_desc[scsi_max_devs].removable=true;
wdenke887afc2002-08-27 09:44:07 +0000130 /* get info for this device */
Stefan Roese4404c162007-11-17 07:58:25 +0100131 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0],
132 &tempbuff[8], 8);
133 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0],
134 &tempbuff[16], 16);
135 scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0],
136 &tempbuff[32], 4);
wdenke887afc2002-08-27 09:44:07 +0000137 scsi_dev_desc[scsi_max_devs].target=pccb->target;
138 scsi_dev_desc[scsi_max_devs].lun=pccb->lun;
139
140 pccb->datalen=0;
141 scsi_setup_test_unit_ready(pccb);
York Sun4a598092013-04-01 11:29:11 -0700142 if (scsi_exec(pccb) != true) {
143 if (scsi_dev_desc[scsi_max_devs].removable == true) {
wdenke887afc2002-08-27 09:44:07 +0000144 scsi_dev_desc[scsi_max_devs].type=perq;
145 goto removable;
146 }
147 scsi_print_error(pccb);
148 continue;
149 }
Gabe Blackbcc35392012-10-29 05:23:57 +0000150 if (scsi_read_capacity(pccb, &capacity, &blksz)) {
wdenke887afc2002-08-27 09:44:07 +0000151 scsi_print_error(pccb);
152 continue;
153 }
wdenke887afc2002-08-27 09:44:07 +0000154 scsi_dev_desc[scsi_max_devs].lba=capacity;
155 scsi_dev_desc[scsi_max_devs].blksz=blksz;
Egbert Eich2eec2ab2013-04-09 21:11:56 +0000156 scsi_dev_desc[scsi_max_devs].log2blksz =
157 LOG2(scsi_dev_desc[scsi_max_devs].blksz);
wdenke887afc2002-08-27 09:44:07 +0000158 scsi_dev_desc[scsi_max_devs].type=perq;
159 init_part(&scsi_dev_desc[scsi_max_devs]);
160removable:
161 if(mode==1) {
162 printf (" Device %d: ", scsi_max_devs);
163 dev_print(&scsi_dev_desc[scsi_max_devs]);
164 } /* if mode */
165 scsi_max_devs++;
166 } /* next LUN */
167 }
168 if(scsi_max_devs>0)
169 scsi_curr_dev=0;
170 else
Wolfgang Denkdc770c72008-07-14 15:19:07 +0200171 scsi_curr_dev = -1;
Stefan Reinauere50a10e2012-10-29 05:23:48 +0000172
173 printf("Found %d device(s).\n", scsi_max_devs);
Dan Murphy0c3ab852014-02-03 06:59:01 -0600174#ifndef CONFIG_SPL_BUILD
Stefan Reinauere50a10e2012-10-29 05:23:48 +0000175 setenv_ulong("scsidevs", scsi_max_devs);
Dan Murphy0c3ab852014-02-03 06:59:01 -0600176#endif
Stefan Reinauere50a10e2012-10-29 05:23:48 +0000177}
178
179int scsi_get_disk_count(void)
180{
181 return scsi_max_devs;
wdenke887afc2002-08-27 09:44:07 +0000182}
183
tang yuantiane36994b2015-03-20 10:27:54 +0800184#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
wdenke887afc2002-08-27 09:44:07 +0000185void scsi_init(void)
186{
Simon Glass7c3f62a2015-11-29 13:17:51 -0700187 int busdevfunc = -1;
Vadim Bendebury3e9fa302012-10-29 05:23:45 +0000188 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 */
Simon Glass7c3f62a2015-11-29 13:17:51 -0700195#ifdef CONFIG_DM_PCI
196 struct udevice *dev;
197 int ret;
198
199 ret = dm_pci_find_device(scsi_device_list[i].vendor,
200 scsi_device_list[i].device, 0, &dev);
201 if (!ret) {
202 busdevfunc = dm_pci_get_bdf(dev);
203 break;
204 }
205#else
Vadim Bendebury3e9fa302012-10-29 05:23:45 +0000206 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
207 scsi_device_list[i].device,
208 0);
Simon Glass7c3f62a2015-11-29 13:17:51 -0700209#endif
Vadim Bendebury3e9fa302012-10-29 05:23:45 +0000210 if (busdevfunc != -1)
211 break;
212 }
wdenke887afc2002-08-27 09:44:07 +0000213
Vadim Bendebury3e9fa302012-10-29 05:23:45 +0000214 if (busdevfunc == -1) {
215 printf("Error: SCSI Controller(s) ");
216 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
217 printf("%04X:%04X ",
218 scsi_device_list[i].vendor,
219 scsi_device_list[i].device);
220 }
221 printf("not found\n");
wdenke887afc2002-08-27 09:44:07 +0000222 return;
223 }
224#ifdef DEBUG
225 else {
Vadim Bendebury3e9fa302012-10-29 05:23:45 +0000226 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
227 scsi_device_list[i].vendor,
228 scsi_device_list[i].device,
229 (busdevfunc >> 16) & 0xFF,
230 (busdevfunc >> 11) & 0x1F,
231 (busdevfunc >> 8) & 0x7);
wdenke887afc2002-08-27 09:44:07 +0000232 }
233#endif
Simon Glass230cfef2015-01-27 22:13:44 -0700234 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
wdenke887afc2002-08-27 09:44:07 +0000235 scsi_low_level_init(busdevfunc);
236 scsi_scan(1);
Simon Glass230cfef2015-01-27 22:13:44 -0700237 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
wdenke887afc2002-08-27 09:44:07 +0000238}
Rob Herringc2829ff2011-07-06 16:13:36 +0000239#endif
wdenke887afc2002-08-27 09:44:07 +0000240
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000241#ifdef CONFIG_PARTITIONS
Simon Glasse3394752016-02-29 15:25:34 -0700242struct blk_desc *scsi_get_dev(int dev)
wdenke887afc2002-08-27 09:44:07 +0000243{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200244 return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL;
wdenke887afc2002-08-27 09:44:07 +0000245}
Matthew McClintock6252b4f2011-05-24 05:31:19 +0000246#endif
wdenke887afc2002-08-27 09:44:07 +0000247
wdenke887afc2002-08-27 09:44:07 +0000248/******************************************************************************
249 * scsi boot command intepreter. Derived from diskboot
250 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200251int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000252{
Rob Herringa20cbf92012-09-21 04:02:30 +0000253 return common_diskboot(cmdtp, "scsi", argc, argv);
wdenke887afc2002-08-27 09:44:07 +0000254}
255
256/*********************************************************************************
257 * scsi command intepreter
258 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200259int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenke887afc2002-08-27 09:44:07 +0000260{
261 switch (argc) {
Simon Glassa06dfc72011-12-10 08:44:01 +0000262 case 0:
263 case 1:
264 return CMD_RET_USAGE;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200265
Simon Glassa06dfc72011-12-10 08:44:01 +0000266 case 2:
wdenke887afc2002-08-27 09:44:07 +0000267 if (strncmp(argv[1],"res",3) == 0) {
268 printf("\nReset SCSI\n");
269 scsi_bus_reset();
270 scsi_scan(1);
271 return 0;
272 }
273 if (strncmp(argv[1],"inf",3) == 0) {
274 int i;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200275 for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) {
wdenke887afc2002-08-27 09:44:07 +0000276 if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN)
277 continue; /* list only known devices */
278 printf ("SCSI dev. %d: ", i);
279 dev_print(&scsi_dev_desc[i]);
280 }
281 return 0;
282 }
283 if (strncmp(argv[1],"dev",3) == 0) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200284 if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) {
wdenke887afc2002-08-27 09:44:07 +0000285 printf("\nno SCSI devices available\n");
286 return 1;
287 }
288 printf ("\n Device %d: ", scsi_curr_dev);
289 dev_print(&scsi_dev_desc[scsi_curr_dev]);
290 return 0;
291 }
292 if (strncmp(argv[1],"scan",4) == 0) {
293 scsi_scan(1);
294 return 0;
295 }
296 if (strncmp(argv[1],"part",4) == 0) {
297 int dev, ok;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200298 for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) {
wdenke887afc2002-08-27 09:44:07 +0000299 if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) {
300 ok++;
301 if (dev)
302 printf("\n");
wdenkf1276d42005-02-03 23:00:49 +0000303 debug ("print_part of %x\n",dev);
wdenke887afc2002-08-27 09:44:07 +0000304 print_part(&scsi_dev_desc[dev]);
305 }
306 }
307 if (!ok)
308 printf("\nno SCSI devices available\n");
309 return 1;
310 }
Simon Glassa06dfc72011-12-10 08:44:01 +0000311 return CMD_RET_USAGE;
Wolfgang Denka1be4762008-05-20 16:00:29 +0200312 case 3:
wdenke887afc2002-08-27 09:44:07 +0000313 if (strncmp(argv[1],"dev",3) == 0) {
314 int dev = (int)simple_strtoul(argv[2], NULL, 10);
315 printf ("\nSCSI device %d: ", dev);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200316 if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) {
wdenke887afc2002-08-27 09:44:07 +0000317 printf("unknown device\n");
318 return 1;
319 }
320 printf ("\n Device %d: ", dev);
321 dev_print(&scsi_dev_desc[dev]);
322 if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) {
323 return 1;
324 }
325 scsi_curr_dev = dev;
326 printf("... is now current device\n");
327 return 0;
328 }
329 if (strncmp(argv[1],"part",4) == 0) {
330 int dev = (int)simple_strtoul(argv[2], NULL, 10);
331 if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) {
332 print_part(&scsi_dev_desc[dev]);
333 }
334 else {
335 printf ("\nSCSI device %d not available\n", dev);
336 }
337 return 1;
338 }
Simon Glassa06dfc72011-12-10 08:44:01 +0000339 return CMD_RET_USAGE;
wdenke887afc2002-08-27 09:44:07 +0000340 default:
341 /* at least 4 args */
342 if (strcmp(argv[1],"read") == 0) {
343 ulong addr = simple_strtoul(argv[2], NULL, 16);
344 ulong blk = simple_strtoul(argv[3], NULL, 16);
345 ulong cnt = simple_strtoul(argv[4], NULL, 16);
346 ulong n;
347 printf ("\nSCSI read: device %d block # %ld, count %ld ... ",
348 scsi_curr_dev, blk, cnt);
Stephen Warrene73f2962015-12-07 11:38:48 -0700349 n = scsi_read(&scsi_dev_desc[scsi_curr_dev],
350 blk, cnt, (ulong *)addr);
wdenke887afc2002-08-27 09:44:07 +0000351 printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR");
352 return 0;
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000353 } else if (strcmp(argv[1], "write") == 0) {
354 ulong addr = simple_strtoul(argv[2], NULL, 16);
355 ulong blk = simple_strtoul(argv[3], NULL, 16);
356 ulong cnt = simple_strtoul(argv[4], NULL, 16);
357 ulong n;
358 printf("\nSCSI write: device %d block # %ld, "
359 "count %ld ... ",
360 scsi_curr_dev, blk, cnt);
Stephen Warrene73f2962015-12-07 11:38:48 -0700361 n = scsi_write(&scsi_dev_desc[scsi_curr_dev],
362 blk, cnt, (ulong *)addr);
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000363 printf("%ld blocks written: %s\n", n,
364 (n == cnt) ? "OK" : "ERROR");
365 return 0;
wdenke887afc2002-08-27 09:44:07 +0000366 }
367 } /* switch */
Simon Glassa06dfc72011-12-10 08:44:01 +0000368 return CMD_RET_USAGE;
wdenke887afc2002-08-27 09:44:07 +0000369}
370
371/****************************************************************************************
372 * scsi_read
373 */
374
Mark Langsdorf5ed06fc2015-06-05 00:58:45 +0100375/* almost the maximum amount of the scsi_ext command.. */
376#define SCSI_MAX_READ_BLK 0xFFFF
377#define SCSI_LBA48_READ 0xFFFFFFF
wdenke887afc2002-08-27 09:44:07 +0000378
Simon Glasse3394752016-02-29 15:25:34 -0700379static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
Stephen Warrene73f2962015-12-07 11:38:48 -0700380 lbaint_t blkcnt, void *buffer)
wdenke887afc2002-08-27 09:44:07 +0000381{
Stephen Warrene73f2962015-12-07 11:38:48 -0700382 int device = block_dev->dev;
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000383 lbaint_t start, blks;
384 uintptr_t buf_addr;
Andre Przywara644a8bd2015-07-02 01:04:23 +0100385 unsigned short smallblks = 0;
wdenke887afc2002-08-27 09:44:07 +0000386 ccb* pccb=(ccb *)&tempccb;
387 device&=0xff;
388 /* Setup device
389 */
390 pccb->target=scsi_dev_desc[device].target;
391 pccb->lun=scsi_dev_desc[device].lun;
392 buf_addr=(unsigned long)buffer;
393 start=blknr;
394 blks=blkcnt;
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000395 debug("\nscsi_read: dev %d startblk " LBAF
396 ", blccnt " LBAF " buffer %lx\n",
397 device, start, blks, (unsigned long)buffer);
wdenke887afc2002-08-27 09:44:07 +0000398 do {
399 pccb->pdata=(unsigned char *)buf_addr;
Mark Langsdorf5ed06fc2015-06-05 00:58:45 +0100400#ifdef CONFIG_SYS_64BIT_LBA
401 if (start > SCSI_LBA48_READ) {
402 unsigned long blocks;
403 blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK);
404 pccb->datalen = scsi_dev_desc[device].blksz * blocks;
405 scsi_setup_read16(pccb, start, blocks);
406 start += blocks;
407 blks -= blocks;
Andre Przywara644a8bd2015-07-02 01:04:23 +0100408 } else
Mark Langsdorf5ed06fc2015-06-05 00:58:45 +0100409#endif
410 if (blks > SCSI_MAX_READ_BLK) {
wdenke887afc2002-08-27 09:44:07 +0000411 pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK;
412 smallblks=SCSI_MAX_READ_BLK;
413 scsi_setup_read_ext(pccb,start,smallblks);
414 start+=SCSI_MAX_READ_BLK;
415 blks-=SCSI_MAX_READ_BLK;
416 }
417 else {
418 pccb->datalen=scsi_dev_desc[device].blksz * blks;
419 smallblks=(unsigned short) blks;
420 scsi_setup_read_ext(pccb,start,smallblks);
421 start+=blks;
422 blks=0;
423 }
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000424 debug("scsi_read_ext: startblk " LBAF
Simon Glassc82961d2014-10-15 04:38:37 -0600425 ", blccnt %x buffer %" PRIXPTR "\n",
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000426 start, smallblks, buf_addr);
York Sun4a598092013-04-01 11:29:11 -0700427 if (scsi_exec(pccb) != true) {
wdenke887afc2002-08-27 09:44:07 +0000428 scsi_print_error(pccb);
429 blkcnt-=blks;
430 break;
431 }
432 buf_addr+=pccb->datalen;
433 } while(blks!=0);
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000434 debug("scsi_read_ext: end startblk " LBAF
Simon Glassc82961d2014-10-15 04:38:37 -0600435 ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr);
wdenke887afc2002-08-27 09:44:07 +0000436 return(blkcnt);
437}
438
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000439/*******************************************************************************
440 * scsi_write
441 */
442
443/* Almost the maximum amount of the scsi_ext command.. */
444#define SCSI_MAX_WRITE_BLK 0xFFFF
445
Simon Glasse3394752016-02-29 15:25:34 -0700446static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000447 lbaint_t blkcnt, const void *buffer)
448{
Stephen Warrene73f2962015-12-07 11:38:48 -0700449 int device = block_dev->dev;
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000450 lbaint_t start, blks;
451 uintptr_t buf_addr;
452 unsigned short smallblks;
453 ccb* pccb = (ccb *)&tempccb;
454 device &= 0xff;
455 /* Setup device
456 */
457 pccb->target = scsi_dev_desc[device].target;
458 pccb->lun = scsi_dev_desc[device].lun;
459 buf_addr = (unsigned long)buffer;
460 start = blknr;
461 blks = blkcnt;
462 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
463 __func__, device, start, blks, (unsigned long)buffer);
464 do {
465 pccb->pdata = (unsigned char *)buf_addr;
466 if (blks > SCSI_MAX_WRITE_BLK) {
467 pccb->datalen = (scsi_dev_desc[device].blksz *
468 SCSI_MAX_WRITE_BLK);
469 smallblks = SCSI_MAX_WRITE_BLK;
470 scsi_setup_write_ext(pccb, start, smallblks);
471 start += SCSI_MAX_WRITE_BLK;
472 blks -= SCSI_MAX_WRITE_BLK;
473 } else {
474 pccb->datalen = scsi_dev_desc[device].blksz * blks;
475 smallblks = (unsigned short)blks;
476 scsi_setup_write_ext(pccb, start, smallblks);
477 start += blks;
478 blks = 0;
479 }
Simon Glassc82961d2014-10-15 04:38:37 -0600480 debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000481 __func__, start, smallblks, buf_addr);
York Sun4a598092013-04-01 11:29:11 -0700482 if (scsi_exec(pccb) != true) {
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000483 scsi_print_error(pccb);
484 blkcnt -= blks;
485 break;
486 }
487 buf_addr += pccb->datalen;
488 } while (blks != 0);
Simon Glassc82961d2014-10-15 04:38:37 -0600489 debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000490 __func__, start, smallblks, buf_addr);
491 return blkcnt;
492}
493
wdenke887afc2002-08-27 09:44:07 +0000494/* copy src to dest, skipping leading and trailing blanks
495 * and null terminate the string
496 */
497void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len)
498{
499 int start,end;
500
501 start=0;
502 while(start<len) {
503 if(src[start]!=' ')
504 break;
505 start++;
506 }
507 end=len-1;
508 while(end>start) {
509 if(src[end]!=' ')
510 break;
511 end--;
512 }
513 for( ; start<=end; start++) {
514 *dest++=src[start];
515 }
516 *dest='\0';
517}
518
519
wdenke887afc2002-08-27 09:44:07 +0000520/* Trim trailing blanks, and NUL-terminate string
521 */
522void scsi_trim_trail (unsigned char *str, unsigned int len)
523{
524 unsigned char *p = str + len - 1;
525
526 while (len-- > 0) {
527 *p-- = '\0';
528 if (*p != ' ') {
529 return;
530 }
531 }
532}
533
Gabe Blackbcc35392012-10-29 05:23:57 +0000534int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, unsigned long *blksz)
535{
536 *capacity = 0;
537
538 memset(pccb->cmd, 0, sizeof(pccb->cmd));
539 pccb->cmd[0] = SCSI_RD_CAPAC10;
540 pccb->cmd[1] = pccb->lun << 5;
541 pccb->cmdlen = 10;
542 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
543
544 pccb->datalen = 8;
York Sun4a598092013-04-01 11:29:11 -0700545 if (scsi_exec(pccb) != true)
Gabe Blackbcc35392012-10-29 05:23:57 +0000546 return 1;
547
548 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
549 ((lbaint_t)pccb->pdata[1] << 16) |
550 ((lbaint_t)pccb->pdata[2] << 8) |
551 ((lbaint_t)pccb->pdata[3]);
552
553 if (*capacity != 0xffffffff) {
554 /* Read capacity (10) was sufficient for this drive. */
555 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
556 ((unsigned long)pccb->pdata[5] << 16) |
557 ((unsigned long)pccb->pdata[6] << 8) |
558 ((unsigned long)pccb->pdata[7]);
559 return 0;
560 }
561
562 /* Read capacity (10) was insufficient. Use read capacity (16). */
563
564 memset(pccb->cmd, 0, sizeof(pccb->cmd));
565 pccb->cmd[0] = SCSI_RD_CAPAC16;
566 pccb->cmd[1] = 0x10;
567 pccb->cmdlen = 16;
568 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
569
570 pccb->datalen = 16;
York Sun4a598092013-04-01 11:29:11 -0700571 if (scsi_exec(pccb) != true)
Gabe Blackbcc35392012-10-29 05:23:57 +0000572 return 1;
573
574 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
575 ((uint64_t)pccb->pdata[1] << 48) |
576 ((uint64_t)pccb->pdata[2] << 40) |
577 ((uint64_t)pccb->pdata[3] << 32) |
578 ((uint64_t)pccb->pdata[4] << 24) |
579 ((uint64_t)pccb->pdata[5] << 16) |
580 ((uint64_t)pccb->pdata[6] << 8) |
581 ((uint64_t)pccb->pdata[7]);
582
583 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
584 ((uint64_t)pccb->pdata[9] << 48) |
585 ((uint64_t)pccb->pdata[10] << 40) |
586 ((uint64_t)pccb->pdata[11] << 32) |
587 ((uint64_t)pccb->pdata[12] << 24) |
588 ((uint64_t)pccb->pdata[13] << 16) |
589 ((uint64_t)pccb->pdata[14] << 8) |
590 ((uint64_t)pccb->pdata[15]);
591
592 return 0;
593}
594
wdenke887afc2002-08-27 09:44:07 +0000595
596/************************************************************************************
597 * Some setup (fill-in) routines
598 */
599void scsi_setup_test_unit_ready(ccb * pccb)
600{
601 pccb->cmd[0]=SCSI_TST_U_RDY;
602 pccb->cmd[1]=pccb->lun<<5;
603 pccb->cmd[2]=0;
604 pccb->cmd[3]=0;
605 pccb->cmd[4]=0;
606 pccb->cmd[5]=0;
607 pccb->cmdlen=6;
608 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
wdenke887afc2002-08-27 09:44:07 +0000609}
Mark Langsdorf5ed06fc2015-06-05 00:58:45 +0100610
611#ifdef CONFIG_SYS_64BIT_LBA
612void scsi_setup_read16(ccb * pccb, lbaint_t start, unsigned long blocks)
613{
614 pccb->cmd[0] = SCSI_READ16;
615 pccb->cmd[1] = pccb->lun<<5;
616 pccb->cmd[2] = ((unsigned char) (start >> 56)) & 0xff;
617 pccb->cmd[3] = ((unsigned char) (start >> 48)) & 0xff;
618 pccb->cmd[4] = ((unsigned char) (start >> 40)) & 0xff;
619 pccb->cmd[5] = ((unsigned char) (start >> 32)) & 0xff;
620 pccb->cmd[6] = ((unsigned char) (start >> 24)) & 0xff;
621 pccb->cmd[7] = ((unsigned char) (start >> 16)) & 0xff;
622 pccb->cmd[8] = ((unsigned char) (start >> 8)) & 0xff;
623 pccb->cmd[9] = ((unsigned char) (start)) & 0xff;
624 pccb->cmd[10] = 0;
625 pccb->cmd[11] = ((unsigned char) (blocks >> 24)) & 0xff;
626 pccb->cmd[12] = ((unsigned char) (blocks >> 16)) & 0xff;
627 pccb->cmd[13] = ((unsigned char) (blocks >> 8)) & 0xff;
628 pccb->cmd[14] = (unsigned char) blocks & 0xff;
629 pccb->cmd[15] = 0;
630 pccb->cmdlen = 16;
631 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
632 debug ("scsi_setup_read16: cmd: %02X %02X "
633 "startblk %02X%02X%02X%02X%02X%02X%02X%02X "
634 "blccnt %02X%02X%02X%02X\n",
635 pccb->cmd[0], pccb->cmd[1],
636 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
637 pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
638 pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
639}
640#endif
wdenke887afc2002-08-27 09:44:07 +0000641
Mark Langsdorf911b9592015-06-05 00:58:44 +0100642void scsi_setup_read_ext(ccb * pccb, lbaint_t start, unsigned short blocks)
wdenke887afc2002-08-27 09:44:07 +0000643{
644 pccb->cmd[0]=SCSI_READ10;
645 pccb->cmd[1]=pccb->lun<<5;
646 pccb->cmd[2]=((unsigned char) (start>>24))&0xff;
647 pccb->cmd[3]=((unsigned char) (start>>16))&0xff;
648 pccb->cmd[4]=((unsigned char) (start>>8))&0xff;
649 pccb->cmd[5]=((unsigned char) (start))&0xff;
650 pccb->cmd[6]=0;
651 pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff;
652 pccb->cmd[8]=(unsigned char) blocks & 0xff;
653 pccb->cmd[6]=0;
654 pccb->cmdlen=10;
655 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
wdenkf1276d42005-02-03 23:00:49 +0000656 debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
wdenke887afc2002-08-27 09:44:07 +0000657 pccb->cmd[0],pccb->cmd[1],
658 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5],
659 pccb->cmd[7],pccb->cmd[8]);
660}
661
Mark Langsdorf911b9592015-06-05 00:58:44 +0100662void scsi_setup_write_ext(ccb *pccb, lbaint_t start, unsigned short blocks)
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000663{
664 pccb->cmd[0] = SCSI_WRITE10;
665 pccb->cmd[1] = pccb->lun << 5;
666 pccb->cmd[2] = ((unsigned char) (start>>24)) & 0xff;
667 pccb->cmd[3] = ((unsigned char) (start>>16)) & 0xff;
668 pccb->cmd[4] = ((unsigned char) (start>>8)) & 0xff;
669 pccb->cmd[5] = ((unsigned char) (start)) & 0xff;
670 pccb->cmd[6] = 0;
671 pccb->cmd[7] = ((unsigned char) (blocks>>8)) & 0xff;
672 pccb->cmd[8] = (unsigned char)blocks & 0xff;
673 pccb->cmd[9] = 0;
674 pccb->cmdlen = 10;
675 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
676 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
677 __func__,
678 pccb->cmd[0], pccb->cmd[1],
679 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
680 pccb->cmd[7], pccb->cmd[8]);
681}
682
Mark Langsdorf911b9592015-06-05 00:58:44 +0100683void scsi_setup_read6(ccb * pccb, lbaint_t start, unsigned short blocks)
wdenke887afc2002-08-27 09:44:07 +0000684{
685 pccb->cmd[0]=SCSI_READ6;
686 pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f);
687 pccb->cmd[2]=((unsigned char) (start>>8))&0xff;
688 pccb->cmd[3]=((unsigned char) (start))&0xff;
689 pccb->cmd[4]=(unsigned char) blocks & 0xff;
690 pccb->cmd[5]=0;
691 pccb->cmdlen=6;
692 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
wdenkf1276d42005-02-03 23:00:49 +0000693 debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n",
wdenke887afc2002-08-27 09:44:07 +0000694 pccb->cmd[0],pccb->cmd[1],
695 pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]);
696}
697
698
699void scsi_setup_inquiry(ccb * pccb)
700{
701 pccb->cmd[0]=SCSI_INQUIRY;
702 pccb->cmd[1]=pccb->lun<<5;
703 pccb->cmd[2]=0;
704 pccb->cmd[3]=0;
705 if(pccb->datalen>255)
706 pccb->cmd[4]=255;
707 else
708 pccb->cmd[4]=(unsigned char)pccb->datalen;
709 pccb->cmd[5]=0;
710 pccb->cmdlen=6;
711 pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */
712}
713
Wolfgang Denk6a3d6b02005-08-04 01:14:12 +0200714
715U_BOOT_CMD(
716 scsi, 5, 1, do_scsi,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600717 "SCSI sub-system",
Wolfgang Denk6a3d6b02005-08-04 01:14:12 +0200718 "reset - reset SCSI controller\n"
719 "scsi info - show available SCSI devices\n"
720 "scsi scan - (re-)scan SCSI bus\n"
721 "scsi device [dev] - show or set current device\n"
722 "scsi part [dev] - print partition table of one or all SCSI devices\n"
723 "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n"
Hung-Te Lin98a6c4e2012-10-29 05:23:46 +0000724 " to memory address `addr'\n"
725 "scsi write addr blk# cnt - write `cnt' blocks starting at block\n"
726 " `blk#' from memory address `addr'"
Wolfgang Denk6a3d6b02005-08-04 01:14:12 +0200727);
728
729U_BOOT_CMD(
730 scsiboot, 3, 1, do_scsiboot,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600731 "boot from SCSI device",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200732 "loadAddr dev:part"
Wolfgang Denk6a3d6b02005-08-04 01:14:12 +0200733);