1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * This file is part of the Chelsio FCoE driver for Linux.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * This software is available to you under a choice of one of two
7*4882a593Smuzhiyun * licenses. You may choose to be licensed under the terms of the GNU
8*4882a593Smuzhiyun * General Public License (GPL) Version 2, available from the file
9*4882a593Smuzhiyun * COPYING in the main directory of this source tree, or the
10*4882a593Smuzhiyun * OpenIB.org BSD license below:
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or
13*4882a593Smuzhiyun * without modification, are permitted provided that the following
14*4882a593Smuzhiyun * conditions are met:
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * - Redistributions of source code must retain the above
17*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
18*4882a593Smuzhiyun * disclaimer.
19*4882a593Smuzhiyun *
20*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above
21*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
22*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials
23*4882a593Smuzhiyun * provided with the distribution.
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28*4882a593Smuzhiyun * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29*4882a593Smuzhiyun * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30*4882a593Smuzhiyun * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31*4882a593Smuzhiyun * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32*4882a593Smuzhiyun * SOFTWARE.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun #ifndef __CSIO_DEFS_H__
36*4882a593Smuzhiyun #define __CSIO_DEFS_H__
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #include <linux/kernel.h>
39*4882a593Smuzhiyun #include <linux/stddef.h>
40*4882a593Smuzhiyun #include <linux/timer.h>
41*4882a593Smuzhiyun #include <linux/list.h>
42*4882a593Smuzhiyun #include <linux/bug.h>
43*4882a593Smuzhiyun #include <linux/pci.h>
44*4882a593Smuzhiyun #include <linux/jiffies.h>
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun #define CSIO_INVALID_IDX 0xFFFFFFFF
47*4882a593Smuzhiyun #define CSIO_INC_STATS(elem, val) ((elem)->stats.val++)
48*4882a593Smuzhiyun #define CSIO_DEC_STATS(elem, val) ((elem)->stats.val--)
49*4882a593Smuzhiyun #define CSIO_VALID_WWN(__n) ((*__n >> 4) == 0x5 ? true : false)
50*4882a593Smuzhiyun #define CSIO_DID_MASK 0xFFFFFF
51*4882a593Smuzhiyun #define CSIO_WORD_TO_BYTE 4
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #ifndef readq
readq(void __iomem * addr)54*4882a593Smuzhiyun static inline u64 readq(void __iomem *addr)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun return readl(addr) + ((u64)readl(addr + 4) << 32);
57*4882a593Smuzhiyun }
58*4882a593Smuzhiyun
writeq(u64 val,void __iomem * addr)59*4882a593Smuzhiyun static inline void writeq(u64 val, void __iomem *addr)
60*4882a593Smuzhiyun {
61*4882a593Smuzhiyun writel(val, addr);
62*4882a593Smuzhiyun writel(val >> 32, addr + 4);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun #endif
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun static inline int
csio_list_deleted(struct list_head * list)67*4882a593Smuzhiyun csio_list_deleted(struct list_head *list)
68*4882a593Smuzhiyun {
69*4882a593Smuzhiyun return ((list->next == list) && (list->prev == list));
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun #define csio_list_next(elem) (((struct list_head *)(elem))->next)
73*4882a593Smuzhiyun #define csio_list_prev(elem) (((struct list_head *)(elem))->prev)
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* State machine */
76*4882a593Smuzhiyun typedef void (*csio_sm_state_t)(void *, uint32_t);
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun struct csio_sm {
79*4882a593Smuzhiyun struct list_head sm_list;
80*4882a593Smuzhiyun csio_sm_state_t sm_state;
81*4882a593Smuzhiyun };
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun static inline void
csio_set_state(void * smp,void * state)84*4882a593Smuzhiyun csio_set_state(void *smp, void *state)
85*4882a593Smuzhiyun {
86*4882a593Smuzhiyun ((struct csio_sm *)smp)->sm_state = (csio_sm_state_t)state;
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun static inline void
csio_init_state(struct csio_sm * smp,void * state)90*4882a593Smuzhiyun csio_init_state(struct csio_sm *smp, void *state)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun csio_set_state(smp, state);
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun static inline void
csio_post_event(void * smp,uint32_t evt)96*4882a593Smuzhiyun csio_post_event(void *smp, uint32_t evt)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun ((struct csio_sm *)smp)->sm_state(smp, evt);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun static inline csio_sm_state_t
csio_get_state(void * smp)102*4882a593Smuzhiyun csio_get_state(void *smp)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun return ((struct csio_sm *)smp)->sm_state;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun static inline bool
csio_match_state(void * smp,void * state)108*4882a593Smuzhiyun csio_match_state(void *smp, void *state)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun return (csio_get_state(smp) == (csio_sm_state_t)state);
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun #define CSIO_ASSERT(cond) BUG_ON(!(cond))
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun #ifdef __CSIO_DEBUG__
116*4882a593Smuzhiyun #define CSIO_DB_ASSERT(__c) CSIO_ASSERT((__c))
117*4882a593Smuzhiyun #else
118*4882a593Smuzhiyun #define CSIO_DB_ASSERT(__c)
119*4882a593Smuzhiyun #endif
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun #endif /* ifndef __CSIO_DEFS_H__ */
122