^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: GPL-2.0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #ifndef _SCSI_SCSI_CMND_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #define _SCSI_SCSI_CMND_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/dma-mapping.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/t10-pi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/scatterlist.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <scsi/scsi_request.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/android_kabi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct Scsi_Host;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) struct scsi_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * MAX_COMMAND_SIZE is:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * The longest fixed-length SCSI CDB as per the SCSI standard.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * fixed-length means: commands that their size can be determined
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * by their opcode and the CDB does not carry a length specifier, (unlike
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * the VARIABLE_LENGTH_CMD(0x7f) command). This is actually not exactly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * true and the SCSI standard also defines extended commands and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * vendor specific commands that can be bigger than 16 bytes. The kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * will support these using the same infrastructure used for VARLEN CDB's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * So in effect MAX_COMMAND_SIZE means the maximum size command scsi-ml
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * supports without specifying a cmd_len by ULD's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define MAX_COMMAND_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #if (MAX_COMMAND_SIZE > BLK_MAX_CDB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) # error MAX_COMMAND_SIZE can not be bigger than BLK_MAX_CDB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct scsi_data_buffer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct sg_table table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) unsigned length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* embedded in scsi_cmnd */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct scsi_pointer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) char *ptr; /* data pointer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) int this_residual; /* left in this buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct scatterlist *buffer; /* which buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int buffers_residual; /* how many buffers left */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) dma_addr_t dma_handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) volatile int Status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) volatile int Message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) volatile int have_data_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) volatile int sent_command;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) volatile int phase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* for scmd->flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) #define SCMD_TAGGED (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define SCMD_UNCHECKED_ISA_DMA (1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define SCMD_INITIALIZED (1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define SCMD_LAST (1 << 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) /* flags preserved across unprep / reprep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define SCMD_PRESERVED_FLAGS (SCMD_UNCHECKED_ISA_DMA | SCMD_INITIALIZED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* for scmd->state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) #define SCMD_STATE_COMPLETE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) #define SCMD_STATE_INFLIGHT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct scsi_cmnd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct scsi_request req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct scsi_device *device;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct list_head eh_entry; /* entry for the host eh_cmd_q */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct delayed_work abort_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) int eh_eflags; /* Used by error handlr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * This is set to jiffies as it was when the command was first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * allocated. It is used to time how long the command has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * been outstanding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned long jiffies_at_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) int retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int allowed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned char prot_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) unsigned char prot_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) unsigned char prot_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) unsigned short cmd_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) enum dma_data_direction sc_data_direction;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) /* These elements define the operation we are about to perform */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) unsigned char *cmnd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* These elements define the operation we ultimately want to perform */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct scsi_data_buffer sdb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct scsi_data_buffer *prot_sdb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) unsigned underflow; /* Return error if less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) this amount is transferred */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) unsigned transfersize; /* How much we are guaranteed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) transfer with each SCSI transfer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) (ie, between disconnect /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) reconnects. Probably == sector
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct request *request; /* The command we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) working on */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) unsigned char *sense_buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) /* obtained by REQUEST SENSE when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * CHECK CONDITION is received on original
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * command (auto-sense). Length must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * SCSI_SENSE_BUFFERSIZE bytes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) /* Low-level done function - can be used by low-level driver to point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * to completion function. Not used by mid/upper level code. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) void (*scsi_done) (struct scsi_cmnd *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) * The following fields can be written to by the host specific code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * Everything else should be left alone.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct scsi_pointer SCp; /* Scratchpad used by some host adapters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) unsigned char *host_scribble; /* The host adapter is allowed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * call scsi_malloc and get some memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * and hang it here. The host adapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * is also expected to call scsi_free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * to release this memory. (The memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * obtained by scsi_malloc is guaranteed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * to be at an address < 16Mb). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) int result; /* Status code from lower level driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int flags; /* Command flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) unsigned long state; /* Command completion state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned char tag; /* SCSI-II queued command tag */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) unsigned int extra_len; /* length of alignment and padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) ANDROID_KABI_RESERVE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) ANDROID_KABI_RESERVE(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) ANDROID_KABI_RESERVE(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ANDROID_KABI_RESERVE(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * Return the driver private allocation behind the command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * Only works if cmd_size is set in the host template.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return cmd + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* make sure not to use it with passthrough commands */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) static inline struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return *(struct scsi_driver **)cmd->request->rq_disk->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) extern void scsi_finish_command(struct scsi_cmnd *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) extern void *scsi_kmap_atomic_sg(struct scatterlist *sg, int sg_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) size_t *offset, size_t *len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) extern void scsi_kunmap_atomic_sg(void *virt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) blk_status_t scsi_alloc_sgtables(struct scsi_cmnd *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) void scsi_free_sgtables(struct scsi_cmnd *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) #ifdef CONFIG_SCSI_DMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) extern int scsi_dma_map(struct scsi_cmnd *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) extern void scsi_dma_unmap(struct scsi_cmnd *cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) #else /* !CONFIG_SCSI_DMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static inline int scsi_dma_map(struct scsi_cmnd *cmd) { return -ENOSYS; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static inline void scsi_dma_unmap(struct scsi_cmnd *cmd) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) #endif /* !CONFIG_SCSI_DMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static inline unsigned scsi_sg_count(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return cmd->sdb.table.nents;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static inline struct scatterlist *scsi_sglist(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return cmd->sdb.table.sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static inline unsigned scsi_bufflen(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return cmd->sdb.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) static inline void scsi_set_resid(struct scsi_cmnd *cmd, unsigned int resid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) cmd->req.resid_len = resid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static inline unsigned int scsi_get_resid(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return cmd->req.resid_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) #define scsi_for_each_sg(cmd, sg, nseg, __i) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) for_each_sg(scsi_sglist(cmd), sg, nseg, __i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static inline int scsi_sg_copy_from_buffer(struct scsi_cmnd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) void *buf, int buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) buf, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static inline int scsi_sg_copy_to_buffer(struct scsi_cmnd *cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) void *buf, int buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) buf, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) * The operations below are hints that tell the controller driver how
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) * to handle I/Os with DIF or similar types of protection information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) enum scsi_prot_operations {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) /* Normal I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) SCSI_PROT_NORMAL = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* OS-HBA: Protected, HBA-Target: Unprotected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) SCSI_PROT_READ_INSERT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) SCSI_PROT_WRITE_STRIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) /* OS-HBA: Unprotected, HBA-Target: Protected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) SCSI_PROT_READ_STRIP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) SCSI_PROT_WRITE_INSERT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /* OS-HBA: Protected, HBA-Target: Protected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) SCSI_PROT_READ_PASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) SCSI_PROT_WRITE_PASS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static inline void scsi_set_prot_op(struct scsi_cmnd *scmd, unsigned char op)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) scmd->prot_op = op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static inline unsigned char scsi_get_prot_op(struct scsi_cmnd *scmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) return scmd->prot_op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) enum scsi_prot_flags {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) SCSI_PROT_TRANSFER_PI = 1 << 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) SCSI_PROT_GUARD_CHECK = 1 << 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) SCSI_PROT_REF_CHECK = 1 << 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) SCSI_PROT_REF_INCREMENT = 1 << 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) SCSI_PROT_IP_CHECKSUM = 1 << 4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * The controller usually does not know anything about the target it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) * is communicating with. However, when DIX is enabled the controller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) * must be know target type so it can verify the protection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * information passed along with the I/O.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) enum scsi_prot_target_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) SCSI_PROT_DIF_TYPE0 = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) SCSI_PROT_DIF_TYPE1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) SCSI_PROT_DIF_TYPE2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) SCSI_PROT_DIF_TYPE3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static inline void scsi_set_prot_type(struct scsi_cmnd *scmd, unsigned char type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) scmd->prot_type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static inline unsigned char scsi_get_prot_type(struct scsi_cmnd *scmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return scmd->prot_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static inline sector_t scsi_get_lba(struct scsi_cmnd *scmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return blk_rq_pos(scmd->request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static inline unsigned int scsi_prot_interval(struct scsi_cmnd *scmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return scmd->device->sector_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static inline unsigned scsi_prot_sg_count(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return cmd->prot_sdb ? cmd->prot_sdb->table.nents : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static inline struct scatterlist *scsi_prot_sglist(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return cmd->prot_sdb ? cmd->prot_sdb->table.sgl : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static inline struct scsi_data_buffer *scsi_prot(struct scsi_cmnd *cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return cmd->prot_sdb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) #define scsi_for_each_prot_sg(cmd, sg, nseg, __i) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) for_each_sg(scsi_prot_sglist(cmd), sg, nseg, __i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) static inline void set_msg_byte(struct scsi_cmnd *cmd, char status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) cmd->result = (cmd->result & 0xffff00ff) | (status << 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static inline void set_host_byte(struct scsi_cmnd *cmd, char status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) cmd->result = (cmd->result & 0xff00ffff) | (status << 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static inline void set_driver_byte(struct scsi_cmnd *cmd, char status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) cmd->result = (cmd->result & 0x00ffffff) | (status << 24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static inline unsigned scsi_transfer_length(struct scsi_cmnd *scmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) unsigned int xfer_len = scmd->sdb.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) unsigned int prot_interval = scsi_prot_interval(scmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (scmd->prot_flags & SCSI_PROT_TRANSFER_PI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) xfer_len += (xfer_len >> ilog2(prot_interval)) * 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) return xfer_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) #endif /* _SCSI_SCSI_CMND_H */