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