linux/drivers/scsi/qla2xxx/qla_target.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 *  qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
   4 *
   5 *  based on qla2x00t.c code:
   6 *
   7 *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
   8 *  Copyright (C) 2004 - 2005 Leonid Stoljar
   9 *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
  10 *  Copyright (C) 2006 - 2010 ID7 Ltd.
  11 *
  12 *  Forward port and refactoring to modern qla2xxx and target/configfs
  13 *
  14 *  Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
  15 */
  16
  17#include <linux/module.h>
  18#include <linux/init.h>
  19#include <linux/types.h>
  20#include <linux/blkdev.h>
  21#include <linux/interrupt.h>
  22#include <linux/pci.h>
  23#include <linux/delay.h>
  24#include <linux/list.h>
  25#include <linux/workqueue.h>
  26#include <asm/unaligned.h>
  27#include <scsi/scsi.h>
  28#include <scsi/scsi_host.h>
  29#include <scsi/scsi_tcq.h>
  30
  31#include "qla_def.h"
  32#include "qla_target.h"
  33
  34static int ql2xtgt_tape_enable;
  35module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
  36MODULE_PARM_DESC(ql2xtgt_tape_enable,
  37                "Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");
  38
  39static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
  40module_param(qlini_mode, charp, S_IRUGO);
  41MODULE_PARM_DESC(qlini_mode,
  42        "Determines when initiator mode will be enabled. Possible values: "
  43        "\"exclusive\" - initiator mode will be enabled on load, "
  44        "disabled on enabling target mode and then on disabling target mode "
  45        "enabled back; "
  46        "\"disabled\" - initiator mode will never be enabled; "
  47        "\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
  48        "when ready "
  49        "\"enabled\" (default) - initiator mode will always stay enabled.");
  50
  51static int ql_dm_tgt_ex_pct = 0;
  52module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
  53MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
  54        "For Dual Mode (qlini_mode=dual), this parameter determines "
  55        "the percentage of exchanges/cmds FW will allocate resources "
  56        "for Target mode.");
  57
  58int ql2xuctrlirq = 1;
  59module_param(ql2xuctrlirq, int, 0644);
  60MODULE_PARM_DESC(ql2xuctrlirq,
  61    "User to control IRQ placement via smp_affinity."
  62    "Valid with qlini_mode=disabled."
  63    "1(default): enable");
  64
  65int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
  66
  67static int qla_sam_status = SAM_STAT_BUSY;
  68static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
  69
  70/*
  71 * From scsi/fc/fc_fcp.h
  72 */
  73enum fcp_resp_rsp_codes {
  74        FCP_TMF_CMPL = 0,
  75        FCP_DATA_LEN_INVALID = 1,
  76        FCP_CMND_FIELDS_INVALID = 2,
  77        FCP_DATA_PARAM_MISMATCH = 3,
  78        FCP_TMF_REJECTED = 4,
  79        FCP_TMF_FAILED = 5,
  80        FCP_TMF_INVALID_LUN = 9,
  81};
  82
  83/*
  84 * fc_pri_ta from scsi/fc/fc_fcp.h
  85 */
  86#define FCP_PTA_SIMPLE      0   /* simple task attribute */
  87#define FCP_PTA_HEADQ       1   /* head of queue task attribute */
  88#define FCP_PTA_ORDERED     2   /* ordered task attribute */
  89#define FCP_PTA_ACA         4   /* auto. contingent allegiance */
  90#define FCP_PTA_MASK        7   /* mask for task attribute field */
  91#define FCP_PRI_SHIFT       3   /* priority field starts in bit 3 */
  92#define FCP_PRI_RESVD_MASK  0x80        /* reserved bits in priority field */
  93
  94/*
  95 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
  96 * must be called under HW lock and could unlock/lock it inside.
  97 * It isn't an issue, since in the current implementation on the time when
  98 * those functions are called:
  99 *
 100 *   - Either context is IRQ and only IRQ handler can modify HW data,
 101 *     including rings related fields,
 102 *
 103 *   - Or access to target mode variables from struct qla_tgt doesn't
 104 *     cross those functions boundaries, except tgt_stop, which
 105 *     additionally protected by irq_cmd_count.
 106 */
 107/* Predefs for callbacks handed to qla2xxx LLD */
 108static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
 109        struct atio_from_isp *pkt, uint8_t);
 110static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
 111        response_t *pkt);
 112static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
 113        int fn, void *iocb, int flags);
 114static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
 115        *cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
 116static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
 117        struct atio_from_isp *atio, uint16_t status, int qfull);
 118static void qlt_disable_vha(struct scsi_qla_host *vha);
 119static void qlt_clear_tgt_db(struct qla_tgt *tgt);
 120static void qlt_send_notify_ack(struct qla_qpair *qpair,
 121        struct imm_ntfy_from_isp *ntfy,
 122        uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
 123        uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
 124static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
 125        struct imm_ntfy_from_isp *imm, int ha_locked);
 126static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
 127        fc_port_t *fcport, bool local);
 128void qlt_unreg_sess(struct fc_port *sess);
 129static void qlt_24xx_handle_abts(struct scsi_qla_host *,
 130        struct abts_recv_from_24xx *);
 131static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
 132    uint16_t);
 133static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
 134static inline uint32_t qlt_make_handle(struct qla_qpair *);
 135
 136/*
 137 * Global Variables
 138 */
 139static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
 140struct kmem_cache *qla_tgt_plogi_cachep;
 141static mempool_t *qla_tgt_mgmt_cmd_mempool;
 142static struct workqueue_struct *qla_tgt_wq;
 143static DEFINE_MUTEX(qla_tgt_mutex);
 144static LIST_HEAD(qla_tgt_glist);
 145
 146static const char *prot_op_str(u32 prot_op)
 147{
 148        switch (prot_op) {
 149        case TARGET_PROT_NORMAL:        return "NORMAL";
 150        case TARGET_PROT_DIN_INSERT:    return "DIN_INSERT";
 151        case TARGET_PROT_DOUT_INSERT:   return "DOUT_INSERT";
 152        case TARGET_PROT_DIN_STRIP:     return "DIN_STRIP";
 153        case TARGET_PROT_DOUT_STRIP:    return "DOUT_STRIP";
 154        case TARGET_PROT_DIN_PASS:      return "DIN_PASS";
 155        case TARGET_PROT_DOUT_PASS:     return "DOUT_PASS";
 156        default:                        return "UNKNOWN";
 157        }
 158}
 159
 160/* This API intentionally takes dest as a parameter, rather than returning
 161 * int value to avoid caller forgetting to issue wmb() after the store */
 162void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
 163{
 164        scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
 165        *dest = atomic_inc_return(&base_vha->generation_tick);
 166        /* memory barrier */
 167        wmb();
 168}
 169
 170/* Might release hw lock, then reaquire!! */
 171static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
 172{
 173        /* Send marker if required */
 174        if (unlikely(vha->marker_needed != 0)) {
 175                int rc = qla2x00_issue_marker(vha, vha_locked);
 176
 177                if (rc != QLA_SUCCESS) {
 178                        ql_dbg(ql_dbg_tgt, vha, 0xe03d,
 179                            "qla_target(%d): issue_marker() failed\n",
 180                            vha->vp_idx);
 181                }
 182                return rc;
 183        }
 184        return QLA_SUCCESS;
 185}
 186
 187static inline
 188struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
 189                                            be_id_t d_id)
 190{
 191        struct scsi_qla_host *host;
 192        uint32_t key;
 193
 194        if (vha->d_id.b.area == d_id.area &&
 195            vha->d_id.b.domain == d_id.domain &&
 196            vha->d_id.b.al_pa == d_id.al_pa)
 197                return vha;
 198
 199        key = be_to_port_id(d_id).b24;
 200
 201        host = btree_lookup32(&vha->hw->tgt.host_map, key);
 202        if (!host)
 203                ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
 204                    "Unable to find host %06x\n", key);
 205
 206        return host;
 207}
 208
 209static inline
 210struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
 211        uint16_t vp_idx)
 212{
 213        struct qla_hw_data *ha = vha->hw;
 214
 215        if (vha->vp_idx == vp_idx)
 216                return vha;
 217
 218        BUG_ON(ha->tgt.tgt_vp_map == NULL);
 219        if (likely(test_bit(vp_idx, ha->vp_idx_map)))
 220                return ha->tgt.tgt_vp_map[vp_idx].vha;
 221
 222        return NULL;
 223}
 224
 225static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
 226{
 227        unsigned long flags;
 228
 229        spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
 230
 231        vha->hw->tgt.num_pend_cmds++;
 232        if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
 233                vha->qla_stats.stat_max_pend_cmds =
 234                        vha->hw->tgt.num_pend_cmds;
 235        spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
 236}
 237static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
 238{
 239        unsigned long flags;
 240
 241        spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
 242        vha->hw->tgt.num_pend_cmds--;
 243        spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
 244}
 245
 246
 247static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
 248        struct atio_from_isp *atio, uint8_t ha_locked)
 249{
 250        struct qla_tgt_sess_op *u;
 251        struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
 252        unsigned long flags;
 253
 254        if (tgt->tgt_stop) {
 255                ql_dbg(ql_dbg_async, vha, 0x502c,
 256                    "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
 257                    vha->vp_idx);
 258                goto out_term;
 259        }
 260
 261        u = kzalloc(sizeof(*u), GFP_ATOMIC);
 262        if (u == NULL)
 263                goto out_term;
 264
 265        u->vha = vha;
 266        memcpy(&u->atio, atio, sizeof(*atio));
 267        INIT_LIST_HEAD(&u->cmd_list);
 268
 269        spin_lock_irqsave(&vha->cmd_list_lock, flags);
 270        list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
 271        spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
 272
 273        schedule_delayed_work(&vha->unknown_atio_work, 1);
 274
 275out:
 276        return;
 277
 278out_term:
 279        qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
 280        goto out;
 281}
 282
 283static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
 284        uint8_t ha_locked)
 285{
 286        struct qla_tgt_sess_op *u, *t;
 287        scsi_qla_host_t *host;
 288        struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
 289        unsigned long flags;
 290        uint8_t queued = 0;
 291
 292        list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
 293                if (u->aborted) {
 294                        ql_dbg(ql_dbg_async, vha, 0x502e,
 295                            "Freeing unknown %s %p, because of Abort\n",
 296                            "ATIO_TYPE7", u);
 297                        qlt_send_term_exchange(vha->hw->base_qpair, NULL,
 298                            &u->atio, ha_locked, 0);
 299                        goto abort;
 300                }
 301
 302                host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
 303                if (host != NULL) {
 304                        ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
 305                            "Requeuing unknown ATIO_TYPE7 %p\n", u);
 306                        qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
 307                } else if (tgt->tgt_stop) {
 308                        ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
 309                            "Freeing unknown %s %p, because tgt is being stopped\n",
 310                            "ATIO_TYPE7", u);
 311                        qlt_send_term_exchange(vha->hw->base_qpair, NULL,
 312                            &u->atio, ha_locked, 0);
 313                } else {
 314                        ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
 315                            "Reschedule u %p, vha %p, host %p\n", u, vha, host);
 316                        if (!queued) {
 317                                queued = 1;
 318                                schedule_delayed_work(&vha->unknown_atio_work,
 319                                    1);
 320                        }
 321                        continue;
 322                }
 323
 324abort:
 325                spin_lock_irqsave(&vha->cmd_list_lock, flags);
 326                list_del(&u->cmd_list);
 327                spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
 328                kfree(u);
 329        }
 330}
 331
 332void qlt_unknown_atio_work_fn(struct work_struct *work)
 333{
 334        struct scsi_qla_host *vha = container_of(to_delayed_work(work),
 335            struct scsi_qla_host, unknown_atio_work);
 336
 337        qlt_try_to_dequeue_unknown_atios(vha, 0);
 338}
 339
 340static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
 341        struct atio_from_isp *atio, uint8_t ha_locked)
 342{
 343        ql_dbg(ql_dbg_tgt, vha, 0xe072,
 344                "%s: qla_target(%d): type %x ox_id %04x\n",
 345                __func__, vha->vp_idx, atio->u.raw.entry_type,
 346                be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
 347
 348        switch (atio->u.raw.entry_type) {
 349        case ATIO_TYPE7:
 350        {
 351                struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
 352                    atio->u.isp24.fcp_hdr.d_id);
 353                if (unlikely(NULL == host)) {
 354                        ql_dbg(ql_dbg_tgt, vha, 0xe03e,
 355                            "qla_target(%d): Received ATIO_TYPE7 "
 356                            "with unknown d_id %x:%x:%x\n", vha->vp_idx,
 357                            atio->u.isp24.fcp_hdr.d_id.domain,
 358                            atio->u.isp24.fcp_hdr.d_id.area,
 359                            atio->u.isp24.fcp_hdr.d_id.al_pa);
 360
 361
 362                        qlt_queue_unknown_atio(vha, atio, ha_locked);
 363                        break;
 364                }
 365                if (unlikely(!list_empty(&vha->unknown_atio_list)))
 366                        qlt_try_to_dequeue_unknown_atios(vha, ha_locked);
 367
 368                qlt_24xx_atio_pkt(host, atio, ha_locked);
 369                break;
 370        }
 371
 372        case IMMED_NOTIFY_TYPE:
 373        {
 374                struct scsi_qla_host *host = vha;
 375                struct imm_ntfy_from_isp *entry =
 376                    (struct imm_ntfy_from_isp *)atio;
 377
 378                qlt_issue_marker(vha, ha_locked);
 379
 380                if ((entry->u.isp24.vp_index != 0xFF) &&
 381                    (entry->u.isp24.nport_handle != cpu_to_le16(0xFFFF))) {
 382                        host = qlt_find_host_by_vp_idx(vha,
 383                            entry->u.isp24.vp_index);
 384                        if (unlikely(!host)) {
 385                                ql_dbg(ql_dbg_tgt, vha, 0xe03f,
 386                                    "qla_target(%d): Received "
 387                                    "ATIO (IMMED_NOTIFY_TYPE) "
 388                                    "with unknown vp_index %d\n",
 389                                    vha->vp_idx, entry->u.isp24.vp_index);
 390                                break;
 391                        }
 392                }
 393                qlt_24xx_atio_pkt(host, atio, ha_locked);
 394                break;
 395        }
 396
 397        case VP_RPT_ID_IOCB_TYPE:
 398                qla24xx_report_id_acquisition(vha,
 399                        (struct vp_rpt_id_entry_24xx *)atio);
 400                break;
 401
 402        case ABTS_RECV_24XX:
 403        {
 404                struct abts_recv_from_24xx *entry =
 405                        (struct abts_recv_from_24xx *)atio;
 406                struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
 407                        entry->vp_index);
 408                unsigned long flags;
 409
 410                if (unlikely(!host)) {
 411                        ql_dbg(ql_dbg_tgt, vha, 0xe00a,
 412                            "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
 413                            "received, with unknown vp_index %d\n",
 414                            vha->vp_idx, entry->vp_index);
 415                        break;
 416                }
 417                if (!ha_locked)
 418                        spin_lock_irqsave(&host->hw->hardware_lock, flags);
 419                qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
 420                if (!ha_locked)
 421                        spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
 422                break;
 423        }
 424
 425        /* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */
 426
 427        default:
 428                ql_dbg(ql_dbg_tgt, vha, 0xe040,
 429                    "qla_target(%d): Received unknown ATIO atio "
 430                    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
 431                break;
 432        }
 433
 434        return false;
 435}
 436
 437void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
 438        struct rsp_que *rsp, response_t *pkt)
 439{
 440        switch (pkt->entry_type) {
 441        case CTIO_CRC2:
 442                ql_dbg(ql_dbg_tgt, vha, 0xe073,
 443                        "qla_target(%d):%s: CRC2 Response pkt\n",
 444                        vha->vp_idx, __func__);
 445                fallthrough;
 446        case CTIO_TYPE7:
 447        {
 448                struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
 449                struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
 450                    entry->vp_index);
 451                if (unlikely(!host)) {
 452                        ql_dbg(ql_dbg_tgt, vha, 0xe041,
 453                            "qla_target(%d): Response pkt (CTIO_TYPE7) "
 454                            "received, with unknown vp_index %d\n",
 455                            vha->vp_idx, entry->vp_index);
 456                        break;
 457                }
 458                qlt_response_pkt(host, rsp, pkt);
 459                break;
 460        }
 461
 462        case IMMED_NOTIFY_TYPE:
 463        {
 464                struct scsi_qla_host *host;
 465                struct imm_ntfy_from_isp *entry =
 466                    (struct imm_ntfy_from_isp *)pkt;
 467
 468                host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
 469                if (unlikely(!host)) {
 470                        ql_dbg(ql_dbg_tgt, vha, 0xe042,
 471                            "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
 472                            "received, with unknown vp_index %d\n",
 473                            vha->vp_idx, entry->u.isp24.vp_index);
 474                        break;
 475                }
 476                qlt_response_pkt(host, rsp, pkt);
 477                break;
 478        }
 479
 480        case NOTIFY_ACK_TYPE:
 481        {
 482                struct scsi_qla_host *host = vha;
 483                struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
 484
 485                if (0xFF != entry->u.isp24.vp_index) {
 486                        host = qlt_find_host_by_vp_idx(vha,
 487                            entry->u.isp24.vp_index);
 488                        if (unlikely(!host)) {
 489                                ql_dbg(ql_dbg_tgt, vha, 0xe043,
 490                                    "qla_target(%d): Response "
 491                                    "pkt (NOTIFY_ACK_TYPE) "
 492                                    "received, with unknown "
 493                                    "vp_index %d\n", vha->vp_idx,
 494                                    entry->u.isp24.vp_index);
 495                                break;
 496                        }
 497                }
 498                qlt_response_pkt(host, rsp, pkt);
 499                break;
 500        }
 501
 502        case ABTS_RECV_24XX:
 503        {
 504                struct abts_recv_from_24xx *entry =
 505                    (struct abts_recv_from_24xx *)pkt;
 506                struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
 507                    entry->vp_index);
 508                if (unlikely(!host)) {
 509                        ql_dbg(ql_dbg_tgt, vha, 0xe044,
 510                            "qla_target(%d): Response pkt "
 511                            "(ABTS_RECV_24XX) received, with unknown "
 512                            "vp_index %d\n", vha->vp_idx, entry->vp_index);
 513                        break;
 514                }
 515                qlt_response_pkt(host, rsp, pkt);
 516                break;
 517        }
 518
 519        case ABTS_RESP_24XX:
 520        {
 521                struct abts_resp_to_24xx *entry =
 522                    (struct abts_resp_to_24xx *)pkt;
 523                struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
 524                    entry->vp_index);
 525                if (unlikely(!host)) {
 526                        ql_dbg(ql_dbg_tgt, vha, 0xe045,
 527                            "qla_target(%d): Response pkt "
 528                            "(ABTS_RECV_24XX) received, with unknown "
 529                            "vp_index %d\n", vha->vp_idx, entry->vp_index);
 530                        break;
 531                }
 532                qlt_response_pkt(host, rsp, pkt);
 533                break;
 534        }
 535        default:
 536                qlt_response_pkt(vha, rsp, pkt);
 537                break;
 538        }
 539
 540}
 541
 542/*
 543 * All qlt_plogi_ack_t operations are protected by hardware_lock
 544 */
 545static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
 546        struct imm_ntfy_from_isp *ntfy, int type)
 547{
 548        struct qla_work_evt *e;
 549
 550        e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
 551        if (!e)
 552                return QLA_FUNCTION_FAILED;
 553
 554        e->u.nack.fcport = fcport;
 555        e->u.nack.type = type;
 556        memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
 557        return qla2x00_post_work(vha, e);
 558}
 559
 560static void qla2x00_async_nack_sp_done(srb_t *sp, int res)
 561{
 562        struct scsi_qla_host *vha = sp->vha;
 563        unsigned long flags;
 564
 565        ql_dbg(ql_dbg_disc, vha, 0x20f2,
 566            "Async done-%s res %x %8phC  type %d\n",
 567            sp->name, res, sp->fcport->port_name, sp->type);
 568
 569        spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
 570        sp->fcport->flags &= ~FCF_ASYNC_SENT;
 571        sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
 572
 573        switch (sp->type) {
 574        case SRB_NACK_PLOGI:
 575                sp->fcport->login_gen++;
 576                sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
 577                sp->fcport->logout_on_delete = 1;
 578                sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
 579                sp->fcport->send_els_logo = 0;
 580                break;
 581
 582        case SRB_NACK_PRLI:
 583                sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
 584                sp->fcport->deleted = 0;
 585                sp->fcport->send_els_logo = 0;
 586
 587                if (!sp->fcport->login_succ &&
 588                    !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
 589                        sp->fcport->login_succ = 1;
 590
 591                        vha->fcport_count++;
 592                        spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
 593                        qla24xx_sched_upd_fcport(sp->fcport);
 594                        spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
 595                } else {
 596                        sp->fcport->login_retry = 0;
 597                        qla2x00_set_fcport_disc_state(sp->fcport,
 598                            DSC_LOGIN_COMPLETE);
 599                        sp->fcport->deleted = 0;
 600                        sp->fcport->logout_on_delete = 1;
 601                }
 602                break;
 603
 604        case SRB_NACK_LOGO:
 605                sp->fcport->login_gen++;
 606                sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
 607                qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
 608                break;
 609        }
 610        spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
 611
 612        sp->free(sp);
 613}
 614
 615int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
 616        struct imm_ntfy_from_isp *ntfy, int type)
 617{
 618        int rval = QLA_FUNCTION_FAILED;
 619        srb_t *sp;
 620        char *c = NULL;
 621
 622        fcport->flags |= FCF_ASYNC_SENT;
 623        switch (type) {
 624        case SRB_NACK_PLOGI:
 625                fcport->fw_login_state = DSC_LS_PLOGI_PEND;
 626                c = "PLOGI";
 627                break;
 628        case SRB_NACK_PRLI:
 629                fcport->fw_login_state = DSC_LS_PRLI_PEND;
 630                fcport->deleted = 0;
 631                c = "PRLI";
 632                break;
 633        case SRB_NACK_LOGO:
 634                fcport->fw_login_state = DSC_LS_LOGO_PEND;
 635                c = "LOGO";
 636                break;
 637        }
 638
 639        sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
 640        if (!sp)
 641                goto done;
 642
 643        sp->type = type;
 644        sp->name = "nack";
 645
 646        sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
 647        qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);
 648
 649        sp->u.iocb_cmd.u.nack.ntfy = ntfy;
 650        sp->done = qla2x00_async_nack_sp_done;
 651
 652        ql_dbg(ql_dbg_disc, vha, 0x20f4,
 653            "Async-%s %8phC hndl %x %s\n",
 654            sp->name, fcport->port_name, sp->handle, c);
 655
 656        rval = qla2x00_start_sp(sp);
 657        if (rval != QLA_SUCCESS)
 658                goto done_free_sp;
 659
 660        return rval;
 661
 662done_free_sp:
 663        sp->free(sp);
 664done:
 665        fcport->flags &= ~FCF_ASYNC_SENT;
 666        return rval;
 667}
 668
 669void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
 670{
 671        fc_port_t *t;
 672
 673        switch (e->u.nack.type) {
 674        case SRB_NACK_PRLI:
 675                t = e->u.nack.fcport;
 676                flush_work(&t->del_work);
 677                flush_work(&t->free_work);
 678                mutex_lock(&vha->vha_tgt.tgt_mutex);
 679                t = qlt_create_sess(vha, e->u.nack.fcport, 0);
 680                mutex_unlock(&vha->vha_tgt.tgt_mutex);
 681                if (t) {
 682                        ql_log(ql_log_info, vha, 0xd034,
 683                            "%s create sess success %p", __func__, t);
 684                        /* create sess has an extra kref */
 685                        vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
 686                }
 687                break;
 688        }
 689        qla24xx_async_notify_ack(vha, e->u.nack.fcport,
 690            (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
 691}
 692
 693void qla24xx_delete_sess_fn(struct work_struct *work)
 694{
 695        fc_port_t *fcport = container_of(work, struct fc_port, del_work);
 696        struct qla_hw_data *ha = fcport->vha->hw;
 697
 698        if (fcport->se_sess) {
 699                ha->tgt.tgt_ops->shutdown_sess(fcport);
 700                ha->tgt.tgt_ops->put_sess(fcport);
 701        } else {
 702                qlt_unreg_sess(fcport);
 703        }
 704}
 705
 706/*
 707 * Called from qla2x00_reg_remote_port()
 708 */
 709void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
 710{
 711        struct qla_hw_data *ha = vha->hw;
 712        struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
 713        struct fc_port *sess = fcport;
 714        unsigned long flags;
 715
 716        if (!vha->hw->tgt.tgt_ops)
 717                return;
 718
 719        spin_lock_irqsave(&ha->tgt.sess_lock, flags);
 720        if (tgt->tgt_stop) {
 721                spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
 722                return;
 723        }
 724
 725        if (fcport->disc_state == DSC_DELETE_PEND) {
 726                spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
 727                return;
 728        }
 729
 730        if (!sess->se_sess) {
 731                spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
 732
 733                mutex_lock(&vha->vha_tgt.tgt_mutex);
 734                sess = qlt_create_sess(vha, fcport, false);
 735                mutex_unlock(&vha->vha_tgt.tgt_mutex);
 736
 737                spin_lock_irqsave(&ha->tgt.sess_lock, flags);
 738        } else {
 739                if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
 740                        spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
 741                        return;
 742                }
 743
 744                if (!kref_get_unless_zero(&sess->sess_kref)) {
 745                        ql_dbg(ql_dbg_disc, vha, 0x2107,
 746                            "%s: kref_get fail sess %8phC \n",
 747                            __func__, sess->port_name);
 748                        spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
 749                        return;
 750                }
 751
 752                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
 753                    "qla_target(%u): %ssession for port %8phC "
 754                    "(loop ID %d) reappeared\n", vha->vp_idx,
 755                    sess->local ? "local " : "", sess->port_name, sess->loop_id);
 756
 757                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
 758                    "Reappeared sess %p\n", sess);
 759
 760                ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
 761                    fcport->loop_id,
 762                    (fcport->flags & FCF_CONF_COMP_SUPPORTED));
 763        }
 764
 765        if (sess && sess->local) {
 766                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
 767                    "qla_target(%u): local session for "
 768                    "port %8phC (loop ID %d) became global\n", vha->vp_idx,
 769                    fcport->port_name, sess->loop_id);
 770                sess->local = 0;
 771        }
 772        spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
 773
 774        ha->tgt.tgt_ops->put_sess(sess);
 775}
 776
 777/*
 778 * This is a zero-base ref-counting solution, since hardware_lock
 779 * guarantees that ref_count is not modified concurrently.
 780 * Upon successful return content of iocb is undefined
 781 */
 782static struct qlt_plogi_ack_t *
 783qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
 784                       struct imm_ntfy_from_isp *iocb)
 785{
 786        struct qlt_plogi_ack_t *pla;
 787
 788        lockdep_assert_held(&vha->hw->hardware_lock);
 789
 790        list_for_each_entry(pla, &vha->plogi_ack_list, list) {
 791                if (pla->id.b24 == id->b24) {
 792                        ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
 793                            "%s %d %8phC Term INOT due to new INOT",
 794                            __func__, __LINE__,
 795                            pla->iocb.u.isp24.port_name);
 796                        qlt_send_term_imm_notif(vha, &pla->iocb, 1);
 797                        memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
 798                        return pla;
 799                }
 800        }
 801
 802        pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
 803        if (!pla) {
 804                ql_dbg(ql_dbg_async, vha, 0x5088,
 805                       "qla_target(%d): Allocation of plogi_ack failed\n",
 806                       vha->vp_idx);
 807                return NULL;
 808        }
 809
 810        memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
 811        pla->id = *id;
 812        list_add_tail(&pla->list, &vha->plogi_ack_list);
 813
 814        return pla;
 815}
 816
 817void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
 818    struct qlt_plogi_ack_t *pla)
 819{
 820        struct imm_ntfy_from_isp *iocb = &pla->iocb;
 821        port_id_t port_id;
 822        uint16_t loop_id;
 823        fc_port_t *fcport = pla->fcport;
 824
 825        BUG_ON(!pla->ref_count);
 826        pla->ref_count--;
 827
 828        if (pla->ref_count)
 829                return;
 830
 831        ql_dbg(ql_dbg_disc, vha, 0x5089,
 832            "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
 833            " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
 834            iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
 835            iocb->u.isp24.port_id[0],
 836            le16_to_cpu(iocb->u.isp24.nport_handle),
 837            iocb->u.isp24.exchange_address, iocb->ox_id);
 838
 839        port_id.b.domain = iocb->u.isp24.port_id[2];
 840        port_id.b.area   = iocb->u.isp24.port_id[1];
 841        port_id.b.al_pa  = iocb->u.isp24.port_id[0];
 842        port_id.b.rsvd_1 = 0;
 843
 844        loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);
 845
 846        fcport->loop_id = loop_id;
 847        fcport->d_id = port_id;
 848        if (iocb->u.isp24.status_subcode == ELS_PLOGI)
 849                qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
 850        else
 851                qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
 852
 853        list_for_each_entry(fcport, &vha->vp_fcports, list) {
 854                if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
 855                        fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
 856                if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
 857                        fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
 858        }
 859
 860        list_del(&pla->list);
 861        kmem_cache_free(qla_tgt_plogi_cachep, pla);
 862}
 863
 864void
 865qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
 866    struct fc_port *sess, enum qlt_plogi_link_t link)
 867{
 868        struct imm_ntfy_from_isp *iocb = &pla->iocb;
 869        /* Inc ref_count first because link might already be pointing at pla */
 870        pla->ref_count++;
 871
 872        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
 873                "Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
 874                " s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
 875                sess, link, sess->port_name,
 876                iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
 877                iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
 878                pla->ref_count, pla, link);
 879
 880        if (link == QLT_PLOGI_LINK_CONFLICT) {
 881                switch (sess->disc_state) {
 882                case DSC_DELETED:
 883                case DSC_DELETE_PEND:
 884                        pla->ref_count--;
 885                        return;
 886                default:
 887                        break;
 888                }
 889        }
 890
 891        if (sess->plogi_link[link])
 892                qlt_plogi_ack_unref(vha, sess->plogi_link[link]);
 893
 894        if (link == QLT_PLOGI_LINK_SAME_WWN)
 895                pla->fcport = sess;
 896
 897        sess->plogi_link[link] = pla;
 898}
 899
 900typedef struct {
 901        /* These fields must be initialized by the caller */
 902        port_id_t id;
 903        /*
 904         * number of cmds dropped while we were waiting for
 905         * initiator to ack LOGO initialize to 1 if LOGO is
 906         * triggered by a command, otherwise, to 0
 907         */
 908        int cmd_count;
 909
 910        /* These fields are used by callee */
 911        struct list_head list;
 912} qlt_port_logo_t;
 913
 914static void
 915qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
 916{
 917        qlt_port_logo_t *tmp;
 918        int res;
 919
 920        mutex_lock(&vha->vha_tgt.tgt_mutex);
 921
 922        list_for_each_entry(tmp, &vha->logo_list, list) {
 923                if (tmp->id.b24 == logo->id.b24) {
 924                        tmp->cmd_count += logo->cmd_count;
 925                        mutex_unlock(&vha->vha_tgt.tgt_mutex);
 926                        return;
 927                }
 928        }
 929
 930        list_add_tail(&logo->list, &vha->logo_list);
 931
 932        mutex_unlock(&vha->vha_tgt.tgt_mutex);
 933
 934        res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);
 935
 936        mutex_lock(&vha->vha_tgt.tgt_mutex);
 937        list_del(&logo->list);
 938        mutex_unlock(&vha->vha_tgt.tgt_mutex);
 939
 940        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
 941            "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
 942            logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
 943            logo->cmd_count, res);
 944}
 945
 946void qlt_free_session_done(struct work_struct *work)
 947{
 948        struct fc_port *sess = container_of(work, struct fc_port,
 949            free_work);
 950        struct qla_tgt *tgt = sess->tgt;
 951        struct scsi_qla_host *vha = sess->vha;
 952        struct qla_hw_data *ha = vha->hw;
 953        unsigned long flags;
 954        bool logout_started = false;
 955        scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
 956        struct qlt_plogi_ack_t *own =
 957                sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
 958
 959        ql_dbg(ql_dbg_disc, vha, 0xf084,
 960                "%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
 961                " s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
 962                __func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
 963                sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
 964                sess->logout_on_delete, sess->keep_nport_handle,
 965                sess->send_els_logo);
 966
 967        if (!IS_SW_RESV_ADDR(sess->d_id)) {
 968                qla2x00_mark_device_lost(vha, sess, 0);
 969
 970                if (sess->send_els_logo) {
 971                        qlt_port_logo_t logo;
 972
 973                        logo.id = sess->d_id;
 974                        logo.cmd_count = 0;
 975                        if (!own)
 976                                qlt_send_first_logo(vha, &logo);
 977                        sess->send_els_logo = 0;
 978                }
 979
 980                if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
 981                        int rc;
 982
 983                        if (!own ||
 984                             (own->iocb.u.isp24.status_subcode == ELS_PLOGI)) {
 985                                rc = qla2x00_post_async_logout_work(vha, sess,
 986                                    NULL);
 987                                if (rc != QLA_SUCCESS)
 988                                        ql_log(ql_log_warn, vha, 0xf085,
 989                                            "Schedule logo failed sess %p rc %d\n",
 990                                            sess, rc);
 991                                else
 992                                        logout_started = true;
 993                        } else if (own && (own->iocb.u.isp24.status_subcode ==
 994                                ELS_PRLI) && ha->flags.rida_fmt2) {
 995                                rc = qla2x00_post_async_prlo_work(vha, sess,
 996                                    NULL);
 997                                if (rc != QLA_SUCCESS)
 998                                        ql_log(ql_log_warn, vha, 0xf085,
 999                                            "Schedule PRLO failed sess %p rc %d\n",
1000                                            sess, rc);
1001                                else
1002                                        logout_started = true;
1003                        }
1004                } /* if sess->logout_on_delete */
1005
1006                if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
1007                    !(sess->nvme_flag & NVME_FLAG_DELETING)) {
1008                        sess->nvme_flag |= NVME_FLAG_DELETING;
1009                        qla_nvme_unregister_remote_port(sess);
1010                }
1011        }
1012
1013        /*
1014         * Release the target session for FC Nexus from fabric module code.
1015         */
1016        if (sess->se_sess != NULL)
1017                ha->tgt.tgt_ops->free_session(sess);
1018
1019        if (logout_started) {
1020                bool traced = false;
1021                u16 cnt = 0;
1022
1023                while (!READ_ONCE(sess->logout_completed)) {
1024                        if (!traced) {
1025                                ql_dbg(ql_dbg_disc, vha, 0xf086,
1026                                        "%s: waiting for sess %p logout\n",
1027                                        __func__, sess);
1028                                traced = true;
1029                        }
1030                        msleep(100);
1031                        cnt++;
1032                        /*
1033                         * Driver timeout is set to 22 Sec, update count value to loop
1034                         * long enough for log-out to complete before advancing. Otherwise,
1035                         * straddling logout can interfere with re-login attempt.
1036                         */
1037                        if (cnt > 230)
1038                                break;
1039                }
1040
1041                ql_dbg(ql_dbg_disc, vha, 0xf087,
1042                    "%s: sess %p logout completed\n", __func__, sess);
1043        }
1044
1045        if (sess->logo_ack_needed) {
1046                sess->logo_ack_needed = 0;
1047                qla24xx_async_notify_ack(vha, sess,
1048                        (struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1049        }
1050
1051        spin_lock_irqsave(&vha->work_lock, flags);
1052        sess->flags &= ~FCF_ASYNC_SENT;
1053        spin_unlock_irqrestore(&vha->work_lock, flags);
1054
1055        spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1056        if (sess->se_sess) {
1057                sess->se_sess = NULL;
1058                if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
1059                        tgt->sess_count--;
1060        }
1061
1062        qla2x00_set_fcport_disc_state(sess, DSC_DELETED);
1063        sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1064        sess->deleted = QLA_SESS_DELETED;
1065
1066        if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
1067                vha->fcport_count--;
1068                sess->login_succ = 0;
1069        }
1070
1071        qla2x00_clear_loop_id(sess);
1072
1073        if (sess->conflict) {
1074                sess->conflict->login_pause = 0;
1075                sess->conflict = NULL;
1076                if (!test_bit(UNLOADING, &vha->dpc_flags))
1077                        set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1078        }
1079
1080        {
1081                struct qlt_plogi_ack_t *con =
1082                    sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1083                struct imm_ntfy_from_isp *iocb;
1084
1085                own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1086
1087                if (con) {
1088                        iocb = &con->iocb;
1089                        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1090                                 "se_sess %p / sess %p port %8phC is gone,"
1091                                 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
1092                                 sess->se_sess, sess, sess->port_name,
1093                                 own ? "releasing own PLOGI" : "no own PLOGI pending",
1094                                 own ? own->ref_count : -1,
1095                                 iocb->u.isp24.port_name, con->ref_count);
1096                        qlt_plogi_ack_unref(vha, con);
1097                        sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1098                } else {
1099                        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
1100                            "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
1101                            sess->se_sess, sess, sess->port_name,
1102                            own ? "releasing own PLOGI" :
1103                            "no own PLOGI pending",
1104                            own ? own->ref_count : -1);
1105                }
1106
1107                if (own) {
1108                        sess->fw_login_state = DSC_LS_PLOGI_PEND;
1109                        qlt_plogi_ack_unref(vha, own);
1110                        sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
1111                }
1112        }
1113
1114        sess->explicit_logout = 0;
1115        spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1116        sess->free_pending = 0;
1117
1118        qla2x00_dfs_remove_rport(vha, sess);
1119
1120        ql_dbg(ql_dbg_disc, vha, 0xf001,
1121            "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
1122                sess, sess->port_name, vha->fcport_count);
1123
1124        if (tgt && (tgt->sess_count == 0))
1125                wake_up_all(&tgt->waitQ);
1126
1127        if (!test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags) &&
1128            !(vha->vp_idx && test_bit(VPORT_DELETE, &vha->dpc_flags)) &&
1129            (!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
1130                switch (vha->host->active_mode) {
1131                case MODE_INITIATOR:
1132                case MODE_DUAL:
1133                        set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
1134                        qla2xxx_wake_dpc(vha);
1135                        break;
1136                case MODE_TARGET:
1137                default:
1138                        /* no-op */
1139                        break;
1140                }
1141        }
1142
1143        if (vha->fcport_count == 0)
1144                wake_up_all(&vha->fcport_waitQ);
1145}
1146
1147/* ha->tgt.sess_lock supposed to be held on entry */
1148void qlt_unreg_sess(struct fc_port *sess)
1149{
1150        struct scsi_qla_host *vha = sess->vha;
1151        unsigned long flags;
1152
1153        ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1154            "%s sess %p for deletion %8phC\n",
1155            __func__, sess, sess->port_name);
1156
1157        spin_lock_irqsave(&sess->vha->work_lock, flags);
1158        if (sess->free_pending) {
1159                spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1160                return;
1161        }
1162        sess->free_pending = 1;
1163        /*
1164         * Use FCF_ASYNC_SENT flag to block other cmds used in sess
1165         * management from being sent.
1166         */
1167        sess->flags |= FCF_ASYNC_SENT;
1168        spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1169
1170        if (sess->se_sess)
1171                vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1172
1173        sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1174        qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1175        sess->last_rscn_gen = sess->rscn_gen;
1176        sess->last_login_gen = sess->login_gen;
1177
1178        queue_work(sess->vha->hw->wq, &sess->free_work);
1179}
1180EXPORT_SYMBOL(qlt_unreg_sess);
1181
1182static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
1183{
1184        struct qla_hw_data *ha = vha->hw;
1185        struct fc_port *sess = NULL;
1186        uint16_t loop_id;
1187        int res = 0;
1188        struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1189        unsigned long flags;
1190
1191        loop_id = le16_to_cpu(n->u.isp24.nport_handle);
1192        if (loop_id == 0xFFFF) {
1193                /* Global event */
1194                atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1195                spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1196                qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1197                spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1198        } else {
1199                spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1200                sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1201                spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1202        }
1203
1204        ql_dbg(ql_dbg_tgt, vha, 0xe000,
1205            "Using sess for qla_tgt_reset: %p\n", sess);
1206        if (!sess) {
1207                res = -ESRCH;
1208                return res;
1209        }
1210
1211        ql_dbg(ql_dbg_tgt, vha, 0xe047,
1212            "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
1213            "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1214            mcmd, loop_id);
1215
1216        return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1217}
1218
1219static void qla24xx_chk_fcp_state(struct fc_port *sess)
1220{
1221        if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1222                sess->logout_on_delete = 0;
1223                sess->logo_ack_needed = 0;
1224                sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
1225        }
1226}
1227
1228void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1229{
1230        struct qla_tgt *tgt = sess->tgt;
1231        unsigned long flags;
1232        u16 sec;
1233
1234        switch (sess->disc_state) {
1235        case DSC_DELETE_PEND:
1236                return;
1237        case DSC_DELETED:
1238                if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
1239                        !sess->plogi_link[QLT_PLOGI_LINK_CONFLICT]) {
1240                        if (tgt && tgt->tgt_stop && tgt->sess_count == 0)
1241                                wake_up_all(&tgt->waitQ);
1242
1243                        if (sess->vha->fcport_count == 0)
1244                                wake_up_all(&sess->vha->fcport_waitQ);
1245                        return;
1246                }
1247                break;
1248        case DSC_UPD_FCPORT:
1249                /*
1250                 * This port is not done reporting to upper layer.
1251                 * let it finish
1252                 */
1253                sess->next_disc_state = DSC_DELETE_PEND;
1254                sec = jiffies_to_msecs(jiffies -
1255                    sess->jiffies_at_registration)/1000;
1256                if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
1257                        sess->sec_since_registration = sec;
1258                        ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
1259                            "%s %8phC : Slow Rport registration(%d Sec)\n",
1260                            __func__, sess->port_name, sec);
1261                }
1262                return;
1263        default:
1264                break;
1265        }
1266
1267        spin_lock_irqsave(&sess->vha->work_lock, flags);
1268        if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1269                spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1270                return;
1271        }
1272        sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1273        spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1274
1275        sess->prli_pend_timer = 0;
1276        qla2x00_set_fcport_disc_state(sess, DSC_DELETE_PEND);
1277
1278        qla24xx_chk_fcp_state(sess);
1279
1280        ql_dbg(ql_log_warn, sess->vha, 0xe001,
1281            "Scheduling sess %p for deletion %8phC\n",
1282            sess, sess->port_name);
1283
1284        WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1285}
1286
1287static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1288{
1289        struct fc_port *sess;
1290        scsi_qla_host_t *vha = tgt->vha;
1291
1292        list_for_each_entry(sess, &vha->vp_fcports, list) {
1293                if (sess->se_sess)
1294                        qlt_schedule_sess_for_deletion(sess);
1295        }
1296
1297        /* At this point tgt could be already dead */
1298}
1299
1300static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1301        uint16_t *loop_id)
1302{
1303        struct qla_hw_data *ha = vha->hw;
1304        dma_addr_t gid_list_dma;
1305        struct gid_list_info *gid_list, *gid;
1306        int res, rc, i;
1307        uint16_t entries;
1308
1309        gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1310            &gid_list_dma, GFP_KERNEL);
1311        if (!gid_list) {
1312                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
1313                    "qla_target(%d): DMA Alloc failed of %u\n",
1314                    vha->vp_idx, qla2x00_gid_list_size(ha));
1315                return -ENOMEM;
1316        }
1317
1318        /* Get list of logged in devices */
1319        rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1320        if (rc != QLA_SUCCESS) {
1321                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
1322                    "qla_target(%d): get_id_list() failed: %x\n",
1323                    vha->vp_idx, rc);
1324                res = -EBUSY;
1325                goto out_free_id_list;
1326        }
1327
1328        gid = gid_list;
1329        res = -ENOENT;
1330        for (i = 0; i < entries; i++) {
1331                if (gid->al_pa == s_id.al_pa &&
1332                    gid->area == s_id.area &&
1333                    gid->domain == s_id.domain) {
1334                        *loop_id = le16_to_cpu(gid->loop_id);
1335                        res = 0;
1336                        break;
1337                }
1338                gid = (void *)gid + ha->gid_list_info_size;
1339        }
1340
1341out_free_id_list:
1342        dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
1343            gid_list, gid_list_dma);
1344        return res;
1345}
1346
1347/*
1348 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
1349 * Caller must put it.
1350 */
1351static struct fc_port *qlt_create_sess(
1352        struct scsi_qla_host *vha,
1353        fc_port_t *fcport,
1354        bool local)
1355{
1356        struct qla_hw_data *ha = vha->hw;
1357        struct fc_port *sess = fcport;
1358        unsigned long flags;
1359
1360        if (vha->vha_tgt.qla_tgt->tgt_stop)
1361                return NULL;
1362
1363        if (fcport->se_sess) {
1364                if (!kref_get_unless_zero(&sess->sess_kref)) {
1365                        ql_dbg(ql_dbg_disc, vha, 0x20f6,
1366                            "%s: kref_get_unless_zero failed for %8phC\n",
1367                            __func__, sess->port_name);
1368                        return NULL;
1369                }
1370                return fcport;
1371        }
1372        sess->tgt = vha->vha_tgt.qla_tgt;
1373        sess->local = local;
1374
1375        /*
1376         * Under normal circumstances we want to logout from firmware when
1377         * session eventually ends and release corresponding nport handle.
1378         * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1379         * code will adjust these flags as necessary.
1380         */
1381        sess->logout_on_delete = 1;
1382        sess->keep_nport_handle = 0;
1383        sess->logout_completed = 0;
1384
1385        if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
1386            &fcport->port_name[0], sess) < 0) {
1387                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1388                    "(%d) %8phC check_initiator_node_acl failed\n",
1389                    vha->vp_idx, fcport->port_name);
1390                return NULL;
1391        } else {
1392                kref_init(&fcport->sess_kref);
1393                /*
1394                 * Take an extra reference to ->sess_kref here to handle
1395                 * fc_port access across ->tgt.sess_lock reaquire.
1396                 */
1397                if (!kref_get_unless_zero(&sess->sess_kref)) {
1398                        ql_dbg(ql_dbg_disc, vha, 0x20f7,
1399                            "%s: kref_get_unless_zero failed for %8phC\n",
1400                            __func__, sess->port_name);
1401                        return NULL;
1402                }
1403
1404                spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1405                if (!IS_SW_RESV_ADDR(sess->d_id))
1406                        vha->vha_tgt.qla_tgt->sess_count++;
1407
1408                qlt_do_generation_tick(vha, &sess->generation);
1409                spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1410        }
1411
1412        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
1413            "Adding sess %p se_sess %p  to tgt %p sess_count %d\n",
1414            sess, sess->se_sess, vha->vha_tgt.qla_tgt,
1415            vha->vha_tgt.qla_tgt->sess_count);
1416
1417        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1418            "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
1419            "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
1420            vha->vp_idx, local ?  "local " : "", fcport->port_name,
1421            fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
1422            sess->d_id.b.al_pa, sess->conf_compl_supported ?  "" : "not ");
1423
1424        return sess;
1425}
1426
1427/*
1428 * max_gen - specifies maximum session generation
1429 * at which this deletion requestion is still valid
1430 */
1431void
1432qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1433{
1434        struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1435        struct fc_port *sess = fcport;
1436        unsigned long flags;
1437
1438        if (!vha->hw->tgt.tgt_ops)
1439                return;
1440
1441        if (!tgt)
1442                return;
1443
1444        spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1445        if (tgt->tgt_stop) {
1446                spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1447                return;
1448        }
1449        if (!sess->se_sess) {
1450                spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1451                return;
1452        }
1453
1454        if (max_gen - sess->generation < 0) {
1455                spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1456                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
1457                    "Ignoring stale deletion request for se_sess %p / sess %p"
1458                    " for port %8phC, req_gen %d, sess_gen %d\n",
1459                    sess->se_sess, sess, sess->port_name, max_gen,
1460                    sess->generation);
1461                return;
1462        }
1463
1464        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);
1465
1466        sess->local = 1;
1467        spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1468        qlt_schedule_sess_for_deletion(sess);
1469}
1470
1471static inline int test_tgt_sess_count(struct qla_tgt *tgt)
1472{
1473        struct qla_hw_data *ha = tgt->ha;
1474        unsigned long flags;
1475        int res;
1476        /*
1477         * We need to protect against race, when tgt is freed before or
1478         * inside wake_up()
1479         */
1480        spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1481        ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1482            "tgt %p, sess_count=%d\n",
1483            tgt, tgt->sess_count);
1484        res = (tgt->sess_count == 0);
1485        spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1486
1487        return res;
1488}
1489
1490/* Called by tcm_qla2xxx configfs code */
1491int qlt_stop_phase1(struct qla_tgt *tgt)
1492{
1493        struct scsi_qla_host *vha = tgt->vha;
1494        struct qla_hw_data *ha = tgt->ha;
1495        unsigned long flags;
1496
1497        mutex_lock(&ha->optrom_mutex);
1498        mutex_lock(&qla_tgt_mutex);
1499
1500        if (tgt->tgt_stop || tgt->tgt_stopped) {
1501                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
1502                    "Already in tgt->tgt_stop or tgt_stopped state\n");
1503                mutex_unlock(&qla_tgt_mutex);
1504                mutex_unlock(&ha->optrom_mutex);
1505                return -EPERM;
1506        }
1507
1508        ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1509            vha->host_no, vha);
1510        /*
1511         * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
1512         * Lock is needed, because we still can get an incoming packet.
1513         */
1514        mutex_lock(&vha->vha_tgt.tgt_mutex);
1515        tgt->tgt_stop = 1;
1516        qlt_clear_tgt_db(tgt);
1517        mutex_unlock(&vha->vha_tgt.tgt_mutex);
1518        mutex_unlock(&qla_tgt_mutex);
1519
1520        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
1521            "Waiting for sess works (tgt %p)", tgt);
1522        spin_lock_irqsave(&tgt->sess_work_lock, flags);
1523        while (!list_empty(&tgt->sess_works_list)) {
1524                spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1525                flush_scheduled_work();
1526                spin_lock_irqsave(&tgt->sess_work_lock, flags);
1527        }
1528        spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1529
1530        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1531            "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1532
1533        wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1534
1535        /* Big hammer */
1536        if (!ha->flags.host_shutting_down &&
1537            (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1538                qlt_disable_vha(vha);
1539
1540        /* Wait for sessions to clear out (just in case) */
1541        wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1542        mutex_unlock(&ha->optrom_mutex);
1543
1544        return 0;
1545}
1546EXPORT_SYMBOL(qlt_stop_phase1);
1547
1548/* Called by tcm_qla2xxx configfs code */
1549void qlt_stop_phase2(struct qla_tgt *tgt)
1550{
1551        scsi_qla_host_t *vha = tgt->vha;
1552
1553        if (tgt->tgt_stopped) {
1554                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1555                    "Already in tgt->tgt_stopped state\n");
1556                dump_stack();
1557                return;
1558        }
1559        if (!tgt->tgt_stop) {
1560                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
1561                    "%s: phase1 stop is not completed\n", __func__);
1562                dump_stack();
1563                return;
1564        }
1565
1566        mutex_lock(&tgt->ha->optrom_mutex);
1567        mutex_lock(&vha->vha_tgt.tgt_mutex);
1568        tgt->tgt_stop = 0;
1569        tgt->tgt_stopped = 1;
1570        mutex_unlock(&vha->vha_tgt.tgt_mutex);
1571        mutex_unlock(&tgt->ha->optrom_mutex);
1572
1573        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1574            tgt);
1575
1576        switch (vha->qlini_mode) {
1577        case QLA2XXX_INI_MODE_EXCLUSIVE:
1578                vha->flags.online = 1;
1579                set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
1580                break;
1581        default:
1582                break;
1583        }
1584}
1585EXPORT_SYMBOL(qlt_stop_phase2);
1586
1587/* Called from qlt_remove_target() -> qla2x00_remove_one() */
1588static void qlt_release(struct qla_tgt *tgt)
1589{
1590        scsi_qla_host_t *vha = tgt->vha;
1591        void *node;
1592        u64 key = 0;
1593        u16 i;
1594        struct qla_qpair_hint *h;
1595        struct qla_hw_data *ha = vha->hw;
1596
1597        if (!tgt->tgt_stop && !tgt->tgt_stopped)
1598                qlt_stop_phase1(tgt);
1599
1600        if (!tgt->tgt_stopped)
1601                qlt_stop_phase2(tgt);
1602
1603        for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
1604                unsigned long flags;
1605
1606                h = &tgt->qphints[i];
1607                if (h->qpair) {
1608                        spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
1609                        list_del(&h->hint_elem);
1610                        spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
1611                        h->qpair = NULL;
1612                }
1613        }
1614        kfree(tgt->qphints);
1615        mutex_lock(&qla_tgt_mutex);
1616        list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
1617        mutex_unlock(&qla_tgt_mutex);
1618
1619        btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
1620                btree_remove64(&tgt->lun_qpair_map, key);
1621
1622        btree_destroy64(&tgt->lun_qpair_map);
1623
1624        if (vha->vp_idx)
1625                if (ha->tgt.tgt_ops &&
1626                    ha->tgt.tgt_ops->remove_target &&
1627                    vha->vha_tgt.target_lport_ptr)
1628                        ha->tgt.tgt_ops->remove_target(vha);
1629
1630        vha->vha_tgt.qla_tgt = NULL;
1631
1632        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1633            "Release of tgt %p finished\n", tgt);
1634
1635        kfree(tgt);
1636}
1637
1638/* ha->hardware_lock supposed to be held on entry */
1639static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
1640        const void *param, unsigned int param_size)
1641{
1642        struct qla_tgt_sess_work_param *prm;
1643        unsigned long flags;
1644
1645        prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
1646        if (!prm) {
1647                ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
1648                    "qla_target(%d): Unable to create session "
1649                    "work, command will be refused", 0);
1650                return -ENOMEM;
1651        }
1652
1653        ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
1654            "Scheduling work (type %d, prm %p)"
1655            " to find session for param %p (size %d, tgt %p)\n",
1656            type, prm, param, param_size, tgt);
1657
1658        prm->type = type;
1659        memcpy(&prm->tm_iocb, param, param_size);
1660
1661        spin_lock_irqsave(&tgt->sess_work_lock, flags);
1662        list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
1663        spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
1664
1665        schedule_work(&tgt->sess_work);
1666
1667        return 0;
1668}
1669
1670/*
1671 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1672 */
1673static void qlt_send_notify_ack(struct qla_qpair *qpair,
1674        struct imm_ntfy_from_isp *ntfy,
1675        uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
1676        uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
1677{
1678        struct scsi_qla_host *vha = qpair->vha;
1679        struct qla_hw_data *ha = vha->hw;
1680        request_t *pkt;
1681        struct nack_to_isp *nack;
1682
1683        if (!ha->flags.fw_started)
1684                return;
1685
1686        ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);
1687
1688        pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1689        if (!pkt) {
1690                ql_dbg(ql_dbg_tgt, vha, 0xe049,
1691                    "qla_target(%d): %s failed: unable to allocate "
1692                    "request packet\n", vha->vp_idx, __func__);
1693                return;
1694        }
1695
1696        if (vha->vha_tgt.qla_tgt != NULL)
1697                vha->vha_tgt.qla_tgt->notify_ack_expected++;
1698
1699        pkt->entry_type = NOTIFY_ACK_TYPE;
1700        pkt->entry_count = 1;
1701
1702        nack = (struct nack_to_isp *)pkt;
1703        nack->ox_id = ntfy->ox_id;
1704
1705        nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1706        nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
1707        if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
1708                nack->u.isp24.flags = ntfy->u.isp24.flags &
1709                        cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
1710        }
1711        nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
1712        nack->u.isp24.status = ntfy->u.isp24.status;
1713        nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1714        nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1715        nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
1716        nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
1717        nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
1718        nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
1719        nack->u.isp24.srr_reject_code = srr_reject_code;
1720        nack->u.isp24.srr_reject_code_expl = srr_explan;
1721        nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
1722
1723        ql_dbg(ql_dbg_tgt, vha, 0xe005,
1724            "qla_target(%d): Sending 24xx Notify Ack %d\n",
1725            vha->vp_idx, nack->u.isp24.status);
1726
1727        /* Memory Barrier */
1728        wmb();
1729        qla2x00_start_iocbs(vha, qpair->req);
1730}
1731
1732static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
1733{
1734        struct scsi_qla_host *vha = mcmd->vha;
1735        struct qla_hw_data *ha = vha->hw;
1736        struct abts_resp_to_24xx *resp;
1737        __le32 f_ctl;
1738        uint32_t h;
1739        uint8_t *p;
1740        int rc;
1741        struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
1742        struct qla_qpair *qpair = mcmd->qpair;
1743
1744        ql_dbg(ql_dbg_tgt, vha, 0xe006,
1745            "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
1746            ha, mcmd->fc_tm_rsp);
1747
1748        rc = qlt_check_reserve_free_req(qpair, 1);
1749        if (rc) {
1750                ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1751                    "qla_target(%d): %s failed: unable to allocate request packet\n",
1752                    vha->vp_idx, __func__);
1753                return -EAGAIN;
1754        }
1755
1756        resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
1757        memset(resp, 0, sizeof(*resp));
1758
1759        h = qlt_make_handle(qpair);
1760        if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
1761                /*
1762                 * CTIO type 7 from the firmware doesn't provide a way to
1763                 * know the initiator's LOOP ID, hence we can't find
1764                 * the session and, so, the command.
1765                 */
1766                return -EAGAIN;
1767        } else {
1768                qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
1769        }
1770
1771        resp->handle = make_handle(qpair->req->id, h);
1772        resp->entry_type = ABTS_RESP_24XX;
1773        resp->entry_count = 1;
1774        resp->nport_handle = abts->nport_handle;
1775        resp->vp_index = vha->vp_idx;
1776        resp->sof_type = abts->sof_type;
1777        resp->exchange_address = abts->exchange_address;
1778        resp->fcp_hdr_le = abts->fcp_hdr_le;
1779        f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1780            F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1781            F_CTL_SEQ_INITIATIVE);
1782        p = (uint8_t *)&f_ctl;
1783        resp->fcp_hdr_le.f_ctl[0] = *p++;
1784        resp->fcp_hdr_le.f_ctl[1] = *p++;
1785        resp->fcp_hdr_le.f_ctl[2] = *p;
1786
1787        resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1788        resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1789
1790        resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1791        if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
1792                resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1793                resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1794                resp->payload.ba_acct.low_seq_cnt = 0x0000;
1795                resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1796                resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1797                resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1798        } else {
1799                resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1800                resp->payload.ba_rjt.reason_code =
1801                        BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1802                /* Other bytes are zero */
1803        }
1804
1805        vha->vha_tgt.qla_tgt->abts_resp_expected++;
1806
1807        /* Memory Barrier */
1808        wmb();
1809        if (qpair->reqq_start_iocbs)
1810                qpair->reqq_start_iocbs(qpair);
1811        else
1812                qla2x00_start_iocbs(vha, qpair->req);
1813
1814        return rc;
1815}
1816
1817/*
1818 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1819 */
1820static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1821        struct abts_recv_from_24xx *abts, uint32_t status,
1822        bool ids_reversed)
1823{
1824        struct scsi_qla_host *vha = qpair->vha;
1825        struct qla_hw_data *ha = vha->hw;
1826        struct abts_resp_to_24xx *resp;
1827        __le32 f_ctl;
1828        uint8_t *p;
1829
1830        ql_dbg(ql_dbg_tgt, vha, 0xe006,
1831            "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
1832            ha, abts, status);
1833
1834        resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
1835            NULL);
1836        if (!resp) {
1837                ql_dbg(ql_dbg_tgt, vha, 0xe04a,
1838                    "qla_target(%d): %s failed: unable to allocate "
1839                    "request packet", vha->vp_idx, __func__);
1840                return;
1841        }
1842
1843        resp->entry_type = ABTS_RESP_24XX;
1844        resp->handle = QLA_TGT_SKIP_HANDLE;
1845        resp->entry_count = 1;
1846        resp->nport_handle = abts->nport_handle;
1847        resp->vp_index = vha->vp_idx;
1848        resp->sof_type = abts->sof_type;
1849        resp->exchange_address = abts->exchange_address;
1850        resp->fcp_hdr_le = abts->fcp_hdr_le;
1851        f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1852            F_CTL_LAST_SEQ | F_CTL_END_SEQ |
1853            F_CTL_SEQ_INITIATIVE);
1854        p = (uint8_t *)&f_ctl;
1855        resp->fcp_hdr_le.f_ctl[0] = *p++;
1856        resp->fcp_hdr_le.f_ctl[1] = *p++;
1857        resp->fcp_hdr_le.f_ctl[2] = *p;
1858        if (ids_reversed) {
1859                resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
1860                resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1861        } else {
1862                resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
1863                resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1864        }
1865        resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
1866        if (status == FCP_TMF_CMPL) {
1867                resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
1868                resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
1869                resp->payload.ba_acct.low_seq_cnt = 0x0000;
1870                resp->payload.ba_acct.high_seq_cnt = cpu_to_le16(0xFFFF);
1871                resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
1872                resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
1873        } else {
1874                resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
1875                resp->payload.ba_rjt.reason_code =
1876                        BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
1877                /* Other bytes are zero */
1878        }
1879
1880        vha->vha_tgt.qla_tgt->abts_resp_expected++;
1881
1882        /* Memory Barrier */
1883        wmb();
1884        if (qpair->reqq_start_iocbs)
1885                qpair->reqq_start_iocbs(qpair);
1886        else
1887                qla2x00_start_iocbs(vha, qpair->req);
1888}
1889
1890/*
1891 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
1892 */
1893static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1894    struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1895{
1896        struct ctio7_to_24xx *ctio;
1897        u16 tmp;
1898        struct abts_recv_from_24xx *entry;
1899
1900        ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1901        if (ctio == NULL) {
1902                ql_dbg(ql_dbg_tgt, vha, 0xe04b,
1903                    "qla_target(%d): %s failed: unable to allocate "
1904                    "request packet\n", vha->vp_idx, __func__);
1905                return;
1906        }
1907
1908        if (mcmd)
1909                /* abts from remote port */
1910                entry = &mcmd->orig_iocb.abts;
1911        else
1912                /* abts from this driver.  */
1913                entry = (struct abts_recv_from_24xx *)pkt;
1914
1915        /*
1916         * We've got on entrance firmware's response on by us generated
1917         * ABTS response. So, in it ID fields are reversed.
1918         */
1919
1920        ctio->entry_type = CTIO_TYPE7;
1921        ctio->entry_count = 1;
1922        ctio->nport_handle = entry->nport_handle;
1923        ctio->handle = QLA_TGT_SKIP_HANDLE |    CTIO_COMPLETION_HANDLE_MARK;
1924        ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1925        ctio->vp_index = vha->vp_idx;
1926        ctio->exchange_addr = entry->exchange_addr_to_abort;
1927        tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1928
1929        if (mcmd) {
1930                ctio->initiator_id = entry->fcp_hdr_le.s_id;
1931
1932                if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
1933                        tmp |= (mcmd->abort_io_attr << 9);
1934                else if (qpair->retry_term_cnt & 1)
1935                        tmp |= (0x4 << 9);
1936        } else {
1937                ctio->initiator_id = entry->fcp_hdr_le.d_id;
1938
1939                if (qpair->retry_term_cnt & 1)
1940                        tmp |= (0x4 << 9);
1941        }
1942        ctio->u.status1.flags = cpu_to_le16(tmp);
1943        ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1944
1945        ql_dbg(ql_dbg_tgt, vha, 0xe007,
1946            "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
1947            le16_to_cpu(ctio->u.status1.flags),
1948            le16_to_cpu(ctio->u.status1.ox_id),
1949            (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1950
1951        /* Memory Barrier */
1952        wmb();
1953        if (qpair->reqq_start_iocbs)
1954                qpair->reqq_start_iocbs(qpair);
1955        else
1956                qla2x00_start_iocbs(vha, qpair->req);
1957
1958        if (mcmd)
1959                qlt_build_abts_resp_iocb(mcmd);
1960        else
1961                qlt_24xx_send_abts_resp(qpair,
1962                    (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1963
1964}
1965
1966/* drop cmds for the given lun
1967 * XXX only looks for cmds on the port through which lun reset was recieved
1968 * XXX does not go through the list of other port (which may have cmds
1969 *     for the same lun)
1970 */
1971static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
1972{
1973        struct qla_tgt_sess_op *op;
1974        struct qla_tgt_cmd *cmd;
1975        uint32_t key;
1976        unsigned long flags;
1977
1978        key = sid_to_key(s_id);
1979        spin_lock_irqsave(&vha->cmd_list_lock, flags);
1980        list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
1981                uint32_t op_key;
1982                u64 op_lun;
1983
1984                op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1985                op_lun = scsilun_to_int(
1986                        (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1987                if (op_key == key && op_lun == lun)
1988                        op->aborted = true;
1989        }
1990
1991        list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
1992                uint32_t op_key;
1993                u64 op_lun;
1994
1995                op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
1996                op_lun = scsilun_to_int(
1997                        (struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
1998                if (op_key == key && op_lun == lun)
1999                        op->aborted = true;
2000        }
2001
2002        list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
2003                uint32_t cmd_key;
2004                u64 cmd_lun;
2005
2006                cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
2007                cmd_lun = scsilun_to_int(
2008                        (struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
2009                if (cmd_key == key && cmd_lun == lun)
2010                        cmd->aborted = 1;
2011        }
2012        spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
2013}
2014
2015static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
2016    uint64_t unpacked_lun)
2017{
2018        struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
2019        struct qla_qpair_hint *h = NULL;
2020
2021        if (vha->flags.qpairs_available) {
2022                h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
2023                if (!h)
2024                        h = &tgt->qphints[0];
2025        } else {
2026                h = &tgt->qphints[0];
2027        }
2028
2029        return h;
2030}
2031
2032static void qlt_do_tmr_work(struct work_struct *work)
2033{
2034        struct qla_tgt_mgmt_cmd *mcmd =
2035                container_of(work, struct qla_tgt_mgmt_cmd, work);
2036        struct qla_hw_data *ha = mcmd->vha->hw;
2037        int rc;
2038        uint32_t tag;
2039        unsigned long flags;
2040
2041        switch (mcmd->tmr_func) {
2042        case QLA_TGT_ABTS:
2043                tag = le32_to_cpu(mcmd->orig_iocb.abts.exchange_addr_to_abort);
2044                break;
2045        default:
2046                tag = 0;
2047                break;
2048        }
2049
2050        rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
2051            mcmd->tmr_func, tag);
2052
2053        if (rc != 0) {
2054                spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
2055                switch (mcmd->tmr_func) {
2056                case QLA_TGT_ABTS:
2057                        mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
2058                        qlt_build_abts_resp_iocb(mcmd);
2059                        break;
2060                case QLA_TGT_LUN_RESET:
2061                case QLA_TGT_CLEAR_TS:
2062                case QLA_TGT_ABORT_TS:
2063                case QLA_TGT_CLEAR_ACA:
2064                case QLA_TGT_TARGET_RESET:
2065                        qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
2066                            qla_sam_status);
2067                        break;
2068
2069                case QLA_TGT_ABORT_ALL:
2070                case QLA_TGT_NEXUS_LOSS_SESS:
2071                case QLA_TGT_NEXUS_LOSS:
2072                        qlt_send_notify_ack(mcmd->qpair,
2073                            &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2074                        break;
2075                }
2076                spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);
2077
2078                ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
2079                    "qla_target(%d):  tgt_ops->handle_tmr() failed: %d\n",
2080                    mcmd->vha->vp_idx, rc);
2081                mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2082        }
2083}
2084
2085/* ha->hardware_lock supposed to be held on entry */
2086static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2087        struct abts_recv_from_24xx *abts, struct fc_port *sess)
2088{
2089        struct qla_hw_data *ha = vha->hw;
2090        struct qla_tgt_mgmt_cmd *mcmd;
2091        struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2092        struct qla_tgt_cmd *abort_cmd;
2093
2094        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
2095            "qla_target(%d): task abort (tag=%d)\n",
2096            vha->vp_idx, abts->exchange_addr_to_abort);
2097
2098        mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
2099        if (mcmd == NULL) {
2100                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
2101                    "qla_target(%d): %s: Allocation of ABORT cmd failed",
2102                    vha->vp_idx, __func__);
2103                return -ENOMEM;
2104        }
2105        memset(mcmd, 0, sizeof(*mcmd));
2106        mcmd->cmd_type = TYPE_TGT_TMCMD;
2107        mcmd->sess = sess;
2108        memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2109        mcmd->reset_count = ha->base_qpair->chip_reset;
2110        mcmd->tmr_func = QLA_TGT_ABTS;
2111        mcmd->qpair = h->qpair;
2112        mcmd->vha = vha;
2113
2114        /*
2115         * LUN is looked up by target-core internally based on the passed
2116         * abts->exchange_addr_to_abort tag.
2117         */
2118        mcmd->se_cmd.cpuid = h->cpuid;
2119
2120        abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
2121                                le32_to_cpu(abts->exchange_addr_to_abort));
2122        if (!abort_cmd)
2123                return -EIO;
2124        mcmd->unpacked_lun = abort_cmd->se_cmd.orig_fe_lun;
2125
2126        if (abort_cmd->qpair) {
2127                mcmd->qpair = abort_cmd->qpair;
2128                mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2129                mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
2130                mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2131        }
2132
2133        INIT_WORK(&mcmd->work, qlt_do_tmr_work);
2134        queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);
2135
2136        return 0;
2137}
2138
2139/*
2140 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2141 */
2142static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2143        struct abts_recv_from_24xx *abts)
2144{
2145        struct qla_hw_data *ha = vha->hw;
2146        struct fc_port *sess;
2147        uint32_t tag = le32_to_cpu(abts->exchange_addr_to_abort);
2148        be_id_t s_id;
2149        int rc;
2150        unsigned long flags;
2151
2152        if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
2153                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
2154                    "qla_target(%d): ABTS: Abort Sequence not "
2155                    "supported\n", vha->vp_idx);
2156                qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2157                    false);
2158                return;
2159        }
2160
2161        if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
2162                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
2163                    "qla_target(%d): ABTS: Unknown Exchange "
2164                    "Address received\n", vha->vp_idx);
2165                qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2166                    false);
2167                return;
2168        }
2169
2170        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
2171            "qla_target(%d): task abort (s_id=%x:%x:%x, "
2172            "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
2173            abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2174            le32_to_cpu(abts->fcp_hdr_le.parameter));
2175
2176        s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2177
2178        spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2179        sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
2180        if (!sess) {
2181                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2182                    "qla_target(%d): task abort for non-existent session\n",
2183                    vha->vp_idx);
2184                spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2185
2186                qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2187                            false);
2188                return;
2189        }
2190        spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
2191
2192
2193        if (sess->deleted) {
2194                qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2195                    false);
2196                return;
2197        }
2198
2199        rc = __qlt_24xx_handle_abts(vha, abts, sess);
2200        if (rc != 0) {
2201                ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
2202                    "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
2203                    vha->vp_idx, rc);
2204                qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
2205                    false);
2206                return;
2207        }
2208}
2209
2210/*
2211 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2212 */
2213static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2214        struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
2215{
2216        struct scsi_qla_host *ha = mcmd->vha;
2217        struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
2218        struct ctio7_to_24xx *ctio;
2219        uint16_t temp;
2220
2221        ql_dbg(ql_dbg_tgt, ha, 0xe008,
2222            "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
2223            ha, atio, resp_code);
2224
2225
2226        ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2227        if (ctio == NULL) {
2228                ql_dbg(ql_dbg_tgt, ha, 0xe04c,
2229                    "qla_target(%d): %s failed: unable to allocate "
2230                    "request packet\n", ha->vp_idx, __func__);
2231                return;
2232        }
2233
2234        ctio->entry_type = CTIO_TYPE7;
2235        ctio->entry_count = 1;
2236        ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
2237        ctio->nport_handle = cpu_to_le16(mcmd->sess->loop_id);
2238        ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2239        ctio->vp_index = ha->vp_idx;
2240        ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2241        ctio->exchange_addr = atio->u.isp24.exchange_addr;
2242        temp = (atio->u.isp24.attr << 9)|
2243                CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2244        ctio->u.status1.flags = cpu_to_le16(temp);
2245        temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2246        ctio->u.status1.ox_id = cpu_to_le16(temp);
2247        ctio->u.status1.scsi_status =
2248            cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
2249        ctio->u.status1.response_len = cpu_to_le16(8);
2250        ctio->u.status1.sense_data[0] = resp_code;
2251
2252        /* Memory Barrier */
2253        wmb();
2254        if (qpair->reqq_start_iocbs)
2255                qpair->reqq_start_iocbs(qpair);
2256        else
2257                qla2x00_start_iocbs(ha, qpair->req);
2258}
2259
2260void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
2261{
2262        mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
2263}
2264EXPORT_SYMBOL(qlt_free_mcmd);
2265
2266/*
2267 * ha->hardware_lock supposed to be held on entry. Might drop it, then
2268 * reacquire
2269 */
2270void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2271    uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
2272{
2273        struct atio_from_isp *atio = &cmd->atio;
2274        struct ctio7_to_24xx *ctio;
2275        uint16_t temp;
2276        struct scsi_qla_host *vha = cmd->vha;
2277
2278        ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
2279            "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
2280            "sense_key=%02x, asc=%02x, ascq=%02x",
2281            vha, atio, scsi_status, sense_key, asc, ascq);
2282
2283        ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
2284        if (!ctio) {
2285                ql_dbg(ql_dbg_async, vha, 0x3067,
2286                    "qla2x00t(%ld): %s failed: unable to allocate request packet",
2287                    vha->host_no, __func__);
2288                goto out;
2289        }
2290
2291        ctio->entry_type = CTIO_TYPE7;
2292        ctio->entry_count = 1;
2293        ctio->handle = QLA_TGT_SKIP_HANDLE;
2294        ctio->nport_handle = cpu_to_le16(cmd->sess->loop_id);
2295        ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2296        ctio->vp_index = vha->vp_idx;
2297        ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2298        ctio->exchange_addr = atio->u.isp24.exchange_addr;
2299        temp = (atio->u.isp24.attr << 9) |
2300            CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
2301        ctio->u.status1.flags = cpu_to_le16(temp);
2302        temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2303        ctio->u.status1.ox_id = cpu_to_le16(temp);
2304        ctio->u.status1.scsi_status =
2305            cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
2306        ctio->u.status1.response_len = cpu_to_le16(18);
2307        ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));
2308
2309        if (ctio->u.status1.residual != 0)
2310                ctio->u.status1.scsi_status |=
2311                    cpu_to_le16(SS_RESIDUAL_UNDER);
2312
2313        /* Fixed format sense data. */
2314        ctio->u.status1.sense_data[0] = 0x70;
2315        ctio->u.status1.sense_data[2] = sense_key;
2316        /* Additional sense length */
2317        ctio->u.status1.sense_data[7] = 0xa;
2318        /* ASC and ASCQ */
2319        ctio->u.status1.sense_data[12] = asc;
2320        ctio->u.status1.sense_data[13] = ascq;
2321
2322        /* Memory Barrier */
2323        wmb();
2324
2325        if (qpair->reqq_start_iocbs)
2326                qpair->reqq_start_iocbs(qpair);
2327        else
2328                qla2x00_start_iocbs(vha, qpair->req);
2329
2330out:
2331        return;
2332}
2333
2334/* callback from target fabric module code */
2335void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
2336{
2337        struct scsi_qla_host *vha = mcmd->sess->vha;
2338        struct qla_hw_data *ha = vha->hw;
2339        unsigned long flags;
2340        struct qla_qpair *qpair = mcmd->qpair;
2341        bool free_mcmd = true;
2342
2343        ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
2344            "TM response mcmd (%p) status %#x state %#x",
2345            mcmd, mcmd->fc_tm_rsp, mcmd->flags);
2346
2347        spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2348
2349        if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2350                /*
2351                 * Either the port is not online or this request was from
2352                 * previous life, just abort the processing.
2353                 */
2354                ql_dbg(ql_dbg_async, vha, 0xe100,
2355                        "RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
2356                        vha->flags.online, qla2x00_reset_active(vha),
2357                        mcmd->reset_count, qpair->chip_reset);
2358                ha->tgt.tgt_ops->free_mcmd(mcmd);
2359                spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2360                return;
2361        }
2362
2363        if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2364                switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
2365                case ELS_LOGO:
2366                case ELS_PRLO:
2367                case ELS_TPRLO:
2368                        ql_dbg(ql_dbg_disc, vha, 0x2106,
2369                            "TM response logo %8phC status %#x state %#x",
2370                            mcmd->sess->port_name, mcmd->fc_tm_rsp,
2371                            mcmd->flags);
2372                        qlt_schedule_sess_for_deletion(mcmd->sess);
2373                        break;
2374                default:
2375                        qlt_send_notify_ack(vha->hw->base_qpair,
2376                            &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2377                        break;
2378                }
2379        } else {
2380                if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
2381                        qlt_build_abts_resp_iocb(mcmd);
2382                        free_mcmd = false;
2383                } else
2384                        qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2385                            mcmd->fc_tm_rsp);
2386        }
2387        /*
2388         * Make the callback for ->free_mcmd() to queue_work() and invoke
2389         * target_put_sess_cmd() to drop cmd_kref to 1.  The final
2390         * target_put_sess_cmd() call will be made from TFO->check_stop_free()
2391         * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
2392         * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
2393         * qlt_xmit_tm_rsp() returns here..
2394         */
2395        if (free_mcmd)
2396                ha->tgt.tgt_ops->free_mcmd(mcmd);
2397
2398        spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2399}
2400EXPORT_SYMBOL(qlt_xmit_tm_rsp);
2401
2402/* No locks */
2403static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
2404{
2405        struct qla_tgt_cmd *cmd = prm->cmd;
2406
2407        BUG_ON(cmd->sg_cnt == 0);
2408
2409        prm->sg = (struct scatterlist *)cmd->sg;
2410        prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2411            cmd->sg_cnt, cmd->dma_data_direction);
2412        if (unlikely(prm->seg_cnt == 0))
2413                goto out_err;
2414
2415        prm->cmd->sg_mapped = 1;
2416
2417        if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
2418                /*
2419                 * If greater than four sg entries then we need to allocate
2420                 * the continuation entries
2421                 */
2422                if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2423                        prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2424                        QLA_TGT_DATASEGS_PER_CMD_24XX,
2425                        QLA_TGT_DATASEGS_PER_CONT_24XX);
2426        } else {
2427                /* DIF */
2428                if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2429                    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2430                        prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
2431                        prm->tot_dsds = prm->seg_cnt;
2432                } else
2433                        prm->tot_dsds = prm->seg_cnt;
2434
2435                if (cmd->prot_sg_cnt) {
2436                        prm->prot_sg      = cmd->prot_sg;
2437                        prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2438                                cmd->prot_sg, cmd->prot_sg_cnt,
2439                                cmd->dma_data_direction);
2440                        if (unlikely(prm->prot_seg_cnt == 0))
2441                                goto out_err;
2442
2443                        if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
2444                            (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
2445                                /* Dif Bundling not support here */
2446                                prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
2447                                                                cmd->blk_sz);
2448                                prm->tot_dsds += prm->prot_seg_cnt;
2449                        } else
2450                                prm->tot_dsds += prm->prot_seg_cnt;
2451                }
2452        }
2453
2454        return 0;
2455
2456out_err:
2457        ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2458            "qla_target(%d): PCI mapping failed: sg_cnt=%d",
2459            0, prm->cmd->sg_cnt);
2460        return -1;
2461}
2462
2463static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2464{
2465        struct qla_hw_data *ha;
2466        struct qla_qpair *qpair;
2467
2468        if (!cmd->sg_mapped)
2469                return;
2470
2471        qpair = cmd->qpair;
2472
2473        dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2474            cmd->dma_data_direction);
2475        cmd->sg_mapped = 0;
2476
2477        if (cmd->prot_sg_cnt)
2478                dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2479                        cmd->dma_data_direction);
2480
2481        if (!cmd->ctx)
2482                return;
2483        ha = vha->hw;
2484        if (cmd->ctx_dsd_alloced)
2485                qla2x00_clean_dsd_pool(ha, cmd->ctx);
2486
2487        dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2488}
2489
2490static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2491        uint32_t req_cnt)
2492{
2493        uint32_t cnt;
2494        struct req_que *req = qpair->req;
2495
2496        if (req->cnt < (req_cnt + 2)) {
2497                cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
2498                    rd_reg_dword_relaxed(req->req_q_out));
2499
2500                if  (req->ring_index < cnt)
2501                        req->cnt = cnt - req->ring_index;
2502                else
2503                        req->cnt = req->length - (req->ring_index - cnt);
2504
2505                if (unlikely(req->cnt < (req_cnt + 2)))
2506                        return -EAGAIN;
2507        }
2508
2509        req->cnt -= req_cnt;
2510
2511        return 0;
2512}
2513
2514/*
2515 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
2516 */
2517static inline void *qlt_get_req_pkt(struct req_que *req)
2518{
2519        /* Adjust ring index. */
2520        req->ring_index++;
2521        if (req->ring_index == req->length) {
2522                req->ring_index = 0;
2523                req->ring_ptr = req->ring;
2524        } else {
2525                req->ring_ptr++;
2526        }
2527        return (cont_entry_t *)req->ring_ptr;
2528}
2529
2530/* ha->hardware_lock supposed to be held on entry */
2531static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2532{
2533        uint32_t h;
2534        int index;
2535        uint8_t found = 0;
2536        struct req_que *req = qpair->req;
2537
2538        h = req->current_outstanding_cmd;
2539
2540        for (index = 1; index < req->num_outstanding_cmds; index++) {
2541                h++;
2542                if (h == req->num_outstanding_cmds)
2543                        h = 1;
2544
2545                if (h == QLA_TGT_SKIP_HANDLE)
2546                        continue;
2547
2548                if (!req->outstanding_cmds[h]) {
2549                        found = 1;
2550                        break;
2551                }
2552        }
2553
2554        if (found) {
2555                req->current_outstanding_cmd = h;
2556        } else {
2557                ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
2558                    "qla_target(%d): Ran out of empty cmd slots\n",
2559                    qpair->vha->vp_idx);
2560                h = QLA_TGT_NULL_HANDLE;
2561        }
2562
2563        return h;
2564}
2565
2566/* ha->hardware_lock supposed to be held on entry */
2567static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
2568        struct qla_tgt_prm *prm)
2569{
2570        uint32_t h;
2571        struct ctio7_to_24xx *pkt;
2572        struct atio_from_isp *atio = &prm->cmd->atio;
2573        uint16_t temp;
2574
2575        pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2576        prm->pkt = pkt;
2577        memset(pkt, 0, sizeof(*pkt));
2578
2579        pkt->entry_type = CTIO_TYPE7;
2580        pkt->entry_count = (uint8_t)prm->req_cnt;
2581        pkt->vp_index = prm->cmd->vp_idx;
2582
2583        h = qlt_make_handle(qpair);
2584        if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
2585                /*
2586                 * CTIO type 7 from the firmware doesn't provide a way to
2587                 * know the initiator's LOOP ID, hence we can't find
2588                 * the session and, so, the command.
2589                 */
2590                return -EAGAIN;
2591        } else
2592                qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2593
2594        pkt->handle = make_handle(qpair->req->id, h);
2595        pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
2596        pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2597        pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2598        pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2599        pkt->exchange_addr = atio->u.isp24.exchange_addr;
2600        temp = atio->u.isp24.attr << 9;
2601        pkt->u.status0.flags |= cpu_to_le16(temp);
2602        temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
2603        pkt->u.status0.ox_id = cpu_to_le16(temp);
2604        pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);
2605
2606        return 0;
2607}
2608
2609/*
2610 * ha->hardware_lock supposed to be held on entry. We have already made sure
2611 * that there is sufficient amount of request entries to not drop it.
2612 */
2613static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2614{
2615        int cnt;
2616        struct dsd64 *cur_dsd;
2617
2618        /* Build continuation packets */
2619        while (prm->seg_cnt > 0) {
2620                cont_a64_entry_t *cont_pkt64 =
2621                        (cont_a64_entry_t *)qlt_get_req_pkt(
2622                           prm->cmd->qpair->req);
2623
2624                /*
2625                 * Make sure that from cont_pkt64 none of
2626                 * 64-bit specific fields used for 32-bit
2627                 * addressing. Cast to (cont_entry_t *) for
2628                 * that.
2629                 */
2630
2631                memset(cont_pkt64, 0, sizeof(*cont_pkt64));
2632
2633                cont_pkt64->entry_count = 1;
2634                cont_pkt64->sys_define = 0;
2635
2636                cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2637                cur_dsd = cont_pkt64->dsd;
2638
2639                /* Load continuation entry data segments */
2640                for (cnt = 0;
2641                    cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2642                    cnt++, prm->seg_cnt--) {
2643                        append_dsd64(&cur_dsd, prm->sg);
2644                        prm->sg = sg_next(prm->sg);
2645                }
2646        }
2647}
2648
2649/*
2650 * ha->hardware_lock supposed to be held on entry. We have already made sure
2651 * that there is sufficient amount of request entries to not drop it.
2652 */
2653static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2654{
2655        int cnt;
2656        struct dsd64 *cur_dsd;
2657        struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;
2658
2659        pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);
2660
2661        /* Setup packet address segment pointer */
2662        cur_dsd = &pkt24->u.status0.dsd;
2663
2664        /* Set total data segment count */
2665        if (prm->seg_cnt)
2666                pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);
2667
2668        if (prm->seg_cnt == 0) {
2669                /* No data transfer */
2670                cur_dsd->address = 0;
2671                cur_dsd->length = 0;
2672                return;
2673        }
2674
2675        /* If scatter gather */
2676
2677        /* Load command entry data segments */
2678        for (cnt = 0;
2679            (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2680            cnt++, prm->seg_cnt--) {
2681                append_dsd64(&cur_dsd, prm->sg);
2682                prm->sg = sg_next(prm->sg);
2683        }
2684
2685        qlt_load_cont_data_segments(prm);
2686}
2687
2688static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
2689{
2690        return cmd->bufflen > 0;
2691}
2692
2693static void qlt_print_dif_err(struct qla_tgt_prm *prm)
2694{
2695        struct qla_tgt_cmd *cmd;
2696        struct scsi_qla_host *vha;
2697
2698        /* asc 0x10=dif error */
2699        if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
2700                cmd = prm->cmd;
2701                vha = cmd->vha;
2702                /* ASCQ */
2703                switch (prm->sense_buffer[13]) {
2704                case 1:
2705                        ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2706                            "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2707                            "se_cmd=%p tag[%x]",
2708                            cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2709                            cmd->atio.u.isp24.exchange_addr);
2710                        break;
2711                case 2:
2712                        ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2713                            "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2714                            "se_cmd=%p tag[%x]",
2715                            cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2716                            cmd->atio.u.isp24.exchange_addr);
2717                        break;
2718                case 3:
2719                        ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2720                            "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
2721                            "se_cmd=%p tag[%x]",
2722                            cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2723                            cmd->atio.u.isp24.exchange_addr);
2724                        break;
2725                default:
2726                        ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2727                            "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
2728                            "se_cmd=%p tag[%x]",
2729                            cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
2730                            cmd->atio.u.isp24.exchange_addr);
2731                        break;
2732                }
2733                ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2734        }
2735}
2736
2737/*
2738 * Called without ha->hardware_lock held
2739 */
2740static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
2741        struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
2742        uint32_t *full_req_cnt)
2743{
2744        struct se_cmd *se_cmd = &cmd->se_cmd;
2745        struct qla_qpair *qpair = cmd->qpair;
2746
2747        prm->cmd = cmd;
2748        prm->tgt = cmd->tgt;
2749        prm->pkt = NULL;
2750        prm->rq_result = scsi_status;
2751        prm->sense_buffer = &cmd->sense_buffer[0];
2752        prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
2753        prm->sg = NULL;
2754        prm->seg_cnt = -1;
2755        prm->req_cnt = 1;
2756        prm->residual = 0;
2757        prm->add_status_pkt = 0;
2758        prm->prot_sg = NULL;
2759        prm->prot_seg_cnt = 0;
2760        prm->tot_dsds = 0;
2761
2762        if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
2763                if  (qlt_pci_map_calc_cnt(prm) != 0)
2764                        return -EAGAIN;
2765        }
2766
2767        *full_req_cnt = prm->req_cnt;
2768
2769        if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
2770                prm->residual = se_cmd->residual_count;
2771                ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2772                    "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2773                       prm->residual, se_cmd->tag,
2774                       se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
2775                       cmd->bufflen, prm->rq_result);
2776                prm->rq_result |= SS_RESIDUAL_UNDER;
2777        } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
2778                prm->residual = se_cmd->residual_count;
2779                ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2780                    "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
2781                       prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
2782                       se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2783                prm->rq_result |= SS_RESIDUAL_OVER;
2784        }
2785
2786        if (xmit_type & QLA_TGT_XMIT_STATUS) {
2787                /*
2788                 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
2789                 * ignored in *xmit_response() below
2790                 */
2791                if (qlt_has_data(cmd)) {
2792                        if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2793                            (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2794                            (prm->rq_result != 0))) {
2795                                prm->add_status_pkt = 1;
2796                                (*full_req_cnt)++;
2797                        }
2798                }
2799        }
2800
2801        return 0;
2802}
2803
2804static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
2805    int sending_sense)
2806{
2807        if (cmd->qpair->enable_class_2)
2808                return 0;
2809
2810        if (sending_sense)
2811                return cmd->conf_compl_supported;
2812        else
2813                return cmd->qpair->enable_explicit_conf &&
2814                    cmd->conf_compl_supported;
2815}
2816
2817static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
2818        struct qla_tgt_prm *prm)
2819{
2820        prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
2821            (uint32_t)sizeof(ctio->u.status1.sense_data));
2822        ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2823        if (qlt_need_explicit_conf(prm->cmd, 0)) {
2824                ctio->u.status0.flags |= cpu_to_le16(
2825                    CTIO7_FLAGS_EXPLICIT_CONFORM |
2826                    CTIO7_FLAGS_CONFORM_REQ);
2827        }
2828        ctio->u.status0.residual = cpu_to_le32(prm->residual);
2829        ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
2830        if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
2831                int i;
2832
2833                if (qlt_need_explicit_conf(prm->cmd, 1)) {
2834                        if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2835                                ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2836                                    "Skipping EXPLICIT_CONFORM and "
2837                                    "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
2838                                    "non GOOD status\n");
2839                                goto skip_explict_conf;
2840                        }
2841                        ctio->u.status1.flags |= cpu_to_le16(
2842                            CTIO7_FLAGS_EXPLICIT_CONFORM |
2843                            CTIO7_FLAGS_CONFORM_REQ);
2844                }
2845skip_explict_conf:
2846                ctio->u.status1.flags &=
2847                    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2848                ctio->u.status1.flags |=
2849                    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2850                ctio->u.status1.scsi_status |=
2851                    cpu_to_le16(SS_SENSE_LEN_VALID);
2852                ctio->u.status1.sense_length =
2853                    cpu_to_le16(prm->sense_buffer_len);
2854                for (i = 0; i < prm->sense_buffer_len/4; i++) {
2855                        uint32_t v;
2856
2857                        v = get_unaligned_be32(
2858                                        &((uint32_t *)prm->sense_buffer)[i]);
2859                        put_unaligned_le32(v,
2860                                &((uint32_t *)ctio->u.status1.sense_data)[i]);
2861                }
2862                qlt_print_dif_err(prm);
2863
2864        } else {
2865                ctio->u.status1.flags &=
2866                    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2867                ctio->u.status1.flags |=
2868                    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2869                ctio->u.status1.sense_length = 0;
2870                memset(ctio->u.status1.sense_data, 0,
2871                    sizeof(ctio->u.status1.sense_data));
2872        }
2873
2874        /* Sense with len > 24, is it possible ??? */
2875}
2876
2877static inline int
2878qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
2879{
2880        switch (se_cmd->prot_op) {
2881        case TARGET_PROT_DOUT_INSERT:
2882        case TARGET_PROT_DIN_STRIP:
2883                if (ql2xenablehba_err_chk >= 1)
2884                        return 1;
2885                break;
2886        case TARGET_PROT_DOUT_PASS:
2887        case TARGET_PROT_DIN_PASS:
2888                if (ql2xenablehba_err_chk >= 2)
2889                        return 1;
2890                break;
2891        case TARGET_PROT_DIN_INSERT:
2892        case TARGET_PROT_DOUT_STRIP:
2893                return 1;
2894        default:
2895                break;
2896        }
2897        return 0;
2898}
2899
2900static inline int
2901qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
2902{
2903        switch (se_cmd->prot_op) {
2904        case TARGET_PROT_DIN_INSERT:
2905        case TARGET_PROT_DOUT_INSERT:
2906        case TARGET_PROT_DIN_STRIP:
2907        case TARGET_PROT_DOUT_STRIP:
2908        case TARGET_PROT_DIN_PASS:
2909        case TARGET_PROT_DOUT_PASS:
2910            return 1;
2911        default:
2912            return 0;
2913        }
2914        return 0;
2915}
2916
2917/*
2918 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2919 */
2920static void
2921qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
2922    uint16_t *pfw_prot_opts)
2923{
2924        struct se_cmd *se_cmd = &cmd->se_cmd;
2925        uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2926        scsi_qla_host_t *vha = cmd->tgt->vha;
2927        struct qla_hw_data *ha = vha->hw;
2928        uint32_t t32 = 0;
2929
2930        /*
2931         * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2932         * have been immplemented by TCM, before AppTag is avail.
2933         * Look for modesense_handlers[]
2934         */
2935        ctx->app_tag = 0;
2936        ctx->app_tag_mask[0] = 0x0;
2937        ctx->app_tag_mask[1] = 0x0;
2938
2939        if (IS_PI_UNINIT_CAPABLE(ha)) {
2940                if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
2941                    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
2942                        *pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
2943                else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
2944                        *pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
2945        }
2946
2947        t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);
2948
2949        switch (se_cmd->prot_type) {
2950        case TARGET_DIF_TYPE0_PROT:
2951                /*
2952                 * No check for ql2xenablehba_err_chk, as it
2953                 * would be an I/O error if hba tag generation
2954                 * is not done.
2955                 */
2956                ctx->ref_tag = cpu_to_le32(lba);
2957                /* enable ALL bytes of the ref tag */
2958                ctx->ref_tag_mask[0] = 0xff;
2959                ctx->ref_tag_mask[1] = 0xff;
2960                ctx->ref_tag_mask[2] = 0xff;
2961                ctx->ref_tag_mask[3] = 0xff;
2962                break;
2963        case TARGET_DIF_TYPE1_PROT:
2964            /*
2965             * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
2966             * REF tag, and 16 bit app tag.
2967             */
2968            ctx->ref_tag = cpu_to_le32(lba);
2969            if (!qla_tgt_ref_mask_check(se_cmd) ||
2970                !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2971                    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2972                    break;
2973            }
2974            /* enable ALL bytes of the ref tag */
2975            ctx->ref_tag_mask[0] = 0xff;
2976            ctx->ref_tag_mask[1] = 0xff;
2977            ctx->ref_tag_mask[2] = 0xff;
2978            ctx->ref_tag_mask[3] = 0xff;
2979            break;
2980        case TARGET_DIF_TYPE2_PROT:
2981            /*
2982             * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
2983             * tag has to match LBA in CDB + N
2984             */
2985            ctx->ref_tag = cpu_to_le32(lba);
2986            if (!qla_tgt_ref_mask_check(se_cmd) ||
2987                !(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
2988                    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
2989                    break;
2990            }
2991            /* enable ALL bytes of the ref tag */
2992            ctx->ref_tag_mask[0] = 0xff;
2993            ctx->ref_tag_mask[1] = 0xff;
2994            ctx->ref_tag_mask[2] = 0xff;
2995            ctx->ref_tag_mask[3] = 0xff;
2996            break;
2997        case TARGET_DIF_TYPE3_PROT:
2998            /* For TYPE 3 protection: 16 bit GUARD only */
2999            *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
3000            ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
3001                ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
3002            break;
3003        }
3004}
3005
3006static inline int
3007qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
3008{
3009        struct dsd64            *cur_dsd;
3010        uint32_t                transfer_length = 0;
3011        uint32_t                data_bytes;
3012        uint32_t                dif_bytes;
3013        uint8_t                 bundling = 1;
3014        struct crc_context      *crc_ctx_pkt = NULL;
3015        struct qla_hw_data      *ha;
3016        struct ctio_crc2_to_fw  *pkt;
3017        dma_addr_t              crc_ctx_dma;
3018        uint16_t                fw_prot_opts = 0;
3019        struct qla_tgt_cmd      *cmd = prm->cmd;
3020        struct se_cmd           *se_cmd = &cmd->se_cmd;
3021        uint32_t h;
3022        struct atio_from_isp *atio = &prm->cmd->atio;
3023        struct qla_tc_param     tc;
3024        uint16_t t16;
3025        scsi_qla_host_t *vha = cmd->vha;
3026
3027        ha = vha->hw;
3028
3029        pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3030        prm->pkt = pkt;
3031        memset(pkt, 0, sizeof(*pkt));
3032
3033        ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3034                "qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3035                cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3036                prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);
3037
3038        if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
3039            (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
3040                bundling = 0;
3041
3042        /* Compute dif len and adjust data len to incude protection */
3043        data_bytes = cmd->bufflen;
3044        dif_bytes  = (data_bytes / cmd->blk_sz) * 8;
3045
3046        switch (se_cmd->prot_op) {
3047        case TARGET_PROT_DIN_INSERT:
3048        case TARGET_PROT_DOUT_STRIP:
3049                transfer_length = data_bytes;
3050                if (cmd->prot_sg_cnt)
3051                        data_bytes += dif_bytes;
3052                break;
3053        case TARGET_PROT_DIN_STRIP:
3054        case TARGET_PROT_DOUT_INSERT:
3055        case TARGET_PROT_DIN_PASS:
3056        case TARGET_PROT_DOUT_PASS:
3057                transfer_length = data_bytes + dif_bytes;
3058                break;
3059        default:
3060                BUG();
3061                break;
3062        }
3063
3064        if (!qlt_hba_err_chk_enabled(se_cmd))
3065                fw_prot_opts |= 0x10; /* Disable Guard tag checking */
3066        /* HBA error checking enabled */
3067        else if (IS_PI_UNINIT_CAPABLE(ha)) {
3068                if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
3069                    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
3070                        fw_prot_opts |= PO_DIS_VALD_APP_ESC;
3071                else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
3072                        fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
3073        }
3074
3075        switch (se_cmd->prot_op) {
3076        case TARGET_PROT_DIN_INSERT:
3077        case TARGET_PROT_DOUT_INSERT:
3078                fw_prot_opts |= PO_MODE_DIF_INSERT;
3079                break;
3080        case TARGET_PROT_DIN_STRIP:
3081        case TARGET_PROT_DOUT_STRIP:
3082                fw_prot_opts |= PO_MODE_DIF_REMOVE;
3083                break;
3084        case TARGET_PROT_DIN_PASS:
3085        case TARGET_PROT_DOUT_PASS:
3086                fw_prot_opts |= PO_MODE_DIF_PASS;
3087                /* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
3088                break;
3089        default:/* Normal Request */
3090                fw_prot_opts |= PO_MODE_DIF_PASS;
3091                break;
3092        }
3093
3094        /* ---- PKT ---- */
3095        /* Update entry type to indicate Command Type CRC_2 IOCB */
3096        pkt->entry_type  = CTIO_CRC2;
3097        pkt->entry_count = 1;
3098        pkt->vp_index = cmd->vp_idx;
3099
3100        h = qlt_make_handle(qpair);
3101        if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
3102                /*
3103                 * CTIO type 7 from the firmware doesn't provide a way to
3104                 * know the initiator's LOOP ID, hence we can't find
3105                 * the session and, so, the command.
3106                 */
3107                return -EAGAIN;
3108        } else
3109                qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3110
3111        pkt->handle  = make_handle(qpair->req->id, h);
3112        pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3113        pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3114        pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3115        pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3116        pkt->exchange_addr   = atio->u.isp24.exchange_addr;
3117
3118        /* silence compile warning */
3119        t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3120        pkt->ox_id  = cpu_to_le16(t16);
3121
3122        t16 = (atio->u.isp24.attr << 9);
3123        pkt->flags |= cpu_to_le16(t16);
3124        pkt->relative_offset = cpu_to_le32(prm->cmd->offset);
3125
3126        /* Set transfer direction */
3127        if (cmd->dma_data_direction == DMA_TO_DEVICE)
3128                pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3129        else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3130                pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3131
3132        pkt->dseg_count = cpu_to_le16(prm->tot_dsds);
3133        /* Fibre channel byte count */
3134        pkt->transfer_length = cpu_to_le32(transfer_length);
3135
3136        /* ----- CRC context -------- */
3137
3138        /* Allocate CRC context from global pool */
3139        crc_ctx_pkt = cmd->ctx =
3140            dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3141
3142        if (!crc_ctx_pkt)
3143                goto crc_queuing_error;
3144
3145        crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
3146        INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
3147
3148        /* Set handle */
3149        crc_ctx_pkt->handle = pkt->handle;
3150
3151        qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3152
3153        put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3154        pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
3155
3156        if (!bundling) {
3157                cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3158        } else {
3159                /*
3160                 * Configure Bundling if we need to fetch interlaving
3161                 * protection PCI accesses
3162                 */
3163                fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
3164                crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
3165                crc_ctx_pkt->u.bundling.dseg_count =
3166                        cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3167                cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3168        }
3169
3170        /* Finish the common fields of CRC pkt */
3171        crc_ctx_pkt->blk_size   = cpu_to_le16(cmd->blk_sz);
3172        crc_ctx_pkt->prot_opts  = cpu_to_le16(fw_prot_opts);
3173        crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3174        crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3175
3176        memset((uint8_t *)&tc, 0 , sizeof(tc));
3177        tc.vha = vha;
3178        tc.blk_sz = cmd->blk_sz;
3179        tc.bufflen = cmd->bufflen;
3180        tc.sg = cmd->sg;
3181        tc.prot_sg = cmd->prot_sg;
3182        tc.ctx = crc_ctx_pkt;
3183        tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3184
3185        /* Walks data segments */
3186        pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3187
3188        if (!bundling && prm->prot_seg_cnt) {
3189                if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3190                        prm->tot_dsds, &tc))
3191                        goto crc_queuing_error;
3192        } else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3193                (prm->tot_dsds - prm->prot_seg_cnt), &tc))
3194                goto crc_queuing_error;
3195
3196        if (bundling && prm->prot_seg_cnt) {
3197                /* Walks dif segments */
3198                pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3199
3200                cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3201                if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3202                        prm->prot_seg_cnt, cmd))
3203                        goto crc_queuing_error;
3204        }
3205        return QLA_SUCCESS;
3206
3207crc_queuing_error:
3208        /* Cleanup will be performed by the caller */
3209        qpair->req->outstanding_cmds[h] = NULL;
3210
3211        return QLA_FUNCTION_FAILED;
3212}
3213
3214/*
3215 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
3216 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
3217 */
3218int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
3219        uint8_t scsi_status)
3220{
3221        struct scsi_qla_host *vha = cmd->vha;
3222        struct qla_qpair *qpair = cmd->qpair;
3223        struct ctio7_to_24xx *pkt;
3224        struct qla_tgt_prm prm;
3225        uint32_t full_req_cnt = 0;
3226        unsigned long flags = 0;
3227        int res;
3228
3229        if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3230            (cmd->sess && cmd->sess->deleted)) {
3231                cmd->state = QLA_TGT_STATE_PROCESSED;
3232                return 0;
3233        }
3234
3235        ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3236            "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3237            (xmit_type & QLA_TGT_XMIT_STATUS) ?
3238            1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3239            &cmd->se_cmd, qpair->id);
3240
3241        res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
3242            &full_req_cnt);
3243        if (unlikely(res != 0)) {
3244                return res;
3245        }
3246
3247        spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3248
3249        if (xmit_type == QLA_TGT_XMIT_STATUS)
3250                qpair->tgt_counters.core_qla_snd_status++;
3251        else
3252                qpair->tgt_counters.core_qla_que_buf++;
3253
3254        if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3255                /*
3256                 * Either the port is not online or this request was from
3257                 * previous life, just abort the processing.
3258                 */
3259                cmd->state = QLA_TGT_STATE_PROCESSED;
3260                ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3261                        "RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
3262                        vha->flags.online, qla2x00_reset_active(vha),
3263                        cmd->reset_count, qpair->chip_reset);
3264                spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3265                return 0;
3266        }
3267
3268        /* Does F/W have an IOCBs for this request */
3269        res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3270        if (unlikely(res))
3271                goto out_unmap_unlock;
3272
3273        if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3274                res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3275        else
3276                res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3277        if (unlikely(res != 0)) {
3278                qpair->req->cnt += full_req_cnt;
3279                goto out_unmap_unlock;
3280        }
3281
3282        pkt = (struct ctio7_to_24xx *)prm.pkt;
3283
3284        if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
3285                pkt->u.status0.flags |=
3286                    cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3287                        CTIO7_FLAGS_STATUS_MODE_0);
3288
3289                if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3290                        qlt_load_data_segments(&prm);
3291
3292                if (prm.add_status_pkt == 0) {
3293                        if (xmit_type & QLA_TGT_XMIT_STATUS) {
3294                                pkt->u.status0.scsi_status =
3295                                    cpu_to_le16(prm.rq_result);
3296                                pkt->u.status0.residual =
3297                                    cpu_to_le32(prm.residual);
3298                                pkt->u.status0.flags |= cpu_to_le16(
3299                                    CTIO7_FLAGS_SEND_STATUS);
3300                                if (qlt_need_explicit_conf(cmd, 0)) {
3301                                        pkt->u.status0.flags |=
3302                                            cpu_to_le16(
3303                                                CTIO7_FLAGS_EXPLICIT_CONFORM |
3304                                                CTIO7_FLAGS_CONFORM_REQ);
3305                                }
3306                        }
3307
3308                } else {
3309                        /*
3310                         * We have already made sure that there is sufficient
3311                         * amount of request entries to not drop HW lock in
3312                         * req_pkt().
3313                         */
3314                        struct ctio7_to_24xx *ctio =
3315                                (struct ctio7_to_24xx *)qlt_get_req_pkt(
3316                                    qpair->req);
3317
3318                        ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3319                            "Building additional status packet 0x%p.\n",
3320                            ctio);
3321
3322                        /*
3323                         * T10Dif: ctio_crc2_to_fw overlay ontop of
3324                         * ctio7_to_24xx
3325                         */
3326                        memcpy(ctio, pkt, sizeof(*ctio));
3327                        /* reset back to CTIO7 */
3328                        ctio->entry_count = 1;
3329                        ctio->entry_type = CTIO_TYPE7;
3330                        ctio->dseg_count = 0;
3331                        ctio->u.status1.flags &= ~cpu_to_le16(
3332                            CTIO7_FLAGS_DATA_IN);
3333
3334                        /* Real finish is ctio_m1's finish */
3335                        pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3336                        pkt->u.status0.flags |= cpu_to_le16(
3337                            CTIO7_FLAGS_DONT_RET_CTIO);
3338
3339                        /* qlt_24xx_init_ctio_to_isp will correct
3340                         * all neccessary fields that's part of CTIO7.
3341                         * There should be no residual of CTIO-CRC2 data.
3342                         */
3343                        qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
3344                            &prm);
3345                }
3346        } else
3347                qlt_24xx_init_ctio_to_isp(pkt, &prm);
3348
3349
3350        cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3351        cmd->cmd_sent_to_fw = 1;
3352        cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3353
3354        /* Memory Barrier */
3355        wmb();
3356        if (qpair->reqq_start_iocbs)
3357                qpair->reqq_start_iocbs(qpair);
3358        else
3359                qla2x00_start_iocbs(vha, qpair->req);
3360        spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3361
3362        return 0;
3363
3364out_unmap_unlock:
3365        qlt_unmap_sg(vha, cmd);
3366        spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3367
3368        return res;
3369}
3370EXPORT_SYMBOL(qlt_xmit_response);
3371
3372int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
3373{
3374        struct ctio7_to_24xx *pkt;
3375        struct scsi_qla_host *vha = cmd->vha;
3376        struct qla_tgt *tgt = cmd->tgt;
3377        struct qla_tgt_prm prm;
3378        unsigned long flags = 0;
3379        int res = 0;
3380        struct qla_qpair *qpair = cmd->qpair;
3381
3382        memset(&prm, 0, sizeof(prm));
3383        prm.cmd = cmd;
3384        prm.tgt = tgt;
3385        prm.sg = NULL;
3386        prm.req_cnt = 1;
3387
3388        /* Calculate number of entries and segments required */
3389        if (qlt_pci_map_calc_cnt(&prm) != 0)
3390                return -EAGAIN;
3391
3392        if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3393            (cmd->sess && cmd->sess->deleted)) {
3394                /*
3395                 * Either the port is not online or this request was from
3396                 * previous life, just abort the processing.
3397                 */
3398                cmd->aborted = 1;
3399                cmd->write_data_transferred = 0;
3400                cmd->state = QLA_TGT_STATE_DATA_IN;
3401                vha->hw->tgt.tgt_ops->handle_data(cmd);
3402                ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3403                        "RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
3404                        vha->flags.online, qla2x00_reset_active(vha),
3405                        cmd->reset_count, qpair->chip_reset);
3406                return 0;
3407        }
3408
3409        spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3410        /* Does F/W have an IOCBs for this request */
3411        res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3412        if (res != 0)
3413                goto out_unlock_free_unmap;
3414        if (cmd->se_cmd.prot_op)
3415                res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3416        else
3417                res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3418
3419        if (unlikely(res != 0)) {
3420                qpair->req->cnt += prm.req_cnt;
3421                goto out_unlock_free_unmap;
3422        }
3423
3424        pkt = (struct ctio7_to_24xx *)prm.pkt;
3425        pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3426            CTIO7_FLAGS_STATUS_MODE_0);
3427
3428        if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3429                qlt_load_data_segments(&prm);
3430
3431        cmd->state = QLA_TGT_STATE_NEED_DATA;
3432        cmd->cmd_sent_to_fw = 1;
3433        cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3434
3435        /* Memory Barrier */
3436        wmb();
3437        if (qpair->reqq_start_iocbs)
3438                qpair->reqq_start_iocbs(qpair);
3439        else
3440                qla2x00_start_iocbs(vha, qpair->req);
3441        spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3442
3443        return res;
3444
3445out_unlock_free_unmap:
3446        qlt_unmap_sg(vha, cmd);
3447        spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3448
3449        return res;
3450}
3451EXPORT_SYMBOL(qlt_rdy_to_xfer);
3452
3453
3454/*
3455 * it is assumed either hardware_lock or qpair lock is held.
3456 */
3457static void
3458qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3459        struct ctio_crc_from_fw *sts)
3460{
3461        uint8_t         *ap = &sts->actual_dif[0];
3462        uint8_t         *ep = &sts->expected_dif[0];
3463        uint64_t        lba = cmd->se_cmd.t_task_lba;
3464        uint8_t scsi_status, sense_key, asc, ascq;
3465        unsigned long flags;
3466        struct scsi_qla_host *vha = cmd->vha;
3467
3468        cmd->trc_flags |= TRC_DIF_ERR;
3469
3470        cmd->a_guard   = get_unaligned_be16(ap + 0);
3471        cmd->a_app_tag = get_unaligned_be16(ap + 2);
3472        cmd->a_ref_tag = get_unaligned_be32(ap + 4);
3473
3474        cmd->e_guard   = get_unaligned_be16(ep + 0);
3475        cmd->e_app_tag = get_unaligned_be16(ep + 2);
3476        cmd->e_ref_tag = get_unaligned_be32(ep + 4);
3477
3478        ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
3479            "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3480
3481        scsi_status = sense_key = asc = ascq = 0;
3482
3483        /* check appl tag */
3484        if (cmd->e_app_tag != cmd->a_app_tag) {
3485                ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
3486                    "App Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3487                    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3488                    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3489                    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3490                    cmd->atio.u.isp24.fcp_hdr.ox_id);
3491
3492                cmd->dif_err_code = DIF_ERR_APP;
3493                scsi_status = SAM_STAT_CHECK_CONDITION;
3494                sense_key = ABORTED_COMMAND;
3495                asc = 0x10;
3496                ascq = 0x2;
3497        }
3498
3499        /* check ref tag */
3500        if (cmd->e_ref_tag != cmd->a_ref_tag) {
3501                ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
3502                    "Ref Tag ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard[%x|%x] cmd=%p ox_id[%04x] ",
3503                    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3504                    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3505                    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3506                    cmd->atio.u.isp24.fcp_hdr.ox_id);
3507
3508                cmd->dif_err_code = DIF_ERR_REF;
3509                scsi_status = SAM_STAT_CHECK_CONDITION;
3510                sense_key = ABORTED_COMMAND;
3511                asc = 0x10;
3512                ascq = 0x3;
3513                goto out;
3514        }
3515
3516        /* check guard */
3517        if (cmd->e_guard != cmd->a_guard) {
3518                ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
3519                    "Guard ERR: cdb[%x] lba[%llx %llx] blks[%x] [Actual|Expected] Ref[%x|%x], App[%x|%x], Guard [%x|%x] cmd=%p ox_id[%04x]",
3520                    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
3521                    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
3522                    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
3523                    cmd->atio.u.isp24.fcp_hdr.ox_id);
3524
3525                cmd->dif_err_code = DIF_ERR_GRD;
3526                scsi_status = SAM_STAT_CHECK_CONDITION;
3527                sense_key = ABORTED_COMMAND;
3528                asc = 0x10;
3529                ascq = 0x1;
3530        }
3531out:
3532        switch (cmd->state) {
3533        case QLA_TGT_STATE_NEED_DATA:
3534                /* handle_data will load DIF error code  */
3535                cmd->state = QLA_TGT_STATE_DATA_IN;
3536                vha->hw->tgt.tgt_ops->handle_data(cmd);
3537                break;
3538        default:
3539                spin_lock_irqsave(&cmd->cmd_lock, flags);
3540                if (cmd->aborted) {
3541                        spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3542                        vha->hw->tgt.tgt_ops->free_cmd(cmd);
3543                        break;
3544                }
3545                spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3546
3547                qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
3548                    ascq);
3549                /* assume scsi status gets out on the wire.
3550                 * Will not wait for completion.
3551                 */
3552                vha->hw->tgt.tgt_ops->free_cmd(cmd);
3553                break;
3554        }
3555}
3556
3557/* If hardware_lock held on entry, might drop it, then reaquire */
3558/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
3559static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3560        struct imm_ntfy_from_isp *ntfy)
3561{
3562        struct nack_to_isp *nack;
3563        struct qla_hw_data *ha = vha->hw;
3564        request_t *pkt;
3565        int ret = 0;
3566
3567        ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
3568            "Sending TERM ELS CTIO (ha=%p)\n", ha);
3569
3570        pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3571        if (pkt == NULL) {
3572                ql_dbg(ql_dbg_tgt, vha, 0xe080,
3573                    "qla_target(%d): %s failed: unable to allocate "
3574                    "request packet\n", vha->vp_idx, __func__);
3575                return -ENOMEM;
3576        }
3577
3578        pkt->entry_type = NOTIFY_ACK_TYPE;
3579        pkt->entry_count = 1;
3580        pkt->handle = QLA_TGT_SKIP_HANDLE;
3581
3582        nack = (struct nack_to_isp *)pkt;
3583        nack->ox_id = ntfy->ox_id;
3584
3585        nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3586        if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3587                nack->u.isp24.flags = ntfy->u.isp24.flags &
3588                        cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3589        }
3590
3591        /* terminate */
3592        nack->u.isp24.flags |=
3593                __constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);
3594
3595        nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3596        nack->u.isp24.status = ntfy->u.isp24.status;
3597        nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3598        nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3599        nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3600        nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3601        nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3602        nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3603
3604        qla2x00_start_iocbs(vha, vha->req);
3605        return ret;
3606}
3607
3608static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
3609        struct imm_ntfy_from_isp *imm, int ha_locked)
3610{
3611        int rc;
3612
3613        WARN_ON_ONCE(!ha_locked);
3614        rc = __qlt_send_term_imm_notif(vha, imm);
3615        pr_debug("rc = %d\n", rc);
3616}
3617
3618/*
3619 * If hardware_lock held on entry, might drop it, then reaquire
3620 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
3621 */
3622static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3623        struct qla_tgt_cmd *cmd,
3624        struct atio_from_isp *atio)
3625{
3626        struct scsi_qla_host *vha = qpair->vha;
3627        struct ctio7_to_24xx *ctio24;
3628        struct qla_hw_data *ha = vha->hw;
3629        request_t *pkt;
3630        int ret = 0;
3631        uint16_t temp;
3632
3633        ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3634
3635        if (cmd)
3636                vha = cmd->vha;
3637
3638        pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3639        if (pkt == NULL) {
3640                ql_dbg(ql_dbg_tgt, vha, 0xe050,
3641                    "qla_target(%d): %s failed: unable to allocate "
3642                    "request packet\n", vha->vp_idx, __func__);
3643                return -ENOMEM;
3644        }
3645
3646        if (cmd != NULL) {
3647                if (cmd->state < QLA_TGT_STATE_PROCESSED) {
3648                        ql_dbg(ql_dbg_tgt, vha, 0xe051,
3649                            "qla_target(%d): Terminating cmd %p with "
3650                            "incorrect state %d\n", vha->vp_idx, cmd,
3651                            cmd->state);
3652                } else
3653                        ret = 1;
3654        }
3655
3656        qpair->tgt_counters.num_term_xchg_sent++;
3657        pkt->entry_count = 1;
3658        pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
3659
3660        ctio24 = (struct ctio7_to_24xx *)pkt;
3661        ctio24->entry_type = CTIO_TYPE7;
3662        ctio24->nport_handle = cpu_to_le16(CTIO7_NHANDLE_UNRECOGNIZED);
3663        ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3664        ctio24->vp_index = vha->vp_idx;
3665        ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3666        ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3667        temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
3668                CTIO7_FLAGS_TERMINATE;
3669        ctio24->u.status1.flags = cpu_to_le16(temp);
3670        temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
3671        ctio24->u.status1.ox_id = cpu_to_le16(temp);
3672
3673        /* Memory Barrier */
3674        wmb();
3675        if (qpair->reqq_start_iocbs)
3676                qpair->reqq_start_iocbs(qpair);
3677        else
3678                qla2x00_start_iocbs(vha, qpair->req);
3679        return ret;
3680}
3681
3682static void qlt_send_term_exchange(struct qla_qpair *qpair,
3683        struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
3684        int ul_abort)
3685{
3686        struct scsi_qla_host *vha;
3687        unsigned long flags = 0;
3688        int rc;
3689
3690        /* why use different vha? NPIV */
3691        if (cmd)
3692                vha = cmd->vha;
3693        else
3694                vha = qpair->vha;
3695
3696        if (ha_locked) {
3697                rc = __qlt_send_term_exchange(qpair, cmd, atio);
3698                if (rc == -ENOMEM)
3699                        qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3700                goto done;
3701        }
3702        spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3703        rc = __qlt_send_term_exchange(qpair, cmd, atio);
3704        if (rc == -ENOMEM)
3705                qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3706
3707done:
3708        if (cmd && !ul_abort && !cmd->aborted) {
3709                if (cmd->sg_mapped)
3710                        qlt_unmap_sg(vha, cmd);
3711                vha->hw->tgt.tgt_ops->free_cmd(cmd);
3712        }
3713
3714        if (!ha_locked)
3715                spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3716
3717        return;
3718}
3719
3720static void qlt_init_term_exchange(struct scsi_qla_host *vha)
3721{
3722        struct list_head free_list;
3723        struct qla_tgt_cmd *cmd, *tcmd;
3724
3725        vha->hw->tgt.leak_exchg_thresh_hold =
3726            (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3727
3728        cmd = tcmd = NULL;
3729        if (!list_empty(&vha->hw->tgt.q_full_list)) {
3730                INIT_LIST_HEAD(&free_list);
3731                list_splice_init(&vha->hw->tgt.q_full_list, &free_list);
3732
3733                list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
3734                        list_del(&cmd->cmd_list);
3735                        /* This cmd was never sent to TCM.  There is no need
3736                         * to schedule free or call free_cmd
3737                         */
3738                        qlt_free_cmd(cmd);
3739                        vha->hw->tgt.num_qfull_cmds_alloc--;
3740                }