1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _SCSI_SCSI_CMND_H
3 #define _SCSI_SCSI_CMND_H
4
5 #include <linux/dma-mapping.h>
6 #include <linux/blkdev.h>
7 #include <linux/t10-pi.h>
8 #include <linux/list.h>
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/scatterlist.h>
12 #include <scsi/scsi_device.h>
13 #include <linux/android_kabi.h>
14
15 struct Scsi_Host;
16
17 /*
18 * MAX_COMMAND_SIZE is:
19 * The longest fixed-length SCSI CDB as per the SCSI standard.
20 * fixed-length means: commands that their size can be determined
21 * by their opcode and the CDB does not carry a length specifier, (unlike
22 * the VARIABLE_LENGTH_CMD(0x7f) command). This is actually not exactly
23 * true and the SCSI standard also defines extended commands and
24 * vendor specific commands that can be bigger than 16 bytes. The kernel
25 * will support these using the same infrastructure used for VARLEN CDB's.
26 * So in effect MAX_COMMAND_SIZE means the maximum size command scsi-ml
27 * supports without specifying a cmd_len by ULD's
28 */
29 #define MAX_COMMAND_SIZE 16
30
31 struct scsi_data_buffer {
32 struct sg_table table;
33 unsigned length;
34 };
35
36 /* embedded in scsi_cmnd */
37 struct scsi_pointer {
38 char *ptr; /* data pointer */
39 int this_residual; /* left in this buffer */
40 struct scatterlist *buffer; /* which buffer */
41 int buffers_residual; /* how many buffers left */
42
43 dma_addr_t dma_handle;
44
45 volatile int Status;
46 volatile int Message;
47 volatile int have_data_in;
48 volatile int sent_command;
49 volatile int phase;
50 };
51
52 /* for scmd->flags */
53 #define SCMD_TAGGED (1 << 0)
54 #define SCMD_INITIALIZED (1 << 1)
55 #define SCMD_LAST (1 << 2)
56 /*
57 * libata uses SCSI EH to fetch sense data for successful commands.
58 * SCSI EH should not overwrite scmd->result when SCMD_FORCE_EH_SUCCESS is set.
59 */
60 #define SCMD_FORCE_EH_SUCCESS (1 << 3)
61 #define SCMD_FAIL_IF_RECOVERING (1 << 4)
62 /* flags preserved across unprep / reprep */
63 #define SCMD_PRESERVED_FLAGS (SCMD_INITIALIZED | SCMD_FAIL_IF_RECOVERING)
64
65 /* for scmd->state */
66 #define SCMD_STATE_COMPLETE 0
67 #define SCMD_STATE_INFLIGHT 1
68
69 enum scsi_cmnd_submitter {
70 SUBMITTED_BY_BLOCK_LAYER = 0,
71 SUBMITTED_BY_SCSI_ERROR_HANDLER = 1,
72 SUBMITTED_BY_SCSI_RESET_IOCTL = 2,
73 } __packed;
74
75 struct scsi_cmnd {
76 struct scsi_device *device;
77 struct list_head eh_entry; /* entry for the host eh_abort_list/eh_cmd_q */
78 struct delayed_work abort_work;
79
80 struct rcu_head rcu;
81
82 int eh_eflags; /* Used by error handlr */
83
84 int budget_token;
85
86 /*
87 * This is set to jiffies as it was when the command was first
88 * allocated. It is used to time how long the command has
89 * been outstanding
90 */
91 unsigned long jiffies_at_alloc;
92
93 int retries;
94 int allowed;
95
96 unsigned char prot_op;
97 unsigned char prot_type;
98 unsigned char prot_flags;
99 enum scsi_cmnd_submitter submitter;
100
101 unsigned short cmd_len;
102 enum dma_data_direction sc_data_direction;
103
104 unsigned char cmnd[32]; /* SCSI CDB */
105
106 /* These elements define the operation we ultimately want to perform */
107 struct scsi_data_buffer sdb;
108 struct scsi_data_buffer *prot_sdb;
109
110 unsigned underflow; /* Return error if less than
111 this amount is transferred */
112
113 unsigned transfersize; /* How much we are guaranteed to
114 transfer with each SCSI transfer
115 (ie, between disconnect /
116 reconnects. Probably == sector
117 size */
118 unsigned resid_len; /* residual count */
119 unsigned sense_len;
120 unsigned char *sense_buffer;
121 /* obtained by REQUEST SENSE when
122 * CHECK CONDITION is received on original
123 * command (auto-sense). Length must be
124 * SCSI_SENSE_BUFFERSIZE bytes. */
125
126 int flags; /* Command flags */
127 unsigned long state; /* Command completion state */
128
129 unsigned int extra_len; /* length of alignment and padding */
130
131 /*
132 * The fields below can be modified by the LLD but the fields above
133 * must not be modified.
134 */
135
136 unsigned char *host_scribble; /* The host adapter is allowed to
137 * call scsi_malloc and get some memory
138 * and hang it here. The host adapter
139 * is also expected to call scsi_free
140 * to release this memory. (The memory
141 * obtained by scsi_malloc is guaranteed
142 * to be at an address < 16Mb). */
143
144 int result; /* Status code from lower level driver */
145
146 ANDROID_KABI_RESERVE(1);
147 ANDROID_KABI_RESERVE(2);
148 ANDROID_KABI_RESERVE(3);
149 ANDROID_KABI_RESERVE(4);
150 };
151
152 /* Variant of blk_mq_rq_from_pdu() that verifies the type of its argument. */
scsi_cmd_to_rq(struct scsi_cmnd * scmd)153 static inline struct request *scsi_cmd_to_rq(struct scsi_cmnd *scmd)
154 {
155 return blk_mq_rq_from_pdu(scmd);
156 }
157
158 /*
159 * Return the driver private allocation behind the command.
160 * Only works if cmd_size is set in the host template.
161 */
scsi_cmd_priv(struct scsi_cmnd * cmd)162 static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
163 {
164 return cmd + 1;
165 }
166
167 void scsi_done(struct scsi_cmnd *cmd);
168 void scsi_done_direct(struct scsi_cmnd *cmd);
169
170 extern void scsi_finish_command(struct scsi_cmnd *cmd);
171
172 extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
173 size_t *offset, size_t *len);
174 extern void scsi_kunmap_atomic_sg(void *virt);
175
176 blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd);
177 void scsi_free_sgtables(struct scsi_cmnd *cmd);
178
179 #ifdef CONFIG_SCSI_DMA
180 extern int scsi_dma_map(struct scsi_cmnd *cmd);
181 extern void scsi_dma_unmap(struct scsi_cmnd *cmd);
182 #else /* !CONFIG_SCSI_DMA */
scsi_dma_map(struct scsi_cmnd * cmd)183 static inline int scsi_dma_map(struct scsi_cmnd *cmd) { return -ENOSYS; }
scsi_dma_unmap(struct scsi_cmnd * cmd)184 static inline void scsi_dma_unmap(struct scsi_cmnd *cmd) { }
185 #endif /* !CONFIG_SCSI_DMA */
186
scsi_sg_count(struct scsi_cmnd * cmd)187 static inline unsigned scsi_sg_count(struct scsi_cmnd *cmd)
188 {
189 return cmd->sdb.table.nents;
190 }
191
scsi_sglist(struct scsi_cmnd * cmd)192 static inline struct scatterlist *scsi_sglist(struct scsi_cmnd *cmd)
193 {
194 return cmd->sdb.table.sgl;
195 }
196
scsi_bufflen(struct scsi_cmnd * cmd)197 static inline unsigned scsi_bufflen(struct scsi_cmnd *cmd)
198 {
199 return cmd->sdb.length;
200 }
201
scsi_set_resid(struct scsi_cmnd * cmd,unsigned int resid)202 static inline void scsi_set_resid(struct scsi_cmnd *cmd, unsigned int resid)
203 {
204 cmd->resid_len = resid;
205 }
206
scsi_get_resid(struct scsi_cmnd * cmd)207 static inline unsigned int scsi_get_resid(struct scsi_cmnd *cmd)
208 {
209 return cmd->resid_len;
210 }
211
212 #define scsi_for_each_sg(cmd, sg, nseg, __i) \
213 for_each_sg(scsi_sglist(cmd), sg, nseg, __i)
214
scsi_sg_copy_from_buffer(struct scsi_cmnd * cmd,const void * buf,int buflen)215 static inline int scsi_sg_copy_from_buffer(struct scsi_cmnd *cmd,
216 const void *buf, int buflen)
217 {
218 return sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
219 buf, buflen);
220 }
221
scsi_sg_copy_to_buffer(struct scsi_cmnd * cmd,void * buf,int buflen)222 static inline int scsi_sg_copy_to_buffer(struct scsi_cmnd *cmd,
223 void *buf, int buflen)
224 {
225 return sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
226 buf, buflen);
227 }
228
scsi_get_sector(struct scsi_cmnd * scmd)229 static inline sector_t scsi_get_sector(struct scsi_cmnd *scmd)
230 {
231 return blk_rq_pos(scsi_cmd_to_rq(scmd));
232 }
233
scsi_get_lba(struct scsi_cmnd * scmd)234 static inline sector_t scsi_get_lba(struct scsi_cmnd *scmd)
235 {
236 unsigned int shift = ilog2(scmd->device->sector_size) - SECTOR_SHIFT;
237
238 return blk_rq_pos(scsi_cmd_to_rq(scmd)) >> shift;
239 }
240
scsi_logical_block_count(struct scsi_cmnd * scmd)241 static inline unsigned int scsi_logical_block_count(struct scsi_cmnd *scmd)
242 {
243 unsigned int shift = ilog2(scmd->device->sector_size);
244
245 return blk_rq_bytes(scsi_cmd_to_rq(scmd)) >> shift;
246 }
247
248 /*
249 * The operations below are hints that tell the controller driver how
250 * to handle I/Os with DIF or similar types of protection information.
251 */
252 enum scsi_prot_operations {
253 /* Normal I/O */
254 SCSI_PROT_NORMAL = 0,
255
256 /* OS-HBA: Protected, HBA-Target: Unprotected */
257 SCSI_PROT_READ_INSERT,
258 SCSI_PROT_WRITE_STRIP,
259
260 /* OS-HBA: Unprotected, HBA-Target: Protected */
261 SCSI_PROT_READ_STRIP,
262 SCSI_PROT_WRITE_INSERT,
263
264 /* OS-HBA: Protected, HBA-Target: Protected */
265 SCSI_PROT_READ_PASS,
266 SCSI_PROT_WRITE_PASS,
267 };
268
scsi_set_prot_op(struct scsi_cmnd * scmd,unsigned char op)269 static inline void scsi_set_prot_op(struct scsi_cmnd *scmd, unsigned char op)
270 {
271 scmd->prot_op = op;
272 }
273
scsi_get_prot_op(struct scsi_cmnd * scmd)274 static inline unsigned char scsi_get_prot_op(struct scsi_cmnd *scmd)
275 {
276 return scmd->prot_op;
277 }
278
279 enum scsi_prot_flags {
280 SCSI_PROT_TRANSFER_PI = 1 << 0,
281 SCSI_PROT_GUARD_CHECK = 1 << 1,
282 SCSI_PROT_REF_CHECK = 1 << 2,
283 SCSI_PROT_REF_INCREMENT = 1 << 3,
284 SCSI_PROT_IP_CHECKSUM = 1 << 4,
285 };
286
287 /*
288 * The controller usually does not know anything about the target it
289 * is communicating with. However, when DIX is enabled the controller
290 * must be know target type so it can verify the protection
291 * information passed along with the I/O.
292 */
293 enum scsi_prot_target_type {
294 SCSI_PROT_DIF_TYPE0 = 0,
295 SCSI_PROT_DIF_TYPE1,
296 SCSI_PROT_DIF_TYPE2,
297 SCSI_PROT_DIF_TYPE3,
298 };
299
scsi_set_prot_type(struct scsi_cmnd * scmd,unsigned char type)300 static inline void scsi_set_prot_type(struct scsi_cmnd *scmd, unsigned char type)
301 {
302 scmd->prot_type = type;
303 }
304
scsi_get_prot_type(struct scsi_cmnd * scmd)305 static inline unsigned char scsi_get_prot_type(struct scsi_cmnd *scmd)
306 {
307 return scmd->prot_type;
308 }
309
scsi_prot_ref_tag(struct scsi_cmnd * scmd)310 static inline u32 scsi_prot_ref_tag(struct scsi_cmnd *scmd)
311 {
312 struct request *rq = blk_mq_rq_from_pdu(scmd);
313
314 return t10_pi_ref_tag(rq);
315 }
316
scsi_prot_interval(struct scsi_cmnd * scmd)317 static inline unsigned int scsi_prot_interval(struct scsi_cmnd *scmd)
318 {
319 return scmd->device->sector_size;
320 }
321
scsi_prot_sg_count(struct scsi_cmnd * cmd)322 static inline unsigned scsi_prot_sg_count(struct scsi_cmnd *cmd)
323 {
324 return cmd->prot_sdb ? cmd->prot_sdb->table.nents : 0;
325 }
326
scsi_prot_sglist(struct scsi_cmnd * cmd)327 static inline struct scatterlist *scsi_prot_sglist(struct scsi_cmnd *cmd)
328 {
329 return cmd->prot_sdb ? cmd->prot_sdb->table.sgl : NULL;
330 }
331
scsi_prot(struct scsi_cmnd * cmd)332 static inline struct scsi_data_buffer *scsi_prot(struct scsi_cmnd *cmd)
333 {
334 return cmd->prot_sdb;
335 }
336
337 #define scsi_for_each_prot_sg(cmd, sg, nseg, __i) \
338 for_each_sg(scsi_prot_sglist(cmd), sg, nseg, __i)
339
set_status_byte(struct scsi_cmnd * cmd,char status)340 static inline void set_status_byte(struct scsi_cmnd *cmd, char status)
341 {
342 cmd->result = (cmd->result & 0xffffff00) | status;
343 }
344
get_status_byte(struct scsi_cmnd * cmd)345 static inline u8 get_status_byte(struct scsi_cmnd *cmd)
346 {
347 return cmd->result & 0xff;
348 }
349
set_host_byte(struct scsi_cmnd * cmd,char status)350 static inline void set_host_byte(struct scsi_cmnd *cmd, char status)
351 {
352 cmd->result = (cmd->result & 0xff00ffff) | (status << 16);
353 }
354
get_host_byte(struct scsi_cmnd * cmd)355 static inline u8 get_host_byte(struct scsi_cmnd *cmd)
356 {
357 return (cmd->result >> 16) & 0xff;
358 }
359
360 /**
361 * scsi_msg_to_host_byte() - translate message byte
362 * @cmd: the SCSI command
363 * @msg: the SCSI parallel message byte to translate
364 *
365 * Translate the SCSI parallel message byte to a matching
366 * host byte setting. A message of COMMAND_COMPLETE indicates
367 * a successful command execution, any other message indicate
368 * an error. As the messages themselves only have a meaning
369 * for the SCSI parallel protocol this function translates
370 * them into a matching host byte value for SCSI EH.
371 */
scsi_msg_to_host_byte(struct scsi_cmnd * cmd,u8 msg)372 static inline void scsi_msg_to_host_byte(struct scsi_cmnd *cmd, u8 msg)
373 {
374 switch (msg) {
375 case COMMAND_COMPLETE:
376 break;
377 case ABORT_TASK_SET:
378 set_host_byte(cmd, DID_ABORT);
379 break;
380 case TARGET_RESET:
381 set_host_byte(cmd, DID_RESET);
382 break;
383 default:
384 set_host_byte(cmd, DID_ERROR);
385 break;
386 }
387 }
388
scsi_transfer_length(struct scsi_cmnd * scmd)389 static inline unsigned scsi_transfer_length(struct scsi_cmnd *scmd)
390 {
391 unsigned int xfer_len = scmd->sdb.length;
392 unsigned int prot_interval = scsi_prot_interval(scmd);
393
394 if (scmd->prot_flags & SCSI_PROT_TRANSFER_PI)
395 xfer_len += (xfer_len >> ilog2(prot_interval)) * 8;
396
397 return xfer_len;
398 }
399
400 extern void scsi_build_sense(struct scsi_cmnd *scmd, int desc,
401 u8 key, u8 asc, u8 ascq);
402
403 struct request *scsi_alloc_request(struct request_queue *q, blk_opf_t opf,
404 blk_mq_req_flags_t flags);
405
406 #endif /* _SCSI_SCSI_CMND_H */
407