aic94xx_hwi.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Aic94xx SAS/SATA driver hardware interface header file.
  4. *
  5. * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
  6. * Copyright (C) 2005 Luben Tuikov <[email protected]>
  7. */
  8. #ifndef _AIC94XX_HWI_H_
  9. #define _AIC94XX_HWI_H_
  10. #include <linux/interrupt.h>
  11. #include <linux/pci.h>
  12. #include <linux/dma-mapping.h>
  13. #include <scsi/libsas.h>
  14. #include "aic94xx.h"
  15. #include "aic94xx_sas.h"
  16. /* Define ASD_MAX_PHYS to the maximum phys ever. Currently 8. */
  17. #define ASD_MAX_PHYS 8
  18. #define ASD_PCBA_SN_SIZE 12
  19. struct asd_ha_addrspace {
  20. void __iomem *addr;
  21. unsigned long start; /* pci resource start */
  22. unsigned long len; /* pci resource len */
  23. unsigned long flags; /* pci resource flags */
  24. /* addresses internal to the host adapter */
  25. u32 swa_base; /* mmspace 1 (MBAR1) uses this only */
  26. u32 swb_base;
  27. u32 swc_base;
  28. };
  29. struct bios_struct {
  30. int present;
  31. u8 maj;
  32. u8 min;
  33. u32 bld;
  34. };
  35. struct unit_element_struct {
  36. u16 num;
  37. u16 size;
  38. void *area;
  39. };
  40. struct flash_struct {
  41. u32 bar;
  42. int present;
  43. int wide;
  44. u8 manuf;
  45. u8 dev_id;
  46. u8 sec_prot;
  47. u8 method;
  48. u32 dir_offs;
  49. };
  50. struct asd_phy_desc {
  51. /* From CTRL-A settings, then set to what is appropriate */
  52. u8 sas_addr[SAS_ADDR_SIZE];
  53. u8 max_sas_lrate;
  54. u8 min_sas_lrate;
  55. u8 max_sata_lrate;
  56. u8 min_sata_lrate;
  57. u8 flags;
  58. #define ASD_CRC_DIS 1
  59. #define ASD_SATA_SPINUP_HOLD 2
  60. u8 phy_control_0; /* mode 5 reg 0x160 */
  61. u8 phy_control_1; /* mode 5 reg 0x161 */
  62. u8 phy_control_2; /* mode 5 reg 0x162 */
  63. u8 phy_control_3; /* mode 5 reg 0x163 */
  64. };
  65. struct asd_dma_tok {
  66. void *vaddr;
  67. dma_addr_t dma_handle;
  68. size_t size;
  69. };
  70. struct hw_profile {
  71. struct bios_struct bios;
  72. struct unit_element_struct ue;
  73. struct flash_struct flash;
  74. u8 sas_addr[SAS_ADDR_SIZE];
  75. char pcba_sn[ASD_PCBA_SN_SIZE+1];
  76. u8 enabled_phys; /* mask of enabled phys */
  77. struct asd_phy_desc phy_desc[ASD_MAX_PHYS];
  78. u32 max_scbs; /* absolute sequencer scb queue size */
  79. struct asd_dma_tok *scb_ext;
  80. u32 max_ddbs;
  81. struct asd_dma_tok *ddb_ext;
  82. spinlock_t ddb_lock;
  83. void *ddb_bitmap;
  84. int num_phys; /* ENABLEABLE */
  85. int max_phys; /* REPORTED + ENABLEABLE */
  86. unsigned addr_range; /* max # of addrs; max # of possible ports */
  87. unsigned port_name_base;
  88. unsigned dev_name_base;
  89. unsigned sata_name_base;
  90. };
  91. struct asd_ascb {
  92. struct list_head list;
  93. struct asd_ha_struct *ha;
  94. struct scb *scb; /* equals dma_scb->vaddr */
  95. struct asd_dma_tok dma_scb;
  96. struct asd_dma_tok *sg_arr;
  97. void (*tasklet_complete)(struct asd_ascb *, struct done_list_struct *);
  98. u8 uldd_timer:1;
  99. /* internally generated command */
  100. struct timer_list timer;
  101. struct completion *completion;
  102. u8 tag_valid:1;
  103. __be16 tag; /* error recovery only */
  104. /* If this is an Empty SCB, index of first edb in seq->edb_arr. */
  105. int edb_index;
  106. /* Used by the timer timeout function. */
  107. int tc_index;
  108. void *uldd_task;
  109. };
  110. #define ASD_DL_SIZE_BITS 0x8
  111. #define ASD_DL_SIZE (1<<(2+ASD_DL_SIZE_BITS))
  112. #define ASD_DEF_DL_TOGGLE 0x01
  113. struct asd_seq_data {
  114. spinlock_t pend_q_lock;
  115. u16 scbpro;
  116. int pending;
  117. struct list_head pend_q;
  118. int can_queue; /* per adapter */
  119. struct asd_dma_tok next_scb; /* next scb to be delivered to CSEQ */
  120. spinlock_t tc_index_lock;
  121. void **tc_index_array;
  122. void *tc_index_bitmap;
  123. int tc_index_bitmap_bits;
  124. struct tasklet_struct dl_tasklet;
  125. struct done_list_struct *dl; /* array of done list entries, equals */
  126. struct asd_dma_tok *actual_dl; /* actual_dl->vaddr */
  127. int dl_toggle;
  128. int dl_next;
  129. int num_edbs;
  130. struct asd_dma_tok **edb_arr;
  131. int num_escbs;
  132. struct asd_ascb **escb_arr; /* array of pointers to escbs */
  133. };
  134. /* This is an internal port structure. These are used to get accurate
  135. * phy_mask for updating DDB 0.
  136. */
  137. struct asd_port {
  138. u8 sas_addr[SAS_ADDR_SIZE];
  139. u8 attached_sas_addr[SAS_ADDR_SIZE];
  140. u32 phy_mask;
  141. int num_phys;
  142. };
  143. /* This is the Host Adapter structure. It describes the hardware
  144. * SAS adapter.
  145. */
  146. struct asd_ha_struct {
  147. struct pci_dev *pcidev;
  148. const char *name;
  149. struct sas_ha_struct sas_ha;
  150. u8 revision_id;
  151. int iospace;
  152. spinlock_t iolock;
  153. struct asd_ha_addrspace io_handle[2];
  154. struct hw_profile hw_prof;
  155. struct asd_phy phys[ASD_MAX_PHYS];
  156. spinlock_t asd_ports_lock;
  157. struct asd_port asd_ports[ASD_MAX_PHYS];
  158. struct asd_sas_port ports[ASD_MAX_PHYS];
  159. struct dma_pool *scb_pool;
  160. struct asd_seq_data seq; /* sequencer related */
  161. u32 bios_status;
  162. const struct firmware *bios_image;
  163. };
  164. /* ---------- Common macros ---------- */
  165. #define ASD_BUSADDR_LO(__dma_handle) ((u32)(__dma_handle))
  166. #define ASD_BUSADDR_HI(__dma_handle) (((sizeof(dma_addr_t))==8) \
  167. ? ((u32)((__dma_handle) >> 32)) \
  168. : ((u32)0))
  169. #define dev_to_asd_ha(__dev) pci_get_drvdata(to_pci_dev(__dev))
  170. #define SCB_SITE_VALID(__site_no) (((__site_no) & 0xF0FF) != 0x00FF \
  171. && ((__site_no) & 0xF0FF) > 0x001F)
  172. /* For each bit set in __lseq_mask, set __lseq to equal the bit
  173. * position of the set bit and execute the statement following.
  174. * __mc is the temporary mask, used as a mask "counter".
  175. */
  176. #define for_each_sequencer(__lseq_mask, __mc, __lseq) \
  177. for ((__mc)=(__lseq_mask),(__lseq)=0;(__mc)!=0;(__lseq++),(__mc)>>=1)\
  178. if (((__mc) & 1))
  179. #define for_each_phy(__lseq_mask, __mc, __lseq) \
  180. for ((__mc)=(__lseq_mask),(__lseq)=0;(__mc)!=0;(__lseq++),(__mc)>>=1)\
  181. if (((__mc) & 1))
  182. #define PHY_ENABLED(_HA, _I) ((_HA)->hw_prof.enabled_phys & (1<<(_I)))
  183. /* ---------- DMA allocs ---------- */
  184. static inline struct asd_dma_tok *asd_dmatok_alloc(gfp_t flags)
  185. {
  186. return kmem_cache_alloc(asd_dma_token_cache, flags);
  187. }
  188. static inline void asd_dmatok_free(struct asd_dma_tok *token)
  189. {
  190. kmem_cache_free(asd_dma_token_cache, token);
  191. }
  192. static inline struct asd_dma_tok *asd_alloc_coherent(struct asd_ha_struct *
  193. asd_ha, size_t size,
  194. gfp_t flags)
  195. {
  196. struct asd_dma_tok *token = asd_dmatok_alloc(flags);
  197. if (token) {
  198. token->size = size;
  199. token->vaddr = dma_alloc_coherent(&asd_ha->pcidev->dev,
  200. token->size,
  201. &token->dma_handle,
  202. flags);
  203. if (!token->vaddr) {
  204. asd_dmatok_free(token);
  205. token = NULL;
  206. }
  207. }
  208. return token;
  209. }
  210. static inline void asd_free_coherent(struct asd_ha_struct *asd_ha,
  211. struct asd_dma_tok *token)
  212. {
  213. if (token) {
  214. dma_free_coherent(&asd_ha->pcidev->dev, token->size,
  215. token->vaddr, token->dma_handle);
  216. asd_dmatok_free(token);
  217. }
  218. }
  219. static inline void asd_init_ascb(struct asd_ha_struct *asd_ha,
  220. struct asd_ascb *ascb)
  221. {
  222. INIT_LIST_HEAD(&ascb->list);
  223. ascb->scb = ascb->dma_scb.vaddr;
  224. ascb->ha = asd_ha;
  225. timer_setup(&ascb->timer, NULL, 0);
  226. ascb->tc_index = -1;
  227. }
  228. /* Must be called with the tc_index_lock held!
  229. */
  230. static inline void asd_tc_index_release(struct asd_seq_data *seq, int index)
  231. {
  232. seq->tc_index_array[index] = NULL;
  233. clear_bit(index, seq->tc_index_bitmap);
  234. }
  235. /* Must be called with the tc_index_lock held!
  236. */
  237. static inline int asd_tc_index_get(struct asd_seq_data *seq, void *ptr)
  238. {
  239. int index;
  240. index = find_first_zero_bit(seq->tc_index_bitmap,
  241. seq->tc_index_bitmap_bits);
  242. if (index == seq->tc_index_bitmap_bits)
  243. return -1;
  244. seq->tc_index_array[index] = ptr;
  245. set_bit(index, seq->tc_index_bitmap);
  246. return index;
  247. }
  248. /* Must be called with the tc_index_lock held!
  249. */
  250. static inline void *asd_tc_index_find(struct asd_seq_data *seq, int index)
  251. {
  252. return seq->tc_index_array[index];
  253. }
  254. /**
  255. * asd_ascb_free -- free a single aSCB after is has completed
  256. * @ascb: pointer to the aSCB of interest
  257. *
  258. * This frees an aSCB after it has been executed/completed by
  259. * the sequencer.
  260. */
  261. static inline void asd_ascb_free(struct asd_ascb *ascb)
  262. {
  263. if (ascb) {
  264. struct asd_ha_struct *asd_ha = ascb->ha;
  265. unsigned long flags;
  266. BUG_ON(!list_empty(&ascb->list));
  267. spin_lock_irqsave(&ascb->ha->seq.tc_index_lock, flags);
  268. asd_tc_index_release(&ascb->ha->seq, ascb->tc_index);
  269. spin_unlock_irqrestore(&ascb->ha->seq.tc_index_lock, flags);
  270. dma_pool_free(asd_ha->scb_pool, ascb->dma_scb.vaddr,
  271. ascb->dma_scb.dma_handle);
  272. kmem_cache_free(asd_ascb_cache, ascb);
  273. }
  274. }
  275. /**
  276. * asd_ascb_list_free -- free a list of ascbs
  277. * @ascb_list: a list of ascbs
  278. *
  279. * This function will free a list of ascbs allocated by asd_ascb_alloc_list.
  280. * It is used when say the scb queueing function returned QUEUE_FULL,
  281. * and we do not need the ascbs any more.
  282. */
  283. static inline void asd_ascb_free_list(struct asd_ascb *ascb_list)
  284. {
  285. LIST_HEAD(list);
  286. struct list_head *n, *pos;
  287. __list_add(&list, ascb_list->list.prev, &ascb_list->list);
  288. list_for_each_safe(pos, n, &list) {
  289. list_del_init(pos);
  290. asd_ascb_free(list_entry(pos, struct asd_ascb, list));
  291. }
  292. }
  293. /* ---------- Function declarations ---------- */
  294. int asd_init_hw(struct asd_ha_struct *asd_ha);
  295. irqreturn_t asd_hw_isr(int irq, void *dev_id);
  296. struct asd_ascb *asd_ascb_alloc_list(struct asd_ha_struct
  297. *asd_ha, int *num,
  298. gfp_t gfp_mask);
  299. int asd_post_ascb_list(struct asd_ha_struct *asd_ha, struct asd_ascb *ascb,
  300. int num);
  301. int asd_post_escb_list(struct asd_ha_struct *asd_ha, struct asd_ascb *ascb,
  302. int num);
  303. int asd_init_post_escbs(struct asd_ha_struct *asd_ha);
  304. void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc);
  305. void asd_control_led(struct asd_ha_struct *asd_ha, int phy_id, int op);
  306. void asd_turn_led(struct asd_ha_struct *asd_ha, int phy_id, int op);
  307. int asd_enable_phys(struct asd_ha_struct *asd_ha, const u8 phy_mask);
  308. void asd_ascb_timedout(struct timer_list *t);
  309. int asd_chip_hardrst(struct asd_ha_struct *asd_ha);
  310. #endif