blob: b18ae37b8619ac5cfb0b35297b2ba9699bc08390 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
wdenke85390d2002-04-01 14:29:03 +00002/*
3 * (C) Copyright 2001
4 * Denis Peter, MPL AG Switzerland
wdenke85390d2002-04-01 14:29:03 +00005 */
6 #ifndef _SCSI_H
7 #define _SCSI_H
8
Simon Glass274e0b02020-05-10 11:39:56 -06009#include <asm/cache.h>
Marek Vasutb980e212023-08-14 01:50:00 +020010#include <bouncebuf.h>
Faiz Abbas20948092019-10-15 18:24:35 +053011#include <linux/dma-direction.h>
12
Simon Glass3ba929a2020-10-30 21:38:53 -060013struct udevice;
14
Simon Glasseccb24a2022-09-21 16:21:33 +020015/**
16 * struct scsi_cmd - information about a SCSI command to be processed
17 *
18 * @cmd: command
19 * @sense_buf: for request sense
20 * @status: SCSI Status
21 * @target: Target ID
22 * @lun: Target LUN
23 * @cmdlen: command len
24 * @datalen: Total data length
25 * @pdata: pointer to data
26 * @msgout: Messge out buffer (NOT USED)
27 * @msgin: Message in buffer
28 * @sensecmdlen: Sense command len
29 * @sensedatalen: Sense data len
30 * @sensecmd: Sense command
31 * @contr_stat: Controller Status
32 * @trans_bytes: tranfered bytes
33 * @priv: Private value
34 * @dma_dir: Direction of data structure
35 */
Simon Glass5fb559d2017-06-14 21:28:30 -060036struct scsi_cmd {
Simon Glasseccb24a2022-09-21 16:21:33 +020037 unsigned char cmd[16];
38 unsigned char sense_buf[64]
Puneet Saxena6c9bb602012-04-03 14:56:06 +053039 __attribute__((aligned(ARCH_DMA_MINALIGN)));
Simon Glasseccb24a2022-09-21 16:21:33 +020040 unsigned char status;
41 unsigned char target;
42 unsigned char lun;
43 unsigned char cmdlen;
44 unsigned long datalen;
45 unsigned char *pdata;
46 unsigned char msgout[12];
47 unsigned char msgin[12];
48 unsigned char sensecmdlen;
49 unsigned long sensedatalen;
50 unsigned char sensecmd[6];
51 unsigned long contr_stat;
52 unsigned long trans_bytes;
wdenke85390d2002-04-01 14:29:03 +000053
Simon Glasseccb24a2022-09-21 16:21:33 +020054 unsigned int priv;
55 enum dma_data_direction dma_dir;
Simon Glass5fb559d2017-06-14 21:28:30 -060056};
wdenke85390d2002-04-01 14:29:03 +000057
58/*-----------------------------------------------------------
59**
60** SCSI constants.
61**
62**-----------------------------------------------------------
63*/
64
65/*
66** Messages
67*/
68
69#define M_COMPLETE (0x00)
70#define M_EXTENDED (0x01)
71#define M_SAVE_DP (0x02)
72#define M_RESTORE_DP (0x03)
73#define M_DISCONNECT (0x04)
74#define M_ID_ERROR (0x05)
75#define M_ABORT (0x06)
76#define M_REJECT (0x07)
77#define M_NOOP (0x08)
78#define M_PARITY (0x09)
79#define M_LCOMPLETE (0x0a)
80#define M_FCOMPLETE (0x0b)
81#define M_RESET (0x0c)
82#define M_ABORT_TAG (0x0d)
83#define M_CLEAR_QUEUE (0x0e)
84#define M_INIT_REC (0x0f)
85#define M_REL_REC (0x10)
86#define M_TERMINATE (0x11)
87#define M_SIMPLE_TAG (0x20)
88#define M_HEAD_TAG (0x21)
89#define M_ORDERED_TAG (0x22)
90#define M_IGN_RESIDUE (0x23)
Wolfgang Denka1be4762008-05-20 16:00:29 +020091#define M_IDENTIFY (0x80)
wdenke85390d2002-04-01 14:29:03 +000092
93#define M_X_MODIFY_DP (0x00)
94#define M_X_SYNC_REQ (0x01)
95#define M_X_WIDE_REQ (0x03)
96#define M_X_PPR_REQ (0x04)
97
wdenke85390d2002-04-01 14:29:03 +000098/*
99** Status
100*/
101
102#define S_GOOD (0x00)
103#define S_CHECK_COND (0x02)
104#define S_COND_MET (0x04)
105#define S_BUSY (0x08)
106#define S_INT (0x10)
107#define S_INT_COND_MET (0x14)
108#define S_CONFLICT (0x18)
109#define S_TERMINATED (0x20)
110#define S_QUEUE_FULL (0x28)
111#define S_ILLEGAL (0xff)
112#define S_SENSE (0x80)
113
114/*
115 * Sense_keys
116 */
117
118#define SENSE_NO_SENSE 0x0
119#define SENSE_RECOVERED_ERROR 0x1
120#define SENSE_NOT_READY 0x2
121#define SENSE_MEDIUM_ERROR 0x3
122#define SENSE_HARDWARE_ERROR 0x4
123#define SENSE_ILLEGAL_REQUEST 0x5
124#define SENSE_UNIT_ATTENTION 0x6
125#define SENSE_DATA_PROTECT 0x7
126#define SENSE_BLANK_CHECK 0x8
127#define SENSE_VENDOR_SPECIFIC 0x9
128#define SENSE_COPY_ABORTED 0xA
129#define SENSE_ABORTED_COMMAND 0xB
130#define SENSE_VOLUME_OVERFLOW 0xD
131#define SENSE_MISCOMPARE 0xE
132
wdenke85390d2002-04-01 14:29:03 +0000133#define SCSI_CHANGE_DEF 0x40 /* Change Definition (Optional) */
134#define SCSI_COMPARE 0x39 /* Compare (O) */
135#define SCSI_COPY 0x18 /* Copy (O) */
136#define SCSI_COP_VERIFY 0x3A /* Copy and Verify (O) */
137#define SCSI_INQUIRY 0x12 /* Inquiry (MANDATORY) */
138#define SCSI_LOG_SELECT 0x4C /* Log Select (O) */
139#define SCSI_LOG_SENSE 0x4D /* Log Sense (O) */
140#define SCSI_MODE_SEL6 0x15 /* Mode Select 6-byte (Device Specific) */
141#define SCSI_MODE_SEL10 0x55 /* Mode Select 10-byte (Device Specific) */
142#define SCSI_MODE_SEN6 0x1A /* Mode Sense 6-byte (Device Specific) */
143#define SCSI_MODE_SEN10 0x5A /* Mode Sense 10-byte (Device Specific) */
144#define SCSI_READ_BUFF 0x3C /* Read Buffer (O) */
145#define SCSI_REQ_SENSE 0x03 /* Request Sense (MANDATORY) */
146#define SCSI_SEND_DIAG 0x1D /* Send Diagnostic (O) */
147#define SCSI_TST_U_RDY 0x00 /* Test Unit Ready (MANDATORY) */
148#define SCSI_WRITE_BUFF 0x3B /* Write Buffer (O) */
149/***************************************************************************
150 * %%% Commands Unique to Direct Access Devices %%%
151 ***************************************************************************/
152#define SCSI_COMPARE 0x39 /* Compare (O) */
153#define SCSI_FORMAT 0x04 /* Format Unit (MANDATORY) */
154#define SCSI_LCK_UN_CAC 0x36 /* Lock Unlock Cache (O) */
155#define SCSI_PREFETCH 0x34 /* Prefetch (O) */
156#define SCSI_MED_REMOVL 0x1E /* Prevent/Allow medium Removal (O) */
157#define SCSI_READ6 0x08 /* Read 6-byte (MANDATORY) */
158#define SCSI_READ10 0x28 /* Read 10-byte (MANDATORY) */
Mark Langsdorf5ed06fc2015-06-05 00:58:45 +0100159#define SCSI_READ16 0x48
wdenke85390d2002-04-01 14:29:03 +0000160#define SCSI_RD_CAPAC 0x25 /* Read Capacity (MANDATORY) */
Gabe Blackdd2c7342012-10-29 05:23:54 +0000161#define SCSI_RD_CAPAC10 SCSI_RD_CAPAC /* Read Capacity (10) */
162#define SCSI_RD_CAPAC16 0x9e /* Read Capacity (16) */
wdenke85390d2002-04-01 14:29:03 +0000163#define SCSI_RD_DEFECT 0x37 /* Read Defect Data (O) */
164#define SCSI_READ_LONG 0x3E /* Read Long (O) */
165#define SCSI_REASS_BLK 0x07 /* Reassign Blocks (O) */
166#define SCSI_RCV_DIAG 0x1C /* Receive Diagnostic Results (O) */
167#define SCSI_RELEASE 0x17 /* Release Unit (MANDATORY) */
168#define SCSI_REZERO 0x01 /* Rezero Unit (O) */
169#define SCSI_SRCH_DAT_E 0x31 /* Search Data Equal (O) */
170#define SCSI_SRCH_DAT_H 0x30 /* Search Data High (O) */
171#define SCSI_SRCH_DAT_L 0x32 /* Search Data Low (O) */
172#define SCSI_SEEK6 0x0B /* Seek 6-Byte (O) */
173#define SCSI_SEEK10 0x2B /* Seek 10-Byte (O) */
174#define SCSI_SEND_DIAG 0x1D /* Send Diagnostics (MANDATORY) */
175#define SCSI_SET_LIMIT 0x33 /* Set Limits (O) */
176#define SCSI_START_STP 0x1B /* Start/Stop Unit (O) */
177#define SCSI_SYNC_CACHE 0x35 /* Synchronize Cache (O) */
178#define SCSI_VERIFY 0x2F /* Verify (O) */
179#define SCSI_WRITE6 0x0A /* Write 6-Byte (MANDATORY) */
180#define SCSI_WRITE10 0x2A /* Write 10-Byte (MANDATORY) */
181#define SCSI_WRT_VERIFY 0x2E /* Write and Verify (O) */
182#define SCSI_WRITE_LONG 0x3F /* Write Long (O) */
183#define SCSI_WRITE_SAME 0x41 /* Write Same (O) */
184
Michal Simekc886f352016-09-08 15:06:45 +0200185/**
Simon Glasse6e05ad2022-09-21 16:21:35 +0200186 * enum scsi_cmd_phase - current phase of the SCSI protocol
187 *
188 * @SCSIPH_START: Start phase
189 * @SCSIPH_DATA: Data phase
190 * @SCSIPH_STATUS: Status phase
191 */
192enum scsi_cmd_phase {
193 SCSIPH_START,
194 SCSIPH_DATA,
195 SCSIPH_STATUS,
196};
197
198/**
Simon Glassbfe2c812022-09-21 16:21:43 +0200199 * struct scsi_inquiry_resp - holds a SCSI inquiry command
200 *
201 * @type; command type
202 * @flags; command flags
203 * @version; command version
204 * @data_format; data format
205 * @additional_len; additional data length
206 * @spare[3]; spare bytes
207 * @vendor[8]; vendor information
208 * @product[16]; production information
209 * @revision[4]; revision information
210 */
211struct scsi_inquiry_resp {
212 u8 type;
213 u8 flags;
214 u8 version;
215 u8 data_format;
216 u8 additional_len;
217 u8 spare[3];
218 char vendor[8];
219 char product[16];
220 char revision[4];
221};
222
223/**
224 * struct scsi_read_capacity_resp - holds the response to a read-capacity cmd
225 *
226 * @last_block_addr: Logical block address of last block
227 * @block_len: Length of each block in bytes
228 */
229struct scsi_read_capacity_resp {
230 u32 last_block_addr;
231 u32 block_len;
232};
233
234/**
235 * struct scsi_read10_req - holds a SCSI READ10 request
236 *
237 * @cmd; command type
238 * @lun_flags; LUN flags
239 * @lba; Logical block address to start reading from
240 * @spare; spare bytes
241 * @xfer_len: number of blocks to read
242 * @spare2: more spare bytes
243 */
244struct __packed scsi_read10_req {
245 u8 cmd;
246 u8 lun_flags;
247 u32 lba;
248 u8 spare;
249 u16 xfer_len;
250 u8 spare2[3];
251};
252
Simon Glass31b4f9e2022-10-20 18:22:55 -0600253/** struct scsi_write10_req - data for the write10 command */
254struct __packed scsi_write10_req {
255 u8 cmd;
256 u8 lun_flags;
257 u32 lba;
258 u8 spare;
259 u16 xfer_len;
260 u8 spare2[3];
261};
262
Simon Glassbfe2c812022-09-21 16:21:43 +0200263/**
Simon Glassb75b15b2020-12-03 16:55:23 -0700264 * struct scsi_plat - stores information about SCSI controller
Michal Simekc886f352016-09-08 15:06:45 +0200265 *
266 * @base: Controller base address
267 * @max_lun: Maximum number of logical units
268 * @max_id: Maximum number of target ids
Faiz Abbasb26c6142019-10-15 18:24:33 +0530269 * @max_bytes_per_req: Maximum number of bytes per read/write request
Michal Simekc886f352016-09-08 15:06:45 +0200270 */
Simon Glassb75b15b2020-12-03 16:55:23 -0700271struct scsi_plat {
Michal Simekc886f352016-09-08 15:06:45 +0200272 unsigned long base;
273 unsigned long max_lun;
274 unsigned long max_id;
Faiz Abbasb26c6142019-10-15 18:24:33 +0530275 unsigned long max_bytes_per_req;
Michal Simekc886f352016-09-08 15:06:45 +0200276};
Simon Glass3df498d2017-06-14 21:28:28 -0600277
Simon Glass28aa9742017-06-14 21:28:39 -0600278/* Operations for SCSI */
279struct scsi_ops {
280 /**
281 * exec() - execute a command
282 *
283 * @dev: SCSI bus
284 * @cmd: Command to execute
285 * @return 0 if OK, -ve on error
286 */
287 int (*exec)(struct udevice *dev, struct scsi_cmd *cmd);
288
289 /**
290 * bus_reset() - reset the bus
291 *
292 * @dev: SCSI bus to reset
293 * @return 0 if OK, -ve on error
294 */
295 int (*bus_reset)(struct udevice *dev);
Marek Vasutb980e212023-08-14 01:50:00 +0200296
297#if IS_ENABLED(CONFIG_BOUNCE_BUFFER)
298 /**
299 * buffer_aligned() - test memory alignment of block operation buffer
300 *
301 * Some devices have limited DMA capabilities and require that the
302 * buffers passed to them fit specific properties. This optional
303 * callback can be used to indicate whether a buffer alignment is
304 * suitable for the device DMA or not, and trigger use of generic
305 * bounce buffer implementation to help use of unsuitable buffers
306 * at the expense of performance degradation.
307 *
308 * @dev: Block device associated with the request
309 * @state: Bounce buffer state
310 * @return 1 if OK, 0 if unaligned
311 */
312 int (*buffer_aligned)(struct udevice *dev, struct bounce_buffer *state);
313#endif /* CONFIG_BOUNCE_BUFFER */
Simon Glass28aa9742017-06-14 21:28:39 -0600314};
315
Simon Glassc4dfa892017-06-14 21:28:43 -0600316#define scsi_get_ops(dev) ((struct scsi_ops *)(dev)->driver->ops)
317
318extern struct scsi_ops scsi_ops;
Michal Simekc886f352016-09-08 15:06:45 +0200319
Simon Glassc4dfa892017-06-14 21:28:43 -0600320/**
321 * scsi_exec() - execute a command
322 *
323 * @dev: SCSI bus
324 * @cmd: Command to execute
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100325 * Return: 0 if OK, -ve on error
Simon Glassc4dfa892017-06-14 21:28:43 -0600326 */
327int scsi_exec(struct udevice *dev, struct scsi_cmd *cmd);
328
329/**
330 * scsi_bus_reset() - reset the bus
331 *
332 * @dev: SCSI bus to reset
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100333 * Return: 0 if OK, -ve on error
Simon Glassc4dfa892017-06-14 21:28:43 -0600334 */
Simon Glass11b2b622017-06-14 21:28:40 -0600335int scsi_bus_reset(struct udevice *dev);
Simon Glass3df498d2017-06-14 21:28:28 -0600336
Simon Glass48228732017-06-14 21:28:41 -0600337/**
338 * scsi_scan() - Scan all SCSI controllers for available devices
339 *
340 * @vebose: true to show information about each device found
Simon Glass3df498d2017-06-14 21:28:28 -0600341 */
Simon Glass48228732017-06-14 21:28:41 -0600342int scsi_scan(bool verbose);
Simon Glass3df498d2017-06-14 21:28:28 -0600343
Simon Glass600d0012017-06-14 21:28:45 -0600344/**
345 * scsi_scan_dev() - scan a SCSI bus and create devices
346 *
347 * @dev: SCSI bus
348 * @verbose: true to show information about each device found
349 */
350int scsi_scan_dev(struct udevice *dev, bool verbose);
351
wdenke85390d2002-04-01 14:29:03 +0000352#define SCSI_IDENTIFY 0xC0 /* not used */
353
354/* Hardware errors */
355#define SCSI_SEL_TIME_OUT 0x00000101 /* Selection time out */
356#define SCSI_HNS_TIME_OUT 0x00000102 /* Handshake */
Wolfgang Denka1be4762008-05-20 16:00:29 +0200357#define SCSI_MA_TIME_OUT 0x00000103 /* Phase error */
wdenke85390d2002-04-01 14:29:03 +0000358#define SCSI_UNEXP_DIS 0x00000104 /* unexpected disconnect */
359
360#define SCSI_INT_STATE 0x00010000 /* unknown Interrupt number is stored in 16 LSB */
361
wdenke85390d2002-04-01 14:29:03 +0000362#endif /* _SCSI_H */