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