blob: 1330482c167402cb0f49d3d1dd2e48a3f6ea8501 [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
Simon Glass1af3eed2023-01-17 10:47:43 -07007#define LOG_CATEGORY UCLASS_SCSI
8
Simon Glassc8e89ab2016-05-01 11:36:09 -06009#include <common.h>
Simon Glass655306c2020-05-10 11:39:58 -060010#include <blk.h>
Simon Glass9985e9c2023-01-17 10:47:45 -070011#include <bootdev.h>
Simon Glass1ea97892020-05-10 11:40:00 -060012#include <bootstage.h>
Simon Glass5bf96dc2016-05-01 11:36:24 -060013#include <dm.h>
Simon Glass07dc93c2019-08-01 09:46:47 -060014#include <env.h>
Stefan Roesec52ee3e2021-04-07 09:12:36 +020015#include <libata.h>
Simon Glass0f2af882020-05-10 11:40:05 -060016#include <log.h>
Marek Vasut60b20b42023-08-07 02:26:12 +020017#include <memalign.h>
Simon Glass655306c2020-05-10 11:39:58 -060018#include <part.h>
Simon Glassc8e89ab2016-05-01 11:36:09 -060019#include <pci.h>
20#include <scsi.h>
Michal Simekc886f352016-09-08 15:06:45 +020021#include <dm/device-internal.h>
22#include <dm/uclass-internal.h>
Simon Glassc8e89ab2016-05-01 11:36:09 -060023
Michal Simekc886f352016-09-08 15:06:45 +020024#if !defined(CONFIG_DM_SCSI)
Tom Rini6173acb2022-12-04 10:13:44 -050025# ifdef CFG_SCSI_DEV_LIST
26# define SCSI_DEV_LIST CFG_SCSI_DEV_LIST
Simon Glass0ada8912017-06-14 21:28:35 -060027# else
28# ifdef CONFIG_SATA_ULI5288
Simon Glassc8e89ab2016-05-01 11:36:09 -060029
Simon Glass0ada8912017-06-14 21:28:35 -060030# define SCSI_VEND_ID 0x10b9
31# define SCSI_DEV_ID 0x5288
Simon Glassc8e89ab2016-05-01 11:36:09 -060032
Simon Glass0ada8912017-06-14 21:28:35 -060033# elif !defined(CONFIG_SCSI_AHCI_PLAT)
34# error no scsi device defined
35# endif
36# define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
37# endif
Michal Simekc886f352016-09-08 15:06:45 +020038#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -060039
Simon Glass57e7c2a2017-06-14 21:28:47 -060040#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
41 !defined(CONFIG_DM_SCSI)
Simon Glassc8e89ab2016-05-01 11:36:09 -060042const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST };
43#endif
Simon Glass5fb559d2017-06-14 21:28:30 -060044static struct scsi_cmd tempccb; /* temporary scsi command buffer */
Simon Glassc8e89ab2016-05-01 11:36:09 -060045
Marek Vasut60b20b42023-08-07 02:26:12 +020046DEFINE_CACHE_ALIGN_BUFFER(u8, tempbuff, 512); /* temporary data buffer */
Simon Glassc8e89ab2016-05-01 11:36:09 -060047
Michal Simekc886f352016-09-08 15:06:45 +020048#if !defined(CONFIG_DM_SCSI)
Simon Glassc8e89ab2016-05-01 11:36:09 -060049static int scsi_max_devs; /* number of highest available scsi device */
50
51static int scsi_curr_dev; /* current device */
52
Simon Glass1a462242022-01-31 07:49:36 -070053static struct blk_desc scsi_dev_desc[SCSI_MAX_DEVICE];
Michal Simekc886f352016-09-08 15:06:45 +020054#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -060055
56/* almost the maximum amount of the scsi_ext command.. */
Faiz Abbasb26c6142019-10-15 18:24:33 +053057#define SCSI_MAX_BLK 0xFFFF
Simon Glassc8e89ab2016-05-01 11:36:09 -060058#define SCSI_LBA48_READ 0xFFFFFFF
59
Simon Glass5fb559d2017-06-14 21:28:30 -060060static void scsi_print_error(struct scsi_cmd *pccb)
Simon Glass16740982017-06-14 21:28:23 -060061{
62 /* Dummy function that could print an error for debugging */
63}
64
Simon Glassc8e89ab2016-05-01 11:36:09 -060065#ifdef CONFIG_SYS_64BIT_LBA
Simon Glass5fb559d2017-06-14 21:28:30 -060066void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start,
67 unsigned long blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -060068{
69 pccb->cmd[0] = SCSI_READ16;
70 pccb->cmd[1] = pccb->lun << 5;
71 pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff;
72 pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff;
73 pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff;
74 pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff;
75 pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff;
76 pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff;
77 pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff;
78 pccb->cmd[9] = (unsigned char)start & 0xff;
79 pccb->cmd[10] = 0;
80 pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff;
81 pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff;
82 pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff;
83 pccb->cmd[14] = (unsigned char)blocks & 0xff;
84 pccb->cmd[15] = 0;
85 pccb->cmdlen = 16;
86 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
87 debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n",
88 pccb->cmd[0], pccb->cmd[1],
89 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
90 pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
91 pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
92}
93#endif
94
Faiz Abbas76a6fa62019-10-15 18:24:32 +053095static void scsi_setup_inquiry(struct scsi_cmd *pccb)
96{
97 pccb->cmd[0] = SCSI_INQUIRY;
98 pccb->cmd[1] = pccb->lun << 5;
99 pccb->cmd[2] = 0;
100 pccb->cmd[3] = 0;
101 if (pccb->datalen > 255)
102 pccb->cmd[4] = 255;
103 else
104 pccb->cmd[4] = (unsigned char)pccb->datalen;
105 pccb->cmd[5] = 0;
106 pccb->cmdlen = 6;
107 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
108}
109
110#ifdef CONFIG_BLK
Simon Glass5fb559d2017-06-14 21:28:30 -0600111static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek9ec32f32016-11-30 11:53:43 +0100112 unsigned short blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600113{
114 pccb->cmd[0] = SCSI_READ10;
115 pccb->cmd[1] = pccb->lun << 5;
116 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
117 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
118 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
119 pccb->cmd[5] = (unsigned char)start & 0xff;
120 pccb->cmd[6] = 0;
121 pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
122 pccb->cmd[8] = (unsigned char)blocks & 0xff;
123 pccb->cmd[6] = 0;
124 pccb->cmdlen = 10;
125 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
126 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
127 pccb->cmd[0], pccb->cmd[1],
128 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
129 pccb->cmd[7], pccb->cmd[8]);
130}
131
Simon Glass5fb559d2017-06-14 21:28:30 -0600132static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek9ec32f32016-11-30 11:53:43 +0100133 unsigned short blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600134{
135 pccb->cmd[0] = SCSI_WRITE10;
136 pccb->cmd[1] = pccb->lun << 5;
137 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
138 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
139 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
140 pccb->cmd[5] = (unsigned char)start & 0xff;
141 pccb->cmd[6] = 0;
142 pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff;
143 pccb->cmd[8] = (unsigned char)blocks & 0xff;
144 pccb->cmd[9] = 0;
145 pccb->cmdlen = 10;
146 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
147 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
148 __func__,
149 pccb->cmd[0], pccb->cmd[1],
150 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
151 pccb->cmd[7], pccb->cmd[8]);
152}
153
Simon Glass5bf96dc2016-05-01 11:36:24 -0600154static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
155 void *buffer)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600156{
Simon Glass71fa5b42020-12-03 16:55:18 -0700157 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass11b2b622017-06-14 21:28:40 -0600158 struct udevice *bdev = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700159 struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530160 lbaint_t start, blks, max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600161 uintptr_t buf_addr;
162 unsigned short smallblks = 0;
Simon Glass5fb559d2017-06-14 21:28:30 -0600163 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600164
165 /* Setup device */
Michal Simekde6840a2016-11-18 16:22:42 +0100166 pccb->target = block_dev->target;
167 pccb->lun = block_dev->lun;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600168 buf_addr = (unsigned long)buffer;
169 start = blknr;
170 blks = blkcnt;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530171 if (uc_plat->max_bytes_per_req)
172 max_blks = uc_plat->max_bytes_per_req / block_dev->blksz;
173 else
174 max_blks = SCSI_MAX_BLK;
175
Simon Glassc8e89ab2016-05-01 11:36:09 -0600176 debug("\nscsi_read: dev %d startblk " LBAF
177 ", blccnt " LBAF " buffer %lx\n",
Michal Simekde6840a2016-11-18 16:22:42 +0100178 block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600179 do {
180 pccb->pdata = (unsigned char *)buf_addr;
Faiz Abbas20948092019-10-15 18:24:35 +0530181 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600182#ifdef CONFIG_SYS_64BIT_LBA
183 if (start > SCSI_LBA48_READ) {
184 unsigned long blocks;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530185 blocks = min_t(lbaint_t, blks, max_blks);
Michal Simekde6840a2016-11-18 16:22:42 +0100186 pccb->datalen = block_dev->blksz * blocks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600187 scsi_setup_read16(pccb, start, blocks);
188 start += blocks;
189 blks -= blocks;
190 } else
191#endif
Faiz Abbasb26c6142019-10-15 18:24:33 +0530192 if (blks > max_blks) {
193 pccb->datalen = block_dev->blksz * max_blks;
194 smallblks = max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600195 scsi_setup_read_ext(pccb, start, smallblks);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530196 start += max_blks;
197 blks -= max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600198 } else {
Michal Simekde6840a2016-11-18 16:22:42 +0100199 pccb->datalen = block_dev->blksz * blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600200 smallblks = (unsigned short)blks;
201 scsi_setup_read_ext(pccb, start, smallblks);
202 start += blks;
203 blks = 0;
204 }
205 debug("scsi_read_ext: startblk " LBAF
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900206 ", blccnt %x buffer %lX\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600207 start, smallblks, buf_addr);
Simon Glass11b2b622017-06-14 21:28:40 -0600208 if (scsi_exec(bdev, pccb)) {
Simon Glassc8e89ab2016-05-01 11:36:09 -0600209 scsi_print_error(pccb);
210 blkcnt -= blks;
211 break;
212 }
213 buf_addr += pccb->datalen;
214 } while (blks != 0);
215 debug("scsi_read_ext: end startblk " LBAF
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900216 ", blccnt %x buffer %lX\n", start, smallblks, buf_addr);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600217 return blkcnt;
218}
219
220/*******************************************************************************
221 * scsi_write
222 */
223
Simon Glass5bf96dc2016-05-01 11:36:24 -0600224static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
225 const void *buffer)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600226{
Simon Glass71fa5b42020-12-03 16:55:18 -0700227 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass11b2b622017-06-14 21:28:40 -0600228 struct udevice *bdev = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700229 struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530230 lbaint_t start, blks, max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600231 uintptr_t buf_addr;
232 unsigned short smallblks;
Simon Glass5fb559d2017-06-14 21:28:30 -0600233 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600234
Simon Glassc8e89ab2016-05-01 11:36:09 -0600235 /* Setup device */
Michal Simekde6840a2016-11-18 16:22:42 +0100236 pccb->target = block_dev->target;
237 pccb->lun = block_dev->lun;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600238 buf_addr = (unsigned long)buffer;
239 start = blknr;
240 blks = blkcnt;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530241 if (uc_plat->max_bytes_per_req)
242 max_blks = uc_plat->max_bytes_per_req / block_dev->blksz;
243 else
244 max_blks = SCSI_MAX_BLK;
245
Simon Glassc8e89ab2016-05-01 11:36:09 -0600246 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
Michal Simekde6840a2016-11-18 16:22:42 +0100247 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600248 do {
249 pccb->pdata = (unsigned char *)buf_addr;
Faiz Abbas20948092019-10-15 18:24:35 +0530250 pccb->dma_dir = DMA_TO_DEVICE;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530251 if (blks > max_blks) {
252 pccb->datalen = block_dev->blksz * max_blks;
253 smallblks = max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600254 scsi_setup_write_ext(pccb, start, smallblks);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530255 start += max_blks;
256 blks -= max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600257 } else {
Michal Simekde6840a2016-11-18 16:22:42 +0100258 pccb->datalen = block_dev->blksz * blks;
Simon Glassc8e89ab2016-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 }
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900264 debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600265 __func__, start, smallblks, buf_addr);
Simon Glass11b2b622017-06-14 21:28:40 -0600266 if (scsi_exec(bdev, pccb)) {
Simon Glassc8e89ab2016-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);
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900273 debug("%s: end startblk " LBAF ", blccnt %x buffer %lX\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600274 __func__, start, smallblks, buf_addr);
275 return blkcnt;
276}
Marek Vasutb980e212023-08-14 01:50:00 +0200277
278#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
279static int scsi_buffer_aligned(struct udevice *dev, struct bounce_buffer *state)
280{
281 struct scsi_ops *ops = scsi_get_ops(dev->parent);
282
283 if (ops->buffer_aligned)
284 return ops->buffer_aligned(dev->parent, state);
285
286 return 1;
287}
288#endif /* CONFIG_BOUNCE_BUFFER */
Faiz Abbas76a6fa62019-10-15 18:24:32 +0530289#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -0600290
Simon Glass57e7c2a2017-06-14 21:28:47 -0600291#if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) && \
292 !defined(CONFIG_DM_SCSI)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600293void scsi_init(void)
294{
295 int busdevfunc = -1;
296 int i;
297 /*
298 * Find a device from the list, this driver will support a single
299 * controller.
300 */
301 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
302 /* get PCI Device ID */
Simon Glassc8e89ab2016-05-01 11:36:09 -0600303 struct udevice *dev;
304 int ret;
305
306 ret = dm_pci_find_device(scsi_device_list[i].vendor,
307 scsi_device_list[i].device, 0, &dev);
308 if (!ret) {
309 busdevfunc = dm_pci_get_bdf(dev);
310 break;
311 }
Simon Glassc8e89ab2016-05-01 11:36:09 -0600312 if (busdevfunc != -1)
313 break;
314 }
315
316 if (busdevfunc == -1) {
317 printf("Error: SCSI Controller(s) ");
318 for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) {
319 printf("%04X:%04X ",
320 scsi_device_list[i].vendor,
321 scsi_device_list[i].device);
322 }
323 printf("not found\n");
324 return;
325 }
326#ifdef DEBUG
327 else {
328 printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n",
329 scsi_device_list[i].vendor,
330 scsi_device_list[i].device,
331 (busdevfunc >> 16) & 0xFF,
332 (busdevfunc >> 11) & 0x1F,
333 (busdevfunc >> 8) & 0x7);
334 }
335#endif
336 bootstage_start(BOOTSTAGE_ID_ACCUM_SCSI, "ahci");
337 scsi_low_level_init(busdevfunc);
Simon Glass48228732017-06-14 21:28:41 -0600338 scsi_scan(true);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600339 bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI);
340}
341#endif
342
Simon Glassc8e89ab2016-05-01 11:36:09 -0600343/* copy src to dest, skipping leading and trailing blanks
344 * and null terminate the string
345 */
Michal Simek9ec32f32016-11-30 11:53:43 +0100346static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
347 unsigned int len)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600348{
349 int start, end;
350
351 start = 0;
352 while (start < len) {
353 if (src[start] != ' ')
354 break;
355 start++;
356 }
357 end = len-1;
358 while (end > start) {
359 if (src[end] != ' ')
360 break;
361 end--;
362 }
363 for (; start <= end; start++)
364 *dest ++= src[start];
365 *dest = '\0';
366}
367
Simon Glass11b2b622017-06-14 21:28:40 -0600368static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb,
369 lbaint_t *capacity, unsigned long *blksz)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600370{
371 *capacity = 0;
372
373 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
374 pccb->cmd[0] = SCSI_RD_CAPAC10;
375 pccb->cmd[1] = pccb->lun << 5;
376 pccb->cmdlen = 10;
Nikita Yushchenkoef793ee2023-11-13 11:51:02 +0600377 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600378 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
379
380 pccb->datalen = 8;
Simon Glassa140e862017-06-14 21:28:44 -0600381 if (scsi_exec(dev, pccb))
Simon Glassc8e89ab2016-05-01 11:36:09 -0600382 return 1;
383
384 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
385 ((lbaint_t)pccb->pdata[1] << 16) |
386 ((lbaint_t)pccb->pdata[2] << 8) |
387 ((lbaint_t)pccb->pdata[3]);
388
389 if (*capacity != 0xffffffff) {
390 /* Read capacity (10) was sufficient for this drive. */
391 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
392 ((unsigned long)pccb->pdata[5] << 16) |
393 ((unsigned long)pccb->pdata[6] << 8) |
394 ((unsigned long)pccb->pdata[7]);
395 return 0;
396 }
397
398 /* Read capacity (10) was insufficient. Use read capacity (16). */
399 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
400 pccb->cmd[0] = SCSI_RD_CAPAC16;
401 pccb->cmd[1] = 0x10;
402 pccb->cmdlen = 16;
403 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
404
405 pccb->datalen = 16;
Faiz Abbas20948092019-10-15 18:24:35 +0530406 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassa140e862017-06-14 21:28:44 -0600407 if (scsi_exec(dev, pccb))
Simon Glassc8e89ab2016-05-01 11:36:09 -0600408 return 1;
409
410 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
411 ((uint64_t)pccb->pdata[1] << 48) |
412 ((uint64_t)pccb->pdata[2] << 40) |
413 ((uint64_t)pccb->pdata[3] << 32) |
414 ((uint64_t)pccb->pdata[4] << 24) |
415 ((uint64_t)pccb->pdata[5] << 16) |
416 ((uint64_t)pccb->pdata[6] << 8) |
417 ((uint64_t)pccb->pdata[7]);
418
419 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
420 ((uint64_t)pccb->pdata[9] << 48) |
421 ((uint64_t)pccb->pdata[10] << 40) |
422 ((uint64_t)pccb->pdata[11] << 32) |
423 ((uint64_t)pccb->pdata[12] << 24) |
424 ((uint64_t)pccb->pdata[13] << 16) |
425 ((uint64_t)pccb->pdata[14] << 8) |
426 ((uint64_t)pccb->pdata[15]);
427
428 return 0;
429}
430
431
432/*
433 * Some setup (fill-in) routines
434 */
Simon Glass5fb559d2017-06-14 21:28:30 -0600435static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600436{
437 pccb->cmd[0] = SCSI_TST_U_RDY;
438 pccb->cmd[1] = pccb->lun << 5;
439 pccb->cmd[2] = 0;
440 pccb->cmd[3] = 0;
441 pccb->cmd[4] = 0;
442 pccb->cmd[5] = 0;
443 pccb->cmdlen = 6;
444 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
445}
446
Michal Simek6241b522016-11-30 12:50:58 +0100447/**
448 * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
449 *
450 * @dev_desc: Block device description pointer
451 */
452static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
Michal Simekac0a18d12016-11-18 15:27:00 +0100453{
Tom Rini0154cfe2023-11-08 14:28:11 -0500454 memset(dev_desc, 0, sizeof(struct blk_desc));
Michal Simekac0a18d12016-11-18 15:27:00 +0100455 dev_desc->target = 0xff;
456 dev_desc->lun = 0xff;
Michal Simekac0a18d12016-11-18 15:27:00 +0100457 dev_desc->log2blksz =
458 LOG2_INVALID(typeof(dev_desc->log2blksz));
459 dev_desc->type = DEV_TYPE_UNKNOWN;
Johan Jonker10a986c2023-10-18 16:01:10 +0200460#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
461 dev_desc->bb = true;
462#endif /* CONFIG_BOUNCE_BUFFER */
Michal Simekac0a18d12016-11-18 15:27:00 +0100463}
464
Michal Simekc886f352016-09-08 15:06:45 +0200465#if !defined(CONFIG_DM_SCSI)
Michal Simek6241b522016-11-30 12:50:58 +0100466/**
467 * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties
468 *
469 * @dev_desc: Block device description pointer
470 * @devnum: Device number
471 */
472static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum)
473{
474 dev_desc->lba = 0;
475 dev_desc->blksz = 0;
Simon Glassfada3f92022-09-17 09:00:09 -0600476 dev_desc->uclass_id = UCLASS_SCSI;
Michal Simek6241b522016-11-30 12:50:58 +0100477 dev_desc->devnum = devnum;
478 dev_desc->part_type = PART_TYPE_UNKNOWN;
479
480 scsi_init_dev_desc_priv(dev_desc);
481}
Michal Simekc886f352016-09-08 15:06:45 +0200482#endif
Michal Simek40917492016-11-18 16:14:24 +0100483
Michal Simekebbde652016-11-18 15:42:13 +0100484/**
485 * scsi_detect_dev - Detect scsi device
486 *
Michal Simek40917492016-11-18 16:14:24 +0100487 * @target: target id
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200488 * @lun: target lun
Michal Simekebbde652016-11-18 15:42:13 +0100489 * @dev_desc: block device description
Michal Simekebbde652016-11-18 15:42:13 +0100490 *
491 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200492 * detected.
Michal Simekebbde652016-11-18 15:42:13 +0100493 *
494 * Return: 0 on success, error value otherwise
495 */
Simon Glass11b2b622017-06-14 21:28:40 -0600496static int scsi_detect_dev(struct udevice *dev, int target, int lun,
497 struct blk_desc *dev_desc)
Michal Simekebbde652016-11-18 15:42:13 +0100498{
499 unsigned char perq, modi;
500 lbaint_t capacity;
501 unsigned long blksz;
Simon Glass5fb559d2017-06-14 21:28:30 -0600502 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Faiz Abbas3645a482019-10-15 18:24:34 +0530503 int count, err;
Michal Simekebbde652016-11-18 15:42:13 +0100504
Michal Simek40917492016-11-18 16:14:24 +0100505 pccb->target = target;
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200506 pccb->lun = lun;
Marek Vasut60b20b42023-08-07 02:26:12 +0200507 pccb->pdata = tempbuff;
Michal Simekebbde652016-11-18 15:42:13 +0100508 pccb->datalen = 512;
Faiz Abbas20948092019-10-15 18:24:35 +0530509 pccb->dma_dir = DMA_FROM_DEVICE;
Michal Simekebbde652016-11-18 15:42:13 +0100510 scsi_setup_inquiry(pccb);
Simon Glassa140e862017-06-14 21:28:44 -0600511 if (scsi_exec(dev, pccb)) {
Michal Simekebbde652016-11-18 15:42:13 +0100512 if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
513 /*
514 * selection timeout => assuming no
515 * device present
516 */
517 debug("Selection timeout ID %d\n",
518 pccb->target);
519 return -ETIMEDOUT;
520 }
521 scsi_print_error(pccb);
522 return -ENODEV;
523 }
524 perq = tempbuff[0];
525 modi = tempbuff[1];
526 if ((perq & 0x1f) == 0x1f)
527 return -ENODEV; /* skip unknown devices */
528 if ((modi & 0x80) == 0x80) /* drive is removable */
529 dev_desc->removable = true;
530 /* get info for this device */
531 scsi_ident_cpy((unsigned char *)dev_desc->vendor,
532 &tempbuff[8], 8);
533 scsi_ident_cpy((unsigned char *)dev_desc->product,
534 &tempbuff[16], 16);
535 scsi_ident_cpy((unsigned char *)dev_desc->revision,
536 &tempbuff[32], 4);
537 dev_desc->target = pccb->target;
538 dev_desc->lun = pccb->lun;
539
Faiz Abbas3645a482019-10-15 18:24:34 +0530540 for (count = 0; count < 3; count++) {
541 pccb->datalen = 0;
Nikita Yushchenkoef793ee2023-11-13 11:51:02 +0600542 pccb->dma_dir = DMA_NONE;
Faiz Abbas3645a482019-10-15 18:24:34 +0530543 scsi_setup_test_unit_ready(pccb);
544 err = scsi_exec(dev, pccb);
545 if (!err)
546 break;
547 }
548 if (err) {
Michal Simekebbde652016-11-18 15:42:13 +0100549 if (dev_desc->removable) {
550 dev_desc->type = perq;
551 goto removable;
552 }
553 scsi_print_error(pccb);
554 return -EINVAL;
555 }
Simon Glass11b2b622017-06-14 21:28:40 -0600556 if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) {
Michal Simekebbde652016-11-18 15:42:13 +0100557 scsi_print_error(pccb);
558 return -EINVAL;
559 }
560 dev_desc->lba = capacity;
561 dev_desc->blksz = blksz;
562 dev_desc->log2blksz = LOG2(dev_desc->blksz);
563 dev_desc->type = perq;
Michal Simekebbde652016-11-18 15:42:13 +0100564removable:
565 return 0;
566}
567
Simon Glassc8e89ab2016-05-01 11:36:09 -0600568/*
569 * (re)-scan the scsi bus and reports scsi device info
570 * to the user if mode = 1
571 */
Michal Simekc886f352016-09-08 15:06:45 +0200572#if defined(CONFIG_DM_SCSI)
Simon Glass48228732017-06-14 21:28:41 -0600573static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200574{
575 int ret;
576 struct udevice *bdev;
577 struct blk_desc bd;
578 struct blk_desc *bdesc;
Simon Glass1af3eed2023-01-17 10:47:43 -0700579 char str[10], *name;
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200580
581 /*
582 * detect the scsi driver to get information about its geometry (block
583 * size, number of blocks) and other parameters (ids, type, ...)
584 */
585 scsi_init_dev_desc_priv(&bd);
Simon Glass11b2b622017-06-14 21:28:40 -0600586 if (scsi_detect_dev(dev, id, lun, &bd))
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200587 return -ENODEV;
588
589 /*
590 * Create only one block device and do detection
591 * to make sure that there won't be a lot of
592 * block devices created
593 */
594 snprintf(str, sizeof(str), "id%dlun%d", id, lun);
Simon Glass1af3eed2023-01-17 10:47:43 -0700595 name = strdup(str);
596 if (!name)
597 return log_msg_ret("nam", -ENOMEM);
598 ret = blk_create_devicef(dev, "scsi_blk", name, UCLASS_SCSI, -1,
Simon Glassdbfa32c2022-08-11 19:34:59 -0600599 bd.blksz, bd.lba, &bdev);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200600 if (ret) {
601 debug("Can't create device\n");
602 return ret;
603 }
Simon Glass1af3eed2023-01-17 10:47:43 -0700604 device_set_name_alloced(bdev);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200605
Simon Glass71fa5b42020-12-03 16:55:18 -0700606 bdesc = dev_get_uclass_plat(bdev);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200607 bdesc->target = id;
608 bdesc->lun = lun;
609 bdesc->removable = bd.removable;
610 bdesc->type = bd.type;
Johan Jonker10a986c2023-10-18 16:01:10 +0200611 bdesc->bb = bd.bb;
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200612 memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
613 memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
614 memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision));
Stefan Roesec52ee3e2021-04-07 09:12:36 +0200615 if (IS_ENABLED(CONFIG_SYS_BIG_ENDIAN)) {
616 ata_swap_buf_le16((u16 *)&bdesc->vendor, sizeof(bd.vendor) / 2);
617 ata_swap_buf_le16((u16 *)&bdesc->product, sizeof(bd.product) / 2);
618 ata_swap_buf_le16((u16 *)&bdesc->revision, sizeof(bd.revision) / 2);
619 }
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200620
AKASHI Takahiro368c3d52022-03-08 20:36:39 +0900621 ret = blk_probe_or_unbind(bdev);
622 if (ret < 0)
623 /* TODO: undo create */
Simon Glass9985e9c2023-01-17 10:47:45 -0700624 return log_msg_ret("pro", ret);
625
Simon Glassb1d581d2023-07-30 11:15:14 -0600626 ret = bootdev_setup_for_sibling_blk(bdev, "scsi_bootdev");
Simon Glass9985e9c2023-01-17 10:47:45 -0700627 if (ret)
628 return log_msg_ret("bd", ret);
AKASHI Takahiro368c3d52022-03-08 20:36:39 +0900629
Simon Glass48228732017-06-14 21:28:41 -0600630 if (verbose) {
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100631 printf(" Device %d: ", bdesc->devnum);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200632 dev_print(bdesc);
633 }
634 return 0;
635}
636
Simon Glass600d0012017-06-14 21:28:45 -0600637int scsi_scan_dev(struct udevice *dev, bool verbose)
638{
Simon Glassb75b15b2020-12-03 16:55:23 -0700639 struct scsi_plat *uc_plat; /* scsi controller plat */
Simon Glass600d0012017-06-14 21:28:45 -0600640 int ret;
641 int i;
642 int lun;
643
644 /* probe SCSI controller driver */
645 ret = device_probe(dev);
646 if (ret)
647 return ret;
648
Simon Glass71fa5b42020-12-03 16:55:18 -0700649 /* Get controller plat */
650 uc_plat = dev_get_uclass_plat(dev);
Simon Glass600d0012017-06-14 21:28:45 -0600651
652 for (i = 0; i < uc_plat->max_id; i++)
653 for (lun = 0; lun < uc_plat->max_lun; lun++)
654 do_scsi_scan_one(dev, i, lun, verbose);
655
656 return 0;
657}
658
Simon Glass48228732017-06-14 21:28:41 -0600659int scsi_scan(bool verbose)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600660{
Michal Simekc886f352016-09-08 15:06:45 +0200661 struct uclass *uc;
662 struct udevice *dev; /* SCSI controller */
Michal Simekebbde652016-11-18 15:42:13 +0100663 int ret;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600664
Simon Glass48228732017-06-14 21:28:41 -0600665 if (verbose)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600666 printf("scanning bus for devices...\n");
Michal Simekc886f352016-09-08 15:06:45 +0200667
668 ret = uclass_get(UCLASS_SCSI, &uc);
669 if (ret)
670 return ret;
671
Simon Glass01ed80a2023-01-17 10:47:44 -0700672 /* remove all children of the SCSI devices */
673 uclass_foreach_dev(dev, uc) {
674 log_debug("unbind %s\n", dev->name);
675 ret = device_chld_remove(dev, NULL, DM_REMOVE_NORMAL);
676 if (!ret)
677 ret = device_chld_unbind(dev, NULL);
678 if (ret) {
679 if (verbose)
680 printf("unable to unbind devices (%dE)\n", ret);
681 return log_msg_ret("unb", ret);
682 }
683 }
684
Michal Simekc886f352016-09-08 15:06:45 +0200685 uclass_foreach_dev(dev, uc) {
Simon Glass600d0012017-06-14 21:28:45 -0600686 ret = scsi_scan_dev(dev, verbose);
Michal Simekc886f352016-09-08 15:06:45 +0200687 if (ret)
688 return ret;
Michal Simekc886f352016-09-08 15:06:45 +0200689 }
690
691 return 0;
692}
693#else
Simon Glass48228732017-06-14 21:28:41 -0600694int scsi_scan(bool verbose)
Michal Simekc886f352016-09-08 15:06:45 +0200695{
696 unsigned char i, lun;
697 int ret;
698
Simon Glass48228732017-06-14 21:28:41 -0600699 if (verbose)
Michal Simekc886f352016-09-08 15:06:45 +0200700 printf("scanning bus for devices...\n");
Simon Glass1a462242022-01-31 07:49:36 -0700701 for (i = 0; i < SCSI_MAX_DEVICE; i++)
Michal Simekac0a18d12016-11-18 15:27:00 +0100702 scsi_init_dev_desc(&scsi_dev_desc[i], i);
703
Simon Glassc8e89ab2016-05-01 11:36:09 -0600704 scsi_max_devs = 0;
705 for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) {
Simon Glassc8e89ab2016-05-01 11:36:09 -0600706 for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) {
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100707 struct blk_desc *bdesc = &scsi_dev_desc[scsi_max_devs];
708
709 ret = scsi_detect_dev(NULL, i, lun, bdesc);
Michal Simekebbde652016-11-18 15:42:13 +0100710 if (ret)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600711 continue;
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100712 part_init(bdesc);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600713
Simon Glass48228732017-06-14 21:28:41 -0600714 if (verbose) {
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100715 printf(" Device %d: ", bdesc->devnum);
716 dev_print(bdesc);
Simon Glass48228732017-06-14 21:28:41 -0600717 }
Simon Glassc8e89ab2016-05-01 11:36:09 -0600718 scsi_max_devs++;
719 } /* next LUN */
720 }
721 if (scsi_max_devs > 0)
722 scsi_curr_dev = 0;
723 else
724 scsi_curr_dev = -1;
725
726 printf("Found %d device(s).\n", scsi_max_devs);
727#ifndef CONFIG_SPL_BUILD
Simon Glass4d949a22017-08-03 12:22:10 -0600728 env_set_ulong("scsidevs", scsi_max_devs);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600729#endif
Michal Simek5dc196f2016-11-30 12:12:31 +0100730 return 0;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600731}
Michal Simekc886f352016-09-08 15:06:45 +0200732#endif
Simon Glassc8e89ab2016-05-01 11:36:09 -0600733
Simon Glass5bf96dc2016-05-01 11:36:24 -0600734#ifdef CONFIG_BLK
735static const struct blk_ops scsi_blk_ops = {
736 .read = scsi_read,
737 .write = scsi_write,
Marek Vasutb980e212023-08-14 01:50:00 +0200738#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
739 .buffer_aligned = scsi_buffer_aligned,
740#endif /* CONFIG_BOUNCE_BUFFER */
Simon Glass5bf96dc2016-05-01 11:36:24 -0600741};
742
743U_BOOT_DRIVER(scsi_blk) = {
744 .name = "scsi_blk",
745 .id = UCLASS_BLK,
746 .ops = &scsi_blk_ops,
747};
748#else
Simon Glassc8e89ab2016-05-01 11:36:09 -0600749U_BOOT_LEGACY_BLK(scsi) = {
Simon Glassfada3f92022-09-17 09:00:09 -0600750 .uclass_idname = "scsi",
751 .uclass_id = UCLASS_SCSI,
Simon Glass1a462242022-01-31 07:49:36 -0700752 .max_devs = SCSI_MAX_DEVICE,
Simon Glassc8e89ab2016-05-01 11:36:09 -0600753 .desc = scsi_dev_desc,
754};
Simon Glass5bf96dc2016-05-01 11:36:24 -0600755#endif