Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Denis Peter, MPL AG Switzerland |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | 1af3eed | 2023-01-17 10:47:43 -0700 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_SCSI |
| 8 | |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 9 | #include <blk.h> |
Simon Glass | 9985e9c | 2023-01-17 10:47:45 -0700 | [diff] [blame] | 10 | #include <bootdev.h> |
Simon Glass | 1ea9789 | 2020-05-10 11:40:00 -0600 | [diff] [blame] | 11 | #include <bootstage.h> |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 07dc93c | 2019-08-01 09:46:47 -0600 | [diff] [blame] | 13 | #include <env.h> |
Stefan Roese | c52ee3e | 2021-04-07 09:12:36 +0200 | [diff] [blame] | 14 | #include <libata.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Marek Vasut | 60b20b4 | 2023-08-07 02:26:12 +0200 | [diff] [blame] | 16 | #include <memalign.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 17 | #include <part.h> |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 18 | #include <pci.h> |
| 19 | #include <scsi.h> |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 20 | #include <dm/device-internal.h> |
| 21 | #include <dm/uclass-internal.h> |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 22 | |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 23 | static struct scsi_cmd tempccb; /* temporary scsi command buffer */ |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 24 | |
Marek Vasut | 60b20b4 | 2023-08-07 02:26:12 +0200 | [diff] [blame] | 25 | DEFINE_CACHE_ALIGN_BUFFER(u8, tempbuff, 512); /* temporary data buffer */ |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 26 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 27 | /* almost the maximum amount of the scsi_ext command.. */ |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 28 | #define SCSI_MAX_BLK 0xFFFF |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 29 | #define SCSI_LBA48_READ 0xFFFFFFF |
| 30 | |
Varadarajan Narayanan | dc4dd21 | 2025-04-01 15:39:59 +0530 | [diff] [blame^] | 31 | #define SCSI_UNMAP_PARAM_RESERVED 0 |
| 32 | #define SCSI_UNMAP_PARAM_LEN 22 |
| 33 | #define SCSI_UNMAP_PARAM_DATA_LEN 16 |
| 34 | |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 35 | static void scsi_print_error(struct scsi_cmd *pccb) |
Simon Glass | 1674098 | 2017-06-14 21:28:23 -0600 | [diff] [blame] | 36 | { |
| 37 | /* Dummy function that could print an error for debugging */ |
| 38 | } |
| 39 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 40 | #ifdef CONFIG_SYS_64BIT_LBA |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 41 | void scsi_setup_read16(struct scsi_cmd *pccb, lbaint_t start, |
| 42 | unsigned long blocks) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 43 | { |
| 44 | pccb->cmd[0] = SCSI_READ16; |
Neil Armstrong | 88fa40a | 2025-01-24 03:01:48 +0100 | [diff] [blame] | 45 | pccb->cmd[1] = 0; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 46 | pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff; |
| 47 | pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff; |
| 48 | pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff; |
| 49 | pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff; |
| 50 | pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff; |
| 51 | pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff; |
| 52 | pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff; |
| 53 | pccb->cmd[9] = (unsigned char)start & 0xff; |
| 54 | pccb->cmd[10] = 0; |
| 55 | pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff; |
| 56 | pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff; |
| 57 | pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff; |
| 58 | pccb->cmd[14] = (unsigned char)blocks & 0xff; |
| 59 | pccb->cmd[15] = 0; |
| 60 | pccb->cmdlen = 16; |
| 61 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 62 | debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n", |
| 63 | pccb->cmd[0], pccb->cmd[1], |
| 64 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 65 | pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9], |
| 66 | pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]); |
| 67 | } |
| 68 | #endif |
| 69 | |
Faiz Abbas | 76a6fa6 | 2019-10-15 18:24:32 +0530 | [diff] [blame] | 70 | static void scsi_setup_inquiry(struct scsi_cmd *pccb) |
| 71 | { |
| 72 | pccb->cmd[0] = SCSI_INQUIRY; |
Neil Armstrong | 88fa40a | 2025-01-24 03:01:48 +0100 | [diff] [blame] | 73 | pccb->cmd[1] = 0; |
Faiz Abbas | 76a6fa6 | 2019-10-15 18:24:32 +0530 | [diff] [blame] | 74 | pccb->cmd[2] = 0; |
| 75 | pccb->cmd[3] = 0; |
| 76 | if (pccb->datalen > 255) |
| 77 | pccb->cmd[4] = 255; |
| 78 | else |
| 79 | pccb->cmd[4] = (unsigned char)pccb->datalen; |
| 80 | pccb->cmd[5] = 0; |
| 81 | pccb->cmdlen = 6; |
| 82 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 83 | } |
| 84 | |
Caleb Connolly | 8dd0379 | 2025-03-26 13:24:09 +0100 | [diff] [blame] | 85 | static void scsi_setup_sync_cache(struct scsi_cmd *pccb, lbaint_t start, |
| 86 | unsigned short blocks) |
| 87 | { |
| 88 | pccb->cmd[0] = SCSI_SYNC_CACHE; |
| 89 | pccb->cmd[1] = 0; |
| 90 | pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff; |
| 91 | pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff; |
| 92 | pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff; |
| 93 | pccb->cmd[5] = (unsigned char)start & 0xff; |
| 94 | pccb->cmd[6] = 0; |
| 95 | pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff; |
| 96 | pccb->cmd[8] = (unsigned char)blocks & 0xff; |
| 97 | pccb->cmd[9] = 0; |
| 98 | pccb->cmdlen = 10; |
| 99 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 100 | } |
| 101 | |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 102 | static void scsi_setup_read_ext(struct scsi_cmd *pccb, lbaint_t start, |
Michal Simek | 9ec32f3 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 103 | unsigned short blocks) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 104 | { |
| 105 | pccb->cmd[0] = SCSI_READ10; |
Neil Armstrong | 88fa40a | 2025-01-24 03:01:48 +0100 | [diff] [blame] | 106 | pccb->cmd[1] = 0; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 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; |
Caleb Connolly | b47af8e | 2025-03-26 13:24:08 +0100 | [diff] [blame] | 114 | pccb->cmd[9] = 0; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 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 Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 123 | static void scsi_setup_write_ext(struct scsi_cmd *pccb, lbaint_t start, |
Michal Simek | 9ec32f3 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 124 | unsigned short blocks) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 125 | { |
| 126 | pccb->cmd[0] = SCSI_WRITE10; |
Neil Armstrong | 88fa40a | 2025-01-24 03:01:48 +0100 | [diff] [blame] | 127 | pccb->cmd[1] = 0; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 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 | |
Varadarajan Narayanan | dc4dd21 | 2025-04-01 15:39:59 +0530 | [diff] [blame^] | 145 | static void scsi_setup_erase_ext(struct scsi_cmd *pccb, lbaint_t start, |
| 146 | unsigned short blocks) |
| 147 | { |
| 148 | u8 *param = tempbuff; |
| 149 | const u8 param_size = 24; |
| 150 | |
| 151 | memset(param, 0, param_size); |
| 152 | param[0] = SCSI_UNMAP_PARAM_RESERVED; |
| 153 | param[1] = SCSI_UNMAP_PARAM_LEN; |
| 154 | param[2] = SCSI_UNMAP_PARAM_RESERVED; |
| 155 | param[3] = SCSI_UNMAP_PARAM_DATA_LEN; |
| 156 | |
| 157 | param[8] = 0x0; |
| 158 | param[9] = 0x0; |
| 159 | param[10] = 0x0; |
| 160 | param[11] = 0x0; |
| 161 | param[12] = (start >> 24) & 0xff; |
| 162 | param[13] = (start >> 16) & 0xff; |
| 163 | param[14] = (start >> 8) & 0xff; |
| 164 | param[15] = (start) & 0xff; |
| 165 | param[16] = (blocks >> 24) & 0xff; |
| 166 | param[17] = (blocks >> 16) & 0xff; |
| 167 | param[18] = (blocks >> 8) & 0xff; |
| 168 | param[19] = (blocks) & 0xff; |
| 169 | |
| 170 | memset(pccb->cmd, 0, sizeof(pccb->cmd)); |
| 171 | pccb->cmd[0] = SCSI_UNMAP; |
| 172 | pccb->cmd[1] = 0; |
| 173 | pccb->cmd[6] = 0; |
| 174 | pccb->cmd[7] = 0; |
| 175 | pccb->cmd[8] = param_size; |
| 176 | pccb->cmd[9] = 0; |
| 177 | pccb->cmdlen = 10; |
| 178 | |
| 179 | pccb->pdata = param; |
| 180 | pccb->datalen = param_size; |
| 181 | pccb->dma_dir = DMA_TO_DEVICE; |
| 182 | |
| 183 | debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
| 184 | __func__, |
| 185 | pccb->cmd[0], pccb->cmd[1], |
| 186 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 187 | pccb->cmd[7], pccb->cmd[8]); |
| 188 | } |
| 189 | |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 190 | static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 191 | void *buffer) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 192 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 193 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 194 | struct udevice *bdev = dev->parent; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 195 | struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev); |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 196 | lbaint_t start, blks, max_blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 197 | uintptr_t buf_addr; |
| 198 | unsigned short smallblks = 0; |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 199 | struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 200 | |
| 201 | /* Setup device */ |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 202 | pccb->target = block_dev->target; |
| 203 | pccb->lun = block_dev->lun; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 204 | buf_addr = (unsigned long)buffer; |
| 205 | start = blknr; |
| 206 | blks = blkcnt; |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 207 | if (uc_plat->max_bytes_per_req) |
| 208 | max_blks = uc_plat->max_bytes_per_req / block_dev->blksz; |
| 209 | else |
| 210 | max_blks = SCSI_MAX_BLK; |
| 211 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 212 | debug("\nscsi_read: dev %d startblk " LBAF |
| 213 | ", blccnt " LBAF " buffer %lx\n", |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 214 | block_dev->devnum, start, blks, (unsigned long)buffer); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 215 | do { |
| 216 | pccb->pdata = (unsigned char *)buf_addr; |
Faiz Abbas | 2094809 | 2019-10-15 18:24:35 +0530 | [diff] [blame] | 217 | pccb->dma_dir = DMA_FROM_DEVICE; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 218 | #ifdef CONFIG_SYS_64BIT_LBA |
| 219 | if (start > SCSI_LBA48_READ) { |
| 220 | unsigned long blocks; |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 221 | blocks = min_t(lbaint_t, blks, max_blks); |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 222 | pccb->datalen = block_dev->blksz * blocks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 223 | scsi_setup_read16(pccb, start, blocks); |
| 224 | start += blocks; |
| 225 | blks -= blocks; |
| 226 | } else |
| 227 | #endif |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 228 | if (blks > max_blks) { |
| 229 | pccb->datalen = block_dev->blksz * max_blks; |
| 230 | smallblks = max_blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 231 | scsi_setup_read_ext(pccb, start, smallblks); |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 232 | start += max_blks; |
| 233 | blks -= max_blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 234 | } else { |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 235 | pccb->datalen = block_dev->blksz * blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 236 | smallblks = (unsigned short)blks; |
| 237 | scsi_setup_read_ext(pccb, start, smallblks); |
| 238 | start += blks; |
| 239 | blks = 0; |
| 240 | } |
| 241 | debug("scsi_read_ext: startblk " LBAF |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 242 | ", blccnt %x buffer %lX\n", |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 243 | start, smallblks, buf_addr); |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 244 | if (scsi_exec(bdev, pccb)) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 245 | scsi_print_error(pccb); |
| 246 | blkcnt -= blks; |
| 247 | break; |
| 248 | } |
| 249 | buf_addr += pccb->datalen; |
| 250 | } while (blks != 0); |
| 251 | debug("scsi_read_ext: end startblk " LBAF |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 252 | ", blccnt %x buffer %lX\n", start, smallblks, buf_addr); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 253 | return blkcnt; |
| 254 | } |
| 255 | |
| 256 | /******************************************************************************* |
| 257 | * scsi_write |
| 258 | */ |
| 259 | |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 260 | static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 261 | const void *buffer) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 262 | { |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 263 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 264 | struct udevice *bdev = dev->parent; |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 265 | struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev); |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 266 | lbaint_t start, blks, max_blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 267 | uintptr_t buf_addr; |
| 268 | unsigned short smallblks; |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 269 | struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 270 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 271 | /* Setup device */ |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 272 | pccb->target = block_dev->target; |
| 273 | pccb->lun = block_dev->lun; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 274 | buf_addr = (unsigned long)buffer; |
| 275 | start = blknr; |
| 276 | blks = blkcnt; |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 277 | if (uc_plat->max_bytes_per_req) |
| 278 | max_blks = uc_plat->max_bytes_per_req / block_dev->blksz; |
| 279 | else |
| 280 | max_blks = SCSI_MAX_BLK; |
| 281 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 282 | debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 283 | __func__, block_dev->devnum, start, blks, (unsigned long)buffer); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 284 | do { |
| 285 | pccb->pdata = (unsigned char *)buf_addr; |
Faiz Abbas | 2094809 | 2019-10-15 18:24:35 +0530 | [diff] [blame] | 286 | pccb->dma_dir = DMA_TO_DEVICE; |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 287 | if (blks > max_blks) { |
| 288 | pccb->datalen = block_dev->blksz * max_blks; |
| 289 | smallblks = max_blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 290 | scsi_setup_write_ext(pccb, start, smallblks); |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 291 | start += max_blks; |
| 292 | blks -= max_blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 293 | } else { |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 294 | pccb->datalen = block_dev->blksz * blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 295 | smallblks = (unsigned short)blks; |
| 296 | scsi_setup_write_ext(pccb, start, smallblks); |
| 297 | start += blks; |
| 298 | blks = 0; |
| 299 | } |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 300 | debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n", |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 301 | __func__, start, smallblks, buf_addr); |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 302 | if (scsi_exec(bdev, pccb)) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 303 | scsi_print_error(pccb); |
| 304 | blkcnt -= blks; |
| 305 | break; |
| 306 | } |
| 307 | buf_addr += pccb->datalen; |
| 308 | } while (blks != 0); |
Caleb Connolly | 8dd0379 | 2025-03-26 13:24:09 +0100 | [diff] [blame] | 309 | |
| 310 | /* Flush the SCSI cache so we don't lose data on board reset. */ |
| 311 | scsi_setup_sync_cache(pccb, 0, 0); |
| 312 | if (scsi_exec(bdev, pccb)) |
| 313 | scsi_print_error(pccb); |
| 314 | |
Masahiro Yamada | c7570a3 | 2018-08-06 20:47:40 +0900 | [diff] [blame] | 315 | debug("%s: end startblk " LBAF ", blccnt %x buffer %lX\n", |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 316 | __func__, start, smallblks, buf_addr); |
| 317 | return blkcnt; |
| 318 | } |
Marek Vasut | b980e21 | 2023-08-14 01:50:00 +0200 | [diff] [blame] | 319 | |
Varadarajan Narayanan | dc4dd21 | 2025-04-01 15:39:59 +0530 | [diff] [blame^] | 320 | /******************************************************************************* |
| 321 | * scsi_erase |
| 322 | */ |
| 323 | static ulong scsi_erase(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt) |
| 324 | { |
| 325 | struct blk_desc *block_dev = dev_get_uclass_plat(dev); |
| 326 | struct udevice *bdev = dev->parent; |
| 327 | struct scsi_plat *uc_plat = dev_get_uclass_plat(bdev); |
| 328 | lbaint_t start, blks, max_blks; |
| 329 | struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb; |
| 330 | |
| 331 | /* Setup device */ |
| 332 | pccb->target = block_dev->target; |
| 333 | pccb->lun = block_dev->lun; |
| 334 | start = blknr; |
| 335 | blks = blkcnt; |
| 336 | if (uc_plat->max_bytes_per_req) |
| 337 | max_blks = uc_plat->max_bytes_per_req / block_dev->blksz; |
| 338 | else |
| 339 | max_blks = SCSI_MAX_BLK; |
| 340 | |
| 341 | debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF "\n", |
| 342 | __func__, block_dev->devnum, start, blks); |
| 343 | do { |
| 344 | if (blks > max_blks) { |
| 345 | scsi_setup_erase_ext(pccb, start, max_blks); |
| 346 | start += max_blks; |
| 347 | blks -= max_blks; |
| 348 | } else { |
| 349 | scsi_setup_erase_ext(pccb, start, blks); |
| 350 | start += blks; |
| 351 | blks = 0; |
| 352 | } |
| 353 | if (scsi_exec(bdev, pccb)) { |
| 354 | scsi_print_error(pccb); |
| 355 | blkcnt -= blks; |
| 356 | break; |
| 357 | } |
| 358 | } while (blks != 0); |
| 359 | return blkcnt; |
| 360 | } |
| 361 | |
Marek Vasut | b980e21 | 2023-08-14 01:50:00 +0200 | [diff] [blame] | 362 | #if IS_ENABLED(CONFIG_BOUNCE_BUFFER) |
| 363 | static int scsi_buffer_aligned(struct udevice *dev, struct bounce_buffer *state) |
| 364 | { |
| 365 | struct scsi_ops *ops = scsi_get_ops(dev->parent); |
| 366 | |
| 367 | if (ops->buffer_aligned) |
| 368 | return ops->buffer_aligned(dev->parent, state); |
| 369 | |
| 370 | return 1; |
| 371 | } |
| 372 | #endif /* CONFIG_BOUNCE_BUFFER */ |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 373 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 374 | /* copy src to dest, skipping leading and trailing blanks |
| 375 | * and null terminate the string |
| 376 | */ |
Michal Simek | 9ec32f3 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 377 | static void scsi_ident_cpy(unsigned char *dest, unsigned char *src, |
| 378 | unsigned int len) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 379 | { |
| 380 | int start, end; |
| 381 | |
| 382 | start = 0; |
| 383 | while (start < len) { |
| 384 | if (src[start] != ' ') |
| 385 | break; |
| 386 | start++; |
| 387 | } |
| 388 | end = len-1; |
| 389 | while (end > start) { |
| 390 | if (src[end] != ' ') |
| 391 | break; |
| 392 | end--; |
| 393 | } |
| 394 | for (; start <= end; start++) |
| 395 | *dest ++= src[start]; |
| 396 | *dest = '\0'; |
| 397 | } |
| 398 | |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 399 | static int scsi_read_capacity(struct udevice *dev, struct scsi_cmd *pccb, |
| 400 | lbaint_t *capacity, unsigned long *blksz) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 401 | { |
| 402 | *capacity = 0; |
| 403 | |
| 404 | memset(pccb->cmd, '\0', sizeof(pccb->cmd)); |
| 405 | pccb->cmd[0] = SCSI_RD_CAPAC10; |
Neil Armstrong | 88fa40a | 2025-01-24 03:01:48 +0100 | [diff] [blame] | 406 | pccb->cmd[1] = 0; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 407 | pccb->cmdlen = 10; |
Nikita Yushchenko | ef793ee | 2023-11-13 11:51:02 +0600 | [diff] [blame] | 408 | pccb->dma_dir = DMA_FROM_DEVICE; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 409 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 410 | |
| 411 | pccb->datalen = 8; |
Simon Glass | a140e86 | 2017-06-14 21:28:44 -0600 | [diff] [blame] | 412 | if (scsi_exec(dev, pccb)) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 413 | return 1; |
| 414 | |
| 415 | *capacity = ((lbaint_t)pccb->pdata[0] << 24) | |
| 416 | ((lbaint_t)pccb->pdata[1] << 16) | |
| 417 | ((lbaint_t)pccb->pdata[2] << 8) | |
| 418 | ((lbaint_t)pccb->pdata[3]); |
| 419 | |
| 420 | if (*capacity != 0xffffffff) { |
| 421 | /* Read capacity (10) was sufficient for this drive. */ |
| 422 | *blksz = ((unsigned long)pccb->pdata[4] << 24) | |
| 423 | ((unsigned long)pccb->pdata[5] << 16) | |
| 424 | ((unsigned long)pccb->pdata[6] << 8) | |
| 425 | ((unsigned long)pccb->pdata[7]); |
Julius Lehmann | 18afb60 | 2024-10-26 20:06:44 +0200 | [diff] [blame] | 426 | *capacity += 1; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* Read capacity (10) was insufficient. Use read capacity (16). */ |
| 431 | memset(pccb->cmd, '\0', sizeof(pccb->cmd)); |
| 432 | pccb->cmd[0] = SCSI_RD_CAPAC16; |
| 433 | pccb->cmd[1] = 0x10; |
| 434 | pccb->cmdlen = 16; |
| 435 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 436 | |
| 437 | pccb->datalen = 16; |
Faiz Abbas | 2094809 | 2019-10-15 18:24:35 +0530 | [diff] [blame] | 438 | pccb->dma_dir = DMA_FROM_DEVICE; |
Simon Glass | a140e86 | 2017-06-14 21:28:44 -0600 | [diff] [blame] | 439 | if (scsi_exec(dev, pccb)) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 440 | return 1; |
| 441 | |
| 442 | *capacity = ((uint64_t)pccb->pdata[0] << 56) | |
| 443 | ((uint64_t)pccb->pdata[1] << 48) | |
| 444 | ((uint64_t)pccb->pdata[2] << 40) | |
| 445 | ((uint64_t)pccb->pdata[3] << 32) | |
| 446 | ((uint64_t)pccb->pdata[4] << 24) | |
| 447 | ((uint64_t)pccb->pdata[5] << 16) | |
| 448 | ((uint64_t)pccb->pdata[6] << 8) | |
| 449 | ((uint64_t)pccb->pdata[7]); |
Julius Lehmann | 18afb60 | 2024-10-26 20:06:44 +0200 | [diff] [blame] | 450 | *capacity += 1; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 451 | |
| 452 | *blksz = ((uint64_t)pccb->pdata[8] << 56) | |
| 453 | ((uint64_t)pccb->pdata[9] << 48) | |
| 454 | ((uint64_t)pccb->pdata[10] << 40) | |
| 455 | ((uint64_t)pccb->pdata[11] << 32) | |
| 456 | ((uint64_t)pccb->pdata[12] << 24) | |
| 457 | ((uint64_t)pccb->pdata[13] << 16) | |
| 458 | ((uint64_t)pccb->pdata[14] << 8) | |
| 459 | ((uint64_t)pccb->pdata[15]); |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 464 | /* |
| 465 | * Some setup (fill-in) routines |
| 466 | */ |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 467 | static void scsi_setup_test_unit_ready(struct scsi_cmd *pccb) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 468 | { |
| 469 | pccb->cmd[0] = SCSI_TST_U_RDY; |
Neil Armstrong | 88fa40a | 2025-01-24 03:01:48 +0100 | [diff] [blame] | 470 | pccb->cmd[1] = 0; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 471 | pccb->cmd[2] = 0; |
| 472 | pccb->cmd[3] = 0; |
| 473 | pccb->cmd[4] = 0; |
| 474 | pccb->cmd[5] = 0; |
| 475 | pccb->cmdlen = 6; |
| 476 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 477 | } |
| 478 | |
Michal Simek | 6241b52 | 2016-11-30 12:50:58 +0100 | [diff] [blame] | 479 | /** |
| 480 | * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties |
| 481 | * |
| 482 | * @dev_desc: Block device description pointer |
| 483 | */ |
| 484 | static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc) |
Michal Simek | ac0a18d1 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 485 | { |
Tom Rini | 0154cfe | 2023-11-08 14:28:11 -0500 | [diff] [blame] | 486 | memset(dev_desc, 0, sizeof(struct blk_desc)); |
Michal Simek | ac0a18d1 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 487 | dev_desc->target = 0xff; |
| 488 | dev_desc->lun = 0xff; |
Michal Simek | ac0a18d1 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 489 | dev_desc->log2blksz = |
| 490 | LOG2_INVALID(typeof(dev_desc->log2blksz)); |
| 491 | dev_desc->type = DEV_TYPE_UNKNOWN; |
Johan Jonker | 10a986c | 2023-10-18 16:01:10 +0200 | [diff] [blame] | 492 | #if IS_ENABLED(CONFIG_BOUNCE_BUFFER) |
| 493 | dev_desc->bb = true; |
| 494 | #endif /* CONFIG_BOUNCE_BUFFER */ |
Michal Simek | ac0a18d1 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 495 | } |
| 496 | |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 497 | /** |
| 498 | * scsi_detect_dev - Detect scsi device |
| 499 | * |
Michal Simek | 4091749 | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 500 | * @target: target id |
Jean-Jacques Hiblot | 0c447f0 | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 501 | * @lun: target lun |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 502 | * @dev_desc: block device description |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 503 | * |
| 504 | * The scsi_detect_dev detects and fills a dev_desc structure when the device is |
Jean-Jacques Hiblot | 0c447f0 | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 505 | * detected. |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 506 | * |
| 507 | * Return: 0 on success, error value otherwise |
| 508 | */ |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 509 | static int scsi_detect_dev(struct udevice *dev, int target, int lun, |
| 510 | struct blk_desc *dev_desc) |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 511 | { |
| 512 | unsigned char perq, modi; |
| 513 | lbaint_t capacity; |
| 514 | unsigned long blksz; |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 515 | struct scsi_cmd *pccb = (struct scsi_cmd *)&tempccb; |
Faiz Abbas | 3645a48 | 2019-10-15 18:24:34 +0530 | [diff] [blame] | 516 | int count, err; |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 517 | |
Michal Simek | 4091749 | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 518 | pccb->target = target; |
Jean-Jacques Hiblot | 0c447f0 | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 519 | pccb->lun = lun; |
Marek Vasut | 60b20b4 | 2023-08-07 02:26:12 +0200 | [diff] [blame] | 520 | pccb->pdata = tempbuff; |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 521 | pccb->datalen = 512; |
Faiz Abbas | 2094809 | 2019-10-15 18:24:35 +0530 | [diff] [blame] | 522 | pccb->dma_dir = DMA_FROM_DEVICE; |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 523 | scsi_setup_inquiry(pccb); |
Simon Glass | a140e86 | 2017-06-14 21:28:44 -0600 | [diff] [blame] | 524 | if (scsi_exec(dev, pccb)) { |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 525 | if (pccb->contr_stat == SCSI_SEL_TIME_OUT) { |
| 526 | /* |
| 527 | * selection timeout => assuming no |
| 528 | * device present |
| 529 | */ |
| 530 | debug("Selection timeout ID %d\n", |
| 531 | pccb->target); |
| 532 | return -ETIMEDOUT; |
| 533 | } |
| 534 | scsi_print_error(pccb); |
| 535 | return -ENODEV; |
| 536 | } |
| 537 | perq = tempbuff[0]; |
| 538 | modi = tempbuff[1]; |
| 539 | if ((perq & 0x1f) == 0x1f) |
| 540 | return -ENODEV; /* skip unknown devices */ |
| 541 | if ((modi & 0x80) == 0x80) /* drive is removable */ |
| 542 | dev_desc->removable = true; |
| 543 | /* get info for this device */ |
| 544 | scsi_ident_cpy((unsigned char *)dev_desc->vendor, |
| 545 | &tempbuff[8], 8); |
| 546 | scsi_ident_cpy((unsigned char *)dev_desc->product, |
| 547 | &tempbuff[16], 16); |
| 548 | scsi_ident_cpy((unsigned char *)dev_desc->revision, |
| 549 | &tempbuff[32], 4); |
| 550 | dev_desc->target = pccb->target; |
| 551 | dev_desc->lun = pccb->lun; |
| 552 | |
Faiz Abbas | 3645a48 | 2019-10-15 18:24:34 +0530 | [diff] [blame] | 553 | for (count = 0; count < 3; count++) { |
| 554 | pccb->datalen = 0; |
Nikita Yushchenko | ef793ee | 2023-11-13 11:51:02 +0600 | [diff] [blame] | 555 | pccb->dma_dir = DMA_NONE; |
Faiz Abbas | 3645a48 | 2019-10-15 18:24:34 +0530 | [diff] [blame] | 556 | scsi_setup_test_unit_ready(pccb); |
| 557 | err = scsi_exec(dev, pccb); |
| 558 | if (!err) |
| 559 | break; |
| 560 | } |
| 561 | if (err) { |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 562 | if (dev_desc->removable) { |
| 563 | dev_desc->type = perq; |
| 564 | goto removable; |
| 565 | } |
| 566 | scsi_print_error(pccb); |
| 567 | return -EINVAL; |
| 568 | } |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 569 | if (scsi_read_capacity(dev, pccb, &capacity, &blksz)) { |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 570 | scsi_print_error(pccb); |
| 571 | return -EINVAL; |
| 572 | } |
| 573 | dev_desc->lba = capacity; |
| 574 | dev_desc->blksz = blksz; |
| 575 | dev_desc->log2blksz = LOG2(dev_desc->blksz); |
| 576 | dev_desc->type = perq; |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 577 | removable: |
| 578 | return 0; |
| 579 | } |
| 580 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 581 | /* |
| 582 | * (re)-scan the scsi bus and reports scsi device info |
| 583 | * to the user if mode = 1 |
| 584 | */ |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 585 | static int do_scsi_scan_one(struct udevice *dev, int id, int lun, bool verbose) |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 586 | { |
| 587 | int ret; |
| 588 | struct udevice *bdev; |
| 589 | struct blk_desc bd; |
| 590 | struct blk_desc *bdesc; |
Simon Glass | 1af3eed | 2023-01-17 10:47:43 -0700 | [diff] [blame] | 591 | char str[10], *name; |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * detect the scsi driver to get information about its geometry (block |
| 595 | * size, number of blocks) and other parameters (ids, type, ...) |
| 596 | */ |
| 597 | scsi_init_dev_desc_priv(&bd); |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 598 | if (scsi_detect_dev(dev, id, lun, &bd)) |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 599 | return -ENODEV; |
| 600 | |
| 601 | /* |
| 602 | * Create only one block device and do detection |
| 603 | * to make sure that there won't be a lot of |
| 604 | * block devices created |
| 605 | */ |
| 606 | snprintf(str, sizeof(str), "id%dlun%d", id, lun); |
Simon Glass | 1af3eed | 2023-01-17 10:47:43 -0700 | [diff] [blame] | 607 | name = strdup(str); |
| 608 | if (!name) |
| 609 | return log_msg_ret("nam", -ENOMEM); |
| 610 | ret = blk_create_devicef(dev, "scsi_blk", name, UCLASS_SCSI, -1, |
Simon Glass | dbfa32c | 2022-08-11 19:34:59 -0600 | [diff] [blame] | 611 | bd.blksz, bd.lba, &bdev); |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 612 | if (ret) { |
| 613 | debug("Can't create device\n"); |
| 614 | return ret; |
| 615 | } |
Simon Glass | 1af3eed | 2023-01-17 10:47:43 -0700 | [diff] [blame] | 616 | device_set_name_alloced(bdev); |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 617 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 618 | bdesc = dev_get_uclass_plat(bdev); |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 619 | bdesc->target = id; |
| 620 | bdesc->lun = lun; |
| 621 | bdesc->removable = bd.removable; |
| 622 | bdesc->type = bd.type; |
Johan Jonker | 10a986c | 2023-10-18 16:01:10 +0200 | [diff] [blame] | 623 | bdesc->bb = bd.bb; |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 624 | memcpy(&bdesc->vendor, &bd.vendor, sizeof(bd.vendor)); |
| 625 | memcpy(&bdesc->product, &bd.product, sizeof(bd.product)); |
| 626 | memcpy(&bdesc->revision, &bd.revision, sizeof(bd.revision)); |
Stefan Roese | c52ee3e | 2021-04-07 09:12:36 +0200 | [diff] [blame] | 627 | if (IS_ENABLED(CONFIG_SYS_BIG_ENDIAN)) { |
| 628 | ata_swap_buf_le16((u16 *)&bdesc->vendor, sizeof(bd.vendor) / 2); |
| 629 | ata_swap_buf_le16((u16 *)&bdesc->product, sizeof(bd.product) / 2); |
| 630 | ata_swap_buf_le16((u16 *)&bdesc->revision, sizeof(bd.revision) / 2); |
| 631 | } |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 632 | |
AKASHI Takahiro | 368c3d5 | 2022-03-08 20:36:39 +0900 | [diff] [blame] | 633 | ret = blk_probe_or_unbind(bdev); |
| 634 | if (ret < 0) |
| 635 | /* TODO: undo create */ |
Simon Glass | 9985e9c | 2023-01-17 10:47:45 -0700 | [diff] [blame] | 636 | return log_msg_ret("pro", ret); |
| 637 | |
Simon Glass | b1d581d | 2023-07-30 11:15:14 -0600 | [diff] [blame] | 638 | ret = bootdev_setup_for_sibling_blk(bdev, "scsi_bootdev"); |
Simon Glass | 9985e9c | 2023-01-17 10:47:45 -0700 | [diff] [blame] | 639 | if (ret) |
| 640 | return log_msg_ret("bd", ret); |
AKASHI Takahiro | 368c3d5 | 2022-03-08 20:36:39 +0900 | [diff] [blame] | 641 | |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 642 | if (verbose) { |
Heinrich Schuchardt | c5382b7 | 2019-02-05 18:06:24 +0100 | [diff] [blame] | 643 | printf(" Device %d: ", bdesc->devnum); |
Jean-Jacques Hiblot | cdf96eb | 2017-04-24 11:51:26 +0200 | [diff] [blame] | 644 | dev_print(bdesc); |
| 645 | } |
| 646 | return 0; |
| 647 | } |
| 648 | |
Simon Glass | 600d001 | 2017-06-14 21:28:45 -0600 | [diff] [blame] | 649 | int scsi_scan_dev(struct udevice *dev, bool verbose) |
| 650 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 651 | struct scsi_plat *uc_plat; /* scsi controller plat */ |
Simon Glass | 600d001 | 2017-06-14 21:28:45 -0600 | [diff] [blame] | 652 | int ret; |
| 653 | int i; |
| 654 | int lun; |
| 655 | |
| 656 | /* probe SCSI controller driver */ |
| 657 | ret = device_probe(dev); |
| 658 | if (ret) |
| 659 | return ret; |
| 660 | |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 661 | /* Get controller plat */ |
| 662 | uc_plat = dev_get_uclass_plat(dev); |
Simon Glass | 600d001 | 2017-06-14 21:28:45 -0600 | [diff] [blame] | 663 | |
| 664 | for (i = 0; i < uc_plat->max_id; i++) |
| 665 | for (lun = 0; lun < uc_plat->max_lun; lun++) |
| 666 | do_scsi_scan_one(dev, i, lun, verbose); |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 671 | int scsi_scan(bool verbose) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 672 | { |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 673 | struct uclass *uc; |
| 674 | struct udevice *dev; /* SCSI controller */ |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 675 | int ret; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 676 | |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 677 | if (verbose) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 678 | printf("scanning bus for devices...\n"); |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 679 | |
| 680 | ret = uclass_get(UCLASS_SCSI, &uc); |
| 681 | if (ret) |
| 682 | return ret; |
| 683 | |
Simon Glass | 01ed80a | 2023-01-17 10:47:44 -0700 | [diff] [blame] | 684 | /* remove all children of the SCSI devices */ |
| 685 | uclass_foreach_dev(dev, uc) { |
| 686 | log_debug("unbind %s\n", dev->name); |
| 687 | ret = device_chld_remove(dev, NULL, DM_REMOVE_NORMAL); |
| 688 | if (!ret) |
| 689 | ret = device_chld_unbind(dev, NULL); |
| 690 | if (ret) { |
| 691 | if (verbose) |
| 692 | printf("unable to unbind devices (%dE)\n", ret); |
| 693 | return log_msg_ret("unb", ret); |
| 694 | } |
| 695 | } |
| 696 | |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 697 | uclass_foreach_dev(dev, uc) { |
Simon Glass | 600d001 | 2017-06-14 21:28:45 -0600 | [diff] [blame] | 698 | ret = scsi_scan_dev(dev, verbose); |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 699 | if (ret) |
| 700 | return ret; |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | return 0; |
| 704 | } |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 705 | |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 706 | static const struct blk_ops scsi_blk_ops = { |
| 707 | .read = scsi_read, |
| 708 | .write = scsi_write, |
Varadarajan Narayanan | dc4dd21 | 2025-04-01 15:39:59 +0530 | [diff] [blame^] | 709 | .erase = scsi_erase, |
Marek Vasut | b980e21 | 2023-08-14 01:50:00 +0200 | [diff] [blame] | 710 | #if IS_ENABLED(CONFIG_BOUNCE_BUFFER) |
| 711 | .buffer_aligned = scsi_buffer_aligned, |
| 712 | #endif /* CONFIG_BOUNCE_BUFFER */ |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 713 | }; |
| 714 | |
| 715 | U_BOOT_DRIVER(scsi_blk) = { |
| 716 | .name = "scsi_blk", |
| 717 | .id = UCLASS_BLK, |
| 718 | .ops = &scsi_blk_ops, |
| 719 | }; |