[SCSI] bfa: Brocade-1860 Fabric Adapter vHBA support.
[firefly-linux-kernel-4.4.55.git] / drivers / scsi / bfa / bfa.h
1 /*
2  * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
3  * All rights reserved
4  * www.brocade.com
5  *
6  * Linux driver for Brocade Fibre Channel Host Bus Adapter.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License (GPL) Version 2 as
10  * published by the Free Software Foundation
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  */
17 #ifndef __BFA_H__
18 #define __BFA_H__
19
20 #include "bfad_drv.h"
21 #include "bfa_cs.h"
22 #include "bfa_plog.h"
23 #include "bfa_defs_svc.h"
24 #include "bfi.h"
25 #include "bfa_ioc.h"
26
27 struct bfa_s;
28
29 typedef void (*bfa_isr_func_t) (struct bfa_s *bfa, struct bfi_msg_s *m);
30 typedef void    (*bfa_cb_cbfn_t) (void *cbarg, bfa_boolean_t complete);
31
32 /*
33  * Interrupt message handlers
34  */
35 void bfa_isr_unhandled(struct bfa_s *bfa, struct bfi_msg_s *m);
36
37 /*
38  * Request and response queue related defines
39  */
40 #define BFA_REQQ_NELEMS_MIN     (4)
41 #define BFA_RSPQ_NELEMS_MIN     (4)
42
43 #define bfa_reqq_pi(__bfa, __reqq)      ((__bfa)->iocfc.req_cq_pi[__reqq])
44 #define bfa_reqq_ci(__bfa, __reqq)                                      \
45         (*(u32 *)((__bfa)->iocfc.req_cq_shadow_ci[__reqq].kva))
46
47 #define bfa_reqq_full(__bfa, __reqq)                            \
48         (((bfa_reqq_pi(__bfa, __reqq) + 1) &                    \
49           ((__bfa)->iocfc.cfg.drvcfg.num_reqq_elems - 1)) ==    \
50          bfa_reqq_ci(__bfa, __reqq))
51
52 #define bfa_reqq_next(__bfa, __reqq)                                    \
53         (bfa_reqq_full(__bfa, __reqq) ? NULL :                          \
54          ((void *)((struct bfi_msg_s *)((__bfa)->iocfc.req_cq_ba[__reqq].kva) \
55                    + bfa_reqq_pi((__bfa), (__reqq)))))
56
57 #define bfa_reqq_produce(__bfa, __reqq, __mh)  do {                     \
58                 (__mh).mtag.h2i.qid     = (__bfa)->iocfc.hw_qid[__reqq];\
59                 (__bfa)->iocfc.req_cq_pi[__reqq]++;                     \
60                 (__bfa)->iocfc.req_cq_pi[__reqq] &=                     \
61                         ((__bfa)->iocfc.cfg.drvcfg.num_reqq_elems - 1); \
62                 writel((__bfa)->iocfc.req_cq_pi[__reqq],                \
63                         (__bfa)->iocfc.bfa_regs.cpe_q_pi[__reqq]);      \
64                 mmiowb();      \
65         } while (0)
66
67 #define bfa_rspq_pi(__bfa, __rspq)                                      \
68         (*(u32 *)((__bfa)->iocfc.rsp_cq_shadow_pi[__rspq].kva))
69
70 #define bfa_rspq_ci(__bfa, __rspq)      ((__bfa)->iocfc.rsp_cq_ci[__rspq])
71 #define bfa_rspq_elem(__bfa, __rspq, __ci)                              \
72         (&((struct bfi_msg_s *)((__bfa)->iocfc.rsp_cq_ba[__rspq].kva))[__ci])
73
74 #define CQ_INCR(__index, __size) do {                   \
75         (__index)++;                                    \
76         (__index) &= ((__size) - 1);                    \
77 } while (0)
78
79 /*
80  * Queue element to wait for room in request queue. FIFO order is
81  * maintained when fullfilling requests.
82  */
83 struct bfa_reqq_wait_s {
84         struct list_head        qe;
85         void            (*qresume) (void *cbarg);
86         void            *cbarg;
87 };
88
89 /*
90  * Circular queue usage assignments
91  */
92 enum {
93         BFA_REQQ_IOC    = 0,    /*  all low-priority IOC msgs   */
94         BFA_REQQ_FCXP   = 0,    /*  all FCXP messages           */
95         BFA_REQQ_LPS    = 0,    /*  all lport service msgs      */
96         BFA_REQQ_PORT   = 0,    /*  all port messages           */
97         BFA_REQQ_FLASH  = 0,    /*  for flash module            */
98         BFA_REQQ_DIAG   = 0,    /*  for diag module             */
99         BFA_REQQ_RPORT  = 0,    /*  all port messages           */
100         BFA_REQQ_SBOOT  = 0,    /*  all san boot messages       */
101         BFA_REQQ_QOS_LO = 1,    /*  all low priority IO */
102         BFA_REQQ_QOS_MD = 2,    /*  all medium priority IO      */
103         BFA_REQQ_QOS_HI = 3,    /*  all high priority IO        */
104 };
105
106 static inline void
107 bfa_reqq_winit(struct bfa_reqq_wait_s *wqe, void (*qresume) (void *cbarg),
108                void *cbarg)
109 {
110         wqe->qresume = qresume;
111         wqe->cbarg = cbarg;
112 }
113
114 #define bfa_reqq(__bfa, __reqq) (&(__bfa)->reqq_waitq[__reqq])
115
116 /*
117  * static inline void
118  * bfa_reqq_wait(struct bfa_s *bfa, int reqq, struct bfa_reqq_wait_s *wqe)
119  */
120 #define bfa_reqq_wait(__bfa, __reqq, __wqe) do {                        \
121                                                                         \
122                 struct list_head *waitq = bfa_reqq(__bfa, __reqq);      \
123                                                                         \
124                 WARN_ON(((__reqq) >= BFI_IOC_MAX_CQS));                 \
125                 WARN_ON(!((__wqe)->qresume && (__wqe)->cbarg));         \
126                                                                         \
127                 list_add_tail(&(__wqe)->qe, waitq);      \
128         } while (0)
129
130 #define bfa_reqq_wcancel(__wqe) list_del(&(__wqe)->qe)
131
132
133 /*
134  * Generic BFA callback element.
135  */
136 struct bfa_cb_qe_s {
137         struct list_head         qe;
138         bfa_cb_cbfn_t  cbfn;
139         bfa_boolean_t   once;
140         void           *cbarg;
141 };
142
143 #define bfa_cb_queue(__bfa, __hcb_qe, __cbfn, __cbarg) do {     \
144                 (__hcb_qe)->cbfn  = (__cbfn);      \
145                 (__hcb_qe)->cbarg = (__cbarg);      \
146                 list_add_tail(&(__hcb_qe)->qe, &(__bfa)->comp_q);      \
147         } while (0)
148
149 #define bfa_cb_dequeue(__hcb_qe)        list_del(&(__hcb_qe)->qe)
150
151 #define bfa_cb_queue_once(__bfa, __hcb_qe, __cbfn, __cbarg) do {        \
152                 (__hcb_qe)->cbfn  = (__cbfn);      \
153                 (__hcb_qe)->cbarg = (__cbarg);      \
154                 if (!(__hcb_qe)->once) {      \
155                         list_add_tail(&(__hcb_qe)->qe, &(__bfa)->comp_q);      \
156                         (__hcb_qe)->once = BFA_TRUE;                    \
157                 }                                                       \
158         } while (0)
159
160 #define bfa_cb_queue_done(__hcb_qe) do {        \
161                 (__hcb_qe)->once = BFA_FALSE;   \
162         } while (0)
163
164
165 /*
166  * PCI devices supported by the current BFA
167  */
168 struct bfa_pciid_s {
169         u16     device_id;
170         u16     vendor_id;
171 };
172
173 extern char     bfa_version[];
174
175 /*
176  * BFA memory resources
177  */
178 enum bfa_mem_type {
179         BFA_MEM_TYPE_KVA = 1,   /*  Kernel Virtual Memory *(non-dma-able) */
180         BFA_MEM_TYPE_DMA = 2,   /*  DMA-able memory */
181         BFA_MEM_TYPE_MAX = BFA_MEM_TYPE_DMA,
182 };
183
184 struct bfa_mem_elem_s {
185         enum bfa_mem_type mem_type;     /* see enum bfa_mem_type */
186         u32     mem_len;        /*  Total Length in Bytes       */
187         u8              *kva;           /*  kernel virtual address      */
188         u64     dma;            /*  dma address if DMA memory   */
189         u8              *kva_curp;      /*  kva allocation cursor       */
190         u64     dma_curp;       /*  dma allocation cursor       */
191 };
192
193 struct bfa_meminfo_s {
194         struct bfa_mem_elem_s meminfo[BFA_MEM_TYPE_MAX];
195 };
196 #define bfa_meminfo_kva(_m)                             \
197         ((_m)->meminfo[BFA_MEM_TYPE_KVA - 1].kva_curp)
198 #define bfa_meminfo_dma_virt(_m)                        \
199         ((_m)->meminfo[BFA_MEM_TYPE_DMA - 1].kva_curp)
200 #define bfa_meminfo_dma_phys(_m)                        \
201         ((_m)->meminfo[BFA_MEM_TYPE_DMA - 1].dma_curp)
202
203 struct bfa_iocfc_regs_s {
204         void __iomem    *intr_status;
205         void __iomem    *intr_mask;
206         void __iomem    *cpe_q_pi[BFI_IOC_MAX_CQS];
207         void __iomem    *cpe_q_ci[BFI_IOC_MAX_CQS];
208         void __iomem    *cpe_q_ctrl[BFI_IOC_MAX_CQS];
209         void __iomem    *rme_q_ci[BFI_IOC_MAX_CQS];
210         void __iomem    *rme_q_pi[BFI_IOC_MAX_CQS];
211         void __iomem    *rme_q_ctrl[BFI_IOC_MAX_CQS];
212 };
213
214 /*
215  * MSIX vector handlers
216  */
217 #define BFA_MSIX_MAX_VECTORS    22
218 typedef void (*bfa_msix_handler_t)(struct bfa_s *bfa, int vec);
219 struct bfa_msix_s {
220         int     nvecs;
221         bfa_msix_handler_t handler[BFA_MSIX_MAX_VECTORS];
222 };
223
224 /*
225  * Chip specific interfaces
226  */
227 struct bfa_hwif_s {
228         void (*hw_reginit)(struct bfa_s *bfa);
229         void (*hw_reqq_ack)(struct bfa_s *bfa, int reqq);
230         void (*hw_rspq_ack)(struct bfa_s *bfa, int rspq);
231         void (*hw_msix_init)(struct bfa_s *bfa, int nvecs);
232         void (*hw_msix_ctrl_install)(struct bfa_s *bfa);
233         void (*hw_msix_queue_install)(struct bfa_s *bfa);
234         void (*hw_msix_uninstall)(struct bfa_s *bfa);
235         void (*hw_isr_mode_set)(struct bfa_s *bfa, bfa_boolean_t msix);
236         void (*hw_msix_getvecs)(struct bfa_s *bfa, u32 *vecmap,
237                                 u32 *nvecs, u32 *maxvec);
238         void (*hw_msix_get_rme_range) (struct bfa_s *bfa, u32 *start,
239                                        u32 *end);
240         int     cpe_vec_q0;
241         int     rme_vec_q0;
242 };
243 typedef void (*bfa_cb_iocfc_t) (void *cbarg, enum bfa_status status);
244
245 struct bfa_faa_cbfn_s {
246         bfa_cb_iocfc_t  faa_cbfn;
247         void            *faa_cbarg;
248 };
249
250 #define BFA_FAA_ENABLED         1
251 #define BFA_FAA_DISABLED        2
252
253 /*
254  *      FAA attributes
255  */
256 struct bfa_faa_attr_s {
257         wwn_t   faa;
258         u8      faa_state;
259         u8      pwwn_source;
260         u8      rsvd[6];
261 };
262
263 struct bfa_faa_args_s {
264         struct bfa_faa_attr_s   *faa_attr;
265         struct bfa_faa_cbfn_s   faa_cb;
266         u8                      faa_state;
267         bfa_boolean_t           busy;
268 };
269
270 struct bfa_iocfc_s {
271         struct bfa_s            *bfa;
272         struct bfa_iocfc_cfg_s  cfg;
273         int                     action;
274         u32             req_cq_pi[BFI_IOC_MAX_CQS];
275         u32             rsp_cq_ci[BFI_IOC_MAX_CQS];
276         u8              hw_qid[BFI_IOC_MAX_CQS];
277         struct bfa_cb_qe_s      init_hcb_qe;
278         struct bfa_cb_qe_s      stop_hcb_qe;
279         struct bfa_cb_qe_s      dis_hcb_qe;
280         struct bfa_cb_qe_s      stats_hcb_qe;
281         bfa_boolean_t           cfgdone;
282
283         struct bfa_dma_s        cfg_info;
284         struct bfi_iocfc_cfg_s *cfginfo;
285         struct bfa_dma_s        cfgrsp_dma;
286         struct bfi_iocfc_cfgrsp_s *cfgrsp;
287         struct bfa_dma_s        req_cq_ba[BFI_IOC_MAX_CQS];
288         struct bfa_dma_s        req_cq_shadow_ci[BFI_IOC_MAX_CQS];
289         struct bfa_dma_s        rsp_cq_ba[BFI_IOC_MAX_CQS];
290         struct bfa_dma_s        rsp_cq_shadow_pi[BFI_IOC_MAX_CQS];
291         struct bfa_iocfc_regs_s bfa_regs;       /*  BFA device registers */
292         struct bfa_hwif_s       hwif;
293         bfa_cb_iocfc_t          updateq_cbfn; /*  bios callback function */
294         void                    *updateq_cbarg; /*  bios callback arg */
295         u32     intr_mask;
296         struct bfa_faa_args_s   faa_args;
297 };
298
299 #define bfa_fn_lpu(__bfa)       \
300         bfi_fn_lpu(bfa_ioc_pcifn(&(__bfa)->ioc), bfa_ioc_portid(&(__bfa)->ioc))
301 #define bfa_msix_init(__bfa, __nvecs)                                   \
302         ((__bfa)->iocfc.hwif.hw_msix_init(__bfa, __nvecs))
303 #define bfa_msix_ctrl_install(__bfa)                                    \
304         ((__bfa)->iocfc.hwif.hw_msix_ctrl_install(__bfa))
305 #define bfa_msix_queue_install(__bfa)                                   \
306         ((__bfa)->iocfc.hwif.hw_msix_queue_install(__bfa))
307 #define bfa_msix_uninstall(__bfa)                                       \
308         ((__bfa)->iocfc.hwif.hw_msix_uninstall(__bfa))
309 #define bfa_isr_rspq_ack(__bfa, __queue) do {                           \
310         if ((__bfa)->iocfc.hwif.hw_rspq_ack)                            \
311                 (__bfa)->iocfc.hwif.hw_rspq_ack(__bfa, __queue);        \
312 } while (0)
313 #define bfa_isr_reqq_ack(__bfa, __queue) do {                           \
314         if ((__bfa)->iocfc.hwif.hw_reqq_ack)                            \
315                 (__bfa)->iocfc.hwif.hw_reqq_ack(__bfa, __queue);        \
316 } while (0)
317 #define bfa_isr_mode_set(__bfa, __msix) do {                            \
318         if ((__bfa)->iocfc.hwif.hw_isr_mode_set)                        \
319                 (__bfa)->iocfc.hwif.hw_isr_mode_set(__bfa, __msix);     \
320 } while (0)
321 #define bfa_msix_getvecs(__bfa, __vecmap, __nvecs, __maxvec)            \
322         ((__bfa)->iocfc.hwif.hw_msix_getvecs(__bfa, __vecmap,           \
323                                         __nvecs, __maxvec))
324 #define bfa_msix_get_rme_range(__bfa, __start, __end)                   \
325         ((__bfa)->iocfc.hwif.hw_msix_get_rme_range(__bfa, __start, __end))
326 #define bfa_msix(__bfa, __vec)                                          \
327         ((__bfa)->msix.handler[__vec](__bfa, __vec))
328
329 /*
330  * FC specific IOC functions.
331  */
332 void bfa_iocfc_meminfo(struct bfa_iocfc_cfg_s *cfg, u32 *km_len,
333                        u32 *dm_len);
334 void bfa_iocfc_attach(struct bfa_s *bfa, void *bfad,
335                       struct bfa_iocfc_cfg_s *cfg,
336                       struct bfa_meminfo_s *meminfo,
337                       struct bfa_pcidev_s *pcidev);
338 void bfa_iocfc_init(struct bfa_s *bfa);
339 void bfa_iocfc_start(struct bfa_s *bfa);
340 void bfa_iocfc_stop(struct bfa_s *bfa);
341 void bfa_iocfc_isr(void *bfa, struct bfi_mbmsg_s *msg);
342 void bfa_iocfc_set_snsbase(struct bfa_s *bfa, u64 snsbase_pa);
343 bfa_boolean_t bfa_iocfc_is_operational(struct bfa_s *bfa);
344 void bfa_iocfc_reset_queues(struct bfa_s *bfa);
345
346 void bfa_msix_all(struct bfa_s *bfa, int vec);
347 void bfa_msix_reqq(struct bfa_s *bfa, int vec);
348 void bfa_msix_rspq(struct bfa_s *bfa, int vec);
349 void bfa_msix_lpu_err(struct bfa_s *bfa, int vec);
350
351 void bfa_hwcb_reginit(struct bfa_s *bfa);
352 void bfa_hwcb_rspq_ack(struct bfa_s *bfa, int rspq);
353 void bfa_hwcb_msix_init(struct bfa_s *bfa, int nvecs);
354 void bfa_hwcb_msix_ctrl_install(struct bfa_s *bfa);
355 void bfa_hwcb_msix_queue_install(struct bfa_s *bfa);
356 void bfa_hwcb_msix_uninstall(struct bfa_s *bfa);
357 void bfa_hwcb_isr_mode_set(struct bfa_s *bfa, bfa_boolean_t msix);
358 void bfa_hwcb_msix_getvecs(struct bfa_s *bfa, u32 *vecmap, u32 *nvecs,
359                            u32 *maxvec);
360 void bfa_hwcb_msix_get_rme_range(struct bfa_s *bfa, u32 *start,
361                                  u32 *end);
362 void bfa_hwct_reginit(struct bfa_s *bfa);
363 void bfa_hwct2_reginit(struct bfa_s *bfa);
364 void bfa_hwct_reqq_ack(struct bfa_s *bfa, int rspq);
365 void bfa_hwct_rspq_ack(struct bfa_s *bfa, int rspq);
366 void bfa_hwct_msix_init(struct bfa_s *bfa, int nvecs);
367 void bfa_hwct_msix_ctrl_install(struct bfa_s *bfa);
368 void bfa_hwct_msix_queue_install(struct bfa_s *bfa);
369 void bfa_hwct_msix_uninstall(struct bfa_s *bfa);
370 void bfa_hwct_isr_mode_set(struct bfa_s *bfa, bfa_boolean_t msix);
371 void bfa_hwct_msix_getvecs(struct bfa_s *bfa, u32 *vecmap, u32 *nvecs,
372                            u32 *maxvec);
373 void bfa_hwct_msix_get_rme_range(struct bfa_s *bfa, u32 *start,
374                                  u32 *end);
375 void bfa_iocfc_get_bootwwns(struct bfa_s *bfa, u8 *nwwns, wwn_t *wwns);
376 wwn_t bfa_iocfc_get_pwwn(struct bfa_s *bfa);
377 wwn_t bfa_iocfc_get_nwwn(struct bfa_s *bfa);
378 int bfa_iocfc_get_pbc_vports(struct bfa_s *bfa,
379                                 struct bfi_pbc_vport_s *pbc_vport);
380
381
382 /*
383  *----------------------------------------------------------------------
384  *              BFA public interfaces
385  *----------------------------------------------------------------------
386  */
387 #define bfa_stats(_mod, _stats) ((_mod)->stats._stats++)
388 #define bfa_ioc_get_stats(__bfa, __ioc_stats)           \
389         bfa_ioc_fetch_stats(&(__bfa)->ioc, __ioc_stats)
390 #define bfa_ioc_clear_stats(__bfa)              \
391         bfa_ioc_clr_stats(&(__bfa)->ioc)
392 #define bfa_get_nports(__bfa)                   \
393         bfa_ioc_get_nports(&(__bfa)->ioc)
394 #define bfa_get_adapter_manufacturer(__bfa, __manufacturer)             \
395         bfa_ioc_get_adapter_manufacturer(&(__bfa)->ioc, __manufacturer)
396 #define bfa_get_adapter_model(__bfa, __model)                   \
397         bfa_ioc_get_adapter_model(&(__bfa)->ioc, __model)
398 #define bfa_get_adapter_serial_num(__bfa, __serial_num)                 \
399         bfa_ioc_get_adapter_serial_num(&(__bfa)->ioc, __serial_num)
400 #define bfa_get_adapter_fw_ver(__bfa, __fw_ver)                 \
401         bfa_ioc_get_adapter_fw_ver(&(__bfa)->ioc, __fw_ver)
402 #define bfa_get_adapter_optrom_ver(__bfa, __optrom_ver)                 \
403         bfa_ioc_get_adapter_optrom_ver(&(__bfa)->ioc, __optrom_ver)
404 #define bfa_get_pci_chip_rev(__bfa, __chip_rev)                 \
405         bfa_ioc_get_pci_chip_rev(&(__bfa)->ioc, __chip_rev)
406 #define bfa_get_ioc_state(__bfa)                \
407         bfa_ioc_get_state(&(__bfa)->ioc)
408 #define bfa_get_type(__bfa)                     \
409         bfa_ioc_get_type(&(__bfa)->ioc)
410 #define bfa_get_mac(__bfa)                      \
411         bfa_ioc_get_mac(&(__bfa)->ioc)
412 #define bfa_get_mfg_mac(__bfa)                  \
413         bfa_ioc_get_mfg_mac(&(__bfa)->ioc)
414 #define bfa_get_fw_clock_res(__bfa)             \
415         ((__bfa)->iocfc.cfgrsp->fwcfg.fw_tick_res)
416
417 void bfa_get_pciids(struct bfa_pciid_s **pciids, int *npciids);
418 void bfa_cfg_get_default(struct bfa_iocfc_cfg_s *cfg);
419 void bfa_cfg_get_min(struct bfa_iocfc_cfg_s *cfg);
420 void bfa_cfg_get_meminfo(struct bfa_iocfc_cfg_s *cfg,
421                          struct bfa_meminfo_s *meminfo);
422 void bfa_attach(struct bfa_s *bfa, void *bfad, struct bfa_iocfc_cfg_s *cfg,
423                 struct bfa_meminfo_s *meminfo,
424                 struct bfa_pcidev_s *pcidev);
425 void bfa_detach(struct bfa_s *bfa);
426 void bfa_cb_init(void *bfad, bfa_status_t status);
427 void bfa_cb_updateq(void *bfad, bfa_status_t status);
428
429 bfa_boolean_t bfa_intx(struct bfa_s *bfa);
430 void bfa_isr_enable(struct bfa_s *bfa);
431 void bfa_isr_disable(struct bfa_s *bfa);
432
433 void bfa_comp_deq(struct bfa_s *bfa, struct list_head *comp_q);
434 void bfa_comp_process(struct bfa_s *bfa, struct list_head *comp_q);
435 void bfa_comp_free(struct bfa_s *bfa, struct list_head *comp_q);
436
437 typedef void (*bfa_cb_ioc_t) (void *cbarg, enum bfa_status status);
438 void bfa_iocfc_get_attr(struct bfa_s *bfa, struct bfa_iocfc_attr_s *attr);
439
440
441 bfa_status_t bfa_iocfc_israttr_set(struct bfa_s *bfa,
442                                    struct bfa_iocfc_intr_attr_s *attr);
443
444 void bfa_iocfc_enable(struct bfa_s *bfa);
445 void bfa_iocfc_disable(struct bfa_s *bfa);
446 #define bfa_timer_start(_bfa, _timer, _timercb, _arg, _timeout)         \
447         bfa_timer_begin(&(_bfa)->timer_mod, _timer, _timercb, _arg, _timeout)
448
449 #endif /* __BFA_H__ */