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