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