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