blob: 7ec7ecc29580c1a1e4b02dc80fed9f25338d53cd [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 Glass099c2392017-06-14 21:28:35 -060017# ifdef CONFIG_SCSI_DEV_LIST
18# define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
19# else
20# ifdef CONFIG_SATA_ULI5288
Simon Glass11f610e2016-05-01 11:36:09 -060021
Simon Glass099c2392017-06-14 21:28:35 -060022# define SCSI_VEND_ID 0x10b9
23# define SCSI_DEV_ID 0x5288
Simon Glass11f610e2016-05-01 11:36:09 -060024
Simon Glass099c2392017-06-14 21:28:35 -060025# 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
Simon Glass7337fcd2017-06-14 21:28:47 -060032#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
33 !defined(CONFIG_DM_SCSI)
Simon Glass11f610e2016-05-01 11:36:09 -060034const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
35#endif
Simon Glassb9560ad2017-06-14 21:28:30 -060036static struct scsi_cmd tempccb; /* temporary scsi command buffer */
Simon Glass11f610e2016-05-01 11:36:09 -060037
38static unsigned char tempbuff[512]; /* temporary data buffer */
39
Michal Simeke8a016b2016-09-08 15:06:45 +020040#if !defined(CONFIG_DM_SCSI)
Simon Glass11f610e2016-05-01 11:36:09 -060041static int scsi_max_devs; /* number of highest available scsi device */
42
43static int scsi_curr_dev; /* current device */
44
45static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE];
Michal Simeke8a016b2016-09-08 15:06:45 +020046#endif
Simon Glass11f610e2016-05-01 11:36:09 -060047
48/* almost the maximum amount of the scsi_ext command.. */
49#define SCSI_MAX_READ_BLK 0xFFFF
50#define SCSI_LBA48_READ 0xFFFFFFF
51
Simon Glassb9560ad2017-06-14 21:28:30 -060052static void scsi_print_error(struct scsi_cmd *pccb)
Simon Glassa6fb1852017-06-14 21:28:23 -060053{
54 /* Dummy function that could print an error for debugging */
55}
56
Simon Glass11f610e2016-05-01 11:36:09 -060057#ifdef CONFIG_SYS_64BIT_LBA
Simon Glassb9560ad2017-06-14 21:28:30 -060058void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start,
59 unsigned long blocks)
Simon Glass11f610e2016-05-01 11:36:09 -060060{
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
Simon Glassb9560ad2017-06-14 21:28:30 -060087static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek545a2842016-11-30 11:53:43 +010088 unsigned short blocks)
Simon Glass11f610e2016-05-01 11:36:09 -060089{
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
Simon Glassb9560ad2017-06-14 21:28:30 -0600108static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek545a2842016-11-30 11:53:43 +0100109 unsigned short blocks)
Simon Glass11f610e2016-05-01 11:36:09 -0600110{
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
Simon Glassb9560ad2017-06-14 21:28:30 -0600130static void scsi_setup_inquiry(struct scsi_cmd *pccb)
Simon Glass11f610e2016-05-01 11:36:09 -0600131{
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
Simon Glass535556b2016-05-01 11:36:24 -0600145#ifdef CONFIG_BLK
146static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
147 void *buffer)
148#else
Simon Glass11f610e2016-05-01 11:36:09 -0600149static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr,
150 lbaint_t blkcnt, void *buffer)
Simon Glass535556b2016-05-01 11:36:24 -0600151#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600152{
Simon Glass535556b2016-05-01 11:36:24 -0600153#ifdef CONFIG_BLK
154 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
Simon Glass4682c8a2017-06-14 21:28:40 -0600155 struct udevice *bdev = dev->parent;
156#else
157 struct udevice *bdev = NULL;
Simon Glass535556b2016-05-01 11:36:24 -0600158#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600159 lbaint_t start, blks;
160 uintptr_t buf_addr;
161 unsigned short smallblks = 0;
Simon Glassb9560ad2017-06-14 21:28:30 -0600162 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glass11f610e2016-05-01 11:36:09 -0600163
164 /* Setup device */
Michal Simekcdb93b22016-11-18 16:22:42 +0100165 pccb->target = block_dev->target;
166 pccb->lun = block_dev->lun;
Simon Glass11f610e2016-05-01 11:36:09 -0600167 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",
Michal Simekcdb93b22016-11-18 16:22:42 +0100172 block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glass11f610e2016-05-01 11:36:09 -0600173 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);
Michal Simekcdb93b22016-11-18 16:22:42 +0100179 pccb->datalen = block_dev->blksz * blocks;
Simon Glass11f610e2016-05-01 11:36:09 -0600180 scsi_setup_read16(pccb, start, blocks);
181 start += blocks;
182 blks -= blocks;
183 } else
184#endif
185 if (blks > SCSI_MAX_READ_BLK) {
Michal Simekcdb93b22016-11-18 16:22:42 +0100186 pccb->datalen = block_dev->blksz *
Simon Glass11f610e2016-05-01 11:36:09 -0600187 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 {
Michal Simekcdb93b22016-11-18 16:22:42 +0100193 pccb->datalen = block_dev->blksz * blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600194 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);
Simon Glass4682c8a2017-06-14 21:28:40 -0600202 if (scsi_exec(bdev, pccb)) {
Simon Glass11f610e2016-05-01 11:36:09 -0600203 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
Simon Glass535556b2016-05-01 11:36:24 -0600221#ifdef CONFIG_BLK
222static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
223 const void *buffer)
224#else
Simon Glass11f610e2016-05-01 11:36:09 -0600225static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr,
226 lbaint_t blkcnt, const void *buffer)
Simon Glass535556b2016-05-01 11:36:24 -0600227#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600228{
Simon Glass535556b2016-05-01 11:36:24 -0600229#ifdef CONFIG_BLK
230 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
Simon Glass4682c8a2017-06-14 21:28:40 -0600231 struct udevice *bdev = dev->parent;
232#else
233 struct udevice *bdev = NULL;
Simon Glass535556b2016-05-01 11:36:24 -0600234#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600235 lbaint_t start, blks;
236 uintptr_t buf_addr;
237 unsigned short smallblks;
Simon Glassb9560ad2017-06-14 21:28:30 -0600238 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glass11f610e2016-05-01 11:36:09 -0600239
Simon Glass11f610e2016-05-01 11:36:09 -0600240 /* Setup device */
Michal Simekcdb93b22016-11-18 16:22:42 +0100241 pccb->target = block_dev->target;
242 pccb->lun = block_dev->lun;
Simon Glass11f610e2016-05-01 11:36:09 -0600243 buf_addr = (unsigned long)buffer;
244 start = blknr;
245 blks = blkcnt;
246 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
Michal Simekcdb93b22016-11-18 16:22:42 +0100247 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glass11f610e2016-05-01 11:36:09 -0600248 do {
249 pccb->pdata = (unsigned char *)buf_addr;
250 if (blks > SCSI_MAX_WRITE_BLK) {
Michal Simekcdb93b22016-11-18 16:22:42 +0100251 pccb->datalen = (block_dev->blksz *
Simon Glass11f610e2016-05-01 11:36:09 -0600252 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 {
Michal Simekcdb93b22016-11-18 16:22:42 +0100258 pccb->datalen = block_dev->blksz * blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600259 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);
Simon Glass4682c8a2017-06-14 21:28:40 -0600266 if (scsi_exec(bdev, pccb)) {
Simon Glass11f610e2016-05-01 11:36:09 -0600267 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
Simon Glass7337fcd2017-06-14 21:28:47 -0600278#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
279 !defined(CONFIG_DM_SCSI)
Simon Glass11f610e2016-05-01 11:36:09 -0600280void 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);
Simon Glass8eab1a52017-06-14 21:28:41 -0600331 scsi_scan(true);
Simon Glass11f610e2016-05-01 11:36:09 -0600332 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
333}
334#endif
335
Simon Glass11f610e2016-05-01 11:36:09 -0600336/* copy src to dest, skipping leading and trailing blanks
337 * and null terminate the string
338 */
Michal Simek545a2842016-11-30 11:53:43 +0100339static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
340 unsigned int len)
Simon Glass11f610e2016-05-01 11:36:09 -0600341{
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
Simon Glass4682c8a2017-06-14 21:28:40 -0600361static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb,
362 lbaint_t *capacity, unsigned long *blksz)
Simon Glass11f610e2016-05-01 11:36:09 -0600363{
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;
Simon Glassf6580ef2017-06-14 21:28:44 -0600373 if (scsi_exec(dev, pccb))
Simon Glass11f610e2016-05-01 11:36:09 -0600374 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;
Simon Glassf6580ef2017-06-14 21:28:44 -0600398 if (scsi_exec(dev, pccb))
Simon Glass11f610e2016-05-01 11:36:09 -0600399 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 */
Simon Glassb9560ad2017-06-14 21:28:30 -0600426static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
Simon Glass11f610e2016-05-01 11:36:09 -0600427{
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
Michal Simek0b3a58e2016-11-30 12:50:58 +0100438/**
439 * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
440 *
441 * @dev_desc: Block device description pointer
442 */
443static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
Michal Simek92ca4762016-11-18 15:27:00 +0100444{
445 dev_desc->target = 0xff;
446 dev_desc->lun = 0xff;
Michal Simek92ca4762016-11-18 15:27:00 +0100447 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;
Michal Simek92ca4762016-11-18 15:27:00 +0100454#ifndef CONFIG_BLK
455 dev_desc->block_read = scsi_read;
456 dev_desc->block_write = scsi_write;
457#endif
458}
459
Michal Simeke8a016b2016-09-08 15:06:45 +0200460#if !defined(CONFIG_DM_SCSI)
Michal Simek0b3a58e2016-11-30 12:50:58 +0100461/**
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 */
467static 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}
Michal Simeke8a016b2016-09-08 15:06:45 +0200477#endif
Michal Simekbccfd9e2016-11-18 16:14:24 +0100478
Michal Simek570712f2016-11-18 15:42:13 +0100479/**
480 * scsi_detect_dev - Detect scsi device
481 *
Michal Simekbccfd9e2016-11-18 16:14:24 +0100482 * @target: target id
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200483 * @lun: target lun
Michal Simek570712f2016-11-18 15:42:13 +0100484 * @dev_desc: block device description
Michal Simek570712f2016-11-18 15:42:13 +0100485 *
486 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200487 * detected.
Michal Simek570712f2016-11-18 15:42:13 +0100488 *
489 * Return: 0 on success, error value otherwise
490 */
Simon Glass4682c8a2017-06-14 21:28:40 -0600491static int scsi_detect_dev(struct udevice *dev, int target, int lun,
492 struct blk_desc *dev_desc)
Michal Simek570712f2016-11-18 15:42:13 +0100493{
494 unsigned char perq, modi;
495 lbaint_t capacity;
496 unsigned long blksz;
Simon Glassb9560ad2017-06-14 21:28:30 -0600497 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Michal Simek570712f2016-11-18 15:42:13 +0100498
Michal Simekbccfd9e2016-11-18 16:14:24 +0100499 pccb->target = target;
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200500 pccb->lun = lun;
Michal Simek570712f2016-11-18 15:42:13 +0100501 pccb->pdata = (unsigned char *)&tempbuff;
502 pccb->datalen = 512;
503 scsi_setup_inquiry(pccb);
Simon Glassf6580ef2017-06-14 21:28:44 -0600504 if (scsi_exec(dev, pccb)) {
Michal Simek570712f2016-11-18 15:42:13 +0100505 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);
Simon Glassf6580ef2017-06-14 21:28:44 -0600535 if (scsi_exec(dev, pccb)) {
Michal Simek570712f2016-11-18 15:42:13 +0100536 if (dev_desc->removable) {
537 dev_desc->type = perq;
538 goto removable;
539 }
540 scsi_print_error(pccb);
541 return -EINVAL;
542 }
Simon Glass4682c8a2017-06-14 21:28:40 -0600543 if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) {
Michal Simek570712f2016-11-18 15:42:13 +0100544 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;
Michal Simek570712f2016-11-18 15:42:13 +0100551removable:
552 return 0;
553}
554
Simon Glass11f610e2016-05-01 11:36:09 -0600555/*
556 * (re)-scan the scsi bus and reports scsi device info
557 * to the user if mode = 1
558 */
Michal Simeke8a016b2016-09-08 15:06:45 +0200559#if defined(CONFIG_DM_SCSI)
Simon Glass8eab1a52017-06-14 21:28:41 -0600560static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200561{
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);
Simon Glass4682c8a2017-06-14 21:28:40 -0600573 if (scsi_detect_dev(dev, id, lun, &bd))
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200574 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.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
Simon Glass8eab1a52017-06-14 21:28:41 -0600599 if (verbose) {
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200600 printf(" Device %d: ", 0);
601 dev_print(bdesc);
602 }
603 return 0;
604}
605
Simon Glass5c561762017-06-14 21:28:45 -0600606int 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
Simon Glass8eab1a52017-06-14 21:28:41 -0600628int scsi_scan(bool verbose)
Michal Simeke8a016b2016-09-08 15:06:45 +0200629{
Michal Simeke8a016b2016-09-08 15:06:45 +0200630 struct uclass *uc;
631 struct udevice *dev; /* SCSI controller */
632 int ret;
633
Simon Glass8eab1a52017-06-14 21:28:41 -0600634 if (verbose)
Michal Simeke8a016b2016-09-08 15:06:45 +0200635 printf("scanning bus for devices...\n");
636
Michal Simekf8f41ae2017-01-02 09:40:09 +0100637 blk_unbind_all(IF_TYPE_SCSI);
638
Michal Simeke8a016b2016-09-08 15:06:45 +0200639 ret = uclass_get(UCLASS_SCSI, &uc);
640 if (ret)
641 return ret;
642
643 uclass_foreach_dev(dev, uc) {
Simon Glass5c561762017-06-14 21:28:45 -0600644 ret = scsi_scan_dev(dev, verbose);
Michal Simeke8a016b2016-09-08 15:06:45 +0200645 if (ret)
646 return ret;
Michal Simeke8a016b2016-09-08 15:06:45 +0200647 }
648
649 return 0;
650}
651#else
Simon Glass8eab1a52017-06-14 21:28:41 -0600652int scsi_scan(bool verbose)
Simon Glass11f610e2016-05-01 11:36:09 -0600653{
Michal Simek570712f2016-11-18 15:42:13 +0100654 unsigned char i, lun;
655 int ret;
Simon Glass11f610e2016-05-01 11:36:09 -0600656
Simon Glass8eab1a52017-06-14 21:28:41 -0600657 if (verbose)
Simon Glass11f610e2016-05-01 11:36:09 -0600658 printf("scanning bus for devices...\n");
Michal Simek92ca4762016-11-18 15:27:00 +0100659 for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++)
660 scsi_init_dev_desc(&scsi_dev_desc[i], i);
661
Simon Glass11f610e2016-05-01 11:36:09 -0600662 scsi_max_devs = 0;
663 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
Simon Glass11f610e2016-05-01 11:36:09 -0600664 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
Simon Glass4682c8a2017-06-14 21:28:40 -0600665 ret = scsi_detect_dev(NULL, i, lun,
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200666 &scsi_dev_desc[scsi_max_devs]);
Michal Simek570712f2016-11-18 15:42:13 +0100667 if (ret)
Simon Glass11f610e2016-05-01 11:36:09 -0600668 continue;
Jean-Jacques Hiblot1330a722017-04-07 13:42:07 +0200669 part_init(&scsi_dev_desc[scsi_max_devs]);
Simon Glass11f610e2016-05-01 11:36:09 -0600670
Simon Glass8eab1a52017-06-14 21:28:41 -0600671 if (verbose) {
Michal Simek570712f2016-11-18 15:42:13 +0100672 printf(" Device %d: ", 0);
Simon Glass11f610e2016-05-01 11:36:09 -0600673 dev_print(&scsi_dev_desc[scsi_max_devs]);
Simon Glass8eab1a52017-06-14 21:28:41 -0600674 }
Simon Glass11f610e2016-05-01 11:36:09 -0600675 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 setenv_ulong("scsidevs", scsi_max_devs);
686#endif
Michal Simekc002e392016-11-30 12:12:31 +0100687 return 0;
Simon Glass11f610e2016-05-01 11:36:09 -0600688}
Michal Simeke8a016b2016-09-08 15:06:45 +0200689#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600690
Simon Glass535556b2016-05-01 11:36:24 -0600691#ifdef CONFIG_BLK
692static const struct blk_ops scsi_blk_ops = {
693 .read = scsi_read,
694 .write = scsi_write,
695};
696
697U_BOOT_DRIVER(scsi_blk) = {
698 .name = "scsi_blk",
699 .id = UCLASS_BLK,
700 .ops = &scsi_blk_ops,
701};
702#else
Simon Glass11f610e2016-05-01 11:36:09 -0600703U_BOOT_LEGACY_BLK(scsi) = {
Ed Swarthout69c125f2016-06-01 08:11:24 -0500704 .if_typename = "scsi",
Simon Glass11f610e2016-05-01 11:36:09 -0600705 .if_type = IF_TYPE_SCSI,
706 .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE,
707 .desc = scsi_dev_desc,
708};
Simon Glass535556b2016-05-01 11:36:24 -0600709#endif