mega_common.h 9.4 KB

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