xref: /openbmc/linux/drivers/scsi/fnic/fnic_scsi.c (revision b6dcefde)
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 <scsi/scsi.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/fc/fc_els.h>
35 #include <scsi/fc/fc_fcoe.h>
36 #include <scsi/libfc.h>
37 #include <scsi/fc_frame.h>
38 #include "fnic_io.h"
39 #include "fnic.h"
40 
41 const char *fnic_state_str[] = {
42 	[FNIC_IN_FC_MODE] =           "FNIC_IN_FC_MODE",
43 	[FNIC_IN_FC_TRANS_ETH_MODE] = "FNIC_IN_FC_TRANS_ETH_MODE",
44 	[FNIC_IN_ETH_MODE] =          "FNIC_IN_ETH_MODE",
45 	[FNIC_IN_ETH_TRANS_FC_MODE] = "FNIC_IN_ETH_TRANS_FC_MODE",
46 };
47 
48 static const char *fnic_ioreq_state_str[] = {
49 	[FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
50 	[FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
51 	[FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
52 	[FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
53 };
54 
55 static const char *fcpio_status_str[] =  {
56 	[FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
57 	[FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
58 	[FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
59 	[FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
60 	[FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
61 	[FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
62 	[FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
63 	[FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
64 	[FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
65 	[FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
66 	[FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
67 	[FCPIO_FW_ERR] = "FCPIO_FW_ERR",
68 	[FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
69 	[FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
70 	[FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
71 	[FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
72 	[FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
73 	[FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
74 	[FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
75 };
76 
77 const char *fnic_state_to_str(unsigned int state)
78 {
79 	if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
80 		return "unknown";
81 
82 	return fnic_state_str[state];
83 }
84 
85 static const char *fnic_ioreq_state_to_str(unsigned int state)
86 {
87 	if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
88 	    !fnic_ioreq_state_str[state])
89 		return "unknown";
90 
91 	return fnic_ioreq_state_str[state];
92 }
93 
94 static const char *fnic_fcpio_status_to_str(unsigned int status)
95 {
96 	if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
97 		return "unknown";
98 
99 	return fcpio_status_str[status];
100 }
101 
102 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
103 
104 static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
105 					    struct scsi_cmnd *sc)
106 {
107 	u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
108 
109 	return &fnic->io_req_lock[hash];
110 }
111 
112 /*
113  * Unmap the data buffer and sense buffer for an io_req,
114  * also unmap and free the device-private scatter/gather list.
115  */
116 static void fnic_release_ioreq_buf(struct fnic *fnic,
117 				   struct fnic_io_req *io_req,
118 				   struct scsi_cmnd *sc)
119 {
120 	if (io_req->sgl_list_pa)
121 		pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
122 				 sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
123 				 PCI_DMA_TODEVICE);
124 	scsi_dma_unmap(sc);
125 
126 	if (io_req->sgl_cnt)
127 		mempool_free(io_req->sgl_list_alloc,
128 			     fnic->io_sgl_pool[io_req->sgl_type]);
129 	if (io_req->sense_buf_pa)
130 		pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
131 				 SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
132 }
133 
134 /* Free up Copy Wq descriptors. Called with copy_wq lock held */
135 static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
136 {
137 	/* if no Ack received from firmware, then nothing to clean */
138 	if (!fnic->fw_ack_recd[0])
139 		return 1;
140 
141 	/*
142 	 * Update desc_available count based on number of freed descriptors
143 	 * Account for wraparound
144 	 */
145 	if (wq->to_clean_index <= fnic->fw_ack_index[0])
146 		wq->ring.desc_avail += (fnic->fw_ack_index[0]
147 					- wq->to_clean_index + 1);
148 	else
149 		wq->ring.desc_avail += (wq->ring.desc_count
150 					- wq->to_clean_index
151 					+ fnic->fw_ack_index[0] + 1);
152 
153 	/*
154 	 * just bump clean index to ack_index+1 accounting for wraparound
155 	 * this will essentially free up all descriptors between
156 	 * to_clean_index and fw_ack_index, both inclusive
157 	 */
158 	wq->to_clean_index =
159 		(fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
160 
161 	/* we have processed the acks received so far */
162 	fnic->fw_ack_recd[0] = 0;
163 	return 0;
164 }
165 
166 
167 /*
168  * fnic_fw_reset_handler
169  * Routine to send reset msg to fw
170  */
171 int fnic_fw_reset_handler(struct fnic *fnic)
172 {
173 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
174 	int ret = 0;
175 	unsigned long flags;
176 
177 	skb_queue_purge(&fnic->frame_queue);
178 	skb_queue_purge(&fnic->tx_queue);
179 
180 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
181 
182 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
183 		free_wq_copy_descs(fnic, wq);
184 
185 	if (!vnic_wq_copy_desc_avail(wq))
186 		ret = -EAGAIN;
187 	else
188 		fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
189 
190 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
191 
192 	if (!ret)
193 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
194 			      "Issued fw reset\n");
195 	else
196 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
197 			      "Failed to issue fw reset\n");
198 	return ret;
199 }
200 
201 
202 /*
203  * fnic_flogi_reg_handler
204  * Routine to send flogi register msg to fw
205  */
206 int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
207 {
208 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
209 	enum fcpio_flogi_reg_format_type format;
210 	struct fc_lport *lp = fnic->lport;
211 	u8 gw_mac[ETH_ALEN];
212 	int ret = 0;
213 	unsigned long flags;
214 
215 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
216 
217 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
218 		free_wq_copy_descs(fnic, wq);
219 
220 	if (!vnic_wq_copy_desc_avail(wq)) {
221 		ret = -EAGAIN;
222 		goto flogi_reg_ioreq_end;
223 	}
224 
225 	if (fnic->ctlr.map_dest) {
226 		memset(gw_mac, 0xff, ETH_ALEN);
227 		format = FCPIO_FLOGI_REG_DEF_DEST;
228 	} else {
229 		memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
230 		format = FCPIO_FLOGI_REG_GW_DEST;
231 	}
232 
233 	if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
234 		fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
235 						fc_id, gw_mac,
236 						fnic->data_src_addr,
237 						lp->r_a_tov, lp->e_d_tov);
238 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
239 			      "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
240 			      fc_id, fnic->data_src_addr, gw_mac);
241 	} else {
242 		fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
243 						  format, fc_id, gw_mac);
244 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
245 			      "FLOGI reg issued fcid %x map %d dest %pM\n",
246 			      fc_id, fnic->ctlr.map_dest, gw_mac);
247 	}
248 
249 flogi_reg_ioreq_end:
250 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
251 	return ret;
252 }
253 
254 /*
255  * fnic_queue_wq_copy_desc
256  * Routine to enqueue a wq copy desc
257  */
258 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
259 					  struct vnic_wq_copy *wq,
260 					  struct fnic_io_req *io_req,
261 					  struct scsi_cmnd *sc,
262 					  int sg_count)
263 {
264 	struct scatterlist *sg;
265 	struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
266 	struct fc_rport_libfc_priv *rp = rport->dd_data;
267 	struct host_sg_desc *desc;
268 	u8 pri_tag = 0;
269 	unsigned int i;
270 	unsigned long intr_flags;
271 	int flags;
272 	u8 exch_flags;
273 	struct scsi_lun fc_lun;
274 	char msg[2];
275 
276 	if (sg_count) {
277 		/* For each SGE, create a device desc entry */
278 		desc = io_req->sgl_list;
279 		for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
280 			desc->addr = cpu_to_le64(sg_dma_address(sg));
281 			desc->len = cpu_to_le32(sg_dma_len(sg));
282 			desc->_resvd = 0;
283 			desc++;
284 		}
285 
286 		io_req->sgl_list_pa = pci_map_single
287 			(fnic->pdev,
288 			 io_req->sgl_list,
289 			 sizeof(io_req->sgl_list[0]) * sg_count,
290 			 PCI_DMA_TODEVICE);
291 	}
292 
293 	io_req->sense_buf_pa = pci_map_single(fnic->pdev,
294 					      sc->sense_buffer,
295 					      SCSI_SENSE_BUFFERSIZE,
296 					      PCI_DMA_FROMDEVICE);
297 
298 	int_to_scsilun(sc->device->lun, &fc_lun);
299 
300 	pri_tag = FCPIO_ICMND_PTA_SIMPLE;
301 	msg[0] = MSG_SIMPLE_TAG;
302 	scsi_populate_tag_msg(sc, msg);
303 	if (msg[0] == MSG_ORDERED_TAG)
304 		pri_tag = FCPIO_ICMND_PTA_ORDERED;
305 
306 	/* Enqueue the descriptor in the Copy WQ */
307 	spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
308 
309 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
310 		free_wq_copy_descs(fnic, wq);
311 
312 	if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
313 		spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
314 		return SCSI_MLQUEUE_HOST_BUSY;
315 	}
316 
317 	flags = 0;
318 	if (sc->sc_data_direction == DMA_FROM_DEVICE)
319 		flags = FCPIO_ICMND_RDDATA;
320 	else if (sc->sc_data_direction == DMA_TO_DEVICE)
321 		flags = FCPIO_ICMND_WRDATA;
322 
323 	exch_flags = 0;
324 	if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
325 	    (rp->flags & FC_RP_FLAGS_RETRY))
326 		exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
327 
328 	fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
329 					 0, exch_flags, io_req->sgl_cnt,
330 					 SCSI_SENSE_BUFFERSIZE,
331 					 io_req->sgl_list_pa,
332 					 io_req->sense_buf_pa,
333 					 0, /* scsi cmd ref, always 0 */
334 					 pri_tag, /* scsi pri and tag */
335 					 flags,	/* command flags */
336 					 sc->cmnd, sc->cmd_len,
337 					 scsi_bufflen(sc),
338 					 fc_lun.scsi_lun, io_req->port_id,
339 					 rport->maxframe_size, rp->r_a_tov,
340 					 rp->e_d_tov);
341 
342 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
343 	return 0;
344 }
345 
346 /*
347  * fnic_queuecommand
348  * Routine to send a scsi cdb
349  * Called with host_lock held and interrupts disabled.
350  */
351 int fnic_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
352 {
353 	struct fc_lport *lp;
354 	struct fc_rport *rport;
355 	struct fnic_io_req *io_req;
356 	struct fnic *fnic;
357 	struct vnic_wq_copy *wq;
358 	int ret;
359 	int sg_count;
360 	unsigned long flags;
361 	unsigned long ptr;
362 
363 	rport = starget_to_rport(scsi_target(sc->device));
364 	ret = fc_remote_port_chkready(rport);
365 	if (ret) {
366 		sc->result = ret;
367 		done(sc);
368 		return 0;
369 	}
370 
371 	lp = shost_priv(sc->device->host);
372 	if (lp->state != LPORT_ST_READY || !(lp->link_up))
373 		return SCSI_MLQUEUE_HOST_BUSY;
374 
375 	/*
376 	 * Release host lock, use driver resource specific locks from here.
377 	 * Don't re-enable interrupts in case they were disabled prior to the
378 	 * caller disabling them.
379 	 */
380 	spin_unlock(lp->host->host_lock);
381 
382 	/* Get a new io_req for this SCSI IO */
383 	fnic = lport_priv(lp);
384 
385 	io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
386 	if (!io_req) {
387 		ret = SCSI_MLQUEUE_HOST_BUSY;
388 		goto out;
389 	}
390 	memset(io_req, 0, sizeof(*io_req));
391 
392 	/* Map the data buffer */
393 	sg_count = scsi_dma_map(sc);
394 	if (sg_count < 0) {
395 		mempool_free(io_req, fnic->io_req_pool);
396 		goto out;
397 	}
398 
399 	/* Determine the type of scatter/gather list we need */
400 	io_req->sgl_cnt = sg_count;
401 	io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
402 	if (sg_count > FNIC_DFLT_SG_DESC_CNT)
403 		io_req->sgl_type = FNIC_SGL_CACHE_MAX;
404 
405 	if (sg_count) {
406 		io_req->sgl_list =
407 			mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
408 				      GFP_ATOMIC | GFP_DMA);
409 		if (!io_req->sgl_list) {
410 			ret = SCSI_MLQUEUE_HOST_BUSY;
411 			scsi_dma_unmap(sc);
412 			mempool_free(io_req, fnic->io_req_pool);
413 			goto out;
414 		}
415 
416 		/* Cache sgl list allocated address before alignment */
417 		io_req->sgl_list_alloc = io_req->sgl_list;
418 		ptr = (unsigned long) io_req->sgl_list;
419 		if (ptr % FNIC_SG_DESC_ALIGN) {
420 			io_req->sgl_list = (struct host_sg_desc *)
421 				(((unsigned long) ptr
422 				  + FNIC_SG_DESC_ALIGN - 1)
423 				 & ~(FNIC_SG_DESC_ALIGN - 1));
424 		}
425 	}
426 
427 	/* initialize rest of io_req */
428 	io_req->port_id = rport->port_id;
429 	CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
430 	CMD_SP(sc) = (char *)io_req;
431 	sc->scsi_done = done;
432 
433 	/* create copy wq desc and enqueue it */
434 	wq = &fnic->wq_copy[0];
435 	ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
436 	if (ret) {
437 		/*
438 		 * In case another thread cancelled the request,
439 		 * refetch the pointer under the lock.
440 		 */
441 		spinlock_t *io_lock = fnic_io_lock_hash(fnic, sc);
442 
443 		spin_lock_irqsave(io_lock, flags);
444 		io_req = (struct fnic_io_req *)CMD_SP(sc);
445 		CMD_SP(sc) = NULL;
446 		CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
447 		spin_unlock_irqrestore(io_lock, flags);
448 		if (io_req) {
449 			fnic_release_ioreq_buf(fnic, io_req, sc);
450 			mempool_free(io_req, fnic->io_req_pool);
451 		}
452 	}
453 out:
454 	/* acquire host lock before returning to SCSI */
455 	spin_lock(lp->host->host_lock);
456 	return ret;
457 }
458 
459 /*
460  * fnic_fcpio_fw_reset_cmpl_handler
461  * Routine to handle fw reset completion
462  */
463 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
464 					    struct fcpio_fw_req *desc)
465 {
466 	u8 type;
467 	u8 hdr_status;
468 	struct fcpio_tag tag;
469 	int ret = 0;
470 	unsigned long flags;
471 
472 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
473 
474 	/* Clean up all outstanding io requests */
475 	fnic_cleanup_io(fnic, SCSI_NO_TAG);
476 
477 	spin_lock_irqsave(&fnic->fnic_lock, flags);
478 
479 	/* fnic should be in FC_TRANS_ETH_MODE */
480 	if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
481 		/* Check status of reset completion */
482 		if (!hdr_status) {
483 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
484 				      "reset cmpl success\n");
485 			/* Ready to send flogi out */
486 			fnic->state = FNIC_IN_ETH_MODE;
487 		} else {
488 			FNIC_SCSI_DBG(KERN_DEBUG,
489 				      fnic->lport->host,
490 				      "fnic fw_reset : failed %s\n",
491 				      fnic_fcpio_status_to_str(hdr_status));
492 
493 			/*
494 			 * Unable to change to eth mode, cannot send out flogi
495 			 * Change state to fc mode, so that subsequent Flogi
496 			 * requests from libFC will cause more attempts to
497 			 * reset the firmware. Free the cached flogi
498 			 */
499 			fnic->state = FNIC_IN_FC_MODE;
500 			ret = -1;
501 		}
502 	} else {
503 		FNIC_SCSI_DBG(KERN_DEBUG,
504 			      fnic->lport->host,
505 			      "Unexpected state %s while processing"
506 			      " reset cmpl\n", fnic_state_to_str(fnic->state));
507 		ret = -1;
508 	}
509 
510 	/* Thread removing device blocks till firmware reset is complete */
511 	if (fnic->remove_wait)
512 		complete(fnic->remove_wait);
513 
514 	/*
515 	 * If fnic is being removed, or fw reset failed
516 	 * free the flogi frame. Else, send it out
517 	 */
518 	if (fnic->remove_wait || ret) {
519 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
520 		skb_queue_purge(&fnic->tx_queue);
521 		goto reset_cmpl_handler_end;
522 	}
523 
524 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
525 
526 	fnic_flush_tx(fnic);
527 
528  reset_cmpl_handler_end:
529 	return ret;
530 }
531 
532 /*
533  * fnic_fcpio_flogi_reg_cmpl_handler
534  * Routine to handle flogi register completion
535  */
536 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
537 					     struct fcpio_fw_req *desc)
538 {
539 	u8 type;
540 	u8 hdr_status;
541 	struct fcpio_tag tag;
542 	int ret = 0;
543 	unsigned long flags;
544 
545 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
546 
547 	/* Update fnic state based on status of flogi reg completion */
548 	spin_lock_irqsave(&fnic->fnic_lock, flags);
549 
550 	if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
551 
552 		/* Check flogi registration completion status */
553 		if (!hdr_status) {
554 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
555 				      "flog reg succeeded\n");
556 			fnic->state = FNIC_IN_FC_MODE;
557 		} else {
558 			FNIC_SCSI_DBG(KERN_DEBUG,
559 				      fnic->lport->host,
560 				      "fnic flogi reg :failed %s\n",
561 				      fnic_fcpio_status_to_str(hdr_status));
562 			fnic->state = FNIC_IN_ETH_MODE;
563 			ret = -1;
564 		}
565 	} else {
566 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
567 			      "Unexpected fnic state %s while"
568 			      " processing flogi reg completion\n",
569 			      fnic_state_to_str(fnic->state));
570 		ret = -1;
571 	}
572 
573 	if (!ret) {
574 		if (fnic->stop_rx_link_events) {
575 			spin_unlock_irqrestore(&fnic->fnic_lock, flags);
576 			goto reg_cmpl_handler_end;
577 		}
578 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
579 
580 		fnic_flush_tx(fnic);
581 		queue_work(fnic_event_queue, &fnic->frame_work);
582 	} else {
583 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
584 	}
585 
586 reg_cmpl_handler_end:
587 	return ret;
588 }
589 
590 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
591 					u16 request_out)
592 {
593 	if (wq->to_clean_index <= wq->to_use_index) {
594 		/* out of range, stale request_out index */
595 		if (request_out < wq->to_clean_index ||
596 		    request_out >= wq->to_use_index)
597 			return 0;
598 	} else {
599 		/* out of range, stale request_out index */
600 		if (request_out < wq->to_clean_index &&
601 		    request_out >= wq->to_use_index)
602 			return 0;
603 	}
604 	/* request_out index is in range */
605 	return 1;
606 }
607 
608 
609 /*
610  * Mark that ack received and store the Ack index. If there are multiple
611  * acks received before Tx thread cleans it up, the latest value will be
612  * used which is correct behavior. This state should be in the copy Wq
613  * instead of in the fnic
614  */
615 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
616 					  unsigned int cq_index,
617 					  struct fcpio_fw_req *desc)
618 {
619 	struct vnic_wq_copy *wq;
620 	u16 request_out = desc->u.ack.request_out;
621 	unsigned long flags;
622 
623 	/* mark the ack state */
624 	wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
625 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
626 
627 	if (is_ack_index_in_range(wq, request_out)) {
628 		fnic->fw_ack_index[0] = request_out;
629 		fnic->fw_ack_recd[0] = 1;
630 	}
631 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
632 }
633 
634 /*
635  * fnic_fcpio_icmnd_cmpl_handler
636  * Routine to handle icmnd completions
637  */
638 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
639 					 struct fcpio_fw_req *desc)
640 {
641 	u8 type;
642 	u8 hdr_status;
643 	struct fcpio_tag tag;
644 	u32 id;
645 	u64 xfer_len = 0;
646 	struct fcpio_icmnd_cmpl *icmnd_cmpl;
647 	struct fnic_io_req *io_req;
648 	struct scsi_cmnd *sc;
649 	unsigned long flags;
650 	spinlock_t *io_lock;
651 
652 	/* Decode the cmpl description to get the io_req id */
653 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
654 	fcpio_tag_id_dec(&tag, &id);
655 
656 	if (id >= FNIC_MAX_IO_REQ)
657 		return;
658 
659 	sc = scsi_host_find_tag(fnic->lport->host, id);
660 	WARN_ON_ONCE(!sc);
661 	if (!sc)
662 		return;
663 
664 	io_lock = fnic_io_lock_hash(fnic, sc);
665 	spin_lock_irqsave(io_lock, flags);
666 	io_req = (struct fnic_io_req *)CMD_SP(sc);
667 	WARN_ON_ONCE(!io_req);
668 	if (!io_req) {
669 		spin_unlock_irqrestore(io_lock, flags);
670 		return;
671 	}
672 
673 	/* firmware completed the io */
674 	io_req->io_completed = 1;
675 
676 	/*
677 	 *  if SCSI-ML has already issued abort on this command,
678 	 * ignore completion of the IO. The abts path will clean it up
679 	 */
680 	if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
681 		spin_unlock_irqrestore(io_lock, flags);
682 		return;
683 	}
684 
685 	/* Mark the IO as complete */
686 	CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
687 
688 	icmnd_cmpl = &desc->u.icmnd_cmpl;
689 
690 	switch (hdr_status) {
691 	case FCPIO_SUCCESS:
692 		sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
693 		xfer_len = scsi_bufflen(sc);
694 		scsi_set_resid(sc, icmnd_cmpl->residual);
695 
696 		if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
697 			xfer_len -= icmnd_cmpl->residual;
698 
699 		/*
700 		 * If queue_full, then try to reduce queue depth for all
701 		 * LUNS on the target. Todo: this should be accompanied
702 		 * by a periodic queue_depth rampup based on successful
703 		 * IO completion.
704 		 */
705 		if (icmnd_cmpl->scsi_status == QUEUE_FULL) {
706 			struct scsi_device *t_sdev;
707 			int qd = 0;
708 
709 			shost_for_each_device(t_sdev, sc->device->host) {
710 				if (t_sdev->id != sc->device->id)
711 					continue;
712 
713 				if (t_sdev->queue_depth > 1) {
714 					qd = scsi_track_queue_full
715 						(t_sdev,
716 						 t_sdev->queue_depth - 1);
717 					if (qd == -1)
718 						qd = t_sdev->host->cmd_per_lun;
719 					shost_printk(KERN_INFO,
720 						     fnic->lport->host,
721 						     "scsi[%d:%d:%d:%d"
722 						     "] queue full detected,"
723 						     "new depth = %d\n",
724 						     t_sdev->host->host_no,
725 						     t_sdev->channel,
726 						     t_sdev->id, t_sdev->lun,
727 						     t_sdev->queue_depth);
728 				}
729 			}
730 		}
731 		break;
732 
733 	case FCPIO_TIMEOUT:          /* request was timed out */
734 		sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
735 		break;
736 
737 	case FCPIO_ABORTED:          /* request was aborted */
738 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
739 		break;
740 
741 	case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
742 		scsi_set_resid(sc, icmnd_cmpl->residual);
743 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
744 		break;
745 
746 	case FCPIO_OUT_OF_RESOURCE:  /* out of resources to complete request */
747 		sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
748 		break;
749 	case FCPIO_INVALID_HEADER:   /* header contains invalid data */
750 	case FCPIO_INVALID_PARAM:    /* some parameter in request invalid */
751 	case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
752 	case FCPIO_IO_NOT_FOUND:     /* requested I/O was not found */
753 	case FCPIO_SGL_INVALID:      /* request was aborted due to sgl error */
754 	case FCPIO_MSS_INVALID:      /* request was aborted due to mss error */
755 	case FCPIO_FW_ERR:           /* request was terminated due fw error */
756 	default:
757 		shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
758 			     fnic_fcpio_status_to_str(hdr_status));
759 		sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
760 		break;
761 	}
762 
763 	/* Break link with the SCSI command */
764 	CMD_SP(sc) = NULL;
765 
766 	spin_unlock_irqrestore(io_lock, flags);
767 
768 	fnic_release_ioreq_buf(fnic, io_req, sc);
769 
770 	mempool_free(io_req, fnic->io_req_pool);
771 
772 	if (sc->sc_data_direction == DMA_FROM_DEVICE) {
773 		fnic->lport->host_stats.fcp_input_requests++;
774 		fnic->fcp_input_bytes += xfer_len;
775 	} else if (sc->sc_data_direction == DMA_TO_DEVICE) {
776 		fnic->lport->host_stats.fcp_output_requests++;
777 		fnic->fcp_output_bytes += xfer_len;
778 	} else
779 		fnic->lport->host_stats.fcp_control_requests++;
780 
781 	/* Call SCSI completion function to complete the IO */
782 	if (sc->scsi_done)
783 		sc->scsi_done(sc);
784 
785 }
786 
787 /* fnic_fcpio_itmf_cmpl_handler
788  * Routine to handle itmf completions
789  */
790 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
791 					struct fcpio_fw_req *desc)
792 {
793 	u8 type;
794 	u8 hdr_status;
795 	struct fcpio_tag tag;
796 	u32 id;
797 	struct scsi_cmnd *sc;
798 	struct fnic_io_req *io_req;
799 	unsigned long flags;
800 	spinlock_t *io_lock;
801 
802 	fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
803 	fcpio_tag_id_dec(&tag, &id);
804 
805 	if ((id & FNIC_TAG_MASK) >= FNIC_MAX_IO_REQ)
806 		return;
807 
808 	sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
809 	WARN_ON_ONCE(!sc);
810 	if (!sc)
811 		return;
812 
813 	io_lock = fnic_io_lock_hash(fnic, sc);
814 	spin_lock_irqsave(io_lock, flags);
815 	io_req = (struct fnic_io_req *)CMD_SP(sc);
816 	WARN_ON_ONCE(!io_req);
817 	if (!io_req) {
818 		spin_unlock_irqrestore(io_lock, flags);
819 		return;
820 	}
821 
822 	if (id & FNIC_TAG_ABORT) {
823 		/* Completion of abort cmd */
824 		if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
825 			/* This is a late completion. Ignore it */
826 			spin_unlock_irqrestore(io_lock, flags);
827 			return;
828 		}
829 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
830 		CMD_ABTS_STATUS(sc) = hdr_status;
831 
832 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
833 			      "abts cmpl recd. id %d status %s\n",
834 			      (int)(id & FNIC_TAG_MASK),
835 			      fnic_fcpio_status_to_str(hdr_status));
836 
837 		/*
838 		 * If scsi_eh thread is blocked waiting for abts to complete,
839 		 * signal completion to it. IO will be cleaned in the thread
840 		 * else clean it in this context
841 		 */
842 		if (io_req->abts_done) {
843 			complete(io_req->abts_done);
844 			spin_unlock_irqrestore(io_lock, flags);
845 		} else {
846 			FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
847 				      "abts cmpl, completing IO\n");
848 			CMD_SP(sc) = NULL;
849 			sc->result = (DID_ERROR << 16);
850 
851 			spin_unlock_irqrestore(io_lock, flags);
852 
853 			fnic_release_ioreq_buf(fnic, io_req, sc);
854 			mempool_free(io_req, fnic->io_req_pool);
855 			if (sc->scsi_done)
856 				sc->scsi_done(sc);
857 		}
858 
859 	} else if (id & FNIC_TAG_DEV_RST) {
860 		/* Completion of device reset */
861 		CMD_LR_STATUS(sc) = hdr_status;
862 		CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
863 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
864 			      "dev reset cmpl recd. id %d status %s\n",
865 			      (int)(id & FNIC_TAG_MASK),
866 			      fnic_fcpio_status_to_str(hdr_status));
867 		if (io_req->dr_done)
868 			complete(io_req->dr_done);
869 		spin_unlock_irqrestore(io_lock, flags);
870 
871 	} else {
872 		shost_printk(KERN_ERR, fnic->lport->host,
873 			     "Unexpected itmf io state %s tag %x\n",
874 			     fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
875 		spin_unlock_irqrestore(io_lock, flags);
876 	}
877 
878 }
879 
880 /*
881  * fnic_fcpio_cmpl_handler
882  * Routine to service the cq for wq_copy
883  */
884 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
885 				   unsigned int cq_index,
886 				   struct fcpio_fw_req *desc)
887 {
888 	struct fnic *fnic = vnic_dev_priv(vdev);
889 	int ret = 0;
890 
891 	switch (desc->hdr.type) {
892 	case FCPIO_ACK: /* fw copied copy wq desc to its queue */
893 		fnic_fcpio_ack_handler(fnic, cq_index, desc);
894 		break;
895 
896 	case FCPIO_ICMND_CMPL: /* fw completed a command */
897 		fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
898 		break;
899 
900 	case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
901 		fnic_fcpio_itmf_cmpl_handler(fnic, desc);
902 		break;
903 
904 	case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
905 	case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
906 		ret = fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
907 		break;
908 
909 	case FCPIO_RESET_CMPL: /* fw completed reset */
910 		ret = fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
911 		break;
912 
913 	default:
914 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
915 			      "firmware completion type %d\n",
916 			      desc->hdr.type);
917 		break;
918 	}
919 
920 	return ret;
921 }
922 
923 /*
924  * fnic_wq_copy_cmpl_handler
925  * Routine to process wq copy
926  */
927 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
928 {
929 	unsigned int wq_work_done = 0;
930 	unsigned int i, cq_index;
931 	unsigned int cur_work_done;
932 
933 	for (i = 0; i < fnic->wq_copy_count; i++) {
934 		cq_index = i + fnic->raw_wq_count + fnic->rq_count;
935 		cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
936 						     fnic_fcpio_cmpl_handler,
937 						     copy_work_to_do);
938 		wq_work_done += cur_work_done;
939 	}
940 	return wq_work_done;
941 }
942 
943 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
944 {
945 	unsigned int i;
946 	struct fnic_io_req *io_req;
947 	unsigned long flags = 0;
948 	struct scsi_cmnd *sc;
949 	spinlock_t *io_lock;
950 
951 	for (i = 0; i < FNIC_MAX_IO_REQ; i++) {
952 		if (i == exclude_id)
953 			continue;
954 
955 		sc = scsi_host_find_tag(fnic->lport->host, i);
956 		if (!sc)
957 			continue;
958 
959 		io_lock = fnic_io_lock_hash(fnic, sc);
960 		spin_lock_irqsave(io_lock, flags);
961 		io_req = (struct fnic_io_req *)CMD_SP(sc);
962 		if (!io_req) {
963 			spin_unlock_irqrestore(io_lock, flags);
964 			goto cleanup_scsi_cmd;
965 		}
966 
967 		CMD_SP(sc) = NULL;
968 
969 		spin_unlock_irqrestore(io_lock, flags);
970 
971 		/*
972 		 * If there is a scsi_cmnd associated with this io_req, then
973 		 * free the corresponding state
974 		 */
975 		fnic_release_ioreq_buf(fnic, io_req, sc);
976 		mempool_free(io_req, fnic->io_req_pool);
977 
978 cleanup_scsi_cmd:
979 		sc->result = DID_TRANSPORT_DISRUPTED << 16;
980 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "fnic_cleanup_io:"
981 			      " DID_TRANSPORT_DISRUPTED\n");
982 
983 		/* Complete the command to SCSI */
984 		if (sc->scsi_done)
985 			sc->scsi_done(sc);
986 	}
987 }
988 
989 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
990 				  struct fcpio_host_req *desc)
991 {
992 	u32 id;
993 	struct fnic *fnic = vnic_dev_priv(wq->vdev);
994 	struct fnic_io_req *io_req;
995 	struct scsi_cmnd *sc;
996 	unsigned long flags;
997 	spinlock_t *io_lock;
998 
999 	/* get the tag reference */
1000 	fcpio_tag_id_dec(&desc->hdr.tag, &id);
1001 	id &= FNIC_TAG_MASK;
1002 
1003 	if (id >= FNIC_MAX_IO_REQ)
1004 		return;
1005 
1006 	sc = scsi_host_find_tag(fnic->lport->host, id);
1007 	if (!sc)
1008 		return;
1009 
1010 	io_lock = fnic_io_lock_hash(fnic, sc);
1011 	spin_lock_irqsave(io_lock, flags);
1012 
1013 	/* Get the IO context which this desc refers to */
1014 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1015 
1016 	/* fnic interrupts are turned off by now */
1017 
1018 	if (!io_req) {
1019 		spin_unlock_irqrestore(io_lock, flags);
1020 		goto wq_copy_cleanup_scsi_cmd;
1021 	}
1022 
1023 	CMD_SP(sc) = NULL;
1024 
1025 	spin_unlock_irqrestore(io_lock, flags);
1026 
1027 	fnic_release_ioreq_buf(fnic, io_req, sc);
1028 	mempool_free(io_req, fnic->io_req_pool);
1029 
1030 wq_copy_cleanup_scsi_cmd:
1031 	sc->result = DID_NO_CONNECT << 16;
1032 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1033 		      " DID_NO_CONNECT\n");
1034 
1035 	if (sc->scsi_done)
1036 		sc->scsi_done(sc);
1037 }
1038 
1039 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1040 					  u32 task_req, u8 *fc_lun,
1041 					  struct fnic_io_req *io_req)
1042 {
1043 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1044 	unsigned long flags;
1045 
1046 	spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1047 
1048 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1049 		free_wq_copy_descs(fnic, wq);
1050 
1051 	if (!vnic_wq_copy_desc_avail(wq)) {
1052 		spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1053 		return 1;
1054 	}
1055 	fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1056 				     0, task_req, tag, fc_lun, io_req->port_id,
1057 				     fnic->config.ra_tov, fnic->config.ed_tov);
1058 
1059 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1060 	return 0;
1061 }
1062 
1063 void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1064 {
1065 	int tag;
1066 	struct fnic_io_req *io_req;
1067 	spinlock_t *io_lock;
1068 	unsigned long flags;
1069 	struct scsi_cmnd *sc;
1070 	struct scsi_lun fc_lun;
1071 	enum fnic_ioreq_state old_ioreq_state;
1072 
1073 	FNIC_SCSI_DBG(KERN_DEBUG,
1074 		      fnic->lport->host,
1075 		      "fnic_rport_reset_exch called portid 0x%06x\n",
1076 		      port_id);
1077 
1078 	if (fnic->in_remove)
1079 		return;
1080 
1081 	for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1082 		sc = scsi_host_find_tag(fnic->lport->host, tag);
1083 		if (!sc)
1084 			continue;
1085 
1086 		io_lock = fnic_io_lock_hash(fnic, sc);
1087 		spin_lock_irqsave(io_lock, flags);
1088 
1089 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1090 
1091 		if (!io_req || io_req->port_id != port_id) {
1092 			spin_unlock_irqrestore(io_lock, flags);
1093 			continue;
1094 		}
1095 
1096 		/*
1097 		 * Found IO that is still pending with firmware and
1098 		 * belongs to rport that went away
1099 		 */
1100 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1101 			spin_unlock_irqrestore(io_lock, flags);
1102 			continue;
1103 		}
1104 		old_ioreq_state = CMD_STATE(sc);
1105 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1106 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1107 
1108 		BUG_ON(io_req->abts_done);
1109 
1110 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1111 			      "fnic_rport_reset_exch: Issuing abts\n");
1112 
1113 		spin_unlock_irqrestore(io_lock, flags);
1114 
1115 		/* Now queue the abort command to firmware */
1116 		int_to_scsilun(sc->device->lun, &fc_lun);
1117 
1118 		if (fnic_queue_abort_io_req(fnic, tag,
1119 					    FCPIO_ITMF_ABT_TASK_TERM,
1120 					    fc_lun.scsi_lun, io_req)) {
1121 			/*
1122 			 * Revert the cmd state back to old state, if
1123 			 * it hasnt changed in between. This cmd will get
1124 			 * aborted later by scsi_eh, or cleaned up during
1125 			 * lun reset
1126 			 */
1127 			io_lock = fnic_io_lock_hash(fnic, sc);
1128 
1129 			spin_lock_irqsave(io_lock, flags);
1130 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1131 				CMD_STATE(sc) = old_ioreq_state;
1132 			spin_unlock_irqrestore(io_lock, flags);
1133 		}
1134 	}
1135 
1136 }
1137 
1138 void fnic_terminate_rport_io(struct fc_rport *rport)
1139 {
1140 	int tag;
1141 	struct fnic_io_req *io_req;
1142 	spinlock_t *io_lock;
1143 	unsigned long flags;
1144 	struct scsi_cmnd *sc;
1145 	struct scsi_lun fc_lun;
1146 	struct fc_rport_libfc_priv *rdata = rport->dd_data;
1147 	struct fc_lport *lport = rdata->local_port;
1148 	struct fnic *fnic = lport_priv(lport);
1149 	struct fc_rport *cmd_rport;
1150 	enum fnic_ioreq_state old_ioreq_state;
1151 
1152 	FNIC_SCSI_DBG(KERN_DEBUG,
1153 		      fnic->lport->host, "fnic_terminate_rport_io called"
1154 		      " wwpn 0x%llx, wwnn0x%llx, portid 0x%06x\n",
1155 		      rport->port_name, rport->node_name,
1156 		      rport->port_id);
1157 
1158 	if (fnic->in_remove)
1159 		return;
1160 
1161 	for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1162 		sc = scsi_host_find_tag(fnic->lport->host, tag);
1163 		if (!sc)
1164 			continue;
1165 
1166 		cmd_rport = starget_to_rport(scsi_target(sc->device));
1167 		if (rport != cmd_rport)
1168 			continue;
1169 
1170 		io_lock = fnic_io_lock_hash(fnic, sc);
1171 		spin_lock_irqsave(io_lock, flags);
1172 
1173 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1174 
1175 		if (!io_req || rport != cmd_rport) {
1176 			spin_unlock_irqrestore(io_lock, flags);
1177 			continue;
1178 		}
1179 
1180 		/*
1181 		 * Found IO that is still pending with firmware and
1182 		 * belongs to rport that went away
1183 		 */
1184 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1185 			spin_unlock_irqrestore(io_lock, flags);
1186 			continue;
1187 		}
1188 		old_ioreq_state = CMD_STATE(sc);
1189 		CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1190 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1191 
1192 		BUG_ON(io_req->abts_done);
1193 
1194 		FNIC_SCSI_DBG(KERN_DEBUG,
1195 			      fnic->lport->host,
1196 			      "fnic_terminate_rport_io: Issuing abts\n");
1197 
1198 		spin_unlock_irqrestore(io_lock, flags);
1199 
1200 		/* Now queue the abort command to firmware */
1201 		int_to_scsilun(sc->device->lun, &fc_lun);
1202 
1203 		if (fnic_queue_abort_io_req(fnic, tag,
1204 					    FCPIO_ITMF_ABT_TASK_TERM,
1205 					    fc_lun.scsi_lun, io_req)) {
1206 			/*
1207 			 * Revert the cmd state back to old state, if
1208 			 * it hasnt changed in between. This cmd will get
1209 			 * aborted later by scsi_eh, or cleaned up during
1210 			 * lun reset
1211 			 */
1212 			io_lock = fnic_io_lock_hash(fnic, sc);
1213 
1214 			spin_lock_irqsave(io_lock, flags);
1215 			if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1216 				CMD_STATE(sc) = old_ioreq_state;
1217 			spin_unlock_irqrestore(io_lock, flags);
1218 		}
1219 	}
1220 
1221 }
1222 
1223 /*
1224  * This function is exported to SCSI for sending abort cmnds.
1225  * A SCSI IO is represented by a io_req in the driver.
1226  * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1227  */
1228 int fnic_abort_cmd(struct scsi_cmnd *sc)
1229 {
1230 	struct fc_lport *lp;
1231 	struct fnic *fnic;
1232 	struct fnic_io_req *io_req;
1233 	struct fc_rport *rport;
1234 	spinlock_t *io_lock;
1235 	unsigned long flags;
1236 	int ret = SUCCESS;
1237 	u32 task_req;
1238 	struct scsi_lun fc_lun;
1239 	DECLARE_COMPLETION_ONSTACK(tm_done);
1240 
1241 	/* Wait for rport to unblock */
1242 	fc_block_scsi_eh(sc);
1243 
1244 	/* Get local-port, check ready and link up */
1245 	lp = shost_priv(sc->device->host);
1246 
1247 	fnic = lport_priv(lp);
1248 	FNIC_SCSI_DBG(KERN_DEBUG,
1249 		      fnic->lport->host,
1250 		      "Abort Cmd called FCID 0x%x, LUN 0x%x TAG %d\n",
1251 		      (starget_to_rport(scsi_target(sc->device)))->port_id,
1252 		      sc->device->lun, sc->request->tag);
1253 
1254 	if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1255 		ret = FAILED;
1256 		goto fnic_abort_cmd_end;
1257 	}
1258 
1259 	/*
1260 	 * Avoid a race between SCSI issuing the abort and the device
1261 	 * completing the command.
1262 	 *
1263 	 * If the command is already completed by the fw cmpl code,
1264 	 * we just return SUCCESS from here. This means that the abort
1265 	 * succeeded. In the SCSI ML, since the timeout for command has
1266 	 * happened, the completion wont actually complete the command
1267 	 * and it will be considered as an aborted command
1268 	 *
1269 	 * The CMD_SP will not be cleared except while holding io_req_lock.
1270 	 */
1271 	io_lock = fnic_io_lock_hash(fnic, sc);
1272 	spin_lock_irqsave(io_lock, flags);
1273 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1274 	if (!io_req) {
1275 		spin_unlock_irqrestore(io_lock, flags);
1276 		goto fnic_abort_cmd_end;
1277 	}
1278 
1279 	io_req->abts_done = &tm_done;
1280 
1281 	if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1282 		spin_unlock_irqrestore(io_lock, flags);
1283 		goto wait_pending;
1284 	}
1285 	/*
1286 	 * Command is still pending, need to abort it
1287 	 * If the firmware completes the command after this point,
1288 	 * the completion wont be done till mid-layer, since abort
1289 	 * has already started.
1290 	 */
1291 	CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1292 	CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1293 
1294 	spin_unlock_irqrestore(io_lock, flags);
1295 
1296 	/*
1297 	 * Check readiness of the remote port. If the path to remote
1298 	 * port is up, then send abts to the remote port to terminate
1299 	 * the IO. Else, just locally terminate the IO in the firmware
1300 	 */
1301 	rport = starget_to_rport(scsi_target(sc->device));
1302 	if (fc_remote_port_chkready(rport) == 0)
1303 		task_req = FCPIO_ITMF_ABT_TASK;
1304 	else
1305 		task_req = FCPIO_ITMF_ABT_TASK_TERM;
1306 
1307 	/* Now queue the abort command to firmware */
1308 	int_to_scsilun(sc->device->lun, &fc_lun);
1309 
1310 	if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1311 				    fc_lun.scsi_lun, io_req)) {
1312 		spin_lock_irqsave(io_lock, flags);
1313 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1314 		if (io_req)
1315 			io_req->abts_done = NULL;
1316 		spin_unlock_irqrestore(io_lock, flags);
1317 		ret = FAILED;
1318 		goto fnic_abort_cmd_end;
1319 	}
1320 
1321 	/*
1322 	 * We queued an abort IO, wait for its completion.
1323 	 * Once the firmware completes the abort command, it will
1324 	 * wake up this thread.
1325 	 */
1326  wait_pending:
1327 	wait_for_completion_timeout(&tm_done,
1328 				    msecs_to_jiffies
1329 				    (2 * fnic->config.ra_tov +
1330 				     fnic->config.ed_tov));
1331 
1332 	/* Check the abort status */
1333 	spin_lock_irqsave(io_lock, flags);
1334 
1335 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1336 	if (!io_req) {
1337 		spin_unlock_irqrestore(io_lock, flags);
1338 		ret = FAILED;
1339 		goto fnic_abort_cmd_end;
1340 	}
1341 	io_req->abts_done = NULL;
1342 
1343 	/* fw did not complete abort, timed out */
1344 	if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1345 		spin_unlock_irqrestore(io_lock, flags);
1346 		ret = FAILED;
1347 		goto fnic_abort_cmd_end;
1348 	}
1349 
1350 	/*
1351 	 * firmware completed the abort, check the status,
1352 	 * free the io_req irrespective of failure or success
1353 	 */
1354 	if (CMD_ABTS_STATUS(sc) != FCPIO_SUCCESS)
1355 		ret = FAILED;
1356 
1357 	CMD_SP(sc) = NULL;
1358 
1359 	spin_unlock_irqrestore(io_lock, flags);
1360 
1361 	fnic_release_ioreq_buf(fnic, io_req, sc);
1362 	mempool_free(io_req, fnic->io_req_pool);
1363 
1364 fnic_abort_cmd_end:
1365 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1366 		      "Returning from abort cmd %s\n",
1367 		      (ret == SUCCESS) ?
1368 		      "SUCCESS" : "FAILED");
1369 	return ret;
1370 }
1371 
1372 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
1373 				       struct scsi_cmnd *sc,
1374 				       struct fnic_io_req *io_req)
1375 {
1376 	struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1377 	struct scsi_lun fc_lun;
1378 	int ret = 0;
1379 	unsigned long intr_flags;
1380 
1381 	spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
1382 
1383 	if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1384 		free_wq_copy_descs(fnic, wq);
1385 
1386 	if (!vnic_wq_copy_desc_avail(wq)) {
1387 		ret = -EAGAIN;
1388 		goto lr_io_req_end;
1389 	}
1390 
1391 	/* fill in the lun info */
1392 	int_to_scsilun(sc->device->lun, &fc_lun);
1393 
1394 	fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
1395 				     0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
1396 				     fc_lun.scsi_lun, io_req->port_id,
1397 				     fnic->config.ra_tov, fnic->config.ed_tov);
1398 
1399 lr_io_req_end:
1400 	spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
1401 
1402 	return ret;
1403 }
1404 
1405 /*
1406  * Clean up any pending aborts on the lun
1407  * For each outstanding IO on this lun, whose abort is not completed by fw,
1408  * issue a local abort. Wait for abort to complete. Return 0 if all commands
1409  * successfully aborted, 1 otherwise
1410  */
1411 static int fnic_clean_pending_aborts(struct fnic *fnic,
1412 				     struct scsi_cmnd *lr_sc)
1413 {
1414 	int tag;
1415 	struct fnic_io_req *io_req;
1416 	spinlock_t *io_lock;
1417 	unsigned long flags;
1418 	int ret = 0;
1419 	struct scsi_cmnd *sc;
1420 	struct fc_rport *rport;
1421 	struct scsi_lun fc_lun;
1422 	struct scsi_device *lun_dev = lr_sc->device;
1423 	DECLARE_COMPLETION_ONSTACK(tm_done);
1424 
1425 	for (tag = 0; tag < FNIC_MAX_IO_REQ; tag++) {
1426 		sc = scsi_host_find_tag(fnic->lport->host, tag);
1427 		/*
1428 		 * ignore this lun reset cmd or cmds that do not belong to
1429 		 * this lun
1430 		 */
1431 		if (!sc || sc == lr_sc || sc->device != lun_dev)
1432 			continue;
1433 
1434 		io_lock = fnic_io_lock_hash(fnic, sc);
1435 		spin_lock_irqsave(io_lock, flags);
1436 
1437 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1438 
1439 		if (!io_req || sc->device != lun_dev) {
1440 			spin_unlock_irqrestore(io_lock, flags);
1441 			continue;
1442 		}
1443 
1444 		/*
1445 		 * Found IO that is still pending with firmware and
1446 		 * belongs to the LUN that we are resetting
1447 		 */
1448 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1449 			      "Found IO in %s on lun\n",
1450 			      fnic_ioreq_state_to_str(CMD_STATE(sc)));
1451 
1452 		BUG_ON(CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING);
1453 
1454 		CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1455 		io_req->abts_done = &tm_done;
1456 		spin_unlock_irqrestore(io_lock, flags);
1457 
1458 		/* Now queue the abort command to firmware */
1459 		int_to_scsilun(sc->device->lun, &fc_lun);
1460 		rport = starget_to_rport(scsi_target(sc->device));
1461 
1462 		if (fnic_queue_abort_io_req(fnic, tag,
1463 					    FCPIO_ITMF_ABT_TASK_TERM,
1464 					    fc_lun.scsi_lun, io_req)) {
1465 			spin_lock_irqsave(io_lock, flags);
1466 			io_req = (struct fnic_io_req *)CMD_SP(sc);
1467 			if (io_req)
1468 				io_req->abts_done = NULL;
1469 			spin_unlock_irqrestore(io_lock, flags);
1470 			ret = 1;
1471 			goto clean_pending_aborts_end;
1472 		}
1473 
1474 		wait_for_completion_timeout(&tm_done,
1475 					    msecs_to_jiffies
1476 					    (fnic->config.ed_tov));
1477 
1478 		/* Recheck cmd state to check if it is now aborted */
1479 		spin_lock_irqsave(io_lock, flags);
1480 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1481 		if (!io_req) {
1482 			spin_unlock_irqrestore(io_lock, flags);
1483 			ret = 1;
1484 			goto clean_pending_aborts_end;
1485 		}
1486 
1487 		io_req->abts_done = NULL;
1488 
1489 		/* if abort is still pending with fw, fail */
1490 		if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1491 			spin_unlock_irqrestore(io_lock, flags);
1492 			ret = 1;
1493 			goto clean_pending_aborts_end;
1494 		}
1495 		CMD_SP(sc) = NULL;
1496 		spin_unlock_irqrestore(io_lock, flags);
1497 
1498 		fnic_release_ioreq_buf(fnic, io_req, sc);
1499 		mempool_free(io_req, fnic->io_req_pool);
1500 	}
1501 
1502 clean_pending_aborts_end:
1503 	return ret;
1504 }
1505 
1506 /*
1507  * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
1508  * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
1509  * on the LUN.
1510  */
1511 int fnic_device_reset(struct scsi_cmnd *sc)
1512 {
1513 	struct fc_lport *lp;
1514 	struct fnic *fnic;
1515 	struct fnic_io_req *io_req;
1516 	struct fc_rport *rport;
1517 	int status;
1518 	int ret = FAILED;
1519 	spinlock_t *io_lock;
1520 	unsigned long flags;
1521 	DECLARE_COMPLETION_ONSTACK(tm_done);
1522 
1523 	/* Wait for rport to unblock */
1524 	fc_block_scsi_eh(sc);
1525 
1526 	/* Get local-port, check ready and link up */
1527 	lp = shost_priv(sc->device->host);
1528 
1529 	fnic = lport_priv(lp);
1530 	FNIC_SCSI_DBG(KERN_DEBUG,
1531 		      fnic->lport->host,
1532 		      "Device reset called FCID 0x%x, LUN 0x%x\n",
1533 		      (starget_to_rport(scsi_target(sc->device)))->port_id,
1534 		      sc->device->lun);
1535 
1536 
1537 	if (lp->state != LPORT_ST_READY || !(lp->link_up))
1538 		goto fnic_device_reset_end;
1539 
1540 	/* Check if remote port up */
1541 	rport = starget_to_rport(scsi_target(sc->device));
1542 	if (fc_remote_port_chkready(rport))
1543 		goto fnic_device_reset_end;
1544 
1545 	io_lock = fnic_io_lock_hash(fnic, sc);
1546 	spin_lock_irqsave(io_lock, flags);
1547 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1548 
1549 	/*
1550 	 * If there is a io_req attached to this command, then use it,
1551 	 * else allocate a new one.
1552 	 */
1553 	if (!io_req) {
1554 		io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
1555 		if (!io_req) {
1556 			spin_unlock_irqrestore(io_lock, flags);
1557 			goto fnic_device_reset_end;
1558 		}
1559 		memset(io_req, 0, sizeof(*io_req));
1560 		io_req->port_id = rport->port_id;
1561 		CMD_SP(sc) = (char *)io_req;
1562 	}
1563 	io_req->dr_done = &tm_done;
1564 	CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
1565 	CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
1566 	spin_unlock_irqrestore(io_lock, flags);
1567 
1568 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %d\n",
1569 		      sc->request->tag);
1570 
1571 	/*
1572 	 * issue the device reset, if enqueue failed, clean up the ioreq
1573 	 * and break assoc with scsi cmd
1574 	 */
1575 	if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
1576 		spin_lock_irqsave(io_lock, flags);
1577 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1578 		if (io_req)
1579 			io_req->dr_done = NULL;
1580 		goto fnic_device_reset_clean;
1581 	}
1582 
1583 	/*
1584 	 * Wait on the local completion for LUN reset.  The io_req may be
1585 	 * freed while we wait since we hold no lock.
1586 	 */
1587 	wait_for_completion_timeout(&tm_done,
1588 				    msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
1589 
1590 	spin_lock_irqsave(io_lock, flags);
1591 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1592 	if (!io_req) {
1593 		spin_unlock_irqrestore(io_lock, flags);
1594 		goto fnic_device_reset_end;
1595 	}
1596 	io_req->dr_done = NULL;
1597 
1598 	status = CMD_LR_STATUS(sc);
1599 	spin_unlock_irqrestore(io_lock, flags);
1600 
1601 	/*
1602 	 * If lun reset not completed, bail out with failed. io_req
1603 	 * gets cleaned up during higher levels of EH
1604 	 */
1605 	if (status == FCPIO_INVALID_CODE) {
1606 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1607 			      "Device reset timed out\n");
1608 		goto fnic_device_reset_end;
1609 	}
1610 
1611 	/* Completed, but not successful, clean up the io_req, return fail */
1612 	if (status != FCPIO_SUCCESS) {
1613 		spin_lock_irqsave(io_lock, flags);
1614 		FNIC_SCSI_DBG(KERN_DEBUG,
1615 			      fnic->lport->host,
1616 			      "Device reset completed - failed\n");
1617 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1618 		goto fnic_device_reset_clean;
1619 	}
1620 
1621 	/*
1622 	 * Clean up any aborts on this lun that have still not
1623 	 * completed. If any of these fail, then LUN reset fails.
1624 	 * clean_pending_aborts cleans all cmds on this lun except
1625 	 * the lun reset cmd. If all cmds get cleaned, the lun reset
1626 	 * succeeds
1627 	 */
1628 	if (fnic_clean_pending_aborts(fnic, sc)) {
1629 		spin_lock_irqsave(io_lock, flags);
1630 		io_req = (struct fnic_io_req *)CMD_SP(sc);
1631 		FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1632 			      "Device reset failed"
1633 			      " since could not abort all IOs\n");
1634 		goto fnic_device_reset_clean;
1635 	}
1636 
1637 	/* Clean lun reset command */
1638 	spin_lock_irqsave(io_lock, flags);
1639 	io_req = (struct fnic_io_req *)CMD_SP(sc);
1640 	if (io_req)
1641 		/* Completed, and successful */
1642 		ret = SUCCESS;
1643 
1644 fnic_device_reset_clean:
1645 	if (io_req)
1646 		CMD_SP(sc) = NULL;
1647 
1648 	spin_unlock_irqrestore(io_lock, flags);
1649 
1650 	if (io_req) {
1651 		fnic_release_ioreq_buf(fnic, io_req, sc);
1652 		mempool_free(io_req, fnic->io_req_pool);
1653 	}
1654 
1655 fnic_device_reset_end:
1656 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1657 		      "Returning from device reset %s\n",
1658 		      (ret == SUCCESS) ?
1659 		      "SUCCESS" : "FAILED");
1660 	return ret;
1661 }
1662 
1663 /* Clean up all IOs, clean up libFC local port */
1664 int fnic_reset(struct Scsi_Host *shost)
1665 {
1666 	struct fc_lport *lp;
1667 	struct fnic *fnic;
1668 	int ret = SUCCESS;
1669 
1670 	lp = shost_priv(shost);
1671 	fnic = lport_priv(lp);
1672 
1673 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1674 		      "fnic_reset called\n");
1675 
1676 	/*
1677 	 * Reset local port, this will clean up libFC exchanges,
1678 	 * reset remote port sessions, and if link is up, begin flogi
1679 	 */
1680 	if (lp->tt.lport_reset(lp))
1681 		ret = FAILED;
1682 
1683 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1684 		      "Returning from fnic reset %s\n",
1685 		      (ret == SUCCESS) ?
1686 		      "SUCCESS" : "FAILED");
1687 
1688 	return ret;
1689 }
1690 
1691 /*
1692  * SCSI Error handling calls driver's eh_host_reset if all prior
1693  * error handling levels return FAILED. If host reset completes
1694  * successfully, and if link is up, then Fabric login begins.
1695  *
1696  * Host Reset is the highest level of error recovery. If this fails, then
1697  * host is offlined by SCSI.
1698  *
1699  */
1700 int fnic_host_reset(struct scsi_cmnd *sc)
1701 {
1702 	int ret;
1703 	unsigned long wait_host_tmo;
1704 	struct Scsi_Host *shost = sc->device->host;
1705 	struct fc_lport *lp = shost_priv(shost);
1706 
1707 	/*
1708 	 * If fnic_reset is successful, wait for fabric login to complete
1709 	 * scsi-ml tries to send a TUR to every device if host reset is
1710 	 * successful, so before returning to scsi, fabric should be up
1711 	 */
1712 	ret = fnic_reset(shost);
1713 	if (ret == SUCCESS) {
1714 		wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
1715 		ret = FAILED;
1716 		while (time_before(jiffies, wait_host_tmo)) {
1717 			if ((lp->state == LPORT_ST_READY) &&
1718 			    (lp->link_up)) {
1719 				ret = SUCCESS;
1720 				break;
1721 			}
1722 			ssleep(1);
1723 		}
1724 	}
1725 
1726 	return ret;
1727 }
1728 
1729 /*
1730  * This fxn is called from libFC when host is removed
1731  */
1732 void fnic_scsi_abort_io(struct fc_lport *lp)
1733 {
1734 	int err = 0;
1735 	unsigned long flags;
1736 	enum fnic_state old_state;
1737 	struct fnic *fnic = lport_priv(lp);
1738 	DECLARE_COMPLETION_ONSTACK(remove_wait);
1739 
1740 	/* Issue firmware reset for fnic, wait for reset to complete */
1741 	spin_lock_irqsave(&fnic->fnic_lock, flags);
1742 	fnic->remove_wait = &remove_wait;
1743 	old_state = fnic->state;
1744 	fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1745 	fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
1746 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1747 
1748 	err = fnic_fw_reset_handler(fnic);
1749 	if (err) {
1750 		spin_lock_irqsave(&fnic->fnic_lock, flags);
1751 		if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1752 			fnic->state = old_state;
1753 		fnic->remove_wait = NULL;
1754 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1755 		return;
1756 	}
1757 
1758 	/* Wait for firmware reset to complete */
1759 	wait_for_completion_timeout(&remove_wait,
1760 				    msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
1761 
1762 	spin_lock_irqsave(&fnic->fnic_lock, flags);
1763 	fnic->remove_wait = NULL;
1764 	FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1765 		      "fnic_scsi_abort_io %s\n",
1766 		      (fnic->state == FNIC_IN_ETH_MODE) ?
1767 		      "SUCCESS" : "FAILED");
1768 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1769 
1770 }
1771 
1772 /*
1773  * This fxn called from libFC to clean up driver IO state on link down
1774  */
1775 void fnic_scsi_cleanup(struct fc_lport *lp)
1776 {
1777 	unsigned long flags;
1778 	enum fnic_state old_state;
1779 	struct fnic *fnic = lport_priv(lp);
1780 
1781 	/* issue fw reset */
1782 	spin_lock_irqsave(&fnic->fnic_lock, flags);
1783 	old_state = fnic->state;
1784 	fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1785 	fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
1786 	spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1787 
1788 	if (fnic_fw_reset_handler(fnic)) {
1789 		spin_lock_irqsave(&fnic->fnic_lock, flags);
1790 		if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
1791 			fnic->state = old_state;
1792 		spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1793 	}
1794 
1795 }
1796 
1797 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
1798 {
1799 }
1800 
1801 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
1802 {
1803 	struct fnic *fnic = lport_priv(lp);
1804 
1805 	/* Non-zero sid, nothing to do */
1806 	if (sid)
1807 		goto call_fc_exch_mgr_reset;
1808 
1809 	if (did) {
1810 		fnic_rport_exch_reset(fnic, did);
1811 		goto call_fc_exch_mgr_reset;
1812 	}
1813 
1814 	/*
1815 	 * sid = 0, did = 0
1816 	 * link down or device being removed
1817 	 */
1818 	if (!fnic->in_remove)
1819 		fnic_scsi_cleanup(lp);
1820 	else
1821 		fnic_scsi_abort_io(lp);
1822 
1823 	/* call libFC exch mgr reset to reset its exchanges */
1824 call_fc_exch_mgr_reset:
1825 	fc_exch_mgr_reset(lp, sid, did);
1826 
1827 }
1828