1/*-
2 * Copyright (c) 2017 Broadcom. All rights reserved.
3 * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 *    this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 *    this list of conditions and the following disclaimer in the documentation
13 *    and/or other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/**
33 * @file
34 * OCS bsd driver common include file
35 */
36
37#if !defined(__OCS_H__)
38#define __OCS_H__
39
40#include "ocs_os.h"
41#include "ocs_utils.h"
42
43#include "ocs_hw.h"
44#include "ocs_scsi.h"
45#include "ocs_io.h"
46
47#include "version.h"
48
49#define DRV_NAME			"ocs_fc"
50#define DRV_VERSION 							\
51	STR_BE_MAJOR "." STR_BE_MINOR "." STR_BE_BUILD "." STR_BE_BRANCH
52
53/**
54 * @brief Interrupt context
55 */
56typedef struct ocs_intr_ctx_s {
57	uint32_t	vec;		/** Zero based interrupt vector */
58	void		*softc;		/** software context for interrupt */
59	char		name[64];	/** label for this context */
60} ocs_intr_ctx_t;
61
62typedef struct ocs_fc_rport_db_s {
63	uint32_t	node_id;
64	uint32_t	state;
65	uint32_t	port_id;
66	uint64_t	wwnn;
67	uint64_t	wwpn;
68	uint32_t	gone_timer;
69} ocs_fc_target_t;
70
71#define OCS_TGT_STATE_NONE		0	/* Empty DB slot */
72#define OCS_TGT_STATE_VALID		1	/* Valid*/
73#define OCS_TGT_STATE_LOST		2	/* LOST*/
74
75typedef struct ocs_fcport_s {
76	ocs_t			*ocs;
77	struct cam_sim		*sim;
78	struct cam_path		*path;
79	uint32_t		role;
80	uint32_t                fc_id;
81
82	ocs_fc_target_t	tgt[OCS_MAX_TARGETS];
83	int lost_device_time;
84	struct callout ldt;     /* device lost timer */
85	struct task ltask;
86
87	ocs_tgt_resource_t	targ_rsrc_wildcard;
88	ocs_tgt_resource_t	targ_rsrc[OCS_MAX_LUN];
89	ocs_vport_spec_t	*vport;
90} ocs_fcport;
91
92#define FCPORT(ocs, chan)	(&((ocs_fcport *)(ocs)->fcports)[(chan)])
93
94/**
95 * @brief Driver's context
96 */
97
98struct ocs_softc {
99	device_t		dev;
100	struct cdev		*cdev;
101
102	ocs_pci_reg_t		reg[PCI_MAX_BAR];
103
104	uint32_t		instance_index;
105	const char		*desc;
106
107	uint32_t		irqid;
108	struct resource		*irq;
109	void			*tag;
110
111	ocs_intr_ctx_t		intr_ctx;
112	uint32_t		n_vec;
113
114	bus_dma_tag_t		dmat;	/** Parent DMA tag */
115	bus_dma_tag_t		buf_dmat;/** IO buffer DMA tag */
116	char display_name[OCS_DISPLAY_NAME_LENGTH];
117	uint16_t		pci_vendor;
118	uint16_t		pci_device;
119	uint16_t		pci_subsystem_vendor;
120	uint16_t		pci_subsystem_device;
121	char			businfo[16];
122	const char		*driver_version;
123	const char		*fw_version;
124	const char		*model;
125
126	ocs_hw_t hw;
127
128	ocs_rlock_t lock;	/**< device wide lock */
129
130	ocs_xport_e		ocs_xport;
131	ocs_xport_t *xport;	/**< pointer to transport object */
132	ocs_domain_t *domain;
133	ocs_list_t domain_list;
134	uint32_t domain_instance_count;
135	void (*domain_list_empty_cb)(ocs_t *ocs, void *arg);
136	void *domain_list_empty_cb_arg;
137
138	uint8_t enable_ini;
139	uint8_t enable_tgt;
140	uint8_t fc_type;
141	int ctrlmask;
142	uint8_t explicit_buffer_list;
143	uint8_t external_loopback;
144	uint8_t skip_hw_teardown;
145	int speed;
146	int topology;
147	int ethernet_license;
148	int num_scsi_ios;
149	uint8_t enable_hlm;
150	uint32_t hlm_group_size;
151	uint32_t max_isr_time_msec;	/*>> Maximum ISR time */
152	uint32_t auto_xfer_rdy_size; /*>> Max sized write to use auto xfer rdy*/
153	uint8_t esoc;
154	int logmask;
155	char *hw_war_version;
156	uint32_t num_vports;
157	uint32_t target_io_timer_sec;
158	uint32_t hw_bounce;
159	uint8_t rq_threads;
160	uint8_t rq_selection_policy;
161	uint8_t rr_quanta;
162	char *filter_def;
163	uint32_t max_remote_nodes;
164
165	/*
166	 * tgt_rscn_delay - delay in kicking off RSCN processing
167	 * (nameserver queries) after receiving an RSCN on the target.
168	 * This prevents thrashing of nameserver requests due to a huge burst of
169	 * RSCNs received in a short period of time.
170	 * Note: this is only valid when target RSCN handling is enabled -- see
171	 * ctrlmask.
172	 */
173	time_t tgt_rscn_delay_msec;	/*>> minimum target RSCN delay */
174
175	/*
176	 * tgt_rscn_period - determines maximum frequency when processing
177	 * back-to-back RSCNs; e.g. if this value is 30, there will never be
178	 * any more than 1 RSCN handling per 30s window. This prevents
179	 * initiators on a faulty link generating many RSCN from causing the
180	 * target to continually query the nameserver.
181	 * Note: This is only valid when target RSCN handling is enabled
182	 */
183	time_t tgt_rscn_period_msec;	/*>> minimum target RSCN period */
184
185	uint32_t		enable_task_set_full;
186	uint32_t		io_in_use;
187	uint32_t		io_high_watermark; /**< used to send task set full */
188	struct mtx		sim_lock;
189	uint32_t		config_tgt:1,	/**< Configured to support target mode */
190				config_ini:1;	/**< Configured to support initiator mode */
191
192	uint32_t nodedb_mask;			/**< Node debugging mask */
193
194	char			modeldesc[64];
195	char			serialnum[64];
196	char			fwrev[64];
197	char			sli_intf[9];
198
199	ocs_ramlog_t		*ramlog;
200	ocs_textbuf_t		ddump_saved;
201
202	ocs_mgmt_functions_t	*mgmt_functions;
203	ocs_mgmt_functions_t	*tgt_mgmt_functions;
204	ocs_mgmt_functions_t	*ini_mgmt_functions;
205
206	ocs_err_injection_e err_injection;
207	uint32_t cmd_err_inject;
208	time_t delay_value_msec;
209
210	bool			attached;
211	struct mtx		dbg_lock;
212
213	struct cam_devq		*devq;
214	ocs_fcport		*fcports;
215
216	void*			tgt_ocs;
217};
218
219static inline void
220ocs_device_lock_init(ocs_t *ocs)
221{
222	ocs_rlock_init(ocs, &ocs->lock, "ocsdevicelock");
223}
224
225static inline int32_t
226ocs_device_lock_try(ocs_t *ocs)
227{
228	return ocs_rlock_try(&ocs->lock);
229}
230
231static inline void
232ocs_device_lock(ocs_t *ocs)
233{
234	ocs_rlock_acquire(&ocs->lock);
235}
236
237static inline void
238ocs_device_unlock(ocs_t *ocs)
239{
240	ocs_rlock_release(&ocs->lock);
241}
242
243static inline void
244ocs_device_lock_free(ocs_t *ocs)
245{
246	ocs_rlock_free(&ocs->lock);
247}
248
249extern int32_t ocs_device_detach(ocs_t *ocs);
250
251extern int32_t ocs_device_attach(ocs_t *ocs);
252
253#define ocs_is_initiator_enabled()	(ocs->enable_ini)
254#define ocs_is_target_enabled()	(ocs->enable_tgt)
255
256#include "ocs_xport.h"
257#include "ocs_domain.h"
258#include "ocs_sport.h"
259#include "ocs_node.h"
260#include "ocs_unsol.h"
261#include "ocs_scsi.h"
262#include "ocs_ioctl.h"
263
264static inline ocs_io_t *
265ocs_io_alloc(ocs_t *ocs)
266{
267	return ocs_io_pool_io_alloc(ocs->xport->io_pool);
268}
269
270static inline void
271ocs_io_free(ocs_t *ocs, ocs_io_t *io)
272{
273	ocs_io_pool_io_free(ocs->xport->io_pool, io);
274}
275
276#endif /* __OCS_H__ */
277