xref: /openbmc/u-boot/drivers/scsi/scsi.c (revision 3991f42e)
1 /*
2  * (C) Copyright 2001
3  * Denis Peter, MPL AG Switzerland
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <inttypes.h>
11 #include <pci.h>
12 #include <scsi.h>
13 #include <dm/device-internal.h>
14 #include <dm/uclass-internal.h>
15 
16 #if !defined(CONFIG_DM_SCSI)
17 # ifdef CONFIG_SCSI_DEV_LIST
18 #  define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
19 # else
20 #  ifdef CONFIG_SATA_ULI5288
21 
22 #   define SCSI_VEND_ID 0x10b9
23 #   define SCSI_DEV_ID  0x5288
24 
25 #  elif !defined(CONFIG_SCSI_AHCI_PLAT)
26 #   error no scsi device defined
27 #  endif
28 # define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
29 # endif
30 #endif
31 
32 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
33 	!defined(CONFIG_DM_SCSI)
34 const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
35 #endif
36 static struct scsi_cmd tempccb;	/* temporary scsi command buffer */
37 
38 static unsigned char tempbuff[512]; /* temporary data buffer */
39 
40 #if !defined(CONFIG_DM_SCSI)
41 static int scsi_max_devs; /* number of highest available scsi device */
42 
43 static int scsi_curr_dev; /* current device */
44 
45 static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
46 #endif
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 static void scsi_print_error(struct scsi_cmd *pccb)
53 {
54 	/* Dummy function that could print an error for debugging */
55 }
56 
57 #ifdef CONFIG_SYS_64BIT_LBA
58 void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start,
59 		       unsigned long blocks)
60 {
61 	pccb->cmd[0] = SCSI_READ16;
62 	pccb->cmd[1] = pccb->lun << 5;
63 	pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff;
64 	pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff;
65 	pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff;
66 	pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff;
67 	pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff;
68 	pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff;
69 	pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff;
70 	pccb->cmd[9] = (unsigned char)start & 0xff;
71 	pccb->cmd[10] = 0;
72 	pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff;
73 	pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff;
74 	pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff;
75 	pccb->cmd[14] = (unsigned char)blocks & 0xff;
76 	pccb->cmd[15] = 0;
77 	pccb->cmdlen = 16;
78 	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
79 	debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n",
80 	      pccb->cmd[0], pccb->cmd[1],
81 	      pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
82 	      pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
83 	      pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
84 }
85 #endif
86 
87 static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
88 				unsigned short blocks)
89 {
90 	pccb->cmd[0] = SCSI_READ10;
91 	pccb->cmd[1] = pccb->lun << 5;
92 	pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
93 	pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
94 	pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
95 	pccb->cmd[5] = (unsigned char)start & 0xff;
96 	pccb->cmd[6] = 0;
97 	pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
98 	pccb->cmd[8] = (unsigned char)blocks & 0xff;
99 	pccb->cmd[6] = 0;
100 	pccb->cmdlen = 10;
101 	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
102 	debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
103 	      pccb->cmd[0], pccb->cmd[1],
104 	      pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
105 	      pccb->cmd[7], pccb->cmd[8]);
106 }
107 
108 static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
109 				 unsigned short blocks)
110 {
111 	pccb->cmd[0] = SCSI_WRITE10;
112 	pccb->cmd[1] = pccb->lun << 5;
113 	pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
114 	pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
115 	pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
116 	pccb->cmd[5] = (unsigned char)start & 0xff;
117 	pccb->cmd[6] = 0;
118 	pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
119 	pccb->cmd[8] = (unsigned char)blocks & 0xff;
120 	pccb->cmd[9] = 0;
121 	pccb->cmdlen = 10;
122 	pccb->msgout[0] = SCSI_IDENTIFY;  /* NOT USED */
123 	debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
124 	      __func__,
125 	      pccb->cmd[0], pccb->cmd[1],
126 	      pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
127 	      pccb->cmd[7], pccb->cmd[8]);
128 }
129 
130 static void scsi_setup_inquiry(struct scsi_cmd *pccb)
131 {
132 	pccb->cmd[0] = SCSI_INQUIRY;
133 	pccb->cmd[1] = pccb->lun << 5;
134 	pccb->cmd[2] = 0;
135 	pccb->cmd[3] = 0;
136 	if (pccb->datalen > 255)
137 		pccb->cmd[4] = 255;
138 	else
139 		pccb->cmd[4] = (unsigned char)pccb->datalen;
140 	pccb->cmd[5] = 0;
141 	pccb->cmdlen = 6;
142 	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
143 }
144 
145 #ifdef CONFIG_BLK
146 static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
147 		       void *buffer)
148 #else
149 static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
150 		       lbaint_t blkcnt, void *buffer)
151 #endif
152 {
153 #ifdef CONFIG_BLK
154 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
155 	struct udevice *bdev = dev->parent;
156 #else
157 	struct udevice *bdev = NULL;
158 #endif
159 	lbaint_t start, blks;
160 	uintptr_t buf_addr;
161 	unsigned short smallblks = 0;
162 	struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
163 
164 	/* Setup device */
165 	pccb->target = block_dev->target;
166 	pccb->lun = block_dev->lun;
167 	buf_addr = (unsigned long)buffer;
168 	start = blknr;
169 	blks = blkcnt;
170 	debug("\nscsi_read: dev %d startblk " LBAF
171 	      ", blccnt " LBAF " buffer %lx\n",
172 	      block_dev->devnum, start, blks, (unsigned long)buffer);
173 	do {
174 		pccb->pdata = (unsigned char *)buf_addr;
175 #ifdef CONFIG_SYS_64BIT_LBA
176 		if (start > SCSI_LBA48_READ) {
177 			unsigned long blocks;
178 			blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK);
179 			pccb->datalen = block_dev->blksz * blocks;
180 			scsi_setup_read16(pccb, start, blocks);
181 			start += blocks;
182 			blks -= blocks;
183 		} else
184 #endif
185 		if (blks > SCSI_MAX_READ_BLK) {
186 			pccb->datalen = block_dev->blksz *
187 				SCSI_MAX_READ_BLK;
188 			smallblks = SCSI_MAX_READ_BLK;
189 			scsi_setup_read_ext(pccb, start, smallblks);
190 			start += SCSI_MAX_READ_BLK;
191 			blks -= SCSI_MAX_READ_BLK;
192 		} else {
193 			pccb->datalen = block_dev->blksz * blks;
194 			smallblks = (unsigned short)blks;
195 			scsi_setup_read_ext(pccb, start, smallblks);
196 			start += blks;
197 			blks = 0;
198 		}
199 		debug("scsi_read_ext: startblk " LBAF
200 		      ", blccnt %x buffer %" PRIXPTR "\n",
201 		      start, smallblks, buf_addr);
202 		if (scsi_exec(bdev, pccb)) {
203 			scsi_print_error(pccb);
204 			blkcnt -= blks;
205 			break;
206 		}
207 		buf_addr += pccb->datalen;
208 	} while (blks != 0);
209 	debug("scsi_read_ext: end startblk " LBAF
210 	      ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr);
211 	return blkcnt;
212 }
213 
214 /*******************************************************************************
215  * scsi_write
216  */
217 
218 /* Almost the maximum amount of the scsi_ext command.. */
219 #define SCSI_MAX_WRITE_BLK 0xFFFF
220 
221 #ifdef CONFIG_BLK
222 static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
223 			const void *buffer)
224 #else
225 static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
226 			lbaint_t blkcnt, const void *buffer)
227 #endif
228 {
229 #ifdef CONFIG_BLK
230 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
231 	struct udevice *bdev = dev->parent;
232 #else
233 	struct udevice *bdev = NULL;
234 #endif
235 	lbaint_t start, blks;
236 	uintptr_t buf_addr;
237 	unsigned short smallblks;
238 	struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
239 
240 	/* Setup device */
241 	pccb->target = block_dev->target;
242 	pccb->lun = block_dev->lun;
243 	buf_addr = (unsigned long)buffer;
244 	start = blknr;
245 	blks = blkcnt;
246 	debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
247 	      __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
248 	do {
249 		pccb->pdata = (unsigned char *)buf_addr;
250 		if (blks > SCSI_MAX_WRITE_BLK) {
251 			pccb->datalen = (block_dev->blksz *
252 					 SCSI_MAX_WRITE_BLK);
253 			smallblks = SCSI_MAX_WRITE_BLK;
254 			scsi_setup_write_ext(pccb, start, smallblks);
255 			start += SCSI_MAX_WRITE_BLK;
256 			blks -= SCSI_MAX_WRITE_BLK;
257 		} else {
258 			pccb->datalen = block_dev->blksz * blks;
259 			smallblks = (unsigned short)blks;
260 			scsi_setup_write_ext(pccb, start, smallblks);
261 			start += blks;
262 			blks = 0;
263 		}
264 		debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
265 		      __func__, start, smallblks, buf_addr);
266 		if (scsi_exec(bdev, pccb)) {
267 			scsi_print_error(pccb);
268 			blkcnt -= blks;
269 			break;
270 		}
271 		buf_addr += pccb->datalen;
272 	} while (blks != 0);
273 	debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
274 	      __func__, start, smallblks, buf_addr);
275 	return blkcnt;
276 }
277 
278 #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
279 	!defined(CONFIG_DM_SCSI)
280 void scsi_init(void)
281 {
282 	int busdevfunc = -1;
283 	int i;
284 	/*
285 	 * Find a device from the list, this driver will support a single
286 	 * controller.
287 	 */
288 	for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
289 		/* get PCI Device ID */
290 #ifdef CONFIG_DM_PCI
291 		struct udevice *dev;
292 		int ret;
293 
294 		ret = dm_pci_find_device(scsi_device_list[i].vendor,
295 					 scsi_device_list[i].device, 0, &dev);
296 		if (!ret) {
297 			busdevfunc = dm_pci_get_bdf(dev);
298 			break;
299 		}
300 #else
301 		busdevfunc = pci_find_device(scsi_device_list[i].vendor,
302 					     scsi_device_list[i].device,
303 					     0);
304 #endif
305 		if (busdevfunc != -1)
306 			break;
307 	}
308 
309 	if (busdevfunc == -1) {
310 		printf("Error: SCSI Controller(s) ");
311 		for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
312 			printf("%04X:%04X ",
313 			       scsi_device_list[i].vendor,
314 			       scsi_device_list[i].device);
315 		}
316 		printf("not found\n");
317 		return;
318 	}
319 #ifdef DEBUG
320 	else {
321 		printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
322 		       scsi_device_list[i].vendor,
323 		       scsi_device_list[i].device,
324 		       (busdevfunc >> 16) & 0xFF,
325 		       (busdevfunc >> 11) & 0x1F,
326 		       (busdevfunc >> 8) & 0x7);
327 	}
328 #endif
329 	bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
330 	scsi_low_level_init(busdevfunc);
331 	scsi_scan(true);
332 	bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
333 }
334 #endif
335 
336 /* copy src to dest, skipping leading and trailing blanks
337  * and null terminate the string
338  */
339 static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
340 			   unsigned int len)
341 {
342 	int start, end;
343 
344 	start = 0;
345 	while (start < len) {
346 		if (src[start] != ' ')
347 			break;
348 		start++;
349 	}
350 	end = len-1;
351 	while (end > start) {
352 		if (src[end] != ' ')
353 			break;
354 		end--;
355 	}
356 	for (; start <= end; start++)
357 		*dest ++= src[start];
358 	*dest = '\0';
359 }
360 
361 static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb,
362 			      lbaint_t *capacity, unsigned long *blksz)
363 {
364 	*capacity = 0;
365 
366 	memset(pccb->cmd, '\0', sizeof(pccb->cmd));
367 	pccb->cmd[0] = SCSI_RD_CAPAC10;
368 	pccb->cmd[1] = pccb->lun << 5;
369 	pccb->cmdlen = 10;
370 	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
371 
372 	pccb->datalen = 8;
373 	if (scsi_exec(dev, pccb))
374 		return 1;
375 
376 	*capacity = ((lbaint_t)pccb->pdata[0] << 24) |
377 		    ((lbaint_t)pccb->pdata[1] << 16) |
378 		    ((lbaint_t)pccb->pdata[2] << 8)  |
379 		    ((lbaint_t)pccb->pdata[3]);
380 
381 	if (*capacity != 0xffffffff) {
382 		/* Read capacity (10) was sufficient for this drive. */
383 		*blksz = ((unsigned long)pccb->pdata[4] << 24) |
384 			 ((unsigned long)pccb->pdata[5] << 16) |
385 			 ((unsigned long)pccb->pdata[6] << 8)  |
386 			 ((unsigned long)pccb->pdata[7]);
387 		return 0;
388 	}
389 
390 	/* Read capacity (10) was insufficient. Use read capacity (16). */
391 	memset(pccb->cmd, '\0', sizeof(pccb->cmd));
392 	pccb->cmd[0] = SCSI_RD_CAPAC16;
393 	pccb->cmd[1] = 0x10;
394 	pccb->cmdlen = 16;
395 	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
396 
397 	pccb->datalen = 16;
398 	if (scsi_exec(dev, pccb))
399 		return 1;
400 
401 	*capacity = ((uint64_t)pccb->pdata[0] << 56) |
402 		    ((uint64_t)pccb->pdata[1] << 48) |
403 		    ((uint64_t)pccb->pdata[2] << 40) |
404 		    ((uint64_t)pccb->pdata[3] << 32) |
405 		    ((uint64_t)pccb->pdata[4] << 24) |
406 		    ((uint64_t)pccb->pdata[5] << 16) |
407 		    ((uint64_t)pccb->pdata[6] << 8)  |
408 		    ((uint64_t)pccb->pdata[7]);
409 
410 	*blksz = ((uint64_t)pccb->pdata[8]  << 56) |
411 		 ((uint64_t)pccb->pdata[9]  << 48) |
412 		 ((uint64_t)pccb->pdata[10] << 40) |
413 		 ((uint64_t)pccb->pdata[11] << 32) |
414 		 ((uint64_t)pccb->pdata[12] << 24) |
415 		 ((uint64_t)pccb->pdata[13] << 16) |
416 		 ((uint64_t)pccb->pdata[14] << 8)  |
417 		 ((uint64_t)pccb->pdata[15]);
418 
419 	return 0;
420 }
421 
422 
423 /*
424  * Some setup (fill-in) routines
425  */
426 static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
427 {
428 	pccb->cmd[0] = SCSI_TST_U_RDY;
429 	pccb->cmd[1] = pccb->lun << 5;
430 	pccb->cmd[2] = 0;
431 	pccb->cmd[3] = 0;
432 	pccb->cmd[4] = 0;
433 	pccb->cmd[5] = 0;
434 	pccb->cmdlen = 6;
435 	pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
436 }
437 
438 /**
439  * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
440  *
441  * @dev_desc: Block device description pointer
442  */
443 static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
444 {
445 	dev_desc->target = 0xff;
446 	dev_desc->lun = 0xff;
447 	dev_desc->log2blksz =
448 		LOG2_INVALID(typeof(dev_desc->log2blksz));
449 	dev_desc->type = DEV_TYPE_UNKNOWN;
450 	dev_desc->vendor[0] = 0;
451 	dev_desc->product[0] = 0;
452 	dev_desc->revision[0] = 0;
453 	dev_desc->removable = false;
454 #if !CONFIG_IS_ENABLED(BLK)
455 	dev_desc->block_read = scsi_read;
456 	dev_desc->block_write = scsi_write;
457 #endif
458 }
459 
460 #if !defined(CONFIG_DM_SCSI)
461 /**
462  * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties
463  *
464  * @dev_desc: Block device description pointer
465  * @devnum: Device number
466  */
467 static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
468 {
469 	dev_desc->lba = 0;
470 	dev_desc->blksz = 0;
471 	dev_desc->if_type = IF_TYPE_SCSI;
472 	dev_desc->devnum = devnum;
473 	dev_desc->part_type = PART_TYPE_UNKNOWN;
474 
475 	scsi_init_dev_desc_priv(dev_desc);
476 }
477 #endif
478 
479 /**
480  * scsi_detect_dev - Detect scsi device
481  *
482  * @target: target id
483  * @lun: target lun
484  * @dev_desc: block device description
485  *
486  * The scsi_detect_dev detects and fills a dev_desc structure when the device is
487  * detected.
488  *
489  * Return: 0 on success, error value otherwise
490  */
491 static int scsi_detect_dev(struct udevice *dev, int target, int lun,
492 			   struct blk_desc *dev_desc)
493 {
494 	unsigned char perq, modi;
495 	lbaint_t capacity;
496 	unsigned long blksz;
497 	struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
498 
499 	pccb->target = target;
500 	pccb->lun = lun;
501 	pccb->pdata = (unsigned char *)&tempbuff;
502 	pccb->datalen = 512;
503 	scsi_setup_inquiry(pccb);
504 	if (scsi_exec(dev, pccb)) {
505 		if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
506 			/*
507 			  * selection timeout => assuming no
508 			  * device present
509 			  */
510 			debug("Selection timeout ID %d\n",
511 			      pccb->target);
512 			return -ETIMEDOUT;
513 		}
514 		scsi_print_error(pccb);
515 		return -ENODEV;
516 	}
517 	perq = tempbuff[0];
518 	modi = tempbuff[1];
519 	if ((perq & 0x1f) == 0x1f)
520 		return -ENODEV; /* skip unknown devices */
521 	if ((modi & 0x80) == 0x80) /* drive is removable */
522 		dev_desc->removable = true;
523 	/* get info for this device */
524 	scsi_ident_cpy((unsigned char *)dev_desc->vendor,
525 		       &tempbuff[8], 8);
526 	scsi_ident_cpy((unsigned char *)dev_desc->product,
527 		       &tempbuff[16], 16);
528 	scsi_ident_cpy((unsigned char *)dev_desc->revision,
529 		       &tempbuff[32], 4);
530 	dev_desc->target = pccb->target;
531 	dev_desc->lun = pccb->lun;
532 
533 	pccb->datalen = 0;
534 	scsi_setup_test_unit_ready(pccb);
535 	if (scsi_exec(dev, pccb)) {
536 		if (dev_desc->removable) {
537 			dev_desc->type = perq;
538 			goto removable;
539 		}
540 		scsi_print_error(pccb);
541 		return -EINVAL;
542 	}
543 	if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) {
544 		scsi_print_error(pccb);
545 		return -EINVAL;
546 	}
547 	dev_desc->lba = capacity;
548 	dev_desc->blksz = blksz;
549 	dev_desc->log2blksz = LOG2(dev_desc->blksz);
550 	dev_desc->type = perq;
551 removable:
552 	return 0;
553 }
554 
555 /*
556  * (re)-scan the scsi bus and reports scsi device info
557  * to the user if mode = 1
558  */
559 #if defined(CONFIG_DM_SCSI)
560 static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
561 {
562 	int ret;
563 	struct udevice *bdev;
564 	struct blk_desc bd;
565 	struct blk_desc *bdesc;
566 	char str[10];
567 
568 	/*
569 	 * detect the scsi driver to get information about its geometry (block
570 	 * size, number of blocks) and other parameters (ids, type, ...)
571 	 */
572 	scsi_init_dev_desc_priv(&bd);
573 	if (scsi_detect_dev(dev, id, lun, &bd))
574 		return -ENODEV;
575 
576 	/*
577 	* Create only one block device and do detection
578 	* to make sure that there won't be a lot of
579 	* block devices created
580 	*/
581 	snprintf(str, sizeof(str), "id%dlun%d", id, lun);
582 	ret = blk_create_devicef(dev, "scsi_blk", str, IF_TYPE_SCSI, -1,
583 			bd.blksz, bd.lba, &bdev);
584 	if (ret) {
585 		debug("Can't create device\n");
586 		return ret;
587 	}
588 
589 	bdesc = dev_get_uclass_platdata(bdev);
590 	bdesc->target = id;
591 	bdesc->lun = lun;
592 	bdesc->removable = bd.removable;
593 	bdesc->type = bd.type;
594 	memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
595 	memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
596 	memcpy(&bdesc->revision, &bd.revision,	sizeof(bd.revision));
597 	part_init(bdesc);
598 
599 	if (verbose) {
600 		printf("  Device %d: ", 0);
601 		dev_print(bdesc);
602 	}
603 	return 0;
604 }
605 
606 int scsi_scan_dev(struct udevice *dev, bool verbose)
607 {
608 	struct scsi_platdata *uc_plat; /* scsi controller platdata */
609 	int ret;
610 	int i;
611 	int lun;
612 
613 	/* probe SCSI controller driver */
614 	ret = device_probe(dev);
615 	if (ret)
616 		return ret;
617 
618 	/* Get controller platdata */
619 	uc_plat = dev_get_uclass_platdata(dev);
620 
621 	for (i = 0; i < uc_plat->max_id; i++)
622 		for (lun = 0; lun < uc_plat->max_lun; lun++)
623 			do_scsi_scan_one(dev, i, lun, verbose);
624 
625 	return 0;
626 }
627 
628 int scsi_scan(bool verbose)
629 {
630 	struct uclass *uc;
631 	struct udevice *dev; /* SCSI controller */
632 	int ret;
633 
634 	if (verbose)
635 		printf("scanning bus for devices...\n");
636 
637 	blk_unbind_all(IF_TYPE_SCSI);
638 
639 	ret = uclass_get(UCLASS_SCSI, &uc);
640 	if (ret)
641 		return ret;
642 
643 	uclass_foreach_dev(dev, uc) {
644 		ret = scsi_scan_dev(dev, verbose);
645 		if (ret)
646 			return ret;
647 	}
648 
649 	return 0;
650 }
651 #else
652 int scsi_scan(bool verbose)
653 {
654 	unsigned char i, lun;
655 	int ret;
656 
657 	if (verbose)
658 		printf("scanning bus for devices...\n");
659 	for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++)
660 		scsi_init_dev_desc(&scsi_dev_desc[i], i);
661 
662 	scsi_max_devs = 0;
663 	for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
664 		for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
665 			ret = scsi_detect_dev(NULL, i, lun,
666 					      &scsi_dev_desc[scsi_max_devs]);
667 			if (ret)
668 				continue;
669 			part_init(&scsi_dev_desc[scsi_max_devs]);
670 
671 			if (verbose) {
672 				printf("  Device %d: ", 0);
673 				dev_print(&scsi_dev_desc[scsi_max_devs]);
674 			}
675 			scsi_max_devs++;
676 		} /* next LUN */
677 	}
678 	if (scsi_max_devs > 0)
679 		scsi_curr_dev = 0;
680 	else
681 		scsi_curr_dev = -1;
682 
683 	printf("Found %d device(s).\n", scsi_max_devs);
684 #ifndef CONFIG_SPL_BUILD
685 	env_set_ulong("scsidevs", scsi_max_devs);
686 #endif
687 	return 0;
688 }
689 #endif
690 
691 #ifdef CONFIG_BLK
692 static const struct blk_ops scsi_blk_ops = {
693 	.read	= scsi_read,
694 	.write	= scsi_write,
695 };
696 
697 U_BOOT_DRIVER(scsi_blk) = {
698 	.name		= "scsi_blk",
699 	.id		= UCLASS_BLK,
700 	.ops		= &scsi_blk_ops,
701 };
702 #else
703 U_BOOT_LEGACY_BLK(scsi) = {
704 	.if_typename	= "scsi",
705 	.if_type	= IF_TYPE_SCSI,
706 	.max_devs	= CONFIG_SYS_SCSI_MAX_DEVICE,
707 	.desc		= scsi_dev_desc,
708 };
709 #endif
710