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