Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter, MPL AG Switzerland |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 10 | #include <inttypes.h> |
| 11 | #include <pci.h> |
| 12 | #include <scsi.h> |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 13 | #include <dm/device-internal.h> |
| 14 | #include <dm/uclass-internal.h> |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 15 | |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 16 | #if !defined(CONFIG_DM_SCSI) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 17 | #ifdef CONFIG_SCSI_DEV_LIST |
| 18 | #define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST |
| 19 | #else |
| 20 | #ifdef CONFIG_SCSI_SYM53C8XX |
| 21 | #define SCSI_VEND_ID 0x1000 |
| 22 | #ifndef CONFIG_SCSI_DEV_ID |
| 23 | #define SCSI_DEV_ID 0x0001 |
| 24 | #else |
| 25 | #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID |
| 26 | #endif |
| 27 | #elif defined CONFIG_SATA_ULI5288 |
| 28 | |
| 29 | #define SCSI_VEND_ID 0x10b9 |
| 30 | #define SCSI_DEV_ID 0x5288 |
| 31 | |
| 32 | #elif !defined(CONFIG_SCSI_AHCI_PLAT) |
| 33 | #error no scsi device defined |
| 34 | #endif |
| 35 | #define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID} |
| 36 | #endif |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 37 | #endif |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 38 | |
| 39 | #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) |
| 40 | const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST }; |
| 41 | #endif |
| 42 | static ccb tempccb; /* temporary scsi command buffer */ |
| 43 | |
| 44 | static unsigned char tempbuff[512]; /* temporary data buffer */ |
| 45 | |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 46 | #if !defined(CONFIG_DM_SCSI) |
Simon Glass | c8e89ab | 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 | |
| 51 | static struct blk_desc scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE]; |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 52 | #endif |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 53 | |
| 54 | /* almost the maximum amount of the scsi_ext command.. */ |
| 55 | #define SCSI_MAX_READ_BLK 0xFFFF |
| 56 | #define SCSI_LBA48_READ 0xFFFFFFF |
| 57 | |
| 58 | #ifdef CONFIG_SYS_64BIT_LBA |
| 59 | void scsi_setup_read16(ccb *pccb, lbaint_t start, unsigned long blocks) |
| 60 | { |
| 61 | pccb->cmd[0] = SCSI_READ16; |
| 62 | pccb->cmd[1] = pccb->lun << 5; |
| 63 | pccb->cmd[2] = (unsigned char)(start >> 56) & 0xff; |
| 64 | pccb->cmd[3] = (unsigned char)(start >> 48) & 0xff; |
| 65 | pccb->cmd[4] = (unsigned char)(start >> 40) & 0xff; |
| 66 | pccb->cmd[5] = (unsigned char)(start >> 32) & 0xff; |
| 67 | pccb->cmd[6] = (unsigned char)(start >> 24) & 0xff; |
| 68 | pccb->cmd[7] = (unsigned char)(start >> 16) & 0xff; |
| 69 | pccb->cmd[8] = (unsigned char)(start >> 8) & 0xff; |
| 70 | pccb->cmd[9] = (unsigned char)start & 0xff; |
| 71 | pccb->cmd[10] = 0; |
| 72 | pccb->cmd[11] = (unsigned char)(blocks >> 24) & 0xff; |
| 73 | pccb->cmd[12] = (unsigned char)(blocks >> 16) & 0xff; |
| 74 | pccb->cmd[13] = (unsigned char)(blocks >> 8) & 0xff; |
| 75 | pccb->cmd[14] = (unsigned char)blocks & 0xff; |
| 76 | pccb->cmd[15] = 0; |
| 77 | pccb->cmdlen = 16; |
| 78 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 79 | debug("scsi_setup_read16: cmd: %02X %02X startblk %02X%02X%02X%02X%02X%02X%02X%02X blccnt %02X%02X%02X%02X\n", |
| 80 | pccb->cmd[0], pccb->cmd[1], |
| 81 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 82 | pccb->cmd[6], pccb->cmd[7], pccb->cmd[8], pccb->cmd[9], |
| 83 | pccb->cmd[11], pccb->cmd[12], pccb->cmd[13], pccb->cmd[14]); |
| 84 | } |
| 85 | #endif |
| 86 | |
Michal Simek | 9ec32f3 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 87 | static void scsi_setup_read_ext(ccb *pccb, lbaint_t start, |
| 88 | unsigned short blocks) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 89 | { |
| 90 | pccb->cmd[0] = SCSI_READ10; |
| 91 | pccb->cmd[1] = pccb->lun << 5; |
| 92 | pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff; |
| 93 | pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff; |
| 94 | pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff; |
| 95 | pccb->cmd[5] = (unsigned char)start & 0xff; |
| 96 | pccb->cmd[6] = 0; |
| 97 | pccb->cmd[7] = (unsigned char)(blocks >> 8) & 0xff; |
| 98 | pccb->cmd[8] = (unsigned char)blocks & 0xff; |
| 99 | pccb->cmd[6] = 0; |
| 100 | pccb->cmdlen = 10; |
| 101 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 102 | debug("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
| 103 | pccb->cmd[0], pccb->cmd[1], |
| 104 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 105 | pccb->cmd[7], pccb->cmd[8]); |
| 106 | } |
| 107 | |
Michal Simek | 9ec32f3 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 108 | static void scsi_setup_write_ext(ccb *pccb, lbaint_t start, |
| 109 | unsigned short blocks) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 110 | { |
| 111 | pccb->cmd[0] = SCSI_WRITE10; |
| 112 | pccb->cmd[1] = pccb->lun << 5; |
| 113 | pccb->cmd[2] = (unsigned char)(start >> 24) & 0xff; |
| 114 | pccb->cmd[3] = (unsigned char)(start >> 16) & 0xff; |
| 115 | pccb->cmd[4] = (unsigned char)(start >> 8) & 0xff; |
| 116 | pccb->cmd[5] = (unsigned char)start & 0xff; |
| 117 | pccb->cmd[6] = 0; |
| 118 | pccb->cmd[7] = ((unsigned char)(blocks >> 8)) & 0xff; |
| 119 | pccb->cmd[8] = (unsigned char)blocks & 0xff; |
| 120 | pccb->cmd[9] = 0; |
| 121 | pccb->cmdlen = 10; |
| 122 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 123 | debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
| 124 | __func__, |
| 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 | |
Michal Simek | 9ec32f3 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 130 | static void scsi_setup_inquiry(ccb *pccb) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 131 | { |
| 132 | pccb->cmd[0] = SCSI_INQUIRY; |
| 133 | pccb->cmd[1] = pccb->lun << 5; |
| 134 | pccb->cmd[2] = 0; |
| 135 | pccb->cmd[3] = 0; |
| 136 | if (pccb->datalen > 255) |
| 137 | pccb->cmd[4] = 255; |
| 138 | else |
| 139 | pccb->cmd[4] = (unsigned char)pccb->datalen; |
| 140 | pccb->cmd[5] = 0; |
| 141 | pccb->cmdlen = 6; |
| 142 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 143 | } |
| 144 | |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 145 | #ifdef CONFIG_BLK |
| 146 | static ulong scsi_read(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 147 | void *buffer) |
| 148 | #else |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 149 | static ulong scsi_read(struct blk_desc *block_dev, lbaint_t blknr, |
| 150 | lbaint_t blkcnt, void *buffer) |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 151 | #endif |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 152 | { |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 153 | #ifdef CONFIG_BLK |
| 154 | struct blk_desc *block_dev = dev_get_uclass_platdata(dev); |
| 155 | #endif |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 156 | lbaint_t start, blks; |
| 157 | uintptr_t buf_addr; |
| 158 | unsigned short smallblks = 0; |
| 159 | ccb *pccb = (ccb *)&tempccb; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 160 | |
| 161 | /* Setup device */ |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 162 | pccb->target = block_dev->target; |
| 163 | pccb->lun = block_dev->lun; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 164 | buf_addr = (unsigned long)buffer; |
| 165 | start = blknr; |
| 166 | blks = blkcnt; |
| 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; |
| 175 | blocks = min_t(lbaint_t, blks, SCSI_MAX_READ_BLK); |
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 |
| 182 | if (blks > SCSI_MAX_READ_BLK) { |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 183 | pccb->datalen = block_dev->blksz * |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 184 | SCSI_MAX_READ_BLK; |
| 185 | smallblks = SCSI_MAX_READ_BLK; |
| 186 | scsi_setup_read_ext(pccb, start, smallblks); |
| 187 | start += SCSI_MAX_READ_BLK; |
| 188 | blks -= SCSI_MAX_READ_BLK; |
| 189 | } else { |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 190 | pccb->datalen = block_dev->blksz * blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 191 | smallblks = (unsigned short)blks; |
| 192 | scsi_setup_read_ext(pccb, start, smallblks); |
| 193 | start += blks; |
| 194 | blks = 0; |
| 195 | } |
| 196 | debug("scsi_read_ext: startblk " LBAF |
| 197 | ", blccnt %x buffer %" PRIXPTR "\n", |
| 198 | start, smallblks, buf_addr); |
| 199 | if (scsi_exec(pccb) != true) { |
| 200 | scsi_print_error(pccb); |
| 201 | blkcnt -= blks; |
| 202 | break; |
| 203 | } |
| 204 | buf_addr += pccb->datalen; |
| 205 | } while (blks != 0); |
| 206 | debug("scsi_read_ext: end startblk " LBAF |
| 207 | ", blccnt %x buffer %" PRIXPTR "\n", start, smallblks, buf_addr); |
| 208 | return blkcnt; |
| 209 | } |
| 210 | |
| 211 | /******************************************************************************* |
| 212 | * scsi_write |
| 213 | */ |
| 214 | |
| 215 | /* Almost the maximum amount of the scsi_ext command.. */ |
| 216 | #define SCSI_MAX_WRITE_BLK 0xFFFF |
| 217 | |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 218 | #ifdef CONFIG_BLK |
| 219 | static ulong scsi_write(struct udevice *dev, lbaint_t blknr, lbaint_t blkcnt, |
| 220 | const void *buffer) |
| 221 | #else |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 222 | static ulong scsi_write(struct blk_desc *block_dev, lbaint_t blknr, |
| 223 | lbaint_t blkcnt, const void *buffer) |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 224 | #endif |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 225 | { |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 226 | #ifdef CONFIG_BLK |
| 227 | struct blk_desc *block_dev = dev_get_uclass_platdata(dev); |
| 228 | #endif |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 229 | lbaint_t start, blks; |
| 230 | uintptr_t buf_addr; |
| 231 | unsigned short smallblks; |
| 232 | ccb *pccb = (ccb *)&tempccb; |
| 233 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 234 | /* Setup device */ |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 235 | pccb->target = block_dev->target; |
| 236 | pccb->lun = block_dev->lun; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 237 | buf_addr = (unsigned long)buffer; |
| 238 | start = blknr; |
| 239 | blks = blkcnt; |
| 240 | debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 241 | __func__, block_dev->devnum, start, blks, (unsigned long)buffer); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 242 | do { |
| 243 | pccb->pdata = (unsigned char *)buf_addr; |
| 244 | if (blks > SCSI_MAX_WRITE_BLK) { |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 245 | pccb->datalen = (block_dev->blksz * |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 246 | SCSI_MAX_WRITE_BLK); |
| 247 | smallblks = SCSI_MAX_WRITE_BLK; |
| 248 | scsi_setup_write_ext(pccb, start, smallblks); |
| 249 | start += SCSI_MAX_WRITE_BLK; |
| 250 | blks -= SCSI_MAX_WRITE_BLK; |
| 251 | } else { |
Michal Simek | de6840a | 2016-11-18 16:22:42 +0100 | [diff] [blame] | 252 | pccb->datalen = block_dev->blksz * blks; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 253 | smallblks = (unsigned short)blks; |
| 254 | scsi_setup_write_ext(pccb, start, smallblks); |
| 255 | start += blks; |
| 256 | blks = 0; |
| 257 | } |
| 258 | debug("%s: startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", |
| 259 | __func__, start, smallblks, buf_addr); |
| 260 | if (scsi_exec(pccb) != true) { |
| 261 | scsi_print_error(pccb); |
| 262 | blkcnt -= blks; |
| 263 | break; |
| 264 | } |
| 265 | buf_addr += pccb->datalen; |
| 266 | } while (blks != 0); |
| 267 | debug("%s: end startblk " LBAF ", blccnt %x buffer %" PRIXPTR "\n", |
| 268 | __func__, start, smallblks, buf_addr); |
| 269 | return blkcnt; |
| 270 | } |
| 271 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 272 | #if defined(CONFIG_PCI) && !defined(CONFIG_SCSI_AHCI_PLAT) |
| 273 | void scsi_init(void) |
| 274 | { |
| 275 | int busdevfunc = -1; |
| 276 | int i; |
| 277 | /* |
| 278 | * Find a device from the list, this driver will support a single |
| 279 | * controller. |
| 280 | */ |
| 281 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 282 | /* get PCI Device ID */ |
| 283 | #ifdef CONFIG_DM_PCI |
| 284 | struct udevice *dev; |
| 285 | int ret; |
| 286 | |
| 287 | ret = dm_pci_find_device(scsi_device_list[i].vendor, |
| 288 | scsi_device_list[i].device, 0, &dev); |
| 289 | if (!ret) { |
| 290 | busdevfunc = dm_pci_get_bdf(dev); |
| 291 | break; |
| 292 | } |
| 293 | #else |
| 294 | busdevfunc = pci_find_device(scsi_device_list[i].vendor, |
| 295 | scsi_device_list[i].device, |
| 296 | 0); |
| 297 | #endif |
| 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); |
| 324 | scsi_scan(1); |
| 325 | bootstage_accum(BOOTSTAGE_ID_ACCUM_SCSI); |
| 326 | } |
| 327 | #endif |
| 328 | |
Simon Glass | c8e89ab | 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 | 9ec32f3 | 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 | c8e89ab | 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 | |
Michal Simek | 9ec32f3 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 354 | static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, |
| 355 | unsigned long *blksz) |
Simon Glass | c8e89ab | 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; |
| 366 | if (scsi_exec(pccb) != true) |
| 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; |
| 391 | if (scsi_exec(pccb) != true) |
| 392 | return 1; |
| 393 | |
| 394 | *capacity = ((uint64_t)pccb->pdata[0] << 56) | |
| 395 | ((uint64_t)pccb->pdata[1] << 48) | |
| 396 | ((uint64_t)pccb->pdata[2] << 40) | |
| 397 | ((uint64_t)pccb->pdata[3] << 32) | |
| 398 | ((uint64_t)pccb->pdata[4] << 24) | |
| 399 | ((uint64_t)pccb->pdata[5] << 16) | |
| 400 | ((uint64_t)pccb->pdata[6] << 8) | |
| 401 | ((uint64_t)pccb->pdata[7]); |
| 402 | |
| 403 | *blksz = ((uint64_t)pccb->pdata[8] << 56) | |
| 404 | ((uint64_t)pccb->pdata[9] << 48) | |
| 405 | ((uint64_t)pccb->pdata[10] << 40) | |
| 406 | ((uint64_t)pccb->pdata[11] << 32) | |
| 407 | ((uint64_t)pccb->pdata[12] << 24) | |
| 408 | ((uint64_t)pccb->pdata[13] << 16) | |
| 409 | ((uint64_t)pccb->pdata[14] << 8) | |
| 410 | ((uint64_t)pccb->pdata[15]); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /* |
| 417 | * Some setup (fill-in) routines |
| 418 | */ |
Michal Simek | 9ec32f3 | 2016-11-30 11:53:43 +0100 | [diff] [blame] | 419 | static void scsi_setup_test_unit_ready(ccb *pccb) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 420 | { |
| 421 | pccb->cmd[0] = SCSI_TST_U_RDY; |
| 422 | pccb->cmd[1] = pccb->lun << 5; |
| 423 | pccb->cmd[2] = 0; |
| 424 | pccb->cmd[3] = 0; |
| 425 | pccb->cmd[4] = 0; |
| 426 | pccb->cmd[5] = 0; |
| 427 | pccb->cmdlen = 6; |
| 428 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 429 | } |
| 430 | |
Michal Simek | 6241b52 | 2016-11-30 12:50:58 +0100 | [diff] [blame] | 431 | /** |
| 432 | * scsi_init_dev_desc_priv - initialize only SCSI specific blk_desc properties |
| 433 | * |
| 434 | * @dev_desc: Block device description pointer |
| 435 | */ |
| 436 | static void scsi_init_dev_desc_priv(struct blk_desc *dev_desc) |
Michal Simek | ac0a18d1 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 437 | { |
| 438 | dev_desc->target = 0xff; |
| 439 | dev_desc->lun = 0xff; |
Michal Simek | ac0a18d1 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 440 | dev_desc->log2blksz = |
| 441 | LOG2_INVALID(typeof(dev_desc->log2blksz)); |
| 442 | dev_desc->type = DEV_TYPE_UNKNOWN; |
| 443 | dev_desc->vendor[0] = 0; |
| 444 | dev_desc->product[0] = 0; |
| 445 | dev_desc->revision[0] = 0; |
| 446 | dev_desc->removable = false; |
Michal Simek | ac0a18d1 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 447 | #ifndef CONFIG_BLK |
| 448 | dev_desc->block_read = scsi_read; |
| 449 | dev_desc->block_write = scsi_write; |
| 450 | #endif |
| 451 | } |
| 452 | |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 453 | #if !defined(CONFIG_DM_SCSI) |
Michal Simek | 6241b52 | 2016-11-30 12:50:58 +0100 | [diff] [blame] | 454 | /** |
| 455 | * scsi_init_dev_desc - initialize all SCSI specific blk_desc properties |
| 456 | * |
| 457 | * @dev_desc: Block device description pointer |
| 458 | * @devnum: Device number |
| 459 | */ |
| 460 | static void scsi_init_dev_desc(struct blk_desc *dev_desc, int devnum) |
| 461 | { |
| 462 | dev_desc->lba = 0; |
| 463 | dev_desc->blksz = 0; |
| 464 | dev_desc->if_type = IF_TYPE_SCSI; |
| 465 | dev_desc->devnum = devnum; |
| 466 | dev_desc->part_type = PART_TYPE_UNKNOWN; |
| 467 | |
| 468 | scsi_init_dev_desc_priv(dev_desc); |
| 469 | } |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 470 | #endif |
Michal Simek | 4091749 | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 471 | |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 472 | /** |
| 473 | * scsi_detect_dev - Detect scsi device |
| 474 | * |
Michal Simek | 4091749 | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 475 | * @target: target id |
Jean-Jacques Hiblot | 0c447f0 | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 476 | * @lun: target lun |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 477 | * @dev_desc: block device description |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 478 | * |
| 479 | * 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] | 480 | * detected. |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 481 | * |
| 482 | * Return: 0 on success, error value otherwise |
| 483 | */ |
Jean-Jacques Hiblot | 0c447f0 | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 484 | static int scsi_detect_dev(int target, int lun, struct blk_desc *dev_desc) |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 485 | { |
| 486 | unsigned char perq, modi; |
| 487 | lbaint_t capacity; |
| 488 | unsigned long blksz; |
Michal Simek | 4091749 | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 489 | ccb *pccb = (ccb *)&tempccb; |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 490 | |
Michal Simek | 4091749 | 2016-11-18 16:14:24 +0100 | [diff] [blame] | 491 | pccb->target = target; |
Jean-Jacques Hiblot | 0c447f0 | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 492 | pccb->lun = lun; |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 493 | pccb->pdata = (unsigned char *)&tempbuff; |
| 494 | pccb->datalen = 512; |
| 495 | scsi_setup_inquiry(pccb); |
| 496 | if (scsi_exec(pccb) != true) { |
| 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 | |
| 525 | pccb->datalen = 0; |
| 526 | scsi_setup_test_unit_ready(pccb); |
| 527 | if (scsi_exec(pccb) != true) { |
| 528 | if (dev_desc->removable) { |
| 529 | dev_desc->type = perq; |
| 530 | goto removable; |
| 531 | } |
| 532 | scsi_print_error(pccb); |
| 533 | return -EINVAL; |
| 534 | } |
| 535 | if (scsi_read_capacity(pccb, &capacity, &blksz)) { |
| 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) |
Michal Simek | 5dc196f | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 552 | int scsi_scan(int mode) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 553 | { |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 554 | unsigned char i, lun; |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 555 | struct uclass *uc; |
| 556 | struct udevice *dev; /* SCSI controller */ |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 557 | int ret; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 558 | |
| 559 | if (mode == 1) |
| 560 | printf("scanning bus for devices...\n"); |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 561 | |
Michal Simek | 9c49b8c | 2017-01-02 09:40:09 +0100 | [diff] [blame] | 562 | blk_unbind_all(IF_TYPE_SCSI); |
| 563 | |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 564 | ret = uclass_get(UCLASS_SCSI, &uc); |
| 565 | if (ret) |
| 566 | return ret; |
| 567 | |
| 568 | uclass_foreach_dev(dev, uc) { |
| 569 | struct scsi_platdata *plat; /* scsi controller platdata */ |
| 570 | |
| 571 | /* probe SCSI controller driver */ |
| 572 | ret = device_probe(dev); |
| 573 | if (ret) |
| 574 | return ret; |
| 575 | |
| 576 | /* Get controller platdata */ |
| 577 | plat = dev_get_platdata(dev); |
| 578 | |
| 579 | for (i = 0; i < plat->max_id; i++) { |
| 580 | for (lun = 0; lun < plat->max_lun; lun++) { |
| 581 | struct udevice *bdev; /* block device */ |
| 582 | /* block device description */ |
Jean-Jacques Hiblot | ffde9dde | 2017-04-07 13:42:08 +0200 | [diff] [blame] | 583 | struct blk_desc _bd; |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 584 | struct blk_desc *bdesc; |
| 585 | char str[10]; |
| 586 | |
Jean-Jacques Hiblot | ffde9dde | 2017-04-07 13:42:08 +0200 | [diff] [blame] | 587 | scsi_init_dev_desc_priv(&_bd); |
| 588 | ret = scsi_detect_dev(i, lun, &_bd); |
| 589 | if (ret) |
| 590 | /* |
| 591 | * no device detected? |
| 592 | * check the next lun. |
| 593 | */ |
| 594 | continue; |
| 595 | |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 596 | /* |
| 597 | * Create only one block device and do detection |
| 598 | * to make sure that there won't be a lot of |
| 599 | * block devices created |
| 600 | */ |
| 601 | snprintf(str, sizeof(str), "id%dlun%d", i, lun); |
| 602 | ret = blk_create_devicef(dev, "scsi_blk", |
Jean-Jacques Hiblot | ffde9dde | 2017-04-07 13:42:08 +0200 | [diff] [blame] | 603 | str, IF_TYPE_SCSI, |
| 604 | -1, |
| 605 | _bd.blksz, |
| 606 | _bd.blksz * _bd.lba, |
| 607 | &bdev); |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 608 | if (ret) { |
| 609 | debug("Can't create device\n"); |
| 610 | return ret; |
| 611 | } |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 612 | |
Jean-Jacques Hiblot | ffde9dde | 2017-04-07 13:42:08 +0200 | [diff] [blame] | 613 | bdesc = dev_get_uclass_platdata(bdev); |
| 614 | bdesc->target = i; |
| 615 | bdesc->lun = lun; |
| 616 | bdesc->removable = _bd.removable; |
| 617 | bdesc->type = _bd.type; |
| 618 | memcpy(&bdesc->vendor, &_bd.vendor, |
| 619 | sizeof(_bd.vendor)); |
| 620 | memcpy(&bdesc->product, &_bd.product, |
| 621 | sizeof(_bd.product)); |
| 622 | memcpy(&bdesc->revision, &_bd.revision, |
| 623 | sizeof(_bd.revision)); |
Jean-Jacques Hiblot | 37e721c | 2017-04-07 13:42:07 +0200 | [diff] [blame] | 624 | part_init(bdesc); |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 625 | |
| 626 | if (mode == 1) { |
| 627 | printf(" Device %d: ", 0); |
| 628 | dev_print(bdesc); |
| 629 | } /* if mode */ |
| 630 | } /* next LUN */ |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | return 0; |
| 635 | } |
| 636 | #else |
| 637 | int scsi_scan(int mode) |
| 638 | { |
| 639 | unsigned char i, lun; |
| 640 | int ret; |
| 641 | |
| 642 | if (mode == 1) |
| 643 | printf("scanning bus for devices...\n"); |
Michal Simek | ac0a18d1 | 2016-11-18 15:27:00 +0100 | [diff] [blame] | 644 | for (i = 0; i < CONFIG_SYS_SCSI_MAX_DEVICE; i++) |
| 645 | scsi_init_dev_desc(&scsi_dev_desc[i], i); |
| 646 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 647 | scsi_max_devs = 0; |
| 648 | for (i = 0; i < CONFIG_SYS_SCSI_MAX_SCSI_ID; i++) { |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 649 | for (lun = 0; lun < CONFIG_SYS_SCSI_MAX_LUN; lun++) { |
Jean-Jacques Hiblot | 0c447f0 | 2017-04-07 13:42:06 +0200 | [diff] [blame] | 650 | ret = scsi_detect_dev(i, lun, |
| 651 | &scsi_dev_desc[scsi_max_devs]); |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 652 | if (ret) |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 653 | continue; |
Jean-Jacques Hiblot | 37e721c | 2017-04-07 13:42:07 +0200 | [diff] [blame] | 654 | part_init(&scsi_dev_desc[scsi_max_devs]); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 655 | |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 656 | if (mode == 1) { |
Michal Simek | ebbde65 | 2016-11-18 15:42:13 +0100 | [diff] [blame] | 657 | printf(" Device %d: ", 0); |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 658 | dev_print(&scsi_dev_desc[scsi_max_devs]); |
| 659 | } /* if mode */ |
| 660 | scsi_max_devs++; |
| 661 | } /* next LUN */ |
| 662 | } |
| 663 | if (scsi_max_devs > 0) |
| 664 | scsi_curr_dev = 0; |
| 665 | else |
| 666 | scsi_curr_dev = -1; |
| 667 | |
| 668 | printf("Found %d device(s).\n", scsi_max_devs); |
| 669 | #ifndef CONFIG_SPL_BUILD |
| 670 | setenv_ulong("scsidevs", scsi_max_devs); |
| 671 | #endif |
Michal Simek | 5dc196f | 2016-11-30 12:12:31 +0100 | [diff] [blame] | 672 | return 0; |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 673 | } |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 674 | #endif |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 675 | |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 676 | #ifdef CONFIG_BLK |
| 677 | static const struct blk_ops scsi_blk_ops = { |
| 678 | .read = scsi_read, |
| 679 | .write = scsi_write, |
| 680 | }; |
| 681 | |
| 682 | U_BOOT_DRIVER(scsi_blk) = { |
| 683 | .name = "scsi_blk", |
| 684 | .id = UCLASS_BLK, |
| 685 | .ops = &scsi_blk_ops, |
| 686 | }; |
| 687 | #else |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 688 | U_BOOT_LEGACY_BLK(scsi) = { |
Ed Swarthout | 987a955 | 2016-06-01 08:11:24 -0500 | [diff] [blame] | 689 | .if_typename = "scsi", |
Simon Glass | c8e89ab | 2016-05-01 11:36:09 -0600 | [diff] [blame] | 690 | .if_type = IF_TYPE_SCSI, |
| 691 | .max_devs = CONFIG_SYS_SCSI_MAX_DEVICE, |
| 692 | .desc = scsi_dev_desc, |
| 693 | }; |
Simon Glass | 5bf96dc | 2016-05-01 11:36:24 -0600 | [diff] [blame] | 694 | #endif |