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