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