xref: /openbmc/linux/drivers/scsi/fnic/fnic.h (revision 2cc39179)
1 /*
2  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  */
18 #ifndef _FNIC_H_
19 #define _FNIC_H_
20 
21 #include <linux/interrupt.h>
22 #include <linux/netdevice.h>
23 #include <linux/workqueue.h>
24 #include <linux/bitops.h>
25 #include <scsi/libfc.h>
26 #include <scsi/libfcoe.h>
27 #include "fnic_io.h"
28 #include "fnic_res.h"
29 #include "fnic_trace.h"
30 #include "fnic_stats.h"
31 #include "vnic_dev.h"
32 #include "vnic_wq.h"
33 #include "vnic_rq.h"
34 #include "vnic_cq.h"
35 #include "vnic_wq_copy.h"
36 #include "vnic_intr.h"
37 #include "vnic_stats.h"
38 #include "vnic_scsi.h"
39 
40 #define DRV_NAME		"fnic"
41 #define DRV_DESCRIPTION		"Cisco FCoE HBA Driver"
42 #define DRV_VERSION		"1.6.0.54"
43 #define PFX			DRV_NAME ": "
44 #define DFX                     DRV_NAME "%d: "
45 
46 #define DESC_CLEAN_LOW_WATERMARK 8
47 #define FNIC_UCSM_DFLT_THROTTLE_CNT_BLD	16 /* UCSM default throttle count */
48 #define FNIC_MIN_IO_REQ			256 /* Min IO throttle count */
49 #define FNIC_MAX_IO_REQ		1024 /* scsi_cmnd tag map entries */
50 #define FNIC_DFLT_IO_REQ        256 /* Default scsi_cmnd tag map entries */
51 #define	FNIC_IO_LOCKS		64 /* IO locks: power of 2 */
52 #define FNIC_DFLT_QUEUE_DEPTH	256
53 #define	FNIC_STATS_RATE_LIMIT	4 /* limit rate at which stats are pulled up */
54 
55 /*
56  * Tag bits used for special requests.
57  */
58 #define FNIC_TAG_ABORT		BIT(30)		/* tag bit indicating abort */
59 #define FNIC_TAG_DEV_RST	BIT(29)		/* indicates device reset */
60 #define FNIC_TAG_MASK		(BIT(24) - 1)	/* mask for lookup */
61 #define FNIC_NO_TAG             -1
62 
63 /*
64  * Command flags to identify the type of command and for other future
65  * use.
66  */
67 #define FNIC_NO_FLAGS                   0
68 #define FNIC_IO_INITIALIZED             BIT(0)
69 #define FNIC_IO_ISSUED                  BIT(1)
70 #define FNIC_IO_DONE                    BIT(2)
71 #define FNIC_IO_REQ_NULL                BIT(3)
72 #define FNIC_IO_ABTS_PENDING            BIT(4)
73 #define FNIC_IO_ABORTED                 BIT(5)
74 #define FNIC_IO_ABTS_ISSUED             BIT(6)
75 #define FNIC_IO_TERM_ISSUED             BIT(7)
76 #define FNIC_IO_INTERNAL_TERM_ISSUED    BIT(8)
77 #define FNIC_IO_ABT_TERM_DONE           BIT(9)
78 #define FNIC_IO_ABT_TERM_REQ_NULL       BIT(10)
79 #define FNIC_IO_ABT_TERM_TIMED_OUT      BIT(11)
80 #define FNIC_DEVICE_RESET               BIT(12)  /* Device reset request */
81 #define FNIC_DEV_RST_ISSUED             BIT(13)
82 #define FNIC_DEV_RST_TIMED_OUT          BIT(14)
83 #define FNIC_DEV_RST_ABTS_ISSUED        BIT(15)
84 #define FNIC_DEV_RST_TERM_ISSUED        BIT(16)
85 #define FNIC_DEV_RST_DONE               BIT(17)
86 #define FNIC_DEV_RST_REQ_NULL           BIT(18)
87 #define FNIC_DEV_RST_ABTS_DONE          BIT(19)
88 #define FNIC_DEV_RST_TERM_DONE          BIT(20)
89 #define FNIC_DEV_RST_ABTS_PENDING       BIT(21)
90 
91 /*
92  * fnic private data per SCSI command.
93  * These fields are locked by the hashed io_req_lock.
94  */
95 struct fnic_cmd_priv {
96 	struct fnic_io_req *io_req;
97 	enum fnic_ioreq_state state;
98 	u32 flags;
99 	u16 abts_status;
100 	u16 lr_status;
101 };
102 
103 static inline struct fnic_cmd_priv *fnic_priv(struct scsi_cmnd *cmd)
104 {
105 	return scsi_cmd_priv(cmd);
106 }
107 
108 static inline u64 fnic_flags_and_state(struct scsi_cmnd *cmd)
109 {
110 	struct fnic_cmd_priv *fcmd = fnic_priv(cmd);
111 
112 	return ((u64)fcmd->flags << 32) | fcmd->state;
113 }
114 
115 #define FCPIO_INVALID_CODE 0x100 /* hdr_status value unused by firmware */
116 
117 #define FNIC_LUN_RESET_TIMEOUT	     10000	/* mSec */
118 #define FNIC_HOST_RESET_TIMEOUT	     10000	/* mSec */
119 #define FNIC_RMDEVICE_TIMEOUT        1000       /* mSec */
120 #define FNIC_HOST_RESET_SETTLE_TIME  30         /* Sec */
121 #define FNIC_ABT_TERM_DELAY_TIMEOUT  500        /* mSec */
122 
123 #define FNIC_MAX_FCP_TARGET     256
124 
125 /**
126  * state_flags to identify host state along along with fnic's state
127  **/
128 #define __FNIC_FLAGS_FWRESET		BIT(0) /* fwreset in progress */
129 #define __FNIC_FLAGS_BLOCK_IO		BIT(1) /* IOs are blocked */
130 
131 #define FNIC_FLAGS_NONE			(0)
132 #define FNIC_FLAGS_FWRESET		(__FNIC_FLAGS_FWRESET | \
133 					__FNIC_FLAGS_BLOCK_IO)
134 
135 #define FNIC_FLAGS_IO_BLOCKED		(__FNIC_FLAGS_BLOCK_IO)
136 
137 #define fnic_set_state_flags(fnicp, st_flags)	\
138 	__fnic_set_state_flags(fnicp, st_flags, 0)
139 
140 #define fnic_clear_state_flags(fnicp, st_flags)  \
141 	__fnic_set_state_flags(fnicp, st_flags, 1)
142 
143 extern unsigned int fnic_log_level;
144 extern unsigned int io_completions;
145 
146 #define FNIC_MAIN_LOGGING 0x01
147 #define FNIC_FCS_LOGGING 0x02
148 #define FNIC_SCSI_LOGGING 0x04
149 #define FNIC_ISR_LOGGING 0x08
150 
151 #define FNIC_CHECK_LOGGING(LEVEL, CMD)				\
152 do {								\
153 	if (unlikely(fnic_log_level & LEVEL))			\
154 		do {						\
155 			CMD;					\
156 		} while (0);					\
157 } while (0)
158 
159 #define FNIC_MAIN_DBG(kern_level, host, fmt, args...)		\
160 	FNIC_CHECK_LOGGING(FNIC_MAIN_LOGGING,			\
161 			 shost_printk(kern_level, host, fmt, ##args);)
162 
163 #define FNIC_FCS_DBG(kern_level, host, fmt, args...)		\
164 	FNIC_CHECK_LOGGING(FNIC_FCS_LOGGING,			\
165 			 shost_printk(kern_level, host, fmt, ##args);)
166 
167 #define FNIC_SCSI_DBG(kern_level, host, fmt, args...)		\
168 	FNIC_CHECK_LOGGING(FNIC_SCSI_LOGGING,			\
169 			 shost_printk(kern_level, host, fmt, ##args);)
170 
171 #define FNIC_ISR_DBG(kern_level, host, fmt, args...)		\
172 	FNIC_CHECK_LOGGING(FNIC_ISR_LOGGING,			\
173 			 shost_printk(kern_level, host, fmt, ##args);)
174 
175 #define FNIC_MAIN_NOTE(kern_level, host, fmt, args...)          \
176 	shost_printk(kern_level, host, fmt, ##args)
177 
178 extern const char *fnic_state_str[];
179 
180 enum fnic_intx_intr_index {
181 	FNIC_INTX_WQ_RQ_COPYWQ,
182 	FNIC_INTX_ERR,
183 	FNIC_INTX_NOTIFY,
184 	FNIC_INTX_INTR_MAX,
185 };
186 
187 enum fnic_msix_intr_index {
188 	FNIC_MSIX_RQ,
189 	FNIC_MSIX_WQ,
190 	FNIC_MSIX_WQ_COPY,
191 	FNIC_MSIX_ERR_NOTIFY,
192 	FNIC_MSIX_INTR_MAX,
193 };
194 
195 struct fnic_msix_entry {
196 	int requested;
197 	char devname[IFNAMSIZ + 11];
198 	irqreturn_t (*isr)(int, void *);
199 	void *devid;
200 };
201 
202 enum fnic_state {
203 	FNIC_IN_FC_MODE = 0,
204 	FNIC_IN_FC_TRANS_ETH_MODE,
205 	FNIC_IN_ETH_MODE,
206 	FNIC_IN_ETH_TRANS_FC_MODE,
207 };
208 
209 #define FNIC_WQ_COPY_MAX 1
210 #define FNIC_WQ_MAX 1
211 #define FNIC_RQ_MAX 1
212 #define FNIC_CQ_MAX (FNIC_WQ_COPY_MAX + FNIC_WQ_MAX + FNIC_RQ_MAX)
213 #define FNIC_DFLT_IO_COMPLETIONS 256
214 
215 struct mempool;
216 
217 enum fnic_evt {
218 	FNIC_EVT_START_VLAN_DISC = 1,
219 	FNIC_EVT_START_FCF_DISC = 2,
220 	FNIC_EVT_MAX,
221 };
222 
223 struct fnic_event {
224 	struct list_head list;
225 	struct fnic *fnic;
226 	enum fnic_evt event;
227 };
228 
229 /* Per-instance private data structure */
230 struct fnic {
231 	struct fc_lport *lport;
232 	struct fcoe_ctlr ctlr;		/* FIP FCoE controller structure */
233 	struct vnic_dev_bar bar0;
234 
235 	struct fnic_msix_entry msix[FNIC_MSIX_INTR_MAX];
236 
237 	struct vnic_stats *stats;
238 	unsigned long stats_time;	/* time of stats update */
239 	unsigned long stats_reset_time; /* time of stats reset */
240 	struct vnic_nic_cfg *nic_cfg;
241 	char name[IFNAMSIZ];
242 	struct timer_list notify_timer; /* used for MSI interrupts */
243 
244 	unsigned int fnic_max_tag_id;
245 	unsigned int err_intr_offset;
246 	unsigned int link_intr_offset;
247 
248 	unsigned int wq_count;
249 	unsigned int cq_count;
250 
251 	struct dentry *fnic_stats_debugfs_host;
252 	struct dentry *fnic_stats_debugfs_file;
253 	struct dentry *fnic_reset_debugfs_file;
254 	unsigned int reset_stats;
255 	atomic64_t io_cmpl_skip;
256 	struct fnic_stats fnic_stats;
257 
258 	u32 vlan_hw_insert:1;	        /* let hw insert the tag */
259 	u32 in_remove:1;                /* fnic device in removal */
260 	u32 stop_rx_link_events:1;      /* stop proc. rx frames, link events */
261 	u32 link_events:1;              /* set when we get any link event*/
262 
263 	struct completion *remove_wait; /* device remove thread blocks */
264 
265 	atomic_t in_flight;		/* io counter */
266 	bool internal_reset_inprogress;
267 	u32 _reserved;			/* fill hole */
268 	unsigned long state_flags;	/* protected by host lock */
269 	enum fnic_state state;
270 	spinlock_t fnic_lock;
271 
272 	u16 vlan_id;	                /* VLAN tag including priority */
273 	u8 data_src_addr[ETH_ALEN];
274 	u64 fcp_input_bytes;		/* internal statistic */
275 	u64 fcp_output_bytes;		/* internal statistic */
276 	u32 link_down_cnt;
277 	int link_status;
278 
279 	struct list_head list;
280 	struct pci_dev *pdev;
281 	struct vnic_fc_config config;
282 	struct vnic_dev *vdev;
283 	unsigned int raw_wq_count;
284 	unsigned int wq_copy_count;
285 	unsigned int rq_count;
286 	int fw_ack_index[FNIC_WQ_COPY_MAX];
287 	unsigned short fw_ack_recd[FNIC_WQ_COPY_MAX];
288 	unsigned short wq_copy_desc_low[FNIC_WQ_COPY_MAX];
289 	unsigned int intr_count;
290 	u32 __iomem *legacy_pba;
291 	struct fnic_host_tag *tags;
292 	mempool_t *io_req_pool;
293 	mempool_t *io_sgl_pool[FNIC_SGL_NUM_CACHES];
294 	spinlock_t io_req_lock[FNIC_IO_LOCKS];	/* locks for scsi cmnds */
295 
296 	struct work_struct link_work;
297 	struct work_struct frame_work;
298 	struct sk_buff_head frame_queue;
299 	struct sk_buff_head tx_queue;
300 
301 	/*** FIP related data members  -- start ***/
302 	void (*set_vlan)(struct fnic *, u16 vlan);
303 	struct work_struct      fip_frame_work;
304 	struct sk_buff_head     fip_frame_queue;
305 	struct timer_list       fip_timer;
306 	struct list_head        vlans;
307 	spinlock_t              vlans_lock;
308 
309 	struct work_struct      event_work;
310 	struct list_head        evlist;
311 	/*** FIP related data members  -- end ***/
312 
313 	/* copy work queue cache line section */
314 	____cacheline_aligned struct vnic_wq_copy wq_copy[FNIC_WQ_COPY_MAX];
315 	/* completion queue cache line section */
316 	____cacheline_aligned struct vnic_cq cq[FNIC_CQ_MAX];
317 
318 	spinlock_t wq_copy_lock[FNIC_WQ_COPY_MAX];
319 
320 	/* work queue cache line section */
321 	____cacheline_aligned struct vnic_wq wq[FNIC_WQ_MAX];
322 	spinlock_t wq_lock[FNIC_WQ_MAX];
323 
324 	/* receive queue cache line section */
325 	____cacheline_aligned struct vnic_rq rq[FNIC_RQ_MAX];
326 
327 	/* interrupt resource cache line section */
328 	____cacheline_aligned struct vnic_intr intr[FNIC_MSIX_INTR_MAX];
329 };
330 
331 static inline struct fnic *fnic_from_ctlr(struct fcoe_ctlr *fip)
332 {
333 	return container_of(fip, struct fnic, ctlr);
334 }
335 
336 extern struct workqueue_struct *fnic_event_queue;
337 extern struct workqueue_struct *fnic_fip_queue;
338 extern const struct attribute_group *fnic_host_groups[];
339 
340 void fnic_clear_intr_mode(struct fnic *fnic);
341 int fnic_set_intr_mode(struct fnic *fnic);
342 void fnic_free_intr(struct fnic *fnic);
343 int fnic_request_intr(struct fnic *fnic);
344 
345 int fnic_send(struct fc_lport *, struct fc_frame *);
346 void fnic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf);
347 void fnic_handle_frame(struct work_struct *work);
348 void fnic_handle_link(struct work_struct *work);
349 void fnic_handle_event(struct work_struct *work);
350 int fnic_rq_cmpl_handler(struct fnic *fnic, int);
351 int fnic_alloc_rq_frame(struct vnic_rq *rq);
352 void fnic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf);
353 void fnic_flush_tx(struct fnic *);
354 void fnic_eth_send(struct fcoe_ctlr *, struct sk_buff *skb);
355 void fnic_set_port_id(struct fc_lport *, u32, struct fc_frame *);
356 void fnic_update_mac(struct fc_lport *, u8 *new);
357 void fnic_update_mac_locked(struct fnic *, u8 *new);
358 
359 int fnic_queuecommand(struct Scsi_Host *, struct scsi_cmnd *);
360 int fnic_abort_cmd(struct scsi_cmnd *);
361 int fnic_device_reset(struct scsi_cmnd *);
362 int fnic_host_reset(struct scsi_cmnd *);
363 int fnic_reset(struct Scsi_Host *);
364 void fnic_scsi_cleanup(struct fc_lport *);
365 void fnic_scsi_abort_io(struct fc_lport *);
366 void fnic_empty_scsi_cleanup(struct fc_lport *);
367 void fnic_exch_mgr_reset(struct fc_lport *, u32, u32);
368 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int);
369 int fnic_wq_cmpl_handler(struct fnic *fnic, int);
370 int fnic_flogi_reg_handler(struct fnic *fnic, u32);
371 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
372 				  struct fcpio_host_req *desc);
373 int fnic_fw_reset_handler(struct fnic *fnic);
374 void fnic_terminate_rport_io(struct fc_rport *);
375 const char *fnic_state_to_str(unsigned int state);
376 
377 void fnic_log_q_error(struct fnic *fnic);
378 void fnic_handle_link_event(struct fnic *fnic);
379 
380 int fnic_is_abts_pending(struct fnic *, struct scsi_cmnd *);
381 
382 void fnic_handle_fip_frame(struct work_struct *work);
383 void fnic_handle_fip_event(struct fnic *fnic);
384 void fnic_fcoe_reset_vlans(struct fnic *fnic);
385 void fnic_fcoe_evlist_free(struct fnic *fnic);
386 extern void fnic_handle_fip_timer(struct fnic *fnic);
387 
388 static inline int
389 fnic_chk_state_flags_locked(struct fnic *fnic, unsigned long st_flags)
390 {
391 	return ((fnic->state_flags & st_flags) == st_flags);
392 }
393 void __fnic_set_state_flags(struct fnic *, unsigned long, unsigned long);
394 void fnic_dump_fchost_stats(struct Scsi_Host *, struct fc_host_statistics *);
395 #endif /* _FNIC_H_ */
396