Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2001 |
| 4 | * Denis Peter, MPL AG Switzerland |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 5 | */ |
| 6 | #ifndef _SCSI_H |
| 7 | #define _SCSI_H |
| 8 | |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 9 | #include <asm/cache.h> |
Marek Vasut | b980e21 | 2023-08-14 01:50:00 +0200 | [diff] [blame] | 10 | #include <bouncebuf.h> |
Faiz Abbas | 2094809 | 2019-10-15 18:24:35 +0530 | [diff] [blame] | 11 | #include <linux/dma-direction.h> |
| 12 | |
Simon Glass | 1a46224 | 2022-01-31 07:49:36 -0700 | [diff] [blame] | 13 | /* Fix this to the maximum */ |
| 14 | #define SCSI_MAX_DEVICE \ |
| 15 | (CONFIG_SYS_SCSI_MAX_SCSI_ID * CONFIG_SYS_SCSI_MAX_LUN) |
| 16 | |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 17 | struct udevice; |
| 18 | |
Simon Glass | eccb24a | 2022-09-21 16:21:33 +0200 | [diff] [blame] | 19 | /** |
| 20 | * struct scsi_cmd - information about a SCSI command to be processed |
| 21 | * |
| 22 | * @cmd: command |
| 23 | * @sense_buf: for request sense |
| 24 | * @status: SCSI Status |
| 25 | * @target: Target ID |
| 26 | * @lun: Target LUN |
| 27 | * @cmdlen: command len |
| 28 | * @datalen: Total data length |
| 29 | * @pdata: pointer to data |
| 30 | * @msgout: Messge out buffer (NOT USED) |
| 31 | * @msgin: Message in buffer |
| 32 | * @sensecmdlen: Sense command len |
| 33 | * @sensedatalen: Sense data len |
| 34 | * @sensecmd: Sense command |
| 35 | * @contr_stat: Controller Status |
| 36 | * @trans_bytes: tranfered bytes |
| 37 | * @priv: Private value |
| 38 | * @dma_dir: Direction of data structure |
| 39 | */ |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 40 | struct scsi_cmd { |
Simon Glass | eccb24a | 2022-09-21 16:21:33 +0200 | [diff] [blame] | 41 | unsigned char cmd[16]; |
| 42 | unsigned char sense_buf[64] |
Puneet Saxena | 6c9bb60 | 2012-04-03 14:56:06 +0530 | [diff] [blame] | 43 | __attribute__((aligned(ARCH_DMA_MINALIGN))); |
Simon Glass | eccb24a | 2022-09-21 16:21:33 +0200 | [diff] [blame] | 44 | unsigned char status; |
| 45 | unsigned char target; |
| 46 | unsigned char lun; |
| 47 | unsigned char cmdlen; |
| 48 | unsigned long datalen; |
| 49 | unsigned char *pdata; |
| 50 | unsigned char msgout[12]; |
| 51 | unsigned char msgin[12]; |
| 52 | unsigned char sensecmdlen; |
| 53 | unsigned long sensedatalen; |
| 54 | unsigned char sensecmd[6]; |
| 55 | unsigned long contr_stat; |
| 56 | unsigned long trans_bytes; |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 57 | |
Simon Glass | eccb24a | 2022-09-21 16:21:33 +0200 | [diff] [blame] | 58 | unsigned int priv; |
| 59 | enum dma_data_direction dma_dir; |
Simon Glass | 5fb559d | 2017-06-14 21:28:30 -0600 | [diff] [blame] | 60 | }; |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 61 | |
| 62 | /*----------------------------------------------------------- |
| 63 | ** |
| 64 | ** SCSI constants. |
| 65 | ** |
| 66 | **----------------------------------------------------------- |
| 67 | */ |
| 68 | |
| 69 | /* |
| 70 | ** Messages |
| 71 | */ |
| 72 | |
| 73 | #define M_COMPLETE (0x00) |
| 74 | #define M_EXTENDED (0x01) |
| 75 | #define M_SAVE_DP (0x02) |
| 76 | #define M_RESTORE_DP (0x03) |
| 77 | #define M_DISCONNECT (0x04) |
| 78 | #define M_ID_ERROR (0x05) |
| 79 | #define M_ABORT (0x06) |
| 80 | #define M_REJECT (0x07) |
| 81 | #define M_NOOP (0x08) |
| 82 | #define M_PARITY (0x09) |
| 83 | #define M_LCOMPLETE (0x0a) |
| 84 | #define M_FCOMPLETE (0x0b) |
| 85 | #define M_RESET (0x0c) |
| 86 | #define M_ABORT_TAG (0x0d) |
| 87 | #define M_CLEAR_QUEUE (0x0e) |
| 88 | #define M_INIT_REC (0x0f) |
| 89 | #define M_REL_REC (0x10) |
| 90 | #define M_TERMINATE (0x11) |
| 91 | #define M_SIMPLE_TAG (0x20) |
| 92 | #define M_HEAD_TAG (0x21) |
| 93 | #define M_ORDERED_TAG (0x22) |
| 94 | #define M_IGN_RESIDUE (0x23) |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 95 | #define M_IDENTIFY (0x80) |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 96 | |
| 97 | #define M_X_MODIFY_DP (0x00) |
| 98 | #define M_X_SYNC_REQ (0x01) |
| 99 | #define M_X_WIDE_REQ (0x03) |
| 100 | #define M_X_PPR_REQ (0x04) |
| 101 | |
| 102 | |
| 103 | /* |
| 104 | ** Status |
| 105 | */ |
| 106 | |
| 107 | #define S_GOOD (0x00) |
| 108 | #define S_CHECK_COND (0x02) |
| 109 | #define S_COND_MET (0x04) |
| 110 | #define S_BUSY (0x08) |
| 111 | #define S_INT (0x10) |
| 112 | #define S_INT_COND_MET (0x14) |
| 113 | #define S_CONFLICT (0x18) |
| 114 | #define S_TERMINATED (0x20) |
| 115 | #define S_QUEUE_FULL (0x28) |
| 116 | #define S_ILLEGAL (0xff) |
| 117 | #define S_SENSE (0x80) |
| 118 | |
| 119 | /* |
| 120 | * Sense_keys |
| 121 | */ |
| 122 | |
| 123 | #define SENSE_NO_SENSE 0x0 |
| 124 | #define SENSE_RECOVERED_ERROR 0x1 |
| 125 | #define SENSE_NOT_READY 0x2 |
| 126 | #define SENSE_MEDIUM_ERROR 0x3 |
| 127 | #define SENSE_HARDWARE_ERROR 0x4 |
| 128 | #define SENSE_ILLEGAL_REQUEST 0x5 |
| 129 | #define SENSE_UNIT_ATTENTION 0x6 |
| 130 | #define SENSE_DATA_PROTECT 0x7 |
| 131 | #define SENSE_BLANK_CHECK 0x8 |
| 132 | #define SENSE_VENDOR_SPECIFIC 0x9 |
| 133 | #define SENSE_COPY_ABORTED 0xA |
| 134 | #define SENSE_ABORTED_COMMAND 0xB |
| 135 | #define SENSE_VOLUME_OVERFLOW 0xD |
| 136 | #define SENSE_MISCOMPARE 0xE |
| 137 | |
| 138 | |
| 139 | #define SCSI_CHANGE_DEF 0x40 /* Change Definition (Optional) */ |
| 140 | #define SCSI_COMPARE 0x39 /* Compare (O) */ |
| 141 | #define SCSI_COPY 0x18 /* Copy (O) */ |
| 142 | #define SCSI_COP_VERIFY 0x3A /* Copy and Verify (O) */ |
| 143 | #define SCSI_INQUIRY 0x12 /* Inquiry (MANDATORY) */ |
| 144 | #define SCSI_LOG_SELECT 0x4C /* Log Select (O) */ |
| 145 | #define SCSI_LOG_SENSE 0x4D /* Log Sense (O) */ |
| 146 | #define SCSI_MODE_SEL6 0x15 /* Mode Select 6-byte (Device Specific) */ |
| 147 | #define SCSI_MODE_SEL10 0x55 /* Mode Select 10-byte (Device Specific) */ |
| 148 | #define SCSI_MODE_SEN6 0x1A /* Mode Sense 6-byte (Device Specific) */ |
| 149 | #define SCSI_MODE_SEN10 0x5A /* Mode Sense 10-byte (Device Specific) */ |
| 150 | #define SCSI_READ_BUFF 0x3C /* Read Buffer (O) */ |
| 151 | #define SCSI_REQ_SENSE 0x03 /* Request Sense (MANDATORY) */ |
| 152 | #define SCSI_SEND_DIAG 0x1D /* Send Diagnostic (O) */ |
| 153 | #define SCSI_TST_U_RDY 0x00 /* Test Unit Ready (MANDATORY) */ |
| 154 | #define SCSI_WRITE_BUFF 0x3B /* Write Buffer (O) */ |
| 155 | /*************************************************************************** |
| 156 | * %%% Commands Unique to Direct Access Devices %%% |
| 157 | ***************************************************************************/ |
| 158 | #define SCSI_COMPARE 0x39 /* Compare (O) */ |
| 159 | #define SCSI_FORMAT 0x04 /* Format Unit (MANDATORY) */ |
| 160 | #define SCSI_LCK_UN_CAC 0x36 /* Lock Unlock Cache (O) */ |
| 161 | #define SCSI_PREFETCH 0x34 /* Prefetch (O) */ |
| 162 | #define SCSI_MED_REMOVL 0x1E /* Prevent/Allow medium Removal (O) */ |
| 163 | #define SCSI_READ6 0x08 /* Read 6-byte (MANDATORY) */ |
| 164 | #define SCSI_READ10 0x28 /* Read 10-byte (MANDATORY) */ |
Mark Langsdorf | 5ed06fc | 2015-06-05 00:58:45 +0100 | [diff] [blame] | 165 | #define SCSI_READ16 0x48 |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 166 | #define SCSI_RD_CAPAC 0x25 /* Read Capacity (MANDATORY) */ |
Gabe Black | dd2c734 | 2012-10-29 05:23:54 +0000 | [diff] [blame] | 167 | #define SCSI_RD_CAPAC10 SCSI_RD_CAPAC /* Read Capacity (10) */ |
| 168 | #define SCSI_RD_CAPAC16 0x9e /* Read Capacity (16) */ |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 169 | #define SCSI_RD_DEFECT 0x37 /* Read Defect Data (O) */ |
| 170 | #define SCSI_READ_LONG 0x3E /* Read Long (O) */ |
| 171 | #define SCSI_REASS_BLK 0x07 /* Reassign Blocks (O) */ |
| 172 | #define SCSI_RCV_DIAG 0x1C /* Receive Diagnostic Results (O) */ |
| 173 | #define SCSI_RELEASE 0x17 /* Release Unit (MANDATORY) */ |
| 174 | #define SCSI_REZERO 0x01 /* Rezero Unit (O) */ |
| 175 | #define SCSI_SRCH_DAT_E 0x31 /* Search Data Equal (O) */ |
| 176 | #define SCSI_SRCH_DAT_H 0x30 /* Search Data High (O) */ |
| 177 | #define SCSI_SRCH_DAT_L 0x32 /* Search Data Low (O) */ |
| 178 | #define SCSI_SEEK6 0x0B /* Seek 6-Byte (O) */ |
| 179 | #define SCSI_SEEK10 0x2B /* Seek 10-Byte (O) */ |
| 180 | #define SCSI_SEND_DIAG 0x1D /* Send Diagnostics (MANDATORY) */ |
| 181 | #define SCSI_SET_LIMIT 0x33 /* Set Limits (O) */ |
| 182 | #define SCSI_START_STP 0x1B /* Start/Stop Unit (O) */ |
| 183 | #define SCSI_SYNC_CACHE 0x35 /* Synchronize Cache (O) */ |
| 184 | #define SCSI_VERIFY 0x2F /* Verify (O) */ |
| 185 | #define SCSI_WRITE6 0x0A /* Write 6-Byte (MANDATORY) */ |
| 186 | #define SCSI_WRITE10 0x2A /* Write 10-Byte (MANDATORY) */ |
| 187 | #define SCSI_WRT_VERIFY 0x2E /* Write and Verify (O) */ |
| 188 | #define SCSI_WRITE_LONG 0x3F /* Write Long (O) */ |
| 189 | #define SCSI_WRITE_SAME 0x41 /* Write Same (O) */ |
| 190 | |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 191 | /** |
Simon Glass | e6e05ad | 2022-09-21 16:21:35 +0200 | [diff] [blame] | 192 | * enum scsi_cmd_phase - current phase of the SCSI protocol |
| 193 | * |
| 194 | * @SCSIPH_START: Start phase |
| 195 | * @SCSIPH_DATA: Data phase |
| 196 | * @SCSIPH_STATUS: Status phase |
| 197 | */ |
| 198 | enum scsi_cmd_phase { |
| 199 | SCSIPH_START, |
| 200 | SCSIPH_DATA, |
| 201 | SCSIPH_STATUS, |
| 202 | }; |
| 203 | |
| 204 | /** |
Simon Glass | bfe2c81 | 2022-09-21 16:21:43 +0200 | [diff] [blame] | 205 | * struct scsi_inquiry_resp - holds a SCSI inquiry command |
| 206 | * |
| 207 | * @type; command type |
| 208 | * @flags; command flags |
| 209 | * @version; command version |
| 210 | * @data_format; data format |
| 211 | * @additional_len; additional data length |
| 212 | * @spare[3]; spare bytes |
| 213 | * @vendor[8]; vendor information |
| 214 | * @product[16]; production information |
| 215 | * @revision[4]; revision information |
| 216 | */ |
| 217 | struct scsi_inquiry_resp { |
| 218 | u8 type; |
| 219 | u8 flags; |
| 220 | u8 version; |
| 221 | u8 data_format; |
| 222 | u8 additional_len; |
| 223 | u8 spare[3]; |
| 224 | char vendor[8]; |
| 225 | char product[16]; |
| 226 | char revision[4]; |
| 227 | }; |
| 228 | |
| 229 | /** |
| 230 | * struct scsi_read_capacity_resp - holds the response to a read-capacity cmd |
| 231 | * |
| 232 | * @last_block_addr: Logical block address of last block |
| 233 | * @block_len: Length of each block in bytes |
| 234 | */ |
| 235 | struct scsi_read_capacity_resp { |
| 236 | u32 last_block_addr; |
| 237 | u32 block_len; |
| 238 | }; |
| 239 | |
| 240 | /** |
| 241 | * struct scsi_read10_req - holds a SCSI READ10 request |
| 242 | * |
| 243 | * @cmd; command type |
| 244 | * @lun_flags; LUN flags |
| 245 | * @lba; Logical block address to start reading from |
| 246 | * @spare; spare bytes |
| 247 | * @xfer_len: number of blocks to read |
| 248 | * @spare2: more spare bytes |
| 249 | */ |
| 250 | struct __packed scsi_read10_req { |
| 251 | u8 cmd; |
| 252 | u8 lun_flags; |
| 253 | u32 lba; |
| 254 | u8 spare; |
| 255 | u16 xfer_len; |
| 256 | u8 spare2[3]; |
| 257 | }; |
| 258 | |
Simon Glass | 31b4f9e | 2022-10-20 18:22:55 -0600 | [diff] [blame] | 259 | /** struct scsi_write10_req - data for the write10 command */ |
| 260 | struct __packed scsi_write10_req { |
| 261 | u8 cmd; |
| 262 | u8 lun_flags; |
| 263 | u32 lba; |
| 264 | u8 spare; |
| 265 | u16 xfer_len; |
| 266 | u8 spare2[3]; |
| 267 | }; |
| 268 | |
Simon Glass | bfe2c81 | 2022-09-21 16:21:43 +0200 | [diff] [blame] | 269 | /** |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 270 | * struct scsi_plat - stores information about SCSI controller |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 271 | * |
| 272 | * @base: Controller base address |
| 273 | * @max_lun: Maximum number of logical units |
| 274 | * @max_id: Maximum number of target ids |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 275 | * @max_bytes_per_req: Maximum number of bytes per read/write request |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 276 | */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 277 | struct scsi_plat { |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 278 | unsigned long base; |
| 279 | unsigned long max_lun; |
| 280 | unsigned long max_id; |
Faiz Abbas | b26c614 | 2019-10-15 18:24:33 +0530 | [diff] [blame] | 281 | unsigned long max_bytes_per_req; |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 282 | }; |
Simon Glass | 3df498d | 2017-06-14 21:28:28 -0600 | [diff] [blame] | 283 | |
Simon Glass | 28aa974 | 2017-06-14 21:28:39 -0600 | [diff] [blame] | 284 | /* Operations for SCSI */ |
| 285 | struct scsi_ops { |
| 286 | /** |
| 287 | * exec() - execute a command |
| 288 | * |
| 289 | * @dev: SCSI bus |
| 290 | * @cmd: Command to execute |
| 291 | * @return 0 if OK, -ve on error |
| 292 | */ |
| 293 | int (*exec)(struct udevice *dev, struct scsi_cmd *cmd); |
| 294 | |
| 295 | /** |
| 296 | * bus_reset() - reset the bus |
| 297 | * |
| 298 | * @dev: SCSI bus to reset |
| 299 | * @return 0 if OK, -ve on error |
| 300 | */ |
| 301 | int (*bus_reset)(struct udevice *dev); |
Marek Vasut | b980e21 | 2023-08-14 01:50:00 +0200 | [diff] [blame] | 302 | |
| 303 | #if IS_ENABLED(CONFIG_BOUNCE_BUFFER) |
| 304 | /** |
| 305 | * buffer_aligned() - test memory alignment of block operation buffer |
| 306 | * |
| 307 | * Some devices have limited DMA capabilities and require that the |
| 308 | * buffers passed to them fit specific properties. This optional |
| 309 | * callback can be used to indicate whether a buffer alignment is |
| 310 | * suitable for the device DMA or not, and trigger use of generic |
| 311 | * bounce buffer implementation to help use of unsuitable buffers |
| 312 | * at the expense of performance degradation. |
| 313 | * |
| 314 | * @dev: Block device associated with the request |
| 315 | * @state: Bounce buffer state |
| 316 | * @return 1 if OK, 0 if unaligned |
| 317 | */ |
| 318 | int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state); |
| 319 | #endif /* CONFIG_BOUNCE_BUFFER */ |
Simon Glass | 28aa974 | 2017-06-14 21:28:39 -0600 | [diff] [blame] | 320 | }; |
| 321 | |
Simon Glass | c4dfa89 | 2017-06-14 21:28:43 -0600 | [diff] [blame] | 322 | #define scsi_get_ops(dev) ((struct scsi_ops *)(dev)->driver->ops) |
| 323 | |
| 324 | extern struct scsi_ops scsi_ops; |
Michal Simek | c886f35 | 2016-09-08 15:06:45 +0200 | [diff] [blame] | 325 | |
Simon Glass | c4dfa89 | 2017-06-14 21:28:43 -0600 | [diff] [blame] | 326 | /** |
| 327 | * scsi_exec() - execute a command |
| 328 | * |
| 329 | * @dev: SCSI bus |
| 330 | * @cmd: Command to execute |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 331 | * Return: 0 if OK, -ve on error |
Simon Glass | c4dfa89 | 2017-06-14 21:28:43 -0600 | [diff] [blame] | 332 | */ |
| 333 | int scsi_exec(struct udevice *dev, struct scsi_cmd *cmd); |
| 334 | |
| 335 | /** |
| 336 | * scsi_bus_reset() - reset the bus |
| 337 | * |
| 338 | * @dev: SCSI bus to reset |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 339 | * Return: 0 if OK, -ve on error |
Simon Glass | c4dfa89 | 2017-06-14 21:28:43 -0600 | [diff] [blame] | 340 | */ |
Simon Glass | 11b2b62 | 2017-06-14 21:28:40 -0600 | [diff] [blame] | 341 | int scsi_bus_reset(struct udevice *dev); |
Simon Glass | 3df498d | 2017-06-14 21:28:28 -0600 | [diff] [blame] | 342 | |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 343 | /** |
| 344 | * scsi_scan() - Scan all SCSI controllers for available devices |
| 345 | * |
| 346 | * @vebose: true to show information about each device found |
Simon Glass | 3df498d | 2017-06-14 21:28:28 -0600 | [diff] [blame] | 347 | */ |
Simon Glass | 4822873 | 2017-06-14 21:28:41 -0600 | [diff] [blame] | 348 | int scsi_scan(bool verbose); |
Simon Glass | 3df498d | 2017-06-14 21:28:28 -0600 | [diff] [blame] | 349 | |
Simon Glass | 600d001 | 2017-06-14 21:28:45 -0600 | [diff] [blame] | 350 | /** |
| 351 | * scsi_scan_dev() - scan a SCSI bus and create devices |
| 352 | * |
| 353 | * @dev: SCSI bus |
| 354 | * @verbose: true to show information about each device found |
| 355 | */ |
| 356 | int scsi_scan_dev(struct udevice *dev, bool verbose); |
| 357 | |
Simon Glass | c4dfa89 | 2017-06-14 21:28:43 -0600 | [diff] [blame] | 358 | #ifndef CONFIG_DM_SCSI |
| 359 | void scsi_low_level_init(int busdevfunc); |
| 360 | void scsi_init(void); |
| 361 | #endif |
| 362 | |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 363 | #define SCSI_IDENTIFY 0xC0 /* not used */ |
| 364 | |
| 365 | /* Hardware errors */ |
| 366 | #define SCSI_SEL_TIME_OUT 0x00000101 /* Selection time out */ |
| 367 | #define SCSI_HNS_TIME_OUT 0x00000102 /* Handshake */ |
Wolfgang Denk | a1be476 | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 368 | #define SCSI_MA_TIME_OUT 0x00000103 /* Phase error */ |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 369 | #define SCSI_UNEXP_DIS 0x00000104 /* unexpected disconnect */ |
| 370 | |
| 371 | #define SCSI_INT_STATE 0x00010000 /* unknown Interrupt number is stored in 16 LSB */ |
| 372 | |
wdenk | e85390d | 2002-04-01 14:29:03 +0000 | [diff] [blame] | 373 | #endif /* _SCSI_H */ |