xref: /linux/drivers/scsi/bnx2fc/bnx2fc_io.c (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 /* bnx2fc_io.c: Broadcom NetXtreme II Linux FCoE offload driver.
2  * IO manager and SCSI IO processing.
3  *
4  * Copyright (c) 2008 - 2013 Broadcom Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation.
9  *
10  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
11  */
12 
13 #include "bnx2fc.h"
14 
15 #define RESERVE_FREE_LIST_INDEX num_possible_cpus()
16 
17 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
18 			   int bd_index);
19 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
20 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
21 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
22 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
23 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
24 				 struct fcoe_fcp_rsp_payload *fcp_rsp,
25 				 u8 num_rq);
26 
27 void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
28 			  unsigned int timer_msec)
29 {
30 	struct bnx2fc_interface *interface = io_req->port->priv;
31 
32 	if (queue_delayed_work(interface->timer_work_queue,
33 			       &io_req->timeout_work,
34 			       msecs_to_jiffies(timer_msec)))
35 		kref_get(&io_req->refcount);
36 }
37 
38 static void bnx2fc_cmd_timeout(struct work_struct *work)
39 {
40 	struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
41 						 timeout_work.work);
42 	struct fc_lport *lport;
43 	struct fc_rport_priv *rdata;
44 	u8 cmd_type = io_req->cmd_type;
45 	struct bnx2fc_rport *tgt = io_req->tgt;
46 	int logo_issued;
47 	int rc;
48 
49 	BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
50 		      "req_flags = %lx\n", cmd_type, io_req->req_flags);
51 
52 	spin_lock_bh(&tgt->tgt_lock);
53 	if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
54 		clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
55 		/*
56 		 * ideally we should hold the io_req until RRQ complets,
57 		 * and release io_req from timeout hold.
58 		 */
59 		spin_unlock_bh(&tgt->tgt_lock);
60 		bnx2fc_send_rrq(io_req);
61 		return;
62 	}
63 	if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
64 		BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
65 		goto done;
66 	}
67 
68 	switch (cmd_type) {
69 	case BNX2FC_SCSI_CMD:
70 		if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
71 							&io_req->req_flags)) {
72 			/* Handle eh_abort timeout */
73 			BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
74 			complete(&io_req->tm_done);
75 		} else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
76 				    &io_req->req_flags)) {
77 			/* Handle internally generated ABTS timeout */
78 			BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
79 					io_req->refcount.refcount.counter);
80 			if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
81 					       &io_req->req_flags))) {
82 
83 				lport = io_req->port->lport;
84 				rdata = io_req->tgt->rdata;
85 				logo_issued = test_and_set_bit(
86 						BNX2FC_FLAG_EXPL_LOGO,
87 						&tgt->flags);
88 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
89 				spin_unlock_bh(&tgt->tgt_lock);
90 
91 				/* Explicitly logo the target */
92 				if (!logo_issued) {
93 					BNX2FC_IO_DBG(io_req, "Explicit "
94 						   "logo - tgt flags = 0x%lx\n",
95 						   tgt->flags);
96 
97 					mutex_lock(&lport->disc.disc_mutex);
98 					lport->tt.rport_logoff(rdata);
99 					mutex_unlock(&lport->disc.disc_mutex);
100 				}
101 				return;
102 			}
103 		} else {
104 			/* Hanlde IO timeout */
105 			BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
106 			if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
107 					     &io_req->req_flags)) {
108 				BNX2FC_IO_DBG(io_req, "IO completed before "
109 							   " timer expiry\n");
110 				goto done;
111 			}
112 
113 			if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
114 					      &io_req->req_flags)) {
115 				rc = bnx2fc_initiate_abts(io_req);
116 				if (rc == SUCCESS)
117 					goto done;
118 				/*
119 				 * Explicitly logo the target if
120 				 * abts initiation fails
121 				 */
122 				lport = io_req->port->lport;
123 				rdata = io_req->tgt->rdata;
124 				logo_issued = test_and_set_bit(
125 						BNX2FC_FLAG_EXPL_LOGO,
126 						&tgt->flags);
127 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
128 				spin_unlock_bh(&tgt->tgt_lock);
129 
130 				if (!logo_issued) {
131 					BNX2FC_IO_DBG(io_req, "Explicit "
132 						   "logo - tgt flags = 0x%lx\n",
133 						   tgt->flags);
134 
135 
136 					mutex_lock(&lport->disc.disc_mutex);
137 					lport->tt.rport_logoff(rdata);
138 					mutex_unlock(&lport->disc.disc_mutex);
139 				}
140 				return;
141 			} else {
142 				BNX2FC_IO_DBG(io_req, "IO already in "
143 						      "ABTS processing\n");
144 			}
145 		}
146 		break;
147 	case BNX2FC_ELS:
148 
149 		if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
150 			BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
151 
152 			if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
153 					      &io_req->req_flags)) {
154 				lport = io_req->port->lport;
155 				rdata = io_req->tgt->rdata;
156 				logo_issued = test_and_set_bit(
157 						BNX2FC_FLAG_EXPL_LOGO,
158 						&tgt->flags);
159 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
160 				spin_unlock_bh(&tgt->tgt_lock);
161 
162 				/* Explicitly logo the target */
163 				if (!logo_issued) {
164 					BNX2FC_IO_DBG(io_req, "Explicitly logo"
165 						   "(els)\n");
166 					mutex_lock(&lport->disc.disc_mutex);
167 					lport->tt.rport_logoff(rdata);
168 					mutex_unlock(&lport->disc.disc_mutex);
169 				}
170 				return;
171 			}
172 		} else {
173 			/*
174 			 * Handle ELS timeout.
175 			 * tgt_lock is used to sync compl path and timeout
176 			 * path. If els compl path is processing this IO, we
177 			 * have nothing to do here, just release the timer hold
178 			 */
179 			BNX2FC_IO_DBG(io_req, "ELS timed out\n");
180 			if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
181 					       &io_req->req_flags))
182 				goto done;
183 
184 			/* Indicate the cb_func that this ELS is timed out */
185 			set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
186 
187 			if ((io_req->cb_func) && (io_req->cb_arg)) {
188 				io_req->cb_func(io_req->cb_arg);
189 				io_req->cb_arg = NULL;
190 			}
191 		}
192 		break;
193 	default:
194 		printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
195 			cmd_type);
196 		break;
197 	}
198 
199 done:
200 	/* release the cmd that was held when timer was set */
201 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
202 	spin_unlock_bh(&tgt->tgt_lock);
203 }
204 
205 static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
206 {
207 	/* Called with host lock held */
208 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
209 
210 	/*
211 	 * active_cmd_queue may have other command types as well,
212 	 * and during flush operation,  we want to error back only
213 	 * scsi commands.
214 	 */
215 	if (io_req->cmd_type != BNX2FC_SCSI_CMD)
216 		return;
217 
218 	BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
219 	if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
220 		/* Do not call scsi done for this IO */
221 		return;
222 	}
223 
224 	bnx2fc_unmap_sg_list(io_req);
225 	io_req->sc_cmd = NULL;
226 	if (!sc_cmd) {
227 		printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
228 				    "IO(0x%x) already cleaned up\n",
229 		       io_req->xid);
230 		return;
231 	}
232 	sc_cmd->result = err_code << 16;
233 
234 	BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
235 		sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
236 		sc_cmd->allowed);
237 	scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
238 	sc_cmd->SCp.ptr = NULL;
239 	sc_cmd->scsi_done(sc_cmd);
240 }
241 
242 struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
243 {
244 	struct bnx2fc_cmd_mgr *cmgr;
245 	struct io_bdt *bdt_info;
246 	struct bnx2fc_cmd *io_req;
247 	size_t len;
248 	u32 mem_size;
249 	u16 xid;
250 	int i;
251 	int num_ios, num_pri_ios;
252 	size_t bd_tbl_sz;
253 	int arr_sz = num_possible_cpus() + 1;
254 	u16 min_xid = BNX2FC_MIN_XID;
255 	u16 max_xid = hba->max_xid;
256 
257 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
258 		printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
259 					and max_xid 0x%x\n", min_xid, max_xid);
260 		return NULL;
261 	}
262 	BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
263 
264 	num_ios = max_xid - min_xid + 1;
265 	len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
266 	len += sizeof(struct bnx2fc_cmd_mgr);
267 
268 	cmgr = kzalloc(len, GFP_KERNEL);
269 	if (!cmgr) {
270 		printk(KERN_ERR PFX "failed to alloc cmgr\n");
271 		return NULL;
272 	}
273 
274 	cmgr->free_list = kzalloc(sizeof(*cmgr->free_list) *
275 				  arr_sz, GFP_KERNEL);
276 	if (!cmgr->free_list) {
277 		printk(KERN_ERR PFX "failed to alloc free_list\n");
278 		goto mem_err;
279 	}
280 
281 	cmgr->free_list_lock = kzalloc(sizeof(*cmgr->free_list_lock) *
282 				       arr_sz, GFP_KERNEL);
283 	if (!cmgr->free_list_lock) {
284 		printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
285 		goto mem_err;
286 	}
287 
288 	cmgr->hba = hba;
289 	cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
290 
291 	for (i = 0; i < arr_sz; i++)  {
292 		INIT_LIST_HEAD(&cmgr->free_list[i]);
293 		spin_lock_init(&cmgr->free_list_lock[i]);
294 	}
295 
296 	/*
297 	 * Pre-allocated pool of bnx2fc_cmds.
298 	 * Last entry in the free list array is the free list
299 	 * of slow path requests.
300 	 */
301 	xid = BNX2FC_MIN_XID;
302 	num_pri_ios = num_ios - hba->elstm_xids;
303 	for (i = 0; i < num_ios; i++) {
304 		io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
305 
306 		if (!io_req) {
307 			printk(KERN_ERR PFX "failed to alloc io_req\n");
308 			goto mem_err;
309 		}
310 
311 		INIT_LIST_HEAD(&io_req->link);
312 		INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
313 
314 		io_req->xid = xid++;
315 		if (i < num_pri_ios)
316 			list_add_tail(&io_req->link,
317 				&cmgr->free_list[io_req->xid %
318 						 num_possible_cpus()]);
319 		else
320 			list_add_tail(&io_req->link,
321 				&cmgr->free_list[num_possible_cpus()]);
322 		io_req++;
323 	}
324 
325 	/* Allocate pool of io_bdts - one for each bnx2fc_cmd */
326 	mem_size = num_ios * sizeof(struct io_bdt *);
327 	cmgr->io_bdt_pool = kmalloc(mem_size, GFP_KERNEL);
328 	if (!cmgr->io_bdt_pool) {
329 		printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
330 		goto mem_err;
331 	}
332 
333 	mem_size = sizeof(struct io_bdt);
334 	for (i = 0; i < num_ios; i++) {
335 		cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
336 		if (!cmgr->io_bdt_pool[i]) {
337 			printk(KERN_ERR PFX "failed to alloc "
338 				"io_bdt_pool[%d]\n", i);
339 			goto mem_err;
340 		}
341 	}
342 
343 	/* Allocate an map fcoe_bdt_ctx structures */
344 	bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
345 	for (i = 0; i < num_ios; i++) {
346 		bdt_info = cmgr->io_bdt_pool[i];
347 		bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
348 						      bd_tbl_sz,
349 						      &bdt_info->bd_tbl_dma,
350 						      GFP_KERNEL);
351 		if (!bdt_info->bd_tbl) {
352 			printk(KERN_ERR PFX "failed to alloc "
353 				"bdt_tbl[%d]\n", i);
354 			goto mem_err;
355 		}
356 	}
357 
358 	return cmgr;
359 
360 mem_err:
361 	bnx2fc_cmd_mgr_free(cmgr);
362 	return NULL;
363 }
364 
365 void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
366 {
367 	struct io_bdt *bdt_info;
368 	struct bnx2fc_hba *hba = cmgr->hba;
369 	size_t bd_tbl_sz;
370 	u16 min_xid = BNX2FC_MIN_XID;
371 	u16 max_xid = hba->max_xid;
372 	int num_ios;
373 	int i;
374 
375 	num_ios = max_xid - min_xid + 1;
376 
377 	/* Free fcoe_bdt_ctx structures */
378 	if (!cmgr->io_bdt_pool)
379 		goto free_cmd_pool;
380 
381 	bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
382 	for (i = 0; i < num_ios; i++) {
383 		bdt_info = cmgr->io_bdt_pool[i];
384 		if (bdt_info->bd_tbl) {
385 			dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
386 					    bdt_info->bd_tbl,
387 					    bdt_info->bd_tbl_dma);
388 			bdt_info->bd_tbl = NULL;
389 		}
390 	}
391 
392 	/* Destroy io_bdt pool */
393 	for (i = 0; i < num_ios; i++) {
394 		kfree(cmgr->io_bdt_pool[i]);
395 		cmgr->io_bdt_pool[i] = NULL;
396 	}
397 
398 	kfree(cmgr->io_bdt_pool);
399 	cmgr->io_bdt_pool = NULL;
400 
401 free_cmd_pool:
402 	kfree(cmgr->free_list_lock);
403 
404 	/* Destroy cmd pool */
405 	if (!cmgr->free_list)
406 		goto free_cmgr;
407 
408 	for (i = 0; i < num_possible_cpus() + 1; i++)  {
409 		struct bnx2fc_cmd *tmp, *io_req;
410 
411 		list_for_each_entry_safe(io_req, tmp,
412 					 &cmgr->free_list[i], link) {
413 			list_del(&io_req->link);
414 			kfree(io_req);
415 		}
416 	}
417 	kfree(cmgr->free_list);
418 free_cmgr:
419 	/* Free command manager itself */
420 	kfree(cmgr);
421 }
422 
423 struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
424 {
425 	struct fcoe_port *port = tgt->port;
426 	struct bnx2fc_interface *interface = port->priv;
427 	struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
428 	struct bnx2fc_cmd *io_req;
429 	struct list_head *listp;
430 	struct io_bdt *bd_tbl;
431 	int index = RESERVE_FREE_LIST_INDEX;
432 	u32 free_sqes;
433 	u32 max_sqes;
434 	u16 xid;
435 
436 	max_sqes = tgt->max_sqes;
437 	switch (type) {
438 	case BNX2FC_TASK_MGMT_CMD:
439 		max_sqes = BNX2FC_TM_MAX_SQES;
440 		break;
441 	case BNX2FC_ELS:
442 		max_sqes = BNX2FC_ELS_MAX_SQES;
443 		break;
444 	default:
445 		break;
446 	}
447 
448 	/*
449 	 * NOTE: Free list insertions and deletions are protected with
450 	 * cmgr lock
451 	 */
452 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
453 	free_sqes = atomic_read(&tgt->free_sqes);
454 	if ((list_empty(&(cmd_mgr->free_list[index]))) ||
455 	    (tgt->num_active_ios.counter  >= max_sqes) ||
456 	    (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
457 		BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
458 			"ios(%d):sqes(%d)\n",
459 			tgt->num_active_ios.counter, tgt->max_sqes);
460 		if (list_empty(&(cmd_mgr->free_list[index])))
461 			printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
462 		spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
463 		return NULL;
464 	}
465 
466 	listp = (struct list_head *)
467 			cmd_mgr->free_list[index].next;
468 	list_del_init(listp);
469 	io_req = (struct bnx2fc_cmd *) listp;
470 	xid = io_req->xid;
471 	cmd_mgr->cmds[xid] = io_req;
472 	atomic_inc(&tgt->num_active_ios);
473 	atomic_dec(&tgt->free_sqes);
474 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
475 
476 	INIT_LIST_HEAD(&io_req->link);
477 
478 	io_req->port = port;
479 	io_req->cmd_mgr = cmd_mgr;
480 	io_req->req_flags = 0;
481 	io_req->cmd_type = type;
482 
483 	/* Bind io_bdt for this io_req */
484 	/* Have a static link between io_req and io_bdt_pool */
485 	bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
486 	bd_tbl->io_req = io_req;
487 
488 	/* Hold the io_req  against deletion */
489 	kref_init(&io_req->refcount);
490 	return io_req;
491 }
492 
493 struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
494 {
495 	struct fcoe_port *port = tgt->port;
496 	struct bnx2fc_interface *interface = port->priv;
497 	struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
498 	struct bnx2fc_cmd *io_req;
499 	struct list_head *listp;
500 	struct io_bdt *bd_tbl;
501 	u32 free_sqes;
502 	u32 max_sqes;
503 	u16 xid;
504 	int index = get_cpu();
505 
506 	max_sqes = BNX2FC_SCSI_MAX_SQES;
507 	/*
508 	 * NOTE: Free list insertions and deletions are protected with
509 	 * cmgr lock
510 	 */
511 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
512 	free_sqes = atomic_read(&tgt->free_sqes);
513 	if ((list_empty(&cmd_mgr->free_list[index])) ||
514 	    (tgt->num_active_ios.counter  >= max_sqes) ||
515 	    (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
516 		spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
517 		put_cpu();
518 		return NULL;
519 	}
520 
521 	listp = (struct list_head *)
522 		cmd_mgr->free_list[index].next;
523 	list_del_init(listp);
524 	io_req = (struct bnx2fc_cmd *) listp;
525 	xid = io_req->xid;
526 	cmd_mgr->cmds[xid] = io_req;
527 	atomic_inc(&tgt->num_active_ios);
528 	atomic_dec(&tgt->free_sqes);
529 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
530 	put_cpu();
531 
532 	INIT_LIST_HEAD(&io_req->link);
533 
534 	io_req->port = port;
535 	io_req->cmd_mgr = cmd_mgr;
536 	io_req->req_flags = 0;
537 
538 	/* Bind io_bdt for this io_req */
539 	/* Have a static link between io_req and io_bdt_pool */
540 	bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
541 	bd_tbl->io_req = io_req;
542 
543 	/* Hold the io_req  against deletion */
544 	kref_init(&io_req->refcount);
545 	return io_req;
546 }
547 
548 void bnx2fc_cmd_release(struct kref *ref)
549 {
550 	struct bnx2fc_cmd *io_req = container_of(ref,
551 						struct bnx2fc_cmd, refcount);
552 	struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
553 	int index;
554 
555 	if (io_req->cmd_type == BNX2FC_SCSI_CMD)
556 		index = io_req->xid % num_possible_cpus();
557 	else
558 		index = RESERVE_FREE_LIST_INDEX;
559 
560 
561 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
562 	if (io_req->cmd_type != BNX2FC_SCSI_CMD)
563 		bnx2fc_free_mp_resc(io_req);
564 	cmd_mgr->cmds[io_req->xid] = NULL;
565 	/* Delete IO from retire queue */
566 	list_del_init(&io_req->link);
567 	/* Add it to the free list */
568 	list_add(&io_req->link,
569 			&cmd_mgr->free_list[index]);
570 	atomic_dec(&io_req->tgt->num_active_ios);
571 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
572 
573 }
574 
575 static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
576 {
577 	struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
578 	struct bnx2fc_interface *interface = io_req->port->priv;
579 	struct bnx2fc_hba *hba = interface->hba;
580 	size_t sz = sizeof(struct fcoe_bd_ctx);
581 
582 	/* clear tm flags */
583 	mp_req->tm_flags = 0;
584 	if (mp_req->mp_req_bd) {
585 		dma_free_coherent(&hba->pcidev->dev, sz,
586 				     mp_req->mp_req_bd,
587 				     mp_req->mp_req_bd_dma);
588 		mp_req->mp_req_bd = NULL;
589 	}
590 	if (mp_req->mp_resp_bd) {
591 		dma_free_coherent(&hba->pcidev->dev, sz,
592 				     mp_req->mp_resp_bd,
593 				     mp_req->mp_resp_bd_dma);
594 		mp_req->mp_resp_bd = NULL;
595 	}
596 	if (mp_req->req_buf) {
597 		dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
598 				     mp_req->req_buf,
599 				     mp_req->req_buf_dma);
600 		mp_req->req_buf = NULL;
601 	}
602 	if (mp_req->resp_buf) {
603 		dma_free_coherent(&hba->pcidev->dev, PAGE_SIZE,
604 				     mp_req->resp_buf,
605 				     mp_req->resp_buf_dma);
606 		mp_req->resp_buf = NULL;
607 	}
608 }
609 
610 int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
611 {
612 	struct bnx2fc_mp_req *mp_req;
613 	struct fcoe_bd_ctx *mp_req_bd;
614 	struct fcoe_bd_ctx *mp_resp_bd;
615 	struct bnx2fc_interface *interface = io_req->port->priv;
616 	struct bnx2fc_hba *hba = interface->hba;
617 	dma_addr_t addr;
618 	size_t sz;
619 
620 	mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
621 	memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
622 
623 	mp_req->req_len = sizeof(struct fcp_cmnd);
624 	io_req->data_xfer_len = mp_req->req_len;
625 	mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
626 					     &mp_req->req_buf_dma,
627 					     GFP_ATOMIC);
628 	if (!mp_req->req_buf) {
629 		printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
630 		bnx2fc_free_mp_resc(io_req);
631 		return FAILED;
632 	}
633 
634 	mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, PAGE_SIZE,
635 					      &mp_req->resp_buf_dma,
636 					      GFP_ATOMIC);
637 	if (!mp_req->resp_buf) {
638 		printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
639 		bnx2fc_free_mp_resc(io_req);
640 		return FAILED;
641 	}
642 	memset(mp_req->req_buf, 0, PAGE_SIZE);
643 	memset(mp_req->resp_buf, 0, PAGE_SIZE);
644 
645 	/* Allocate and map mp_req_bd and mp_resp_bd */
646 	sz = sizeof(struct fcoe_bd_ctx);
647 	mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
648 						 &mp_req->mp_req_bd_dma,
649 						 GFP_ATOMIC);
650 	if (!mp_req->mp_req_bd) {
651 		printk(KERN_ERR PFX "unable to alloc MP req bd\n");
652 		bnx2fc_free_mp_resc(io_req);
653 		return FAILED;
654 	}
655 	mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
656 						 &mp_req->mp_resp_bd_dma,
657 						 GFP_ATOMIC);
658 	if (!mp_req->mp_resp_bd) {
659 		printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
660 		bnx2fc_free_mp_resc(io_req);
661 		return FAILED;
662 	}
663 	/* Fill bd table */
664 	addr = mp_req->req_buf_dma;
665 	mp_req_bd = mp_req->mp_req_bd;
666 	mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
667 	mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
668 	mp_req_bd->buf_len = PAGE_SIZE;
669 	mp_req_bd->flags = 0;
670 
671 	/*
672 	 * MP buffer is either a task mgmt command or an ELS.
673 	 * So the assumption is that it consumes a single bd
674 	 * entry in the bd table
675 	 */
676 	mp_resp_bd = mp_req->mp_resp_bd;
677 	addr = mp_req->resp_buf_dma;
678 	mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
679 	mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
680 	mp_resp_bd->buf_len = PAGE_SIZE;
681 	mp_resp_bd->flags = 0;
682 
683 	return SUCCESS;
684 }
685 
686 static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
687 {
688 	struct fc_lport *lport;
689 	struct fc_rport *rport;
690 	struct fc_rport_libfc_priv *rp;
691 	struct fcoe_port *port;
692 	struct bnx2fc_interface *interface;
693 	struct bnx2fc_rport *tgt;
694 	struct bnx2fc_cmd *io_req;
695 	struct bnx2fc_mp_req *tm_req;
696 	struct fcoe_task_ctx_entry *task;
697 	struct fcoe_task_ctx_entry *task_page;
698 	struct Scsi_Host *host = sc_cmd->device->host;
699 	struct fc_frame_header *fc_hdr;
700 	struct fcp_cmnd *fcp_cmnd;
701 	int task_idx, index;
702 	int rc = SUCCESS;
703 	u16 xid;
704 	u32 sid, did;
705 	unsigned long start = jiffies;
706 
707 	lport = shost_priv(host);
708 	rport = starget_to_rport(scsi_target(sc_cmd->device));
709 	port = lport_priv(lport);
710 	interface = port->priv;
711 
712 	if (rport == NULL) {
713 		printk(KERN_ERR PFX "device_reset: rport is NULL\n");
714 		rc = FAILED;
715 		goto tmf_err;
716 	}
717 	rp = rport->dd_data;
718 
719 	rc = fc_block_scsi_eh(sc_cmd);
720 	if (rc)
721 		return rc;
722 
723 	if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
724 		printk(KERN_ERR PFX "device_reset: link is not ready\n");
725 		rc = FAILED;
726 		goto tmf_err;
727 	}
728 	/* rport and tgt are allocated together, so tgt should be non-NULL */
729 	tgt = (struct bnx2fc_rport *)&rp[1];
730 
731 	if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
732 		printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
733 		rc = FAILED;
734 		goto tmf_err;
735 	}
736 retry_tmf:
737 	io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
738 	if (!io_req) {
739 		if (time_after(jiffies, start + HZ)) {
740 			printk(KERN_ERR PFX "tmf: Failed TMF");
741 			rc = FAILED;
742 			goto tmf_err;
743 		}
744 		msleep(20);
745 		goto retry_tmf;
746 	}
747 	/* Initialize rest of io_req fields */
748 	io_req->sc_cmd = sc_cmd;
749 	io_req->port = port;
750 	io_req->tgt = tgt;
751 
752 	tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
753 
754 	rc = bnx2fc_init_mp_req(io_req);
755 	if (rc == FAILED) {
756 		printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
757 		spin_lock_bh(&tgt->tgt_lock);
758 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
759 		spin_unlock_bh(&tgt->tgt_lock);
760 		goto tmf_err;
761 	}
762 
763 	/* Set TM flags */
764 	io_req->io_req_flags = 0;
765 	tm_req->tm_flags = tm_flags;
766 
767 	/* Fill FCP_CMND */
768 	bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
769 	fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
770 	memset(fcp_cmnd->fc_cdb, 0,  sc_cmd->cmd_len);
771 	fcp_cmnd->fc_dl = 0;
772 
773 	/* Fill FC header */
774 	fc_hdr = &(tm_req->req_fc_hdr);
775 	sid = tgt->sid;
776 	did = rport->port_id;
777 	__fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
778 			   FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
779 			   FC_FC_SEQ_INIT, 0);
780 	/* Obtain exchange id */
781 	xid = io_req->xid;
782 
783 	BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
784 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
785 	index = xid % BNX2FC_TASKS_PER_PAGE;
786 
787 	/* Initialize task context for this IO request */
788 	task_page = (struct fcoe_task_ctx_entry *)
789 			interface->hba->task_ctx[task_idx];
790 	task = &(task_page[index]);
791 	bnx2fc_init_mp_task(io_req, task);
792 
793 	sc_cmd->SCp.ptr = (char *)io_req;
794 
795 	/* Obtain free SQ entry */
796 	spin_lock_bh(&tgt->tgt_lock);
797 	bnx2fc_add_2_sq(tgt, xid);
798 
799 	/* Enqueue the io_req to active_tm_queue */
800 	io_req->on_tmf_queue = 1;
801 	list_add_tail(&io_req->link, &tgt->active_tm_queue);
802 
803 	init_completion(&io_req->tm_done);
804 	io_req->wait_for_comp = 1;
805 
806 	/* Ring doorbell */
807 	bnx2fc_ring_doorbell(tgt);
808 	spin_unlock_bh(&tgt->tgt_lock);
809 
810 	rc = wait_for_completion_timeout(&io_req->tm_done,
811 					 BNX2FC_TM_TIMEOUT * HZ);
812 	spin_lock_bh(&tgt->tgt_lock);
813 
814 	io_req->wait_for_comp = 0;
815 	if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
816 		set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
817 		if (io_req->on_tmf_queue) {
818 			list_del_init(&io_req->link);
819 			io_req->on_tmf_queue = 0;
820 		}
821 		io_req->wait_for_comp = 1;
822 		bnx2fc_initiate_cleanup(io_req);
823 		spin_unlock_bh(&tgt->tgt_lock);
824 		rc = wait_for_completion_timeout(&io_req->tm_done,
825 						 BNX2FC_FW_TIMEOUT);
826 		spin_lock_bh(&tgt->tgt_lock);
827 		io_req->wait_for_comp = 0;
828 		if (!rc)
829 			kref_put(&io_req->refcount, bnx2fc_cmd_release);
830 	}
831 
832 	spin_unlock_bh(&tgt->tgt_lock);
833 
834 	if (!rc) {
835 		BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
836 		rc = FAILED;
837 	} else {
838 		BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
839 		rc = SUCCESS;
840 	}
841 tmf_err:
842 	return rc;
843 }
844 
845 int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
846 {
847 	struct fc_lport *lport;
848 	struct bnx2fc_rport *tgt = io_req->tgt;
849 	struct fc_rport *rport = tgt->rport;
850 	struct fc_rport_priv *rdata = tgt->rdata;
851 	struct bnx2fc_interface *interface;
852 	struct fcoe_port *port;
853 	struct bnx2fc_cmd *abts_io_req;
854 	struct fcoe_task_ctx_entry *task;
855 	struct fcoe_task_ctx_entry *task_page;
856 	struct fc_frame_header *fc_hdr;
857 	struct bnx2fc_mp_req *abts_req;
858 	int task_idx, index;
859 	u32 sid, did;
860 	u16 xid;
861 	int rc = SUCCESS;
862 	u32 r_a_tov = rdata->r_a_tov;
863 
864 	/* called with tgt_lock held */
865 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
866 
867 	port = io_req->port;
868 	interface = port->priv;
869 	lport = port->lport;
870 
871 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
872 		printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
873 		rc = FAILED;
874 		goto abts_err;
875 	}
876 
877 	if (rport == NULL) {
878 		printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
879 		rc = FAILED;
880 		goto abts_err;
881 	}
882 
883 	if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
884 		printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
885 		rc = FAILED;
886 		goto abts_err;
887 	}
888 
889 	abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
890 	if (!abts_io_req) {
891 		printk(KERN_ERR PFX "abts: couldnt allocate cmd\n");
892 		rc = FAILED;
893 		goto abts_err;
894 	}
895 
896 	/* Initialize rest of io_req fields */
897 	abts_io_req->sc_cmd = NULL;
898 	abts_io_req->port = port;
899 	abts_io_req->tgt = tgt;
900 	abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
901 
902 	abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
903 	memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
904 
905 	/* Fill FC header */
906 	fc_hdr = &(abts_req->req_fc_hdr);
907 
908 	/* Obtain oxid and rxid for the original exchange to be aborted */
909 	fc_hdr->fh_ox_id = htons(io_req->xid);
910 	fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
911 
912 	sid = tgt->sid;
913 	did = rport->port_id;
914 
915 	__fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
916 			   FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
917 			   FC_FC_SEQ_INIT, 0);
918 
919 	xid = abts_io_req->xid;
920 	BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
921 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
922 	index = xid % BNX2FC_TASKS_PER_PAGE;
923 
924 	/* Initialize task context for this IO request */
925 	task_page = (struct fcoe_task_ctx_entry *)
926 			interface->hba->task_ctx[task_idx];
927 	task = &(task_page[index]);
928 	bnx2fc_init_mp_task(abts_io_req, task);
929 
930 	/*
931 	 * ABTS task is a temporary task that will be cleaned up
932 	 * irrespective of ABTS response. We need to start the timer
933 	 * for the original exchange, as the CQE is posted for the original
934 	 * IO request.
935 	 *
936 	 * Timer for ABTS is started only when it is originated by a
937 	 * TM request. For the ABTS issued as part of ULP timeout,
938 	 * scsi-ml maintains the timers.
939 	 */
940 
941 	/* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
942 	bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
943 
944 	/* Obtain free SQ entry */
945 	bnx2fc_add_2_sq(tgt, xid);
946 
947 	/* Ring doorbell */
948 	bnx2fc_ring_doorbell(tgt);
949 
950 abts_err:
951 	return rc;
952 }
953 
954 int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
955 				enum fc_rctl r_ctl)
956 {
957 	struct fc_lport *lport;
958 	struct bnx2fc_rport *tgt = orig_io_req->tgt;
959 	struct bnx2fc_interface *interface;
960 	struct fcoe_port *port;
961 	struct bnx2fc_cmd *seq_clnp_req;
962 	struct fcoe_task_ctx_entry *task;
963 	struct fcoe_task_ctx_entry *task_page;
964 	struct bnx2fc_els_cb_arg *cb_arg = NULL;
965 	int task_idx, index;
966 	u16 xid;
967 	int rc = 0;
968 
969 	BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
970 		   orig_io_req->xid);
971 	kref_get(&orig_io_req->refcount);
972 
973 	port = orig_io_req->port;
974 	interface = port->priv;
975 	lport = port->lport;
976 
977 	cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
978 	if (!cb_arg) {
979 		printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
980 		rc = -ENOMEM;
981 		goto cleanup_err;
982 	}
983 
984 	seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
985 	if (!seq_clnp_req) {
986 		printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
987 		rc = -ENOMEM;
988 		kfree(cb_arg);
989 		goto cleanup_err;
990 	}
991 	/* Initialize rest of io_req fields */
992 	seq_clnp_req->sc_cmd = NULL;
993 	seq_clnp_req->port = port;
994 	seq_clnp_req->tgt = tgt;
995 	seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
996 
997 	xid = seq_clnp_req->xid;
998 
999 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1000 	index = xid % BNX2FC_TASKS_PER_PAGE;
1001 
1002 	/* Initialize task context for this IO request */
1003 	task_page = (struct fcoe_task_ctx_entry *)
1004 		     interface->hba->task_ctx[task_idx];
1005 	task = &(task_page[index]);
1006 	cb_arg->aborted_io_req = orig_io_req;
1007 	cb_arg->io_req = seq_clnp_req;
1008 	cb_arg->r_ctl = r_ctl;
1009 	cb_arg->offset = offset;
1010 	seq_clnp_req->cb_arg = cb_arg;
1011 
1012 	printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
1013 	bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
1014 
1015 	/* Obtain free SQ entry */
1016 	bnx2fc_add_2_sq(tgt, xid);
1017 
1018 	/* Ring doorbell */
1019 	bnx2fc_ring_doorbell(tgt);
1020 cleanup_err:
1021 	return rc;
1022 }
1023 
1024 int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
1025 {
1026 	struct fc_lport *lport;
1027 	struct bnx2fc_rport *tgt = io_req->tgt;
1028 	struct bnx2fc_interface *interface;
1029 	struct fcoe_port *port;
1030 	struct bnx2fc_cmd *cleanup_io_req;
1031 	struct fcoe_task_ctx_entry *task;
1032 	struct fcoe_task_ctx_entry *task_page;
1033 	int task_idx, index;
1034 	u16 xid, orig_xid;
1035 	int rc = 0;
1036 
1037 	/* ASSUMPTION: called with tgt_lock held */
1038 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
1039 
1040 	port = io_req->port;
1041 	interface = port->priv;
1042 	lport = port->lport;
1043 
1044 	cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
1045 	if (!cleanup_io_req) {
1046 		printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
1047 		rc = -1;
1048 		goto cleanup_err;
1049 	}
1050 
1051 	/* Initialize rest of io_req fields */
1052 	cleanup_io_req->sc_cmd = NULL;
1053 	cleanup_io_req->port = port;
1054 	cleanup_io_req->tgt = tgt;
1055 	cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
1056 
1057 	xid = cleanup_io_req->xid;
1058 
1059 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1060 	index = xid % BNX2FC_TASKS_PER_PAGE;
1061 
1062 	/* Initialize task context for this IO request */
1063 	task_page = (struct fcoe_task_ctx_entry *)
1064 			interface->hba->task_ctx[task_idx];
1065 	task = &(task_page[index]);
1066 	orig_xid = io_req->xid;
1067 
1068 	BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
1069 
1070 	bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
1071 
1072 	/* Obtain free SQ entry */
1073 	bnx2fc_add_2_sq(tgt, xid);
1074 
1075 	/* Ring doorbell */
1076 	bnx2fc_ring_doorbell(tgt);
1077 
1078 cleanup_err:
1079 	return rc;
1080 }
1081 
1082 /**
1083  * bnx2fc_eh_target_reset: Reset a target
1084  *
1085  * @sc_cmd:	SCSI command
1086  *
1087  * Set from SCSI host template to send task mgmt command to the target
1088  *	and wait for the response
1089  */
1090 int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
1091 {
1092 	return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
1093 }
1094 
1095 /**
1096  * bnx2fc_eh_device_reset - Reset a single LUN
1097  *
1098  * @sc_cmd:	SCSI command
1099  *
1100  * Set from SCSI host template to send task mgmt command to the target
1101  *	and wait for the response
1102  */
1103 int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1104 {
1105 	return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
1106 }
1107 
1108 int bnx2fc_expl_logo(struct fc_lport *lport, struct bnx2fc_cmd *io_req)
1109 {
1110 	struct bnx2fc_rport *tgt = io_req->tgt;
1111 	struct fc_rport_priv *rdata = tgt->rdata;
1112 	int logo_issued;
1113 	int rc = SUCCESS;
1114 	int wait_cnt = 0;
1115 
1116 	BNX2FC_IO_DBG(io_req, "Expl logo - tgt flags = 0x%lx\n",
1117 		      tgt->flags);
1118 	logo_issued = test_and_set_bit(BNX2FC_FLAG_EXPL_LOGO,
1119 				       &tgt->flags);
1120 	io_req->wait_for_comp = 1;
1121 	bnx2fc_initiate_cleanup(io_req);
1122 
1123 	spin_unlock_bh(&tgt->tgt_lock);
1124 
1125 	wait_for_completion(&io_req->tm_done);
1126 
1127 	io_req->wait_for_comp = 0;
1128 	/*
1129 	 * release the reference taken in eh_abort to allow the
1130 	 * target to re-login after flushing IOs
1131 	 */
1132 	 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1133 
1134 	if (!logo_issued) {
1135 		clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
1136 		mutex_lock(&lport->disc.disc_mutex);
1137 		lport->tt.rport_logoff(rdata);
1138 		mutex_unlock(&lport->disc.disc_mutex);
1139 		do {
1140 			msleep(BNX2FC_RELOGIN_WAIT_TIME);
1141 			if (wait_cnt++ > BNX2FC_RELOGIN_WAIT_CNT) {
1142 				rc = FAILED;
1143 				break;
1144 			}
1145 		} while (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags));
1146 	}
1147 	spin_lock_bh(&tgt->tgt_lock);
1148 	return rc;
1149 }
1150 /**
1151  * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
1152  *			SCSI command
1153  *
1154  * @sc_cmd:	SCSI_ML command pointer
1155  *
1156  * SCSI abort request handler
1157  */
1158 int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
1159 {
1160 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1161 	struct fc_rport_libfc_priv *rp = rport->dd_data;
1162 	struct bnx2fc_cmd *io_req;
1163 	struct fc_lport *lport;
1164 	struct bnx2fc_rport *tgt;
1165 	int rc = FAILED;
1166 
1167 
1168 	rc = fc_block_scsi_eh(sc_cmd);
1169 	if (rc)
1170 		return rc;
1171 
1172 	lport = shost_priv(sc_cmd->device->host);
1173 	if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1174 		printk(KERN_ERR PFX "eh_abort: link not ready\n");
1175 		return rc;
1176 	}
1177 
1178 	tgt = (struct bnx2fc_rport *)&rp[1];
1179 
1180 	BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1181 
1182 	spin_lock_bh(&tgt->tgt_lock);
1183 	io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr;
1184 	if (!io_req) {
1185 		/* Command might have just completed */
1186 		printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
1187 		spin_unlock_bh(&tgt->tgt_lock);
1188 		return SUCCESS;
1189 	}
1190 	BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
1191 		      io_req->refcount.refcount.counter);
1192 
1193 	/* Hold IO request across abort processing */
1194 	kref_get(&io_req->refcount);
1195 
1196 	BUG_ON(tgt != io_req->tgt);
1197 
1198 	/* Remove the io_req from the active_q. */
1199 	/*
1200 	 * Task Mgmt functions (LUN RESET & TGT RESET) will not
1201 	 * issue an ABTS on this particular IO req, as the
1202 	 * io_req is no longer in the active_q.
1203 	 */
1204 	if (tgt->flush_in_prog) {
1205 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1206 			"flush in progress\n", io_req->xid);
1207 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1208 		spin_unlock_bh(&tgt->tgt_lock);
1209 		return SUCCESS;
1210 	}
1211 
1212 	if (io_req->on_active_queue == 0) {
1213 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1214 				"not on active_q\n", io_req->xid);
1215 		/*
1216 		 * This condition can happen only due to the FW bug,
1217 		 * where we do not receive cleanup response from
1218 		 * the FW. Handle this case gracefully by erroring
1219 		 * back the IO request to SCSI-ml
1220 		 */
1221 		bnx2fc_scsi_done(io_req, DID_ABORT);
1222 
1223 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1224 		spin_unlock_bh(&tgt->tgt_lock);
1225 		return SUCCESS;
1226 	}
1227 
1228 	/*
1229 	 * Only eh_abort processing will remove the IO from
1230 	 * active_cmd_q before processing the request. this is
1231 	 * done to avoid race conditions between IOs aborted
1232 	 * as part of task management completion and eh_abort
1233 	 * processing
1234 	 */
1235 	list_del_init(&io_req->link);
1236 	io_req->on_active_queue = 0;
1237 	/* Move IO req to retire queue */
1238 	list_add_tail(&io_req->link, &tgt->io_retire_queue);
1239 
1240 	init_completion(&io_req->tm_done);
1241 
1242 	if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
1243 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1244 				"already in abts processing\n", io_req->xid);
1245 		if (cancel_delayed_work(&io_req->timeout_work))
1246 			kref_put(&io_req->refcount,
1247 				 bnx2fc_cmd_release); /* drop timer hold */
1248 		rc = bnx2fc_expl_logo(lport, io_req);
1249 		/* This only occurs when an task abort was requested while ABTS
1250 		   is in progress.  Setting the IO_CLEANUP flag will skip the
1251 		   RRQ process in the case when the fw generated SCSI_CMD cmpl
1252 		   was a result from the ABTS request rather than the CLEANUP
1253 		   request */
1254 		set_bit(BNX2FC_FLAG_IO_CLEANUP,	&io_req->req_flags);
1255 		goto out;
1256 	}
1257 
1258 	/* Cancel the current timer running on this io_req */
1259 	if (cancel_delayed_work(&io_req->timeout_work))
1260 		kref_put(&io_req->refcount,
1261 			 bnx2fc_cmd_release); /* drop timer hold */
1262 	set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
1263 	io_req->wait_for_comp = 1;
1264 	rc = bnx2fc_initiate_abts(io_req);
1265 	if (rc == FAILED) {
1266 		bnx2fc_initiate_cleanup(io_req);
1267 		spin_unlock_bh(&tgt->tgt_lock);
1268 		wait_for_completion(&io_req->tm_done);
1269 		spin_lock_bh(&tgt->tgt_lock);
1270 		io_req->wait_for_comp = 0;
1271 		goto done;
1272 	}
1273 	spin_unlock_bh(&tgt->tgt_lock);
1274 
1275 	wait_for_completion(&io_req->tm_done);
1276 
1277 	spin_lock_bh(&tgt->tgt_lock);
1278 	io_req->wait_for_comp = 0;
1279 	if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1280 		BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
1281 		rc = SUCCESS;
1282 	} else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1283 				      &io_req->req_flags))) {
1284 		/* Let the scsi-ml try to recover this command */
1285 		printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
1286 		       io_req->xid);
1287 		rc = bnx2fc_expl_logo(lport, io_req);
1288 		goto out;
1289 	} else {
1290 		/*
1291 		 * We come here even when there was a race condition
1292 		 * between timeout and abts completion, and abts
1293 		 * completion happens just in time.
1294 		 */
1295 		BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1296 		rc = SUCCESS;
1297 		bnx2fc_scsi_done(io_req, DID_ABORT);
1298 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1299 	}
1300 done:
1301 	/* release the reference taken in eh_abort */
1302 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
1303 out:
1304 	spin_unlock_bh(&tgt->tgt_lock);
1305 	return rc;
1306 }
1307 
1308 void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
1309 				      struct fcoe_task_ctx_entry *task,
1310 				      u8 rx_state)
1311 {
1312 	struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
1313 	struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
1314 	u32 offset = cb_arg->offset;
1315 	enum fc_rctl r_ctl = cb_arg->r_ctl;
1316 	int rc = 0;
1317 	struct bnx2fc_rport *tgt = orig_io_req->tgt;
1318 
1319 	BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
1320 			      "cmd_type = %d\n",
1321 		   seq_clnp_req->xid, seq_clnp_req->cmd_type);
1322 
1323 	if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
1324 		printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
1325 			seq_clnp_req->xid);
1326 		goto free_cb_arg;
1327 	}
1328 
1329 	spin_unlock_bh(&tgt->tgt_lock);
1330 	rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
1331 	spin_lock_bh(&tgt->tgt_lock);
1332 
1333 	if (rc)
1334 		printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
1335 			" IO will abort\n");
1336 	seq_clnp_req->cb_arg = NULL;
1337 	kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
1338 free_cb_arg:
1339 	kfree(cb_arg);
1340 	return;
1341 }
1342 
1343 void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1344 				  struct fcoe_task_ctx_entry *task,
1345 				  u8 num_rq)
1346 {
1347 	BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
1348 			      "refcnt = %d, cmd_type = %d\n",
1349 		   io_req->refcount.refcount.counter, io_req->cmd_type);
1350 	bnx2fc_scsi_done(io_req, DID_ERROR);
1351 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
1352 	if (io_req->wait_for_comp)
1353 		complete(&io_req->tm_done);
1354 }
1355 
1356 void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1357 			       struct fcoe_task_ctx_entry *task,
1358 			       u8 num_rq)
1359 {
1360 	u32 r_ctl;
1361 	u32 r_a_tov = FC_DEF_R_A_TOV;
1362 	u8 issue_rrq = 0;
1363 	struct bnx2fc_rport *tgt = io_req->tgt;
1364 
1365 	BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1366 			      "refcnt = %d, cmd_type = %d\n",
1367 		   io_req->xid,
1368 		   io_req->refcount.refcount.counter, io_req->cmd_type);
1369 
1370 	if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1371 				       &io_req->req_flags)) {
1372 		BNX2FC_IO_DBG(io_req, "Timer context finished processing"
1373 				" this io\n");
1374 		return;
1375 	}
1376 
1377 	/* Do not issue RRQ as this IO is already cleanedup */
1378 	if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
1379 				&io_req->req_flags))
1380 		goto io_compl;
1381 
1382 	/*
1383 	 * For ABTS issued due to SCSI eh_abort_handler, timeout
1384 	 * values are maintained by scsi-ml itself. Cancel timeout
1385 	 * in case ABTS issued as part of task management function
1386 	 * or due to FW error.
1387 	 */
1388 	if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
1389 		if (cancel_delayed_work(&io_req->timeout_work))
1390 			kref_put(&io_req->refcount,
1391 				 bnx2fc_cmd_release); /* drop timer hold */
1392 
1393 	r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
1394 
1395 	switch (r_ctl) {
1396 	case FC_RCTL_BA_ACC:
1397 		/*
1398 		 * Dont release this cmd yet. It will be relesed
1399 		 * after we get RRQ response
1400 		 */
1401 		BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1402 		issue_rrq = 1;
1403 		break;
1404 
1405 	case FC_RCTL_BA_RJT:
1406 		BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1407 		break;
1408 	default:
1409 		printk(KERN_ERR PFX "Unknown ABTS response\n");
1410 		break;
1411 	}
1412 
1413 	if (issue_rrq) {
1414 		BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1415 		set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1416 	}
1417 	set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1418 	bnx2fc_cmd_timer_set(io_req, r_a_tov);
1419 
1420 io_compl:
1421 	if (io_req->wait_for_comp) {
1422 		if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1423 				       &io_req->req_flags))
1424 			complete(&io_req->tm_done);
1425 	} else {
1426 		/*
1427 		 * We end up here when ABTS is issued as
1428 		 * in asynchronous context, i.e., as part
1429 		 * of task management completion, or
1430 		 * when FW error is received or when the
1431 		 * ABTS is issued when the IO is timed
1432 		 * out.
1433 		 */
1434 
1435 		if (io_req->on_active_queue) {
1436 			list_del_init(&io_req->link);
1437 			io_req->on_active_queue = 0;
1438 			/* Move IO req to retire queue */
1439 			list_add_tail(&io_req->link, &tgt->io_retire_queue);
1440 		}
1441 		bnx2fc_scsi_done(io_req, DID_ERROR);
1442 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1443 	}
1444 }
1445 
1446 static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1447 {
1448 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1449 	struct bnx2fc_rport *tgt = io_req->tgt;
1450 	struct bnx2fc_cmd *cmd, *tmp;
1451 	int tm_lun = sc_cmd->device->lun;
1452 	int rc = 0;
1453 	int lun;
1454 
1455 	/* called with tgt_lock held */
1456 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1457 	/*
1458 	 * Walk thru the active_ios queue and ABORT the IO
1459 	 * that matches with the LUN that was reset
1460 	 */
1461 	list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1462 		BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
1463 		lun = cmd->sc_cmd->device->lun;
1464 		if (lun == tm_lun) {
1465 			/* Initiate ABTS on this cmd */
1466 			if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1467 					      &cmd->req_flags)) {
1468 				/* cancel the IO timeout */
1469 				if (cancel_delayed_work(&io_req->timeout_work))
1470 					kref_put(&io_req->refcount,
1471 						 bnx2fc_cmd_release);
1472 							/* timer hold */
1473 				rc = bnx2fc_initiate_abts(cmd);
1474 				/* abts shouldn't fail in this context */
1475 				WARN_ON(rc != SUCCESS);
1476 			} else
1477 				printk(KERN_ERR PFX "lun_rst: abts already in"
1478 					" progress for this IO 0x%x\n",
1479 					cmd->xid);
1480 		}
1481 	}
1482 }
1483 
1484 static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1485 {
1486 	struct bnx2fc_rport *tgt = io_req->tgt;
1487 	struct bnx2fc_cmd *cmd, *tmp;
1488 	int rc = 0;
1489 
1490 	/* called with tgt_lock held */
1491 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1492 	/*
1493 	 * Walk thru the active_ios queue and ABORT the IO
1494 	 * that matches with the LUN that was reset
1495 	 */
1496 	list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1497 		BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
1498 		/* Initiate ABTS */
1499 		if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1500 							&cmd->req_flags)) {
1501 			/* cancel the IO timeout */
1502 			if (cancel_delayed_work(&io_req->timeout_work))
1503 				kref_put(&io_req->refcount,
1504 					 bnx2fc_cmd_release); /* timer hold */
1505 			rc = bnx2fc_initiate_abts(cmd);
1506 			/* abts shouldn't fail in this context */
1507 			WARN_ON(rc != SUCCESS);
1508 
1509 		} else
1510 			printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1511 				" for this IO 0x%x\n", cmd->xid);
1512 	}
1513 }
1514 
1515 void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
1516 			     struct fcoe_task_ctx_entry *task, u8 num_rq)
1517 {
1518 	struct bnx2fc_mp_req *tm_req;
1519 	struct fc_frame_header *fc_hdr;
1520 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1521 	u64 *hdr;
1522 	u64 *temp_hdr;
1523 	void *rsp_buf;
1524 
1525 	/* Called with tgt_lock held */
1526 	BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
1527 
1528 	if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
1529 		set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
1530 	else {
1531 		/* TM has already timed out and we got
1532 		 * delayed completion. Ignore completion
1533 		 * processing.
1534 		 */
1535 		return;
1536 	}
1537 
1538 	tm_req = &(io_req->mp_req);
1539 	fc_hdr = &(tm_req->resp_fc_hdr);
1540 	hdr = (u64 *)fc_hdr;
1541 	temp_hdr = (u64 *)
1542 		&task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
1543 	hdr[0] = cpu_to_be64(temp_hdr[0]);
1544 	hdr[1] = cpu_to_be64(temp_hdr[1]);
1545 	hdr[2] = cpu_to_be64(temp_hdr[2]);
1546 
1547 	tm_req->resp_len =
1548 		task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
1549 
1550 	rsp_buf = tm_req->resp_buf;
1551 
1552 	if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
1553 		bnx2fc_parse_fcp_rsp(io_req,
1554 				     (struct fcoe_fcp_rsp_payload *)
1555 				     rsp_buf, num_rq);
1556 		if (io_req->fcp_rsp_code == 0) {
1557 			/* TM successful */
1558 			if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
1559 				bnx2fc_lun_reset_cmpl(io_req);
1560 			else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
1561 				bnx2fc_tgt_reset_cmpl(io_req);
1562 		}
1563 	} else {
1564 		printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
1565 			fc_hdr->fh_r_ctl);
1566 	}
1567 	if (!sc_cmd->SCp.ptr) {
1568 		printk(KERN_ERR PFX "tm_compl: SCp.ptr is NULL\n");
1569 		return;
1570 	}
1571 	switch (io_req->fcp_status) {
1572 	case FC_GOOD:
1573 		if (io_req->cdb_status == 0) {
1574 			/* Good IO completion */
1575 			sc_cmd->result = DID_OK << 16;
1576 		} else {
1577 			/* Transport status is good, SCSI status not good */
1578 			sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1579 		}
1580 		if (io_req->fcp_resid)
1581 			scsi_set_resid(sc_cmd, io_req->fcp_resid);
1582 		break;
1583 
1584 	default:
1585 		BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
1586 			   io_req->fcp_status);
1587 		break;
1588 	}
1589 
1590 	sc_cmd = io_req->sc_cmd;
1591 	io_req->sc_cmd = NULL;
1592 
1593 	/* check if the io_req exists in tgt's tmf_q */
1594 	if (io_req->on_tmf_queue) {
1595 
1596 		list_del_init(&io_req->link);
1597 		io_req->on_tmf_queue = 0;
1598 	} else {
1599 
1600 		printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
1601 		return;
1602 	}
1603 
1604 	sc_cmd->SCp.ptr = NULL;
1605 	sc_cmd->scsi_done(sc_cmd);
1606 
1607 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
1608 	if (io_req->wait_for_comp) {
1609 		BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
1610 		complete(&io_req->tm_done);
1611 	}
1612 }
1613 
1614 static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
1615 			   int bd_index)
1616 {
1617 	struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1618 	int frag_size, sg_frags;
1619 
1620 	sg_frags = 0;
1621 	while (sg_len) {
1622 		if (sg_len >= BNX2FC_BD_SPLIT_SZ)
1623 			frag_size = BNX2FC_BD_SPLIT_SZ;
1624 		else
1625 			frag_size = sg_len;
1626 		bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
1627 		bd[bd_index + sg_frags].buf_addr_hi  = addr >> 32;
1628 		bd[bd_index + sg_frags].buf_len = (u16)frag_size;
1629 		bd[bd_index + sg_frags].flags = 0;
1630 
1631 		addr += (u64) frag_size;
1632 		sg_frags++;
1633 		sg_len -= frag_size;
1634 	}
1635 	return sg_frags;
1636 
1637 }
1638 
1639 static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
1640 {
1641 	struct bnx2fc_interface *interface = io_req->port->priv;
1642 	struct bnx2fc_hba *hba = interface->hba;
1643 	struct scsi_cmnd *sc = io_req->sc_cmd;
1644 	struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1645 	struct scatterlist *sg;
1646 	int byte_count = 0;
1647 	int sg_count = 0;
1648 	int bd_count = 0;
1649 	int sg_frags;
1650 	unsigned int sg_len;
1651 	u64 addr;
1652 	int i;
1653 
1654 	sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
1655 			      scsi_sg_count(sc), sc->sc_data_direction);
1656 	scsi_for_each_sg(sc, sg, sg_count, i) {
1657 		sg_len = sg_dma_len(sg);
1658 		addr = sg_dma_address(sg);
1659 		if (sg_len > BNX2FC_MAX_BD_LEN) {
1660 			sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
1661 						   bd_count);
1662 		} else {
1663 
1664 			sg_frags = 1;
1665 			bd[bd_count].buf_addr_lo = addr & 0xffffffff;
1666 			bd[bd_count].buf_addr_hi  = addr >> 32;
1667 			bd[bd_count].buf_len = (u16)sg_len;
1668 			bd[bd_count].flags = 0;
1669 		}
1670 		bd_count += sg_frags;
1671 		byte_count += sg_len;
1672 	}
1673 	if (byte_count != scsi_bufflen(sc))
1674 		printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
1675 			"task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
1676 			io_req->xid);
1677 	return bd_count;
1678 }
1679 
1680 static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
1681 {
1682 	struct scsi_cmnd *sc = io_req->sc_cmd;
1683 	struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1684 	int bd_count;
1685 
1686 	if (scsi_sg_count(sc)) {
1687 		bd_count = bnx2fc_map_sg(io_req);
1688 		if (bd_count == 0)
1689 			return -ENOMEM;
1690 	} else {
1691 		bd_count = 0;
1692 		bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1693 		bd[0].buf_len = bd[0].flags = 0;
1694 	}
1695 	io_req->bd_tbl->bd_valid = bd_count;
1696 
1697 	return 0;
1698 }
1699 
1700 static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1701 {
1702 	struct scsi_cmnd *sc = io_req->sc_cmd;
1703 
1704 	if (io_req->bd_tbl->bd_valid && sc) {
1705 		scsi_dma_unmap(sc);
1706 		io_req->bd_tbl->bd_valid = 0;
1707 	}
1708 }
1709 
1710 void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1711 				  struct fcp_cmnd *fcp_cmnd)
1712 {
1713 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1714 	char tag[2];
1715 
1716 	memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1717 
1718 	int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
1719 
1720 	fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1721 	memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1722 
1723 	fcp_cmnd->fc_cmdref = 0;
1724 	fcp_cmnd->fc_pri_ta = 0;
1725 	fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
1726 	fcp_cmnd->fc_flags = io_req->io_req_flags;
1727 
1728 	if (scsi_populate_tag_msg(sc_cmd, tag)) {
1729 		switch (tag[0]) {
1730 		case HEAD_OF_QUEUE_TAG:
1731 			fcp_cmnd->fc_pri_ta = FCP_PTA_HEADQ;
1732 			break;
1733 		case ORDERED_QUEUE_TAG:
1734 			fcp_cmnd->fc_pri_ta = FCP_PTA_ORDERED;
1735 			break;
1736 		default:
1737 			fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
1738 			break;
1739 		}
1740 	} else {
1741 		fcp_cmnd->fc_pri_ta = 0;
1742 	}
1743 }
1744 
1745 static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1746 				 struct fcoe_fcp_rsp_payload *fcp_rsp,
1747 				 u8 num_rq)
1748 {
1749 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1750 	struct bnx2fc_rport *tgt = io_req->tgt;
1751 	u8 rsp_flags = fcp_rsp->fcp_flags.flags;
1752 	u32 rq_buff_len = 0;
1753 	int i;
1754 	unsigned char *rq_data;
1755 	unsigned char *dummy;
1756 	int fcp_sns_len = 0;
1757 	int fcp_rsp_len = 0;
1758 
1759 	io_req->fcp_status = FC_GOOD;
1760 	io_req->fcp_resid = fcp_rsp->fcp_resid;
1761 
1762 	io_req->scsi_comp_flags = rsp_flags;
1763 	CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1764 				fcp_rsp->scsi_status_code;
1765 
1766 	/* Fetch fcp_rsp_info and fcp_sns_info if available */
1767 	if (num_rq) {
1768 
1769 		/*
1770 		 * We do not anticipate num_rq >1, as the linux defined
1771 		 * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
1772 		 * 256 bytes of single rq buffer is good enough to hold this.
1773 		 */
1774 
1775 		if (rsp_flags &
1776 		    FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1777 			fcp_rsp_len = rq_buff_len
1778 					= fcp_rsp->fcp_rsp_len;
1779 		}
1780 
1781 		if (rsp_flags &
1782 		    FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
1783 			fcp_sns_len = fcp_rsp->fcp_sns_len;
1784 			rq_buff_len += fcp_rsp->fcp_sns_len;
1785 		}
1786 
1787 		io_req->fcp_rsp_len = fcp_rsp_len;
1788 		io_req->fcp_sns_len = fcp_sns_len;
1789 
1790 		if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
1791 			/* Invalid sense sense length. */
1792 			printk(KERN_ERR PFX "invalid sns length %d\n",
1793 				rq_buff_len);
1794 			/* reset rq_buff_len */
1795 			rq_buff_len =  num_rq * BNX2FC_RQ_BUF_SZ;
1796 		}
1797 
1798 		rq_data = bnx2fc_get_next_rqe(tgt, 1);
1799 
1800 		if (num_rq > 1) {
1801 			/* We do not need extra sense data */
1802 			for (i = 1; i < num_rq; i++)
1803 				dummy = bnx2fc_get_next_rqe(tgt, 1);
1804 		}
1805 
1806 		/* fetch fcp_rsp_code */
1807 		if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1808 			/* Only for task management function */
1809 			io_req->fcp_rsp_code = rq_data[3];
1810 			printk(KERN_ERR PFX "fcp_rsp_code = %d\n",
1811 				io_req->fcp_rsp_code);
1812 		}
1813 
1814 		/* fetch sense data */
1815 		rq_data += fcp_rsp_len;
1816 
1817 		if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1818 			printk(KERN_ERR PFX "Truncating sense buffer\n");
1819 			fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1820 		}
1821 
1822 		memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1823 		if (fcp_sns_len)
1824 			memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1825 
1826 		/* return RQ entries */
1827 		for (i = 0; i < num_rq; i++)
1828 			bnx2fc_return_rqe(tgt, 1);
1829 	}
1830 }
1831 
1832 /**
1833  * bnx2fc_queuecommand - Queuecommand function of the scsi template
1834  *
1835  * @host:	The Scsi_Host the command was issued to
1836  * @sc_cmd:	struct scsi_cmnd to be executed
1837  *
1838  * This is the IO strategy routine, called by SCSI-ML
1839  **/
1840 int bnx2fc_queuecommand(struct Scsi_Host *host,
1841 			struct scsi_cmnd *sc_cmd)
1842 {
1843 	struct fc_lport *lport = shost_priv(host);
1844 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1845 	struct fc_rport_libfc_priv *rp = rport->dd_data;
1846 	struct bnx2fc_rport *tgt;
1847 	struct bnx2fc_cmd *io_req;
1848 	int rc = 0;
1849 	int rval;
1850 
1851 	rval = fc_remote_port_chkready(rport);
1852 	if (rval) {
1853 		sc_cmd->result = rval;
1854 		sc_cmd->scsi_done(sc_cmd);
1855 		return 0;
1856 	}
1857 
1858 	if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1859 		rc = SCSI_MLQUEUE_HOST_BUSY;
1860 		goto exit_qcmd;
1861 	}
1862 
1863 	/* rport and tgt are allocated together, so tgt should be non-NULL */
1864 	tgt = (struct bnx2fc_rport *)&rp[1];
1865 
1866 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1867 		/*
1868 		 * Session is not offloaded yet. Let SCSI-ml retry
1869 		 * the command.
1870 		 */
1871 		rc = SCSI_MLQUEUE_TARGET_BUSY;
1872 		goto exit_qcmd;
1873 	}
1874 
1875 	io_req = bnx2fc_cmd_alloc(tgt);
1876 	if (!io_req) {
1877 		rc = SCSI_MLQUEUE_HOST_BUSY;
1878 		goto exit_qcmd;
1879 	}
1880 	io_req->sc_cmd = sc_cmd;
1881 
1882 	if (bnx2fc_post_io_req(tgt, io_req)) {
1883 		printk(KERN_ERR PFX "Unable to post io_req\n");
1884 		rc = SCSI_MLQUEUE_HOST_BUSY;
1885 		goto exit_qcmd;
1886 	}
1887 exit_qcmd:
1888 	return rc;
1889 }
1890 
1891 void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1892 				   struct fcoe_task_ctx_entry *task,
1893 				   u8 num_rq)
1894 {
1895 	struct fcoe_fcp_rsp_payload *fcp_rsp;
1896 	struct bnx2fc_rport *tgt = io_req->tgt;
1897 	struct scsi_cmnd *sc_cmd;
1898 	struct Scsi_Host *host;
1899 
1900 
1901 	/* scsi_cmd_cmpl is called with tgt lock held */
1902 
1903 	if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1904 		/* we will not receive ABTS response for this IO */
1905 		BNX2FC_IO_DBG(io_req, "Timer context finished processing "
1906 			   "this scsi cmd\n");
1907 	}
1908 
1909 	/* Cancel the timeout_work, as we received IO completion */
1910 	if (cancel_delayed_work(&io_req->timeout_work))
1911 		kref_put(&io_req->refcount,
1912 			 bnx2fc_cmd_release); /* drop timer hold */
1913 
1914 	sc_cmd = io_req->sc_cmd;
1915 	if (sc_cmd == NULL) {
1916 		printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
1917 		return;
1918 	}
1919 
1920 	/* Fetch fcp_rsp from task context and perform cmd completion */
1921 	fcp_rsp = (struct fcoe_fcp_rsp_payload *)
1922 		   &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
1923 
1924 	/* parse fcp_rsp and obtain sense data from RQ if available */
1925 	bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq);
1926 
1927 	host = sc_cmd->device->host;
1928 	if (!sc_cmd->SCp.ptr) {
1929 		printk(KERN_ERR PFX "SCp.ptr is NULL\n");
1930 		return;
1931 	}
1932 
1933 	if (io_req->on_active_queue) {
1934 		list_del_init(&io_req->link);
1935 		io_req->on_active_queue = 0;
1936 		/* Move IO req to retire queue */
1937 		list_add_tail(&io_req->link, &tgt->io_retire_queue);
1938 	} else {
1939 		/* This should not happen, but could have been pulled
1940 		 * by bnx2fc_flush_active_ios(), or during a race
1941 		 * between command abort and (late) completion.
1942 		 */
1943 		BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
1944 		if (io_req->wait_for_comp)
1945 			if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1946 					       &io_req->req_flags))
1947 				complete(&io_req->tm_done);
1948 	}
1949 
1950 	bnx2fc_unmap_sg_list(io_req);
1951 	io_req->sc_cmd = NULL;
1952 
1953 	switch (io_req->fcp_status) {
1954 	case FC_GOOD:
1955 		if (io_req->cdb_status == 0) {
1956 			/* Good IO completion */
1957 			sc_cmd->result = DID_OK << 16;
1958 		} else {
1959 			/* Transport status is good, SCSI status not good */
1960 			BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
1961 				 " fcp_resid = 0x%x\n",
1962 				io_req->cdb_status, io_req->fcp_resid);
1963 			sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1964 		}
1965 		if (io_req->fcp_resid)
1966 			scsi_set_resid(sc_cmd, io_req->fcp_resid);
1967 		break;
1968 	default:
1969 		printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
1970 			io_req->fcp_status);
1971 		break;
1972 	}
1973 	sc_cmd->SCp.ptr = NULL;
1974 	sc_cmd->scsi_done(sc_cmd);
1975 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
1976 }
1977 
1978 int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
1979 			       struct bnx2fc_cmd *io_req)
1980 {
1981 	struct fcoe_task_ctx_entry *task;
1982 	struct fcoe_task_ctx_entry *task_page;
1983 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1984 	struct fcoe_port *port = tgt->port;
1985 	struct bnx2fc_interface *interface = port->priv;
1986 	struct bnx2fc_hba *hba = interface->hba;
1987 	struct fc_lport *lport = port->lport;
1988 	struct fc_stats *stats;
1989 	int task_idx, index;
1990 	u16 xid;
1991 
1992 	/* Initialize rest of io_req fields */
1993 	io_req->cmd_type = BNX2FC_SCSI_CMD;
1994 	io_req->port = port;
1995 	io_req->tgt = tgt;
1996 	io_req->data_xfer_len = scsi_bufflen(sc_cmd);
1997 	sc_cmd->SCp.ptr = (char *)io_req;
1998 
1999 	stats = per_cpu_ptr(lport->stats, get_cpu());
2000 	if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
2001 		io_req->io_req_flags = BNX2FC_READ;
2002 		stats->InputRequests++;
2003 		stats->InputBytes += io_req->data_xfer_len;
2004 	} else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
2005 		io_req->io_req_flags = BNX2FC_WRITE;
2006 		stats->OutputRequests++;
2007 		stats->OutputBytes += io_req->data_xfer_len;
2008 	} else {
2009 		io_req->io_req_flags = 0;
2010 		stats->ControlRequests++;
2011 	}
2012 	put_cpu();
2013 
2014 	xid = io_req->xid;
2015 
2016 	/* Build buffer descriptor list for firmware from sg list */
2017 	if (bnx2fc_build_bd_list_from_sg(io_req)) {
2018 		printk(KERN_ERR PFX "BD list creation failed\n");
2019 		spin_lock_bh(&tgt->tgt_lock);
2020 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2021 		spin_unlock_bh(&tgt->tgt_lock);
2022 		return -EAGAIN;
2023 	}
2024 
2025 	task_idx = xid / BNX2FC_TASKS_PER_PAGE;
2026 	index = xid % BNX2FC_TASKS_PER_PAGE;
2027 
2028 	/* Initialize task context for this IO request */
2029 	task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
2030 	task = &(task_page[index]);
2031 	bnx2fc_init_task(io_req, task);
2032 
2033 	spin_lock_bh(&tgt->tgt_lock);
2034 
2035 	if (tgt->flush_in_prog) {
2036 		printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
2037 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2038 		spin_unlock_bh(&tgt->tgt_lock);
2039 		return -EAGAIN;
2040 	}
2041 
2042 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
2043 		printk(KERN_ERR PFX "Session not ready...post_io\n");
2044 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2045 		spin_unlock_bh(&tgt->tgt_lock);
2046 		return -EAGAIN;
2047 	}
2048 
2049 	/* Time IO req */
2050 	if (tgt->io_timeout)
2051 		bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
2052 	/* Obtain free SQ entry */
2053 	bnx2fc_add_2_sq(tgt, xid);
2054 
2055 	/* Enqueue the io_req to active_cmd_queue */
2056 
2057 	io_req->on_active_queue = 1;
2058 	/* move io_req from pending_queue to active_queue */
2059 	list_add_tail(&io_req->link, &tgt->active_cmd_queue);
2060 
2061 	/* Ring doorbell */
2062 	bnx2fc_ring_doorbell(tgt);
2063 	spin_unlock_bh(&tgt->tgt_lock);
2064 	return 0;
2065 }
2066