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