blob: 9efefea99bb90b5fc7e5c24d3fa0a6d45389c494 [file] [log] [blame]
Tom Rini83d290c2018-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 Glass90526e92020-05-10 11:39:56 -06009#include <asm/cache.h>
Faiz Abbas8fbac8e2019-10-15 18:24:35 +053010#include <linux/dma-direction.h>
11
Simon Glassce30e3f2022-01-31 07:49:36 -070012/* Fix this to the maximum */
13#define SCSI_MAX_DEVICE \
14 (CONFIG_SYS_SCSI_MAX_SCSI_ID * CONFIG_SYS_SCSI_MAX_LUN)
15
Simon Glass401d1c42020-10-30 21:38:53 -060016struct udevice;
17
Simon Glassa966fa92022-09-21 16:21:33 +020018/**
19 * struct scsi_cmd - information about a SCSI command to be processed
20 *
21 * @cmd: command
22 * @sense_buf: for request sense
23 * @status: SCSI Status
24 * @target: Target ID
25 * @lun: Target LUN
26 * @cmdlen: command len
27 * @datalen: Total data length
28 * @pdata: pointer to data
29 * @msgout: Messge out buffer (NOT USED)
30 * @msgin: Message in buffer
31 * @sensecmdlen: Sense command len
32 * @sensedatalen: Sense data len
33 * @sensecmd: Sense command
34 * @contr_stat: Controller Status
35 * @trans_bytes: tranfered bytes
36 * @priv: Private value
37 * @dma_dir: Direction of data structure
38 */
Simon Glassb9560ad2017-06-14 21:28:30 -060039struct scsi_cmd {
Simon Glassa966fa92022-09-21 16:21:33 +020040 unsigned char cmd[16];
41 unsigned char sense_buf[64]
Puneet Saxenaf5766132012-04-03 14:56:06 +053042 __attribute__((aligned(ARCH_DMA_MINALIGN)));
Simon Glassa966fa92022-09-21 16:21:33 +020043 unsigned char status;
44 unsigned char target;
45 unsigned char lun;
46 unsigned char cmdlen;
47 unsigned long datalen;
48 unsigned char *pdata;
49 unsigned char msgout[12];
50 unsigned char msgin[12];
51 unsigned char sensecmdlen;
52 unsigned long sensedatalen;
53 unsigned char sensecmd[6];
54 unsigned long contr_stat;
55 unsigned long trans_bytes;
wdenke85390d2002-04-01 14:29:03 +000056
Simon Glassa966fa92022-09-21 16:21:33 +020057 unsigned int priv;
58 enum dma_data_direction dma_dir;
Simon Glassb9560ad2017-06-14 21:28:30 -060059};
wdenke85390d2002-04-01 14:29:03 +000060
61/*-----------------------------------------------------------
62**
63** SCSI constants.
64**
65**-----------------------------------------------------------
66*/
67
68/*
69** Messages
70*/
71
72#define M_COMPLETE (0x00)
73#define M_EXTENDED (0x01)
74#define M_SAVE_DP (0x02)
75#define M_RESTORE_DP (0x03)
76#define M_DISCONNECT (0x04)
77#define M_ID_ERROR (0x05)
78#define M_ABORT (0x06)
79#define M_REJECT (0x07)
80#define M_NOOP (0x08)
81#define M_PARITY (0x09)
82#define M_LCOMPLETE (0x0a)
83#define M_FCOMPLETE (0x0b)
84#define M_RESET (0x0c)
85#define M_ABORT_TAG (0x0d)
86#define M_CLEAR_QUEUE (0x0e)
87#define M_INIT_REC (0x0f)
88#define M_REL_REC (0x10)
89#define M_TERMINATE (0x11)
90#define M_SIMPLE_TAG (0x20)
91#define M_HEAD_TAG (0x21)
92#define M_ORDERED_TAG (0x22)
93#define M_IGN_RESIDUE (0x23)
Wolfgang Denk53677ef2008-05-20 16:00:29 +020094#define M_IDENTIFY (0x80)
wdenke85390d2002-04-01 14:29:03 +000095
96#define M_X_MODIFY_DP (0x00)
97#define M_X_SYNC_REQ (0x01)
98#define M_X_WIDE_REQ (0x03)
99#define M_X_PPR_REQ (0x04)
100
101
102/*
103** Status
104*/
105
106#define S_GOOD (0x00)
107#define S_CHECK_COND (0x02)
108#define S_COND_MET (0x04)
109#define S_BUSY (0x08)
110#define S_INT (0x10)
111#define S_INT_COND_MET (0x14)
112#define S_CONFLICT (0x18)
113#define S_TERMINATED (0x20)
114#define S_QUEUE_FULL (0x28)
115#define S_ILLEGAL (0xff)
116#define S_SENSE (0x80)
117
118/*
119 * Sense_keys
120 */
121
122#define SENSE_NO_SENSE 0x0
123#define SENSE_RECOVERED_ERROR 0x1
124#define SENSE_NOT_READY 0x2
125#define SENSE_MEDIUM_ERROR 0x3
126#define SENSE_HARDWARE_ERROR 0x4
127#define SENSE_ILLEGAL_REQUEST 0x5
128#define SENSE_UNIT_ATTENTION 0x6
129#define SENSE_DATA_PROTECT 0x7
130#define SENSE_BLANK_CHECK 0x8
131#define SENSE_VENDOR_SPECIFIC 0x9
132#define SENSE_COPY_ABORTED 0xA
133#define SENSE_ABORTED_COMMAND 0xB
134#define SENSE_VOLUME_OVERFLOW 0xD
135#define SENSE_MISCOMPARE 0xE
136
137
138#define SCSI_CHANGE_DEF 0x40 /* Change Definition (Optional) */
139#define SCSI_COMPARE 0x39 /* Compare (O) */
140#define SCSI_COPY 0x18 /* Copy (O) */
141#define SCSI_COP_VERIFY 0x3A /* Copy and Verify (O) */
142#define SCSI_INQUIRY 0x12 /* Inquiry (MANDATORY) */
143#define SCSI_LOG_SELECT 0x4C /* Log Select (O) */
144#define SCSI_LOG_SENSE 0x4D /* Log Sense (O) */
145#define SCSI_MODE_SEL6 0x15 /* Mode Select 6-byte (Device Specific) */
146#define SCSI_MODE_SEL10 0x55 /* Mode Select 10-byte (Device Specific) */
147#define SCSI_MODE_SEN6 0x1A /* Mode Sense 6-byte (Device Specific) */
148#define SCSI_MODE_SEN10 0x5A /* Mode Sense 10-byte (Device Specific) */
149#define SCSI_READ_BUFF 0x3C /* Read Buffer (O) */
150#define SCSI_REQ_SENSE 0x03 /* Request Sense (MANDATORY) */
151#define SCSI_SEND_DIAG 0x1D /* Send Diagnostic (O) */
152#define SCSI_TST_U_RDY 0x00 /* Test Unit Ready (MANDATORY) */
153#define SCSI_WRITE_BUFF 0x3B /* Write Buffer (O) */
154/***************************************************************************
155 * %%% Commands Unique to Direct Access Devices %%%
156 ***************************************************************************/
157#define SCSI_COMPARE 0x39 /* Compare (O) */
158#define SCSI_FORMAT 0x04 /* Format Unit (MANDATORY) */
159#define SCSI_LCK_UN_CAC 0x36 /* Lock Unlock Cache (O) */
160#define SCSI_PREFETCH 0x34 /* Prefetch (O) */
161#define SCSI_MED_REMOVL 0x1E /* Prevent/Allow medium Removal (O) */
162#define SCSI_READ6 0x08 /* Read 6-byte (MANDATORY) */
163#define SCSI_READ10 0x28 /* Read 10-byte (MANDATORY) */
Mark Langsdorf2b42c932015-06-05 00:58:45 +0100164#define SCSI_READ16 0x48
wdenke85390d2002-04-01 14:29:03 +0000165#define SCSI_RD_CAPAC 0x25 /* Read Capacity (MANDATORY) */
Gabe Black19d1d412012-10-29 05:23:54 +0000166#define SCSI_RD_CAPAC10 SCSI_RD_CAPAC /* Read Capacity (10) */
167#define SCSI_RD_CAPAC16 0x9e /* Read Capacity (16) */
wdenke85390d2002-04-01 14:29:03 +0000168#define SCSI_RD_DEFECT 0x37 /* Read Defect Data (O) */
169#define SCSI_READ_LONG 0x3E /* Read Long (O) */
170#define SCSI_REASS_BLK 0x07 /* Reassign Blocks (O) */
171#define SCSI_RCV_DIAG 0x1C /* Receive Diagnostic Results (O) */
172#define SCSI_RELEASE 0x17 /* Release Unit (MANDATORY) */
173#define SCSI_REZERO 0x01 /* Rezero Unit (O) */
174#define SCSI_SRCH_DAT_E 0x31 /* Search Data Equal (O) */
175#define SCSI_SRCH_DAT_H 0x30 /* Search Data High (O) */
176#define SCSI_SRCH_DAT_L 0x32 /* Search Data Low (O) */
177#define SCSI_SEEK6 0x0B /* Seek 6-Byte (O) */
178#define SCSI_SEEK10 0x2B /* Seek 10-Byte (O) */
179#define SCSI_SEND_DIAG 0x1D /* Send Diagnostics (MANDATORY) */
180#define SCSI_SET_LIMIT 0x33 /* Set Limits (O) */
181#define SCSI_START_STP 0x1B /* Start/Stop Unit (O) */
182#define SCSI_SYNC_CACHE 0x35 /* Synchronize Cache (O) */
183#define SCSI_VERIFY 0x2F /* Verify (O) */
184#define SCSI_WRITE6 0x0A /* Write 6-Byte (MANDATORY) */
185#define SCSI_WRITE10 0x2A /* Write 10-Byte (MANDATORY) */
186#define SCSI_WRT_VERIFY 0x2E /* Write and Verify (O) */
187#define SCSI_WRITE_LONG 0x3F /* Write Long (O) */
188#define SCSI_WRITE_SAME 0x41 /* Write Same (O) */
189
Michal Simeke8a016b2016-09-08 15:06:45 +0200190/**
Simon Glass0e0b9e92022-09-21 16:21:35 +0200191 * enum scsi_cmd_phase - current phase of the SCSI protocol
192 *
193 * @SCSIPH_START: Start phase
194 * @SCSIPH_DATA: Data phase
195 * @SCSIPH_STATUS: Status phase
196 */
197enum scsi_cmd_phase {
198 SCSIPH_START,
199 SCSIPH_DATA,
200 SCSIPH_STATUS,
201};
202
203/**
Simon Glass1ac42902022-09-21 16:21:43 +0200204 * struct scsi_inquiry_resp - holds a SCSI inquiry command
205 *
206 * @type; command type
207 * @flags; command flags
208 * @version; command version
209 * @data_format; data format
210 * @additional_len; additional data length
211 * @spare[3]; spare bytes
212 * @vendor[8]; vendor information
213 * @product[16]; production information
214 * @revision[4]; revision information
215 */
216struct scsi_inquiry_resp {
217 u8 type;
218 u8 flags;
219 u8 version;
220 u8 data_format;
221 u8 additional_len;
222 u8 spare[3];
223 char vendor[8];
224 char product[16];
225 char revision[4];
226};
227
228/**
229 * struct scsi_read_capacity_resp - holds the response to a read-capacity cmd
230 *
231 * @last_block_addr: Logical block address of last block
232 * @block_len: Length of each block in bytes
233 */
234struct scsi_read_capacity_resp {
235 u32 last_block_addr;
236 u32 block_len;
237};
238
239/**
240 * struct scsi_read10_req - holds a SCSI READ10 request
241 *
242 * @cmd; command type
243 * @lun_flags; LUN flags
244 * @lba; Logical block address to start reading from
245 * @spare; spare bytes
246 * @xfer_len: number of blocks to read
247 * @spare2: more spare bytes
248 */
249struct __packed scsi_read10_req {
250 u8 cmd;
251 u8 lun_flags;
252 u32 lba;
253 u8 spare;
254 u16 xfer_len;
255 u8 spare2[3];
256};
257
Simon Glass2ff3db32022-10-20 18:22:55 -0600258/** struct scsi_write10_req - data for the write10 command */
259struct __packed scsi_write10_req {
260 u8 cmd;
261 u8 lun_flags;
262 u32 lba;
263 u8 spare;
264 u16 xfer_len;
265 u8 spare2[3];
266};
267
Simon Glass1ac42902022-09-21 16:21:43 +0200268/**
Simon Glass8a8d24b2020-12-03 16:55:23 -0700269 * struct scsi_plat - stores information about SCSI controller
Michal Simeke8a016b2016-09-08 15:06:45 +0200270 *
271 * @base: Controller base address
272 * @max_lun: Maximum number of logical units
273 * @max_id: Maximum number of target ids
Faiz Abbas4ff57282019-10-15 18:24:33 +0530274 * @max_bytes_per_req: Maximum number of bytes per read/write request
Michal Simeke8a016b2016-09-08 15:06:45 +0200275 */
Simon Glass8a8d24b2020-12-03 16:55:23 -0700276struct scsi_plat {
Michal Simeke8a016b2016-09-08 15:06:45 +0200277 unsigned long base;
278 unsigned long max_lun;
279 unsigned long max_id;
Faiz Abbas4ff57282019-10-15 18:24:33 +0530280 unsigned long max_bytes_per_req;
Michal Simeke8a016b2016-09-08 15:06:45 +0200281};
Simon Glass04368242017-06-14 21:28:28 -0600282
Simon Glass322f73f2017-06-14 21:28:39 -0600283/* Operations for SCSI */
284struct scsi_ops {
285 /**
286 * exec() - execute a command
287 *
288 * @dev: SCSI bus
289 * @cmd: Command to execute
290 * @return 0 if OK, -ve on error
291 */
292 int (*exec)(struct udevice *dev, struct scsi_cmd *cmd);
293
294 /**
295 * bus_reset() - reset the bus
296 *
297 * @dev: SCSI bus to reset
298 * @return 0 if OK, -ve on error
299 */
300 int (*bus_reset)(struct udevice *dev);
301};
302
Simon Glassf6ab5a92017-06-14 21:28:43 -0600303#define scsi_get_ops(dev) ((struct scsi_ops *)(dev)->driver->ops)
Michal Simeke8a016b2016-09-08 15:06:45 +0200304
Simon Glassf6ab5a92017-06-14 21:28:43 -0600305extern struct scsi_ops scsi_ops;
306
307/**
308 * scsi_exec() - execute a command
309 *
310 * @dev: SCSI bus
311 * @cmd: Command to execute
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100312 * Return: 0 if OK, -ve on error
Simon Glassf6ab5a92017-06-14 21:28:43 -0600313 */
314int scsi_exec(struct udevice *dev, struct scsi_cmd *cmd);
315
316/**
317 * scsi_bus_reset() - reset the bus
318 *
319 * @dev: SCSI bus to reset
Heinrich Schuchardt185f8122022-01-19 18:05:50 +0100320 * Return: 0 if OK, -ve on error
Simon Glassf6ab5a92017-06-14 21:28:43 -0600321 */
Simon Glass4682c8a2017-06-14 21:28:40 -0600322int scsi_bus_reset(struct udevice *dev);
Simon Glass04368242017-06-14 21:28:28 -0600323
Simon Glass8eab1a52017-06-14 21:28:41 -0600324/**
325 * scsi_scan() - Scan all SCSI controllers for available devices
326 *
327 * @vebose: true to show information about each device found
Simon Glass04368242017-06-14 21:28:28 -0600328 */
Simon Glass8eab1a52017-06-14 21:28:41 -0600329int scsi_scan(bool verbose);
Simon Glass04368242017-06-14 21:28:28 -0600330
Simon Glass5c561762017-06-14 21:28:45 -0600331/**
332 * scsi_scan_dev() - scan a SCSI bus and create devices
333 *
334 * @dev: SCSI bus
335 * @verbose: true to show information about each device found
336 */
337int scsi_scan_dev(struct udevice *dev, bool verbose);
338
Simon Glassf6ab5a92017-06-14 21:28:43 -0600339#ifndef CONFIG_DM_SCSI
340void scsi_low_level_init(int busdevfunc);
341void scsi_init(void);
342#endif
343
wdenke85390d2002-04-01 14:29:03 +0000344#define SCSI_IDENTIFY 0xC0 /* not used */
345
346/* Hardware errors */
347#define SCSI_SEL_TIME_OUT 0x00000101 /* Selection time out */
348#define SCSI_HNS_TIME_OUT 0x00000102 /* Handshake */
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200349#define SCSI_MA_TIME_OUT 0x00000103 /* Phase error */
wdenke85390d2002-04-01 14:29:03 +0000350#define SCSI_UNEXP_DIS 0x00000104 /* unexpected disconnect */
351
352#define SCSI_INT_STATE 0x00010000 /* unknown Interrupt number is stored in 16 LSB */
353
wdenke85390d2002-04-01 14:29:03 +0000354#endif /* _SCSI_H */