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