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