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