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