xref: /openbmc/linux/drivers/scsi/bnx2fc/bnx2fc_io.c (revision adcf7dfb)
1de909d87SChad Dupuis /* bnx2fc_io.c: QLogic Linux FCoE offload driver.
2853e2bd2SBhanu Gollapudi  * IO manager and SCSI IO processing.
3853e2bd2SBhanu Gollapudi  *
4cf122191SBhanu Prakash Gollapudi  * Copyright (c) 2008-2013 Broadcom Corporation
597586090SChad Dupuis  * Copyright (c) 2014-2015 QLogic Corporation
6853e2bd2SBhanu Gollapudi  *
7853e2bd2SBhanu Gollapudi  * This program is free software; you can redistribute it and/or modify
8853e2bd2SBhanu Gollapudi  * it under the terms of the GNU General Public License as published by
9853e2bd2SBhanu Gollapudi  * the Free Software Foundation.
10853e2bd2SBhanu Gollapudi  *
11853e2bd2SBhanu Gollapudi  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
12853e2bd2SBhanu Gollapudi  */
13853e2bd2SBhanu Gollapudi 
14853e2bd2SBhanu Gollapudi #include "bnx2fc.h"
150ea5c275SBhanu Gollapudi 
160ea5c275SBhanu Gollapudi #define RESERVE_FREE_LIST_INDEX num_possible_cpus()
170ea5c275SBhanu Gollapudi 
18853e2bd2SBhanu Gollapudi static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
19853e2bd2SBhanu Gollapudi 			   int bd_index);
20853e2bd2SBhanu Gollapudi static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
21822f2903SBhanu Prakash Gollapudi static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
22853e2bd2SBhanu Gollapudi static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
23853e2bd2SBhanu Gollapudi static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
24853e2bd2SBhanu Gollapudi static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
25853e2bd2SBhanu Gollapudi 				 struct fcoe_fcp_rsp_payload *fcp_rsp,
26853e2bd2SBhanu Gollapudi 				 u8 num_rq);
27853e2bd2SBhanu Gollapudi 
28853e2bd2SBhanu Gollapudi void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
29853e2bd2SBhanu Gollapudi 			  unsigned int timer_msec)
30853e2bd2SBhanu Gollapudi {
31aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = io_req->port->priv;
32853e2bd2SBhanu Gollapudi 
33aea71a02SBhanu Prakash Gollapudi 	if (queue_delayed_work(interface->timer_work_queue,
34aea71a02SBhanu Prakash Gollapudi 			       &io_req->timeout_work,
35853e2bd2SBhanu Gollapudi 			       msecs_to_jiffies(timer_msec)))
36853e2bd2SBhanu Gollapudi 		kref_get(&io_req->refcount);
37853e2bd2SBhanu Gollapudi }
38853e2bd2SBhanu Gollapudi 
39853e2bd2SBhanu Gollapudi static void bnx2fc_cmd_timeout(struct work_struct *work)
40853e2bd2SBhanu Gollapudi {
41853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
42853e2bd2SBhanu Gollapudi 						 timeout_work.work);
43853e2bd2SBhanu Gollapudi 	struct fc_lport *lport;
44853e2bd2SBhanu Gollapudi 	struct fc_rport_priv *rdata;
45853e2bd2SBhanu Gollapudi 	u8 cmd_type = io_req->cmd_type;
46853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
47853e2bd2SBhanu Gollapudi 	int logo_issued;
48853e2bd2SBhanu Gollapudi 	int rc;
49853e2bd2SBhanu Gollapudi 
50853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
51853e2bd2SBhanu Gollapudi 		      "req_flags = %lx\n", cmd_type, io_req->req_flags);
52853e2bd2SBhanu Gollapudi 
53853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
54853e2bd2SBhanu Gollapudi 	if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
55853e2bd2SBhanu Gollapudi 		clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
56853e2bd2SBhanu Gollapudi 		/*
57853e2bd2SBhanu Gollapudi 		 * ideally we should hold the io_req until RRQ complets,
58853e2bd2SBhanu Gollapudi 		 * and release io_req from timeout hold.
59853e2bd2SBhanu Gollapudi 		 */
60853e2bd2SBhanu Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
61853e2bd2SBhanu Gollapudi 		bnx2fc_send_rrq(io_req);
62853e2bd2SBhanu Gollapudi 		return;
63853e2bd2SBhanu Gollapudi 	}
64853e2bd2SBhanu Gollapudi 	if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
65853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
66853e2bd2SBhanu Gollapudi 		goto done;
67853e2bd2SBhanu Gollapudi 	}
68853e2bd2SBhanu Gollapudi 
69853e2bd2SBhanu Gollapudi 	switch (cmd_type) {
70853e2bd2SBhanu Gollapudi 	case BNX2FC_SCSI_CMD:
71853e2bd2SBhanu Gollapudi 		if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
72853e2bd2SBhanu Gollapudi 							&io_req->req_flags)) {
73853e2bd2SBhanu Gollapudi 			/* Handle eh_abort timeout */
74853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
75853e2bd2SBhanu Gollapudi 			complete(&io_req->tm_done);
76853e2bd2SBhanu Gollapudi 		} else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
77853e2bd2SBhanu Gollapudi 				    &io_req->req_flags)) {
78853e2bd2SBhanu Gollapudi 			/* Handle internally generated ABTS timeout */
79853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
80853e2bd2SBhanu Gollapudi 					io_req->refcount.refcount.counter);
81853e2bd2SBhanu Gollapudi 			if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
82853e2bd2SBhanu Gollapudi 					       &io_req->req_flags))) {
83853e2bd2SBhanu Gollapudi 
84853e2bd2SBhanu Gollapudi 				lport = io_req->port->lport;
85853e2bd2SBhanu Gollapudi 				rdata = io_req->tgt->rdata;
86853e2bd2SBhanu Gollapudi 				logo_issued = test_and_set_bit(
87853e2bd2SBhanu Gollapudi 						BNX2FC_FLAG_EXPL_LOGO,
88853e2bd2SBhanu Gollapudi 						&tgt->flags);
89853e2bd2SBhanu Gollapudi 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
90853e2bd2SBhanu Gollapudi 				spin_unlock_bh(&tgt->tgt_lock);
91853e2bd2SBhanu Gollapudi 
92853e2bd2SBhanu Gollapudi 				/* Explicitly logo the target */
93853e2bd2SBhanu Gollapudi 				if (!logo_issued) {
94853e2bd2SBhanu Gollapudi 					BNX2FC_IO_DBG(io_req, "Explicit "
95853e2bd2SBhanu Gollapudi 						   "logo - tgt flags = 0x%lx\n",
96853e2bd2SBhanu Gollapudi 						   tgt->flags);
97853e2bd2SBhanu Gollapudi 
98853e2bd2SBhanu Gollapudi 					mutex_lock(&lport->disc.disc_mutex);
99853e2bd2SBhanu Gollapudi 					lport->tt.rport_logoff(rdata);
100853e2bd2SBhanu Gollapudi 					mutex_unlock(&lport->disc.disc_mutex);
101853e2bd2SBhanu Gollapudi 				}
102853e2bd2SBhanu Gollapudi 				return;
103853e2bd2SBhanu Gollapudi 			}
104853e2bd2SBhanu Gollapudi 		} else {
105853e2bd2SBhanu Gollapudi 			/* Hanlde IO timeout */
106853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
107853e2bd2SBhanu Gollapudi 			if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
108853e2bd2SBhanu Gollapudi 					     &io_req->req_flags)) {
109853e2bd2SBhanu Gollapudi 				BNX2FC_IO_DBG(io_req, "IO completed before "
110853e2bd2SBhanu Gollapudi 							   " timer expiry\n");
111853e2bd2SBhanu Gollapudi 				goto done;
112853e2bd2SBhanu Gollapudi 			}
113853e2bd2SBhanu Gollapudi 
114853e2bd2SBhanu Gollapudi 			if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
115853e2bd2SBhanu Gollapudi 					      &io_req->req_flags)) {
116853e2bd2SBhanu Gollapudi 				rc = bnx2fc_initiate_abts(io_req);
117853e2bd2SBhanu Gollapudi 				if (rc == SUCCESS)
118853e2bd2SBhanu Gollapudi 					goto done;
119853e2bd2SBhanu Gollapudi 				/*
120853e2bd2SBhanu Gollapudi 				 * Explicitly logo the target if
121853e2bd2SBhanu Gollapudi 				 * abts initiation fails
122853e2bd2SBhanu Gollapudi 				 */
123853e2bd2SBhanu Gollapudi 				lport = io_req->port->lport;
124853e2bd2SBhanu Gollapudi 				rdata = io_req->tgt->rdata;
125853e2bd2SBhanu Gollapudi 				logo_issued = test_and_set_bit(
126853e2bd2SBhanu Gollapudi 						BNX2FC_FLAG_EXPL_LOGO,
127853e2bd2SBhanu Gollapudi 						&tgt->flags);
128853e2bd2SBhanu Gollapudi 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
129853e2bd2SBhanu Gollapudi 				spin_unlock_bh(&tgt->tgt_lock);
130853e2bd2SBhanu Gollapudi 
131853e2bd2SBhanu Gollapudi 				if (!logo_issued) {
132853e2bd2SBhanu Gollapudi 					BNX2FC_IO_DBG(io_req, "Explicit "
133853e2bd2SBhanu Gollapudi 						   "logo - tgt flags = 0x%lx\n",
134853e2bd2SBhanu Gollapudi 						   tgt->flags);
135853e2bd2SBhanu Gollapudi 
136853e2bd2SBhanu Gollapudi 
137853e2bd2SBhanu Gollapudi 					mutex_lock(&lport->disc.disc_mutex);
138853e2bd2SBhanu Gollapudi 					lport->tt.rport_logoff(rdata);
139853e2bd2SBhanu Gollapudi 					mutex_unlock(&lport->disc.disc_mutex);
140853e2bd2SBhanu Gollapudi 				}
141853e2bd2SBhanu Gollapudi 				return;
142853e2bd2SBhanu Gollapudi 			} else {
143853e2bd2SBhanu Gollapudi 				BNX2FC_IO_DBG(io_req, "IO already in "
144853e2bd2SBhanu Gollapudi 						      "ABTS processing\n");
145853e2bd2SBhanu Gollapudi 			}
146853e2bd2SBhanu Gollapudi 		}
147853e2bd2SBhanu Gollapudi 		break;
148853e2bd2SBhanu Gollapudi 	case BNX2FC_ELS:
149853e2bd2SBhanu Gollapudi 
150853e2bd2SBhanu Gollapudi 		if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
151853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
152853e2bd2SBhanu Gollapudi 
153853e2bd2SBhanu Gollapudi 			if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
154853e2bd2SBhanu Gollapudi 					      &io_req->req_flags)) {
155853e2bd2SBhanu Gollapudi 				lport = io_req->port->lport;
156853e2bd2SBhanu Gollapudi 				rdata = io_req->tgt->rdata;
157853e2bd2SBhanu Gollapudi 				logo_issued = test_and_set_bit(
158853e2bd2SBhanu Gollapudi 						BNX2FC_FLAG_EXPL_LOGO,
159853e2bd2SBhanu Gollapudi 						&tgt->flags);
160853e2bd2SBhanu Gollapudi 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
161853e2bd2SBhanu Gollapudi 				spin_unlock_bh(&tgt->tgt_lock);
162853e2bd2SBhanu Gollapudi 
163853e2bd2SBhanu Gollapudi 				/* Explicitly logo the target */
164853e2bd2SBhanu Gollapudi 				if (!logo_issued) {
165853e2bd2SBhanu Gollapudi 					BNX2FC_IO_DBG(io_req, "Explicitly logo"
166853e2bd2SBhanu Gollapudi 						   "(els)\n");
167853e2bd2SBhanu Gollapudi 					mutex_lock(&lport->disc.disc_mutex);
168853e2bd2SBhanu Gollapudi 					lport->tt.rport_logoff(rdata);
169853e2bd2SBhanu Gollapudi 					mutex_unlock(&lport->disc.disc_mutex);
170853e2bd2SBhanu Gollapudi 				}
171853e2bd2SBhanu Gollapudi 				return;
172853e2bd2SBhanu Gollapudi 			}
173853e2bd2SBhanu Gollapudi 		} else {
174853e2bd2SBhanu Gollapudi 			/*
175853e2bd2SBhanu Gollapudi 			 * Handle ELS timeout.
176853e2bd2SBhanu Gollapudi 			 * tgt_lock is used to sync compl path and timeout
177853e2bd2SBhanu Gollapudi 			 * path. If els compl path is processing this IO, we
178853e2bd2SBhanu Gollapudi 			 * have nothing to do here, just release the timer hold
179853e2bd2SBhanu Gollapudi 			 */
180853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "ELS timed out\n");
181853e2bd2SBhanu Gollapudi 			if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
182853e2bd2SBhanu Gollapudi 					       &io_req->req_flags))
183853e2bd2SBhanu Gollapudi 				goto done;
184853e2bd2SBhanu Gollapudi 
185853e2bd2SBhanu Gollapudi 			/* Indicate the cb_func that this ELS is timed out */
186853e2bd2SBhanu Gollapudi 			set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
187853e2bd2SBhanu Gollapudi 
188853e2bd2SBhanu Gollapudi 			if ((io_req->cb_func) && (io_req->cb_arg)) {
189853e2bd2SBhanu Gollapudi 				io_req->cb_func(io_req->cb_arg);
190853e2bd2SBhanu Gollapudi 				io_req->cb_arg = NULL;
191853e2bd2SBhanu Gollapudi 			}
192853e2bd2SBhanu Gollapudi 		}
193853e2bd2SBhanu Gollapudi 		break;
194853e2bd2SBhanu Gollapudi 	default:
195853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
196853e2bd2SBhanu Gollapudi 			cmd_type);
197853e2bd2SBhanu Gollapudi 		break;
198853e2bd2SBhanu Gollapudi 	}
199853e2bd2SBhanu Gollapudi 
200853e2bd2SBhanu Gollapudi done:
201853e2bd2SBhanu Gollapudi 	/* release the cmd that was held when timer was set */
202853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
203853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
204853e2bd2SBhanu Gollapudi }
205853e2bd2SBhanu Gollapudi 
206853e2bd2SBhanu Gollapudi static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
207853e2bd2SBhanu Gollapudi {
208853e2bd2SBhanu Gollapudi 	/* Called with host lock held */
209853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
210853e2bd2SBhanu Gollapudi 
211853e2bd2SBhanu Gollapudi 	/*
212853e2bd2SBhanu Gollapudi 	 * active_cmd_queue may have other command types as well,
213853e2bd2SBhanu Gollapudi 	 * and during flush operation,  we want to error back only
214853e2bd2SBhanu Gollapudi 	 * scsi commands.
215853e2bd2SBhanu Gollapudi 	 */
216853e2bd2SBhanu Gollapudi 	if (io_req->cmd_type != BNX2FC_SCSI_CMD)
217853e2bd2SBhanu Gollapudi 		return;
218853e2bd2SBhanu Gollapudi 
219853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
22074446954SBhanu Prakash Gollapudi 	if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
22174446954SBhanu Prakash Gollapudi 		/* Do not call scsi done for this IO */
22274446954SBhanu Prakash Gollapudi 		return;
22374446954SBhanu Prakash Gollapudi 	}
22474446954SBhanu Prakash Gollapudi 
225853e2bd2SBhanu Gollapudi 	bnx2fc_unmap_sg_list(io_req);
226853e2bd2SBhanu Gollapudi 	io_req->sc_cmd = NULL;
227853e2bd2SBhanu Gollapudi 	if (!sc_cmd) {
228853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
229853e2bd2SBhanu Gollapudi 				    "IO(0x%x) already cleaned up\n",
230853e2bd2SBhanu Gollapudi 		       io_req->xid);
231853e2bd2SBhanu Gollapudi 		return;
232853e2bd2SBhanu Gollapudi 	}
233853e2bd2SBhanu Gollapudi 	sc_cmd->result = err_code << 16;
234853e2bd2SBhanu Gollapudi 
235853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
236853e2bd2SBhanu Gollapudi 		sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
237853e2bd2SBhanu Gollapudi 		sc_cmd->allowed);
238853e2bd2SBhanu Gollapudi 	scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
239853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = NULL;
240853e2bd2SBhanu Gollapudi 	sc_cmd->scsi_done(sc_cmd);
241853e2bd2SBhanu Gollapudi }
242853e2bd2SBhanu Gollapudi 
2430eb43b4bSBhanu Prakash Gollapudi struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
244853e2bd2SBhanu Gollapudi {
245853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd_mgr *cmgr;
246853e2bd2SBhanu Gollapudi 	struct io_bdt *bdt_info;
247853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
248853e2bd2SBhanu Gollapudi 	size_t len;
249853e2bd2SBhanu Gollapudi 	u32 mem_size;
250853e2bd2SBhanu Gollapudi 	u16 xid;
251853e2bd2SBhanu Gollapudi 	int i;
2520ea5c275SBhanu Gollapudi 	int num_ios, num_pri_ios;
253853e2bd2SBhanu Gollapudi 	size_t bd_tbl_sz;
2540ea5c275SBhanu Gollapudi 	int arr_sz = num_possible_cpus() + 1;
2550eb43b4bSBhanu Prakash Gollapudi 	u16 min_xid = BNX2FC_MIN_XID;
2560eb43b4bSBhanu Prakash Gollapudi 	u16 max_xid = hba->max_xid;
257853e2bd2SBhanu Gollapudi 
258853e2bd2SBhanu Gollapudi 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
259853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
260853e2bd2SBhanu Gollapudi 					and max_xid 0x%x\n", min_xid, max_xid);
261853e2bd2SBhanu Gollapudi 		return NULL;
262853e2bd2SBhanu Gollapudi 	}
263853e2bd2SBhanu Gollapudi 	BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
264853e2bd2SBhanu Gollapudi 
265853e2bd2SBhanu Gollapudi 	num_ios = max_xid - min_xid + 1;
266853e2bd2SBhanu Gollapudi 	len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
267853e2bd2SBhanu Gollapudi 	len += sizeof(struct bnx2fc_cmd_mgr);
268853e2bd2SBhanu Gollapudi 
269853e2bd2SBhanu Gollapudi 	cmgr = kzalloc(len, GFP_KERNEL);
270853e2bd2SBhanu Gollapudi 	if (!cmgr) {
271853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "failed to alloc cmgr\n");
272853e2bd2SBhanu Gollapudi 		return NULL;
273853e2bd2SBhanu Gollapudi 	}
274853e2bd2SBhanu Gollapudi 
275853e2bd2SBhanu Gollapudi 	cmgr->free_list = kzalloc(sizeof(*cmgr->free_list) *
2760ea5c275SBhanu Gollapudi 				  arr_sz, GFP_KERNEL);
277853e2bd2SBhanu Gollapudi 	if (!cmgr->free_list) {
278853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "failed to alloc free_list\n");
279853e2bd2SBhanu Gollapudi 		goto mem_err;
280853e2bd2SBhanu Gollapudi 	}
281853e2bd2SBhanu Gollapudi 
282853e2bd2SBhanu Gollapudi 	cmgr->free_list_lock = kzalloc(sizeof(*cmgr->free_list_lock) *
2830ea5c275SBhanu Gollapudi 				       arr_sz, GFP_KERNEL);
284853e2bd2SBhanu Gollapudi 	if (!cmgr->free_list_lock) {
285853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
2869172b763SMaurizio Lombardi 		kfree(cmgr->free_list);
2879172b763SMaurizio Lombardi 		cmgr->free_list = NULL;
288853e2bd2SBhanu Gollapudi 		goto mem_err;
289853e2bd2SBhanu Gollapudi 	}
290853e2bd2SBhanu Gollapudi 
291853e2bd2SBhanu Gollapudi 	cmgr->hba = hba;
292853e2bd2SBhanu Gollapudi 	cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
293853e2bd2SBhanu Gollapudi 
2940ea5c275SBhanu Gollapudi 	for (i = 0; i < arr_sz; i++)  {
295853e2bd2SBhanu Gollapudi 		INIT_LIST_HEAD(&cmgr->free_list[i]);
296853e2bd2SBhanu Gollapudi 		spin_lock_init(&cmgr->free_list_lock[i]);
297853e2bd2SBhanu Gollapudi 	}
298853e2bd2SBhanu Gollapudi 
2990ea5c275SBhanu Gollapudi 	/*
3000ea5c275SBhanu Gollapudi 	 * Pre-allocated pool of bnx2fc_cmds.
3010ea5c275SBhanu Gollapudi 	 * Last entry in the free list array is the free list
3020ea5c275SBhanu Gollapudi 	 * of slow path requests.
3030ea5c275SBhanu Gollapudi 	 */
304853e2bd2SBhanu Gollapudi 	xid = BNX2FC_MIN_XID;
3050eb43b4bSBhanu Prakash Gollapudi 	num_pri_ios = num_ios - hba->elstm_xids;
306853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
307853e2bd2SBhanu Gollapudi 		io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
308853e2bd2SBhanu Gollapudi 
309853e2bd2SBhanu Gollapudi 		if (!io_req) {
310853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "failed to alloc io_req\n");
311853e2bd2SBhanu Gollapudi 			goto mem_err;
312853e2bd2SBhanu Gollapudi 		}
313853e2bd2SBhanu Gollapudi 
314853e2bd2SBhanu Gollapudi 		INIT_LIST_HEAD(&io_req->link);
315853e2bd2SBhanu Gollapudi 		INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
316853e2bd2SBhanu Gollapudi 
317853e2bd2SBhanu Gollapudi 		io_req->xid = xid++;
3180ea5c275SBhanu Gollapudi 		if (i < num_pri_ios)
319853e2bd2SBhanu Gollapudi 			list_add_tail(&io_req->link,
3200ea5c275SBhanu Gollapudi 				&cmgr->free_list[io_req->xid %
3210ea5c275SBhanu Gollapudi 						 num_possible_cpus()]);
3220ea5c275SBhanu Gollapudi 		else
3230ea5c275SBhanu Gollapudi 			list_add_tail(&io_req->link,
3240ea5c275SBhanu Gollapudi 				&cmgr->free_list[num_possible_cpus()]);
325853e2bd2SBhanu Gollapudi 		io_req++;
326853e2bd2SBhanu Gollapudi 	}
327853e2bd2SBhanu Gollapudi 
328853e2bd2SBhanu Gollapudi 	/* Allocate pool of io_bdts - one for each bnx2fc_cmd */
329853e2bd2SBhanu Gollapudi 	mem_size = num_ios * sizeof(struct io_bdt *);
330853e2bd2SBhanu Gollapudi 	cmgr->io_bdt_pool = kmalloc(mem_size, GFP_KERNEL);
331853e2bd2SBhanu Gollapudi 	if (!cmgr->io_bdt_pool) {
332853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
333853e2bd2SBhanu Gollapudi 		goto mem_err;
334853e2bd2SBhanu Gollapudi 	}
335853e2bd2SBhanu Gollapudi 
336853e2bd2SBhanu Gollapudi 	mem_size = sizeof(struct io_bdt);
337853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
338853e2bd2SBhanu Gollapudi 		cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
339853e2bd2SBhanu Gollapudi 		if (!cmgr->io_bdt_pool[i]) {
340853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "failed to alloc "
341853e2bd2SBhanu Gollapudi 				"io_bdt_pool[%d]\n", i);
342853e2bd2SBhanu Gollapudi 			goto mem_err;
343853e2bd2SBhanu Gollapudi 		}
344853e2bd2SBhanu Gollapudi 	}
345853e2bd2SBhanu Gollapudi 
346853e2bd2SBhanu Gollapudi 	/* Allocate an map fcoe_bdt_ctx structures */
347853e2bd2SBhanu Gollapudi 	bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
348853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
349853e2bd2SBhanu Gollapudi 		bdt_info = cmgr->io_bdt_pool[i];
350853e2bd2SBhanu Gollapudi 		bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
351853e2bd2SBhanu Gollapudi 						      bd_tbl_sz,
352853e2bd2SBhanu Gollapudi 						      &bdt_info->bd_tbl_dma,
353853e2bd2SBhanu Gollapudi 						      GFP_KERNEL);
354853e2bd2SBhanu Gollapudi 		if (!bdt_info->bd_tbl) {
355853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "failed to alloc "
356853e2bd2SBhanu Gollapudi 				"bdt_tbl[%d]\n", i);
357853e2bd2SBhanu Gollapudi 			goto mem_err;
358853e2bd2SBhanu Gollapudi 		}
359853e2bd2SBhanu Gollapudi 	}
360853e2bd2SBhanu Gollapudi 
361853e2bd2SBhanu Gollapudi 	return cmgr;
362853e2bd2SBhanu Gollapudi 
363853e2bd2SBhanu Gollapudi mem_err:
364853e2bd2SBhanu Gollapudi 	bnx2fc_cmd_mgr_free(cmgr);
365853e2bd2SBhanu Gollapudi 	return NULL;
366853e2bd2SBhanu Gollapudi }
367853e2bd2SBhanu Gollapudi 
368853e2bd2SBhanu Gollapudi void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
369853e2bd2SBhanu Gollapudi {
370853e2bd2SBhanu Gollapudi 	struct io_bdt *bdt_info;
371853e2bd2SBhanu Gollapudi 	struct bnx2fc_hba *hba = cmgr->hba;
372853e2bd2SBhanu Gollapudi 	size_t bd_tbl_sz;
373853e2bd2SBhanu Gollapudi 	u16 min_xid = BNX2FC_MIN_XID;
3740eb43b4bSBhanu Prakash Gollapudi 	u16 max_xid = hba->max_xid;
375853e2bd2SBhanu Gollapudi 	int num_ios;
376853e2bd2SBhanu Gollapudi 	int i;
377853e2bd2SBhanu Gollapudi 
378853e2bd2SBhanu Gollapudi 	num_ios = max_xid - min_xid + 1;
379853e2bd2SBhanu Gollapudi 
380853e2bd2SBhanu Gollapudi 	/* Free fcoe_bdt_ctx structures */
381853e2bd2SBhanu Gollapudi 	if (!cmgr->io_bdt_pool)
382853e2bd2SBhanu Gollapudi 		goto free_cmd_pool;
383853e2bd2SBhanu Gollapudi 
384853e2bd2SBhanu Gollapudi 	bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
385853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
386853e2bd2SBhanu Gollapudi 		bdt_info = cmgr->io_bdt_pool[i];
387853e2bd2SBhanu Gollapudi 		if (bdt_info->bd_tbl) {
388853e2bd2SBhanu Gollapudi 			dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
389853e2bd2SBhanu Gollapudi 					    bdt_info->bd_tbl,
390853e2bd2SBhanu Gollapudi 					    bdt_info->bd_tbl_dma);
391853e2bd2SBhanu Gollapudi 			bdt_info->bd_tbl = NULL;
392853e2bd2SBhanu Gollapudi 		}
393853e2bd2SBhanu Gollapudi 	}
394853e2bd2SBhanu Gollapudi 
395853e2bd2SBhanu Gollapudi 	/* Destroy io_bdt pool */
396853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
397853e2bd2SBhanu Gollapudi 		kfree(cmgr->io_bdt_pool[i]);
398853e2bd2SBhanu Gollapudi 		cmgr->io_bdt_pool[i] = NULL;
399853e2bd2SBhanu Gollapudi 	}
400853e2bd2SBhanu Gollapudi 
401853e2bd2SBhanu Gollapudi 	kfree(cmgr->io_bdt_pool);
402853e2bd2SBhanu Gollapudi 	cmgr->io_bdt_pool = NULL;
403853e2bd2SBhanu Gollapudi 
404853e2bd2SBhanu Gollapudi free_cmd_pool:
405853e2bd2SBhanu Gollapudi 	kfree(cmgr->free_list_lock);
406853e2bd2SBhanu Gollapudi 
407853e2bd2SBhanu Gollapudi 	/* Destroy cmd pool */
408853e2bd2SBhanu Gollapudi 	if (!cmgr->free_list)
409853e2bd2SBhanu Gollapudi 		goto free_cmgr;
410853e2bd2SBhanu Gollapudi 
4110ea5c275SBhanu Gollapudi 	for (i = 0; i < num_possible_cpus() + 1; i++)  {
412d71fb3bdSBhanu Prakash Gollapudi 		struct bnx2fc_cmd *tmp, *io_req;
413853e2bd2SBhanu Gollapudi 
414d71fb3bdSBhanu Prakash Gollapudi 		list_for_each_entry_safe(io_req, tmp,
415d71fb3bdSBhanu Prakash Gollapudi 					 &cmgr->free_list[i], link) {
416853e2bd2SBhanu Gollapudi 			list_del(&io_req->link);
417853e2bd2SBhanu Gollapudi 			kfree(io_req);
418853e2bd2SBhanu Gollapudi 		}
419853e2bd2SBhanu Gollapudi 	}
420853e2bd2SBhanu Gollapudi 	kfree(cmgr->free_list);
421853e2bd2SBhanu Gollapudi free_cmgr:
422853e2bd2SBhanu Gollapudi 	/* Free command manager itself */
423853e2bd2SBhanu Gollapudi 	kfree(cmgr);
424853e2bd2SBhanu Gollapudi }
425853e2bd2SBhanu Gollapudi 
426853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
427853e2bd2SBhanu Gollapudi {
428853e2bd2SBhanu Gollapudi 	struct fcoe_port *port = tgt->port;
429aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = port->priv;
430aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
431853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
432853e2bd2SBhanu Gollapudi 	struct list_head *listp;
433853e2bd2SBhanu Gollapudi 	struct io_bdt *bd_tbl;
4340ea5c275SBhanu Gollapudi 	int index = RESERVE_FREE_LIST_INDEX;
435619c5cb6SVlad Zolotarov 	u32 free_sqes;
436853e2bd2SBhanu Gollapudi 	u32 max_sqes;
437853e2bd2SBhanu Gollapudi 	u16 xid;
438853e2bd2SBhanu Gollapudi 
439853e2bd2SBhanu Gollapudi 	max_sqes = tgt->max_sqes;
440853e2bd2SBhanu Gollapudi 	switch (type) {
441853e2bd2SBhanu Gollapudi 	case BNX2FC_TASK_MGMT_CMD:
442853e2bd2SBhanu Gollapudi 		max_sqes = BNX2FC_TM_MAX_SQES;
443853e2bd2SBhanu Gollapudi 		break;
444853e2bd2SBhanu Gollapudi 	case BNX2FC_ELS:
445853e2bd2SBhanu Gollapudi 		max_sqes = BNX2FC_ELS_MAX_SQES;
446853e2bd2SBhanu Gollapudi 		break;
447853e2bd2SBhanu Gollapudi 	default:
448853e2bd2SBhanu Gollapudi 		break;
449853e2bd2SBhanu Gollapudi 	}
450853e2bd2SBhanu Gollapudi 
451853e2bd2SBhanu Gollapudi 	/*
452853e2bd2SBhanu Gollapudi 	 * NOTE: Free list insertions and deletions are protected with
453853e2bd2SBhanu Gollapudi 	 * cmgr lock
454853e2bd2SBhanu Gollapudi 	 */
4550ea5c275SBhanu Gollapudi 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
456619c5cb6SVlad Zolotarov 	free_sqes = atomic_read(&tgt->free_sqes);
4570ea5c275SBhanu Gollapudi 	if ((list_empty(&(cmd_mgr->free_list[index]))) ||
458619c5cb6SVlad Zolotarov 	    (tgt->num_active_ios.counter  >= max_sqes) ||
459619c5cb6SVlad Zolotarov 	    (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
460853e2bd2SBhanu Gollapudi 		BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
461853e2bd2SBhanu Gollapudi 			"ios(%d):sqes(%d)\n",
462853e2bd2SBhanu Gollapudi 			tgt->num_active_ios.counter, tgt->max_sqes);
4630ea5c275SBhanu Gollapudi 		if (list_empty(&(cmd_mgr->free_list[index])))
464853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
4650ea5c275SBhanu Gollapudi 		spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
466853e2bd2SBhanu Gollapudi 		return NULL;
467853e2bd2SBhanu Gollapudi 	}
468853e2bd2SBhanu Gollapudi 
469853e2bd2SBhanu Gollapudi 	listp = (struct list_head *)
4700ea5c275SBhanu Gollapudi 			cmd_mgr->free_list[index].next;
471853e2bd2SBhanu Gollapudi 	list_del_init(listp);
472853e2bd2SBhanu Gollapudi 	io_req = (struct bnx2fc_cmd *) listp;
473853e2bd2SBhanu Gollapudi 	xid = io_req->xid;
474853e2bd2SBhanu Gollapudi 	cmd_mgr->cmds[xid] = io_req;
475853e2bd2SBhanu Gollapudi 	atomic_inc(&tgt->num_active_ios);
476619c5cb6SVlad Zolotarov 	atomic_dec(&tgt->free_sqes);
4770ea5c275SBhanu Gollapudi 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
478853e2bd2SBhanu Gollapudi 
479853e2bd2SBhanu Gollapudi 	INIT_LIST_HEAD(&io_req->link);
480853e2bd2SBhanu Gollapudi 
481853e2bd2SBhanu Gollapudi 	io_req->port = port;
482853e2bd2SBhanu Gollapudi 	io_req->cmd_mgr = cmd_mgr;
483853e2bd2SBhanu Gollapudi 	io_req->req_flags = 0;
484853e2bd2SBhanu Gollapudi 	io_req->cmd_type = type;
485853e2bd2SBhanu Gollapudi 
486853e2bd2SBhanu Gollapudi 	/* Bind io_bdt for this io_req */
487853e2bd2SBhanu Gollapudi 	/* Have a static link between io_req and io_bdt_pool */
488853e2bd2SBhanu Gollapudi 	bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
489853e2bd2SBhanu Gollapudi 	bd_tbl->io_req = io_req;
490853e2bd2SBhanu Gollapudi 
491853e2bd2SBhanu Gollapudi 	/* Hold the io_req  against deletion */
492853e2bd2SBhanu Gollapudi 	kref_init(&io_req->refcount);
493853e2bd2SBhanu Gollapudi 	return io_req;
494853e2bd2SBhanu Gollapudi }
495aea71a02SBhanu Prakash Gollapudi 
496aea71a02SBhanu Prakash Gollapudi struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
497853e2bd2SBhanu Gollapudi {
498853e2bd2SBhanu Gollapudi 	struct fcoe_port *port = tgt->port;
499aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = port->priv;
500aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
501853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
502853e2bd2SBhanu Gollapudi 	struct list_head *listp;
503853e2bd2SBhanu Gollapudi 	struct io_bdt *bd_tbl;
504619c5cb6SVlad Zolotarov 	u32 free_sqes;
505853e2bd2SBhanu Gollapudi 	u32 max_sqes;
506853e2bd2SBhanu Gollapudi 	u16 xid;
5070ea5c275SBhanu Gollapudi 	int index = get_cpu();
508853e2bd2SBhanu Gollapudi 
509853e2bd2SBhanu Gollapudi 	max_sqes = BNX2FC_SCSI_MAX_SQES;
510853e2bd2SBhanu Gollapudi 	/*
511853e2bd2SBhanu Gollapudi 	 * NOTE: Free list insertions and deletions are protected with
512853e2bd2SBhanu Gollapudi 	 * cmgr lock
513853e2bd2SBhanu Gollapudi 	 */
5140ea5c275SBhanu Gollapudi 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
515619c5cb6SVlad Zolotarov 	free_sqes = atomic_read(&tgt->free_sqes);
5160ea5c275SBhanu Gollapudi 	if ((list_empty(&cmd_mgr->free_list[index])) ||
517619c5cb6SVlad Zolotarov 	    (tgt->num_active_ios.counter  >= max_sqes) ||
518619c5cb6SVlad Zolotarov 	    (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
5190ea5c275SBhanu Gollapudi 		spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
5200ea5c275SBhanu Gollapudi 		put_cpu();
521853e2bd2SBhanu Gollapudi 		return NULL;
522853e2bd2SBhanu Gollapudi 	}
523853e2bd2SBhanu Gollapudi 
524853e2bd2SBhanu Gollapudi 	listp = (struct list_head *)
5250ea5c275SBhanu Gollapudi 		cmd_mgr->free_list[index].next;
526853e2bd2SBhanu Gollapudi 	list_del_init(listp);
527853e2bd2SBhanu Gollapudi 	io_req = (struct bnx2fc_cmd *) listp;
528853e2bd2SBhanu Gollapudi 	xid = io_req->xid;
529853e2bd2SBhanu Gollapudi 	cmd_mgr->cmds[xid] = io_req;
530853e2bd2SBhanu Gollapudi 	atomic_inc(&tgt->num_active_ios);
531619c5cb6SVlad Zolotarov 	atomic_dec(&tgt->free_sqes);
5320ea5c275SBhanu Gollapudi 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
5330ea5c275SBhanu Gollapudi 	put_cpu();
534853e2bd2SBhanu Gollapudi 
535853e2bd2SBhanu Gollapudi 	INIT_LIST_HEAD(&io_req->link);
536853e2bd2SBhanu Gollapudi 
537853e2bd2SBhanu Gollapudi 	io_req->port = port;
538853e2bd2SBhanu Gollapudi 	io_req->cmd_mgr = cmd_mgr;
539853e2bd2SBhanu Gollapudi 	io_req->req_flags = 0;
540853e2bd2SBhanu Gollapudi 
541853e2bd2SBhanu Gollapudi 	/* Bind io_bdt for this io_req */
542853e2bd2SBhanu Gollapudi 	/* Have a static link between io_req and io_bdt_pool */
543853e2bd2SBhanu Gollapudi 	bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
544853e2bd2SBhanu Gollapudi 	bd_tbl->io_req = io_req;
545853e2bd2SBhanu Gollapudi 
546853e2bd2SBhanu Gollapudi 	/* Hold the io_req  against deletion */
547853e2bd2SBhanu Gollapudi 	kref_init(&io_req->refcount);
548853e2bd2SBhanu Gollapudi 	return io_req;
549853e2bd2SBhanu Gollapudi }
550853e2bd2SBhanu Gollapudi 
551853e2bd2SBhanu Gollapudi void bnx2fc_cmd_release(struct kref *ref)
552853e2bd2SBhanu Gollapudi {
553853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req = container_of(ref,
554853e2bd2SBhanu Gollapudi 						struct bnx2fc_cmd, refcount);
555853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
5560ea5c275SBhanu Gollapudi 	int index;
557853e2bd2SBhanu Gollapudi 
5580ea5c275SBhanu Gollapudi 	if (io_req->cmd_type == BNX2FC_SCSI_CMD)
5590ea5c275SBhanu Gollapudi 		index = io_req->xid % num_possible_cpus();
5600ea5c275SBhanu Gollapudi 	else
5610ea5c275SBhanu Gollapudi 		index = RESERVE_FREE_LIST_INDEX;
5620ea5c275SBhanu Gollapudi 
5630ea5c275SBhanu Gollapudi 
5640ea5c275SBhanu Gollapudi 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
565853e2bd2SBhanu Gollapudi 	if (io_req->cmd_type != BNX2FC_SCSI_CMD)
566853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
567853e2bd2SBhanu Gollapudi 	cmd_mgr->cmds[io_req->xid] = NULL;
568853e2bd2SBhanu Gollapudi 	/* Delete IO from retire queue */
569853e2bd2SBhanu Gollapudi 	list_del_init(&io_req->link);
570853e2bd2SBhanu Gollapudi 	/* Add it to the free list */
571853e2bd2SBhanu Gollapudi 	list_add(&io_req->link,
5720ea5c275SBhanu Gollapudi 			&cmd_mgr->free_list[index]);
573853e2bd2SBhanu Gollapudi 	atomic_dec(&io_req->tgt->num_active_ios);
5740ea5c275SBhanu Gollapudi 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
5750ea5c275SBhanu Gollapudi 
576853e2bd2SBhanu Gollapudi }
577853e2bd2SBhanu Gollapudi 
578853e2bd2SBhanu Gollapudi static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
579853e2bd2SBhanu Gollapudi {
580853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
581aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = io_req->port->priv;
582aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_hba *hba = interface->hba;
583853e2bd2SBhanu Gollapudi 	size_t sz = sizeof(struct fcoe_bd_ctx);
584853e2bd2SBhanu Gollapudi 
585853e2bd2SBhanu Gollapudi 	/* clear tm flags */
586853e2bd2SBhanu Gollapudi 	mp_req->tm_flags = 0;
587853e2bd2SBhanu Gollapudi 	if (mp_req->mp_req_bd) {
588853e2bd2SBhanu Gollapudi 		dma_free_coherent(&hba->pcidev->dev, sz,
589853e2bd2SBhanu Gollapudi 				     mp_req->mp_req_bd,
590853e2bd2SBhanu Gollapudi 				     mp_req->mp_req_bd_dma);
591853e2bd2SBhanu Gollapudi 		mp_req->mp_req_bd = NULL;
592853e2bd2SBhanu Gollapudi 	}
593853e2bd2SBhanu Gollapudi 	if (mp_req->mp_resp_bd) {
594853e2bd2SBhanu Gollapudi 		dma_free_coherent(&hba->pcidev->dev, sz,
595853e2bd2SBhanu Gollapudi 				     mp_req->mp_resp_bd,
596853e2bd2SBhanu Gollapudi 				     mp_req->mp_resp_bd_dma);
597853e2bd2SBhanu Gollapudi 		mp_req->mp_resp_bd = NULL;
598853e2bd2SBhanu Gollapudi 	}
599853e2bd2SBhanu Gollapudi 	if (mp_req->req_buf) {
600be1fefc2SMichael Chan 		dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
601853e2bd2SBhanu Gollapudi 				     mp_req->req_buf,
602853e2bd2SBhanu Gollapudi 				     mp_req->req_buf_dma);
603853e2bd2SBhanu Gollapudi 		mp_req->req_buf = NULL;
604853e2bd2SBhanu Gollapudi 	}
605853e2bd2SBhanu Gollapudi 	if (mp_req->resp_buf) {
606be1fefc2SMichael Chan 		dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
607853e2bd2SBhanu Gollapudi 				     mp_req->resp_buf,
608853e2bd2SBhanu Gollapudi 				     mp_req->resp_buf_dma);
609853e2bd2SBhanu Gollapudi 		mp_req->resp_buf = NULL;
610853e2bd2SBhanu Gollapudi 	}
611853e2bd2SBhanu Gollapudi }
612853e2bd2SBhanu Gollapudi 
613853e2bd2SBhanu Gollapudi int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
614853e2bd2SBhanu Gollapudi {
615853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *mp_req;
616853e2bd2SBhanu Gollapudi 	struct fcoe_bd_ctx *mp_req_bd;
617853e2bd2SBhanu Gollapudi 	struct fcoe_bd_ctx *mp_resp_bd;
618aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = io_req->port->priv;
619aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_hba *hba = interface->hba;
620853e2bd2SBhanu Gollapudi 	dma_addr_t addr;
621853e2bd2SBhanu Gollapudi 	size_t sz;
622853e2bd2SBhanu Gollapudi 
623853e2bd2SBhanu Gollapudi 	mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
624853e2bd2SBhanu Gollapudi 	memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
625853e2bd2SBhanu Gollapudi 
6261fffa199SChad Dupuis 	if (io_req->cmd_type != BNX2FC_ELS) {
627853e2bd2SBhanu Gollapudi 		mp_req->req_len = sizeof(struct fcp_cmnd);
628853e2bd2SBhanu Gollapudi 		io_req->data_xfer_len = mp_req->req_len;
6291fffa199SChad Dupuis 	} else
6301fffa199SChad Dupuis 		mp_req->req_len = io_req->data_xfer_len;
6311fffa199SChad Dupuis 
632be1fefc2SMichael Chan 	mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
633853e2bd2SBhanu Gollapudi 					     &mp_req->req_buf_dma,
634853e2bd2SBhanu Gollapudi 					     GFP_ATOMIC);
635853e2bd2SBhanu Gollapudi 	if (!mp_req->req_buf) {
636853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
637853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
638853e2bd2SBhanu Gollapudi 		return FAILED;
639853e2bd2SBhanu Gollapudi 	}
640853e2bd2SBhanu Gollapudi 
641be1fefc2SMichael Chan 	mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
642853e2bd2SBhanu Gollapudi 					      &mp_req->resp_buf_dma,
643853e2bd2SBhanu Gollapudi 					      GFP_ATOMIC);
644853e2bd2SBhanu Gollapudi 	if (!mp_req->resp_buf) {
645853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
646853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
647853e2bd2SBhanu Gollapudi 		return FAILED;
648853e2bd2SBhanu Gollapudi 	}
649be1fefc2SMichael Chan 	memset(mp_req->req_buf, 0, CNIC_PAGE_SIZE);
650be1fefc2SMichael Chan 	memset(mp_req->resp_buf, 0, CNIC_PAGE_SIZE);
651853e2bd2SBhanu Gollapudi 
652853e2bd2SBhanu Gollapudi 	/* Allocate and map mp_req_bd and mp_resp_bd */
653853e2bd2SBhanu Gollapudi 	sz = sizeof(struct fcoe_bd_ctx);
654853e2bd2SBhanu Gollapudi 	mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
655853e2bd2SBhanu Gollapudi 						 &mp_req->mp_req_bd_dma,
656853e2bd2SBhanu Gollapudi 						 GFP_ATOMIC);
657853e2bd2SBhanu Gollapudi 	if (!mp_req->mp_req_bd) {
658853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "unable to alloc MP req bd\n");
659853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
660853e2bd2SBhanu Gollapudi 		return FAILED;
661853e2bd2SBhanu Gollapudi 	}
662853e2bd2SBhanu Gollapudi 	mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
663853e2bd2SBhanu Gollapudi 						 &mp_req->mp_resp_bd_dma,
664853e2bd2SBhanu Gollapudi 						 GFP_ATOMIC);
665b0d5e15cSJulia Lawall 	if (!mp_req->mp_resp_bd) {
666853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
667853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
668853e2bd2SBhanu Gollapudi 		return FAILED;
669853e2bd2SBhanu Gollapudi 	}
670853e2bd2SBhanu Gollapudi 	/* Fill bd table */
671853e2bd2SBhanu Gollapudi 	addr = mp_req->req_buf_dma;
672853e2bd2SBhanu Gollapudi 	mp_req_bd = mp_req->mp_req_bd;
673853e2bd2SBhanu Gollapudi 	mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
674853e2bd2SBhanu Gollapudi 	mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
675be1fefc2SMichael Chan 	mp_req_bd->buf_len = CNIC_PAGE_SIZE;
676853e2bd2SBhanu Gollapudi 	mp_req_bd->flags = 0;
677853e2bd2SBhanu Gollapudi 
678853e2bd2SBhanu Gollapudi 	/*
679853e2bd2SBhanu Gollapudi 	 * MP buffer is either a task mgmt command or an ELS.
680853e2bd2SBhanu Gollapudi 	 * So the assumption is that it consumes a single bd
681853e2bd2SBhanu Gollapudi 	 * entry in the bd table
682853e2bd2SBhanu Gollapudi 	 */
683853e2bd2SBhanu Gollapudi 	mp_resp_bd = mp_req->mp_resp_bd;
684853e2bd2SBhanu Gollapudi 	addr = mp_req->resp_buf_dma;
685853e2bd2SBhanu Gollapudi 	mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
686853e2bd2SBhanu Gollapudi 	mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
687be1fefc2SMichael Chan 	mp_resp_bd->buf_len = CNIC_PAGE_SIZE;
688853e2bd2SBhanu Gollapudi 	mp_resp_bd->flags = 0;
689853e2bd2SBhanu Gollapudi 
690853e2bd2SBhanu Gollapudi 	return SUCCESS;
691853e2bd2SBhanu Gollapudi }
692853e2bd2SBhanu Gollapudi 
693853e2bd2SBhanu Gollapudi static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
694853e2bd2SBhanu Gollapudi {
695853e2bd2SBhanu Gollapudi 	struct fc_lport *lport;
69633c7da05SJulia Lawall 	struct fc_rport *rport;
69733c7da05SJulia Lawall 	struct fc_rport_libfc_priv *rp;
698853e2bd2SBhanu Gollapudi 	struct fcoe_port *port;
699aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface;
700853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt;
701853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
702853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *tm_req;
703853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task;
704853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task_page;
705853e2bd2SBhanu Gollapudi 	struct Scsi_Host *host = sc_cmd->device->host;
706853e2bd2SBhanu Gollapudi 	struct fc_frame_header *fc_hdr;
707853e2bd2SBhanu Gollapudi 	struct fcp_cmnd *fcp_cmnd;
708853e2bd2SBhanu Gollapudi 	int task_idx, index;
709853e2bd2SBhanu Gollapudi 	int rc = SUCCESS;
710853e2bd2SBhanu Gollapudi 	u16 xid;
711853e2bd2SBhanu Gollapudi 	u32 sid, did;
712853e2bd2SBhanu Gollapudi 	unsigned long start = jiffies;
713853e2bd2SBhanu Gollapudi 
714853e2bd2SBhanu Gollapudi 	lport = shost_priv(host);
71533c7da05SJulia Lawall 	rport = starget_to_rport(scsi_target(sc_cmd->device));
716853e2bd2SBhanu Gollapudi 	port = lport_priv(lport);
717aea71a02SBhanu Prakash Gollapudi 	interface = port->priv;
718853e2bd2SBhanu Gollapudi 
719853e2bd2SBhanu Gollapudi 	if (rport == NULL) {
720b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "device_reset: rport is NULL\n");
721853e2bd2SBhanu Gollapudi 		rc = FAILED;
722853e2bd2SBhanu Gollapudi 		goto tmf_err;
723853e2bd2SBhanu Gollapudi 	}
72433c7da05SJulia Lawall 	rp = rport->dd_data;
725853e2bd2SBhanu Gollapudi 
726853e2bd2SBhanu Gollapudi 	rc = fc_block_scsi_eh(sc_cmd);
727853e2bd2SBhanu Gollapudi 	if (rc)
728853e2bd2SBhanu Gollapudi 		return rc;
729853e2bd2SBhanu Gollapudi 
730853e2bd2SBhanu Gollapudi 	if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
731853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "device_reset: link is not ready\n");
732853e2bd2SBhanu Gollapudi 		rc = FAILED;
733853e2bd2SBhanu Gollapudi 		goto tmf_err;
734853e2bd2SBhanu Gollapudi 	}
735853e2bd2SBhanu Gollapudi 	/* rport and tgt are allocated together, so tgt should be non-NULL */
736853e2bd2SBhanu Gollapudi 	tgt = (struct bnx2fc_rport *)&rp[1];
737853e2bd2SBhanu Gollapudi 
738853e2bd2SBhanu Gollapudi 	if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
739853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
740853e2bd2SBhanu Gollapudi 		rc = FAILED;
741853e2bd2SBhanu Gollapudi 		goto tmf_err;
742853e2bd2SBhanu Gollapudi 	}
743853e2bd2SBhanu Gollapudi retry_tmf:
744853e2bd2SBhanu Gollapudi 	io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
745853e2bd2SBhanu Gollapudi 	if (!io_req) {
746853e2bd2SBhanu Gollapudi 		if (time_after(jiffies, start + HZ)) {
747853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "tmf: Failed TMF");
748853e2bd2SBhanu Gollapudi 			rc = FAILED;
749853e2bd2SBhanu Gollapudi 			goto tmf_err;
750853e2bd2SBhanu Gollapudi 		}
751853e2bd2SBhanu Gollapudi 		msleep(20);
752853e2bd2SBhanu Gollapudi 		goto retry_tmf;
753853e2bd2SBhanu Gollapudi 	}
754853e2bd2SBhanu Gollapudi 	/* Initialize rest of io_req fields */
755853e2bd2SBhanu Gollapudi 	io_req->sc_cmd = sc_cmd;
756853e2bd2SBhanu Gollapudi 	io_req->port = port;
757853e2bd2SBhanu Gollapudi 	io_req->tgt = tgt;
758853e2bd2SBhanu Gollapudi 
759853e2bd2SBhanu Gollapudi 	tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
760853e2bd2SBhanu Gollapudi 
761853e2bd2SBhanu Gollapudi 	rc = bnx2fc_init_mp_req(io_req);
762853e2bd2SBhanu Gollapudi 	if (rc == FAILED) {
763853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
764bd4d5de8SBhanu Prakash Gollapudi 		spin_lock_bh(&tgt->tgt_lock);
765853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
766bd4d5de8SBhanu Prakash Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
767853e2bd2SBhanu Gollapudi 		goto tmf_err;
768853e2bd2SBhanu Gollapudi 	}
769853e2bd2SBhanu Gollapudi 
770853e2bd2SBhanu Gollapudi 	/* Set TM flags */
771853e2bd2SBhanu Gollapudi 	io_req->io_req_flags = 0;
772853e2bd2SBhanu Gollapudi 	tm_req->tm_flags = tm_flags;
773853e2bd2SBhanu Gollapudi 
774853e2bd2SBhanu Gollapudi 	/* Fill FCP_CMND */
775853e2bd2SBhanu Gollapudi 	bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
776853e2bd2SBhanu Gollapudi 	fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
777853e2bd2SBhanu Gollapudi 	memset(fcp_cmnd->fc_cdb, 0,  sc_cmd->cmd_len);
778853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_dl = 0;
779853e2bd2SBhanu Gollapudi 
780853e2bd2SBhanu Gollapudi 	/* Fill FC header */
781853e2bd2SBhanu Gollapudi 	fc_hdr = &(tm_req->req_fc_hdr);
782853e2bd2SBhanu Gollapudi 	sid = tgt->sid;
783853e2bd2SBhanu Gollapudi 	did = rport->port_id;
784853e2bd2SBhanu Gollapudi 	__fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
785853e2bd2SBhanu Gollapudi 			   FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
786853e2bd2SBhanu Gollapudi 			   FC_FC_SEQ_INIT, 0);
787853e2bd2SBhanu Gollapudi 	/* Obtain exchange id */
788853e2bd2SBhanu Gollapudi 	xid = io_req->xid;
789853e2bd2SBhanu Gollapudi 
790853e2bd2SBhanu Gollapudi 	BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
791853e2bd2SBhanu Gollapudi 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
792853e2bd2SBhanu Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
793853e2bd2SBhanu Gollapudi 
794853e2bd2SBhanu Gollapudi 	/* Initialize task context for this IO request */
795aea71a02SBhanu Prakash Gollapudi 	task_page = (struct fcoe_task_ctx_entry *)
796aea71a02SBhanu Prakash Gollapudi 			interface->hba->task_ctx[task_idx];
797853e2bd2SBhanu Gollapudi 	task = &(task_page[index]);
798853e2bd2SBhanu Gollapudi 	bnx2fc_init_mp_task(io_req, task);
799853e2bd2SBhanu Gollapudi 
800853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = (char *)io_req;
801853e2bd2SBhanu Gollapudi 
802853e2bd2SBhanu Gollapudi 	/* Obtain free SQ entry */
803853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
804853e2bd2SBhanu Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
805853e2bd2SBhanu Gollapudi 
806853e2bd2SBhanu Gollapudi 	/* Enqueue the io_req to active_tm_queue */
807853e2bd2SBhanu Gollapudi 	io_req->on_tmf_queue = 1;
808853e2bd2SBhanu Gollapudi 	list_add_tail(&io_req->link, &tgt->active_tm_queue);
809853e2bd2SBhanu Gollapudi 
810853e2bd2SBhanu Gollapudi 	init_completion(&io_req->tm_done);
811853e2bd2SBhanu Gollapudi 	io_req->wait_for_comp = 1;
812853e2bd2SBhanu Gollapudi 
813853e2bd2SBhanu Gollapudi 	/* Ring doorbell */
814853e2bd2SBhanu Gollapudi 	bnx2fc_ring_doorbell(tgt);
815853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
816853e2bd2SBhanu Gollapudi 
817853e2bd2SBhanu Gollapudi 	rc = wait_for_completion_timeout(&io_req->tm_done,
818853e2bd2SBhanu Gollapudi 					 BNX2FC_TM_TIMEOUT * HZ);
819853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
820853e2bd2SBhanu Gollapudi 
821853e2bd2SBhanu Gollapudi 	io_req->wait_for_comp = 0;
82292886c9cSBhanu Prakash Gollapudi 	if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
823853e2bd2SBhanu Gollapudi 		set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
82492886c9cSBhanu Prakash Gollapudi 		if (io_req->on_tmf_queue) {
82592886c9cSBhanu Prakash Gollapudi 			list_del_init(&io_req->link);
82692886c9cSBhanu Prakash Gollapudi 			io_req->on_tmf_queue = 0;
82792886c9cSBhanu Prakash Gollapudi 		}
82892886c9cSBhanu Prakash Gollapudi 		io_req->wait_for_comp = 1;
82992886c9cSBhanu Prakash Gollapudi 		bnx2fc_initiate_cleanup(io_req);
83092886c9cSBhanu Prakash Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
83192886c9cSBhanu Prakash Gollapudi 		rc = wait_for_completion_timeout(&io_req->tm_done,
83292886c9cSBhanu Prakash Gollapudi 						 BNX2FC_FW_TIMEOUT);
83392886c9cSBhanu Prakash Gollapudi 		spin_lock_bh(&tgt->tgt_lock);
83492886c9cSBhanu Prakash Gollapudi 		io_req->wait_for_comp = 0;
83592886c9cSBhanu Prakash Gollapudi 		if (!rc)
83692886c9cSBhanu Prakash Gollapudi 			kref_put(&io_req->refcount, bnx2fc_cmd_release);
83792886c9cSBhanu Prakash Gollapudi 	}
838853e2bd2SBhanu Gollapudi 
839853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
840853e2bd2SBhanu Gollapudi 
841853e2bd2SBhanu Gollapudi 	if (!rc) {
842b2a554ffSBhanu Prakash Gollapudi 		BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
843853e2bd2SBhanu Gollapudi 		rc = FAILED;
844853e2bd2SBhanu Gollapudi 	} else {
845b2a554ffSBhanu Prakash Gollapudi 		BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
846853e2bd2SBhanu Gollapudi 		rc = SUCCESS;
847853e2bd2SBhanu Gollapudi 	}
848853e2bd2SBhanu Gollapudi tmf_err:
849853e2bd2SBhanu Gollapudi 	return rc;
850853e2bd2SBhanu Gollapudi }
851853e2bd2SBhanu Gollapudi 
852853e2bd2SBhanu Gollapudi int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
853853e2bd2SBhanu Gollapudi {
854853e2bd2SBhanu Gollapudi 	struct fc_lport *lport;
855853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
856853e2bd2SBhanu Gollapudi 	struct fc_rport *rport = tgt->rport;
857853e2bd2SBhanu Gollapudi 	struct fc_rport_priv *rdata = tgt->rdata;
858aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface;
859853e2bd2SBhanu Gollapudi 	struct fcoe_port *port;
860853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *abts_io_req;
861853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task;
862853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task_page;
863853e2bd2SBhanu Gollapudi 	struct fc_frame_header *fc_hdr;
864853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *abts_req;
865853e2bd2SBhanu Gollapudi 	int task_idx, index;
866853e2bd2SBhanu Gollapudi 	u32 sid, did;
867853e2bd2SBhanu Gollapudi 	u16 xid;
868853e2bd2SBhanu Gollapudi 	int rc = SUCCESS;
869853e2bd2SBhanu Gollapudi 	u32 r_a_tov = rdata->r_a_tov;
870853e2bd2SBhanu Gollapudi 
871853e2bd2SBhanu Gollapudi 	/* called with tgt_lock held */
872853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
873853e2bd2SBhanu Gollapudi 
874853e2bd2SBhanu Gollapudi 	port = io_req->port;
875aea71a02SBhanu Prakash Gollapudi 	interface = port->priv;
876853e2bd2SBhanu Gollapudi 	lport = port->lport;
877853e2bd2SBhanu Gollapudi 
878853e2bd2SBhanu Gollapudi 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
879853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
880853e2bd2SBhanu Gollapudi 		rc = FAILED;
881853e2bd2SBhanu Gollapudi 		goto abts_err;
882853e2bd2SBhanu Gollapudi 	}
883853e2bd2SBhanu Gollapudi 
884853e2bd2SBhanu Gollapudi 	if (rport == NULL) {
885b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
886853e2bd2SBhanu Gollapudi 		rc = FAILED;
887853e2bd2SBhanu Gollapudi 		goto abts_err;
888853e2bd2SBhanu Gollapudi 	}
889853e2bd2SBhanu Gollapudi 
890853e2bd2SBhanu Gollapudi 	if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
891853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
892853e2bd2SBhanu Gollapudi 		rc = FAILED;
893853e2bd2SBhanu Gollapudi 		goto abts_err;
894853e2bd2SBhanu Gollapudi 	}
895853e2bd2SBhanu Gollapudi 
896853e2bd2SBhanu Gollapudi 	abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
897853e2bd2SBhanu Gollapudi 	if (!abts_io_req) {
898853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "abts: couldnt allocate cmd\n");
899853e2bd2SBhanu Gollapudi 		rc = FAILED;
900853e2bd2SBhanu Gollapudi 		goto abts_err;
901853e2bd2SBhanu Gollapudi 	}
902853e2bd2SBhanu Gollapudi 
903853e2bd2SBhanu Gollapudi 	/* Initialize rest of io_req fields */
904853e2bd2SBhanu Gollapudi 	abts_io_req->sc_cmd = NULL;
905853e2bd2SBhanu Gollapudi 	abts_io_req->port = port;
906853e2bd2SBhanu Gollapudi 	abts_io_req->tgt = tgt;
907853e2bd2SBhanu Gollapudi 	abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
908853e2bd2SBhanu Gollapudi 
909853e2bd2SBhanu Gollapudi 	abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
910853e2bd2SBhanu Gollapudi 	memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
911853e2bd2SBhanu Gollapudi 
912853e2bd2SBhanu Gollapudi 	/* Fill FC header */
913853e2bd2SBhanu Gollapudi 	fc_hdr = &(abts_req->req_fc_hdr);
914853e2bd2SBhanu Gollapudi 
915853e2bd2SBhanu Gollapudi 	/* Obtain oxid and rxid for the original exchange to be aborted */
916853e2bd2SBhanu Gollapudi 	fc_hdr->fh_ox_id = htons(io_req->xid);
917619c5cb6SVlad Zolotarov 	fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
918853e2bd2SBhanu Gollapudi 
919853e2bd2SBhanu Gollapudi 	sid = tgt->sid;
920853e2bd2SBhanu Gollapudi 	did = rport->port_id;
921853e2bd2SBhanu Gollapudi 
922853e2bd2SBhanu Gollapudi 	__fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
923853e2bd2SBhanu Gollapudi 			   FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
924853e2bd2SBhanu Gollapudi 			   FC_FC_SEQ_INIT, 0);
925853e2bd2SBhanu Gollapudi 
926853e2bd2SBhanu Gollapudi 	xid = abts_io_req->xid;
927853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
928853e2bd2SBhanu Gollapudi 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
929853e2bd2SBhanu Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
930853e2bd2SBhanu Gollapudi 
931853e2bd2SBhanu Gollapudi 	/* Initialize task context for this IO request */
932aea71a02SBhanu Prakash Gollapudi 	task_page = (struct fcoe_task_ctx_entry *)
933aea71a02SBhanu Prakash Gollapudi 			interface->hba->task_ctx[task_idx];
934853e2bd2SBhanu Gollapudi 	task = &(task_page[index]);
935853e2bd2SBhanu Gollapudi 	bnx2fc_init_mp_task(abts_io_req, task);
936853e2bd2SBhanu Gollapudi 
937853e2bd2SBhanu Gollapudi 	/*
938853e2bd2SBhanu Gollapudi 	 * ABTS task is a temporary task that will be cleaned up
939853e2bd2SBhanu Gollapudi 	 * irrespective of ABTS response. We need to start the timer
940853e2bd2SBhanu Gollapudi 	 * for the original exchange, as the CQE is posted for the original
941853e2bd2SBhanu Gollapudi 	 * IO request.
942853e2bd2SBhanu Gollapudi 	 *
943853e2bd2SBhanu Gollapudi 	 * Timer for ABTS is started only when it is originated by a
944853e2bd2SBhanu Gollapudi 	 * TM request. For the ABTS issued as part of ULP timeout,
945853e2bd2SBhanu Gollapudi 	 * scsi-ml maintains the timers.
946853e2bd2SBhanu Gollapudi 	 */
947853e2bd2SBhanu Gollapudi 
948853e2bd2SBhanu Gollapudi 	/* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
949853e2bd2SBhanu Gollapudi 	bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
950853e2bd2SBhanu Gollapudi 
951853e2bd2SBhanu Gollapudi 	/* Obtain free SQ entry */
952853e2bd2SBhanu Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
953853e2bd2SBhanu Gollapudi 
954853e2bd2SBhanu Gollapudi 	/* Ring doorbell */
955853e2bd2SBhanu Gollapudi 	bnx2fc_ring_doorbell(tgt);
956853e2bd2SBhanu Gollapudi 
957853e2bd2SBhanu Gollapudi abts_err:
958853e2bd2SBhanu Gollapudi 	return rc;
959853e2bd2SBhanu Gollapudi }
960853e2bd2SBhanu Gollapudi 
9616c5a7ce4SBhanu Prakash Gollapudi int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
9626c5a7ce4SBhanu Prakash Gollapudi 				enum fc_rctl r_ctl)
9636c5a7ce4SBhanu Prakash Gollapudi {
9646c5a7ce4SBhanu Prakash Gollapudi 	struct fc_lport *lport;
9656c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_rport *tgt = orig_io_req->tgt;
9666c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface;
9676c5a7ce4SBhanu Prakash Gollapudi 	struct fcoe_port *port;
9686c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_cmd *seq_clnp_req;
9696c5a7ce4SBhanu Prakash Gollapudi 	struct fcoe_task_ctx_entry *task;
9706c5a7ce4SBhanu Prakash Gollapudi 	struct fcoe_task_ctx_entry *task_page;
9716c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_els_cb_arg *cb_arg = NULL;
9726c5a7ce4SBhanu Prakash Gollapudi 	int task_idx, index;
9736c5a7ce4SBhanu Prakash Gollapudi 	u16 xid;
9746c5a7ce4SBhanu Prakash Gollapudi 	int rc = 0;
9756c5a7ce4SBhanu Prakash Gollapudi 
9766c5a7ce4SBhanu Prakash Gollapudi 	BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
9776c5a7ce4SBhanu Prakash Gollapudi 		   orig_io_req->xid);
9786c5a7ce4SBhanu Prakash Gollapudi 	kref_get(&orig_io_req->refcount);
9796c5a7ce4SBhanu Prakash Gollapudi 
9806c5a7ce4SBhanu Prakash Gollapudi 	port = orig_io_req->port;
9816c5a7ce4SBhanu Prakash Gollapudi 	interface = port->priv;
9826c5a7ce4SBhanu Prakash Gollapudi 	lport = port->lport;
9836c5a7ce4SBhanu Prakash Gollapudi 
9846c5a7ce4SBhanu Prakash Gollapudi 	cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
9856c5a7ce4SBhanu Prakash Gollapudi 	if (!cb_arg) {
9866c5a7ce4SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
9876c5a7ce4SBhanu Prakash Gollapudi 		rc = -ENOMEM;
9886c5a7ce4SBhanu Prakash Gollapudi 		goto cleanup_err;
9896c5a7ce4SBhanu Prakash Gollapudi 	}
9906c5a7ce4SBhanu Prakash Gollapudi 
9916c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
9926c5a7ce4SBhanu Prakash Gollapudi 	if (!seq_clnp_req) {
9936c5a7ce4SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
9946c5a7ce4SBhanu Prakash Gollapudi 		rc = -ENOMEM;
9956c5a7ce4SBhanu Prakash Gollapudi 		kfree(cb_arg);
9966c5a7ce4SBhanu Prakash Gollapudi 		goto cleanup_err;
9976c5a7ce4SBhanu Prakash Gollapudi 	}
9986c5a7ce4SBhanu Prakash Gollapudi 	/* Initialize rest of io_req fields */
9996c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->sc_cmd = NULL;
10006c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->port = port;
10016c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->tgt = tgt;
10026c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
10036c5a7ce4SBhanu Prakash Gollapudi 
10046c5a7ce4SBhanu Prakash Gollapudi 	xid = seq_clnp_req->xid;
10056c5a7ce4SBhanu Prakash Gollapudi 
10066c5a7ce4SBhanu Prakash Gollapudi 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
10076c5a7ce4SBhanu Prakash Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
10086c5a7ce4SBhanu Prakash Gollapudi 
10096c5a7ce4SBhanu Prakash Gollapudi 	/* Initialize task context for this IO request */
10106c5a7ce4SBhanu Prakash Gollapudi 	task_page = (struct fcoe_task_ctx_entry *)
10116c5a7ce4SBhanu Prakash Gollapudi 		     interface->hba->task_ctx[task_idx];
10126c5a7ce4SBhanu Prakash Gollapudi 	task = &(task_page[index]);
10136c5a7ce4SBhanu Prakash Gollapudi 	cb_arg->aborted_io_req = orig_io_req;
10146c5a7ce4SBhanu Prakash Gollapudi 	cb_arg->io_req = seq_clnp_req;
10156c5a7ce4SBhanu Prakash Gollapudi 	cb_arg->r_ctl = r_ctl;
10166c5a7ce4SBhanu Prakash Gollapudi 	cb_arg->offset = offset;
10176c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->cb_arg = cb_arg;
10186c5a7ce4SBhanu Prakash Gollapudi 
10196c5a7ce4SBhanu Prakash Gollapudi 	printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
10206c5a7ce4SBhanu Prakash Gollapudi 	bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
10216c5a7ce4SBhanu Prakash Gollapudi 
10226c5a7ce4SBhanu Prakash Gollapudi 	/* Obtain free SQ entry */
10236c5a7ce4SBhanu Prakash Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
10246c5a7ce4SBhanu Prakash Gollapudi 
10256c5a7ce4SBhanu Prakash Gollapudi 	/* Ring doorbell */
10266c5a7ce4SBhanu Prakash Gollapudi 	bnx2fc_ring_doorbell(tgt);
10276c5a7ce4SBhanu Prakash Gollapudi cleanup_err:
10286c5a7ce4SBhanu Prakash Gollapudi 	return rc;
10296c5a7ce4SBhanu Prakash Gollapudi }
10306c5a7ce4SBhanu Prakash Gollapudi 
1031853e2bd2SBhanu Gollapudi int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
1032853e2bd2SBhanu Gollapudi {
1033853e2bd2SBhanu Gollapudi 	struct fc_lport *lport;
1034853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1035aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface;
1036853e2bd2SBhanu Gollapudi 	struct fcoe_port *port;
1037853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *cleanup_io_req;
1038853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task;
1039853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task_page;
1040853e2bd2SBhanu Gollapudi 	int task_idx, index;
1041853e2bd2SBhanu Gollapudi 	u16 xid, orig_xid;
1042853e2bd2SBhanu Gollapudi 	int rc = 0;
1043853e2bd2SBhanu Gollapudi 
1044853e2bd2SBhanu Gollapudi 	/* ASSUMPTION: called with tgt_lock held */
1045853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
1046853e2bd2SBhanu Gollapudi 
1047853e2bd2SBhanu Gollapudi 	port = io_req->port;
1048aea71a02SBhanu Prakash Gollapudi 	interface = port->priv;
1049853e2bd2SBhanu Gollapudi 	lport = port->lport;
1050853e2bd2SBhanu Gollapudi 
1051853e2bd2SBhanu Gollapudi 	cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
1052853e2bd2SBhanu Gollapudi 	if (!cleanup_io_req) {
1053853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "cleanup: couldnt allocate cmd\n");
1054853e2bd2SBhanu Gollapudi 		rc = -1;
1055853e2bd2SBhanu Gollapudi 		goto cleanup_err;
1056853e2bd2SBhanu Gollapudi 	}
1057853e2bd2SBhanu Gollapudi 
1058853e2bd2SBhanu Gollapudi 	/* Initialize rest of io_req fields */
1059853e2bd2SBhanu Gollapudi 	cleanup_io_req->sc_cmd = NULL;
1060853e2bd2SBhanu Gollapudi 	cleanup_io_req->port = port;
1061853e2bd2SBhanu Gollapudi 	cleanup_io_req->tgt = tgt;
1062853e2bd2SBhanu Gollapudi 	cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
1063853e2bd2SBhanu Gollapudi 
1064853e2bd2SBhanu Gollapudi 	xid = cleanup_io_req->xid;
1065853e2bd2SBhanu Gollapudi 
1066853e2bd2SBhanu Gollapudi 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1067853e2bd2SBhanu Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
1068853e2bd2SBhanu Gollapudi 
1069853e2bd2SBhanu Gollapudi 	/* Initialize task context for this IO request */
1070aea71a02SBhanu Prakash Gollapudi 	task_page = (struct fcoe_task_ctx_entry *)
1071aea71a02SBhanu Prakash Gollapudi 			interface->hba->task_ctx[task_idx];
1072853e2bd2SBhanu Gollapudi 	task = &(task_page[index]);
1073853e2bd2SBhanu Gollapudi 	orig_xid = io_req->xid;
1074853e2bd2SBhanu Gollapudi 
1075853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
1076853e2bd2SBhanu Gollapudi 
1077853e2bd2SBhanu Gollapudi 	bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
1078853e2bd2SBhanu Gollapudi 
1079853e2bd2SBhanu Gollapudi 	/* Obtain free SQ entry */
1080853e2bd2SBhanu Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
1081853e2bd2SBhanu Gollapudi 
1082853e2bd2SBhanu Gollapudi 	/* Ring doorbell */
1083853e2bd2SBhanu Gollapudi 	bnx2fc_ring_doorbell(tgt);
1084853e2bd2SBhanu Gollapudi 
1085853e2bd2SBhanu Gollapudi cleanup_err:
1086853e2bd2SBhanu Gollapudi 	return rc;
1087853e2bd2SBhanu Gollapudi }
1088853e2bd2SBhanu Gollapudi 
1089853e2bd2SBhanu Gollapudi /**
1090853e2bd2SBhanu Gollapudi  * bnx2fc_eh_target_reset: Reset a target
1091853e2bd2SBhanu Gollapudi  *
1092853e2bd2SBhanu Gollapudi  * @sc_cmd:	SCSI command
1093853e2bd2SBhanu Gollapudi  *
1094853e2bd2SBhanu Gollapudi  * Set from SCSI host template to send task mgmt command to the target
1095853e2bd2SBhanu Gollapudi  *	and wait for the response
1096853e2bd2SBhanu Gollapudi  */
1097853e2bd2SBhanu Gollapudi int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
1098853e2bd2SBhanu Gollapudi {
1099853e2bd2SBhanu Gollapudi 	return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
1100853e2bd2SBhanu Gollapudi }
1101853e2bd2SBhanu Gollapudi 
1102853e2bd2SBhanu Gollapudi /**
1103853e2bd2SBhanu Gollapudi  * bnx2fc_eh_device_reset - Reset a single LUN
1104853e2bd2SBhanu Gollapudi  *
1105853e2bd2SBhanu Gollapudi  * @sc_cmd:	SCSI command
1106853e2bd2SBhanu Gollapudi  *
1107853e2bd2SBhanu Gollapudi  * Set from SCSI host template to send task mgmt command to the target
1108853e2bd2SBhanu Gollapudi  *	and wait for the response
1109853e2bd2SBhanu Gollapudi  */
1110853e2bd2SBhanu Gollapudi int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1111853e2bd2SBhanu Gollapudi {
1112853e2bd2SBhanu Gollapudi 	return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
1113853e2bd2SBhanu Gollapudi }
1114853e2bd2SBhanu Gollapudi 
1115c1bb4f33SBhanu Prakash Gollapudi int bnx2fc_expl_logo(struct fc_lport *lport, struct bnx2fc_cmd *io_req)
1116c1bb4f33SBhanu Prakash Gollapudi {
1117c1bb4f33SBhanu Prakash Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1118c1bb4f33SBhanu Prakash Gollapudi 	struct fc_rport_priv *rdata = tgt->rdata;
1119c1bb4f33SBhanu Prakash Gollapudi 	int logo_issued;
1120c1bb4f33SBhanu Prakash Gollapudi 	int rc = SUCCESS;
1121c1bb4f33SBhanu Prakash Gollapudi 	int wait_cnt = 0;
1122c1bb4f33SBhanu Prakash Gollapudi 
1123c1bb4f33SBhanu Prakash Gollapudi 	BNX2FC_IO_DBG(io_req, "Expl logo - tgt flags = 0x%lx\n",
1124c1bb4f33SBhanu Prakash Gollapudi 		      tgt->flags);
1125c1bb4f33SBhanu Prakash Gollapudi 	logo_issued = test_and_set_bit(BNX2FC_FLAG_EXPL_LOGO,
1126c1bb4f33SBhanu Prakash Gollapudi 				       &tgt->flags);
1127c1bb4f33SBhanu Prakash Gollapudi 	io_req->wait_for_comp = 1;
1128c1bb4f33SBhanu Prakash Gollapudi 	bnx2fc_initiate_cleanup(io_req);
1129c1bb4f33SBhanu Prakash Gollapudi 
1130c1bb4f33SBhanu Prakash Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
1131c1bb4f33SBhanu Prakash Gollapudi 
1132c1bb4f33SBhanu Prakash Gollapudi 	wait_for_completion(&io_req->tm_done);
1133c1bb4f33SBhanu Prakash Gollapudi 
1134c1bb4f33SBhanu Prakash Gollapudi 	io_req->wait_for_comp = 0;
1135c1bb4f33SBhanu Prakash Gollapudi 	/*
1136c1bb4f33SBhanu Prakash Gollapudi 	 * release the reference taken in eh_abort to allow the
1137c1bb4f33SBhanu Prakash Gollapudi 	 * target to re-login after flushing IOs
1138c1bb4f33SBhanu Prakash Gollapudi 	 */
1139c1bb4f33SBhanu Prakash Gollapudi 	 kref_put(&io_req->refcount, bnx2fc_cmd_release);
1140c1bb4f33SBhanu Prakash Gollapudi 
1141c1bb4f33SBhanu Prakash Gollapudi 	if (!logo_issued) {
1142c1bb4f33SBhanu Prakash Gollapudi 		clear_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags);
1143c1bb4f33SBhanu Prakash Gollapudi 		mutex_lock(&lport->disc.disc_mutex);
1144c1bb4f33SBhanu Prakash Gollapudi 		lport->tt.rport_logoff(rdata);
1145c1bb4f33SBhanu Prakash Gollapudi 		mutex_unlock(&lport->disc.disc_mutex);
1146c1bb4f33SBhanu Prakash Gollapudi 		do {
1147c1bb4f33SBhanu Prakash Gollapudi 			msleep(BNX2FC_RELOGIN_WAIT_TIME);
1148c1bb4f33SBhanu Prakash Gollapudi 			if (wait_cnt++ > BNX2FC_RELOGIN_WAIT_CNT) {
1149c1bb4f33SBhanu Prakash Gollapudi 				rc = FAILED;
1150c1bb4f33SBhanu Prakash Gollapudi 				break;
1151c1bb4f33SBhanu Prakash Gollapudi 			}
1152c1bb4f33SBhanu Prakash Gollapudi 		} while (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags));
1153c1bb4f33SBhanu Prakash Gollapudi 	}
1154c1bb4f33SBhanu Prakash Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
1155c1bb4f33SBhanu Prakash Gollapudi 	return rc;
1156c1bb4f33SBhanu Prakash Gollapudi }
1157853e2bd2SBhanu Gollapudi /**
1158853e2bd2SBhanu Gollapudi  * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
1159853e2bd2SBhanu Gollapudi  *			SCSI command
1160853e2bd2SBhanu Gollapudi  *
1161853e2bd2SBhanu Gollapudi  * @sc_cmd:	SCSI_ML command pointer
1162853e2bd2SBhanu Gollapudi  *
1163853e2bd2SBhanu Gollapudi  * SCSI abort request handler
1164853e2bd2SBhanu Gollapudi  */
1165853e2bd2SBhanu Gollapudi int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
1166853e2bd2SBhanu Gollapudi {
1167853e2bd2SBhanu Gollapudi 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1168853e2bd2SBhanu Gollapudi 	struct fc_rport_libfc_priv *rp = rport->dd_data;
1169853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
1170853e2bd2SBhanu Gollapudi 	struct fc_lport *lport;
1171853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt;
1172853e2bd2SBhanu Gollapudi 	int rc = FAILED;
1173853e2bd2SBhanu Gollapudi 
1174853e2bd2SBhanu Gollapudi 
1175853e2bd2SBhanu Gollapudi 	rc = fc_block_scsi_eh(sc_cmd);
1176853e2bd2SBhanu Gollapudi 	if (rc)
1177853e2bd2SBhanu Gollapudi 		return rc;
1178853e2bd2SBhanu Gollapudi 
1179853e2bd2SBhanu Gollapudi 	lport = shost_priv(sc_cmd->device->host);
1180853e2bd2SBhanu Gollapudi 	if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1181b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "eh_abort: link not ready\n");
1182853e2bd2SBhanu Gollapudi 		return rc;
1183853e2bd2SBhanu Gollapudi 	}
1184853e2bd2SBhanu Gollapudi 
1185853e2bd2SBhanu Gollapudi 	tgt = (struct bnx2fc_rport *)&rp[1];
1186853e2bd2SBhanu Gollapudi 
1187853e2bd2SBhanu Gollapudi 	BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1188853e2bd2SBhanu Gollapudi 
1189853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
1190853e2bd2SBhanu Gollapudi 	io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr;
1191853e2bd2SBhanu Gollapudi 	if (!io_req) {
1192853e2bd2SBhanu Gollapudi 		/* Command might have just completed */
1193853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
1194853e2bd2SBhanu Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
1195853e2bd2SBhanu Gollapudi 		return SUCCESS;
1196853e2bd2SBhanu Gollapudi 	}
1197853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
1198853e2bd2SBhanu Gollapudi 		      io_req->refcount.refcount.counter);
1199853e2bd2SBhanu Gollapudi 
1200853e2bd2SBhanu Gollapudi 	/* Hold IO request across abort processing */
1201853e2bd2SBhanu Gollapudi 	kref_get(&io_req->refcount);
1202853e2bd2SBhanu Gollapudi 
1203853e2bd2SBhanu Gollapudi 	BUG_ON(tgt != io_req->tgt);
1204853e2bd2SBhanu Gollapudi 
1205853e2bd2SBhanu Gollapudi 	/* Remove the io_req from the active_q. */
1206853e2bd2SBhanu Gollapudi 	/*
1207853e2bd2SBhanu Gollapudi 	 * Task Mgmt functions (LUN RESET & TGT RESET) will not
1208853e2bd2SBhanu Gollapudi 	 * issue an ABTS on this particular IO req, as the
1209853e2bd2SBhanu Gollapudi 	 * io_req is no longer in the active_q.
1210853e2bd2SBhanu Gollapudi 	 */
1211853e2bd2SBhanu Gollapudi 	if (tgt->flush_in_prog) {
1212b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1213853e2bd2SBhanu Gollapudi 			"flush in progress\n", io_req->xid);
1214853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1215853e2bd2SBhanu Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
1216853e2bd2SBhanu Gollapudi 		return SUCCESS;
1217853e2bd2SBhanu Gollapudi 	}
1218853e2bd2SBhanu Gollapudi 
1219853e2bd2SBhanu Gollapudi 	if (io_req->on_active_queue == 0) {
1220b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1221853e2bd2SBhanu Gollapudi 				"not on active_q\n", io_req->xid);
1222853e2bd2SBhanu Gollapudi 		/*
1223853e2bd2SBhanu Gollapudi 		 * This condition can happen only due to the FW bug,
1224853e2bd2SBhanu Gollapudi 		 * where we do not receive cleanup response from
1225853e2bd2SBhanu Gollapudi 		 * the FW. Handle this case gracefully by erroring
1226853e2bd2SBhanu Gollapudi 		 * back the IO request to SCSI-ml
1227853e2bd2SBhanu Gollapudi 		 */
1228853e2bd2SBhanu Gollapudi 		bnx2fc_scsi_done(io_req, DID_ABORT);
1229853e2bd2SBhanu Gollapudi 
1230853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1231853e2bd2SBhanu Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
1232853e2bd2SBhanu Gollapudi 		return SUCCESS;
1233853e2bd2SBhanu Gollapudi 	}
1234853e2bd2SBhanu Gollapudi 
1235853e2bd2SBhanu Gollapudi 	/*
1236853e2bd2SBhanu Gollapudi 	 * Only eh_abort processing will remove the IO from
1237853e2bd2SBhanu Gollapudi 	 * active_cmd_q before processing the request. this is
1238853e2bd2SBhanu Gollapudi 	 * done to avoid race conditions between IOs aborted
1239853e2bd2SBhanu Gollapudi 	 * as part of task management completion and eh_abort
1240853e2bd2SBhanu Gollapudi 	 * processing
1241853e2bd2SBhanu Gollapudi 	 */
1242853e2bd2SBhanu Gollapudi 	list_del_init(&io_req->link);
1243853e2bd2SBhanu Gollapudi 	io_req->on_active_queue = 0;
1244853e2bd2SBhanu Gollapudi 	/* Move IO req to retire queue */
1245853e2bd2SBhanu Gollapudi 	list_add_tail(&io_req->link, &tgt->io_retire_queue);
1246853e2bd2SBhanu Gollapudi 
1247853e2bd2SBhanu Gollapudi 	init_completion(&io_req->tm_done);
1248853e2bd2SBhanu Gollapudi 
1249c1bb4f33SBhanu Prakash Gollapudi 	if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
1250b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1251853e2bd2SBhanu Gollapudi 				"already in abts processing\n", io_req->xid);
125299cc600cSBhanu Prakash Gollapudi 		if (cancel_delayed_work(&io_req->timeout_work))
125399cc600cSBhanu Prakash Gollapudi 			kref_put(&io_req->refcount,
125499cc600cSBhanu Prakash Gollapudi 				 bnx2fc_cmd_release); /* drop timer hold */
1255c1bb4f33SBhanu Prakash Gollapudi 		rc = bnx2fc_expl_logo(lport, io_req);
125621837896SEddie Wai 		/* This only occurs when an task abort was requested while ABTS
125721837896SEddie Wai 		   is in progress.  Setting the IO_CLEANUP flag will skip the
125821837896SEddie Wai 		   RRQ process in the case when the fw generated SCSI_CMD cmpl
125921837896SEddie Wai 		   was a result from the ABTS request rather than the CLEANUP
126021837896SEddie Wai 		   request */
126121837896SEddie Wai 		set_bit(BNX2FC_FLAG_IO_CLEANUP,	&io_req->req_flags);
1262c1bb4f33SBhanu Prakash Gollapudi 		goto out;
1263c1bb4f33SBhanu Prakash Gollapudi 	}
1264c1bb4f33SBhanu Prakash Gollapudi 
1265c1bb4f33SBhanu Prakash Gollapudi 	/* Cancel the current timer running on this io_req */
1266c1bb4f33SBhanu Prakash Gollapudi 	if (cancel_delayed_work(&io_req->timeout_work))
1267c1bb4f33SBhanu Prakash Gollapudi 		kref_put(&io_req->refcount,
1268c1bb4f33SBhanu Prakash Gollapudi 			 bnx2fc_cmd_release); /* drop timer hold */
1269c1bb4f33SBhanu Prakash Gollapudi 	set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
1270c1bb4f33SBhanu Prakash Gollapudi 	io_req->wait_for_comp = 1;
1271c1bb4f33SBhanu Prakash Gollapudi 	rc = bnx2fc_initiate_abts(io_req);
1272c1bb4f33SBhanu Prakash Gollapudi 	if (rc == FAILED) {
127399cc600cSBhanu Prakash Gollapudi 		bnx2fc_initiate_cleanup(io_req);
127499cc600cSBhanu Prakash Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
127599cc600cSBhanu Prakash Gollapudi 		wait_for_completion(&io_req->tm_done);
127699cc600cSBhanu Prakash Gollapudi 		spin_lock_bh(&tgt->tgt_lock);
127799cc600cSBhanu Prakash Gollapudi 		io_req->wait_for_comp = 0;
1278c1bb4f33SBhanu Prakash Gollapudi 		goto done;
1279853e2bd2SBhanu Gollapudi 	}
1280853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
1281853e2bd2SBhanu Gollapudi 
1282853e2bd2SBhanu Gollapudi 	wait_for_completion(&io_req->tm_done);
1283853e2bd2SBhanu Gollapudi 
1284853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
1285853e2bd2SBhanu Gollapudi 	io_req->wait_for_comp = 0;
12865d78f175SBhanu Prakash Gollapudi 	if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
12875d78f175SBhanu Prakash Gollapudi 		BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
12885d78f175SBhanu Prakash Gollapudi 		rc = SUCCESS;
12895d78f175SBhanu Prakash Gollapudi 	} else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1290853e2bd2SBhanu Gollapudi 				      &io_req->req_flags))) {
1291853e2bd2SBhanu Gollapudi 		/* Let the scsi-ml try to recover this command */
1292853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
1293853e2bd2SBhanu Gollapudi 		       io_req->xid);
1294c1bb4f33SBhanu Prakash Gollapudi 		rc = bnx2fc_expl_logo(lport, io_req);
1295c1bb4f33SBhanu Prakash Gollapudi 		goto out;
1296853e2bd2SBhanu Gollapudi 	} else {
1297853e2bd2SBhanu Gollapudi 		/*
1298853e2bd2SBhanu Gollapudi 		 * We come here even when there was a race condition
1299853e2bd2SBhanu Gollapudi 		 * between timeout and abts completion, and abts
1300853e2bd2SBhanu Gollapudi 		 * completion happens just in time.
1301853e2bd2SBhanu Gollapudi 		 */
1302853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1303853e2bd2SBhanu Gollapudi 		rc = SUCCESS;
1304853e2bd2SBhanu Gollapudi 		bnx2fc_scsi_done(io_req, DID_ABORT);
1305853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1306853e2bd2SBhanu Gollapudi 	}
1307c1bb4f33SBhanu Prakash Gollapudi done:
1308853e2bd2SBhanu Gollapudi 	/* release the reference taken in eh_abort */
1309853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
1310c1bb4f33SBhanu Prakash Gollapudi out:
1311853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
1312853e2bd2SBhanu Gollapudi 	return rc;
1313853e2bd2SBhanu Gollapudi }
1314853e2bd2SBhanu Gollapudi 
13156c5a7ce4SBhanu Prakash Gollapudi void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
13166c5a7ce4SBhanu Prakash Gollapudi 				      struct fcoe_task_ctx_entry *task,
13176c5a7ce4SBhanu Prakash Gollapudi 				      u8 rx_state)
13186c5a7ce4SBhanu Prakash Gollapudi {
13196c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
13206c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
13216c5a7ce4SBhanu Prakash Gollapudi 	u32 offset = cb_arg->offset;
13226c5a7ce4SBhanu Prakash Gollapudi 	enum fc_rctl r_ctl = cb_arg->r_ctl;
13236c5a7ce4SBhanu Prakash Gollapudi 	int rc = 0;
13246c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_rport *tgt = orig_io_req->tgt;
13256c5a7ce4SBhanu Prakash Gollapudi 
13266c5a7ce4SBhanu Prakash Gollapudi 	BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
13276c5a7ce4SBhanu Prakash Gollapudi 			      "cmd_type = %d\n",
13286c5a7ce4SBhanu Prakash Gollapudi 		   seq_clnp_req->xid, seq_clnp_req->cmd_type);
13296c5a7ce4SBhanu Prakash Gollapudi 
13306c5a7ce4SBhanu Prakash Gollapudi 	if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
13316c5a7ce4SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
13326c5a7ce4SBhanu Prakash Gollapudi 			seq_clnp_req->xid);
13336c5a7ce4SBhanu Prakash Gollapudi 		goto free_cb_arg;
13346c5a7ce4SBhanu Prakash Gollapudi 	}
13356c5a7ce4SBhanu Prakash Gollapudi 
13366c5a7ce4SBhanu Prakash Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
13376c5a7ce4SBhanu Prakash Gollapudi 	rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
13386c5a7ce4SBhanu Prakash Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
13396c5a7ce4SBhanu Prakash Gollapudi 
13406c5a7ce4SBhanu Prakash Gollapudi 	if (rc)
13416c5a7ce4SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
13426c5a7ce4SBhanu Prakash Gollapudi 			" IO will abort\n");
13436c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->cb_arg = NULL;
13446c5a7ce4SBhanu Prakash Gollapudi 	kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
13456c5a7ce4SBhanu Prakash Gollapudi free_cb_arg:
13466c5a7ce4SBhanu Prakash Gollapudi 	kfree(cb_arg);
13476c5a7ce4SBhanu Prakash Gollapudi 	return;
13486c5a7ce4SBhanu Prakash Gollapudi }
13496c5a7ce4SBhanu Prakash Gollapudi 
1350853e2bd2SBhanu Gollapudi void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1351853e2bd2SBhanu Gollapudi 				  struct fcoe_task_ctx_entry *task,
1352853e2bd2SBhanu Gollapudi 				  u8 num_rq)
1353853e2bd2SBhanu Gollapudi {
1354853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
1355853e2bd2SBhanu Gollapudi 			      "refcnt = %d, cmd_type = %d\n",
1356853e2bd2SBhanu Gollapudi 		   io_req->refcount.refcount.counter, io_req->cmd_type);
1357853e2bd2SBhanu Gollapudi 	bnx2fc_scsi_done(io_req, DID_ERROR);
1358853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
135999cc600cSBhanu Prakash Gollapudi 	if (io_req->wait_for_comp)
136099cc600cSBhanu Prakash Gollapudi 		complete(&io_req->tm_done);
1361853e2bd2SBhanu Gollapudi }
1362853e2bd2SBhanu Gollapudi 
1363853e2bd2SBhanu Gollapudi void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1364853e2bd2SBhanu Gollapudi 			       struct fcoe_task_ctx_entry *task,
1365853e2bd2SBhanu Gollapudi 			       u8 num_rq)
1366853e2bd2SBhanu Gollapudi {
1367853e2bd2SBhanu Gollapudi 	u32 r_ctl;
1368853e2bd2SBhanu Gollapudi 	u32 r_a_tov = FC_DEF_R_A_TOV;
1369853e2bd2SBhanu Gollapudi 	u8 issue_rrq = 0;
1370853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1371853e2bd2SBhanu Gollapudi 
1372853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1373853e2bd2SBhanu Gollapudi 			      "refcnt = %d, cmd_type = %d\n",
1374853e2bd2SBhanu Gollapudi 		   io_req->xid,
1375853e2bd2SBhanu Gollapudi 		   io_req->refcount.refcount.counter, io_req->cmd_type);
1376853e2bd2SBhanu Gollapudi 
1377853e2bd2SBhanu Gollapudi 	if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1378853e2bd2SBhanu Gollapudi 				       &io_req->req_flags)) {
1379853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "Timer context finished processing"
1380853e2bd2SBhanu Gollapudi 				" this io\n");
1381853e2bd2SBhanu Gollapudi 		return;
1382853e2bd2SBhanu Gollapudi 	}
1383853e2bd2SBhanu Gollapudi 
1384853e2bd2SBhanu Gollapudi 	/* Do not issue RRQ as this IO is already cleanedup */
1385853e2bd2SBhanu Gollapudi 	if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
1386853e2bd2SBhanu Gollapudi 				&io_req->req_flags))
1387853e2bd2SBhanu Gollapudi 		goto io_compl;
1388853e2bd2SBhanu Gollapudi 
1389853e2bd2SBhanu Gollapudi 	/*
1390853e2bd2SBhanu Gollapudi 	 * For ABTS issued due to SCSI eh_abort_handler, timeout
1391853e2bd2SBhanu Gollapudi 	 * values are maintained by scsi-ml itself. Cancel timeout
1392853e2bd2SBhanu Gollapudi 	 * in case ABTS issued as part of task management function
1393853e2bd2SBhanu Gollapudi 	 * or due to FW error.
1394853e2bd2SBhanu Gollapudi 	 */
1395853e2bd2SBhanu Gollapudi 	if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
1396853e2bd2SBhanu Gollapudi 		if (cancel_delayed_work(&io_req->timeout_work))
1397853e2bd2SBhanu Gollapudi 			kref_put(&io_req->refcount,
1398853e2bd2SBhanu Gollapudi 				 bnx2fc_cmd_release); /* drop timer hold */
1399853e2bd2SBhanu Gollapudi 
1400619c5cb6SVlad Zolotarov 	r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
1401853e2bd2SBhanu Gollapudi 
1402853e2bd2SBhanu Gollapudi 	switch (r_ctl) {
1403853e2bd2SBhanu Gollapudi 	case FC_RCTL_BA_ACC:
1404853e2bd2SBhanu Gollapudi 		/*
1405853e2bd2SBhanu Gollapudi 		 * Dont release this cmd yet. It will be relesed
1406853e2bd2SBhanu Gollapudi 		 * after we get RRQ response
1407853e2bd2SBhanu Gollapudi 		 */
1408853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1409853e2bd2SBhanu Gollapudi 		issue_rrq = 1;
1410853e2bd2SBhanu Gollapudi 		break;
1411853e2bd2SBhanu Gollapudi 
1412853e2bd2SBhanu Gollapudi 	case FC_RCTL_BA_RJT:
1413853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1414853e2bd2SBhanu Gollapudi 		break;
1415853e2bd2SBhanu Gollapudi 	default:
1416853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Unknown ABTS response\n");
1417853e2bd2SBhanu Gollapudi 		break;
1418853e2bd2SBhanu Gollapudi 	}
1419853e2bd2SBhanu Gollapudi 
1420853e2bd2SBhanu Gollapudi 	if (issue_rrq) {
1421853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1422853e2bd2SBhanu Gollapudi 		set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1423853e2bd2SBhanu Gollapudi 	}
1424853e2bd2SBhanu Gollapudi 	set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1425853e2bd2SBhanu Gollapudi 	bnx2fc_cmd_timer_set(io_req, r_a_tov);
1426853e2bd2SBhanu Gollapudi 
1427853e2bd2SBhanu Gollapudi io_compl:
1428853e2bd2SBhanu Gollapudi 	if (io_req->wait_for_comp) {
1429853e2bd2SBhanu Gollapudi 		if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1430853e2bd2SBhanu Gollapudi 				       &io_req->req_flags))
1431853e2bd2SBhanu Gollapudi 			complete(&io_req->tm_done);
1432853e2bd2SBhanu Gollapudi 	} else {
1433853e2bd2SBhanu Gollapudi 		/*
1434853e2bd2SBhanu Gollapudi 		 * We end up here when ABTS is issued as
1435853e2bd2SBhanu Gollapudi 		 * in asynchronous context, i.e., as part
1436853e2bd2SBhanu Gollapudi 		 * of task management completion, or
1437853e2bd2SBhanu Gollapudi 		 * when FW error is received or when the
1438853e2bd2SBhanu Gollapudi 		 * ABTS is issued when the IO is timed
1439853e2bd2SBhanu Gollapudi 		 * out.
1440853e2bd2SBhanu Gollapudi 		 */
1441853e2bd2SBhanu Gollapudi 
1442853e2bd2SBhanu Gollapudi 		if (io_req->on_active_queue) {
1443853e2bd2SBhanu Gollapudi 			list_del_init(&io_req->link);
1444853e2bd2SBhanu Gollapudi 			io_req->on_active_queue = 0;
1445853e2bd2SBhanu Gollapudi 			/* Move IO req to retire queue */
1446853e2bd2SBhanu Gollapudi 			list_add_tail(&io_req->link, &tgt->io_retire_queue);
1447853e2bd2SBhanu Gollapudi 		}
1448853e2bd2SBhanu Gollapudi 		bnx2fc_scsi_done(io_req, DID_ERROR);
1449853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1450853e2bd2SBhanu Gollapudi 	}
1451853e2bd2SBhanu Gollapudi }
1452853e2bd2SBhanu Gollapudi 
1453853e2bd2SBhanu Gollapudi static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1454853e2bd2SBhanu Gollapudi {
1455853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1456853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1457d71fb3bdSBhanu Prakash Gollapudi 	struct bnx2fc_cmd *cmd, *tmp;
14589cb78c16SHannes Reinecke 	u64 tm_lun = sc_cmd->device->lun;
14599cb78c16SHannes Reinecke 	u64 lun;
1460853e2bd2SBhanu Gollapudi 	int rc = 0;
1461853e2bd2SBhanu Gollapudi 
1462853e2bd2SBhanu Gollapudi 	/* called with tgt_lock held */
1463853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1464853e2bd2SBhanu Gollapudi 	/*
1465853e2bd2SBhanu Gollapudi 	 * Walk thru the active_ios queue and ABORT the IO
1466853e2bd2SBhanu Gollapudi 	 * that matches with the LUN that was reset
1467853e2bd2SBhanu Gollapudi 	 */
1468d71fb3bdSBhanu Prakash Gollapudi 	list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1469853e2bd2SBhanu Gollapudi 		BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
1470853e2bd2SBhanu Gollapudi 		lun = cmd->sc_cmd->device->lun;
1471853e2bd2SBhanu Gollapudi 		if (lun == tm_lun) {
1472853e2bd2SBhanu Gollapudi 			/* Initiate ABTS on this cmd */
1473853e2bd2SBhanu Gollapudi 			if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1474853e2bd2SBhanu Gollapudi 					      &cmd->req_flags)) {
1475853e2bd2SBhanu Gollapudi 				/* cancel the IO timeout */
1476853e2bd2SBhanu Gollapudi 				if (cancel_delayed_work(&io_req->timeout_work))
1477853e2bd2SBhanu Gollapudi 					kref_put(&io_req->refcount,
1478853e2bd2SBhanu Gollapudi 						 bnx2fc_cmd_release);
1479853e2bd2SBhanu Gollapudi 							/* timer hold */
1480853e2bd2SBhanu Gollapudi 				rc = bnx2fc_initiate_abts(cmd);
148125985edcSLucas De Marchi 				/* abts shouldn't fail in this context */
1482853e2bd2SBhanu Gollapudi 				WARN_ON(rc != SUCCESS);
1483853e2bd2SBhanu Gollapudi 			} else
1484853e2bd2SBhanu Gollapudi 				printk(KERN_ERR PFX "lun_rst: abts already in"
1485853e2bd2SBhanu Gollapudi 					" progress for this IO 0x%x\n",
1486853e2bd2SBhanu Gollapudi 					cmd->xid);
1487853e2bd2SBhanu Gollapudi 		}
1488853e2bd2SBhanu Gollapudi 	}
1489853e2bd2SBhanu Gollapudi }
1490853e2bd2SBhanu Gollapudi 
1491853e2bd2SBhanu Gollapudi static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1492853e2bd2SBhanu Gollapudi {
1493853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1494d71fb3bdSBhanu Prakash Gollapudi 	struct bnx2fc_cmd *cmd, *tmp;
1495853e2bd2SBhanu Gollapudi 	int rc = 0;
1496853e2bd2SBhanu Gollapudi 
1497853e2bd2SBhanu Gollapudi 	/* called with tgt_lock held */
1498853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1499853e2bd2SBhanu Gollapudi 	/*
1500853e2bd2SBhanu Gollapudi 	 * Walk thru the active_ios queue and ABORT the IO
1501853e2bd2SBhanu Gollapudi 	 * that matches with the LUN that was reset
1502853e2bd2SBhanu Gollapudi 	 */
1503d71fb3bdSBhanu Prakash Gollapudi 	list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1504853e2bd2SBhanu Gollapudi 		BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
1505853e2bd2SBhanu Gollapudi 		/* Initiate ABTS */
1506853e2bd2SBhanu Gollapudi 		if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1507853e2bd2SBhanu Gollapudi 							&cmd->req_flags)) {
1508853e2bd2SBhanu Gollapudi 			/* cancel the IO timeout */
1509853e2bd2SBhanu Gollapudi 			if (cancel_delayed_work(&io_req->timeout_work))
1510853e2bd2SBhanu Gollapudi 				kref_put(&io_req->refcount,
1511853e2bd2SBhanu Gollapudi 					 bnx2fc_cmd_release); /* timer hold */
1512853e2bd2SBhanu Gollapudi 			rc = bnx2fc_initiate_abts(cmd);
151325985edcSLucas De Marchi 			/* abts shouldn't fail in this context */
1514853e2bd2SBhanu Gollapudi 			WARN_ON(rc != SUCCESS);
1515853e2bd2SBhanu Gollapudi 
1516853e2bd2SBhanu Gollapudi 		} else
1517853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1518853e2bd2SBhanu Gollapudi 				" for this IO 0x%x\n", cmd->xid);
1519853e2bd2SBhanu Gollapudi 	}
1520853e2bd2SBhanu Gollapudi }
1521853e2bd2SBhanu Gollapudi 
1522853e2bd2SBhanu Gollapudi void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
1523853e2bd2SBhanu Gollapudi 			     struct fcoe_task_ctx_entry *task, u8 num_rq)
1524853e2bd2SBhanu Gollapudi {
1525853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *tm_req;
1526853e2bd2SBhanu Gollapudi 	struct fc_frame_header *fc_hdr;
1527853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1528853e2bd2SBhanu Gollapudi 	u64 *hdr;
1529853e2bd2SBhanu Gollapudi 	u64 *temp_hdr;
1530853e2bd2SBhanu Gollapudi 	void *rsp_buf;
1531853e2bd2SBhanu Gollapudi 
1532853e2bd2SBhanu Gollapudi 	/* Called with tgt_lock held */
1533853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered process_tm_compl\n");
1534853e2bd2SBhanu Gollapudi 
1535853e2bd2SBhanu Gollapudi 	if (!(test_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags)))
1536853e2bd2SBhanu Gollapudi 		set_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags);
1537853e2bd2SBhanu Gollapudi 	else {
1538853e2bd2SBhanu Gollapudi 		/* TM has already timed out and we got
1539853e2bd2SBhanu Gollapudi 		 * delayed completion. Ignore completion
1540853e2bd2SBhanu Gollapudi 		 * processing.
1541853e2bd2SBhanu Gollapudi 		 */
1542853e2bd2SBhanu Gollapudi 		return;
1543853e2bd2SBhanu Gollapudi 	}
1544853e2bd2SBhanu Gollapudi 
1545853e2bd2SBhanu Gollapudi 	tm_req = &(io_req->mp_req);
1546853e2bd2SBhanu Gollapudi 	fc_hdr = &(tm_req->resp_fc_hdr);
1547853e2bd2SBhanu Gollapudi 	hdr = (u64 *)fc_hdr;
1548853e2bd2SBhanu Gollapudi 	temp_hdr = (u64 *)
1549619c5cb6SVlad Zolotarov 		&task->rxwr_only.union_ctx.comp_info.mp_rsp.fc_hdr;
1550853e2bd2SBhanu Gollapudi 	hdr[0] = cpu_to_be64(temp_hdr[0]);
1551853e2bd2SBhanu Gollapudi 	hdr[1] = cpu_to_be64(temp_hdr[1]);
1552853e2bd2SBhanu Gollapudi 	hdr[2] = cpu_to_be64(temp_hdr[2]);
1553853e2bd2SBhanu Gollapudi 
1554619c5cb6SVlad Zolotarov 	tm_req->resp_len =
1555619c5cb6SVlad Zolotarov 		task->rxwr_only.union_ctx.comp_info.mp_rsp.mp_payload_len;
1556853e2bd2SBhanu Gollapudi 
1557853e2bd2SBhanu Gollapudi 	rsp_buf = tm_req->resp_buf;
1558853e2bd2SBhanu Gollapudi 
1559853e2bd2SBhanu Gollapudi 	if (fc_hdr->fh_r_ctl == FC_RCTL_DD_CMD_STATUS) {
1560853e2bd2SBhanu Gollapudi 		bnx2fc_parse_fcp_rsp(io_req,
1561853e2bd2SBhanu Gollapudi 				     (struct fcoe_fcp_rsp_payload *)
1562853e2bd2SBhanu Gollapudi 				     rsp_buf, num_rq);
1563853e2bd2SBhanu Gollapudi 		if (io_req->fcp_rsp_code == 0) {
1564853e2bd2SBhanu Gollapudi 			/* TM successful */
1565853e2bd2SBhanu Gollapudi 			if (tm_req->tm_flags & FCP_TMF_LUN_RESET)
1566853e2bd2SBhanu Gollapudi 				bnx2fc_lun_reset_cmpl(io_req);
1567853e2bd2SBhanu Gollapudi 			else if (tm_req->tm_flags & FCP_TMF_TGT_RESET)
1568853e2bd2SBhanu Gollapudi 				bnx2fc_tgt_reset_cmpl(io_req);
1569853e2bd2SBhanu Gollapudi 		}
1570853e2bd2SBhanu Gollapudi 	} else {
1571853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "tmf's fc_hdr r_ctl = 0x%x\n",
1572853e2bd2SBhanu Gollapudi 			fc_hdr->fh_r_ctl);
1573853e2bd2SBhanu Gollapudi 	}
1574853e2bd2SBhanu Gollapudi 	if (!sc_cmd->SCp.ptr) {
1575b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "tm_compl: SCp.ptr is NULL\n");
1576853e2bd2SBhanu Gollapudi 		return;
1577853e2bd2SBhanu Gollapudi 	}
1578853e2bd2SBhanu Gollapudi 	switch (io_req->fcp_status) {
1579853e2bd2SBhanu Gollapudi 	case FC_GOOD:
1580853e2bd2SBhanu Gollapudi 		if (io_req->cdb_status == 0) {
1581853e2bd2SBhanu Gollapudi 			/* Good IO completion */
1582853e2bd2SBhanu Gollapudi 			sc_cmd->result = DID_OK << 16;
1583853e2bd2SBhanu Gollapudi 		} else {
1584853e2bd2SBhanu Gollapudi 			/* Transport status is good, SCSI status not good */
1585853e2bd2SBhanu Gollapudi 			sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1586853e2bd2SBhanu Gollapudi 		}
1587853e2bd2SBhanu Gollapudi 		if (io_req->fcp_resid)
1588853e2bd2SBhanu Gollapudi 			scsi_set_resid(sc_cmd, io_req->fcp_resid);
1589853e2bd2SBhanu Gollapudi 		break;
1590853e2bd2SBhanu Gollapudi 
1591853e2bd2SBhanu Gollapudi 	default:
1592853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "process_tm_compl: fcp_status = %d\n",
1593853e2bd2SBhanu Gollapudi 			   io_req->fcp_status);
1594853e2bd2SBhanu Gollapudi 		break;
1595853e2bd2SBhanu Gollapudi 	}
1596853e2bd2SBhanu Gollapudi 
1597853e2bd2SBhanu Gollapudi 	sc_cmd = io_req->sc_cmd;
1598853e2bd2SBhanu Gollapudi 	io_req->sc_cmd = NULL;
1599853e2bd2SBhanu Gollapudi 
1600853e2bd2SBhanu Gollapudi 	/* check if the io_req exists in tgt's tmf_q */
1601853e2bd2SBhanu Gollapudi 	if (io_req->on_tmf_queue) {
1602853e2bd2SBhanu Gollapudi 
1603853e2bd2SBhanu Gollapudi 		list_del_init(&io_req->link);
1604853e2bd2SBhanu Gollapudi 		io_req->on_tmf_queue = 0;
1605853e2bd2SBhanu Gollapudi 	} else {
1606853e2bd2SBhanu Gollapudi 
1607b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "Command not on active_cmd_queue!\n");
1608853e2bd2SBhanu Gollapudi 		return;
1609853e2bd2SBhanu Gollapudi 	}
1610853e2bd2SBhanu Gollapudi 
1611853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = NULL;
1612853e2bd2SBhanu Gollapudi 	sc_cmd->scsi_done(sc_cmd);
1613853e2bd2SBhanu Gollapudi 
1614853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
1615853e2bd2SBhanu Gollapudi 	if (io_req->wait_for_comp) {
1616853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
1617853e2bd2SBhanu Gollapudi 		complete(&io_req->tm_done);
1618853e2bd2SBhanu Gollapudi 	}
1619853e2bd2SBhanu Gollapudi }
1620853e2bd2SBhanu Gollapudi 
1621853e2bd2SBhanu Gollapudi static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
1622853e2bd2SBhanu Gollapudi 			   int bd_index)
1623853e2bd2SBhanu Gollapudi {
1624853e2bd2SBhanu Gollapudi 	struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1625853e2bd2SBhanu Gollapudi 	int frag_size, sg_frags;
1626853e2bd2SBhanu Gollapudi 
1627853e2bd2SBhanu Gollapudi 	sg_frags = 0;
1628853e2bd2SBhanu Gollapudi 	while (sg_len) {
1629853e2bd2SBhanu Gollapudi 		if (sg_len >= BNX2FC_BD_SPLIT_SZ)
1630853e2bd2SBhanu Gollapudi 			frag_size = BNX2FC_BD_SPLIT_SZ;
1631853e2bd2SBhanu Gollapudi 		else
1632853e2bd2SBhanu Gollapudi 			frag_size = sg_len;
1633853e2bd2SBhanu Gollapudi 		bd[bd_index + sg_frags].buf_addr_lo = addr & 0xffffffff;
1634853e2bd2SBhanu Gollapudi 		bd[bd_index + sg_frags].buf_addr_hi  = addr >> 32;
1635853e2bd2SBhanu Gollapudi 		bd[bd_index + sg_frags].buf_len = (u16)frag_size;
1636853e2bd2SBhanu Gollapudi 		bd[bd_index + sg_frags].flags = 0;
1637853e2bd2SBhanu Gollapudi 
1638853e2bd2SBhanu Gollapudi 		addr += (u64) frag_size;
1639853e2bd2SBhanu Gollapudi 		sg_frags++;
1640853e2bd2SBhanu Gollapudi 		sg_len -= frag_size;
1641853e2bd2SBhanu Gollapudi 	}
1642853e2bd2SBhanu Gollapudi 	return sg_frags;
1643853e2bd2SBhanu Gollapudi 
1644853e2bd2SBhanu Gollapudi }
1645853e2bd2SBhanu Gollapudi 
1646853e2bd2SBhanu Gollapudi static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req)
1647853e2bd2SBhanu Gollapudi {
16483ce41ea1SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = io_req->port->priv;
16493ce41ea1SBhanu Prakash Gollapudi 	struct bnx2fc_hba *hba = interface->hba;
1650853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc = io_req->sc_cmd;
1651853e2bd2SBhanu Gollapudi 	struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1652853e2bd2SBhanu Gollapudi 	struct scatterlist *sg;
1653853e2bd2SBhanu Gollapudi 	int byte_count = 0;
1654853e2bd2SBhanu Gollapudi 	int sg_count = 0;
1655853e2bd2SBhanu Gollapudi 	int bd_count = 0;
1656853e2bd2SBhanu Gollapudi 	int sg_frags;
1657853e2bd2SBhanu Gollapudi 	unsigned int sg_len;
1658853e2bd2SBhanu Gollapudi 	u64 addr;
1659853e2bd2SBhanu Gollapudi 	int i;
1660853e2bd2SBhanu Gollapudi 
16615565461eSChad Dupuis 	/*
16625565461eSChad Dupuis 	 * Use dma_map_sg directly to ensure we're using the correct
16635565461eSChad Dupuis 	 * dev struct off of pcidev.
16645565461eSChad Dupuis 	 */
16653ce41ea1SBhanu Prakash Gollapudi 	sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
16663ce41ea1SBhanu Prakash Gollapudi 			      scsi_sg_count(sc), sc->sc_data_direction);
1667853e2bd2SBhanu Gollapudi 	scsi_for_each_sg(sc, sg, sg_count, i) {
1668853e2bd2SBhanu Gollapudi 		sg_len = sg_dma_len(sg);
1669853e2bd2SBhanu Gollapudi 		addr = sg_dma_address(sg);
1670853e2bd2SBhanu Gollapudi 		if (sg_len > BNX2FC_MAX_BD_LEN) {
1671853e2bd2SBhanu Gollapudi 			sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
1672853e2bd2SBhanu Gollapudi 						   bd_count);
1673853e2bd2SBhanu Gollapudi 		} else {
1674853e2bd2SBhanu Gollapudi 
1675853e2bd2SBhanu Gollapudi 			sg_frags = 1;
1676853e2bd2SBhanu Gollapudi 			bd[bd_count].buf_addr_lo = addr & 0xffffffff;
1677853e2bd2SBhanu Gollapudi 			bd[bd_count].buf_addr_hi  = addr >> 32;
1678853e2bd2SBhanu Gollapudi 			bd[bd_count].buf_len = (u16)sg_len;
1679853e2bd2SBhanu Gollapudi 			bd[bd_count].flags = 0;
1680853e2bd2SBhanu Gollapudi 		}
1681853e2bd2SBhanu Gollapudi 		bd_count += sg_frags;
1682853e2bd2SBhanu Gollapudi 		byte_count += sg_len;
1683853e2bd2SBhanu Gollapudi 	}
1684853e2bd2SBhanu Gollapudi 	if (byte_count != scsi_bufflen(sc))
1685853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
1686853e2bd2SBhanu Gollapudi 			"task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
1687853e2bd2SBhanu Gollapudi 			io_req->xid);
1688853e2bd2SBhanu Gollapudi 	return bd_count;
1689853e2bd2SBhanu Gollapudi }
1690853e2bd2SBhanu Gollapudi 
1691822f2903SBhanu Prakash Gollapudi static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
1692853e2bd2SBhanu Gollapudi {
1693853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc = io_req->sc_cmd;
1694853e2bd2SBhanu Gollapudi 	struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1695853e2bd2SBhanu Gollapudi 	int bd_count;
1696853e2bd2SBhanu Gollapudi 
1697822f2903SBhanu Prakash Gollapudi 	if (scsi_sg_count(sc)) {
1698853e2bd2SBhanu Gollapudi 		bd_count = bnx2fc_map_sg(io_req);
1699822f2903SBhanu Prakash Gollapudi 		if (bd_count == 0)
1700822f2903SBhanu Prakash Gollapudi 			return -ENOMEM;
1701822f2903SBhanu Prakash Gollapudi 	} else {
1702853e2bd2SBhanu Gollapudi 		bd_count = 0;
1703853e2bd2SBhanu Gollapudi 		bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1704853e2bd2SBhanu Gollapudi 		bd[0].buf_len = bd[0].flags = 0;
1705853e2bd2SBhanu Gollapudi 	}
1706853e2bd2SBhanu Gollapudi 	io_req->bd_tbl->bd_valid = bd_count;
1707822f2903SBhanu Prakash Gollapudi 
1708822f2903SBhanu Prakash Gollapudi 	return 0;
1709853e2bd2SBhanu Gollapudi }
1710853e2bd2SBhanu Gollapudi 
1711853e2bd2SBhanu Gollapudi static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1712853e2bd2SBhanu Gollapudi {
1713853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc = io_req->sc_cmd;
17145565461eSChad Dupuis 	struct bnx2fc_interface *interface = io_req->port->priv;
17155565461eSChad Dupuis 	struct bnx2fc_hba *hba = interface->hba;
1716853e2bd2SBhanu Gollapudi 
17175565461eSChad Dupuis 	/*
17185565461eSChad Dupuis 	 * Use dma_unmap_sg directly to ensure we're using the correct
17195565461eSChad Dupuis 	 * dev struct off of pcidev.
17205565461eSChad Dupuis 	 */
17215565461eSChad Dupuis 	if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
17225565461eSChad Dupuis 		dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc),
17235565461eSChad Dupuis 		    scsi_sg_count(sc), sc->sc_data_direction);
1724853e2bd2SBhanu Gollapudi 		io_req->bd_tbl->bd_valid = 0;
1725853e2bd2SBhanu Gollapudi 	}
1726853e2bd2SBhanu Gollapudi }
1727853e2bd2SBhanu Gollapudi 
1728853e2bd2SBhanu Gollapudi void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1729853e2bd2SBhanu Gollapudi 				  struct fcp_cmnd *fcp_cmnd)
1730853e2bd2SBhanu Gollapudi {
1731853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1732853e2bd2SBhanu Gollapudi 
1733853e2bd2SBhanu Gollapudi 	memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1734853e2bd2SBhanu Gollapudi 
1735e35fa8c2SAndy Grover 	int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
1736853e2bd2SBhanu Gollapudi 
1737853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1738853e2bd2SBhanu Gollapudi 	memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1739853e2bd2SBhanu Gollapudi 
1740853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_cmdref = 0;
1741853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_pri_ta = 0;
1742853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
1743853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_flags = io_req->io_req_flags;
1744853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
1745853e2bd2SBhanu Gollapudi }
1746853e2bd2SBhanu Gollapudi 
1747853e2bd2SBhanu Gollapudi static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1748853e2bd2SBhanu Gollapudi 				 struct fcoe_fcp_rsp_payload *fcp_rsp,
1749853e2bd2SBhanu Gollapudi 				 u8 num_rq)
1750853e2bd2SBhanu Gollapudi {
1751853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1752853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1753853e2bd2SBhanu Gollapudi 	u8 rsp_flags = fcp_rsp->fcp_flags.flags;
1754853e2bd2SBhanu Gollapudi 	u32 rq_buff_len = 0;
1755853e2bd2SBhanu Gollapudi 	int i;
1756853e2bd2SBhanu Gollapudi 	unsigned char *rq_data;
1757853e2bd2SBhanu Gollapudi 	unsigned char *dummy;
1758853e2bd2SBhanu Gollapudi 	int fcp_sns_len = 0;
1759853e2bd2SBhanu Gollapudi 	int fcp_rsp_len = 0;
1760853e2bd2SBhanu Gollapudi 
1761853e2bd2SBhanu Gollapudi 	io_req->fcp_status = FC_GOOD;
1762adcf7dfbSChad Dupuis 	io_req->fcp_resid = 0;
1763adcf7dfbSChad Dupuis 	if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
1764adcf7dfbSChad Dupuis 	    FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
1765853e2bd2SBhanu Gollapudi 		io_req->fcp_resid = fcp_rsp->fcp_resid;
1766853e2bd2SBhanu Gollapudi 
1767853e2bd2SBhanu Gollapudi 	io_req->scsi_comp_flags = rsp_flags;
1768853e2bd2SBhanu Gollapudi 	CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1769853e2bd2SBhanu Gollapudi 				fcp_rsp->scsi_status_code;
1770853e2bd2SBhanu Gollapudi 
1771853e2bd2SBhanu Gollapudi 	/* Fetch fcp_rsp_info and fcp_sns_info if available */
1772853e2bd2SBhanu Gollapudi 	if (num_rq) {
1773853e2bd2SBhanu Gollapudi 
1774853e2bd2SBhanu Gollapudi 		/*
1775853e2bd2SBhanu Gollapudi 		 * We do not anticipate num_rq >1, as the linux defined
1776853e2bd2SBhanu Gollapudi 		 * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
1777853e2bd2SBhanu Gollapudi 		 * 256 bytes of single rq buffer is good enough to hold this.
1778853e2bd2SBhanu Gollapudi 		 */
1779853e2bd2SBhanu Gollapudi 
1780853e2bd2SBhanu Gollapudi 		if (rsp_flags &
1781853e2bd2SBhanu Gollapudi 		    FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1782853e2bd2SBhanu Gollapudi 			fcp_rsp_len = rq_buff_len
1783853e2bd2SBhanu Gollapudi 					= fcp_rsp->fcp_rsp_len;
1784853e2bd2SBhanu Gollapudi 		}
1785853e2bd2SBhanu Gollapudi 
1786853e2bd2SBhanu Gollapudi 		if (rsp_flags &
1787853e2bd2SBhanu Gollapudi 		    FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
1788853e2bd2SBhanu Gollapudi 			fcp_sns_len = fcp_rsp->fcp_sns_len;
1789853e2bd2SBhanu Gollapudi 			rq_buff_len += fcp_rsp->fcp_sns_len;
1790853e2bd2SBhanu Gollapudi 		}
1791853e2bd2SBhanu Gollapudi 
1792853e2bd2SBhanu Gollapudi 		io_req->fcp_rsp_len = fcp_rsp_len;
1793853e2bd2SBhanu Gollapudi 		io_req->fcp_sns_len = fcp_sns_len;
1794853e2bd2SBhanu Gollapudi 
1795853e2bd2SBhanu Gollapudi 		if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
1796853e2bd2SBhanu Gollapudi 			/* Invalid sense sense length. */
1797b2a554ffSBhanu Prakash Gollapudi 			printk(KERN_ERR PFX "invalid sns length %d\n",
1798853e2bd2SBhanu Gollapudi 				rq_buff_len);
1799853e2bd2SBhanu Gollapudi 			/* reset rq_buff_len */
1800853e2bd2SBhanu Gollapudi 			rq_buff_len =  num_rq * BNX2FC_RQ_BUF_SZ;
1801853e2bd2SBhanu Gollapudi 		}
1802853e2bd2SBhanu Gollapudi 
1803853e2bd2SBhanu Gollapudi 		rq_data = bnx2fc_get_next_rqe(tgt, 1);
1804853e2bd2SBhanu Gollapudi 
1805853e2bd2SBhanu Gollapudi 		if (num_rq > 1) {
1806853e2bd2SBhanu Gollapudi 			/* We do not need extra sense data */
1807853e2bd2SBhanu Gollapudi 			for (i = 1; i < num_rq; i++)
1808853e2bd2SBhanu Gollapudi 				dummy = bnx2fc_get_next_rqe(tgt, 1);
1809853e2bd2SBhanu Gollapudi 		}
1810853e2bd2SBhanu Gollapudi 
1811853e2bd2SBhanu Gollapudi 		/* fetch fcp_rsp_code */
1812853e2bd2SBhanu Gollapudi 		if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1813853e2bd2SBhanu Gollapudi 			/* Only for task management function */
1814853e2bd2SBhanu Gollapudi 			io_req->fcp_rsp_code = rq_data[3];
1815853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "fcp_rsp_code = %d\n",
1816853e2bd2SBhanu Gollapudi 				io_req->fcp_rsp_code);
1817853e2bd2SBhanu Gollapudi 		}
1818853e2bd2SBhanu Gollapudi 
1819853e2bd2SBhanu Gollapudi 		/* fetch sense data */
1820853e2bd2SBhanu Gollapudi 		rq_data += fcp_rsp_len;
1821853e2bd2SBhanu Gollapudi 
1822853e2bd2SBhanu Gollapudi 		if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1823853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "Truncating sense buffer\n");
1824853e2bd2SBhanu Gollapudi 			fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1825853e2bd2SBhanu Gollapudi 		}
1826853e2bd2SBhanu Gollapudi 
182716da05b1SAndi Kleen 		memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1828853e2bd2SBhanu Gollapudi 		if (fcp_sns_len)
1829853e2bd2SBhanu Gollapudi 			memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1830853e2bd2SBhanu Gollapudi 
1831853e2bd2SBhanu Gollapudi 		/* return RQ entries */
1832853e2bd2SBhanu Gollapudi 		for (i = 0; i < num_rq; i++)
1833853e2bd2SBhanu Gollapudi 			bnx2fc_return_rqe(tgt, 1);
1834853e2bd2SBhanu Gollapudi 	}
1835853e2bd2SBhanu Gollapudi }
1836853e2bd2SBhanu Gollapudi 
1837853e2bd2SBhanu Gollapudi /**
1838853e2bd2SBhanu Gollapudi  * bnx2fc_queuecommand - Queuecommand function of the scsi template
1839853e2bd2SBhanu Gollapudi  *
1840853e2bd2SBhanu Gollapudi  * @host:	The Scsi_Host the command was issued to
1841853e2bd2SBhanu Gollapudi  * @sc_cmd:	struct scsi_cmnd to be executed
1842853e2bd2SBhanu Gollapudi  *
1843853e2bd2SBhanu Gollapudi  * This is the IO strategy routine, called by SCSI-ML
1844853e2bd2SBhanu Gollapudi  **/
1845853e2bd2SBhanu Gollapudi int bnx2fc_queuecommand(struct Scsi_Host *host,
1846853e2bd2SBhanu Gollapudi 			struct scsi_cmnd *sc_cmd)
1847853e2bd2SBhanu Gollapudi {
1848853e2bd2SBhanu Gollapudi 	struct fc_lport *lport = shost_priv(host);
1849853e2bd2SBhanu Gollapudi 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1850853e2bd2SBhanu Gollapudi 	struct fc_rport_libfc_priv *rp = rport->dd_data;
1851853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt;
1852853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
1853853e2bd2SBhanu Gollapudi 	int rc = 0;
1854853e2bd2SBhanu Gollapudi 	int rval;
1855853e2bd2SBhanu Gollapudi 
1856853e2bd2SBhanu Gollapudi 	rval = fc_remote_port_chkready(rport);
1857853e2bd2SBhanu Gollapudi 	if (rval) {
1858853e2bd2SBhanu Gollapudi 		sc_cmd->result = rval;
1859853e2bd2SBhanu Gollapudi 		sc_cmd->scsi_done(sc_cmd);
1860853e2bd2SBhanu Gollapudi 		return 0;
1861853e2bd2SBhanu Gollapudi 	}
1862853e2bd2SBhanu Gollapudi 
1863853e2bd2SBhanu Gollapudi 	if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1864853e2bd2SBhanu Gollapudi 		rc = SCSI_MLQUEUE_HOST_BUSY;
1865853e2bd2SBhanu Gollapudi 		goto exit_qcmd;
1866853e2bd2SBhanu Gollapudi 	}
1867853e2bd2SBhanu Gollapudi 
1868853e2bd2SBhanu Gollapudi 	/* rport and tgt are allocated together, so tgt should be non-NULL */
1869853e2bd2SBhanu Gollapudi 	tgt = (struct bnx2fc_rport *)&rp[1];
1870853e2bd2SBhanu Gollapudi 
1871853e2bd2SBhanu Gollapudi 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1872853e2bd2SBhanu Gollapudi 		/*
1873853e2bd2SBhanu Gollapudi 		 * Session is not offloaded yet. Let SCSI-ml retry
1874853e2bd2SBhanu Gollapudi 		 * the command.
1875853e2bd2SBhanu Gollapudi 		 */
1876853e2bd2SBhanu Gollapudi 		rc = SCSI_MLQUEUE_TARGET_BUSY;
1877853e2bd2SBhanu Gollapudi 		goto exit_qcmd;
1878853e2bd2SBhanu Gollapudi 	}
1879245a5754SEddie Wai 	if (tgt->retry_delay_timestamp) {
1880245a5754SEddie Wai 		if (time_after(jiffies, tgt->retry_delay_timestamp)) {
1881245a5754SEddie Wai 			tgt->retry_delay_timestamp = 0;
1882245a5754SEddie Wai 		} else {
1883245a5754SEddie Wai 			/* If retry_delay timer is active, flow off the ML */
1884245a5754SEddie Wai 			rc = SCSI_MLQUEUE_TARGET_BUSY;
1885245a5754SEddie Wai 			goto exit_qcmd;
1886245a5754SEddie Wai 		}
1887245a5754SEddie Wai 	}
1888dc6311ddSMaurizio Lombardi 
1889dc6311ddSMaurizio Lombardi 	spin_lock_bh(&tgt->tgt_lock);
1890dc6311ddSMaurizio Lombardi 
1891853e2bd2SBhanu Gollapudi 	io_req = bnx2fc_cmd_alloc(tgt);
1892853e2bd2SBhanu Gollapudi 	if (!io_req) {
1893853e2bd2SBhanu Gollapudi 		rc = SCSI_MLQUEUE_HOST_BUSY;
1894dc6311ddSMaurizio Lombardi 		goto exit_qcmd_tgtlock;
1895853e2bd2SBhanu Gollapudi 	}
1896853e2bd2SBhanu Gollapudi 	io_req->sc_cmd = sc_cmd;
1897853e2bd2SBhanu Gollapudi 
1898853e2bd2SBhanu Gollapudi 	if (bnx2fc_post_io_req(tgt, io_req)) {
1899853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Unable to post io_req\n");
1900853e2bd2SBhanu Gollapudi 		rc = SCSI_MLQUEUE_HOST_BUSY;
1901dc6311ddSMaurizio Lombardi 		goto exit_qcmd_tgtlock;
1902853e2bd2SBhanu Gollapudi 	}
1903dc6311ddSMaurizio Lombardi 
1904dc6311ddSMaurizio Lombardi exit_qcmd_tgtlock:
1905dc6311ddSMaurizio Lombardi 	spin_unlock_bh(&tgt->tgt_lock);
1906853e2bd2SBhanu Gollapudi exit_qcmd:
1907853e2bd2SBhanu Gollapudi 	return rc;
1908853e2bd2SBhanu Gollapudi }
1909853e2bd2SBhanu Gollapudi 
1910853e2bd2SBhanu Gollapudi void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1911853e2bd2SBhanu Gollapudi 				   struct fcoe_task_ctx_entry *task,
1912853e2bd2SBhanu Gollapudi 				   u8 num_rq)
1913853e2bd2SBhanu Gollapudi {
1914853e2bd2SBhanu Gollapudi 	struct fcoe_fcp_rsp_payload *fcp_rsp;
1915853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1916853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd;
1917853e2bd2SBhanu Gollapudi 	struct Scsi_Host *host;
1918853e2bd2SBhanu Gollapudi 
1919853e2bd2SBhanu Gollapudi 
1920853e2bd2SBhanu Gollapudi 	/* scsi_cmd_cmpl is called with tgt lock held */
1921853e2bd2SBhanu Gollapudi 
1922853e2bd2SBhanu Gollapudi 	if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1923853e2bd2SBhanu Gollapudi 		/* we will not receive ABTS response for this IO */
1924853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "Timer context finished processing "
1925853e2bd2SBhanu Gollapudi 			   "this scsi cmd\n");
1926853e2bd2SBhanu Gollapudi 	}
1927853e2bd2SBhanu Gollapudi 
1928853e2bd2SBhanu Gollapudi 	/* Cancel the timeout_work, as we received IO completion */
1929853e2bd2SBhanu Gollapudi 	if (cancel_delayed_work(&io_req->timeout_work))
1930853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount,
1931853e2bd2SBhanu Gollapudi 			 bnx2fc_cmd_release); /* drop timer hold */
1932853e2bd2SBhanu Gollapudi 
1933853e2bd2SBhanu Gollapudi 	sc_cmd = io_req->sc_cmd;
1934853e2bd2SBhanu Gollapudi 	if (sc_cmd == NULL) {
1935853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
1936853e2bd2SBhanu Gollapudi 		return;
1937853e2bd2SBhanu Gollapudi 	}
1938853e2bd2SBhanu Gollapudi 
1939853e2bd2SBhanu Gollapudi 	/* Fetch fcp_rsp from task context and perform cmd completion */
1940853e2bd2SBhanu Gollapudi 	fcp_rsp = (struct fcoe_fcp_rsp_payload *)
1941619c5cb6SVlad Zolotarov 		   &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
1942853e2bd2SBhanu Gollapudi 
1943853e2bd2SBhanu Gollapudi 	/* parse fcp_rsp and obtain sense data from RQ if available */
1944853e2bd2SBhanu Gollapudi 	bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq);
1945853e2bd2SBhanu Gollapudi 
1946853e2bd2SBhanu Gollapudi 	host = sc_cmd->device->host;
1947853e2bd2SBhanu Gollapudi 	if (!sc_cmd->SCp.ptr) {
1948853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "SCp.ptr is NULL\n");
1949853e2bd2SBhanu Gollapudi 		return;
1950853e2bd2SBhanu Gollapudi 	}
1951853e2bd2SBhanu Gollapudi 
1952853e2bd2SBhanu Gollapudi 	if (io_req->on_active_queue) {
1953853e2bd2SBhanu Gollapudi 		list_del_init(&io_req->link);
1954853e2bd2SBhanu Gollapudi 		io_req->on_active_queue = 0;
1955853e2bd2SBhanu Gollapudi 		/* Move IO req to retire queue */
1956853e2bd2SBhanu Gollapudi 		list_add_tail(&io_req->link, &tgt->io_retire_queue);
1957853e2bd2SBhanu Gollapudi 	} else {
1958853e2bd2SBhanu Gollapudi 		/* This should not happen, but could have been pulled
1959853e2bd2SBhanu Gollapudi 		 * by bnx2fc_flush_active_ios(), or during a race
1960853e2bd2SBhanu Gollapudi 		 * between command abort and (late) completion.
1961853e2bd2SBhanu Gollapudi 		 */
1962853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
1963853e2bd2SBhanu Gollapudi 		if (io_req->wait_for_comp)
1964853e2bd2SBhanu Gollapudi 			if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1965853e2bd2SBhanu Gollapudi 					       &io_req->req_flags))
1966853e2bd2SBhanu Gollapudi 				complete(&io_req->tm_done);
1967853e2bd2SBhanu Gollapudi 	}
1968853e2bd2SBhanu Gollapudi 
1969853e2bd2SBhanu Gollapudi 	bnx2fc_unmap_sg_list(io_req);
1970b5a95fe7SBhanu Prakash Gollapudi 	io_req->sc_cmd = NULL;
1971853e2bd2SBhanu Gollapudi 
1972853e2bd2SBhanu Gollapudi 	switch (io_req->fcp_status) {
1973853e2bd2SBhanu Gollapudi 	case FC_GOOD:
1974853e2bd2SBhanu Gollapudi 		if (io_req->cdb_status == 0) {
1975853e2bd2SBhanu Gollapudi 			/* Good IO completion */
1976853e2bd2SBhanu Gollapudi 			sc_cmd->result = DID_OK << 16;
1977853e2bd2SBhanu Gollapudi 		} else {
1978853e2bd2SBhanu Gollapudi 			/* Transport status is good, SCSI status not good */
1979853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
1980853e2bd2SBhanu Gollapudi 				 " fcp_resid = 0x%x\n",
1981853e2bd2SBhanu Gollapudi 				io_req->cdb_status, io_req->fcp_resid);
1982853e2bd2SBhanu Gollapudi 			sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1983245a5754SEddie Wai 
1984245a5754SEddie Wai 			if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
1985245a5754SEddie Wai 			    io_req->cdb_status == SAM_STAT_BUSY) {
1986245a5754SEddie Wai 				/* Set the jiffies + retry_delay_timer * 100ms
1987245a5754SEddie Wai 				   for the rport/tgt */
1988245a5754SEddie Wai 				tgt->retry_delay_timestamp = jiffies +
1989245a5754SEddie Wai 					fcp_rsp->retry_delay_timer * HZ / 10;
1990245a5754SEddie Wai 			}
1991245a5754SEddie Wai 
1992853e2bd2SBhanu Gollapudi 		}
1993853e2bd2SBhanu Gollapudi 		if (io_req->fcp_resid)
1994853e2bd2SBhanu Gollapudi 			scsi_set_resid(sc_cmd, io_req->fcp_resid);
1995853e2bd2SBhanu Gollapudi 		break;
1996853e2bd2SBhanu Gollapudi 	default:
1997b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
1998853e2bd2SBhanu Gollapudi 			io_req->fcp_status);
1999853e2bd2SBhanu Gollapudi 		break;
2000853e2bd2SBhanu Gollapudi 	}
2001853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = NULL;
2002853e2bd2SBhanu Gollapudi 	sc_cmd->scsi_done(sc_cmd);
2003853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
2004853e2bd2SBhanu Gollapudi }
2005853e2bd2SBhanu Gollapudi 
200674446954SBhanu Prakash Gollapudi int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
2007853e2bd2SBhanu Gollapudi 			       struct bnx2fc_cmd *io_req)
2008853e2bd2SBhanu Gollapudi {
2009853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task;
2010853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task_page;
2011853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
2012853e2bd2SBhanu Gollapudi 	struct fcoe_port *port = tgt->port;
2013aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = port->priv;
2014aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_hba *hba = interface->hba;
2015853e2bd2SBhanu Gollapudi 	struct fc_lport *lport = port->lport;
20161bd49b48SVasu Dev 	struct fc_stats *stats;
2017853e2bd2SBhanu Gollapudi 	int task_idx, index;
2018853e2bd2SBhanu Gollapudi 	u16 xid;
2019853e2bd2SBhanu Gollapudi 
2020dc6311ddSMaurizio Lombardi 	/* bnx2fc_post_io_req() is called with the tgt_lock held */
2021dc6311ddSMaurizio Lombardi 
2022853e2bd2SBhanu Gollapudi 	/* Initialize rest of io_req fields */
2023853e2bd2SBhanu Gollapudi 	io_req->cmd_type = BNX2FC_SCSI_CMD;
2024853e2bd2SBhanu Gollapudi 	io_req->port = port;
2025853e2bd2SBhanu Gollapudi 	io_req->tgt = tgt;
2026853e2bd2SBhanu Gollapudi 	io_req->data_xfer_len = scsi_bufflen(sc_cmd);
2027853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = (char *)io_req;
2028853e2bd2SBhanu Gollapudi 
20291bd49b48SVasu Dev 	stats = per_cpu_ptr(lport->stats, get_cpu());
2030853e2bd2SBhanu Gollapudi 	if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
2031853e2bd2SBhanu Gollapudi 		io_req->io_req_flags = BNX2FC_READ;
2032853e2bd2SBhanu Gollapudi 		stats->InputRequests++;
2033853e2bd2SBhanu Gollapudi 		stats->InputBytes += io_req->data_xfer_len;
2034853e2bd2SBhanu Gollapudi 	} else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
2035853e2bd2SBhanu Gollapudi 		io_req->io_req_flags = BNX2FC_WRITE;
2036853e2bd2SBhanu Gollapudi 		stats->OutputRequests++;
2037853e2bd2SBhanu Gollapudi 		stats->OutputBytes += io_req->data_xfer_len;
2038853e2bd2SBhanu Gollapudi 	} else {
2039853e2bd2SBhanu Gollapudi 		io_req->io_req_flags = 0;
2040853e2bd2SBhanu Gollapudi 		stats->ControlRequests++;
2041853e2bd2SBhanu Gollapudi 	}
2042853e2bd2SBhanu Gollapudi 	put_cpu();
2043853e2bd2SBhanu Gollapudi 
2044853e2bd2SBhanu Gollapudi 	xid = io_req->xid;
2045853e2bd2SBhanu Gollapudi 
2046853e2bd2SBhanu Gollapudi 	/* Build buffer descriptor list for firmware from sg list */
2047822f2903SBhanu Prakash Gollapudi 	if (bnx2fc_build_bd_list_from_sg(io_req)) {
2048822f2903SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "BD list creation failed\n");
2049822f2903SBhanu Prakash Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2050822f2903SBhanu Prakash Gollapudi 		return -EAGAIN;
2051822f2903SBhanu Prakash Gollapudi 	}
2052853e2bd2SBhanu Gollapudi 
2053853e2bd2SBhanu Gollapudi 	task_idx = xid / BNX2FC_TASKS_PER_PAGE;
2054853e2bd2SBhanu Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
2055853e2bd2SBhanu Gollapudi 
2056853e2bd2SBhanu Gollapudi 	/* Initialize task context for this IO request */
2057853e2bd2SBhanu Gollapudi 	task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
2058853e2bd2SBhanu Gollapudi 	task = &(task_page[index]);
2059853e2bd2SBhanu Gollapudi 	bnx2fc_init_task(io_req, task);
2060853e2bd2SBhanu Gollapudi 
2061853e2bd2SBhanu Gollapudi 	if (tgt->flush_in_prog) {
2062853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
2063853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2064853e2bd2SBhanu Gollapudi 		return -EAGAIN;
2065853e2bd2SBhanu Gollapudi 	}
2066853e2bd2SBhanu Gollapudi 
2067853e2bd2SBhanu Gollapudi 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
2068853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Session not ready...post_io\n");
2069853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2070853e2bd2SBhanu Gollapudi 		return -EAGAIN;
2071853e2bd2SBhanu Gollapudi 	}
2072853e2bd2SBhanu Gollapudi 
2073853e2bd2SBhanu Gollapudi 	/* Time IO req */
2074b252f4c7SBhanu Prakash Gollapudi 	if (tgt->io_timeout)
2075853e2bd2SBhanu Gollapudi 		bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
2076853e2bd2SBhanu Gollapudi 	/* Obtain free SQ entry */
2077853e2bd2SBhanu Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
2078853e2bd2SBhanu Gollapudi 
2079853e2bd2SBhanu Gollapudi 	/* Enqueue the io_req to active_cmd_queue */
2080853e2bd2SBhanu Gollapudi 
2081853e2bd2SBhanu Gollapudi 	io_req->on_active_queue = 1;
2082853e2bd2SBhanu Gollapudi 	/* move io_req from pending_queue to active_queue */
2083853e2bd2SBhanu Gollapudi 	list_add_tail(&io_req->link, &tgt->active_cmd_queue);
2084853e2bd2SBhanu Gollapudi 
2085853e2bd2SBhanu Gollapudi 	/* Ring doorbell */
2086853e2bd2SBhanu Gollapudi 	bnx2fc_ring_doorbell(tgt);
2087853e2bd2SBhanu Gollapudi 	return 0;
2088853e2bd2SBhanu Gollapudi }
2089