xref: /OK3568_Linux_fs/kernel/drivers/scsi/bnx2fc/bnx2fc_tgt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* bnx2fc_tgt.c: QLogic Linux FCoE offload driver.
2*4882a593Smuzhiyun  * Handles operations such as session offload/upload etc, and manages
3*4882a593Smuzhiyun  * session resources such as connection id and qp resources.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2008-2013 Broadcom Corporation
6*4882a593Smuzhiyun  * Copyright (c) 2014-2016 QLogic Corporation
7*4882a593Smuzhiyun  * Copyright (c) 2016-2017 Cavium Inc.
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This program is free software; you can redistribute it and/or modify
10*4882a593Smuzhiyun  * it under the terms of the GNU General Public License as published by
11*4882a593Smuzhiyun  * the Free Software Foundation.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "bnx2fc.h"
17*4882a593Smuzhiyun static void bnx2fc_upld_timer(struct timer_list *t);
18*4882a593Smuzhiyun static void bnx2fc_ofld_timer(struct timer_list *t);
19*4882a593Smuzhiyun static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
20*4882a593Smuzhiyun 			   struct fcoe_port *port,
21*4882a593Smuzhiyun 			   struct fc_rport_priv *rdata);
22*4882a593Smuzhiyun static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
23*4882a593Smuzhiyun 				struct bnx2fc_rport *tgt);
24*4882a593Smuzhiyun static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
25*4882a593Smuzhiyun 			      struct bnx2fc_rport *tgt);
26*4882a593Smuzhiyun static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
27*4882a593Smuzhiyun 			      struct bnx2fc_rport *tgt);
28*4882a593Smuzhiyun static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id);
29*4882a593Smuzhiyun 
bnx2fc_upld_timer(struct timer_list * t)30*4882a593Smuzhiyun static void bnx2fc_upld_timer(struct timer_list *t)
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	struct bnx2fc_rport *tgt = from_timer(tgt, t, upld_timer);
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "upld_timer - Upload compl not received!!\n");
36*4882a593Smuzhiyun 	/* fake upload completion */
37*4882a593Smuzhiyun 	clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
38*4882a593Smuzhiyun 	clear_bit(BNX2FC_FLAG_ENABLED, &tgt->flags);
39*4882a593Smuzhiyun 	set_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
40*4882a593Smuzhiyun 	wake_up_interruptible(&tgt->upld_wait);
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
bnx2fc_ofld_timer(struct timer_list * t)43*4882a593Smuzhiyun static void bnx2fc_ofld_timer(struct timer_list *t)
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	struct bnx2fc_rport *tgt = from_timer(tgt, t, ofld_timer);
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "entered bnx2fc_ofld_timer\n");
49*4882a593Smuzhiyun 	/* NOTE: This function should never be called, as
50*4882a593Smuzhiyun 	 * offload should never timeout
51*4882a593Smuzhiyun 	 */
52*4882a593Smuzhiyun 	/*
53*4882a593Smuzhiyun 	 * If the timer has expired, this session is dead
54*4882a593Smuzhiyun 	 * Clear offloaded flag and logout of this device.
55*4882a593Smuzhiyun 	 * Since OFFLOADED flag is cleared, this case
56*4882a593Smuzhiyun 	 * will be considered as offload error and the
57*4882a593Smuzhiyun 	 * port will be logged off, and conn_id, session
58*4882a593Smuzhiyun 	 * resources are freed up in bnx2fc_offload_session
59*4882a593Smuzhiyun 	 */
60*4882a593Smuzhiyun 	clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
61*4882a593Smuzhiyun 	clear_bit(BNX2FC_FLAG_ENABLED, &tgt->flags);
62*4882a593Smuzhiyun 	set_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
63*4882a593Smuzhiyun 	wake_up_interruptible(&tgt->ofld_wait);
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun 
bnx2fc_ofld_wait(struct bnx2fc_rport * tgt)66*4882a593Smuzhiyun static void bnx2fc_ofld_wait(struct bnx2fc_rport *tgt)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	timer_setup(&tgt->ofld_timer, bnx2fc_ofld_timer, 0);
69*4882a593Smuzhiyun 	mod_timer(&tgt->ofld_timer, jiffies + BNX2FC_FW_TIMEOUT);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	wait_event_interruptible(tgt->ofld_wait,
72*4882a593Smuzhiyun 				 (test_bit(
73*4882a593Smuzhiyun 				  BNX2FC_FLAG_OFLD_REQ_CMPL,
74*4882a593Smuzhiyun 				  &tgt->flags)));
75*4882a593Smuzhiyun 	if (signal_pending(current))
76*4882a593Smuzhiyun 		flush_signals(current);
77*4882a593Smuzhiyun 	del_timer_sync(&tgt->ofld_timer);
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
bnx2fc_offload_session(struct fcoe_port * port,struct bnx2fc_rport * tgt,struct fc_rport_priv * rdata)80*4882a593Smuzhiyun static void bnx2fc_offload_session(struct fcoe_port *port,
81*4882a593Smuzhiyun 					struct bnx2fc_rport *tgt,
82*4882a593Smuzhiyun 					struct fc_rport_priv *rdata)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun 	struct fc_rport *rport = rdata->rport;
85*4882a593Smuzhiyun 	struct bnx2fc_interface *interface = port->priv;
86*4882a593Smuzhiyun 	struct bnx2fc_hba *hba = interface->hba;
87*4882a593Smuzhiyun 	int rval;
88*4882a593Smuzhiyun 	int i = 0;
89*4882a593Smuzhiyun 
90*4882a593Smuzhiyun 	/* Initialize bnx2fc_rport */
91*4882a593Smuzhiyun 	/* NOTE: tgt is already bzero'd */
92*4882a593Smuzhiyun 	rval = bnx2fc_init_tgt(tgt, port, rdata);
93*4882a593Smuzhiyun 	if (rval) {
94*4882a593Smuzhiyun 		printk(KERN_ERR PFX "Failed to allocate conn id for "
95*4882a593Smuzhiyun 			"port_id (%6x)\n", rport->port_id);
96*4882a593Smuzhiyun 		goto tgt_init_err;
97*4882a593Smuzhiyun 	}
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/* Allocate session resources */
100*4882a593Smuzhiyun 	rval = bnx2fc_alloc_session_resc(hba, tgt);
101*4882a593Smuzhiyun 	if (rval) {
102*4882a593Smuzhiyun 		printk(KERN_ERR PFX "Failed to allocate resources\n");
103*4882a593Smuzhiyun 		goto ofld_err;
104*4882a593Smuzhiyun 	}
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	/*
107*4882a593Smuzhiyun 	 * Initialize FCoE session offload process.
108*4882a593Smuzhiyun 	 * Upon completion of offload process add
109*4882a593Smuzhiyun 	 * rport to list of rports
110*4882a593Smuzhiyun 	 */
111*4882a593Smuzhiyun retry_ofld:
112*4882a593Smuzhiyun 	clear_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
113*4882a593Smuzhiyun 	rval = bnx2fc_send_session_ofld_req(port, tgt);
114*4882a593Smuzhiyun 	if (rval) {
115*4882a593Smuzhiyun 		printk(KERN_ERR PFX "ofld_req failed\n");
116*4882a593Smuzhiyun 		goto ofld_err;
117*4882a593Smuzhiyun 	}
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	/*
120*4882a593Smuzhiyun 	 * wait for the session is offloaded and enabled. 3 Secs
121*4882a593Smuzhiyun 	 * should be ample time for this process to complete.
122*4882a593Smuzhiyun 	 */
123*4882a593Smuzhiyun 	bnx2fc_ofld_wait(tgt);
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (!(test_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags))) {
126*4882a593Smuzhiyun 		if (test_and_clear_bit(BNX2FC_FLAG_CTX_ALLOC_FAILURE,
127*4882a593Smuzhiyun 				       &tgt->flags)) {
128*4882a593Smuzhiyun 			BNX2FC_TGT_DBG(tgt, "ctx_alloc_failure, "
129*4882a593Smuzhiyun 				"retry ofld..%d\n", i++);
130*4882a593Smuzhiyun 			msleep_interruptible(1000);
131*4882a593Smuzhiyun 			if (i > 3) {
132*4882a593Smuzhiyun 				i = 0;
133*4882a593Smuzhiyun 				goto ofld_err;
134*4882a593Smuzhiyun 			}
135*4882a593Smuzhiyun 			goto retry_ofld;
136*4882a593Smuzhiyun 		}
137*4882a593Smuzhiyun 		goto ofld_err;
138*4882a593Smuzhiyun 	}
139*4882a593Smuzhiyun 	if (bnx2fc_map_doorbell(tgt)) {
140*4882a593Smuzhiyun 		printk(KERN_ERR PFX "map doorbell failed - no mem\n");
141*4882a593Smuzhiyun 		goto ofld_err;
142*4882a593Smuzhiyun 	}
143*4882a593Smuzhiyun 	clear_bit(BNX2FC_FLAG_OFLD_REQ_CMPL, &tgt->flags);
144*4882a593Smuzhiyun 	rval = bnx2fc_send_session_enable_req(port, tgt);
145*4882a593Smuzhiyun 	if (rval) {
146*4882a593Smuzhiyun 		pr_err(PFX "enable session failed\n");
147*4882a593Smuzhiyun 		goto ofld_err;
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 	bnx2fc_ofld_wait(tgt);
150*4882a593Smuzhiyun 	if (!(test_bit(BNX2FC_FLAG_ENABLED, &tgt->flags)))
151*4882a593Smuzhiyun 		goto ofld_err;
152*4882a593Smuzhiyun 	return;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun ofld_err:
155*4882a593Smuzhiyun 	/* couldn't offload the session. log off from this rport */
156*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "bnx2fc_offload_session - offload error\n");
157*4882a593Smuzhiyun 	clear_bit(BNX2FC_FLAG_OFFLOADED, &tgt->flags);
158*4882a593Smuzhiyun 	/* Free session resources */
159*4882a593Smuzhiyun 	bnx2fc_free_session_resc(hba, tgt);
160*4882a593Smuzhiyun tgt_init_err:
161*4882a593Smuzhiyun 	if (tgt->fcoe_conn_id != -1)
162*4882a593Smuzhiyun 		bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
163*4882a593Smuzhiyun 	fc_rport_logoff(rdata);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
bnx2fc_flush_active_ios(struct bnx2fc_rport * tgt)166*4882a593Smuzhiyun void bnx2fc_flush_active_ios(struct bnx2fc_rport *tgt)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	struct bnx2fc_cmd *io_req;
169*4882a593Smuzhiyun 	struct bnx2fc_cmd *tmp;
170*4882a593Smuzhiyun 	int rc;
171*4882a593Smuzhiyun 	int i = 0;
172*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "Entered flush_active_ios - %d\n",
173*4882a593Smuzhiyun 		       tgt->num_active_ios.counter);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	spin_lock_bh(&tgt->tgt_lock);
176*4882a593Smuzhiyun 	tgt->flush_in_prog = 1;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	list_for_each_entry_safe(io_req, tmp, &tgt->active_cmd_queue, link) {
179*4882a593Smuzhiyun 		i++;
180*4882a593Smuzhiyun 		list_del_init(&io_req->link);
181*4882a593Smuzhiyun 		io_req->on_active_queue = 0;
182*4882a593Smuzhiyun 		BNX2FC_IO_DBG(io_req, "cmd_queue cleanup\n");
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 		if (cancel_delayed_work(&io_req->timeout_work)) {
185*4882a593Smuzhiyun 			if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
186*4882a593Smuzhiyun 						&io_req->req_flags)) {
187*4882a593Smuzhiyun 				/* Handle eh_abort timeout */
188*4882a593Smuzhiyun 				BNX2FC_IO_DBG(io_req, "eh_abort for IO "
189*4882a593Smuzhiyun 					      "cleaned up\n");
190*4882a593Smuzhiyun 				complete(&io_req->abts_done);
191*4882a593Smuzhiyun 			}
192*4882a593Smuzhiyun 			kref_put(&io_req->refcount,
193*4882a593Smuzhiyun 				 bnx2fc_cmd_release); /* drop timer hold */
194*4882a593Smuzhiyun 		}
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags);
197*4882a593Smuzhiyun 		set_bit(BNX2FC_FLAG_IO_CLEANUP, &io_req->req_flags);
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 		/* Do not issue cleanup when disable request failed */
200*4882a593Smuzhiyun 		if (test_bit(BNX2FC_FLAG_DISABLE_FAILED, &tgt->flags))
201*4882a593Smuzhiyun 			bnx2fc_process_cleanup_compl(io_req, io_req->task, 0);
202*4882a593Smuzhiyun 		else {
203*4882a593Smuzhiyun 			rc = bnx2fc_initiate_cleanup(io_req);
204*4882a593Smuzhiyun 			BUG_ON(rc);
205*4882a593Smuzhiyun 		}
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	list_for_each_entry_safe(io_req, tmp, &tgt->active_tm_queue, link) {
209*4882a593Smuzhiyun 		i++;
210*4882a593Smuzhiyun 		list_del_init(&io_req->link);
211*4882a593Smuzhiyun 		io_req->on_tmf_queue = 0;
212*4882a593Smuzhiyun 		BNX2FC_IO_DBG(io_req, "tm_queue cleanup\n");
213*4882a593Smuzhiyun 		if (io_req->wait_for_abts_comp)
214*4882a593Smuzhiyun 			complete(&io_req->abts_done);
215*4882a593Smuzhiyun 	}
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	list_for_each_entry_safe(io_req, tmp, &tgt->els_queue, link) {
218*4882a593Smuzhiyun 		i++;
219*4882a593Smuzhiyun 		list_del_init(&io_req->link);
220*4882a593Smuzhiyun 		io_req->on_active_queue = 0;
221*4882a593Smuzhiyun 
222*4882a593Smuzhiyun 		BNX2FC_IO_DBG(io_req, "els_queue cleanup\n");
223*4882a593Smuzhiyun 
224*4882a593Smuzhiyun 		if (cancel_delayed_work(&io_req->timeout_work))
225*4882a593Smuzhiyun 			kref_put(&io_req->refcount,
226*4882a593Smuzhiyun 				 bnx2fc_cmd_release); /* drop timer hold */
227*4882a593Smuzhiyun 
228*4882a593Smuzhiyun 		if ((io_req->cb_func) && (io_req->cb_arg)) {
229*4882a593Smuzhiyun 			io_req->cb_func(io_req->cb_arg);
230*4882a593Smuzhiyun 			io_req->cb_arg = NULL;
231*4882a593Smuzhiyun 		}
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 		/* Do not issue cleanup when disable request failed */
234*4882a593Smuzhiyun 		if (test_bit(BNX2FC_FLAG_DISABLE_FAILED, &tgt->flags))
235*4882a593Smuzhiyun 			bnx2fc_process_cleanup_compl(io_req, io_req->task, 0);
236*4882a593Smuzhiyun 		else {
237*4882a593Smuzhiyun 			rc = bnx2fc_initiate_cleanup(io_req);
238*4882a593Smuzhiyun 			BUG_ON(rc);
239*4882a593Smuzhiyun 		}
240*4882a593Smuzhiyun 	}
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	list_for_each_entry_safe(io_req, tmp, &tgt->io_retire_queue, link) {
243*4882a593Smuzhiyun 		i++;
244*4882a593Smuzhiyun 		list_del_init(&io_req->link);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 		BNX2FC_IO_DBG(io_req, "retire_queue flush\n");
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 		if (cancel_delayed_work(&io_req->timeout_work)) {
249*4882a593Smuzhiyun 			if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
250*4882a593Smuzhiyun 						&io_req->req_flags)) {
251*4882a593Smuzhiyun 				/* Handle eh_abort timeout */
252*4882a593Smuzhiyun 				BNX2FC_IO_DBG(io_req, "eh_abort for IO "
253*4882a593Smuzhiyun 					      "in retire_q\n");
254*4882a593Smuzhiyun 				if (io_req->wait_for_abts_comp)
255*4882a593Smuzhiyun 					complete(&io_req->abts_done);
256*4882a593Smuzhiyun 			}
257*4882a593Smuzhiyun 			kref_put(&io_req->refcount, bnx2fc_cmd_release);
258*4882a593Smuzhiyun 		}
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 		clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
261*4882a593Smuzhiyun 	}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "IOs flushed = %d\n", i);
264*4882a593Smuzhiyun 	i = 0;
265*4882a593Smuzhiyun 	spin_unlock_bh(&tgt->tgt_lock);
266*4882a593Smuzhiyun 	/* wait for active_ios to go to 0 */
267*4882a593Smuzhiyun 	while ((tgt->num_active_ios.counter != 0) && (i++ < BNX2FC_WAIT_CNT))
268*4882a593Smuzhiyun 		msleep(25);
269*4882a593Smuzhiyun 	if (tgt->num_active_ios.counter != 0)
270*4882a593Smuzhiyun 		printk(KERN_ERR PFX "CLEANUP on port 0x%x:"
271*4882a593Smuzhiyun 				    " active_ios = %d\n",
272*4882a593Smuzhiyun 			tgt->rdata->ids.port_id, tgt->num_active_ios.counter);
273*4882a593Smuzhiyun 	spin_lock_bh(&tgt->tgt_lock);
274*4882a593Smuzhiyun 	tgt->flush_in_prog = 0;
275*4882a593Smuzhiyun 	spin_unlock_bh(&tgt->tgt_lock);
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun 
bnx2fc_upld_wait(struct bnx2fc_rport * tgt)278*4882a593Smuzhiyun static void bnx2fc_upld_wait(struct bnx2fc_rport *tgt)
279*4882a593Smuzhiyun {
280*4882a593Smuzhiyun 	timer_setup(&tgt->upld_timer, bnx2fc_upld_timer, 0);
281*4882a593Smuzhiyun 	mod_timer(&tgt->upld_timer, jiffies + BNX2FC_FW_TIMEOUT);
282*4882a593Smuzhiyun 	wait_event_interruptible(tgt->upld_wait,
283*4882a593Smuzhiyun 				 (test_bit(
284*4882a593Smuzhiyun 				  BNX2FC_FLAG_UPLD_REQ_COMPL,
285*4882a593Smuzhiyun 				  &tgt->flags)));
286*4882a593Smuzhiyun 	if (signal_pending(current))
287*4882a593Smuzhiyun 		flush_signals(current);
288*4882a593Smuzhiyun 	del_timer_sync(&tgt->upld_timer);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun 
bnx2fc_upload_session(struct fcoe_port * port,struct bnx2fc_rport * tgt)291*4882a593Smuzhiyun static void bnx2fc_upload_session(struct fcoe_port *port,
292*4882a593Smuzhiyun 					struct bnx2fc_rport *tgt)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun 	struct bnx2fc_interface *interface = port->priv;
295*4882a593Smuzhiyun 	struct bnx2fc_hba *hba = interface->hba;
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "upload_session: active_ios = %d\n",
298*4882a593Smuzhiyun 		tgt->num_active_ios.counter);
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun 	/*
301*4882a593Smuzhiyun 	 * Called with hba->hba_mutex held.
302*4882a593Smuzhiyun 	 * This is a blocking call
303*4882a593Smuzhiyun 	 */
304*4882a593Smuzhiyun 	clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
305*4882a593Smuzhiyun 	bnx2fc_send_session_disable_req(port, tgt);
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun 	/*
308*4882a593Smuzhiyun 	 * wait for upload to complete. 3 Secs
309*4882a593Smuzhiyun 	 * should be sufficient time for this process to complete.
310*4882a593Smuzhiyun 	 */
311*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "waiting for disable compl\n");
312*4882a593Smuzhiyun 	bnx2fc_upld_wait(tgt);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	/*
315*4882a593Smuzhiyun 	 * traverse thru the active_q and tmf_q and cleanup
316*4882a593Smuzhiyun 	 * IOs in these lists
317*4882a593Smuzhiyun 	 */
318*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "flush/upload - disable wait flags = 0x%lx\n",
319*4882a593Smuzhiyun 		       tgt->flags);
320*4882a593Smuzhiyun 	bnx2fc_flush_active_ios(tgt);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	/* Issue destroy KWQE */
323*4882a593Smuzhiyun 	if (test_bit(BNX2FC_FLAG_DISABLED, &tgt->flags)) {
324*4882a593Smuzhiyun 		BNX2FC_TGT_DBG(tgt, "send destroy req\n");
325*4882a593Smuzhiyun 		clear_bit(BNX2FC_FLAG_UPLD_REQ_COMPL, &tgt->flags);
326*4882a593Smuzhiyun 		bnx2fc_send_session_destroy_req(hba, tgt);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 		/* wait for destroy to complete */
329*4882a593Smuzhiyun 		bnx2fc_upld_wait(tgt);
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 		if (!(test_bit(BNX2FC_FLAG_DESTROYED, &tgt->flags)))
332*4882a593Smuzhiyun 			printk(KERN_ERR PFX "ERROR!! destroy timed out\n");
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		BNX2FC_TGT_DBG(tgt, "destroy wait complete flags = 0x%lx\n",
335*4882a593Smuzhiyun 			tgt->flags);
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 	} else if (test_bit(BNX2FC_FLAG_DISABLE_FAILED, &tgt->flags)) {
338*4882a593Smuzhiyun 		printk(KERN_ERR PFX "ERROR!! DISABLE req failed, destroy"
339*4882a593Smuzhiyun 				" not sent to FW\n");
340*4882a593Smuzhiyun 	} else {
341*4882a593Smuzhiyun 		printk(KERN_ERR PFX "ERROR!! DISABLE req timed out, destroy"
342*4882a593Smuzhiyun 				" not sent to FW\n");
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	/* Free session resources */
346*4882a593Smuzhiyun 	bnx2fc_free_session_resc(hba, tgt);
347*4882a593Smuzhiyun 	bnx2fc_free_conn_id(hba, tgt->fcoe_conn_id);
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun 
bnx2fc_init_tgt(struct bnx2fc_rport * tgt,struct fcoe_port * port,struct fc_rport_priv * rdata)350*4882a593Smuzhiyun static int bnx2fc_init_tgt(struct bnx2fc_rport *tgt,
351*4882a593Smuzhiyun 			   struct fcoe_port *port,
352*4882a593Smuzhiyun 			   struct fc_rport_priv *rdata)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	struct fc_rport *rport = rdata->rport;
356*4882a593Smuzhiyun 	struct bnx2fc_interface *interface = port->priv;
357*4882a593Smuzhiyun 	struct bnx2fc_hba *hba = interface->hba;
358*4882a593Smuzhiyun 	struct b577xx_doorbell_set_prod *sq_db = &tgt->sq_db;
359*4882a593Smuzhiyun 	struct b577xx_fcoe_rx_doorbell *rx_db = &tgt->rx_db;
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	tgt->rport = rport;
362*4882a593Smuzhiyun 	tgt->rdata = rdata;
363*4882a593Smuzhiyun 	tgt->port = port;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	if (hba->num_ofld_sess >= BNX2FC_NUM_MAX_SESS) {
366*4882a593Smuzhiyun 		BNX2FC_TGT_DBG(tgt, "exceeded max sessions. logoff this tgt\n");
367*4882a593Smuzhiyun 		tgt->fcoe_conn_id = -1;
368*4882a593Smuzhiyun 		return -1;
369*4882a593Smuzhiyun 	}
370*4882a593Smuzhiyun 
371*4882a593Smuzhiyun 	tgt->fcoe_conn_id = bnx2fc_alloc_conn_id(hba, tgt);
372*4882a593Smuzhiyun 	if (tgt->fcoe_conn_id == -1)
373*4882a593Smuzhiyun 		return -1;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "init_tgt - conn_id = 0x%x\n", tgt->fcoe_conn_id);
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	tgt->max_sqes = BNX2FC_SQ_WQES_MAX;
378*4882a593Smuzhiyun 	tgt->max_rqes = BNX2FC_RQ_WQES_MAX;
379*4882a593Smuzhiyun 	tgt->max_cqes = BNX2FC_CQ_WQES_MAX;
380*4882a593Smuzhiyun 	atomic_set(&tgt->free_sqes, BNX2FC_SQ_WQES_MAX);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 	/* Initialize the toggle bit */
383*4882a593Smuzhiyun 	tgt->sq_curr_toggle_bit = 1;
384*4882a593Smuzhiyun 	tgt->cq_curr_toggle_bit = 1;
385*4882a593Smuzhiyun 	tgt->sq_prod_idx = 0;
386*4882a593Smuzhiyun 	tgt->cq_cons_idx = 0;
387*4882a593Smuzhiyun 	tgt->rq_prod_idx = 0x8000;
388*4882a593Smuzhiyun 	tgt->rq_cons_idx = 0;
389*4882a593Smuzhiyun 	atomic_set(&tgt->num_active_ios, 0);
390*4882a593Smuzhiyun 	tgt->retry_delay_timestamp = 0;
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if (rdata->flags & FC_RP_FLAGS_RETRY &&
393*4882a593Smuzhiyun 	    rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
394*4882a593Smuzhiyun 	    !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
395*4882a593Smuzhiyun 		tgt->dev_type = TYPE_TAPE;
396*4882a593Smuzhiyun 		tgt->io_timeout = 0; /* use default ULP timeout */
397*4882a593Smuzhiyun 	} else {
398*4882a593Smuzhiyun 		tgt->dev_type = TYPE_DISK;
399*4882a593Smuzhiyun 		tgt->io_timeout = BNX2FC_IO_TIMEOUT;
400*4882a593Smuzhiyun 	}
401*4882a593Smuzhiyun 
402*4882a593Smuzhiyun 	/* initialize sq doorbell */
403*4882a593Smuzhiyun 	sq_db->header.header = B577XX_DOORBELL_HDR_DB_TYPE;
404*4882a593Smuzhiyun 	sq_db->header.header |= B577XX_FCOE_CONNECTION_TYPE <<
405*4882a593Smuzhiyun 					B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT;
406*4882a593Smuzhiyun 	/* initialize rx doorbell */
407*4882a593Smuzhiyun 	rx_db->hdr.header = ((0x1 << B577XX_DOORBELL_HDR_RX_SHIFT) |
408*4882a593Smuzhiyun 			  (0x1 << B577XX_DOORBELL_HDR_DB_TYPE_SHIFT) |
409*4882a593Smuzhiyun 			  (B577XX_FCOE_CONNECTION_TYPE <<
410*4882a593Smuzhiyun 				B577XX_DOORBELL_HDR_CONN_TYPE_SHIFT));
411*4882a593Smuzhiyun 	rx_db->params = (0x2 << B577XX_FCOE_RX_DOORBELL_NEGATIVE_ARM_SHIFT) |
412*4882a593Smuzhiyun 		     (0x3 << B577XX_FCOE_RX_DOORBELL_OPCODE_SHIFT);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	spin_lock_init(&tgt->tgt_lock);
415*4882a593Smuzhiyun 	spin_lock_init(&tgt->cq_lock);
416*4882a593Smuzhiyun 
417*4882a593Smuzhiyun 	/* Initialize active_cmd_queue list */
418*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tgt->active_cmd_queue);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	/* Initialize IO retire queue */
421*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tgt->io_retire_queue);
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tgt->els_queue);
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	/* Initialize active_tm_queue list */
426*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tgt->active_tm_queue);
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	init_waitqueue_head(&tgt->ofld_wait);
429*4882a593Smuzhiyun 	init_waitqueue_head(&tgt->upld_wait);
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	return 0;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun /*
435*4882a593Smuzhiyun  * This event_callback is called after successful completion of libfc
436*4882a593Smuzhiyun  * initiated target login. bnx2fc can proceed with initiating the session
437*4882a593Smuzhiyun  * establishment.
438*4882a593Smuzhiyun  */
bnx2fc_rport_event_handler(struct fc_lport * lport,struct fc_rport_priv * rdata,enum fc_rport_event event)439*4882a593Smuzhiyun void bnx2fc_rport_event_handler(struct fc_lport *lport,
440*4882a593Smuzhiyun 				struct fc_rport_priv *rdata,
441*4882a593Smuzhiyun 				enum fc_rport_event event)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	struct fcoe_port *port = lport_priv(lport);
444*4882a593Smuzhiyun 	struct bnx2fc_interface *interface = port->priv;
445*4882a593Smuzhiyun 	struct bnx2fc_hba *hba = interface->hba;
446*4882a593Smuzhiyun 	struct fc_rport *rport = rdata->rport;
447*4882a593Smuzhiyun 	struct fc_rport_libfc_priv *rp;
448*4882a593Smuzhiyun 	struct bnx2fc_rport *tgt;
449*4882a593Smuzhiyun 	u32 port_id;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	BNX2FC_HBA_DBG(lport, "rport_event_hdlr: event = %d, port_id = 0x%x\n",
452*4882a593Smuzhiyun 		event, rdata->ids.port_id);
453*4882a593Smuzhiyun 	switch (event) {
454*4882a593Smuzhiyun 	case RPORT_EV_READY:
455*4882a593Smuzhiyun 		if (!rport) {
456*4882a593Smuzhiyun 			printk(KERN_ERR PFX "rport is NULL: ERROR!\n");
457*4882a593Smuzhiyun 			break;
458*4882a593Smuzhiyun 		}
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 		rp = rport->dd_data;
461*4882a593Smuzhiyun 		if (rport->port_id == FC_FID_DIR_SERV) {
462*4882a593Smuzhiyun 			/*
463*4882a593Smuzhiyun 			 * bnx2fc_rport structure doesn't exist for
464*4882a593Smuzhiyun 			 * directory server.
465*4882a593Smuzhiyun 			 * We should not come here, as lport will
466*4882a593Smuzhiyun 			 * take care of fabric login
467*4882a593Smuzhiyun 			 */
468*4882a593Smuzhiyun 			printk(KERN_ERR PFX "%x - rport_event_handler ERROR\n",
469*4882a593Smuzhiyun 				rdata->ids.port_id);
470*4882a593Smuzhiyun 			break;
471*4882a593Smuzhiyun 		}
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 		if (rdata->spp_type != FC_TYPE_FCP) {
474*4882a593Smuzhiyun 			BNX2FC_HBA_DBG(lport, "not FCP type target."
475*4882a593Smuzhiyun 				   " not offloading\n");
476*4882a593Smuzhiyun 			break;
477*4882a593Smuzhiyun 		}
478*4882a593Smuzhiyun 		if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
479*4882a593Smuzhiyun 			BNX2FC_HBA_DBG(lport, "not FCP_TARGET"
480*4882a593Smuzhiyun 				   " not offloading\n");
481*4882a593Smuzhiyun 			break;
482*4882a593Smuzhiyun 		}
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 		/*
485*4882a593Smuzhiyun 		 * Offlaod process is protected with hba mutex.
486*4882a593Smuzhiyun 		 * Use the same mutex_lock for upload process too
487*4882a593Smuzhiyun 		 */
488*4882a593Smuzhiyun 		mutex_lock(&hba->hba_mutex);
489*4882a593Smuzhiyun 		tgt = (struct bnx2fc_rport *)&rp[1];
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 		/* This can happen when ADISC finds the same target */
492*4882a593Smuzhiyun 		if (test_bit(BNX2FC_FLAG_ENABLED, &tgt->flags)) {
493*4882a593Smuzhiyun 			BNX2FC_TGT_DBG(tgt, "already offloaded\n");
494*4882a593Smuzhiyun 			mutex_unlock(&hba->hba_mutex);
495*4882a593Smuzhiyun 			return;
496*4882a593Smuzhiyun 		}
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 		/*
499*4882a593Smuzhiyun 		 * Offload the session. This is a blocking call, and will
500*4882a593Smuzhiyun 		 * wait until the session is offloaded.
501*4882a593Smuzhiyun 		 */
502*4882a593Smuzhiyun 		bnx2fc_offload_session(port, tgt, rdata);
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 		BNX2FC_TGT_DBG(tgt, "OFFLOAD num_ofld_sess = %d\n",
505*4882a593Smuzhiyun 			hba->num_ofld_sess);
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 		if (test_bit(BNX2FC_FLAG_ENABLED, &tgt->flags)) {
508*4882a593Smuzhiyun 			/* Session is offloaded and enabled.  */
509*4882a593Smuzhiyun 			BNX2FC_TGT_DBG(tgt, "sess offloaded\n");
510*4882a593Smuzhiyun 			/* This counter is protected with hba mutex */
511*4882a593Smuzhiyun 			hba->num_ofld_sess++;
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 			set_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
514*4882a593Smuzhiyun 		} else {
515*4882a593Smuzhiyun 			/*
516*4882a593Smuzhiyun 			 * Offload or enable would have failed.
517*4882a593Smuzhiyun 			 * In offload/enable completion path, the
518*4882a593Smuzhiyun 			 * rport would have already been removed
519*4882a593Smuzhiyun 			 */
520*4882a593Smuzhiyun 			BNX2FC_TGT_DBG(tgt, "Port is being logged off as "
521*4882a593Smuzhiyun 				   "offloaded flag not set\n");
522*4882a593Smuzhiyun 		}
523*4882a593Smuzhiyun 		mutex_unlock(&hba->hba_mutex);
524*4882a593Smuzhiyun 		break;
525*4882a593Smuzhiyun 	case RPORT_EV_LOGO:
526*4882a593Smuzhiyun 	case RPORT_EV_FAILED:
527*4882a593Smuzhiyun 	case RPORT_EV_STOP:
528*4882a593Smuzhiyun 		port_id = rdata->ids.port_id;
529*4882a593Smuzhiyun 		if (port_id == FC_FID_DIR_SERV)
530*4882a593Smuzhiyun 			break;
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 		if (!rport) {
533*4882a593Smuzhiyun 			printk(KERN_INFO PFX "%x - rport not created Yet!!\n",
534*4882a593Smuzhiyun 				port_id);
535*4882a593Smuzhiyun 			break;
536*4882a593Smuzhiyun 		}
537*4882a593Smuzhiyun 		rp = rport->dd_data;
538*4882a593Smuzhiyun 		mutex_lock(&hba->hba_mutex);
539*4882a593Smuzhiyun 		/*
540*4882a593Smuzhiyun 		 * Perform session upload. Note that rdata->peers is already
541*4882a593Smuzhiyun 		 * removed from disc->rports list before we get this event.
542*4882a593Smuzhiyun 		 */
543*4882a593Smuzhiyun 		tgt = (struct bnx2fc_rport *)&rp[1];
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 		if (!(test_bit(BNX2FC_FLAG_ENABLED, &tgt->flags))) {
546*4882a593Smuzhiyun 			mutex_unlock(&hba->hba_mutex);
547*4882a593Smuzhiyun 			break;
548*4882a593Smuzhiyun 		}
549*4882a593Smuzhiyun 		clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 		bnx2fc_upload_session(port, tgt);
552*4882a593Smuzhiyun 		hba->num_ofld_sess--;
553*4882a593Smuzhiyun 		BNX2FC_TGT_DBG(tgt, "UPLOAD num_ofld_sess = %d\n",
554*4882a593Smuzhiyun 			hba->num_ofld_sess);
555*4882a593Smuzhiyun 		/*
556*4882a593Smuzhiyun 		 * Try to wake up the linkdown wait thread. If num_ofld_sess
557*4882a593Smuzhiyun 		 * is 0, the waiting therad wakes up
558*4882a593Smuzhiyun 		 */
559*4882a593Smuzhiyun 		if ((hba->wait_for_link_down) &&
560*4882a593Smuzhiyun 		    (hba->num_ofld_sess == 0)) {
561*4882a593Smuzhiyun 			wake_up_interruptible(&hba->shutdown_wait);
562*4882a593Smuzhiyun 		}
563*4882a593Smuzhiyun 		mutex_unlock(&hba->hba_mutex);
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 		break;
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 	case RPORT_EV_NONE:
568*4882a593Smuzhiyun 		break;
569*4882a593Smuzhiyun 	}
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /**
573*4882a593Smuzhiyun  * bnx2fc_tgt_lookup() - Lookup a bnx2fc_rport by port_id
574*4882a593Smuzhiyun  *
575*4882a593Smuzhiyun  * @port:  fcoe_port struct to lookup the target port on
576*4882a593Smuzhiyun  * @port_id: The remote port ID to look up
577*4882a593Smuzhiyun  */
bnx2fc_tgt_lookup(struct fcoe_port * port,u32 port_id)578*4882a593Smuzhiyun struct bnx2fc_rport *bnx2fc_tgt_lookup(struct fcoe_port *port,
579*4882a593Smuzhiyun 					     u32 port_id)
580*4882a593Smuzhiyun {
581*4882a593Smuzhiyun 	struct bnx2fc_interface *interface = port->priv;
582*4882a593Smuzhiyun 	struct bnx2fc_hba *hba = interface->hba;
583*4882a593Smuzhiyun 	struct bnx2fc_rport *tgt;
584*4882a593Smuzhiyun 	struct fc_rport_priv *rdata;
585*4882a593Smuzhiyun 	int i;
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun 	for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
588*4882a593Smuzhiyun 		tgt = hba->tgt_ofld_list[i];
589*4882a593Smuzhiyun 		if ((tgt) && (tgt->port == port)) {
590*4882a593Smuzhiyun 			rdata = tgt->rdata;
591*4882a593Smuzhiyun 			if (rdata->ids.port_id == port_id) {
592*4882a593Smuzhiyun 				if (rdata->rp_state != RPORT_ST_DELETE) {
593*4882a593Smuzhiyun 					BNX2FC_TGT_DBG(tgt, "rport "
594*4882a593Smuzhiyun 						"obtained\n");
595*4882a593Smuzhiyun 					return tgt;
596*4882a593Smuzhiyun 				} else {
597*4882a593Smuzhiyun 					BNX2FC_TGT_DBG(tgt, "rport 0x%x "
598*4882a593Smuzhiyun 						"is in DELETED state\n",
599*4882a593Smuzhiyun 						rdata->ids.port_id);
600*4882a593Smuzhiyun 					return NULL;
601*4882a593Smuzhiyun 				}
602*4882a593Smuzhiyun 			}
603*4882a593Smuzhiyun 		}
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 	return NULL;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun /**
610*4882a593Smuzhiyun  * bnx2fc_alloc_conn_id - allocates FCOE Connection id
611*4882a593Smuzhiyun  *
612*4882a593Smuzhiyun  * @hba:	pointer to adapter structure
613*4882a593Smuzhiyun  * @tgt:	pointer to bnx2fc_rport structure
614*4882a593Smuzhiyun  */
bnx2fc_alloc_conn_id(struct bnx2fc_hba * hba,struct bnx2fc_rport * tgt)615*4882a593Smuzhiyun static u32 bnx2fc_alloc_conn_id(struct bnx2fc_hba *hba,
616*4882a593Smuzhiyun 				struct bnx2fc_rport *tgt)
617*4882a593Smuzhiyun {
618*4882a593Smuzhiyun 	u32 conn_id, next;
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	/* called with hba mutex held */
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	/*
623*4882a593Smuzhiyun 	 * tgt_ofld_list access is synchronized using
624*4882a593Smuzhiyun 	 * both hba mutex and hba lock. Atleast hba mutex or
625*4882a593Smuzhiyun 	 * hba lock needs to be held for read access.
626*4882a593Smuzhiyun 	 */
627*4882a593Smuzhiyun 
628*4882a593Smuzhiyun 	spin_lock_bh(&hba->hba_lock);
629*4882a593Smuzhiyun 	next = hba->next_conn_id;
630*4882a593Smuzhiyun 	conn_id = hba->next_conn_id++;
631*4882a593Smuzhiyun 	if (hba->next_conn_id == BNX2FC_NUM_MAX_SESS)
632*4882a593Smuzhiyun 		hba->next_conn_id = 0;
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 	while (hba->tgt_ofld_list[conn_id] != NULL) {
635*4882a593Smuzhiyun 		conn_id++;
636*4882a593Smuzhiyun 		if (conn_id == BNX2FC_NUM_MAX_SESS)
637*4882a593Smuzhiyun 			conn_id = 0;
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 		if (conn_id == next) {
640*4882a593Smuzhiyun 			/* No free conn_ids are available */
641*4882a593Smuzhiyun 			spin_unlock_bh(&hba->hba_lock);
642*4882a593Smuzhiyun 			return -1;
643*4882a593Smuzhiyun 		}
644*4882a593Smuzhiyun 	}
645*4882a593Smuzhiyun 	hba->tgt_ofld_list[conn_id] = tgt;
646*4882a593Smuzhiyun 	tgt->fcoe_conn_id = conn_id;
647*4882a593Smuzhiyun 	spin_unlock_bh(&hba->hba_lock);
648*4882a593Smuzhiyun 	return conn_id;
649*4882a593Smuzhiyun }
650*4882a593Smuzhiyun 
bnx2fc_free_conn_id(struct bnx2fc_hba * hba,u32 conn_id)651*4882a593Smuzhiyun static void bnx2fc_free_conn_id(struct bnx2fc_hba *hba, u32 conn_id)
652*4882a593Smuzhiyun {
653*4882a593Smuzhiyun 	/* called with hba mutex held */
654*4882a593Smuzhiyun 	spin_lock_bh(&hba->hba_lock);
655*4882a593Smuzhiyun 	hba->tgt_ofld_list[conn_id] = NULL;
656*4882a593Smuzhiyun 	spin_unlock_bh(&hba->hba_lock);
657*4882a593Smuzhiyun }
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun /*
660*4882a593Smuzhiyun  * bnx2fc_alloc_session_resc - Allocate qp resources for the session
661*4882a593Smuzhiyun  */
bnx2fc_alloc_session_resc(struct bnx2fc_hba * hba,struct bnx2fc_rport * tgt)662*4882a593Smuzhiyun static int bnx2fc_alloc_session_resc(struct bnx2fc_hba *hba,
663*4882a593Smuzhiyun 					struct bnx2fc_rport *tgt)
664*4882a593Smuzhiyun {
665*4882a593Smuzhiyun 	dma_addr_t page;
666*4882a593Smuzhiyun 	int num_pages;
667*4882a593Smuzhiyun 	u32 *pbl;
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun 	/* Allocate and map SQ */
670*4882a593Smuzhiyun 	tgt->sq_mem_size = tgt->max_sqes * BNX2FC_SQ_WQE_SIZE;
671*4882a593Smuzhiyun 	tgt->sq_mem_size = (tgt->sq_mem_size + (CNIC_PAGE_SIZE - 1)) &
672*4882a593Smuzhiyun 			   CNIC_PAGE_MASK;
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun 	tgt->sq = dma_alloc_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
675*4882a593Smuzhiyun 				     &tgt->sq_dma, GFP_KERNEL);
676*4882a593Smuzhiyun 	if (!tgt->sq) {
677*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate SQ memory %d\n",
678*4882a593Smuzhiyun 			tgt->sq_mem_size);
679*4882a593Smuzhiyun 		goto mem_alloc_failure;
680*4882a593Smuzhiyun 	}
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	/* Allocate and map CQ */
683*4882a593Smuzhiyun 	tgt->cq_mem_size = tgt->max_cqes * BNX2FC_CQ_WQE_SIZE;
684*4882a593Smuzhiyun 	tgt->cq_mem_size = (tgt->cq_mem_size + (CNIC_PAGE_SIZE - 1)) &
685*4882a593Smuzhiyun 			   CNIC_PAGE_MASK;
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	tgt->cq = dma_alloc_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
688*4882a593Smuzhiyun 				     &tgt->cq_dma, GFP_KERNEL);
689*4882a593Smuzhiyun 	if (!tgt->cq) {
690*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate CQ memory %d\n",
691*4882a593Smuzhiyun 			tgt->cq_mem_size);
692*4882a593Smuzhiyun 		goto mem_alloc_failure;
693*4882a593Smuzhiyun 	}
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	/* Allocate and map RQ and RQ PBL */
696*4882a593Smuzhiyun 	tgt->rq_mem_size = tgt->max_rqes * BNX2FC_RQ_WQE_SIZE;
697*4882a593Smuzhiyun 	tgt->rq_mem_size = (tgt->rq_mem_size + (CNIC_PAGE_SIZE - 1)) &
698*4882a593Smuzhiyun 			   CNIC_PAGE_MASK;
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	tgt->rq = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
701*4882a593Smuzhiyun 				     &tgt->rq_dma, GFP_KERNEL);
702*4882a593Smuzhiyun 	if (!tgt->rq) {
703*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate RQ memory %d\n",
704*4882a593Smuzhiyun 			tgt->rq_mem_size);
705*4882a593Smuzhiyun 		goto mem_alloc_failure;
706*4882a593Smuzhiyun 	}
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	tgt->rq_pbl_size = (tgt->rq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
709*4882a593Smuzhiyun 	tgt->rq_pbl_size = (tgt->rq_pbl_size + (CNIC_PAGE_SIZE - 1)) &
710*4882a593Smuzhiyun 			   CNIC_PAGE_MASK;
711*4882a593Smuzhiyun 
712*4882a593Smuzhiyun 	tgt->rq_pbl = dma_alloc_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
713*4882a593Smuzhiyun 					 &tgt->rq_pbl_dma, GFP_KERNEL);
714*4882a593Smuzhiyun 	if (!tgt->rq_pbl) {
715*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate RQ PBL %d\n",
716*4882a593Smuzhiyun 			tgt->rq_pbl_size);
717*4882a593Smuzhiyun 		goto mem_alloc_failure;
718*4882a593Smuzhiyun 	}
719*4882a593Smuzhiyun 
720*4882a593Smuzhiyun 	num_pages = tgt->rq_mem_size / CNIC_PAGE_SIZE;
721*4882a593Smuzhiyun 	page = tgt->rq_dma;
722*4882a593Smuzhiyun 	pbl = (u32 *)tgt->rq_pbl;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	while (num_pages--) {
725*4882a593Smuzhiyun 		*pbl = (u32)page;
726*4882a593Smuzhiyun 		pbl++;
727*4882a593Smuzhiyun 		*pbl = (u32)((u64)page >> 32);
728*4882a593Smuzhiyun 		pbl++;
729*4882a593Smuzhiyun 		page += CNIC_PAGE_SIZE;
730*4882a593Smuzhiyun 	}
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	/* Allocate and map XFERQ */
733*4882a593Smuzhiyun 	tgt->xferq_mem_size = tgt->max_sqes * BNX2FC_XFERQ_WQE_SIZE;
734*4882a593Smuzhiyun 	tgt->xferq_mem_size = (tgt->xferq_mem_size + (CNIC_PAGE_SIZE - 1)) &
735*4882a593Smuzhiyun 			       CNIC_PAGE_MASK;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	tgt->xferq = dma_alloc_coherent(&hba->pcidev->dev,
738*4882a593Smuzhiyun 					tgt->xferq_mem_size, &tgt->xferq_dma,
739*4882a593Smuzhiyun 					GFP_KERNEL);
740*4882a593Smuzhiyun 	if (!tgt->xferq) {
741*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate XFERQ %d\n",
742*4882a593Smuzhiyun 			tgt->xferq_mem_size);
743*4882a593Smuzhiyun 		goto mem_alloc_failure;
744*4882a593Smuzhiyun 	}
745*4882a593Smuzhiyun 
746*4882a593Smuzhiyun 	/* Allocate and map CONFQ & CONFQ PBL */
747*4882a593Smuzhiyun 	tgt->confq_mem_size = tgt->max_sqes * BNX2FC_CONFQ_WQE_SIZE;
748*4882a593Smuzhiyun 	tgt->confq_mem_size = (tgt->confq_mem_size + (CNIC_PAGE_SIZE - 1)) &
749*4882a593Smuzhiyun 			       CNIC_PAGE_MASK;
750*4882a593Smuzhiyun 
751*4882a593Smuzhiyun 	tgt->confq = dma_alloc_coherent(&hba->pcidev->dev,
752*4882a593Smuzhiyun 					tgt->confq_mem_size, &tgt->confq_dma,
753*4882a593Smuzhiyun 					GFP_KERNEL);
754*4882a593Smuzhiyun 	if (!tgt->confq) {
755*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate CONFQ %d\n",
756*4882a593Smuzhiyun 			tgt->confq_mem_size);
757*4882a593Smuzhiyun 		goto mem_alloc_failure;
758*4882a593Smuzhiyun 	}
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	tgt->confq_pbl_size =
761*4882a593Smuzhiyun 		(tgt->confq_mem_size / CNIC_PAGE_SIZE) * sizeof(void *);
762*4882a593Smuzhiyun 	tgt->confq_pbl_size =
763*4882a593Smuzhiyun 		(tgt->confq_pbl_size + (CNIC_PAGE_SIZE - 1)) & CNIC_PAGE_MASK;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	tgt->confq_pbl = dma_alloc_coherent(&hba->pcidev->dev,
766*4882a593Smuzhiyun 					    tgt->confq_pbl_size,
767*4882a593Smuzhiyun 					    &tgt->confq_pbl_dma, GFP_KERNEL);
768*4882a593Smuzhiyun 	if (!tgt->confq_pbl) {
769*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate CONFQ PBL %d\n",
770*4882a593Smuzhiyun 			tgt->confq_pbl_size);
771*4882a593Smuzhiyun 		goto mem_alloc_failure;
772*4882a593Smuzhiyun 	}
773*4882a593Smuzhiyun 
774*4882a593Smuzhiyun 	num_pages = tgt->confq_mem_size / CNIC_PAGE_SIZE;
775*4882a593Smuzhiyun 	page = tgt->confq_dma;
776*4882a593Smuzhiyun 	pbl = (u32 *)tgt->confq_pbl;
777*4882a593Smuzhiyun 
778*4882a593Smuzhiyun 	while (num_pages--) {
779*4882a593Smuzhiyun 		*pbl = (u32)page;
780*4882a593Smuzhiyun 		pbl++;
781*4882a593Smuzhiyun 		*pbl = (u32)((u64)page >> 32);
782*4882a593Smuzhiyun 		pbl++;
783*4882a593Smuzhiyun 		page += CNIC_PAGE_SIZE;
784*4882a593Smuzhiyun 	}
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	/* Allocate and map ConnDB */
787*4882a593Smuzhiyun 	tgt->conn_db_mem_size = sizeof(struct fcoe_conn_db);
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	tgt->conn_db = dma_alloc_coherent(&hba->pcidev->dev,
790*4882a593Smuzhiyun 					  tgt->conn_db_mem_size,
791*4882a593Smuzhiyun 					  &tgt->conn_db_dma, GFP_KERNEL);
792*4882a593Smuzhiyun 	if (!tgt->conn_db) {
793*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate conn_db %d\n",
794*4882a593Smuzhiyun 						tgt->conn_db_mem_size);
795*4882a593Smuzhiyun 		goto mem_alloc_failure;
796*4882a593Smuzhiyun 	}
797*4882a593Smuzhiyun 
798*4882a593Smuzhiyun 
799*4882a593Smuzhiyun 	/* Allocate and map LCQ */
800*4882a593Smuzhiyun 	tgt->lcq_mem_size = (tgt->max_sqes + 8) * BNX2FC_SQ_WQE_SIZE;
801*4882a593Smuzhiyun 	tgt->lcq_mem_size = (tgt->lcq_mem_size + (CNIC_PAGE_SIZE - 1)) &
802*4882a593Smuzhiyun 			     CNIC_PAGE_MASK;
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	tgt->lcq = dma_alloc_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
805*4882a593Smuzhiyun 				      &tgt->lcq_dma, GFP_KERNEL);
806*4882a593Smuzhiyun 
807*4882a593Smuzhiyun 	if (!tgt->lcq) {
808*4882a593Smuzhiyun 		printk(KERN_ERR PFX "unable to allocate lcq %d\n",
809*4882a593Smuzhiyun 		       tgt->lcq_mem_size);
810*4882a593Smuzhiyun 		goto mem_alloc_failure;
811*4882a593Smuzhiyun 	}
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 	tgt->conn_db->rq_prod = 0x8000;
814*4882a593Smuzhiyun 
815*4882a593Smuzhiyun 	return 0;
816*4882a593Smuzhiyun 
817*4882a593Smuzhiyun mem_alloc_failure:
818*4882a593Smuzhiyun 	return -ENOMEM;
819*4882a593Smuzhiyun }
820*4882a593Smuzhiyun 
821*4882a593Smuzhiyun /**
822*4882a593Smuzhiyun  * bnx2i_free_session_resc - free qp resources for the session
823*4882a593Smuzhiyun  *
824*4882a593Smuzhiyun  * @hba:	adapter structure pointer
825*4882a593Smuzhiyun  * @tgt:	bnx2fc_rport structure pointer
826*4882a593Smuzhiyun  *
827*4882a593Smuzhiyun  * Free QP resources - SQ/RQ/CQ/XFERQ memory and PBL
828*4882a593Smuzhiyun  */
bnx2fc_free_session_resc(struct bnx2fc_hba * hba,struct bnx2fc_rport * tgt)829*4882a593Smuzhiyun static void bnx2fc_free_session_resc(struct bnx2fc_hba *hba,
830*4882a593Smuzhiyun 						struct bnx2fc_rport *tgt)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun 	void __iomem *ctx_base_ptr;
833*4882a593Smuzhiyun 
834*4882a593Smuzhiyun 	BNX2FC_TGT_DBG(tgt, "Freeing up session resources\n");
835*4882a593Smuzhiyun 
836*4882a593Smuzhiyun 	spin_lock_bh(&tgt->cq_lock);
837*4882a593Smuzhiyun 	ctx_base_ptr = tgt->ctx_base;
838*4882a593Smuzhiyun 	tgt->ctx_base = NULL;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	/* Free LCQ */
841*4882a593Smuzhiyun 	if (tgt->lcq) {
842*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->lcq_mem_size,
843*4882a593Smuzhiyun 				    tgt->lcq, tgt->lcq_dma);
844*4882a593Smuzhiyun 		tgt->lcq = NULL;
845*4882a593Smuzhiyun 	}
846*4882a593Smuzhiyun 	/* Free connDB */
847*4882a593Smuzhiyun 	if (tgt->conn_db) {
848*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->conn_db_mem_size,
849*4882a593Smuzhiyun 				    tgt->conn_db, tgt->conn_db_dma);
850*4882a593Smuzhiyun 		tgt->conn_db = NULL;
851*4882a593Smuzhiyun 	}
852*4882a593Smuzhiyun 	/* Free confq  and confq pbl */
853*4882a593Smuzhiyun 	if (tgt->confq_pbl) {
854*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->confq_pbl_size,
855*4882a593Smuzhiyun 				    tgt->confq_pbl, tgt->confq_pbl_dma);
856*4882a593Smuzhiyun 		tgt->confq_pbl = NULL;
857*4882a593Smuzhiyun 	}
858*4882a593Smuzhiyun 	if (tgt->confq) {
859*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->confq_mem_size,
860*4882a593Smuzhiyun 				    tgt->confq, tgt->confq_dma);
861*4882a593Smuzhiyun 		tgt->confq = NULL;
862*4882a593Smuzhiyun 	}
863*4882a593Smuzhiyun 	/* Free XFERQ */
864*4882a593Smuzhiyun 	if (tgt->xferq) {
865*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->xferq_mem_size,
866*4882a593Smuzhiyun 				    tgt->xferq, tgt->xferq_dma);
867*4882a593Smuzhiyun 		tgt->xferq = NULL;
868*4882a593Smuzhiyun 	}
869*4882a593Smuzhiyun 	/* Free RQ PBL and RQ */
870*4882a593Smuzhiyun 	if (tgt->rq_pbl) {
871*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->rq_pbl_size,
872*4882a593Smuzhiyun 				    tgt->rq_pbl, tgt->rq_pbl_dma);
873*4882a593Smuzhiyun 		tgt->rq_pbl = NULL;
874*4882a593Smuzhiyun 	}
875*4882a593Smuzhiyun 	if (tgt->rq) {
876*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->rq_mem_size,
877*4882a593Smuzhiyun 				    tgt->rq, tgt->rq_dma);
878*4882a593Smuzhiyun 		tgt->rq = NULL;
879*4882a593Smuzhiyun 	}
880*4882a593Smuzhiyun 	/* Free CQ */
881*4882a593Smuzhiyun 	if (tgt->cq) {
882*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->cq_mem_size,
883*4882a593Smuzhiyun 				    tgt->cq, tgt->cq_dma);
884*4882a593Smuzhiyun 		tgt->cq = NULL;
885*4882a593Smuzhiyun 	}
886*4882a593Smuzhiyun 	/* Free SQ */
887*4882a593Smuzhiyun 	if (tgt->sq) {
888*4882a593Smuzhiyun 		dma_free_coherent(&hba->pcidev->dev, tgt->sq_mem_size,
889*4882a593Smuzhiyun 				    tgt->sq, tgt->sq_dma);
890*4882a593Smuzhiyun 		tgt->sq = NULL;
891*4882a593Smuzhiyun 	}
892*4882a593Smuzhiyun 	spin_unlock_bh(&tgt->cq_lock);
893*4882a593Smuzhiyun 
894*4882a593Smuzhiyun 	if (ctx_base_ptr)
895*4882a593Smuzhiyun 		iounmap(ctx_base_ptr);
896*4882a593Smuzhiyun }
897