blob: 51cacf3479236be6c6ea3e7d15b87e03f10e7f3a [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 Glass655306c2020-05-10 11:39:58 -06009#include <blk.h>
Simon Glass9985e9c2023-01-17 10:47:45 -070010#include <bootdev.h>
Simon Glass1ea97892020-05-10 11:40:00 -060011#include <bootstage.h>
Simon Glass5bf96dc2016-05-01 11:36:24 -060012#include <dm.h>
Simon Glass07dc93c2019-08-01 09:46:47 -060013#include <env.h>
Stefan Roesec52ee3e2021-04-07 09:12:36 +020014#include <libata.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Marek Vasut60b20b42023-08-07 02:26:12 +020016#include <memalign.h>
Simon Glass655306c2020-05-10 11:39:58 -060017#include <part.h>
Simon Glassc8e89ab2016-05-01 11:36:09 -060018#include <pci.h>
19#include <scsi.h>
Michal Simekc886f352016-09-08 15:06:45 +020020#include <dm/device-internal.h>
21#include <dm/uclass-internal.h>
Simon Glassc8e89ab2016-05-01 11:36:09 -060022
Simon Glass5fb559d2017-06-14 21:28:30 -060023static struct scsi_cmd tempccb; /* temporary scsi command buffer */
Simon Glassc8e89ab2016-05-01 11:36:09 -060024
Marek Vasut60b20b42023-08-07 02:26:12 +020025DEFINE_CACHE_ALIGN_BUFFER(u8, tempbuff, 512); /* temporary data buffer */
Simon Glassc8e89ab2016-05-01 11:36:09 -060026
Simon Glassc8e89ab2016-05-01 11:36:09 -060027/* almost the maximum amount of the scsi_ext command.. */
Faiz Abbasb26c6142019-10-15 18:24:33 +053028#define SCSI_MAX_BLK 0xFFFF
Simon Glassc8e89ab2016-05-01 11:36:09 -060029#define SCSI_LBA48_READ 0xFFFFFFF
30
Simon Glass5fb559d2017-06-14 21:28:30 -060031static void scsi_print_error(struct scsi_cmd *pccb)
Simon Glass16740982017-06-14 21:28:23 -060032{
33 /* Dummy function that could print an error for debugging */
34}
35
Simon Glassc8e89ab2016-05-01 11:36:09 -060036#ifdef CONFIG_SYS_64BIT_LBA
Simon Glass5fb559d2017-06-14 21:28:30 -060037void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start,
38 unsigned long blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -060039{
40 pccb->cmd[0] = SCSI_READ16;
41 pccb->cmd[1] = pccb->lun << 5;
42 pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff;
43 pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff;
44 pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff;
45 pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff;
46 pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff;
47 pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff;
48 pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff;
49 pccb->cmd[9] = (unsigned char)start & 0xff;
50 pccb->cmd[10] = 0;
51 pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff;
52 pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff;
53 pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff;
54 pccb->cmd[14] = (unsigned char)blocks & 0xff;
55 pccb->cmd[15] = 0;
56 pccb->cmdlen = 16;
57 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
58 debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n",
59 pccb->cmd[0], pccb->cmd[1],
60 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
61 pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9],
62 pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]);
63}
64#endif
65
Faiz Abbas76a6fa62019-10-15 18:24:32 +053066static void scsi_setup_inquiry(struct scsi_cmd *pccb)
67{
68 pccb->cmd[0] = SCSI_INQUIRY;
69 pccb->cmd[1] = pccb->lun << 5;
70 pccb->cmd[2] = 0;
71 pccb->cmd[3] = 0;
72 if (pccb->datalen > 255)
73 pccb->cmd[4] = 255;
74 else
75 pccb->cmd[4] = (unsigned char)pccb->datalen;
76 pccb->cmd[5] = 0;
77 pccb->cmdlen = 6;
78 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
79}
80
Simon Glass5fb559d2017-06-14 21:28:30 -060081static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek9ec32f32016-11-30 11:53:43 +010082 unsigned short blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -060083{
84 pccb->cmd[0] = SCSI_READ10;
85 pccb->cmd[1] = pccb->lun << 5;
86 pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff;
87 pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff;
88 pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff;
89 pccb->cmd[5] = (unsigned char)start & 0xff;
90 pccb->cmd[6] = 0;
91 pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff;
92 pccb->cmd[8] = (unsigned char)blocks & 0xff;
93 pccb->cmd[6] = 0;
94 pccb->cmdlen = 10;
95 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
96 debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
97 pccb->cmd[0], pccb->cmd[1],
98 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
99 pccb->cmd[7], pccb->cmd[8]);
100}
101
Simon Glass5fb559d2017-06-14 21:28:30 -0600102static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start,
Michal Simek9ec32f32016-11-30 11:53:43 +0100103 unsigned short blocks)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600104{
105 pccb->cmd[0] = SCSI_WRITE10;
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[9] = 0;
115 pccb->cmdlen = 10;
116 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
117 debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n",
118 __func__,
119 pccb->cmd[0], pccb->cmd[1],
120 pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5],
121 pccb->cmd[7], pccb->cmd[8]);
122}
123
Simon Glass5bf96dc2016-05-01 11:36:24 -0600124static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
125 void *buffer)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600126{
Simon Glass71fa5b42020-12-03 16:55:18 -0700127 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass11b2b622017-06-14 21:28:40 -0600128 struct udevice *bdev = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700129 struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530130 lbaint_t start, blks, max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600131 uintptr_t buf_addr;
132 unsigned short smallblks = 0;
Simon Glass5fb559d2017-06-14 21:28:30 -0600133 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600134
135 /* Setup device */
Michal Simekde6840a2016-11-18 16:22:42 +0100136 pccb->target = block_dev->target;
137 pccb->lun = block_dev->lun;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600138 buf_addr = (unsigned long)buffer;
139 start = blknr;
140 blks = blkcnt;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530141 if (uc_plat->max_bytes_per_req)
142 max_blks = uc_plat->max_bytes_per_req / block_dev->blksz;
143 else
144 max_blks = SCSI_MAX_BLK;
145
Simon Glassc8e89ab2016-05-01 11:36:09 -0600146 debug("\nscsi_read: dev %d startblk " LBAF
147 ", blccnt " LBAF " buffer %lx\n",
Michal Simekde6840a2016-11-18 16:22:42 +0100148 block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600149 do {
150 pccb->pdata = (unsigned char *)buf_addr;
Faiz Abbas20948092019-10-15 18:24:35 +0530151 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600152#ifdef CONFIG_SYS_64BIT_LBA
153 if (start > SCSI_LBA48_READ) {
154 unsigned long blocks;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530155 blocks = min_t(lbaint_t, blks, max_blks);
Michal Simekde6840a2016-11-18 16:22:42 +0100156 pccb->datalen = block_dev->blksz * blocks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600157 scsi_setup_read16(pccb, start, blocks);
158 start += blocks;
159 blks -= blocks;
160 } else
161#endif
Faiz Abbasb26c6142019-10-15 18:24:33 +0530162 if (blks > max_blks) {
163 pccb->datalen = block_dev->blksz * max_blks;
164 smallblks = max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600165 scsi_setup_read_ext(pccb, start, smallblks);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530166 start += max_blks;
167 blks -= max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600168 } else {
Michal Simekde6840a2016-11-18 16:22:42 +0100169 pccb->datalen = block_dev->blksz * blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600170 smallblks = (unsigned short)blks;
171 scsi_setup_read_ext(pccb, start, smallblks);
172 start += blks;
173 blks = 0;
174 }
175 debug("scsi_read_ext: startblk " LBAF
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900176 ", blccnt %x buffer %lX\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600177 start, smallblks, buf_addr);
Simon Glass11b2b622017-06-14 21:28:40 -0600178 if (scsi_exec(bdev, pccb)) {
Simon Glassc8e89ab2016-05-01 11:36:09 -0600179 scsi_print_error(pccb);
180 blkcnt -= blks;
181 break;
182 }
183 buf_addr += pccb->datalen;
184 } while (blks != 0);
185 debug("scsi_read_ext: end startblk " LBAF
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900186 ", blccnt %x buffer %lX\n", start, smallblks, buf_addr);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600187 return blkcnt;
188}
189
190/*******************************************************************************
191 * scsi_write
192 */
193
Simon Glass5bf96dc2016-05-01 11:36:24 -0600194static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt,
195 const void *buffer)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600196{
Simon Glass71fa5b42020-12-03 16:55:18 -0700197 struct blk_desc *block_dev = dev_get_uclass_plat(dev);
Simon Glass11b2b622017-06-14 21:28:40 -0600198 struct udevice *bdev = dev->parent;
Simon Glassb75b15b2020-12-03 16:55:23 -0700199 struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530200 lbaint_t start, blks, max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600201 uintptr_t buf_addr;
202 unsigned short smallblks;
Simon Glass5fb559d2017-06-14 21:28:30 -0600203 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600204
Simon Glassc8e89ab2016-05-01 11:36:09 -0600205 /* Setup device */
Michal Simekde6840a2016-11-18 16:22:42 +0100206 pccb->target = block_dev->target;
207 pccb->lun = block_dev->lun;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600208 buf_addr = (unsigned long)buffer;
209 start = blknr;
210 blks = blkcnt;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530211 if (uc_plat->max_bytes_per_req)
212 max_blks = uc_plat->max_bytes_per_req / block_dev->blksz;
213 else
214 max_blks = SCSI_MAX_BLK;
215
Simon Glassc8e89ab2016-05-01 11:36:09 -0600216 debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n",
Michal Simekde6840a2016-11-18 16:22:42 +0100217 __func__, block_dev->devnum, start, blks, (unsigned long)buffer);
Simon Glassc8e89ab2016-05-01 11:36:09 -0600218 do {
219 pccb->pdata = (unsigned char *)buf_addr;
Faiz Abbas20948092019-10-15 18:24:35 +0530220 pccb->dma_dir = DMA_TO_DEVICE;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530221 if (blks > max_blks) {
222 pccb->datalen = block_dev->blksz * max_blks;
223 smallblks = max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600224 scsi_setup_write_ext(pccb, start, smallblks);
Faiz Abbasb26c6142019-10-15 18:24:33 +0530225 start += max_blks;
226 blks -= max_blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600227 } else {
Michal Simekde6840a2016-11-18 16:22:42 +0100228 pccb->datalen = block_dev->blksz * blks;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600229 smallblks = (unsigned short)blks;
230 scsi_setup_write_ext(pccb, start, smallblks);
231 start += blks;
232 blks = 0;
233 }
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900234 debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600235 __func__, start, smallblks, buf_addr);
Simon Glass11b2b622017-06-14 21:28:40 -0600236 if (scsi_exec(bdev, pccb)) {
Simon Glassc8e89ab2016-05-01 11:36:09 -0600237 scsi_print_error(pccb);
238 blkcnt -= blks;
239 break;
240 }
241 buf_addr += pccb->datalen;
242 } while (blks != 0);
Masahiro Yamadac7570a32018-08-06 20:47:40 +0900243 debug("%s: end startblk " LBAF ", blccnt %x buffer %lX\n",
Simon Glassc8e89ab2016-05-01 11:36:09 -0600244 __func__, start, smallblks, buf_addr);
245 return blkcnt;
246}
Marek Vasutb980e212023-08-14 01:50:00 +0200247
248#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
249static int scsi_buffer_aligned(struct udevice *dev, struct bounce_buffer *state)
250{
251 struct scsi_ops *ops = scsi_get_ops(dev->parent);
252
253 if (ops->buffer_aligned)
254 return ops->buffer_aligned(dev->parent, state);
255
256 return 1;
257}
258#endif /* CONFIG_BOUNCE_BUFFER */
Simon Glassc8e89ab2016-05-01 11:36:09 -0600259
Simon Glassc8e89ab2016-05-01 11:36:09 -0600260/* copy src to dest, skipping leading and trailing blanks
261 * and null terminate the string
262 */
Michal Simek9ec32f32016-11-30 11:53:43 +0100263static void scsi_ident_cpy(unsigned char *dest, unsigned char *src,
264 unsigned int len)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600265{
266 int start, end;
267
268 start = 0;
269 while (start < len) {
270 if (src[start] != ' ')
271 break;
272 start++;
273 }
274 end = len-1;
275 while (end > start) {
276 if (src[end] != ' ')
277 break;
278 end--;
279 }
280 for (; start <= end; start++)
281 *dest ++= src[start];
282 *dest = '\0';
283}
284
Simon Glass11b2b622017-06-14 21:28:40 -0600285static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb,
286 lbaint_t *capacity, unsigned long *blksz)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600287{
288 *capacity = 0;
289
290 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
291 pccb->cmd[0] = SCSI_RD_CAPAC10;
292 pccb->cmd[1] = pccb->lun << 5;
293 pccb->cmdlen = 10;
Nikita Yushchenkoef793ee2023-11-13 11:51:02 +0600294 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600295 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
296
297 pccb->datalen = 8;
Simon Glassa140e862017-06-14 21:28:44 -0600298 if (scsi_exec(dev, pccb))
Simon Glassc8e89ab2016-05-01 11:36:09 -0600299 return 1;
300
301 *capacity = ((lbaint_t)pccb->pdata[0] << 24) |
302 ((lbaint_t)pccb->pdata[1] << 16) |
303 ((lbaint_t)pccb->pdata[2] << 8) |
304 ((lbaint_t)pccb->pdata[3]);
305
306 if (*capacity != 0xffffffff) {
307 /* Read capacity (10) was sufficient for this drive. */
308 *blksz = ((unsigned long)pccb->pdata[4] << 24) |
309 ((unsigned long)pccb->pdata[5] << 16) |
310 ((unsigned long)pccb->pdata[6] << 8) |
311 ((unsigned long)pccb->pdata[7]);
312 return 0;
313 }
314
315 /* Read capacity (10) was insufficient. Use read capacity (16). */
316 memset(pccb->cmd, '\0', sizeof(pccb->cmd));
317 pccb->cmd[0] = SCSI_RD_CAPAC16;
318 pccb->cmd[1] = 0x10;
319 pccb->cmdlen = 16;
320 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
321
322 pccb->datalen = 16;
Faiz Abbas20948092019-10-15 18:24:35 +0530323 pccb->dma_dir = DMA_FROM_DEVICE;
Simon Glassa140e862017-06-14 21:28:44 -0600324 if (scsi_exec(dev, pccb))
Simon Glassc8e89ab2016-05-01 11:36:09 -0600325 return 1;
326
327 *capacity = ((uint64_t)pccb->pdata[0] << 56) |
328 ((uint64_t)pccb->pdata[1] << 48) |
329 ((uint64_t)pccb->pdata[2] << 40) |
330 ((uint64_t)pccb->pdata[3] << 32) |
331 ((uint64_t)pccb->pdata[4] << 24) |
332 ((uint64_t)pccb->pdata[5] << 16) |
333 ((uint64_t)pccb->pdata[6] << 8) |
334 ((uint64_t)pccb->pdata[7]);
335
336 *blksz = ((uint64_t)pccb->pdata[8] << 56) |
337 ((uint64_t)pccb->pdata[9] << 48) |
338 ((uint64_t)pccb->pdata[10] << 40) |
339 ((uint64_t)pccb->pdata[11] << 32) |
340 ((uint64_t)pccb->pdata[12] << 24) |
341 ((uint64_t)pccb->pdata[13] << 16) |
342 ((uint64_t)pccb->pdata[14] << 8) |
343 ((uint64_t)pccb->pdata[15]);
344
345 return 0;
346}
347
Simon Glassc8e89ab2016-05-01 11:36:09 -0600348/*
349 * Some setup (fill-in) routines
350 */
Simon Glass5fb559d2017-06-14 21:28:30 -0600351static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600352{
353 pccb->cmd[0] = SCSI_TST_U_RDY;
354 pccb->cmd[1] = pccb->lun << 5;
355 pccb->cmd[2] = 0;
356 pccb->cmd[3] = 0;
357 pccb->cmd[4] = 0;
358 pccb->cmd[5] = 0;
359 pccb->cmdlen = 6;
360 pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */
361}
362
Michal Simek6241b522016-11-30 12:50:58 +0100363/**
364 * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties
365 *
366 * @dev_desc: Block device description pointer
367 */
368static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc)
Michal Simekac0a18d12016-11-18 15:27:00 +0100369{
Tom Rini0154cfe2023-11-08 14:28:11 -0500370 memset(dev_desc, 0, sizeof(struct blk_desc));
Michal Simekac0a18d12016-11-18 15:27:00 +0100371 dev_desc->target = 0xff;
372 dev_desc->lun = 0xff;
Michal Simekac0a18d12016-11-18 15:27:00 +0100373 dev_desc->log2blksz =
374 LOG2_INVALID(typeof(dev_desc->log2blksz));
375 dev_desc->type = DEV_TYPE_UNKNOWN;
Johan Jonker10a986c2023-10-18 16:01:10 +0200376#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
377 dev_desc->bb = true;
378#endif /* CONFIG_BOUNCE_BUFFER */
Michal Simekac0a18d12016-11-18 15:27:00 +0100379}
380
Michal Simekebbde652016-11-18 15:42:13 +0100381/**
382 * scsi_detect_dev - Detect scsi device
383 *
Michal Simek40917492016-11-18 16:14:24 +0100384 * @target: target id
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200385 * @lun: target lun
Michal Simekebbde652016-11-18 15:42:13 +0100386 * @dev_desc: block device description
Michal Simekebbde652016-11-18 15:42:13 +0100387 *
388 * The scsi_detect_dev detects and fills a dev_desc structure when the device is
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200389 * detected.
Michal Simekebbde652016-11-18 15:42:13 +0100390 *
391 * Return: 0 on success, error value otherwise
392 */
Simon Glass11b2b622017-06-14 21:28:40 -0600393static int scsi_detect_dev(struct udevice *dev, int target, int lun,
394 struct blk_desc *dev_desc)
Michal Simekebbde652016-11-18 15:42:13 +0100395{
396 unsigned char perq, modi;
397 lbaint_t capacity;
398 unsigned long blksz;
Simon Glass5fb559d2017-06-14 21:28:30 -0600399 struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb;
Faiz Abbas3645a482019-10-15 18:24:34 +0530400 int count, err;
Michal Simekebbde652016-11-18 15:42:13 +0100401
Michal Simek40917492016-11-18 16:14:24 +0100402 pccb->target = target;
Jean-Jacques Hiblot0c447f02017-04-07 13:42:06 +0200403 pccb->lun = lun;
Marek Vasut60b20b42023-08-07 02:26:12 +0200404 pccb->pdata = tempbuff;
Michal Simekebbde652016-11-18 15:42:13 +0100405 pccb->datalen = 512;
Faiz Abbas20948092019-10-15 18:24:35 +0530406 pccb->dma_dir = DMA_FROM_DEVICE;
Michal Simekebbde652016-11-18 15:42:13 +0100407 scsi_setup_inquiry(pccb);
Simon Glassa140e862017-06-14 21:28:44 -0600408 if (scsi_exec(dev, pccb)) {
Michal Simekebbde652016-11-18 15:42:13 +0100409 if (pccb->contr_stat == SCSI_SEL_TIME_OUT) {
410 /*
411 * selection timeout => assuming no
412 * device present
413 */
414 debug("Selection timeout ID %d\n",
415 pccb->target);
416 return -ETIMEDOUT;
417 }
418 scsi_print_error(pccb);
419 return -ENODEV;
420 }
421 perq = tempbuff[0];
422 modi = tempbuff[1];
423 if ((perq & 0x1f) == 0x1f)
424 return -ENODEV; /* skip unknown devices */
425 if ((modi & 0x80) == 0x80) /* drive is removable */
426 dev_desc->removable = true;
427 /* get info for this device */
428 scsi_ident_cpy((unsigned char *)dev_desc->vendor,
429 &tempbuff[8], 8);
430 scsi_ident_cpy((unsigned char *)dev_desc->product,
431 &tempbuff[16], 16);
432 scsi_ident_cpy((unsigned char *)dev_desc->revision,
433 &tempbuff[32], 4);
434 dev_desc->target = pccb->target;
435 dev_desc->lun = pccb->lun;
436
Faiz Abbas3645a482019-10-15 18:24:34 +0530437 for (count = 0; count < 3; count++) {
438 pccb->datalen = 0;
Nikita Yushchenkoef793ee2023-11-13 11:51:02 +0600439 pccb->dma_dir = DMA_NONE;
Faiz Abbas3645a482019-10-15 18:24:34 +0530440 scsi_setup_test_unit_ready(pccb);
441 err = scsi_exec(dev, pccb);
442 if (!err)
443 break;
444 }
445 if (err) {
Michal Simekebbde652016-11-18 15:42:13 +0100446 if (dev_desc->removable) {
447 dev_desc->type = perq;
448 goto removable;
449 }
450 scsi_print_error(pccb);
451 return -EINVAL;
452 }
Simon Glass11b2b622017-06-14 21:28:40 -0600453 if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) {
Michal Simekebbde652016-11-18 15:42:13 +0100454 scsi_print_error(pccb);
455 return -EINVAL;
456 }
457 dev_desc->lba = capacity;
458 dev_desc->blksz = blksz;
459 dev_desc->log2blksz = LOG2(dev_desc->blksz);
460 dev_desc->type = perq;
Michal Simekebbde652016-11-18 15:42:13 +0100461removable:
462 return 0;
463}
464
Simon Glassc8e89ab2016-05-01 11:36:09 -0600465/*
466 * (re)-scan the scsi bus and reports scsi device info
467 * to the user if mode = 1
468 */
Simon Glass48228732017-06-14 21:28:41 -0600469static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose)
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200470{
471 int ret;
472 struct udevice *bdev;
473 struct blk_desc bd;
474 struct blk_desc *bdesc;
Simon Glass1af3eed2023-01-17 10:47:43 -0700475 char str[10], *name;
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200476
477 /*
478 * detect the scsi driver to get information about its geometry (block
479 * size, number of blocks) and other parameters (ids, type, ...)
480 */
481 scsi_init_dev_desc_priv(&bd);
Simon Glass11b2b622017-06-14 21:28:40 -0600482 if (scsi_detect_dev(dev, id, lun, &bd))
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200483 return -ENODEV;
484
485 /*
486 * Create only one block device and do detection
487 * to make sure that there won't be a lot of
488 * block devices created
489 */
490 snprintf(str, sizeof(str), "id%dlun%d", id, lun);
Simon Glass1af3eed2023-01-17 10:47:43 -0700491 name = strdup(str);
492 if (!name)
493 return log_msg_ret("nam", -ENOMEM);
494 ret = blk_create_devicef(dev, "scsi_blk", name, UCLASS_SCSI, -1,
Simon Glassdbfa32c2022-08-11 19:34:59 -0600495 bd.blksz, bd.lba, &bdev);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200496 if (ret) {
497 debug("Can't create device\n");
498 return ret;
499 }
Simon Glass1af3eed2023-01-17 10:47:43 -0700500 device_set_name_alloced(bdev);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200501
Simon Glass71fa5b42020-12-03 16:55:18 -0700502 bdesc = dev_get_uclass_plat(bdev);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200503 bdesc->target = id;
504 bdesc->lun = lun;
505 bdesc->removable = bd.removable;
506 bdesc->type = bd.type;
Johan Jonker10a986c2023-10-18 16:01:10 +0200507 bdesc->bb = bd.bb;
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200508 memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor));
509 memcpy(&bdesc->product, &bd.product, sizeof(bd.product));
510 memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision));
Stefan Roesec52ee3e2021-04-07 09:12:36 +0200511 if (IS_ENABLED(CONFIG_SYS_BIG_ENDIAN)) {
512 ata_swap_buf_le16((u16 *)&bdesc->vendor, sizeof(bd.vendor) / 2);
513 ata_swap_buf_le16((u16 *)&bdesc->product, sizeof(bd.product) / 2);
514 ata_swap_buf_le16((u16 *)&bdesc->revision, sizeof(bd.revision) / 2);
515 }
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200516
AKASHI Takahiro368c3d52022-03-08 20:36:39 +0900517 ret = blk_probe_or_unbind(bdev);
518 if (ret < 0)
519 /* TODO: undo create */
Simon Glass9985e9c2023-01-17 10:47:45 -0700520 return log_msg_ret("pro", ret);
521
Simon Glassb1d581d2023-07-30 11:15:14 -0600522 ret = bootdev_setup_for_sibling_blk(bdev, "scsi_bootdev");
Simon Glass9985e9c2023-01-17 10:47:45 -0700523 if (ret)
524 return log_msg_ret("bd", ret);
AKASHI Takahiro368c3d52022-03-08 20:36:39 +0900525
Simon Glass48228732017-06-14 21:28:41 -0600526 if (verbose) {
Heinrich Schuchardtc5382b72019-02-05 18:06:24 +0100527 printf(" Device %d: ", bdesc->devnum);
Jean-Jacques Hiblotcdf96eb2017-04-24 11:51:26 +0200528 dev_print(bdesc);
529 }
530 return 0;
531}
532
Simon Glass600d0012017-06-14 21:28:45 -0600533int scsi_scan_dev(struct udevice *dev, bool verbose)
534{
Simon Glassb75b15b2020-12-03 16:55:23 -0700535 struct scsi_plat *uc_plat; /* scsi controller plat */
Simon Glass600d0012017-06-14 21:28:45 -0600536 int ret;
537 int i;
538 int lun;
539
540 /* probe SCSI controller driver */
541 ret = device_probe(dev);
542 if (ret)
543 return ret;
544
Simon Glass71fa5b42020-12-03 16:55:18 -0700545 /* Get controller plat */
546 uc_plat = dev_get_uclass_plat(dev);
Simon Glass600d0012017-06-14 21:28:45 -0600547
548 for (i = 0; i < uc_plat->max_id; i++)
549 for (lun = 0; lun < uc_plat->max_lun; lun++)
550 do_scsi_scan_one(dev, i, lun, verbose);
551
552 return 0;
553}
554
Simon Glass48228732017-06-14 21:28:41 -0600555int scsi_scan(bool verbose)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600556{
Michal Simekc886f352016-09-08 15:06:45 +0200557 struct uclass *uc;
558 struct udevice *dev; /* SCSI controller */
Michal Simekebbde652016-11-18 15:42:13 +0100559 int ret;
Simon Glassc8e89ab2016-05-01 11:36:09 -0600560
Simon Glass48228732017-06-14 21:28:41 -0600561 if (verbose)
Simon Glassc8e89ab2016-05-01 11:36:09 -0600562 printf("scanning bus for devices...\n");
Michal Simekc886f352016-09-08 15:06:45 +0200563
564 ret = uclass_get(UCLASS_SCSI, &uc);
565 if (ret)
566 return ret;
567
Simon Glass01ed80a2023-01-17 10:47:44 -0700568 /* remove all children of the SCSI devices */
569 uclass_foreach_dev(dev, uc) {
570 log_debug("unbind %s\n", dev->name);
571 ret = device_chld_remove(dev, NULL, DM_REMOVE_NORMAL);
572 if (!ret)
573 ret = device_chld_unbind(dev, NULL);
574 if (ret) {
575 if (verbose)
576 printf("unable to unbind devices (%dE)\n", ret);
577 return log_msg_ret("unb", ret);
578 }
579 }
580
Michal Simekc886f352016-09-08 15:06:45 +0200581 uclass_foreach_dev(dev, uc) {
Simon Glass600d0012017-06-14 21:28:45 -0600582 ret = scsi_scan_dev(dev, verbose);
Michal Simekc886f352016-09-08 15:06:45 +0200583 if (ret)
584 return ret;
Michal Simekc886f352016-09-08 15:06:45 +0200585 }
586
587 return 0;
588}
Michal Simekc886f352016-09-08 15:06:45 +0200589
Simon Glass5bf96dc2016-05-01 11:36:24 -0600590static const struct blk_ops scsi_blk_ops = {
591 .read = scsi_read,
592 .write = scsi_write,
Marek Vasutb980e212023-08-14 01:50:00 +0200593#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
594 .buffer_aligned = scsi_buffer_aligned,
595#endif /* CONFIG_BOUNCE_BUFFER */
Simon Glass5bf96dc2016-05-01 11:36:24 -0600596};
597
598U_BOOT_DRIVER(scsi_blk) = {
599 .name = "scsi_blk",
600 .id = UCLASS_BLK,
601 .ops = &scsi_blk_ops,
602};