blob: 788a250ffb8a01b05a6701cab2ecd1e3308016f7 [file] [log] [blame]
Jing Huang7725ccf2009-09-23 17:46:15 -07001/*
Krishna Gudipatia36c61f2010-09-15 11:50:55 -07002 * Copyright (c) 2005-2010 Brocade Communications Systems, Inc.
Jing Huang7725ccf2009-09-23 17:46:15 -07003 * 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 * Contains declarations all OS Specific files needed for BFA layer
20 */
21
22#ifndef __BFA_OS_INC_H__
23#define __BFA_OS_INC_H__
24
Jing Huang7725ccf2009-09-23 17:46:15 -070025#include <linux/types.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070026#include <linux/version.h>
27#include <linux/pci.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070028#include <linux/dma-mapping.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070029#include <linux/idr.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070030#include <linux/interrupt.h>
31#include <linux/cdev.h>
32#include <linux/fs.h>
33#include <linux/delay.h>
34#include <linux/vmalloc.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070035#include <linux/workqueue.h>
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070036#include <linux/bitops.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070037#include <scsi/scsi.h>
38#include <scsi/scsi_host.h>
Jing Huang7725ccf2009-09-23 17:46:15 -070039#include <scsi/scsi_tcq.h>
40#include <scsi/scsi_transport_fc.h>
41#include <scsi/scsi_transport.h>
42
Jing Huang077424e2010-03-19 11:07:36 -070043#ifdef __BIG_ENDIAN
44#define __BIGENDIAN
45#endif
46
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070047static inline u64 bfa_os_get_clock(void)
48{
49 return jiffies;
50}
Jing Huang7725ccf2009-09-23 17:46:15 -070051
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070052static inline u64 bfa_os_get_log_time(void)
53{
54 u64 system_time = 0;
55 struct timeval tv;
56 do_gettimeofday(&tv);
Jing Huang7725ccf2009-09-23 17:46:15 -070057
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070058 /* We are interested in seconds only. */
59 system_time = tv.tv_sec;
60 return system_time;
61}
62
63#define bfa_io_lat_clock_res_div HZ
64#define bfa_io_lat_clock_res_mul 1000
Jing Huang7725ccf2009-09-23 17:46:15 -070065
66#define BFA_ASSERT(p) do { \
67 if (!(p)) { \
68 printk(KERN_ERR "assert(%s) failed at %s:%d\n", \
69 #p, __FILE__, __LINE__); \
Jing Huang7725ccf2009-09-23 17:46:15 -070070 } \
71} while (0)
72
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070073#define BFA_LOG(level, bfad, mask, fmt, arg...) \
74do { \
75 if (((mask) == 4) || (level[1] <= '4')) \
76 dev_printk(level, &((bfad)->pcidev)->dev, fmt, ##arg); \
Jing Huang7725ccf2009-09-23 17:46:15 -070077} while (0)
78
Jing Huang7725ccf2009-09-23 17:46:15 -070079#define bfa_swap_3b(_x) \
80 ((((_x) & 0xff) << 16) | \
81 ((_x) & 0x00ff00) | \
82 (((_x) & 0xff0000) >> 16))
83
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070084#define bfa_swap_8b(_x) \
85 ((((_x) & 0xff00000000000000ull) >> 56) \
86 | (((_x) & 0x00ff000000000000ull) >> 40) \
87 | (((_x) & 0x0000ff0000000000ull) >> 24) \
88 | (((_x) & 0x000000ff00000000ull) >> 8) \
89 | (((_x) & 0x00000000ff000000ull) << 8) \
90 | (((_x) & 0x0000000000ff0000ull) << 24) \
91 | (((_x) & 0x000000000000ff00ull) << 40) \
92 | (((_x) & 0x00000000000000ffull) << 56))
Jing Huang7725ccf2009-09-23 17:46:15 -070093
Krishna Gudipatia36c61f2010-09-15 11:50:55 -070094#define bfa_os_swap32(_x) \
95 ((((_x) & 0xff) << 24) | \
Jing Huang7725ccf2009-09-23 17:46:15 -070096 (((_x) & 0x0000ff00) << 8) | \
97 (((_x) & 0x00ff0000) >> 8) | \
98 (((_x) & 0xff000000) >> 24))
99
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700100#define bfa_os_swap_sgaddr(_x) ((u64)( \
101 (((u64)(_x) & (u64)0x00000000000000ffull) << 32) | \
102 (((u64)(_x) & (u64)0x000000000000ff00ull) << 32) | \
103 (((u64)(_x) & (u64)0x0000000000ff0000ull) << 32) | \
104 (((u64)(_x) & (u64)0x00000000ff000000ull) << 32) | \
105 (((u64)(_x) & (u64)0x000000ff00000000ull) >> 32) | \
106 (((u64)(_x) & (u64)0x0000ff0000000000ull) >> 32) | \
107 (((u64)(_x) & (u64)0x00ff000000000000ull) >> 32) | \
Jing Huang077424e2010-03-19 11:07:36 -0700108 (((u64)(_x) & (u64)0xff00000000000000ull) >> 32)))
Jing Huang7725ccf2009-09-23 17:46:15 -0700109
110#ifndef __BIGENDIAN
111#define bfa_os_htons(_x) ((u16)((((_x) & 0xff00) >> 8) | \
112 (((_x) & 0x00ff) << 8)))
Jing Huang7725ccf2009-09-23 17:46:15 -0700113#define bfa_os_htonl(_x) bfa_os_swap32(_x)
114#define bfa_os_htonll(_x) bfa_swap_8b(_x)
115#define bfa_os_hton3b(_x) bfa_swap_3b(_x)
Jing Huang7725ccf2009-09-23 17:46:15 -0700116#define bfa_os_wtole(_x) (_x)
Jing Huang077424e2010-03-19 11:07:36 -0700117#define bfa_os_sgaddr(_x) (_x)
Jing Huang7725ccf2009-09-23 17:46:15 -0700118
119#else
120
121#define bfa_os_htons(_x) (_x)
122#define bfa_os_htonl(_x) (_x)
123#define bfa_os_hton3b(_x) (_x)
124#define bfa_os_htonll(_x) (_x)
125#define bfa_os_wtole(_x) bfa_os_swap32(_x)
Jing Huang077424e2010-03-19 11:07:36 -0700126#define bfa_os_sgaddr(_x) bfa_os_swap_sgaddr(_x)
Jing Huang7725ccf2009-09-23 17:46:15 -0700127
128#endif
129
130#define bfa_os_ntohs(_x) bfa_os_htons(_x)
131#define bfa_os_ntohl(_x) bfa_os_htonl(_x)
132#define bfa_os_ntohll(_x) bfa_os_htonll(_x)
133#define bfa_os_ntoh3b(_x) bfa_os_hton3b(_x)
134
135#define bfa_os_u32(__pa64) ((__pa64) >> 32)
136
137#define bfa_os_memset memset
138#define bfa_os_memcpy memcpy
139#define bfa_os_udelay udelay
140#define bfa_os_vsprintf vsprintf
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700141#define bfa_os_snprintf snprintf
Jing Huang7725ccf2009-09-23 17:46:15 -0700142
143#define bfa_os_assign(__t, __s) __t = __s
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700144#define bfa_os_addr_t void __iomem *
Jing Huang7725ccf2009-09-23 17:46:15 -0700145
Jing Huang8637ac32010-04-13 12:22:29 -0700146#define bfa_os_reg_read(_raddr) readl(_raddr)
147#define bfa_os_reg_write(_raddr, _val) writel((_val), (_raddr))
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700148#define bfa_os_mem_read(_raddr, _off) \
Jing Huang8637ac32010-04-13 12:22:29 -0700149 bfa_os_swap32(readl(((_raddr) + (_off))))
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700150#define bfa_os_mem_write(_raddr, _off, _val) \
Jing Huang8637ac32010-04-13 12:22:29 -0700151 writel(bfa_os_swap32((_val)), ((_raddr) + (_off)))
Jing Huang7725ccf2009-09-23 17:46:15 -0700152
153#define BFA_TRC_TS(_trcm) \
154 ({ \
155 struct timeval tv; \
156 \
157 do_gettimeofday(&tv); \
158 (tv.tv_sec*1000000+tv.tv_usec); \
159 })
160
Jing Huang7725ccf2009-09-23 17:46:15 -0700161#define boolean_t int
162
163/**
164 * For current time stamp, OS API will fill-in
165 */
166struct bfa_timeval_s {
167 u32 tv_sec; /* seconds */
168 u32 tv_usec; /* microseconds */
169};
170
Krishna Gudipatia36c61f2010-09-15 11:50:55 -0700171static inline void
172bfa_os_gettimeofday(struct bfa_timeval_s *tv)
173{
174 struct timeval tmp_tv;
175
176 do_gettimeofday(&tmp_tv);
177 tv->tv_sec = (u32) tmp_tv.tv_sec;
178 tv->tv_usec = (u32) tmp_tv.tv_usec;
179}
Jing Huang7725ccf2009-09-23 17:46:15 -0700180
181static inline void
182wwn2str(char *wwn_str, u64 wwn)
183{
184 union {
185 u64 wwn;
186 u8 byte[8];
187 } w;
188
189 w.wwn = wwn;
190 sprintf(wwn_str, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", w.byte[0],
191 w.byte[1], w.byte[2], w.byte[3], w.byte[4], w.byte[5],
192 w.byte[6], w.byte[7]);
193}
194
195static inline void
196fcid2str(char *fcid_str, u32 fcid)
197{
198 union {
199 u32 fcid;
200 u8 byte[4];
201 } f;
202
203 f.fcid = fcid;
204 sprintf(fcid_str, "%02x:%02x:%02x", f.byte[1], f.byte[2], f.byte[3]);
205}
206
207#endif /* __BFA_OS_INC_H__ */