1 /* 2 * Copyright (c) 2005-2014 Brocade Communications Systems, Inc. 3 * Copyright (c) 2014- QLogic Corporation. 4 * All rights reserved 5 * www.qlogic.com 6 * 7 * Linux driver for QLogic BR-series Fibre Channel Host Bus Adapter. 8 * 9 * This program is free software; you can redistribute it and/or modify it 10 * under the terms of the GNU General Public License (GPL) Version 2 as 11 * published by the Free Software Foundation 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 */ 18 19 /* 20 * bfa_cs.h BFA common services 21 */ 22 23 #ifndef __BFA_CS_H__ 24 #define __BFA_CS_H__ 25 26 #include "bfad_drv.h" 27 28 /* 29 * BFA TRC 30 */ 31 32 #ifndef BFA_TRC_MAX 33 #define BFA_TRC_MAX (4 * 1024) 34 #endif 35 36 #define BFA_TRC_TS(_trcm) \ 37 ({ \ 38 struct timeval tv; \ 39 \ 40 do_gettimeofday(&tv); \ 41 (tv.tv_sec*1000000+tv.tv_usec); \ 42 }) 43 44 #ifndef BFA_TRC_TS 45 #define BFA_TRC_TS(_trcm) ((_trcm)->ticks++) 46 #endif 47 48 struct bfa_trc_s { 49 #ifdef __BIG_ENDIAN 50 u16 fileno; 51 u16 line; 52 #else 53 u16 line; 54 u16 fileno; 55 #endif 56 u32 timestamp; 57 union { 58 struct { 59 u32 rsvd; 60 u32 u32; 61 } u32; 62 u64 u64; 63 } data; 64 }; 65 66 struct bfa_trc_mod_s { 67 u32 head; 68 u32 tail; 69 u32 ntrc; 70 u32 stopped; 71 u32 ticks; 72 u32 rsvd[3]; 73 struct bfa_trc_s trc[BFA_TRC_MAX]; 74 }; 75 76 enum { 77 BFA_TRC_HAL = 1, /* BFA modules */ 78 BFA_TRC_FCS = 2, /* BFA FCS modules */ 79 BFA_TRC_LDRV = 3, /* Linux driver modules */ 80 BFA_TRC_CNA = 4, /* Common modules */ 81 }; 82 #define BFA_TRC_MOD_SH 10 83 #define BFA_TRC_MOD(__mod) ((BFA_TRC_ ## __mod) << BFA_TRC_MOD_SH) 84 85 /* 86 * Define a new tracing file (module). Module should match one defined above. 87 */ 88 #define BFA_TRC_FILE(__mod, __submod) \ 89 static int __trc_fileno = ((BFA_TRC_ ## __mod ## _ ## __submod) | \ 90 BFA_TRC_MOD(__mod)) 91 92 93 #define bfa_trc32(_trcp, _data) \ 94 __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u32)_data) 95 #define bfa_trc(_trcp, _data) \ 96 __bfa_trc((_trcp)->trcmod, __trc_fileno, __LINE__, (u64)_data) 97 98 static inline void 99 bfa_trc_init(struct bfa_trc_mod_s *trcm) 100 { 101 trcm->head = trcm->tail = trcm->stopped = 0; 102 trcm->ntrc = BFA_TRC_MAX; 103 } 104 105 static inline void 106 bfa_trc_stop(struct bfa_trc_mod_s *trcm) 107 { 108 trcm->stopped = 1; 109 } 110 111 static inline void 112 __bfa_trc(struct bfa_trc_mod_s *trcm, int fileno, int line, u64 data) 113 { 114 int tail = trcm->tail; 115 struct bfa_trc_s *trc = &trcm->trc[tail]; 116 117 if (trcm->stopped) 118 return; 119 120 trc->fileno = (u16) fileno; 121 trc->line = (u16) line; 122 trc->data.u64 = data; 123 trc->timestamp = BFA_TRC_TS(trcm); 124 125 trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1); 126 if (trcm->tail == trcm->head) 127 trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1); 128 } 129 130 131 static inline void 132 __bfa_trc32(struct bfa_trc_mod_s *trcm, int fileno, int line, u32 data) 133 { 134 int tail = trcm->tail; 135 struct bfa_trc_s *trc = &trcm->trc[tail]; 136 137 if (trcm->stopped) 138 return; 139 140 trc->fileno = (u16) fileno; 141 trc->line = (u16) line; 142 trc->data.u32.u32 = data; 143 trc->timestamp = BFA_TRC_TS(trcm); 144 145 trcm->tail = (trcm->tail + 1) & (BFA_TRC_MAX - 1); 146 if (trcm->tail == trcm->head) 147 trcm->head = (trcm->head + 1) & (BFA_TRC_MAX - 1); 148 } 149 150 #define bfa_sm_fault(__mod, __event) do { \ 151 bfa_trc(__mod, (((u32)0xDEAD << 16) | __event)); \ 152 printk(KERN_ERR "Assertion failure: %s:%d: %d", \ 153 __FILE__, __LINE__, (__event)); \ 154 } while (0) 155 156 /* BFA queue definitions */ 157 #define bfa_q_first(_q) ((void *)(((struct list_head *) (_q))->next)) 158 #define bfa_q_next(_qe) (((struct list_head *) (_qe))->next) 159 #define bfa_q_prev(_qe) (((struct list_head *) (_qe))->prev) 160 161 /* 162 * bfa_q_qe_init - to initialize a queue element 163 */ 164 #define bfa_q_qe_init(_qe) { \ 165 bfa_q_next(_qe) = (struct list_head *) NULL; \ 166 bfa_q_prev(_qe) = (struct list_head *) NULL; \ 167 } 168 169 /* 170 * bfa_q_deq - dequeue an element from head of the queue 171 */ 172 #define bfa_q_deq(_q, _qe) do { \ 173 if (!list_empty(_q)) { \ 174 (*((struct list_head **) (_qe))) = bfa_q_next(_q); \ 175 bfa_q_prev(bfa_q_next(*((struct list_head **) _qe))) = \ 176 (struct list_head *) (_q); \ 177 bfa_q_next(_q) = bfa_q_next(*((struct list_head **) _qe));\ 178 } else { \ 179 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\ 180 } \ 181 } while (0) 182 183 /* 184 * bfa_q_deq_tail - dequeue an element from tail of the queue 185 */ 186 #define bfa_q_deq_tail(_q, _qe) { \ 187 if (!list_empty(_q)) { \ 188 *((struct list_head **) (_qe)) = bfa_q_prev(_q); \ 189 bfa_q_next(bfa_q_prev(*((struct list_head **) _qe))) = \ 190 (struct list_head *) (_q); \ 191 bfa_q_prev(_q) = bfa_q_prev(*(struct list_head **) _qe);\ 192 } else { \ 193 *((struct list_head **) (_qe)) = (struct list_head *) NULL;\ 194 } \ 195 } 196 197 static inline int 198 bfa_q_is_on_q_func(struct list_head *q, struct list_head *qe) 199 { 200 struct list_head *tqe; 201 202 tqe = bfa_q_next(q); 203 while (tqe != q) { 204 if (tqe == qe) 205 return 1; 206 tqe = bfa_q_next(tqe); 207 if (tqe == NULL) 208 break; 209 } 210 return 0; 211 } 212 213 #define bfa_q_is_on_q(_q, _qe) \ 214 bfa_q_is_on_q_func(_q, (struct list_head *)(_qe)) 215 216 /* 217 * @ BFA state machine interfaces 218 */ 219 220 typedef void (*bfa_sm_t)(void *sm, int event); 221 222 /* 223 * oc - object class eg. bfa_ioc 224 * st - state, eg. reset 225 * otype - object type, eg. struct bfa_ioc_s 226 * etype - object type, eg. enum ioc_event 227 */ 228 #define bfa_sm_state_decl(oc, st, otype, etype) \ 229 static void oc ## _sm_ ## st(otype * fsm, etype event) 230 231 #define bfa_sm_set_state(_sm, _state) ((_sm)->sm = (bfa_sm_t)(_state)) 232 #define bfa_sm_send_event(_sm, _event) ((_sm)->sm((_sm), (_event))) 233 #define bfa_sm_get_state(_sm) ((_sm)->sm) 234 #define bfa_sm_cmp_state(_sm, _state) ((_sm)->sm == (bfa_sm_t)(_state)) 235 236 /* 237 * For converting from state machine function to state encoding. 238 */ 239 struct bfa_sm_table_s { 240 bfa_sm_t sm; /* state machine function */ 241 int state; /* state machine encoding */ 242 char *name; /* state name for display */ 243 }; 244 #define BFA_SM(_sm) ((bfa_sm_t)(_sm)) 245 246 /* 247 * State machine with entry actions. 248 */ 249 typedef void (*bfa_fsm_t)(void *fsm, int event); 250 251 /* 252 * oc - object class eg. bfa_ioc 253 * st - state, eg. reset 254 * otype - object type, eg. struct bfa_ioc_s 255 * etype - object type, eg. enum ioc_event 256 */ 257 #define bfa_fsm_state_decl(oc, st, otype, etype) \ 258 static void oc ## _sm_ ## st(otype * fsm, etype event); \ 259 static void oc ## _sm_ ## st ## _entry(otype * fsm) 260 261 #define bfa_fsm_set_state(_fsm, _state) do { \ 262 (_fsm)->fsm = (bfa_fsm_t)(_state); \ 263 _state ## _entry(_fsm); \ 264 } while (0) 265 266 #define bfa_fsm_send_event(_fsm, _event) ((_fsm)->fsm((_fsm), (_event))) 267 #define bfa_fsm_get_state(_fsm) ((_fsm)->fsm) 268 #define bfa_fsm_cmp_state(_fsm, _state) \ 269 ((_fsm)->fsm == (bfa_fsm_t)(_state)) 270 271 static inline int 272 bfa_sm_to_state(struct bfa_sm_table_s *smt, bfa_sm_t sm) 273 { 274 int i = 0; 275 276 while (smt[i].sm && smt[i].sm != sm) 277 i++; 278 return smt[i].state; 279 } 280 281 /* 282 * @ Generic wait counter. 283 */ 284 285 typedef void (*bfa_wc_resume_t) (void *cbarg); 286 287 struct bfa_wc_s { 288 bfa_wc_resume_t wc_resume; 289 void *wc_cbarg; 290 int wc_count; 291 }; 292 293 static inline void 294 bfa_wc_up(struct bfa_wc_s *wc) 295 { 296 wc->wc_count++; 297 } 298 299 static inline void 300 bfa_wc_down(struct bfa_wc_s *wc) 301 { 302 wc->wc_count--; 303 if (wc->wc_count == 0) 304 wc->wc_resume(wc->wc_cbarg); 305 } 306 307 /* 308 * Initialize a waiting counter. 309 */ 310 static inline void 311 bfa_wc_init(struct bfa_wc_s *wc, bfa_wc_resume_t wc_resume, void *wc_cbarg) 312 { 313 wc->wc_resume = wc_resume; 314 wc->wc_cbarg = wc_cbarg; 315 wc->wc_count = 0; 316 bfa_wc_up(wc); 317 } 318 319 /* 320 * Wait for counter to reach zero 321 */ 322 static inline void 323 bfa_wc_wait(struct bfa_wc_s *wc) 324 { 325 bfa_wc_down(wc); 326 } 327 328 static inline void 329 wwn2str(char *wwn_str, u64 wwn) 330 { 331 union { 332 u64 wwn; 333 u8 byte[8]; 334 } w; 335 336 w.wwn = wwn; 337 sprintf(wwn_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", w.byte[0], 338 w.byte[1], w.byte[2], w.byte[3], w.byte[4], w.byte[5], 339 w.byte[6], w.byte[7]); 340 } 341 342 static inline void 343 fcid2str(char *fcid_str, u32 fcid) 344 { 345 union { 346 u32 fcid; 347 u8 byte[4]; 348 } f; 349 350 f.fcid = fcid; 351 sprintf(fcid_str, "%02x:%02x:%02x", f.byte[1], f.byte[2], f.byte[3]); 352 } 353 354 #define bfa_swap_3b(_x) \ 355 ((((_x) & 0xff) << 16) | \ 356 ((_x) & 0x00ff00) | \ 357 (((_x) & 0xff0000) >> 16)) 358 359 #ifndef __BIG_ENDIAN 360 #define bfa_hton3b(_x) bfa_swap_3b(_x) 361 #else 362 #define bfa_hton3b(_x) (_x) 363 #endif 364 365 #define bfa_ntoh3b(_x) bfa_hton3b(_x) 366 367 #endif /* __BFA_CS_H__ */ 368