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