mega_common.h 9.1 KB

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