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