Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-only */
0002 /*
0003  * Copyright (c) 2005-2014 Brocade Communications Systems, Inc.
0004  * Copyright (c) 2014- QLogic Corporation.
0005  * All rights reserved
0006  * www.qlogic.com
0007  *
0008  * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter.
0009  */
0010 
0011 /*
0012  *  bfa_cs.h BFA common services
0013  */
0014 
0015 #ifndef __BFA_CS_H__
0016 #define __BFA_CS_H__
0017 
0018 #include "bfad_drv.h"
0019 
0020 /*
0021  * BFA TRC
0022  */
0023 
0024 #ifndef BFA_TRC_MAX
0025 #define BFA_TRC_MAX (4 * 1024)
0026 #endif
0027 
0028 #define BFA_TRC_TS(_trcm)                               \
0029     ({                                              \
0030         struct timespec64 ts;                   \
0031                             \
0032         ktime_get_ts64(&ts);                    \
0033         (ts.tv_sec*1000000+ts.tv_nsec / 1000);  \
0034     })
0035 
0036 #ifndef BFA_TRC_TS
0037 #define BFA_TRC_TS(_trcm)   ((_trcm)->ticks++)
0038 #endif
0039 
0040 struct bfa_trc_s {
0041 #ifdef __BIG_ENDIAN
0042     u16 fileno;
0043     u16 line;
0044 #else
0045     u16 line;
0046     u16 fileno;
0047 #endif
0048     u32 timestamp;
0049     union {
0050         struct {
0051             u32 rsvd;
0052             u32 u32;
0053         } u32;
0054         u64 u64;
0055     } data;
0056 };
0057 
0058 struct bfa_trc_mod_s {
0059     u32 head;
0060     u32 tail;
0061     u32 ntrc;
0062     u32 stopped;
0063     u32 ticks;
0064     u32 rsvd[3];
0065     struct bfa_trc_s trc[BFA_TRC_MAX];
0066 };
0067 
0068 enum {
0069     BFA_TRC_HAL  = 1,   /*  BFA modules */
0070     BFA_TRC_FCS  = 2,   /*  BFA FCS modules */
0071     BFA_TRC_LDRV = 3,   /*  Linux driver modules */
0072     BFA_TRC_CNA  = 4,   /*  Common modules */
0073 };
0074 #define BFA_TRC_MOD_SH  10
0075 #define BFA_TRC_MOD(__mod)  ((BFA_TRC_ ## __mod) << BFA_TRC_MOD_SH)
0076 
0077 /*
0078  * Define a new tracing file (module). Module should match one defined above.
0079  */
0080 #define BFA_TRC_FILE(__mod, __submod)                   \
0081     static int __trc_fileno = ((BFA_TRC_ ## __mod ## _ ## __submod) | \
0082                          BFA_TRC_MOD(__mod))
0083 
0084 
0085 #define bfa_trc32(_trcp, _data) \
0086     __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u32)_data)
0087 #define bfa_trc(_trcp, _data)   \
0088     __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u64)_data)
0089 
0090 static inline void
0091 bfa_trc_init(struct bfa_trc_mod_s *trcm)
0092 {
0093     trcm->head = trcm->tail = trcm->stopped = 0;
0094     trcm->ntrc = BFA_TRC_MAX;
0095 }
0096 
0097 static inline void
0098 bfa_trc_stop(struct bfa_trc_mod_s *trcm)
0099 {
0100     trcm->stopped = 1;
0101 }
0102 
0103 void
0104 __bfa_trc(struct bfa_trc_mod_s *trcm, int fileno, int line, u64 data);
0105 
0106 void
0107 __bfa_trc32(struct bfa_trc_mod_s *trcm, int fileno, int line, u32 data);
0108 
0109 #define bfa_sm_fault(__mod, __event)    do {                \
0110     bfa_trc(__mod, (((u32)0xDEAD << 16) | __event));        \
0111     printk(KERN_ERR "Assertion failure: %s:%d: %d",         \
0112         __FILE__, __LINE__, (__event));             \
0113 } while (0)
0114 
0115 /* BFA queue definitions */
0116 #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next))
0117 #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next)
0118 #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev)
0119 
0120 /*
0121  * bfa_q_qe_init - to initialize a queue element
0122  */
0123 #define bfa_q_qe_init(_qe) {                \
0124     bfa_q_next(_qe) = (struct list_head *) NULL;    \
0125     bfa_q_prev(_qe) = (struct list_head *) NULL;    \
0126 }
0127 
0128 /*
0129  * bfa_q_deq - dequeue an element from head of the queue
0130  */
0131 #define bfa_q_deq(_q, _qe) do {                     \
0132     if (!list_empty(_q)) {                      \
0133         (*((struct list_head **) (_qe))) = bfa_q_next(_q);  \
0134         bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) =  \
0135                 (struct list_head *) (_q);      \
0136         bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\
0137     } else {                            \
0138         *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
0139     }                               \
0140 } while (0)
0141 
0142 /*
0143  * bfa_q_deq_tail - dequeue an element from tail of the queue
0144  */
0145 #define bfa_q_deq_tail(_q, _qe) {                   \
0146     if (!list_empty(_q)) {                      \
0147         *((struct list_head **) (_qe)) = bfa_q_prev(_q);    \
0148         bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) =  \
0149             (struct list_head *) (_q);          \
0150         bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\
0151     } else {                            \
0152         *((struct list_head **) (_qe)) = (struct list_head *) NULL;\
0153     }                               \
0154 }
0155 
0156 static inline int
0157 bfa_q_is_on_q_func(struct list_head *q, struct list_head *qe)
0158 {
0159     struct list_head        *tqe;
0160 
0161     tqe = bfa_q_next(q);
0162     while (tqe != q) {
0163         if (tqe == qe)
0164             return 1;
0165         tqe = bfa_q_next(tqe);
0166         if (tqe == NULL)
0167             break;
0168     }
0169     return 0;
0170 }
0171 
0172 #define bfa_q_is_on_q(_q, _qe)      \
0173     bfa_q_is_on_q_func(_q, (struct list_head *)(_qe))
0174 
0175 /*
0176  * @ BFA state machine interfaces
0177  */
0178 
0179 typedef void (*bfa_sm_t)(void *sm, int event);
0180 
0181 /*
0182  * oc - object class eg. bfa_ioc
0183  * st - state, eg. reset
0184  * otype - object type, eg. struct bfa_ioc_s
0185  * etype - object type, eg. enum ioc_event
0186  */
0187 #define bfa_sm_state_decl(oc, st, otype, etype)     \
0188     static void oc ## _sm_ ## st(otype * fsm, etype event)
0189 
0190 #define bfa_sm_set_state(_sm, _state)   ((_sm)->sm = (bfa_sm_t)(_state))
0191 #define bfa_sm_send_event(_sm, _event)  ((_sm)->sm((_sm), (_event)))
0192 #define bfa_sm_get_state(_sm)       ((_sm)->sm)
0193 #define bfa_sm_cmp_state(_sm, _state)   ((_sm)->sm == (bfa_sm_t)(_state))
0194 
0195 /*
0196  * For converting from state machine function to state encoding.
0197  */
0198 struct bfa_sm_table_s {
0199     bfa_sm_t    sm; /*  state machine function  */
0200     int     state;  /*  state machine encoding  */
0201     char        *name;  /*  state name for display  */
0202 };
0203 #define BFA_SM(_sm) ((bfa_sm_t)(_sm))
0204 
0205 /*
0206  * State machine with entry actions.
0207  */
0208 typedef void (*bfa_fsm_t)(void *fsm, int event);
0209 
0210 /*
0211  * oc - object class eg. bfa_ioc
0212  * st - state, eg. reset
0213  * otype - object type, eg. struct bfa_ioc_s
0214  * etype - object type, eg. enum ioc_event
0215  */
0216 #define bfa_fsm_state_decl(oc, st, otype, etype)        \
0217     static void oc ## _sm_ ## st(otype * fsm, etype event);      \
0218     static void oc ## _sm_ ## st ## _entry(otype * fsm)
0219 
0220 #define bfa_fsm_set_state(_fsm, _state) do {    \
0221     (_fsm)->fsm = (bfa_fsm_t)(_state);      \
0222     _state ## _entry(_fsm);      \
0223 } while (0)
0224 
0225 #define bfa_fsm_send_event(_fsm, _event)    ((_fsm)->fsm((_fsm), (_event)))
0226 #define bfa_fsm_get_state(_fsm)         ((_fsm)->fsm)
0227 #define bfa_fsm_cmp_state(_fsm, _state)     \
0228     ((_fsm)->fsm == (bfa_fsm_t)(_state))
0229 
0230 static inline int
0231 bfa_sm_to_state(struct bfa_sm_table_s *smt, bfa_sm_t sm)
0232 {
0233     int i = 0;
0234 
0235     while (smt[i].sm && smt[i].sm != sm)
0236         i++;
0237     return smt[i].state;
0238 }
0239 
0240 /*
0241  * @ Generic wait counter.
0242  */
0243 
0244 typedef void (*bfa_wc_resume_t) (void *cbarg);
0245 
0246 struct bfa_wc_s {
0247     bfa_wc_resume_t wc_resume;
0248     void        *wc_cbarg;
0249     int     wc_count;
0250 };
0251 
0252 static inline void
0253 bfa_wc_up(struct bfa_wc_s *wc)
0254 {
0255     wc->wc_count++;
0256 }
0257 
0258 static inline void
0259 bfa_wc_down(struct bfa_wc_s *wc)
0260 {
0261     wc->wc_count--;
0262     if (wc->wc_count == 0)
0263         wc->wc_resume(wc->wc_cbarg);
0264 }
0265 
0266 /*
0267  * Initialize a waiting counter.
0268  */
0269 static inline void
0270 bfa_wc_init(struct bfa_wc_s *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg)
0271 {
0272     wc->wc_resume = wc_resume;
0273     wc->wc_cbarg = wc_cbarg;
0274     wc->wc_count = 0;
0275     bfa_wc_up(wc);
0276 }
0277 
0278 /*
0279  * Wait for counter to reach zero
0280  */
0281 static inline void
0282 bfa_wc_wait(struct bfa_wc_s *wc)
0283 {
0284     bfa_wc_down(wc);
0285 }
0286 
0287 static inline void
0288 wwn2str(char *wwn_str, u64 wwn)
0289 {
0290     union {
0291         u64 wwn;
0292         u8 byte[8];
0293     } w;
0294 
0295     w.wwn = wwn;
0296     sprintf(wwn_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", w.byte[0],
0297         w.byte[1], w.byte[2], w.byte[3], w.byte[4], w.byte[5],
0298         w.byte[6], w.byte[7]);
0299 }
0300 
0301 static inline void
0302 fcid2str(char *fcid_str, u32 fcid)
0303 {
0304     union {
0305         u32 fcid;
0306         u8 byte[4];
0307     } f;
0308 
0309     f.fcid = fcid;
0310     sprintf(fcid_str, "%02x:%02x:%02x", f.byte[1], f.byte[2], f.byte[3]);
0311 }
0312 
0313 #define bfa_swap_3b(_x)             \
0314     ((((_x) & 0xff) << 16) |        \
0315     ((_x) & 0x00ff00) |         \
0316     (((_x) & 0xff0000) >> 16))
0317 
0318 #ifndef __BIG_ENDIAN
0319 #define bfa_hton3b(_x)  bfa_swap_3b(_x)
0320 #else
0321 #define bfa_hton3b(_x)  (_x)
0322 #endif
0323 
0324 #define bfa_ntoh3b(_x)  bfa_hton3b(_x)
0325 
0326 #endif /* __BFA_CS_H__ */