root/drivers/scsi/snic/snic_scsi.c

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

DEFINITIONS

This source file includes following definitions.
  1. snic_state_to_str
  2. snic_io_status_to_str
  3. snic_ioreq_state_to_str
  4. snic_io_lock_hash
  5. snic_io_lock_tag
  6. snic_release_req_buf
  7. snic_queue_icmnd_req
  8. snic_issue_scsi_req
  9. snic_queuecommand
  10. snic_proc_tmreq_pending_state
  11. snic_process_io_failed_state
  12. snic_tmreq_pending
  13. snic_process_icmnd_cmpl_status
  14. snic_icmnd_cmpl_handler
  15. snic_proc_dr_cmpl_locked
  16. snic_update_abort_stats
  17. snic_process_itmf_cmpl
  18. snic_itmf_cmpl_handler
  19. snic_hba_reset_scsi_cleanup
  20. snic_hba_reset_cmpl_handler
  21. snic_msg_ack_handler
  22. snic_aen_handler
  23. snic_io_cmpl_handler
  24. snic_fwcq_cmpl_handler
  25. snic_queue_itmf_req
  26. snic_issue_tm_req
  27. snic_queue_abort_req
  28. snic_abort_finish
  29. snic_send_abort_and_wait
  30. snic_abort_cmd
  31. snic_is_abts_pending
  32. snic_dr_clean_single_req
  33. snic_dr_clean_pending_req
  34. snic_dr_finish
  35. snic_queue_dr_req
  36. snic_send_dr_and_wait
  37. snic_dev_reset_supported
  38. snic_unlink_and_release_req
  39. snic_device_reset
  40. snic_issue_hba_reset
  41. snic_reset
  42. snic_host_reset
  43. snic_cmpl_pending_tmreq
  44. snic_scsi_cleanup
  45. snic_shutdown_scsi_cleanup
  46. snic_internal_abort_io
  47. snic_tgt_scsi_abort_io

   1 /*
   2  * Copyright 2014 Cisco Systems, Inc.  All rights reserved.
   3  *
   4  * This program is free software; you may redistribute it and/or modify
   5  * it under the terms of the GNU General Public License as published by
   6  * the Free Software Foundation; version 2 of the License.
   7  *
   8  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   9  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15  * SOFTWARE.
  16  */
  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/spinlock.h>
  24 #include <linux/delay.h>
  25 #include <linux/gfp.h>
  26 #include <scsi/scsi.h>
  27 #include <scsi/scsi_host.h>
  28 #include <scsi/scsi_device.h>
  29 #include <scsi/scsi_cmnd.h>
  30 #include <scsi/scsi_tcq.h>
  31 #include <scsi/scsi_dbg.h>
  32 
  33 #include "snic_io.h"
  34 #include "snic.h"
  35 
  36 #define snic_cmd_tag(sc)        (((struct scsi_cmnd *) sc)->request->tag)
  37 
  38 const char *snic_state_str[] = {
  39         [SNIC_INIT]     = "SNIC_INIT",
  40         [SNIC_ERROR]    = "SNIC_ERROR",
  41         [SNIC_ONLINE]   = "SNIC_ONLINE",
  42         [SNIC_OFFLINE]  = "SNIC_OFFLINE",
  43         [SNIC_FWRESET]  = "SNIC_FWRESET",
  44 };
  45 
  46 static const char * const snic_req_state_str[] = {
  47         [SNIC_IOREQ_NOT_INITED] = "SNIC_IOREQ_NOT_INITED",
  48         [SNIC_IOREQ_PENDING]    = "SNIC_IOREQ_PENDING",
  49         [SNIC_IOREQ_ABTS_PENDING] = "SNIC_IOREQ_ABTS_PENDING",
  50         [SNIC_IOREQ_ABTS_COMPLETE] = "SNIC_IOREQ_ABTS_COMPLETE",
  51         [SNIC_IOREQ_LR_PENDING] = "SNIC_IOREQ_LR_PENDING",
  52         [SNIC_IOREQ_LR_COMPLETE] = "SNIC_IOREQ_LR_COMPLETE",
  53         [SNIC_IOREQ_COMPLETE]   = "SNIC_IOREQ_CMD_COMPLETE",
  54 };
  55 
  56 /* snic cmd status strings */
  57 static const char * const snic_io_status_str[] = {
  58         [SNIC_STAT_IO_SUCCESS]  = "SNIC_STAT_IO_SUCCESS", /* 0x0 */
  59         [SNIC_STAT_INVALID_HDR] = "SNIC_STAT_INVALID_HDR",
  60         [SNIC_STAT_OUT_OF_RES]  = "SNIC_STAT_OUT_OF_RES",
  61         [SNIC_STAT_INVALID_PARM] = "SNIC_STAT_INVALID_PARM",
  62         [SNIC_STAT_REQ_NOT_SUP] = "SNIC_STAT_REQ_NOT_SUP",
  63         [SNIC_STAT_IO_NOT_FOUND] = "SNIC_STAT_IO_NOT_FOUND",
  64         [SNIC_STAT_ABORTED]     = "SNIC_STAT_ABORTED",
  65         [SNIC_STAT_TIMEOUT]     = "SNIC_STAT_TIMEOUT",
  66         [SNIC_STAT_SGL_INVALID] = "SNIC_STAT_SGL_INVALID",
  67         [SNIC_STAT_DATA_CNT_MISMATCH] = "SNIC_STAT_DATA_CNT_MISMATCH",
  68         [SNIC_STAT_FW_ERR]      = "SNIC_STAT_FW_ERR",
  69         [SNIC_STAT_ITMF_REJECT] = "SNIC_STAT_ITMF_REJECT",
  70         [SNIC_STAT_ITMF_FAIL]   = "SNIC_STAT_ITMF_FAIL",
  71         [SNIC_STAT_ITMF_INCORRECT_LUN] = "SNIC_STAT_ITMF_INCORRECT_LUN",
  72         [SNIC_STAT_CMND_REJECT] = "SNIC_STAT_CMND_REJECT",
  73         [SNIC_STAT_DEV_OFFLINE] = "SNIC_STAT_DEV_OFFLINE",
  74         [SNIC_STAT_NO_BOOTLUN]  = "SNIC_STAT_NO_BOOTLUN",
  75         [SNIC_STAT_SCSI_ERR]    = "SNIC_STAT_SCSI_ERR",
  76         [SNIC_STAT_NOT_READY]   = "SNIC_STAT_NOT_READY",
  77         [SNIC_STAT_FATAL_ERROR] = "SNIC_STAT_FATAL_ERROR",
  78 };
  79 
  80 static void snic_scsi_cleanup(struct snic *, int);
  81 
  82 const char *
  83 snic_state_to_str(unsigned int state)
  84 {
  85         if (state >= ARRAY_SIZE(snic_state_str) || !snic_state_str[state])
  86                 return "Unknown";
  87 
  88         return snic_state_str[state];
  89 }
  90 
  91 static const char *
  92 snic_io_status_to_str(unsigned int state)
  93 {
  94         if ((state >= ARRAY_SIZE(snic_io_status_str)) ||
  95              (!snic_io_status_str[state]))
  96                 return "Unknown";
  97 
  98         return snic_io_status_str[state];
  99 }
 100 
 101 static const char *
 102 snic_ioreq_state_to_str(unsigned int state)
 103 {
 104         if (state >= ARRAY_SIZE(snic_req_state_str) ||
 105                         !snic_req_state_str[state])
 106                 return "Unknown";
 107 
 108         return snic_req_state_str[state];
 109 }
 110 
 111 static inline spinlock_t *
 112 snic_io_lock_hash(struct snic *snic, struct scsi_cmnd *sc)
 113 {
 114         u32 hash = snic_cmd_tag(sc) & (SNIC_IO_LOCKS - 1);
 115 
 116         return &snic->io_req_lock[hash];
 117 }
 118 
 119 static inline spinlock_t *
 120 snic_io_lock_tag(struct snic *snic, int tag)
 121 {
 122         return &snic->io_req_lock[tag & (SNIC_IO_LOCKS - 1)];
 123 }
 124 
 125 /* snic_release_req_buf : Releases snic_req_info */
 126 static void
 127 snic_release_req_buf(struct snic *snic,
 128                    struct snic_req_info *rqi,
 129                    struct scsi_cmnd *sc)
 130 {
 131         struct snic_host_req *req = rqi_to_req(rqi);
 132 
 133         /* Freeing cmd without marking completion, not okay */
 134         SNIC_BUG_ON(!((CMD_STATE(sc) == SNIC_IOREQ_COMPLETE) ||
 135                       (CMD_STATE(sc) == SNIC_IOREQ_ABTS_COMPLETE) ||
 136                       (CMD_FLAGS(sc) & SNIC_DEV_RST_NOTSUP) ||
 137                       (CMD_FLAGS(sc) & SNIC_IO_INTERNAL_TERM_ISSUED) ||
 138                       (CMD_FLAGS(sc) & SNIC_DEV_RST_TERM_ISSUED) ||
 139                       (CMD_FLAGS(sc) & SNIC_SCSI_CLEANUP) ||
 140                       (CMD_STATE(sc) == SNIC_IOREQ_LR_COMPLETE)));
 141 
 142         SNIC_SCSI_DBG(snic->shost,
 143                       "Rel_req:sc %p:tag %x:rqi %p:ioreq %p:abt %p:dr %p: state %s:flags 0x%llx\n",
 144                       sc, snic_cmd_tag(sc), rqi, rqi->req, rqi->abort_req,
 145                       rqi->dr_req, snic_ioreq_state_to_str(CMD_STATE(sc)),
 146                       CMD_FLAGS(sc));
 147 
 148         if (req->u.icmnd.sense_addr)
 149                 dma_unmap_single(&snic->pdev->dev,
 150                                  le64_to_cpu(req->u.icmnd.sense_addr),
 151                                  SCSI_SENSE_BUFFERSIZE,
 152                                  DMA_FROM_DEVICE);
 153 
 154         scsi_dma_unmap(sc);
 155 
 156         snic_req_free(snic, rqi);
 157 } /* end of snic_release_req_buf */
 158 
 159 /*
 160  * snic_queue_icmnd_req : Queues snic_icmnd request
 161  */
 162 static int
 163 snic_queue_icmnd_req(struct snic *snic,
 164                      struct snic_req_info *rqi,
 165                      struct scsi_cmnd *sc,
 166                      int sg_cnt)
 167 {
 168         struct scatterlist *sg;
 169         struct snic_sg_desc *sgd;
 170         dma_addr_t pa = 0;
 171         struct scsi_lun lun;
 172         u16 flags = 0;
 173         int ret = 0;
 174         unsigned int i;
 175 
 176         if (sg_cnt) {
 177                 flags = SNIC_ICMND_ESGL;
 178                 sgd = (struct snic_sg_desc *) req_to_sgl(rqi->req);
 179 
 180                 for_each_sg(scsi_sglist(sc), sg, sg_cnt, i) {
 181                         sgd->addr = cpu_to_le64(sg_dma_address(sg));
 182                         sgd->len = cpu_to_le32(sg_dma_len(sg));
 183                         sgd->_resvd = 0;
 184                         sgd++;
 185                 }
 186         }
 187 
 188         pa = dma_map_single(&snic->pdev->dev,
 189                             sc->sense_buffer,
 190                             SCSI_SENSE_BUFFERSIZE,
 191                             DMA_FROM_DEVICE);
 192         if (dma_mapping_error(&snic->pdev->dev, pa)) {
 193                 SNIC_HOST_ERR(snic->shost,
 194                               "QIcmnd:PCI Map Failed for sns buf %p tag %x\n",
 195                               sc->sense_buffer, snic_cmd_tag(sc));
 196                 ret = -ENOMEM;
 197 
 198                 return ret;
 199         }
 200 
 201         int_to_scsilun(sc->device->lun, &lun);
 202         if (sc->sc_data_direction == DMA_FROM_DEVICE)
 203                 flags |= SNIC_ICMND_RD;
 204         if (sc->sc_data_direction == DMA_TO_DEVICE)
 205                 flags |= SNIC_ICMND_WR;
 206 
 207         /* Initialize icmnd */
 208         snic_icmnd_init(rqi->req,
 209                         snic_cmd_tag(sc),
 210                         snic->config.hid, /* hid */
 211                         (ulong) rqi,
 212                         flags, /* command flags */
 213                         rqi->tgt_id,
 214                         lun.scsi_lun,
 215                         sc->cmnd,
 216                         sc->cmd_len,
 217                         scsi_bufflen(sc),
 218                         sg_cnt,
 219                         (ulong) req_to_sgl(rqi->req),
 220                         pa, /* sense buffer pa */
 221                         SCSI_SENSE_BUFFERSIZE);
 222 
 223         atomic64_inc(&snic->s_stats.io.active);
 224         ret = snic_queue_wq_desc(snic, rqi->req, rqi->req_len);
 225         if (ret) {
 226                 atomic64_dec(&snic->s_stats.io.active);
 227                 SNIC_HOST_ERR(snic->shost,
 228                               "QIcmnd: Queuing Icmnd Failed. ret = %d\n",
 229                               ret);
 230         } else
 231                 snic_stats_update_active_ios(&snic->s_stats);
 232 
 233         return ret;
 234 } /* end of snic_queue_icmnd_req */
 235 
 236 /*
 237  * snic_issue_scsi_req : Prepares IO request and Issues to FW.
 238  */
 239 static int
 240 snic_issue_scsi_req(struct snic *snic,
 241                       struct snic_tgt *tgt,
 242                       struct scsi_cmnd *sc)
 243 {
 244         struct snic_req_info *rqi = NULL;
 245         int sg_cnt = 0;
 246         int ret = 0;
 247         u32 tag = snic_cmd_tag(sc);
 248         u64 cmd_trc = 0, cmd_st_flags = 0;
 249         spinlock_t *io_lock = NULL;
 250         unsigned long flags;
 251 
 252         CMD_STATE(sc) = SNIC_IOREQ_NOT_INITED;
 253         CMD_FLAGS(sc) = SNIC_NO_FLAGS;
 254         sg_cnt = scsi_dma_map(sc);
 255         if (sg_cnt < 0) {
 256                 SNIC_TRC((u16)snic->shost->host_no, tag, (ulong) sc, 0,
 257                          sc->cmnd[0], sg_cnt, CMD_STATE(sc));
 258 
 259                 SNIC_HOST_ERR(snic->shost, "issue_sc:Failed to map SG List.\n");
 260                 ret = -ENOMEM;
 261 
 262                 goto issue_sc_end;
 263         }
 264 
 265         rqi = snic_req_init(snic, sg_cnt);
 266         if (!rqi) {
 267                 scsi_dma_unmap(sc);
 268                 ret = -ENOMEM;
 269 
 270                 goto issue_sc_end;
 271         }
 272 
 273         rqi->tgt_id = tgt->id;
 274         rqi->sc = sc;
 275 
 276         CMD_STATE(sc) = SNIC_IOREQ_PENDING;
 277         CMD_SP(sc) = (char *) rqi;
 278         cmd_trc = SNIC_TRC_CMD(sc);
 279         CMD_FLAGS(sc) |= (SNIC_IO_INITIALIZED | SNIC_IO_ISSUED);
 280         cmd_st_flags = SNIC_TRC_CMD_STATE_FLAGS(sc);
 281         io_lock = snic_io_lock_hash(snic, sc);
 282 
 283         /* create wq desc and enqueue it */
 284         ret = snic_queue_icmnd_req(snic, rqi, sc, sg_cnt);
 285         if (ret) {
 286                 SNIC_HOST_ERR(snic->shost,
 287                               "issue_sc: icmnd qing Failed for sc %p, err %d\n",
 288                               sc, ret);
 289 
 290                 spin_lock_irqsave(io_lock, flags);
 291                 rqi = (struct snic_req_info *) CMD_SP(sc);
 292                 CMD_SP(sc) = NULL;
 293                 CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
 294                 CMD_FLAGS(sc) &= ~SNIC_IO_ISSUED; /* turn off the flag */
 295                 spin_unlock_irqrestore(io_lock, flags);
 296 
 297                 if (rqi)
 298                         snic_release_req_buf(snic, rqi, sc);
 299 
 300                 SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, 0, 0, 0,
 301                          SNIC_TRC_CMD_STATE_FLAGS(sc));
 302         } else {
 303                 u32 io_sz = scsi_bufflen(sc) >> 9;
 304                 u32 qtime = jiffies - rqi->start_time;
 305                 struct snic_io_stats *iostats = &snic->s_stats.io;
 306 
 307                 if (io_sz > atomic64_read(&iostats->max_io_sz))
 308                         atomic64_set(&iostats->max_io_sz, io_sz);
 309 
 310                 if (qtime > atomic64_read(&iostats->max_qtime))
 311                         atomic64_set(&iostats->max_qtime, qtime);
 312 
 313                 SNIC_SCSI_DBG(snic->shost,
 314                               "issue_sc:sc %p, tag %d queued to WQ.\n",
 315                               sc, tag);
 316 
 317                 SNIC_TRC(snic->shost->host_no, tag, (ulong) sc, (ulong) rqi,
 318                          sg_cnt, cmd_trc, cmd_st_flags);
 319         }
 320 
 321 issue_sc_end:
 322 
 323         return ret;
 324 } /* end of snic_issue_scsi_req */
 325 
 326 
 327 /*
 328  * snic_queuecommand
 329  * Routine to send a scsi cdb to LLD
 330  * Called with host_lock held and interrupts disabled
 331  */
 332 int
 333 snic_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc)
 334 {
 335         struct snic_tgt *tgt = NULL;
 336         struct snic *snic = shost_priv(shost);
 337         int ret;
 338 
 339         tgt = starget_to_tgt(scsi_target(sc->device));
 340         ret = snic_tgt_chkready(tgt);
 341         if (ret) {
 342                 SNIC_HOST_ERR(shost, "Tgt %p id %d Not Ready.\n", tgt, tgt->id);
 343                 atomic64_inc(&snic->s_stats.misc.tgt_not_rdy);
 344                 sc->result = ret;
 345                 sc->scsi_done(sc);
 346 
 347                 return 0;
 348         }
 349 
 350         if (snic_get_state(snic) != SNIC_ONLINE) {
 351                 SNIC_HOST_ERR(shost, "snic state is %s\n",
 352                               snic_state_str[snic_get_state(snic)]);
 353 
 354                 return SCSI_MLQUEUE_HOST_BUSY;
 355         }
 356         atomic_inc(&snic->ios_inflight);
 357 
 358         SNIC_SCSI_DBG(shost, "sc %p Tag %d (sc %0x) lun %lld in snic_qcmd\n",
 359                       sc, snic_cmd_tag(sc), sc->cmnd[0], sc->device->lun);
 360 
 361         ret = snic_issue_scsi_req(snic, tgt, sc);
 362         if (ret) {
 363                 SNIC_HOST_ERR(shost, "Failed to Q, Scsi Req w/ err %d.\n", ret);
 364                 ret = SCSI_MLQUEUE_HOST_BUSY;
 365         }
 366 
 367         atomic_dec(&snic->ios_inflight);
 368 
 369         return ret;
 370 } /* end of snic_queuecommand */
 371 
 372 /*
 373  * snic_process_abts_pending_state:
 374  * caller should hold IO lock
 375  */
 376 static void
 377 snic_proc_tmreq_pending_state(struct snic *snic,
 378                               struct scsi_cmnd *sc,
 379                               u8 cmpl_status)
 380 {
 381         int state = CMD_STATE(sc);
 382 
 383         if (state == SNIC_IOREQ_ABTS_PENDING)
 384                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_PENDING;
 385         else if (state == SNIC_IOREQ_LR_PENDING)
 386                 CMD_FLAGS(sc) |= SNIC_DEV_RST_PENDING;
 387         else
 388                 SNIC_BUG_ON(1);
 389 
 390         switch (cmpl_status) {
 391         case SNIC_STAT_IO_SUCCESS:
 392                 CMD_FLAGS(sc) |= SNIC_IO_DONE;
 393                 break;
 394 
 395         case SNIC_STAT_ABORTED:
 396                 CMD_FLAGS(sc) |= SNIC_IO_ABORTED;
 397                 break;
 398 
 399         default:
 400                 SNIC_BUG_ON(1);
 401         }
 402 }
 403 
 404 /*
 405  * snic_process_io_failed_state:
 406  * Processes IO's error states
 407  */
 408 static void
 409 snic_process_io_failed_state(struct snic *snic,
 410                              struct snic_icmnd_cmpl *icmnd_cmpl,
 411                              struct scsi_cmnd *sc,
 412                              u8 cmpl_stat)
 413 {
 414         int res = 0;
 415 
 416         switch (cmpl_stat) {
 417         case SNIC_STAT_TIMEOUT:         /* Req was timedout */
 418                 atomic64_inc(&snic->s_stats.misc.io_tmo);
 419                 res = DID_TIME_OUT;
 420                 break;
 421 
 422         case SNIC_STAT_ABORTED:         /* Req was aborted */
 423                 atomic64_inc(&snic->s_stats.misc.io_aborted);
 424                 res = DID_ABORT;
 425                 break;
 426 
 427         case SNIC_STAT_DATA_CNT_MISMATCH:/* Recv/Sent more/less data than exp */
 428                 atomic64_inc(&snic->s_stats.misc.data_cnt_mismat);
 429                 scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
 430                 res = DID_ERROR;
 431                 break;
 432 
 433         case SNIC_STAT_OUT_OF_RES: /* Out of resources to complete request */
 434                 atomic64_inc(&snic->s_stats.fw.out_of_res);
 435                 res = DID_REQUEUE;
 436                 break;
 437 
 438         case SNIC_STAT_IO_NOT_FOUND:    /* Requested I/O was not found */
 439                 atomic64_inc(&snic->s_stats.io.io_not_found);
 440                 res = DID_ERROR;
 441                 break;
 442 
 443         case SNIC_STAT_SGL_INVALID:     /* Req was aborted to due to sgl error*/
 444                 atomic64_inc(&snic->s_stats.misc.sgl_inval);
 445                 res = DID_ERROR;
 446                 break;
 447 
 448         case SNIC_STAT_FW_ERR:          /* Req terminated due to FW Error */
 449                 atomic64_inc(&snic->s_stats.fw.io_errs);
 450                 res = DID_ERROR;
 451                 break;
 452 
 453         case SNIC_STAT_SCSI_ERR:        /* FW hits SCSI Error */
 454                 atomic64_inc(&snic->s_stats.fw.scsi_errs);
 455                 break;
 456 
 457         case SNIC_STAT_NOT_READY:       /* XPT yet to initialize */
 458         case SNIC_STAT_DEV_OFFLINE:     /* Device offline */
 459                 res = DID_NO_CONNECT;
 460                 break;
 461 
 462         case SNIC_STAT_INVALID_HDR:     /* Hdr contains invalid data */
 463         case SNIC_STAT_INVALID_PARM:    /* Some param in req is invalid */
 464         case SNIC_STAT_REQ_NOT_SUP:     /* Req type is not supported */
 465         case SNIC_STAT_CMND_REJECT:     /* Req rejected */
 466         case SNIC_STAT_FATAL_ERROR:     /* XPT Error */
 467         default:
 468                 SNIC_SCSI_DBG(snic->shost,
 469                               "Invalid Hdr/Param or Req Not Supported or Cmnd Rejected or Device Offline. or Unknown\n");
 470                 res = DID_ERROR;
 471                 break;
 472         }
 473 
 474         SNIC_HOST_ERR(snic->shost, "fw returns failed status %s flags 0x%llx\n",
 475                       snic_io_status_to_str(cmpl_stat), CMD_FLAGS(sc));
 476 
 477         /* Set sc->result */
 478         sc->result = (res << 16) | icmnd_cmpl->scsi_status;
 479 } /* end of snic_process_io_failed_state */
 480 
 481 /*
 482  * snic_tmreq_pending : is task management in progress.
 483  */
 484 static int
 485 snic_tmreq_pending(struct scsi_cmnd *sc)
 486 {
 487         int state = CMD_STATE(sc);
 488 
 489         return ((state == SNIC_IOREQ_ABTS_PENDING) ||
 490                         (state == SNIC_IOREQ_LR_PENDING));
 491 }
 492 
 493 /*
 494  * snic_process_icmnd_cmpl_status:
 495  * Caller should hold io_lock
 496  */
 497 static int
 498 snic_process_icmnd_cmpl_status(struct snic *snic,
 499                                struct snic_icmnd_cmpl *icmnd_cmpl,
 500                                u8 cmpl_stat,
 501                                struct scsi_cmnd *sc)
 502 {
 503         u8 scsi_stat = icmnd_cmpl->scsi_status;
 504         u64 xfer_len = 0;
 505         int ret = 0;
 506 
 507         /* Mark the IO as complete */
 508         CMD_STATE(sc) = SNIC_IOREQ_COMPLETE;
 509 
 510         if (likely(cmpl_stat == SNIC_STAT_IO_SUCCESS)) {
 511                 sc->result = (DID_OK << 16) | scsi_stat;
 512 
 513                 xfer_len = scsi_bufflen(sc);
 514 
 515                 /* Update SCSI Cmd with resid value */
 516                 scsi_set_resid(sc, le32_to_cpu(icmnd_cmpl->resid));
 517 
 518                 if (icmnd_cmpl->flags & SNIC_ICMND_CMPL_UNDR_RUN) {
 519                         xfer_len -= le32_to_cpu(icmnd_cmpl->resid);
 520                         atomic64_inc(&snic->s_stats.misc.io_under_run);
 521                 }
 522 
 523                 if (icmnd_cmpl->scsi_status == SAM_STAT_TASK_SET_FULL)
 524                         atomic64_inc(&snic->s_stats.misc.qfull);
 525 
 526                 ret = 0;
 527         } else {
 528                 snic_process_io_failed_state(snic, icmnd_cmpl, sc, cmpl_stat);
 529                 atomic64_inc(&snic->s_stats.io.fail);
 530                 SNIC_HOST_ERR(snic->shost,
 531                               "icmnd_cmpl: IO Failed : Hdr Status %s flags 0x%llx\n",
 532                               snic_io_status_to_str(cmpl_stat), CMD_FLAGS(sc));
 533                 ret = 1;
 534         }
 535 
 536         return ret;
 537 } /* end of snic_process_icmnd_cmpl_status */
 538 
 539 
 540 /*
 541  * snic_icmnd_cmpl_handler
 542  * Routine to handle icmnd completions
 543  */
 544 static void
 545 snic_icmnd_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
 546 {
 547         u8 typ, hdr_stat;
 548         u32 cmnd_id, hid;
 549         ulong ctx;
 550         struct scsi_cmnd *sc = NULL;
 551         struct snic_icmnd_cmpl *icmnd_cmpl = NULL;
 552         struct snic_host_req *req = NULL;
 553         struct snic_req_info *rqi = NULL;
 554         unsigned long flags, start_time;
 555         spinlock_t *io_lock;
 556         u8 sc_stat = 0;
 557 
 558         snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
 559         icmnd_cmpl = &fwreq->u.icmnd_cmpl;
 560         sc_stat = icmnd_cmpl->scsi_status;
 561 
 562         SNIC_SCSI_DBG(snic->shost,
 563                       "Icmnd_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,i ctx = %lx\n",
 564                       typ, hdr_stat, cmnd_id, hid, ctx);
 565 
 566         if (cmnd_id >= snic->max_tag_id) {
 567                 SNIC_HOST_ERR(snic->shost,
 568                               "Icmnd_cmpl:Tag Error:Out of Range Tag %d, hdr status = %s\n",
 569                               cmnd_id, snic_io_status_to_str(hdr_stat));
 570                 return;
 571         }
 572 
 573         sc = scsi_host_find_tag(snic->shost, cmnd_id);
 574         WARN_ON_ONCE(!sc);
 575 
 576         if (!sc) {
 577                 atomic64_inc(&snic->s_stats.io.sc_null);
 578                 SNIC_HOST_ERR(snic->shost,
 579                               "Icmnd_cmpl: Scsi Cmnd Not found, sc = NULL Hdr Status = %s tag = 0x%x fwreq = 0x%p\n",
 580                               snic_io_status_to_str(hdr_stat),
 581                               cmnd_id,
 582                               fwreq);
 583 
 584                 SNIC_TRC(snic->shost->host_no, cmnd_id, 0,
 585                          ((u64)hdr_stat << 16 |
 586                           (u64)sc_stat << 8 | (u64)icmnd_cmpl->flags),
 587                          (ulong) fwreq, le32_to_cpu(icmnd_cmpl->resid), ctx);
 588 
 589                 return;
 590         }
 591 
 592         io_lock = snic_io_lock_hash(snic, sc);
 593 
 594         spin_lock_irqsave(io_lock, flags);
 595         rqi = (struct snic_req_info *) CMD_SP(sc);
 596         SNIC_SCSI_DBG(snic->shost,
 597                       "Icmnd_cmpl:lun %lld sc %p cmd %xtag %d flags 0x%llx rqi %p\n",
 598                       sc->device->lun, sc, sc->cmnd[0], snic_cmd_tag(sc),
 599                       CMD_FLAGS(sc), rqi);
 600 
 601         if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
 602                 spin_unlock_irqrestore(io_lock, flags);
 603 
 604                 return;
 605         }
 606 
 607         SNIC_BUG_ON(rqi != (struct snic_req_info *)ctx);
 608         WARN_ON_ONCE(req);
 609         if (!rqi) {
 610                 atomic64_inc(&snic->s_stats.io.req_null);
 611                 CMD_FLAGS(sc) |= SNIC_IO_REQ_NULL;
 612                 spin_unlock_irqrestore(io_lock, flags);
 613 
 614                 SNIC_HOST_ERR(snic->shost,
 615                               "Icmnd_cmpl:Host Req Not Found(null), Hdr Status %s, Tag 0x%x, sc 0x%p flags 0x%llx\n",
 616                               snic_io_status_to_str(hdr_stat),
 617                               cmnd_id, sc, CMD_FLAGS(sc));
 618                 return;
 619         }
 620 
 621         rqi = (struct snic_req_info *) ctx;
 622         start_time = rqi->start_time;
 623 
 624         /* firmware completed the io */
 625         rqi->io_cmpl = 1;
 626 
 627         /*
 628          * if SCSI-ML has already issued abort on this command,
 629          * ignore completion of the IO. The abts path will clean it up
 630          */
 631         if (unlikely(snic_tmreq_pending(sc))) {
 632                 snic_proc_tmreq_pending_state(snic, sc, hdr_stat);
 633                 spin_unlock_irqrestore(io_lock, flags);
 634 
 635                 snic_stats_update_io_cmpl(&snic->s_stats);
 636 
 637                 /* Expected value is SNIC_STAT_ABORTED */
 638                 if (likely(hdr_stat == SNIC_STAT_ABORTED))
 639                         return;
 640 
 641                 SNIC_SCSI_DBG(snic->shost,
 642                               "icmnd_cmpl:TM Req Pending(%s), Hdr Status %s sc 0x%p scsi status %x resid %d flags 0x%llx\n",
 643                               snic_ioreq_state_to_str(CMD_STATE(sc)),
 644                               snic_io_status_to_str(hdr_stat),
 645                               sc, sc_stat, le32_to_cpu(icmnd_cmpl->resid),
 646                               CMD_FLAGS(sc));
 647 
 648                 SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
 649                          jiffies_to_msecs(jiffies - start_time), (ulong) fwreq,
 650                          SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
 651 
 652                 return;
 653         }
 654 
 655         if (snic_process_icmnd_cmpl_status(snic, icmnd_cmpl, hdr_stat, sc)) {
 656                 scsi_print_command(sc);
 657                 SNIC_HOST_ERR(snic->shost,
 658                               "icmnd_cmpl:IO Failed, sc 0x%p Tag %d Cmd %x Hdr Status %s flags 0x%llx\n",
 659                               sc, sc->cmnd[0], cmnd_id,
 660                               snic_io_status_to_str(hdr_stat), CMD_FLAGS(sc));
 661         }
 662 
 663         /* Break link with the SCSI Command */
 664         CMD_SP(sc) = NULL;
 665         CMD_FLAGS(sc) |= SNIC_IO_DONE;
 666 
 667         spin_unlock_irqrestore(io_lock, flags);
 668 
 669         /* For now, consider only successful IO. */
 670         snic_calc_io_process_time(snic, rqi);
 671 
 672         snic_release_req_buf(snic, rqi, sc);
 673 
 674         SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
 675                  jiffies_to_msecs(jiffies - start_time), (ulong) fwreq,
 676                  SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
 677 
 678 
 679         if (sc->scsi_done)
 680                 sc->scsi_done(sc);
 681 
 682         snic_stats_update_io_cmpl(&snic->s_stats);
 683 } /* end of snic_icmnd_cmpl_handler */
 684 
 685 static void
 686 snic_proc_dr_cmpl_locked(struct snic *snic,
 687                          struct snic_fw_req *fwreq,
 688                          u8 cmpl_stat,
 689                          u32 cmnd_id,
 690                          struct scsi_cmnd *sc)
 691 {
 692         struct snic_req_info *rqi = (struct snic_req_info *) CMD_SP(sc);
 693         u32 start_time = rqi->start_time;
 694 
 695         CMD_LR_STATUS(sc) = cmpl_stat;
 696 
 697         SNIC_SCSI_DBG(snic->shost, "itmf_cmpl: Cmd State = %s\n",
 698                       snic_ioreq_state_to_str(CMD_STATE(sc)));
 699 
 700         if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
 701                 CMD_FLAGS(sc) |= SNIC_DEV_RST_ABTS_PENDING;
 702 
 703                 SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
 704                          jiffies_to_msecs(jiffies - start_time),
 705                          (ulong) fwreq, 0, SNIC_TRC_CMD_STATE_FLAGS(sc));
 706 
 707                 SNIC_SCSI_DBG(snic->shost,
 708                               "itmf_cmpl: Terminate Pending Dev Reset Cmpl Recvd.id %x, status %s flags 0x%llx\n",
 709                               (int)(cmnd_id & SNIC_TAG_MASK),
 710                               snic_io_status_to_str(cmpl_stat),
 711                               CMD_FLAGS(sc));
 712 
 713                 return;
 714         }
 715 
 716 
 717         if (CMD_FLAGS(sc) & SNIC_DEV_RST_TIMEDOUT) {
 718                 SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
 719                          jiffies_to_msecs(jiffies - start_time),
 720                          (ulong) fwreq, 0, SNIC_TRC_CMD_STATE_FLAGS(sc));
 721 
 722                 SNIC_SCSI_DBG(snic->shost,
 723                               "itmf_cmpl:Dev Reset Completion Received after timeout. id %d cmpl status %s flags 0x%llx\n",
 724                               (int)(cmnd_id & SNIC_TAG_MASK),
 725                               snic_io_status_to_str(cmpl_stat),
 726                               CMD_FLAGS(sc));
 727 
 728                 return;
 729         }
 730 
 731         CMD_STATE(sc) = SNIC_IOREQ_LR_COMPLETE;
 732         CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
 733 
 734         SNIC_SCSI_DBG(snic->shost,
 735                       "itmf_cmpl:Dev Reset Cmpl Recvd id %d cmpl status %s flags 0x%llx\n",
 736                       (int)(cmnd_id & SNIC_TAG_MASK),
 737                       snic_io_status_to_str(cmpl_stat),
 738                       CMD_FLAGS(sc));
 739 
 740         if (rqi->dr_done)
 741                 complete(rqi->dr_done);
 742 } /* end of snic_proc_dr_cmpl_locked */
 743 
 744 /*
 745  * snic_update_abort_stats : Updates abort stats based on completion status.
 746  */
 747 static void
 748 snic_update_abort_stats(struct snic *snic, u8 cmpl_stat)
 749 {
 750         struct snic_abort_stats *abt_stats = &snic->s_stats.abts;
 751 
 752         SNIC_SCSI_DBG(snic->shost, "Updating Abort stats.\n");
 753 
 754         switch (cmpl_stat) {
 755         case  SNIC_STAT_IO_SUCCESS:
 756                 break;
 757 
 758         case SNIC_STAT_TIMEOUT:
 759                 atomic64_inc(&abt_stats->fw_tmo);
 760                 break;
 761 
 762         case SNIC_STAT_IO_NOT_FOUND:
 763                 atomic64_inc(&abt_stats->io_not_found);
 764                 break;
 765 
 766         default:
 767                 atomic64_inc(&abt_stats->fail);
 768                 break;
 769         }
 770 }
 771 
 772 static int
 773 snic_process_itmf_cmpl(struct snic *snic,
 774                        struct snic_fw_req *fwreq,
 775                        u32 cmnd_id,
 776                        u8 cmpl_stat,
 777                        struct scsi_cmnd *sc)
 778 {
 779         struct snic_req_info *rqi = NULL;
 780         u32 tm_tags = 0;
 781         spinlock_t *io_lock = NULL;
 782         unsigned long flags;
 783         u32 start_time = 0;
 784         int ret = 0;
 785 
 786         io_lock = snic_io_lock_hash(snic, sc);
 787         spin_lock_irqsave(io_lock, flags);
 788         if (CMD_FLAGS(sc) & SNIC_HOST_RESET_CMD_TERM) {
 789                 spin_unlock_irqrestore(io_lock, flags);
 790 
 791                 return ret;
 792         }
 793         rqi = (struct snic_req_info *) CMD_SP(sc);
 794         WARN_ON_ONCE(!rqi);
 795 
 796         if (!rqi) {
 797                 atomic64_inc(&snic->s_stats.io.req_null);
 798                 spin_unlock_irqrestore(io_lock, flags);
 799                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
 800                 SNIC_HOST_ERR(snic->shost,
 801                               "itmf_cmpl: rqi is null,Hdr stat = %s Tag = 0x%x sc = 0x%p flags 0x%llx\n",
 802                               snic_io_status_to_str(cmpl_stat), cmnd_id, sc,
 803                               CMD_FLAGS(sc));
 804 
 805                 return ret;
 806         }
 807 
 808         /* Extract task management flags */
 809         tm_tags = cmnd_id & ~(SNIC_TAG_MASK);
 810 
 811         start_time = rqi->start_time;
 812         cmnd_id &= (SNIC_TAG_MASK);
 813 
 814         switch (tm_tags) {
 815         case SNIC_TAG_ABORT:
 816                 /* Abort only issued on cmd */
 817                 snic_update_abort_stats(snic, cmpl_stat);
 818 
 819                 if (CMD_STATE(sc) != SNIC_IOREQ_ABTS_PENDING) {
 820                         /* This is a late completion. Ignore it. */
 821                         ret = -1;
 822                         spin_unlock_irqrestore(io_lock, flags);
 823                         break;
 824                 }
 825 
 826                 CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
 827                 CMD_ABTS_STATUS(sc) = cmpl_stat;
 828                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
 829 
 830                 SNIC_SCSI_DBG(snic->shost,
 831                               "itmf_cmpl:Abort Cmpl Recvd.Tag 0x%x Status %s flags 0x%llx\n",
 832                               cmnd_id,
 833                               snic_io_status_to_str(cmpl_stat),
 834                               CMD_FLAGS(sc));
 835 
 836                 /*
 837                  * If scsi_eh thread is blocked waiting for abts complete,
 838                  * signal completion to it. IO will be cleaned in the thread,
 839                  * else clean it in this context.
 840                  */
 841                 if (rqi->abts_done) {
 842                         complete(rqi->abts_done);
 843                         spin_unlock_irqrestore(io_lock, flags);
 844 
 845                         break; /* jump out */
 846                 }
 847 
 848                 CMD_SP(sc) = NULL;
 849                 sc->result = (DID_ERROR << 16);
 850                 SNIC_SCSI_DBG(snic->shost,
 851                               "itmf_cmpl: Completing IO. sc %p flags 0x%llx\n",
 852                               sc, CMD_FLAGS(sc));
 853 
 854                 spin_unlock_irqrestore(io_lock, flags);
 855 
 856                 snic_release_req_buf(snic, rqi, sc);
 857 
 858                 if (sc->scsi_done) {
 859                         SNIC_TRC(snic->shost->host_no, cmnd_id, (ulong) sc,
 860                                  jiffies_to_msecs(jiffies - start_time),
 861                                  (ulong) fwreq, SNIC_TRC_CMD(sc),
 862                                  SNIC_TRC_CMD_STATE_FLAGS(sc));
 863 
 864                         sc->scsi_done(sc);
 865                 }
 866 
 867                 break;
 868 
 869         case SNIC_TAG_DEV_RST:
 870         case SNIC_TAG_DEV_RST | SNIC_TAG_IOCTL_DEV_RST:
 871                 snic_proc_dr_cmpl_locked(snic, fwreq, cmpl_stat, cmnd_id, sc);
 872                 spin_unlock_irqrestore(io_lock, flags);
 873                 ret = 0;
 874 
 875                 break;
 876 
 877         case SNIC_TAG_ABORT | SNIC_TAG_DEV_RST:
 878                 /* Abort and terminate completion of device reset req */
 879 
 880                 CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
 881                 CMD_ABTS_STATUS(sc) = cmpl_stat;
 882                 CMD_FLAGS(sc) |= SNIC_DEV_RST_DONE;
 883 
 884                 SNIC_SCSI_DBG(snic->shost,
 885                               "itmf_cmpl:dev reset abts cmpl recvd. id %d status %s flags 0x%llx\n",
 886                               cmnd_id, snic_io_status_to_str(cmpl_stat),
 887                               CMD_FLAGS(sc));
 888 
 889                 if (rqi->abts_done)
 890                         complete(rqi->abts_done);
 891 
 892                 spin_unlock_irqrestore(io_lock, flags);
 893 
 894                 break;
 895 
 896         default:
 897                 spin_unlock_irqrestore(io_lock, flags);
 898                 SNIC_HOST_ERR(snic->shost,
 899                               "itmf_cmpl: Unknown TM tag bit 0x%x\n", tm_tags);
 900 
 901                 SNIC_HOST_ERR(snic->shost,
 902                               "itmf_cmpl:Unexpected itmf io stat %s Tag = 0x%x flags 0x%llx\n",
 903                               snic_ioreq_state_to_str(CMD_STATE(sc)),
 904                               cmnd_id,
 905                               CMD_FLAGS(sc));
 906                 ret = -1;
 907                 SNIC_BUG_ON(1);
 908 
 909                 break;
 910         }
 911 
 912         return ret;
 913 } /* end of snic_process_itmf_cmpl_status */
 914 
 915 /*
 916  * snic_itmf_cmpl_handler.
 917  * Routine to handle itmf completions.
 918  */
 919 static void
 920 snic_itmf_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
 921 {
 922         struct scsi_cmnd  *sc = NULL;
 923         struct snic_req_info *rqi = NULL;
 924         struct snic_itmf_cmpl *itmf_cmpl = NULL;
 925         ulong ctx;
 926         u32 cmnd_id;
 927         u32 hid;
 928         u8 typ;
 929         u8 hdr_stat;
 930 
 931         snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
 932         SNIC_SCSI_DBG(snic->shost,
 933                       "Itmf_cmpl: %s: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x,ctx = %lx\n",
 934                       __func__, typ, hdr_stat, cmnd_id, hid, ctx);
 935 
 936         itmf_cmpl = &fwreq->u.itmf_cmpl;
 937         SNIC_SCSI_DBG(snic->shost,
 938                       "Itmf_cmpl: nterm %u , flags 0x%x\n",
 939                       le32_to_cpu(itmf_cmpl->nterminated), itmf_cmpl->flags);
 940 
 941         /* spl case, dev reset issued through ioctl */
 942         if (cmnd_id & SNIC_TAG_IOCTL_DEV_RST) {
 943                 rqi = (struct snic_req_info *) ctx;
 944                 sc = rqi->sc;
 945 
 946                 goto ioctl_dev_rst;
 947         }
 948 
 949         if ((cmnd_id & SNIC_TAG_MASK) >= snic->max_tag_id) {
 950                 SNIC_HOST_ERR(snic->shost,
 951                               "Itmf_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
 952                               cmnd_id, snic_io_status_to_str(hdr_stat));
 953                 SNIC_BUG_ON(1);
 954 
 955                 return;
 956         }
 957 
 958         sc = scsi_host_find_tag(snic->shost, cmnd_id & SNIC_TAG_MASK);
 959         WARN_ON_ONCE(!sc);
 960 
 961 ioctl_dev_rst:
 962         if (!sc) {
 963                 atomic64_inc(&snic->s_stats.io.sc_null);
 964                 SNIC_HOST_ERR(snic->shost,
 965                               "Itmf_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
 966                               snic_io_status_to_str(hdr_stat), cmnd_id);
 967 
 968                 return;
 969         }
 970 
 971         snic_process_itmf_cmpl(snic, fwreq, cmnd_id, hdr_stat, sc);
 972 } /* end of snic_itmf_cmpl_handler */
 973 
 974 
 975 
 976 static void
 977 snic_hba_reset_scsi_cleanup(struct snic *snic, struct scsi_cmnd *sc)
 978 {
 979         struct snic_stats *st = &snic->s_stats;
 980         long act_ios = 0, act_fwreqs = 0;
 981 
 982         SNIC_SCSI_DBG(snic->shost, "HBA Reset scsi cleanup.\n");
 983         snic_scsi_cleanup(snic, snic_cmd_tag(sc));
 984 
 985         /* Update stats on pending IOs */
 986         act_ios = atomic64_read(&st->io.active);
 987         atomic64_add(act_ios, &st->io.compl);
 988         atomic64_sub(act_ios, &st->io.active);
 989 
 990         act_fwreqs = atomic64_read(&st->fw.actv_reqs);
 991         atomic64_sub(act_fwreqs, &st->fw.actv_reqs);
 992 }
 993 
 994 /*
 995  * snic_hba_reset_cmpl_handler :
 996  *
 997  * Notes :
 998  * 1. Cleanup all the scsi cmds, release all snic specific cmds
 999  * 2. Issue Report Targets in case of SAN targets
1000  */
1001 static int
1002 snic_hba_reset_cmpl_handler(struct snic *snic, struct snic_fw_req *fwreq)
1003 {
1004         ulong ctx;
1005         u32 cmnd_id;
1006         u32 hid;
1007         u8 typ;
1008         u8 hdr_stat;
1009         struct scsi_cmnd *sc = NULL;
1010         struct snic_req_info *rqi = NULL;
1011         spinlock_t *io_lock = NULL;
1012         unsigned long flags, gflags;
1013         int ret = 0;
1014 
1015         snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
1016         SNIC_HOST_INFO(snic->shost,
1017                        "reset_cmpl:Tag %d ctx %lx cmpl status %s HBA Reset Completion received.\n",
1018                        cmnd_id, ctx, snic_io_status_to_str(hdr_stat));
1019 
1020         SNIC_SCSI_DBG(snic->shost,
1021                       "reset_cmpl: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
1022                       typ, hdr_stat, cmnd_id, hid, ctx);
1023 
1024         /* spl case, host reset issued through ioctl */
1025         if (cmnd_id == SCSI_NO_TAG) {
1026                 rqi = (struct snic_req_info *) ctx;
1027                 SNIC_HOST_INFO(snic->shost,
1028                                "reset_cmpl:Tag %d ctx %lx cmpl stat %s\n",
1029                                cmnd_id, ctx, snic_io_status_to_str(hdr_stat));
1030                 sc = rqi->sc;
1031 
1032                 goto ioctl_hba_rst;
1033         }
1034 
1035         if (cmnd_id >= snic->max_tag_id) {
1036                 SNIC_HOST_ERR(snic->shost,
1037                               "reset_cmpl: Tag 0x%x out of Range,HdrStat %s\n",
1038                               cmnd_id, snic_io_status_to_str(hdr_stat));
1039                 SNIC_BUG_ON(1);
1040 
1041                 return 1;
1042         }
1043 
1044         sc = scsi_host_find_tag(snic->shost, cmnd_id);
1045 ioctl_hba_rst:
1046         if (!sc) {
1047                 atomic64_inc(&snic->s_stats.io.sc_null);
1048                 SNIC_HOST_ERR(snic->shost,
1049                               "reset_cmpl: sc is NULL - Hdr Stat %s Tag 0x%x\n",
1050                               snic_io_status_to_str(hdr_stat), cmnd_id);
1051                 ret = 1;
1052 
1053                 return ret;
1054         }
1055 
1056         SNIC_HOST_INFO(snic->shost,
1057                        "reset_cmpl: sc %p rqi %p Tag %d flags 0x%llx\n",
1058                        sc, rqi, cmnd_id, CMD_FLAGS(sc));
1059 
1060         io_lock = snic_io_lock_hash(snic, sc);
1061         spin_lock_irqsave(io_lock, flags);
1062 
1063         if (!snic->remove_wait) {
1064                 spin_unlock_irqrestore(io_lock, flags);
1065                 SNIC_HOST_ERR(snic->shost,
1066                               "reset_cmpl:host reset completed after timeout\n");
1067                 ret = 1;
1068 
1069                 return ret;
1070         }
1071 
1072         rqi = (struct snic_req_info *) CMD_SP(sc);
1073         WARN_ON_ONCE(!rqi);
1074 
1075         if (!rqi) {
1076                 atomic64_inc(&snic->s_stats.io.req_null);
1077                 spin_unlock_irqrestore(io_lock, flags);
1078                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1079                 SNIC_HOST_ERR(snic->shost,
1080                               "reset_cmpl: rqi is null,Hdr stat %s Tag 0x%x sc 0x%p flags 0x%llx\n",
1081                               snic_io_status_to_str(hdr_stat), cmnd_id, sc,
1082                               CMD_FLAGS(sc));
1083 
1084                 ret = 1;
1085 
1086                 return ret;
1087         }
1088         /* stats */
1089         spin_unlock_irqrestore(io_lock, flags);
1090 
1091         /* scsi cleanup */
1092         snic_hba_reset_scsi_cleanup(snic, sc);
1093 
1094         SNIC_BUG_ON(snic_get_state(snic) != SNIC_OFFLINE &&
1095                     snic_get_state(snic) != SNIC_FWRESET);
1096 
1097         /* Careful locking between snic_lock and io lock */
1098         spin_lock_irqsave(io_lock, flags);
1099         spin_lock_irqsave(&snic->snic_lock, gflags);
1100         if (snic_get_state(snic) == SNIC_FWRESET)
1101                 snic_set_state(snic, SNIC_ONLINE);
1102         spin_unlock_irqrestore(&snic->snic_lock, gflags);
1103 
1104         if (snic->remove_wait)
1105                 complete(snic->remove_wait);
1106 
1107         spin_unlock_irqrestore(io_lock, flags);
1108         atomic64_inc(&snic->s_stats.reset.hba_reset_cmpl);
1109 
1110         ret = 0;
1111         /* Rediscovery is for SAN */
1112         if (snic->config.xpt_type == SNIC_DAS)
1113                         return ret;
1114 
1115         SNIC_SCSI_DBG(snic->shost, "reset_cmpl: Queuing discovery work.\n");
1116         queue_work(snic_glob->event_q, &snic->disc_work);
1117 
1118         return ret;
1119 }
1120 
1121 static void
1122 snic_msg_ack_handler(struct snic *snic, struct snic_fw_req *fwreq)
1123 {
1124         SNIC_HOST_INFO(snic->shost, "Message Ack Received.\n");
1125 
1126         SNIC_ASSERT_NOT_IMPL(1);
1127 }
1128 
1129 static void
1130 snic_aen_handler(struct snic *snic, struct snic_fw_req *fwreq)
1131 {
1132         u8 typ, hdr_stat;
1133         u32 cmnd_id, hid;
1134         ulong ctx;
1135         struct snic_async_evnotify *aen = &fwreq->u.async_ev;
1136         u32 event_id = 0;
1137 
1138         snic_io_hdr_dec(&fwreq->hdr, &typ, &hdr_stat, &cmnd_id, &hid, &ctx);
1139         SNIC_SCSI_DBG(snic->shost,
1140                       "aen: type = %x, hdr_stat = %x, cmnd_id = %x, hid = %x, ctx = %lx\n",
1141                       typ, hdr_stat, cmnd_id, hid, ctx);
1142 
1143         event_id = le32_to_cpu(aen->ev_id);
1144 
1145         switch (event_id) {
1146         case SNIC_EV_TGT_OFFLINE:
1147                 SNIC_HOST_INFO(snic->shost, "aen:TGT_OFFLINE Event Recvd.\n");
1148                 break;
1149 
1150         case SNIC_EV_TGT_ONLINE:
1151                 SNIC_HOST_INFO(snic->shost, "aen:TGT_ONLINE Event Recvd.\n");
1152                 break;
1153 
1154         case SNIC_EV_LUN_OFFLINE:
1155                 SNIC_HOST_INFO(snic->shost, "aen:LUN_OFFLINE Event Recvd.\n");
1156                 break;
1157 
1158         case SNIC_EV_LUN_ONLINE:
1159                 SNIC_HOST_INFO(snic->shost, "aen:LUN_ONLINE Event Recvd.\n");
1160                 break;
1161 
1162         case SNIC_EV_CONF_CHG:
1163                 SNIC_HOST_INFO(snic->shost, "aen:Config Change Event Recvd.\n");
1164                 break;
1165 
1166         case SNIC_EV_TGT_ADDED:
1167                 SNIC_HOST_INFO(snic->shost, "aen:TGT_ADD Event Recvd.\n");
1168                 break;
1169 
1170         case SNIC_EV_TGT_DELTD:
1171                 SNIC_HOST_INFO(snic->shost, "aen:TGT_DEL Event Recvd.\n");
1172                 break;
1173 
1174         case SNIC_EV_LUN_ADDED:
1175                 SNIC_HOST_INFO(snic->shost, "aen:LUN_ADD Event Recvd.\n");
1176                 break;
1177 
1178         case SNIC_EV_LUN_DELTD:
1179                 SNIC_HOST_INFO(snic->shost, "aen:LUN_DEL Event Recvd.\n");
1180                 break;
1181 
1182         case SNIC_EV_DISC_CMPL:
1183                 SNIC_HOST_INFO(snic->shost, "aen:DISC_CMPL Event Recvd.\n");
1184                 break;
1185 
1186         default:
1187                 SNIC_HOST_INFO(snic->shost, "aen:Unknown Event Recvd.\n");
1188                 SNIC_BUG_ON(1);
1189                 break;
1190         }
1191 
1192         SNIC_ASSERT_NOT_IMPL(1);
1193 } /* end of snic_aen_handler */
1194 
1195 /*
1196  * snic_io_cmpl_handler
1197  * Routine to process CQ entries(IO Completions) posted by fw.
1198  */
1199 static int
1200 snic_io_cmpl_handler(struct vnic_dev *vdev,
1201                      unsigned int cq_idx,
1202                      struct snic_fw_req *fwreq)
1203 {
1204         struct snic *snic = svnic_dev_priv(vdev);
1205         u64 start = jiffies, cmpl_time;
1206 
1207         snic_print_desc(__func__, (char *)fwreq, sizeof(*fwreq));
1208 
1209         /* Update FW Stats */
1210         if ((fwreq->hdr.type >= SNIC_RSP_REPORT_TGTS_CMPL) &&
1211                 (fwreq->hdr.type <= SNIC_RSP_BOOT_LUNS_CMPL))
1212                 atomic64_dec(&snic->s_stats.fw.actv_reqs);
1213 
1214         SNIC_BUG_ON((fwreq->hdr.type > SNIC_RSP_BOOT_LUNS_CMPL) &&
1215                     (fwreq->hdr.type < SNIC_MSG_ASYNC_EVNOTIFY));
1216 
1217         /* Check for snic subsys errors */
1218         switch (fwreq->hdr.status) {
1219         case SNIC_STAT_NOT_READY:       /* XPT yet to initialize */
1220                 SNIC_HOST_ERR(snic->shost,
1221                               "sNIC SubSystem is NOT Ready.\n");
1222                 break;
1223 
1224         case SNIC_STAT_FATAL_ERROR:     /* XPT Error */
1225                 SNIC_HOST_ERR(snic->shost,
1226                               "sNIC SubSystem in Unrecoverable State.\n");
1227                 break;
1228         }
1229 
1230         switch (fwreq->hdr.type) {
1231         case SNIC_RSP_EXCH_VER_CMPL:
1232                 snic_io_exch_ver_cmpl_handler(snic, fwreq);
1233                 break;
1234 
1235         case SNIC_RSP_REPORT_TGTS_CMPL:
1236                 snic_report_tgt_cmpl_handler(snic, fwreq);
1237                 break;
1238 
1239         case SNIC_RSP_ICMND_CMPL:
1240                 snic_icmnd_cmpl_handler(snic, fwreq);
1241                 break;
1242 
1243         case SNIC_RSP_ITMF_CMPL:
1244                 snic_itmf_cmpl_handler(snic, fwreq);
1245                 break;
1246 
1247         case SNIC_RSP_HBA_RESET_CMPL:
1248                 snic_hba_reset_cmpl_handler(snic, fwreq);
1249                 break;
1250 
1251         case SNIC_MSG_ACK:
1252                 snic_msg_ack_handler(snic, fwreq);
1253                 break;
1254 
1255         case SNIC_MSG_ASYNC_EVNOTIFY:
1256                 snic_aen_handler(snic, fwreq);
1257                 break;
1258 
1259         default:
1260                 SNIC_BUG_ON(1);
1261                 SNIC_SCSI_DBG(snic->shost,
1262                               "Unknown Firmware completion request type %d\n",
1263                               fwreq->hdr.type);
1264                 break;
1265         }
1266 
1267         /* Update Stats */
1268         cmpl_time = jiffies - start;
1269         if (cmpl_time > atomic64_read(&snic->s_stats.io.max_cmpl_time))
1270                 atomic64_set(&snic->s_stats.io.max_cmpl_time, cmpl_time);
1271 
1272         return 0;
1273 } /* end of snic_io_cmpl_handler */
1274 
1275 /*
1276  * snic_fwcq_cmpl_handler
1277  * Routine to process fwCQ
1278  * This CQ is independent, and not associated with wq/rq/wq_copy queues
1279  */
1280 int
1281 snic_fwcq_cmpl_handler(struct snic *snic, int io_cmpl_work)
1282 {
1283         unsigned int num_ent = 0;       /* number cq entries processed */
1284         unsigned int cq_idx;
1285         unsigned int nent_per_cq;
1286         struct snic_misc_stats *misc_stats = &snic->s_stats.misc;
1287 
1288         for (cq_idx = snic->wq_count; cq_idx < snic->cq_count; cq_idx++) {
1289                 nent_per_cq = vnic_cq_fw_service(&snic->cq[cq_idx],
1290                                                  snic_io_cmpl_handler,
1291                                                  io_cmpl_work);
1292                 num_ent += nent_per_cq;
1293 
1294                 if (nent_per_cq > atomic64_read(&misc_stats->max_cq_ents))
1295                         atomic64_set(&misc_stats->max_cq_ents, nent_per_cq);
1296         }
1297 
1298         return num_ent;
1299 } /* end of snic_fwcq_cmpl_handler */
1300 
1301 /*
1302  * snic_queue_itmf_req: Common API to queue Task Management requests.
1303  * Use rqi->tm_tag for passing special tags.
1304  * @req_id : aborted request's tag, -1 for lun reset.
1305  */
1306 static int
1307 snic_queue_itmf_req(struct snic *snic,
1308                     struct snic_host_req *tmreq,
1309                     struct scsi_cmnd *sc,
1310                     u32 tmf,
1311                     u32 req_id)
1312 {
1313         struct snic_req_info *rqi = req_to_rqi(tmreq);
1314         struct scsi_lun lun;
1315         int tm_tag = snic_cmd_tag(sc) | rqi->tm_tag;
1316         int ret = 0;
1317 
1318         SNIC_BUG_ON(!rqi);
1319         SNIC_BUG_ON(!rqi->tm_tag);
1320 
1321         /* fill in lun info */
1322         int_to_scsilun(sc->device->lun, &lun);
1323 
1324         /* Initialize snic_host_req: itmf */
1325         snic_itmf_init(tmreq,
1326                        tm_tag,
1327                        snic->config.hid,
1328                        (ulong) rqi,
1329                        0 /* flags */,
1330                        req_id, /* Command to be aborted. */
1331                        rqi->tgt_id,
1332                        lun.scsi_lun,
1333                        tmf);
1334 
1335         /*
1336          * In case of multiple aborts on same cmd,
1337          * use try_wait_for_completion and completion_done() to check
1338          * whether it queues aborts even after completion of abort issued
1339          * prior.SNIC_BUG_ON(completion_done(&rqi->done));
1340          */
1341 
1342         ret = snic_queue_wq_desc(snic, tmreq, sizeof(*tmreq));
1343         if (ret)
1344                 SNIC_HOST_ERR(snic->shost,
1345                               "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d tag %d Failed, ret = %d\n",
1346                               tmf, sc, rqi, req_id, snic_cmd_tag(sc), ret);
1347         else
1348                 SNIC_SCSI_DBG(snic->shost,
1349                               "qitmf:Queuing ITMF(%d) Req sc %p, rqi %p, req_id %d, tag %d (req_id)- Success.",
1350                               tmf, sc, rqi, req_id, snic_cmd_tag(sc));
1351 
1352         return ret;
1353 } /* end of snic_queue_itmf_req */
1354 
1355 static int
1356 snic_issue_tm_req(struct snic *snic,
1357                     struct snic_req_info *rqi,
1358                     struct scsi_cmnd *sc,
1359                     int tmf)
1360 {
1361         struct snic_host_req *tmreq = NULL;
1362         int req_id = 0, tag = snic_cmd_tag(sc);
1363         int ret = 0;
1364 
1365         if (snic_get_state(snic) == SNIC_FWRESET)
1366                 return -EBUSY;
1367 
1368         atomic_inc(&snic->ios_inflight);
1369 
1370         SNIC_SCSI_DBG(snic->shost,
1371                       "issu_tmreq: Task mgmt req %d. rqi %p w/ tag %x\n",
1372                       tmf, rqi, tag);
1373 
1374 
1375         if (tmf == SNIC_ITMF_LUN_RESET) {
1376                 tmreq = snic_dr_req_init(snic, rqi);
1377                 req_id = SCSI_NO_TAG;
1378         } else {
1379                 tmreq = snic_abort_req_init(snic, rqi);
1380                 req_id = tag;
1381         }
1382 
1383         if (!tmreq) {
1384                 ret = -ENOMEM;
1385 
1386                 goto tmreq_err;
1387         }
1388 
1389         ret = snic_queue_itmf_req(snic, tmreq, sc, tmf, req_id);
1390         if (ret)
1391                 goto tmreq_err;
1392 
1393         ret = 0;
1394 
1395 tmreq_err:
1396         if (ret) {
1397                 SNIC_HOST_ERR(snic->shost,
1398                               "issu_tmreq: Queing ITMF(%d) Req, sc %p rqi %p req_id %d tag %x fails err = %d\n",
1399                               tmf, sc, rqi, req_id, tag, ret);
1400         } else {
1401                 SNIC_SCSI_DBG(snic->shost,
1402                               "issu_tmreq: Queuing ITMF(%d) Req, sc %p, rqi %p, req_id %d tag %x - Success.\n",
1403                               tmf, sc, rqi, req_id, tag);
1404         }
1405 
1406         atomic_dec(&snic->ios_inflight);
1407 
1408         return ret;
1409 }
1410 
1411 /*
1412  * snic_queue_abort_req : Queues abort req to WQ
1413  */
1414 static int
1415 snic_queue_abort_req(struct snic *snic,
1416                      struct snic_req_info *rqi,
1417                      struct scsi_cmnd *sc,
1418                      int tmf)
1419 {
1420         SNIC_SCSI_DBG(snic->shost, "q_abtreq: sc %p, rqi %p, tag %x, tmf %d\n",
1421                       sc, rqi, snic_cmd_tag(sc), tmf);
1422 
1423         /* Add special tag for abort */
1424         rqi->tm_tag |= SNIC_TAG_ABORT;
1425 
1426         return snic_issue_tm_req(snic, rqi, sc, tmf);
1427 }
1428 
1429 /*
1430  * snic_abort_finish : called by snic_abort_cmd on queuing abort successfully.
1431  */
1432 static int
1433 snic_abort_finish(struct snic *snic, struct scsi_cmnd *sc)
1434 {
1435         struct snic_req_info *rqi = NULL;
1436         spinlock_t *io_lock = NULL;
1437         unsigned long flags;
1438         int ret = 0, tag = snic_cmd_tag(sc);
1439 
1440         io_lock = snic_io_lock_hash(snic, sc);
1441         spin_lock_irqsave(io_lock, flags);
1442         rqi = (struct snic_req_info *) CMD_SP(sc);
1443         if (!rqi) {
1444                 atomic64_inc(&snic->s_stats.io.req_null);
1445                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1446 
1447                 SNIC_SCSI_DBG(snic->shost,
1448                               "abt_fini:req info is null tag 0x%x, sc 0x%p flags 0x%llx\n",
1449                               tag, sc, CMD_FLAGS(sc));
1450                 ret = FAILED;
1451 
1452                 goto abort_fail;
1453         }
1454 
1455         rqi->abts_done = NULL;
1456 
1457         ret = FAILED;
1458 
1459         /* Check the abort status. */
1460         switch (CMD_ABTS_STATUS(sc)) {
1461         case SNIC_INVALID_CODE:
1462                 /* Firmware didn't complete abort req, timedout */
1463                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TIMEDOUT;
1464                 atomic64_inc(&snic->s_stats.abts.drv_tmo);
1465                 SNIC_SCSI_DBG(snic->shost,
1466                               "abt_fini:sc %p Tag %x Driver Timeout.flags 0x%llx\n",
1467                               sc, snic_cmd_tag(sc), CMD_FLAGS(sc));
1468                 /* do not release snic request in timedout case */
1469                 rqi = NULL;
1470 
1471                 goto abort_fail;
1472 
1473         case SNIC_STAT_IO_SUCCESS:
1474         case SNIC_STAT_IO_NOT_FOUND:
1475                 ret = SUCCESS;
1476                 /*
1477                  * If abort path doesn't call scsi_done(),
1478                  * the # IO timeouts == 2, will cause the LUN offline.
1479                  * Call scsi_done to complete the IO.
1480                  */
1481                 sc->result = (DID_ERROR << 16);
1482                 sc->scsi_done(sc);
1483                 break;
1484 
1485         default:
1486                 /* Firmware completed abort with error */
1487                 ret = FAILED;
1488                 rqi = NULL;
1489                 break;
1490         }
1491 
1492         CMD_SP(sc) = NULL;
1493         SNIC_HOST_INFO(snic->shost,
1494                        "abt_fini: Tag %x, Cmpl Status %s flags 0x%llx\n",
1495                        tag, snic_io_status_to_str(CMD_ABTS_STATUS(sc)),
1496                        CMD_FLAGS(sc));
1497 
1498 abort_fail:
1499         spin_unlock_irqrestore(io_lock, flags);
1500         if (rqi)
1501                 snic_release_req_buf(snic, rqi, sc);
1502 
1503         return ret;
1504 } /* end of snic_abort_finish */
1505 
1506 /*
1507  * snic_send_abort_and_wait : Issues Abort, and Waits
1508  */
1509 static int
1510 snic_send_abort_and_wait(struct snic *snic, struct scsi_cmnd *sc)
1511 {
1512         struct snic_req_info *rqi = NULL;
1513         enum snic_ioreq_state sv_state;
1514         struct snic_tgt *tgt = NULL;
1515         spinlock_t *io_lock = NULL;
1516         DECLARE_COMPLETION_ONSTACK(tm_done);
1517         unsigned long flags;
1518         int ret = 0, tmf = 0, tag = snic_cmd_tag(sc);
1519 
1520         tgt = starget_to_tgt(scsi_target(sc->device));
1521         if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
1522                 tmf = SNIC_ITMF_ABTS_TASK_TERM;
1523         else
1524                 tmf = SNIC_ITMF_ABTS_TASK;
1525 
1526         /* stats */
1527 
1528         io_lock = snic_io_lock_hash(snic, sc);
1529 
1530         /*
1531          * Avoid a race between SCSI issuing the abort and the device
1532          * completing the command.
1533          *
1534          * If the command is already completed by fw_cmpl code,
1535          * we just return SUCCESS from here. This means that the abort
1536          * succeeded. In the SCSI ML, since the timeout for command has
1537          * happend, the completion wont actually complete the command
1538          * and it will be considered as an aborted command
1539          *
1540          * The CMD_SP will not be cleared except while holding io_lock
1541          */
1542         spin_lock_irqsave(io_lock, flags);
1543         rqi = (struct snic_req_info *) CMD_SP(sc);
1544         if (!rqi) {
1545                 spin_unlock_irqrestore(io_lock, flags);
1546 
1547                 SNIC_HOST_ERR(snic->shost,
1548                               "abt_cmd: rqi is null. Tag %d flags 0x%llx\n",
1549                               tag, CMD_FLAGS(sc));
1550 
1551                 ret = SUCCESS;
1552 
1553                 goto send_abts_end;
1554         }
1555 
1556         rqi->abts_done = &tm_done;
1557         if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1558                 spin_unlock_irqrestore(io_lock, flags);
1559 
1560                 ret = 0;
1561                 goto abts_pending;
1562         }
1563         SNIC_BUG_ON(!rqi->abts_done);
1564 
1565         /* Save Command State, should be restored on failed to Queue. */
1566         sv_state = CMD_STATE(sc);
1567 
1568         /*
1569          * Command is still pending, need to abort it
1570          * If the fw completes the command after this point,
1571          * the completion won't be done till mid-layer, since abot
1572          * has already started.
1573          */
1574         CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1575         CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1576 
1577         SNIC_SCSI_DBG(snic->shost, "send_abt_cmd: TAG 0x%x\n", tag);
1578 
1579         spin_unlock_irqrestore(io_lock, flags);
1580 
1581         /* Now Queue the abort command to firmware */
1582         ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1583         if (ret) {
1584                 atomic64_inc(&snic->s_stats.abts.q_fail);
1585                 SNIC_HOST_ERR(snic->shost,
1586                               "send_abt_cmd: IO w/ Tag 0x%x fail w/ err %d flags 0x%llx\n",
1587                               tag, ret, CMD_FLAGS(sc));
1588 
1589                 spin_lock_irqsave(io_lock, flags);
1590                 /* Restore Command's previous state */
1591                 CMD_STATE(sc) = sv_state;
1592                 rqi = (struct snic_req_info *) CMD_SP(sc);
1593                 if (rqi)
1594                         rqi->abts_done = NULL;
1595                 spin_unlock_irqrestore(io_lock, flags);
1596                 ret = FAILED;
1597 
1598                 goto send_abts_end;
1599         }
1600 
1601         spin_lock_irqsave(io_lock, flags);
1602         if (tmf == SNIC_ITMF_ABTS_TASK) {
1603                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_ISSUED;
1604                 atomic64_inc(&snic->s_stats.abts.num);
1605         } else {
1606                 /* term stats */
1607                 CMD_FLAGS(sc) |= SNIC_IO_TERM_ISSUED;
1608         }
1609         spin_unlock_irqrestore(io_lock, flags);
1610 
1611         SNIC_SCSI_DBG(snic->shost,
1612                       "send_abt_cmd: sc %p Tag %x flags 0x%llx\n",
1613                       sc, tag, CMD_FLAGS(sc));
1614 
1615 
1616         ret = 0;
1617 
1618 abts_pending:
1619         /*
1620          * Queued an abort IO, wait for its completion.
1621          * Once the fw completes the abort command, it will
1622          * wakeup this thread.
1623          */
1624         wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1625 
1626 send_abts_end:
1627         return ret;
1628 } /* end of snic_send_abort_and_wait */
1629 
1630 /*
1631  * This function is exported to SCSI for sending abort cmnds.
1632  * A SCSI IO is represent by snic_ioreq in the driver.
1633  * The snic_ioreq is linked to the SCSI Cmd, thus a link with the ULP'S IO
1634  */
1635 int
1636 snic_abort_cmd(struct scsi_cmnd *sc)
1637 {
1638         struct snic *snic = shost_priv(sc->device->host);
1639         int ret = SUCCESS, tag = snic_cmd_tag(sc);
1640         u32 start_time = jiffies;
1641 
1642         SNIC_SCSI_DBG(snic->shost, "abt_cmd:sc %p :0x%x :req = %p :tag = %d\n",
1643                        sc, sc->cmnd[0], sc->request, tag);
1644 
1645         if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
1646                 SNIC_HOST_ERR(snic->shost,
1647                               "abt_cmd: tag %x Parent Devs are not rdy\n",
1648                               tag);
1649                 ret = FAST_IO_FAIL;
1650 
1651                 goto abort_end;
1652         }
1653 
1654 
1655         ret = snic_send_abort_and_wait(snic, sc);
1656         if (ret)
1657                 goto abort_end;
1658 
1659         ret = snic_abort_finish(snic, sc);
1660 
1661 abort_end:
1662         SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
1663                  jiffies_to_msecs(jiffies - start_time), 0,
1664                  SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
1665 
1666         SNIC_SCSI_DBG(snic->shost,
1667                       "abts: Abort Req Status = %s\n",
1668                       (ret == SUCCESS) ? "SUCCESS" :
1669                        ((ret == FAST_IO_FAIL) ? "FAST_IO_FAIL" : "FAILED"));
1670 
1671         return ret;
1672 }
1673 
1674 
1675 
1676 static int
1677 snic_is_abts_pending(struct snic *snic, struct scsi_cmnd *lr_sc)
1678 {
1679         struct snic_req_info *rqi = NULL;
1680         struct scsi_cmnd *sc = NULL;
1681         struct scsi_device *lr_sdev = NULL;
1682         spinlock_t *io_lock = NULL;
1683         u32 tag;
1684         unsigned long flags;
1685 
1686         if (lr_sc)
1687                 lr_sdev = lr_sc->device;
1688 
1689         /* walk through the tag map, an dcheck if IOs are still pending in fw*/
1690         for (tag = 0; tag < snic->max_tag_id; tag++) {
1691                 io_lock = snic_io_lock_tag(snic, tag);
1692 
1693                 spin_lock_irqsave(io_lock, flags);
1694                 sc = scsi_host_find_tag(snic->shost, tag);
1695 
1696                 if (!sc || (lr_sc && (sc->device != lr_sdev || sc == lr_sc))) {
1697                         spin_unlock_irqrestore(io_lock, flags);
1698 
1699                         continue;
1700                 }
1701 
1702                 rqi = (struct snic_req_info *) CMD_SP(sc);
1703                 if (!rqi) {
1704                         spin_unlock_irqrestore(io_lock, flags);
1705 
1706                         continue;
1707                 }
1708 
1709                 /*
1710                  * Found IO that is still pending w/ firmware and belongs to
1711                  * the LUN that is under reset, if lr_sc != NULL
1712                  */
1713                 SNIC_SCSI_DBG(snic->shost, "Found IO in %s on LUN\n",
1714                               snic_ioreq_state_to_str(CMD_STATE(sc)));
1715 
1716                 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING) {
1717                         spin_unlock_irqrestore(io_lock, flags);
1718 
1719                         return 1;
1720                 }
1721 
1722                 spin_unlock_irqrestore(io_lock, flags);
1723         }
1724 
1725         return 0;
1726 } /* end of snic_is_abts_pending */
1727 
1728 static int
1729 snic_dr_clean_single_req(struct snic *snic,
1730                          u32 tag,
1731                          struct scsi_device *lr_sdev)
1732 {
1733         struct snic_req_info *rqi = NULL;
1734         struct snic_tgt *tgt = NULL;
1735         struct scsi_cmnd *sc = NULL;
1736         spinlock_t *io_lock = NULL;
1737         u32 sv_state = 0, tmf = 0;
1738         DECLARE_COMPLETION_ONSTACK(tm_done);
1739         unsigned long flags;
1740         int ret = 0;
1741 
1742         io_lock = snic_io_lock_tag(snic, tag);
1743         spin_lock_irqsave(io_lock, flags);
1744         sc = scsi_host_find_tag(snic->shost, tag);
1745 
1746         /* Ignore Cmd that don't belong to Lun Reset device */
1747         if (!sc || sc->device != lr_sdev)
1748                 goto skip_clean;
1749 
1750         rqi = (struct snic_req_info *) CMD_SP(sc);
1751 
1752         if (!rqi)
1753                 goto skip_clean;
1754 
1755 
1756         if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1757                 goto skip_clean;
1758 
1759 
1760         if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
1761                         (!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
1762 
1763                 SNIC_SCSI_DBG(snic->shost,
1764                               "clean_single_req: devrst is not pending sc 0x%p\n",
1765                               sc);
1766 
1767                 goto skip_clean;
1768         }
1769 
1770         SNIC_SCSI_DBG(snic->shost,
1771                 "clean_single_req: Found IO in %s on lun\n",
1772                 snic_ioreq_state_to_str(CMD_STATE(sc)));
1773 
1774         /* Save Command State */
1775         sv_state = CMD_STATE(sc);
1776 
1777         /*
1778          * Any pending IO issued prior to reset is expected to be
1779          * in abts pending state, if not we need to set SNIC_IOREQ_ABTS_PENDING
1780          * to indicate the IO is abort pending.
1781          * When IO is completed, the IO will be handed over and handled
1782          * in this function.
1783          */
1784 
1785         CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
1786         SNIC_BUG_ON(rqi->abts_done);
1787 
1788         if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
1789                 rqi->tm_tag = SNIC_TAG_DEV_RST;
1790 
1791                 SNIC_SCSI_DBG(snic->shost,
1792                               "clean_single_req:devrst sc 0x%p\n", sc);
1793         }
1794 
1795         CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
1796         rqi->abts_done = &tm_done;
1797         spin_unlock_irqrestore(io_lock, flags);
1798 
1799         tgt = starget_to_tgt(scsi_target(sc->device));
1800         if ((snic_tgt_chkready(tgt) != 0) && (tgt->tdata.typ == SNIC_TGT_SAN))
1801                 tmf = SNIC_ITMF_ABTS_TASK_TERM;
1802         else
1803                 tmf = SNIC_ITMF_ABTS_TASK;
1804 
1805         /* Now queue the abort command to firmware */
1806         ret = snic_queue_abort_req(snic, rqi, sc, tmf);
1807         if (ret) {
1808                 SNIC_HOST_ERR(snic->shost,
1809                               "clean_single_req_err:sc %p, tag %d abt failed. tm_tag %d flags 0x%llx\n",
1810                               sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
1811 
1812                 spin_lock_irqsave(io_lock, flags);
1813                 rqi = (struct snic_req_info *) CMD_SP(sc);
1814                 if (rqi)
1815                         rqi->abts_done = NULL;
1816 
1817                 /* Restore Command State */
1818                 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
1819                         CMD_STATE(sc) = sv_state;
1820 
1821                 ret = 1;
1822                 goto skip_clean;
1823         }
1824 
1825         spin_lock_irqsave(io_lock, flags);
1826         if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
1827                 CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
1828 
1829         CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
1830         spin_unlock_irqrestore(io_lock, flags);
1831 
1832         wait_for_completion_timeout(&tm_done, SNIC_ABTS_TIMEOUT);
1833 
1834         /* Recheck cmd state to check if it now aborted. */
1835         spin_lock_irqsave(io_lock, flags);
1836         rqi = (struct snic_req_info *) CMD_SP(sc);
1837         if (!rqi) {
1838                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_REQ_NULL;
1839                 goto skip_clean;
1840         }
1841         rqi->abts_done = NULL;
1842 
1843         /* if abort is still pending w/ fw, fail */
1844         if (CMD_ABTS_STATUS(sc) == SNIC_INVALID_CODE) {
1845                 SNIC_HOST_ERR(snic->shost,
1846                               "clean_single_req_err:sc %p tag %d abt still pending w/ fw, tm_tag %d flags 0x%llx\n",
1847                               sc, tag, rqi->tm_tag, CMD_FLAGS(sc));
1848 
1849                 CMD_FLAGS(sc) |= SNIC_IO_ABTS_TERM_DONE;
1850                 ret = 1;
1851 
1852                 goto skip_clean;
1853         }
1854 
1855         CMD_STATE(sc) = SNIC_IOREQ_ABTS_COMPLETE;
1856         CMD_SP(sc) = NULL;
1857         spin_unlock_irqrestore(io_lock, flags);
1858 
1859         snic_release_req_buf(snic, rqi, sc);
1860 
1861         sc->result = (DID_ERROR << 16);
1862         sc->scsi_done(sc);
1863 
1864         ret = 0;
1865 
1866         return ret;
1867 
1868 skip_clean:
1869         spin_unlock_irqrestore(io_lock, flags);
1870 
1871         return ret;
1872 } /* end of snic_dr_clean_single_req */
1873 
1874 static int
1875 snic_dr_clean_pending_req(struct snic *snic, struct scsi_cmnd *lr_sc)
1876 {
1877         struct scsi_device *lr_sdev = lr_sc->device;
1878         u32 tag = 0;
1879         int ret = FAILED;
1880 
1881         for (tag = 0; tag < snic->max_tag_id; tag++) {
1882                 if (tag == snic_cmd_tag(lr_sc))
1883                         continue;
1884 
1885                 ret = snic_dr_clean_single_req(snic, tag, lr_sdev);
1886                 if (ret) {
1887                         SNIC_HOST_ERR(snic->shost, "clean_err:tag = %d\n", tag);
1888 
1889                         goto clean_err;
1890                 }
1891         }
1892 
1893         schedule_timeout(msecs_to_jiffies(100));
1894 
1895         /* Walk through all the cmds and check abts status. */
1896         if (snic_is_abts_pending(snic, lr_sc)) {
1897                 ret = FAILED;
1898 
1899                 goto clean_err;
1900         }
1901 
1902         ret = 0;
1903         SNIC_SCSI_DBG(snic->shost, "clean_pending_req: Success.\n");
1904 
1905         return ret;
1906 
1907 clean_err:
1908         ret = FAILED;
1909         SNIC_HOST_ERR(snic->shost,
1910                       "Failed to Clean Pending IOs on %s device.\n",
1911                       dev_name(&lr_sdev->sdev_gendev));
1912 
1913         return ret;
1914 
1915 } /* end of snic_dr_clean_pending_req */
1916 
1917 /*
1918  * snic_dr_finish : Called by snic_device_reset
1919  */
1920 static int
1921 snic_dr_finish(struct snic *snic, struct scsi_cmnd *sc)
1922 {
1923         struct snic_req_info *rqi = NULL;
1924         spinlock_t *io_lock = NULL;
1925         unsigned long flags;
1926         int lr_res = 0;
1927         int ret = FAILED;
1928 
1929         io_lock = snic_io_lock_hash(snic, sc);
1930         spin_lock_irqsave(io_lock, flags);
1931         rqi = (struct snic_req_info *) CMD_SP(sc);
1932         if (!rqi) {
1933                 spin_unlock_irqrestore(io_lock, flags);
1934                 SNIC_SCSI_DBG(snic->shost,
1935                               "dr_fini: rqi is null tag 0x%x sc 0x%p flags 0x%llx\n",
1936                               snic_cmd_tag(sc), sc, CMD_FLAGS(sc));
1937 
1938                 ret = FAILED;
1939                 goto dr_fini_end;
1940         }
1941 
1942         rqi->dr_done = NULL;
1943 
1944         lr_res = CMD_LR_STATUS(sc);
1945 
1946         switch (lr_res) {
1947         case SNIC_INVALID_CODE:
1948                 /* stats */
1949                 SNIC_SCSI_DBG(snic->shost,
1950                               "dr_fini: Tag %x Dev Reset Timedout. flags 0x%llx\n",
1951                               snic_cmd_tag(sc), CMD_FLAGS(sc));
1952 
1953                 CMD_FLAGS(sc) |= SNIC_DEV_RST_TIMEDOUT;
1954                 ret = FAILED;
1955 
1956                 goto dr_failed;
1957 
1958         case SNIC_STAT_IO_SUCCESS:
1959                 SNIC_SCSI_DBG(snic->shost,
1960                               "dr_fini: Tag %x Dev Reset cmpl\n",
1961                               snic_cmd_tag(sc));
1962                 ret = 0;
1963                 break;
1964 
1965         default:
1966                 SNIC_HOST_ERR(snic->shost,
1967                               "dr_fini:Device Reset completed& failed.Tag = %x lr_status %s flags 0x%llx\n",
1968                               snic_cmd_tag(sc),
1969                               snic_io_status_to_str(lr_res), CMD_FLAGS(sc));
1970                 ret = FAILED;
1971                 goto dr_failed;
1972         }
1973         spin_unlock_irqrestore(io_lock, flags);
1974 
1975         /*
1976          * Cleanup any IOs on this LUN that have still not completed.
1977          * If any of these fail, then LUN Reset fails.
1978          * Cleanup cleans all commands on this LUN except
1979          * the lun reset command. If all cmds get cleaned, the LUN Reset
1980          * succeeds.
1981          */
1982 
1983         ret = snic_dr_clean_pending_req(snic, sc);
1984         if (ret) {
1985                 spin_lock_irqsave(io_lock, flags);
1986                 SNIC_SCSI_DBG(snic->shost,
1987                               "dr_fini: Device Reset Failed since could not abort all IOs. Tag = %x.\n",
1988                               snic_cmd_tag(sc));
1989                 rqi = (struct snic_req_info *) CMD_SP(sc);
1990 
1991                 goto dr_failed;
1992         } else {
1993                 /* Cleanup LUN Reset Command */
1994                 spin_lock_irqsave(io_lock, flags);
1995                 rqi = (struct snic_req_info *) CMD_SP(sc);
1996                 if (rqi)
1997                         ret = SUCCESS; /* Completed Successfully */
1998                 else
1999                         ret = FAILED;
2000         }
2001 
2002 dr_failed:
2003         lockdep_assert_held(io_lock);
2004         if (rqi)
2005                 CMD_SP(sc) = NULL;
2006         spin_unlock_irqrestore(io_lock, flags);
2007 
2008         if (rqi)
2009                 snic_release_req_buf(snic, rqi, sc);
2010 
2011 dr_fini_end:
2012         return ret;
2013 } /* end of snic_dr_finish */
2014 
2015 static int
2016 snic_queue_dr_req(struct snic *snic,
2017                   struct snic_req_info *rqi,
2018                   struct scsi_cmnd *sc)
2019 {
2020         /* Add special tag for device reset */
2021         rqi->tm_tag |= SNIC_TAG_DEV_RST;
2022 
2023         return snic_issue_tm_req(snic, rqi, sc, SNIC_ITMF_LUN_RESET);
2024 }
2025 
2026 static int
2027 snic_send_dr_and_wait(struct snic *snic, struct scsi_cmnd *sc)
2028 {
2029         struct snic_req_info *rqi = NULL;
2030         enum snic_ioreq_state sv_state;
2031         spinlock_t *io_lock = NULL;
2032         unsigned long flags;
2033         DECLARE_COMPLETION_ONSTACK(tm_done);
2034         int ret = FAILED, tag = snic_cmd_tag(sc);
2035 
2036         io_lock = snic_io_lock_hash(snic, sc);
2037         spin_lock_irqsave(io_lock, flags);
2038         CMD_FLAGS(sc) |= SNIC_DEVICE_RESET;
2039         rqi = (struct snic_req_info *) CMD_SP(sc);
2040         if (!rqi) {
2041                 SNIC_HOST_ERR(snic->shost,
2042                               "send_dr: rqi is null, Tag 0x%x flags 0x%llx\n",
2043                               tag, CMD_FLAGS(sc));
2044                 spin_unlock_irqrestore(io_lock, flags);
2045 
2046                 ret = FAILED;
2047                 goto send_dr_end;
2048         }
2049 
2050         /* Save Command state to restore in case Queuing failed. */
2051         sv_state = CMD_STATE(sc);
2052 
2053         CMD_STATE(sc) = SNIC_IOREQ_LR_PENDING;
2054         CMD_LR_STATUS(sc) = SNIC_INVALID_CODE;
2055 
2056         SNIC_SCSI_DBG(snic->shost, "dr: TAG = %x\n", tag);
2057 
2058         rqi->dr_done = &tm_done;
2059         SNIC_BUG_ON(!rqi->dr_done);
2060 
2061         spin_unlock_irqrestore(io_lock, flags);
2062         /*
2063          * The Command state is changed to IOREQ_PENDING,
2064          * in this case, if the command is completed, the icmnd_cmpl will
2065          * mark the cmd as completed.
2066          * This logic still makes LUN Reset is inevitable.
2067          */
2068 
2069         ret = snic_queue_dr_req(snic, rqi, sc);
2070         if (ret) {
2071                 SNIC_HOST_ERR(snic->shost,
2072                               "send_dr: IO w/ Tag 0x%x Failed err = %d. flags 0x%llx\n",
2073                               tag, ret, CMD_FLAGS(sc));
2074 
2075                 spin_lock_irqsave(io_lock, flags);
2076                 /* Restore State */
2077                 CMD_STATE(sc) = sv_state;
2078                 rqi = (struct snic_req_info *) CMD_SP(sc);
2079                 if (rqi)
2080                         rqi->dr_done = NULL;
2081                 /* rqi is freed in caller. */
2082                 spin_unlock_irqrestore(io_lock, flags);
2083                 ret = FAILED;
2084 
2085                 goto send_dr_end;
2086         }
2087 
2088         spin_lock_irqsave(io_lock, flags);
2089         CMD_FLAGS(sc) |= SNIC_DEV_RST_ISSUED;
2090         spin_unlock_irqrestore(io_lock, flags);
2091 
2092         ret = 0;
2093 
2094         wait_for_completion_timeout(&tm_done, SNIC_LUN_RESET_TIMEOUT);
2095 
2096 send_dr_end:
2097         return ret;
2098 }
2099 
2100 /*
2101  * auxillary funciton to check lun reset op is supported or not
2102  * Not supported if returns 0
2103  */
2104 static int
2105 snic_dev_reset_supported(struct scsi_device *sdev)
2106 {
2107         struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
2108 
2109         if (tgt->tdata.typ == SNIC_TGT_DAS)
2110                 return 0;
2111 
2112         return 1;
2113 }
2114 
2115 static void
2116 snic_unlink_and_release_req(struct snic *snic, struct scsi_cmnd *sc, int flag)
2117 {
2118         struct snic_req_info *rqi = NULL;
2119         spinlock_t *io_lock = NULL;
2120         unsigned long flags;
2121         u32 start_time = jiffies;
2122 
2123         io_lock = snic_io_lock_hash(snic, sc);
2124         spin_lock_irqsave(io_lock, flags);
2125         rqi = (struct snic_req_info *) CMD_SP(sc);
2126         if (rqi) {
2127                 start_time = rqi->start_time;
2128                 CMD_SP(sc) = NULL;
2129         }
2130 
2131         CMD_FLAGS(sc) |= flag;
2132         spin_unlock_irqrestore(io_lock, flags);
2133 
2134         if (rqi)
2135                 snic_release_req_buf(snic, rqi, sc);
2136 
2137         SNIC_TRC(snic->shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2138                  jiffies_to_msecs(jiffies - start_time), (ulong) rqi,
2139                  SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2140 }
2141 
2142 /*
2143  * SCSI Eh thread issues a LUN Reset when one or more commands on a LUN
2144  * fail to get aborted. It calls driver's eh_device_reset with a SCSI
2145  * command on the LUN.
2146  */
2147 int
2148 snic_device_reset(struct scsi_cmnd *sc)
2149 {
2150         struct Scsi_Host *shost = sc->device->host;
2151         struct snic *snic = shost_priv(shost);
2152         struct snic_req_info *rqi = NULL;
2153         int tag = snic_cmd_tag(sc);
2154         int start_time = jiffies;
2155         int ret = FAILED;
2156         int dr_supp = 0;
2157 
2158         SNIC_SCSI_DBG(shost, "dev_reset:sc %p :0x%x :req = %p :tag = %d\n",
2159                       sc, sc->cmnd[0], sc->request,
2160                       snic_cmd_tag(sc));
2161         dr_supp = snic_dev_reset_supported(sc->device);
2162         if (!dr_supp) {
2163                 /* device reset op is not supported */
2164                 SNIC_HOST_INFO(shost, "LUN Reset Op not supported.\n");
2165                 snic_unlink_and_release_req(snic, sc, SNIC_DEV_RST_NOTSUP);
2166 
2167                 goto dev_rst_end;
2168         }
2169 
2170         if (unlikely(snic_get_state(snic) != SNIC_ONLINE)) {
2171                 snic_unlink_and_release_req(snic, sc, 0);
2172                 SNIC_HOST_ERR(shost, "Devrst: Parent Devs are not online.\n");
2173 
2174                 goto dev_rst_end;
2175         }
2176 
2177         /* There is no tag when lun reset is issue through ioctl. */
2178         if (unlikely(tag <= SNIC_NO_TAG)) {
2179                 SNIC_HOST_INFO(snic->shost,
2180                                "Devrst: LUN Reset Recvd thru IOCTL.\n");
2181 
2182                 rqi = snic_req_init(snic, 0);
2183                 if (!rqi)
2184                         goto dev_rst_end;
2185 
2186                 memset(scsi_cmd_priv(sc), 0,
2187                         sizeof(struct snic_internal_io_state));
2188                 CMD_SP(sc) = (char *)rqi;
2189                 CMD_FLAGS(sc) = SNIC_NO_FLAGS;
2190 
2191                 /* Add special tag for dr coming from user spc */
2192                 rqi->tm_tag = SNIC_TAG_IOCTL_DEV_RST;
2193                 rqi->sc = sc;
2194         }
2195 
2196         ret = snic_send_dr_and_wait(snic, sc);
2197         if (ret) {
2198                 SNIC_HOST_ERR(snic->shost,
2199                               "Devrst: IO w/ Tag %x Failed w/ err = %d\n",
2200                               tag, ret);
2201 
2202                 snic_unlink_and_release_req(snic, sc, 0);
2203 
2204                 goto dev_rst_end;
2205         }
2206 
2207         ret = snic_dr_finish(snic, sc);
2208 
2209 dev_rst_end:
2210         SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2211                  jiffies_to_msecs(jiffies - start_time),
2212                  0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2213 
2214         SNIC_SCSI_DBG(snic->shost,
2215                       "Devrst: Returning from Device Reset : %s\n",
2216                       (ret == SUCCESS) ? "SUCCESS" : "FAILED");
2217 
2218         return ret;
2219 } /* end of snic_device_reset */
2220 
2221 /*
2222  * SCSI Error handling calls driver's eh_host_reset if all prior
2223  * error handling levels return FAILED.
2224  *
2225  * Host Reset is the highest level of error recovery. If this fails, then
2226  * host is offlined by SCSI.
2227  */
2228 /*
2229  * snic_issue_hba_reset : Queues FW Reset Request.
2230  */
2231 static int
2232 snic_issue_hba_reset(struct snic *snic, struct scsi_cmnd *sc)
2233 {
2234         struct snic_req_info *rqi = NULL;
2235         struct snic_host_req *req = NULL;
2236         spinlock_t *io_lock = NULL;
2237         DECLARE_COMPLETION_ONSTACK(wait);
2238         unsigned long flags;
2239         int ret = -ENOMEM;
2240 
2241         rqi = snic_req_init(snic, 0);
2242         if (!rqi) {
2243                 ret = -ENOMEM;
2244 
2245                 goto hba_rst_end;
2246         }
2247 
2248         if (snic_cmd_tag(sc) == SCSI_NO_TAG) {
2249                 memset(scsi_cmd_priv(sc), 0,
2250                         sizeof(struct snic_internal_io_state));
2251                 SNIC_HOST_INFO(snic->shost, "issu_hr:Host reset thru ioctl.\n");
2252                 rqi->sc = sc;
2253         }
2254 
2255         req = rqi_to_req(rqi);
2256 
2257         io_lock = snic_io_lock_hash(snic, sc);
2258         spin_lock_irqsave(io_lock, flags);
2259         SNIC_BUG_ON(CMD_SP(sc) != NULL);
2260         CMD_STATE(sc) = SNIC_IOREQ_PENDING;
2261         CMD_SP(sc) = (char *) rqi;
2262         CMD_FLAGS(sc) |= SNIC_IO_INITIALIZED;
2263         snic->remove_wait = &wait;
2264         spin_unlock_irqrestore(io_lock, flags);
2265 
2266         /* Initialize Request */
2267         snic_io_hdr_enc(&req->hdr, SNIC_REQ_HBA_RESET, 0, snic_cmd_tag(sc),
2268                         snic->config.hid, 0, (ulong) rqi);
2269 
2270         req->u.reset.flags = 0;
2271 
2272         ret = snic_queue_wq_desc(snic, req, sizeof(*req));
2273         if (ret) {
2274                 SNIC_HOST_ERR(snic->shost,
2275                               "issu_hr:Queuing HBA Reset Failed. w err %d\n",
2276                               ret);
2277 
2278                 goto hba_rst_err;
2279         }
2280 
2281         spin_lock_irqsave(io_lock, flags);
2282         CMD_FLAGS(sc) |= SNIC_HOST_RESET_ISSUED;
2283         spin_unlock_irqrestore(io_lock, flags);
2284         atomic64_inc(&snic->s_stats.reset.hba_resets);
2285         SNIC_HOST_INFO(snic->shost, "Queued HBA Reset Successfully.\n");
2286 
2287         wait_for_completion_timeout(snic->remove_wait,
2288                                     SNIC_HOST_RESET_TIMEOUT);
2289 
2290         if (snic_get_state(snic) == SNIC_FWRESET) {
2291                 SNIC_HOST_ERR(snic->shost, "reset_cmpl: Reset Timedout.\n");
2292                 ret = -ETIMEDOUT;
2293 
2294                 goto hba_rst_err;
2295         }
2296 
2297         spin_lock_irqsave(io_lock, flags);
2298         snic->remove_wait = NULL;
2299         rqi = (struct snic_req_info *) CMD_SP(sc);
2300         CMD_SP(sc) = NULL;
2301         spin_unlock_irqrestore(io_lock, flags);
2302 
2303         if (rqi)
2304                 snic_req_free(snic, rqi);
2305 
2306         ret = 0;
2307 
2308         return ret;
2309 
2310 hba_rst_err:
2311         spin_lock_irqsave(io_lock, flags);
2312         snic->remove_wait = NULL;
2313         rqi = (struct snic_req_info *) CMD_SP(sc);
2314         CMD_SP(sc) = NULL;
2315         spin_unlock_irqrestore(io_lock, flags);
2316 
2317         if (rqi)
2318                 snic_req_free(snic, rqi);
2319 
2320 hba_rst_end:
2321         SNIC_HOST_ERR(snic->shost,
2322                       "reset:HBA Reset Failed w/ err = %d.\n",
2323                       ret);
2324 
2325         return ret;
2326 } /* end of snic_issue_hba_reset */
2327 
2328 int
2329 snic_reset(struct Scsi_Host *shost, struct scsi_cmnd *sc)
2330 {
2331         struct snic *snic = shost_priv(shost);
2332         enum snic_state sv_state;
2333         unsigned long flags;
2334         int ret = FAILED;
2335 
2336         /* Set snic state as SNIC_FWRESET*/
2337         sv_state = snic_get_state(snic);
2338 
2339         spin_lock_irqsave(&snic->snic_lock, flags);
2340         if (snic_get_state(snic) == SNIC_FWRESET) {
2341                 spin_unlock_irqrestore(&snic->snic_lock, flags);
2342                 SNIC_HOST_INFO(shost, "reset:prev reset is in progres\n");
2343 
2344                 msleep(SNIC_HOST_RESET_TIMEOUT);
2345                 ret = SUCCESS;
2346 
2347                 goto reset_end;
2348         }
2349 
2350         snic_set_state(snic, SNIC_FWRESET);
2351         spin_unlock_irqrestore(&snic->snic_lock, flags);
2352 
2353 
2354         /* Wait for all the IOs that are entered in Qcmd */
2355         while (atomic_read(&snic->ios_inflight))
2356                 schedule_timeout(msecs_to_jiffies(1));
2357 
2358         ret = snic_issue_hba_reset(snic, sc);
2359         if (ret) {
2360                 SNIC_HOST_ERR(shost,
2361                               "reset:Host Reset Failed w/ err %d.\n",
2362                               ret);
2363                 spin_lock_irqsave(&snic->snic_lock, flags);
2364                 snic_set_state(snic, sv_state);
2365                 spin_unlock_irqrestore(&snic->snic_lock, flags);
2366                 atomic64_inc(&snic->s_stats.reset.hba_reset_fail);
2367                 ret = FAILED;
2368 
2369                 goto reset_end;
2370         }
2371 
2372         ret = SUCCESS;
2373 
2374 reset_end:
2375         return ret;
2376 } /* end of snic_reset */
2377 
2378 /*
2379  * SCSI Error handling calls driver's eh_host_reset if all prior
2380  * error handling levels return FAILED.
2381  *
2382  * Host Reset is the highest level of error recovery. If this fails, then
2383  * host is offlined by SCSI.
2384  */
2385 int
2386 snic_host_reset(struct scsi_cmnd *sc)
2387 {
2388         struct Scsi_Host *shost = sc->device->host;
2389         u32 start_time  = jiffies;
2390         int ret = FAILED;
2391 
2392         SNIC_SCSI_DBG(shost,
2393                       "host reset:sc %p sc_cmd 0x%x req %p tag %d flags 0x%llx\n",
2394                       sc, sc->cmnd[0], sc->request,
2395                       snic_cmd_tag(sc), CMD_FLAGS(sc));
2396 
2397         ret = snic_reset(shost, sc);
2398 
2399         SNIC_TRC(shost->host_no, snic_cmd_tag(sc), (ulong) sc,
2400                  jiffies_to_msecs(jiffies - start_time),
2401                  0, SNIC_TRC_CMD(sc), SNIC_TRC_CMD_STATE_FLAGS(sc));
2402 
2403         return ret;
2404 } /* end of snic_host_reset */
2405 
2406 /*
2407  * snic_cmpl_pending_tmreq : Caller should hold io_lock
2408  */
2409 static void
2410 snic_cmpl_pending_tmreq(struct snic *snic, struct scsi_cmnd *sc)
2411 {
2412         struct snic_req_info *rqi = NULL;
2413 
2414         SNIC_SCSI_DBG(snic->shost,
2415                       "Completing Pending TM Req sc %p, state %s flags 0x%llx\n",
2416                       sc, snic_io_status_to_str(CMD_STATE(sc)), CMD_FLAGS(sc));
2417 
2418         /*
2419          * CASE : FW didn't post itmf completion due to PCIe Errors.
2420          * Marking the abort status as Success to call scsi completion
2421          * in snic_abort_finish()
2422          */
2423         CMD_ABTS_STATUS(sc) = SNIC_STAT_IO_SUCCESS;
2424 
2425         rqi = (struct snic_req_info *) CMD_SP(sc);
2426         if (!rqi)
2427                 return;
2428 
2429         if (rqi->dr_done)
2430                 complete(rqi->dr_done);
2431         else if (rqi->abts_done)
2432                 complete(rqi->abts_done);
2433 }
2434 
2435 /*
2436  * snic_scsi_cleanup: Walks through tag map and releases the reqs
2437  */
2438 static void
2439 snic_scsi_cleanup(struct snic *snic, int ex_tag)
2440 {
2441         struct snic_req_info *rqi = NULL;
2442         struct scsi_cmnd *sc = NULL;
2443         spinlock_t *io_lock = NULL;
2444         unsigned long flags;
2445         int tag;
2446         u64 st_time = 0;
2447 
2448         SNIC_SCSI_DBG(snic->shost, "sc_clean: scsi cleanup.\n");
2449 
2450         for (tag = 0; tag < snic->max_tag_id; tag++) {
2451                 /* Skip ex_tag */
2452                 if (tag == ex_tag)
2453                         continue;
2454 
2455                 io_lock = snic_io_lock_tag(snic, tag);
2456                 spin_lock_irqsave(io_lock, flags);
2457                 sc = scsi_host_find_tag(snic->shost, tag);
2458                 if (!sc) {
2459                         spin_unlock_irqrestore(io_lock, flags);
2460 
2461                         continue;
2462                 }
2463 
2464                 if (unlikely(snic_tmreq_pending(sc))) {
2465                         /*
2466                          * When FW Completes reset w/o sending completions
2467                          * for outstanding ios.
2468                          */
2469                         snic_cmpl_pending_tmreq(snic, sc);
2470                         spin_unlock_irqrestore(io_lock, flags);
2471 
2472                         continue;
2473                 }
2474 
2475                 rqi = (struct snic_req_info *) CMD_SP(sc);
2476                 if (!rqi) {
2477                         spin_unlock_irqrestore(io_lock, flags);
2478 
2479                         goto cleanup;
2480                 }
2481 
2482                 SNIC_SCSI_DBG(snic->shost,
2483                               "sc_clean: sc %p, rqi %p, tag %d flags 0x%llx\n",
2484                               sc, rqi, tag, CMD_FLAGS(sc));
2485 
2486                 CMD_SP(sc) = NULL;
2487                 CMD_FLAGS(sc) |= SNIC_SCSI_CLEANUP;
2488                 spin_unlock_irqrestore(io_lock, flags);
2489                 st_time = rqi->start_time;
2490 
2491                 SNIC_HOST_INFO(snic->shost,
2492                                "sc_clean: Releasing rqi %p : flags 0x%llx\n",
2493                                rqi, CMD_FLAGS(sc));
2494 
2495                 snic_release_req_buf(snic, rqi, sc);
2496 
2497 cleanup:
2498                 sc->result = DID_TRANSPORT_DISRUPTED << 16;
2499                 SNIC_HOST_INFO(snic->shost,
2500                                "sc_clean: DID_TRANSPORT_DISRUPTED for sc %p, Tag %d flags 0x%llx rqi %p duration %u msecs\n",
2501                                sc, sc->request->tag, CMD_FLAGS(sc), rqi,
2502                                jiffies_to_msecs(jiffies - st_time));
2503 
2504                 /* Update IO stats */
2505                 snic_stats_update_io_cmpl(&snic->s_stats);
2506 
2507                 if (sc->scsi_done) {
2508                         SNIC_TRC(snic->shost->host_no, tag, (ulong) sc,
2509                                  jiffies_to_msecs(jiffies - st_time), 0,
2510                                  SNIC_TRC_CMD(sc),
2511                                  SNIC_TRC_CMD_STATE_FLAGS(sc));
2512 
2513                         sc->scsi_done(sc);
2514                 }
2515         }
2516 } /* end of snic_scsi_cleanup */
2517 
2518 void
2519 snic_shutdown_scsi_cleanup(struct snic *snic)
2520 {
2521         SNIC_HOST_INFO(snic->shost, "Shutdown time SCSI Cleanup.\n");
2522 
2523         snic_scsi_cleanup(snic, SCSI_NO_TAG);
2524 } /* end of snic_shutdown_scsi_cleanup */
2525 
2526 /*
2527  * snic_internal_abort_io
2528  * called by : snic_tgt_scsi_abort_io
2529  */
2530 static int
2531 snic_internal_abort_io(struct snic *snic, struct scsi_cmnd *sc, int tmf)
2532 {
2533         struct snic_req_info *rqi = NULL;
2534         spinlock_t *io_lock = NULL;
2535         unsigned long flags;
2536         u32 sv_state = 0;
2537         int ret = 0;
2538 
2539         io_lock = snic_io_lock_hash(snic, sc);
2540         spin_lock_irqsave(io_lock, flags);
2541         rqi = (struct snic_req_info *) CMD_SP(sc);
2542         if (!rqi)
2543                 goto skip_internal_abts;
2544 
2545         if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2546                 goto skip_internal_abts;
2547 
2548         if ((CMD_FLAGS(sc) & SNIC_DEVICE_RESET) &&
2549                 (!(CMD_FLAGS(sc) & SNIC_DEV_RST_ISSUED))) {
2550 
2551                 SNIC_SCSI_DBG(snic->shost,
2552                               "internal_abts: dev rst not pending sc 0x%p\n",
2553                               sc);
2554 
2555                 goto skip_internal_abts;
2556         }
2557 
2558 
2559         if (!(CMD_FLAGS(sc) & SNIC_IO_ISSUED)) {
2560                 SNIC_SCSI_DBG(snic->shost,
2561                         "internal_abts: IO not yet issued sc 0x%p tag 0x%x flags 0x%llx state %d\n",
2562                         sc, snic_cmd_tag(sc), CMD_FLAGS(sc), CMD_STATE(sc));
2563 
2564                 goto skip_internal_abts;
2565         }
2566 
2567         sv_state = CMD_STATE(sc);
2568         CMD_STATE(sc) = SNIC_IOREQ_ABTS_PENDING;
2569         CMD_ABTS_STATUS(sc) = SNIC_INVALID_CODE;
2570         CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_PENDING;
2571 
2572         if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET) {
2573                 /* stats */
2574                 rqi->tm_tag = SNIC_TAG_DEV_RST;
2575                 SNIC_SCSI_DBG(snic->shost, "internal_abts:dev rst sc %p\n", sc);
2576         }
2577 
2578         SNIC_SCSI_DBG(snic->shost, "internal_abts: Issuing abts tag %x\n",
2579                       snic_cmd_tag(sc));
2580         SNIC_BUG_ON(rqi->abts_done);
2581         spin_unlock_irqrestore(io_lock, flags);
2582 
2583         ret = snic_queue_abort_req(snic, rqi, sc, tmf);
2584         if (ret) {
2585                 SNIC_HOST_ERR(snic->shost,
2586                               "internal_abts: Tag = %x , Failed w/ err = %d\n",
2587                               snic_cmd_tag(sc), ret);
2588 
2589                 spin_lock_irqsave(io_lock, flags);
2590 
2591                 if (CMD_STATE(sc) == SNIC_IOREQ_ABTS_PENDING)
2592                         CMD_STATE(sc) = sv_state;
2593 
2594                 goto skip_internal_abts;
2595         }
2596 
2597         spin_lock_irqsave(io_lock, flags);
2598         if (CMD_FLAGS(sc) & SNIC_DEVICE_RESET)
2599                 CMD_FLAGS(sc) |= SNIC_DEV_RST_TERM_ISSUED;
2600         else
2601                 CMD_FLAGS(sc) |= SNIC_IO_INTERNAL_TERM_ISSUED;
2602 
2603         ret = SUCCESS;
2604 
2605 skip_internal_abts:
2606         lockdep_assert_held(io_lock);
2607         spin_unlock_irqrestore(io_lock, flags);
2608 
2609         return ret;
2610 } /* end of snic_internal_abort_io */
2611 
2612 /*
2613  * snic_tgt_scsi_abort_io : called by snic_tgt_del
2614  */
2615 int
2616 snic_tgt_scsi_abort_io(struct snic_tgt *tgt)
2617 {
2618         struct snic *snic = NULL;
2619         struct scsi_cmnd *sc = NULL;
2620         struct snic_tgt *sc_tgt = NULL;
2621         spinlock_t *io_lock = NULL;
2622         unsigned long flags;
2623         int ret = 0, tag, abt_cnt = 0, tmf = 0;
2624 
2625         if (!tgt)
2626                 return -1;
2627 
2628         snic = shost_priv(snic_tgt_to_shost(tgt));
2629         SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: Cleaning Pending IOs.\n");
2630 
2631         if (tgt->tdata.typ == SNIC_TGT_DAS)
2632                 tmf = SNIC_ITMF_ABTS_TASK;
2633         else
2634                 tmf = SNIC_ITMF_ABTS_TASK_TERM;
2635 
2636         for (tag = 0; tag < snic->max_tag_id; tag++) {
2637                 io_lock = snic_io_lock_tag(snic, tag);
2638 
2639                 spin_lock_irqsave(io_lock, flags);
2640                 sc = scsi_host_find_tag(snic->shost, tag);
2641                 if (!sc) {
2642                         spin_unlock_irqrestore(io_lock, flags);
2643 
2644                         continue;
2645                 }
2646 
2647                 sc_tgt = starget_to_tgt(scsi_target(sc->device));
2648                 if (sc_tgt != tgt) {
2649                         spin_unlock_irqrestore(io_lock, flags);
2650 
2651                         continue;
2652                 }
2653                 spin_unlock_irqrestore(io_lock, flags);
2654 
2655                 ret = snic_internal_abort_io(snic, sc, tmf);
2656                 if (ret < 0) {
2657                         SNIC_HOST_ERR(snic->shost,
2658                                       "tgt_abt_io: Tag %x, Failed w err = %d\n",
2659                                       tag, ret);
2660 
2661                         continue;
2662                 }
2663 
2664                 if (ret == SUCCESS)
2665                         abt_cnt++;
2666         }
2667 
2668         SNIC_SCSI_DBG(snic->shost, "tgt_abt_io: abt_cnt = %d\n", abt_cnt);
2669 
2670         return 0;
2671 } /* end of snic_tgt_scsi_abort_io */

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