blob: 839b30b9bb8ee57e592be8b6f2e6ddcc153ec436 [file] [log] [blame]
Simon Glassc8e89ab2016-05-01 11:36:09 -06001/*
2 * (C) Copyright 2001
3 * Denis Peter, MPL AG Switzerland
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
Simon Glass5bf96dc2016-05-01 11:36:24 -06009#include <dm.h>
Simon Glassc8e89ab2016-05-01 11:36:09 -060010#include <inttypes.h>
11#include <pci.h>
12#include <scsi.h>
13
14#ifdef CONFIG_SCSI_DEV_LIST
15#define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
16#else
17#ifdef CONFIG_SCSI_SYM53C8XX
18#define SCSI_VEND_ID 0x1000
19#ifndef CONFIG_SCSI_DEV_ID
20#define SCSI_DEV_ID 0x0001
21#else
22#define SCSI_DEV_ID CONFIG_SCSI_DEV_ID
23#endif
24#elif defined CONFIG_SATA_ULI5288
25
26#define SCSI_VEND_ID 0x10b9
27#define SCSI_DEV_ID 0x5288
28
29#elif !defined(CONFIG_SCSI_AHCI_PLAT)
30#error no scsi device defined
31#endif
32#define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
33#endif
34
35#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
36const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
37#endif
38static ccb tempccb; /* temporary scsi command buffer */
39
40static unsigned char tempbuff[512]; /* temporary data buffer */
41
42static int scsi_max_devs; /* number of highest available scsi device */
43
44static int scsi_curr_dev; /* current device */
45
46static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
47
48/* almost the maximum amount of the scsi_ext command.. */
49#define SCSI_MAX_READ_BLK 0xFFFF
50#define SCSI_LBA48_READ 0xFFFFFFF
51
52#ifdef CONFIG_SYS_64BIT_LBA
53void scsi_setup_read16(ccb *pccb, lbaint_t start, unsigned long blocks)
54{
55 pccb->cmd[0] = SCSI_READ16;
56 pccb->cmd[1] = pccb->lun << 5;
57 pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff;
58 pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff;
59 pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff;
60 pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff;
61 pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff;
62 pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff;
63 pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff;
64 pccb->cmd[9] = (unsigned char)start & 0xff;
65 pccb->cmd[10] = 0;
66 pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff;
67 pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff;
68 pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff;
69 pccb->cmd[14] = (unsigned char)blocks & 0xff;
70 pccb->cmd[15] = 0;
71 pccb->cmdlen = 16;
72 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
73 debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n",
74 pccb->cmd[0], pccb->cmd[1],
75 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
76 pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
77 pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
78}
79#endif
80
Michal Simek9ec32f32016-11-30 11:53:43 +010081static void scsi_setup_read_ext(ccb *pccb, lbaint_t start,
82 unsigned short blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -060083{
84 pccb->cmd[0] = SCSI_READ10;
85 pccb->cmd[1] = pccb->lun << 5;
86 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
87 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
88 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
89 pccb->cmd[5] = (unsigned char)start & 0xff;
90 pccb->cmd[6] = 0;
91 pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
92 pccb->cmd[8] = (unsigned char)blocks & 0xff;
93 pccb->cmd[6] = 0;
94 pccb->cmdlen = 10;
95 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
96 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
97 pccb->cmd[0], pccb->cmd[1],
98 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
99 pccb->cmd[7], pccb->cmd[8]);
100}
101
Michal Simek9ec32f32016-11-30 11:53:43 +0100102static void scsi_setup_write_ext(ccb *pccb, lbaint_t start,
103 unsigned short blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600104{
105 pccb->cmd[0] = SCSI_WRITE10;
106 pccb->cmd[1] = pccb->lun << 5;
107 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
108 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
109 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
110 pccb->cmd[5] = (unsigned char)start & 0xff;
111 pccb->cmd[6] = 0;
112 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
113 pccb->cmd[8] = (unsigned char)blocks & 0xff;
114 pccb->cmd[9] = 0;
115 pccb->cmdlen = 10;
116 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
117 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
118 __func__,
119 pccb->cmd[0], pccb->cmd[1],
120 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
121 pccb->cmd[7], pccb->cmd[8]);
122}
123
Michal Simek9ec32f32016-11-30 11:53:43 +0100124static void scsi_setup_inquiry(ccb *pccb)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600125{
126 pccb->cmd[0] = SCSI_INQUIRY;
127 pccb->cmd[1] = pccb->lun << 5;
128 pccb->cmd[2] = 0;
129 pccb->cmd[3] = 0;
130 if (pccb->datalen > 255)
131 pccb->cmd[4] = 255;
132 else
133 pccb->cmd[4] = (unsigned char)pccb->datalen;
134 pccb->cmd[5] = 0;
135 pccb->cmdlen = 6;
136 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
137}
138
Simon Glass5bf96dc2016-05-01 11:36:24 -0600139#ifdef CONFIG_BLK
140static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
141 void *buffer)
142#else
Simon Glassc8e89ab2016-05-01 11:36:09 -0600143static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
144 lbaint_t blkcnt, void *buffer)
Simon Glass5bf96dc2016-05-01 11:36:24 -0600145#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -0600146{
Simon Glass5bf96dc2016-05-01 11:36:24 -0600147#ifdef CONFIG_BLK
148 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
149#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -0600150 lbaint_t start, blks;
151 uintptr_t buf_addr;
152 unsigned short smallblks = 0;
153 ccb *pccb = (ccb *)&tempccb;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600154
155 /* Setup device */
Michal Simekde6840a2016-11-18 16:22:42 +0100156 pccb->target = block_dev->target;
157 pccb->lun = block_dev->lun;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600158 buf_addr = (unsigned long)buffer;
159 start = blknr;
160 blks = blkcnt;
161 debug("\nscsi_read: dev %d startblk " LBAF
162 ", blccnt " LBAF " buffer %lx\n",
Michal Simekde6840a2016-11-18 16:22:42 +0100163 block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600164 do {
165 pccb->pdata = (unsigned char *)buf_addr;
166#ifdef CONFIG_SYS_64BIT_LBA
167 if (start > SCSI_LBA48_READ) {
168 unsigned long blocks;
169 blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK);
Michal Simekde6840a2016-11-18 16:22:42 +0100170 pccb->datalen = block_dev->blksz * blocks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600171 scsi_setup_read16(pccb, start, blocks);
172 start += blocks;
173 blks -= blocks;
174 } else
175#endif
176 if (blks > SCSI_MAX_READ_BLK) {
Michal Simekde6840a2016-11-18 16:22:42 +0100177 pccb->datalen = block_dev->blksz *
Simon Glassc8e89ab2016-05-01 11:36:09 -0600178 SCSI_MAX_READ_BLK;
179 smallblks = SCSI_MAX_READ_BLK;
180 scsi_setup_read_ext(pccb, start, smallblks);
181 start += SCSI_MAX_READ_BLK;
182 blks -= SCSI_MAX_READ_BLK;
183 } else {
Michal Simekde6840a2016-11-18 16:22:42 +0100184 pccb->datalen = block_dev->blksz * blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600185 smallblks = (unsigned short)blks;
186 scsi_setup_read_ext(pccb, start, smallblks);
187 start += blks;
188 blks = 0;
189 }
190 debug("scsi_read_ext: startblk " LBAF
191 ", blccnt %x buffer %" PRIXPTR "\n",
192 start, smallblks, buf_addr);
193 if (scsi_exec(pccb) != true) {
194 scsi_print_error(pccb);
195 blkcnt -= blks;
196 break;
197 }
198 buf_addr += pccb->datalen;
199 } while (blks != 0);
200 debug("scsi_read_ext: end startblk " LBAF
201 ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr);
202 return blkcnt;
203}
204
205/*******************************************************************************
206 * scsi_write
207 */
208
209/* Almost the maximum amount of the scsi_ext command.. */
210#define SCSI_MAX_WRITE_BLK 0xFFFF
211
Simon Glass5bf96dc2016-05-01 11:36:24 -0600212#ifdef CONFIG_BLK
213static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
214 const void *buffer)
215#else
Simon Glassc8e89ab2016-05-01 11:36:09 -0600216static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
217 lbaint_t blkcnt, const void *buffer)
Simon Glass5bf96dc2016-05-01 11:36:24 -0600218#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -0600219{
Simon Glass5bf96dc2016-05-01 11:36:24 -0600220#ifdef CONFIG_BLK
221 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
222#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -0600223 lbaint_t start, blks;
224 uintptr_t buf_addr;
225 unsigned short smallblks;
226 ccb *pccb = (ccb *)&tempccb;
227
Simon Glassc8e89ab2016-05-01 11:36:09 -0600228 /* Setup device */
Michal Simekde6840a2016-11-18 16:22:42 +0100229 pccb->target = block_dev->target;
230 pccb->lun = block_dev->lun;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600231 buf_addr = (unsigned long)buffer;
232 start = blknr;
233 blks = blkcnt;
234 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
Michal Simekde6840a2016-11-18 16:22:42 +0100235 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600236 do {
237 pccb->pdata = (unsigned char *)buf_addr;
238 if (blks > SCSI_MAX_WRITE_BLK) {
Michal Simekde6840a2016-11-18 16:22:42 +0100239 pccb->datalen = (block_dev->blksz *
Simon Glassc8e89ab2016-05-01 11:36:09 -0600240 SCSI_MAX_WRITE_BLK);
241 smallblks = SCSI_MAX_WRITE_BLK;
242 scsi_setup_write_ext(pccb, start, smallblks);
243 start += SCSI_MAX_WRITE_BLK;
244 blks -= SCSI_MAX_WRITE_BLK;
245 } else {
Michal Simekde6840a2016-11-18 16:22:42 +0100246 pccb->datalen = block_dev->blksz * blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600247 smallblks = (unsigned short)blks;
248 scsi_setup_write_ext(pccb, start, smallblks);
249 start += blks;
250 blks = 0;
251 }
252 debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
253 __func__, start, smallblks, buf_addr);
254 if (scsi_exec(pccb) != true) {
255 scsi_print_error(pccb);
256 blkcnt -= blks;
257 break;
258 }
259 buf_addr += pccb->datalen;
260 } while (blks != 0);
261 debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
262 __func__, start, smallblks, buf_addr);
263 return blkcnt;
264}
265
Simon Glassc8e89ab2016-05-01 11:36:09 -0600266#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
267void scsi_init(void)
268{
269 int busdevfunc = -1;
270 int i;
271 /*
272 * Find a device from the list, this driver will support a single
273 * controller.
274 */
275 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
276 /* get PCI Device ID */
277#ifdef CONFIG_DM_PCI
278 struct udevice *dev;
279 int ret;
280
281 ret = dm_pci_find_device(scsi_device_list[i].vendor,
282 scsi_device_list[i].device, 0, &dev);
283 if (!ret) {
284 busdevfunc = dm_pci_get_bdf(dev);
285 break;
286 }
287#else
288 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
289 scsi_device_list[i].device,
290 0);
291#endif
292 if (busdevfunc != -1)
293 break;
294 }
295
296 if (busdevfunc == -1) {
297 printf("Error: SCSI Controller(s) ");
298 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
299 printf("%04X:%04X ",
300 scsi_device_list[i].vendor,
301 scsi_device_list[i].device);
302 }
303 printf("not found\n");
304 return;
305 }
306#ifdef DEBUG
307 else {
308 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
309 scsi_device_list[i].vendor,
310 scsi_device_list[i].device,
311 (busdevfunc >> 16) & 0xFF,
312 (busdevfunc >> 11) & 0x1F,
313 (busdevfunc >> 8) & 0x7);
314 }
315#endif
316 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
317 scsi_low_level_init(busdevfunc);
318 scsi_scan(1);
319 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
320}
321#endif
322
Simon Glassc8e89ab2016-05-01 11:36:09 -0600323/* copy src to dest, skipping leading and trailing blanks
324 * and null terminate the string
325 */
Michal Simek9ec32f32016-11-30 11:53:43 +0100326static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
327 unsigned int len)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600328{
329 int start, end;
330
331 start = 0;
332 while (start < len) {
333 if (src[start] != ' ')
334 break;
335 start++;
336 }
337 end = len-1;
338 while (end > start) {
339 if (src[end] != ' ')
340 break;
341 end--;
342 }
343 for (; start <= end; start++)
344 *dest ++= src[start];
345 *dest = '\0';
346}
347
Michal Simek9ec32f32016-11-30 11:53:43 +0100348static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity,
349 unsigned long *blksz)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600350{
351 *capacity = 0;
352
353 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
354 pccb->cmd[0] = SCSI_RD_CAPAC10;
355 pccb->cmd[1] = pccb->lun << 5;
356 pccb->cmdlen = 10;
357 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
358
359 pccb->datalen = 8;
360 if (scsi_exec(pccb) != true)
361 return 1;
362
363 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
364 ((lbaint_t)pccb->pdata[1] << 16) |
365 ((lbaint_t)pccb->pdata[2] << 8) |
366 ((lbaint_t)pccb->pdata[3]);
367
368 if (*capacity != 0xffffffff) {
369 /* Read capacity (10) was sufficient for this drive. */
370 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
371 ((unsigned long)pccb->pdata[5] << 16) |
372 ((unsigned long)pccb->pdata[6] << 8) |
373 ((unsigned long)pccb->pdata[7]);
374 return 0;
375 }
376
377 /* Read capacity (10) was insufficient. Use read capacity (16). */
378 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
379 pccb->cmd[0] = SCSI_RD_CAPAC16;
380 pccb->cmd[1] = 0x10;
381 pccb->cmdlen = 16;
382 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
383
384 pccb->datalen = 16;
385 if (scsi_exec(pccb) != true)
386 return 1;
387
388 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
389 ((uint64_t)pccb->pdata[1] << 48) |
390 ((uint64_t)pccb->pdata[2] << 40) |
391 ((uint64_t)pccb->pdata[3] << 32) |
392 ((uint64_t)pccb->pdata[4] << 24) |
393 ((uint64_t)pccb->pdata[5] << 16) |
394 ((uint64_t)pccb->pdata[6] << 8) |
395 ((uint64_t)pccb->pdata[7]);
396
397 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
398 ((uint64_t)pccb->pdata[9] << 48) |
399 ((uint64_t)pccb->pdata[10] << 40) |
400 ((uint64_t)pccb->pdata[11] << 32) |
401 ((uint64_t)pccb->pdata[12] << 24) |
402 ((uint64_t)pccb->pdata[13] << 16) |
403 ((uint64_t)pccb->pdata[14] << 8) |
404 ((uint64_t)pccb->pdata[15]);
405
406 return 0;
407}
408
409
410/*
411 * Some setup (fill-in) routines
412 */
Michal Simek9ec32f32016-11-30 11:53:43 +0100413static void scsi_setup_test_unit_ready(ccb *pccb)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600414{
415 pccb->cmd[0] = SCSI_TST_U_RDY;
416 pccb->cmd[1] = pccb->lun << 5;
417 pccb->cmd[2] = 0;
418 pccb->cmd[3] = 0;
419 pccb->cmd[4] = 0;
420 pccb->cmd[5] = 0;
421 pccb->cmdlen = 6;
422 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
423}
424
Michal Simekac0a18d12016-11-18 15:27:00 +0100425static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
426{
427 dev_desc->target = 0xff;
428 dev_desc->lun = 0xff;
429 dev_desc->lba = 0;
430 dev_desc->blksz = 0;
431 dev_desc->log2blksz =
432 LOG2_INVALID(typeof(dev_desc->log2blksz));
433 dev_desc->type = DEV_TYPE_UNKNOWN;
434 dev_desc->vendor[0] = 0;
435 dev_desc->product[0] = 0;
436 dev_desc->revision[0] = 0;
437 dev_desc->removable = false;
438 dev_desc->if_type = IF_TYPE_SCSI;
439 dev_desc->devnum = devnum;
440 dev_desc->part_type = PART_TYPE_UNKNOWN;
441#ifndef CONFIG_BLK
442 dev_desc->block_read = scsi_read;
443 dev_desc->block_write = scsi_write;
444#endif
445}
446
Michal Simek40917492016-11-18 16:14:24 +0100447
Michal Simekebbde652016-11-18 15:42:13 +0100448/**
449 * scsi_detect_dev - Detect scsi device
450 *
Michal Simek40917492016-11-18 16:14:24 +0100451 * @target: target id
Michal Simekebbde652016-11-18 15:42:13 +0100452 * @dev_desc: block device description
Michal Simekebbde652016-11-18 15:42:13 +0100453 *
454 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
Michal Simek3c0073b2016-11-18 15:52:51 +0100455 * detected. The LUN number is taken from the struct blk_desc *dev_desc.
Michal Simekebbde652016-11-18 15:42:13 +0100456 *
457 * Return: 0 on success, error value otherwise
458 */
Michal Simek40917492016-11-18 16:14:24 +0100459static int scsi_detect_dev(int target, struct blk_desc *dev_desc)
Michal Simekebbde652016-11-18 15:42:13 +0100460{
461 unsigned char perq, modi;
462 lbaint_t capacity;
463 unsigned long blksz;
Michal Simek40917492016-11-18 16:14:24 +0100464 ccb *pccb = (ccb *)&tempccb;
Michal Simekebbde652016-11-18 15:42:13 +0100465
Michal Simek40917492016-11-18 16:14:24 +0100466 pccb->target = target;
Michal Simek3c0073b2016-11-18 15:52:51 +0100467 pccb->lun = dev_desc->lun;
Michal Simekebbde652016-11-18 15:42:13 +0100468 pccb->pdata = (unsigned char *)&tempbuff;
469 pccb->datalen = 512;
470 scsi_setup_inquiry(pccb);
471 if (scsi_exec(pccb) != true) {
472 if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
473 /*
474 * selection timeout => assuming no
475 * device present
476 */
477 debug("Selection timeout ID %d\n",
478 pccb->target);
479 return -ETIMEDOUT;
480 }
481 scsi_print_error(pccb);
482 return -ENODEV;
483 }
484 perq = tempbuff[0];
485 modi = tempbuff[1];
486 if ((perq & 0x1f) == 0x1f)
487 return -ENODEV; /* skip unknown devices */
488 if ((modi & 0x80) == 0x80) /* drive is removable */
489 dev_desc->removable = true;
490 /* get info for this device */
491 scsi_ident_cpy((unsigned char *)dev_desc->vendor,
492 &tempbuff[8], 8);
493 scsi_ident_cpy((unsigned char *)dev_desc->product,
494 &tempbuff[16], 16);
495 scsi_ident_cpy((unsigned char *)dev_desc->revision,
496 &tempbuff[32], 4);
497 dev_desc->target = pccb->target;
498 dev_desc->lun = pccb->lun;
499
500 pccb->datalen = 0;
501 scsi_setup_test_unit_ready(pccb);
502 if (scsi_exec(pccb) != true) {
503 if (dev_desc->removable) {
504 dev_desc->type = perq;
505 goto removable;
506 }
507 scsi_print_error(pccb);
508 return -EINVAL;
509 }
510 if (scsi_read_capacity(pccb, &capacity, &blksz)) {
511 scsi_print_error(pccb);
512 return -EINVAL;
513 }
514 dev_desc->lba = capacity;
515 dev_desc->blksz = blksz;
516 dev_desc->log2blksz = LOG2(dev_desc->blksz);
517 dev_desc->type = perq;
518 part_init(&dev_desc[0]);
519removable:
520 return 0;
521}
522
Simon Glassc8e89ab2016-05-01 11:36:09 -0600523/*
524 * (re)-scan the scsi bus and reports scsi device info
525 * to the user if mode = 1
526 */
Michal Simek5dc196f2016-11-30 12:12:31 +0100527int scsi_scan(int mode)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600528{
Michal Simekebbde652016-11-18 15:42:13 +0100529 unsigned char i, lun;
530 int ret;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600531
532 if (mode == 1)
533 printf("scanning bus for devices...\n");
Michal Simekac0a18d12016-11-18 15:27:00 +0100534 for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++)
535 scsi_init_dev_desc(&scsi_dev_desc[i], i);
536
Simon Glassc8e89ab2016-05-01 11:36:09 -0600537 scsi_max_devs = 0;
538 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
Simon Glassc8e89ab2016-05-01 11:36:09 -0600539 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
Michal Simek3c0073b2016-11-18 15:52:51 +0100540 scsi_dev_desc[scsi_max_devs].lun = lun;
Michal Simek40917492016-11-18 16:14:24 +0100541 ret = scsi_detect_dev(i, &scsi_dev_desc[scsi_max_devs]);
Michal Simekebbde652016-11-18 15:42:13 +0100542 if (ret)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600543 continue;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600544
Simon Glassc8e89ab2016-05-01 11:36:09 -0600545 if (mode == 1) {
Michal Simekebbde652016-11-18 15:42:13 +0100546 printf(" Device %d: ", 0);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600547 dev_print(&scsi_dev_desc[scsi_max_devs]);
548 } /* if mode */
549 scsi_max_devs++;
550 } /* next LUN */
551 }
552 if (scsi_max_devs > 0)
553 scsi_curr_dev = 0;
554 else
555 scsi_curr_dev = -1;
556
557 printf("Found %d device(s).\n", scsi_max_devs);
558#ifndef CONFIG_SPL_BUILD
559 setenv_ulong("scsidevs", scsi_max_devs);
560#endif
Michal Simek5dc196f2016-11-30 12:12:31 +0100561 return 0;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600562}
563
Simon Glass5bf96dc2016-05-01 11:36:24 -0600564#ifdef CONFIG_BLK
565static const struct blk_ops scsi_blk_ops = {
566 .read = scsi_read,
567 .write = scsi_write,
568};
569
570U_BOOT_DRIVER(scsi_blk) = {
571 .name = "scsi_blk",
572 .id = UCLASS_BLK,
573 .ops = &scsi_blk_ops,
574};
575#else
Simon Glassc8e89ab2016-05-01 11:36:09 -0600576U_BOOT_LEGACY_BLK(scsi) = {
Ed Swarthout987a9552016-06-01 08:11:24 -0500577 .if_typename = "scsi",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600578 .if_type = IF_TYPE_SCSI,
579 .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE,
580 .desc = scsi_dev_desc,
581};
Simon Glass5bf96dc2016-05-01 11:36:24 -0600582#endif