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