scsi: fnic: Zero io_cmpl_skip on fw reset completion
[linux-2.6-microblaze.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_NOT_INITED] = "FNIC_IOREQ_NOT_INITED",
51         [FNIC_IOREQ_CMD_PENDING] = "FNIC_IOREQ_CMD_PENDING",
52         [FNIC_IOREQ_ABTS_PENDING] = "FNIC_IOREQ_ABTS_PENDING",
53         [FNIC_IOREQ_ABTS_COMPLETE] = "FNIC_IOREQ_ABTS_COMPLETE",
54         [FNIC_IOREQ_CMD_COMPLETE] = "FNIC_IOREQ_CMD_COMPLETE",
55 };
56
57 static const char *fcpio_status_str[] =  {
58         [FCPIO_SUCCESS] = "FCPIO_SUCCESS", /*0x0*/
59         [FCPIO_INVALID_HEADER] = "FCPIO_INVALID_HEADER",
60         [FCPIO_OUT_OF_RESOURCE] = "FCPIO_OUT_OF_RESOURCE",
61         [FCPIO_INVALID_PARAM] = "FCPIO_INVALID_PARAM]",
62         [FCPIO_REQ_NOT_SUPPORTED] = "FCPIO_REQ_NOT_SUPPORTED",
63         [FCPIO_IO_NOT_FOUND] = "FCPIO_IO_NOT_FOUND",
64         [FCPIO_ABORTED] = "FCPIO_ABORTED", /*0x41*/
65         [FCPIO_TIMEOUT] = "FCPIO_TIMEOUT",
66         [FCPIO_SGL_INVALID] = "FCPIO_SGL_INVALID",
67         [FCPIO_MSS_INVALID] = "FCPIO_MSS_INVALID",
68         [FCPIO_DATA_CNT_MISMATCH] = "FCPIO_DATA_CNT_MISMATCH",
69         [FCPIO_FW_ERR] = "FCPIO_FW_ERR",
70         [FCPIO_ITMF_REJECTED] = "FCPIO_ITMF_REJECTED",
71         [FCPIO_ITMF_FAILED] = "FCPIO_ITMF_FAILED",
72         [FCPIO_ITMF_INCORRECT_LUN] = "FCPIO_ITMF_INCORRECT_LUN",
73         [FCPIO_CMND_REJECTED] = "FCPIO_CMND_REJECTED",
74         [FCPIO_NO_PATH_AVAIL] = "FCPIO_NO_PATH_AVAIL",
75         [FCPIO_PATH_FAILED] = "FCPIO_PATH_FAILED",
76         [FCPIO_LUNMAP_CHNG_PEND] = "FCPIO_LUNHMAP_CHNG_PEND",
77 };
78
79 const char *fnic_state_to_str(unsigned int state)
80 {
81         if (state >= ARRAY_SIZE(fnic_state_str) || !fnic_state_str[state])
82                 return "unknown";
83
84         return fnic_state_str[state];
85 }
86
87 static const char *fnic_ioreq_state_to_str(unsigned int state)
88 {
89         if (state >= ARRAY_SIZE(fnic_ioreq_state_str) ||
90             !fnic_ioreq_state_str[state])
91                 return "unknown";
92
93         return fnic_ioreq_state_str[state];
94 }
95
96 static const char *fnic_fcpio_status_to_str(unsigned int status)
97 {
98         if (status >= ARRAY_SIZE(fcpio_status_str) || !fcpio_status_str[status])
99                 return "unknown";
100
101         return fcpio_status_str[status];
102 }
103
104 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id);
105
106 static inline spinlock_t *fnic_io_lock_hash(struct fnic *fnic,
107                                             struct scsi_cmnd *sc)
108 {
109         u32 hash = sc->request->tag & (FNIC_IO_LOCKS - 1);
110
111         return &fnic->io_req_lock[hash];
112 }
113
114 static inline spinlock_t *fnic_io_lock_tag(struct fnic *fnic,
115                                             int tag)
116 {
117         return &fnic->io_req_lock[tag & (FNIC_IO_LOCKS - 1)];
118 }
119
120 /*
121  * Unmap the data buffer and sense buffer for an io_req,
122  * also unmap and free the device-private scatter/gather list.
123  */
124 static void fnic_release_ioreq_buf(struct fnic *fnic,
125                                    struct fnic_io_req *io_req,
126                                    struct scsi_cmnd *sc)
127 {
128         if (io_req->sgl_list_pa)
129                 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
130                                  sizeof(io_req->sgl_list[0]) * io_req->sgl_cnt,
131                                  PCI_DMA_TODEVICE);
132         scsi_dma_unmap(sc);
133
134         if (io_req->sgl_cnt)
135                 mempool_free(io_req->sgl_list_alloc,
136                              fnic->io_sgl_pool[io_req->sgl_type]);
137         if (io_req->sense_buf_pa)
138                 pci_unmap_single(fnic->pdev, io_req->sense_buf_pa,
139                                  SCSI_SENSE_BUFFERSIZE, PCI_DMA_FROMDEVICE);
140 }
141
142 /* Free up Copy Wq descriptors. Called with copy_wq lock held */
143 static int free_wq_copy_descs(struct fnic *fnic, struct vnic_wq_copy *wq)
144 {
145         /* if no Ack received from firmware, then nothing to clean */
146         if (!fnic->fw_ack_recd[0])
147                 return 1;
148
149         /*
150          * Update desc_available count based on number of freed descriptors
151          * Account for wraparound
152          */
153         if (wq->to_clean_index <= fnic->fw_ack_index[0])
154                 wq->ring.desc_avail += (fnic->fw_ack_index[0]
155                                         - wq->to_clean_index + 1);
156         else
157                 wq->ring.desc_avail += (wq->ring.desc_count
158                                         - wq->to_clean_index
159                                         + fnic->fw_ack_index[0] + 1);
160
161         /*
162          * just bump clean index to ack_index+1 accounting for wraparound
163          * this will essentially free up all descriptors between
164          * to_clean_index and fw_ack_index, both inclusive
165          */
166         wq->to_clean_index =
167                 (fnic->fw_ack_index[0] + 1) % wq->ring.desc_count;
168
169         /* we have processed the acks received so far */
170         fnic->fw_ack_recd[0] = 0;
171         return 0;
172 }
173
174
175 /**
176  * __fnic_set_state_flags
177  * Sets/Clears bits in fnic's state_flags
178  **/
179 void
180 __fnic_set_state_flags(struct fnic *fnic, unsigned long st_flags,
181                         unsigned long clearbits)
182 {
183         struct Scsi_Host *host = fnic->lport->host;
184         int sh_locked = spin_is_locked(host->host_lock);
185         unsigned long flags = 0;
186
187         if (!sh_locked)
188                 spin_lock_irqsave(host->host_lock, flags);
189
190         if (clearbits)
191                 fnic->state_flags &= ~st_flags;
192         else
193                 fnic->state_flags |= st_flags;
194
195         if (!sh_locked)
196                 spin_unlock_irqrestore(host->host_lock, flags);
197
198         return;
199 }
200
201
202 /*
203  * fnic_fw_reset_handler
204  * Routine to send reset msg to fw
205  */
206 int fnic_fw_reset_handler(struct fnic *fnic)
207 {
208         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
209         int ret = 0;
210         unsigned long flags;
211
212         /* indicate fwreset to io path */
213         fnic_set_state_flags(fnic, FNIC_FLAGS_FWRESET);
214
215         skb_queue_purge(&fnic->frame_queue);
216         skb_queue_purge(&fnic->tx_queue);
217
218         /* wait for io cmpl */
219         while (atomic_read(&fnic->in_flight))
220                 schedule_timeout(msecs_to_jiffies(1));
221
222         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
223
224         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
225                 free_wq_copy_descs(fnic, wq);
226
227         if (!vnic_wq_copy_desc_avail(wq))
228                 ret = -EAGAIN;
229         else {
230                 fnic_queue_wq_copy_desc_fw_reset(wq, SCSI_NO_TAG);
231                 atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
232                 if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
233                           atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
234                         atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
235                                 atomic64_read(
236                                   &fnic->fnic_stats.fw_stats.active_fw_reqs));
237         }
238
239         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
240
241         if (!ret) {
242                 atomic64_inc(&fnic->fnic_stats.reset_stats.fw_resets);
243                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
244                               "Issued fw reset\n");
245         } else {
246                 fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
247                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
248                               "Failed to issue fw reset\n");
249         }
250
251         return ret;
252 }
253
254
255 /*
256  * fnic_flogi_reg_handler
257  * Routine to send flogi register msg to fw
258  */
259 int fnic_flogi_reg_handler(struct fnic *fnic, u32 fc_id)
260 {
261         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
262         enum fcpio_flogi_reg_format_type format;
263         struct fc_lport *lp = fnic->lport;
264         u8 gw_mac[ETH_ALEN];
265         int ret = 0;
266         unsigned long flags;
267
268         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
269
270         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
271                 free_wq_copy_descs(fnic, wq);
272
273         if (!vnic_wq_copy_desc_avail(wq)) {
274                 ret = -EAGAIN;
275                 goto flogi_reg_ioreq_end;
276         }
277
278         if (fnic->ctlr.map_dest) {
279                 memset(gw_mac, 0xff, ETH_ALEN);
280                 format = FCPIO_FLOGI_REG_DEF_DEST;
281         } else {
282                 memcpy(gw_mac, fnic->ctlr.dest_addr, ETH_ALEN);
283                 format = FCPIO_FLOGI_REG_GW_DEST;
284         }
285
286         if ((fnic->config.flags & VFCF_FIP_CAPABLE) && !fnic->ctlr.map_dest) {
287                 fnic_queue_wq_copy_desc_fip_reg(wq, SCSI_NO_TAG,
288                                                 fc_id, gw_mac,
289                                                 fnic->data_src_addr,
290                                                 lp->r_a_tov, lp->e_d_tov);
291                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
292                               "FLOGI FIP reg issued fcid %x src %pM dest %pM\n",
293                               fc_id, fnic->data_src_addr, gw_mac);
294         } else {
295                 fnic_queue_wq_copy_desc_flogi_reg(wq, SCSI_NO_TAG,
296                                                   format, fc_id, gw_mac);
297                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
298                               "FLOGI reg issued fcid %x map %d dest %pM\n",
299                               fc_id, fnic->ctlr.map_dest, gw_mac);
300         }
301
302         atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
303         if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
304                   atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
305                 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
306                   atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
307
308 flogi_reg_ioreq_end:
309         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
310         return ret;
311 }
312
313 /*
314  * fnic_queue_wq_copy_desc
315  * Routine to enqueue a wq copy desc
316  */
317 static inline int fnic_queue_wq_copy_desc(struct fnic *fnic,
318                                           struct vnic_wq_copy *wq,
319                                           struct fnic_io_req *io_req,
320                                           struct scsi_cmnd *sc,
321                                           int sg_count)
322 {
323         struct scatterlist *sg;
324         struct fc_rport *rport = starget_to_rport(scsi_target(sc->device));
325         struct fc_rport_libfc_priv *rp = rport->dd_data;
326         struct host_sg_desc *desc;
327         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
328         unsigned int i;
329         unsigned long intr_flags;
330         int flags;
331         u8 exch_flags;
332         struct scsi_lun fc_lun;
333         int r;
334
335         if (sg_count) {
336                 /* For each SGE, create a device desc entry */
337                 desc = io_req->sgl_list;
338                 for_each_sg(scsi_sglist(sc), sg, sg_count, i) {
339                         desc->addr = cpu_to_le64(sg_dma_address(sg));
340                         desc->len = cpu_to_le32(sg_dma_len(sg));
341                         desc->_resvd = 0;
342                         desc++;
343                 }
344
345                 io_req->sgl_list_pa = pci_map_single
346                         (fnic->pdev,
347                          io_req->sgl_list,
348                          sizeof(io_req->sgl_list[0]) * sg_count,
349                          PCI_DMA_TODEVICE);
350
351                 r = pci_dma_mapping_error(fnic->pdev, io_req->sgl_list_pa);
352                 if (r) {
353                         printk(KERN_ERR "PCI mapping failed with error %d\n", r);
354                         return SCSI_MLQUEUE_HOST_BUSY;
355                 }
356         }
357
358         io_req->sense_buf_pa = pci_map_single(fnic->pdev,
359                                               sc->sense_buffer,
360                                               SCSI_SENSE_BUFFERSIZE,
361                                               PCI_DMA_FROMDEVICE);
362
363         r = pci_dma_mapping_error(fnic->pdev, io_req->sense_buf_pa);
364         if (r) {
365                 pci_unmap_single(fnic->pdev, io_req->sgl_list_pa,
366                                 sizeof(io_req->sgl_list[0]) * sg_count,
367                                 PCI_DMA_TODEVICE);
368                 printk(KERN_ERR "PCI mapping failed with error %d\n", r);
369                 return SCSI_MLQUEUE_HOST_BUSY;
370         }
371
372         int_to_scsilun(sc->device->lun, &fc_lun);
373
374         /* Enqueue the descriptor in the Copy WQ */
375         spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
376
377         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
378                 free_wq_copy_descs(fnic, wq);
379
380         if (unlikely(!vnic_wq_copy_desc_avail(wq))) {
381                 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
382                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
383                           "fnic_queue_wq_copy_desc failure - no descriptors\n");
384                 atomic64_inc(&misc_stats->io_cpwq_alloc_failures);
385                 return SCSI_MLQUEUE_HOST_BUSY;
386         }
387
388         flags = 0;
389         if (sc->sc_data_direction == DMA_FROM_DEVICE)
390                 flags = FCPIO_ICMND_RDDATA;
391         else if (sc->sc_data_direction == DMA_TO_DEVICE)
392                 flags = FCPIO_ICMND_WRDATA;
393
394         exch_flags = 0;
395         if ((fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR) &&
396             (rp->flags & FC_RP_FLAGS_RETRY))
397                 exch_flags |= FCPIO_ICMND_SRFLAG_RETRY;
398
399         fnic_queue_wq_copy_desc_icmnd_16(wq, sc->request->tag,
400                                          0, exch_flags, io_req->sgl_cnt,
401                                          SCSI_SENSE_BUFFERSIZE,
402                                          io_req->sgl_list_pa,
403                                          io_req->sense_buf_pa,
404                                          0, /* scsi cmd ref, always 0 */
405                                          FCPIO_ICMND_PTA_SIMPLE,
406                                                 /* scsi pri and tag */
407                                          flags, /* command flags */
408                                          sc->cmnd, sc->cmd_len,
409                                          scsi_bufflen(sc),
410                                          fc_lun.scsi_lun, io_req->port_id,
411                                          rport->maxframe_size, rp->r_a_tov,
412                                          rp->e_d_tov);
413
414         atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
415         if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
416                   atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
417                 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
418                   atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
419
420         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
421         return 0;
422 }
423
424 /*
425  * fnic_queuecommand
426  * Routine to send a scsi cdb
427  * Called with host_lock held and interrupts disabled.
428  */
429 static int fnic_queuecommand_lck(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
430 {
431         struct fc_lport *lp = shost_priv(sc->device->host);
432         struct fc_rport *rport;
433         struct fnic_io_req *io_req = NULL;
434         struct fnic *fnic = lport_priv(lp);
435         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
436         struct vnic_wq_copy *wq;
437         int ret;
438         u64 cmd_trace;
439         int sg_count = 0;
440         unsigned long flags = 0;
441         unsigned long ptr;
442         spinlock_t *io_lock = NULL;
443         int io_lock_acquired = 0;
444         struct fc_rport_libfc_priv *rp;
445
446         if (unlikely(fnic_chk_state_flags_locked(fnic, FNIC_FLAGS_IO_BLOCKED)))
447                 return SCSI_MLQUEUE_HOST_BUSY;
448
449         rport = starget_to_rport(scsi_target(sc->device));
450         if (!rport) {
451                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
452                                 "returning DID_NO_CONNECT for IO as rport is NULL\n");
453                 sc->result = DID_NO_CONNECT << 16;
454                 done(sc);
455                 return 0;
456         }
457
458         ret = fc_remote_port_chkready(rport);
459         if (ret) {
460                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
461                                 "rport is not ready\n");
462                 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
463                 sc->result = ret;
464                 done(sc);
465                 return 0;
466         }
467
468         rp = rport->dd_data;
469         if (!rp || rp->rp_state != RPORT_ST_READY) {
470                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
471                                 "returning DID_NO_CONNECT for IO as rport is removed\n");
472                 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
473                 sc->result = DID_NO_CONNECT<<16;
474                 done(sc);
475                 return 0;
476         }
477
478         if (lp->state != LPORT_ST_READY || !(lp->link_up))
479                 return SCSI_MLQUEUE_HOST_BUSY;
480
481         atomic_inc(&fnic->in_flight);
482
483         /*
484          * Release host lock, use driver resource specific locks from here.
485          * Don't re-enable interrupts in case they were disabled prior to the
486          * caller disabling them.
487          */
488         spin_unlock(lp->host->host_lock);
489         CMD_STATE(sc) = FNIC_IOREQ_NOT_INITED;
490         CMD_FLAGS(sc) = FNIC_NO_FLAGS;
491
492         /* Get a new io_req for this SCSI IO */
493         io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
494         if (!io_req) {
495                 atomic64_inc(&fnic_stats->io_stats.alloc_failures);
496                 ret = SCSI_MLQUEUE_HOST_BUSY;
497                 goto out;
498         }
499         memset(io_req, 0, sizeof(*io_req));
500
501         /* Map the data buffer */
502         sg_count = scsi_dma_map(sc);
503         if (sg_count < 0) {
504                 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
505                           sc->request->tag, sc, 0, sc->cmnd[0],
506                           sg_count, CMD_STATE(sc));
507                 mempool_free(io_req, fnic->io_req_pool);
508                 goto out;
509         }
510
511         /* Determine the type of scatter/gather list we need */
512         io_req->sgl_cnt = sg_count;
513         io_req->sgl_type = FNIC_SGL_CACHE_DFLT;
514         if (sg_count > FNIC_DFLT_SG_DESC_CNT)
515                 io_req->sgl_type = FNIC_SGL_CACHE_MAX;
516
517         if (sg_count) {
518                 io_req->sgl_list =
519                         mempool_alloc(fnic->io_sgl_pool[io_req->sgl_type],
520                                       GFP_ATOMIC);
521                 if (!io_req->sgl_list) {
522                         atomic64_inc(&fnic_stats->io_stats.alloc_failures);
523                         ret = SCSI_MLQUEUE_HOST_BUSY;
524                         scsi_dma_unmap(sc);
525                         mempool_free(io_req, fnic->io_req_pool);
526                         goto out;
527                 }
528
529                 /* Cache sgl list allocated address before alignment */
530                 io_req->sgl_list_alloc = io_req->sgl_list;
531                 ptr = (unsigned long) io_req->sgl_list;
532                 if (ptr % FNIC_SG_DESC_ALIGN) {
533                         io_req->sgl_list = (struct host_sg_desc *)
534                                 (((unsigned long) ptr
535                                   + FNIC_SG_DESC_ALIGN - 1)
536                                  & ~(FNIC_SG_DESC_ALIGN - 1));
537                 }
538         }
539
540         /*
541         * Will acquire lock defore setting to IO initialized.
542         */
543
544         io_lock = fnic_io_lock_hash(fnic, sc);
545         spin_lock_irqsave(io_lock, flags);
546
547         /* initialize rest of io_req */
548         io_lock_acquired = 1;
549         io_req->port_id = rport->port_id;
550         io_req->start_time = jiffies;
551         CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
552         CMD_SP(sc) = (char *)io_req;
553         CMD_FLAGS(sc) |= FNIC_IO_INITIALIZED;
554         sc->scsi_done = done;
555
556         /* create copy wq desc and enqueue it */
557         wq = &fnic->wq_copy[0];
558         ret = fnic_queue_wq_copy_desc(fnic, wq, io_req, sc, sg_count);
559         if (ret) {
560                 /*
561                  * In case another thread cancelled the request,
562                  * refetch the pointer under the lock.
563                  */
564                 FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
565                           sc->request->tag, sc, 0, 0, 0,
566                           (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
567                 io_req = (struct fnic_io_req *)CMD_SP(sc);
568                 CMD_SP(sc) = NULL;
569                 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
570                 spin_unlock_irqrestore(io_lock, flags);
571                 if (io_req) {
572                         fnic_release_ioreq_buf(fnic, io_req, sc);
573                         mempool_free(io_req, fnic->io_req_pool);
574                 }
575                 atomic_dec(&fnic->in_flight);
576                 /* acquire host lock before returning to SCSI */
577                 spin_lock(lp->host->host_lock);
578                 return ret;
579         } else {
580                 atomic64_inc(&fnic_stats->io_stats.active_ios);
581                 atomic64_inc(&fnic_stats->io_stats.num_ios);
582                 if (atomic64_read(&fnic_stats->io_stats.active_ios) >
583                           atomic64_read(&fnic_stats->io_stats.max_active_ios))
584                         atomic64_set(&fnic_stats->io_stats.max_active_ios,
585                              atomic64_read(&fnic_stats->io_stats.active_ios));
586
587                 /* REVISIT: Use per IO lock in the final code */
588                 CMD_FLAGS(sc) |= FNIC_IO_ISSUED;
589         }
590 out:
591         cmd_trace = ((u64)sc->cmnd[0] << 56 | (u64)sc->cmnd[7] << 40 |
592                         (u64)sc->cmnd[8] << 32 | (u64)sc->cmnd[2] << 24 |
593                         (u64)sc->cmnd[3] << 16 | (u64)sc->cmnd[4] << 8 |
594                         sc->cmnd[5]);
595
596         FNIC_TRACE(fnic_queuecommand, sc->device->host->host_no,
597                   sc->request->tag, sc, io_req,
598                   sg_count, cmd_trace,
599                   (((u64)CMD_FLAGS(sc) >> 32) | CMD_STATE(sc)));
600
601         /* if only we issued IO, will we have the io lock */
602         if (io_lock_acquired)
603                 spin_unlock_irqrestore(io_lock, flags);
604
605         atomic_dec(&fnic->in_flight);
606         /* acquire host lock before returning to SCSI */
607         spin_lock(lp->host->host_lock);
608         return ret;
609 }
610
611 DEF_SCSI_QCMD(fnic_queuecommand)
612
613 /*
614  * fnic_fcpio_fw_reset_cmpl_handler
615  * Routine to handle fw reset completion
616  */
617 static int fnic_fcpio_fw_reset_cmpl_handler(struct fnic *fnic,
618                                             struct fcpio_fw_req *desc)
619 {
620         u8 type;
621         u8 hdr_status;
622         struct fcpio_tag tag;
623         int ret = 0;
624         unsigned long flags;
625         struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
626
627         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
628
629         atomic64_inc(&reset_stats->fw_reset_completions);
630
631         /* Clean up all outstanding io requests */
632         fnic_cleanup_io(fnic, SCSI_NO_TAG);
633
634         atomic64_set(&fnic->fnic_stats.fw_stats.active_fw_reqs, 0);
635         atomic64_set(&fnic->fnic_stats.io_stats.active_ios, 0);
636         atomic64_set(&fnic->io_cmpl_skip, 0);
637
638         spin_lock_irqsave(&fnic->fnic_lock, flags);
639
640         /* fnic should be in FC_TRANS_ETH_MODE */
641         if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE) {
642                 /* Check status of reset completion */
643                 if (!hdr_status) {
644                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
645                                       "reset cmpl success\n");
646                         /* Ready to send flogi out */
647                         fnic->state = FNIC_IN_ETH_MODE;
648                 } else {
649                         FNIC_SCSI_DBG(KERN_DEBUG,
650                                       fnic->lport->host,
651                                       "fnic fw_reset : failed %s\n",
652                                       fnic_fcpio_status_to_str(hdr_status));
653
654                         /*
655                          * Unable to change to eth mode, cannot send out flogi
656                          * Change state to fc mode, so that subsequent Flogi
657                          * requests from libFC will cause more attempts to
658                          * reset the firmware. Free the cached flogi
659                          */
660                         fnic->state = FNIC_IN_FC_MODE;
661                         atomic64_inc(&reset_stats->fw_reset_failures);
662                         ret = -1;
663                 }
664         } else {
665                 FNIC_SCSI_DBG(KERN_DEBUG,
666                               fnic->lport->host,
667                               "Unexpected state %s while processing"
668                               " reset cmpl\n", fnic_state_to_str(fnic->state));
669                 atomic64_inc(&reset_stats->fw_reset_failures);
670                 ret = -1;
671         }
672
673         /* Thread removing device blocks till firmware reset is complete */
674         if (fnic->remove_wait)
675                 complete(fnic->remove_wait);
676
677         /*
678          * If fnic is being removed, or fw reset failed
679          * free the flogi frame. Else, send it out
680          */
681         if (fnic->remove_wait || ret) {
682                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
683                 skb_queue_purge(&fnic->tx_queue);
684                 goto reset_cmpl_handler_end;
685         }
686
687         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
688
689         fnic_flush_tx(fnic);
690
691  reset_cmpl_handler_end:
692         fnic_clear_state_flags(fnic, FNIC_FLAGS_FWRESET);
693
694         return ret;
695 }
696
697 /*
698  * fnic_fcpio_flogi_reg_cmpl_handler
699  * Routine to handle flogi register completion
700  */
701 static int fnic_fcpio_flogi_reg_cmpl_handler(struct fnic *fnic,
702                                              struct fcpio_fw_req *desc)
703 {
704         u8 type;
705         u8 hdr_status;
706         struct fcpio_tag tag;
707         int ret = 0;
708         unsigned long flags;
709
710         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
711
712         /* Update fnic state based on status of flogi reg completion */
713         spin_lock_irqsave(&fnic->fnic_lock, flags);
714
715         if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE) {
716
717                 /* Check flogi registration completion status */
718                 if (!hdr_status) {
719                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
720                                       "flog reg succeeded\n");
721                         fnic->state = FNIC_IN_FC_MODE;
722                 } else {
723                         FNIC_SCSI_DBG(KERN_DEBUG,
724                                       fnic->lport->host,
725                                       "fnic flogi reg :failed %s\n",
726                                       fnic_fcpio_status_to_str(hdr_status));
727                         fnic->state = FNIC_IN_ETH_MODE;
728                         ret = -1;
729                 }
730         } else {
731                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
732                               "Unexpected fnic state %s while"
733                               " processing flogi reg completion\n",
734                               fnic_state_to_str(fnic->state));
735                 ret = -1;
736         }
737
738         if (!ret) {
739                 if (fnic->stop_rx_link_events) {
740                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
741                         goto reg_cmpl_handler_end;
742                 }
743                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
744
745                 fnic_flush_tx(fnic);
746                 queue_work(fnic_event_queue, &fnic->frame_work);
747         } else {
748                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
749         }
750
751 reg_cmpl_handler_end:
752         return ret;
753 }
754
755 static inline int is_ack_index_in_range(struct vnic_wq_copy *wq,
756                                         u16 request_out)
757 {
758         if (wq->to_clean_index <= wq->to_use_index) {
759                 /* out of range, stale request_out index */
760                 if (request_out < wq->to_clean_index ||
761                     request_out >= wq->to_use_index)
762                         return 0;
763         } else {
764                 /* out of range, stale request_out index */
765                 if (request_out < wq->to_clean_index &&
766                     request_out >= wq->to_use_index)
767                         return 0;
768         }
769         /* request_out index is in range */
770         return 1;
771 }
772
773
774 /*
775  * Mark that ack received and store the Ack index. If there are multiple
776  * acks received before Tx thread cleans it up, the latest value will be
777  * used which is correct behavior. This state should be in the copy Wq
778  * instead of in the fnic
779  */
780 static inline void fnic_fcpio_ack_handler(struct fnic *fnic,
781                                           unsigned int cq_index,
782                                           struct fcpio_fw_req *desc)
783 {
784         struct vnic_wq_copy *wq;
785         u16 request_out = desc->u.ack.request_out;
786         unsigned long flags;
787         u64 *ox_id_tag = (u64 *)(void *)desc;
788
789         /* mark the ack state */
790         wq = &fnic->wq_copy[cq_index - fnic->raw_wq_count - fnic->rq_count];
791         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
792
793         fnic->fnic_stats.misc_stats.last_ack_time = jiffies;
794         if (is_ack_index_in_range(wq, request_out)) {
795                 fnic->fw_ack_index[0] = request_out;
796                 fnic->fw_ack_recd[0] = 1;
797         } else
798                 atomic64_inc(
799                         &fnic->fnic_stats.misc_stats.ack_index_out_of_range);
800
801         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
802         FNIC_TRACE(fnic_fcpio_ack_handler,
803                   fnic->lport->host->host_no, 0, 0, ox_id_tag[2], ox_id_tag[3],
804                   ox_id_tag[4], ox_id_tag[5]);
805 }
806
807 /*
808  * fnic_fcpio_icmnd_cmpl_handler
809  * Routine to handle icmnd completions
810  */
811 static void fnic_fcpio_icmnd_cmpl_handler(struct fnic *fnic,
812                                          struct fcpio_fw_req *desc)
813 {
814         u8 type;
815         u8 hdr_status;
816         struct fcpio_tag tag;
817         u32 id;
818         u64 xfer_len = 0;
819         struct fcpio_icmnd_cmpl *icmnd_cmpl;
820         struct fnic_io_req *io_req;
821         struct scsi_cmnd *sc;
822         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
823         unsigned long flags;
824         spinlock_t *io_lock;
825         u64 cmd_trace;
826         unsigned long start_time;
827         unsigned long io_duration_time;
828
829         /* Decode the cmpl description to get the io_req id */
830         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
831         fcpio_tag_id_dec(&tag, &id);
832         icmnd_cmpl = &desc->u.icmnd_cmpl;
833
834         if (id >= fnic->fnic_max_tag_id) {
835                 shost_printk(KERN_ERR, fnic->lport->host,
836                         "Tag out of range tag %x hdr status = %s\n",
837                              id, fnic_fcpio_status_to_str(hdr_status));
838                 return;
839         }
840
841         sc = scsi_host_find_tag(fnic->lport->host, id);
842         WARN_ON_ONCE(!sc);
843         if (!sc) {
844                 atomic64_inc(&fnic_stats->io_stats.sc_null);
845                 shost_printk(KERN_ERR, fnic->lport->host,
846                           "icmnd_cmpl sc is null - "
847                           "hdr status = %s tag = 0x%x desc = 0x%p\n",
848                           fnic_fcpio_status_to_str(hdr_status), id, desc);
849                 FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
850                           fnic->lport->host->host_no, id,
851                           ((u64)icmnd_cmpl->_resvd0[1] << 16 |
852                           (u64)icmnd_cmpl->_resvd0[0]),
853                           ((u64)hdr_status << 16 |
854                           (u64)icmnd_cmpl->scsi_status << 8 |
855                           (u64)icmnd_cmpl->flags), desc,
856                           (u64)icmnd_cmpl->residual, 0);
857                 return;
858         }
859
860         io_lock = fnic_io_lock_hash(fnic, sc);
861         spin_lock_irqsave(io_lock, flags);
862         io_req = (struct fnic_io_req *)CMD_SP(sc);
863         WARN_ON_ONCE(!io_req);
864         if (!io_req) {
865                 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
866                 CMD_FLAGS(sc) |= FNIC_IO_REQ_NULL;
867                 spin_unlock_irqrestore(io_lock, flags);
868                 shost_printk(KERN_ERR, fnic->lport->host,
869                           "icmnd_cmpl io_req is null - "
870                           "hdr status = %s tag = 0x%x sc 0x%p\n",
871                           fnic_fcpio_status_to_str(hdr_status), id, sc);
872                 return;
873         }
874         start_time = io_req->start_time;
875
876         /* firmware completed the io */
877         io_req->io_completed = 1;
878
879         /*
880          *  if SCSI-ML has already issued abort on this command,
881          *  set completion of the IO. The abts path will clean it up
882          */
883         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
884
885                 /*
886                  * set the FNIC_IO_DONE so that this doesn't get
887                  * flagged as 'out of order' if it was not aborted
888                  */
889                 CMD_FLAGS(sc) |= FNIC_IO_DONE;
890                 CMD_FLAGS(sc) |= FNIC_IO_ABTS_PENDING;
891                 spin_unlock_irqrestore(io_lock, flags);
892                 if(FCPIO_ABORTED == hdr_status)
893                         CMD_FLAGS(sc) |= FNIC_IO_ABORTED;
894
895                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
896                         "icmnd_cmpl abts pending "
897                           "hdr status = %s tag = 0x%x sc = 0x%p"
898                           "scsi_status = %x residual = %d\n",
899                           fnic_fcpio_status_to_str(hdr_status),
900                           id, sc,
901                           icmnd_cmpl->scsi_status,
902                           icmnd_cmpl->residual);
903                 return;
904         }
905
906         /* Mark the IO as complete */
907         CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
908
909         icmnd_cmpl = &desc->u.icmnd_cmpl;
910
911         switch (hdr_status) {
912         case FCPIO_SUCCESS:
913                 sc->result = (DID_OK << 16) | icmnd_cmpl->scsi_status;
914                 xfer_len = scsi_bufflen(sc);
915                 scsi_set_resid(sc, icmnd_cmpl->residual);
916
917                 if (icmnd_cmpl->flags & FCPIO_ICMND_CMPL_RESID_UNDER)
918                         xfer_len -= icmnd_cmpl->residual;
919
920                 if (icmnd_cmpl->scsi_status == SAM_STAT_CHECK_CONDITION)
921                         atomic64_inc(&fnic_stats->misc_stats.check_condition);
922
923                 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
924                         atomic64_inc(&fnic_stats->misc_stats.queue_fulls);
925                 break;
926
927         case FCPIO_TIMEOUT:          /* request was timed out */
928                 atomic64_inc(&fnic_stats->misc_stats.fcpio_timeout);
929                 sc->result = (DID_TIME_OUT << 16) | icmnd_cmpl->scsi_status;
930                 break;
931
932         case FCPIO_ABORTED:          /* request was aborted */
933                 atomic64_inc(&fnic_stats->misc_stats.fcpio_aborted);
934                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
935                 break;
936
937         case FCPIO_DATA_CNT_MISMATCH: /* recv/sent more/less data than exp. */
938                 atomic64_inc(&fnic_stats->misc_stats.data_count_mismatch);
939                 scsi_set_resid(sc, icmnd_cmpl->residual);
940                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
941                 break;
942
943         case FCPIO_OUT_OF_RESOURCE:  /* out of resources to complete request */
944                 atomic64_inc(&fnic_stats->fw_stats.fw_out_of_resources);
945                 sc->result = (DID_REQUEUE << 16) | icmnd_cmpl->scsi_status;
946                 break;
947
948         case FCPIO_IO_NOT_FOUND:     /* requested I/O was not found */
949                 atomic64_inc(&fnic_stats->io_stats.io_not_found);
950                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
951                 break;
952
953         case FCPIO_SGL_INVALID:      /* request was aborted due to sgl error */
954                 atomic64_inc(&fnic_stats->misc_stats.sgl_invalid);
955                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
956                 break;
957
958         case FCPIO_FW_ERR:           /* request was terminated due fw error */
959                 atomic64_inc(&fnic_stats->fw_stats.io_fw_errs);
960                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
961                 break;
962
963         case FCPIO_MSS_INVALID:      /* request was aborted due to mss error */
964                 atomic64_inc(&fnic_stats->misc_stats.mss_invalid);
965                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
966                 break;
967
968         case FCPIO_INVALID_HEADER:   /* header contains invalid data */
969         case FCPIO_INVALID_PARAM:    /* some parameter in request invalid */
970         case FCPIO_REQ_NOT_SUPPORTED:/* request type is not supported */
971         default:
972                 sc->result = (DID_ERROR << 16) | icmnd_cmpl->scsi_status;
973                 break;
974         }
975
976         /* Break link with the SCSI command */
977         CMD_SP(sc) = NULL;
978         CMD_FLAGS(sc) |= FNIC_IO_DONE;
979
980         spin_unlock_irqrestore(io_lock, flags);
981
982         if (hdr_status != FCPIO_SUCCESS) {
983                 atomic64_inc(&fnic_stats->io_stats.io_failures);
984                 shost_printk(KERN_ERR, fnic->lport->host, "hdr status = %s\n",
985                              fnic_fcpio_status_to_str(hdr_status));
986         }
987
988         fnic_release_ioreq_buf(fnic, io_req, sc);
989
990         mempool_free(io_req, fnic->io_req_pool);
991
992         cmd_trace = ((u64)hdr_status << 56) |
993                   (u64)icmnd_cmpl->scsi_status << 48 |
994                   (u64)icmnd_cmpl->flags << 40 | (u64)sc->cmnd[0] << 32 |
995                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
996                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5];
997
998         FNIC_TRACE(fnic_fcpio_icmnd_cmpl_handler,
999                   sc->device->host->host_no, id, sc,
1000                   ((u64)icmnd_cmpl->_resvd0[1] << 56 |
1001                   (u64)icmnd_cmpl->_resvd0[0] << 48 |
1002                   jiffies_to_msecs(jiffies - start_time)),
1003                   desc, cmd_trace,
1004                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1005
1006         if (sc->sc_data_direction == DMA_FROM_DEVICE) {
1007                 fnic->lport->host_stats.fcp_input_requests++;
1008                 fnic->fcp_input_bytes += xfer_len;
1009         } else if (sc->sc_data_direction == DMA_TO_DEVICE) {
1010                 fnic->lport->host_stats.fcp_output_requests++;
1011                 fnic->fcp_output_bytes += xfer_len;
1012         } else
1013                 fnic->lport->host_stats.fcp_control_requests++;
1014
1015         atomic64_dec(&fnic_stats->io_stats.active_ios);
1016         if (atomic64_read(&fnic->io_cmpl_skip))
1017                 atomic64_dec(&fnic->io_cmpl_skip);
1018         else
1019                 atomic64_inc(&fnic_stats->io_stats.io_completions);
1020
1021
1022         io_duration_time = jiffies_to_msecs(jiffies) - jiffies_to_msecs(io_req->start_time);
1023
1024         if(io_duration_time <= 10)
1025                 atomic64_inc(&fnic_stats->io_stats.io_btw_0_to_10_msec);
1026         else if(io_duration_time <= 100)
1027                 atomic64_inc(&fnic_stats->io_stats.io_btw_10_to_100_msec);
1028         else if(io_duration_time <= 500)
1029                 atomic64_inc(&fnic_stats->io_stats.io_btw_100_to_500_msec);
1030         else if(io_duration_time <= 5000)
1031                 atomic64_inc(&fnic_stats->io_stats.io_btw_500_to_5000_msec);
1032         else if(io_duration_time <= 10000)
1033                 atomic64_inc(&fnic_stats->io_stats.io_btw_5000_to_10000_msec);
1034         else if(io_duration_time <= 30000)
1035                 atomic64_inc(&fnic_stats->io_stats.io_btw_10000_to_30000_msec);
1036         else {
1037                 atomic64_inc(&fnic_stats->io_stats.io_greater_than_30000_msec);
1038
1039                 if(io_duration_time > atomic64_read(&fnic_stats->io_stats.current_max_io_time))
1040                         atomic64_set(&fnic_stats->io_stats.current_max_io_time, io_duration_time);
1041         }
1042
1043         /* Call SCSI completion function to complete the IO */
1044         if (sc->scsi_done)
1045                 sc->scsi_done(sc);
1046 }
1047
1048 /* fnic_fcpio_itmf_cmpl_handler
1049  * Routine to handle itmf completions
1050  */
1051 static void fnic_fcpio_itmf_cmpl_handler(struct fnic *fnic,
1052                                         struct fcpio_fw_req *desc)
1053 {
1054         u8 type;
1055         u8 hdr_status;
1056         struct fcpio_tag tag;
1057         u32 id;
1058         struct scsi_cmnd *sc;
1059         struct fnic_io_req *io_req;
1060         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1061         struct abort_stats *abts_stats = &fnic->fnic_stats.abts_stats;
1062         struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1063         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1064         unsigned long flags;
1065         spinlock_t *io_lock;
1066         unsigned long start_time;
1067
1068         fcpio_header_dec(&desc->hdr, &type, &hdr_status, &tag);
1069         fcpio_tag_id_dec(&tag, &id);
1070
1071         if ((id & FNIC_TAG_MASK) >= fnic->fnic_max_tag_id) {
1072                 shost_printk(KERN_ERR, fnic->lport->host,
1073                 "Tag out of range tag %x hdr status = %s\n",
1074                 id, fnic_fcpio_status_to_str(hdr_status));
1075                 return;
1076         }
1077
1078         sc = scsi_host_find_tag(fnic->lport->host, id & FNIC_TAG_MASK);
1079         WARN_ON_ONCE(!sc);
1080         if (!sc) {
1081                 atomic64_inc(&fnic_stats->io_stats.sc_null);
1082                 shost_printk(KERN_ERR, fnic->lport->host,
1083                           "itmf_cmpl sc is null - hdr status = %s tag = 0x%x\n",
1084                           fnic_fcpio_status_to_str(hdr_status), id);
1085                 return;
1086         }
1087         io_lock = fnic_io_lock_hash(fnic, sc);
1088         spin_lock_irqsave(io_lock, flags);
1089         io_req = (struct fnic_io_req *)CMD_SP(sc);
1090         WARN_ON_ONCE(!io_req);
1091         if (!io_req) {
1092                 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1093                 spin_unlock_irqrestore(io_lock, flags);
1094                 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1095                 shost_printk(KERN_ERR, fnic->lport->host,
1096                           "itmf_cmpl io_req is null - "
1097                           "hdr status = %s tag = 0x%x sc 0x%p\n",
1098                           fnic_fcpio_status_to_str(hdr_status), id, sc);
1099                 return;
1100         }
1101         start_time = io_req->start_time;
1102
1103         if ((id & FNIC_TAG_ABORT) && (id & FNIC_TAG_DEV_RST)) {
1104                 /* Abort and terminate completion of device reset req */
1105                 /* REVISIT : Add asserts about various flags */
1106                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1107                               "dev reset abts cmpl recd. id %x status %s\n",
1108                               id, fnic_fcpio_status_to_str(hdr_status));
1109                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1110                 CMD_ABTS_STATUS(sc) = hdr_status;
1111                 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1112                 if (io_req->abts_done)
1113                         complete(io_req->abts_done);
1114                 spin_unlock_irqrestore(io_lock, flags);
1115         } else if (id & FNIC_TAG_ABORT) {
1116                 /* Completion of abort cmd */
1117                 switch (hdr_status) {
1118                 case FCPIO_SUCCESS:
1119                         break;
1120                 case FCPIO_TIMEOUT:
1121                         if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1122                                 atomic64_inc(&abts_stats->abort_fw_timeouts);
1123                         else
1124                                 atomic64_inc(
1125                                         &term_stats->terminate_fw_timeouts);
1126                         break;
1127                 case FCPIO_ITMF_REJECTED:
1128                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1129                                 "abort reject recd. id %d\n",
1130                                 (int)(id & FNIC_TAG_MASK));
1131                         break;
1132                 case FCPIO_IO_NOT_FOUND:
1133                         if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1134                                 atomic64_inc(&abts_stats->abort_io_not_found);
1135                         else
1136                                 atomic64_inc(
1137                                         &term_stats->terminate_io_not_found);
1138                         break;
1139                 default:
1140                         if (CMD_FLAGS(sc) & FNIC_IO_ABTS_ISSUED)
1141                                 atomic64_inc(&abts_stats->abort_failures);
1142                         else
1143                                 atomic64_inc(
1144                                         &term_stats->terminate_failures);
1145                         break;
1146                 }
1147                 if (CMD_STATE(sc) != FNIC_IOREQ_ABTS_PENDING) {
1148                         /* This is a late completion. Ignore it */
1149                         spin_unlock_irqrestore(io_lock, flags);
1150                         return;
1151                 }
1152
1153                 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
1154                 CMD_ABTS_STATUS(sc) = hdr_status;
1155
1156                 /* If the status is IO not found consider it as success */
1157                 if (hdr_status == FCPIO_IO_NOT_FOUND)
1158                         CMD_ABTS_STATUS(sc) = FCPIO_SUCCESS;
1159
1160                 if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE)))
1161                         atomic64_inc(&misc_stats->no_icmnd_itmf_cmpls);
1162
1163                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1164                               "abts cmpl recd. id %d status %s\n",
1165                               (int)(id & FNIC_TAG_MASK),
1166                               fnic_fcpio_status_to_str(hdr_status));
1167
1168                 /*
1169                  * If scsi_eh thread is blocked waiting for abts to complete,
1170                  * signal completion to it. IO will be cleaned in the thread
1171                  * else clean it in this context
1172                  */
1173                 if (io_req->abts_done) {
1174                         complete(io_req->abts_done);
1175                         spin_unlock_irqrestore(io_lock, flags);
1176                 } else {
1177                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1178                                       "abts cmpl, completing IO\n");
1179                         CMD_SP(sc) = NULL;
1180                         sc->result = (DID_ERROR << 16);
1181
1182                         spin_unlock_irqrestore(io_lock, flags);
1183
1184                         fnic_release_ioreq_buf(fnic, io_req, sc);
1185                         mempool_free(io_req, fnic->io_req_pool);
1186                         if (sc->scsi_done) {
1187                                 FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1188                                         sc->device->host->host_no, id,
1189                                         sc,
1190                                         jiffies_to_msecs(jiffies - start_time),
1191                                         desc,
1192                                         (((u64)hdr_status << 40) |
1193                                         (u64)sc->cmnd[0] << 32 |
1194                                         (u64)sc->cmnd[2] << 24 |
1195                                         (u64)sc->cmnd[3] << 16 |
1196                                         (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1197                                         (((u64)CMD_FLAGS(sc) << 32) |
1198                                         CMD_STATE(sc)));
1199                                 sc->scsi_done(sc);
1200                                 atomic64_dec(&fnic_stats->io_stats.active_ios);
1201                                 if (atomic64_read(&fnic->io_cmpl_skip))
1202                                         atomic64_dec(&fnic->io_cmpl_skip);
1203                                 else
1204                                         atomic64_inc(&fnic_stats->io_stats.io_completions);
1205                         }
1206                 }
1207
1208         } else if (id & FNIC_TAG_DEV_RST) {
1209                 /* Completion of device reset */
1210                 CMD_LR_STATUS(sc) = hdr_status;
1211                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1212                         spin_unlock_irqrestore(io_lock, flags);
1213                         CMD_FLAGS(sc) |= FNIC_DEV_RST_ABTS_PENDING;
1214                         FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1215                                   sc->device->host->host_no, id, sc,
1216                                   jiffies_to_msecs(jiffies - start_time),
1217                                   desc, 0,
1218                                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1219                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1220                                 "Terminate pending "
1221                                 "dev reset cmpl recd. id %d status %s\n",
1222                                 (int)(id & FNIC_TAG_MASK),
1223                                 fnic_fcpio_status_to_str(hdr_status));
1224                         return;
1225                 }
1226                 if (CMD_FLAGS(sc) & FNIC_DEV_RST_TIMED_OUT) {
1227                         /* Need to wait for terminate completion */
1228                         spin_unlock_irqrestore(io_lock, flags);
1229                         FNIC_TRACE(fnic_fcpio_itmf_cmpl_handler,
1230                                   sc->device->host->host_no, id, sc,
1231                                   jiffies_to_msecs(jiffies - start_time),
1232                                   desc, 0,
1233                                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1234                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1235                                 "dev reset cmpl recd after time out. "
1236                                 "id %d status %s\n",
1237                                 (int)(id & FNIC_TAG_MASK),
1238                                 fnic_fcpio_status_to_str(hdr_status));
1239                         return;
1240                 }
1241                 CMD_STATE(sc) = FNIC_IOREQ_CMD_COMPLETE;
1242                 CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1243                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1244                               "dev reset cmpl recd. id %d status %s\n",
1245                               (int)(id & FNIC_TAG_MASK),
1246                               fnic_fcpio_status_to_str(hdr_status));
1247                 if (io_req->dr_done)
1248                         complete(io_req->dr_done);
1249                 spin_unlock_irqrestore(io_lock, flags);
1250
1251         } else {
1252                 shost_printk(KERN_ERR, fnic->lport->host,
1253                              "Unexpected itmf io state %s tag %x\n",
1254                              fnic_ioreq_state_to_str(CMD_STATE(sc)), id);
1255                 spin_unlock_irqrestore(io_lock, flags);
1256         }
1257
1258 }
1259
1260 /*
1261  * fnic_fcpio_cmpl_handler
1262  * Routine to service the cq for wq_copy
1263  */
1264 static int fnic_fcpio_cmpl_handler(struct vnic_dev *vdev,
1265                                    unsigned int cq_index,
1266                                    struct fcpio_fw_req *desc)
1267 {
1268         struct fnic *fnic = vnic_dev_priv(vdev);
1269
1270         switch (desc->hdr.type) {
1271         case FCPIO_ICMND_CMPL: /* fw completed a command */
1272         case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1273         case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1274         case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1275         case FCPIO_RESET_CMPL: /* fw completed reset */
1276                 atomic64_dec(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1277                 break;
1278         default:
1279                 break;
1280         }
1281
1282         switch (desc->hdr.type) {
1283         case FCPIO_ACK: /* fw copied copy wq desc to its queue */
1284                 fnic_fcpio_ack_handler(fnic, cq_index, desc);
1285                 break;
1286
1287         case FCPIO_ICMND_CMPL: /* fw completed a command */
1288                 fnic_fcpio_icmnd_cmpl_handler(fnic, desc);
1289                 break;
1290
1291         case FCPIO_ITMF_CMPL: /* fw completed itmf (abort cmd, lun reset)*/
1292                 fnic_fcpio_itmf_cmpl_handler(fnic, desc);
1293                 break;
1294
1295         case FCPIO_FLOGI_REG_CMPL: /* fw completed flogi_reg */
1296         case FCPIO_FLOGI_FIP_REG_CMPL: /* fw completed flogi_fip_reg */
1297                 fnic_fcpio_flogi_reg_cmpl_handler(fnic, desc);
1298                 break;
1299
1300         case FCPIO_RESET_CMPL: /* fw completed reset */
1301                 fnic_fcpio_fw_reset_cmpl_handler(fnic, desc);
1302                 break;
1303
1304         default:
1305                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1306                               "firmware completion type %d\n",
1307                               desc->hdr.type);
1308                 break;
1309         }
1310
1311         return 0;
1312 }
1313
1314 /*
1315  * fnic_wq_copy_cmpl_handler
1316  * Routine to process wq copy
1317  */
1318 int fnic_wq_copy_cmpl_handler(struct fnic *fnic, int copy_work_to_do)
1319 {
1320         unsigned int wq_work_done = 0;
1321         unsigned int i, cq_index;
1322         unsigned int cur_work_done;
1323
1324         for (i = 0; i < fnic->wq_copy_count; i++) {
1325                 cq_index = i + fnic->raw_wq_count + fnic->rq_count;
1326                 cur_work_done = vnic_cq_copy_service(&fnic->cq[cq_index],
1327                                                      fnic_fcpio_cmpl_handler,
1328                                                      copy_work_to_do);
1329                 wq_work_done += cur_work_done;
1330         }
1331         return wq_work_done;
1332 }
1333
1334 static void fnic_cleanup_io(struct fnic *fnic, int exclude_id)
1335 {
1336         int i;
1337         struct fnic_io_req *io_req;
1338         unsigned long flags = 0;
1339         struct scsi_cmnd *sc;
1340         spinlock_t *io_lock;
1341         unsigned long start_time = 0;
1342         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1343
1344         for (i = 0; i < fnic->fnic_max_tag_id; i++) {
1345                 if (i == exclude_id)
1346                         continue;
1347
1348                 io_lock = fnic_io_lock_tag(fnic, i);
1349                 spin_lock_irqsave(io_lock, flags);
1350                 sc = scsi_host_find_tag(fnic->lport->host, i);
1351                 if (!sc) {
1352                         spin_unlock_irqrestore(io_lock, flags);
1353                         continue;
1354                 }
1355
1356                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1357                 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1358                         !(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
1359                         /*
1360                          * We will be here only when FW completes reset
1361                          * without sending completions for outstanding ios.
1362                          */
1363                         CMD_FLAGS(sc) |= FNIC_DEV_RST_DONE;
1364                         if (io_req && io_req->dr_done)
1365                                 complete(io_req->dr_done);
1366                         else if (io_req && io_req->abts_done)
1367                                 complete(io_req->abts_done);
1368                         spin_unlock_irqrestore(io_lock, flags);
1369                         continue;
1370                 } else if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1371                         spin_unlock_irqrestore(io_lock, flags);
1372                         continue;
1373                 }
1374                 if (!io_req) {
1375                         spin_unlock_irqrestore(io_lock, flags);
1376                         goto cleanup_scsi_cmd;
1377                 }
1378
1379                 CMD_SP(sc) = NULL;
1380
1381                 spin_unlock_irqrestore(io_lock, flags);
1382
1383                 /*
1384                  * If there is a scsi_cmnd associated with this io_req, then
1385                  * free the corresponding state
1386                  */
1387                 start_time = io_req->start_time;
1388                 fnic_release_ioreq_buf(fnic, io_req, sc);
1389                 mempool_free(io_req, fnic->io_req_pool);
1390
1391 cleanup_scsi_cmd:
1392                 sc->result = DID_TRANSPORT_DISRUPTED << 16;
1393                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1394                               "%s: sc duration = %lu DID_TRANSPORT_DISRUPTED\n",
1395                               __func__, (jiffies - start_time));
1396
1397                 if (atomic64_read(&fnic->io_cmpl_skip))
1398                         atomic64_dec(&fnic->io_cmpl_skip);
1399                 else
1400                         atomic64_inc(&fnic_stats->io_stats.io_completions);
1401
1402                 /* Complete the command to SCSI */
1403                 if (sc->scsi_done) {
1404                         FNIC_TRACE(fnic_cleanup_io,
1405                                   sc->device->host->host_no, i, sc,
1406                                   jiffies_to_msecs(jiffies - start_time),
1407                                   0, ((u64)sc->cmnd[0] << 32 |
1408                                   (u64)sc->cmnd[2] << 24 |
1409                                   (u64)sc->cmnd[3] << 16 |
1410                                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1411                                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1412
1413                         sc->scsi_done(sc);
1414                 }
1415         }
1416 }
1417
1418 void fnic_wq_copy_cleanup_handler(struct vnic_wq_copy *wq,
1419                                   struct fcpio_host_req *desc)
1420 {
1421         u32 id;
1422         struct fnic *fnic = vnic_dev_priv(wq->vdev);
1423         struct fnic_io_req *io_req;
1424         struct scsi_cmnd *sc;
1425         unsigned long flags;
1426         spinlock_t *io_lock;
1427         unsigned long start_time = 0;
1428
1429         /* get the tag reference */
1430         fcpio_tag_id_dec(&desc->hdr.tag, &id);
1431         id &= FNIC_TAG_MASK;
1432
1433         if (id >= fnic->fnic_max_tag_id)
1434                 return;
1435
1436         sc = scsi_host_find_tag(fnic->lport->host, id);
1437         if (!sc)
1438                 return;
1439
1440         io_lock = fnic_io_lock_hash(fnic, sc);
1441         spin_lock_irqsave(io_lock, flags);
1442
1443         /* Get the IO context which this desc refers to */
1444         io_req = (struct fnic_io_req *)CMD_SP(sc);
1445
1446         /* fnic interrupts are turned off by now */
1447
1448         if (!io_req) {
1449                 spin_unlock_irqrestore(io_lock, flags);
1450                 goto wq_copy_cleanup_scsi_cmd;
1451         }
1452
1453         CMD_SP(sc) = NULL;
1454
1455         spin_unlock_irqrestore(io_lock, flags);
1456
1457         start_time = io_req->start_time;
1458         fnic_release_ioreq_buf(fnic, io_req, sc);
1459         mempool_free(io_req, fnic->io_req_pool);
1460
1461 wq_copy_cleanup_scsi_cmd:
1462         sc->result = DID_NO_CONNECT << 16;
1463         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "wq_copy_cleanup_handler:"
1464                       " DID_NO_CONNECT\n");
1465
1466         if (sc->scsi_done) {
1467                 FNIC_TRACE(fnic_wq_copy_cleanup_handler,
1468                           sc->device->host->host_no, id, sc,
1469                           jiffies_to_msecs(jiffies - start_time),
1470                           0, ((u64)sc->cmnd[0] << 32 |
1471                           (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
1472                           (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
1473                           (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
1474
1475                 sc->scsi_done(sc);
1476         }
1477 }
1478
1479 static inline int fnic_queue_abort_io_req(struct fnic *fnic, int tag,
1480                                           u32 task_req, u8 *fc_lun,
1481                                           struct fnic_io_req *io_req)
1482 {
1483         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
1484         struct Scsi_Host *host = fnic->lport->host;
1485         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
1486         unsigned long flags;
1487
1488         spin_lock_irqsave(host->host_lock, flags);
1489         if (unlikely(fnic_chk_state_flags_locked(fnic,
1490                                                 FNIC_FLAGS_IO_BLOCKED))) {
1491                 spin_unlock_irqrestore(host->host_lock, flags);
1492                 return 1;
1493         } else
1494                 atomic_inc(&fnic->in_flight);
1495         spin_unlock_irqrestore(host->host_lock, flags);
1496
1497         spin_lock_irqsave(&fnic->wq_copy_lock[0], flags);
1498
1499         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
1500                 free_wq_copy_descs(fnic, wq);
1501
1502         if (!vnic_wq_copy_desc_avail(wq)) {
1503                 spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1504                 atomic_dec(&fnic->in_flight);
1505                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1506                         "fnic_queue_abort_io_req: failure: no descriptors\n");
1507                 atomic64_inc(&misc_stats->abts_cpwq_alloc_failures);
1508                 return 1;
1509         }
1510         fnic_queue_wq_copy_desc_itmf(wq, tag | FNIC_TAG_ABORT,
1511                                      0, task_req, tag, fc_lun, io_req->port_id,
1512                                      fnic->config.ra_tov, fnic->config.ed_tov);
1513
1514         atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
1515         if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
1516                   atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
1517                 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
1518                   atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
1519
1520         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], flags);
1521         atomic_dec(&fnic->in_flight);
1522
1523         return 0;
1524 }
1525
1526 static void fnic_rport_exch_reset(struct fnic *fnic, u32 port_id)
1527 {
1528         int tag;
1529         int abt_tag;
1530         int term_cnt = 0;
1531         struct fnic_io_req *io_req;
1532         spinlock_t *io_lock;
1533         unsigned long flags;
1534         struct scsi_cmnd *sc;
1535         struct reset_stats *reset_stats = &fnic->fnic_stats.reset_stats;
1536         struct terminate_stats *term_stats = &fnic->fnic_stats.term_stats;
1537         struct scsi_lun fc_lun;
1538         enum fnic_ioreq_state old_ioreq_state;
1539
1540         FNIC_SCSI_DBG(KERN_DEBUG,
1541                       fnic->lport->host,
1542                       "fnic_rport_exch_reset called portid 0x%06x\n",
1543                       port_id);
1544
1545         if (fnic->in_remove)
1546                 return;
1547
1548         for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1549                 abt_tag = tag;
1550                 io_lock = fnic_io_lock_tag(fnic, tag);
1551                 spin_lock_irqsave(io_lock, flags);
1552                 sc = scsi_host_find_tag(fnic->lport->host, tag);
1553                 if (!sc) {
1554                         spin_unlock_irqrestore(io_lock, flags);
1555                         continue;
1556                 }
1557
1558                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1559
1560                 if (!io_req || io_req->port_id != port_id) {
1561                         spin_unlock_irqrestore(io_lock, flags);
1562                         continue;
1563                 }
1564
1565                 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1566                         (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1567                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1568                         "fnic_rport_exch_reset dev rst not pending sc 0x%p\n",
1569                         sc);
1570                         spin_unlock_irqrestore(io_lock, flags);
1571                         continue;
1572                 }
1573
1574                 /*
1575                  * Found IO that is still pending with firmware and
1576                  * belongs to rport that went away
1577                  */
1578                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1579                         spin_unlock_irqrestore(io_lock, flags);
1580                         continue;
1581                 }
1582                 if (io_req->abts_done) {
1583                         shost_printk(KERN_ERR, fnic->lport->host,
1584                         "fnic_rport_exch_reset: io_req->abts_done is set "
1585                         "state is %s\n",
1586                         fnic_ioreq_state_to_str(CMD_STATE(sc)));
1587                 }
1588
1589                 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1590                         shost_printk(KERN_ERR, fnic->lport->host,
1591                                   "rport_exch_reset "
1592                                   "IO not yet issued %p tag 0x%x flags "
1593                                   "%x state %d\n",
1594                                   sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1595                 }
1596                 old_ioreq_state = CMD_STATE(sc);
1597                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1598                 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1599                 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1600                         atomic64_inc(&reset_stats->device_reset_terminates);
1601                         abt_tag = (tag | FNIC_TAG_DEV_RST);
1602                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1603                         "fnic_rport_exch_reset dev rst sc 0x%p\n",
1604                         sc);
1605                 }
1606
1607                 BUG_ON(io_req->abts_done);
1608
1609                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1610                               "fnic_rport_reset_exch: Issuing abts\n");
1611
1612                 spin_unlock_irqrestore(io_lock, flags);
1613
1614                 /* Now queue the abort command to firmware */
1615                 int_to_scsilun(sc->device->lun, &fc_lun);
1616
1617                 if (fnic_queue_abort_io_req(fnic, abt_tag,
1618                                             FCPIO_ITMF_ABT_TASK_TERM,
1619                                             fc_lun.scsi_lun, io_req)) {
1620                         /*
1621                          * Revert the cmd state back to old state, if
1622                          * it hasn't changed in between. This cmd will get
1623                          * aborted later by scsi_eh, or cleaned up during
1624                          * lun reset
1625                          */
1626                         spin_lock_irqsave(io_lock, flags);
1627                         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1628                                 CMD_STATE(sc) = old_ioreq_state;
1629                         spin_unlock_irqrestore(io_lock, flags);
1630                 } else {
1631                         spin_lock_irqsave(io_lock, flags);
1632                         if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1633                                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1634                         else
1635                                 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1636                         spin_unlock_irqrestore(io_lock, flags);
1637                         atomic64_inc(&term_stats->terminates);
1638                         term_cnt++;
1639                 }
1640         }
1641         if (term_cnt > atomic64_read(&term_stats->max_terminates))
1642                 atomic64_set(&term_stats->max_terminates, term_cnt);
1643
1644 }
1645
1646 void fnic_terminate_rport_io(struct fc_rport *rport)
1647 {
1648         int tag;
1649         int abt_tag;
1650         int term_cnt = 0;
1651         struct fnic_io_req *io_req;
1652         spinlock_t *io_lock;
1653         unsigned long flags;
1654         struct scsi_cmnd *sc;
1655         struct scsi_lun fc_lun;
1656         struct fc_rport_libfc_priv *rdata;
1657         struct fc_lport *lport;
1658         struct fnic *fnic;
1659         struct fc_rport *cmd_rport;
1660         struct reset_stats *reset_stats;
1661         struct terminate_stats *term_stats;
1662         enum fnic_ioreq_state old_ioreq_state;
1663
1664         if (!rport) {
1665                 printk(KERN_ERR "fnic_terminate_rport_io: rport is NULL\n");
1666                 return;
1667         }
1668         rdata = rport->dd_data;
1669
1670         if (!rdata) {
1671                 printk(KERN_ERR "fnic_terminate_rport_io: rdata is NULL\n");
1672                 return;
1673         }
1674         lport = rdata->local_port;
1675
1676         if (!lport) {
1677                 printk(KERN_ERR "fnic_terminate_rport_io: lport is NULL\n");
1678                 return;
1679         }
1680         fnic = lport_priv(lport);
1681         FNIC_SCSI_DBG(KERN_DEBUG,
1682                       fnic->lport->host, "fnic_terminate_rport_io called"
1683                       " wwpn 0x%llx, wwnn0x%llx, rport 0x%p, portid 0x%06x\n",
1684                       rport->port_name, rport->node_name, rport,
1685                       rport->port_id);
1686
1687         if (fnic->in_remove)
1688                 return;
1689
1690         reset_stats = &fnic->fnic_stats.reset_stats;
1691         term_stats = &fnic->fnic_stats.term_stats;
1692
1693         for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
1694                 abt_tag = tag;
1695                 io_lock = fnic_io_lock_tag(fnic, tag);
1696                 spin_lock_irqsave(io_lock, flags);
1697                 sc = scsi_host_find_tag(fnic->lport->host, tag);
1698                 if (!sc) {
1699                         spin_unlock_irqrestore(io_lock, flags);
1700                         continue;
1701                 }
1702
1703                 cmd_rport = starget_to_rport(scsi_target(sc->device));
1704                 if (rport != cmd_rport) {
1705                         spin_unlock_irqrestore(io_lock, flags);
1706                         continue;
1707                 }
1708
1709                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1710
1711                 if (!io_req || rport != cmd_rport) {
1712                         spin_unlock_irqrestore(io_lock, flags);
1713                         continue;
1714                 }
1715
1716                 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
1717                         (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
1718                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1719                         "fnic_terminate_rport_io dev rst not pending sc 0x%p\n",
1720                         sc);
1721                         spin_unlock_irqrestore(io_lock, flags);
1722                         continue;
1723                 }
1724                 /*
1725                  * Found IO that is still pending with firmware and
1726                  * belongs to rport that went away
1727                  */
1728                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1729                         spin_unlock_irqrestore(io_lock, flags);
1730                         continue;
1731                 }
1732                 if (io_req->abts_done) {
1733                         shost_printk(KERN_ERR, fnic->lport->host,
1734                         "fnic_terminate_rport_io: io_req->abts_done is set "
1735                         "state is %s\n",
1736                         fnic_ioreq_state_to_str(CMD_STATE(sc)));
1737                 }
1738                 if (!(CMD_FLAGS(sc) & FNIC_IO_ISSUED)) {
1739                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1740                                   "fnic_terminate_rport_io "
1741                                   "IO not yet issued %p tag 0x%x flags "
1742                                   "%x state %d\n",
1743                                   sc, tag, CMD_FLAGS(sc), CMD_STATE(sc));
1744                 }
1745                 old_ioreq_state = CMD_STATE(sc);
1746                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1747                 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1748                 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
1749                         atomic64_inc(&reset_stats->device_reset_terminates);
1750                         abt_tag = (tag | FNIC_TAG_DEV_RST);
1751                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1752                         "fnic_terminate_rport_io dev rst sc 0x%p\n", sc);
1753                 }
1754
1755                 BUG_ON(io_req->abts_done);
1756
1757                 FNIC_SCSI_DBG(KERN_DEBUG,
1758                               fnic->lport->host,
1759                               "fnic_terminate_rport_io: Issuing abts\n");
1760
1761                 spin_unlock_irqrestore(io_lock, flags);
1762
1763                 /* Now queue the abort command to firmware */
1764                 int_to_scsilun(sc->device->lun, &fc_lun);
1765
1766                 if (fnic_queue_abort_io_req(fnic, abt_tag,
1767                                             FCPIO_ITMF_ABT_TASK_TERM,
1768                                             fc_lun.scsi_lun, io_req)) {
1769                         /*
1770                          * Revert the cmd state back to old state, if
1771                          * it hasn't changed in between. This cmd will get
1772                          * aborted later by scsi_eh, or cleaned up during
1773                          * lun reset
1774                          */
1775                         spin_lock_irqsave(io_lock, flags);
1776                         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1777                                 CMD_STATE(sc) = old_ioreq_state;
1778                         spin_unlock_irqrestore(io_lock, flags);
1779                 } else {
1780                         spin_lock_irqsave(io_lock, flags);
1781                         if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
1782                                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
1783                         else
1784                                 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
1785                         spin_unlock_irqrestore(io_lock, flags);
1786                         atomic64_inc(&term_stats->terminates);
1787                         term_cnt++;
1788                 }
1789         }
1790         if (term_cnt > atomic64_read(&term_stats->max_terminates))
1791                 atomic64_set(&term_stats->max_terminates, term_cnt);
1792
1793 }
1794
1795 /*
1796  * This function is exported to SCSI for sending abort cmnds.
1797  * A SCSI IO is represented by a io_req in the driver.
1798  * The ioreq is linked to the SCSI Cmd, thus a link with the ULP's IO.
1799  */
1800 int fnic_abort_cmd(struct scsi_cmnd *sc)
1801 {
1802         struct fc_lport *lp;
1803         struct fnic *fnic;
1804         struct fnic_io_req *io_req = NULL;
1805         struct fc_rport *rport;
1806         spinlock_t *io_lock;
1807         unsigned long flags;
1808         unsigned long start_time = 0;
1809         int ret = SUCCESS;
1810         u32 task_req = 0;
1811         struct scsi_lun fc_lun;
1812         struct fnic_stats *fnic_stats;
1813         struct abort_stats *abts_stats;
1814         struct terminate_stats *term_stats;
1815         enum fnic_ioreq_state old_ioreq_state;
1816         int tag;
1817         unsigned long abt_issued_time;
1818         DECLARE_COMPLETION_ONSTACK(tm_done);
1819
1820         /* Wait for rport to unblock */
1821         fc_block_scsi_eh(sc);
1822
1823         /* Get local-port, check ready and link up */
1824         lp = shost_priv(sc->device->host);
1825
1826         fnic = lport_priv(lp);
1827         fnic_stats = &fnic->fnic_stats;
1828         abts_stats = &fnic->fnic_stats.abts_stats;
1829         term_stats = &fnic->fnic_stats.term_stats;
1830
1831         rport = starget_to_rport(scsi_target(sc->device));
1832         tag = sc->request->tag;
1833         FNIC_SCSI_DBG(KERN_DEBUG,
1834                 fnic->lport->host,
1835                 "Abort Cmd called FCID 0x%x, LUN 0x%llx TAG %x flags %x\n",
1836                 rport->port_id, sc->device->lun, tag, CMD_FLAGS(sc));
1837
1838         CMD_FLAGS(sc) = FNIC_NO_FLAGS;
1839
1840         if (lp->state != LPORT_ST_READY || !(lp->link_up)) {
1841                 ret = FAILED;
1842                 goto fnic_abort_cmd_end;
1843         }
1844
1845         /*
1846          * Avoid a race between SCSI issuing the abort and the device
1847          * completing the command.
1848          *
1849          * If the command is already completed by the fw cmpl code,
1850          * we just return SUCCESS from here. This means that the abort
1851          * succeeded. In the SCSI ML, since the timeout for command has
1852          * happened, the completion wont actually complete the command
1853          * and it will be considered as an aborted command
1854          *
1855          * The CMD_SP will not be cleared except while holding io_req_lock.
1856          */
1857         io_lock = fnic_io_lock_hash(fnic, sc);
1858         spin_lock_irqsave(io_lock, flags);
1859         io_req = (struct fnic_io_req *)CMD_SP(sc);
1860         if (!io_req) {
1861                 spin_unlock_irqrestore(io_lock, flags);
1862                 goto fnic_abort_cmd_end;
1863         }
1864
1865         io_req->abts_done = &tm_done;
1866
1867         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
1868                 spin_unlock_irqrestore(io_lock, flags);
1869                 goto wait_pending;
1870         }
1871
1872         abt_issued_time = jiffies_to_msecs(jiffies) - jiffies_to_msecs(io_req->start_time);
1873         if (abt_issued_time <= 6000)
1874                 atomic64_inc(&abts_stats->abort_issued_btw_0_to_6_sec);
1875         else if (abt_issued_time > 6000 && abt_issued_time <= 20000)
1876                 atomic64_inc(&abts_stats->abort_issued_btw_6_to_20_sec);
1877         else if (abt_issued_time > 20000 && abt_issued_time <= 30000)
1878                 atomic64_inc(&abts_stats->abort_issued_btw_20_to_30_sec);
1879         else if (abt_issued_time > 30000 && abt_issued_time <= 40000)
1880                 atomic64_inc(&abts_stats->abort_issued_btw_30_to_40_sec);
1881         else if (abt_issued_time > 40000 && abt_issued_time <= 50000)
1882                 atomic64_inc(&abts_stats->abort_issued_btw_40_to_50_sec);
1883         else if (abt_issued_time > 50000 && abt_issued_time <= 60000)
1884                 atomic64_inc(&abts_stats->abort_issued_btw_50_to_60_sec);
1885         else
1886                 atomic64_inc(&abts_stats->abort_issued_greater_than_60_sec);
1887
1888         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
1889                 "CBD Opcode: %02x Abort issued time: %lu msec\n", sc->cmnd[0], abt_issued_time);
1890         /*
1891          * Command is still pending, need to abort it
1892          * If the firmware completes the command after this point,
1893          * the completion wont be done till mid-layer, since abort
1894          * has already started.
1895          */
1896         old_ioreq_state = CMD_STATE(sc);
1897         CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
1898         CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
1899
1900         spin_unlock_irqrestore(io_lock, flags);
1901
1902         /*
1903          * Check readiness of the remote port. If the path to remote
1904          * port is up, then send abts to the remote port to terminate
1905          * the IO. Else, just locally terminate the IO in the firmware
1906          */
1907         if (fc_remote_port_chkready(rport) == 0)
1908                 task_req = FCPIO_ITMF_ABT_TASK;
1909         else {
1910                 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
1911                 task_req = FCPIO_ITMF_ABT_TASK_TERM;
1912         }
1913
1914         /* Now queue the abort command to firmware */
1915         int_to_scsilun(sc->device->lun, &fc_lun);
1916
1917         if (fnic_queue_abort_io_req(fnic, sc->request->tag, task_req,
1918                                     fc_lun.scsi_lun, io_req)) {
1919                 spin_lock_irqsave(io_lock, flags);
1920                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
1921                         CMD_STATE(sc) = old_ioreq_state;
1922                 io_req = (struct fnic_io_req *)CMD_SP(sc);
1923                 if (io_req)
1924                         io_req->abts_done = NULL;
1925                 spin_unlock_irqrestore(io_lock, flags);
1926                 ret = FAILED;
1927                 goto fnic_abort_cmd_end;
1928         }
1929         if (task_req == FCPIO_ITMF_ABT_TASK) {
1930                 CMD_FLAGS(sc) |= FNIC_IO_ABTS_ISSUED;
1931                 atomic64_inc(&fnic_stats->abts_stats.aborts);
1932         } else {
1933                 CMD_FLAGS(sc) |= FNIC_IO_TERM_ISSUED;
1934                 atomic64_inc(&fnic_stats->term_stats.terminates);
1935         }
1936
1937         /*
1938          * We queued an abort IO, wait for its completion.
1939          * Once the firmware completes the abort command, it will
1940          * wake up this thread.
1941          */
1942  wait_pending:
1943         wait_for_completion_timeout(&tm_done,
1944                                     msecs_to_jiffies
1945                                     (2 * fnic->config.ra_tov +
1946                                      fnic->config.ed_tov));
1947
1948         /* Check the abort status */
1949         spin_lock_irqsave(io_lock, flags);
1950
1951         io_req = (struct fnic_io_req *)CMD_SP(sc);
1952         if (!io_req) {
1953                 atomic64_inc(&fnic_stats->io_stats.ioreq_null);
1954                 spin_unlock_irqrestore(io_lock, flags);
1955                 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
1956                 ret = FAILED;
1957                 goto fnic_abort_cmd_end;
1958         }
1959         io_req->abts_done = NULL;
1960
1961         /* fw did not complete abort, timed out */
1962         if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
1963                 spin_unlock_irqrestore(io_lock, flags);
1964                 if (task_req == FCPIO_ITMF_ABT_TASK) {
1965                         atomic64_inc(&abts_stats->abort_drv_timeouts);
1966                 } else {
1967                         atomic64_inc(&term_stats->terminate_drv_timeouts);
1968                 }
1969                 CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_TIMED_OUT;
1970                 ret = FAILED;
1971                 goto fnic_abort_cmd_end;
1972         }
1973
1974         /* IO out of order */
1975
1976         if (!(CMD_FLAGS(sc) & (FNIC_IO_ABORTED | FNIC_IO_DONE))) {
1977                 spin_unlock_irqrestore(io_lock, flags);
1978                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1979                         "Issuing Host reset due to out of order IO\n");
1980
1981                 if (fnic_host_reset(sc) == FAILED) {
1982                         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
1983                                 "fnic_host_reset failed.\n");
1984                 }
1985                 ret = FAILED;
1986                 goto fnic_abort_cmd_end;
1987         }
1988
1989         CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
1990
1991         start_time = io_req->start_time;
1992         /*
1993          * firmware completed the abort, check the status,
1994          * free the io_req if successful. If abort fails,
1995          * Device reset will clean the I/O.
1996          */
1997         if (CMD_ABTS_STATUS(sc) == FCPIO_SUCCESS)
1998                 CMD_SP(sc) = NULL;
1999         else {
2000                 ret = FAILED;
2001                 spin_unlock_irqrestore(io_lock, flags);
2002                 goto fnic_abort_cmd_end;
2003         }
2004
2005         spin_unlock_irqrestore(io_lock, flags);
2006
2007         fnic_release_ioreq_buf(fnic, io_req, sc);
2008         mempool_free(io_req, fnic->io_req_pool);
2009
2010         if (sc->scsi_done) {
2011         /* Call SCSI completion function to complete the IO */
2012                 sc->result = (DID_ABORT << 16);
2013                 sc->scsi_done(sc);
2014                 atomic64_dec(&fnic_stats->io_stats.active_ios);
2015                 if (atomic64_read(&fnic->io_cmpl_skip))
2016                         atomic64_dec(&fnic->io_cmpl_skip);
2017                 else
2018                         atomic64_inc(&fnic_stats->io_stats.io_completions);
2019         }
2020
2021 fnic_abort_cmd_end:
2022         FNIC_TRACE(fnic_abort_cmd, sc->device->host->host_no,
2023                   sc->request->tag, sc,
2024                   jiffies_to_msecs(jiffies - start_time),
2025                   0, ((u64)sc->cmnd[0] << 32 |
2026                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2027                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2028                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2029
2030         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2031                       "Returning from abort cmd type %x %s\n", task_req,
2032                       (ret == SUCCESS) ?
2033                       "SUCCESS" : "FAILED");
2034         return ret;
2035 }
2036
2037 static inline int fnic_queue_dr_io_req(struct fnic *fnic,
2038                                        struct scsi_cmnd *sc,
2039                                        struct fnic_io_req *io_req)
2040 {
2041         struct vnic_wq_copy *wq = &fnic->wq_copy[0];
2042         struct Scsi_Host *host = fnic->lport->host;
2043         struct misc_stats *misc_stats = &fnic->fnic_stats.misc_stats;
2044         struct scsi_lun fc_lun;
2045         int ret = 0;
2046         unsigned long intr_flags;
2047
2048         spin_lock_irqsave(host->host_lock, intr_flags);
2049         if (unlikely(fnic_chk_state_flags_locked(fnic,
2050                                                 FNIC_FLAGS_IO_BLOCKED))) {
2051                 spin_unlock_irqrestore(host->host_lock, intr_flags);
2052                 return FAILED;
2053         } else
2054                 atomic_inc(&fnic->in_flight);
2055         spin_unlock_irqrestore(host->host_lock, intr_flags);
2056
2057         spin_lock_irqsave(&fnic->wq_copy_lock[0], intr_flags);
2058
2059         if (vnic_wq_copy_desc_avail(wq) <= fnic->wq_copy_desc_low[0])
2060                 free_wq_copy_descs(fnic, wq);
2061
2062         if (!vnic_wq_copy_desc_avail(wq)) {
2063                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2064                           "queue_dr_io_req failure - no descriptors\n");
2065                 atomic64_inc(&misc_stats->devrst_cpwq_alloc_failures);
2066                 ret = -EAGAIN;
2067                 goto lr_io_req_end;
2068         }
2069
2070         /* fill in the lun info */
2071         int_to_scsilun(sc->device->lun, &fc_lun);
2072
2073         fnic_queue_wq_copy_desc_itmf(wq, sc->request->tag | FNIC_TAG_DEV_RST,
2074                                      0, FCPIO_ITMF_LUN_RESET, SCSI_NO_TAG,
2075                                      fc_lun.scsi_lun, io_req->port_id,
2076                                      fnic->config.ra_tov, fnic->config.ed_tov);
2077
2078         atomic64_inc(&fnic->fnic_stats.fw_stats.active_fw_reqs);
2079         if (atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs) >
2080                   atomic64_read(&fnic->fnic_stats.fw_stats.max_fw_reqs))
2081                 atomic64_set(&fnic->fnic_stats.fw_stats.max_fw_reqs,
2082                   atomic64_read(&fnic->fnic_stats.fw_stats.active_fw_reqs));
2083
2084 lr_io_req_end:
2085         spin_unlock_irqrestore(&fnic->wq_copy_lock[0], intr_flags);
2086         atomic_dec(&fnic->in_flight);
2087
2088         return ret;
2089 }
2090
2091 /*
2092  * Clean up any pending aborts on the lun
2093  * For each outstanding IO on this lun, whose abort is not completed by fw,
2094  * issue a local abort. Wait for abort to complete. Return 0 if all commands
2095  * successfully aborted, 1 otherwise
2096  */
2097 static int fnic_clean_pending_aborts(struct fnic *fnic,
2098                                      struct scsi_cmnd *lr_sc,
2099                                          bool new_sc)
2100
2101 {
2102         int tag, abt_tag;
2103         struct fnic_io_req *io_req;
2104         spinlock_t *io_lock;
2105         unsigned long flags;
2106         int ret = 0;
2107         struct scsi_cmnd *sc;
2108         struct scsi_lun fc_lun;
2109         struct scsi_device *lun_dev = lr_sc->device;
2110         DECLARE_COMPLETION_ONSTACK(tm_done);
2111         enum fnic_ioreq_state old_ioreq_state;
2112
2113         for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2114                 io_lock = fnic_io_lock_tag(fnic, tag);
2115                 spin_lock_irqsave(io_lock, flags);
2116                 sc = scsi_host_find_tag(fnic->lport->host, tag);
2117                 /*
2118                  * ignore this lun reset cmd if issued using new SC
2119                  * or cmds that do not belong to this lun
2120                  */
2121                 if (!sc || ((sc == lr_sc) && new_sc) || sc->device != lun_dev) {
2122                         spin_unlock_irqrestore(io_lock, flags);
2123                         continue;
2124                 }
2125
2126                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2127
2128                 if (!io_req || sc->device != lun_dev) {
2129                         spin_unlock_irqrestore(io_lock, flags);
2130                         continue;
2131                 }
2132
2133                 /*
2134                  * Found IO that is still pending with firmware and
2135                  * belongs to the LUN that we are resetting
2136                  */
2137                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2138                               "Found IO in %s on lun\n",
2139                               fnic_ioreq_state_to_str(CMD_STATE(sc)));
2140
2141                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING) {
2142                         spin_unlock_irqrestore(io_lock, flags);
2143                         continue;
2144                 }
2145                 if ((CMD_FLAGS(sc) & FNIC_DEVICE_RESET) &&
2146                         (!(CMD_FLAGS(sc) & FNIC_DEV_RST_ISSUED))) {
2147                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2148                                 "%s dev rst not pending sc 0x%p\n", __func__,
2149                                 sc);
2150                         spin_unlock_irqrestore(io_lock, flags);
2151                         continue;
2152                 }
2153
2154                 if (io_req->abts_done)
2155                         shost_printk(KERN_ERR, fnic->lport->host,
2156                           "%s: io_req->abts_done is set state is %s\n",
2157                           __func__, fnic_ioreq_state_to_str(CMD_STATE(sc)));
2158                 old_ioreq_state = CMD_STATE(sc);
2159                 /*
2160                  * Any pending IO issued prior to reset is expected to be
2161                  * in abts pending state, if not we need to set
2162                  * FNIC_IOREQ_ABTS_PENDING to indicate the IO is abort pending.
2163                  * When IO is completed, the IO will be handed over and
2164                  * handled in this function.
2165                  */
2166                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2167
2168                 BUG_ON(io_req->abts_done);
2169
2170                 abt_tag = tag;
2171                 if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET) {
2172                         abt_tag |= FNIC_TAG_DEV_RST;
2173                         FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2174                                   "%s: dev rst sc 0x%p\n", __func__, sc);
2175                 }
2176
2177                 CMD_ABTS_STATUS(sc) = FCPIO_INVALID_CODE;
2178                 io_req->abts_done = &tm_done;
2179                 spin_unlock_irqrestore(io_lock, flags);
2180
2181                 /* Now queue the abort command to firmware */
2182                 int_to_scsilun(sc->device->lun, &fc_lun);
2183
2184                 if (fnic_queue_abort_io_req(fnic, abt_tag,
2185                                             FCPIO_ITMF_ABT_TASK_TERM,
2186                                             fc_lun.scsi_lun, io_req)) {
2187                         spin_lock_irqsave(io_lock, flags);
2188                         io_req = (struct fnic_io_req *)CMD_SP(sc);
2189                         if (io_req)
2190                                 io_req->abts_done = NULL;
2191                         if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2192                                 CMD_STATE(sc) = old_ioreq_state;
2193                         spin_unlock_irqrestore(io_lock, flags);
2194                         ret = 1;
2195                         goto clean_pending_aborts_end;
2196                 } else {
2197                         spin_lock_irqsave(io_lock, flags);
2198                         if (CMD_FLAGS(sc) & FNIC_DEVICE_RESET)
2199                                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2200                         spin_unlock_irqrestore(io_lock, flags);
2201                 }
2202                 CMD_FLAGS(sc) |= FNIC_IO_INTERNAL_TERM_ISSUED;
2203
2204                 wait_for_completion_timeout(&tm_done,
2205                                             msecs_to_jiffies
2206                                             (fnic->config.ed_tov));
2207
2208                 /* Recheck cmd state to check if it is now aborted */
2209                 spin_lock_irqsave(io_lock, flags);
2210                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2211                 if (!io_req) {
2212                         spin_unlock_irqrestore(io_lock, flags);
2213                         CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_REQ_NULL;
2214                         continue;
2215                 }
2216
2217                 io_req->abts_done = NULL;
2218
2219                 /* if abort is still pending with fw, fail */
2220                 if (CMD_ABTS_STATUS(sc) == FCPIO_INVALID_CODE) {
2221                         spin_unlock_irqrestore(io_lock, flags);
2222                         CMD_FLAGS(sc) |= FNIC_IO_ABT_TERM_DONE;
2223                         ret = 1;
2224                         goto clean_pending_aborts_end;
2225                 }
2226                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_COMPLETE;
2227
2228                 /* original sc used for lr is handled by dev reset code */
2229                 if (sc != lr_sc)
2230                         CMD_SP(sc) = NULL;
2231                 spin_unlock_irqrestore(io_lock, flags);
2232
2233                 /* original sc used for lr is handled by dev reset code */
2234                 if (sc != lr_sc) {
2235                         fnic_release_ioreq_buf(fnic, io_req, sc);
2236                         mempool_free(io_req, fnic->io_req_pool);
2237                 }
2238
2239                 /*
2240                  * Any IO is returned during reset, it needs to call scsi_done
2241                  * to return the scsi_cmnd to upper layer.
2242                  */
2243                 if (sc->scsi_done) {
2244                         /* Set result to let upper SCSI layer retry */
2245                         sc->result = DID_RESET << 16;
2246                         sc->scsi_done(sc);
2247                 }
2248         }
2249
2250         schedule_timeout(msecs_to_jiffies(2 * fnic->config.ed_tov));
2251
2252         /* walk again to check, if IOs are still pending in fw */
2253         if (fnic_is_abts_pending(fnic, lr_sc))
2254                 ret = FAILED;
2255
2256 clean_pending_aborts_end:
2257         return ret;
2258 }
2259
2260 /**
2261  * fnic_scsi_host_start_tag
2262  * Allocates tagid from host's tag list
2263  **/
2264 static inline int
2265 fnic_scsi_host_start_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2266 {
2267         struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2268         int tag, ret = SCSI_NO_TAG;
2269
2270         BUG_ON(!bqt);
2271         if (!bqt) {
2272                 pr_err("Tags are not supported\n");
2273                 goto end;
2274         }
2275
2276         do {
2277                 tag = find_next_zero_bit(bqt->tag_map, bqt->max_depth, 1);
2278                 if (tag >= bqt->max_depth) {
2279                         pr_err("Tag allocation failure\n");
2280                         goto end;
2281                 }
2282         } while (test_and_set_bit(tag, bqt->tag_map));
2283
2284         bqt->tag_index[tag] = sc->request;
2285         sc->request->tag = tag;
2286         sc->tag = tag;
2287         if (!sc->request->special)
2288                 sc->request->special = sc;
2289
2290         ret = tag;
2291
2292 end:
2293         return ret;
2294 }
2295
2296 /**
2297  * fnic_scsi_host_end_tag
2298  * frees tag allocated by fnic_scsi_host_start_tag.
2299  **/
2300 static inline void
2301 fnic_scsi_host_end_tag(struct fnic *fnic, struct scsi_cmnd *sc)
2302 {
2303         struct blk_queue_tag *bqt = fnic->lport->host->bqt;
2304         int tag = sc->request->tag;
2305
2306         if (tag == SCSI_NO_TAG)
2307                 return;
2308
2309         BUG_ON(!bqt || !bqt->tag_index[tag]);
2310         if (!bqt)
2311                 return;
2312
2313         bqt->tag_index[tag] = NULL;
2314         clear_bit(tag, bqt->tag_map);
2315
2316         return;
2317 }
2318
2319 /*
2320  * SCSI Eh thread issues a Lun Reset when one or more commands on a LUN
2321  * fail to get aborted. It calls driver's eh_device_reset with a SCSI command
2322  * on the LUN.
2323  */
2324 int fnic_device_reset(struct scsi_cmnd *sc)
2325 {
2326         struct fc_lport *lp;
2327         struct fnic *fnic;
2328         struct fnic_io_req *io_req = NULL;
2329         struct fc_rport *rport;
2330         int status;
2331         int ret = FAILED;
2332         spinlock_t *io_lock;
2333         unsigned long flags;
2334         unsigned long start_time = 0;
2335         struct scsi_lun fc_lun;
2336         struct fnic_stats *fnic_stats;
2337         struct reset_stats *reset_stats;
2338         int tag = 0;
2339         DECLARE_COMPLETION_ONSTACK(tm_done);
2340         int tag_gen_flag = 0;   /*to track tags allocated by fnic driver*/
2341         bool new_sc = 0;
2342
2343         /* Wait for rport to unblock */
2344         fc_block_scsi_eh(sc);
2345
2346         /* Get local-port, check ready and link up */
2347         lp = shost_priv(sc->device->host);
2348
2349         fnic = lport_priv(lp);
2350         fnic_stats = &fnic->fnic_stats;
2351         reset_stats = &fnic->fnic_stats.reset_stats;
2352
2353         atomic64_inc(&reset_stats->device_resets);
2354
2355         rport = starget_to_rport(scsi_target(sc->device));
2356         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2357                       "Device reset called FCID 0x%x, LUN 0x%llx sc 0x%p\n",
2358                       rport->port_id, sc->device->lun, sc);
2359
2360         if (lp->state != LPORT_ST_READY || !(lp->link_up))
2361                 goto fnic_device_reset_end;
2362
2363         /* Check if remote port up */
2364         if (fc_remote_port_chkready(rport)) {
2365                 atomic64_inc(&fnic_stats->misc_stats.rport_not_ready);
2366                 goto fnic_device_reset_end;
2367         }
2368
2369         CMD_FLAGS(sc) = FNIC_DEVICE_RESET;
2370         /* Allocate tag if not present */
2371
2372         tag = sc->request->tag;
2373         if (unlikely(tag < 0)) {
2374                 /*
2375                  * XXX(hch): current the midlayer fakes up a struct
2376                  * request for the explicit reset ioctls, and those
2377                  * don't have a tag allocated to them.  The below
2378                  * code pokes into midlayer structures to paper over
2379                  * this design issue, but that won't work for blk-mq.
2380                  *
2381                  * Either someone who can actually test the hardware
2382                  * will have to come up with a similar hack for the
2383                  * blk-mq case, or we'll have to bite the bullet and
2384                  * fix the way the EH ioctls work for real, but until
2385                  * that happens we fail these explicit requests here.
2386                  */
2387
2388                 tag = fnic_scsi_host_start_tag(fnic, sc);
2389                 if (unlikely(tag == SCSI_NO_TAG))
2390                         goto fnic_device_reset_end;
2391                 tag_gen_flag = 1;
2392                 new_sc = 1;
2393         }
2394         io_lock = fnic_io_lock_hash(fnic, sc);
2395         spin_lock_irqsave(io_lock, flags);
2396         io_req = (struct fnic_io_req *)CMD_SP(sc);
2397
2398         /*
2399          * If there is a io_req attached to this command, then use it,
2400          * else allocate a new one.
2401          */
2402         if (!io_req) {
2403                 io_req = mempool_alloc(fnic->io_req_pool, GFP_ATOMIC);
2404                 if (!io_req) {
2405                         spin_unlock_irqrestore(io_lock, flags);
2406                         goto fnic_device_reset_end;
2407                 }
2408                 memset(io_req, 0, sizeof(*io_req));
2409                 io_req->port_id = rport->port_id;
2410                 CMD_SP(sc) = (char *)io_req;
2411         }
2412         io_req->dr_done = &tm_done;
2413         CMD_STATE(sc) = FNIC_IOREQ_CMD_PENDING;
2414         CMD_LR_STATUS(sc) = FCPIO_INVALID_CODE;
2415         spin_unlock_irqrestore(io_lock, flags);
2416
2417         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host, "TAG %x\n", tag);
2418
2419         /*
2420          * issue the device reset, if enqueue failed, clean up the ioreq
2421          * and break assoc with scsi cmd
2422          */
2423         if (fnic_queue_dr_io_req(fnic, sc, io_req)) {
2424                 spin_lock_irqsave(io_lock, flags);
2425                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2426                 if (io_req)
2427                         io_req->dr_done = NULL;
2428                 goto fnic_device_reset_clean;
2429         }
2430         spin_lock_irqsave(io_lock, flags);
2431         CMD_FLAGS(sc) |= FNIC_DEV_RST_ISSUED;
2432         spin_unlock_irqrestore(io_lock, flags);
2433
2434         /*
2435          * Wait on the local completion for LUN reset.  The io_req may be
2436          * freed while we wait since we hold no lock.
2437          */
2438         wait_for_completion_timeout(&tm_done,
2439                                     msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2440
2441         spin_lock_irqsave(io_lock, flags);
2442         io_req = (struct fnic_io_req *)CMD_SP(sc);
2443         if (!io_req) {
2444                 spin_unlock_irqrestore(io_lock, flags);
2445                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2446                                 "io_req is null tag 0x%x sc 0x%p\n", tag, sc);
2447                 goto fnic_device_reset_end;
2448         }
2449         io_req->dr_done = NULL;
2450
2451         status = CMD_LR_STATUS(sc);
2452
2453         /*
2454          * If lun reset not completed, bail out with failed. io_req
2455          * gets cleaned up during higher levels of EH
2456          */
2457         if (status == FCPIO_INVALID_CODE) {
2458                 atomic64_inc(&reset_stats->device_reset_timeouts);
2459                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2460                               "Device reset timed out\n");
2461                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TIMED_OUT;
2462                 spin_unlock_irqrestore(io_lock, flags);
2463                 int_to_scsilun(sc->device->lun, &fc_lun);
2464                 /*
2465                  * Issue abort and terminate on device reset request.
2466                  * If q'ing of terminate fails, retry it after a delay.
2467                  */
2468                 while (1) {
2469                         spin_lock_irqsave(io_lock, flags);
2470                         if (CMD_FLAGS(sc) & FNIC_DEV_RST_TERM_ISSUED) {
2471                                 spin_unlock_irqrestore(io_lock, flags);
2472                                 break;
2473                         }
2474                         spin_unlock_irqrestore(io_lock, flags);
2475                         if (fnic_queue_abort_io_req(fnic,
2476                                 tag | FNIC_TAG_DEV_RST,
2477                                 FCPIO_ITMF_ABT_TASK_TERM,
2478                                 fc_lun.scsi_lun, io_req)) {
2479                                 wait_for_completion_timeout(&tm_done,
2480                                 msecs_to_jiffies(FNIC_ABT_TERM_DELAY_TIMEOUT));
2481                         } else {
2482                                 spin_lock_irqsave(io_lock, flags);
2483                                 CMD_FLAGS(sc) |= FNIC_DEV_RST_TERM_ISSUED;
2484                                 CMD_STATE(sc) = FNIC_IOREQ_ABTS_PENDING;
2485                                 io_req->abts_done = &tm_done;
2486                                 spin_unlock_irqrestore(io_lock, flags);
2487                                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2488                                 "Abort and terminate issued on Device reset "
2489                                 "tag 0x%x sc 0x%p\n", tag, sc);
2490                                 break;
2491                         }
2492                 }
2493                 while (1) {
2494                         spin_lock_irqsave(io_lock, flags);
2495                         if (!(CMD_FLAGS(sc) & FNIC_DEV_RST_DONE)) {
2496                                 spin_unlock_irqrestore(io_lock, flags);
2497                                 wait_for_completion_timeout(&tm_done,
2498                                 msecs_to_jiffies(FNIC_LUN_RESET_TIMEOUT));
2499                                 break;
2500                         } else {
2501                                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2502                                 io_req->abts_done = NULL;
2503                                 goto fnic_device_reset_clean;
2504                         }
2505                 }
2506         } else {
2507                 spin_unlock_irqrestore(io_lock, flags);
2508         }
2509
2510         /* Completed, but not successful, clean up the io_req, return fail */
2511         if (status != FCPIO_SUCCESS) {
2512                 spin_lock_irqsave(io_lock, flags);
2513                 FNIC_SCSI_DBG(KERN_DEBUG,
2514                               fnic->lport->host,
2515                               "Device reset completed - failed\n");
2516                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2517                 goto fnic_device_reset_clean;
2518         }
2519
2520         /*
2521          * Clean up any aborts on this lun that have still not
2522          * completed. If any of these fail, then LUN reset fails.
2523          * clean_pending_aborts cleans all cmds on this lun except
2524          * the lun reset cmd. If all cmds get cleaned, the lun reset
2525          * succeeds
2526          */
2527         if (fnic_clean_pending_aborts(fnic, sc, new_sc)) {
2528                 spin_lock_irqsave(io_lock, flags);
2529                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2530                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2531                               "Device reset failed"
2532                               " since could not abort all IOs\n");
2533                 goto fnic_device_reset_clean;
2534         }
2535
2536         /* Clean lun reset command */
2537         spin_lock_irqsave(io_lock, flags);
2538         io_req = (struct fnic_io_req *)CMD_SP(sc);
2539         if (io_req)
2540                 /* Completed, and successful */
2541                 ret = SUCCESS;
2542
2543 fnic_device_reset_clean:
2544         if (io_req)
2545                 CMD_SP(sc) = NULL;
2546
2547         spin_unlock_irqrestore(io_lock, flags);
2548
2549         if (io_req) {
2550                 start_time = io_req->start_time;
2551                 fnic_release_ioreq_buf(fnic, io_req, sc);
2552                 mempool_free(io_req, fnic->io_req_pool);
2553         }
2554
2555 fnic_device_reset_end:
2556         FNIC_TRACE(fnic_device_reset, sc->device->host->host_no,
2557                   sc->request->tag, sc,
2558                   jiffies_to_msecs(jiffies - start_time),
2559                   0, ((u64)sc->cmnd[0] << 32 |
2560                   (u64)sc->cmnd[2] << 24 | (u64)sc->cmnd[3] << 16 |
2561                   (u64)sc->cmnd[4] << 8 | sc->cmnd[5]),
2562                   (((u64)CMD_FLAGS(sc) << 32) | CMD_STATE(sc)));
2563
2564         /* free tag if it is allocated */
2565         if (unlikely(tag_gen_flag))
2566                 fnic_scsi_host_end_tag(fnic, sc);
2567
2568         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2569                       "Returning from device reset %s\n",
2570                       (ret == SUCCESS) ?
2571                       "SUCCESS" : "FAILED");
2572
2573         if (ret == FAILED)
2574                 atomic64_inc(&reset_stats->device_reset_failures);
2575
2576         return ret;
2577 }
2578
2579 /* Clean up all IOs, clean up libFC local port */
2580 int fnic_reset(struct Scsi_Host *shost)
2581 {
2582         struct fc_lport *lp;
2583         struct fnic *fnic;
2584         int ret = 0;
2585         struct reset_stats *reset_stats;
2586
2587         lp = shost_priv(shost);
2588         fnic = lport_priv(lp);
2589         reset_stats = &fnic->fnic_stats.reset_stats;
2590
2591         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2592                       "fnic_reset called\n");
2593
2594         atomic64_inc(&reset_stats->fnic_resets);
2595
2596         /*
2597          * Reset local port, this will clean up libFC exchanges,
2598          * reset remote port sessions, and if link is up, begin flogi
2599          */
2600         ret = fc_lport_reset(lp);
2601
2602         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2603                       "Returning from fnic reset %s\n",
2604                       (ret == 0) ?
2605                       "SUCCESS" : "FAILED");
2606
2607         if (ret == 0)
2608                 atomic64_inc(&reset_stats->fnic_reset_completions);
2609         else
2610                 atomic64_inc(&reset_stats->fnic_reset_failures);
2611
2612         return ret;
2613 }
2614
2615 /*
2616  * SCSI Error handling calls driver's eh_host_reset if all prior
2617  * error handling levels return FAILED. If host reset completes
2618  * successfully, and if link is up, then Fabric login begins.
2619  *
2620  * Host Reset is the highest level of error recovery. If this fails, then
2621  * host is offlined by SCSI.
2622  *
2623  */
2624 int fnic_host_reset(struct scsi_cmnd *sc)
2625 {
2626         int ret;
2627         unsigned long wait_host_tmo;
2628         struct Scsi_Host *shost = sc->device->host;
2629         struct fc_lport *lp = shost_priv(shost);
2630         struct fnic *fnic = lport_priv(lp);
2631         unsigned long flags;
2632
2633         spin_lock_irqsave(&fnic->fnic_lock, flags);
2634         if (fnic->internal_reset_inprogress == 0) {
2635                 fnic->internal_reset_inprogress = 1;
2636         } else {
2637                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2638                 FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2639                         "host reset in progress skipping another host reset\n");
2640                 return SUCCESS;
2641         }
2642         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2643
2644         /*
2645          * If fnic_reset is successful, wait for fabric login to complete
2646          * scsi-ml tries to send a TUR to every device if host reset is
2647          * successful, so before returning to scsi, fabric should be up
2648          */
2649         ret = (fnic_reset(shost) == 0) ? SUCCESS : FAILED;
2650         if (ret == SUCCESS) {
2651                 wait_host_tmo = jiffies + FNIC_HOST_RESET_SETTLE_TIME * HZ;
2652                 ret = FAILED;
2653                 while (time_before(jiffies, wait_host_tmo)) {
2654                         if ((lp->state == LPORT_ST_READY) &&
2655                             (lp->link_up)) {
2656                                 ret = SUCCESS;
2657                                 break;
2658                         }
2659                         ssleep(1);
2660                 }
2661         }
2662
2663         spin_lock_irqsave(&fnic->fnic_lock, flags);
2664         fnic->internal_reset_inprogress = 0;
2665         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2666         return ret;
2667 }
2668
2669 /*
2670  * This fxn is called from libFC when host is removed
2671  */
2672 void fnic_scsi_abort_io(struct fc_lport *lp)
2673 {
2674         int err = 0;
2675         unsigned long flags;
2676         enum fnic_state old_state;
2677         struct fnic *fnic = lport_priv(lp);
2678         DECLARE_COMPLETION_ONSTACK(remove_wait);
2679
2680         /* Issue firmware reset for fnic, wait for reset to complete */
2681 retry_fw_reset:
2682         spin_lock_irqsave(&fnic->fnic_lock, flags);
2683         if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2684                 /* fw reset is in progress, poll for its completion */
2685                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2686                 schedule_timeout(msecs_to_jiffies(100));
2687                 goto retry_fw_reset;
2688         }
2689
2690         fnic->remove_wait = &remove_wait;
2691         old_state = fnic->state;
2692         fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2693         fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2694         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2695
2696         err = fnic_fw_reset_handler(fnic);
2697         if (err) {
2698                 spin_lock_irqsave(&fnic->fnic_lock, flags);
2699                 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2700                         fnic->state = old_state;
2701                 fnic->remove_wait = NULL;
2702                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2703                 return;
2704         }
2705
2706         /* Wait for firmware reset to complete */
2707         wait_for_completion_timeout(&remove_wait,
2708                                     msecs_to_jiffies(FNIC_RMDEVICE_TIMEOUT));
2709
2710         spin_lock_irqsave(&fnic->fnic_lock, flags);
2711         fnic->remove_wait = NULL;
2712         FNIC_SCSI_DBG(KERN_DEBUG, fnic->lport->host,
2713                       "fnic_scsi_abort_io %s\n",
2714                       (fnic->state == FNIC_IN_ETH_MODE) ?
2715                       "SUCCESS" : "FAILED");
2716         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2717
2718 }
2719
2720 /*
2721  * This fxn called from libFC to clean up driver IO state on link down
2722  */
2723 void fnic_scsi_cleanup(struct fc_lport *lp)
2724 {
2725         unsigned long flags;
2726         enum fnic_state old_state;
2727         struct fnic *fnic = lport_priv(lp);
2728
2729         /* issue fw reset */
2730 retry_fw_reset:
2731         spin_lock_irqsave(&fnic->fnic_lock, flags);
2732         if (unlikely(fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)) {
2733                 /* fw reset is in progress, poll for its completion */
2734                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2735                 schedule_timeout(msecs_to_jiffies(100));
2736                 goto retry_fw_reset;
2737         }
2738         old_state = fnic->state;
2739         fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
2740         fnic_update_mac_locked(fnic, fnic->ctlr.ctl_src_addr);
2741         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2742
2743         if (fnic_fw_reset_handler(fnic)) {
2744                 spin_lock_irqsave(&fnic->fnic_lock, flags);
2745                 if (fnic->state == FNIC_IN_FC_TRANS_ETH_MODE)
2746                         fnic->state = old_state;
2747                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
2748         }
2749
2750 }
2751
2752 void fnic_empty_scsi_cleanup(struct fc_lport *lp)
2753 {
2754 }
2755
2756 void fnic_exch_mgr_reset(struct fc_lport *lp, u32 sid, u32 did)
2757 {
2758         struct fnic *fnic = lport_priv(lp);
2759
2760         /* Non-zero sid, nothing to do */
2761         if (sid)
2762                 goto call_fc_exch_mgr_reset;
2763
2764         if (did) {
2765                 fnic_rport_exch_reset(fnic, did);
2766                 goto call_fc_exch_mgr_reset;
2767         }
2768
2769         /*
2770          * sid = 0, did = 0
2771          * link down or device being removed
2772          */
2773         if (!fnic->in_remove)
2774                 fnic_scsi_cleanup(lp);
2775         else
2776                 fnic_scsi_abort_io(lp);
2777
2778         /* call libFC exch mgr reset to reset its exchanges */
2779 call_fc_exch_mgr_reset:
2780         fc_exch_mgr_reset(lp, sid, did);
2781
2782 }
2783
2784 /*
2785  * fnic_is_abts_pending() is a helper function that
2786  * walks through tag map to check if there is any IOs pending,if there is one,
2787  * then it returns 1 (true), otherwise 0 (false)
2788  * if @lr_sc is non NULL, then it checks IOs specific to particular LUN,
2789  * otherwise, it checks for all IOs.
2790  */
2791 int fnic_is_abts_pending(struct fnic *fnic, struct scsi_cmnd *lr_sc)
2792 {
2793         int tag;
2794         struct fnic_io_req *io_req;
2795         spinlock_t *io_lock;
2796         unsigned long flags;
2797         int ret = 0;
2798         struct scsi_cmnd *sc;
2799         struct scsi_device *lun_dev = NULL;
2800
2801         if (lr_sc)
2802                 lun_dev = lr_sc->device;
2803
2804         /* walk again to check, if IOs are still pending in fw */
2805         for (tag = 0; tag < fnic->fnic_max_tag_id; tag++) {
2806                 sc = scsi_host_find_tag(fnic->lport->host, tag);
2807                 /*
2808                  * ignore this lun reset cmd or cmds that do not belong to
2809                  * this lun
2810                  */
2811                 if (!sc || (lr_sc && (sc->device != lun_dev || sc == lr_sc)))
2812                         continue;
2813
2814                 io_lock = fnic_io_lock_hash(fnic, sc);
2815                 spin_lock_irqsave(io_lock, flags);
2816
2817                 io_req = (struct fnic_io_req *)CMD_SP(sc);
2818
2819                 if (!io_req || sc->device != lun_dev) {
2820                         spin_unlock_irqrestore(io_lock, flags);
2821                         continue;
2822                 }
2823
2824                 /*
2825                  * Found IO that is still pending with firmware and
2826                  * belongs to the LUN that we are resetting
2827                  */
2828                 FNIC_SCSI_DBG(KERN_INFO, fnic->lport->host,
2829                               "Found IO in %s on lun\n",
2830                               fnic_ioreq_state_to_str(CMD_STATE(sc)));
2831
2832                 if (CMD_STATE(sc) == FNIC_IOREQ_ABTS_PENDING)
2833                         ret = 1;
2834                 spin_unlock_irqrestore(io_lock, flags);
2835         }
2836
2837         return ret;
2838 }