1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*4882a593Smuzhiyun /* 3*4882a593Smuzhiyun * 4*4882a593Smuzhiyun * Linux MegaRAID device driver 5*4882a593Smuzhiyun * 6*4882a593Smuzhiyun * Copyright (c) 2003-2004 LSI Logic Corporation. 7*4882a593Smuzhiyun * 8*4882a593Smuzhiyun * FILE : mega_common.h 9*4882a593Smuzhiyun * 10*4882a593Smuzhiyun * Libaray of common routine used by all low-level megaraid drivers 11*4882a593Smuzhiyun */ 12*4882a593Smuzhiyun 13*4882a593Smuzhiyun #ifndef _MEGA_COMMON_H_ 14*4882a593Smuzhiyun #define _MEGA_COMMON_H_ 15*4882a593Smuzhiyun 16*4882a593Smuzhiyun #include <linux/kernel.h> 17*4882a593Smuzhiyun #include <linux/types.h> 18*4882a593Smuzhiyun #include <linux/pci.h> 19*4882a593Smuzhiyun #include <linux/spinlock.h> 20*4882a593Smuzhiyun #include <linux/mutex.h> 21*4882a593Smuzhiyun #include <linux/interrupt.h> 22*4882a593Smuzhiyun #include <linux/delay.h> 23*4882a593Smuzhiyun #include <linux/blkdev.h> 24*4882a593Smuzhiyun #include <linux/list.h> 25*4882a593Smuzhiyun #include <linux/moduleparam.h> 26*4882a593Smuzhiyun #include <linux/dma-mapping.h> 27*4882a593Smuzhiyun #include <scsi/scsi.h> 28*4882a593Smuzhiyun #include <scsi/scsi_cmnd.h> 29*4882a593Smuzhiyun #include <scsi/scsi_device.h> 30*4882a593Smuzhiyun #include <scsi/scsi_host.h> 31*4882a593Smuzhiyun 32*4882a593Smuzhiyun 33*4882a593Smuzhiyun #define LSI_MAX_CHANNELS 16 34*4882a593Smuzhiyun #define LSI_MAX_LOGICAL_DRIVES_64LD (64+1) 35*4882a593Smuzhiyun 36*4882a593Smuzhiyun #define HBA_SIGNATURE_64_BIT 0x299 37*4882a593Smuzhiyun #define PCI_CONF_AMISIG64 0xa4 38*4882a593Smuzhiyun 39*4882a593Smuzhiyun #define MEGA_SCSI_INQ_EVPD 1 40*4882a593Smuzhiyun #define MEGA_INVALID_FIELD_IN_CDB 0x24 41*4882a593Smuzhiyun 42*4882a593Smuzhiyun 43*4882a593Smuzhiyun /** 44*4882a593Smuzhiyun * scb_t - scsi command control block 45*4882a593Smuzhiyun * @ccb : command control block for individual driver 46*4882a593Smuzhiyun * @list : list of control blocks 47*4882a593Smuzhiyun * @gp : general purpose field for LLDs 48*4882a593Smuzhiyun * @sno : all SCBs have a serial number 49*4882a593Smuzhiyun * @scp : associated scsi command 50*4882a593Smuzhiyun * @state : current state of scb 51*4882a593Smuzhiyun * @dma_dir : direction of data transfer 52*4882a593Smuzhiyun * @dma_type : transfer with sg list, buffer, or no data transfer 53*4882a593Smuzhiyun * @dev_channel : actual channel on the device 54*4882a593Smuzhiyun * @dev_target : actual target on the device 55*4882a593Smuzhiyun * @status : completion status 56*4882a593Smuzhiyun * 57*4882a593Smuzhiyun * This is our central data structure to issue commands the each driver. 58*4882a593Smuzhiyun * Driver specific data structures are maintained in the ccb field. 59*4882a593Smuzhiyun * scb provides a field 'gp', which can be used by LLD for its own purposes 60*4882a593Smuzhiyun * 61*4882a593Smuzhiyun * dev_channel and dev_target must be initialized with the actual channel and 62*4882a593Smuzhiyun * target on the controller. 63*4882a593Smuzhiyun */ 64*4882a593Smuzhiyun typedef struct { 65*4882a593Smuzhiyun caddr_t ccb; 66*4882a593Smuzhiyun struct list_head list; 67*4882a593Smuzhiyun unsigned long gp; 68*4882a593Smuzhiyun unsigned int sno; 69*4882a593Smuzhiyun struct scsi_cmnd *scp; 70*4882a593Smuzhiyun uint32_t state; 71*4882a593Smuzhiyun uint32_t dma_direction; 72*4882a593Smuzhiyun uint32_t dma_type; 73*4882a593Smuzhiyun uint16_t dev_channel; 74*4882a593Smuzhiyun uint16_t dev_target; 75*4882a593Smuzhiyun uint32_t status; 76*4882a593Smuzhiyun } scb_t; 77*4882a593Smuzhiyun 78*4882a593Smuzhiyun /* 79*4882a593Smuzhiyun * SCB states as it transitions from one state to another 80*4882a593Smuzhiyun */ 81*4882a593Smuzhiyun #define SCB_FREE 0x0000 /* on the free list */ 82*4882a593Smuzhiyun #define SCB_ACTIVE 0x0001 /* off the free list */ 83*4882a593Smuzhiyun #define SCB_PENDQ 0x0002 /* on the pending queue */ 84*4882a593Smuzhiyun #define SCB_ISSUED 0x0004 /* issued - owner f/w */ 85*4882a593Smuzhiyun #define SCB_ABORT 0x0008 /* Got an abort for this one */ 86*4882a593Smuzhiyun #define SCB_RESET 0x0010 /* Got a reset for this one */ 87*4882a593Smuzhiyun 88*4882a593Smuzhiyun /* 89*4882a593Smuzhiyun * DMA types for scb 90*4882a593Smuzhiyun */ 91*4882a593Smuzhiyun #define MRAID_DMA_NONE 0x0000 /* no data transfer for this command */ 92*4882a593Smuzhiyun #define MRAID_DMA_WSG 0x0001 /* data transfer using a sg list */ 93*4882a593Smuzhiyun #define MRAID_DMA_WBUF 0x0002 /* data transfer using a contiguous buffer */ 94*4882a593Smuzhiyun 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun /** 97*4882a593Smuzhiyun * struct adapter_t - driver's initialization structure 98*4882a593Smuzhiyun * @aram dpc_h : tasklet handle 99*4882a593Smuzhiyun * @pdev : pci configuration pointer for kernel 100*4882a593Smuzhiyun * @host : pointer to host structure of mid-layer 101*4882a593Smuzhiyun * @lock : synchronization lock for mid-layer and driver 102*4882a593Smuzhiyun * @quiescent : driver is quiescent for now. 103*4882a593Smuzhiyun * @outstanding_cmds : number of commands pending in the driver 104*4882a593Smuzhiyun * @kscb_list : pointer to the bulk of SCBs pointers for IO 105*4882a593Smuzhiyun * @kscb_pool : pool of free scbs for IO 106*4882a593Smuzhiyun * @kscb_pool_lock : lock for pool of free scbs 107*4882a593Smuzhiyun * @pend_list : pending commands list 108*4882a593Smuzhiyun * @pend_list_lock : exclusion lock for pending commands list 109*4882a593Smuzhiyun * @completed_list : list of completed commands 110*4882a593Smuzhiyun * @completed_list_lock : exclusion lock for list of completed commands 111*4882a593Smuzhiyun * @sglen : max sg elements supported 112*4882a593Smuzhiyun * @device_ids : to convert kernel device addr to our devices. 113*4882a593Smuzhiyun * @raid_device : raid adapter specific pointer 114*4882a593Smuzhiyun * @max_channel : maximum channel number supported - inclusive 115*4882a593Smuzhiyun * @max_target : max target supported - inclusive 116*4882a593Smuzhiyun * @max_lun : max lun supported - inclusive 117*4882a593Smuzhiyun * @unique_id : unique identifier for each adapter 118*4882a593Smuzhiyun * @irq : IRQ for this adapter 119*4882a593Smuzhiyun * @ito : internal timeout value, (-1) means no timeout 120*4882a593Smuzhiyun * @ibuf : buffer to issue internal commands 121*4882a593Smuzhiyun * @ibuf_dma_h : dma handle for the above buffer 122*4882a593Smuzhiyun * @uscb_list : SCB pointers for user cmds, common mgmt module 123*4882a593Smuzhiyun * @uscb_pool : pool of SCBs for user commands 124*4882a593Smuzhiyun * @uscb_pool_lock : exclusion lock for these SCBs 125*4882a593Smuzhiyun * @max_cmds : max outstanding commands 126*4882a593Smuzhiyun * @fw_version : firmware version 127*4882a593Smuzhiyun * @bios_version : bios version 128*4882a593Smuzhiyun * @max_cdb_sz : biggest CDB size supported. 129*4882a593Smuzhiyun * @ha : is high availability present - clustering 130*4882a593Smuzhiyun * @init_id : initiator ID, the default value should be 7 131*4882a593Smuzhiyun * @max_sectors : max sectors per request 132*4882a593Smuzhiyun * @cmd_per_lun : max outstanding commands per LUN 133*4882a593Smuzhiyun * @being_detached : set when unloading, no more mgmt calls 134*4882a593Smuzhiyun * 135*4882a593Smuzhiyun * 136*4882a593Smuzhiyun * mraid_setup_device_map() can be called anytime after the device map is 137*4882a593Smuzhiyun * available and MRAID_GET_DEVICE_MAP() can be called whenever the mapping is 138*4882a593Smuzhiyun * required, usually from LLD's queue entry point. The formar API sets up the 139*4882a593Smuzhiyun * MRAID_IS_LOGICAL(adapter_t *, struct scsi_cmnd *) to find out if the 140*4882a593Smuzhiyun * device in question is a logical drive. 141*4882a593Smuzhiyun * 142*4882a593Smuzhiyun * quiescent flag should be set by the driver if it is not accepting more 143*4882a593Smuzhiyun * commands 144*4882a593Smuzhiyun * 145*4882a593Smuzhiyun * NOTE: The fields of this structures are placed to minimize cache misses 146*4882a593Smuzhiyun */ 147*4882a593Smuzhiyun 148*4882a593Smuzhiyun // amount of space required to store the bios and firmware version strings 149*4882a593Smuzhiyun #define VERSION_SIZE 16 150*4882a593Smuzhiyun 151*4882a593Smuzhiyun typedef struct { 152*4882a593Smuzhiyun struct tasklet_struct dpc_h; 153*4882a593Smuzhiyun struct pci_dev *pdev; 154*4882a593Smuzhiyun struct Scsi_Host *host; 155*4882a593Smuzhiyun spinlock_t lock; 156*4882a593Smuzhiyun uint8_t quiescent; 157*4882a593Smuzhiyun int outstanding_cmds; 158*4882a593Smuzhiyun scb_t *kscb_list; 159*4882a593Smuzhiyun struct list_head kscb_pool; 160*4882a593Smuzhiyun spinlock_t kscb_pool_lock; 161*4882a593Smuzhiyun struct list_head pend_list; 162*4882a593Smuzhiyun spinlock_t pend_list_lock; 163*4882a593Smuzhiyun struct list_head completed_list; 164*4882a593Smuzhiyun spinlock_t completed_list_lock; 165*4882a593Smuzhiyun uint16_t sglen; 166*4882a593Smuzhiyun int device_ids[LSI_MAX_CHANNELS] 167*4882a593Smuzhiyun [LSI_MAX_LOGICAL_DRIVES_64LD]; 168*4882a593Smuzhiyun caddr_t raid_device; 169*4882a593Smuzhiyun uint8_t max_channel; 170*4882a593Smuzhiyun uint16_t max_target; 171*4882a593Smuzhiyun uint8_t max_lun; 172*4882a593Smuzhiyun 173*4882a593Smuzhiyun uint32_t unique_id; 174*4882a593Smuzhiyun int irq; 175*4882a593Smuzhiyun uint8_t ito; 176*4882a593Smuzhiyun caddr_t ibuf; 177*4882a593Smuzhiyun dma_addr_t ibuf_dma_h; 178*4882a593Smuzhiyun scb_t *uscb_list; 179*4882a593Smuzhiyun struct list_head uscb_pool; 180*4882a593Smuzhiyun spinlock_t uscb_pool_lock; 181*4882a593Smuzhiyun int max_cmds; 182*4882a593Smuzhiyun uint8_t fw_version[VERSION_SIZE]; 183*4882a593Smuzhiyun uint8_t bios_version[VERSION_SIZE]; 184*4882a593Smuzhiyun uint8_t max_cdb_sz; 185*4882a593Smuzhiyun uint8_t ha; 186*4882a593Smuzhiyun uint16_t init_id; 187*4882a593Smuzhiyun uint16_t max_sectors; 188*4882a593Smuzhiyun uint16_t cmd_per_lun; 189*4882a593Smuzhiyun atomic_t being_detached; 190*4882a593Smuzhiyun } adapter_t; 191*4882a593Smuzhiyun 192*4882a593Smuzhiyun #define SCSI_FREE_LIST_LOCK(adapter) (&adapter->kscb_pool_lock) 193*4882a593Smuzhiyun #define USER_FREE_LIST_LOCK(adapter) (&adapter->uscb_pool_lock) 194*4882a593Smuzhiyun #define PENDING_LIST_LOCK(adapter) (&adapter->pend_list_lock) 195*4882a593Smuzhiyun #define COMPLETED_LIST_LOCK(adapter) (&adapter->completed_list_lock) 196*4882a593Smuzhiyun 197*4882a593Smuzhiyun 198*4882a593Smuzhiyun // conversion from scsi command 199*4882a593Smuzhiyun #define SCP2HOST(scp) (scp)->device->host // to host 200*4882a593Smuzhiyun #define SCP2HOSTDATA(scp) SCP2HOST(scp)->hostdata // to soft state 201*4882a593Smuzhiyun #define SCP2CHANNEL(scp) (scp)->device->channel // to channel 202*4882a593Smuzhiyun #define SCP2TARGET(scp) (scp)->device->id // to target 203*4882a593Smuzhiyun #define SCP2LUN(scp) (u32)(scp)->device->lun // to LUN 204*4882a593Smuzhiyun 205*4882a593Smuzhiyun // generic macro to convert scsi command and host to controller's soft state 206*4882a593Smuzhiyun #define SCSIHOST2ADAP(host) (((caddr_t *)(host->hostdata))[0]) 207*4882a593Smuzhiyun #define SCP2ADAPTER(scp) (adapter_t *)SCSIHOST2ADAP(SCP2HOST(scp)) 208*4882a593Smuzhiyun 209*4882a593Smuzhiyun 210*4882a593Smuzhiyun #define MRAID_IS_LOGICAL(adp, scp) \ 211*4882a593Smuzhiyun (SCP2CHANNEL(scp) == (adp)->max_channel) ? 1 : 0 212*4882a593Smuzhiyun 213*4882a593Smuzhiyun #define MRAID_IS_LOGICAL_SDEV(adp, sdev) \ 214*4882a593Smuzhiyun (sdev->channel == (adp)->max_channel) ? 1 : 0 215*4882a593Smuzhiyun 216*4882a593Smuzhiyun /** 217*4882a593Smuzhiyun * MRAID_GET_DEVICE_MAP - device ids 218*4882a593Smuzhiyun * @adp : adapter's soft state 219*4882a593Smuzhiyun * @scp : mid-layer scsi command pointer 220*4882a593Smuzhiyun * @p_chan : physical channel on the controller 221*4882a593Smuzhiyun * @target : target id of the device or logical drive number 222*4882a593Smuzhiyun * @islogical : set if the command is for the logical drive 223*4882a593Smuzhiyun * 224*4882a593Smuzhiyun * Macro to retrieve information about device class, logical or physical and 225*4882a593Smuzhiyun * the corresponding physical channel and target or logical drive number 226*4882a593Smuzhiyun */ 227*4882a593Smuzhiyun #define MRAID_GET_DEVICE_MAP(adp, scp, p_chan, target, islogical) \ 228*4882a593Smuzhiyun /* \ 229*4882a593Smuzhiyun * Is the request coming for the virtual channel \ 230*4882a593Smuzhiyun */ \ 231*4882a593Smuzhiyun islogical = MRAID_IS_LOGICAL(adp, scp); \ 232*4882a593Smuzhiyun \ 233*4882a593Smuzhiyun /* \ 234*4882a593Smuzhiyun * Get an index into our table of drive ids mapping \ 235*4882a593Smuzhiyun */ \ 236*4882a593Smuzhiyun if (islogical) { \ 237*4882a593Smuzhiyun p_chan = 0xFF; \ 238*4882a593Smuzhiyun target = \ 239*4882a593Smuzhiyun (adp)->device_ids[(adp)->max_channel][SCP2TARGET(scp)]; \ 240*4882a593Smuzhiyun } \ 241*4882a593Smuzhiyun else { \ 242*4882a593Smuzhiyun p_chan = ((adp)->device_ids[SCP2CHANNEL(scp)] \ 243*4882a593Smuzhiyun [SCP2TARGET(scp)] >> 8) & 0xFF; \ 244*4882a593Smuzhiyun target = ((adp)->device_ids[SCP2CHANNEL(scp)] \ 245*4882a593Smuzhiyun [SCP2TARGET(scp)] & 0xFF); \ 246*4882a593Smuzhiyun } 247*4882a593Smuzhiyun 248*4882a593Smuzhiyun /* 249*4882a593Smuzhiyun * ### Helper routines ### 250*4882a593Smuzhiyun */ 251*4882a593Smuzhiyun #define LSI_DBGLVL mraid_debug_level // each LLD must define a global 252*4882a593Smuzhiyun // mraid_debug_level 253*4882a593Smuzhiyun 254*4882a593Smuzhiyun #ifdef DEBUG 255*4882a593Smuzhiyun #if defined (_ASSERT_PANIC) 256*4882a593Smuzhiyun #define ASSERT_ACTION panic 257*4882a593Smuzhiyun #else 258*4882a593Smuzhiyun #define ASSERT_ACTION printk 259*4882a593Smuzhiyun #endif 260*4882a593Smuzhiyun 261*4882a593Smuzhiyun #define ASSERT(expression) \ 262*4882a593Smuzhiyun if (!(expression)) { \ 263*4882a593Smuzhiyun ASSERT_ACTION("assertion failed:(%s), file: %s, line: %d:%s\n", \ 264*4882a593Smuzhiyun #expression, __FILE__, __LINE__, __func__); \ 265*4882a593Smuzhiyun } 266*4882a593Smuzhiyun #else 267*4882a593Smuzhiyun #define ASSERT(expression) 268*4882a593Smuzhiyun #endif 269*4882a593Smuzhiyun 270*4882a593Smuzhiyun /** 271*4882a593Smuzhiyun * struct mraid_pci_blk - structure holds DMA memory block info 272*4882a593Smuzhiyun * @vaddr : virtual address to a memory block 273*4882a593Smuzhiyun * @dma_addr : DMA handle to a memory block 274*4882a593Smuzhiyun * 275*4882a593Smuzhiyun * This structure is filled up for the caller. It is the responsibilty of the 276*4882a593Smuzhiyun * caller to allocate this array big enough to store addresses for all 277*4882a593Smuzhiyun * requested elements 278*4882a593Smuzhiyun */ 279*4882a593Smuzhiyun struct mraid_pci_blk { 280*4882a593Smuzhiyun caddr_t vaddr; 281*4882a593Smuzhiyun dma_addr_t dma_addr; 282*4882a593Smuzhiyun }; 283*4882a593Smuzhiyun 284*4882a593Smuzhiyun #endif // _MEGA_COMMON_H_ 285*4882a593Smuzhiyun 286*4882a593Smuzhiyun // vim: set ts=8 sw=8 tw=78: 287