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