blob: 99be5aef877d949142ef0a9998c5ae3118dc944c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassc8e89ab2016-05-01 11:36:09 -06002/*
3 * (C) Copyright 2001
4 * Denis Peter, MPL AG Switzerland
Simon Glassc8e89ab2016-05-01 11:36:09 -06005 */
6
7#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -06008#include <blk.h>
Simon Glass1ea97892020-05-10 11:40:00 -06009#include <bootstage.h>
Simon Glass5bf96dc2016-05-01 11:36:24 -060010#include <dm.h>
Simon Glass07dc93c2019-08-01 09:46:47 -060011#include <env.h>
Stefan Roesec52ee3e2021-04-07 09:12:36 +020012#include <libata.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass655306c2020-05-10 11:39:58 -060014#include <part.h>
Simon Glassc8e89ab2016-05-01 11:36:09 -060015#include <pci.h>
16#include <scsi.h>
Michal Simekc886f352016-09-08 15:06:45 +020017#include <dm/device-internal.h>
18#include <dm/uclass-internal.h>
Simon Glassc8e89ab2016-05-01 11:36:09 -060019
Michal Simekc886f352016-09-08 15:06:45 +020020#if !defined(CONFIG_DM_SCSI)
Simon Glass0ada8912017-06-14 21:28:35 -060021# ifdef CONFIG_SCSI_DEV_LIST
22# define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST
23# else
24# ifdef CONFIG_SATA_ULI5288
Simon Glassc8e89ab2016-05-01 11:36:09 -060025
Simon Glass0ada8912017-06-14 21:28:35 -060026# define SCSI_VEND_ID 0x10b9
27# define SCSI_DEV_ID 0x5288
Simon Glassc8e89ab2016-05-01 11:36:09 -060028
Simon Glass0ada8912017-06-14 21:28:35 -060029# elif !defined(CONFIG_SCSI_AHCI_PLAT)
30# error no scsi device defined
31# endif
32# define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
33# endif
Michal Simekc886f352016-09-08 15:06:45 +020034#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -060035
Simon Glass57e7c2a2017-06-14 21:28:47 -060036#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
37 !defined(CONFIG_DM_SCSI)
Simon Glassc8e89ab2016-05-01 11:36:09 -060038const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
39#endif
Simon Glass5fb559d2017-06-14 21:28:30 -060040static struct scsi_cmd tempccb; /* temporary scsi command buffer */
Simon Glassc8e89ab2016-05-01 11:36:09 -060041
42static unsigned char tempbuff[512]; /* temporary data buffer */
43
Michal Simekc886f352016-09-08 15:06:45 +020044#if !defined(CONFIG_DM_SCSI)
Simon Glassc8e89ab2016-05-01 11:36:09 -060045static int scsi_max_devs; /* number of highest available scsi device */
46
47static int scsi_curr_dev; /* current device */
48
Simon Glass1a462242022-01-31 07:49:36 -070049static struct blk_desc scsi_dev_desc[SCSI_MAX_DEVICE];
Michal Simekc886f352016-09-08 15:06:45 +020050#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -060051
52/* almost the maximum amount of the scsi_ext command.. */
Faiz Abbasb26c6142019-10-15 18:24:33 +053053#define SCSI_MAX_BLK 0xFFFF
Simon Glassc8e89ab2016-05-01 11:36:09 -060054#define SCSI_LBA48_READ 0xFFFFFFF
55
Simon Glass5fb559d2017-06-14 21:28:30 -060056static void scsi_print_error(struct scsi_cmd *pccb)
Simon Glass16740982017-06-14 21:28:23 -060057{
58 /* Dummy function that could print an error for debugging */
59}
60
Simon Glassc8e89ab2016-05-01 11:36:09 -060061#ifdef CONFIG_SYS_64BIT_LBA
Simon Glass5fb559d2017-06-14 21:28:30 -060062void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start,
63 unsigned long blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -060064{
65 pccb->cmd[0] = SCSI_READ16;
66 pccb->cmd[1] = pccb->lun << 5;
67 pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff;
68 pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff;
69 pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff;
70 pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff;
71 pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff;
72 pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff;
73 pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff;
74 pccb->cmd[9] = (unsigned char)start & 0xff;
75 pccb->cmd[10] = 0;
76 pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff;
77 pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff;
78 pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff;
79 pccb->cmd[14] = (unsigned char)blocks & 0xff;
80 pccb->cmd[15] = 0;
81 pccb->cmdlen = 16;
82 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
83 debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n",
84 pccb->cmd[0], pccb->cmd[1],
85 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
86 pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
87 pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
88}
89#endif
90
Faiz Abbas76a6fa62019-10-15 18:24:32 +053091static void scsi_setup_inquiry(struct scsi_cmd *pccb)
92{
93 pccb->cmd[0] = SCSI_INQUIRY;
94 pccb->cmd[1] = pccb->lun << 5;
95 pccb->cmd[2] = 0;
96 pccb->cmd[3] = 0;
97 if (pccb->datalen > 255)
98 pccb->cmd[4] = 255;
99 else
100 pccb->cmd[4] = (unsigned char)pccb->datalen;
101 pccb->cmd[5] = 0;
102 pccb->cmdlen = 6;
103 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
104}
105
106#ifdef CONFIG_BLK
Simon Glass5fb559d2017-06-14 21:28:30 -0600107static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek9ec32f32016-11-30 11:53:43 +0100108 unsigned short blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600109{
110 pccb->cmd[0] = SCSI_READ10;
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[6] = 0;
120 pccb->cmdlen = 10;
121 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
122 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
123 pccb->cmd[0], pccb->cmd[1],
124 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
125 pccb->cmd[7], pccb->cmd[8]);
126}
127
Simon Glass5fb559d2017-06-14 21:28:30 -0600128static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek9ec32f32016-11-30 11:53:43 +0100129 unsigned short blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600130{
131 pccb->cmd[0] = SCSI_WRITE10;
132 pccb->cmd[1] = pccb->lun << 5;
133 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
134 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
135 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
136 pccb->cmd[5] = (unsigned char)start & 0xff;
137 pccb->cmd[6] = 0;
138 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
139 pccb->cmd[8] = (unsigned char)blocks & 0xff;
140 pccb->cmd[9] = 0;
141 pccb->cmdlen = 10;
142 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
143 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
144 __func__,
145 pccb->cmd[0], pccb->cmd[1],
146 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
147 pccb->cmd[7], pccb->cmd[8]);
148}
149
Simon Glass5bf96dc2016-05-01 11:36:24 -0600150static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
151 void *buffer)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600152{
Simon Glass71fa5b42020-12-03 16:55:18 -0700153 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass11b2b622017-06-14 21:28:40 -0600154 struct udevice *bdev = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700155 struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530156 lbaint_t start, blks, max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600157 uintptr_t buf_addr;
158 unsigned short smallblks = 0;
Simon Glass5fb559d2017-06-14 21:28:30 -0600159 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600160
161 /* Setup device */
Michal Simekde6840a2016-11-18 16:22:42 +0100162 pccb->target = block_dev->target;
163 pccb->lun = block_dev->lun;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600164 buf_addr = (unsigned long)buffer;
165 start = blknr;
166 blks = blkcnt;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530167 if (uc_plat->max_bytes_per_req)
168 max_blks = uc_plat->max_bytes_per_req / block_dev->blksz;
169 else
170 max_blks = SCSI_MAX_BLK;
171
Simon Glassc8e89ab2016-05-01 11:36:09 -0600172 debug("\nscsi_read: dev %d startblk " LBAF
173 ", blccnt " LBAF " buffer %lx\n",
Michal Simekde6840a2016-11-18 16:22:42 +0100174 block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600175 do {
176 pccb->pdata = (unsigned char *)buf_addr;
Faiz Abbas20948092019-10-15 18:24:35 +0530177 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600178#ifdef CONFIG_SYS_64BIT_LBA
179 if (start > SCSI_LBA48_READ) {
180 unsigned long blocks;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530181 blocks = min_t(lbaint_t, blks, max_blks);
Michal Simekde6840a2016-11-18 16:22:42 +0100182 pccb->datalen = block_dev->blksz * blocks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600183 scsi_setup_read16(pccb, start, blocks);
184 start += blocks;
185 blks -= blocks;
186 } else
187#endif
Faiz Abbasb26c6142019-10-15 18:24:33 +0530188 if (blks > max_blks) {
189 pccb->datalen = block_dev->blksz * max_blks;
190 smallblks = max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600191 scsi_setup_read_ext(pccb, start, smallblks);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530192 start += max_blks;
193 blks -= max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600194 } else {
Michal Simekde6840a2016-11-18 16:22:42 +0100195 pccb->datalen = block_dev->blksz * blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600196 smallblks = (unsigned short)blks;
197 scsi_setup_read_ext(pccb, start, smallblks);
198 start += blks;
199 blks = 0;
200 }
201 debug("scsi_read_ext: startblk " LBAF
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900202 ", blccnt %x buffer %lX\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600203 start, smallblks, buf_addr);
Simon Glass11b2b622017-06-14 21:28:40 -0600204 if (scsi_exec(bdev, pccb)) {
Simon Glassc8e89ab2016-05-01 11:36:09 -0600205 scsi_print_error(pccb);
206 blkcnt -= blks;
207 break;
208 }
209 buf_addr += pccb->datalen;
210 } while (blks != 0);
211 debug("scsi_read_ext: end startblk " LBAF
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900212 ", blccnt %x buffer %lX\n", start, smallblks, buf_addr);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600213 return blkcnt;
214}
215
216/*******************************************************************************
217 * scsi_write
218 */
219
Simon Glass5bf96dc2016-05-01 11:36:24 -0600220static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
221 const void *buffer)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600222{
Simon Glass71fa5b42020-12-03 16:55:18 -0700223 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass11b2b622017-06-14 21:28:40 -0600224 struct udevice *bdev = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700225 struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530226 lbaint_t start, blks, max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600227 uintptr_t buf_addr;
228 unsigned short smallblks;
Simon Glass5fb559d2017-06-14 21:28:30 -0600229 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600230
Simon Glassc8e89ab2016-05-01 11:36:09 -0600231 /* Setup device */
Michal Simekde6840a2016-11-18 16:22:42 +0100232 pccb->target = block_dev->target;
233 pccb->lun = block_dev->lun;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600234 buf_addr = (unsigned long)buffer;
235 start = blknr;
236 blks = blkcnt;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530237 if (uc_plat->max_bytes_per_req)
238 max_blks = uc_plat->max_bytes_per_req / block_dev->blksz;
239 else
240 max_blks = SCSI_MAX_BLK;
241
Simon Glassc8e89ab2016-05-01 11:36:09 -0600242 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
Michal Simekde6840a2016-11-18 16:22:42 +0100243 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600244 do {
245 pccb->pdata = (unsigned char *)buf_addr;
Faiz Abbas20948092019-10-15 18:24:35 +0530246 pccb->dma_dir = DMA_TO_DEVICE;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530247 if (blks > max_blks) {
248 pccb->datalen = block_dev->blksz * max_blks;
249 smallblks = max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600250 scsi_setup_write_ext(pccb, start, smallblks);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530251 start += max_blks;
252 blks -= max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600253 } else {
Michal Simekde6840a2016-11-18 16:22:42 +0100254 pccb->datalen = block_dev->blksz * blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600255 smallblks = (unsigned short)blks;
256 scsi_setup_write_ext(pccb, start, smallblks);
257 start += blks;
258 blks = 0;
259 }
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900260 debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600261 __func__, start, smallblks, buf_addr);
Simon Glass11b2b622017-06-14 21:28:40 -0600262 if (scsi_exec(bdev, pccb)) {
Simon Glassc8e89ab2016-05-01 11:36:09 -0600263 scsi_print_error(pccb);
264 blkcnt -= blks;
265 break;
266 }
267 buf_addr += pccb->datalen;
268 } while (blks != 0);
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900269 debug("%s: end startblk " LBAF ", blccnt %x buffer %lX\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600270 __func__, start, smallblks, buf_addr);
271 return blkcnt;
272}
Faiz Abbas76a6fa62019-10-15 18:24:32 +0530273#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -0600274
Simon Glass57e7c2a2017-06-14 21:28:47 -0600275#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
276 !defined(CONFIG_DM_SCSI)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600277void scsi_init(void)
278{
279 int busdevfunc = -1;
280 int i;
281 /*
282 * Find a device from the list, this driver will support a single
283 * controller.
284 */
285 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
286 /* get PCI Device ID */
Simon Glassc8e89ab2016-05-01 11:36:09 -0600287 struct udevice *dev;
288 int ret;
289
290 ret = dm_pci_find_device(scsi_device_list[i].vendor,
291 scsi_device_list[i].device, 0, &dev);
292 if (!ret) {
293 busdevfunc = dm_pci_get_bdf(dev);
294 break;
295 }
Simon Glassc8e89ab2016-05-01 11:36:09 -0600296 if (busdevfunc != -1)
297 break;
298 }
299
300 if (busdevfunc == -1) {
301 printf("Error: SCSI Controller(s) ");
302 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
303 printf("%04X:%04X ",
304 scsi_device_list[i].vendor,
305 scsi_device_list[i].device);
306 }
307 printf("not found\n");
308 return;
309 }
310#ifdef DEBUG
311 else {
312 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
313 scsi_device_list[i].vendor,
314 scsi_device_list[i].device,
315 (busdevfunc >> 16) & 0xFF,
316 (busdevfunc >> 11) & 0x1F,
317 (busdevfunc >> 8) & 0x7);
318 }
319#endif
320 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
321 scsi_low_level_init(busdevfunc);
Simon Glass48228732017-06-14 21:28:41 -0600322 scsi_scan(true);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600323 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
324}
325#endif
326
Simon Glassc8e89ab2016-05-01 11:36:09 -0600327/* copy src to dest, skipping leading and trailing blanks
328 * and null terminate the string
329 */
Michal Simek9ec32f32016-11-30 11:53:43 +0100330static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
331 unsigned int len)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600332{
333 int start, end;
334
335 start = 0;
336 while (start < len) {
337 if (src[start] != ' ')
338 break;
339 start++;
340 }
341 end = len-1;
342 while (end > start) {
343 if (src[end] != ' ')
344 break;
345 end--;
346 }
347 for (; start <= end; start++)
348 *dest ++= src[start];
349 *dest = '\0';
350}
351
Simon Glass11b2b622017-06-14 21:28:40 -0600352static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb,
353 lbaint_t *capacity, unsigned long *blksz)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600354{
355 *capacity = 0;
356
357 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
358 pccb->cmd[0] = SCSI_RD_CAPAC10;
359 pccb->cmd[1] = pccb->lun << 5;
360 pccb->cmdlen = 10;
361 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
362
363 pccb->datalen = 8;
Simon Glassa140e862017-06-14 21:28:44 -0600364 if (scsi_exec(dev, pccb))
Simon Glassc8e89ab2016-05-01 11:36:09 -0600365 return 1;
366
367 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
368 ((lbaint_t)pccb->pdata[1] << 16) |
369 ((lbaint_t)pccb->pdata[2] << 8) |
370 ((lbaint_t)pccb->pdata[3]);
371
372 if (*capacity != 0xffffffff) {
373 /* Read capacity (10) was sufficient for this drive. */
374 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
375 ((unsigned long)pccb->pdata[5] << 16) |
376 ((unsigned long)pccb->pdata[6] << 8) |
377 ((unsigned long)pccb->pdata[7]);
378 return 0;
379 }
380
381 /* Read capacity (10) was insufficient. Use read capacity (16). */
382 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
383 pccb->cmd[0] = SCSI_RD_CAPAC16;
384 pccb->cmd[1] = 0x10;
385 pccb->cmdlen = 16;
386 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
387
388 pccb->datalen = 16;
Faiz Abbas20948092019-10-15 18:24:35 +0530389 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassa140e862017-06-14 21:28:44 -0600390 if (scsi_exec(dev, pccb))
Simon Glassc8e89ab2016-05-01 11:36:09 -0600391 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 Glass5fb559d2017-06-14 21:28:30 -0600418static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
Simon Glassc8e89ab2016-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 Simek6241b522016-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 Simekac0a18d12016-11-18 15:27:00 +0100436{
437 dev_desc->target = 0xff;
438 dev_desc->lun = 0xff;
Michal Simekac0a18d12016-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 Simekac0a18d12016-11-18 15:27:00 +0100446}
447
Michal Simekc886f352016-09-08 15:06:45 +0200448#if !defined(CONFIG_DM_SCSI)
Michal Simek6241b522016-11-30 12:50:58 +0100449/**
450 * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties
451 *
452 * @dev_desc: Block device description pointer
453 * @devnum: Device number
454 */
455static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
456{
457 dev_desc->lba = 0;
458 dev_desc->blksz = 0;
Simon Glassdbfa32c2022-08-11 19:34:59 -0600459 dev_desc->if_type = UCLASS_SCSI;
Michal Simek6241b522016-11-30 12:50:58 +0100460 dev_desc->devnum = devnum;
461 dev_desc->part_type = PART_TYPE_UNKNOWN;
462
463 scsi_init_dev_desc_priv(dev_desc);
464}
Michal Simekc886f352016-09-08 15:06:45 +0200465#endif
Michal Simek40917492016-11-18 16:14:24 +0100466
Michal Simekebbde652016-11-18 15:42:13 +0100467/**
468 * scsi_detect_dev - Detect scsi device
469 *
Michal Simek40917492016-11-18 16:14:24 +0100470 * @target: target id
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200471 * @lun: target lun
Michal Simekebbde652016-11-18 15:42:13 +0100472 * @dev_desc: block device description
Michal Simekebbde652016-11-18 15:42:13 +0100473 *
474 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200475 * detected.
Michal Simekebbde652016-11-18 15:42:13 +0100476 *
477 * Return: 0 on success, error value otherwise
478 */
Simon Glass11b2b622017-06-14 21:28:40 -0600479static int scsi_detect_dev(struct udevice *dev, int target, int lun,
480 struct blk_desc *dev_desc)
Michal Simekebbde652016-11-18 15:42:13 +0100481{
482 unsigned char perq, modi;
483 lbaint_t capacity;
484 unsigned long blksz;
Simon Glass5fb559d2017-06-14 21:28:30 -0600485 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Faiz Abbas3645a482019-10-15 18:24:34 +0530486 int count, err;
Michal Simekebbde652016-11-18 15:42:13 +0100487
Michal Simek40917492016-11-18 16:14:24 +0100488 pccb->target = target;
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200489 pccb->lun = lun;
Michal Simekebbde652016-11-18 15:42:13 +0100490 pccb->pdata = (unsigned char *)&tempbuff;
491 pccb->datalen = 512;
Faiz Abbas20948092019-10-15 18:24:35 +0530492 pccb->dma_dir = DMA_FROM_DEVICE;
Michal Simekebbde652016-11-18 15:42:13 +0100493 scsi_setup_inquiry(pccb);
Simon Glassa140e862017-06-14 21:28:44 -0600494 if (scsi_exec(dev, pccb)) {
Michal Simekebbde652016-11-18 15:42:13 +0100495 if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
496 /*
497 * selection timeout => assuming no
498 * device present
499 */
500 debug("Selection timeout ID %d\n",
501 pccb->target);
502 return -ETIMEDOUT;
503 }
504 scsi_print_error(pccb);
505 return -ENODEV;
506 }
507 perq = tempbuff[0];
508 modi = tempbuff[1];
509 if ((perq & 0x1f) == 0x1f)
510 return -ENODEV; /* skip unknown devices */
511 if ((modi & 0x80) == 0x80) /* drive is removable */
512 dev_desc->removable = true;
513 /* get info for this device */
514 scsi_ident_cpy((unsigned char *)dev_desc->vendor,
515 &tempbuff[8], 8);
516 scsi_ident_cpy((unsigned char *)dev_desc->product,
517 &tempbuff[16], 16);
518 scsi_ident_cpy((unsigned char *)dev_desc->revision,
519 &tempbuff[32], 4);
520 dev_desc->target = pccb->target;
521 dev_desc->lun = pccb->lun;
522
Faiz Abbas3645a482019-10-15 18:24:34 +0530523 for (count = 0; count < 3; count++) {
524 pccb->datalen = 0;
525 scsi_setup_test_unit_ready(pccb);
526 err = scsi_exec(dev, pccb);
527 if (!err)
528 break;
529 }
530 if (err) {
Michal Simekebbde652016-11-18 15:42:13 +0100531 if (dev_desc->removable) {
532 dev_desc->type = perq;
533 goto removable;
534 }
535 scsi_print_error(pccb);
536 return -EINVAL;
537 }
Simon Glass11b2b622017-06-14 21:28:40 -0600538 if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) {
Michal Simekebbde652016-11-18 15:42:13 +0100539 scsi_print_error(pccb);
540 return -EINVAL;
541 }
542 dev_desc->lba = capacity;
543 dev_desc->blksz = blksz;
544 dev_desc->log2blksz = LOG2(dev_desc->blksz);
545 dev_desc->type = perq;
Michal Simekebbde652016-11-18 15:42:13 +0100546removable:
547 return 0;
548}
549
Simon Glassc8e89ab2016-05-01 11:36:09 -0600550/*
551 * (re)-scan the scsi bus and reports scsi device info
552 * to the user if mode = 1
553 */
Michal Simekc886f352016-09-08 15:06:45 +0200554#if defined(CONFIG_DM_SCSI)
Simon Glass48228732017-06-14 21:28:41 -0600555static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200556{
557 int ret;
558 struct udevice *bdev;
559 struct blk_desc bd;
560 struct blk_desc *bdesc;
561 char str[10];
562
563 /*
564 * detect the scsi driver to get information about its geometry (block
565 * size, number of blocks) and other parameters (ids, type, ...)
566 */
567 scsi_init_dev_desc_priv(&bd);
Simon Glass11b2b622017-06-14 21:28:40 -0600568 if (scsi_detect_dev(dev, id, lun, &bd))
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200569 return -ENODEV;
570
571 /*
572 * Create only one block device and do detection
573 * to make sure that there won't be a lot of
574 * block devices created
575 */
576 snprintf(str, sizeof(str), "id%dlun%d", id, lun);
Simon Glassdbfa32c2022-08-11 19:34:59 -0600577 ret = blk_create_devicef(dev, "scsi_blk", str, UCLASS_SCSI, -1,
578 bd.blksz, bd.lba, &bdev);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200579 if (ret) {
580 debug("Can't create device\n");
581 return ret;
582 }
583
Simon Glass71fa5b42020-12-03 16:55:18 -0700584 bdesc = dev_get_uclass_plat(bdev);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200585 bdesc->target = id;
586 bdesc->lun = lun;
587 bdesc->removable = bd.removable;
588 bdesc->type = bd.type;
589 memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
590 memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
591 memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision));
Stefan Roesec52ee3e2021-04-07 09:12:36 +0200592 if (IS_ENABLED(CONFIG_SYS_BIG_ENDIAN)) {
593 ata_swap_buf_le16((u16 *)&bdesc->vendor, sizeof(bd.vendor) / 2);
594 ata_swap_buf_le16((u16 *)&bdesc->product, sizeof(bd.product) / 2);
595 ata_swap_buf_le16((u16 *)&bdesc->revision, sizeof(bd.revision) / 2);
596 }
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200597
AKASHI Takahiro368c3d52022-03-08 20:36:39 +0900598 ret = blk_probe_or_unbind(bdev);
599 if (ret < 0)
600 /* TODO: undo create */
601 return ret;
602
Simon Glass48228732017-06-14 21:28:41 -0600603 if (verbose) {
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100604 printf(" Device %d: ", bdesc->devnum);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200605 dev_print(bdesc);
606 }
607 return 0;
608}
609
Simon Glass600d0012017-06-14 21:28:45 -0600610int scsi_scan_dev(struct udevice *dev, bool verbose)
611{
Simon Glassb75b15b2020-12-03 16:55:23 -0700612 struct scsi_plat *uc_plat; /* scsi controller plat */
Simon Glass600d0012017-06-14 21:28:45 -0600613 int ret;
614 int i;
615 int lun;
616
617 /* probe SCSI controller driver */
618 ret = device_probe(dev);
619 if (ret)
620 return ret;
621
Simon Glass71fa5b42020-12-03 16:55:18 -0700622 /* Get controller plat */
623 uc_plat = dev_get_uclass_plat(dev);
Simon Glass600d0012017-06-14 21:28:45 -0600624
625 for (i = 0; i < uc_plat->max_id; i++)
626 for (lun = 0; lun < uc_plat->max_lun; lun++)
627 do_scsi_scan_one(dev, i, lun, verbose);
628
629 return 0;
630}
631
Simon Glass48228732017-06-14 21:28:41 -0600632int scsi_scan(bool verbose)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600633{
Michal Simekc886f352016-09-08 15:06:45 +0200634 struct uclass *uc;
635 struct udevice *dev; /* SCSI controller */
Michal Simekebbde652016-11-18 15:42:13 +0100636 int ret;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600637
Simon Glass48228732017-06-14 21:28:41 -0600638 if (verbose)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600639 printf("scanning bus for devices...\n");
Michal Simekc886f352016-09-08 15:06:45 +0200640
Simon Glassdbfa32c2022-08-11 19:34:59 -0600641 blk_unbind_all(UCLASS_SCSI);
Michal Simek9c49b8c2017-01-02 09:40:09 +0100642
Michal Simekc886f352016-09-08 15:06:45 +0200643 ret = uclass_get(UCLASS_SCSI, &uc);
644 if (ret)
645 return ret;
646
647 uclass_foreach_dev(dev, uc) {
Simon Glass600d0012017-06-14 21:28:45 -0600648 ret = scsi_scan_dev(dev, verbose);
Michal Simekc886f352016-09-08 15:06:45 +0200649 if (ret)
650 return ret;
Michal Simekc886f352016-09-08 15:06:45 +0200651 }
652
653 return 0;
654}
655#else
Simon Glass48228732017-06-14 21:28:41 -0600656int scsi_scan(bool verbose)
Michal Simekc886f352016-09-08 15:06:45 +0200657{
658 unsigned char i, lun;
659 int ret;
660
Simon Glass48228732017-06-14 21:28:41 -0600661 if (verbose)
Michal Simekc886f352016-09-08 15:06:45 +0200662 printf("scanning bus for devices...\n");
Simon Glass1a462242022-01-31 07:49:36 -0700663 for (i = 0; i < SCSI_MAX_DEVICE; i++)
Michal Simekac0a18d12016-11-18 15:27:00 +0100664 scsi_init_dev_desc(&scsi_dev_desc[i], i);
665
Simon Glassc8e89ab2016-05-01 11:36:09 -0600666 scsi_max_devs = 0;
667 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
Simon Glassc8e89ab2016-05-01 11:36:09 -0600668 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100669 struct blk_desc *bdesc = &scsi_dev_desc[scsi_max_devs];
670
671 ret = scsi_detect_dev(NULL, i, lun, bdesc);
Michal Simekebbde652016-11-18 15:42:13 +0100672 if (ret)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600673 continue;
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100674 part_init(bdesc);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600675
Simon Glass48228732017-06-14 21:28:41 -0600676 if (verbose) {
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100677 printf(" Device %d: ", bdesc->devnum);
678 dev_print(bdesc);
Simon Glass48228732017-06-14 21:28:41 -0600679 }
Simon Glassc8e89ab2016-05-01 11:36:09 -0600680 scsi_max_devs++;
681 } /* next LUN */
682 }
683 if (scsi_max_devs > 0)
684 scsi_curr_dev = 0;
685 else
686 scsi_curr_dev = -1;
687
688 printf("Found %d device(s).\n", scsi_max_devs);
689#ifndef CONFIG_SPL_BUILD
Simon Glass4d949a22017-08-03 12:22:10 -0600690 env_set_ulong("scsidevs", scsi_max_devs);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600691#endif
Michal Simek5dc196f2016-11-30 12:12:31 +0100692 return 0;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600693}
Michal Simekc886f352016-09-08 15:06:45 +0200694#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -0600695
Simon Glass5bf96dc2016-05-01 11:36:24 -0600696#ifdef CONFIG_BLK
697static const struct blk_ops scsi_blk_ops = {
698 .read = scsi_read,
699 .write = scsi_write,
700};
701
702U_BOOT_DRIVER(scsi_blk) = {
703 .name = "scsi_blk",
704 .id = UCLASS_BLK,
705 .ops = &scsi_blk_ops,
706};
707#else
Simon Glassc8e89ab2016-05-01 11:36:09 -0600708U_BOOT_LEGACY_BLK(scsi) = {
Ed Swarthout987a9552016-06-01 08:11:24 -0500709 .if_typename = "scsi",
Simon Glassdbfa32c2022-08-11 19:34:59 -0600710 .if_type = UCLASS_SCSI,
Simon Glass1a462242022-01-31 07:49:36 -0700711 .max_devs = SCSI_MAX_DEVICE,
Simon Glassc8e89ab2016-05-01 11:36:09 -0600712 .desc = scsi_dev_desc,
713};
Simon Glass5bf96dc2016-05-01 11:36:24 -0600714#endif