xref: /openbmc/linux/drivers/scsi/bnx2fc/bnx2fc_io.c (revision 886a0b54)
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
521144b80SChad Dupuis  * Copyright (c) 2014-2016 QLogic Corporation
621144b80SChad Dupuis  * Copyright (c) 2016-2017 Cavium Inc.
7853e2bd2SBhanu Gollapudi  *
8853e2bd2SBhanu Gollapudi  * This program is free software; you can redistribute it and/or modify
9853e2bd2SBhanu Gollapudi  * it under the terms of the GNU General Public License as published by
10853e2bd2SBhanu Gollapudi  * the Free Software Foundation.
11853e2bd2SBhanu Gollapudi  *
12853e2bd2SBhanu Gollapudi  * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
13853e2bd2SBhanu Gollapudi  */
14853e2bd2SBhanu Gollapudi 
15853e2bd2SBhanu Gollapudi #include "bnx2fc.h"
160ea5c275SBhanu Gollapudi 
170ea5c275SBhanu Gollapudi #define RESERVE_FREE_LIST_INDEX num_possible_cpus()
180ea5c275SBhanu Gollapudi 
19853e2bd2SBhanu Gollapudi static int bnx2fc_split_bd(struct bnx2fc_cmd *io_req, u64 addr, int sg_len,
20853e2bd2SBhanu Gollapudi 			   int bd_index);
21853e2bd2SBhanu Gollapudi static int bnx2fc_map_sg(struct bnx2fc_cmd *io_req);
22822f2903SBhanu Prakash Gollapudi static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req);
23853e2bd2SBhanu Gollapudi static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req);
24853e2bd2SBhanu Gollapudi static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req);
25853e2bd2SBhanu Gollapudi static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
26853e2bd2SBhanu Gollapudi 				 struct fcoe_fcp_rsp_payload *fcp_rsp,
2777331115SJaved Hasan 				 u8 num_rq, unsigned char *rq_data);
28853e2bd2SBhanu Gollapudi 
29853e2bd2SBhanu Gollapudi void bnx2fc_cmd_timer_set(struct bnx2fc_cmd *io_req,
30853e2bd2SBhanu Gollapudi 			  unsigned int timer_msec)
31853e2bd2SBhanu Gollapudi {
32aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = io_req->port->priv;
33853e2bd2SBhanu Gollapudi 
34aea71a02SBhanu Prakash Gollapudi 	if (queue_delayed_work(interface->timer_work_queue,
35aea71a02SBhanu Prakash Gollapudi 			       &io_req->timeout_work,
36853e2bd2SBhanu Gollapudi 			       msecs_to_jiffies(timer_msec)))
37853e2bd2SBhanu Gollapudi 		kref_get(&io_req->refcount);
38853e2bd2SBhanu Gollapudi }
39853e2bd2SBhanu Gollapudi 
40853e2bd2SBhanu Gollapudi static void bnx2fc_cmd_timeout(struct work_struct *work)
41853e2bd2SBhanu Gollapudi {
42853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req = container_of(work, struct bnx2fc_cmd,
43853e2bd2SBhanu Gollapudi 						 timeout_work.work);
44853e2bd2SBhanu Gollapudi 	u8 cmd_type = io_req->cmd_type;
45853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
46853e2bd2SBhanu Gollapudi 	int rc;
47853e2bd2SBhanu Gollapudi 
48853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "cmd_timeout, cmd_type = %d,"
49853e2bd2SBhanu Gollapudi 		      "req_flags = %lx\n", cmd_type, io_req->req_flags);
50853e2bd2SBhanu Gollapudi 
51853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
52853e2bd2SBhanu Gollapudi 	if (test_and_clear_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags)) {
53853e2bd2SBhanu Gollapudi 		clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
54853e2bd2SBhanu Gollapudi 		/*
55853e2bd2SBhanu Gollapudi 		 * ideally we should hold the io_req until RRQ complets,
56853e2bd2SBhanu Gollapudi 		 * and release io_req from timeout hold.
57853e2bd2SBhanu Gollapudi 		 */
58853e2bd2SBhanu Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
59853e2bd2SBhanu Gollapudi 		bnx2fc_send_rrq(io_req);
60853e2bd2SBhanu Gollapudi 		return;
61853e2bd2SBhanu Gollapudi 	}
62853e2bd2SBhanu Gollapudi 	if (test_and_clear_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags)) {
63853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "IO ready for reuse now\n");
64853e2bd2SBhanu Gollapudi 		goto done;
65853e2bd2SBhanu Gollapudi 	}
66853e2bd2SBhanu Gollapudi 
67853e2bd2SBhanu Gollapudi 	switch (cmd_type) {
68853e2bd2SBhanu Gollapudi 	case BNX2FC_SCSI_CMD:
69853e2bd2SBhanu Gollapudi 		if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
70853e2bd2SBhanu Gollapudi 							&io_req->req_flags)) {
71853e2bd2SBhanu Gollapudi 			/* Handle eh_abort timeout */
72853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "eh_abort timed out\n");
730e0fcef9SSaurav Kashyap 			complete(&io_req->abts_done);
74853e2bd2SBhanu Gollapudi 		} else if (test_bit(BNX2FC_FLAG_ISSUE_ABTS,
75853e2bd2SBhanu Gollapudi 				    &io_req->req_flags)) {
76853e2bd2SBhanu Gollapudi 			/* Handle internally generated ABTS timeout */
77853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "ABTS timed out refcnt = %d\n",
782c935bc5SPeter Zijlstra 					kref_read(&io_req->refcount));
79853e2bd2SBhanu Gollapudi 			if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
80853e2bd2SBhanu Gollapudi 					       &io_req->req_flags))) {
8150a87414SChad Dupuis 				/*
8250a87414SChad Dupuis 				 * Cleanup and return original command to
8350a87414SChad Dupuis 				 * mid-layer.
8450a87414SChad Dupuis 				 */
8550a87414SChad Dupuis 				bnx2fc_initiate_cleanup(io_req);
86853e2bd2SBhanu Gollapudi 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
87853e2bd2SBhanu Gollapudi 				spin_unlock_bh(&tgt->tgt_lock);
88853e2bd2SBhanu Gollapudi 
89853e2bd2SBhanu Gollapudi 				return;
90853e2bd2SBhanu Gollapudi 			}
91853e2bd2SBhanu Gollapudi 		} else {
92853e2bd2SBhanu Gollapudi 			/* Hanlde IO timeout */
93853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "IO timed out. issue ABTS\n");
94853e2bd2SBhanu Gollapudi 			if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL,
95853e2bd2SBhanu Gollapudi 					     &io_req->req_flags)) {
96853e2bd2SBhanu Gollapudi 				BNX2FC_IO_DBG(io_req, "IO completed before "
97853e2bd2SBhanu Gollapudi 							   " timer expiry\n");
98853e2bd2SBhanu Gollapudi 				goto done;
99853e2bd2SBhanu Gollapudi 			}
100853e2bd2SBhanu Gollapudi 
101853e2bd2SBhanu Gollapudi 			if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
102853e2bd2SBhanu Gollapudi 					      &io_req->req_flags)) {
103853e2bd2SBhanu Gollapudi 				rc = bnx2fc_initiate_abts(io_req);
104853e2bd2SBhanu Gollapudi 				if (rc == SUCCESS)
105853e2bd2SBhanu Gollapudi 					goto done;
10650a87414SChad Dupuis 
107853e2bd2SBhanu Gollapudi 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
108853e2bd2SBhanu Gollapudi 				spin_unlock_bh(&tgt->tgt_lock);
109853e2bd2SBhanu Gollapudi 
110853e2bd2SBhanu Gollapudi 				return;
111853e2bd2SBhanu Gollapudi 			} else {
112853e2bd2SBhanu Gollapudi 				BNX2FC_IO_DBG(io_req, "IO already in "
113853e2bd2SBhanu Gollapudi 						      "ABTS processing\n");
114853e2bd2SBhanu Gollapudi 			}
115853e2bd2SBhanu Gollapudi 		}
116853e2bd2SBhanu Gollapudi 		break;
117853e2bd2SBhanu Gollapudi 	case BNX2FC_ELS:
118853e2bd2SBhanu Gollapudi 
119853e2bd2SBhanu Gollapudi 		if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
120853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "ABTS for ELS timed out\n");
121853e2bd2SBhanu Gollapudi 
122853e2bd2SBhanu Gollapudi 			if (!test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
123853e2bd2SBhanu Gollapudi 					      &io_req->req_flags)) {
124853e2bd2SBhanu Gollapudi 				kref_put(&io_req->refcount, bnx2fc_cmd_release);
125853e2bd2SBhanu Gollapudi 				spin_unlock_bh(&tgt->tgt_lock);
126853e2bd2SBhanu Gollapudi 
127853e2bd2SBhanu Gollapudi 				return;
128853e2bd2SBhanu Gollapudi 			}
129853e2bd2SBhanu Gollapudi 		} else {
130853e2bd2SBhanu Gollapudi 			/*
131853e2bd2SBhanu Gollapudi 			 * Handle ELS timeout.
132853e2bd2SBhanu Gollapudi 			 * tgt_lock is used to sync compl path and timeout
133853e2bd2SBhanu Gollapudi 			 * path. If els compl path is processing this IO, we
134853e2bd2SBhanu Gollapudi 			 * have nothing to do here, just release the timer hold
135853e2bd2SBhanu Gollapudi 			 */
136853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "ELS timed out\n");
137853e2bd2SBhanu Gollapudi 			if (test_and_set_bit(BNX2FC_FLAG_ELS_DONE,
138853e2bd2SBhanu Gollapudi 					       &io_req->req_flags))
139853e2bd2SBhanu Gollapudi 				goto done;
140853e2bd2SBhanu Gollapudi 
141853e2bd2SBhanu Gollapudi 			/* Indicate the cb_func that this ELS is timed out */
142853e2bd2SBhanu Gollapudi 			set_bit(BNX2FC_FLAG_ELS_TIMEOUT, &io_req->req_flags);
143853e2bd2SBhanu Gollapudi 
144853e2bd2SBhanu Gollapudi 			if ((io_req->cb_func) && (io_req->cb_arg)) {
145853e2bd2SBhanu Gollapudi 				io_req->cb_func(io_req->cb_arg);
146853e2bd2SBhanu Gollapudi 				io_req->cb_arg = NULL;
147853e2bd2SBhanu Gollapudi 			}
148853e2bd2SBhanu Gollapudi 		}
149853e2bd2SBhanu Gollapudi 		break;
150853e2bd2SBhanu Gollapudi 	default:
151853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "cmd_timeout: invalid cmd_type %d\n",
152853e2bd2SBhanu Gollapudi 			cmd_type);
153853e2bd2SBhanu Gollapudi 		break;
154853e2bd2SBhanu Gollapudi 	}
155853e2bd2SBhanu Gollapudi 
156853e2bd2SBhanu Gollapudi done:
157853e2bd2SBhanu Gollapudi 	/* release the cmd that was held when timer was set */
158853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
159853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
160853e2bd2SBhanu Gollapudi }
161853e2bd2SBhanu Gollapudi 
162853e2bd2SBhanu Gollapudi static void bnx2fc_scsi_done(struct bnx2fc_cmd *io_req, int err_code)
163853e2bd2SBhanu Gollapudi {
164853e2bd2SBhanu Gollapudi 	/* Called with host lock held */
165853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
166853e2bd2SBhanu Gollapudi 
167853e2bd2SBhanu Gollapudi 	/*
168853e2bd2SBhanu Gollapudi 	 * active_cmd_queue may have other command types as well,
169853e2bd2SBhanu Gollapudi 	 * and during flush operation,  we want to error back only
170853e2bd2SBhanu Gollapudi 	 * scsi commands.
171853e2bd2SBhanu Gollapudi 	 */
172853e2bd2SBhanu Gollapudi 	if (io_req->cmd_type != BNX2FC_SCSI_CMD)
173853e2bd2SBhanu Gollapudi 		return;
174853e2bd2SBhanu Gollapudi 
175853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "scsi_done. err_code = 0x%x\n", err_code);
17674446954SBhanu Prakash Gollapudi 	if (test_bit(BNX2FC_FLAG_CMD_LOST, &io_req->req_flags)) {
17774446954SBhanu Prakash Gollapudi 		/* Do not call scsi done for this IO */
17874446954SBhanu Prakash Gollapudi 		return;
17974446954SBhanu Prakash Gollapudi 	}
18074446954SBhanu Prakash Gollapudi 
181853e2bd2SBhanu Gollapudi 	bnx2fc_unmap_sg_list(io_req);
182853e2bd2SBhanu Gollapudi 	io_req->sc_cmd = NULL;
183d02327a8SChad Dupuis 
184d02327a8SChad Dupuis 	/* Sanity checks before returning command to mid-layer */
185853e2bd2SBhanu Gollapudi 	if (!sc_cmd) {
186853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "scsi_done - sc_cmd NULL. "
187853e2bd2SBhanu Gollapudi 				    "IO(0x%x) already cleaned up\n",
188853e2bd2SBhanu Gollapudi 		       io_req->xid);
189853e2bd2SBhanu Gollapudi 		return;
190853e2bd2SBhanu Gollapudi 	}
191d02327a8SChad Dupuis 	if (!sc_cmd->device) {
192d02327a8SChad Dupuis 		pr_err(PFX "0x%x: sc_cmd->device is NULL.\n", io_req->xid);
193d02327a8SChad Dupuis 		return;
194d02327a8SChad Dupuis 	}
195d02327a8SChad Dupuis 	if (!sc_cmd->device->host) {
196d02327a8SChad Dupuis 		pr_err(PFX "0x%x: sc_cmd->device->host is NULL.\n",
197d02327a8SChad Dupuis 		    io_req->xid);
198d02327a8SChad Dupuis 		return;
199d02327a8SChad Dupuis 	}
200d02327a8SChad Dupuis 
201853e2bd2SBhanu Gollapudi 	sc_cmd->result = err_code << 16;
202853e2bd2SBhanu Gollapudi 
203853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "sc=%p, result=0x%x, retries=%d, allowed=%d\n",
204853e2bd2SBhanu Gollapudi 		sc_cmd, host_byte(sc_cmd->result), sc_cmd->retries,
205853e2bd2SBhanu Gollapudi 		sc_cmd->allowed);
206853e2bd2SBhanu Gollapudi 	scsi_set_resid(sc_cmd, scsi_bufflen(sc_cmd));
207853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = NULL;
208853e2bd2SBhanu Gollapudi 	sc_cmd->scsi_done(sc_cmd);
209853e2bd2SBhanu Gollapudi }
210853e2bd2SBhanu Gollapudi 
2110eb43b4bSBhanu Prakash Gollapudi struct bnx2fc_cmd_mgr *bnx2fc_cmd_mgr_alloc(struct bnx2fc_hba *hba)
212853e2bd2SBhanu Gollapudi {
213853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd_mgr *cmgr;
214853e2bd2SBhanu Gollapudi 	struct io_bdt *bdt_info;
215853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
216853e2bd2SBhanu Gollapudi 	size_t len;
217853e2bd2SBhanu Gollapudi 	u32 mem_size;
218853e2bd2SBhanu Gollapudi 	u16 xid;
219853e2bd2SBhanu Gollapudi 	int i;
2200ea5c275SBhanu Gollapudi 	int num_ios, num_pri_ios;
221853e2bd2SBhanu Gollapudi 	size_t bd_tbl_sz;
2220ea5c275SBhanu Gollapudi 	int arr_sz = num_possible_cpus() + 1;
2230eb43b4bSBhanu Prakash Gollapudi 	u16 min_xid = BNX2FC_MIN_XID;
2240eb43b4bSBhanu Prakash Gollapudi 	u16 max_xid = hba->max_xid;
225853e2bd2SBhanu Gollapudi 
226853e2bd2SBhanu Gollapudi 	if (max_xid <= min_xid || max_xid == FC_XID_UNKNOWN) {
227853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "cmd_mgr_alloc: Invalid min_xid 0x%x \
228853e2bd2SBhanu Gollapudi 					and max_xid 0x%x\n", min_xid, max_xid);
229853e2bd2SBhanu Gollapudi 		return NULL;
230853e2bd2SBhanu Gollapudi 	}
231853e2bd2SBhanu Gollapudi 	BNX2FC_MISC_DBG("min xid 0x%x, max xid 0x%x\n", min_xid, max_xid);
232853e2bd2SBhanu Gollapudi 
233853e2bd2SBhanu Gollapudi 	num_ios = max_xid - min_xid + 1;
234853e2bd2SBhanu Gollapudi 	len = (num_ios * (sizeof(struct bnx2fc_cmd *)));
235853e2bd2SBhanu Gollapudi 	len += sizeof(struct bnx2fc_cmd_mgr);
236853e2bd2SBhanu Gollapudi 
237853e2bd2SBhanu Gollapudi 	cmgr = kzalloc(len, GFP_KERNEL);
238853e2bd2SBhanu Gollapudi 	if (!cmgr) {
239853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "failed to alloc cmgr\n");
240853e2bd2SBhanu Gollapudi 		return NULL;
241853e2bd2SBhanu Gollapudi 	}
242853e2bd2SBhanu Gollapudi 
243b2d3492fSDan Carpenter 	cmgr->hba = hba;
2446396bb22SKees Cook 	cmgr->free_list = kcalloc(arr_sz, sizeof(*cmgr->free_list),
2456396bb22SKees Cook 				  GFP_KERNEL);
246853e2bd2SBhanu Gollapudi 	if (!cmgr->free_list) {
247853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "failed to alloc free_list\n");
248853e2bd2SBhanu Gollapudi 		goto mem_err;
249853e2bd2SBhanu Gollapudi 	}
250853e2bd2SBhanu Gollapudi 
2516396bb22SKees Cook 	cmgr->free_list_lock = kcalloc(arr_sz, sizeof(*cmgr->free_list_lock),
2526396bb22SKees Cook 				       GFP_KERNEL);
253853e2bd2SBhanu Gollapudi 	if (!cmgr->free_list_lock) {
254853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "failed to alloc free_list_lock\n");
2559172b763SMaurizio Lombardi 		kfree(cmgr->free_list);
2569172b763SMaurizio Lombardi 		cmgr->free_list = NULL;
257853e2bd2SBhanu Gollapudi 		goto mem_err;
258853e2bd2SBhanu Gollapudi 	}
259853e2bd2SBhanu Gollapudi 
260853e2bd2SBhanu Gollapudi 	cmgr->cmds = (struct bnx2fc_cmd **)(cmgr + 1);
261853e2bd2SBhanu Gollapudi 
2620ea5c275SBhanu Gollapudi 	for (i = 0; i < arr_sz; i++)  {
263853e2bd2SBhanu Gollapudi 		INIT_LIST_HEAD(&cmgr->free_list[i]);
264853e2bd2SBhanu Gollapudi 		spin_lock_init(&cmgr->free_list_lock[i]);
265853e2bd2SBhanu Gollapudi 	}
266853e2bd2SBhanu Gollapudi 
2670ea5c275SBhanu Gollapudi 	/*
2680ea5c275SBhanu Gollapudi 	 * Pre-allocated pool of bnx2fc_cmds.
2690ea5c275SBhanu Gollapudi 	 * Last entry in the free list array is the free list
2700ea5c275SBhanu Gollapudi 	 * of slow path requests.
2710ea5c275SBhanu Gollapudi 	 */
272853e2bd2SBhanu Gollapudi 	xid = BNX2FC_MIN_XID;
2730eb43b4bSBhanu Prakash Gollapudi 	num_pri_ios = num_ios - hba->elstm_xids;
274853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
275853e2bd2SBhanu Gollapudi 		io_req = kzalloc(sizeof(*io_req), GFP_KERNEL);
276853e2bd2SBhanu Gollapudi 
277853e2bd2SBhanu Gollapudi 		if (!io_req) {
278853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "failed to alloc io_req\n");
279853e2bd2SBhanu Gollapudi 			goto mem_err;
280853e2bd2SBhanu Gollapudi 		}
281853e2bd2SBhanu Gollapudi 
282853e2bd2SBhanu Gollapudi 		INIT_LIST_HEAD(&io_req->link);
283853e2bd2SBhanu Gollapudi 		INIT_DELAYED_WORK(&io_req->timeout_work, bnx2fc_cmd_timeout);
284853e2bd2SBhanu Gollapudi 
285853e2bd2SBhanu Gollapudi 		io_req->xid = xid++;
2860ea5c275SBhanu Gollapudi 		if (i < num_pri_ios)
287853e2bd2SBhanu Gollapudi 			list_add_tail(&io_req->link,
2880ea5c275SBhanu Gollapudi 				&cmgr->free_list[io_req->xid %
2890ea5c275SBhanu Gollapudi 						 num_possible_cpus()]);
2900ea5c275SBhanu Gollapudi 		else
2910ea5c275SBhanu Gollapudi 			list_add_tail(&io_req->link,
2920ea5c275SBhanu Gollapudi 				&cmgr->free_list[num_possible_cpus()]);
293853e2bd2SBhanu Gollapudi 		io_req++;
294853e2bd2SBhanu Gollapudi 	}
295853e2bd2SBhanu Gollapudi 
296853e2bd2SBhanu Gollapudi 	/* Allocate pool of io_bdts - one for each bnx2fc_cmd */
297853e2bd2SBhanu Gollapudi 	mem_size = num_ios * sizeof(struct io_bdt *);
298b2d3492fSDan Carpenter 	cmgr->io_bdt_pool = kzalloc(mem_size, GFP_KERNEL);
299853e2bd2SBhanu Gollapudi 	if (!cmgr->io_bdt_pool) {
300853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "failed to alloc io_bdt_pool\n");
301853e2bd2SBhanu Gollapudi 		goto mem_err;
302853e2bd2SBhanu Gollapudi 	}
303853e2bd2SBhanu Gollapudi 
304853e2bd2SBhanu Gollapudi 	mem_size = sizeof(struct io_bdt);
305853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
306853e2bd2SBhanu Gollapudi 		cmgr->io_bdt_pool[i] = kmalloc(mem_size, GFP_KERNEL);
307853e2bd2SBhanu Gollapudi 		if (!cmgr->io_bdt_pool[i]) {
308853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "failed to alloc "
309853e2bd2SBhanu Gollapudi 				"io_bdt_pool[%d]\n", i);
310853e2bd2SBhanu Gollapudi 			goto mem_err;
311853e2bd2SBhanu Gollapudi 		}
312853e2bd2SBhanu Gollapudi 	}
313853e2bd2SBhanu Gollapudi 
314853e2bd2SBhanu Gollapudi 	/* Allocate an map fcoe_bdt_ctx structures */
315853e2bd2SBhanu Gollapudi 	bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
316853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
317853e2bd2SBhanu Gollapudi 		bdt_info = cmgr->io_bdt_pool[i];
318853e2bd2SBhanu Gollapudi 		bdt_info->bd_tbl = dma_alloc_coherent(&hba->pcidev->dev,
319853e2bd2SBhanu Gollapudi 						      bd_tbl_sz,
320853e2bd2SBhanu Gollapudi 						      &bdt_info->bd_tbl_dma,
321853e2bd2SBhanu Gollapudi 						      GFP_KERNEL);
322853e2bd2SBhanu Gollapudi 		if (!bdt_info->bd_tbl) {
323853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "failed to alloc "
324853e2bd2SBhanu Gollapudi 				"bdt_tbl[%d]\n", i);
325853e2bd2SBhanu Gollapudi 			goto mem_err;
326853e2bd2SBhanu Gollapudi 		}
327853e2bd2SBhanu Gollapudi 	}
328853e2bd2SBhanu Gollapudi 
329853e2bd2SBhanu Gollapudi 	return cmgr;
330853e2bd2SBhanu Gollapudi 
331853e2bd2SBhanu Gollapudi mem_err:
332853e2bd2SBhanu Gollapudi 	bnx2fc_cmd_mgr_free(cmgr);
333853e2bd2SBhanu Gollapudi 	return NULL;
334853e2bd2SBhanu Gollapudi }
335853e2bd2SBhanu Gollapudi 
336853e2bd2SBhanu Gollapudi void bnx2fc_cmd_mgr_free(struct bnx2fc_cmd_mgr *cmgr)
337853e2bd2SBhanu Gollapudi {
338853e2bd2SBhanu Gollapudi 	struct io_bdt *bdt_info;
339853e2bd2SBhanu Gollapudi 	struct bnx2fc_hba *hba = cmgr->hba;
340853e2bd2SBhanu Gollapudi 	size_t bd_tbl_sz;
341853e2bd2SBhanu Gollapudi 	u16 min_xid = BNX2FC_MIN_XID;
3420eb43b4bSBhanu Prakash Gollapudi 	u16 max_xid = hba->max_xid;
343853e2bd2SBhanu Gollapudi 	int num_ios;
344853e2bd2SBhanu Gollapudi 	int i;
345853e2bd2SBhanu Gollapudi 
346853e2bd2SBhanu Gollapudi 	num_ios = max_xid - min_xid + 1;
347853e2bd2SBhanu Gollapudi 
348853e2bd2SBhanu Gollapudi 	/* Free fcoe_bdt_ctx structures */
349853e2bd2SBhanu Gollapudi 	if (!cmgr->io_bdt_pool)
350853e2bd2SBhanu Gollapudi 		goto free_cmd_pool;
351853e2bd2SBhanu Gollapudi 
352853e2bd2SBhanu Gollapudi 	bd_tbl_sz = BNX2FC_MAX_BDS_PER_CMD * sizeof(struct fcoe_bd_ctx);
353853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
354853e2bd2SBhanu Gollapudi 		bdt_info = cmgr->io_bdt_pool[i];
355853e2bd2SBhanu Gollapudi 		if (bdt_info->bd_tbl) {
356853e2bd2SBhanu Gollapudi 			dma_free_coherent(&hba->pcidev->dev, bd_tbl_sz,
357853e2bd2SBhanu Gollapudi 					    bdt_info->bd_tbl,
358853e2bd2SBhanu Gollapudi 					    bdt_info->bd_tbl_dma);
359853e2bd2SBhanu Gollapudi 			bdt_info->bd_tbl = NULL;
360853e2bd2SBhanu Gollapudi 		}
361853e2bd2SBhanu Gollapudi 	}
362853e2bd2SBhanu Gollapudi 
363853e2bd2SBhanu Gollapudi 	/* Destroy io_bdt pool */
364853e2bd2SBhanu Gollapudi 	for (i = 0; i < num_ios; i++) {
365853e2bd2SBhanu Gollapudi 		kfree(cmgr->io_bdt_pool[i]);
366853e2bd2SBhanu Gollapudi 		cmgr->io_bdt_pool[i] = NULL;
367853e2bd2SBhanu Gollapudi 	}
368853e2bd2SBhanu Gollapudi 
369853e2bd2SBhanu Gollapudi 	kfree(cmgr->io_bdt_pool);
370853e2bd2SBhanu Gollapudi 	cmgr->io_bdt_pool = NULL;
371853e2bd2SBhanu Gollapudi 
372853e2bd2SBhanu Gollapudi free_cmd_pool:
373853e2bd2SBhanu Gollapudi 	kfree(cmgr->free_list_lock);
374853e2bd2SBhanu Gollapudi 
375853e2bd2SBhanu Gollapudi 	/* Destroy cmd pool */
376853e2bd2SBhanu Gollapudi 	if (!cmgr->free_list)
377853e2bd2SBhanu Gollapudi 		goto free_cmgr;
378853e2bd2SBhanu Gollapudi 
3790ea5c275SBhanu Gollapudi 	for (i = 0; i < num_possible_cpus() + 1; i++)  {
380d71fb3bdSBhanu Prakash Gollapudi 		struct bnx2fc_cmd *tmp, *io_req;
381853e2bd2SBhanu Gollapudi 
382d71fb3bdSBhanu Prakash Gollapudi 		list_for_each_entry_safe(io_req, tmp,
383d71fb3bdSBhanu Prakash Gollapudi 					 &cmgr->free_list[i], link) {
384853e2bd2SBhanu Gollapudi 			list_del(&io_req->link);
385853e2bd2SBhanu Gollapudi 			kfree(io_req);
386853e2bd2SBhanu Gollapudi 		}
387853e2bd2SBhanu Gollapudi 	}
388853e2bd2SBhanu Gollapudi 	kfree(cmgr->free_list);
389853e2bd2SBhanu Gollapudi free_cmgr:
390853e2bd2SBhanu Gollapudi 	/* Free command manager itself */
391853e2bd2SBhanu Gollapudi 	kfree(cmgr);
392853e2bd2SBhanu Gollapudi }
393853e2bd2SBhanu Gollapudi 
394853e2bd2SBhanu Gollapudi struct bnx2fc_cmd *bnx2fc_elstm_alloc(struct bnx2fc_rport *tgt, int type)
395853e2bd2SBhanu Gollapudi {
396853e2bd2SBhanu Gollapudi 	struct fcoe_port *port = tgt->port;
397aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = port->priv;
398aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
399853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
400853e2bd2SBhanu Gollapudi 	struct list_head *listp;
401853e2bd2SBhanu Gollapudi 	struct io_bdt *bd_tbl;
4020ea5c275SBhanu Gollapudi 	int index = RESERVE_FREE_LIST_INDEX;
403619c5cb6SVlad Zolotarov 	u32 free_sqes;
404853e2bd2SBhanu Gollapudi 	u32 max_sqes;
405853e2bd2SBhanu Gollapudi 	u16 xid;
406853e2bd2SBhanu Gollapudi 
407853e2bd2SBhanu Gollapudi 	max_sqes = tgt->max_sqes;
408853e2bd2SBhanu Gollapudi 	switch (type) {
409853e2bd2SBhanu Gollapudi 	case BNX2FC_TASK_MGMT_CMD:
410853e2bd2SBhanu Gollapudi 		max_sqes = BNX2FC_TM_MAX_SQES;
411853e2bd2SBhanu Gollapudi 		break;
412853e2bd2SBhanu Gollapudi 	case BNX2FC_ELS:
413853e2bd2SBhanu Gollapudi 		max_sqes = BNX2FC_ELS_MAX_SQES;
414853e2bd2SBhanu Gollapudi 		break;
415853e2bd2SBhanu Gollapudi 	default:
416853e2bd2SBhanu Gollapudi 		break;
417853e2bd2SBhanu Gollapudi 	}
418853e2bd2SBhanu Gollapudi 
419853e2bd2SBhanu Gollapudi 	/*
420853e2bd2SBhanu Gollapudi 	 * NOTE: Free list insertions and deletions are protected with
421853e2bd2SBhanu Gollapudi 	 * cmgr lock
422853e2bd2SBhanu Gollapudi 	 */
4230ea5c275SBhanu Gollapudi 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
424619c5cb6SVlad Zolotarov 	free_sqes = atomic_read(&tgt->free_sqes);
4250ea5c275SBhanu Gollapudi 	if ((list_empty(&(cmd_mgr->free_list[index]))) ||
426619c5cb6SVlad Zolotarov 	    (tgt->num_active_ios.counter  >= max_sqes) ||
427619c5cb6SVlad Zolotarov 	    (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
428853e2bd2SBhanu Gollapudi 		BNX2FC_TGT_DBG(tgt, "No free els_tm cmds available "
429853e2bd2SBhanu Gollapudi 			"ios(%d):sqes(%d)\n",
430853e2bd2SBhanu Gollapudi 			tgt->num_active_ios.counter, tgt->max_sqes);
4310ea5c275SBhanu Gollapudi 		if (list_empty(&(cmd_mgr->free_list[index])))
432853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "elstm_alloc: list_empty\n");
4330ea5c275SBhanu Gollapudi 		spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
434853e2bd2SBhanu Gollapudi 		return NULL;
435853e2bd2SBhanu Gollapudi 	}
436853e2bd2SBhanu Gollapudi 
437853e2bd2SBhanu Gollapudi 	listp = (struct list_head *)
4380ea5c275SBhanu Gollapudi 			cmd_mgr->free_list[index].next;
439853e2bd2SBhanu Gollapudi 	list_del_init(listp);
440853e2bd2SBhanu Gollapudi 	io_req = (struct bnx2fc_cmd *) listp;
441853e2bd2SBhanu Gollapudi 	xid = io_req->xid;
442853e2bd2SBhanu Gollapudi 	cmd_mgr->cmds[xid] = io_req;
443853e2bd2SBhanu Gollapudi 	atomic_inc(&tgt->num_active_ios);
444619c5cb6SVlad Zolotarov 	atomic_dec(&tgt->free_sqes);
4450ea5c275SBhanu Gollapudi 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
446853e2bd2SBhanu Gollapudi 
447853e2bd2SBhanu Gollapudi 	INIT_LIST_HEAD(&io_req->link);
448853e2bd2SBhanu Gollapudi 
449853e2bd2SBhanu Gollapudi 	io_req->port = port;
450853e2bd2SBhanu Gollapudi 	io_req->cmd_mgr = cmd_mgr;
451853e2bd2SBhanu Gollapudi 	io_req->req_flags = 0;
452853e2bd2SBhanu Gollapudi 	io_req->cmd_type = type;
453853e2bd2SBhanu Gollapudi 
454853e2bd2SBhanu Gollapudi 	/* Bind io_bdt for this io_req */
455853e2bd2SBhanu Gollapudi 	/* Have a static link between io_req and io_bdt_pool */
456853e2bd2SBhanu Gollapudi 	bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
457853e2bd2SBhanu Gollapudi 	bd_tbl->io_req = io_req;
458853e2bd2SBhanu Gollapudi 
459853e2bd2SBhanu Gollapudi 	/* Hold the io_req  against deletion */
460853e2bd2SBhanu Gollapudi 	kref_init(&io_req->refcount);
461853e2bd2SBhanu Gollapudi 	return io_req;
462853e2bd2SBhanu Gollapudi }
463aea71a02SBhanu Prakash Gollapudi 
464aea71a02SBhanu Prakash Gollapudi struct bnx2fc_cmd *bnx2fc_cmd_alloc(struct bnx2fc_rport *tgt)
465853e2bd2SBhanu Gollapudi {
466853e2bd2SBhanu Gollapudi 	struct fcoe_port *port = tgt->port;
467aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = port->priv;
468aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_cmd_mgr *cmd_mgr = interface->hba->cmd_mgr;
469853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
470853e2bd2SBhanu Gollapudi 	struct list_head *listp;
471853e2bd2SBhanu Gollapudi 	struct io_bdt *bd_tbl;
472619c5cb6SVlad Zolotarov 	u32 free_sqes;
473853e2bd2SBhanu Gollapudi 	u32 max_sqes;
474853e2bd2SBhanu Gollapudi 	u16 xid;
4750ea5c275SBhanu Gollapudi 	int index = get_cpu();
476853e2bd2SBhanu Gollapudi 
477853e2bd2SBhanu Gollapudi 	max_sqes = BNX2FC_SCSI_MAX_SQES;
478853e2bd2SBhanu Gollapudi 	/*
479853e2bd2SBhanu Gollapudi 	 * NOTE: Free list insertions and deletions are protected with
480853e2bd2SBhanu Gollapudi 	 * cmgr lock
481853e2bd2SBhanu Gollapudi 	 */
4820ea5c275SBhanu Gollapudi 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
483619c5cb6SVlad Zolotarov 	free_sqes = atomic_read(&tgt->free_sqes);
4840ea5c275SBhanu Gollapudi 	if ((list_empty(&cmd_mgr->free_list[index])) ||
485619c5cb6SVlad Zolotarov 	    (tgt->num_active_ios.counter  >= max_sqes) ||
486619c5cb6SVlad Zolotarov 	    (free_sqes + max_sqes <= BNX2FC_SQ_WQES_MAX)) {
4870ea5c275SBhanu Gollapudi 		spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
4880ea5c275SBhanu Gollapudi 		put_cpu();
489853e2bd2SBhanu Gollapudi 		return NULL;
490853e2bd2SBhanu Gollapudi 	}
491853e2bd2SBhanu Gollapudi 
492853e2bd2SBhanu Gollapudi 	listp = (struct list_head *)
4930ea5c275SBhanu Gollapudi 		cmd_mgr->free_list[index].next;
494853e2bd2SBhanu Gollapudi 	list_del_init(listp);
495853e2bd2SBhanu Gollapudi 	io_req = (struct bnx2fc_cmd *) listp;
496853e2bd2SBhanu Gollapudi 	xid = io_req->xid;
497853e2bd2SBhanu Gollapudi 	cmd_mgr->cmds[xid] = io_req;
498853e2bd2SBhanu Gollapudi 	atomic_inc(&tgt->num_active_ios);
499619c5cb6SVlad Zolotarov 	atomic_dec(&tgt->free_sqes);
5000ea5c275SBhanu Gollapudi 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
5010ea5c275SBhanu Gollapudi 	put_cpu();
502853e2bd2SBhanu Gollapudi 
503853e2bd2SBhanu Gollapudi 	INIT_LIST_HEAD(&io_req->link);
504853e2bd2SBhanu Gollapudi 
505853e2bd2SBhanu Gollapudi 	io_req->port = port;
506853e2bd2SBhanu Gollapudi 	io_req->cmd_mgr = cmd_mgr;
507853e2bd2SBhanu Gollapudi 	io_req->req_flags = 0;
508853e2bd2SBhanu Gollapudi 
509853e2bd2SBhanu Gollapudi 	/* Bind io_bdt for this io_req */
510853e2bd2SBhanu Gollapudi 	/* Have a static link between io_req and io_bdt_pool */
511853e2bd2SBhanu Gollapudi 	bd_tbl = io_req->bd_tbl = cmd_mgr->io_bdt_pool[xid];
512853e2bd2SBhanu Gollapudi 	bd_tbl->io_req = io_req;
513853e2bd2SBhanu Gollapudi 
514853e2bd2SBhanu Gollapudi 	/* Hold the io_req  against deletion */
515853e2bd2SBhanu Gollapudi 	kref_init(&io_req->refcount);
516853e2bd2SBhanu Gollapudi 	return io_req;
517853e2bd2SBhanu Gollapudi }
518853e2bd2SBhanu Gollapudi 
519853e2bd2SBhanu Gollapudi void bnx2fc_cmd_release(struct kref *ref)
520853e2bd2SBhanu Gollapudi {
521853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req = container_of(ref,
522853e2bd2SBhanu Gollapudi 						struct bnx2fc_cmd, refcount);
523853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd_mgr *cmd_mgr = io_req->cmd_mgr;
5240ea5c275SBhanu Gollapudi 	int index;
525853e2bd2SBhanu Gollapudi 
5260ea5c275SBhanu Gollapudi 	if (io_req->cmd_type == BNX2FC_SCSI_CMD)
5270ea5c275SBhanu Gollapudi 		index = io_req->xid % num_possible_cpus();
5280ea5c275SBhanu Gollapudi 	else
5290ea5c275SBhanu Gollapudi 		index = RESERVE_FREE_LIST_INDEX;
5300ea5c275SBhanu Gollapudi 
5310ea5c275SBhanu Gollapudi 
5320ea5c275SBhanu Gollapudi 	spin_lock_bh(&cmd_mgr->free_list_lock[index]);
533853e2bd2SBhanu Gollapudi 	if (io_req->cmd_type != BNX2FC_SCSI_CMD)
534853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
535853e2bd2SBhanu Gollapudi 	cmd_mgr->cmds[io_req->xid] = NULL;
536853e2bd2SBhanu Gollapudi 	/* Delete IO from retire queue */
537853e2bd2SBhanu Gollapudi 	list_del_init(&io_req->link);
538853e2bd2SBhanu Gollapudi 	/* Add it to the free list */
539853e2bd2SBhanu Gollapudi 	list_add(&io_req->link,
5400ea5c275SBhanu Gollapudi 			&cmd_mgr->free_list[index]);
541853e2bd2SBhanu Gollapudi 	atomic_dec(&io_req->tgt->num_active_ios);
5420ea5c275SBhanu Gollapudi 	spin_unlock_bh(&cmd_mgr->free_list_lock[index]);
5430ea5c275SBhanu Gollapudi 
544853e2bd2SBhanu Gollapudi }
545853e2bd2SBhanu Gollapudi 
546853e2bd2SBhanu Gollapudi static void bnx2fc_free_mp_resc(struct bnx2fc_cmd *io_req)
547853e2bd2SBhanu Gollapudi {
548853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *mp_req = &(io_req->mp_req);
549aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = io_req->port->priv;
550aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_hba *hba = interface->hba;
551853e2bd2SBhanu Gollapudi 	size_t sz = sizeof(struct fcoe_bd_ctx);
552853e2bd2SBhanu Gollapudi 
553853e2bd2SBhanu Gollapudi 	/* clear tm flags */
554853e2bd2SBhanu Gollapudi 	mp_req->tm_flags = 0;
555853e2bd2SBhanu Gollapudi 	if (mp_req->mp_req_bd) {
556853e2bd2SBhanu Gollapudi 		dma_free_coherent(&hba->pcidev->dev, sz,
557853e2bd2SBhanu Gollapudi 				     mp_req->mp_req_bd,
558853e2bd2SBhanu Gollapudi 				     mp_req->mp_req_bd_dma);
559853e2bd2SBhanu Gollapudi 		mp_req->mp_req_bd = NULL;
560853e2bd2SBhanu Gollapudi 	}
561853e2bd2SBhanu Gollapudi 	if (mp_req->mp_resp_bd) {
562853e2bd2SBhanu Gollapudi 		dma_free_coherent(&hba->pcidev->dev, sz,
563853e2bd2SBhanu Gollapudi 				     mp_req->mp_resp_bd,
564853e2bd2SBhanu Gollapudi 				     mp_req->mp_resp_bd_dma);
565853e2bd2SBhanu Gollapudi 		mp_req->mp_resp_bd = NULL;
566853e2bd2SBhanu Gollapudi 	}
567853e2bd2SBhanu Gollapudi 	if (mp_req->req_buf) {
568be1fefc2SMichael Chan 		dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
569853e2bd2SBhanu Gollapudi 				     mp_req->req_buf,
570853e2bd2SBhanu Gollapudi 				     mp_req->req_buf_dma);
571853e2bd2SBhanu Gollapudi 		mp_req->req_buf = NULL;
572853e2bd2SBhanu Gollapudi 	}
573853e2bd2SBhanu Gollapudi 	if (mp_req->resp_buf) {
574be1fefc2SMichael Chan 		dma_free_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
575853e2bd2SBhanu Gollapudi 				     mp_req->resp_buf,
576853e2bd2SBhanu Gollapudi 				     mp_req->resp_buf_dma);
577853e2bd2SBhanu Gollapudi 		mp_req->resp_buf = NULL;
578853e2bd2SBhanu Gollapudi 	}
579853e2bd2SBhanu Gollapudi }
580853e2bd2SBhanu Gollapudi 
581853e2bd2SBhanu Gollapudi int bnx2fc_init_mp_req(struct bnx2fc_cmd *io_req)
582853e2bd2SBhanu Gollapudi {
583853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *mp_req;
584853e2bd2SBhanu Gollapudi 	struct fcoe_bd_ctx *mp_req_bd;
585853e2bd2SBhanu Gollapudi 	struct fcoe_bd_ctx *mp_resp_bd;
586aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = io_req->port->priv;
587aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_hba *hba = interface->hba;
588853e2bd2SBhanu Gollapudi 	dma_addr_t addr;
589853e2bd2SBhanu Gollapudi 	size_t sz;
590853e2bd2SBhanu Gollapudi 
591853e2bd2SBhanu Gollapudi 	mp_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
592853e2bd2SBhanu Gollapudi 	memset(mp_req, 0, sizeof(struct bnx2fc_mp_req));
593853e2bd2SBhanu Gollapudi 
5941fffa199SChad Dupuis 	if (io_req->cmd_type != BNX2FC_ELS) {
595853e2bd2SBhanu Gollapudi 		mp_req->req_len = sizeof(struct fcp_cmnd);
596853e2bd2SBhanu Gollapudi 		io_req->data_xfer_len = mp_req->req_len;
5971fffa199SChad Dupuis 	} else
5981fffa199SChad Dupuis 		mp_req->req_len = io_req->data_xfer_len;
5991fffa199SChad Dupuis 
600be1fefc2SMichael Chan 	mp_req->req_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
601853e2bd2SBhanu Gollapudi 					     &mp_req->req_buf_dma,
602853e2bd2SBhanu Gollapudi 					     GFP_ATOMIC);
603853e2bd2SBhanu Gollapudi 	if (!mp_req->req_buf) {
604853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "unable to alloc MP req buffer\n");
605853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
606853e2bd2SBhanu Gollapudi 		return FAILED;
607853e2bd2SBhanu Gollapudi 	}
608853e2bd2SBhanu Gollapudi 
609be1fefc2SMichael Chan 	mp_req->resp_buf = dma_alloc_coherent(&hba->pcidev->dev, CNIC_PAGE_SIZE,
610853e2bd2SBhanu Gollapudi 					      &mp_req->resp_buf_dma,
611853e2bd2SBhanu Gollapudi 					      GFP_ATOMIC);
612853e2bd2SBhanu Gollapudi 	if (!mp_req->resp_buf) {
613853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "unable to alloc TM resp buffer\n");
614853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
615853e2bd2SBhanu Gollapudi 		return FAILED;
616853e2bd2SBhanu Gollapudi 	}
617be1fefc2SMichael Chan 	memset(mp_req->req_buf, 0, CNIC_PAGE_SIZE);
618be1fefc2SMichael Chan 	memset(mp_req->resp_buf, 0, CNIC_PAGE_SIZE);
619853e2bd2SBhanu Gollapudi 
620853e2bd2SBhanu Gollapudi 	/* Allocate and map mp_req_bd and mp_resp_bd */
621853e2bd2SBhanu Gollapudi 	sz = sizeof(struct fcoe_bd_ctx);
622853e2bd2SBhanu Gollapudi 	mp_req->mp_req_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
623853e2bd2SBhanu Gollapudi 						 &mp_req->mp_req_bd_dma,
624853e2bd2SBhanu Gollapudi 						 GFP_ATOMIC);
625853e2bd2SBhanu Gollapudi 	if (!mp_req->mp_req_bd) {
626853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "unable to alloc MP req bd\n");
627853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
628853e2bd2SBhanu Gollapudi 		return FAILED;
629853e2bd2SBhanu Gollapudi 	}
630853e2bd2SBhanu Gollapudi 	mp_req->mp_resp_bd = dma_alloc_coherent(&hba->pcidev->dev, sz,
631853e2bd2SBhanu Gollapudi 						 &mp_req->mp_resp_bd_dma,
632853e2bd2SBhanu Gollapudi 						 GFP_ATOMIC);
633b0d5e15cSJulia Lawall 	if (!mp_req->mp_resp_bd) {
634853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "unable to alloc MP resp bd\n");
635853e2bd2SBhanu Gollapudi 		bnx2fc_free_mp_resc(io_req);
636853e2bd2SBhanu Gollapudi 		return FAILED;
637853e2bd2SBhanu Gollapudi 	}
638853e2bd2SBhanu Gollapudi 	/* Fill bd table */
639853e2bd2SBhanu Gollapudi 	addr = mp_req->req_buf_dma;
640853e2bd2SBhanu Gollapudi 	mp_req_bd = mp_req->mp_req_bd;
641853e2bd2SBhanu Gollapudi 	mp_req_bd->buf_addr_lo = (u32)addr & 0xffffffff;
642853e2bd2SBhanu Gollapudi 	mp_req_bd->buf_addr_hi = (u32)((u64)addr >> 32);
643be1fefc2SMichael Chan 	mp_req_bd->buf_len = CNIC_PAGE_SIZE;
644853e2bd2SBhanu Gollapudi 	mp_req_bd->flags = 0;
645853e2bd2SBhanu Gollapudi 
646853e2bd2SBhanu Gollapudi 	/*
647853e2bd2SBhanu Gollapudi 	 * MP buffer is either a task mgmt command or an ELS.
648853e2bd2SBhanu Gollapudi 	 * So the assumption is that it consumes a single bd
649853e2bd2SBhanu Gollapudi 	 * entry in the bd table
650853e2bd2SBhanu Gollapudi 	 */
651853e2bd2SBhanu Gollapudi 	mp_resp_bd = mp_req->mp_resp_bd;
652853e2bd2SBhanu Gollapudi 	addr = mp_req->resp_buf_dma;
653853e2bd2SBhanu Gollapudi 	mp_resp_bd->buf_addr_lo = (u32)addr & 0xffffffff;
654853e2bd2SBhanu Gollapudi 	mp_resp_bd->buf_addr_hi = (u32)((u64)addr >> 32);
655be1fefc2SMichael Chan 	mp_resp_bd->buf_len = CNIC_PAGE_SIZE;
656853e2bd2SBhanu Gollapudi 	mp_resp_bd->flags = 0;
657853e2bd2SBhanu Gollapudi 
658853e2bd2SBhanu Gollapudi 	return SUCCESS;
659853e2bd2SBhanu Gollapudi }
660853e2bd2SBhanu Gollapudi 
661853e2bd2SBhanu Gollapudi static int bnx2fc_initiate_tmf(struct scsi_cmnd *sc_cmd, u8 tm_flags)
662853e2bd2SBhanu Gollapudi {
663853e2bd2SBhanu Gollapudi 	struct fc_lport *lport;
66433c7da05SJulia Lawall 	struct fc_rport *rport;
66533c7da05SJulia Lawall 	struct fc_rport_libfc_priv *rp;
666853e2bd2SBhanu Gollapudi 	struct fcoe_port *port;
667aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface;
668853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt;
669853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
670853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *tm_req;
671853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task;
672853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task_page;
673853e2bd2SBhanu Gollapudi 	struct Scsi_Host *host = sc_cmd->device->host;
674853e2bd2SBhanu Gollapudi 	struct fc_frame_header *fc_hdr;
675853e2bd2SBhanu Gollapudi 	struct fcp_cmnd *fcp_cmnd;
676853e2bd2SBhanu Gollapudi 	int task_idx, index;
677853e2bd2SBhanu Gollapudi 	int rc = SUCCESS;
678853e2bd2SBhanu Gollapudi 	u16 xid;
679853e2bd2SBhanu Gollapudi 	u32 sid, did;
680853e2bd2SBhanu Gollapudi 	unsigned long start = jiffies;
681853e2bd2SBhanu Gollapudi 
682853e2bd2SBhanu Gollapudi 	lport = shost_priv(host);
68333c7da05SJulia Lawall 	rport = starget_to_rport(scsi_target(sc_cmd->device));
684853e2bd2SBhanu Gollapudi 	port = lport_priv(lport);
685aea71a02SBhanu Prakash Gollapudi 	interface = port->priv;
686853e2bd2SBhanu Gollapudi 
687853e2bd2SBhanu Gollapudi 	if (rport == NULL) {
688b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "device_reset: rport is NULL\n");
689853e2bd2SBhanu Gollapudi 		rc = FAILED;
690853e2bd2SBhanu Gollapudi 		goto tmf_err;
691853e2bd2SBhanu Gollapudi 	}
69233c7da05SJulia Lawall 	rp = rport->dd_data;
693853e2bd2SBhanu Gollapudi 
694853e2bd2SBhanu Gollapudi 	rc = fc_block_scsi_eh(sc_cmd);
695853e2bd2SBhanu Gollapudi 	if (rc)
696853e2bd2SBhanu Gollapudi 		return rc;
697853e2bd2SBhanu Gollapudi 
698853e2bd2SBhanu Gollapudi 	if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
699853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "device_reset: link is not ready\n");
700853e2bd2SBhanu Gollapudi 		rc = FAILED;
701853e2bd2SBhanu Gollapudi 		goto tmf_err;
702853e2bd2SBhanu Gollapudi 	}
703853e2bd2SBhanu Gollapudi 	/* rport and tgt are allocated together, so tgt should be non-NULL */
704853e2bd2SBhanu Gollapudi 	tgt = (struct bnx2fc_rport *)&rp[1];
705853e2bd2SBhanu Gollapudi 
706853e2bd2SBhanu Gollapudi 	if (!(test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
707853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "device_reset: tgt not offloaded\n");
708853e2bd2SBhanu Gollapudi 		rc = FAILED;
709853e2bd2SBhanu Gollapudi 		goto tmf_err;
710853e2bd2SBhanu Gollapudi 	}
711853e2bd2SBhanu Gollapudi retry_tmf:
712853e2bd2SBhanu Gollapudi 	io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_TASK_MGMT_CMD);
713853e2bd2SBhanu Gollapudi 	if (!io_req) {
714853e2bd2SBhanu Gollapudi 		if (time_after(jiffies, start + HZ)) {
715853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "tmf: Failed TMF");
716853e2bd2SBhanu Gollapudi 			rc = FAILED;
717853e2bd2SBhanu Gollapudi 			goto tmf_err;
718853e2bd2SBhanu Gollapudi 		}
719853e2bd2SBhanu Gollapudi 		msleep(20);
720853e2bd2SBhanu Gollapudi 		goto retry_tmf;
721853e2bd2SBhanu Gollapudi 	}
722853e2bd2SBhanu Gollapudi 	/* Initialize rest of io_req fields */
723853e2bd2SBhanu Gollapudi 	io_req->sc_cmd = sc_cmd;
724853e2bd2SBhanu Gollapudi 	io_req->port = port;
725853e2bd2SBhanu Gollapudi 	io_req->tgt = tgt;
726853e2bd2SBhanu Gollapudi 
727853e2bd2SBhanu Gollapudi 	tm_req = (struct bnx2fc_mp_req *)&(io_req->mp_req);
728853e2bd2SBhanu Gollapudi 
729853e2bd2SBhanu Gollapudi 	rc = bnx2fc_init_mp_req(io_req);
730853e2bd2SBhanu Gollapudi 	if (rc == FAILED) {
731853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Task mgmt MP request init failed\n");
732bd4d5de8SBhanu Prakash Gollapudi 		spin_lock_bh(&tgt->tgt_lock);
733853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
734bd4d5de8SBhanu Prakash Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
735853e2bd2SBhanu Gollapudi 		goto tmf_err;
736853e2bd2SBhanu Gollapudi 	}
737853e2bd2SBhanu Gollapudi 
738853e2bd2SBhanu Gollapudi 	/* Set TM flags */
739853e2bd2SBhanu Gollapudi 	io_req->io_req_flags = 0;
740853e2bd2SBhanu Gollapudi 	tm_req->tm_flags = tm_flags;
741853e2bd2SBhanu Gollapudi 
742853e2bd2SBhanu Gollapudi 	/* Fill FCP_CMND */
743853e2bd2SBhanu Gollapudi 	bnx2fc_build_fcp_cmnd(io_req, (struct fcp_cmnd *)tm_req->req_buf);
744853e2bd2SBhanu Gollapudi 	fcp_cmnd = (struct fcp_cmnd *)tm_req->req_buf;
745853e2bd2SBhanu Gollapudi 	memset(fcp_cmnd->fc_cdb, 0,  sc_cmd->cmd_len);
746853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_dl = 0;
747853e2bd2SBhanu Gollapudi 
748853e2bd2SBhanu Gollapudi 	/* Fill FC header */
749853e2bd2SBhanu Gollapudi 	fc_hdr = &(tm_req->req_fc_hdr);
750853e2bd2SBhanu Gollapudi 	sid = tgt->sid;
751853e2bd2SBhanu Gollapudi 	did = rport->port_id;
752853e2bd2SBhanu Gollapudi 	__fc_fill_fc_hdr(fc_hdr, FC_RCTL_DD_UNSOL_CMD, did, sid,
753853e2bd2SBhanu Gollapudi 			   FC_TYPE_FCP, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
754853e2bd2SBhanu Gollapudi 			   FC_FC_SEQ_INIT, 0);
755853e2bd2SBhanu Gollapudi 	/* Obtain exchange id */
756853e2bd2SBhanu Gollapudi 	xid = io_req->xid;
757853e2bd2SBhanu Gollapudi 
758853e2bd2SBhanu Gollapudi 	BNX2FC_TGT_DBG(tgt, "Initiate TMF - xid = 0x%x\n", xid);
759853e2bd2SBhanu Gollapudi 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
760853e2bd2SBhanu Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
761853e2bd2SBhanu Gollapudi 
762853e2bd2SBhanu Gollapudi 	/* Initialize task context for this IO request */
763aea71a02SBhanu Prakash Gollapudi 	task_page = (struct fcoe_task_ctx_entry *)
764aea71a02SBhanu Prakash Gollapudi 			interface->hba->task_ctx[task_idx];
765853e2bd2SBhanu Gollapudi 	task = &(task_page[index]);
766853e2bd2SBhanu Gollapudi 	bnx2fc_init_mp_task(io_req, task);
767853e2bd2SBhanu Gollapudi 
768853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = (char *)io_req;
769853e2bd2SBhanu Gollapudi 
770853e2bd2SBhanu Gollapudi 	/* Obtain free SQ entry */
771853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
772853e2bd2SBhanu Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
773853e2bd2SBhanu Gollapudi 
774853e2bd2SBhanu Gollapudi 	/* Enqueue the io_req to active_tm_queue */
775853e2bd2SBhanu Gollapudi 	io_req->on_tmf_queue = 1;
776853e2bd2SBhanu Gollapudi 	list_add_tail(&io_req->link, &tgt->active_tm_queue);
777853e2bd2SBhanu Gollapudi 
7780e0fcef9SSaurav Kashyap 	init_completion(&io_req->abts_done);
7790e0fcef9SSaurav Kashyap 	io_req->wait_for_abts_comp = 1;
780853e2bd2SBhanu Gollapudi 
781853e2bd2SBhanu Gollapudi 	/* Ring doorbell */
782853e2bd2SBhanu Gollapudi 	bnx2fc_ring_doorbell(tgt);
783853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
784853e2bd2SBhanu Gollapudi 
7850e0fcef9SSaurav Kashyap 	rc = wait_for_completion_timeout(&io_req->abts_done,
78610755d3fSJoe Carnuccio 					 interface->tm_timeout * HZ);
787853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
788853e2bd2SBhanu Gollapudi 
7890e0fcef9SSaurav Kashyap 	io_req->wait_for_abts_comp = 0;
79092886c9cSBhanu Prakash Gollapudi 	if (!(test_bit(BNX2FC_FLAG_TM_COMPL, &io_req->req_flags))) {
791853e2bd2SBhanu Gollapudi 		set_bit(BNX2FC_FLAG_TM_TIMEOUT, &io_req->req_flags);
79292886c9cSBhanu Prakash Gollapudi 		if (io_req->on_tmf_queue) {
79392886c9cSBhanu Prakash Gollapudi 			list_del_init(&io_req->link);
79492886c9cSBhanu Prakash Gollapudi 			io_req->on_tmf_queue = 0;
79592886c9cSBhanu Prakash Gollapudi 		}
7960e0fcef9SSaurav Kashyap 		io_req->wait_for_cleanup_comp = 1;
7970e0fcef9SSaurav Kashyap 		init_completion(&io_req->cleanup_done);
79892886c9cSBhanu Prakash Gollapudi 		bnx2fc_initiate_cleanup(io_req);
79992886c9cSBhanu Prakash Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
8000e0fcef9SSaurav Kashyap 		rc = wait_for_completion_timeout(&io_req->cleanup_done,
80192886c9cSBhanu Prakash Gollapudi 						 BNX2FC_FW_TIMEOUT);
80292886c9cSBhanu Prakash Gollapudi 		spin_lock_bh(&tgt->tgt_lock);
8030e0fcef9SSaurav Kashyap 		io_req->wait_for_cleanup_comp = 0;
80492886c9cSBhanu Prakash Gollapudi 		if (!rc)
80592886c9cSBhanu Prakash Gollapudi 			kref_put(&io_req->refcount, bnx2fc_cmd_release);
80692886c9cSBhanu Prakash Gollapudi 	}
807853e2bd2SBhanu Gollapudi 
808853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
809853e2bd2SBhanu Gollapudi 
810853e2bd2SBhanu Gollapudi 	if (!rc) {
811b2a554ffSBhanu Prakash Gollapudi 		BNX2FC_TGT_DBG(tgt, "task mgmt command failed...\n");
812853e2bd2SBhanu Gollapudi 		rc = FAILED;
813853e2bd2SBhanu Gollapudi 	} else {
814b2a554ffSBhanu Prakash Gollapudi 		BNX2FC_TGT_DBG(tgt, "task mgmt command success...\n");
815853e2bd2SBhanu Gollapudi 		rc = SUCCESS;
816853e2bd2SBhanu Gollapudi 	}
817853e2bd2SBhanu Gollapudi tmf_err:
818853e2bd2SBhanu Gollapudi 	return rc;
819853e2bd2SBhanu Gollapudi }
820853e2bd2SBhanu Gollapudi 
821853e2bd2SBhanu Gollapudi int bnx2fc_initiate_abts(struct bnx2fc_cmd *io_req)
822853e2bd2SBhanu Gollapudi {
823853e2bd2SBhanu Gollapudi 	struct fc_lport *lport;
824853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
825853e2bd2SBhanu Gollapudi 	struct fc_rport *rport = tgt->rport;
826853e2bd2SBhanu Gollapudi 	struct fc_rport_priv *rdata = tgt->rdata;
827aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface;
828853e2bd2SBhanu Gollapudi 	struct fcoe_port *port;
829853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *abts_io_req;
830853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task;
831853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task_page;
832853e2bd2SBhanu Gollapudi 	struct fc_frame_header *fc_hdr;
833853e2bd2SBhanu Gollapudi 	struct bnx2fc_mp_req *abts_req;
834853e2bd2SBhanu Gollapudi 	int task_idx, index;
835853e2bd2SBhanu Gollapudi 	u32 sid, did;
836853e2bd2SBhanu Gollapudi 	u16 xid;
837853e2bd2SBhanu Gollapudi 	int rc = SUCCESS;
838853e2bd2SBhanu Gollapudi 	u32 r_a_tov = rdata->r_a_tov;
839853e2bd2SBhanu Gollapudi 
840853e2bd2SBhanu Gollapudi 	/* called with tgt_lock held */
841853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_abts\n");
842853e2bd2SBhanu Gollapudi 
843853e2bd2SBhanu Gollapudi 	port = io_req->port;
844aea71a02SBhanu Prakash Gollapudi 	interface = port->priv;
845853e2bd2SBhanu Gollapudi 	lport = port->lport;
846853e2bd2SBhanu Gollapudi 
847853e2bd2SBhanu Gollapudi 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
848853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "initiate_abts: tgt not offloaded\n");
849853e2bd2SBhanu Gollapudi 		rc = FAILED;
850853e2bd2SBhanu Gollapudi 		goto abts_err;
851853e2bd2SBhanu Gollapudi 	}
852853e2bd2SBhanu Gollapudi 
853853e2bd2SBhanu Gollapudi 	if (rport == NULL) {
854b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "initiate_abts: rport is NULL\n");
855853e2bd2SBhanu Gollapudi 		rc = FAILED;
856853e2bd2SBhanu Gollapudi 		goto abts_err;
857853e2bd2SBhanu Gollapudi 	}
858853e2bd2SBhanu Gollapudi 
859853e2bd2SBhanu Gollapudi 	if (lport->state != LPORT_ST_READY || !(lport->link_up)) {
860853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "initiate_abts: link is not ready\n");
861853e2bd2SBhanu Gollapudi 		rc = FAILED;
862853e2bd2SBhanu Gollapudi 		goto abts_err;
863853e2bd2SBhanu Gollapudi 	}
864853e2bd2SBhanu Gollapudi 
865853e2bd2SBhanu Gollapudi 	abts_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_ABTS);
866853e2bd2SBhanu Gollapudi 	if (!abts_io_req) {
867886a0b54SColin Ian King 		printk(KERN_ERR PFX "abts: couldn't allocate cmd\n");
868853e2bd2SBhanu Gollapudi 		rc = FAILED;
869853e2bd2SBhanu Gollapudi 		goto abts_err;
870853e2bd2SBhanu Gollapudi 	}
871853e2bd2SBhanu Gollapudi 
872853e2bd2SBhanu Gollapudi 	/* Initialize rest of io_req fields */
873853e2bd2SBhanu Gollapudi 	abts_io_req->sc_cmd = NULL;
874853e2bd2SBhanu Gollapudi 	abts_io_req->port = port;
875853e2bd2SBhanu Gollapudi 	abts_io_req->tgt = tgt;
876853e2bd2SBhanu Gollapudi 	abts_io_req->data_xfer_len = 0; /* No data transfer for ABTS */
877853e2bd2SBhanu Gollapudi 
878853e2bd2SBhanu Gollapudi 	abts_req = (struct bnx2fc_mp_req *)&(abts_io_req->mp_req);
879853e2bd2SBhanu Gollapudi 	memset(abts_req, 0, sizeof(struct bnx2fc_mp_req));
880853e2bd2SBhanu Gollapudi 
881853e2bd2SBhanu Gollapudi 	/* Fill FC header */
882853e2bd2SBhanu Gollapudi 	fc_hdr = &(abts_req->req_fc_hdr);
883853e2bd2SBhanu Gollapudi 
884853e2bd2SBhanu Gollapudi 	/* Obtain oxid and rxid for the original exchange to be aborted */
885853e2bd2SBhanu Gollapudi 	fc_hdr->fh_ox_id = htons(io_req->xid);
886619c5cb6SVlad Zolotarov 	fc_hdr->fh_rx_id = htons(io_req->task->rxwr_txrd.var_ctx.rx_id);
887853e2bd2SBhanu Gollapudi 
888853e2bd2SBhanu Gollapudi 	sid = tgt->sid;
889853e2bd2SBhanu Gollapudi 	did = rport->port_id;
890853e2bd2SBhanu Gollapudi 
891853e2bd2SBhanu Gollapudi 	__fc_fill_fc_hdr(fc_hdr, FC_RCTL_BA_ABTS, did, sid,
892853e2bd2SBhanu Gollapudi 			   FC_TYPE_BLS, FC_FC_FIRST_SEQ | FC_FC_END_SEQ |
893853e2bd2SBhanu Gollapudi 			   FC_FC_SEQ_INIT, 0);
894853e2bd2SBhanu Gollapudi 
895853e2bd2SBhanu Gollapudi 	xid = abts_io_req->xid;
896853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(abts_io_req, "ABTS io_req\n");
897853e2bd2SBhanu Gollapudi 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
898853e2bd2SBhanu Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
899853e2bd2SBhanu Gollapudi 
900853e2bd2SBhanu Gollapudi 	/* Initialize task context for this IO request */
901aea71a02SBhanu Prakash Gollapudi 	task_page = (struct fcoe_task_ctx_entry *)
902aea71a02SBhanu Prakash Gollapudi 			interface->hba->task_ctx[task_idx];
903853e2bd2SBhanu Gollapudi 	task = &(task_page[index]);
904853e2bd2SBhanu Gollapudi 	bnx2fc_init_mp_task(abts_io_req, task);
905853e2bd2SBhanu Gollapudi 
906853e2bd2SBhanu Gollapudi 	/*
907853e2bd2SBhanu Gollapudi 	 * ABTS task is a temporary task that will be cleaned up
908853e2bd2SBhanu Gollapudi 	 * irrespective of ABTS response. We need to start the timer
909853e2bd2SBhanu Gollapudi 	 * for the original exchange, as the CQE is posted for the original
910853e2bd2SBhanu Gollapudi 	 * IO request.
911853e2bd2SBhanu Gollapudi 	 *
912853e2bd2SBhanu Gollapudi 	 * Timer for ABTS is started only when it is originated by a
913853e2bd2SBhanu Gollapudi 	 * TM request. For the ABTS issued as part of ULP timeout,
914853e2bd2SBhanu Gollapudi 	 * scsi-ml maintains the timers.
915853e2bd2SBhanu Gollapudi 	 */
916853e2bd2SBhanu Gollapudi 
917853e2bd2SBhanu Gollapudi 	/* if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))*/
918853e2bd2SBhanu Gollapudi 	bnx2fc_cmd_timer_set(io_req, 2 * r_a_tov);
919853e2bd2SBhanu Gollapudi 
920853e2bd2SBhanu Gollapudi 	/* Obtain free SQ entry */
921853e2bd2SBhanu Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
922853e2bd2SBhanu Gollapudi 
923853e2bd2SBhanu Gollapudi 	/* Ring doorbell */
924853e2bd2SBhanu Gollapudi 	bnx2fc_ring_doorbell(tgt);
925853e2bd2SBhanu Gollapudi 
926853e2bd2SBhanu Gollapudi abts_err:
927853e2bd2SBhanu Gollapudi 	return rc;
928853e2bd2SBhanu Gollapudi }
929853e2bd2SBhanu Gollapudi 
9306c5a7ce4SBhanu Prakash Gollapudi int bnx2fc_initiate_seq_cleanup(struct bnx2fc_cmd *orig_io_req, u32 offset,
9316c5a7ce4SBhanu Prakash Gollapudi 				enum fc_rctl r_ctl)
9326c5a7ce4SBhanu Prakash Gollapudi {
9336c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_rport *tgt = orig_io_req->tgt;
9346c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface;
9356c5a7ce4SBhanu Prakash Gollapudi 	struct fcoe_port *port;
9366c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_cmd *seq_clnp_req;
9376c5a7ce4SBhanu Prakash Gollapudi 	struct fcoe_task_ctx_entry *task;
9386c5a7ce4SBhanu Prakash Gollapudi 	struct fcoe_task_ctx_entry *task_page;
9396c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_els_cb_arg *cb_arg = NULL;
9406c5a7ce4SBhanu Prakash Gollapudi 	int task_idx, index;
9416c5a7ce4SBhanu Prakash Gollapudi 	u16 xid;
9426c5a7ce4SBhanu Prakash Gollapudi 	int rc = 0;
9436c5a7ce4SBhanu Prakash Gollapudi 
9446c5a7ce4SBhanu Prakash Gollapudi 	BNX2FC_IO_DBG(orig_io_req, "bnx2fc_initiate_seq_cleanup xid = 0x%x\n",
9456c5a7ce4SBhanu Prakash Gollapudi 		   orig_io_req->xid);
9466c5a7ce4SBhanu Prakash Gollapudi 	kref_get(&orig_io_req->refcount);
9476c5a7ce4SBhanu Prakash Gollapudi 
9486c5a7ce4SBhanu Prakash Gollapudi 	port = orig_io_req->port;
9496c5a7ce4SBhanu Prakash Gollapudi 	interface = port->priv;
9506c5a7ce4SBhanu Prakash Gollapudi 
9516c5a7ce4SBhanu Prakash Gollapudi 	cb_arg = kzalloc(sizeof(struct bnx2fc_els_cb_arg), GFP_ATOMIC);
9526c5a7ce4SBhanu Prakash Gollapudi 	if (!cb_arg) {
9536c5a7ce4SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "Unable to alloc cb_arg for seq clnup\n");
9546c5a7ce4SBhanu Prakash Gollapudi 		rc = -ENOMEM;
9556c5a7ce4SBhanu Prakash Gollapudi 		goto cleanup_err;
9566c5a7ce4SBhanu Prakash Gollapudi 	}
9576c5a7ce4SBhanu Prakash Gollapudi 
9586c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req = bnx2fc_elstm_alloc(tgt, BNX2FC_SEQ_CLEANUP);
9596c5a7ce4SBhanu Prakash Gollapudi 	if (!seq_clnp_req) {
960886a0b54SColin Ian King 		printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
9616c5a7ce4SBhanu Prakash Gollapudi 		rc = -ENOMEM;
9626c5a7ce4SBhanu Prakash Gollapudi 		kfree(cb_arg);
9636c5a7ce4SBhanu Prakash Gollapudi 		goto cleanup_err;
9646c5a7ce4SBhanu Prakash Gollapudi 	}
9656c5a7ce4SBhanu Prakash Gollapudi 	/* Initialize rest of io_req fields */
9666c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->sc_cmd = NULL;
9676c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->port = port;
9686c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->tgt = tgt;
9696c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->data_xfer_len = 0; /* No data transfer for cleanup */
9706c5a7ce4SBhanu Prakash Gollapudi 
9716c5a7ce4SBhanu Prakash Gollapudi 	xid = seq_clnp_req->xid;
9726c5a7ce4SBhanu Prakash Gollapudi 
9736c5a7ce4SBhanu Prakash Gollapudi 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
9746c5a7ce4SBhanu Prakash Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
9756c5a7ce4SBhanu Prakash Gollapudi 
9766c5a7ce4SBhanu Prakash Gollapudi 	/* Initialize task context for this IO request */
9776c5a7ce4SBhanu Prakash Gollapudi 	task_page = (struct fcoe_task_ctx_entry *)
9786c5a7ce4SBhanu Prakash Gollapudi 		     interface->hba->task_ctx[task_idx];
9796c5a7ce4SBhanu Prakash Gollapudi 	task = &(task_page[index]);
9806c5a7ce4SBhanu Prakash Gollapudi 	cb_arg->aborted_io_req = orig_io_req;
9816c5a7ce4SBhanu Prakash Gollapudi 	cb_arg->io_req = seq_clnp_req;
9826c5a7ce4SBhanu Prakash Gollapudi 	cb_arg->r_ctl = r_ctl;
9836c5a7ce4SBhanu Prakash Gollapudi 	cb_arg->offset = offset;
9846c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->cb_arg = cb_arg;
9856c5a7ce4SBhanu Prakash Gollapudi 
9866c5a7ce4SBhanu Prakash Gollapudi 	printk(KERN_ERR PFX "call init_seq_cleanup_task\n");
9876c5a7ce4SBhanu Prakash Gollapudi 	bnx2fc_init_seq_cleanup_task(seq_clnp_req, task, orig_io_req, offset);
9886c5a7ce4SBhanu Prakash Gollapudi 
9896c5a7ce4SBhanu Prakash Gollapudi 	/* Obtain free SQ entry */
9906c5a7ce4SBhanu Prakash Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
9916c5a7ce4SBhanu Prakash Gollapudi 
9926c5a7ce4SBhanu Prakash Gollapudi 	/* Ring doorbell */
9936c5a7ce4SBhanu Prakash Gollapudi 	bnx2fc_ring_doorbell(tgt);
9946c5a7ce4SBhanu Prakash Gollapudi cleanup_err:
9956c5a7ce4SBhanu Prakash Gollapudi 	return rc;
9966c5a7ce4SBhanu Prakash Gollapudi }
9976c5a7ce4SBhanu Prakash Gollapudi 
998853e2bd2SBhanu Gollapudi int bnx2fc_initiate_cleanup(struct bnx2fc_cmd *io_req)
999853e2bd2SBhanu Gollapudi {
1000853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1001aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface;
1002853e2bd2SBhanu Gollapudi 	struct fcoe_port *port;
1003853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *cleanup_io_req;
1004853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task;
1005853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task_page;
1006853e2bd2SBhanu Gollapudi 	int task_idx, index;
1007853e2bd2SBhanu Gollapudi 	u16 xid, orig_xid;
1008853e2bd2SBhanu Gollapudi 	int rc = 0;
1009853e2bd2SBhanu Gollapudi 
1010853e2bd2SBhanu Gollapudi 	/* ASSUMPTION: called with tgt_lock held */
1011853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_initiate_cleanup\n");
1012853e2bd2SBhanu Gollapudi 
1013853e2bd2SBhanu Gollapudi 	port = io_req->port;
1014aea71a02SBhanu Prakash Gollapudi 	interface = port->priv;
1015853e2bd2SBhanu Gollapudi 
1016853e2bd2SBhanu Gollapudi 	cleanup_io_req = bnx2fc_elstm_alloc(tgt, BNX2FC_CLEANUP);
1017853e2bd2SBhanu Gollapudi 	if (!cleanup_io_req) {
1018886a0b54SColin Ian King 		printk(KERN_ERR PFX "cleanup: couldn't allocate cmd\n");
1019853e2bd2SBhanu Gollapudi 		rc = -1;
1020853e2bd2SBhanu Gollapudi 		goto cleanup_err;
1021853e2bd2SBhanu Gollapudi 	}
1022853e2bd2SBhanu Gollapudi 
1023853e2bd2SBhanu Gollapudi 	/* Initialize rest of io_req fields */
1024853e2bd2SBhanu Gollapudi 	cleanup_io_req->sc_cmd = NULL;
1025853e2bd2SBhanu Gollapudi 	cleanup_io_req->port = port;
1026853e2bd2SBhanu Gollapudi 	cleanup_io_req->tgt = tgt;
1027853e2bd2SBhanu Gollapudi 	cleanup_io_req->data_xfer_len = 0; /* No data transfer for cleanup */
1028853e2bd2SBhanu Gollapudi 
1029853e2bd2SBhanu Gollapudi 	xid = cleanup_io_req->xid;
1030853e2bd2SBhanu Gollapudi 
1031853e2bd2SBhanu Gollapudi 	task_idx = xid/BNX2FC_TASKS_PER_PAGE;
1032853e2bd2SBhanu Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
1033853e2bd2SBhanu Gollapudi 
1034853e2bd2SBhanu Gollapudi 	/* Initialize task context for this IO request */
1035aea71a02SBhanu Prakash Gollapudi 	task_page = (struct fcoe_task_ctx_entry *)
1036aea71a02SBhanu Prakash Gollapudi 			interface->hba->task_ctx[task_idx];
1037853e2bd2SBhanu Gollapudi 	task = &(task_page[index]);
1038853e2bd2SBhanu Gollapudi 	orig_xid = io_req->xid;
1039853e2bd2SBhanu Gollapudi 
1040853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "CLEANUP io_req xid = 0x%x\n", xid);
1041853e2bd2SBhanu Gollapudi 
1042853e2bd2SBhanu Gollapudi 	bnx2fc_init_cleanup_task(cleanup_io_req, task, orig_xid);
1043853e2bd2SBhanu Gollapudi 
1044853e2bd2SBhanu Gollapudi 	/* Obtain free SQ entry */
1045853e2bd2SBhanu Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
1046853e2bd2SBhanu Gollapudi 
104725ad7394SSaurav Kashyap 	/* Set flag that cleanup request is pending with the firmware */
104825ad7394SSaurav Kashyap 	set_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
104925ad7394SSaurav Kashyap 
1050853e2bd2SBhanu Gollapudi 	/* Ring doorbell */
1051853e2bd2SBhanu Gollapudi 	bnx2fc_ring_doorbell(tgt);
1052853e2bd2SBhanu Gollapudi 
1053853e2bd2SBhanu Gollapudi cleanup_err:
1054853e2bd2SBhanu Gollapudi 	return rc;
1055853e2bd2SBhanu Gollapudi }
1056853e2bd2SBhanu Gollapudi 
1057853e2bd2SBhanu Gollapudi /**
1058853e2bd2SBhanu Gollapudi  * bnx2fc_eh_target_reset: Reset a target
1059853e2bd2SBhanu Gollapudi  *
1060853e2bd2SBhanu Gollapudi  * @sc_cmd:	SCSI command
1061853e2bd2SBhanu Gollapudi  *
1062853e2bd2SBhanu Gollapudi  * Set from SCSI host template to send task mgmt command to the target
1063853e2bd2SBhanu Gollapudi  *	and wait for the response
1064853e2bd2SBhanu Gollapudi  */
1065853e2bd2SBhanu Gollapudi int bnx2fc_eh_target_reset(struct scsi_cmnd *sc_cmd)
1066853e2bd2SBhanu Gollapudi {
1067853e2bd2SBhanu Gollapudi 	return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
1068853e2bd2SBhanu Gollapudi }
1069853e2bd2SBhanu Gollapudi 
1070853e2bd2SBhanu Gollapudi /**
1071853e2bd2SBhanu Gollapudi  * bnx2fc_eh_device_reset - Reset a single LUN
1072853e2bd2SBhanu Gollapudi  *
1073853e2bd2SBhanu Gollapudi  * @sc_cmd:	SCSI command
1074853e2bd2SBhanu Gollapudi  *
1075853e2bd2SBhanu Gollapudi  * Set from SCSI host template to send task mgmt command to the target
1076853e2bd2SBhanu Gollapudi  *	and wait for the response
1077853e2bd2SBhanu Gollapudi  */
1078853e2bd2SBhanu Gollapudi int bnx2fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
1079853e2bd2SBhanu Gollapudi {
1080853e2bd2SBhanu Gollapudi 	return bnx2fc_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
1081853e2bd2SBhanu Gollapudi }
1082853e2bd2SBhanu Gollapudi 
10833f7d67daSBaoyou Xie static int bnx2fc_abts_cleanup(struct bnx2fc_cmd *io_req)
1084bc834e07SJules Irenge 	__must_hold(&tgt->tgt_lock)
1085c1bb4f33SBhanu Prakash Gollapudi {
1086c1bb4f33SBhanu Prakash Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1087faae19beSChad Dupuis 	unsigned int time_left;
1088c1bb4f33SBhanu Prakash Gollapudi 
10890e0fcef9SSaurav Kashyap 	init_completion(&io_req->cleanup_done);
10900e0fcef9SSaurav Kashyap 	io_req->wait_for_cleanup_comp = 1;
1091c1bb4f33SBhanu Prakash Gollapudi 	bnx2fc_initiate_cleanup(io_req);
1092c1bb4f33SBhanu Prakash Gollapudi 
1093c1bb4f33SBhanu Prakash Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
1094c1bb4f33SBhanu Prakash Gollapudi 
1095c1bb4f33SBhanu Prakash Gollapudi 	/*
1096faae19beSChad Dupuis 	 * Can't wait forever on cleanup response lest we let the SCSI error
1097faae19beSChad Dupuis 	 * handler wait forever
1098faae19beSChad Dupuis 	 */
10990e0fcef9SSaurav Kashyap 	time_left = wait_for_completion_timeout(&io_req->cleanup_done,
1100faae19beSChad Dupuis 						BNX2FC_FW_TIMEOUT);
1101a92ac6eeSChad Dupuis 	if (!time_left) {
1102faae19beSChad Dupuis 		BNX2FC_IO_DBG(io_req, "%s(): Wait for cleanup timed out.\n",
1103faae19beSChad Dupuis 			      __func__);
1104faae19beSChad Dupuis 
1105faae19beSChad Dupuis 		/*
1106a92ac6eeSChad Dupuis 		 * Put the extra reference to the SCSI command since it would
1107a92ac6eeSChad Dupuis 		 * not have been returned in this case.
1108c1bb4f33SBhanu Prakash Gollapudi 		 */
1109c1bb4f33SBhanu Prakash Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1110a92ac6eeSChad Dupuis 	}
1111c1bb4f33SBhanu Prakash Gollapudi 
1112c1bb4f33SBhanu Prakash Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
11130e0fcef9SSaurav Kashyap 	io_req->wait_for_cleanup_comp = 0;
11149e29a682SDing Xiang 	return SUCCESS;
1115c1bb4f33SBhanu Prakash Gollapudi }
1116faae19beSChad Dupuis 
1117853e2bd2SBhanu Gollapudi /**
1118853e2bd2SBhanu Gollapudi  * bnx2fc_eh_abort - eh_abort_handler api to abort an outstanding
1119853e2bd2SBhanu Gollapudi  *			SCSI command
1120853e2bd2SBhanu Gollapudi  *
1121853e2bd2SBhanu Gollapudi  * @sc_cmd:	SCSI_ML command pointer
1122853e2bd2SBhanu Gollapudi  *
1123853e2bd2SBhanu Gollapudi  * SCSI abort request handler
1124853e2bd2SBhanu Gollapudi  */
1125853e2bd2SBhanu Gollapudi int bnx2fc_eh_abort(struct scsi_cmnd *sc_cmd)
1126853e2bd2SBhanu Gollapudi {
1127853e2bd2SBhanu Gollapudi 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1128853e2bd2SBhanu Gollapudi 	struct fc_rport_libfc_priv *rp = rport->dd_data;
1129853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
1130853e2bd2SBhanu Gollapudi 	struct fc_lport *lport;
1131853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt;
1132617757deSMaurizio Lombardi 	int rc;
1133faae19beSChad Dupuis 	unsigned int time_left;
1134853e2bd2SBhanu Gollapudi 
1135853e2bd2SBhanu Gollapudi 	rc = fc_block_scsi_eh(sc_cmd);
1136853e2bd2SBhanu Gollapudi 	if (rc)
1137853e2bd2SBhanu Gollapudi 		return rc;
1138853e2bd2SBhanu Gollapudi 
1139853e2bd2SBhanu Gollapudi 	lport = shost_priv(sc_cmd->device->host);
1140853e2bd2SBhanu Gollapudi 	if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1141b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "eh_abort: link not ready\n");
1142617757deSMaurizio Lombardi 		return FAILED;
1143853e2bd2SBhanu Gollapudi 	}
1144853e2bd2SBhanu Gollapudi 
1145853e2bd2SBhanu Gollapudi 	tgt = (struct bnx2fc_rport *)&rp[1];
1146853e2bd2SBhanu Gollapudi 
1147853e2bd2SBhanu Gollapudi 	BNX2FC_TGT_DBG(tgt, "Entered bnx2fc_eh_abort\n");
1148853e2bd2SBhanu Gollapudi 
1149853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
1150853e2bd2SBhanu Gollapudi 	io_req = (struct bnx2fc_cmd *)sc_cmd->SCp.ptr;
1151853e2bd2SBhanu Gollapudi 	if (!io_req) {
1152853e2bd2SBhanu Gollapudi 		/* Command might have just completed */
1153853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "eh_abort: io_req is NULL\n");
1154853e2bd2SBhanu Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
1155853e2bd2SBhanu Gollapudi 		return SUCCESS;
1156853e2bd2SBhanu Gollapudi 	}
1157853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "eh_abort - refcnt = %d\n",
11582c935bc5SPeter Zijlstra 		      kref_read(&io_req->refcount));
1159853e2bd2SBhanu Gollapudi 
1160853e2bd2SBhanu Gollapudi 	/* Hold IO request across abort processing */
1161853e2bd2SBhanu Gollapudi 	kref_get(&io_req->refcount);
1162853e2bd2SBhanu Gollapudi 
1163853e2bd2SBhanu Gollapudi 	BUG_ON(tgt != io_req->tgt);
1164853e2bd2SBhanu Gollapudi 
1165853e2bd2SBhanu Gollapudi 	/* Remove the io_req from the active_q. */
1166853e2bd2SBhanu Gollapudi 	/*
1167853e2bd2SBhanu Gollapudi 	 * Task Mgmt functions (LUN RESET & TGT RESET) will not
1168853e2bd2SBhanu Gollapudi 	 * issue an ABTS on this particular IO req, as the
1169853e2bd2SBhanu Gollapudi 	 * io_req is no longer in the active_q.
1170853e2bd2SBhanu Gollapudi 	 */
1171853e2bd2SBhanu Gollapudi 	if (tgt->flush_in_prog) {
1172b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1173853e2bd2SBhanu Gollapudi 			"flush in progress\n", io_req->xid);
1174853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1175853e2bd2SBhanu Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
1176853e2bd2SBhanu Gollapudi 		return SUCCESS;
1177853e2bd2SBhanu Gollapudi 	}
1178853e2bd2SBhanu Gollapudi 
1179853e2bd2SBhanu Gollapudi 	if (io_req->on_active_queue == 0) {
1180b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1181853e2bd2SBhanu Gollapudi 				"not on active_q\n", io_req->xid);
1182853e2bd2SBhanu Gollapudi 		/*
11835c63daf6SChad Dupuis 		 * The IO is still with the FW.
11845c63daf6SChad Dupuis 		 * Return failure and let SCSI-ml retry eh_abort.
1185853e2bd2SBhanu Gollapudi 		 */
1186853e2bd2SBhanu Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
11875c63daf6SChad Dupuis 		return FAILED;
1188853e2bd2SBhanu Gollapudi 	}
1189853e2bd2SBhanu Gollapudi 
1190853e2bd2SBhanu Gollapudi 	/*
1191853e2bd2SBhanu Gollapudi 	 * Only eh_abort processing will remove the IO from
1192853e2bd2SBhanu Gollapudi 	 * active_cmd_q before processing the request. this is
1193853e2bd2SBhanu Gollapudi 	 * done to avoid race conditions between IOs aborted
1194853e2bd2SBhanu Gollapudi 	 * as part of task management completion and eh_abort
1195853e2bd2SBhanu Gollapudi 	 * processing
1196853e2bd2SBhanu Gollapudi 	 */
1197853e2bd2SBhanu Gollapudi 	list_del_init(&io_req->link);
1198853e2bd2SBhanu Gollapudi 	io_req->on_active_queue = 0;
1199853e2bd2SBhanu Gollapudi 	/* Move IO req to retire queue */
1200853e2bd2SBhanu Gollapudi 	list_add_tail(&io_req->link, &tgt->io_retire_queue);
1201853e2bd2SBhanu Gollapudi 
12020e0fcef9SSaurav Kashyap 	init_completion(&io_req->abts_done);
12030e0fcef9SSaurav Kashyap 	init_completion(&io_req->cleanup_done);
1204853e2bd2SBhanu Gollapudi 
1205c1bb4f33SBhanu Prakash Gollapudi 	if (test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags)) {
1206b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "eh_abort: io_req (xid = 0x%x) "
1207853e2bd2SBhanu Gollapudi 				"already in abts processing\n", io_req->xid);
120899cc600cSBhanu Prakash Gollapudi 		if (cancel_delayed_work(&io_req->timeout_work))
120999cc600cSBhanu Prakash Gollapudi 			kref_put(&io_req->refcount,
121099cc600cSBhanu Prakash Gollapudi 				 bnx2fc_cmd_release); /* drop timer hold */
1211faae19beSChad Dupuis 		/*
1212faae19beSChad Dupuis 		 * We don't want to hold off the upper layer timer so simply
1213faae19beSChad Dupuis 		 * cleanup the command and return that I/O was successfully
1214faae19beSChad Dupuis 		 * aborted.
1215faae19beSChad Dupuis 		 */
121650a87414SChad Dupuis 		rc = bnx2fc_abts_cleanup(io_req);
121721837896SEddie Wai 		/* This only occurs when an task abort was requested while ABTS
121821837896SEddie Wai 		   is in progress.  Setting the IO_CLEANUP flag will skip the
121921837896SEddie Wai 		   RRQ process in the case when the fw generated SCSI_CMD cmpl
122021837896SEddie Wai 		   was a result from the ABTS request rather than the CLEANUP
122121837896SEddie Wai 		   request */
122221837896SEddie Wai 		set_bit(BNX2FC_FLAG_IO_CLEANUP,	&io_req->req_flags);
1223faae19beSChad Dupuis 		goto done;
1224c1bb4f33SBhanu Prakash Gollapudi 	}
1225c1bb4f33SBhanu Prakash Gollapudi 
1226c1bb4f33SBhanu Prakash Gollapudi 	/* Cancel the current timer running on this io_req */
1227c1bb4f33SBhanu Prakash Gollapudi 	if (cancel_delayed_work(&io_req->timeout_work))
1228c1bb4f33SBhanu Prakash Gollapudi 		kref_put(&io_req->refcount,
1229c1bb4f33SBhanu Prakash Gollapudi 			 bnx2fc_cmd_release); /* drop timer hold */
1230c1bb4f33SBhanu Prakash Gollapudi 	set_bit(BNX2FC_FLAG_EH_ABORT, &io_req->req_flags);
12310e0fcef9SSaurav Kashyap 	io_req->wait_for_abts_comp = 1;
1232c1bb4f33SBhanu Prakash Gollapudi 	rc = bnx2fc_initiate_abts(io_req);
1233c1bb4f33SBhanu Prakash Gollapudi 	if (rc == FAILED) {
12340e0fcef9SSaurav Kashyap 		io_req->wait_for_cleanup_comp = 1;
123599cc600cSBhanu Prakash Gollapudi 		bnx2fc_initiate_cleanup(io_req);
123699cc600cSBhanu Prakash Gollapudi 		spin_unlock_bh(&tgt->tgt_lock);
12370e0fcef9SSaurav Kashyap 		wait_for_completion(&io_req->cleanup_done);
123899cc600cSBhanu Prakash Gollapudi 		spin_lock_bh(&tgt->tgt_lock);
12390e0fcef9SSaurav Kashyap 		io_req->wait_for_cleanup_comp = 0;
1240c1bb4f33SBhanu Prakash Gollapudi 		goto done;
1241853e2bd2SBhanu Gollapudi 	}
1242853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
1243853e2bd2SBhanu Gollapudi 
1244faae19beSChad Dupuis 	/* Wait 2 * RA_TOV + 1 to be sure timeout function hasn't fired */
12450e0fcef9SSaurav Kashyap 	time_left = wait_for_completion_timeout(&io_req->abts_done,
124665309ef6SLaurence Oberman 					msecs_to_jiffies(2 * rp->r_a_tov + 1));
1247faae19beSChad Dupuis 	if (time_left)
12480e0fcef9SSaurav Kashyap 		BNX2FC_IO_DBG(io_req,
12490e0fcef9SSaurav Kashyap 			      "Timed out in eh_abort waiting for abts_done");
1250853e2bd2SBhanu Gollapudi 
1251853e2bd2SBhanu Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
12520e0fcef9SSaurav Kashyap 	io_req->wait_for_abts_comp = 0;
12535d78f175SBhanu Prakash Gollapudi 	if (test_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
12545d78f175SBhanu Prakash Gollapudi 		BNX2FC_IO_DBG(io_req, "IO completed in a different context\n");
12555d78f175SBhanu Prakash Gollapudi 		rc = SUCCESS;
12565d78f175SBhanu Prakash Gollapudi 	} else if (!(test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1257853e2bd2SBhanu Gollapudi 				      &io_req->req_flags))) {
1258853e2bd2SBhanu Gollapudi 		/* Let the scsi-ml try to recover this command */
1259853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "abort failed, xid = 0x%x\n",
1260853e2bd2SBhanu Gollapudi 		       io_req->xid);
1261faae19beSChad Dupuis 		/*
1262faae19beSChad Dupuis 		 * Cleanup firmware residuals before returning control back
1263faae19beSChad Dupuis 		 * to SCSI ML.
1264faae19beSChad Dupuis 		 */
126550a87414SChad Dupuis 		rc = bnx2fc_abts_cleanup(io_req);
1266faae19beSChad Dupuis 		goto done;
1267853e2bd2SBhanu Gollapudi 	} else {
1268853e2bd2SBhanu Gollapudi 		/*
1269853e2bd2SBhanu Gollapudi 		 * We come here even when there was a race condition
1270853e2bd2SBhanu Gollapudi 		 * between timeout and abts completion, and abts
1271853e2bd2SBhanu Gollapudi 		 * completion happens just in time.
1272853e2bd2SBhanu Gollapudi 		 */
1273853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "abort succeeded\n");
1274853e2bd2SBhanu Gollapudi 		rc = SUCCESS;
1275853e2bd2SBhanu Gollapudi 		bnx2fc_scsi_done(io_req, DID_ABORT);
1276853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1277853e2bd2SBhanu Gollapudi 	}
1278c1bb4f33SBhanu Prakash Gollapudi done:
1279853e2bd2SBhanu Gollapudi 	/* release the reference taken in eh_abort */
1280853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
1281853e2bd2SBhanu Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
1282853e2bd2SBhanu Gollapudi 	return rc;
1283853e2bd2SBhanu Gollapudi }
1284853e2bd2SBhanu Gollapudi 
12856c5a7ce4SBhanu Prakash Gollapudi void bnx2fc_process_seq_cleanup_compl(struct bnx2fc_cmd *seq_clnp_req,
12866c5a7ce4SBhanu Prakash Gollapudi 				      struct fcoe_task_ctx_entry *task,
12876c5a7ce4SBhanu Prakash Gollapudi 				      u8 rx_state)
12886c5a7ce4SBhanu Prakash Gollapudi {
12896c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_els_cb_arg *cb_arg = seq_clnp_req->cb_arg;
12906c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_cmd *orig_io_req = cb_arg->aborted_io_req;
12916c5a7ce4SBhanu Prakash Gollapudi 	u32 offset = cb_arg->offset;
12926c5a7ce4SBhanu Prakash Gollapudi 	enum fc_rctl r_ctl = cb_arg->r_ctl;
12936c5a7ce4SBhanu Prakash Gollapudi 	int rc = 0;
12946c5a7ce4SBhanu Prakash Gollapudi 	struct bnx2fc_rport *tgt = orig_io_req->tgt;
12956c5a7ce4SBhanu Prakash Gollapudi 
12966c5a7ce4SBhanu Prakash Gollapudi 	BNX2FC_IO_DBG(orig_io_req, "Entered process_cleanup_compl xid = 0x%x"
12976c5a7ce4SBhanu Prakash Gollapudi 			      "cmd_type = %d\n",
12986c5a7ce4SBhanu Prakash Gollapudi 		   seq_clnp_req->xid, seq_clnp_req->cmd_type);
12996c5a7ce4SBhanu Prakash Gollapudi 
13006c5a7ce4SBhanu Prakash Gollapudi 	if (rx_state == FCOE_TASK_RX_STATE_IGNORED_SEQUENCE_CLEANUP) {
13016c5a7ce4SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "seq cleanup ignored - xid = 0x%x\n",
13026c5a7ce4SBhanu Prakash Gollapudi 			seq_clnp_req->xid);
13036c5a7ce4SBhanu Prakash Gollapudi 		goto free_cb_arg;
13046c5a7ce4SBhanu Prakash Gollapudi 	}
13056c5a7ce4SBhanu Prakash Gollapudi 
13066c5a7ce4SBhanu Prakash Gollapudi 	spin_unlock_bh(&tgt->tgt_lock);
13076c5a7ce4SBhanu Prakash Gollapudi 	rc = bnx2fc_send_srr(orig_io_req, offset, r_ctl);
13086c5a7ce4SBhanu Prakash Gollapudi 	spin_lock_bh(&tgt->tgt_lock);
13096c5a7ce4SBhanu Prakash Gollapudi 
13106c5a7ce4SBhanu Prakash Gollapudi 	if (rc)
13116c5a7ce4SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "clnup_compl: Unable to send SRR"
13126c5a7ce4SBhanu Prakash Gollapudi 			" IO will abort\n");
13136c5a7ce4SBhanu Prakash Gollapudi 	seq_clnp_req->cb_arg = NULL;
13146c5a7ce4SBhanu Prakash Gollapudi 	kref_put(&orig_io_req->refcount, bnx2fc_cmd_release);
13156c5a7ce4SBhanu Prakash Gollapudi free_cb_arg:
13166c5a7ce4SBhanu Prakash Gollapudi 	kfree(cb_arg);
13176c5a7ce4SBhanu Prakash Gollapudi 	return;
13186c5a7ce4SBhanu Prakash Gollapudi }
13196c5a7ce4SBhanu Prakash Gollapudi 
1320853e2bd2SBhanu Gollapudi void bnx2fc_process_cleanup_compl(struct bnx2fc_cmd *io_req,
1321853e2bd2SBhanu Gollapudi 				  struct fcoe_task_ctx_entry *task,
1322853e2bd2SBhanu Gollapudi 				  u8 num_rq)
1323853e2bd2SBhanu Gollapudi {
1324853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered process_cleanup_compl "
1325853e2bd2SBhanu Gollapudi 			      "refcnt = %d, cmd_type = %d\n",
13262c935bc5SPeter Zijlstra 		   kref_read(&io_req->refcount), io_req->cmd_type);
132725ad7394SSaurav Kashyap 	/*
132825ad7394SSaurav Kashyap 	 * Test whether there is a cleanup request pending. If not just
132925ad7394SSaurav Kashyap 	 * exit.
133025ad7394SSaurav Kashyap 	 */
133125ad7394SSaurav Kashyap 	if (!test_and_clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ,
133225ad7394SSaurav Kashyap 				&io_req->req_flags))
133325ad7394SSaurav Kashyap 		return;
133425ad7394SSaurav Kashyap 	/*
133525ad7394SSaurav Kashyap 	 * If we receive a cleanup completion for this request then the
133625ad7394SSaurav Kashyap 	 * firmware will not give us an abort completion for this request
133725ad7394SSaurav Kashyap 	 * so clear any ABTS pending flags.
133825ad7394SSaurav Kashyap 	 */
133925ad7394SSaurav Kashyap 	if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags) &&
134025ad7394SSaurav Kashyap 	    !test_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags)) {
134125ad7394SSaurav Kashyap 		set_bit(BNX2FC_FLAG_ABTS_DONE, &io_req->req_flags);
134225ad7394SSaurav Kashyap 		if (io_req->wait_for_abts_comp)
134325ad7394SSaurav Kashyap 			complete(&io_req->abts_done);
134425ad7394SSaurav Kashyap 	}
134525ad7394SSaurav Kashyap 
1346853e2bd2SBhanu Gollapudi 	bnx2fc_scsi_done(io_req, DID_ERROR);
1347853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
13480e0fcef9SSaurav Kashyap 	if (io_req->wait_for_cleanup_comp)
13490e0fcef9SSaurav Kashyap 		complete(&io_req->cleanup_done);
1350853e2bd2SBhanu Gollapudi }
1351853e2bd2SBhanu Gollapudi 
1352853e2bd2SBhanu Gollapudi void bnx2fc_process_abts_compl(struct bnx2fc_cmd *io_req,
1353853e2bd2SBhanu Gollapudi 			       struct fcoe_task_ctx_entry *task,
1354853e2bd2SBhanu Gollapudi 			       u8 num_rq)
1355853e2bd2SBhanu Gollapudi {
1356853e2bd2SBhanu Gollapudi 	u32 r_ctl;
1357853e2bd2SBhanu Gollapudi 	u32 r_a_tov = FC_DEF_R_A_TOV;
1358853e2bd2SBhanu Gollapudi 	u8 issue_rrq = 0;
1359853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1360853e2bd2SBhanu Gollapudi 
1361853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered process_abts_compl xid = 0x%x"
1362853e2bd2SBhanu Gollapudi 			      "refcnt = %d, cmd_type = %d\n",
1363853e2bd2SBhanu Gollapudi 		   io_req->xid,
13642c935bc5SPeter Zijlstra 		   kref_read(&io_req->refcount), io_req->cmd_type);
1365853e2bd2SBhanu Gollapudi 
1366853e2bd2SBhanu Gollapudi 	if (test_and_set_bit(BNX2FC_FLAG_ABTS_DONE,
1367853e2bd2SBhanu Gollapudi 				       &io_req->req_flags)) {
1368853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "Timer context finished processing"
1369853e2bd2SBhanu Gollapudi 				" this io\n");
1370853e2bd2SBhanu Gollapudi 		return;
1371853e2bd2SBhanu Gollapudi 	}
1372853e2bd2SBhanu Gollapudi 
137325ad7394SSaurav Kashyap 	/*
137425ad7394SSaurav Kashyap 	 * If we receive an ABTS completion here then we will not receive
137525ad7394SSaurav Kashyap 	 * a cleanup completion so clear any cleanup pending flags.
137625ad7394SSaurav Kashyap 	 */
137725ad7394SSaurav Kashyap 	if (test_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags)) {
137825ad7394SSaurav Kashyap 		clear_bit(BNX2FC_FLAG_ISSUE_CLEANUP_REQ, &io_req->req_flags);
137925ad7394SSaurav Kashyap 		if (io_req->wait_for_cleanup_comp)
138025ad7394SSaurav Kashyap 			complete(&io_req->cleanup_done);
138125ad7394SSaurav Kashyap 	}
138225ad7394SSaurav Kashyap 
1383853e2bd2SBhanu Gollapudi 	/* Do not issue RRQ as this IO is already cleanedup */
1384853e2bd2SBhanu Gollapudi 	if (test_and_set_bit(BNX2FC_FLAG_IO_CLEANUP,
1385853e2bd2SBhanu Gollapudi 				&io_req->req_flags))
1386853e2bd2SBhanu Gollapudi 		goto io_compl;
1387853e2bd2SBhanu Gollapudi 
1388853e2bd2SBhanu Gollapudi 	/*
1389853e2bd2SBhanu Gollapudi 	 * For ABTS issued due to SCSI eh_abort_handler, timeout
1390853e2bd2SBhanu Gollapudi 	 * values are maintained by scsi-ml itself. Cancel timeout
1391853e2bd2SBhanu Gollapudi 	 * in case ABTS issued as part of task management function
1392853e2bd2SBhanu Gollapudi 	 * or due to FW error.
1393853e2bd2SBhanu Gollapudi 	 */
1394853e2bd2SBhanu Gollapudi 	if (test_bit(BNX2FC_FLAG_ISSUE_ABTS, &io_req->req_flags))
1395853e2bd2SBhanu Gollapudi 		if (cancel_delayed_work(&io_req->timeout_work))
1396853e2bd2SBhanu Gollapudi 			kref_put(&io_req->refcount,
1397853e2bd2SBhanu Gollapudi 				 bnx2fc_cmd_release); /* drop timer hold */
1398853e2bd2SBhanu Gollapudi 
1399619c5cb6SVlad Zolotarov 	r_ctl = (u8)task->rxwr_only.union_ctx.comp_info.abts_rsp.r_ctl;
1400853e2bd2SBhanu Gollapudi 
1401853e2bd2SBhanu Gollapudi 	switch (r_ctl) {
1402853e2bd2SBhanu Gollapudi 	case FC_RCTL_BA_ACC:
1403853e2bd2SBhanu Gollapudi 		/*
1404853e2bd2SBhanu Gollapudi 		 * Dont release this cmd yet. It will be relesed
1405853e2bd2SBhanu Gollapudi 		 * after we get RRQ response
1406853e2bd2SBhanu Gollapudi 		 */
1407853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "ABTS response - ACC Send RRQ\n");
1408853e2bd2SBhanu Gollapudi 		issue_rrq = 1;
1409853e2bd2SBhanu Gollapudi 		break;
1410853e2bd2SBhanu Gollapudi 
1411853e2bd2SBhanu Gollapudi 	case FC_RCTL_BA_RJT:
1412853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "ABTS response - RJT\n");
1413853e2bd2SBhanu Gollapudi 		break;
1414853e2bd2SBhanu Gollapudi 	default:
1415853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Unknown ABTS response\n");
1416853e2bd2SBhanu Gollapudi 		break;
1417853e2bd2SBhanu Gollapudi 	}
1418853e2bd2SBhanu Gollapudi 
1419853e2bd2SBhanu Gollapudi 	if (issue_rrq) {
1420853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "Issue RRQ after R_A_TOV\n");
1421853e2bd2SBhanu Gollapudi 		set_bit(BNX2FC_FLAG_ISSUE_RRQ, &io_req->req_flags);
1422853e2bd2SBhanu Gollapudi 	}
1423853e2bd2SBhanu Gollapudi 	set_bit(BNX2FC_FLAG_RETIRE_OXID, &io_req->req_flags);
1424853e2bd2SBhanu Gollapudi 	bnx2fc_cmd_timer_set(io_req, r_a_tov);
1425853e2bd2SBhanu Gollapudi 
1426853e2bd2SBhanu Gollapudi io_compl:
14270e0fcef9SSaurav Kashyap 	if (io_req->wait_for_abts_comp) {
1428853e2bd2SBhanu Gollapudi 		if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1429853e2bd2SBhanu Gollapudi 				       &io_req->req_flags))
14300e0fcef9SSaurav Kashyap 			complete(&io_req->abts_done);
1431853e2bd2SBhanu Gollapudi 	} else {
1432853e2bd2SBhanu Gollapudi 		/*
1433853e2bd2SBhanu Gollapudi 		 * We end up here when ABTS is issued as
1434853e2bd2SBhanu Gollapudi 		 * in asynchronous context, i.e., as part
1435853e2bd2SBhanu Gollapudi 		 * of task management completion, or
1436853e2bd2SBhanu Gollapudi 		 * when FW error is received or when the
1437853e2bd2SBhanu Gollapudi 		 * ABTS is issued when the IO is timed
1438853e2bd2SBhanu Gollapudi 		 * out.
1439853e2bd2SBhanu Gollapudi 		 */
1440853e2bd2SBhanu Gollapudi 
1441853e2bd2SBhanu Gollapudi 		if (io_req->on_active_queue) {
1442853e2bd2SBhanu Gollapudi 			list_del_init(&io_req->link);
1443853e2bd2SBhanu Gollapudi 			io_req->on_active_queue = 0;
1444853e2bd2SBhanu Gollapudi 			/* Move IO req to retire queue */
1445853e2bd2SBhanu Gollapudi 			list_add_tail(&io_req->link, &tgt->io_retire_queue);
1446853e2bd2SBhanu Gollapudi 		}
1447853e2bd2SBhanu Gollapudi 		bnx2fc_scsi_done(io_req, DID_ERROR);
1448853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
1449853e2bd2SBhanu Gollapudi 	}
1450853e2bd2SBhanu Gollapudi }
1451853e2bd2SBhanu Gollapudi 
1452853e2bd2SBhanu Gollapudi static void bnx2fc_lun_reset_cmpl(struct bnx2fc_cmd *io_req)
1453853e2bd2SBhanu Gollapudi {
1454853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1455853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1456d71fb3bdSBhanu Prakash Gollapudi 	struct bnx2fc_cmd *cmd, *tmp;
14579cb78c16SHannes Reinecke 	u64 tm_lun = sc_cmd->device->lun;
14589cb78c16SHannes Reinecke 	u64 lun;
1459853e2bd2SBhanu Gollapudi 	int rc = 0;
1460853e2bd2SBhanu Gollapudi 
1461853e2bd2SBhanu Gollapudi 	/* called with tgt_lock held */
1462853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_lun_reset_cmpl\n");
1463853e2bd2SBhanu Gollapudi 	/*
1464853e2bd2SBhanu Gollapudi 	 * Walk thru the active_ios queue and ABORT the IO
1465853e2bd2SBhanu Gollapudi 	 * that matches with the LUN that was reset
1466853e2bd2SBhanu Gollapudi 	 */
1467d71fb3bdSBhanu Prakash Gollapudi 	list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1468853e2bd2SBhanu Gollapudi 		BNX2FC_TGT_DBG(tgt, "LUN RST cmpl: scan for pending IOs\n");
1469853e2bd2SBhanu Gollapudi 		lun = cmd->sc_cmd->device->lun;
1470853e2bd2SBhanu Gollapudi 		if (lun == tm_lun) {
1471853e2bd2SBhanu Gollapudi 			/* Initiate ABTS on this cmd */
1472853e2bd2SBhanu Gollapudi 			if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1473853e2bd2SBhanu Gollapudi 					      &cmd->req_flags)) {
1474853e2bd2SBhanu Gollapudi 				/* cancel the IO timeout */
1475853e2bd2SBhanu Gollapudi 				if (cancel_delayed_work(&io_req->timeout_work))
1476853e2bd2SBhanu Gollapudi 					kref_put(&io_req->refcount,
1477853e2bd2SBhanu Gollapudi 						 bnx2fc_cmd_release);
1478853e2bd2SBhanu Gollapudi 							/* timer hold */
1479853e2bd2SBhanu Gollapudi 				rc = bnx2fc_initiate_abts(cmd);
148025985edcSLucas De Marchi 				/* abts shouldn't fail in this context */
1481853e2bd2SBhanu Gollapudi 				WARN_ON(rc != SUCCESS);
1482853e2bd2SBhanu Gollapudi 			} else
1483853e2bd2SBhanu Gollapudi 				printk(KERN_ERR PFX "lun_rst: abts already in"
1484853e2bd2SBhanu Gollapudi 					" progress for this IO 0x%x\n",
1485853e2bd2SBhanu Gollapudi 					cmd->xid);
1486853e2bd2SBhanu Gollapudi 		}
1487853e2bd2SBhanu Gollapudi 	}
1488853e2bd2SBhanu Gollapudi }
1489853e2bd2SBhanu Gollapudi 
1490853e2bd2SBhanu Gollapudi static void bnx2fc_tgt_reset_cmpl(struct bnx2fc_cmd *io_req)
1491853e2bd2SBhanu Gollapudi {
1492853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1493d71fb3bdSBhanu Prakash Gollapudi 	struct bnx2fc_cmd *cmd, *tmp;
1494853e2bd2SBhanu Gollapudi 	int rc = 0;
1495853e2bd2SBhanu Gollapudi 
1496853e2bd2SBhanu Gollapudi 	/* called with tgt_lock held */
1497853e2bd2SBhanu Gollapudi 	BNX2FC_IO_DBG(io_req, "Entered bnx2fc_tgt_reset_cmpl\n");
1498853e2bd2SBhanu Gollapudi 	/*
1499853e2bd2SBhanu Gollapudi 	 * Walk thru the active_ios queue and ABORT the IO
1500853e2bd2SBhanu Gollapudi 	 * that matches with the LUN that was reset
1501853e2bd2SBhanu Gollapudi 	 */
1502d71fb3bdSBhanu Prakash Gollapudi 	list_for_each_entry_safe(cmd, tmp, &tgt->active_cmd_queue, link) {
1503853e2bd2SBhanu Gollapudi 		BNX2FC_TGT_DBG(tgt, "TGT RST cmpl: scan for pending IOs\n");
1504853e2bd2SBhanu Gollapudi 		/* Initiate ABTS */
1505853e2bd2SBhanu Gollapudi 		if (!test_and_set_bit(BNX2FC_FLAG_ISSUE_ABTS,
1506853e2bd2SBhanu Gollapudi 							&cmd->req_flags)) {
1507853e2bd2SBhanu Gollapudi 			/* cancel the IO timeout */
1508853e2bd2SBhanu Gollapudi 			if (cancel_delayed_work(&io_req->timeout_work))
1509853e2bd2SBhanu Gollapudi 				kref_put(&io_req->refcount,
1510853e2bd2SBhanu Gollapudi 					 bnx2fc_cmd_release); /* timer hold */
1511853e2bd2SBhanu Gollapudi 			rc = bnx2fc_initiate_abts(cmd);
151225985edcSLucas De Marchi 			/* abts shouldn't fail in this context */
1513853e2bd2SBhanu Gollapudi 			WARN_ON(rc != SUCCESS);
1514853e2bd2SBhanu Gollapudi 
1515853e2bd2SBhanu Gollapudi 		} else
1516853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "tgt_rst: abts already in progress"
1517853e2bd2SBhanu Gollapudi 				" for this IO 0x%x\n", cmd->xid);
1518853e2bd2SBhanu Gollapudi 	}
1519853e2bd2SBhanu Gollapudi }
1520853e2bd2SBhanu Gollapudi 
1521853e2bd2SBhanu Gollapudi void bnx2fc_process_tm_compl(struct bnx2fc_cmd *io_req,
152277331115SJaved Hasan 			     struct fcoe_task_ctx_entry *task, u8 num_rq,
152377331115SJaved Hasan 				  unsigned char *rq_data)
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 *)
156277331115SJaved Hasan 				     rsp_buf, num_rq, rq_data);
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);
16150e0fcef9SSaurav Kashyap 	if (io_req->wait_for_abts_comp) {
1616853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "tm_compl - wake up the waiter\n");
16170e0fcef9SSaurav Kashyap 		complete(&io_req->abts_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 
16613c97b569SSaurav Kashyap 	WARN_ON(scsi_sg_count(sc) > BNX2FC_MAX_BDS_PER_CMD);
16625565461eSChad Dupuis 	/*
16635565461eSChad Dupuis 	 * Use dma_map_sg directly to ensure we're using the correct
16645565461eSChad Dupuis 	 * dev struct off of pcidev.
16655565461eSChad Dupuis 	 */
16663ce41ea1SBhanu Prakash Gollapudi 	sg_count = dma_map_sg(&hba->pcidev->dev, scsi_sglist(sc),
16673ce41ea1SBhanu Prakash Gollapudi 			      scsi_sg_count(sc), sc->sc_data_direction);
1668853e2bd2SBhanu Gollapudi 	scsi_for_each_sg(sc, sg, sg_count, i) {
1669853e2bd2SBhanu Gollapudi 		sg_len = sg_dma_len(sg);
1670853e2bd2SBhanu Gollapudi 		addr = sg_dma_address(sg);
1671853e2bd2SBhanu Gollapudi 		if (sg_len > BNX2FC_MAX_BD_LEN) {
1672853e2bd2SBhanu Gollapudi 			sg_frags = bnx2fc_split_bd(io_req, addr, sg_len,
1673853e2bd2SBhanu Gollapudi 						   bd_count);
1674853e2bd2SBhanu Gollapudi 		} else {
1675853e2bd2SBhanu Gollapudi 
1676853e2bd2SBhanu Gollapudi 			sg_frags = 1;
1677853e2bd2SBhanu Gollapudi 			bd[bd_count].buf_addr_lo = addr & 0xffffffff;
1678853e2bd2SBhanu Gollapudi 			bd[bd_count].buf_addr_hi  = addr >> 32;
1679853e2bd2SBhanu Gollapudi 			bd[bd_count].buf_len = (u16)sg_len;
1680853e2bd2SBhanu Gollapudi 			bd[bd_count].flags = 0;
1681853e2bd2SBhanu Gollapudi 		}
1682853e2bd2SBhanu Gollapudi 		bd_count += sg_frags;
1683853e2bd2SBhanu Gollapudi 		byte_count += sg_len;
1684853e2bd2SBhanu Gollapudi 	}
1685853e2bd2SBhanu Gollapudi 	if (byte_count != scsi_bufflen(sc))
1686853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "byte_count = %d != scsi_bufflen = %d, "
1687853e2bd2SBhanu Gollapudi 			"task_id = 0x%x\n", byte_count, scsi_bufflen(sc),
1688853e2bd2SBhanu Gollapudi 			io_req->xid);
1689853e2bd2SBhanu Gollapudi 	return bd_count;
1690853e2bd2SBhanu Gollapudi }
1691853e2bd2SBhanu Gollapudi 
1692822f2903SBhanu Prakash Gollapudi static int bnx2fc_build_bd_list_from_sg(struct bnx2fc_cmd *io_req)
1693853e2bd2SBhanu Gollapudi {
1694853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc = io_req->sc_cmd;
1695853e2bd2SBhanu Gollapudi 	struct fcoe_bd_ctx *bd = io_req->bd_tbl->bd_tbl;
1696853e2bd2SBhanu Gollapudi 	int bd_count;
1697853e2bd2SBhanu Gollapudi 
1698822f2903SBhanu Prakash Gollapudi 	if (scsi_sg_count(sc)) {
1699853e2bd2SBhanu Gollapudi 		bd_count = bnx2fc_map_sg(io_req);
1700822f2903SBhanu Prakash Gollapudi 		if (bd_count == 0)
1701822f2903SBhanu Prakash Gollapudi 			return -ENOMEM;
1702822f2903SBhanu Prakash Gollapudi 	} else {
1703853e2bd2SBhanu Gollapudi 		bd_count = 0;
1704853e2bd2SBhanu Gollapudi 		bd[0].buf_addr_lo = bd[0].buf_addr_hi = 0;
1705853e2bd2SBhanu Gollapudi 		bd[0].buf_len = bd[0].flags = 0;
1706853e2bd2SBhanu Gollapudi 	}
1707853e2bd2SBhanu Gollapudi 	io_req->bd_tbl->bd_valid = bd_count;
1708822f2903SBhanu Prakash Gollapudi 
17093c97b569SSaurav Kashyap 	/*
17103c97b569SSaurav Kashyap 	 * Return the command to ML if BD count exceeds the max number
17113c97b569SSaurav Kashyap 	 * that can be handled by FW.
17123c97b569SSaurav Kashyap 	 */
17133c97b569SSaurav Kashyap 	if (bd_count > BNX2FC_FW_MAX_BDS_PER_CMD) {
17143c97b569SSaurav Kashyap 		pr_err("bd_count = %d exceeded FW supported max BD(255), task_id = 0x%x\n",
17153c97b569SSaurav Kashyap 		       bd_count, io_req->xid);
17163c97b569SSaurav Kashyap 		return -ENOMEM;
17173c97b569SSaurav Kashyap 	}
17183c97b569SSaurav Kashyap 
1719822f2903SBhanu Prakash Gollapudi 	return 0;
1720853e2bd2SBhanu Gollapudi }
1721853e2bd2SBhanu Gollapudi 
1722853e2bd2SBhanu Gollapudi static void bnx2fc_unmap_sg_list(struct bnx2fc_cmd *io_req)
1723853e2bd2SBhanu Gollapudi {
1724853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc = io_req->sc_cmd;
17255565461eSChad Dupuis 	struct bnx2fc_interface *interface = io_req->port->priv;
17265565461eSChad Dupuis 	struct bnx2fc_hba *hba = interface->hba;
1727853e2bd2SBhanu Gollapudi 
17285565461eSChad Dupuis 	/*
17295565461eSChad Dupuis 	 * Use dma_unmap_sg directly to ensure we're using the correct
17305565461eSChad Dupuis 	 * dev struct off of pcidev.
17315565461eSChad Dupuis 	 */
17325565461eSChad Dupuis 	if (io_req->bd_tbl->bd_valid && sc && scsi_sg_count(sc)) {
17335565461eSChad Dupuis 		dma_unmap_sg(&hba->pcidev->dev, scsi_sglist(sc),
17345565461eSChad Dupuis 		    scsi_sg_count(sc), sc->sc_data_direction);
1735853e2bd2SBhanu Gollapudi 		io_req->bd_tbl->bd_valid = 0;
1736853e2bd2SBhanu Gollapudi 	}
1737853e2bd2SBhanu Gollapudi }
1738853e2bd2SBhanu Gollapudi 
1739853e2bd2SBhanu Gollapudi void bnx2fc_build_fcp_cmnd(struct bnx2fc_cmd *io_req,
1740853e2bd2SBhanu Gollapudi 				  struct fcp_cmnd *fcp_cmnd)
1741853e2bd2SBhanu Gollapudi {
1742853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1743853e2bd2SBhanu Gollapudi 
1744853e2bd2SBhanu Gollapudi 	memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
1745853e2bd2SBhanu Gollapudi 
1746e35fa8c2SAndy Grover 	int_to_scsilun(sc_cmd->device->lun, &fcp_cmnd->fc_lun);
1747853e2bd2SBhanu Gollapudi 
1748853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_dl = htonl(io_req->data_xfer_len);
1749853e2bd2SBhanu Gollapudi 	memcpy(fcp_cmnd->fc_cdb, sc_cmd->cmnd, sc_cmd->cmd_len);
1750853e2bd2SBhanu Gollapudi 
1751853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_cmdref = 0;
1752853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_pri_ta = 0;
1753853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_tm_flags = io_req->mp_req.tm_flags;
1754853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_flags = io_req->io_req_flags;
1755853e2bd2SBhanu Gollapudi 	fcp_cmnd->fc_pri_ta = FCP_PTA_SIMPLE;
1756853e2bd2SBhanu Gollapudi }
1757853e2bd2SBhanu Gollapudi 
1758853e2bd2SBhanu Gollapudi static void bnx2fc_parse_fcp_rsp(struct bnx2fc_cmd *io_req,
1759853e2bd2SBhanu Gollapudi 				 struct fcoe_fcp_rsp_payload *fcp_rsp,
176077331115SJaved Hasan 				 u8 num_rq, unsigned char *rq_data)
1761853e2bd2SBhanu Gollapudi {
1762853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
1763853e2bd2SBhanu Gollapudi 	u8 rsp_flags = fcp_rsp->fcp_flags.flags;
1764853e2bd2SBhanu Gollapudi 	u32 rq_buff_len = 0;
1765853e2bd2SBhanu Gollapudi 	int fcp_sns_len = 0;
1766853e2bd2SBhanu Gollapudi 	int fcp_rsp_len = 0;
1767853e2bd2SBhanu Gollapudi 
1768853e2bd2SBhanu Gollapudi 	io_req->fcp_status = FC_GOOD;
1769adcf7dfbSChad Dupuis 	io_req->fcp_resid = 0;
1770adcf7dfbSChad Dupuis 	if (rsp_flags & (FCOE_FCP_RSP_FLAGS_FCP_RESID_OVER |
1771adcf7dfbSChad Dupuis 	    FCOE_FCP_RSP_FLAGS_FCP_RESID_UNDER))
1772853e2bd2SBhanu Gollapudi 		io_req->fcp_resid = fcp_rsp->fcp_resid;
1773853e2bd2SBhanu Gollapudi 
1774853e2bd2SBhanu Gollapudi 	io_req->scsi_comp_flags = rsp_flags;
1775853e2bd2SBhanu Gollapudi 	CMD_SCSI_STATUS(sc_cmd) = io_req->cdb_status =
1776853e2bd2SBhanu Gollapudi 				fcp_rsp->scsi_status_code;
1777853e2bd2SBhanu Gollapudi 
1778853e2bd2SBhanu Gollapudi 	/* Fetch fcp_rsp_info and fcp_sns_info if available */
1779853e2bd2SBhanu Gollapudi 	if (num_rq) {
1780853e2bd2SBhanu Gollapudi 
1781853e2bd2SBhanu Gollapudi 		/*
1782853e2bd2SBhanu Gollapudi 		 * We do not anticipate num_rq >1, as the linux defined
1783853e2bd2SBhanu Gollapudi 		 * SCSI_SENSE_BUFFERSIZE is 96 bytes + 8 bytes of FCP_RSP_INFO
1784853e2bd2SBhanu Gollapudi 		 * 256 bytes of single rq buffer is good enough to hold this.
1785853e2bd2SBhanu Gollapudi 		 */
1786853e2bd2SBhanu Gollapudi 
1787853e2bd2SBhanu Gollapudi 		if (rsp_flags &
1788853e2bd2SBhanu Gollapudi 		    FCOE_FCP_RSP_FLAGS_FCP_RSP_LEN_VALID) {
1789853e2bd2SBhanu Gollapudi 			fcp_rsp_len = rq_buff_len
1790853e2bd2SBhanu Gollapudi 					= fcp_rsp->fcp_rsp_len;
1791853e2bd2SBhanu Gollapudi 		}
1792853e2bd2SBhanu Gollapudi 
1793853e2bd2SBhanu Gollapudi 		if (rsp_flags &
1794853e2bd2SBhanu Gollapudi 		    FCOE_FCP_RSP_FLAGS_FCP_SNS_LEN_VALID) {
1795853e2bd2SBhanu Gollapudi 			fcp_sns_len = fcp_rsp->fcp_sns_len;
1796853e2bd2SBhanu Gollapudi 			rq_buff_len += fcp_rsp->fcp_sns_len;
1797853e2bd2SBhanu Gollapudi 		}
1798853e2bd2SBhanu Gollapudi 
1799853e2bd2SBhanu Gollapudi 		io_req->fcp_rsp_len = fcp_rsp_len;
1800853e2bd2SBhanu Gollapudi 		io_req->fcp_sns_len = fcp_sns_len;
1801853e2bd2SBhanu Gollapudi 
1802853e2bd2SBhanu Gollapudi 		if (rq_buff_len > num_rq * BNX2FC_RQ_BUF_SZ) {
1803853e2bd2SBhanu Gollapudi 			/* Invalid sense sense length. */
1804b2a554ffSBhanu Prakash Gollapudi 			printk(KERN_ERR PFX "invalid sns length %d\n",
1805853e2bd2SBhanu Gollapudi 				rq_buff_len);
1806853e2bd2SBhanu Gollapudi 			/* reset rq_buff_len */
1807853e2bd2SBhanu Gollapudi 			rq_buff_len =  num_rq * BNX2FC_RQ_BUF_SZ;
1808853e2bd2SBhanu Gollapudi 		}
1809853e2bd2SBhanu Gollapudi 
1810853e2bd2SBhanu Gollapudi 		/* fetch fcp_rsp_code */
1811853e2bd2SBhanu Gollapudi 		if ((fcp_rsp_len == 4) || (fcp_rsp_len == 8)) {
1812853e2bd2SBhanu Gollapudi 			/* Only for task management function */
1813853e2bd2SBhanu Gollapudi 			io_req->fcp_rsp_code = rq_data[3];
18148a30a3c7SMaurizio Lombardi 			BNX2FC_IO_DBG(io_req, "fcp_rsp_code = %d\n",
1815853e2bd2SBhanu Gollapudi 				io_req->fcp_rsp_code);
1816853e2bd2SBhanu Gollapudi 		}
1817853e2bd2SBhanu Gollapudi 
1818853e2bd2SBhanu Gollapudi 		/* fetch sense data */
1819853e2bd2SBhanu Gollapudi 		rq_data += fcp_rsp_len;
1820853e2bd2SBhanu Gollapudi 
1821853e2bd2SBhanu Gollapudi 		if (fcp_sns_len > SCSI_SENSE_BUFFERSIZE) {
1822853e2bd2SBhanu Gollapudi 			printk(KERN_ERR PFX "Truncating sense buffer\n");
1823853e2bd2SBhanu Gollapudi 			fcp_sns_len = SCSI_SENSE_BUFFERSIZE;
1824853e2bd2SBhanu Gollapudi 		}
1825853e2bd2SBhanu Gollapudi 
182616da05b1SAndi Kleen 		memset(sc_cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
1827853e2bd2SBhanu Gollapudi 		if (fcp_sns_len)
1828853e2bd2SBhanu Gollapudi 			memcpy(sc_cmd->sense_buffer, rq_data, fcp_sns_len);
1829853e2bd2SBhanu Gollapudi 
1830853e2bd2SBhanu Gollapudi 	}
1831853e2bd2SBhanu Gollapudi }
1832853e2bd2SBhanu Gollapudi 
1833853e2bd2SBhanu Gollapudi /**
1834853e2bd2SBhanu Gollapudi  * bnx2fc_queuecommand - Queuecommand function of the scsi template
1835853e2bd2SBhanu Gollapudi  *
1836853e2bd2SBhanu Gollapudi  * @host:	The Scsi_Host the command was issued to
1837853e2bd2SBhanu Gollapudi  * @sc_cmd:	struct scsi_cmnd to be executed
1838853e2bd2SBhanu Gollapudi  *
1839853e2bd2SBhanu Gollapudi  * This is the IO strategy routine, called by SCSI-ML
1840853e2bd2SBhanu Gollapudi  **/
1841853e2bd2SBhanu Gollapudi int bnx2fc_queuecommand(struct Scsi_Host *host,
1842853e2bd2SBhanu Gollapudi 			struct scsi_cmnd *sc_cmd)
1843853e2bd2SBhanu Gollapudi {
1844853e2bd2SBhanu Gollapudi 	struct fc_lport *lport = shost_priv(host);
1845853e2bd2SBhanu Gollapudi 	struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
1846853e2bd2SBhanu Gollapudi 	struct fc_rport_libfc_priv *rp = rport->dd_data;
1847853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt;
1848853e2bd2SBhanu Gollapudi 	struct bnx2fc_cmd *io_req;
1849853e2bd2SBhanu Gollapudi 	int rc = 0;
1850853e2bd2SBhanu Gollapudi 	int rval;
1851853e2bd2SBhanu Gollapudi 
1852853e2bd2SBhanu Gollapudi 	rval = fc_remote_port_chkready(rport);
1853853e2bd2SBhanu Gollapudi 	if (rval) {
1854853e2bd2SBhanu Gollapudi 		sc_cmd->result = rval;
1855853e2bd2SBhanu Gollapudi 		sc_cmd->scsi_done(sc_cmd);
1856853e2bd2SBhanu Gollapudi 		return 0;
1857853e2bd2SBhanu Gollapudi 	}
1858853e2bd2SBhanu Gollapudi 
1859853e2bd2SBhanu Gollapudi 	if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
1860853e2bd2SBhanu Gollapudi 		rc = SCSI_MLQUEUE_HOST_BUSY;
1861853e2bd2SBhanu Gollapudi 		goto exit_qcmd;
1862853e2bd2SBhanu Gollapudi 	}
1863853e2bd2SBhanu Gollapudi 
1864853e2bd2SBhanu Gollapudi 	/* rport and tgt are allocated together, so tgt should be non-NULL */
1865853e2bd2SBhanu Gollapudi 	tgt = (struct bnx2fc_rport *)&rp[1];
1866853e2bd2SBhanu Gollapudi 
1867853e2bd2SBhanu Gollapudi 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
1868853e2bd2SBhanu Gollapudi 		/*
1869853e2bd2SBhanu Gollapudi 		 * Session is not offloaded yet. Let SCSI-ml retry
1870853e2bd2SBhanu Gollapudi 		 * the command.
1871853e2bd2SBhanu Gollapudi 		 */
1872853e2bd2SBhanu Gollapudi 		rc = SCSI_MLQUEUE_TARGET_BUSY;
1873853e2bd2SBhanu Gollapudi 		goto exit_qcmd;
1874853e2bd2SBhanu Gollapudi 	}
1875245a5754SEddie Wai 	if (tgt->retry_delay_timestamp) {
1876245a5754SEddie Wai 		if (time_after(jiffies, tgt->retry_delay_timestamp)) {
1877245a5754SEddie Wai 			tgt->retry_delay_timestamp = 0;
1878245a5754SEddie Wai 		} else {
1879245a5754SEddie Wai 			/* If retry_delay timer is active, flow off the ML */
1880245a5754SEddie Wai 			rc = SCSI_MLQUEUE_TARGET_BUSY;
1881245a5754SEddie Wai 			goto exit_qcmd;
1882245a5754SEddie Wai 		}
1883245a5754SEddie Wai 	}
1884dc6311ddSMaurizio Lombardi 
1885dc6311ddSMaurizio Lombardi 	spin_lock_bh(&tgt->tgt_lock);
1886dc6311ddSMaurizio Lombardi 
1887853e2bd2SBhanu Gollapudi 	io_req = bnx2fc_cmd_alloc(tgt);
1888853e2bd2SBhanu Gollapudi 	if (!io_req) {
1889853e2bd2SBhanu Gollapudi 		rc = SCSI_MLQUEUE_HOST_BUSY;
1890dc6311ddSMaurizio Lombardi 		goto exit_qcmd_tgtlock;
1891853e2bd2SBhanu Gollapudi 	}
1892853e2bd2SBhanu Gollapudi 	io_req->sc_cmd = sc_cmd;
1893853e2bd2SBhanu Gollapudi 
1894853e2bd2SBhanu Gollapudi 	if (bnx2fc_post_io_req(tgt, io_req)) {
1895853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Unable to post io_req\n");
1896853e2bd2SBhanu Gollapudi 		rc = SCSI_MLQUEUE_HOST_BUSY;
1897dc6311ddSMaurizio Lombardi 		goto exit_qcmd_tgtlock;
1898853e2bd2SBhanu Gollapudi 	}
1899dc6311ddSMaurizio Lombardi 
1900dc6311ddSMaurizio Lombardi exit_qcmd_tgtlock:
1901dc6311ddSMaurizio Lombardi 	spin_unlock_bh(&tgt->tgt_lock);
1902853e2bd2SBhanu Gollapudi exit_qcmd:
1903853e2bd2SBhanu Gollapudi 	return rc;
1904853e2bd2SBhanu Gollapudi }
1905853e2bd2SBhanu Gollapudi 
1906853e2bd2SBhanu Gollapudi void bnx2fc_process_scsi_cmd_compl(struct bnx2fc_cmd *io_req,
1907853e2bd2SBhanu Gollapudi 				   struct fcoe_task_ctx_entry *task,
190877331115SJaved Hasan 				   u8 num_rq, unsigned char *rq_data)
1909853e2bd2SBhanu Gollapudi {
1910853e2bd2SBhanu Gollapudi 	struct fcoe_fcp_rsp_payload *fcp_rsp;
1911853e2bd2SBhanu Gollapudi 	struct bnx2fc_rport *tgt = io_req->tgt;
1912853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd;
1913c9c53749SLaurence Oberman 	u16 scope = 0, qualifier = 0;
1914853e2bd2SBhanu Gollapudi 
1915853e2bd2SBhanu Gollapudi 	/* scsi_cmd_cmpl is called with tgt lock held */
1916853e2bd2SBhanu Gollapudi 
1917853e2bd2SBhanu Gollapudi 	if (test_and_set_bit(BNX2FC_FLAG_IO_COMPL, &io_req->req_flags)) {
1918853e2bd2SBhanu Gollapudi 		/* we will not receive ABTS response for this IO */
1919853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "Timer context finished processing "
1920853e2bd2SBhanu Gollapudi 			   "this scsi cmd\n");
192130e73671SSaurav Kashyap 		if (test_and_clear_bit(BNX2FC_FLAG_IO_CLEANUP,
192230e73671SSaurav Kashyap 				       &io_req->req_flags)) {
192330e73671SSaurav Kashyap 			BNX2FC_IO_DBG(io_req,
192430e73671SSaurav Kashyap 				      "Actual completion after cleanup request cleaning up\n");
192530e73671SSaurav Kashyap 			bnx2fc_process_cleanup_compl(io_req, task, num_rq);
192630e73671SSaurav Kashyap 		}
1927ecf7ff49SChad Dupuis 		return;
1928853e2bd2SBhanu Gollapudi 	}
1929853e2bd2SBhanu Gollapudi 
1930853e2bd2SBhanu Gollapudi 	/* Cancel the timeout_work, as we received IO completion */
1931853e2bd2SBhanu Gollapudi 	if (cancel_delayed_work(&io_req->timeout_work))
1932853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount,
1933853e2bd2SBhanu Gollapudi 			 bnx2fc_cmd_release); /* drop timer hold */
1934853e2bd2SBhanu Gollapudi 
1935853e2bd2SBhanu Gollapudi 	sc_cmd = io_req->sc_cmd;
1936853e2bd2SBhanu Gollapudi 	if (sc_cmd == NULL) {
1937853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "scsi_cmd_compl - sc_cmd is NULL\n");
1938853e2bd2SBhanu Gollapudi 		return;
1939853e2bd2SBhanu Gollapudi 	}
1940853e2bd2SBhanu Gollapudi 
1941853e2bd2SBhanu Gollapudi 	/* Fetch fcp_rsp from task context and perform cmd completion */
1942853e2bd2SBhanu Gollapudi 	fcp_rsp = (struct fcoe_fcp_rsp_payload *)
1943619c5cb6SVlad Zolotarov 		   &(task->rxwr_only.union_ctx.comp_info.fcp_rsp.payload);
1944853e2bd2SBhanu Gollapudi 
1945853e2bd2SBhanu Gollapudi 	/* parse fcp_rsp and obtain sense data from RQ if available */
194677331115SJaved Hasan 	bnx2fc_parse_fcp_rsp(io_req, fcp_rsp, num_rq, rq_data);
1947853e2bd2SBhanu Gollapudi 
1948853e2bd2SBhanu Gollapudi 	if (!sc_cmd->SCp.ptr) {
1949853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "SCp.ptr is NULL\n");
1950853e2bd2SBhanu Gollapudi 		return;
1951853e2bd2SBhanu Gollapudi 	}
1952853e2bd2SBhanu Gollapudi 
1953853e2bd2SBhanu Gollapudi 	if (io_req->on_active_queue) {
1954853e2bd2SBhanu Gollapudi 		list_del_init(&io_req->link);
1955853e2bd2SBhanu Gollapudi 		io_req->on_active_queue = 0;
1956853e2bd2SBhanu Gollapudi 		/* Move IO req to retire queue */
1957853e2bd2SBhanu Gollapudi 		list_add_tail(&io_req->link, &tgt->io_retire_queue);
1958853e2bd2SBhanu Gollapudi 	} else {
1959853e2bd2SBhanu Gollapudi 		/* This should not happen, but could have been pulled
1960853e2bd2SBhanu Gollapudi 		 * by bnx2fc_flush_active_ios(), or during a race
1961853e2bd2SBhanu Gollapudi 		 * between command abort and (late) completion.
1962853e2bd2SBhanu Gollapudi 		 */
1963853e2bd2SBhanu Gollapudi 		BNX2FC_IO_DBG(io_req, "xid not on active_cmd_queue\n");
19640e0fcef9SSaurav Kashyap 		if (io_req->wait_for_abts_comp)
1965853e2bd2SBhanu Gollapudi 			if (test_and_clear_bit(BNX2FC_FLAG_EH_ABORT,
1966853e2bd2SBhanu Gollapudi 					       &io_req->req_flags))
19670e0fcef9SSaurav Kashyap 				complete(&io_req->abts_done);
1968853e2bd2SBhanu Gollapudi 	}
1969853e2bd2SBhanu Gollapudi 
1970853e2bd2SBhanu Gollapudi 	bnx2fc_unmap_sg_list(io_req);
1971b5a95fe7SBhanu Prakash Gollapudi 	io_req->sc_cmd = NULL;
1972853e2bd2SBhanu Gollapudi 
1973853e2bd2SBhanu Gollapudi 	switch (io_req->fcp_status) {
1974853e2bd2SBhanu Gollapudi 	case FC_GOOD:
1975853e2bd2SBhanu Gollapudi 		if (io_req->cdb_status == 0) {
1976853e2bd2SBhanu Gollapudi 			/* Good IO completion */
1977853e2bd2SBhanu Gollapudi 			sc_cmd->result = DID_OK << 16;
1978853e2bd2SBhanu Gollapudi 		} else {
1979853e2bd2SBhanu Gollapudi 			/* Transport status is good, SCSI status not good */
1980853e2bd2SBhanu Gollapudi 			BNX2FC_IO_DBG(io_req, "scsi_cmpl: cdb_status = %d"
1981853e2bd2SBhanu Gollapudi 				 " fcp_resid = 0x%x\n",
1982853e2bd2SBhanu Gollapudi 				io_req->cdb_status, io_req->fcp_resid);
1983853e2bd2SBhanu Gollapudi 			sc_cmd->result = (DID_OK << 16) | io_req->cdb_status;
1984245a5754SEddie Wai 
1985245a5754SEddie Wai 			if (io_req->cdb_status == SAM_STAT_TASK_SET_FULL ||
1986245a5754SEddie Wai 			    io_req->cdb_status == SAM_STAT_BUSY) {
1987c9c53749SLaurence Oberman 				/* Newer array firmware with BUSY or
1988c9c53749SLaurence Oberman 				 * TASK_SET_FULL may return a status that needs
1989c9c53749SLaurence Oberman 				 * the scope bits masked.
1990c9c53749SLaurence Oberman 				 * Or a huge delay timestamp up to 27 minutes
1991c9c53749SLaurence Oberman 				 * can result.
1992c9c53749SLaurence Oberman 				 */
1993c9c53749SLaurence Oberman 				if (fcp_rsp->retry_delay_timer) {
1994c9c53749SLaurence Oberman 					/* Upper 2 bits */
1995c9c53749SLaurence Oberman 					scope = fcp_rsp->retry_delay_timer
1996c9c53749SLaurence Oberman 						& 0xC000;
1997c9c53749SLaurence Oberman 					/* Lower 14 bits */
1998c9c53749SLaurence Oberman 					qualifier = fcp_rsp->retry_delay_timer
1999c9c53749SLaurence Oberman 						& 0x3FFF;
2000245a5754SEddie Wai 				}
2001c9c53749SLaurence Oberman 				if (scope > 0 && qualifier > 0 &&
2002c9c53749SLaurence Oberman 					qualifier <= 0x3FEF) {
2003c9c53749SLaurence Oberman 					/* Set the jiffies +
2004c9c53749SLaurence Oberman 					 * retry_delay_timer * 100ms
2005c9c53749SLaurence Oberman 					 * for the rport/tgt
2006c9c53749SLaurence Oberman 					 */
2007c9c53749SLaurence Oberman 					tgt->retry_delay_timestamp = jiffies +
2008c9c53749SLaurence Oberman 						(qualifier * HZ / 10);
2009c9c53749SLaurence Oberman 				}
2010c9c53749SLaurence Oberman 			}
2011853e2bd2SBhanu Gollapudi 		}
2012853e2bd2SBhanu Gollapudi 		if (io_req->fcp_resid)
2013853e2bd2SBhanu Gollapudi 			scsi_set_resid(sc_cmd, io_req->fcp_resid);
2014853e2bd2SBhanu Gollapudi 		break;
2015853e2bd2SBhanu Gollapudi 	default:
2016b2a554ffSBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "scsi_cmd_compl: fcp_status = %d\n",
2017853e2bd2SBhanu Gollapudi 			io_req->fcp_status);
2018853e2bd2SBhanu Gollapudi 		break;
2019853e2bd2SBhanu Gollapudi 	}
2020853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = NULL;
2021853e2bd2SBhanu Gollapudi 	sc_cmd->scsi_done(sc_cmd);
2022853e2bd2SBhanu Gollapudi 	kref_put(&io_req->refcount, bnx2fc_cmd_release);
2023853e2bd2SBhanu Gollapudi }
2024853e2bd2SBhanu Gollapudi 
202574446954SBhanu Prakash Gollapudi int bnx2fc_post_io_req(struct bnx2fc_rport *tgt,
2026853e2bd2SBhanu Gollapudi 			       struct bnx2fc_cmd *io_req)
2027853e2bd2SBhanu Gollapudi {
2028853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task;
2029853e2bd2SBhanu Gollapudi 	struct fcoe_task_ctx_entry *task_page;
2030853e2bd2SBhanu Gollapudi 	struct scsi_cmnd *sc_cmd = io_req->sc_cmd;
2031853e2bd2SBhanu Gollapudi 	struct fcoe_port *port = tgt->port;
2032aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_interface *interface = port->priv;
2033aea71a02SBhanu Prakash Gollapudi 	struct bnx2fc_hba *hba = interface->hba;
2034853e2bd2SBhanu Gollapudi 	struct fc_lport *lport = port->lport;
20351bd49b48SVasu Dev 	struct fc_stats *stats;
2036853e2bd2SBhanu Gollapudi 	int task_idx, index;
2037853e2bd2SBhanu Gollapudi 	u16 xid;
2038853e2bd2SBhanu Gollapudi 
2039dc6311ddSMaurizio Lombardi 	/* bnx2fc_post_io_req() is called with the tgt_lock held */
2040dc6311ddSMaurizio Lombardi 
2041853e2bd2SBhanu Gollapudi 	/* Initialize rest of io_req fields */
2042853e2bd2SBhanu Gollapudi 	io_req->cmd_type = BNX2FC_SCSI_CMD;
2043853e2bd2SBhanu Gollapudi 	io_req->port = port;
2044853e2bd2SBhanu Gollapudi 	io_req->tgt = tgt;
2045853e2bd2SBhanu Gollapudi 	io_req->data_xfer_len = scsi_bufflen(sc_cmd);
2046853e2bd2SBhanu Gollapudi 	sc_cmd->SCp.ptr = (char *)io_req;
2047853e2bd2SBhanu Gollapudi 
20481bd49b48SVasu Dev 	stats = per_cpu_ptr(lport->stats, get_cpu());
2049853e2bd2SBhanu Gollapudi 	if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
2050853e2bd2SBhanu Gollapudi 		io_req->io_req_flags = BNX2FC_READ;
2051853e2bd2SBhanu Gollapudi 		stats->InputRequests++;
2052853e2bd2SBhanu Gollapudi 		stats->InputBytes += io_req->data_xfer_len;
2053853e2bd2SBhanu Gollapudi 	} else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
2054853e2bd2SBhanu Gollapudi 		io_req->io_req_flags = BNX2FC_WRITE;
2055853e2bd2SBhanu Gollapudi 		stats->OutputRequests++;
2056853e2bd2SBhanu Gollapudi 		stats->OutputBytes += io_req->data_xfer_len;
2057853e2bd2SBhanu Gollapudi 	} else {
2058853e2bd2SBhanu Gollapudi 		io_req->io_req_flags = 0;
2059853e2bd2SBhanu Gollapudi 		stats->ControlRequests++;
2060853e2bd2SBhanu Gollapudi 	}
2061853e2bd2SBhanu Gollapudi 	put_cpu();
2062853e2bd2SBhanu Gollapudi 
2063853e2bd2SBhanu Gollapudi 	xid = io_req->xid;
2064853e2bd2SBhanu Gollapudi 
2065853e2bd2SBhanu Gollapudi 	/* Build buffer descriptor list for firmware from sg list */
2066822f2903SBhanu Prakash Gollapudi 	if (bnx2fc_build_bd_list_from_sg(io_req)) {
2067822f2903SBhanu Prakash Gollapudi 		printk(KERN_ERR PFX "BD list creation failed\n");
2068822f2903SBhanu Prakash Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2069822f2903SBhanu Prakash Gollapudi 		return -EAGAIN;
2070822f2903SBhanu Prakash Gollapudi 	}
2071853e2bd2SBhanu Gollapudi 
2072853e2bd2SBhanu Gollapudi 	task_idx = xid / BNX2FC_TASKS_PER_PAGE;
2073853e2bd2SBhanu Gollapudi 	index = xid % BNX2FC_TASKS_PER_PAGE;
2074853e2bd2SBhanu Gollapudi 
2075853e2bd2SBhanu Gollapudi 	/* Initialize task context for this IO request */
2076853e2bd2SBhanu Gollapudi 	task_page = (struct fcoe_task_ctx_entry *) hba->task_ctx[task_idx];
2077853e2bd2SBhanu Gollapudi 	task = &(task_page[index]);
2078853e2bd2SBhanu Gollapudi 	bnx2fc_init_task(io_req, task);
2079853e2bd2SBhanu Gollapudi 
2080853e2bd2SBhanu Gollapudi 	if (tgt->flush_in_prog) {
2081853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Flush in progress..Host Busy\n");
2082853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2083853e2bd2SBhanu Gollapudi 		return -EAGAIN;
2084853e2bd2SBhanu Gollapudi 	}
2085853e2bd2SBhanu Gollapudi 
2086853e2bd2SBhanu Gollapudi 	if (!test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags)) {
2087853e2bd2SBhanu Gollapudi 		printk(KERN_ERR PFX "Session not ready...post_io\n");
2088853e2bd2SBhanu Gollapudi 		kref_put(&io_req->refcount, bnx2fc_cmd_release);
2089853e2bd2SBhanu Gollapudi 		return -EAGAIN;
2090853e2bd2SBhanu Gollapudi 	}
2091853e2bd2SBhanu Gollapudi 
2092853e2bd2SBhanu Gollapudi 	/* Time IO req */
2093b252f4c7SBhanu Prakash Gollapudi 	if (tgt->io_timeout)
2094853e2bd2SBhanu Gollapudi 		bnx2fc_cmd_timer_set(io_req, BNX2FC_IO_TIMEOUT);
2095853e2bd2SBhanu Gollapudi 	/* Obtain free SQ entry */
2096853e2bd2SBhanu Gollapudi 	bnx2fc_add_2_sq(tgt, xid);
2097853e2bd2SBhanu Gollapudi 
2098853e2bd2SBhanu Gollapudi 	/* Enqueue the io_req to active_cmd_queue */
2099853e2bd2SBhanu Gollapudi 
2100853e2bd2SBhanu Gollapudi 	io_req->on_active_queue = 1;
2101853e2bd2SBhanu Gollapudi 	/* move io_req from pending_queue to active_queue */
2102853e2bd2SBhanu Gollapudi 	list_add_tail(&io_req->link, &tgt->active_cmd_queue);
2103853e2bd2SBhanu Gollapudi 
2104853e2bd2SBhanu Gollapudi 	/* Ring doorbell */
2105853e2bd2SBhanu Gollapudi 	bnx2fc_ring_doorbell(tgt);
2106853e2bd2SBhanu Gollapudi 	return 0;
2107853e2bd2SBhanu Gollapudi }
2108