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