mega_common.h 8.9 KB

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