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