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