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