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