]>
Commit | Line | Data |
---|---|---|
e887afc9 WD |
1 | /* |
2 | * (C) Copyright 2001 | |
3 | * Denis Peter, MPL AG Switzerland | |
4 | * | |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
e887afc9 WD |
6 | */ |
7 | ||
8 | /* | |
9 | * SCSI support. | |
10 | */ | |
e887afc9 WD |
11 | #include <common.h> |
12 | #include <command.h> | |
f3609473 | 13 | #include <inttypes.h> |
e887afc9 WD |
14 | #include <asm/processor.h> |
15 | #include <scsi.h> | |
16 | #include <image.h> | |
e887afc9 WD |
17 | #include <pci.h> |
18 | ||
4ae5eb7c VB |
19 | #ifdef CONFIG_SCSI_DEV_LIST |
20 | #define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST | |
21 | #else | |
e887afc9 WD |
22 | #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 | |
4782ac80 JZ |
29 | #elif defined CONFIG_SATA_ULI5288 |
30 | ||
31 | #define SCSI_VEND_ID 0x10b9 | |
32 | #define SCSI_DEV_ID 0x5288 | |
33 | ||
942e3143 | 34 | #elif !defined(CONFIG_SCSI_AHCI_PLAT) |
4782ac80 | 35 | #error no scsi device defined |
e887afc9 | 36 | #endif |
4ae5eb7c VB |
37 | #define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID} |
38 | #endif | |
e887afc9 | 39 | |
4ae5eb7c VB |
40 | #ifdef CONFIG_PCI |
41 | const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST }; | |
42 | #endif | |
e887afc9 WD |
43 | static ccb tempccb; /* temporary scsi command buffer */ |
44 | ||
45 | static unsigned char tempbuff[512]; /* temporary data buffer */ | |
46 | ||
47 | static int scsi_max_devs; /* number of highest available scsi device */ | |
48 | ||
49 | static int scsi_curr_dev; /* current device */ | |
50 | ||
6d0f6bcf | 51 | static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE]; |
e887afc9 WD |
52 | |
53 | /******************************************************************************** | |
54 | * forward declerations of some Setup Routines | |
55 | */ | |
56 | void scsi_setup_test_unit_ready(ccb * pccb); | |
e887afc9 WD |
57 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks); |
58 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks); | |
758c9e69 HTL |
59 | static void scsi_setup_write_ext(ccb *pccb, unsigned long start, |
60 | unsigned short blocks); | |
e887afc9 WD |
61 | void scsi_setup_inquiry(ccb * pccb); |
62 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len); | |
63 | ||
64 | ||
b4c5bbce GB |
65 | static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, |
66 | unsigned long *blksz); | |
4f6aa346 SG |
67 | static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt, |
68 | void *buffer); | |
69 | static ulong scsi_write(int device, lbaint_t blknr, | |
758c9e69 | 70 | lbaint_t blkcnt, const void *buffer); |
e887afc9 WD |
71 | |
72 | ||
73 | /********************************************************************************* | |
74 | * (re)-scan the scsi bus and reports scsi device info | |
75 | * to the user if mode = 1 | |
76 | */ | |
77 | void scsi_scan(int mode) | |
78 | { | |
79 | unsigned char i,perq,modi,lun; | |
b4c5bbce GB |
80 | lbaint_t capacity; |
81 | unsigned long blksz; | |
e887afc9 WD |
82 | ccb* pccb=(ccb *)&tempccb; |
83 | ||
84 | if(mode==1) { | |
85 | printf("scanning bus for devices...\n"); | |
86 | } | |
6d0f6bcf | 87 | for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) { |
e887afc9 WD |
88 | scsi_dev_desc[i].target=0xff; |
89 | scsi_dev_desc[i].lun=0xff; | |
90 | scsi_dev_desc[i].lba=0; | |
91 | scsi_dev_desc[i].blksz=0; | |
0472fbfd EE |
92 | scsi_dev_desc[i].log2blksz = |
93 | LOG2_INVALID(typeof(scsi_dev_desc[i].log2blksz)); | |
e887afc9 WD |
94 | scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN; |
95 | scsi_dev_desc[i].vendor[0]=0; | |
96 | scsi_dev_desc[i].product[0]=0; | |
97 | scsi_dev_desc[i].revision[0]=0; | |
472d5460 | 98 | scsi_dev_desc[i].removable = false; |
e887afc9 WD |
99 | scsi_dev_desc[i].if_type=IF_TYPE_SCSI; |
100 | scsi_dev_desc[i].dev=i; | |
101 | scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN; | |
102 | scsi_dev_desc[i].block_read=scsi_read; | |
758c9e69 | 103 | scsi_dev_desc[i].block_write = scsi_write; |
e887afc9 WD |
104 | } |
105 | scsi_max_devs=0; | |
6d0f6bcf | 106 | for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) { |
e887afc9 | 107 | pccb->target=i; |
6d0f6bcf | 108 | for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) { |
e887afc9 WD |
109 | pccb->lun=lun; |
110 | pccb->pdata=(unsigned char *)&tempbuff; | |
111 | pccb->datalen=512; | |
112 | scsi_setup_inquiry(pccb); | |
472d5460 | 113 | if (scsi_exec(pccb) != true) { |
e887afc9 | 114 | if(pccb->contr_stat==SCSI_SEL_TIME_OUT) { |
1a344f29 | 115 | debug ("Selection timeout ID %d\n",pccb->target); |
e887afc9 WD |
116 | continue; /* selection timeout => assuming no device present */ |
117 | } | |
118 | scsi_print_error(pccb); | |
119 | continue; | |
120 | } | |
121 | perq=tempbuff[0]; | |
122 | modi=tempbuff[1]; | |
123 | if((perq & 0x1f)==0x1f) { | |
124 | continue; /* skip unknown devices */ | |
125 | } | |
126 | if((modi&0x80)==0x80) /* drive is removable */ | |
472d5460 | 127 | scsi_dev_desc[scsi_max_devs].removable=true; |
e887afc9 | 128 | /* get info for this device */ |
2309c130 SR |
129 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0], |
130 | &tempbuff[8], 8); | |
131 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0], | |
132 | &tempbuff[16], 16); | |
133 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0], | |
134 | &tempbuff[32], 4); | |
e887afc9 WD |
135 | scsi_dev_desc[scsi_max_devs].target=pccb->target; |
136 | scsi_dev_desc[scsi_max_devs].lun=pccb->lun; | |
137 | ||
138 | pccb->datalen=0; | |
139 | scsi_setup_test_unit_ready(pccb); | |
472d5460 YS |
140 | if (scsi_exec(pccb) != true) { |
141 | if (scsi_dev_desc[scsi_max_devs].removable == true) { | |
e887afc9 WD |
142 | scsi_dev_desc[scsi_max_devs].type=perq; |
143 | goto removable; | |
144 | } | |
145 | scsi_print_error(pccb); | |
146 | continue; | |
147 | } | |
b4c5bbce | 148 | if (scsi_read_capacity(pccb, &capacity, &blksz)) { |
e887afc9 WD |
149 | scsi_print_error(pccb); |
150 | continue; | |
151 | } | |
e887afc9 WD |
152 | scsi_dev_desc[scsi_max_devs].lba=capacity; |
153 | scsi_dev_desc[scsi_max_devs].blksz=blksz; | |
0472fbfd EE |
154 | scsi_dev_desc[scsi_max_devs].log2blksz = |
155 | LOG2(scsi_dev_desc[scsi_max_devs].blksz); | |
e887afc9 WD |
156 | scsi_dev_desc[scsi_max_devs].type=perq; |
157 | init_part(&scsi_dev_desc[scsi_max_devs]); | |
158 | removable: | |
159 | if(mode==1) { | |
160 | printf (" Device %d: ", scsi_max_devs); | |
161 | dev_print(&scsi_dev_desc[scsi_max_devs]); | |
162 | } /* if mode */ | |
163 | scsi_max_devs++; | |
164 | } /* next LUN */ | |
165 | } | |
166 | if(scsi_max_devs>0) | |
167 | scsi_curr_dev=0; | |
168 | else | |
d0ff51ba | 169 | scsi_curr_dev = -1; |
447c031b SR |
170 | |
171 | printf("Found %d device(s).\n", scsi_max_devs); | |
fff40a7e | 172 | #ifndef CONFIG_SPL_BUILD |
447c031b | 173 | setenv_ulong("scsidevs", scsi_max_devs); |
fff40a7e | 174 | #endif |
447c031b SR |
175 | } |
176 | ||
177 | int scsi_get_disk_count(void) | |
178 | { | |
179 | return scsi_max_devs; | |
e887afc9 WD |
180 | } |
181 | ||
942e3143 | 182 | #ifdef CONFIG_PCI |
e887afc9 WD |
183 | void scsi_init(void) |
184 | { | |
185 | int busdevfunc; | |
4ae5eb7c VB |
186 | int i; |
187 | /* | |
188 | * Find a device from the list, this driver will support a single | |
189 | * controller. | |
190 | */ | |
191 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { | |
192 | /* get PCI Device ID */ | |
193 | busdevfunc = pci_find_device(scsi_device_list[i].vendor, | |
194 | scsi_device_list[i].device, | |
195 | 0); | |
196 | if (busdevfunc != -1) | |
197 | break; | |
198 | } | |
e887afc9 | 199 | |
4ae5eb7c VB |
200 | if (busdevfunc == -1) { |
201 | printf("Error: SCSI Controller(s) "); | |
202 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { | |
203 | printf("%04X:%04X ", | |
204 | scsi_device_list[i].vendor, | |
205 | scsi_device_list[i].device); | |
206 | } | |
207 | printf("not found\n"); | |
e887afc9 WD |
208 | return; |
209 | } | |
210 | #ifdef DEBUG | |
211 | else { | |
4ae5eb7c VB |
212 | printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n", |
213 | scsi_device_list[i].vendor, | |
214 | scsi_device_list[i].device, | |
215 | (busdevfunc >> 16) & 0xFF, | |
216 | (busdevfunc >> 11) & 0x1F, | |
217 | (busdevfunc >> 8) & 0x7); | |
e887afc9 WD |
218 | } |
219 | #endif | |
220 | scsi_low_level_init(busdevfunc); | |
221 | scsi_scan(1); | |
222 | } | |
942e3143 | 223 | #endif |
e887afc9 | 224 | |
df3fc526 | 225 | #ifdef CONFIG_PARTITIONS |
e887afc9 WD |
226 | block_dev_desc_t * scsi_get_dev(int dev) |
227 | { | |
6d0f6bcf | 228 | return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL; |
e887afc9 | 229 | } |
df3fc526 | 230 | #endif |
e887afc9 | 231 | |
e887afc9 WD |
232 | /****************************************************************************** |
233 | * scsi boot command intepreter. Derived from diskboot | |
234 | */ | |
54841ab5 | 235 | int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
e887afc9 | 236 | { |
7405a133 | 237 | return common_diskboot(cmdtp, "scsi", argc, argv); |
e887afc9 WD |
238 | } |
239 | ||
240 | /********************************************************************************* | |
241 | * scsi command intepreter | |
242 | */ | |
54841ab5 | 243 | int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
e887afc9 WD |
244 | { |
245 | switch (argc) { | |
4c12eeb8 SG |
246 | case 0: |
247 | case 1: | |
248 | return CMD_RET_USAGE; | |
47e26b1b | 249 | |
4c12eeb8 | 250 | case 2: |
e887afc9 WD |
251 | if (strncmp(argv[1],"res",3) == 0) { |
252 | printf("\nReset SCSI\n"); | |
253 | scsi_bus_reset(); | |
254 | scsi_scan(1); | |
255 | return 0; | |
256 | } | |
257 | if (strncmp(argv[1],"inf",3) == 0) { | |
258 | int i; | |
6d0f6bcf | 259 | for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) { |
e887afc9 WD |
260 | if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN) |
261 | continue; /* list only known devices */ | |
262 | printf ("SCSI dev. %d: ", i); | |
263 | dev_print(&scsi_dev_desc[i]); | |
264 | } | |
265 | return 0; | |
266 | } | |
267 | if (strncmp(argv[1],"dev",3) == 0) { | |
6d0f6bcf | 268 | if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) { |
e887afc9 WD |
269 | printf("\nno SCSI devices available\n"); |
270 | return 1; | |
271 | } | |
272 | printf ("\n Device %d: ", scsi_curr_dev); | |
273 | dev_print(&scsi_dev_desc[scsi_curr_dev]); | |
274 | return 0; | |
275 | } | |
276 | if (strncmp(argv[1],"scan",4) == 0) { | |
277 | scsi_scan(1); | |
278 | return 0; | |
279 | } | |
280 | if (strncmp(argv[1],"part",4) == 0) { | |
281 | int dev, ok; | |
6d0f6bcf | 282 | for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) { |
e887afc9 WD |
283 | if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) { |
284 | ok++; | |
285 | if (dev) | |
286 | printf("\n"); | |
1a344f29 | 287 | debug ("print_part of %x\n",dev); |
e887afc9 WD |
288 | print_part(&scsi_dev_desc[dev]); |
289 | } | |
290 | } | |
291 | if (!ok) | |
292 | printf("\nno SCSI devices available\n"); | |
293 | return 1; | |
294 | } | |
4c12eeb8 | 295 | return CMD_RET_USAGE; |
53677ef1 | 296 | case 3: |
e887afc9 WD |
297 | if (strncmp(argv[1],"dev",3) == 0) { |
298 | int dev = (int)simple_strtoul(argv[2], NULL, 10); | |
299 | printf ("\nSCSI device %d: ", dev); | |
6d0f6bcf | 300 | if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) { |
e887afc9 WD |
301 | printf("unknown device\n"); |
302 | return 1; | |
303 | } | |
304 | printf ("\n Device %d: ", dev); | |
305 | dev_print(&scsi_dev_desc[dev]); | |
306 | if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) { | |
307 | return 1; | |
308 | } | |
309 | scsi_curr_dev = dev; | |
310 | printf("... is now current device\n"); | |
311 | return 0; | |
312 | } | |
313 | if (strncmp(argv[1],"part",4) == 0) { | |
314 | int dev = (int)simple_strtoul(argv[2], NULL, 10); | |
315 | if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) { | |
316 | print_part(&scsi_dev_desc[dev]); | |
317 | } | |
318 | else { | |
319 | printf ("\nSCSI device %d not available\n", dev); | |
320 | } | |
321 | return 1; | |
322 | } | |
4c12eeb8 | 323 | return CMD_RET_USAGE; |
e887afc9 WD |
324 | default: |
325 | /* at least 4 args */ | |
326 | if (strcmp(argv[1],"read") == 0) { | |
327 | ulong addr = simple_strtoul(argv[2], NULL, 16); | |
328 | ulong blk = simple_strtoul(argv[3], NULL, 16); | |
329 | ulong cnt = simple_strtoul(argv[4], NULL, 16); | |
330 | ulong n; | |
331 | printf ("\nSCSI read: device %d block # %ld, count %ld ... ", | |
332 | scsi_curr_dev, blk, cnt); | |
333 | n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr); | |
334 | printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR"); | |
335 | return 0; | |
758c9e69 HTL |
336 | } else if (strcmp(argv[1], "write") == 0) { |
337 | ulong addr = simple_strtoul(argv[2], NULL, 16); | |
338 | ulong blk = simple_strtoul(argv[3], NULL, 16); | |
339 | ulong cnt = simple_strtoul(argv[4], NULL, 16); | |
340 | ulong n; | |
341 | printf("\nSCSI write: device %d block # %ld, " | |
342 | "count %ld ... ", | |
343 | scsi_curr_dev, blk, cnt); | |
344 | n = scsi_write(scsi_curr_dev, blk, cnt, | |
345 | (ulong *)addr); | |
346 | printf("%ld blocks written: %s\n", n, | |
347 | (n == cnt) ? "OK" : "ERROR"); | |
348 | return 0; | |
e887afc9 WD |
349 | } |
350 | } /* switch */ | |
4c12eeb8 | 351 | return CMD_RET_USAGE; |
e887afc9 WD |
352 | } |
353 | ||
354 | /**************************************************************************************** | |
355 | * scsi_read | |
356 | */ | |
357 | ||
358 | #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */ | |
359 | ||
4f6aa346 SG |
360 | static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt, |
361 | void *buffer) | |
e887afc9 | 362 | { |
758c9e69 HTL |
363 | lbaint_t start, blks; |
364 | uintptr_t buf_addr; | |
e887afc9 WD |
365 | unsigned short smallblks; |
366 | ccb* pccb=(ccb *)&tempccb; | |
367 | device&=0xff; | |
368 | /* Setup device | |
369 | */ | |
370 | pccb->target=scsi_dev_desc[device].target; | |
371 | pccb->lun=scsi_dev_desc[device].lun; | |
372 | buf_addr=(unsigned long)buffer; | |
373 | start=blknr; | |
374 | blks=blkcnt; | |
758c9e69 HTL |
375 | debug("\nscsi_read: dev %d startblk " LBAF |
376 | ", blccnt " LBAF " buffer %lx\n", | |
377 | device, start, blks, (unsigned long)buffer); | |
e887afc9 WD |
378 | do { |
379 | pccb->pdata=(unsigned char *)buf_addr; | |
380 | if(blks>SCSI_MAX_READ_BLK) { | |
381 | pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK; | |
382 | smallblks=SCSI_MAX_READ_BLK; | |
383 | scsi_setup_read_ext(pccb,start,smallblks); | |
384 | start+=SCSI_MAX_READ_BLK; | |
385 | blks-=SCSI_MAX_READ_BLK; | |
386 | } | |
387 | else { | |
388 | pccb->datalen=scsi_dev_desc[device].blksz * blks; | |
389 | smallblks=(unsigned short) blks; | |
390 | scsi_setup_read_ext(pccb,start,smallblks); | |
391 | start+=blks; | |
392 | blks=0; | |
393 | } | |
758c9e69 | 394 | debug("scsi_read_ext: startblk " LBAF |
f3609473 | 395 | ", blccnt %x buffer %" PRIXPTR "\n", |
758c9e69 | 396 | start, smallblks, buf_addr); |
472d5460 | 397 | if (scsi_exec(pccb) != true) { |
e887afc9 WD |
398 | scsi_print_error(pccb); |
399 | blkcnt-=blks; | |
400 | break; | |
401 | } | |
402 | buf_addr+=pccb->datalen; | |
403 | } while(blks!=0); | |
758c9e69 | 404 | debug("scsi_read_ext: end startblk " LBAF |
f3609473 | 405 | ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr); |
e887afc9 WD |
406 | return(blkcnt); |
407 | } | |
408 | ||
758c9e69 HTL |
409 | /******************************************************************************* |
410 | * scsi_write | |
411 | */ | |
412 | ||
413 | /* Almost the maximum amount of the scsi_ext command.. */ | |
414 | #define SCSI_MAX_WRITE_BLK 0xFFFF | |
415 | ||
4f6aa346 | 416 | static ulong scsi_write(int device, lbaint_t blknr, |
758c9e69 HTL |
417 | lbaint_t blkcnt, const void *buffer) |
418 | { | |
419 | lbaint_t start, blks; | |
420 | uintptr_t buf_addr; | |
421 | unsigned short smallblks; | |
422 | ccb* pccb = (ccb *)&tempccb; | |
423 | device &= 0xff; | |
424 | /* Setup device | |
425 | */ | |
426 | pccb->target = scsi_dev_desc[device].target; | |
427 | pccb->lun = scsi_dev_desc[device].lun; | |
428 | buf_addr = (unsigned long)buffer; | |
429 | start = blknr; | |
430 | blks = blkcnt; | |
431 | debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", | |
432 | __func__, device, start, blks, (unsigned long)buffer); | |
433 | do { | |
434 | pccb->pdata = (unsigned char *)buf_addr; | |
435 | if (blks > SCSI_MAX_WRITE_BLK) { | |
436 | pccb->datalen = (scsi_dev_desc[device].blksz * | |
437 | SCSI_MAX_WRITE_BLK); | |
438 | smallblks = SCSI_MAX_WRITE_BLK; | |
439 | scsi_setup_write_ext(pccb, start, smallblks); | |
440 | start += SCSI_MAX_WRITE_BLK; | |
441 | blks -= SCSI_MAX_WRITE_BLK; | |
442 | } else { | |
443 | pccb->datalen = scsi_dev_desc[device].blksz * blks; | |
444 | smallblks = (unsigned short)blks; | |
445 | scsi_setup_write_ext(pccb, start, smallblks); | |
446 | start += blks; | |
447 | blks = 0; | |
448 | } | |
f3609473 | 449 | debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", |
758c9e69 | 450 | __func__, start, smallblks, buf_addr); |
472d5460 | 451 | if (scsi_exec(pccb) != true) { |
758c9e69 HTL |
452 | scsi_print_error(pccb); |
453 | blkcnt -= blks; | |
454 | break; | |
455 | } | |
456 | buf_addr += pccb->datalen; | |
457 | } while (blks != 0); | |
f3609473 | 458 | debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", |
758c9e69 HTL |
459 | __func__, start, smallblks, buf_addr); |
460 | return blkcnt; | |
461 | } | |
462 | ||
e887afc9 WD |
463 | /* copy src to dest, skipping leading and trailing blanks |
464 | * and null terminate the string | |
465 | */ | |
466 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len) | |
467 | { | |
468 | int start,end; | |
469 | ||
470 | start=0; | |
471 | while(start<len) { | |
472 | if(src[start]!=' ') | |
473 | break; | |
474 | start++; | |
475 | } | |
476 | end=len-1; | |
477 | while(end>start) { | |
478 | if(src[end]!=' ') | |
479 | break; | |
480 | end--; | |
481 | } | |
482 | for( ; start<=end; start++) { | |
483 | *dest++=src[start]; | |
484 | } | |
485 | *dest='\0'; | |
486 | } | |
487 | ||
488 | ||
e887afc9 WD |
489 | /* Trim trailing blanks, and NUL-terminate string |
490 | */ | |
491 | void scsi_trim_trail (unsigned char *str, unsigned int len) | |
492 | { | |
493 | unsigned char *p = str + len - 1; | |
494 | ||
495 | while (len-- > 0) { | |
496 | *p-- = '\0'; | |
497 | if (*p != ' ') { | |
498 | return; | |
499 | } | |
500 | } | |
501 | } | |
502 | ||
b4c5bbce GB |
503 | int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, unsigned long *blksz) |
504 | { | |
505 | *capacity = 0; | |
506 | ||
507 | memset(pccb->cmd, 0, sizeof(pccb->cmd)); | |
508 | pccb->cmd[0] = SCSI_RD_CAPAC10; | |
509 | pccb->cmd[1] = pccb->lun << 5; | |
510 | pccb->cmdlen = 10; | |
511 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ | |
512 | ||
513 | pccb->datalen = 8; | |
472d5460 | 514 | if (scsi_exec(pccb) != true) |
b4c5bbce GB |
515 | return 1; |
516 | ||
517 | *capacity = ((lbaint_t)pccb->pdata[0] << 24) | | |
518 | ((lbaint_t)pccb->pdata[1] << 16) | | |
519 | ((lbaint_t)pccb->pdata[2] << 8) | | |
520 | ((lbaint_t)pccb->pdata[3]); | |
521 | ||
522 | if (*capacity != 0xffffffff) { | |
523 | /* Read capacity (10) was sufficient for this drive. */ | |
524 | *blksz = ((unsigned long)pccb->pdata[4] << 24) | | |
525 | ((unsigned long)pccb->pdata[5] << 16) | | |
526 | ((unsigned long)pccb->pdata[6] << 8) | | |
527 | ((unsigned long)pccb->pdata[7]); | |
528 | return 0; | |
529 | } | |
530 | ||
531 | /* Read capacity (10) was insufficient. Use read capacity (16). */ | |
532 | ||
533 | memset(pccb->cmd, 0, sizeof(pccb->cmd)); | |
534 | pccb->cmd[0] = SCSI_RD_CAPAC16; | |
535 | pccb->cmd[1] = 0x10; | |
536 | pccb->cmdlen = 16; | |
537 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ | |
538 | ||
539 | pccb->datalen = 16; | |
472d5460 | 540 | if (scsi_exec(pccb) != true) |
b4c5bbce GB |
541 | return 1; |
542 | ||
543 | *capacity = ((uint64_t)pccb->pdata[0] << 56) | | |
544 | ((uint64_t)pccb->pdata[1] << 48) | | |
545 | ((uint64_t)pccb->pdata[2] << 40) | | |
546 | ((uint64_t)pccb->pdata[3] << 32) | | |
547 | ((uint64_t)pccb->pdata[4] << 24) | | |
548 | ((uint64_t)pccb->pdata[5] << 16) | | |
549 | ((uint64_t)pccb->pdata[6] << 8) | | |
550 | ((uint64_t)pccb->pdata[7]); | |
551 | ||
552 | *blksz = ((uint64_t)pccb->pdata[8] << 56) | | |
553 | ((uint64_t)pccb->pdata[9] << 48) | | |
554 | ((uint64_t)pccb->pdata[10] << 40) | | |
555 | ((uint64_t)pccb->pdata[11] << 32) | | |
556 | ((uint64_t)pccb->pdata[12] << 24) | | |
557 | ((uint64_t)pccb->pdata[13] << 16) | | |
558 | ((uint64_t)pccb->pdata[14] << 8) | | |
559 | ((uint64_t)pccb->pdata[15]); | |
560 | ||
561 | return 0; | |
562 | } | |
563 | ||
e887afc9 WD |
564 | |
565 | /************************************************************************************ | |
566 | * Some setup (fill-in) routines | |
567 | */ | |
568 | void scsi_setup_test_unit_ready(ccb * pccb) | |
569 | { | |
570 | pccb->cmd[0]=SCSI_TST_U_RDY; | |
571 | pccb->cmd[1]=pccb->lun<<5; | |
572 | pccb->cmd[2]=0; | |
573 | pccb->cmd[3]=0; | |
574 | pccb->cmd[4]=0; | |
575 | pccb->cmd[5]=0; | |
576 | pccb->cmdlen=6; | |
577 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ | |
578 | } | |
579 | ||
e887afc9 WD |
580 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks) |
581 | { | |
582 | pccb->cmd[0]=SCSI_READ10; | |
583 | pccb->cmd[1]=pccb->lun<<5; | |
584 | pccb->cmd[2]=((unsigned char) (start>>24))&0xff; | |
585 | pccb->cmd[3]=((unsigned char) (start>>16))&0xff; | |
586 | pccb->cmd[4]=((unsigned char) (start>>8))&0xff; | |
587 | pccb->cmd[5]=((unsigned char) (start))&0xff; | |
588 | pccb->cmd[6]=0; | |
589 | pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff; | |
590 | pccb->cmd[8]=(unsigned char) blocks & 0xff; | |
591 | pccb->cmd[6]=0; | |
592 | pccb->cmdlen=10; | |
593 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ | |
1a344f29 | 594 | debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
e887afc9 WD |
595 | pccb->cmd[0],pccb->cmd[1], |
596 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5], | |
597 | pccb->cmd[7],pccb->cmd[8]); | |
598 | } | |
599 | ||
758c9e69 HTL |
600 | void scsi_setup_write_ext(ccb *pccb, unsigned long start, unsigned short blocks) |
601 | { | |
602 | pccb->cmd[0] = SCSI_WRITE10; | |
603 | pccb->cmd[1] = pccb->lun << 5; | |
604 | pccb->cmd[2] = ((unsigned char) (start>>24)) & 0xff; | |
605 | pccb->cmd[3] = ((unsigned char) (start>>16)) & 0xff; | |
606 | pccb->cmd[4] = ((unsigned char) (start>>8)) & 0xff; | |
607 | pccb->cmd[5] = ((unsigned char) (start)) & 0xff; | |
608 | pccb->cmd[6] = 0; | |
609 | pccb->cmd[7] = ((unsigned char) (blocks>>8)) & 0xff; | |
610 | pccb->cmd[8] = (unsigned char)blocks & 0xff; | |
611 | pccb->cmd[9] = 0; | |
612 | pccb->cmdlen = 10; | |
613 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ | |
614 | debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", | |
615 | __func__, | |
616 | pccb->cmd[0], pccb->cmd[1], | |
617 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], | |
618 | pccb->cmd[7], pccb->cmd[8]); | |
619 | } | |
620 | ||
e887afc9 WD |
621 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks) |
622 | { | |
623 | pccb->cmd[0]=SCSI_READ6; | |
624 | pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f); | |
625 | pccb->cmd[2]=((unsigned char) (start>>8))&0xff; | |
626 | pccb->cmd[3]=((unsigned char) (start))&0xff; | |
627 | pccb->cmd[4]=(unsigned char) blocks & 0xff; | |
628 | pccb->cmd[5]=0; | |
629 | pccb->cmdlen=6; | |
630 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ | |
1a344f29 | 631 | debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n", |
e887afc9 WD |
632 | pccb->cmd[0],pccb->cmd[1], |
633 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]); | |
634 | } | |
635 | ||
636 | ||
637 | void scsi_setup_inquiry(ccb * pccb) | |
638 | { | |
639 | pccb->cmd[0]=SCSI_INQUIRY; | |
640 | pccb->cmd[1]=pccb->lun<<5; | |
641 | pccb->cmd[2]=0; | |
642 | pccb->cmd[3]=0; | |
643 | if(pccb->datalen>255) | |
644 | pccb->cmd[4]=255; | |
645 | else | |
646 | pccb->cmd[4]=(unsigned char)pccb->datalen; | |
647 | pccb->cmd[5]=0; | |
648 | pccb->cmdlen=6; | |
649 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ | |
650 | } | |
651 | ||
460c322f WD |
652 | |
653 | U_BOOT_CMD( | |
654 | scsi, 5, 1, do_scsi, | |
2fb2604d | 655 | "SCSI sub-system", |
460c322f WD |
656 | "reset - reset SCSI controller\n" |
657 | "scsi info - show available SCSI devices\n" | |
658 | "scsi scan - (re-)scan SCSI bus\n" | |
659 | "scsi device [dev] - show or set current device\n" | |
660 | "scsi part [dev] - print partition table of one or all SCSI devices\n" | |
661 | "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" | |
758c9e69 HTL |
662 | " to memory address `addr'\n" |
663 | "scsi write addr blk# cnt - write `cnt' blocks starting at block\n" | |
664 | " `blk#' from memory address `addr'" | |
460c322f WD |
665 | ); |
666 | ||
667 | U_BOOT_CMD( | |
668 | scsiboot, 3, 1, do_scsiboot, | |
2fb2604d | 669 | "boot from SCSI device", |
a89c33db | 670 | "loadAddr dev:part" |
460c322f | 671 | ); |