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