xref: /openbmc/linux/drivers/scsi/fnic/fnic_scsi.c (revision ff6defa6)
1 /*
2  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  */
18 #include <linux/mempool.h>
19 #include <linux/errno.h>
20 #include <linux/init.h>
21 #include <linux/workqueue.h>
22 #include <linux/pci.h>
23 #include <linux/scatterlist.h>
24 #include <linux/skbuff.h>
25 #include <linux/spinlock.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/delay.h>
29 #include <linux/gfp.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_cmnd.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/fc/fc_els.h>
36 #include <scsi/fc/fc_fcoe.h>
37 #include <scsi/libfc.h>
38 #include <scsi/fc_frame.h>
39 #include "fnic_io.h"
40 #include "fnic.h"
41 
42 const char *fnic_state_str[] = {
43 	[FNIC_IN_FC_MODE] =           "FNIC_IN_FC_MODE",
44 	[FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
45 	[FNIC_IN_ETH_MODE] =          "FNIC_IN_ETH_MODE",
46 	[FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
47 };
48 
49 static const char *fnic_ioreq_state_str[] = {
50 	[FNIC_IOREQ_NOT_INITED] = "FNIC_IOREQ_NOT_INITED",
51 	[FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
52 	[FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
53 	[FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
54 	[FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
55 };
56 
57 static const char *fcpio_status_str[] =  {
58 	[FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
59 	[FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
60 	[FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
61 	[FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
62 	[FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
63 	[FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
64 	[FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
65 	[FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
66 	[FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
67 	[FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
68 	[FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
69 	[FCPIO_FW_ERR] = "FCPIO_FW_ERR",
70 	[FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
71 	[FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
72 	[FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
73 	[FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
74 	[FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
75 	[FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
76 	[FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
77 };
78 
79 const char *fnic_state_to_str(unsigned int state)
80 {
81 	if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
82 		return "unknown";
83 
84 	return fnic_state_str[state];
85 }
86 
87 static const char *fnic_ioreq_state_to_str(unsigned int state)
88 {
89 	if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
90 	    !fnic_ioreq_state_str[state])
91 		return "unknown";
92 
93 	return fnic_ioreq_state_str[state];
94 }
95 
96 static const char *fnic_fcpio_status_to_str(unsigned int status)
97 {
98 	if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
99 		return "unknown";
100 
101 	return fcpio_status_str[status];
102 }
103 
104 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
105 
106 static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
107 					    struct scsi_cmnd *sc)
108 {
109 	u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
110 
111 	return &fnic->io_req_lock[hash];
112 }
113 
114 static inline spinlock_t *fnic_io_lock_tag(struct fnic *fnic,
115 					    int tag)
116 {
117 	return &fnic->io_req_lock[tag & (FNIC_IO_LOCKS - 1)];
118 }
119 
120 /*
121  * Unmap the data buffer and sense buffer for an io_req,
122  * also unmap and free the device-private scatter/gather list.
123  */
124 static void fnic_release_ioreq_buf(struct fnic *fnic,
125 				   struct fnic_io_req *io_req,
126 				   struct scsi_cmnd *sc)
127 {
128 	if (io_req->sgl_list_pa)
129 		pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
130 				 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
131 				 PCI_DMA_TODEVICE);
132 	scsi_dma_unmap(sc);
133 
134 	if (io_req->sgl_cnt)
135 		mempool_free(io_req->sgl_list_alloc,
136 			     fnic->io_sgl_pool[io_req->sgl_type]);
137 	if (io_req->sense_buf_pa)
138 		pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
139 				 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
140 }
141 
142 /* Free up Copy Wq descriptors. Called with copy_wq lock held */
143 static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
144 {
145 	/* if no Ack received from firmware, then nothing to clean */
146 	if (!fnic->fw_ack_recd[0])
147 		return 1;
148 
149 	/*
150 	 * Update desc_available count based on number of freed descriptors
151 	 * Account for wraparound
152 	 */
153 	if (wq->to_clean_index <= fnic->fw_ack_index[0])
154 		wq->ring.desc_avail += (fnic->fw_ack_index[0]
155 					- wq->to_clean_index + 1);
156 	else
157 		wq->ring.desc_avail += (wq->ring.desc_count
158 					- wq->to_clean_index
159 					+ fnic->fw_ack_index[0] + 1);
160 
161 	/*
162 	 * just bump clean index to ack_index+1 accounting for wraparound
163 	 * this will essentially free up all descriptors between
164 	 * to_clean_index and fw_ack_index, both inclusive
165 	 */
166 	wq->to_clean_index =
167 		(fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
168 
169 	/* we have processed the acks received so far */
170 	fnic->fw_ack_recd[0] = 0;
171 	return 0;
172 }
173 
174 
175 /**
176  * __fnic_set_state_flags
177  * Sets/Clears bits in fnic's state_flags
178  **/
179 void
180 __fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
181 			unsigned long clearbits)
182 {
183 	struct Scsi_Host *host = fnic->lport->host;
184 	int sh_locked = spin_is_locked(host->host_lock);
185 	unsigned long flags = 0;
186 
187 	if (!sh_locked)
188 		spin_lock_irqsave(host->host_lock, flags);
189 
190 	if (clearbits)
191 		fnic->state_flags &= ~st_flags;
192 	else
193 		fnic->state_flags |= st_flags;
194 
195 	if (!sh_locked)
196 		spin_unlock_irqrestore(host->host_lock, flags);
197 
198 	return;
199 }
200 
201 
202 /*
203  * fnic_fw_reset_handler
204  * Routine to send reset msg to fw
205  */
206 int fnic_fw_reset_handler(struct fnic *fnic)
207 {
208 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
209 	int ret = 0;
210 	unsigned long flags;
211 
212 	/* indicate fwreset to io path */
213 	fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
214 
215 	skb_queue_purge(&fnic->frame_queue);
216 	skb_queue_purge(&fnic->tx_queue);
217 
218 	/* wait for io cmpl */
219 	while (atomic_read(&fnic->in_flight))
220 		schedule_timeout(msecs_to_jiffies(1));
221 
222 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
223 
224 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
225 		free_wq_copy_descs(fnic, wq);
226 
227 	if (!vnic_wq_copy_desc_avail(wq))
228 		ret = -EAGAIN;
229 	else {
230 		fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
231 		atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
232 		if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
233 			  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
234 			atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
235 				atomic64_read(
236 				  &fnic->fnic_stats.fw_stats.active_fw_reqs));
237 	}
238 
239 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
240 
241 	if (!ret) {
242 		atomic64_inc(&fnic->fnic_stats.reset_stats.fw_resets);
243 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
244 			      "Issued fw reset\n");
245 	} else {
246 		fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
247 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
248 			      "Failed to issue fw reset\n");
249 	}
250 
251 	return ret;
252 }
253 
254 
255 /*
256  * fnic_flogi_reg_handler
257  * Routine to send flogi register msg to fw
258  */
259 int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
260 {
261 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
262 	enum fcpio_flogi_reg_format_type format;
263 	struct fc_lport *lp = fnic->lport;
264 	u8 gw_mac[ETH_ALEN];
265 	int ret = 0;
266 	unsigned long flags;
267 
268 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
269 
270 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
271 		free_wq_copy_descs(fnic, wq);
272 
273 	if (!vnic_wq_copy_desc_avail(wq)) {
274 		ret = -EAGAIN;
275 		goto flogi_reg_ioreq_end;
276 	}
277 
278 	if (fnic->ctlr.map_dest) {
279 		memset(gw_mac, 0xff, ETH_ALEN);
280 		format = FCPIO_FLOGI_REG_DEF_DEST;
281 	} else {
282 		memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
283 		format = FCPIO_FLOGI_REG_GW_DEST;
284 	}
285 
286 	if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
287 		fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
288 						fc_id, gw_mac,
289 						fnic->data_src_addr,
290 						lp->r_a_tov, lp->e_d_tov);
291 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
292 			      "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
293 			      fc_id, fnic->data_src_addr, gw_mac);
294 	} else {
295 		fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
296 						  format, fc_id, gw_mac);
297 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
298 			      "FLOGI reg issued fcid %x map %d dest %pM\n",
299 			      fc_id, fnic->ctlr.map_dest, gw_mac);
300 	}
301 
302 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
303 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
304 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
305 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
306 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
307 
308 flogi_reg_ioreq_end:
309 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
310 	return ret;
311 }
312 
313 /*
314  * fnic_queue_wq_copy_desc
315  * Routine to enqueue a wq copy desc
316  */
317 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
318 					  struct vnic_wq_copy *wq,
319 					  struct fnic_io_req *io_req,
320 					  struct scsi_cmnd *sc,
321 					  int sg_count)
322 {
323 	struct scatterlist *sg;
324 	struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
325 	struct fc_rport_libfc_priv *rp = rport->dd_data;
326 	struct host_sg_desc *desc;
327 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
328 	unsigned int i;
329 	unsigned long intr_flags;
330 	int flags;
331 	u8 exch_flags;
332 	struct scsi_lun fc_lun;
333 
334 	if (sg_count) {
335 		/* For each SGE, create a device desc entry */
336 		desc = io_req->sgl_list;
337 		for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
338 			desc->addr = cpu_to_le64(sg_dma_address(sg));
339 			desc->len = cpu_to_le32(sg_dma_len(sg));
340 			desc->_resvd = 0;
341 			desc++;
342 		}
343 
344 		io_req->sgl_list_pa = pci_map_single
345 			(fnic->pdev,
346 			 io_req->sgl_list,
347 			 sizeof(io_req->sgl_list[0]) * sg_count,
348 			 PCI_DMA_TODEVICE);
349 	}
350 
351 	io_req->sense_buf_pa = pci_map_single(fnic->pdev,
352 					      sc->sense_buffer,
353 					      SCSI_SENSE_BUFFERSIZE,
354 					      PCI_DMA_FROMDEVICE);
355 
356 	int_to_scsilun(sc->device->lun, &fc_lun);
357 
358 	/* Enqueue the descriptor in the Copy WQ */
359 	spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
360 
361 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
362 		free_wq_copy_descs(fnic, wq);
363 
364 	if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
365 		spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
366 		FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
367 			  "fnic_queue_wq_copy_desc failure - no descriptors\n");
368 		atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
369 		return SCSI_MLQUEUE_HOST_BUSY;
370 	}
371 
372 	flags = 0;
373 	if (sc->sc_data_direction == DMA_FROM_DEVICE)
374 		flags = FCPIO_ICMND_RDDATA;
375 	else if (sc->sc_data_direction == DMA_TO_DEVICE)
376 		flags = FCPIO_ICMND_WRDATA;
377 
378 	exch_flags = 0;
379 	if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
380 	    (rp->flags & FC_RP_FLAGS_RETRY))
381 		exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
382 
383 	fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
384 					 0, exch_flags, io_req->sgl_cnt,
385 					 SCSI_SENSE_BUFFERSIZE,
386 					 io_req->sgl_list_pa,
387 					 io_req->sense_buf_pa,
388 					 0, /* scsi cmd ref, always 0 */
389 					 FCPIO_ICMND_PTA_SIMPLE,
390 					 	/* scsi pri and tag */
391 					 flags,	/* command flags */
392 					 sc->cmnd, sc->cmd_len,
393 					 scsi_bufflen(sc),
394 					 fc_lun.scsi_lun, io_req->port_id,
395 					 rport->maxframe_size, rp->r_a_tov,
396 					 rp->e_d_tov);
397 
398 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
399 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
400 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
401 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
402 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
403 
404 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
405 	return 0;
406 }
407 
408 /*
409  * fnic_queuecommand
410  * Routine to send a scsi cdb
411  * Called with host_lock held and interrupts disabled.
412  */
413 static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
414 {
415 	struct fc_lport *lp = shost_priv(sc->device->host);
416 	struct fc_rport *rport;
417 	struct fnic_io_req *io_req = NULL;
418 	struct fnic *fnic = lport_priv(lp);
419 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
420 	struct vnic_wq_copy *wq;
421 	int ret;
422 	u64 cmd_trace;
423 	int sg_count = 0;
424 	unsigned long flags = 0;
425 	unsigned long ptr;
426 	struct fc_rport_priv *rdata;
427 	spinlock_t *io_lock = NULL;
428 
429 	if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
430 		return SCSI_MLQUEUE_HOST_BUSY;
431 
432 	rport = starget_to_rport(scsi_target(sc->device));
433 	ret = fc_remote_port_chkready(rport);
434 	if (ret) {
435 		atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
436 		sc->result = ret;
437 		done(sc);
438 		return 0;
439 	}
440 
441 	rdata = lp->tt.rport_lookup(lp, rport->port_id);
442 	if (!rdata || (rdata->rp_state == RPORT_ST_DELETE)) {
443 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
444 			"returning IO as rport is removed\n");
445 		atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
446 		sc->result = DID_NO_CONNECT;
447 		done(sc);
448 		return 0;
449 	}
450 
451 	if (lp->state != LPORT_ST_READY || !(lp->link_up))
452 		return SCSI_MLQUEUE_HOST_BUSY;
453 
454 	atomic_inc(&fnic->in_flight);
455 
456 	/*
457 	 * Release host lock, use driver resource specific locks from here.
458 	 * Don't re-enable interrupts in case they were disabled prior to the
459 	 * caller disabling them.
460 	 */
461 	spin_unlock(lp->host->host_lock);
462 	CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
463 	CMD_FLAGS(sc) = FNIC_NO_FLAGS;
464 
465 	/* Get a new io_req for this SCSI IO */
466 	io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
467 	if (!io_req) {
468 		atomic64_inc(&fnic_stats->io_stats.alloc_failures);
469 		ret = SCSI_MLQUEUE_HOST_BUSY;
470 		goto out;
471 	}
472 	memset(io_req, 0, sizeof(*io_req));
473 
474 	/* Map the data buffer */
475 	sg_count = scsi_dma_map(sc);
476 	if (sg_count < 0) {
477 		FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
478 			  sc->request->tag, sc, 0, sc->cmnd[0],
479 			  sg_count, CMD_STATE(sc));
480 		mempool_free(io_req, fnic->io_req_pool);
481 		goto out;
482 	}
483 
484 	/* Determine the type of scatter/gather list we need */
485 	io_req->sgl_cnt = sg_count;
486 	io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
487 	if (sg_count > FNIC_DFLT_SG_DESC_CNT)
488 		io_req->sgl_type = FNIC_SGL_CACHE_MAX;
489 
490 	if (sg_count) {
491 		io_req->sgl_list =
492 			mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
493 				      GFP_ATOMIC);
494 		if (!io_req->sgl_list) {
495 			atomic64_inc(&fnic_stats->io_stats.alloc_failures);
496 			ret = SCSI_MLQUEUE_HOST_BUSY;
497 			scsi_dma_unmap(sc);
498 			mempool_free(io_req, fnic->io_req_pool);
499 			goto out;
500 		}
501 
502 		/* Cache sgl list allocated address before alignment */
503 		io_req->sgl_list_alloc = io_req->sgl_list;
504 		ptr = (unsigned long) io_req->sgl_list;
505 		if (ptr % FNIC_SG_DESC_ALIGN) {
506 			io_req->sgl_list = (struct host_sg_desc *)
507 				(((unsigned long) ptr
508 				  + FNIC_SG_DESC_ALIGN - 1)
509 				 & ~(FNIC_SG_DESC_ALIGN - 1));
510 		}
511 	}
512 
513 	/*
514 	* Will acquire lock defore setting to IO initialized.
515 	*/
516 
517 	io_lock = fnic_io_lock_hash(fnic, sc);
518 	spin_lock_irqsave(io_lock, flags);
519 
520 	/* initialize rest of io_req */
521 	io_req->port_id = rport->port_id;
522 	io_req->start_time = jiffies;
523 	CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
524 	CMD_SP(sc) = (char *)io_req;
525 	CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
526 	sc->scsi_done = done;
527 
528 	/* create copy wq desc and enqueue it */
529 	wq = &fnic->wq_copy[0];
530 	ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
531 	if (ret) {
532 		/*
533 		 * In case another thread cancelled the request,
534 		 * refetch the pointer under the lock.
535 		 */
536 		FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
537 			  sc->request->tag, sc, 0, 0, 0,
538 			  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
539 		io_req = (struct fnic_io_req *)CMD_SP(sc);
540 		CMD_SP(sc) = NULL;
541 		CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
542 		spin_unlock_irqrestore(io_lock, flags);
543 		if (io_req) {
544 			fnic_release_ioreq_buf(fnic, io_req, sc);
545 			mempool_free(io_req, fnic->io_req_pool);
546 		}
547 		atomic_dec(&fnic->in_flight);
548 		/* acquire host lock before returning to SCSI */
549 		spin_lock(lp->host->host_lock);
550 		return ret;
551 	} else {
552 		atomic64_inc(&fnic_stats->io_stats.active_ios);
553 		atomic64_inc(&fnic_stats->io_stats.num_ios);
554 		if (atomic64_read(&fnic_stats->io_stats.active_ios) >
555 			  atomic64_read(&fnic_stats->io_stats.max_active_ios))
556 			atomic64_set(&fnic_stats->io_stats.max_active_ios,
557 			     atomic64_read(&fnic_stats->io_stats.active_ios));
558 
559 		/* REVISIT: Use per IO lock in the final code */
560 		CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
561 	}
562 out:
563 	cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
564 			(u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
565 			(u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
566 			sc->cmnd[5]);
567 
568 	FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
569 		  sc->request->tag, sc, io_req,
570 		  sg_count, cmd_trace,
571 		  (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
572 
573 	/* if only we issued IO, will we have the io lock */
574 	if (CMD_FLAGS(sc) & FNIC_IO_INITIALIZED)
575 		spin_unlock_irqrestore(io_lock, flags);
576 
577 	atomic_dec(&fnic->in_flight);
578 	/* acquire host lock before returning to SCSI */
579 	spin_lock(lp->host->host_lock);
580 	return ret;
581 }
582 
583 DEF_SCSI_QCMD(fnic_queuecommand)
584 
585 /*
586  * fnic_fcpio_fw_reset_cmpl_handler
587  * Routine to handle fw reset completion
588  */
589 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
590 					    struct fcpio_fw_req *desc)
591 {
592 	u8 type;
593 	u8 hdr_status;
594 	struct fcpio_tag tag;
595 	int ret = 0;
596 	unsigned long flags;
597 	struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
598 
599 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
600 
601 	atomic64_inc(&reset_stats->fw_reset_completions);
602 
603 	/* Clean up all outstanding io requests */
604 	fnic_cleanup_io(fnic, SCSI_NO_TAG);
605 
606 	atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
607 	atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
608 
609 	spin_lock_irqsave(&fnic->fnic_lock, flags);
610 
611 	/* fnic should be in FC_TRANS_ETH_MODE */
612 	if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
613 		/* Check status of reset completion */
614 		if (!hdr_status) {
615 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
616 				      "reset cmpl success\n");
617 			/* Ready to send flogi out */
618 			fnic->state = FNIC_IN_ETH_MODE;
619 		} else {
620 			FNIC_SCSI_DBG(KERN_DEBUG,
621 				      fnic->lport->host,
622 				      "fnic fw_reset : failed %s\n",
623 				      fnic_fcpio_status_to_str(hdr_status));
624 
625 			/*
626 			 * Unable to change to eth mode, cannot send out flogi
627 			 * Change state to fc mode, so that subsequent Flogi
628 			 * requests from libFC will cause more attempts to
629 			 * reset the firmware. Free the cached flogi
630 			 */
631 			fnic->state = FNIC_IN_FC_MODE;
632 			atomic64_inc(&reset_stats->fw_reset_failures);
633 			ret = -1;
634 		}
635 	} else {
636 		FNIC_SCSI_DBG(KERN_DEBUG,
637 			      fnic->lport->host,
638 			      "Unexpected state %s while processing"
639 			      " reset cmpl\n", fnic_state_to_str(fnic->state));
640 		atomic64_inc(&reset_stats->fw_reset_failures);
641 		ret = -1;
642 	}
643 
644 	/* Thread removing device blocks till firmware reset is complete */
645 	if (fnic->remove_wait)
646 		complete(fnic->remove_wait);
647 
648 	/*
649 	 * If fnic is being removed, or fw reset failed
650 	 * free the flogi frame. Else, send it out
651 	 */
652 	if (fnic->remove_wait || ret) {
653 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
654 		skb_queue_purge(&fnic->tx_queue);
655 		goto reset_cmpl_handler_end;
656 	}
657 
658 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
659 
660 	fnic_flush_tx(fnic);
661 
662  reset_cmpl_handler_end:
663 	fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
664 
665 	return ret;
666 }
667 
668 /*
669  * fnic_fcpio_flogi_reg_cmpl_handler
670  * Routine to handle flogi register completion
671  */
672 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
673 					     struct fcpio_fw_req *desc)
674 {
675 	u8 type;
676 	u8 hdr_status;
677 	struct fcpio_tag tag;
678 	int ret = 0;
679 	unsigned long flags;
680 
681 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
682 
683 	/* Update fnic state based on status of flogi reg completion */
684 	spin_lock_irqsave(&fnic->fnic_lock, flags);
685 
686 	if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
687 
688 		/* Check flogi registration completion status */
689 		if (!hdr_status) {
690 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
691 				      "flog reg succeeded\n");
692 			fnic->state = FNIC_IN_FC_MODE;
693 		} else {
694 			FNIC_SCSI_DBG(KERN_DEBUG,
695 				      fnic->lport->host,
696 				      "fnic flogi reg :failed %s\n",
697 				      fnic_fcpio_status_to_str(hdr_status));
698 			fnic->state = FNIC_IN_ETH_MODE;
699 			ret = -1;
700 		}
701 	} else {
702 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
703 			      "Unexpected fnic state %s while"
704 			      " processing flogi reg completion\n",
705 			      fnic_state_to_str(fnic->state));
706 		ret = -1;
707 	}
708 
709 	if (!ret) {
710 		if (fnic->stop_rx_link_events) {
711 			spin_unlock_irqrestore(&fnic->fnic_lock, flags);
712 			goto reg_cmpl_handler_end;
713 		}
714 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
715 
716 		fnic_flush_tx(fnic);
717 		queue_work(fnic_event_queue, &fnic->frame_work);
718 	} else {
719 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
720 	}
721 
722 reg_cmpl_handler_end:
723 	return ret;
724 }
725 
726 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
727 					u16 request_out)
728 {
729 	if (wq->to_clean_index <= wq->to_use_index) {
730 		/* out of range, stale request_out index */
731 		if (request_out < wq->to_clean_index ||
732 		    request_out >= wq->to_use_index)
733 			return 0;
734 	} else {
735 		/* out of range, stale request_out index */
736 		if (request_out < wq->to_clean_index &&
737 		    request_out >= wq->to_use_index)
738 			return 0;
739 	}
740 	/* request_out index is in range */
741 	return 1;
742 }
743 
744 
745 /*
746  * Mark that ack received and store the Ack index. If there are multiple
747  * acks received before Tx thread cleans it up, the latest value will be
748  * used which is correct behavior. This state should be in the copy Wq
749  * instead of in the fnic
750  */
751 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
752 					  unsigned int cq_index,
753 					  struct fcpio_fw_req *desc)
754 {
755 	struct vnic_wq_copy *wq;
756 	u16 request_out = desc->u.ack.request_out;
757 	unsigned long flags;
758 	u64 *ox_id_tag = (u64 *)(void *)desc;
759 
760 	/* mark the ack state */
761 	wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
762 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
763 
764 	fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
765 	if (is_ack_index_in_range(wq, request_out)) {
766 		fnic->fw_ack_index[0] = request_out;
767 		fnic->fw_ack_recd[0] = 1;
768 	} else
769 		atomic64_inc(
770 			&fnic->fnic_stats.misc_stats.ack_index_out_of_range);
771 
772 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
773 	FNIC_TRACE(fnic_fcpio_ack_handler,
774 		  fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
775 		  ox_id_tag[4], ox_id_tag[5]);
776 }
777 
778 /*
779  * fnic_fcpio_icmnd_cmpl_handler
780  * Routine to handle icmnd completions
781  */
782 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
783 					 struct fcpio_fw_req *desc)
784 {
785 	u8 type;
786 	u8 hdr_status;
787 	struct fcpio_tag tag;
788 	u32 id;
789 	u64 xfer_len = 0;
790 	struct fcpio_icmnd_cmpl *icmnd_cmpl;
791 	struct fnic_io_req *io_req;
792 	struct scsi_cmnd *sc;
793 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
794 	unsigned long flags;
795 	spinlock_t *io_lock;
796 	u64 cmd_trace;
797 	unsigned long start_time;
798 
799 	/* Decode the cmpl description to get the io_req id */
800 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
801 	fcpio_tag_id_dec(&tag, &id);
802 	icmnd_cmpl = &desc->u.icmnd_cmpl;
803 
804 	if (id >= fnic->fnic_max_tag_id) {
805 		shost_printk(KERN_ERR, fnic->lport->host,
806 			"Tag out of range tag %x hdr status = %s\n",
807 			     id, fnic_fcpio_status_to_str(hdr_status));
808 		return;
809 	}
810 
811 	sc = scsi_host_find_tag(fnic->lport->host, id);
812 	WARN_ON_ONCE(!sc);
813 	if (!sc) {
814 		atomic64_inc(&fnic_stats->io_stats.sc_null);
815 		shost_printk(KERN_ERR, fnic->lport->host,
816 			  "icmnd_cmpl sc is null - "
817 			  "hdr status = %s tag = 0x%x desc = 0x%p\n",
818 			  fnic_fcpio_status_to_str(hdr_status), id, desc);
819 		FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
820 			  fnic->lport->host->host_no, id,
821 			  ((u64)icmnd_cmpl->_resvd0[1] << 16 |
822 			  (u64)icmnd_cmpl->_resvd0[0]),
823 			  ((u64)hdr_status << 16 |
824 			  (u64)icmnd_cmpl->scsi_status << 8 |
825 			  (u64)icmnd_cmpl->flags), desc,
826 			  (u64)icmnd_cmpl->residual, 0);
827 		return;
828 	}
829 
830 	io_lock = fnic_io_lock_hash(fnic, sc);
831 	spin_lock_irqsave(io_lock, flags);
832 	io_req = (struct fnic_io_req *)CMD_SP(sc);
833 	WARN_ON_ONCE(!io_req);
834 	if (!io_req) {
835 		atomic64_inc(&fnic_stats->io_stats.ioreq_null);
836 		CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
837 		spin_unlock_irqrestore(io_lock, flags);
838 		shost_printk(KERN_ERR, fnic->lport->host,
839 			  "icmnd_cmpl io_req is null - "
840 			  "hdr status = %s tag = 0x%x sc 0x%p\n",
841 			  fnic_fcpio_status_to_str(hdr_status), id, sc);
842 		return;
843 	}
844 	start_time = io_req->start_time;
845 
846 	/* firmware completed the io */
847 	io_req->io_completed = 1;
848 
849 	/*
850 	 *  if SCSI-ML has already issued abort on this command,
851 	 * ignore completion of the IO. The abts path will clean it up
852 	 */
853 	if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
854 		spin_unlock_irqrestore(io_lock, flags);
855 		CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
856 		switch (hdr_status) {
857 		case FCPIO_SUCCESS:
858 			CMD_FLAGS(sc) |= FNIC_IO_DONE;
859 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
860 				  "icmnd_cmpl ABTS pending hdr status = %s "
861 				  "sc  0x%p scsi_status %x  residual %d\n",
862 				  fnic_fcpio_status_to_str(hdr_status), sc,
863 				  icmnd_cmpl->scsi_status,
864 				  icmnd_cmpl->residual);
865 			break;
866 		case FCPIO_ABORTED:
867 			CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
868 			break;
869 		default:
870 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
871 					  "icmnd_cmpl abts pending "
872 					  "hdr status = %s tag = 0x%x sc = 0x%p\n",
873 					  fnic_fcpio_status_to_str(hdr_status),
874 					  id, sc);
875 			break;
876 		}
877 		return;
878 	}
879 
880 	/* Mark the IO as complete */
881 	CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
882 
883 	icmnd_cmpl = &desc->u.icmnd_cmpl;
884 
885 	switch (hdr_status) {
886 	case FCPIO_SUCCESS:
887 		sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
888 		xfer_len = scsi_bufflen(sc);
889 		scsi_set_resid(sc, icmnd_cmpl->residual);
890 
891 		if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
892 			xfer_len -= icmnd_cmpl->residual;
893 
894 		if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
895 			atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
896 		break;
897 
898 	case FCPIO_TIMEOUT:          /* request was timed out */
899 		atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
900 		sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
901 		break;
902 
903 	case FCPIO_ABORTED:          /* request was aborted */
904 		atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
905 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
906 		break;
907 
908 	case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
909 		atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
910 		scsi_set_resid(sc, icmnd_cmpl->residual);
911 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
912 		break;
913 
914 	case FCPIO_OUT_OF_RESOURCE:  /* out of resources to complete request */
915 		atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
916 		sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
917 		break;
918 
919 	case FCPIO_IO_NOT_FOUND:     /* requested I/O was not found */
920 		atomic64_inc(&fnic_stats->io_stats.io_not_found);
921 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
922 		break;
923 
924 	case FCPIO_SGL_INVALID:      /* request was aborted due to sgl error */
925 		atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
926 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
927 		break;
928 
929 	case FCPIO_FW_ERR:           /* request was terminated due fw error */
930 		atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
931 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
932 		break;
933 
934 	case FCPIO_MSS_INVALID:      /* request was aborted due to mss error */
935 		atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
936 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
937 		break;
938 
939 	case FCPIO_INVALID_HEADER:   /* header contains invalid data */
940 	case FCPIO_INVALID_PARAM:    /* some parameter in request invalid */
941 	case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
942 	default:
943 		shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
944 			     fnic_fcpio_status_to_str(hdr_status));
945 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
946 		break;
947 	}
948 
949 	if (hdr_status != FCPIO_SUCCESS) {
950 		atomic64_inc(&fnic_stats->io_stats.io_failures);
951 		shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
952 			     fnic_fcpio_status_to_str(hdr_status));
953 	}
954 	/* Break link with the SCSI command */
955 	CMD_SP(sc) = NULL;
956 	CMD_FLAGS(sc) |= FNIC_IO_DONE;
957 
958 	spin_unlock_irqrestore(io_lock, flags);
959 
960 	fnic_release_ioreq_buf(fnic, io_req, sc);
961 
962 	mempool_free(io_req, fnic->io_req_pool);
963 
964 	cmd_trace = ((u64)hdr_status << 56) |
965 		  (u64)icmnd_cmpl->scsi_status << 48 |
966 		  (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
967 		  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
968 		  (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
969 
970 	FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
971 		  sc->device->host->host_no, id, sc,
972 		  ((u64)icmnd_cmpl->_resvd0[1] << 56 |
973 		  (u64)icmnd_cmpl->_resvd0[0] << 48 |
974 		  jiffies_to_msecs(jiffies - start_time)),
975 		  desc, cmd_trace,
976 		  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
977 
978 	if (sc->sc_data_direction == DMA_FROM_DEVICE) {
979 		fnic->lport->host_stats.fcp_input_requests++;
980 		fnic->fcp_input_bytes += xfer_len;
981 	} else if (sc->sc_data_direction == DMA_TO_DEVICE) {
982 		fnic->lport->host_stats.fcp_output_requests++;
983 		fnic->fcp_output_bytes += xfer_len;
984 	} else
985 		fnic->lport->host_stats.fcp_control_requests++;
986 
987 	atomic64_dec(&fnic_stats->io_stats.active_ios);
988 	if (atomic64_read(&fnic->io_cmpl_skip))
989 		atomic64_dec(&fnic->io_cmpl_skip);
990 	else
991 		atomic64_inc(&fnic_stats->io_stats.io_completions);
992 
993 	/* Call SCSI completion function to complete the IO */
994 	if (sc->scsi_done)
995 		sc->scsi_done(sc);
996 }
997 
998 /* fnic_fcpio_itmf_cmpl_handler
999  * Routine to handle itmf completions
1000  */
1001 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1002 					struct fcpio_fw_req *desc)
1003 {
1004 	u8 type;
1005 	u8 hdr_status;
1006 	struct fcpio_tag tag;
1007 	u32 id;
1008 	struct scsi_cmnd *sc;
1009 	struct fnic_io_req *io_req;
1010 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1011 	struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1012 	struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1013 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1014 	unsigned long flags;
1015 	spinlock_t *io_lock;
1016 	unsigned long start_time;
1017 
1018 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1019 	fcpio_tag_id_dec(&tag, &id);
1020 
1021 	if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
1022 		shost_printk(KERN_ERR, fnic->lport->host,
1023 		"Tag out of range tag %x hdr status = %s\n",
1024 		id, fnic_fcpio_status_to_str(hdr_status));
1025 		return;
1026 	}
1027 
1028 	sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1029 	WARN_ON_ONCE(!sc);
1030 	if (!sc) {
1031 		atomic64_inc(&fnic_stats->io_stats.sc_null);
1032 		shost_printk(KERN_ERR, fnic->lport->host,
1033 			  "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1034 			  fnic_fcpio_status_to_str(hdr_status), id);
1035 		return;
1036 	}
1037 	io_lock = fnic_io_lock_hash(fnic, sc);
1038 	spin_lock_irqsave(io_lock, flags);
1039 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1040 	WARN_ON_ONCE(!io_req);
1041 	if (!io_req) {
1042 		atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1043 		spin_unlock_irqrestore(io_lock, flags);
1044 		CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1045 		shost_printk(KERN_ERR, fnic->lport->host,
1046 			  "itmf_cmpl io_req is null - "
1047 			  "hdr status = %s tag = 0x%x sc 0x%p\n",
1048 			  fnic_fcpio_status_to_str(hdr_status), id, sc);
1049 		return;
1050 	}
1051 	start_time = io_req->start_time;
1052 
1053 	if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1054 		/* Abort and terminate completion of device reset req */
1055 		/* REVISIT : Add asserts about various flags */
1056 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1057 			      "dev reset abts cmpl recd. id %x status %s\n",
1058 			      id, fnic_fcpio_status_to_str(hdr_status));
1059 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1060 		CMD_ABTS_STATUS(sc) = hdr_status;
1061 		CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1062 		if (io_req->abts_done)
1063 			complete(io_req->abts_done);
1064 		spin_unlock_irqrestore(io_lock, flags);
1065 	} else if (id & FNIC_TAG_ABORT) {
1066 		/* Completion of abort cmd */
1067 		switch (hdr_status) {
1068 		case FCPIO_SUCCESS:
1069 			break;
1070 		case FCPIO_TIMEOUT:
1071 			if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1072 				atomic64_inc(&abts_stats->abort_fw_timeouts);
1073 			else
1074 				atomic64_inc(
1075 					&term_stats->terminate_fw_timeouts);
1076 			break;
1077 		case FCPIO_IO_NOT_FOUND:
1078 			if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1079 				atomic64_inc(&abts_stats->abort_io_not_found);
1080 			else
1081 				atomic64_inc(
1082 					&term_stats->terminate_io_not_found);
1083 			break;
1084 		default:
1085 			if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1086 				atomic64_inc(&abts_stats->abort_failures);
1087 			else
1088 				atomic64_inc(
1089 					&term_stats->terminate_failures);
1090 			break;
1091 		}
1092 		if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1093 			/* This is a late completion. Ignore it */
1094 			spin_unlock_irqrestore(io_lock, flags);
1095 			return;
1096 		}
1097 		CMD_ABTS_STATUS(sc) = hdr_status;
1098 		CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
1099 
1100 		atomic64_dec(&fnic_stats->io_stats.active_ios);
1101 		if (atomic64_read(&fnic->io_cmpl_skip))
1102 			atomic64_dec(&fnic->io_cmpl_skip);
1103 		else
1104 			atomic64_inc(&fnic_stats->io_stats.io_completions);
1105 
1106 		if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1107 			atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1108 
1109 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1110 			      "abts cmpl recd. id %d status %s\n",
1111 			      (int)(id & FNIC_TAG_MASK),
1112 			      fnic_fcpio_status_to_str(hdr_status));
1113 
1114 		/*
1115 		 * If scsi_eh thread is blocked waiting for abts to complete,
1116 		 * signal completion to it. IO will be cleaned in the thread
1117 		 * else clean it in this context
1118 		 */
1119 		if (io_req->abts_done) {
1120 			complete(io_req->abts_done);
1121 			spin_unlock_irqrestore(io_lock, flags);
1122 		} else {
1123 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1124 				      "abts cmpl, completing IO\n");
1125 			CMD_SP(sc) = NULL;
1126 			sc->result = (DID_ERROR << 16);
1127 
1128 			spin_unlock_irqrestore(io_lock, flags);
1129 
1130 			fnic_release_ioreq_buf(fnic, io_req, sc);
1131 			mempool_free(io_req, fnic->io_req_pool);
1132 			if (sc->scsi_done) {
1133 				FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1134 					sc->device->host->host_no, id,
1135 					sc,
1136 					jiffies_to_msecs(jiffies - start_time),
1137 					desc,
1138 					(((u64)hdr_status << 40) |
1139 					(u64)sc->cmnd[0] << 32 |
1140 					(u64)sc->cmnd[2] << 24 |
1141 					(u64)sc->cmnd[3] << 16 |
1142 					(u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1143 					(((u64)CMD_FLAGS(sc) << 32) |
1144 					CMD_STATE(sc)));
1145 				sc->scsi_done(sc);
1146 			}
1147 		}
1148 
1149 	} else if (id & FNIC_TAG_DEV_RST) {
1150 		/* Completion of device reset */
1151 		CMD_LR_STATUS(sc) = hdr_status;
1152 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1153 			spin_unlock_irqrestore(io_lock, flags);
1154 			CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
1155 			FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1156 				  sc->device->host->host_no, id, sc,
1157 				  jiffies_to_msecs(jiffies - start_time),
1158 				  desc, 0,
1159 				  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1160 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1161 				"Terminate pending "
1162 				"dev reset cmpl recd. id %d status %s\n",
1163 				(int)(id & FNIC_TAG_MASK),
1164 				fnic_fcpio_status_to_str(hdr_status));
1165 			return;
1166 		}
1167 		if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1168 			/* Need to wait for terminate completion */
1169 			spin_unlock_irqrestore(io_lock, flags);
1170 			FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1171 				  sc->device->host->host_no, id, sc,
1172 				  jiffies_to_msecs(jiffies - start_time),
1173 				  desc, 0,
1174 				  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1175 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1176 				"dev reset cmpl recd after time out. "
1177 				"id %d status %s\n",
1178 				(int)(id & FNIC_TAG_MASK),
1179 				fnic_fcpio_status_to_str(hdr_status));
1180 			return;
1181 		}
1182 		CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
1183 		CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1184 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1185 			      "dev reset cmpl recd. id %d status %s\n",
1186 			      (int)(id & FNIC_TAG_MASK),
1187 			      fnic_fcpio_status_to_str(hdr_status));
1188 		if (io_req->dr_done)
1189 			complete(io_req->dr_done);
1190 		spin_unlock_irqrestore(io_lock, flags);
1191 
1192 	} else {
1193 		shost_printk(KERN_ERR, fnic->lport->host,
1194 			     "Unexpected itmf io state %s tag %x\n",
1195 			     fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1196 		spin_unlock_irqrestore(io_lock, flags);
1197 	}
1198 
1199 }
1200 
1201 /*
1202  * fnic_fcpio_cmpl_handler
1203  * Routine to service the cq for wq_copy
1204  */
1205 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1206 				   unsigned int cq_index,
1207 				   struct fcpio_fw_req *desc)
1208 {
1209 	struct fnic *fnic = vnic_dev_priv(vdev);
1210 
1211 	switch (desc->hdr.type) {
1212 	case FCPIO_ICMND_CMPL: /* fw completed a command */
1213 	case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1214 	case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1215 	case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1216 	case FCPIO_RESET_CMPL: /* fw completed reset */
1217 		atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1218 		break;
1219 	default:
1220 		break;
1221 	}
1222 
1223 	switch (desc->hdr.type) {
1224 	case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1225 		fnic_fcpio_ack_handler(fnic, cq_index, desc);
1226 		break;
1227 
1228 	case FCPIO_ICMND_CMPL: /* fw completed a command */
1229 		fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1230 		break;
1231 
1232 	case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1233 		fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1234 		break;
1235 
1236 	case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1237 	case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1238 		fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1239 		break;
1240 
1241 	case FCPIO_RESET_CMPL: /* fw completed reset */
1242 		fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1243 		break;
1244 
1245 	default:
1246 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1247 			      "firmware completion type %d\n",
1248 			      desc->hdr.type);
1249 		break;
1250 	}
1251 
1252 	return 0;
1253 }
1254 
1255 /*
1256  * fnic_wq_copy_cmpl_handler
1257  * Routine to process wq copy
1258  */
1259 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1260 {
1261 	unsigned int wq_work_done = 0;
1262 	unsigned int i, cq_index;
1263 	unsigned int cur_work_done;
1264 
1265 	for (i = 0; i < fnic->wq_copy_count; i++) {
1266 		cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1267 		cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1268 						     fnic_fcpio_cmpl_handler,
1269 						     copy_work_to_do);
1270 		wq_work_done += cur_work_done;
1271 	}
1272 	return wq_work_done;
1273 }
1274 
1275 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1276 {
1277 	int i;
1278 	struct fnic_io_req *io_req;
1279 	unsigned long flags = 0;
1280 	struct scsi_cmnd *sc;
1281 	spinlock_t *io_lock;
1282 	unsigned long start_time = 0;
1283 	struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1284 
1285 	for (i = 0; i < fnic->fnic_max_tag_id; i++) {
1286 		if (i == exclude_id)
1287 			continue;
1288 
1289 		io_lock = fnic_io_lock_tag(fnic, i);
1290 		spin_lock_irqsave(io_lock, flags);
1291 		sc = scsi_host_find_tag(fnic->lport->host, i);
1292 		if (!sc) {
1293 			spin_unlock_irqrestore(io_lock, flags);
1294 			continue;
1295 		}
1296 
1297 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1298 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1299 			!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1300 			/*
1301 			 * We will be here only when FW completes reset
1302 			 * without sending completions for outstanding ios.
1303 			 */
1304 			CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1305 			if (io_req && io_req->dr_done)
1306 				complete(io_req->dr_done);
1307 			else if (io_req && io_req->abts_done)
1308 				complete(io_req->abts_done);
1309 			spin_unlock_irqrestore(io_lock, flags);
1310 			continue;
1311 		} else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1312 			spin_unlock_irqrestore(io_lock, flags);
1313 			continue;
1314 		}
1315 		if (!io_req) {
1316 			spin_unlock_irqrestore(io_lock, flags);
1317 			goto cleanup_scsi_cmd;
1318 		}
1319 
1320 		CMD_SP(sc) = NULL;
1321 
1322 		spin_unlock_irqrestore(io_lock, flags);
1323 
1324 		/*
1325 		 * If there is a scsi_cmnd associated with this io_req, then
1326 		 * free the corresponding state
1327 		 */
1328 		start_time = io_req->start_time;
1329 		fnic_release_ioreq_buf(fnic, io_req, sc);
1330 		mempool_free(io_req, fnic->io_req_pool);
1331 
1332 cleanup_scsi_cmd:
1333 		sc->result = DID_TRANSPORT_DISRUPTED << 16;
1334 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1335 			      "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1336 			      __func__, (jiffies - start_time));
1337 
1338 		if (atomic64_read(&fnic->io_cmpl_skip))
1339 			atomic64_dec(&fnic->io_cmpl_skip);
1340 		else
1341 			atomic64_inc(&fnic_stats->io_stats.io_completions);
1342 
1343 		/* Complete the command to SCSI */
1344 		if (sc->scsi_done) {
1345 			FNIC_TRACE(fnic_cleanup_io,
1346 				  sc->device->host->host_no, i, sc,
1347 				  jiffies_to_msecs(jiffies - start_time),
1348 				  0, ((u64)sc->cmnd[0] << 32 |
1349 				  (u64)sc->cmnd[2] << 24 |
1350 				  (u64)sc->cmnd[3] << 16 |
1351 				  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1352 				  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1353 
1354 			sc->scsi_done(sc);
1355 		}
1356 	}
1357 }
1358 
1359 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1360 				  struct fcpio_host_req *desc)
1361 {
1362 	u32 id;
1363 	struct fnic *fnic = vnic_dev_priv(wq->vdev);
1364 	struct fnic_io_req *io_req;
1365 	struct scsi_cmnd *sc;
1366 	unsigned long flags;
1367 	spinlock_t *io_lock;
1368 	unsigned long start_time = 0;
1369 
1370 	/* get the tag reference */
1371 	fcpio_tag_id_dec(&desc->hdr.tag, &id);
1372 	id &= FNIC_TAG_MASK;
1373 
1374 	if (id >= fnic->fnic_max_tag_id)
1375 		return;
1376 
1377 	sc = scsi_host_find_tag(fnic->lport->host, id);
1378 	if (!sc)
1379 		return;
1380 
1381 	io_lock = fnic_io_lock_hash(fnic, sc);
1382 	spin_lock_irqsave(io_lock, flags);
1383 
1384 	/* Get the IO context which this desc refers to */
1385 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1386 
1387 	/* fnic interrupts are turned off by now */
1388 
1389 	if (!io_req) {
1390 		spin_unlock_irqrestore(io_lock, flags);
1391 		goto wq_copy_cleanup_scsi_cmd;
1392 	}
1393 
1394 	CMD_SP(sc) = NULL;
1395 
1396 	spin_unlock_irqrestore(io_lock, flags);
1397 
1398 	start_time = io_req->start_time;
1399 	fnic_release_ioreq_buf(fnic, io_req, sc);
1400 	mempool_free(io_req, fnic->io_req_pool);
1401 
1402 wq_copy_cleanup_scsi_cmd:
1403 	sc->result = DID_NO_CONNECT << 16;
1404 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1405 		      " DID_NO_CONNECT\n");
1406 
1407 	if (sc->scsi_done) {
1408 		FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1409 			  sc->device->host->host_no, id, sc,
1410 			  jiffies_to_msecs(jiffies - start_time),
1411 			  0, ((u64)sc->cmnd[0] << 32 |
1412 			  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1413 			  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1414 			  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1415 
1416 		sc->scsi_done(sc);
1417 	}
1418 }
1419 
1420 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1421 					  u32 task_req, u8 *fc_lun,
1422 					  struct fnic_io_req *io_req)
1423 {
1424 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1425 	struct Scsi_Host *host = fnic->lport->host;
1426 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1427 	unsigned long flags;
1428 
1429 	spin_lock_irqsave(host->host_lock, flags);
1430 	if (unlikely(fnic_chk_state_flags_locked(fnic,
1431 						FNIC_FLAGS_IO_BLOCKED))) {
1432 		spin_unlock_irqrestore(host->host_lock, flags);
1433 		return 1;
1434 	} else
1435 		atomic_inc(&fnic->in_flight);
1436 	spin_unlock_irqrestore(host->host_lock, flags);
1437 
1438 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1439 
1440 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1441 		free_wq_copy_descs(fnic, wq);
1442 
1443 	if (!vnic_wq_copy_desc_avail(wq)) {
1444 		spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1445 		atomic_dec(&fnic->in_flight);
1446 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1447 			"fnic_queue_abort_io_req: failure: no descriptors\n");
1448 		atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
1449 		return 1;
1450 	}
1451 	fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1452 				     0, task_req, tag, fc_lun, io_req->port_id,
1453 				     fnic->config.ra_tov, fnic->config.ed_tov);
1454 
1455 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1456 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1457 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1458 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1459 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1460 
1461 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1462 	atomic_dec(&fnic->in_flight);
1463 
1464 	return 0;
1465 }
1466 
1467 static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1468 {
1469 	int tag;
1470 	int abt_tag;
1471 	int term_cnt = 0;
1472 	struct fnic_io_req *io_req;
1473 	spinlock_t *io_lock;
1474 	unsigned long flags;
1475 	struct scsi_cmnd *sc;
1476 	struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1477 	struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1478 	struct scsi_lun fc_lun;
1479 	enum fnic_ioreq_state old_ioreq_state;
1480 
1481 	FNIC_SCSI_DBG(KERN_DEBUG,
1482 		      fnic->lport->host,
1483 		      "fnic_rport_exch_reset called portid 0x%06x\n",
1484 		      port_id);
1485 
1486 	if (fnic->in_remove)
1487 		return;
1488 
1489 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1490 		abt_tag = tag;
1491 		io_lock = fnic_io_lock_tag(fnic, tag);
1492 		spin_lock_irqsave(io_lock, flags);
1493 		sc = scsi_host_find_tag(fnic->lport->host, tag);
1494 		if (!sc) {
1495 			spin_unlock_irqrestore(io_lock, flags);
1496 			continue;
1497 		}
1498 
1499 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1500 
1501 		if (!io_req || io_req->port_id != port_id) {
1502 			spin_unlock_irqrestore(io_lock, flags);
1503 			continue;
1504 		}
1505 
1506 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1507 			(!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1508 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1509 			"fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1510 			sc);
1511 			spin_unlock_irqrestore(io_lock, flags);
1512 			continue;
1513 		}
1514 
1515 		/*
1516 		 * Found IO that is still pending with firmware and
1517 		 * belongs to rport that went away
1518 		 */
1519 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1520 			spin_unlock_irqrestore(io_lock, flags);
1521 			continue;
1522 		}
1523 		if (io_req->abts_done) {
1524 			shost_printk(KERN_ERR, fnic->lport->host,
1525 			"fnic_rport_exch_reset: io_req->abts_done is set "
1526 			"state is %s\n",
1527 			fnic_ioreq_state_to_str(CMD_STATE(sc)));
1528 		}
1529 
1530 		if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1531 			shost_printk(KERN_ERR, fnic->lport->host,
1532 				  "rport_exch_reset "
1533 				  "IO not yet issued %p tag 0x%x flags "
1534 				  "%x state %d\n",
1535 				  sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1536 		}
1537 		old_ioreq_state = CMD_STATE(sc);
1538 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1539 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1540 		if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1541 			atomic64_inc(&reset_stats->device_reset_terminates);
1542 			abt_tag = (tag | FNIC_TAG_DEV_RST);
1543 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1544 			"fnic_rport_exch_reset dev rst sc 0x%p\n",
1545 			sc);
1546 		}
1547 
1548 		BUG_ON(io_req->abts_done);
1549 
1550 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1551 			      "fnic_rport_reset_exch: Issuing abts\n");
1552 
1553 		spin_unlock_irqrestore(io_lock, flags);
1554 
1555 		/* Now queue the abort command to firmware */
1556 		int_to_scsilun(sc->device->lun, &fc_lun);
1557 
1558 		if (fnic_queue_abort_io_req(fnic, abt_tag,
1559 					    FCPIO_ITMF_ABT_TASK_TERM,
1560 					    fc_lun.scsi_lun, io_req)) {
1561 			/*
1562 			 * Revert the cmd state back to old state, if
1563 			 * it hasn't changed in between. This cmd will get
1564 			 * aborted later by scsi_eh, or cleaned up during
1565 			 * lun reset
1566 			 */
1567 			spin_lock_irqsave(io_lock, flags);
1568 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1569 				CMD_STATE(sc) = old_ioreq_state;
1570 			spin_unlock_irqrestore(io_lock, flags);
1571 		} else {
1572 			spin_lock_irqsave(io_lock, flags);
1573 			if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1574 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1575 			else
1576 				CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1577 			spin_unlock_irqrestore(io_lock, flags);
1578 			atomic64_inc(&term_stats->terminates);
1579 			term_cnt++;
1580 		}
1581 	}
1582 	if (term_cnt > atomic64_read(&term_stats->max_terminates))
1583 		atomic64_set(&term_stats->max_terminates, term_cnt);
1584 
1585 }
1586 
1587 void fnic_terminate_rport_io(struct fc_rport *rport)
1588 {
1589 	int tag;
1590 	int abt_tag;
1591 	int term_cnt = 0;
1592 	struct fnic_io_req *io_req;
1593 	spinlock_t *io_lock;
1594 	unsigned long flags;
1595 	struct scsi_cmnd *sc;
1596 	struct scsi_lun fc_lun;
1597 	struct fc_rport_libfc_priv *rdata;
1598 	struct fc_lport *lport;
1599 	struct fnic *fnic;
1600 	struct fc_rport *cmd_rport;
1601 	struct reset_stats *reset_stats;
1602 	struct terminate_stats *term_stats;
1603 	enum fnic_ioreq_state old_ioreq_state;
1604 
1605 	if (!rport) {
1606 		printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1607 		return;
1608 	}
1609 	rdata = rport->dd_data;
1610 
1611 	if (!rdata) {
1612 		printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1613 		return;
1614 	}
1615 	lport = rdata->local_port;
1616 
1617 	if (!lport) {
1618 		printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1619 		return;
1620 	}
1621 	fnic = lport_priv(lport);
1622 	FNIC_SCSI_DBG(KERN_DEBUG,
1623 		      fnic->lport->host, "fnic_terminate_rport_io called"
1624 		      " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1625 		      rport->port_name, rport->node_name, rport,
1626 		      rport->port_id);
1627 
1628 	if (fnic->in_remove)
1629 		return;
1630 
1631 	reset_stats = &fnic->fnic_stats.reset_stats;
1632 	term_stats = &fnic->fnic_stats.term_stats;
1633 
1634 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1635 		abt_tag = tag;
1636 		io_lock = fnic_io_lock_tag(fnic, tag);
1637 		spin_lock_irqsave(io_lock, flags);
1638 		sc = scsi_host_find_tag(fnic->lport->host, tag);
1639 		if (!sc) {
1640 			spin_unlock_irqrestore(io_lock, flags);
1641 			continue;
1642 		}
1643 
1644 		cmd_rport = starget_to_rport(scsi_target(sc->device));
1645 		if (rport != cmd_rport) {
1646 			spin_unlock_irqrestore(io_lock, flags);
1647 			continue;
1648 		}
1649 
1650 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1651 
1652 		if (!io_req || rport != cmd_rport) {
1653 			spin_unlock_irqrestore(io_lock, flags);
1654 			continue;
1655 		}
1656 
1657 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1658 			(!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1659 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1660 			"fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1661 			sc);
1662 			spin_unlock_irqrestore(io_lock, flags);
1663 			continue;
1664 		}
1665 		/*
1666 		 * Found IO that is still pending with firmware and
1667 		 * belongs to rport that went away
1668 		 */
1669 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1670 			spin_unlock_irqrestore(io_lock, flags);
1671 			continue;
1672 		}
1673 		if (io_req->abts_done) {
1674 			shost_printk(KERN_ERR, fnic->lport->host,
1675 			"fnic_terminate_rport_io: io_req->abts_done is set "
1676 			"state is %s\n",
1677 			fnic_ioreq_state_to_str(CMD_STATE(sc)));
1678 		}
1679 		if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1680 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1681 				  "fnic_terminate_rport_io "
1682 				  "IO not yet issued %p tag 0x%x flags "
1683 				  "%x state %d\n",
1684 				  sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1685 		}
1686 		old_ioreq_state = CMD_STATE(sc);
1687 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1688 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1689 		if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1690 			atomic64_inc(&reset_stats->device_reset_terminates);
1691 			abt_tag = (tag | FNIC_TAG_DEV_RST);
1692 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1693 			"fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1694 		}
1695 
1696 		BUG_ON(io_req->abts_done);
1697 
1698 		FNIC_SCSI_DBG(KERN_DEBUG,
1699 			      fnic->lport->host,
1700 			      "fnic_terminate_rport_io: Issuing abts\n");
1701 
1702 		spin_unlock_irqrestore(io_lock, flags);
1703 
1704 		/* Now queue the abort command to firmware */
1705 		int_to_scsilun(sc->device->lun, &fc_lun);
1706 
1707 		if (fnic_queue_abort_io_req(fnic, abt_tag,
1708 					    FCPIO_ITMF_ABT_TASK_TERM,
1709 					    fc_lun.scsi_lun, io_req)) {
1710 			/*
1711 			 * Revert the cmd state back to old state, if
1712 			 * it hasn't changed in between. This cmd will get
1713 			 * aborted later by scsi_eh, or cleaned up during
1714 			 * lun reset
1715 			 */
1716 			spin_lock_irqsave(io_lock, flags);
1717 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1718 				CMD_STATE(sc) = old_ioreq_state;
1719 			spin_unlock_irqrestore(io_lock, flags);
1720 		} else {
1721 			spin_lock_irqsave(io_lock, flags);
1722 			if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1723 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1724 			else
1725 				CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1726 			spin_unlock_irqrestore(io_lock, flags);
1727 			atomic64_inc(&term_stats->terminates);
1728 			term_cnt++;
1729 		}
1730 	}
1731 	if (term_cnt > atomic64_read(&term_stats->max_terminates))
1732 		atomic64_set(&term_stats->max_terminates, term_cnt);
1733 
1734 }
1735 
1736 /*
1737  * This function is exported to SCSI for sending abort cmnds.
1738  * A SCSI IO is represented by a io_req in the driver.
1739  * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1740  */
1741 int fnic_abort_cmd(struct scsi_cmnd *sc)
1742 {
1743 	struct fc_lport *lp;
1744 	struct fnic *fnic;
1745 	struct fnic_io_req *io_req = NULL;
1746 	struct fc_rport *rport;
1747 	spinlock_t *io_lock;
1748 	unsigned long flags;
1749 	unsigned long start_time = 0;
1750 	int ret = SUCCESS;
1751 	u32 task_req = 0;
1752 	struct scsi_lun fc_lun;
1753 	struct fnic_stats *fnic_stats;
1754 	struct abort_stats *abts_stats;
1755 	struct terminate_stats *term_stats;
1756 	enum fnic_ioreq_state old_ioreq_state;
1757 	int tag;
1758 	DECLARE_COMPLETION_ONSTACK(tm_done);
1759 
1760 	/* Wait for rport to unblock */
1761 	fc_block_scsi_eh(sc);
1762 
1763 	/* Get local-port, check ready and link up */
1764 	lp = shost_priv(sc->device->host);
1765 
1766 	fnic = lport_priv(lp);
1767 	fnic_stats = &fnic->fnic_stats;
1768 	abts_stats = &fnic->fnic_stats.abts_stats;
1769 	term_stats = &fnic->fnic_stats.term_stats;
1770 
1771 	rport = starget_to_rport(scsi_target(sc->device));
1772 	tag = sc->request->tag;
1773 	FNIC_SCSI_DBG(KERN_DEBUG,
1774 		fnic->lport->host,
1775 		"Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
1776 		rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1777 
1778 	CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1779 
1780 	if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1781 		ret = FAILED;
1782 		goto fnic_abort_cmd_end;
1783 	}
1784 
1785 	/*
1786 	 * Avoid a race between SCSI issuing the abort and the device
1787 	 * completing the command.
1788 	 *
1789 	 * If the command is already completed by the fw cmpl code,
1790 	 * we just return SUCCESS from here. This means that the abort
1791 	 * succeeded. In the SCSI ML, since the timeout for command has
1792 	 * happened, the completion wont actually complete the command
1793 	 * and it will be considered as an aborted command
1794 	 *
1795 	 * The CMD_SP will not be cleared except while holding io_req_lock.
1796 	 */
1797 	io_lock = fnic_io_lock_hash(fnic, sc);
1798 	spin_lock_irqsave(io_lock, flags);
1799 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1800 	if (!io_req) {
1801 		spin_unlock_irqrestore(io_lock, flags);
1802 		goto fnic_abort_cmd_end;
1803 	}
1804 
1805 	io_req->abts_done = &tm_done;
1806 
1807 	if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1808 		spin_unlock_irqrestore(io_lock, flags);
1809 		goto wait_pending;
1810 	}
1811 	/*
1812 	 * Command is still pending, need to abort it
1813 	 * If the firmware completes the command after this point,
1814 	 * the completion wont be done till mid-layer, since abort
1815 	 * has already started.
1816 	 */
1817 	old_ioreq_state = CMD_STATE(sc);
1818 	CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1819 	CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1820 
1821 	spin_unlock_irqrestore(io_lock, flags);
1822 
1823 	/*
1824 	 * Check readiness of the remote port. If the path to remote
1825 	 * port is up, then send abts to the remote port to terminate
1826 	 * the IO. Else, just locally terminate the IO in the firmware
1827 	 */
1828 	if (fc_remote_port_chkready(rport) == 0)
1829 		task_req = FCPIO_ITMF_ABT_TASK;
1830 	else {
1831 		atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
1832 		task_req = FCPIO_ITMF_ABT_TASK_TERM;
1833 	}
1834 
1835 	/* Now queue the abort command to firmware */
1836 	int_to_scsilun(sc->device->lun, &fc_lun);
1837 
1838 	if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1839 				    fc_lun.scsi_lun, io_req)) {
1840 		spin_lock_irqsave(io_lock, flags);
1841 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1842 			CMD_STATE(sc) = old_ioreq_state;
1843 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1844 		if (io_req)
1845 			io_req->abts_done = NULL;
1846 		spin_unlock_irqrestore(io_lock, flags);
1847 		ret = FAILED;
1848 		goto fnic_abort_cmd_end;
1849 	}
1850 	if (task_req == FCPIO_ITMF_ABT_TASK) {
1851 		CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1852 		atomic64_inc(&fnic_stats->abts_stats.aborts);
1853 	} else {
1854 		CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
1855 		atomic64_inc(&fnic_stats->term_stats.terminates);
1856 	}
1857 
1858 	/*
1859 	 * We queued an abort IO, wait for its completion.
1860 	 * Once the firmware completes the abort command, it will
1861 	 * wake up this thread.
1862 	 */
1863  wait_pending:
1864 	wait_for_completion_timeout(&tm_done,
1865 				    msecs_to_jiffies
1866 				    (2 * fnic->config.ra_tov +
1867 				     fnic->config.ed_tov));
1868 
1869 	/* Check the abort status */
1870 	spin_lock_irqsave(io_lock, flags);
1871 
1872 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1873 	if (!io_req) {
1874 		atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1875 		spin_unlock_irqrestore(io_lock, flags);
1876 		CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1877 		ret = FAILED;
1878 		goto fnic_abort_cmd_end;
1879 	}
1880 	io_req->abts_done = NULL;
1881 
1882 	/* fw did not complete abort, timed out */
1883 	if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1884 		spin_unlock_irqrestore(io_lock, flags);
1885 		if (task_req == FCPIO_ITMF_ABT_TASK) {
1886 			atomic64_inc(&abts_stats->abort_drv_timeouts);
1887 		} else {
1888 			atomic64_inc(&term_stats->terminate_drv_timeouts);
1889 		}
1890 		CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
1891 		ret = FAILED;
1892 		goto fnic_abort_cmd_end;
1893 	}
1894 
1895 	CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1896 
1897 	/*
1898 	 * firmware completed the abort, check the status,
1899 	 * free the io_req irrespective of failure or success
1900 	 */
1901 	if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1902 		ret = FAILED;
1903 
1904 	CMD_SP(sc) = NULL;
1905 
1906 	spin_unlock_irqrestore(io_lock, flags);
1907 
1908 	start_time = io_req->start_time;
1909 	fnic_release_ioreq_buf(fnic, io_req, sc);
1910 	mempool_free(io_req, fnic->io_req_pool);
1911 
1912 fnic_abort_cmd_end:
1913 	FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1914 		  sc->request->tag, sc,
1915 		  jiffies_to_msecs(jiffies - start_time),
1916 		  0, ((u64)sc->cmnd[0] << 32 |
1917 		  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1918 		  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1919 		  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1920 
1921 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1922 		      "Returning from abort cmd type %x %s\n", task_req,
1923 		      (ret == SUCCESS) ?
1924 		      "SUCCESS" : "FAILED");
1925 	return ret;
1926 }
1927 
1928 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1929 				       struct scsi_cmnd *sc,
1930 				       struct fnic_io_req *io_req)
1931 {
1932 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1933 	struct Scsi_Host *host = fnic->lport->host;
1934 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1935 	struct scsi_lun fc_lun;
1936 	int ret = 0;
1937 	unsigned long intr_flags;
1938 
1939 	spin_lock_irqsave(host->host_lock, intr_flags);
1940 	if (unlikely(fnic_chk_state_flags_locked(fnic,
1941 						FNIC_FLAGS_IO_BLOCKED))) {
1942 		spin_unlock_irqrestore(host->host_lock, intr_flags);
1943 		return FAILED;
1944 	} else
1945 		atomic_inc(&fnic->in_flight);
1946 	spin_unlock_irqrestore(host->host_lock, intr_flags);
1947 
1948 	spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1949 
1950 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1951 		free_wq_copy_descs(fnic, wq);
1952 
1953 	if (!vnic_wq_copy_desc_avail(wq)) {
1954 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1955 			  "queue_dr_io_req failure - no descriptors\n");
1956 		atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
1957 		ret = -EAGAIN;
1958 		goto lr_io_req_end;
1959 	}
1960 
1961 	/* fill in the lun info */
1962 	int_to_scsilun(sc->device->lun, &fc_lun);
1963 
1964 	fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1965 				     0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1966 				     fc_lun.scsi_lun, io_req->port_id,
1967 				     fnic->config.ra_tov, fnic->config.ed_tov);
1968 
1969 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1970 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1971 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1972 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1973 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1974 
1975 lr_io_req_end:
1976 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1977 	atomic_dec(&fnic->in_flight);
1978 
1979 	return ret;
1980 }
1981 
1982 /*
1983  * Clean up any pending aborts on the lun
1984  * For each outstanding IO on this lun, whose abort is not completed by fw,
1985  * issue a local abort. Wait for abort to complete. Return 0 if all commands
1986  * successfully aborted, 1 otherwise
1987  */
1988 static int fnic_clean_pending_aborts(struct fnic *fnic,
1989 				     struct scsi_cmnd *lr_sc)
1990 {
1991 	int tag, abt_tag;
1992 	struct fnic_io_req *io_req;
1993 	spinlock_t *io_lock;
1994 	unsigned long flags;
1995 	int ret = 0;
1996 	struct scsi_cmnd *sc;
1997 	struct scsi_lun fc_lun;
1998 	struct scsi_device *lun_dev = lr_sc->device;
1999 	DECLARE_COMPLETION_ONSTACK(tm_done);
2000 	enum fnic_ioreq_state old_ioreq_state;
2001 
2002 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2003 		io_lock = fnic_io_lock_tag(fnic, tag);
2004 		spin_lock_irqsave(io_lock, flags);
2005 		sc = scsi_host_find_tag(fnic->lport->host, tag);
2006 		/*
2007 		 * ignore this lun reset cmd or cmds that do not belong to
2008 		 * this lun
2009 		 */
2010 		if (!sc || sc == lr_sc || sc->device != lun_dev) {
2011 			spin_unlock_irqrestore(io_lock, flags);
2012 			continue;
2013 		}
2014 
2015 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2016 
2017 		if (!io_req || sc->device != lun_dev) {
2018 			spin_unlock_irqrestore(io_lock, flags);
2019 			continue;
2020 		}
2021 
2022 		/*
2023 		 * Found IO that is still pending with firmware and
2024 		 * belongs to the LUN that we are resetting
2025 		 */
2026 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2027 			      "Found IO in %s on lun\n",
2028 			      fnic_ioreq_state_to_str(CMD_STATE(sc)));
2029 
2030 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2031 			spin_unlock_irqrestore(io_lock, flags);
2032 			continue;
2033 		}
2034 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
2035 			(!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
2036 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2037 				"%s dev rst not pending sc 0x%p\n", __func__,
2038 				sc);
2039 			spin_unlock_irqrestore(io_lock, flags);
2040 			continue;
2041 		}
2042 
2043 		if (io_req->abts_done)
2044 			shost_printk(KERN_ERR, fnic->lport->host,
2045 			  "%s: io_req->abts_done is set state is %s\n",
2046 			  __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
2047 		old_ioreq_state = CMD_STATE(sc);
2048 		/*
2049 		 * Any pending IO issued prior to reset is expected to be
2050 		 * in abts pending state, if not we need to set
2051 		 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2052 		 * When IO is completed, the IO will be handed over and
2053 		 * handled in this function.
2054 		 */
2055 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2056 
2057 		BUG_ON(io_req->abts_done);
2058 
2059 		abt_tag = tag;
2060 		if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2061 			abt_tag |= FNIC_TAG_DEV_RST;
2062 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2063 				  "%s: dev rst sc 0x%p\n", __func__, sc);
2064 		}
2065 
2066 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2067 		io_req->abts_done = &tm_done;
2068 		spin_unlock_irqrestore(io_lock, flags);
2069 
2070 		/* Now queue the abort command to firmware */
2071 		int_to_scsilun(sc->device->lun, &fc_lun);
2072 
2073 		if (fnic_queue_abort_io_req(fnic, abt_tag,
2074 					    FCPIO_ITMF_ABT_TASK_TERM,
2075 					    fc_lun.scsi_lun, io_req)) {
2076 			spin_lock_irqsave(io_lock, flags);
2077 			io_req = (struct fnic_io_req *)CMD_SP(sc);
2078 			if (io_req)
2079 				io_req->abts_done = NULL;
2080 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2081 				CMD_STATE(sc) = old_ioreq_state;
2082 			spin_unlock_irqrestore(io_lock, flags);
2083 			ret = 1;
2084 			goto clean_pending_aborts_end;
2085 		} else {
2086 			spin_lock_irqsave(io_lock, flags);
2087 			if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2088 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2089 			spin_unlock_irqrestore(io_lock, flags);
2090 		}
2091 		CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
2092 
2093 		wait_for_completion_timeout(&tm_done,
2094 					    msecs_to_jiffies
2095 					    (fnic->config.ed_tov));
2096 
2097 		/* Recheck cmd state to check if it is now aborted */
2098 		spin_lock_irqsave(io_lock, flags);
2099 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2100 		if (!io_req) {
2101 			spin_unlock_irqrestore(io_lock, flags);
2102 			CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
2103 			continue;
2104 		}
2105 
2106 		io_req->abts_done = NULL;
2107 
2108 		/* if abort is still pending with fw, fail */
2109 		if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
2110 			spin_unlock_irqrestore(io_lock, flags);
2111 			CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
2112 			ret = 1;
2113 			goto clean_pending_aborts_end;
2114 		}
2115 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
2116 		CMD_SP(sc) = NULL;
2117 		spin_unlock_irqrestore(io_lock, flags);
2118 
2119 		fnic_release_ioreq_buf(fnic, io_req, sc);
2120 		mempool_free(io_req, fnic->io_req_pool);
2121 	}
2122 
2123 	schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2124 
2125 	/* walk again to check, if IOs are still pending in fw */
2126 	if (fnic_is_abts_pending(fnic, lr_sc))
2127 		ret = FAILED;
2128 
2129 clean_pending_aborts_end:
2130 	return ret;
2131 }
2132 
2133 /**
2134  * fnic_scsi_host_start_tag
2135  * Allocates tagid from host's tag list
2136  **/
2137 static inline int
2138 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2139 {
2140 	struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2141 	int tag, ret = SCSI_NO_TAG;
2142 
2143 	BUG_ON(!bqt);
2144 	if (!bqt) {
2145 		pr_err("Tags are not supported\n");
2146 		goto end;
2147 	}
2148 
2149 	do {
2150 		tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2151 		if (tag >= bqt->max_depth) {
2152 			pr_err("Tag allocation failure\n");
2153 			goto end;
2154 		}
2155 	} while (test_and_set_bit(tag, bqt->tag_map));
2156 
2157 	bqt->tag_index[tag] = sc->request;
2158 	sc->request->tag = tag;
2159 	sc->tag = tag;
2160 	if (!sc->request->special)
2161 		sc->request->special = sc;
2162 
2163 	ret = tag;
2164 
2165 end:
2166 	return ret;
2167 }
2168 
2169 /**
2170  * fnic_scsi_host_end_tag
2171  * frees tag allocated by fnic_scsi_host_start_tag.
2172  **/
2173 static inline void
2174 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2175 {
2176 	struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2177 	int tag = sc->request->tag;
2178 
2179 	if (tag == SCSI_NO_TAG)
2180 		return;
2181 
2182 	BUG_ON(!bqt || !bqt->tag_index[tag]);
2183 	if (!bqt)
2184 		return;
2185 
2186 	bqt->tag_index[tag] = NULL;
2187 	clear_bit(tag, bqt->tag_map);
2188 
2189 	return;
2190 }
2191 
2192 /*
2193  * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2194  * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2195  * on the LUN.
2196  */
2197 int fnic_device_reset(struct scsi_cmnd *sc)
2198 {
2199 	struct fc_lport *lp;
2200 	struct fnic *fnic;
2201 	struct fnic_io_req *io_req = NULL;
2202 	struct fc_rport *rport;
2203 	int status;
2204 	int ret = FAILED;
2205 	spinlock_t *io_lock;
2206 	unsigned long flags;
2207 	unsigned long start_time = 0;
2208 	struct scsi_lun fc_lun;
2209 	struct fnic_stats *fnic_stats;
2210 	struct reset_stats *reset_stats;
2211 	int tag = 0;
2212 	DECLARE_COMPLETION_ONSTACK(tm_done);
2213 	int tag_gen_flag = 0;   /*to track tags allocated by fnic driver*/
2214 
2215 	/* Wait for rport to unblock */
2216 	fc_block_scsi_eh(sc);
2217 
2218 	/* Get local-port, check ready and link up */
2219 	lp = shost_priv(sc->device->host);
2220 
2221 	fnic = lport_priv(lp);
2222 	fnic_stats = &fnic->fnic_stats;
2223 	reset_stats = &fnic->fnic_stats.reset_stats;
2224 
2225 	atomic64_inc(&reset_stats->device_resets);
2226 
2227 	rport = starget_to_rport(scsi_target(sc->device));
2228 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2229 		      "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
2230 		      rport->port_id, sc->device->lun, sc);
2231 
2232 	if (lp->state != LPORT_ST_READY || !(lp->link_up))
2233 		goto fnic_device_reset_end;
2234 
2235 	/* Check if remote port up */
2236 	if (fc_remote_port_chkready(rport)) {
2237 		atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2238 		goto fnic_device_reset_end;
2239 	}
2240 
2241 	CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2242 	/* Allocate tag if not present */
2243 
2244 	tag = sc->request->tag;
2245 	if (unlikely(tag < 0)) {
2246 		/*
2247 		 * XXX(hch): current the midlayer fakes up a struct
2248 		 * request for the explicit reset ioctls, and those
2249 		 * don't have a tag allocated to them.  The below
2250 		 * code pokes into midlayer structures to paper over
2251 		 * this design issue, but that won't work for blk-mq.
2252 		 *
2253 		 * Either someone who can actually test the hardware
2254 		 * will have to come up with a similar hack for the
2255 		 * blk-mq case, or we'll have to bite the bullet and
2256 		 * fix the way the EH ioctls work for real, but until
2257 		 * that happens we fail these explicit requests here.
2258 		 */
2259 		if (shost_use_blk_mq(sc->device->host))
2260 			goto fnic_device_reset_end;
2261 
2262 		tag = fnic_scsi_host_start_tag(fnic, sc);
2263 		if (unlikely(tag == SCSI_NO_TAG))
2264 			goto fnic_device_reset_end;
2265 		tag_gen_flag = 1;
2266 	}
2267 	io_lock = fnic_io_lock_hash(fnic, sc);
2268 	spin_lock_irqsave(io_lock, flags);
2269 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2270 
2271 	/*
2272 	 * If there is a io_req attached to this command, then use it,
2273 	 * else allocate a new one.
2274 	 */
2275 	if (!io_req) {
2276 		io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2277 		if (!io_req) {
2278 			spin_unlock_irqrestore(io_lock, flags);
2279 			goto fnic_device_reset_end;
2280 		}
2281 		memset(io_req, 0, sizeof(*io_req));
2282 		io_req->port_id = rport->port_id;
2283 		CMD_SP(sc) = (char *)io_req;
2284 	}
2285 	io_req->dr_done = &tm_done;
2286 	CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2287 	CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2288 	spin_unlock_irqrestore(io_lock, flags);
2289 
2290 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2291 
2292 	/*
2293 	 * issue the device reset, if enqueue failed, clean up the ioreq
2294 	 * and break assoc with scsi cmd
2295 	 */
2296 	if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2297 		spin_lock_irqsave(io_lock, flags);
2298 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2299 		if (io_req)
2300 			io_req->dr_done = NULL;
2301 		goto fnic_device_reset_clean;
2302 	}
2303 	spin_lock_irqsave(io_lock, flags);
2304 	CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2305 	spin_unlock_irqrestore(io_lock, flags);
2306 
2307 	/*
2308 	 * Wait on the local completion for LUN reset.  The io_req may be
2309 	 * freed while we wait since we hold no lock.
2310 	 */
2311 	wait_for_completion_timeout(&tm_done,
2312 				    msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2313 
2314 	spin_lock_irqsave(io_lock, flags);
2315 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2316 	if (!io_req) {
2317 		spin_unlock_irqrestore(io_lock, flags);
2318 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2319 				"io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2320 		goto fnic_device_reset_end;
2321 	}
2322 	io_req->dr_done = NULL;
2323 
2324 	status = CMD_LR_STATUS(sc);
2325 
2326 	/*
2327 	 * If lun reset not completed, bail out with failed. io_req
2328 	 * gets cleaned up during higher levels of EH
2329 	 */
2330 	if (status == FCPIO_INVALID_CODE) {
2331 		atomic64_inc(&reset_stats->device_reset_timeouts);
2332 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2333 			      "Device reset timed out\n");
2334 		CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2335 		spin_unlock_irqrestore(io_lock, flags);
2336 		int_to_scsilun(sc->device->lun, &fc_lun);
2337 		/*
2338 		 * Issue abort and terminate on device reset request.
2339 		 * If q'ing of terminate fails, retry it after a delay.
2340 		 */
2341 		while (1) {
2342 			spin_lock_irqsave(io_lock, flags);
2343 			if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2344 				spin_unlock_irqrestore(io_lock, flags);
2345 				break;
2346 			}
2347 			spin_unlock_irqrestore(io_lock, flags);
2348 			if (fnic_queue_abort_io_req(fnic,
2349 				tag | FNIC_TAG_DEV_RST,
2350 				FCPIO_ITMF_ABT_TASK_TERM,
2351 				fc_lun.scsi_lun, io_req)) {
2352 				wait_for_completion_timeout(&tm_done,
2353 				msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2354 			} else {
2355 				spin_lock_irqsave(io_lock, flags);
2356 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2357 				CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2358 				io_req->abts_done = &tm_done;
2359 				spin_unlock_irqrestore(io_lock, flags);
2360 				FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2361 				"Abort and terminate issued on Device reset "
2362 				"tag 0x%x sc 0x%p\n", tag, sc);
2363 				break;
2364 			}
2365 		}
2366 		while (1) {
2367 			spin_lock_irqsave(io_lock, flags);
2368 			if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2369 				spin_unlock_irqrestore(io_lock, flags);
2370 				wait_for_completion_timeout(&tm_done,
2371 				msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2372 				break;
2373 			} else {
2374 				io_req = (struct fnic_io_req *)CMD_SP(sc);
2375 				io_req->abts_done = NULL;
2376 				goto fnic_device_reset_clean;
2377 			}
2378 		}
2379 	} else {
2380 		spin_unlock_irqrestore(io_lock, flags);
2381 	}
2382 
2383 	/* Completed, but not successful, clean up the io_req, return fail */
2384 	if (status != FCPIO_SUCCESS) {
2385 		spin_lock_irqsave(io_lock, flags);
2386 		FNIC_SCSI_DBG(KERN_DEBUG,
2387 			      fnic->lport->host,
2388 			      "Device reset completed - failed\n");
2389 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2390 		goto fnic_device_reset_clean;
2391 	}
2392 
2393 	/*
2394 	 * Clean up any aborts on this lun that have still not
2395 	 * completed. If any of these fail, then LUN reset fails.
2396 	 * clean_pending_aborts cleans all cmds on this lun except
2397 	 * the lun reset cmd. If all cmds get cleaned, the lun reset
2398 	 * succeeds
2399 	 */
2400 	if (fnic_clean_pending_aborts(fnic, sc)) {
2401 		spin_lock_irqsave(io_lock, flags);
2402 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2403 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2404 			      "Device reset failed"
2405 			      " since could not abort all IOs\n");
2406 		goto fnic_device_reset_clean;
2407 	}
2408 
2409 	/* Clean lun reset command */
2410 	spin_lock_irqsave(io_lock, flags);
2411 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2412 	if (io_req)
2413 		/* Completed, and successful */
2414 		ret = SUCCESS;
2415 
2416 fnic_device_reset_clean:
2417 	if (io_req)
2418 		CMD_SP(sc) = NULL;
2419 
2420 	spin_unlock_irqrestore(io_lock, flags);
2421 
2422 	if (io_req) {
2423 		start_time = io_req->start_time;
2424 		fnic_release_ioreq_buf(fnic, io_req, sc);
2425 		mempool_free(io_req, fnic->io_req_pool);
2426 	}
2427 
2428 fnic_device_reset_end:
2429 	FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2430 		  sc->request->tag, sc,
2431 		  jiffies_to_msecs(jiffies - start_time),
2432 		  0, ((u64)sc->cmnd[0] << 32 |
2433 		  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2434 		  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2435 		  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2436 
2437 	/* free tag if it is allocated */
2438 	if (unlikely(tag_gen_flag))
2439 		fnic_scsi_host_end_tag(fnic, sc);
2440 
2441 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2442 		      "Returning from device reset %s\n",
2443 		      (ret == SUCCESS) ?
2444 		      "SUCCESS" : "FAILED");
2445 
2446 	if (ret == FAILED)
2447 		atomic64_inc(&reset_stats->device_reset_failures);
2448 
2449 	return ret;
2450 }
2451 
2452 /* Clean up all IOs, clean up libFC local port */
2453 int fnic_reset(struct Scsi_Host *shost)
2454 {
2455 	struct fc_lport *lp;
2456 	struct fnic *fnic;
2457 	int ret = 0;
2458 	struct reset_stats *reset_stats;
2459 
2460 	lp = shost_priv(shost);
2461 	fnic = lport_priv(lp);
2462 	reset_stats = &fnic->fnic_stats.reset_stats;
2463 
2464 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2465 		      "fnic_reset called\n");
2466 
2467 	atomic64_inc(&reset_stats->fnic_resets);
2468 
2469 	/*
2470 	 * Reset local port, this will clean up libFC exchanges,
2471 	 * reset remote port sessions, and if link is up, begin flogi
2472 	 */
2473 	ret = lp->tt.lport_reset(lp);
2474 
2475 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2476 		      "Returning from fnic reset %s\n",
2477 		      (ret == 0) ?
2478 		      "SUCCESS" : "FAILED");
2479 
2480 	if (ret == 0)
2481 		atomic64_inc(&reset_stats->fnic_reset_completions);
2482 	else
2483 		atomic64_inc(&reset_stats->fnic_reset_failures);
2484 
2485 	return ret;
2486 }
2487 
2488 /*
2489  * SCSI Error handling calls driver's eh_host_reset if all prior
2490  * error handling levels return FAILED. If host reset completes
2491  * successfully, and if link is up, then Fabric login begins.
2492  *
2493  * Host Reset is the highest level of error recovery. If this fails, then
2494  * host is offlined by SCSI.
2495  *
2496  */
2497 int fnic_host_reset(struct scsi_cmnd *sc)
2498 {
2499 	int ret;
2500 	unsigned long wait_host_tmo;
2501 	struct Scsi_Host *shost = sc->device->host;
2502 	struct fc_lport *lp = shost_priv(shost);
2503 
2504 	/*
2505 	 * If fnic_reset is successful, wait for fabric login to complete
2506 	 * scsi-ml tries to send a TUR to every device if host reset is
2507 	 * successful, so before returning to scsi, fabric should be up
2508 	 */
2509 	ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2510 	if (ret == SUCCESS) {
2511 		wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2512 		ret = FAILED;
2513 		while (time_before(jiffies, wait_host_tmo)) {
2514 			if ((lp->state == LPORT_ST_READY) &&
2515 			    (lp->link_up)) {
2516 				ret = SUCCESS;
2517 				break;
2518 			}
2519 			ssleep(1);
2520 		}
2521 	}
2522 
2523 	return ret;
2524 }
2525 
2526 /*
2527  * This fxn is called from libFC when host is removed
2528  */
2529 void fnic_scsi_abort_io(struct fc_lport *lp)
2530 {
2531 	int err = 0;
2532 	unsigned long flags;
2533 	enum fnic_state old_state;
2534 	struct fnic *fnic = lport_priv(lp);
2535 	DECLARE_COMPLETION_ONSTACK(remove_wait);
2536 
2537 	/* Issue firmware reset for fnic, wait for reset to complete */
2538 retry_fw_reset:
2539 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2540 	if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2541 		/* fw reset is in progress, poll for its completion */
2542 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2543 		schedule_timeout(msecs_to_jiffies(100));
2544 		goto retry_fw_reset;
2545 	}
2546 
2547 	fnic->remove_wait = &remove_wait;
2548 	old_state = fnic->state;
2549 	fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2550 	fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2551 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2552 
2553 	err = fnic_fw_reset_handler(fnic);
2554 	if (err) {
2555 		spin_lock_irqsave(&fnic->fnic_lock, flags);
2556 		if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2557 			fnic->state = old_state;
2558 		fnic->remove_wait = NULL;
2559 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2560 		return;
2561 	}
2562 
2563 	/* Wait for firmware reset to complete */
2564 	wait_for_completion_timeout(&remove_wait,
2565 				    msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2566 
2567 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2568 	fnic->remove_wait = NULL;
2569 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2570 		      "fnic_scsi_abort_io %s\n",
2571 		      (fnic->state == FNIC_IN_ETH_MODE) ?
2572 		      "SUCCESS" : "FAILED");
2573 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2574 
2575 }
2576 
2577 /*
2578  * This fxn called from libFC to clean up driver IO state on link down
2579  */
2580 void fnic_scsi_cleanup(struct fc_lport *lp)
2581 {
2582 	unsigned long flags;
2583 	enum fnic_state old_state;
2584 	struct fnic *fnic = lport_priv(lp);
2585 
2586 	/* issue fw reset */
2587 retry_fw_reset:
2588 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2589 	if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2590 		/* fw reset is in progress, poll for its completion */
2591 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2592 		schedule_timeout(msecs_to_jiffies(100));
2593 		goto retry_fw_reset;
2594 	}
2595 	old_state = fnic->state;
2596 	fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2597 	fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2598 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2599 
2600 	if (fnic_fw_reset_handler(fnic)) {
2601 		spin_lock_irqsave(&fnic->fnic_lock, flags);
2602 		if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2603 			fnic->state = old_state;
2604 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2605 	}
2606 
2607 }
2608 
2609 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2610 {
2611 }
2612 
2613 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2614 {
2615 	struct fnic *fnic = lport_priv(lp);
2616 
2617 	/* Non-zero sid, nothing to do */
2618 	if (sid)
2619 		goto call_fc_exch_mgr_reset;
2620 
2621 	if (did) {
2622 		fnic_rport_exch_reset(fnic, did);
2623 		goto call_fc_exch_mgr_reset;
2624 	}
2625 
2626 	/*
2627 	 * sid = 0, did = 0
2628 	 * link down or device being removed
2629 	 */
2630 	if (!fnic->in_remove)
2631 		fnic_scsi_cleanup(lp);
2632 	else
2633 		fnic_scsi_abort_io(lp);
2634 
2635 	/* call libFC exch mgr reset to reset its exchanges */
2636 call_fc_exch_mgr_reset:
2637 	fc_exch_mgr_reset(lp, sid, did);
2638 
2639 }
2640 
2641 /*
2642  * fnic_is_abts_pending() is a helper function that
2643  * walks through tag map to check if there is any IOs pending,if there is one,
2644  * then it returns 1 (true), otherwise 0 (false)
2645  * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2646  * otherwise, it checks for all IOs.
2647  */
2648 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2649 {
2650 	int tag;
2651 	struct fnic_io_req *io_req;
2652 	spinlock_t *io_lock;
2653 	unsigned long flags;
2654 	int ret = 0;
2655 	struct scsi_cmnd *sc;
2656 	struct scsi_device *lun_dev = NULL;
2657 
2658 	if (lr_sc)
2659 		lun_dev = lr_sc->device;
2660 
2661 	/* walk again to check, if IOs are still pending in fw */
2662 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2663 		sc = scsi_host_find_tag(fnic->lport->host, tag);
2664 		/*
2665 		 * ignore this lun reset cmd or cmds that do not belong to
2666 		 * this lun
2667 		 */
2668 		if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2669 			continue;
2670 
2671 		io_lock = fnic_io_lock_hash(fnic, sc);
2672 		spin_lock_irqsave(io_lock, flags);
2673 
2674 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2675 
2676 		if (!io_req || sc->device != lun_dev) {
2677 			spin_unlock_irqrestore(io_lock, flags);
2678 			continue;
2679 		}
2680 
2681 		/*
2682 		 * Found IO that is still pending with firmware and
2683 		 * belongs to the LUN that we are resetting
2684 		 */
2685 		FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2686 			      "Found IO in %s on lun\n",
2687 			      fnic_ioreq_state_to_str(CMD_STATE(sc)));
2688 
2689 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2690 			ret = 1;
2691 		spin_unlock_irqrestore(io_lock, flags);
2692 	}
2693 
2694 	return ret;
2695 }
2696