xref: /openbmc/linux/drivers/scsi/bnx2fc/bnx2fc.h (revision 565d76cb)
1 #ifndef _BNX2FC_H_
2 #define _BNX2FC_H_
3 /* bnx2fc.h: Broadcom NetXtreme II Linux FCoE offload driver.
4  *
5  * Copyright (c) 2008 - 2010 Broadcom Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation.
10  *
11  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
12  */
13 
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/kernel.h>
17 #include <linux/skbuff.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if_ether.h>
21 #include <linux/if_vlan.h>
22 #include <linux/kthread.h>
23 #include <linux/crc32.h>
24 #include <linux/cpu.h>
25 #include <linux/types.h>
26 #include <linux/list.h>
27 #include <linux/delay.h>
28 #include <linux/timer.h>
29 #include <linux/errno.h>
30 #include <linux/pci.h>
31 #include <linux/init.h>
32 #include <linux/dma-mapping.h>
33 #include <linux/workqueue.h>
34 #include <linux/mutex.h>
35 #include <linux/spinlock.h>
36 #include <linux/bitops.h>
37 #include <linux/log2.h>
38 #include <linux/interrupt.h>
39 #include <linux/sched.h>
40 #include <linux/io.h>
41 
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <scsi/scsi_eh.h>
47 #include <scsi/scsi_tcq.h>
48 #include <scsi/libfc.h>
49 #include <scsi/libfcoe.h>
50 #include <scsi/fc_encode.h>
51 #include <scsi/scsi_transport.h>
52 #include <scsi/scsi_transport_fc.h>
53 #include <scsi/fc/fc_fip.h>
54 #include <scsi/fc/fc_fc2.h>
55 #include <scsi/fc_frame.h>
56 #include <scsi/fc/fc_fcoe.h>
57 #include <scsi/fc/fc_fcp.h>
58 
59 #include "57xx_hsi_bnx2fc.h"
60 #include "bnx2fc_debug.h"
61 #include "../../net/cnic_if.h"
62 #include "bnx2fc_constants.h"
63 
64 #define BNX2FC_NAME		"bnx2fc"
65 #define BNX2FC_VERSION		"1.0.0"
66 
67 #define PFX			"bnx2fc: "
68 
69 #define BNX2X_DOORBELL_PCI_BAR		2
70 
71 #define BNX2FC_MAX_BD_LEN		0xffff
72 #define BNX2FC_BD_SPLIT_SZ		0x8000
73 #define BNX2FC_MAX_BDS_PER_CMD		256
74 
75 #define BNX2FC_SQ_WQES_MAX	256
76 
77 #define BNX2FC_SCSI_MAX_SQES	((3 * BNX2FC_SQ_WQES_MAX) / 8)
78 #define BNX2FC_TM_MAX_SQES	((BNX2FC_SQ_WQES_MAX) / 2)
79 #define BNX2FC_ELS_MAX_SQES	(BNX2FC_TM_MAX_SQES - 1)
80 
81 #define BNX2FC_RQ_WQES_MAX	16
82 #define BNX2FC_CQ_WQES_MAX	(BNX2FC_SQ_WQES_MAX + BNX2FC_RQ_WQES_MAX)
83 
84 #define BNX2FC_NUM_MAX_SESS	128
85 #define BNX2FC_NUM_MAX_SESS_LOG	(ilog2(BNX2FC_NUM_MAX_SESS))
86 
87 #define BNX2FC_MAX_OUTSTANDING_CMNDS	4096
88 #define BNX2FC_MIN_PAYLOAD		256
89 #define BNX2FC_MAX_PAYLOAD		2048
90 
91 #define BNX2FC_RQ_BUF_SZ		256
92 #define BNX2FC_RQ_BUF_LOG_SZ		(ilog2(BNX2FC_RQ_BUF_SZ))
93 
94 #define BNX2FC_SQ_WQE_SIZE		(sizeof(struct fcoe_sqe))
95 #define BNX2FC_CQ_WQE_SIZE		(sizeof(struct fcoe_cqe))
96 #define BNX2FC_RQ_WQE_SIZE		(BNX2FC_RQ_BUF_SZ)
97 #define BNX2FC_XFERQ_WQE_SIZE		(sizeof(struct fcoe_xfrqe))
98 #define BNX2FC_CONFQ_WQE_SIZE		(sizeof(struct fcoe_confqe))
99 #define BNX2FC_5771X_DB_PAGE_SIZE	128
100 
101 #define BNX2FC_MAX_TASKS		BNX2FC_MAX_OUTSTANDING_CMNDS
102 #define BNX2FC_TASK_SIZE		128
103 #define	BNX2FC_TASKS_PER_PAGE		(PAGE_SIZE/BNX2FC_TASK_SIZE)
104 #define BNX2FC_TASK_CTX_ARR_SZ		(BNX2FC_MAX_TASKS/BNX2FC_TASKS_PER_PAGE)
105 
106 #define BNX2FC_MAX_ROWS_IN_HASH_TBL	8
107 #define BNX2FC_HASH_TBL_CHUNK_SIZE	(16 * 1024)
108 
109 #define BNX2FC_MAX_SEQS			255
110 
111 #define BNX2FC_READ			(1 << 1)
112 #define BNX2FC_WRITE			(1 << 0)
113 
114 #define BNX2FC_MIN_XID			0
115 #define BNX2FC_MAX_XID			(BNX2FC_MAX_OUTSTANDING_CMNDS - 1)
116 #define FCOE_MIN_XID			(BNX2FC_MAX_OUTSTANDING_CMNDS)
117 #define FCOE_MAX_XID		\
118 			(BNX2FC_MAX_OUTSTANDING_CMNDS + (nr_cpu_ids * 256))
119 #define BNX2FC_MAX_LUN			0xFFFF
120 #define BNX2FC_MAX_FCP_TGT		256
121 #define BNX2FC_MAX_CMD_LEN		16
122 
123 #define BNX2FC_TM_TIMEOUT		60	/* secs */
124 #define BNX2FC_IO_TIMEOUT		20000UL	/* msecs */
125 
126 #define BNX2FC_WAIT_CNT			120
127 #define BNX2FC_FW_TIMEOUT		(3 * HZ)
128 
129 #define PORT_MAX			2
130 
131 #define CMD_SCSI_STATUS(Cmnd)		((Cmnd)->SCp.Status)
132 
133 /* FC FCP Status */
134 #define	FC_GOOD				0
135 
136 #define BNX2FC_RNID_HBA			0x7
137 
138 /* bnx2fc driver uses only one instance of fcoe_percpu_s */
139 extern struct fcoe_percpu_s bnx2fc_global;
140 
141 extern struct workqueue_struct *bnx2fc_wq;
142 
143 struct bnx2fc_percpu_s {
144 	struct task_struct *iothread;
145 	struct list_head work_list;
146 	spinlock_t fp_work_lock;
147 };
148 
149 
150 struct bnx2fc_hba {
151 	struct list_head link;
152 	struct cnic_dev *cnic;
153 	struct pci_dev *pcidev;
154 	struct net_device *netdev;
155 	struct net_device *phys_dev;
156 	unsigned long reg_with_cnic;
157 		#define BNX2FC_CNIC_REGISTERED           1
158 	struct packet_type fcoe_packet_type;
159 	struct packet_type fip_packet_type;
160 	struct bnx2fc_cmd_mgr *cmd_mgr;
161 	struct workqueue_struct *timer_work_queue;
162 	struct kref kref;
163 	spinlock_t hba_lock;
164 	struct mutex hba_mutex;
165 	unsigned long adapter_state;
166 		#define ADAPTER_STATE_UP		0
167 		#define ADAPTER_STATE_GOING_DOWN	1
168 		#define ADAPTER_STATE_LINK_DOWN		2
169 		#define ADAPTER_STATE_READY		3
170 	u32 flags;
171 	unsigned long init_done;
172 		#define BNX2FC_FW_INIT_DONE		0
173 		#define BNX2FC_CTLR_INIT_DONE		1
174 		#define BNX2FC_CREATE_DONE		2
175 	struct fcoe_ctlr ctlr;
176 	u8 vlan_enabled;
177 	int vlan_id;
178 	u32 next_conn_id;
179 	struct fcoe_task_ctx_entry **task_ctx;
180 	dma_addr_t *task_ctx_dma;
181 	struct regpair *task_ctx_bd_tbl;
182 	dma_addr_t task_ctx_bd_dma;
183 
184 	int hash_tbl_segment_count;
185 	void **hash_tbl_segments;
186 	void *hash_tbl_pbl;
187 	dma_addr_t hash_tbl_pbl_dma;
188 	struct fcoe_t2_hash_table_entry *t2_hash_tbl;
189 	dma_addr_t t2_hash_tbl_dma;
190 	char *t2_hash_tbl_ptr;
191 	dma_addr_t t2_hash_tbl_ptr_dma;
192 
193 	char *dummy_buffer;
194 	dma_addr_t dummy_buf_dma;
195 
196 	struct fcoe_statistics_params *stats_buffer;
197 	dma_addr_t stats_buf_dma;
198 
199 	/*
200 	 * PCI related info.
201 	 */
202 	u16 pci_did;
203 	u16 pci_vid;
204 	u16 pci_sdid;
205 	u16 pci_svid;
206 	u16 pci_func;
207 	u16 pci_devno;
208 
209 	struct task_struct *l2_thread;
210 
211 	/* linkdown handling */
212 	wait_queue_head_t shutdown_wait;
213 	int wait_for_link_down;
214 
215 	/*destroy handling */
216 	struct timer_list destroy_timer;
217 	wait_queue_head_t destroy_wait;
218 
219 	/* Active list of offloaded sessions */
220 	struct bnx2fc_rport *tgt_ofld_list[BNX2FC_NUM_MAX_SESS];
221 	int num_ofld_sess;
222 
223 	/* statistics */
224 	struct completion stat_req_done;
225 };
226 
227 #define bnx2fc_from_ctlr(fip) container_of(fip, struct bnx2fc_hba, ctlr)
228 
229 struct bnx2fc_cmd_mgr {
230 	struct bnx2fc_hba *hba;
231 	u16 next_idx;
232 	struct list_head *free_list;
233 	spinlock_t *free_list_lock;
234 	struct io_bdt **io_bdt_pool;
235 	struct bnx2fc_cmd **cmds;
236 };
237 
238 struct bnx2fc_rport {
239 	struct fcoe_port *port;
240 	struct fc_rport *rport;
241 	struct fc_rport_priv *rdata;
242 	void __iomem *ctx_base;
243 #define DPM_TRIGER_TYPE		0x40
244 	u32 fcoe_conn_id;
245 	u32 context_id;
246 	u32 sid;
247 
248 	unsigned long flags;
249 #define BNX2FC_FLAG_SESSION_READY	0x1
250 #define BNX2FC_FLAG_OFFLOADED		0x2
251 #define BNX2FC_FLAG_DISABLED		0x3
252 #define BNX2FC_FLAG_DESTROYED		0x4
253 #define BNX2FC_FLAG_OFLD_REQ_CMPL	0x5
254 #define BNX2FC_FLAG_DESTROY_CMPL	0x6
255 #define BNX2FC_FLAG_CTX_ALLOC_FAILURE	0x7
256 #define BNX2FC_FLAG_UPLD_REQ_COMPL	0x8
257 #define BNX2FC_FLAG_EXPL_LOGO		0x9
258 
259 	u32 max_sqes;
260 	u32 max_rqes;
261 	u32 max_cqes;
262 
263 	struct fcoe_sqe *sq;
264 	dma_addr_t sq_dma;
265 	u16 sq_prod_idx;
266 	u8 sq_curr_toggle_bit;
267 	u32 sq_mem_size;
268 
269 	struct fcoe_cqe *cq;
270 	dma_addr_t cq_dma;
271 	u32 cq_cons_idx;
272 	u8 cq_curr_toggle_bit;
273 	u32 cq_mem_size;
274 
275 	void *rq;
276 	dma_addr_t rq_dma;
277 	u32 rq_prod_idx;
278 	u32 rq_cons_idx;
279 	u32 rq_mem_size;
280 
281 	void *rq_pbl;
282 	dma_addr_t rq_pbl_dma;
283 	u32 rq_pbl_size;
284 
285 	struct fcoe_xfrqe *xferq;
286 	dma_addr_t xferq_dma;
287 	u32 xferq_mem_size;
288 
289 	struct fcoe_confqe *confq;
290 	dma_addr_t confq_dma;
291 	u32 confq_mem_size;
292 
293 	void *confq_pbl;
294 	dma_addr_t confq_pbl_dma;
295 	u32 confq_pbl_size;
296 
297 	struct fcoe_conn_db *conn_db;
298 	dma_addr_t conn_db_dma;
299 	u32 conn_db_mem_size;
300 
301 	struct fcoe_sqe *lcq;
302 	dma_addr_t lcq_dma;
303 	u32 lcq_mem_size;
304 
305 	void *ofld_req[4];
306 	dma_addr_t ofld_req_dma[4];
307 	void *enbl_req;
308 	dma_addr_t enbl_req_dma;
309 
310 	spinlock_t tgt_lock;
311 	spinlock_t cq_lock;
312 	atomic_t num_active_ios;
313 	u32 flush_in_prog;
314 	unsigned long work_time_slice;
315 	unsigned long timestamp;
316 	struct list_head free_task_list;
317 	struct bnx2fc_cmd *pending_queue[BNX2FC_SQ_WQES_MAX+1];
318 	atomic_t pi;
319 	atomic_t ci;
320 	struct list_head active_cmd_queue;
321 	struct list_head els_queue;
322 	struct list_head io_retire_queue;
323 	struct list_head active_tm_queue;
324 
325 	struct timer_list ofld_timer;
326 	wait_queue_head_t ofld_wait;
327 
328 	struct timer_list upld_timer;
329 	wait_queue_head_t upld_wait;
330 };
331 
332 struct bnx2fc_mp_req {
333 	u8 tm_flags;
334 
335 	u32 req_len;
336 	void *req_buf;
337 	dma_addr_t req_buf_dma;
338 	struct fcoe_bd_ctx *mp_req_bd;
339 	dma_addr_t mp_req_bd_dma;
340 	struct fc_frame_header req_fc_hdr;
341 
342 	u32 resp_len;
343 	void *resp_buf;
344 	dma_addr_t resp_buf_dma;
345 	struct fcoe_bd_ctx *mp_resp_bd;
346 	dma_addr_t mp_resp_bd_dma;
347 	struct fc_frame_header resp_fc_hdr;
348 };
349 
350 struct bnx2fc_els_cb_arg {
351 	struct bnx2fc_cmd *aborted_io_req;
352 	struct bnx2fc_cmd *io_req;
353 	u16 l2_oxid;
354 };
355 
356 /* bnx2fc command structure */
357 struct bnx2fc_cmd {
358 	struct list_head link;
359 	u8 on_active_queue;
360 	u8 on_tmf_queue;
361 	u8 cmd_type;
362 #define BNX2FC_SCSI_CMD		1
363 #define BNX2FC_TASK_MGMT_CMD		2
364 #define BNX2FC_ABTS			3
365 #define BNX2FC_ELS			4
366 #define BNX2FC_CLEANUP			5
367 	u8 io_req_flags;
368 	struct kref refcount;
369 	struct fcoe_port *port;
370 	struct bnx2fc_rport *tgt;
371 	struct scsi_cmnd *sc_cmd;
372 	struct bnx2fc_cmd_mgr *cmd_mgr;
373 	struct bnx2fc_mp_req mp_req;
374 	void (*cb_func)(struct bnx2fc_els_cb_arg *cb_arg);
375 	struct bnx2fc_els_cb_arg *cb_arg;
376 	struct delayed_work timeout_work; /* timer for ULP timeouts */
377 	struct completion tm_done;
378 	int wait_for_comp;
379 	u16 xid;
380 	struct fcoe_task_ctx_entry *task;
381 	struct io_bdt *bd_tbl;
382 	struct fcp_rsp *rsp;
383 	size_t data_xfer_len;
384 	unsigned long req_flags;
385 #define BNX2FC_FLAG_ISSUE_RRQ		0x1
386 #define BNX2FC_FLAG_ISSUE_ABTS		0x2
387 #define BNX2FC_FLAG_ABTS_DONE		0x3
388 #define BNX2FC_FLAG_TM_COMPL		0x4
389 #define BNX2FC_FLAG_TM_TIMEOUT		0x5
390 #define BNX2FC_FLAG_IO_CLEANUP		0x6
391 #define BNX2FC_FLAG_RETIRE_OXID		0x7
392 #define	BNX2FC_FLAG_EH_ABORT		0x8
393 #define BNX2FC_FLAG_IO_COMPL		0x9
394 #define BNX2FC_FLAG_ELS_DONE		0xa
395 #define BNX2FC_FLAG_ELS_TIMEOUT		0xb
396 	u32 fcp_resid;
397 	u32 fcp_rsp_len;
398 	u32 fcp_sns_len;
399 	u8 cdb_status; /* SCSI IO status */
400 	u8 fcp_status; /* FCP IO status */
401 	u8 fcp_rsp_code;
402 	u8 scsi_comp_flags;
403 };
404 
405 struct io_bdt {
406 	struct bnx2fc_cmd *io_req;
407 	struct fcoe_bd_ctx *bd_tbl;
408 	dma_addr_t bd_tbl_dma;
409 	u16 bd_valid;
410 };
411 
412 struct bnx2fc_work {
413 	struct list_head list;
414 	struct bnx2fc_rport *tgt;
415 	u16 wqe;
416 };
417 struct bnx2fc_unsol_els {
418 	struct fc_lport *lport;
419 	struct fc_frame *fp;
420 	struct work_struct unsol_els_work;
421 };
422 
423 
424 
425 struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type);
426 void bnx2fc_cmd_release(struct kref *ref);
427 int bnx2fc_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *sc_cmd);
428 int bnx2fc_send_fw_fcoe_init_msg(struct bnx2fc_hba *hba);
429 int bnx2fc_send_fw_fcoe_destroy_msg(struct bnx2fc_hba *hba);
430 int bnx2fc_send_session_ofld_req(struct fcoe_port *port,
431 					struct bnx2fc_rport *tgt);
432 int bnx2fc_send_session_disable_req(struct fcoe_port *port,
433 				    struct bnx2fc_rport *tgt);
434 int bnx2fc_send_session_destroy_req(struct bnx2fc_hba *hba,
435 					struct bnx2fc_rport *tgt);
436 int bnx2fc_map_doorbell(struct bnx2fc_rport *tgt);
437 void bnx2fc_indicate_kcqe(void *context, struct kcqe *kcq[],
438 					u32 num_cqe);
439 int bnx2fc_setup_task_ctx(struct bnx2fc_hba *hba);
440 void bnx2fc_free_task_ctx(struct bnx2fc_hba *hba);
441 int bnx2fc_setup_fw_resc(struct bnx2fc_hba *hba);
442 void bnx2fc_free_fw_resc(struct bnx2fc_hba *hba);
443 struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba,
444 						u16 min_xid, u16 max_xid);
445 void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr);
446 void bnx2fc_get_link_state(struct bnx2fc_hba *hba);
447 char *bnx2fc_get_next_rqe(struct bnx2fc_rport *tgt, u8 num_items);
448 void bnx2fc_return_rqe(struct bnx2fc_rport *tgt, u8 num_items);
449 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen);
450 int bnx2fc_send_rrq(struct bnx2fc_cmd *aborted_io_req);
451 int bnx2fc_send_adisc(struct bnx2fc_rport *tgt, struct fc_frame *fp);
452 int bnx2fc_send_logo(struct bnx2fc_rport *tgt, struct fc_frame *fp);
453 int bnx2fc_send_rls(struct bnx2fc_rport *tgt, struct fc_frame *fp);
454 int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req);
455 int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req);
456 void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
457 			  unsigned int timer_msec);
458 int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req);
459 void bnx2fc_init_cleanup_task(struct bnx2fc_cmd *io_req,
460 			      struct fcoe_task_ctx_entry *task,
461 			      u16 orig_xid);
462 void bnx2fc_init_mp_task(struct bnx2fc_cmd *io_req,
463 			 struct fcoe_task_ctx_entry *task);
464 void bnx2fc_init_task(struct bnx2fc_cmd *io_req,
465 			     struct fcoe_task_ctx_entry *task);
466 void bnx2fc_add_2_sq(struct bnx2fc_rport *tgt, u16 xid);
467 void bnx2fc_ring_doorbell(struct bnx2fc_rport *tgt);
468 int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd);
469 int bnx2fc_eh_host_reset(struct scsi_cmnd *sc_cmd);
470 int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd);
471 int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd);
472 void bnx2fc_rport_event_handler(struct fc_lport *lport,
473 				struct fc_rport_priv *rport,
474 				enum fc_rport_event event);
475 void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
476 				   struct fcoe_task_ctx_entry *task,
477 				   u8 num_rq);
478 void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
479 			       struct fcoe_task_ctx_entry *task,
480 			       u8 num_rq);
481 void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
482 			       struct fcoe_task_ctx_entry *task,
483 			       u8 num_rq);
484 void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
485 			     struct fcoe_task_ctx_entry *task,
486 			     u8 num_rq);
487 void bnx2fc_process_els_compl(struct bnx2fc_cmd *els_req,
488 			      struct fcoe_task_ctx_entry *task,
489 			      u8 num_rq);
490 void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
491 			   struct fcp_cmnd *fcp_cmnd);
492 
493 
494 
495 void bnx2fc_flush_active_ios(struct bnx2fc_rport *tgt);
496 struct fc_seq *bnx2fc_elsct_send(struct fc_lport *lport, u32 did,
497 				      struct fc_frame *fp, unsigned int op,
498 				      void (*resp)(struct fc_seq *,
499 						   struct fc_frame *,
500 						   void *),
501 				      void *arg, u32 timeout);
502 int bnx2fc_process_new_cqes(struct bnx2fc_rport *tgt);
503 void bnx2fc_process_cq_compl(struct bnx2fc_rport *tgt, u16 wqe);
504 struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
505 					     u32 port_id);
506 void bnx2fc_process_l2_frame_compl(struct bnx2fc_rport *tgt,
507 				   unsigned char *buf,
508 				   u32 frame_len, u16 l2_oxid);
509 int bnx2fc_send_stat_req(struct bnx2fc_hba *hba);
510 
511 #endif
512