wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2001 |
| 3 | * Denis Peter, MPL AG Switzerland |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * SCSI support. |
| 10 | */ |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <command.h> |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 13 | #include <asm/processor.h> |
| 14 | #include <scsi.h> |
| 15 | #include <image.h> |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 16 | #include <pci.h> |
| 17 | |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 18 | #ifdef CONFIG_SCSI_DEV_LIST |
| 19 | #define SCSI_DEV_LIST CONFIG_SCSI_DEV_LIST |
| 20 | #else |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 21 | #ifdef CONFIG_SCSI_SYM53C8XX |
| 22 | #define SCSI_VEND_ID 0x1000 |
| 23 | #ifndef CONFIG_SCSI_DEV_ID |
| 24 | #define SCSI_DEV_ID 0x0001 |
| 25 | #else |
| 26 | #define SCSI_DEV_ID CONFIG_SCSI_DEV_ID |
| 27 | #endif |
Jin Zhengxiong | ae180dc | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 28 | #elif defined CONFIG_SATA_ULI5288 |
| 29 | |
| 30 | #define SCSI_VEND_ID 0x10b9 |
| 31 | #define SCSI_DEV_ID 0x5288 |
| 32 | |
Rob Herring | c2829ff | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 33 | #elif !defined(CONFIG_SCSI_AHCI_PLAT) |
Jin Zhengxiong | ae180dc | 2006-08-23 19:10:44 +0800 | [diff] [blame] | 34 | #error no scsi device defined |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 35 | #endif |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 36 | #define SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID} |
| 37 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 38 | |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 39 | #ifdef CONFIG_PCI |
| 40 | const struct pci_device_id scsi_device_list[] = { SCSI_DEV_LIST }; |
| 41 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 42 | static ccb tempccb; /* temporary scsi command buffer */ |
| 43 | |
| 44 | static unsigned char tempbuff[512]; /* temporary data buffer */ |
| 45 | |
| 46 | static int scsi_max_devs; /* number of highest available scsi device */ |
| 47 | |
| 48 | static int scsi_curr_dev; /* current device */ |
| 49 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 50 | static block_dev_desc_t scsi_dev_desc[CONFIG_SYS_SCSI_MAX_DEVICE]; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 51 | |
| 52 | /******************************************************************************** |
| 53 | * forward declerations of some Setup Routines |
| 54 | */ |
| 55 | void scsi_setup_test_unit_ready(ccb * pccb); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 56 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks); |
| 57 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks); |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 58 | static void scsi_setup_write_ext(ccb *pccb, unsigned long start, |
| 59 | unsigned short blocks); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 60 | void scsi_setup_inquiry(ccb * pccb); |
| 61 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len); |
| 62 | |
| 63 | |
Gabe Black | bcc3539 | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 64 | static int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, |
| 65 | unsigned long *blksz); |
Simon Glass | 0d996a8 | 2013-07-03 07:11:41 -0700 | [diff] [blame] | 66 | static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt, |
| 67 | void *buffer); |
| 68 | static ulong scsi_write(int device, lbaint_t blknr, |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 69 | lbaint_t blkcnt, const void *buffer); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | /********************************************************************************* |
| 73 | * (re)-scan the scsi bus and reports scsi device info |
| 74 | * to the user if mode = 1 |
| 75 | */ |
| 76 | void scsi_scan(int mode) |
| 77 | { |
| 78 | unsigned char i,perq,modi,lun; |
Gabe Black | bcc3539 | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 79 | lbaint_t capacity; |
| 80 | unsigned long blksz; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 81 | ccb* pccb=(ccb *)&tempccb; |
| 82 | |
| 83 | if(mode==1) { |
| 84 | printf("scanning bus for devices...\n"); |
| 85 | } |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | for(i=0;i<CONFIG_SYS_SCSI_MAX_DEVICE;i++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 87 | scsi_dev_desc[i].target=0xff; |
| 88 | scsi_dev_desc[i].lun=0xff; |
| 89 | scsi_dev_desc[i].lba=0; |
| 90 | scsi_dev_desc[i].blksz=0; |
Egbert Eich | 2eec2ab | 2013-04-09 21:11:56 +0000 | [diff] [blame] | 91 | scsi_dev_desc[i].log2blksz = |
| 92 | LOG2_INVALID(typeof(scsi_dev_desc[i].log2blksz)); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 93 | scsi_dev_desc[i].type=DEV_TYPE_UNKNOWN; |
| 94 | scsi_dev_desc[i].vendor[0]=0; |
| 95 | scsi_dev_desc[i].product[0]=0; |
| 96 | scsi_dev_desc[i].revision[0]=0; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 97 | scsi_dev_desc[i].removable = false; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 98 | scsi_dev_desc[i].if_type=IF_TYPE_SCSI; |
| 99 | scsi_dev_desc[i].dev=i; |
| 100 | scsi_dev_desc[i].part_type=PART_TYPE_UNKNOWN; |
| 101 | scsi_dev_desc[i].block_read=scsi_read; |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 102 | scsi_dev_desc[i].block_write = scsi_write; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 103 | } |
| 104 | scsi_max_devs=0; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 105 | for(i=0;i<CONFIG_SYS_SCSI_MAX_SCSI_ID;i++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 106 | pccb->target=i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 107 | for(lun=0;lun<CONFIG_SYS_SCSI_MAX_LUN;lun++) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 108 | pccb->lun=lun; |
| 109 | pccb->pdata=(unsigned char *)&tempbuff; |
| 110 | pccb->datalen=512; |
| 111 | scsi_setup_inquiry(pccb); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 112 | if (scsi_exec(pccb) != true) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 113 | if(pccb->contr_stat==SCSI_SEL_TIME_OUT) { |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 114 | debug ("Selection timeout ID %d\n",pccb->target); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 115 | continue; /* selection timeout => assuming no device present */ |
| 116 | } |
| 117 | scsi_print_error(pccb); |
| 118 | continue; |
| 119 | } |
| 120 | perq=tempbuff[0]; |
| 121 | modi=tempbuff[1]; |
| 122 | if((perq & 0x1f)==0x1f) { |
| 123 | continue; /* skip unknown devices */ |
| 124 | } |
| 125 | if((modi&0x80)==0x80) /* drive is removable */ |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 126 | scsi_dev_desc[scsi_max_devs].removable=true; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 127 | /* get info for this device */ |
Stefan Roese | 4404c16 | 2007-11-17 07:58:25 +0100 | [diff] [blame] | 128 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].vendor[0], |
| 129 | &tempbuff[8], 8); |
| 130 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].product[0], |
| 131 | &tempbuff[16], 16); |
| 132 | scsi_ident_cpy((unsigned char *)&scsi_dev_desc[scsi_max_devs].revision[0], |
| 133 | &tempbuff[32], 4); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 134 | scsi_dev_desc[scsi_max_devs].target=pccb->target; |
| 135 | scsi_dev_desc[scsi_max_devs].lun=pccb->lun; |
| 136 | |
| 137 | pccb->datalen=0; |
| 138 | scsi_setup_test_unit_ready(pccb); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 139 | if (scsi_exec(pccb) != true) { |
| 140 | if (scsi_dev_desc[scsi_max_devs].removable == true) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 141 | scsi_dev_desc[scsi_max_devs].type=perq; |
| 142 | goto removable; |
| 143 | } |
| 144 | scsi_print_error(pccb); |
| 145 | continue; |
| 146 | } |
Gabe Black | bcc3539 | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 147 | if (scsi_read_capacity(pccb, &capacity, &blksz)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 148 | scsi_print_error(pccb); |
| 149 | continue; |
| 150 | } |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 151 | scsi_dev_desc[scsi_max_devs].lba=capacity; |
| 152 | scsi_dev_desc[scsi_max_devs].blksz=blksz; |
Egbert Eich | 2eec2ab | 2013-04-09 21:11:56 +0000 | [diff] [blame] | 153 | scsi_dev_desc[scsi_max_devs].log2blksz = |
| 154 | LOG2(scsi_dev_desc[scsi_max_devs].blksz); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 155 | scsi_dev_desc[scsi_max_devs].type=perq; |
| 156 | init_part(&scsi_dev_desc[scsi_max_devs]); |
| 157 | removable: |
| 158 | if(mode==1) { |
| 159 | printf (" Device %d: ", scsi_max_devs); |
| 160 | dev_print(&scsi_dev_desc[scsi_max_devs]); |
| 161 | } /* if mode */ |
| 162 | scsi_max_devs++; |
| 163 | } /* next LUN */ |
| 164 | } |
| 165 | if(scsi_max_devs>0) |
| 166 | scsi_curr_dev=0; |
| 167 | else |
Wolfgang Denk | dc770c7 | 2008-07-14 15:19:07 +0200 | [diff] [blame] | 168 | scsi_curr_dev = -1; |
Stefan Reinauer | e50a10e | 2012-10-29 05:23:48 +0000 | [diff] [blame] | 169 | |
| 170 | printf("Found %d device(s).\n", scsi_max_devs); |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 171 | #ifndef CONFIG_SPL_BUILD |
Stefan Reinauer | e50a10e | 2012-10-29 05:23:48 +0000 | [diff] [blame] | 172 | setenv_ulong("scsidevs", scsi_max_devs); |
Dan Murphy | 0c3ab85 | 2014-02-03 06:59:01 -0600 | [diff] [blame] | 173 | #endif |
Stefan Reinauer | e50a10e | 2012-10-29 05:23:48 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int scsi_get_disk_count(void) |
| 177 | { |
| 178 | return scsi_max_devs; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Rob Herring | c2829ff | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 181 | #ifdef CONFIG_PCI |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 182 | void scsi_init(void) |
| 183 | { |
| 184 | int busdevfunc; |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 185 | int i; |
| 186 | /* |
| 187 | * Find a device from the list, this driver will support a single |
| 188 | * controller. |
| 189 | */ |
| 190 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 191 | /* get PCI Device ID */ |
| 192 | busdevfunc = pci_find_device(scsi_device_list[i].vendor, |
| 193 | scsi_device_list[i].device, |
| 194 | 0); |
| 195 | if (busdevfunc != -1) |
| 196 | break; |
| 197 | } |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 198 | |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 199 | if (busdevfunc == -1) { |
| 200 | printf("Error: SCSI Controller(s) "); |
| 201 | for (i = 0; i < ARRAY_SIZE(scsi_device_list); i++) { |
| 202 | printf("%04X:%04X ", |
| 203 | scsi_device_list[i].vendor, |
| 204 | scsi_device_list[i].device); |
| 205 | } |
| 206 | printf("not found\n"); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | #ifdef DEBUG |
| 210 | else { |
Vadim Bendebury | 3e9fa30 | 2012-10-29 05:23:45 +0000 | [diff] [blame] | 211 | printf("SCSI Controller (%04X,%04X) found (%d:%d:%d)\n", |
| 212 | scsi_device_list[i].vendor, |
| 213 | scsi_device_list[i].device, |
| 214 | (busdevfunc >> 16) & 0xFF, |
| 215 | (busdevfunc >> 11) & 0x1F, |
| 216 | (busdevfunc >> 8) & 0x7); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 217 | } |
| 218 | #endif |
| 219 | scsi_low_level_init(busdevfunc); |
| 220 | scsi_scan(1); |
| 221 | } |
Rob Herring | c2829ff | 2011-07-06 16:13:36 +0000 | [diff] [blame] | 222 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 223 | |
Matthew McClintock | 6252b4f | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 224 | #ifdef CONFIG_PARTITIONS |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 225 | block_dev_desc_t * scsi_get_dev(int dev) |
| 226 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 227 | return (dev < CONFIG_SYS_SCSI_MAX_DEVICE) ? &scsi_dev_desc[dev] : NULL; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 228 | } |
Matthew McClintock | 6252b4f | 2011-05-24 05:31:19 +0000 | [diff] [blame] | 229 | #endif |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 230 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 231 | /****************************************************************************** |
| 232 | * scsi boot command intepreter. Derived from diskboot |
| 233 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 234 | int do_scsiboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 235 | { |
Rob Herring | a20cbf9 | 2012-09-21 04:02:30 +0000 | [diff] [blame] | 236 | return common_diskboot(cmdtp, "scsi", argc, argv); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /********************************************************************************* |
| 240 | * scsi command intepreter |
| 241 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 242 | int do_scsi (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 243 | { |
| 244 | switch (argc) { |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 245 | case 0: |
| 246 | case 1: |
| 247 | return CMD_RET_USAGE; |
Wolfgang Denk | 3b68311 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 248 | |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 249 | case 2: |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 250 | if (strncmp(argv[1],"res",3) == 0) { |
| 251 | printf("\nReset SCSI\n"); |
| 252 | scsi_bus_reset(); |
| 253 | scsi_scan(1); |
| 254 | return 0; |
| 255 | } |
| 256 | if (strncmp(argv[1],"inf",3) == 0) { |
| 257 | int i; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 258 | for (i=0; i<CONFIG_SYS_SCSI_MAX_DEVICE; ++i) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 259 | if(scsi_dev_desc[i].type==DEV_TYPE_UNKNOWN) |
| 260 | continue; /* list only known devices */ |
| 261 | printf ("SCSI dev. %d: ", i); |
| 262 | dev_print(&scsi_dev_desc[i]); |
| 263 | } |
| 264 | return 0; |
| 265 | } |
| 266 | if (strncmp(argv[1],"dev",3) == 0) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 267 | if ((scsi_curr_dev < 0) || (scsi_curr_dev >= CONFIG_SYS_SCSI_MAX_DEVICE)) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 268 | printf("\nno SCSI devices available\n"); |
| 269 | return 1; |
| 270 | } |
| 271 | printf ("\n Device %d: ", scsi_curr_dev); |
| 272 | dev_print(&scsi_dev_desc[scsi_curr_dev]); |
| 273 | return 0; |
| 274 | } |
| 275 | if (strncmp(argv[1],"scan",4) == 0) { |
| 276 | scsi_scan(1); |
| 277 | return 0; |
| 278 | } |
| 279 | if (strncmp(argv[1],"part",4) == 0) { |
| 280 | int dev, ok; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 281 | for (ok=0, dev=0; dev<CONFIG_SYS_SCSI_MAX_DEVICE; ++dev) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 282 | if (scsi_dev_desc[dev].type!=DEV_TYPE_UNKNOWN) { |
| 283 | ok++; |
| 284 | if (dev) |
| 285 | printf("\n"); |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 286 | debug ("print_part of %x\n",dev); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 287 | print_part(&scsi_dev_desc[dev]); |
| 288 | } |
| 289 | } |
| 290 | if (!ok) |
| 291 | printf("\nno SCSI devices available\n"); |
| 292 | return 1; |
| 293 | } |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 294 | return CMD_RET_USAGE; |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 295 | case 3: |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 296 | if (strncmp(argv[1],"dev",3) == 0) { |
| 297 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 298 | printf ("\nSCSI device %d: ", dev); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 299 | if (dev >= CONFIG_SYS_SCSI_MAX_DEVICE) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 300 | printf("unknown device\n"); |
| 301 | return 1; |
| 302 | } |
| 303 | printf ("\n Device %d: ", dev); |
| 304 | dev_print(&scsi_dev_desc[dev]); |
| 305 | if(scsi_dev_desc[dev].type == DEV_TYPE_UNKNOWN) { |
| 306 | return 1; |
| 307 | } |
| 308 | scsi_curr_dev = dev; |
| 309 | printf("... is now current device\n"); |
| 310 | return 0; |
| 311 | } |
| 312 | if (strncmp(argv[1],"part",4) == 0) { |
| 313 | int dev = (int)simple_strtoul(argv[2], NULL, 10); |
| 314 | if(scsi_dev_desc[dev].type != DEV_TYPE_UNKNOWN) { |
| 315 | print_part(&scsi_dev_desc[dev]); |
| 316 | } |
| 317 | else { |
| 318 | printf ("\nSCSI device %d not available\n", dev); |
| 319 | } |
| 320 | return 1; |
| 321 | } |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 322 | return CMD_RET_USAGE; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 323 | default: |
| 324 | /* at least 4 args */ |
| 325 | if (strcmp(argv[1],"read") == 0) { |
| 326 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 327 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 328 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 329 | ulong n; |
| 330 | printf ("\nSCSI read: device %d block # %ld, count %ld ... ", |
| 331 | scsi_curr_dev, blk, cnt); |
| 332 | n = scsi_read(scsi_curr_dev, blk, cnt, (ulong *)addr); |
| 333 | printf ("%ld blocks read: %s\n",n,(n==cnt) ? "OK" : "ERROR"); |
| 334 | return 0; |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 335 | } else if (strcmp(argv[1], "write") == 0) { |
| 336 | ulong addr = simple_strtoul(argv[2], NULL, 16); |
| 337 | ulong blk = simple_strtoul(argv[3], NULL, 16); |
| 338 | ulong cnt = simple_strtoul(argv[4], NULL, 16); |
| 339 | ulong n; |
| 340 | printf("\nSCSI write: device %d block # %ld, " |
| 341 | "count %ld ... ", |
| 342 | scsi_curr_dev, blk, cnt); |
| 343 | n = scsi_write(scsi_curr_dev, blk, cnt, |
| 344 | (ulong *)addr); |
| 345 | printf("%ld blocks written: %s\n", n, |
| 346 | (n == cnt) ? "OK" : "ERROR"); |
| 347 | return 0; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 348 | } |
| 349 | } /* switch */ |
Simon Glass | a06dfc7 | 2011-12-10 08:44:01 +0000 | [diff] [blame] | 350 | return CMD_RET_USAGE; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /**************************************************************************************** |
| 354 | * scsi_read |
| 355 | */ |
| 356 | |
| 357 | #define SCSI_MAX_READ_BLK 0xFFFF /* almost the maximum amount of the scsi_ext command.. */ |
| 358 | |
Simon Glass | 0d996a8 | 2013-07-03 07:11:41 -0700 | [diff] [blame] | 359 | static ulong scsi_read(int device, lbaint_t blknr, lbaint_t blkcnt, |
| 360 | void *buffer) |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 361 | { |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 362 | lbaint_t start, blks; |
| 363 | uintptr_t buf_addr; |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 364 | unsigned short smallblks; |
| 365 | ccb* pccb=(ccb *)&tempccb; |
| 366 | device&=0xff; |
| 367 | /* Setup device |
| 368 | */ |
| 369 | pccb->target=scsi_dev_desc[device].target; |
| 370 | pccb->lun=scsi_dev_desc[device].lun; |
| 371 | buf_addr=(unsigned long)buffer; |
| 372 | start=blknr; |
| 373 | blks=blkcnt; |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 374 | debug("\nscsi_read: dev %d startblk " LBAF |
| 375 | ", blccnt " LBAF " buffer %lx\n", |
| 376 | device, start, blks, (unsigned long)buffer); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 377 | do { |
| 378 | pccb->pdata=(unsigned char *)buf_addr; |
| 379 | if(blks>SCSI_MAX_READ_BLK) { |
| 380 | pccb->datalen=scsi_dev_desc[device].blksz * SCSI_MAX_READ_BLK; |
| 381 | smallblks=SCSI_MAX_READ_BLK; |
| 382 | scsi_setup_read_ext(pccb,start,smallblks); |
| 383 | start+=SCSI_MAX_READ_BLK; |
| 384 | blks-=SCSI_MAX_READ_BLK; |
| 385 | } |
| 386 | else { |
| 387 | pccb->datalen=scsi_dev_desc[device].blksz * blks; |
| 388 | smallblks=(unsigned short) blks; |
| 389 | scsi_setup_read_ext(pccb,start,smallblks); |
| 390 | start+=blks; |
| 391 | blks=0; |
| 392 | } |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 393 | debug("scsi_read_ext: startblk " LBAF |
| 394 | ", blccnt %x buffer %lx\n", |
| 395 | start, smallblks, buf_addr); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 396 | if (scsi_exec(pccb) != true) { |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 397 | scsi_print_error(pccb); |
| 398 | blkcnt-=blks; |
| 399 | break; |
| 400 | } |
| 401 | buf_addr+=pccb->datalen; |
| 402 | } while(blks!=0); |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 403 | debug("scsi_read_ext: end startblk " LBAF |
| 404 | ", blccnt %x buffer %lx\n", start, smallblks, buf_addr); |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 405 | return(blkcnt); |
| 406 | } |
| 407 | |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 408 | /******************************************************************************* |
| 409 | * scsi_write |
| 410 | */ |
| 411 | |
| 412 | /* Almost the maximum amount of the scsi_ext command.. */ |
| 413 | #define SCSI_MAX_WRITE_BLK 0xFFFF |
| 414 | |
Simon Glass | 0d996a8 | 2013-07-03 07:11:41 -0700 | [diff] [blame] | 415 | static ulong scsi_write(int device, lbaint_t blknr, |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 416 | lbaint_t blkcnt, const void *buffer) |
| 417 | { |
| 418 | lbaint_t start, blks; |
| 419 | uintptr_t buf_addr; |
| 420 | unsigned short smallblks; |
| 421 | ccb* pccb = (ccb *)&tempccb; |
| 422 | device &= 0xff; |
| 423 | /* Setup device |
| 424 | */ |
| 425 | pccb->target = scsi_dev_desc[device].target; |
| 426 | pccb->lun = scsi_dev_desc[device].lun; |
| 427 | buf_addr = (unsigned long)buffer; |
| 428 | start = blknr; |
| 429 | blks = blkcnt; |
| 430 | debug("\n%s: dev %d startblk " LBAF ", blccnt " LBAF " buffer %lx\n", |
| 431 | __func__, device, start, blks, (unsigned long)buffer); |
| 432 | do { |
| 433 | pccb->pdata = (unsigned char *)buf_addr; |
| 434 | if (blks > SCSI_MAX_WRITE_BLK) { |
| 435 | pccb->datalen = (scsi_dev_desc[device].blksz * |
| 436 | SCSI_MAX_WRITE_BLK); |
| 437 | smallblks = SCSI_MAX_WRITE_BLK; |
| 438 | scsi_setup_write_ext(pccb, start, smallblks); |
| 439 | start += SCSI_MAX_WRITE_BLK; |
| 440 | blks -= SCSI_MAX_WRITE_BLK; |
| 441 | } else { |
| 442 | pccb->datalen = scsi_dev_desc[device].blksz * blks; |
| 443 | smallblks = (unsigned short)blks; |
| 444 | scsi_setup_write_ext(pccb, start, smallblks); |
| 445 | start += blks; |
| 446 | blks = 0; |
| 447 | } |
| 448 | debug("%s: startblk " LBAF ", blccnt %x buffer %lx\n", |
| 449 | __func__, start, smallblks, buf_addr); |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 450 | if (scsi_exec(pccb) != true) { |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 451 | scsi_print_error(pccb); |
| 452 | blkcnt -= blks; |
| 453 | break; |
| 454 | } |
| 455 | buf_addr += pccb->datalen; |
| 456 | } while (blks != 0); |
| 457 | debug("%s: end startblk " LBAF ", blccnt %x buffer %lx\n", |
| 458 | __func__, start, smallblks, buf_addr); |
| 459 | return blkcnt; |
| 460 | } |
| 461 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 462 | /* copy src to dest, skipping leading and trailing blanks |
| 463 | * and null terminate the string |
| 464 | */ |
| 465 | void scsi_ident_cpy (unsigned char *dest, unsigned char *src, unsigned int len) |
| 466 | { |
| 467 | int start,end; |
| 468 | |
| 469 | start=0; |
| 470 | while(start<len) { |
| 471 | if(src[start]!=' ') |
| 472 | break; |
| 473 | start++; |
| 474 | } |
| 475 | end=len-1; |
| 476 | while(end>start) { |
| 477 | if(src[end]!=' ') |
| 478 | break; |
| 479 | end--; |
| 480 | } |
| 481 | for( ; start<=end; start++) { |
| 482 | *dest++=src[start]; |
| 483 | } |
| 484 | *dest='\0'; |
| 485 | } |
| 486 | |
| 487 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 488 | /* Trim trailing blanks, and NUL-terminate string |
| 489 | */ |
| 490 | void scsi_trim_trail (unsigned char *str, unsigned int len) |
| 491 | { |
| 492 | unsigned char *p = str + len - 1; |
| 493 | |
| 494 | while (len-- > 0) { |
| 495 | *p-- = '\0'; |
| 496 | if (*p != ' ') { |
| 497 | return; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
Gabe Black | bcc3539 | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 502 | int scsi_read_capacity(ccb *pccb, lbaint_t *capacity, unsigned long *blksz) |
| 503 | { |
| 504 | *capacity = 0; |
| 505 | |
| 506 | memset(pccb->cmd, 0, sizeof(pccb->cmd)); |
| 507 | pccb->cmd[0] = SCSI_RD_CAPAC10; |
| 508 | pccb->cmd[1] = pccb->lun << 5; |
| 509 | pccb->cmdlen = 10; |
| 510 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 511 | |
| 512 | pccb->datalen = 8; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 513 | if (scsi_exec(pccb) != true) |
Gabe Black | bcc3539 | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 514 | return 1; |
| 515 | |
| 516 | *capacity = ((lbaint_t)pccb->pdata[0] << 24) | |
| 517 | ((lbaint_t)pccb->pdata[1] << 16) | |
| 518 | ((lbaint_t)pccb->pdata[2] << 8) | |
| 519 | ((lbaint_t)pccb->pdata[3]); |
| 520 | |
| 521 | if (*capacity != 0xffffffff) { |
| 522 | /* Read capacity (10) was sufficient for this drive. */ |
| 523 | *blksz = ((unsigned long)pccb->pdata[4] << 24) | |
| 524 | ((unsigned long)pccb->pdata[5] << 16) | |
| 525 | ((unsigned long)pccb->pdata[6] << 8) | |
| 526 | ((unsigned long)pccb->pdata[7]); |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | /* Read capacity (10) was insufficient. Use read capacity (16). */ |
| 531 | |
| 532 | memset(pccb->cmd, 0, sizeof(pccb->cmd)); |
| 533 | pccb->cmd[0] = SCSI_RD_CAPAC16; |
| 534 | pccb->cmd[1] = 0x10; |
| 535 | pccb->cmdlen = 16; |
| 536 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 537 | |
| 538 | pccb->datalen = 16; |
York Sun | 4a59809 | 2013-04-01 11:29:11 -0700 | [diff] [blame] | 539 | if (scsi_exec(pccb) != true) |
Gabe Black | bcc3539 | 2012-10-29 05:23:57 +0000 | [diff] [blame] | 540 | return 1; |
| 541 | |
| 542 | *capacity = ((uint64_t)pccb->pdata[0] << 56) | |
| 543 | ((uint64_t)pccb->pdata[1] << 48) | |
| 544 | ((uint64_t)pccb->pdata[2] << 40) | |
| 545 | ((uint64_t)pccb->pdata[3] << 32) | |
| 546 | ((uint64_t)pccb->pdata[4] << 24) | |
| 547 | ((uint64_t)pccb->pdata[5] << 16) | |
| 548 | ((uint64_t)pccb->pdata[6] << 8) | |
| 549 | ((uint64_t)pccb->pdata[7]); |
| 550 | |
| 551 | *blksz = ((uint64_t)pccb->pdata[8] << 56) | |
| 552 | ((uint64_t)pccb->pdata[9] << 48) | |
| 553 | ((uint64_t)pccb->pdata[10] << 40) | |
| 554 | ((uint64_t)pccb->pdata[11] << 32) | |
| 555 | ((uint64_t)pccb->pdata[12] << 24) | |
| 556 | ((uint64_t)pccb->pdata[13] << 16) | |
| 557 | ((uint64_t)pccb->pdata[14] << 8) | |
| 558 | ((uint64_t)pccb->pdata[15]); |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 563 | |
| 564 | /************************************************************************************ |
| 565 | * Some setup (fill-in) routines |
| 566 | */ |
| 567 | void scsi_setup_test_unit_ready(ccb * pccb) |
| 568 | { |
| 569 | pccb->cmd[0]=SCSI_TST_U_RDY; |
| 570 | pccb->cmd[1]=pccb->lun<<5; |
| 571 | pccb->cmd[2]=0; |
| 572 | pccb->cmd[3]=0; |
| 573 | pccb->cmd[4]=0; |
| 574 | pccb->cmd[5]=0; |
| 575 | pccb->cmdlen=6; |
| 576 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | void scsi_setup_read_ext(ccb * pccb, unsigned long start, unsigned short blocks) |
| 580 | { |
| 581 | pccb->cmd[0]=SCSI_READ10; |
| 582 | pccb->cmd[1]=pccb->lun<<5; |
| 583 | pccb->cmd[2]=((unsigned char) (start>>24))&0xff; |
| 584 | pccb->cmd[3]=((unsigned char) (start>>16))&0xff; |
| 585 | pccb->cmd[4]=((unsigned char) (start>>8))&0xff; |
| 586 | pccb->cmd[5]=((unsigned char) (start))&0xff; |
| 587 | pccb->cmd[6]=0; |
| 588 | pccb->cmd[7]=((unsigned char) (blocks>>8))&0xff; |
| 589 | pccb->cmd[8]=(unsigned char) blocks & 0xff; |
| 590 | pccb->cmd[6]=0; |
| 591 | pccb->cmdlen=10; |
| 592 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 593 | debug ("scsi_setup_read_ext: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 594 | pccb->cmd[0],pccb->cmd[1], |
| 595 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4],pccb->cmd[5], |
| 596 | pccb->cmd[7],pccb->cmd[8]); |
| 597 | } |
| 598 | |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 599 | void scsi_setup_write_ext(ccb *pccb, unsigned long start, unsigned short blocks) |
| 600 | { |
| 601 | pccb->cmd[0] = SCSI_WRITE10; |
| 602 | pccb->cmd[1] = pccb->lun << 5; |
| 603 | pccb->cmd[2] = ((unsigned char) (start>>24)) & 0xff; |
| 604 | pccb->cmd[3] = ((unsigned char) (start>>16)) & 0xff; |
| 605 | pccb->cmd[4] = ((unsigned char) (start>>8)) & 0xff; |
| 606 | pccb->cmd[5] = ((unsigned char) (start)) & 0xff; |
| 607 | pccb->cmd[6] = 0; |
| 608 | pccb->cmd[7] = ((unsigned char) (blocks>>8)) & 0xff; |
| 609 | pccb->cmd[8] = (unsigned char)blocks & 0xff; |
| 610 | pccb->cmd[9] = 0; |
| 611 | pccb->cmdlen = 10; |
| 612 | pccb->msgout[0] = SCSI_IDENTIFY; /* NOT USED */ |
| 613 | debug("%s: cmd: %02X %02X startblk %02X%02X%02X%02X blccnt %02X%02X\n", |
| 614 | __func__, |
| 615 | pccb->cmd[0], pccb->cmd[1], |
| 616 | pccb->cmd[2], pccb->cmd[3], pccb->cmd[4], pccb->cmd[5], |
| 617 | pccb->cmd[7], pccb->cmd[8]); |
| 618 | } |
| 619 | |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 620 | void scsi_setup_read6(ccb * pccb, unsigned long start, unsigned short blocks) |
| 621 | { |
| 622 | pccb->cmd[0]=SCSI_READ6; |
| 623 | pccb->cmd[1]=pccb->lun<<5 | (((unsigned char)(start>>16))&0x1f); |
| 624 | pccb->cmd[2]=((unsigned char) (start>>8))&0xff; |
| 625 | pccb->cmd[3]=((unsigned char) (start))&0xff; |
| 626 | pccb->cmd[4]=(unsigned char) blocks & 0xff; |
| 627 | pccb->cmd[5]=0; |
| 628 | pccb->cmdlen=6; |
| 629 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
wdenk | f1276d4 | 2005-02-03 23:00:49 +0000 | [diff] [blame] | 630 | debug ("scsi_setup_read6: cmd: %02X %02X startblk %02X%02X blccnt %02X\n", |
wdenk | e887afc | 2002-08-27 09:44:07 +0000 | [diff] [blame] | 631 | pccb->cmd[0],pccb->cmd[1], |
| 632 | pccb->cmd[2],pccb->cmd[3],pccb->cmd[4]); |
| 633 | } |
| 634 | |
| 635 | |
| 636 | void scsi_setup_inquiry(ccb * pccb) |
| 637 | { |
| 638 | pccb->cmd[0]=SCSI_INQUIRY; |
| 639 | pccb->cmd[1]=pccb->lun<<5; |
| 640 | pccb->cmd[2]=0; |
| 641 | pccb->cmd[3]=0; |
| 642 | if(pccb->datalen>255) |
| 643 | pccb->cmd[4]=255; |
| 644 | else |
| 645 | pccb->cmd[4]=(unsigned char)pccb->datalen; |
| 646 | pccb->cmd[5]=0; |
| 647 | pccb->cmdlen=6; |
| 648 | pccb->msgout[0]=SCSI_IDENTIFY; /* NOT USED */ |
| 649 | } |
| 650 | |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 651 | |
| 652 | U_BOOT_CMD( |
| 653 | scsi, 5, 1, do_scsi, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 654 | "SCSI sub-system", |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 655 | "reset - reset SCSI controller\n" |
| 656 | "scsi info - show available SCSI devices\n" |
| 657 | "scsi scan - (re-)scan SCSI bus\n" |
| 658 | "scsi device [dev] - show or set current device\n" |
| 659 | "scsi part [dev] - print partition table of one or all SCSI devices\n" |
| 660 | "scsi read addr blk# cnt - read `cnt' blocks starting at block `blk#'\n" |
Hung-Te Lin | 98a6c4e | 2012-10-29 05:23:46 +0000 | [diff] [blame] | 661 | " to memory address `addr'\n" |
| 662 | "scsi write addr blk# cnt - write `cnt' blocks starting at block\n" |
| 663 | " `blk#' from memory address `addr'" |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 664 | ); |
| 665 | |
| 666 | U_BOOT_CMD( |
| 667 | scsiboot, 3, 1, do_scsiboot, |
Peter Tyser | dfb72b8 | 2009-01-27 18:03:12 -0600 | [diff] [blame] | 668 | "boot from SCSI device", |
Wolfgang Denk | c54781c | 2009-05-24 17:06:54 +0200 | [diff] [blame] | 669 | "loadAddr dev:part" |
Wolfgang Denk | 6a3d6b0 | 2005-08-04 01:14:12 +0200 | [diff] [blame] | 670 | ); |