Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  *
0004  *          Linux MegaRAID device driver
0005  *
0006  * Copyright (c) 2003-2004  LSI Logic Corporation.
0007  *
0008  * FILE     : mega_common.h
0009  *
0010  * Libaray of common routine used by all low-level megaraid drivers
0011  */
0012 
0013 #ifndef _MEGA_COMMON_H_
0014 #define _MEGA_COMMON_H_
0015 
0016 #include <linux/kernel.h>
0017 #include <linux/types.h>
0018 #include <linux/pci.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/mutex.h>
0021 #include <linux/interrupt.h>
0022 #include <linux/delay.h>
0023 #include <linux/blkdev.h>
0024 #include <linux/list.h>
0025 #include <linux/moduleparam.h>
0026 #include <linux/dma-mapping.h>
0027 #include <scsi/scsi.h>
0028 #include <scsi/scsi_cmnd.h>
0029 #include <scsi/scsi_device.h>
0030 #include <scsi/scsi_host.h>
0031 
0032 
0033 #define LSI_MAX_CHANNELS        16
0034 #define LSI_MAX_LOGICAL_DRIVES_64LD (64+1)
0035 
0036 #define HBA_SIGNATURE_64_BIT        0x299
0037 #define PCI_CONF_AMISIG64       0xa4
0038 
0039 #define MEGA_SCSI_INQ_EVPD      1
0040 #define MEGA_INVALID_FIELD_IN_CDB   0x24
0041 
0042 
0043 /**
0044  * scb_t - scsi command control block
0045  * @ccb         : command control block for individual driver
0046  * @list        : list of control blocks
0047  * @gp          : general purpose field for LLDs
0048  * @sno         : all SCBs have a serial number
0049  * @scp         : associated scsi command
0050  * @state       : current state of scb
0051  * @dma_dir     : direction of data transfer
0052  * @dma_type        : transfer with sg list, buffer, or no data transfer
0053  * @dev_channel     : actual channel on the device
0054  * @dev_target      : actual target on the device
0055  * @status      : completion status
0056  *
0057  * This is our central data structure to issue commands the each driver.
0058  * Driver specific data structures are maintained in the ccb field.
0059  * scb provides a field 'gp', which can be used by LLD for its own purposes
0060  *
0061  * dev_channel and dev_target must be initialized with the actual channel and
0062  * target on the controller.
0063  */
0064 typedef struct {
0065     caddr_t         ccb;
0066     struct list_head    list;
0067     unsigned long       gp;
0068     unsigned int        sno;
0069     struct scsi_cmnd    *scp;
0070     uint32_t        state;
0071     uint32_t        dma_direction;
0072     uint32_t        dma_type;
0073     uint16_t        dev_channel;
0074     uint16_t        dev_target;
0075     uint32_t        status;
0076 } scb_t;
0077 
0078 /*
0079  * SCB states as it transitions from one state to another
0080  */
0081 #define SCB_FREE    0x0000  /* on the free list */
0082 #define SCB_ACTIVE  0x0001  /* off the free list */
0083 #define SCB_PENDQ   0x0002  /* on the pending queue */
0084 #define SCB_ISSUED  0x0004  /* issued - owner f/w */
0085 #define SCB_ABORT   0x0008  /* Got an abort for this one */
0086 #define SCB_RESET   0x0010  /* Got a reset for this one */
0087 
0088 /*
0089  * DMA types for scb
0090  */
0091 #define MRAID_DMA_NONE  0x0000  /* no data transfer for this command */
0092 #define MRAID_DMA_WSG   0x0001  /* data transfer using a sg list */
0093 #define MRAID_DMA_WBUF  0x0002  /* data transfer using a contiguous buffer */
0094 
0095 
0096 /**
0097  * struct adapter_t - driver's initialization structure
0098  * @aram dpc_h          : tasklet handle
0099  * @pdev            : pci configuration pointer for kernel
0100  * @host            : pointer to host structure of mid-layer
0101  * @lock            : synchronization lock for mid-layer and driver
0102  * @quiescent           : driver is quiescent for now.
0103  * @outstanding_cmds        : number of commands pending in the driver
0104  * @kscb_list           : pointer to the bulk of SCBs pointers for IO
0105  * @kscb_pool           : pool of free scbs for IO
0106  * @kscb_pool_lock      : lock for pool of free scbs
0107  * @pend_list           : pending commands list
0108  * @pend_list_lock      : exclusion lock for pending commands list
0109  * @completed_list      : list of completed commands
0110  * @completed_list_lock     : exclusion lock for list of completed commands
0111  * @sglen           : max sg elements supported
0112  * @device_ids          : to convert kernel device addr to our devices.
0113  * @raid_device         : raid adapter specific pointer
0114  * @max_channel         : maximum channel number supported - inclusive
0115  * @max_target          : max target supported - inclusive
0116  * @max_lun         : max lun supported - inclusive
0117  * @unique_id           : unique identifier for each adapter
0118  * @irq             : IRQ for this adapter
0119  * @ito             : internal timeout value, (-1) means no timeout
0120  * @ibuf            : buffer to issue internal commands
0121  * @ibuf_dma_h          : dma handle for the above buffer
0122  * @uscb_list           : SCB pointers for user cmds, common mgmt module
0123  * @uscb_pool           : pool of SCBs for user commands
0124  * @uscb_pool_lock      : exclusion lock for these SCBs
0125  * @max_cmds            : max outstanding commands
0126  * @fw_version          : firmware version
0127  * @bios_version        : bios version
0128  * @max_cdb_sz          : biggest CDB size supported.
0129  * @ha              : is high availability present - clustering
0130  * @init_id         : initiator ID, the default value should be 7
0131  * @max_sectors         : max sectors per request
0132  * @cmd_per_lun         : max outstanding commands per LUN
0133  * @being_detached      : set when unloading, no more mgmt calls
0134  *
0135  *
0136  * mraid_setup_device_map() can be called anytime after the device map is
0137  * available and MRAID_GET_DEVICE_MAP() can be called whenever the mapping is
0138  * required, usually from LLD's queue entry point. The formar API sets up the
0139  * MRAID_IS_LOGICAL(adapter_t *, struct scsi_cmnd *) to find out if the
0140  * device in question is a logical drive.
0141  *
0142  * quiescent flag should be set by the driver if it is not accepting more
0143  * commands
0144  *
0145  * NOTE: The fields of this structures are placed to minimize cache misses
0146  */
0147 
0148 // amount of space required to store the bios and firmware version strings
0149 #define VERSION_SIZE    16
0150 
0151 typedef struct {
0152     struct tasklet_struct   dpc_h;
0153     struct pci_dev      *pdev;
0154     struct Scsi_Host    *host;
0155     spinlock_t      lock;
0156     uint8_t         quiescent;
0157     int         outstanding_cmds;
0158     scb_t           *kscb_list;
0159     struct list_head    kscb_pool;
0160     spinlock_t      kscb_pool_lock;
0161     struct list_head    pend_list;
0162     spinlock_t      pend_list_lock;
0163     struct list_head    completed_list;
0164     spinlock_t      completed_list_lock;
0165     uint16_t        sglen;
0166     int         device_ids[LSI_MAX_CHANNELS]
0167                     [LSI_MAX_LOGICAL_DRIVES_64LD];
0168     caddr_t         raid_device;
0169     uint8_t         max_channel;
0170     uint16_t        max_target;
0171     uint8_t         max_lun;
0172 
0173     uint32_t        unique_id;
0174     int         irq;
0175     uint8_t         ito;
0176     caddr_t         ibuf;
0177     dma_addr_t      ibuf_dma_h;
0178     scb_t           *uscb_list;
0179     struct list_head    uscb_pool;
0180     spinlock_t      uscb_pool_lock;
0181     int         max_cmds;
0182     uint8_t         fw_version[VERSION_SIZE];
0183     uint8_t         bios_version[VERSION_SIZE];
0184     uint8_t         max_cdb_sz;
0185     uint8_t         ha;
0186     uint16_t        init_id;
0187     uint16_t        max_sectors;
0188     uint16_t        cmd_per_lun;
0189     atomic_t        being_detached;
0190 } adapter_t;
0191 
0192 #define SCSI_FREE_LIST_LOCK(adapter)    (&adapter->kscb_pool_lock)
0193 #define USER_FREE_LIST_LOCK(adapter)    (&adapter->uscb_pool_lock)
0194 #define PENDING_LIST_LOCK(adapter)  (&adapter->pend_list_lock)
0195 #define COMPLETED_LIST_LOCK(adapter)    (&adapter->completed_list_lock)
0196 
0197 
0198 // conversion from scsi command
0199 #define SCP2HOST(scp)           (scp)->device->host // to host
0200 #define SCP2HOSTDATA(scp)       SCP2HOST(scp)->hostdata // to soft state
0201 #define SCP2CHANNEL(scp)        (scp)->device->channel  // to channel
0202 #define SCP2TARGET(scp)         (scp)->device->id   // to target
0203 #define SCP2LUN(scp)            (u32)(scp)->device->lun // to LUN
0204 
0205 // generic macro to convert scsi command and host to controller's soft state
0206 #define SCSIHOST2ADAP(host) (((caddr_t *)(host->hostdata))[0])
0207 #define SCP2ADAPTER(scp)    (adapter_t *)SCSIHOST2ADAP(SCP2HOST(scp))
0208 
0209 
0210 #define MRAID_IS_LOGICAL(adp, scp)  \
0211     (SCP2CHANNEL(scp) == (adp)->max_channel) ? 1 : 0
0212 
0213 #define MRAID_IS_LOGICAL_SDEV(adp, sdev)    \
0214     (sdev->channel == (adp)->max_channel) ? 1 : 0
0215 
0216 /**
0217  * MRAID_GET_DEVICE_MAP - device ids
0218  * @adp         : adapter's soft state
0219  * @scp         : mid-layer scsi command pointer
0220  * @p_chan      : physical channel on the controller
0221  * @target      : target id of the device or logical drive number
0222  * @islogical       : set if the command is for the logical drive
0223  *
0224  * Macro to retrieve information about device class, logical or physical and
0225  * the corresponding physical channel and target or logical drive number
0226  */
0227 #define MRAID_GET_DEVICE_MAP(adp, scp, p_chan, target, islogical)   \
0228     /*                              \
0229      * Is the request coming for the virtual channel        \
0230      */                             \
0231     islogical = MRAID_IS_LOGICAL(adp, scp);             \
0232                                     \
0233     /*                              \
0234      * Get an index into our table of drive ids mapping     \
0235      */                             \
0236     if (islogical) {                        \
0237         p_chan = 0xFF;                      \
0238         target =                        \
0239         (adp)->device_ids[(adp)->max_channel][SCP2TARGET(scp)]; \
0240     }                               \
0241     else {                              \
0242         p_chan = ((adp)->device_ids[SCP2CHANNEL(scp)]       \
0243                     [SCP2TARGET(scp)] >> 8) & 0xFF; \
0244         target = ((adp)->device_ids[SCP2CHANNEL(scp)]       \
0245                     [SCP2TARGET(scp)] & 0xFF);  \
0246     }
0247 
0248 /*
0249  * ### Helper routines ###
0250  */
0251 #define LSI_DBGLVL mraid_debug_level    // each LLD must define a global
0252                     // mraid_debug_level
0253 
0254 #ifdef DEBUG
0255 #if defined (_ASSERT_PANIC)
0256 #define ASSERT_ACTION   panic
0257 #else
0258 #define ASSERT_ACTION   printk
0259 #endif
0260 
0261 #define ASSERT(expression)                      \
0262     if (!(expression)) {                        \
0263     ASSERT_ACTION("assertion failed:(%s), file: %s, line: %d:%s\n", \
0264             #expression, __FILE__, __LINE__, __func__); \
0265     }
0266 #else
0267 #define ASSERT(expression)
0268 #endif
0269 
0270 /**
0271  * struct mraid_pci_blk - structure holds DMA memory block info
0272  * @vaddr       : virtual address to a memory block
0273  * @dma_addr        : DMA handle to a memory block
0274  *
0275  * This structure is filled up for the caller. It is the responsibilty of the
0276  * caller to allocate this array big enough to store addresses for all
0277  * requested elements
0278  */
0279 struct mraid_pci_blk {
0280     caddr_t     vaddr;
0281     dma_addr_t  dma_addr;
0282 };
0283 
0284 #endif // _MEGA_COMMON_H_