1
2#ifndef _SCSI_SCSI_CMND_H
3#define _SCSI_SCSI_CMND_H
4
5#include <linux/rh_kabi.h>
6
7#include <linux/dma-mapping.h>
8#include <linux/blkdev.h>
9#include <linux/t10-pi.h>
10#include <linux/list.h>
11#include <linux/types.h>
12#include <linux/timer.h>
13#include <linux/scatterlist.h>
14#include <scsi/scsi_device.h>
15#include RH_KABI_HIDE_INCLUDE(<scsi/scsi_host.h>)
16#include <scsi/scsi_request.h>
17
18struct Scsi_Host;
19struct scsi_driver;
20
21
22
23
24
25
26
27
28
29
30
31
32
33#define MAX_COMMAND_SIZE 16
34#if (MAX_COMMAND_SIZE > BLK_MAX_CDB)
35# error MAX_COMMAND_SIZE can not be bigger than BLK_MAX_CDB
36#endif
37
38struct scsi_data_buffer {
39 struct sg_table table;
40 unsigned length;
41 int resid;
42};
43
44
45struct scsi_pointer {
46 char *ptr;
47 int this_residual;
48 struct scatterlist *buffer;
49 int buffers_residual;
50
51 dma_addr_t dma_handle;
52
53 volatile int Status;
54 volatile int Message;
55 volatile int have_data_in;
56 volatile int sent_command;
57 volatile int phase;
58};
59
60
61#define SCMD_TAGGED (1 << 0)
62#define SCMD_UNCHECKED_ISA_DMA (1 << 1)
63#define SCMD_INITIALIZED (1 << 2)
64#define SCMD_LAST (1 << 3)
65
66#define SCMD_PRESERVED_FLAGS (SCMD_UNCHECKED_ISA_DMA | SCMD_INITIALIZED)
67
68
69#define SCMD_STATE_COMPLETE 0
70#define SCMD_STATE_INFLIGHT 1
71
72struct scsi_cmnd {
73 struct scsi_request req;
74 struct scsi_device *device;
75 struct list_head list;
76 struct list_head eh_entry;
77 struct delayed_work abort_work;
78
79 struct rcu_head rcu;
80
81 int eh_eflags;
82
83
84
85
86
87
88
89
90
91 unsigned long serial_number;
92
93
94
95
96
97
98 unsigned long jiffies_at_alloc;
99
100 int retries;
101 int allowed;
102
103 unsigned char prot_op;
104 unsigned char prot_type;
105 unsigned char prot_flags;
106
107 unsigned short cmd_len;
108 enum dma_data_direction sc_data_direction;
109
110
111 unsigned char *cmnd;
112
113
114
115 struct scsi_data_buffer sdb;
116 struct scsi_data_buffer *prot_sdb;
117
118 unsigned underflow;
119
120
121 unsigned transfersize;
122
123
124
125
126
127 struct request *request;
128
129
130 unsigned char *sense_buffer;
131
132
133
134
135
136
137
138 void (*scsi_done) (struct scsi_cmnd *);
139
140
141
142
143
144 struct scsi_pointer SCp;
145
146 unsigned char *host_scribble;
147
148
149
150
151
152
153
154 int result;
155 int flags;
156 unsigned long state;
157
158 unsigned char tag;
159
160
161
162
163
164
165 RH_KABI_USE(1,int budget_token)
166 RH_KABI_RESERVE(2)
167 RH_KABI_RESERVE(3)
168 RH_KABI_RESERVE(4)
169};
170
171
172
173
174
175static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
176{
177 return cmd + 1;
178}
179
180
181static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
182{
183 return *(struct scsi_driver **)cmd->request->rq_disk->private_data;
184}
185
186extern void scsi_put_command(struct scsi_cmnd *);
187extern void scsi_finish_command(struct scsi_cmnd *cmd);
188
189extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
190 size_t *offset, size_t *len);
191extern void scsi_kunmap_atomic_sg(void *virt);
192
193blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd);
194void scsi_free_sgtables(struct scsi_cmnd *cmd);
195
196#ifdef CONFIG_SCSI_DMA
197extern int scsi_dma_map(struct scsi_cmnd *cmd);
198extern void scsi_dma_unmap(struct scsi_cmnd *cmd);
199#else
200static inline int scsi_dma_map(struct scsi_cmnd *cmd) { return -ENOSYS; }
201static inline void scsi_dma_unmap(struct scsi_cmnd *cmd) { }
202#endif
203
204static inline unsigned scsi_sg_count(struct scsi_cmnd *cmd)
205{
206 return cmd->sdb.table.nents;
207}
208
209static inline struct scatterlist *scsi_sglist(struct scsi_cmnd *cmd)
210{
211 return cmd->sdb.table.sgl;
212}
213
214static inline unsigned scsi_bufflen(struct scsi_cmnd *cmd)
215{
216 return cmd->sdb.length;
217}
218
219static inline void scsi_set_resid(struct scsi_cmnd *cmd, int resid)
220{
221 cmd->sdb.resid = resid;
222}
223
224static inline int scsi_get_resid(struct scsi_cmnd *cmd)
225{
226 return cmd->sdb.resid;
227}
228
229#define scsi_for_each_sg(cmd, sg, nseg, __i) \
230 for_each_sg(scsi_sglist(cmd), sg, nseg, __i)
231
232static inline int scsi_sg_copy_from_buffer(struct scsi_cmnd *cmd,
233 void *buf, int buflen)
234{
235 return sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
236 buf, buflen);
237}
238
239static inline int scsi_sg_copy_to_buffer(struct scsi_cmnd *cmd,
240 void *buf, int buflen)
241{
242 return sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
243 buf, buflen);
244}
245
246
247
248
249
250enum scsi_prot_operations {
251
252 SCSI_PROT_NORMAL = 0,
253
254
255 SCSI_PROT_READ_INSERT,
256 SCSI_PROT_WRITE_STRIP,
257
258
259 SCSI_PROT_READ_STRIP,
260 SCSI_PROT_WRITE_INSERT,
261
262
263 SCSI_PROT_READ_PASS,
264 SCSI_PROT_WRITE_PASS,
265};
266
267static inline void scsi_set_prot_op(struct scsi_cmnd *scmd, unsigned char op)
268{
269 scmd->prot_op = op;
270}
271
272static inline unsigned char scsi_get_prot_op(struct scsi_cmnd *scmd)
273{
274 return scmd->prot_op;
275}
276
277enum scsi_prot_flags {
278 SCSI_PROT_TRANSFER_PI = 1 << 0,
279 SCSI_PROT_GUARD_CHECK = 1 << 1,
280 SCSI_PROT_REF_CHECK = 1 << 2,
281 SCSI_PROT_REF_INCREMENT = 1 << 3,
282 SCSI_PROT_IP_CHECKSUM = 1 << 4,
283};
284
285
286
287
288
289
290
291enum scsi_prot_target_type {
292 SCSI_PROT_DIF_TYPE0 = 0,
293 SCSI_PROT_DIF_TYPE1,
294 SCSI_PROT_DIF_TYPE2,
295 SCSI_PROT_DIF_TYPE3,
296};
297
298static inline void scsi_set_prot_type(struct scsi_cmnd *scmd, unsigned char type)
299{
300 scmd->prot_type = type;
301}
302
303static inline unsigned char scsi_get_prot_type(struct scsi_cmnd *scmd)
304{
305 return scmd->prot_type;
306}
307
308static inline sector_t scsi_get_lba(struct scsi_cmnd *scmd)
309{
310 return blk_rq_pos(scmd->request);
311}
312
313static inline unsigned int scsi_prot_interval(struct scsi_cmnd *scmd)
314{
315 return scmd->device->sector_size;
316}
317
318static inline unsigned scsi_prot_sg_count(struct scsi_cmnd *cmd)
319{
320 return cmd->prot_sdb ? cmd->prot_sdb->table.nents : 0;
321}
322
323static inline struct scatterlist *scsi_prot_sglist(struct scsi_cmnd *cmd)
324{
325 return cmd->prot_sdb ? cmd->prot_sdb->table.sgl : NULL;
326}
327
328static inline struct scsi_data_buffer *scsi_prot(struct scsi_cmnd *cmd)
329{
330 return cmd->prot_sdb;
331}
332
333#define scsi_for_each_prot_sg(cmd, sg, nseg, __i) \
334 for_each_sg(scsi_prot_sglist(cmd), sg, nseg, __i)
335
336static inline void set_status_byte(struct scsi_cmnd *cmd, char status)
337{
338 cmd->result = (cmd->result & 0xffffff00) | status;
339}
340
341static inline void set_msg_byte(struct scsi_cmnd *cmd, char status)
342{
343 cmd->result = (cmd->result & 0xffff00ff) | (status << 8);
344}
345
346static inline void set_host_byte(struct scsi_cmnd *cmd, char status)
347{
348 cmd->result = (cmd->result & 0xff00ffff) | (status << 16);
349}
350
351static inline void set_driver_byte(struct scsi_cmnd *cmd, char status)
352{
353 cmd->result = (cmd->result & 0x00ffffff) | (status << 24);
354}
355
356static inline unsigned scsi_transfer_length(struct scsi_cmnd *scmd)
357{
358 unsigned int xfer_len = scmd->sdb.length;
359 unsigned int prot_interval = scsi_prot_interval(scmd);
360
361 if (scmd->prot_flags & SCSI_PROT_TRANSFER_PI)
362 xfer_len += (xfer_len >> ilog2(prot_interval)) * 8;
363
364 return xfer_len;
365}
366
367#endif
368