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  * Contains base driver definitions.
20  */
21 
22 /*
23  *  bfa_drv.h Linux driver data structures.
24  */
25 
26 #ifndef __BFAD_DRV_H__
27 #define __BFAD_DRV_H__
28 
29 #include <linux/types.h>
30 #include <linux/version.h>
31 #include <linux/pci.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/idr.h>
34 #include <linux/interrupt.h>
35 #include <linux/cdev.h>
36 #include <linux/fs.h>
37 #include <linux/delay.h>
38 #include <linux/vmalloc.h>
39 #include <linux/workqueue.h>
40 #include <linux/bitops.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_host.h>
43 #include <scsi/scsi_tcq.h>
44 #include <scsi/scsi_transport_fc.h>
45 #include <scsi/scsi_transport.h>
46 
47 #include "bfa_modules.h"
48 #include "bfa_fcs.h"
49 #include "bfa_defs_fcs.h"
50 
51 #include "bfa_plog.h"
52 #include "bfa_cs.h"
53 
54 #define BFAD_DRIVER_NAME	"bfa"
55 #ifdef BFA_DRIVER_VERSION
56 #define BFAD_DRIVER_VERSION    BFA_DRIVER_VERSION
57 #else
58 #define BFAD_DRIVER_VERSION    "2.3.2.3"
59 #endif
60 
61 #define BFAD_PROTO_NAME FCPI_NAME
62 #define BFAD_IRQ_FLAGS IRQF_SHARED
63 
64 #ifndef FC_PORTSPEED_8GBIT
65 #define FC_PORTSPEED_8GBIT 0x10
66 #endif
67 
68 /*
69  * BFAD flags
70  */
71 #define BFAD_MSIX_ON				0x00000001
72 #define BFAD_HAL_INIT_DONE			0x00000002
73 #define BFAD_DRV_INIT_DONE			0x00000004
74 #define BFAD_CFG_PPORT_DONE			0x00000008
75 #define BFAD_HAL_START_DONE			0x00000010
76 #define BFAD_PORT_ONLINE			0x00000020
77 #define BFAD_RPORT_ONLINE			0x00000040
78 #define BFAD_FCS_INIT_DONE			0x00000080
79 #define BFAD_HAL_INIT_FAIL			0x00000100
80 #define BFAD_FC4_PROBE_DONE			0x00000200
81 #define BFAD_PORT_DELETE			0x00000001
82 
83 /*
84  * BFAD related definition
85  */
86 #define SCSI_SCAN_DELAY		HZ
87 #define BFAD_STOP_TIMEOUT	30
88 #define BFAD_SUSPEND_TIMEOUT	BFAD_STOP_TIMEOUT
89 
90 /*
91  * BFAD configuration parameter default values
92  */
93 #define BFAD_LUN_QUEUE_DEPTH	32
94 #define BFAD_IO_MAX_SGE		SG_ALL
95 
96 #define bfad_isr_t irq_handler_t
97 
98 #define MAX_MSIX_ENTRY 22
99 
100 struct bfad_msix_s {
101 	struct bfad_s *bfad;
102 	struct msix_entry msix;
103 	char name[32];
104 };
105 
106 /*
107  * Only append to the enums defined here to avoid any versioning
108  * needed between trace utility and driver version
109  */
110 enum {
111 	BFA_TRC_LDRV_BFAD		= 1,
112 	BFA_TRC_LDRV_IM			= 2,
113 };
114 
115 enum bfad_port_pvb_type {
116 	BFAD_PORT_PHYS_BASE = 0,
117 	BFAD_PORT_PHYS_VPORT = 1,
118 	BFAD_PORT_VF_BASE = 2,
119 	BFAD_PORT_VF_VPORT = 3,
120 };
121 
122 /*
123  * PORT data structure
124  */
125 struct bfad_port_s {
126 	struct list_head list_entry;
127 	struct bfad_s	*bfad;
128 	struct bfa_fcs_lport_s *fcs_port;
129 	u32	roles;
130 	s32		flags;
131 	u32	supported_fc4s;
132 	enum bfad_port_pvb_type pvb_type;
133 	struct bfad_im_port_s *im_port;	/* IM specific data */
134 	/* port debugfs specific data */
135 	struct dentry *port_debugfs_root;
136 };
137 
138 /*
139  * VPORT data structure
140  */
141 struct bfad_vport_s {
142 	struct bfad_port_s     drv_port;
143 	struct bfa_fcs_vport_s fcs_vport;
144 	struct completion *comp_del;
145 	struct list_head list_entry;
146 };
147 
148 /*
149  * VF data structure
150  */
151 struct bfad_vf_s {
152 	bfa_fcs_vf_t    fcs_vf;
153 	struct bfad_port_s    base_port;	/* base port for vf */
154 	struct bfad_s   *bfad;
155 };
156 
157 struct bfad_cfg_param_s {
158 	u32	rport_del_timeout;
159 	u32	ioc_queue_depth;
160 	u32	lun_queue_depth;
161 	u32	io_max_sge;
162 	u32	binding_method;
163 };
164 
165 union bfad_tmp_buf {
166 	/* From struct bfa_adapter_attr_s */
167 	char		manufacturer[BFA_ADAPTER_MFG_NAME_LEN];
168 	char		serial_num[BFA_ADAPTER_SERIAL_NUM_LEN];
169 	char		model[BFA_ADAPTER_MODEL_NAME_LEN];
170 	char		fw_ver[BFA_VERSION_LEN];
171 	char		optrom_ver[BFA_VERSION_LEN];
172 
173 	/* From struct bfa_ioc_pci_attr_s */
174 	u8		chip_rev[BFA_IOC_CHIP_REV_LEN];  /*  chip revision */
175 
176 	wwn_t		wwn[BFA_FCS_MAX_LPORTS];
177 };
178 
179 /*
180  * BFAD (PCI function) data structure
181  */
182 struct bfad_s {
183 	bfa_sm_t	sm;	/* state machine */
184 	struct list_head list_entry;
185 	struct bfa_s	bfa;
186 	struct bfa_fcs_s bfa_fcs;
187 	struct pci_dev *pcidev;
188 	const char *pci_name;
189 	struct bfa_pcidev_s hal_pcidev;
190 	struct bfa_ioc_pci_attr_s pci_attr;
191 	void __iomem   *pci_bar0_kva;
192 	struct completion comp;
193 	struct completion suspend;
194 	struct completion disable_comp;
195 	bfa_boolean_t   disable_active;
196 	struct bfad_port_s     pport;	/* physical port of the BFAD */
197 	struct bfa_meminfo_s meminfo;
198 	struct bfa_iocfc_cfg_s   ioc_cfg;
199 	u32	inst_no;	/* BFAD instance number */
200 	u32	bfad_flags;
201 	spinlock_t      bfad_lock;
202 	struct task_struct *bfad_tsk;
203 	struct bfad_cfg_param_s cfg_data;
204 	struct bfad_msix_s msix_tab[MAX_MSIX_ENTRY];
205 	int		nvec;
206 	char	adapter_name[BFA_ADAPTER_SYM_NAME_LEN];
207 	char	port_name[BFA_ADAPTER_SYM_NAME_LEN];
208 	struct timer_list hal_tmo;
209 	unsigned long   hs_start;
210 	struct bfad_im_s *im;		/* IM specific data */
211 	struct bfa_trc_mod_s  *trcmod;
212 	struct bfa_plog_s      plog_buf;
213 	int		ref_count;
214 	union bfad_tmp_buf tmp_buf;
215 	struct fc_host_statistics link_stats;
216 	struct list_head pbc_vport_list;
217 	/* debugfs specific data */
218 	char *regdata;
219 	u32 reglen;
220 	struct dentry *bfad_dentry_files[5];
221 };
222 
223 /* BFAD state machine events */
224 enum bfad_sm_event {
225 	BFAD_E_CREATE			= 1,
226 	BFAD_E_KTHREAD_CREATE_FAILED	= 2,
227 	BFAD_E_INIT			= 3,
228 	BFAD_E_INIT_SUCCESS		= 4,
229 	BFAD_E_INIT_FAILED		= 5,
230 	BFAD_E_INTR_INIT_FAILED		= 6,
231 	BFAD_E_FCS_EXIT_COMP		= 7,
232 	BFAD_E_EXIT_COMP		= 8,
233 	BFAD_E_STOP			= 9
234 };
235 
236 /*
237  * RPORT data structure
238  */
239 struct bfad_rport_s {
240 	struct bfa_fcs_rport_s fcs_rport;
241 };
242 
243 struct bfad_buf_info {
244 	void		*virt;
245 	dma_addr_t      phys;
246 	u32	size;
247 };
248 
249 struct bfad_fcxp {
250 	struct bfad_port_s    *port;
251 	struct bfa_rport_s *bfa_rport;
252 	bfa_status_t    req_status;
253 	u16	tag;
254 	u16	rsp_len;
255 	u16	rsp_maxlen;
256 	u8		use_ireqbuf;
257 	u8		use_irspbuf;
258 	u32	num_req_sgles;
259 	u32	num_rsp_sgles;
260 	struct fchs_s	fchs;
261 	void		*reqbuf_info;
262 	void		*rspbuf_info;
263 	struct bfa_sge_s  *req_sge;
264 	struct bfa_sge_s  *rsp_sge;
265 	fcxp_send_cb_t  send_cbfn;
266 	void		*send_cbarg;
267 	void		*bfa_fcxp;
268 	struct completion comp;
269 };
270 
271 struct bfad_hal_comp {
272 	bfa_status_t    status;
273 	struct completion comp;
274 };
275 
276 /*
277  * Macro to obtain the immediate lower power
278  * of two for the integer.
279  */
280 #define nextLowerInt(x)                         \
281 do {                                            \
282 	int __i;                                  \
283 	(*x)--;					\
284 	for (__i = 1; __i < (sizeof(int)*8); __i <<= 1) \
285 		(*x) = (*x) | (*x) >> __i;	\
286 	(*x)++;					\
287 	(*x) = (*x) >> 1;			\
288 } while (0)
289 
290 
291 #define BFA_LOG(level, bfad, mask, fmt, arg...)				\
292 do {									\
293 	if (((mask) == 4) || (level[1] <= '4'))				\
294 		dev_printk(level, &((bfad)->pcidev)->dev, fmt, ##arg);	\
295 } while (0)
296 
297 bfa_status_t	bfad_vport_create(struct bfad_s *bfad, u16 vf_id,
298 				  struct bfa_lport_cfg_s *port_cfg,
299 				  struct device *dev);
300 bfa_status_t	bfad_vf_create(struct bfad_s *bfad, u16 vf_id,
301 			       struct bfa_lport_cfg_s *port_cfg);
302 bfa_status_t	bfad_cfg_pport(struct bfad_s *bfad, enum bfa_lport_role role);
303 bfa_status_t	bfad_drv_init(struct bfad_s *bfad);
304 bfa_status_t	bfad_start_ops(struct bfad_s *bfad);
305 void		bfad_drv_start(struct bfad_s *bfad);
306 void		bfad_uncfg_pport(struct bfad_s *bfad);
307 void		bfad_stop(struct bfad_s *bfad);
308 void		bfad_fcs_stop(struct bfad_s *bfad);
309 void		bfad_remove_intr(struct bfad_s *bfad);
310 void		bfad_hal_mem_release(struct bfad_s *bfad);
311 void		bfad_hcb_comp(void *arg, bfa_status_t status);
312 
313 int		bfad_setup_intr(struct bfad_s *bfad);
314 void		bfad_remove_intr(struct bfad_s *bfad);
315 void		bfad_update_hal_cfg(struct bfa_iocfc_cfg_s *bfa_cfg);
316 bfa_status_t	bfad_hal_mem_alloc(struct bfad_s *bfad);
317 void		bfad_bfa_tmo(unsigned long data);
318 void		bfad_init_timer(struct bfad_s *bfad);
319 int		bfad_pci_init(struct pci_dev *pdev, struct bfad_s *bfad);
320 void		bfad_pci_uninit(struct pci_dev *pdev, struct bfad_s *bfad);
321 void		bfad_drv_uninit(struct bfad_s *bfad);
322 int		bfad_worker(void *ptr);
323 void		bfad_debugfs_init(struct bfad_port_s *port);
324 void		bfad_debugfs_exit(struct bfad_port_s *port);
325 
326 void bfad_pci_remove(struct pci_dev *pdev);
327 int bfad_pci_probe(struct pci_dev *pdev, const struct pci_device_id *pid);
328 void bfad_rport_online_wait(struct bfad_s *bfad);
329 int bfad_get_linkup_delay(struct bfad_s *bfad);
330 int bfad_install_msix_handler(struct bfad_s *bfad);
331 
332 extern struct idr bfad_im_port_index;
333 extern struct pci_device_id bfad_id_table[];
334 extern struct list_head bfad_list;
335 extern char	*os_name;
336 extern char	*os_patch;
337 extern char	*host_name;
338 extern int	num_rports;
339 extern int	num_ios;
340 extern int	num_tms;
341 extern int	num_fcxps;
342 extern int	num_ufbufs;
343 extern int	reqq_size;
344 extern int	rspq_size;
345 extern int	num_sgpgs;
346 extern int      rport_del_timeout;
347 extern int      bfa_lun_queue_depth;
348 extern int      bfa_io_max_sge;
349 extern int      bfa_log_level;
350 extern int      ioc_auto_recover;
351 extern int      bfa_linkup_delay;
352 extern int      msix_disable_cb;
353 extern int      msix_disable_ct;
354 extern int      fdmi_enable;
355 extern int      supported_fc4s;
356 extern int	pcie_max_read_reqsz;
357 extern int bfa_debugfs_enable;
358 extern struct mutex bfad_mutex;
359 
360 #endif /* __BFAD_DRV_H__ */
361