xref: /linux/drivers/scsi/fnic/fnic_scsi.c (revision 93d90ad708b8da6efc0e487b66111aa9db7f70c7)
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 	/* IO out of order */
1896 
1897 	if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1898 		spin_unlock_irqrestore(io_lock, flags);
1899 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1900 			"Issuing Host reset due to out of order IO\n");
1901 
1902 		if (fnic_host_reset(sc) == FAILED) {
1903 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1904 				"fnic_host_reset failed.\n");
1905 		}
1906 		ret = FAILED;
1907 		goto fnic_abort_cmd_end;
1908 	}
1909 
1910 	CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1911 
1912 	/*
1913 	 * firmware completed the abort, check the status,
1914 	 * free the io_req irrespective of failure or success
1915 	 */
1916 	if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1917 		ret = FAILED;
1918 
1919 	CMD_SP(sc) = NULL;
1920 
1921 	spin_unlock_irqrestore(io_lock, flags);
1922 
1923 	start_time = io_req->start_time;
1924 	fnic_release_ioreq_buf(fnic, io_req, sc);
1925 	mempool_free(io_req, fnic->io_req_pool);
1926 
1927 fnic_abort_cmd_end:
1928 	FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
1929 		  sc->request->tag, sc,
1930 		  jiffies_to_msecs(jiffies - start_time),
1931 		  0, ((u64)sc->cmnd[0] << 32 |
1932 		  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1933 		  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1934 		  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1935 
1936 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1937 		      "Returning from abort cmd type %x %s\n", task_req,
1938 		      (ret == SUCCESS) ?
1939 		      "SUCCESS" : "FAILED");
1940 	return ret;
1941 }
1942 
1943 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1944 				       struct scsi_cmnd *sc,
1945 				       struct fnic_io_req *io_req)
1946 {
1947 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1948 	struct Scsi_Host *host = fnic->lport->host;
1949 	struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1950 	struct scsi_lun fc_lun;
1951 	int ret = 0;
1952 	unsigned long intr_flags;
1953 
1954 	spin_lock_irqsave(host->host_lock, intr_flags);
1955 	if (unlikely(fnic_chk_state_flags_locked(fnic,
1956 						FNIC_FLAGS_IO_BLOCKED))) {
1957 		spin_unlock_irqrestore(host->host_lock, intr_flags);
1958 		return FAILED;
1959 	} else
1960 		atomic_inc(&fnic->in_flight);
1961 	spin_unlock_irqrestore(host->host_lock, intr_flags);
1962 
1963 	spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1964 
1965 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1966 		free_wq_copy_descs(fnic, wq);
1967 
1968 	if (!vnic_wq_copy_desc_avail(wq)) {
1969 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1970 			  "queue_dr_io_req failure - no descriptors\n");
1971 		atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
1972 		ret = -EAGAIN;
1973 		goto lr_io_req_end;
1974 	}
1975 
1976 	/* fill in the lun info */
1977 	int_to_scsilun(sc->device->lun, &fc_lun);
1978 
1979 	fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1980 				     0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1981 				     fc_lun.scsi_lun, io_req->port_id,
1982 				     fnic->config.ra_tov, fnic->config.ed_tov);
1983 
1984 	atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1985 	if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1986 		  atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1987 		atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1988 		  atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1989 
1990 lr_io_req_end:
1991 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1992 	atomic_dec(&fnic->in_flight);
1993 
1994 	return ret;
1995 }
1996 
1997 /*
1998  * Clean up any pending aborts on the lun
1999  * For each outstanding IO on this lun, whose abort is not completed by fw,
2000  * issue a local abort. Wait for abort to complete. Return 0 if all commands
2001  * successfully aborted, 1 otherwise
2002  */
2003 static int fnic_clean_pending_aborts(struct fnic *fnic,
2004 				     struct scsi_cmnd *lr_sc)
2005 {
2006 	int tag, abt_tag;
2007 	struct fnic_io_req *io_req;
2008 	spinlock_t *io_lock;
2009 	unsigned long flags;
2010 	int ret = 0;
2011 	struct scsi_cmnd *sc;
2012 	struct scsi_lun fc_lun;
2013 	struct scsi_device *lun_dev = lr_sc->device;
2014 	DECLARE_COMPLETION_ONSTACK(tm_done);
2015 	enum fnic_ioreq_state old_ioreq_state;
2016 
2017 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2018 		io_lock = fnic_io_lock_tag(fnic, tag);
2019 		spin_lock_irqsave(io_lock, flags);
2020 		sc = scsi_host_find_tag(fnic->lport->host, tag);
2021 		/*
2022 		 * ignore this lun reset cmd or cmds that do not belong to
2023 		 * this lun
2024 		 */
2025 		if (!sc || sc == lr_sc || sc->device != lun_dev) {
2026 			spin_unlock_irqrestore(io_lock, flags);
2027 			continue;
2028 		}
2029 
2030 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2031 
2032 		if (!io_req || sc->device != lun_dev) {
2033 			spin_unlock_irqrestore(io_lock, flags);
2034 			continue;
2035 		}
2036 
2037 		/*
2038 		 * Found IO that is still pending with firmware and
2039 		 * belongs to the LUN that we are resetting
2040 		 */
2041 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2042 			      "Found IO in %s on lun\n",
2043 			      fnic_ioreq_state_to_str(CMD_STATE(sc)));
2044 
2045 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2046 			spin_unlock_irqrestore(io_lock, flags);
2047 			continue;
2048 		}
2049 		if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
2050 			(!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
2051 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2052 				"%s dev rst not pending sc 0x%p\n", __func__,
2053 				sc);
2054 			spin_unlock_irqrestore(io_lock, flags);
2055 			continue;
2056 		}
2057 
2058 		if (io_req->abts_done)
2059 			shost_printk(KERN_ERR, fnic->lport->host,
2060 			  "%s: io_req->abts_done is set state is %s\n",
2061 			  __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
2062 		old_ioreq_state = CMD_STATE(sc);
2063 		/*
2064 		 * Any pending IO issued prior to reset is expected to be
2065 		 * in abts pending state, if not we need to set
2066 		 * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2067 		 * When IO is completed, the IO will be handed over and
2068 		 * handled in this function.
2069 		 */
2070 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2071 
2072 		BUG_ON(io_req->abts_done);
2073 
2074 		abt_tag = tag;
2075 		if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2076 			abt_tag |= FNIC_TAG_DEV_RST;
2077 			FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2078 				  "%s: dev rst sc 0x%p\n", __func__, sc);
2079 		}
2080 
2081 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2082 		io_req->abts_done = &tm_done;
2083 		spin_unlock_irqrestore(io_lock, flags);
2084 
2085 		/* Now queue the abort command to firmware */
2086 		int_to_scsilun(sc->device->lun, &fc_lun);
2087 
2088 		if (fnic_queue_abort_io_req(fnic, abt_tag,
2089 					    FCPIO_ITMF_ABT_TASK_TERM,
2090 					    fc_lun.scsi_lun, io_req)) {
2091 			spin_lock_irqsave(io_lock, flags);
2092 			io_req = (struct fnic_io_req *)CMD_SP(sc);
2093 			if (io_req)
2094 				io_req->abts_done = NULL;
2095 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2096 				CMD_STATE(sc) = old_ioreq_state;
2097 			spin_unlock_irqrestore(io_lock, flags);
2098 			ret = 1;
2099 			goto clean_pending_aborts_end;
2100 		} else {
2101 			spin_lock_irqsave(io_lock, flags);
2102 			if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2103 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2104 			spin_unlock_irqrestore(io_lock, flags);
2105 		}
2106 		CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
2107 
2108 		wait_for_completion_timeout(&tm_done,
2109 					    msecs_to_jiffies
2110 					    (fnic->config.ed_tov));
2111 
2112 		/* Recheck cmd state to check if it is now aborted */
2113 		spin_lock_irqsave(io_lock, flags);
2114 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2115 		if (!io_req) {
2116 			spin_unlock_irqrestore(io_lock, flags);
2117 			CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
2118 			continue;
2119 		}
2120 
2121 		io_req->abts_done = NULL;
2122 
2123 		/* if abort is still pending with fw, fail */
2124 		if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
2125 			spin_unlock_irqrestore(io_lock, flags);
2126 			CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
2127 			ret = 1;
2128 			goto clean_pending_aborts_end;
2129 		}
2130 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
2131 		CMD_SP(sc) = NULL;
2132 		spin_unlock_irqrestore(io_lock, flags);
2133 
2134 		fnic_release_ioreq_buf(fnic, io_req, sc);
2135 		mempool_free(io_req, fnic->io_req_pool);
2136 	}
2137 
2138 	schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2139 
2140 	/* walk again to check, if IOs are still pending in fw */
2141 	if (fnic_is_abts_pending(fnic, lr_sc))
2142 		ret = FAILED;
2143 
2144 clean_pending_aborts_end:
2145 	return ret;
2146 }
2147 
2148 /**
2149  * fnic_scsi_host_start_tag
2150  * Allocates tagid from host's tag list
2151  **/
2152 static inline int
2153 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2154 {
2155 	struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2156 	int tag, ret = SCSI_NO_TAG;
2157 
2158 	BUG_ON(!bqt);
2159 	if (!bqt) {
2160 		pr_err("Tags are not supported\n");
2161 		goto end;
2162 	}
2163 
2164 	do {
2165 		tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2166 		if (tag >= bqt->max_depth) {
2167 			pr_err("Tag allocation failure\n");
2168 			goto end;
2169 		}
2170 	} while (test_and_set_bit(tag, bqt->tag_map));
2171 
2172 	bqt->tag_index[tag] = sc->request;
2173 	sc->request->tag = tag;
2174 	sc->tag = tag;
2175 	if (!sc->request->special)
2176 		sc->request->special = sc;
2177 
2178 	ret = tag;
2179 
2180 end:
2181 	return ret;
2182 }
2183 
2184 /**
2185  * fnic_scsi_host_end_tag
2186  * frees tag allocated by fnic_scsi_host_start_tag.
2187  **/
2188 static inline void
2189 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2190 {
2191 	struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2192 	int tag = sc->request->tag;
2193 
2194 	if (tag == SCSI_NO_TAG)
2195 		return;
2196 
2197 	BUG_ON(!bqt || !bqt->tag_index[tag]);
2198 	if (!bqt)
2199 		return;
2200 
2201 	bqt->tag_index[tag] = NULL;
2202 	clear_bit(tag, bqt->tag_map);
2203 
2204 	return;
2205 }
2206 
2207 /*
2208  * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2209  * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2210  * on the LUN.
2211  */
2212 int fnic_device_reset(struct scsi_cmnd *sc)
2213 {
2214 	struct fc_lport *lp;
2215 	struct fnic *fnic;
2216 	struct fnic_io_req *io_req = NULL;
2217 	struct fc_rport *rport;
2218 	int status;
2219 	int ret = FAILED;
2220 	spinlock_t *io_lock;
2221 	unsigned long flags;
2222 	unsigned long start_time = 0;
2223 	struct scsi_lun fc_lun;
2224 	struct fnic_stats *fnic_stats;
2225 	struct reset_stats *reset_stats;
2226 	int tag = 0;
2227 	DECLARE_COMPLETION_ONSTACK(tm_done);
2228 	int tag_gen_flag = 0;   /*to track tags allocated by fnic driver*/
2229 
2230 	/* Wait for rport to unblock */
2231 	fc_block_scsi_eh(sc);
2232 
2233 	/* Get local-port, check ready and link up */
2234 	lp = shost_priv(sc->device->host);
2235 
2236 	fnic = lport_priv(lp);
2237 	fnic_stats = &fnic->fnic_stats;
2238 	reset_stats = &fnic->fnic_stats.reset_stats;
2239 
2240 	atomic64_inc(&reset_stats->device_resets);
2241 
2242 	rport = starget_to_rport(scsi_target(sc->device));
2243 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2244 		      "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
2245 		      rport->port_id, sc->device->lun, sc);
2246 
2247 	if (lp->state != LPORT_ST_READY || !(lp->link_up))
2248 		goto fnic_device_reset_end;
2249 
2250 	/* Check if remote port up */
2251 	if (fc_remote_port_chkready(rport)) {
2252 		atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2253 		goto fnic_device_reset_end;
2254 	}
2255 
2256 	CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2257 	/* Allocate tag if not present */
2258 
2259 	tag = sc->request->tag;
2260 	if (unlikely(tag < 0)) {
2261 		/*
2262 		 * XXX(hch): current the midlayer fakes up a struct
2263 		 * request for the explicit reset ioctls, and those
2264 		 * don't have a tag allocated to them.  The below
2265 		 * code pokes into midlayer structures to paper over
2266 		 * this design issue, but that won't work for blk-mq.
2267 		 *
2268 		 * Either someone who can actually test the hardware
2269 		 * will have to come up with a similar hack for the
2270 		 * blk-mq case, or we'll have to bite the bullet and
2271 		 * fix the way the EH ioctls work for real, but until
2272 		 * that happens we fail these explicit requests here.
2273 		 */
2274 		if (shost_use_blk_mq(sc->device->host))
2275 			goto fnic_device_reset_end;
2276 
2277 		tag = fnic_scsi_host_start_tag(fnic, sc);
2278 		if (unlikely(tag == SCSI_NO_TAG))
2279 			goto fnic_device_reset_end;
2280 		tag_gen_flag = 1;
2281 	}
2282 	io_lock = fnic_io_lock_hash(fnic, sc);
2283 	spin_lock_irqsave(io_lock, flags);
2284 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2285 
2286 	/*
2287 	 * If there is a io_req attached to this command, then use it,
2288 	 * else allocate a new one.
2289 	 */
2290 	if (!io_req) {
2291 		io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2292 		if (!io_req) {
2293 			spin_unlock_irqrestore(io_lock, flags);
2294 			goto fnic_device_reset_end;
2295 		}
2296 		memset(io_req, 0, sizeof(*io_req));
2297 		io_req->port_id = rport->port_id;
2298 		CMD_SP(sc) = (char *)io_req;
2299 	}
2300 	io_req->dr_done = &tm_done;
2301 	CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2302 	CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2303 	spin_unlock_irqrestore(io_lock, flags);
2304 
2305 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2306 
2307 	/*
2308 	 * issue the device reset, if enqueue failed, clean up the ioreq
2309 	 * and break assoc with scsi cmd
2310 	 */
2311 	if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2312 		spin_lock_irqsave(io_lock, flags);
2313 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2314 		if (io_req)
2315 			io_req->dr_done = NULL;
2316 		goto fnic_device_reset_clean;
2317 	}
2318 	spin_lock_irqsave(io_lock, flags);
2319 	CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2320 	spin_unlock_irqrestore(io_lock, flags);
2321 
2322 	/*
2323 	 * Wait on the local completion for LUN reset.  The io_req may be
2324 	 * freed while we wait since we hold no lock.
2325 	 */
2326 	wait_for_completion_timeout(&tm_done,
2327 				    msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2328 
2329 	spin_lock_irqsave(io_lock, flags);
2330 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2331 	if (!io_req) {
2332 		spin_unlock_irqrestore(io_lock, flags);
2333 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2334 				"io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2335 		goto fnic_device_reset_end;
2336 	}
2337 	io_req->dr_done = NULL;
2338 
2339 	status = CMD_LR_STATUS(sc);
2340 
2341 	/*
2342 	 * If lun reset not completed, bail out with failed. io_req
2343 	 * gets cleaned up during higher levels of EH
2344 	 */
2345 	if (status == FCPIO_INVALID_CODE) {
2346 		atomic64_inc(&reset_stats->device_reset_timeouts);
2347 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2348 			      "Device reset timed out\n");
2349 		CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2350 		spin_unlock_irqrestore(io_lock, flags);
2351 		int_to_scsilun(sc->device->lun, &fc_lun);
2352 		/*
2353 		 * Issue abort and terminate on device reset request.
2354 		 * If q'ing of terminate fails, retry it after a delay.
2355 		 */
2356 		while (1) {
2357 			spin_lock_irqsave(io_lock, flags);
2358 			if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2359 				spin_unlock_irqrestore(io_lock, flags);
2360 				break;
2361 			}
2362 			spin_unlock_irqrestore(io_lock, flags);
2363 			if (fnic_queue_abort_io_req(fnic,
2364 				tag | FNIC_TAG_DEV_RST,
2365 				FCPIO_ITMF_ABT_TASK_TERM,
2366 				fc_lun.scsi_lun, io_req)) {
2367 				wait_for_completion_timeout(&tm_done,
2368 				msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2369 			} else {
2370 				spin_lock_irqsave(io_lock, flags);
2371 				CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2372 				CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2373 				io_req->abts_done = &tm_done;
2374 				spin_unlock_irqrestore(io_lock, flags);
2375 				FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2376 				"Abort and terminate issued on Device reset "
2377 				"tag 0x%x sc 0x%p\n", tag, sc);
2378 				break;
2379 			}
2380 		}
2381 		while (1) {
2382 			spin_lock_irqsave(io_lock, flags);
2383 			if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2384 				spin_unlock_irqrestore(io_lock, flags);
2385 				wait_for_completion_timeout(&tm_done,
2386 				msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2387 				break;
2388 			} else {
2389 				io_req = (struct fnic_io_req *)CMD_SP(sc);
2390 				io_req->abts_done = NULL;
2391 				goto fnic_device_reset_clean;
2392 			}
2393 		}
2394 	} else {
2395 		spin_unlock_irqrestore(io_lock, flags);
2396 	}
2397 
2398 	/* Completed, but not successful, clean up the io_req, return fail */
2399 	if (status != FCPIO_SUCCESS) {
2400 		spin_lock_irqsave(io_lock, flags);
2401 		FNIC_SCSI_DBG(KERN_DEBUG,
2402 			      fnic->lport->host,
2403 			      "Device reset completed - failed\n");
2404 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2405 		goto fnic_device_reset_clean;
2406 	}
2407 
2408 	/*
2409 	 * Clean up any aborts on this lun that have still not
2410 	 * completed. If any of these fail, then LUN reset fails.
2411 	 * clean_pending_aborts cleans all cmds on this lun except
2412 	 * the lun reset cmd. If all cmds get cleaned, the lun reset
2413 	 * succeeds
2414 	 */
2415 	if (fnic_clean_pending_aborts(fnic, sc)) {
2416 		spin_lock_irqsave(io_lock, flags);
2417 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2418 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2419 			      "Device reset failed"
2420 			      " since could not abort all IOs\n");
2421 		goto fnic_device_reset_clean;
2422 	}
2423 
2424 	/* Clean lun reset command */
2425 	spin_lock_irqsave(io_lock, flags);
2426 	io_req = (struct fnic_io_req *)CMD_SP(sc);
2427 	if (io_req)
2428 		/* Completed, and successful */
2429 		ret = SUCCESS;
2430 
2431 fnic_device_reset_clean:
2432 	if (io_req)
2433 		CMD_SP(sc) = NULL;
2434 
2435 	spin_unlock_irqrestore(io_lock, flags);
2436 
2437 	if (io_req) {
2438 		start_time = io_req->start_time;
2439 		fnic_release_ioreq_buf(fnic, io_req, sc);
2440 		mempool_free(io_req, fnic->io_req_pool);
2441 	}
2442 
2443 fnic_device_reset_end:
2444 	FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2445 		  sc->request->tag, sc,
2446 		  jiffies_to_msecs(jiffies - start_time),
2447 		  0, ((u64)sc->cmnd[0] << 32 |
2448 		  (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2449 		  (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2450 		  (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2451 
2452 	/* free tag if it is allocated */
2453 	if (unlikely(tag_gen_flag))
2454 		fnic_scsi_host_end_tag(fnic, sc);
2455 
2456 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2457 		      "Returning from device reset %s\n",
2458 		      (ret == SUCCESS) ?
2459 		      "SUCCESS" : "FAILED");
2460 
2461 	if (ret == FAILED)
2462 		atomic64_inc(&reset_stats->device_reset_failures);
2463 
2464 	return ret;
2465 }
2466 
2467 /* Clean up all IOs, clean up libFC local port */
2468 int fnic_reset(struct Scsi_Host *shost)
2469 {
2470 	struct fc_lport *lp;
2471 	struct fnic *fnic;
2472 	int ret = 0;
2473 	struct reset_stats *reset_stats;
2474 
2475 	lp = shost_priv(shost);
2476 	fnic = lport_priv(lp);
2477 	reset_stats = &fnic->fnic_stats.reset_stats;
2478 
2479 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2480 		      "fnic_reset called\n");
2481 
2482 	atomic64_inc(&reset_stats->fnic_resets);
2483 
2484 	/*
2485 	 * Reset local port, this will clean up libFC exchanges,
2486 	 * reset remote port sessions, and if link is up, begin flogi
2487 	 */
2488 	ret = lp->tt.lport_reset(lp);
2489 
2490 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2491 		      "Returning from fnic reset %s\n",
2492 		      (ret == 0) ?
2493 		      "SUCCESS" : "FAILED");
2494 
2495 	if (ret == 0)
2496 		atomic64_inc(&reset_stats->fnic_reset_completions);
2497 	else
2498 		atomic64_inc(&reset_stats->fnic_reset_failures);
2499 
2500 	return ret;
2501 }
2502 
2503 /*
2504  * SCSI Error handling calls driver's eh_host_reset if all prior
2505  * error handling levels return FAILED. If host reset completes
2506  * successfully, and if link is up, then Fabric login begins.
2507  *
2508  * Host Reset is the highest level of error recovery. If this fails, then
2509  * host is offlined by SCSI.
2510  *
2511  */
2512 int fnic_host_reset(struct scsi_cmnd *sc)
2513 {
2514 	int ret;
2515 	unsigned long wait_host_tmo;
2516 	struct Scsi_Host *shost = sc->device->host;
2517 	struct fc_lport *lp = shost_priv(shost);
2518 
2519 	/*
2520 	 * If fnic_reset is successful, wait for fabric login to complete
2521 	 * scsi-ml tries to send a TUR to every device if host reset is
2522 	 * successful, so before returning to scsi, fabric should be up
2523 	 */
2524 	ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2525 	if (ret == SUCCESS) {
2526 		wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2527 		ret = FAILED;
2528 		while (time_before(jiffies, wait_host_tmo)) {
2529 			if ((lp->state == LPORT_ST_READY) &&
2530 			    (lp->link_up)) {
2531 				ret = SUCCESS;
2532 				break;
2533 			}
2534 			ssleep(1);
2535 		}
2536 	}
2537 
2538 	return ret;
2539 }
2540 
2541 /*
2542  * This fxn is called from libFC when host is removed
2543  */
2544 void fnic_scsi_abort_io(struct fc_lport *lp)
2545 {
2546 	int err = 0;
2547 	unsigned long flags;
2548 	enum fnic_state old_state;
2549 	struct fnic *fnic = lport_priv(lp);
2550 	DECLARE_COMPLETION_ONSTACK(remove_wait);
2551 
2552 	/* Issue firmware reset for fnic, wait for reset to complete */
2553 retry_fw_reset:
2554 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2555 	if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2556 		/* fw reset is in progress, poll for its completion */
2557 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2558 		schedule_timeout(msecs_to_jiffies(100));
2559 		goto retry_fw_reset;
2560 	}
2561 
2562 	fnic->remove_wait = &remove_wait;
2563 	old_state = fnic->state;
2564 	fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2565 	fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2566 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2567 
2568 	err = fnic_fw_reset_handler(fnic);
2569 	if (err) {
2570 		spin_lock_irqsave(&fnic->fnic_lock, flags);
2571 		if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2572 			fnic->state = old_state;
2573 		fnic->remove_wait = NULL;
2574 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2575 		return;
2576 	}
2577 
2578 	/* Wait for firmware reset to complete */
2579 	wait_for_completion_timeout(&remove_wait,
2580 				    msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2581 
2582 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2583 	fnic->remove_wait = NULL;
2584 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2585 		      "fnic_scsi_abort_io %s\n",
2586 		      (fnic->state == FNIC_IN_ETH_MODE) ?
2587 		      "SUCCESS" : "FAILED");
2588 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2589 
2590 }
2591 
2592 /*
2593  * This fxn called from libFC to clean up driver IO state on link down
2594  */
2595 void fnic_scsi_cleanup(struct fc_lport *lp)
2596 {
2597 	unsigned long flags;
2598 	enum fnic_state old_state;
2599 	struct fnic *fnic = lport_priv(lp);
2600 
2601 	/* issue fw reset */
2602 retry_fw_reset:
2603 	spin_lock_irqsave(&fnic->fnic_lock, flags);
2604 	if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2605 		/* fw reset is in progress, poll for its completion */
2606 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2607 		schedule_timeout(msecs_to_jiffies(100));
2608 		goto retry_fw_reset;
2609 	}
2610 	old_state = fnic->state;
2611 	fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2612 	fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2613 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2614 
2615 	if (fnic_fw_reset_handler(fnic)) {
2616 		spin_lock_irqsave(&fnic->fnic_lock, flags);
2617 		if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2618 			fnic->state = old_state;
2619 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2620 	}
2621 
2622 }
2623 
2624 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2625 {
2626 }
2627 
2628 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2629 {
2630 	struct fnic *fnic = lport_priv(lp);
2631 
2632 	/* Non-zero sid, nothing to do */
2633 	if (sid)
2634 		goto call_fc_exch_mgr_reset;
2635 
2636 	if (did) {
2637 		fnic_rport_exch_reset(fnic, did);
2638 		goto call_fc_exch_mgr_reset;
2639 	}
2640 
2641 	/*
2642 	 * sid = 0, did = 0
2643 	 * link down or device being removed
2644 	 */
2645 	if (!fnic->in_remove)
2646 		fnic_scsi_cleanup(lp);
2647 	else
2648 		fnic_scsi_abort_io(lp);
2649 
2650 	/* call libFC exch mgr reset to reset its exchanges */
2651 call_fc_exch_mgr_reset:
2652 	fc_exch_mgr_reset(lp, sid, did);
2653 
2654 }
2655 
2656 /*
2657  * fnic_is_abts_pending() is a helper function that
2658  * walks through tag map to check if there is any IOs pending,if there is one,
2659  * then it returns 1 (true), otherwise 0 (false)
2660  * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2661  * otherwise, it checks for all IOs.
2662  */
2663 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2664 {
2665 	int tag;
2666 	struct fnic_io_req *io_req;
2667 	spinlock_t *io_lock;
2668 	unsigned long flags;
2669 	int ret = 0;
2670 	struct scsi_cmnd *sc;
2671 	struct scsi_device *lun_dev = NULL;
2672 
2673 	if (lr_sc)
2674 		lun_dev = lr_sc->device;
2675 
2676 	/* walk again to check, if IOs are still pending in fw */
2677 	for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2678 		sc = scsi_host_find_tag(fnic->lport->host, tag);
2679 		/*
2680 		 * ignore this lun reset cmd or cmds that do not belong to
2681 		 * this lun
2682 		 */
2683 		if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2684 			continue;
2685 
2686 		io_lock = fnic_io_lock_hash(fnic, sc);
2687 		spin_lock_irqsave(io_lock, flags);
2688 
2689 		io_req = (struct fnic_io_req *)CMD_SP(sc);
2690 
2691 		if (!io_req || sc->device != lun_dev) {
2692 			spin_unlock_irqrestore(io_lock, flags);
2693 			continue;
2694 		}
2695 
2696 		/*
2697 		 * Found IO that is still pending with firmware and
2698 		 * belongs to the LUN that we are resetting
2699 		 */
2700 		FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2701 			      "Found IO in %s on lun\n",
2702 			      fnic_ioreq_state_to_str(CMD_STATE(sc)));
2703 
2704 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2705 			ret = 1;
2706 		spin_unlock_irqrestore(io_lock, flags);
2707 	}
2708 
2709 	return ret;
2710 }
2711