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