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