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