linux/include/scsi/scsi_cmnd.h
<<
>>
Prefs
   1/* SPDX-License-Identifier: GPL-2.0 */
   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 * MAX_COMMAND_SIZE is:
  23 * The longest fixed-length SCSI CDB as per the SCSI standard.
  24 * fixed-length means: commands that their size can be determined
  25 * by their opcode and the CDB does not carry a length specifier, (unlike
  26 * the VARIABLE_LENGTH_CMD(0x7f) command). This is actually not exactly
  27 * true and the SCSI standard also defines extended commands and
  28 * vendor specific commands that can be bigger than 16 bytes. The kernel
  29 * will support these using the same infrastructure used for VARLEN CDB's.
  30 * So in effect MAX_COMMAND_SIZE means the maximum size command scsi-ml
  31 * supports without specifying a cmd_len by ULD's
  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/* embedded in scsi_cmnd */
  45struct scsi_pointer {
  46        char *ptr;              /* data pointer */
  47        int this_residual;      /* left in this buffer */
  48        struct scatterlist *buffer;     /* which buffer */
  49        int buffers_residual;   /* how many buffers left */
  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/* for scmd->flags */
  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/* flags preserved across unprep / reprep */
  66#define SCMD_PRESERVED_FLAGS    (SCMD_UNCHECKED_ISA_DMA | SCMD_INITIALIZED)
  67
  68/* for scmd->state */
  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;  /* scsi_cmnd participates in queue lists */
  76        struct list_head eh_entry; /* entry for the host eh_abort_list/eh_cmd_q */
  77        struct delayed_work abort_work;
  78
  79        struct rcu_head rcu;
  80
  81        int eh_eflags;          /* Used by error handlr */
  82
  83        /*
  84         * A SCSI Command is assigned a nonzero serial_number before passed
  85         * to the driver's queue command function.  The serial_number is
  86         * cleared when scsi_done is entered indicating that the command
  87         * has been completed.  It is a bug for LLDDs to use this number
  88         * for purposes other than printk (and even that is only useful
  89         * for debugging).
  90         */
  91        unsigned long serial_number;
  92
  93        /*
  94         * This is set to jiffies as it was when the command was first
  95         * allocated.  It is used to time how long the command has
  96         * been outstanding
  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        /* These elements define the operation we are about to perform */
 111        unsigned char *cmnd;
 112
 113
 114        /* These elements define the operation we ultimately want to perform */
 115        struct scsi_data_buffer sdb;
 116        struct scsi_data_buffer *prot_sdb;
 117
 118        unsigned underflow;     /* Return error if less than
 119                                   this amount is transferred */
 120
 121        unsigned transfersize;  /* How much we are guaranteed to
 122                                   transfer with each SCSI transfer
 123                                   (ie, between disconnect / 
 124                                   reconnects.   Probably == sector
 125                                   size */
 126
 127        struct request *request;        /* The command we are
 128                                           working on */
 129
 130        unsigned char *sense_buffer;
 131                                /* obtained by REQUEST SENSE when
 132                                 * CHECK CONDITION is received on original
 133                                 * command (auto-sense). Length must be
 134                                 * SCSI_SENSE_BUFFERSIZE bytes. */
 135
 136        /* Low-level done function - can be used by low-level driver to point
 137         *        to completion function.  Not used by mid/upper level code. */
 138        void (*scsi_done) (struct scsi_cmnd *);
 139
 140        /*
 141         * The following fields can be written to by the host specific code. 
 142         * Everything else should be left alone. 
 143         */
 144        struct scsi_pointer SCp;        /* Scratchpad used by some host adapters */
 145
 146        unsigned char *host_scribble;   /* The host adapter is allowed to
 147                                         * call scsi_malloc and get some memory
 148                                         * and hang it here.  The host adapter
 149                                         * is also expected to call scsi_free
 150                                         * to release this memory.  (The memory
 151                                         * obtained by scsi_malloc is guaranteed
 152                                         * to be at an address < 16Mb). */
 153
 154        int result;             /* Status code from lower level driver */
 155        int flags;              /* Command flags */
 156        unsigned long state;    /* Command completion state */
 157
 158        unsigned char tag;      /* SCSI-II queued command tag */
 159
 160        /* FOR RH USE ONLY
 161         *
 162         * The following padding has been inserted before ABI freeze to
 163         * allow extending the structure while preserving ABI.
 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 * Return the driver private allocation behind the command.
 173 * Only works if cmd_size is set in the host template.
 174 */
 175static inline void *scsi_cmd_priv(struct scsi_cmnd *cmd)
 176{
 177        return cmd + 1;
 178}
 179
 180/* make sure not to use it with passthrough commands */
 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 /* !CONFIG_SCSI_DMA */
 200static inline int scsi_dma_map(struct scsi_cmnd *cmd) { return -ENOSYS; }
 201static inline void scsi_dma_unmap(struct scsi_cmnd *cmd) { }
 202#endif /* !CONFIG_SCSI_DMA */
 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 * The operations below are hints that tell the controller driver how
 248 * to handle I/Os with DIF or similar types of protection information.
 249 */
 250enum scsi_prot_operations {
 251        /* Normal I/O */
 252        SCSI_PROT_NORMAL = 0,
 253
 254        /* OS-HBA: Protected, HBA-Target: Unprotected */
 255        SCSI_PROT_READ_INSERT,
 256        SCSI_PROT_WRITE_STRIP,
 257
 258        /* OS-HBA: Unprotected, HBA-Target: Protected */
 259        SCSI_PROT_READ_STRIP,
 260        SCSI_PROT_WRITE_INSERT,
 261
 262        /* OS-HBA: Protected, HBA-Target: Protected */
 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 * The controller usually does not know anything about the target it
 287 * is communicating with.  However, when DIX is enabled the controller
 288 * must be know target type so it can verify the protection
 289 * information passed along with the I/O.
 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 /* _SCSI_SCSI_CMND_H */
 368