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