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