blob: 2d0f81ad1f1ad51165033bb539c9bb2ca6313413 [file] [log] [blame]
Simon Glass11f610e2016-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 Glass535556b2016-05-01 11:36:24 -06009#include <dm.h>
Simon Glass11f610e2016-05-01 11:36:09 -060010#include <inttypes.h>
11#include <pci.h>
12#include <scsi.h>
Michal Simeke8a016b2016-09-08 15:06:45 +020013#include <dm/device-internal.h>
14#include <dm/uclass-internal.h>
Simon Glass11f610e2016-05-01 11:36:09 -060015
Michal Simeke8a016b2016-09-08 15:06:45 +020016#if !defined(CONFIG_DM_SCSI)
Simon Glass11f610e2016-05-01 11:36:09 -060017#ifdef CONFIG_SCSI_DEV_LIST
18#define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
19#else
Simon Glassb8beb6b2017-06-14 21:28:22 -060020#ifdef CONFIG_SATA_ULI5288
Simon Glass11f610e2016-05-01 11:36:09 -060021
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
Michal Simeke8a016b2016-09-08 15:06:45 +020030#endif
Simon Glass11f610e2016-05-01 11:36:09 -060031
32#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
33const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
34#endif
Simon Glassb9560ad2017-06-14 21:28:30 -060035static struct scsi_cmd tempccb; /* temporary scsi command buffer */
Simon Glass11f610e2016-05-01 11:36:09 -060036
37static unsigned char tempbuff[512]; /* temporary data buffer */
38
Michal Simeke8a016b2016-09-08 15:06:45 +020039#if !defined(CONFIG_DM_SCSI)
Simon Glass11f610e2016-05-01 11:36:09 -060040static int scsi_max_devs; /* number of highest available scsi device */
41
42static int scsi_curr_dev; /* current device */
43
44static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
Michal Simeke8a016b2016-09-08 15:06:45 +020045#endif
Simon Glass11f610e2016-05-01 11:36:09 -060046
47/* almost the maximum amount of the scsi_ext command.. */
48#define SCSI_MAX_READ_BLK 0xFFFF
49#define SCSI_LBA48_READ 0xFFFFFFF
50
Simon Glassb9560ad2017-06-14 21:28:30 -060051static void scsi_print_error(struct scsi_cmd *pccb)
Simon Glassa6fb1852017-06-14 21:28:23 -060052{
53 /* Dummy function that could print an error for debugging */
54}
55
Simon Glass11f610e2016-05-01 11:36:09 -060056#ifdef CONFIG_SYS_64BIT_LBA
Simon Glassb9560ad2017-06-14 21:28:30 -060057void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start,
58 unsigned long blocks)
Simon Glass11f610e2016-05-01 11:36:09 -060059{
60 pccb->cmd[0] = SCSI_READ16;
61 pccb->cmd[1] = pccb->lun << 5;
62 pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff;
63 pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff;
64 pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff;
65 pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff;
66 pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff;
67 pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff;
68 pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff;
69 pccb->cmd[9] = (unsigned char)start & 0xff;
70 pccb->cmd[10] = 0;
71 pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff;
72 pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff;
73 pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff;
74 pccb->cmd[14] = (unsigned char)blocks & 0xff;
75 pccb->cmd[15] = 0;
76 pccb->cmdlen = 16;
77 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
78 debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n",
79 pccb->cmd[0], pccb->cmd[1],
80 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
81 pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
82 pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
83}
84#endif
85
Simon Glassb9560ad2017-06-14 21:28:30 -060086static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek545a2842016-11-30 11:53:43 +010087 unsigned short blocks)
Simon Glass11f610e2016-05-01 11:36:09 -060088{
89 pccb->cmd[0] = SCSI_READ10;
90 pccb->cmd[1] = pccb->lun << 5;
91 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
92 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
93 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
94 pccb->cmd[5] = (unsigned char)start & 0xff;
95 pccb->cmd[6] = 0;
96 pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
97 pccb->cmd[8] = (unsigned char)blocks & 0xff;
98 pccb->cmd[6] = 0;
99 pccb->cmdlen = 10;
100 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
101 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
102 pccb->cmd[0], pccb->cmd[1],
103 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
104 pccb->cmd[7], pccb->cmd[8]);
105}
106
Simon Glassb9560ad2017-06-14 21:28:30 -0600107static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek545a2842016-11-30 11:53:43 +0100108 unsigned short blocks)
Simon Glass11f610e2016-05-01 11:36:09 -0600109{
110 pccb->cmd[0] = SCSI_WRITE10;
111 pccb->cmd[1] = pccb->lun << 5;
112 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
113 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
114 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
115 pccb->cmd[5] = (unsigned char)start & 0xff;
116 pccb->cmd[6] = 0;
117 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
118 pccb->cmd[8] = (unsigned char)blocks & 0xff;
119 pccb->cmd[9] = 0;
120 pccb->cmdlen = 10;
121 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
122 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
123 __func__,
124 pccb->cmd[0], pccb->cmd[1],
125 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
126 pccb->cmd[7], pccb->cmd[8]);
127}
128
Simon Glassb9560ad2017-06-14 21:28:30 -0600129static void scsi_setup_inquiry(struct scsi_cmd *pccb)
Simon Glass11f610e2016-05-01 11:36:09 -0600130{
131 pccb->cmd[0] = SCSI_INQUIRY;
132 pccb->cmd[1] = pccb->lun << 5;
133 pccb->cmd[2] = 0;
134 pccb->cmd[3] = 0;
135 if (pccb->datalen > 255)
136 pccb->cmd[4] = 255;
137 else
138 pccb->cmd[4] = (unsigned char)pccb->datalen;
139 pccb->cmd[5] = 0;
140 pccb->cmdlen = 6;
141 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
142}
143
Simon Glass535556b2016-05-01 11:36:24 -0600144#ifdef CONFIG_BLK
145static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
146 void *buffer)
147#else
Simon Glass11f610e2016-05-01 11:36:09 -0600148static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
149 lbaint_t blkcnt, void *buffer)
Simon Glass535556b2016-05-01 11:36:24 -0600150#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600151{
Simon Glass535556b2016-05-01 11:36:24 -0600152#ifdef CONFIG_BLK
153 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
154#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600155 lbaint_t start, blks;
156 uintptr_t buf_addr;
157 unsigned short smallblks = 0;
Simon Glassb9560ad2017-06-14 21:28:30 -0600158 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glass11f610e2016-05-01 11:36:09 -0600159
160 /* Setup device */
Michal Simekcdb93b22016-11-18 16:22:42 +0100161 pccb->target = block_dev->target;
162 pccb->lun = block_dev->lun;
Simon Glass11f610e2016-05-01 11:36:09 -0600163 buf_addr = (unsigned long)buffer;
164 start = blknr;
165 blks = blkcnt;
166 debug("\nscsi_read: dev %d startblk " LBAF
167 ", blccnt " LBAF " buffer %lx\n",
Michal Simekcdb93b22016-11-18 16:22:42 +0100168 block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glass11f610e2016-05-01 11:36:09 -0600169 do {
170 pccb->pdata = (unsigned char *)buf_addr;
171#ifdef CONFIG_SYS_64BIT_LBA
172 if (start > SCSI_LBA48_READ) {
173 unsigned long blocks;
174 blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK);
Michal Simekcdb93b22016-11-18 16:22:42 +0100175 pccb->datalen = block_dev->blksz * blocks;
Simon Glass11f610e2016-05-01 11:36:09 -0600176 scsi_setup_read16(pccb, start, blocks);
177 start += blocks;
178 blks -= blocks;
179 } else
180#endif
181 if (blks > SCSI_MAX_READ_BLK) {
Michal Simekcdb93b22016-11-18 16:22:42 +0100182 pccb->datalen = block_dev->blksz *
Simon Glass11f610e2016-05-01 11:36:09 -0600183 SCSI_MAX_READ_BLK;
184 smallblks = SCSI_MAX_READ_BLK;
185 scsi_setup_read_ext(pccb, start, smallblks);
186 start += SCSI_MAX_READ_BLK;
187 blks -= SCSI_MAX_READ_BLK;
188 } else {
Michal Simekcdb93b22016-11-18 16:22:42 +0100189 pccb->datalen = block_dev->blksz * blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600190 smallblks = (unsigned short)blks;
191 scsi_setup_read_ext(pccb, start, smallblks);
192 start += blks;
193 blks = 0;
194 }
195 debug("scsi_read_ext: startblk " LBAF
196 ", blccnt %x buffer %" PRIXPTR "\n",
197 start, smallblks, buf_addr);
198 if (scsi_exec(pccb) != true) {
199 scsi_print_error(pccb);
200 blkcnt -= blks;
201 break;
202 }
203 buf_addr += pccb->datalen;
204 } while (blks != 0);
205 debug("scsi_read_ext: end startblk " LBAF
206 ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr);
207 return blkcnt;
208}
209
210/*******************************************************************************
211 * scsi_write
212 */
213
214/* Almost the maximum amount of the scsi_ext command.. */
215#define SCSI_MAX_WRITE_BLK 0xFFFF
216
Simon Glass535556b2016-05-01 11:36:24 -0600217#ifdef CONFIG_BLK
218static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
219 const void *buffer)
220#else
Simon Glass11f610e2016-05-01 11:36:09 -0600221static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
222 lbaint_t blkcnt, const void *buffer)
Simon Glass535556b2016-05-01 11:36:24 -0600223#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600224{
Simon Glass535556b2016-05-01 11:36:24 -0600225#ifdef CONFIG_BLK
226 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
227#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600228 lbaint_t start, blks;
229 uintptr_t buf_addr;
230 unsigned short smallblks;
Simon Glassb9560ad2017-06-14 21:28:30 -0600231 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glass11f610e2016-05-01 11:36:09 -0600232
Simon Glass11f610e2016-05-01 11:36:09 -0600233 /* Setup device */
Michal Simekcdb93b22016-11-18 16:22:42 +0100234 pccb->target = block_dev->target;
235 pccb->lun = block_dev->lun;
Simon Glass11f610e2016-05-01 11:36:09 -0600236 buf_addr = (unsigned long)buffer;
237 start = blknr;
238 blks = blkcnt;
239 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
Michal Simekcdb93b22016-11-18 16:22:42 +0100240 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glass11f610e2016-05-01 11:36:09 -0600241 do {
242 pccb->pdata = (unsigned char *)buf_addr;
243 if (blks > SCSI_MAX_WRITE_BLK) {
Michal Simekcdb93b22016-11-18 16:22:42 +0100244 pccb->datalen = (block_dev->blksz *
Simon Glass11f610e2016-05-01 11:36:09 -0600245 SCSI_MAX_WRITE_BLK);
246 smallblks = SCSI_MAX_WRITE_BLK;
247 scsi_setup_write_ext(pccb, start, smallblks);
248 start += SCSI_MAX_WRITE_BLK;
249 blks -= SCSI_MAX_WRITE_BLK;
250 } else {
Michal Simekcdb93b22016-11-18 16:22:42 +0100251 pccb->datalen = block_dev->blksz * blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600252 smallblks = (unsigned short)blks;
253 scsi_setup_write_ext(pccb, start, smallblks);
254 start += blks;
255 blks = 0;
256 }
257 debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
258 __func__, start, smallblks, buf_addr);
259 if (scsi_exec(pccb) != true) {
260 scsi_print_error(pccb);
261 blkcnt -= blks;
262 break;
263 }
264 buf_addr += pccb->datalen;
265 } while (blks != 0);
266 debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n",
267 __func__, start, smallblks, buf_addr);
268 return blkcnt;
269}
270
Simon Glass11f610e2016-05-01 11:36:09 -0600271#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT)
272void scsi_init(void)
273{
274 int busdevfunc = -1;
275 int i;
276 /*
277 * Find a device from the list, this driver will support a single
278 * controller.
279 */
280 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
281 /* get PCI Device ID */
282#ifdef CONFIG_DM_PCI
283 struct udevice *dev;
284 int ret;
285
286 ret = dm_pci_find_device(scsi_device_list[i].vendor,
287 scsi_device_list[i].device, 0, &dev);
288 if (!ret) {
289 busdevfunc = dm_pci_get_bdf(dev);
290 break;
291 }
292#else
293 busdevfunc = pci_find_device(scsi_device_list[i].vendor,
294 scsi_device_list[i].device,
295 0);
296#endif
297 if (busdevfunc != -1)
298 break;
299 }
300
301 if (busdevfunc == -1) {
302 printf("Error: SCSI Controller(s) ");
303 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
304 printf("%04X:%04X ",
305 scsi_device_list[i].vendor,
306 scsi_device_list[i].device);
307 }
308 printf("not found\n");
309 return;
310 }
311#ifdef DEBUG
312 else {
313 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
314 scsi_device_list[i].vendor,
315 scsi_device_list[i].device,
316 (busdevfunc >> 16) & 0xFF,
317 (busdevfunc >> 11) & 0x1F,
318 (busdevfunc >> 8) & 0x7);
319 }
320#endif
321 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
322 scsi_low_level_init(busdevfunc);
323 scsi_scan(1);
324 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
325}
326#endif
327
Simon Glass11f610e2016-05-01 11:36:09 -0600328/* copy src to dest, skipping leading and trailing blanks
329 * and null terminate the string
330 */
Michal Simek545a2842016-11-30 11:53:43 +0100331static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
332 unsigned int len)
Simon Glass11f610e2016-05-01 11:36:09 -0600333{
334 int start, end;
335
336 start = 0;
337 while (start < len) {
338 if (src[start] != ' ')
339 break;
340 start++;
341 }
342 end = len-1;
343 while (end > start) {
344 if (src[end] != ' ')
345 break;
346 end--;
347 }
348 for (; start <= end; start++)
349 *dest ++= src[start];
350 *dest = '\0';
351}
352
Simon Glassb9560ad2017-06-14 21:28:30 -0600353static int scsi_read_capacity(struct scsi_cmd *pccb, lbaint_t *capacity,
Michal Simek545a2842016-11-30 11:53:43 +0100354 unsigned long *blksz)
Simon Glass11f610e2016-05-01 11:36:09 -0600355{
356 *capacity = 0;
357
358 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
359 pccb->cmd[0] = SCSI_RD_CAPAC10;
360 pccb->cmd[1] = pccb->lun << 5;
361 pccb->cmdlen = 10;
362 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
363
364 pccb->datalen = 8;
365 if (scsi_exec(pccb) != true)
366 return 1;
367
368 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
369 ((lbaint_t)pccb->pdata[1] << 16) |
370 ((lbaint_t)pccb->pdata[2] << 8) |
371 ((lbaint_t)pccb->pdata[3]);
372
373 if (*capacity != 0xffffffff) {
374 /* Read capacity (10) was sufficient for this drive. */
375 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
376 ((unsigned long)pccb->pdata[5] << 16) |
377 ((unsigned long)pccb->pdata[6] << 8) |
378 ((unsigned long)pccb->pdata[7]);
379 return 0;
380 }
381
382 /* Read capacity (10) was insufficient. Use read capacity (16). */
383 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
384 pccb->cmd[0] = SCSI_RD_CAPAC16;
385 pccb->cmd[1] = 0x10;
386 pccb->cmdlen = 16;
387 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
388
389 pccb->datalen = 16;
390 if (scsi_exec(pccb) != true)
391 return 1;
392
393 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
394 ((uint64_t)pccb->pdata[1] << 48) |
395 ((uint64_t)pccb->pdata[2] << 40) |
396 ((uint64_t)pccb->pdata[3] << 32) |
397 ((uint64_t)pccb->pdata[4] << 24) |
398 ((uint64_t)pccb->pdata[5] << 16) |
399 ((uint64_t)pccb->pdata[6] << 8) |
400 ((uint64_t)pccb->pdata[7]);
401
402 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
403 ((uint64_t)pccb->pdata[9] << 48) |
404 ((uint64_t)pccb->pdata[10] << 40) |
405 ((uint64_t)pccb->pdata[11] << 32) |
406 ((uint64_t)pccb->pdata[12] << 24) |
407 ((uint64_t)pccb->pdata[13] << 16) |
408 ((uint64_t)pccb->pdata[14] << 8) |
409 ((uint64_t)pccb->pdata[15]);
410
411 return 0;
412}
413
414
415/*
416 * Some setup (fill-in) routines
417 */
Simon Glassb9560ad2017-06-14 21:28:30 -0600418static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
Simon Glass11f610e2016-05-01 11:36:09 -0600419{
420 pccb->cmd[0] = SCSI_TST_U_RDY;
421 pccb->cmd[1] = pccb->lun << 5;
422 pccb->cmd[2] = 0;
423 pccb->cmd[3] = 0;
424 pccb->cmd[4] = 0;
425 pccb->cmd[5] = 0;
426 pccb->cmdlen = 6;
427 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
428}
429
Michal Simek0b3a58e2016-11-30 12:50:58 +0100430/**
431 * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
432 *
433 * @dev_desc: Block device description pointer
434 */
435static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
Michal Simek92ca4762016-11-18 15:27:00 +0100436{
437 dev_desc->target = 0xff;
438 dev_desc->lun = 0xff;
Michal Simek92ca4762016-11-18 15:27:00 +0100439 dev_desc->log2blksz =
440 LOG2_INVALID(typeof(dev_desc->log2blksz));
441 dev_desc->type = DEV_TYPE_UNKNOWN;
442 dev_desc->vendor[0] = 0;
443 dev_desc->product[0] = 0;
444 dev_desc->revision[0] = 0;
445 dev_desc->removable = false;
Michal Simek92ca4762016-11-18 15:27:00 +0100446#ifndef CONFIG_BLK
447 dev_desc->block_read = scsi_read;
448 dev_desc->block_write = scsi_write;
449#endif
450}
451
Michal Simeke8a016b2016-09-08 15:06:45 +0200452#if !defined(CONFIG_DM_SCSI)
Michal Simek0b3a58e2016-11-30 12:50:58 +0100453/**
454 * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties
455 *
456 * @dev_desc: Block device description pointer
457 * @devnum: Device number
458 */
459static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
460{
461 dev_desc->lba = 0;
462 dev_desc->blksz = 0;
463 dev_desc->if_type = IF_TYPE_SCSI;
464 dev_desc->devnum = devnum;
465 dev_desc->part_type = PART_TYPE_UNKNOWN;
466
467 scsi_init_dev_desc_priv(dev_desc);
468}
Michal Simeke8a016b2016-09-08 15:06:45 +0200469#endif
Michal Simekbccfd9e2016-11-18 16:14:24 +0100470
Michal Simek570712f2016-11-18 15:42:13 +0100471/**
472 * scsi_detect_dev - Detect scsi device
473 *
Michal Simekbccfd9e2016-11-18 16:14:24 +0100474 * @target: target id
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200475 * @lun: target lun
Michal Simek570712f2016-11-18 15:42:13 +0100476 * @dev_desc: block device description
Michal Simek570712f2016-11-18 15:42:13 +0100477 *
478 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200479 * detected.
Michal Simek570712f2016-11-18 15:42:13 +0100480 *
481 * Return: 0 on success, error value otherwise
482 */
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200483static int scsi_detect_dev(int target, int lun, struct blk_desc *dev_desc)
Michal Simek570712f2016-11-18 15:42:13 +0100484{
485 unsigned char perq, modi;
486 lbaint_t capacity;
487 unsigned long blksz;
Simon Glassb9560ad2017-06-14 21:28:30 -0600488 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Michal Simek570712f2016-11-18 15:42:13 +0100489
Michal Simekbccfd9e2016-11-18 16:14:24 +0100490 pccb->target = target;
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200491 pccb->lun = lun;
Michal Simek570712f2016-11-18 15:42:13 +0100492 pccb->pdata = (unsigned char *)&tempbuff;
493 pccb->datalen = 512;
494 scsi_setup_inquiry(pccb);
495 if (scsi_exec(pccb) != true) {
496 if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
497 /*
498 * selection timeout => assuming no
499 * device present
500 */
501 debug("Selection timeout ID %d\n",
502 pccb->target);
503 return -ETIMEDOUT;
504 }
505 scsi_print_error(pccb);
506 return -ENODEV;
507 }
508 perq = tempbuff[0];
509 modi = tempbuff[1];
510 if ((perq & 0x1f) == 0x1f)
511 return -ENODEV; /* skip unknown devices */
512 if ((modi & 0x80) == 0x80) /* drive is removable */
513 dev_desc->removable = true;
514 /* get info for this device */
515 scsi_ident_cpy((unsigned char *)dev_desc->vendor,
516 &tempbuff[8], 8);
517 scsi_ident_cpy((unsigned char *)dev_desc->product,
518 &tempbuff[16], 16);
519 scsi_ident_cpy((unsigned char *)dev_desc->revision,
520 &tempbuff[32], 4);
521 dev_desc->target = pccb->target;
522 dev_desc->lun = pccb->lun;
523
524 pccb->datalen = 0;
525 scsi_setup_test_unit_ready(pccb);
526 if (scsi_exec(pccb) != true) {
527 if (dev_desc->removable) {
528 dev_desc->type = perq;
529 goto removable;
530 }
531 scsi_print_error(pccb);
532 return -EINVAL;
533 }
534 if (scsi_read_capacity(pccb, &capacity, &blksz)) {
535 scsi_print_error(pccb);
536 return -EINVAL;
537 }
538 dev_desc->lba = capacity;
539 dev_desc->blksz = blksz;
540 dev_desc->log2blksz = LOG2(dev_desc->blksz);
541 dev_desc->type = perq;
Michal Simek570712f2016-11-18 15:42:13 +0100542removable:
543 return 0;
544}
545
Simon Glass11f610e2016-05-01 11:36:09 -0600546/*
547 * (re)-scan the scsi bus and reports scsi device info
548 * to the user if mode = 1
549 */
Michal Simeke8a016b2016-09-08 15:06:45 +0200550#if defined(CONFIG_DM_SCSI)
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200551static int do_scsi_scan_one(struct udevice *dev, int id, int lun, int mode)
552{
553 int ret;
554 struct udevice *bdev;
555 struct blk_desc bd;
556 struct blk_desc *bdesc;
557 char str[10];
558
559 /*
560 * detect the scsi driver to get information about its geometry (block
561 * size, number of blocks) and other parameters (ids, type, ...)
562 */
563 scsi_init_dev_desc_priv(&bd);
564 if (scsi_detect_dev(id, lun, &bd))
565 return -ENODEV;
566
567 /*
568 * Create only one block device and do detection
569 * to make sure that there won't be a lot of
570 * block devices created
571 */
572 snprintf(str, sizeof(str), "id%dlun%d", id, lun);
573 ret = blk_create_devicef(dev, "scsi_blk", str, IF_TYPE_SCSI, -1,
574 bd.blksz, bd.blksz * bd.lba, &bdev);
575 if (ret) {
576 debug("Can't create device\n");
577 return ret;
578 }
579
580 bdesc = dev_get_uclass_platdata(bdev);
581 bdesc->target = id;
582 bdesc->lun = lun;
583 bdesc->removable = bd.removable;
584 bdesc->type = bd.type;
585 memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
586 memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
587 memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision));
588 part_init(bdesc);
589
590 if (mode == 1) {
591 printf(" Device %d: ", 0);
592 dev_print(bdesc);
593 }
594 return 0;
595}
596
Michal Simeke8a016b2016-09-08 15:06:45 +0200597int scsi_scan(int mode)
598{
599 unsigned char i, lun;
600 struct uclass *uc;
601 struct udevice *dev; /* SCSI controller */
602 int ret;
603
604 if (mode == 1)
605 printf("scanning bus for devices...\n");
606
Michal Simekf8f41ae2017-01-02 09:40:09 +0100607 blk_unbind_all(IF_TYPE_SCSI);
608
Michal Simeke8a016b2016-09-08 15:06:45 +0200609 ret = uclass_get(UCLASS_SCSI, &uc);
610 if (ret)
611 return ret;
612
613 uclass_foreach_dev(dev, uc) {
614 struct scsi_platdata *plat; /* scsi controller platdata */
615
616 /* probe SCSI controller driver */
617 ret = device_probe(dev);
618 if (ret)
619 return ret;
620
621 /* Get controller platdata */
Simon Glass1dc64f62017-06-14 21:28:31 -0600622 plat = dev_get_uclass_platdata(dev);
Michal Simeke8a016b2016-09-08 15:06:45 +0200623
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200624 for (i = 0; i < plat->max_id; i++)
625 for (lun = 0; lun < plat->max_lun; lun++)
626 do_scsi_scan_one(dev, i, lun, mode);
Michal Simeke8a016b2016-09-08 15:06:45 +0200627 }
628
629 return 0;
630}
631#else
Michal Simekc002e392016-11-30 12:12:31 +0100632int scsi_scan(int mode)
Simon Glass11f610e2016-05-01 11:36:09 -0600633{
Michal Simek570712f2016-11-18 15:42:13 +0100634 unsigned char i, lun;
635 int ret;
Simon Glass11f610e2016-05-01 11:36:09 -0600636
637 if (mode == 1)
638 printf("scanning bus for devices...\n");
Michal Simek92ca4762016-11-18 15:27:00 +0100639 for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++)
640 scsi_init_dev_desc(&scsi_dev_desc[i], i);
641
Simon Glass11f610e2016-05-01 11:36:09 -0600642 scsi_max_devs = 0;
643 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
Simon Glass11f610e2016-05-01 11:36:09 -0600644 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200645 ret = scsi_detect_dev(i, lun,
646 &scsi_dev_desc[scsi_max_devs]);
Michal Simek570712f2016-11-18 15:42:13 +0100647 if (ret)
Simon Glass11f610e2016-05-01 11:36:09 -0600648 continue;
Jean-Jacques Hiblot1330a722017-04-07 13:42:07 +0200649 part_init(&scsi_dev_desc[scsi_max_devs]);
Simon Glass11f610e2016-05-01 11:36:09 -0600650
Simon Glass11f610e2016-05-01 11:36:09 -0600651 if (mode == 1) {
Michal Simek570712f2016-11-18 15:42:13 +0100652 printf(" Device %d: ", 0);
Simon Glass11f610e2016-05-01 11:36:09 -0600653 dev_print(&scsi_dev_desc[scsi_max_devs]);
654 } /* if mode */
655 scsi_max_devs++;
656 } /* next LUN */
657 }
658 if (scsi_max_devs > 0)
659 scsi_curr_dev = 0;
660 else
661 scsi_curr_dev = -1;
662
663 printf("Found %d device(s).\n", scsi_max_devs);
664#ifndef CONFIG_SPL_BUILD
665 setenv_ulong("scsidevs", scsi_max_devs);
666#endif
Michal Simekc002e392016-11-30 12:12:31 +0100667 return 0;
Simon Glass11f610e2016-05-01 11:36:09 -0600668}
Michal Simeke8a016b2016-09-08 15:06:45 +0200669#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600670
Simon Glass535556b2016-05-01 11:36:24 -0600671#ifdef CONFIG_BLK
672static const struct blk_ops scsi_blk_ops = {
673 .read = scsi_read,
674 .write = scsi_write,
675};
676
677U_BOOT_DRIVER(scsi_blk) = {
678 .name = "scsi_blk",
679 .id = UCLASS_BLK,
680 .ops = &scsi_blk_ops,
681};
682#else
Simon Glass11f610e2016-05-01 11:36:09 -0600683U_BOOT_LEGACY_BLK(scsi) = {
Ed Swarthout69c125f2016-06-01 08:11:24 -0500684 .if_typename = "scsi",
Simon Glass11f610e2016-05-01 11:36:09 -0600685 .if_type = IF_TYPE_SCSI,
686 .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE,
687 .desc = scsi_dev_desc,
688};
Simon Glass535556b2016-05-01 11:36:24 -0600689#endif