blob: 69de6a53d56a3d226f8f44f56f1aa345283ec4e6 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass11f610e2016-05-01 11:36:09 -06002/*
3 * (C) Copyright 2001
4 * Denis Peter, MPL AG Switzerland
Simon Glass11f610e2016-05-01 11:36:09 -06005 */
6
7#include <common.h>
Simon Glass535556b2016-05-01 11:36:24 -06008#include <dm.h>
Simon Glass168068f2019-08-01 09:46:47 -06009#include <env.h>
Simon Glass11f610e2016-05-01 11:36:09 -060010#include <pci.h>
11#include <scsi.h>
Michal Simeke8a016b2016-09-08 15:06:45 +020012#include <dm/device-internal.h>
13#include <dm/uclass-internal.h>
Simon Glass11f610e2016-05-01 11:36:09 -060014
Michal Simeke8a016b2016-09-08 15:06:45 +020015#if !defined(CONFIG_DM_SCSI)
Simon Glass099c2392017-06-14 21:28:35 -060016# ifdef CONFIG_SCSI_DEV_LIST
17# define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
18# else
19# ifdef CONFIG_SATA_ULI5288
Simon Glass11f610e2016-05-01 11:36:09 -060020
Simon Glass099c2392017-06-14 21:28:35 -060021# define SCSI_VEND_ID 0x10b9
22# define SCSI_DEV_ID 0x5288
Simon Glass11f610e2016-05-01 11:36:09 -060023
Simon Glass099c2392017-06-14 21:28:35 -060024# elif !defined(CONFIG_SCSI_AHCI_PLAT)
25# error no scsi device defined
26# endif
27# define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
28# endif
Michal Simeke8a016b2016-09-08 15:06:45 +020029#endif
Simon Glass11f610e2016-05-01 11:36:09 -060030
Simon Glass7337fcd2017-06-14 21:28:47 -060031#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
32 !defined(CONFIG_DM_SCSI)
Simon Glass11f610e2016-05-01 11:36:09 -060033const 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.. */
Faiz Abbas4ff57282019-10-15 18:24:33 +053048#define SCSI_MAX_BLK 0xFFFF
Simon Glass11f610e2016-05-01 11:36:09 -060049#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
Faiz Abbas66c54f12019-10-15 18:24:32 +053086static void scsi_setup_inquiry(struct scsi_cmd *pccb)
87{
88 pccb->cmd[0] = SCSI_INQUIRY;
89 pccb->cmd[1] = pccb->lun << 5;
90 pccb->cmd[2] = 0;
91 pccb->cmd[3] = 0;
92 if (pccb->datalen > 255)
93 pccb->cmd[4] = 255;
94 else
95 pccb->cmd[4] = (unsigned char)pccb->datalen;
96 pccb->cmd[5] = 0;
97 pccb->cmdlen = 6;
98 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
99}
100
101#ifdef CONFIG_BLK
Simon Glassb9560ad2017-06-14 21:28:30 -0600102static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek545a2842016-11-30 11:53:43 +0100103 unsigned short blocks)
Simon Glass11f610e2016-05-01 11:36:09 -0600104{
105 pccb->cmd[0] = SCSI_READ10;
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[6] = 0;
115 pccb->cmdlen = 10;
116 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
117 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
118 pccb->cmd[0], pccb->cmd[1],
119 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
120 pccb->cmd[7], pccb->cmd[8]);
121}
122
Simon Glassb9560ad2017-06-14 21:28:30 -0600123static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek545a2842016-11-30 11:53:43 +0100124 unsigned short blocks)
Simon Glass11f610e2016-05-01 11:36:09 -0600125{
126 pccb->cmd[0] = SCSI_WRITE10;
127 pccb->cmd[1] = pccb->lun << 5;
128 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
129 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
130 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
131 pccb->cmd[5] = (unsigned char)start & 0xff;
132 pccb->cmd[6] = 0;
133 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
134 pccb->cmd[8] = (unsigned char)blocks & 0xff;
135 pccb->cmd[9] = 0;
136 pccb->cmdlen = 10;
137 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
138 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
139 __func__,
140 pccb->cmd[0], pccb->cmd[1],
141 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
142 pccb->cmd[7], pccb->cmd[8]);
143}
144
Simon Glass535556b2016-05-01 11:36:24 -0600145static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
146 void *buffer)
Simon Glass11f610e2016-05-01 11:36:09 -0600147{
Simon Glass535556b2016-05-01 11:36:24 -0600148 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
Simon Glass4682c8a2017-06-14 21:28:40 -0600149 struct udevice *bdev = dev->parent;
Faiz Abbas4ff57282019-10-15 18:24:33 +0530150 struct scsi_platdata *uc_plat = dev_get_uclass_platdata(bdev);
151 lbaint_t start, blks, max_blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600152 uintptr_t buf_addr;
153 unsigned short smallblks = 0;
Simon Glassb9560ad2017-06-14 21:28:30 -0600154 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glass11f610e2016-05-01 11:36:09 -0600155
156 /* Setup device */
Michal Simekcdb93b22016-11-18 16:22:42 +0100157 pccb->target = block_dev->target;
158 pccb->lun = block_dev->lun;
Simon Glass11f610e2016-05-01 11:36:09 -0600159 buf_addr = (unsigned long)buffer;
160 start = blknr;
161 blks = blkcnt;
Faiz Abbas4ff57282019-10-15 18:24:33 +0530162 if (uc_plat->max_bytes_per_req)
163 max_blks = uc_plat->max_bytes_per_req / block_dev->blksz;
164 else
165 max_blks = SCSI_MAX_BLK;
166
Simon Glass11f610e2016-05-01 11:36:09 -0600167 debug("\nscsi_read: dev %d startblk " LBAF
168 ", blccnt " LBAF " buffer %lx\n",
Michal Simekcdb93b22016-11-18 16:22:42 +0100169 block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glass11f610e2016-05-01 11:36:09 -0600170 do {
171 pccb->pdata = (unsigned char *)buf_addr;
Faiz Abbas8fbac8e2019-10-15 18:24:35 +0530172 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glass11f610e2016-05-01 11:36:09 -0600173#ifdef CONFIG_SYS_64BIT_LBA
174 if (start > SCSI_LBA48_READ) {
175 unsigned long blocks;
Faiz Abbas4ff57282019-10-15 18:24:33 +0530176 blocks = min_t(lbaint_t, blks, max_blks);
Michal Simekcdb93b22016-11-18 16:22:42 +0100177 pccb->datalen = block_dev->blksz * blocks;
Simon Glass11f610e2016-05-01 11:36:09 -0600178 scsi_setup_read16(pccb, start, blocks);
179 start += blocks;
180 blks -= blocks;
181 } else
182#endif
Faiz Abbas4ff57282019-10-15 18:24:33 +0530183 if (blks > max_blks) {
184 pccb->datalen = block_dev->blksz * max_blks;
185 smallblks = max_blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600186 scsi_setup_read_ext(pccb, start, smallblks);
Faiz Abbas4ff57282019-10-15 18:24:33 +0530187 start += max_blks;
188 blks -= max_blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600189 } else {
Michal Simekcdb93b22016-11-18 16:22:42 +0100190 pccb->datalen = block_dev->blksz * blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600191 smallblks = (unsigned short)blks;
192 scsi_setup_read_ext(pccb, start, smallblks);
193 start += blks;
194 blks = 0;
195 }
196 debug("scsi_read_ext: startblk " LBAF
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900197 ", blccnt %x buffer %lX\n",
Simon Glass11f610e2016-05-01 11:36:09 -0600198 start, smallblks, buf_addr);
Simon Glass4682c8a2017-06-14 21:28:40 -0600199 if (scsi_exec(bdev, pccb)) {
Simon Glass11f610e2016-05-01 11:36:09 -0600200 scsi_print_error(pccb);
201 blkcnt -= blks;
202 break;
203 }
204 buf_addr += pccb->datalen;
205 } while (blks != 0);
206 debug("scsi_read_ext: end startblk " LBAF
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900207 ", blccnt %x buffer %lX\n", start, smallblks, buf_addr);
Simon Glass11f610e2016-05-01 11:36:09 -0600208 return blkcnt;
209}
210
211/*******************************************************************************
212 * scsi_write
213 */
214
Simon Glass535556b2016-05-01 11:36:24 -0600215static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
216 const void *buffer)
Simon Glass11f610e2016-05-01 11:36:09 -0600217{
Simon Glass535556b2016-05-01 11:36:24 -0600218 struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
Simon Glass4682c8a2017-06-14 21:28:40 -0600219 struct udevice *bdev = dev->parent;
Faiz Abbas4ff57282019-10-15 18:24:33 +0530220 struct scsi_platdata *uc_plat = dev_get_uclass_platdata(bdev);
221 lbaint_t start, blks, max_blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600222 uintptr_t buf_addr;
223 unsigned short smallblks;
Simon Glassb9560ad2017-06-14 21:28:30 -0600224 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glass11f610e2016-05-01 11:36:09 -0600225
Simon Glass11f610e2016-05-01 11:36:09 -0600226 /* Setup device */
Michal Simekcdb93b22016-11-18 16:22:42 +0100227 pccb->target = block_dev->target;
228 pccb->lun = block_dev->lun;
Simon Glass11f610e2016-05-01 11:36:09 -0600229 buf_addr = (unsigned long)buffer;
230 start = blknr;
231 blks = blkcnt;
Faiz Abbas4ff57282019-10-15 18:24:33 +0530232 if (uc_plat->max_bytes_per_req)
233 max_blks = uc_plat->max_bytes_per_req / block_dev->blksz;
234 else
235 max_blks = SCSI_MAX_BLK;
236
Simon Glass11f610e2016-05-01 11:36:09 -0600237 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
Michal Simekcdb93b22016-11-18 16:22:42 +0100238 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glass11f610e2016-05-01 11:36:09 -0600239 do {
240 pccb->pdata = (unsigned char *)buf_addr;
Faiz Abbas8fbac8e2019-10-15 18:24:35 +0530241 pccb->dma_dir = DMA_TO_DEVICE;
Faiz Abbas4ff57282019-10-15 18:24:33 +0530242 if (blks > max_blks) {
243 pccb->datalen = block_dev->blksz * max_blks;
244 smallblks = max_blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600245 scsi_setup_write_ext(pccb, start, smallblks);
Faiz Abbas4ff57282019-10-15 18:24:33 +0530246 start += max_blks;
247 blks -= max_blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600248 } else {
Michal Simekcdb93b22016-11-18 16:22:42 +0100249 pccb->datalen = block_dev->blksz * blks;
Simon Glass11f610e2016-05-01 11:36:09 -0600250 smallblks = (unsigned short)blks;
251 scsi_setup_write_ext(pccb, start, smallblks);
252 start += blks;
253 blks = 0;
254 }
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900255 debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
Simon Glass11f610e2016-05-01 11:36:09 -0600256 __func__, start, smallblks, buf_addr);
Simon Glass4682c8a2017-06-14 21:28:40 -0600257 if (scsi_exec(bdev, pccb)) {
Simon Glass11f610e2016-05-01 11:36:09 -0600258 scsi_print_error(pccb);
259 blkcnt -= blks;
260 break;
261 }
262 buf_addr += pccb->datalen;
263 } while (blks != 0);
Masahiro Yamadadee37fc2018-08-06 20:47:40 +0900264 debug("%s: end startblk " LBAF ", blccnt %x buffer %lX\n",
Simon Glass11f610e2016-05-01 11:36:09 -0600265 __func__, start, smallblks, buf_addr);
266 return blkcnt;
267}
Faiz Abbas66c54f12019-10-15 18:24:32 +0530268#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600269
Simon Glass7337fcd2017-06-14 21:28:47 -0600270#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
271 !defined(CONFIG_DM_SCSI)
Simon Glass11f610e2016-05-01 11:36:09 -0600272void 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);
Simon Glass8eab1a52017-06-14 21:28:41 -0600323 scsi_scan(true);
Simon Glass11f610e2016-05-01 11:36:09 -0600324 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 Glass4682c8a2017-06-14 21:28:40 -0600353static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb,
354 lbaint_t *capacity, 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;
Simon Glassf6580ef2017-06-14 21:28:44 -0600365 if (scsi_exec(dev, pccb))
Simon Glass11f610e2016-05-01 11:36:09 -0600366 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;
Faiz Abbas8fbac8e2019-10-15 18:24:35 +0530390 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassf6580ef2017-06-14 21:28:44 -0600391 if (scsi_exec(dev, pccb))
Simon Glass11f610e2016-05-01 11:36:09 -0600392 return 1;
393
394 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
395 ((uint64_t)pccb->pdata[1] << 48) |
396 ((uint64_t)pccb->pdata[2] << 40) |
397 ((uint64_t)pccb->pdata[3] << 32) |
398 ((uint64_t)pccb->pdata[4] << 24) |
399 ((uint64_t)pccb->pdata[5] << 16) |
400 ((uint64_t)pccb->pdata[6] << 8) |
401 ((uint64_t)pccb->pdata[7]);
402
403 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
404 ((uint64_t)pccb->pdata[9] << 48) |
405 ((uint64_t)pccb->pdata[10] << 40) |
406 ((uint64_t)pccb->pdata[11] << 32) |
407 ((uint64_t)pccb->pdata[12] << 24) |
408 ((uint64_t)pccb->pdata[13] << 16) |
409 ((uint64_t)pccb->pdata[14] << 8) |
410 ((uint64_t)pccb->pdata[15]);
411
412 return 0;
413}
414
415
416/*
417 * Some setup (fill-in) routines
418 */
Simon Glassb9560ad2017-06-14 21:28:30 -0600419static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
Simon Glass11f610e2016-05-01 11:36:09 -0600420{
421 pccb->cmd[0] = SCSI_TST_U_RDY;
422 pccb->cmd[1] = pccb->lun << 5;
423 pccb->cmd[2] = 0;
424 pccb->cmd[3] = 0;
425 pccb->cmd[4] = 0;
426 pccb->cmd[5] = 0;
427 pccb->cmdlen = 6;
428 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
429}
430
Michal Simek0b3a58e2016-11-30 12:50:58 +0100431/**
432 * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
433 *
434 * @dev_desc: Block device description pointer
435 */
436static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
Michal Simek92ca4762016-11-18 15:27:00 +0100437{
438 dev_desc->target = 0xff;
439 dev_desc->lun = 0xff;
Michal Simek92ca4762016-11-18 15:27:00 +0100440 dev_desc->log2blksz =
441 LOG2_INVALID(typeof(dev_desc->log2blksz));
442 dev_desc->type = DEV_TYPE_UNKNOWN;
443 dev_desc->vendor[0] = 0;
444 dev_desc->product[0] = 0;
445 dev_desc->revision[0] = 0;
446 dev_desc->removable = false;
Michal Simek92ca4762016-11-18 15:27:00 +0100447}
448
Michal Simeke8a016b2016-09-08 15:06:45 +0200449#if !defined(CONFIG_DM_SCSI)
Michal Simek0b3a58e2016-11-30 12:50:58 +0100450/**
451 * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties
452 *
453 * @dev_desc: Block device description pointer
454 * @devnum: Device number
455 */
456static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
457{
458 dev_desc->lba = 0;
459 dev_desc->blksz = 0;
460 dev_desc->if_type = IF_TYPE_SCSI;
461 dev_desc->devnum = devnum;
462 dev_desc->part_type = PART_TYPE_UNKNOWN;
463
464 scsi_init_dev_desc_priv(dev_desc);
465}
Michal Simeke8a016b2016-09-08 15:06:45 +0200466#endif
Michal Simekbccfd9e2016-11-18 16:14:24 +0100467
Michal Simek570712f2016-11-18 15:42:13 +0100468/**
469 * scsi_detect_dev - Detect scsi device
470 *
Michal Simekbccfd9e2016-11-18 16:14:24 +0100471 * @target: target id
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200472 * @lun: target lun
Michal Simek570712f2016-11-18 15:42:13 +0100473 * @dev_desc: block device description
Michal Simek570712f2016-11-18 15:42:13 +0100474 *
475 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200476 * detected.
Michal Simek570712f2016-11-18 15:42:13 +0100477 *
478 * Return: 0 on success, error value otherwise
479 */
Simon Glass4682c8a2017-06-14 21:28:40 -0600480static int scsi_detect_dev(struct udevice *dev, int target, int lun,
481 struct blk_desc *dev_desc)
Michal Simek570712f2016-11-18 15:42:13 +0100482{
483 unsigned char perq, modi;
484 lbaint_t capacity;
485 unsigned long blksz;
Simon Glassb9560ad2017-06-14 21:28:30 -0600486 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Faiz Abbasd48f00e2019-10-15 18:24:34 +0530487 int count, err;
Michal Simek570712f2016-11-18 15:42:13 +0100488
Michal Simekbccfd9e2016-11-18 16:14:24 +0100489 pccb->target = target;
Jean-Jacques Hiblote39cecf2017-04-07 13:42:06 +0200490 pccb->lun = lun;
Michal Simek570712f2016-11-18 15:42:13 +0100491 pccb->pdata = (unsigned char *)&tempbuff;
492 pccb->datalen = 512;
Faiz Abbas8fbac8e2019-10-15 18:24:35 +0530493 pccb->dma_dir = DMA_FROM_DEVICE;
Michal Simek570712f2016-11-18 15:42:13 +0100494 scsi_setup_inquiry(pccb);
Simon Glassf6580ef2017-06-14 21:28:44 -0600495 if (scsi_exec(dev, pccb)) {
Michal Simek570712f2016-11-18 15:42:13 +0100496 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
Faiz Abbasd48f00e2019-10-15 18:24:34 +0530524 for (count = 0; count < 3; count++) {
525 pccb->datalen = 0;
526 scsi_setup_test_unit_ready(pccb);
527 err = scsi_exec(dev, pccb);
528 if (!err)
529 break;
530 }
531 if (err) {
Michal Simek570712f2016-11-18 15:42:13 +0100532 if (dev_desc->removable) {
533 dev_desc->type = perq;
534 goto removable;
535 }
536 scsi_print_error(pccb);
537 return -EINVAL;
538 }
Simon Glass4682c8a2017-06-14 21:28:40 -0600539 if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) {
Michal Simek570712f2016-11-18 15:42:13 +0100540 scsi_print_error(pccb);
541 return -EINVAL;
542 }
543 dev_desc->lba = capacity;
544 dev_desc->blksz = blksz;
545 dev_desc->log2blksz = LOG2(dev_desc->blksz);
546 dev_desc->type = perq;
Michal Simek570712f2016-11-18 15:42:13 +0100547removable:
548 return 0;
549}
550
Simon Glass11f610e2016-05-01 11:36:09 -0600551/*
552 * (re)-scan the scsi bus and reports scsi device info
553 * to the user if mode = 1
554 */
Michal Simeke8a016b2016-09-08 15:06:45 +0200555#if defined(CONFIG_DM_SCSI)
Simon Glass8eab1a52017-06-14 21:28:41 -0600556static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200557{
558 int ret;
559 struct udevice *bdev;
560 struct blk_desc bd;
561 struct blk_desc *bdesc;
562 char str[10];
563
564 /*
565 * detect the scsi driver to get information about its geometry (block
566 * size, number of blocks) and other parameters (ids, type, ...)
567 */
568 scsi_init_dev_desc_priv(&bd);
Simon Glass4682c8a2017-06-14 21:28:40 -0600569 if (scsi_detect_dev(dev, id, lun, &bd))
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200570 return -ENODEV;
571
572 /*
573 * Create only one block device and do detection
574 * to make sure that there won't be a lot of
575 * block devices created
576 */
577 snprintf(str, sizeof(str), "id%dlun%d", id, lun);
578 ret = blk_create_devicef(dev, "scsi_blk", str, IF_TYPE_SCSI, -1,
Jean-Jacques Hiblot5fe77022017-06-09 16:45:18 +0200579 bd.blksz, bd.lba, &bdev);
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200580 if (ret) {
581 debug("Can't create device\n");
582 return ret;
583 }
584
585 bdesc = dev_get_uclass_platdata(bdev);
586 bdesc->target = id;
587 bdesc->lun = lun;
588 bdesc->removable = bd.removable;
589 bdesc->type = bd.type;
590 memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
591 memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
592 memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision));
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200593
Simon Glass8eab1a52017-06-14 21:28:41 -0600594 if (verbose) {
Heinrich Schuchardt90037d42019-02-05 18:06:24 +0100595 printf(" Device %d: ", bdesc->devnum);
Jean-Jacques Hiblotd52063b2017-04-24 11:51:26 +0200596 dev_print(bdesc);
597 }
598 return 0;
599}
600
Simon Glass5c561762017-06-14 21:28:45 -0600601int scsi_scan_dev(struct udevice *dev, bool verbose)
602{
603 struct scsi_platdata *uc_plat; /* scsi controller platdata */
604 int ret;
605 int i;
606 int lun;
607
608 /* probe SCSI controller driver */
609 ret = device_probe(dev);
610 if (ret)
611 return ret;
612
613 /* Get controller platdata */
614 uc_plat = dev_get_uclass_platdata(dev);
615
616 for (i = 0; i < uc_plat->max_id; i++)
617 for (lun = 0; lun < uc_plat->max_lun; lun++)
618 do_scsi_scan_one(dev, i, lun, verbose);
619
620 return 0;
621}
622
Simon Glass8eab1a52017-06-14 21:28:41 -0600623int scsi_scan(bool verbose)
Michal Simeke8a016b2016-09-08 15:06:45 +0200624{
Michal Simeke8a016b2016-09-08 15:06:45 +0200625 struct uclass *uc;
626 struct udevice *dev; /* SCSI controller */
627 int ret;
628
Simon Glass8eab1a52017-06-14 21:28:41 -0600629 if (verbose)
Michal Simeke8a016b2016-09-08 15:06:45 +0200630 printf("scanning bus for devices...\n");
631
Michal Simekf8f41ae2017-01-02 09:40:09 +0100632 blk_unbind_all(IF_TYPE_SCSI);
633
Michal Simeke8a016b2016-09-08 15:06:45 +0200634 ret = uclass_get(UCLASS_SCSI, &uc);
635 if (ret)
636 return ret;
637
638 uclass_foreach_dev(dev, uc) {
Simon Glass5c561762017-06-14 21:28:45 -0600639 ret = scsi_scan_dev(dev, verbose);
Michal Simeke8a016b2016-09-08 15:06:45 +0200640 if (ret)
641 return ret;
Michal Simeke8a016b2016-09-08 15:06:45 +0200642 }
643
644 return 0;
645}
646#else
Simon Glass8eab1a52017-06-14 21:28:41 -0600647int scsi_scan(bool verbose)
Simon Glass11f610e2016-05-01 11:36:09 -0600648{
Michal Simek570712f2016-11-18 15:42:13 +0100649 unsigned char i, lun;
650 int ret;
Simon Glass11f610e2016-05-01 11:36:09 -0600651
Simon Glass8eab1a52017-06-14 21:28:41 -0600652 if (verbose)
Simon Glass11f610e2016-05-01 11:36:09 -0600653 printf("scanning bus for devices...\n");
Michal Simek92ca4762016-11-18 15:27:00 +0100654 for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++)
655 scsi_init_dev_desc(&scsi_dev_desc[i], i);
656
Simon Glass11f610e2016-05-01 11:36:09 -0600657 scsi_max_devs = 0;
658 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
Simon Glass11f610e2016-05-01 11:36:09 -0600659 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
Heinrich Schuchardt90037d42019-02-05 18:06:24 +0100660 struct blk_desc *bdesc = &scsi_dev_desc[scsi_max_devs];
661
662 ret = scsi_detect_dev(NULL, i, lun, bdesc);
Michal Simek570712f2016-11-18 15:42:13 +0100663 if (ret)
Simon Glass11f610e2016-05-01 11:36:09 -0600664 continue;
Heinrich Schuchardt90037d42019-02-05 18:06:24 +0100665 part_init(bdesc);
Simon Glass11f610e2016-05-01 11:36:09 -0600666
Simon Glass8eab1a52017-06-14 21:28:41 -0600667 if (verbose) {
Heinrich Schuchardt90037d42019-02-05 18:06:24 +0100668 printf(" Device %d: ", bdesc->devnum);
669 dev_print(bdesc);
Simon Glass8eab1a52017-06-14 21:28:41 -0600670 }
Simon Glass11f610e2016-05-01 11:36:09 -0600671 scsi_max_devs++;
672 } /* next LUN */
673 }
674 if (scsi_max_devs > 0)
675 scsi_curr_dev = 0;
676 else
677 scsi_curr_dev = -1;
678
679 printf("Found %d device(s).\n", scsi_max_devs);
680#ifndef CONFIG_SPL_BUILD
Simon Glass018f5302017-08-03 12:22:10 -0600681 env_set_ulong("scsidevs", scsi_max_devs);
Simon Glass11f610e2016-05-01 11:36:09 -0600682#endif
Michal Simekc002e392016-11-30 12:12:31 +0100683 return 0;
Simon Glass11f610e2016-05-01 11:36:09 -0600684}
Michal Simeke8a016b2016-09-08 15:06:45 +0200685#endif
Simon Glass11f610e2016-05-01 11:36:09 -0600686
Simon Glass535556b2016-05-01 11:36:24 -0600687#ifdef CONFIG_BLK
688static const struct blk_ops scsi_blk_ops = {
689 .read = scsi_read,
690 .write = scsi_write,
691};
692
693U_BOOT_DRIVER(scsi_blk) = {
694 .name = "scsi_blk",
695 .id = UCLASS_BLK,
696 .ops = &scsi_blk_ops,
697};
698#else
Simon Glass11f610e2016-05-01 11:36:09 -0600699U_BOOT_LEGACY_BLK(scsi) = {
Ed Swarthout69c125f2016-06-01 08:11:24 -0500700 .if_typename = "scsi",
Simon Glass11f610e2016-05-01 11:36:09 -0600701 .if_type = IF_TYPE_SCSI,
702 .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE,
703 .desc = scsi_dev_desc,
704};
Simon Glass535556b2016-05-01 11:36:24 -0600705#endif