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