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