root/drivers/scsi/fnic/fnic_scsi.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. fnic_state_to_str
  2. fnic_ioreq_state_to_str
  3. fnic_fcpio_status_to_str
  4. fnic_io_lock_hash
  5. fnic_io_lock_tag
  6. fnic_release_ioreq_buf
  7. free_wq_copy_descs
  8. __fnic_set_state_flags
  9. fnic_fw_reset_handler
  10. fnic_flogi_reg_handler
  11. fnic_queue_wq_copy_desc
  12. fnic_queuecommand_lck
  13. DEF_SCSI_QCMD
  14. fnic_fcpio_flogi_reg_cmpl_handler
  15. is_ack_index_in_range
  16. fnic_fcpio_ack_handler
  17. fnic_fcpio_icmnd_cmpl_handler
  18. fnic_fcpio_itmf_cmpl_handler
  19. fnic_fcpio_cmpl_handler
  20. fnic_wq_copy_cmpl_handler
  21. fnic_cleanup_io
  22. fnic_wq_copy_cleanup_handler
  23. fnic_queue_abort_io_req
  24. fnic_rport_exch_reset
  25. fnic_terminate_rport_io
  26. fnic_abort_cmd
  27. fnic_queue_dr_io_req
  28. fnic_clean_pending_aborts
  29. fnic_scsi_host_start_tag
  30. fnic_scsi_host_end_tag
  31. fnic_device_reset
  32. fnic_reset
  33. fnic_host_reset
  34. fnic_scsi_abort_io
  35. fnic_scsi_cleanup
  36. fnic_empty_scsi_cleanup
  37. fnic_exch_mgr_reset
  38. fnic_is_abts_pending

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

/* [<][>][^][v][top][bottom][index][help] */