qla_target.c 196.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13
/*
 *  qla_target.c SCSI LLD infrastructure for QLogic 22xx/23xx/24xx/25xx
 *
 *  based on qla2x00t.c code:
 *
 *  Copyright (C) 2004 - 2010 Vladislav Bolkhovitin <vst@vlnb.net>
 *  Copyright (C) 2004 - 2005 Leonid Stoljar
 *  Copyright (C) 2006 Nathaniel Clark <nate@misrule.us>
 *  Copyright (C) 2006 - 2010 ID7 Ltd.
 *
 *  Forward port and refactoring to modern qla2xxx and target/configfs
 *
14
 *  Copyright (C) 2010-2013 Nicholas A. Bellinger <nab@kernel.org>
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/blkdev.h>
#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/list.h>
#include <linux/workqueue.h>
#include <asm/unaligned.h>
#include <scsi/scsi.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_tcq.h>
#include <target/target_core_base.h>
#include <target/target_core_fabric.h>

#include "qla_def.h"
#include "qla_target.h"

36 37 38 39 40
static int ql2xtgt_tape_enable;
module_param(ql2xtgt_tape_enable, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(ql2xtgt_tape_enable,
		"Enables Sequence level error recovery (aka FC Tape). Default is 0 - no SLER. 1 - Enable SLER.");

41 42 43 44 45 46 47 48
static char *qlini_mode = QLA2XXX_INI_MODE_STR_ENABLED;
module_param(qlini_mode, charp, S_IRUGO);
MODULE_PARM_DESC(qlini_mode,
	"Determines when initiator mode will be enabled. Possible values: "
	"\"exclusive\" - initiator mode will be enabled on load, "
	"disabled on enabling target mode and then on disabling target mode "
	"enabled back; "
	"\"disabled\" - initiator mode will never be enabled; "
49 50
	"\"dual\" - Initiator Modes will be enabled. Target Mode can be activated "
	"when ready "
51 52
	"\"enabled\" (default) - initiator mode will always stay enabled.");

53
static int ql_dm_tgt_ex_pct = 0;
54 55 56 57 58 59
module_param(ql_dm_tgt_ex_pct, int, S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(ql_dm_tgt_ex_pct,
	"For Dual Mode (qlini_mode=dual), this parameter determines "
	"the percentage of exchanges/cmds FW will allocate resources "
	"for Target mode.");

60 61 62 63 64 65 66
int ql2xuctrlirq = 1;
module_param(ql2xuctrlirq, int, 0644);
MODULE_PARM_DESC(ql2xuctrlirq,
    "User to control IRQ placement via smp_affinity."
    "Valid with qlini_mode=disabled."
    "1(default): enable");

67
int ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
68

69 70
static int qla_sam_status = SAM_STAT_BUSY;
static int tc_sam_status = SAM_STAT_TASK_SET_FULL; /* target core */
71

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
/*
 * From scsi/fc/fc_fcp.h
 */
enum fcp_resp_rsp_codes {
	FCP_TMF_CMPL = 0,
	FCP_DATA_LEN_INVALID = 1,
	FCP_CMND_FIELDS_INVALID = 2,
	FCP_DATA_PARAM_MISMATCH = 3,
	FCP_TMF_REJECTED = 4,
	FCP_TMF_FAILED = 5,
	FCP_TMF_INVALID_LUN = 9,
};

/*
 * fc_pri_ta from scsi/fc/fc_fcp.h
 */
#define FCP_PTA_SIMPLE      0   /* simple task attribute */
#define FCP_PTA_HEADQ       1   /* head of queue task attribute */
#define FCP_PTA_ORDERED     2   /* ordered task attribute */
91
#define FCP_PTA_ACA         4   /* auto. contingent allegiance */
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#define FCP_PTA_MASK        7   /* mask for task attribute field */
#define FCP_PRI_SHIFT       3   /* priority field starts in bit 3 */
#define FCP_PRI_RESVD_MASK  0x80        /* reserved bits in priority field */

/*
 * This driver calls qla2x00_alloc_iocbs() and qla2x00_issue_marker(), which
 * must be called under HW lock and could unlock/lock it inside.
 * It isn't an issue, since in the current implementation on the time when
 * those functions are called:
 *
 *   - Either context is IRQ and only IRQ handler can modify HW data,
 *     including rings related fields,
 *
 *   - Or access to target mode variables from struct qla_tgt doesn't
 *     cross those functions boundaries, except tgt_stop, which
 *     additionally protected by irq_cmd_count.
 */
/* Predefs for callbacks handed to qla2xxx LLD */
static void qlt_24xx_atio_pkt(struct scsi_qla_host *ha,
111
	struct atio_from_isp *pkt, uint8_t);
112 113
static void qlt_response_pkt(struct scsi_qla_host *ha, struct rsp_que *rsp,
	response_t *pkt);
114
static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
115
	int fn, void *iocb, int flags);
116
static void qlt_send_term_exchange(struct qla_qpair *, struct qla_tgt_cmd
117
	*cmd, struct atio_from_isp *atio, int ha_locked, int ul_abort);
118 119
static void qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
	struct atio_from_isp *atio, uint16_t status, int qfull);
120
static void qlt_disable_vha(struct scsi_qla_host *vha);
121
static void qlt_clear_tgt_db(struct qla_tgt *tgt);
122
static void qlt_send_notify_ack(struct qla_qpair *qpair,
123 124 125
	struct imm_ntfy_from_isp *ntfy,
	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan);
126 127
static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
	struct imm_ntfy_from_isp *imm, int ha_locked);
128 129 130
static struct fc_port *qlt_create_sess(struct scsi_qla_host *vha,
	fc_port_t *fcport, bool local);
void qlt_unreg_sess(struct fc_port *sess);
131 132
static void qlt_24xx_handle_abts(struct scsi_qla_host *,
	struct abts_recv_from_24xx *);
133 134
static void qlt_send_busy(struct qla_qpair *, struct atio_from_isp *,
    uint16_t);
135 136
static int qlt_check_reserve_free_req(struct qla_qpair *qpair, uint32_t);
static inline uint32_t qlt_make_handle(struct qla_qpair *);
137

138 139 140 141
/*
 * Global Variables
 */
static struct kmem_cache *qla_tgt_mgmt_cmd_cachep;
142
struct kmem_cache *qla_tgt_plogi_cachep;
143 144 145 146 147
static mempool_t *qla_tgt_mgmt_cmd_mempool;
static struct workqueue_struct *qla_tgt_wq;
static DEFINE_MUTEX(qla_tgt_mutex);
static LIST_HEAD(qla_tgt_glist);

148 149 150 151 152 153 154 155 156 157 158 159 160 161
static const char *prot_op_str(u32 prot_op)
{
	switch (prot_op) {
	case TARGET_PROT_NORMAL:	return "NORMAL";
	case TARGET_PROT_DIN_INSERT:	return "DIN_INSERT";
	case TARGET_PROT_DOUT_INSERT:	return "DOUT_INSERT";
	case TARGET_PROT_DIN_STRIP:	return "DIN_STRIP";
	case TARGET_PROT_DOUT_STRIP:	return "DOUT_STRIP";
	case TARGET_PROT_DIN_PASS:	return "DIN_PASS";
	case TARGET_PROT_DOUT_PASS:	return "DOUT_PASS";
	default:			return "UNKNOWN";
	}
}

162 163 164 165 166 167 168 169 170 171
/* This API intentionally takes dest as a parameter, rather than returning
 * int value to avoid caller forgetting to issue wmb() after the store */
void qlt_do_generation_tick(struct scsi_qla_host *vha, int *dest)
{
	scsi_qla_host_t *base_vha = pci_get_drvdata(vha->hw->pdev);
	*dest = atomic_inc_return(&base_vha->generation_tick);
	/* memory barrier */
	wmb();
}

172 173 174 175 176 177
/* Might release hw lock, then reaquire!! */
static inline int qlt_issue_marker(struct scsi_qla_host *vha, int vha_locked)
{
	/* Send marker if required */
	if (unlikely(vha->marker_needed != 0)) {
		int rc = qla2x00_issue_marker(vha, vha_locked);
178

179 180 181 182 183 184 185 186 187 188 189 190
		if (rc != QLA_SUCCESS) {
			ql_dbg(ql_dbg_tgt, vha, 0xe03d,
			    "qla_target(%d): issue_marker() failed\n",
			    vha->vp_idx);
		}
		return rc;
	}
	return QLA_SUCCESS;
}

static inline
struct scsi_qla_host *qlt_find_host_by_d_id(struct scsi_qla_host *vha,
191
					    be_id_t d_id)
192
{
193
	struct scsi_qla_host *host;
194
	uint32_t key;
195

196 197 198
	if (vha->d_id.b.area == d_id.area &&
	    vha->d_id.b.domain == d_id.domain &&
	    vha->d_id.b.al_pa == d_id.al_pa)
199 200
		return vha;

201
	key = be_to_port_id(d_id).b24;
202

203 204
	host = btree_lookup32(&vha->hw->tgt.host_map, key);
	if (!host)
205
		ql_dbg(ql_dbg_tgt_mgt + ql_dbg_verbose, vha, 0xf005,
206
		    "Unable to find host %06x\n", key);
207 208

	return host;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
}

static inline
struct scsi_qla_host *qlt_find_host_by_vp_idx(struct scsi_qla_host *vha,
	uint16_t vp_idx)
{
	struct qla_hw_data *ha = vha->hw;

	if (vha->vp_idx == vp_idx)
		return vha;

	BUG_ON(ha->tgt.tgt_vp_map == NULL);
	if (likely(test_bit(vp_idx, ha->vp_idx_map)))
		return ha->tgt.tgt_vp_map[vp_idx].vha;

	return NULL;
}

227 228 229 230 231 232 233
static inline void qlt_incr_num_pend_cmds(struct scsi_qla_host *vha)
{
	unsigned long flags;

	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);

	vha->hw->tgt.num_pend_cmds++;
234 235
	if (vha->hw->tgt.num_pend_cmds > vha->qla_stats.stat_max_pend_cmds)
		vha->qla_stats.stat_max_pend_cmds =
236 237 238 239 240 241 242 243 244 245 246 247
			vha->hw->tgt.num_pend_cmds;
	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
}
static inline void qlt_decr_num_pend_cmds(struct scsi_qla_host *vha)
{
	unsigned long flags;

	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
	vha->hw->tgt.num_pend_cmds--;
	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
}

248 249

static void qlt_queue_unknown_atio(scsi_qla_host_t *vha,
250
	struct atio_from_isp *atio, uint8_t ha_locked)
251 252 253 254 255 256
{
	struct qla_tgt_sess_op *u;
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
	unsigned long flags;

	if (tgt->tgt_stop) {
257 258 259
		ql_dbg(ql_dbg_async, vha, 0x502c,
		    "qla_target(%d): dropping unknown ATIO_TYPE7, because tgt is being stopped",
		    vha->vp_idx);
260 261 262 263
		goto out_term;
	}

	u = kzalloc(sizeof(*u), GFP_ATOMIC);
264
	if (u == NULL)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		goto out_term;

	u->vha = vha;
	memcpy(&u->atio, atio, sizeof(*atio));
	INIT_LIST_HEAD(&u->cmd_list);

	spin_lock_irqsave(&vha->cmd_list_lock, flags);
	list_add_tail(&u->cmd_list, &vha->unknown_atio_list);
	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);

	schedule_delayed_work(&vha->unknown_atio_work, 1);

out:
	return;

out_term:
281
	qlt_send_term_exchange(vha->hw->base_qpair, NULL, atio, ha_locked, 0);
282 283 284 285 286 287 288 289 290 291 292 293 294 295
	goto out;
}

static void qlt_try_to_dequeue_unknown_atios(struct scsi_qla_host *vha,
	uint8_t ha_locked)
{
	struct qla_tgt_sess_op *u, *t;
	scsi_qla_host_t *host;
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
	unsigned long flags;
	uint8_t queued = 0;

	list_for_each_entry_safe(u, t, &vha->unknown_atio_list, cmd_list) {
		if (u->aborted) {
296 297
			ql_dbg(ql_dbg_async, vha, 0x502e,
			    "Freeing unknown %s %p, because of Abort\n",
298
			    "ATIO_TYPE7", u);
299 300
			qlt_send_term_exchange(vha->hw->base_qpair, NULL,
			    &u->atio, ha_locked, 0);
301 302 303 304 305
			goto abort;
		}

		host = qlt_find_host_by_d_id(vha, u->atio.u.isp24.fcp_hdr.d_id);
		if (host != NULL) {
306
			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x502f,
307
			    "Requeuing unknown ATIO_TYPE7 %p\n", u);
308 309
			qlt_24xx_atio_pkt(host, &u->atio, ha_locked);
		} else if (tgt->tgt_stop) {
310
			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503a,
311 312
			    "Freeing unknown %s %p, because tgt is being stopped\n",
			    "ATIO_TYPE7", u);
313 314
			qlt_send_term_exchange(vha->hw->base_qpair, NULL,
			    &u->atio, ha_locked, 0);
315
		} else {
316
			ql_dbg(ql_dbg_async + ql_dbg_verbose, vha, 0x503d,
317
			    "Reschedule u %p, vha %p, host %p\n", u, vha, host);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
			if (!queued) {
				queued = 1;
				schedule_delayed_work(&vha->unknown_atio_work,
				    1);
			}
			continue;
		}

abort:
		spin_lock_irqsave(&vha->cmd_list_lock, flags);
		list_del(&u->cmd_list);
		spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
		kfree(u);
	}
}

void qlt_unknown_atio_work_fn(struct work_struct *work)
{
	struct scsi_qla_host *vha = container_of(to_delayed_work(work),
	    struct scsi_qla_host, unknown_atio_work);

	qlt_try_to_dequeue_unknown_atios(vha, 0);
}

342
static bool qlt_24xx_atio_pkt_all_vps(struct scsi_qla_host *vha,
343
	struct atio_from_isp *atio, uint8_t ha_locked)
344
{
345 346 347 348 349
	ql_dbg(ql_dbg_tgt, vha, 0xe072,
		"%s: qla_target(%d): type %x ox_id %04x\n",
		__func__, vha->vp_idx, atio->u.raw.entry_type,
		be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));

350 351 352 353 354 355 356 357 358
	switch (atio->u.raw.entry_type) {
	case ATIO_TYPE7:
	{
		struct scsi_qla_host *host = qlt_find_host_by_d_id(vha,
		    atio->u.isp24.fcp_hdr.d_id);
		if (unlikely(NULL == host)) {
			ql_dbg(ql_dbg_tgt, vha, 0xe03e,
			    "qla_target(%d): Received ATIO_TYPE7 "
			    "with unknown d_id %x:%x:%x\n", vha->vp_idx,
359 360 361
			    atio->u.isp24.fcp_hdr.d_id.domain,
			    atio->u.isp24.fcp_hdr.d_id.area,
			    atio->u.isp24.fcp_hdr.d_id.al_pa);
362 363 364


			qlt_queue_unknown_atio(vha, atio, ha_locked);
365 366
			break;
		}
367 368 369
		if (unlikely(!list_empty(&vha->unknown_atio_list)))
			qlt_try_to_dequeue_unknown_atios(vha, ha_locked);

370
		qlt_24xx_atio_pkt(host, atio, ha_locked);
371 372 373 374 375 376 377 378 379
		break;
	}

	case IMMED_NOTIFY_TYPE:
	{
		struct scsi_qla_host *host = vha;
		struct imm_ntfy_from_isp *entry =
		    (struct imm_ntfy_from_isp *)atio;

380 381
		qlt_issue_marker(vha, ha_locked);

382 383 384 385 386 387 388 389 390 391 392 393 394
		if ((entry->u.isp24.vp_index != 0xFF) &&
		    (entry->u.isp24.nport_handle != 0xFFFF)) {
			host = qlt_find_host_by_vp_idx(vha,
			    entry->u.isp24.vp_index);
			if (unlikely(!host)) {
				ql_dbg(ql_dbg_tgt, vha, 0xe03f,
				    "qla_target(%d): Received "
				    "ATIO (IMMED_NOTIFY_TYPE) "
				    "with unknown vp_index %d\n",
				    vha->vp_idx, entry->u.isp24.vp_index);
				break;
			}
		}
395
		qlt_24xx_atio_pkt(host, atio, ha_locked);
396 397 398
		break;
	}

399 400 401 402 403 404 405 406 407 408 409
	case VP_RPT_ID_IOCB_TYPE:
		qla24xx_report_id_acquisition(vha,
			(struct vp_rpt_id_entry_24xx *)atio);
		break;

	case ABTS_RECV_24XX:
	{
		struct abts_recv_from_24xx *entry =
			(struct abts_recv_from_24xx *)atio;
		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
			entry->vp_index);
410 411
		unsigned long flags;

412
		if (unlikely(!host)) {
413
			ql_dbg(ql_dbg_tgt, vha, 0xe00a,
414 415 416 417 418
			    "qla_target(%d): Response pkt (ABTS_RECV_24XX) "
			    "received, with unknown vp_index %d\n",
			    vha->vp_idx, entry->vp_index);
			break;
		}
419 420 421 422 423
		if (!ha_locked)
			spin_lock_irqsave(&host->hw->hardware_lock, flags);
		qlt_24xx_handle_abts(host, (struct abts_recv_from_24xx *)atio);
		if (!ha_locked)
			spin_unlock_irqrestore(&host->hw->hardware_lock, flags);
424 425 426 427 428
		break;
	}

	/* case PUREX_IOCB_TYPE: ql2xmvasynctoatio */

429 430 431 432 433 434 435
	default:
		ql_dbg(ql_dbg_tgt, vha, 0xe040,
		    "qla_target(%d): Received unknown ATIO atio "
		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
		break;
	}

436
	return false;
437 438
}

439 440
void qlt_response_pkt_all_vps(struct scsi_qla_host *vha,
	struct rsp_que *rsp, response_t *pkt)
441 442
{
	switch (pkt->entry_type) {
443 444 445 446
	case CTIO_CRC2:
		ql_dbg(ql_dbg_tgt, vha, 0xe073,
			"qla_target(%d):%s: CRC2 Response pkt\n",
			vha->vp_idx, __func__);
447
		/* fall through */
448 449 450 451 452 453 454 455 456 457 458 459
	case CTIO_TYPE7:
	{
		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
		    entry->vp_index);
		if (unlikely(!host)) {
			ql_dbg(ql_dbg_tgt, vha, 0xe041,
			    "qla_target(%d): Response pkt (CTIO_TYPE7) "
			    "received, with unknown vp_index %d\n",
			    vha->vp_idx, entry->vp_index);
			break;
		}
460
		qlt_response_pkt(host, rsp, pkt);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
		break;
	}

	case IMMED_NOTIFY_TYPE:
	{
		struct scsi_qla_host *host = vha;
		struct imm_ntfy_from_isp *entry =
		    (struct imm_ntfy_from_isp *)pkt;

		host = qlt_find_host_by_vp_idx(vha, entry->u.isp24.vp_index);
		if (unlikely(!host)) {
			ql_dbg(ql_dbg_tgt, vha, 0xe042,
			    "qla_target(%d): Response pkt (IMMED_NOTIFY_TYPE) "
			    "received, with unknown vp_index %d\n",
			    vha->vp_idx, entry->u.isp24.vp_index);
			break;
		}
478
		qlt_response_pkt(host, rsp, pkt);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
		break;
	}

	case NOTIFY_ACK_TYPE:
	{
		struct scsi_qla_host *host = vha;
		struct nack_to_isp *entry = (struct nack_to_isp *)pkt;

		if (0xFF != entry->u.isp24.vp_index) {
			host = qlt_find_host_by_vp_idx(vha,
			    entry->u.isp24.vp_index);
			if (unlikely(!host)) {
				ql_dbg(ql_dbg_tgt, vha, 0xe043,
				    "qla_target(%d): Response "
				    "pkt (NOTIFY_ACK_TYPE) "
				    "received, with unknown "
				    "vp_index %d\n", vha->vp_idx,
				    entry->u.isp24.vp_index);
				break;
			}
		}
500
		qlt_response_pkt(host, rsp, pkt);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
		break;
	}

	case ABTS_RECV_24XX:
	{
		struct abts_recv_from_24xx *entry =
		    (struct abts_recv_from_24xx *)pkt;
		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
		    entry->vp_index);
		if (unlikely(!host)) {
			ql_dbg(ql_dbg_tgt, vha, 0xe044,
			    "qla_target(%d): Response pkt "
			    "(ABTS_RECV_24XX) received, with unknown "
			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
			break;
		}
517
		qlt_response_pkt(host, rsp, pkt);
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		break;
	}

	case ABTS_RESP_24XX:
	{
		struct abts_resp_to_24xx *entry =
		    (struct abts_resp_to_24xx *)pkt;
		struct scsi_qla_host *host = qlt_find_host_by_vp_idx(vha,
		    entry->vp_index);
		if (unlikely(!host)) {
			ql_dbg(ql_dbg_tgt, vha, 0xe045,
			    "qla_target(%d): Response pkt "
			    "(ABTS_RECV_24XX) received, with unknown "
			    "vp_index %d\n", vha->vp_idx, entry->vp_index);
			break;
		}
534
		qlt_response_pkt(host, rsp, pkt);
535 536 537
		break;
	}
	default:
538
		qlt_response_pkt(vha, rsp, pkt);
539 540 541 542 543
		break;
	}

}

544 545 546
/*
 * All qlt_plogi_ack_t operations are protected by hardware_lock
 */
547 548 549 550
static int qla24xx_post_nack_work(struct scsi_qla_host *vha, fc_port_t *fcport,
	struct imm_ntfy_from_isp *ntfy, int type)
{
	struct qla_work_evt *e;
551

552 553 554 555 556 557 558 559 560 561 562
	e = qla2x00_alloc_work(vha, QLA_EVT_NACK);
	if (!e)
		return QLA_FUNCTION_FAILED;

	e->u.nack.fcport = fcport;
	e->u.nack.type = type;
	memcpy(e->u.nack.iocb, ntfy, sizeof(struct imm_ntfy_from_isp));
	return qla2x00_post_work(vha, e);
}

static
563
void qla2x00_async_nack_sp_done(void *s, int res)
564 565
{
	struct srb *sp = (struct srb *)s;
566
	struct scsi_qla_host *vha = sp->vha;
567 568
	unsigned long flags;

569 570 571
	ql_dbg(ql_dbg_disc, vha, 0x20f2,
	    "Async done-%s res %x %8phC  type %d\n",
	    sp->name, res, sp->fcport->port_name, sp->type);
572 573 574

	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
	sp->fcport->flags &= ~FCF_ASYNC_SENT;
575
	sp->fcport->chip_reset = vha->hw->base_qpair->chip_reset;
576 577 578 579 580 581

	switch (sp->type) {
	case SRB_NACK_PLOGI:
		sp->fcport->login_gen++;
		sp->fcport->fw_login_state = DSC_LS_PLOGI_COMP;
		sp->fcport->logout_on_delete = 1;
582
		sp->fcport->plogi_nack_done_deadline = jiffies + HZ;
583
		sp->fcport->send_els_logo = 0;
584 585 586 587 588
		break;

	case SRB_NACK_PRLI:
		sp->fcport->fw_login_state = DSC_LS_PRLI_COMP;
		sp->fcport->deleted = 0;
589
		sp->fcport->send_els_logo = 0;
590 591 592 593 594 595

		if (!sp->fcport->login_succ &&
		    !IS_SW_RESV_ADDR(sp->fcport->d_id)) {
			sp->fcport->login_succ = 1;

			vha->fcport_count++;
596
			spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
597
			qla24xx_sched_upd_fcport(sp->fcport);
598
			spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
599 600 601 602 603
		} else {
			sp->fcport->login_retry = 0;
			sp->fcport->disc_state = DSC_LOGIN_COMPLETE;
			sp->fcport->deleted = 0;
			sp->fcport->logout_on_delete = 1;
604 605 606 607 608 609 610 611 612 613 614
		}
		break;

	case SRB_NACK_LOGO:
		sp->fcport->login_gen++;
		sp->fcport->fw_login_state = DSC_LS_PORT_UNAVAIL;
		qlt_logo_completion_handler(sp->fcport, MBS_COMMAND_COMPLETE);
		break;
	}
	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);

615
	sp->free(sp);
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
}

int qla24xx_async_notify_ack(scsi_qla_host_t *vha, fc_port_t *fcport,
	struct imm_ntfy_from_isp *ntfy, int type)
{
	int rval = QLA_FUNCTION_FAILED;
	srb_t *sp;
	char *c = NULL;

	fcport->flags |= FCF_ASYNC_SENT;
	switch (type) {
	case SRB_NACK_PLOGI:
		fcport->fw_login_state = DSC_LS_PLOGI_PEND;
		c = "PLOGI";
		break;
	case SRB_NACK_PRLI:
		fcport->fw_login_state = DSC_LS_PRLI_PEND;
633
		fcport->deleted = 0;
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
		c = "PRLI";
		break;
	case SRB_NACK_LOGO:
		fcport->fw_login_state = DSC_LS_LOGO_PEND;
		c = "LOGO";
		break;
	}

	sp = qla2x00_get_sp(vha, fcport, GFP_ATOMIC);
	if (!sp)
		goto done;

	sp->type = type;
	sp->name = "nack";

649
	sp->u.iocb_cmd.timeout = qla2x00_async_iocb_timeout;
650 651 652 653 654
	qla2x00_init_timer(sp, qla2x00_get_async_timeout(vha)+2);

	sp->u.iocb_cmd.u.nack.ntfy = ntfy;
	sp->done = qla2x00_async_nack_sp_done;

655 656 657
	ql_dbg(ql_dbg_disc, vha, 0x20f4,
	    "Async-%s %8phC hndl %x %s\n",
	    sp->name, fcport->port_name, sp->handle, c);
658

659 660 661 662
	rval = qla2x00_start_sp(sp);
	if (rval != QLA_SUCCESS)
		goto done_free_sp;

663 664 665
	return rval;

done_free_sp:
666
	sp->free(sp);
667 668 669 670 671 672 673 674 675 676 677
done:
	fcport->flags &= ~FCF_ASYNC_SENT;
	return rval;
}

void qla24xx_do_nack_work(struct scsi_qla_host *vha, struct qla_work_evt *e)
{
	fc_port_t *t;

	switch (e->u.nack.type) {
	case SRB_NACK_PRLI:
678 679 680
		t = e->u.nack.fcport;
		flush_work(&t->del_work);
		flush_work(&t->free_work);
681 682 683 684
		mutex_lock(&vha->vha_tgt.tgt_mutex);
		t = qlt_create_sess(vha, e->u.nack.fcport, 0);
		mutex_unlock(&vha->vha_tgt.tgt_mutex);
		if (t) {
685
			ql_log(ql_log_info, vha, 0xd034,
686 687 688 689 690 691 692
			    "%s create sess success %p", __func__, t);
			/* create sess has an extra kref */
			vha->hw->tgt.tgt_ops->put_sess(e->u.nack.fcport);
		}
		break;
	}
	qla24xx_async_notify_ack(vha, e->u.nack.fcport,
693
	    (struct imm_ntfy_from_isp *)e->u.nack.iocb, e->u.nack.type);
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
}

void qla24xx_delete_sess_fn(struct work_struct *work)
{
	fc_port_t *fcport = container_of(work, struct fc_port, del_work);
	struct qla_hw_data *ha = fcport->vha->hw;

	if (fcport->se_sess) {
		ha->tgt.tgt_ops->shutdown_sess(fcport);
		ha->tgt.tgt_ops->put_sess(fcport);
	} else {
		qlt_unreg_sess(fcport);
	}
}

/*
 * Called from qla2x00_reg_remote_port()
 */
void qlt_fc_port_added(struct scsi_qla_host *vha, fc_port_t *fcport)
{
	struct qla_hw_data *ha = vha->hw;
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
	struct fc_port *sess = fcport;
	unsigned long flags;

	if (!vha->hw->tgt.tgt_ops)
		return;

	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
	if (tgt->tgt_stop) {
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
		return;
	}

	if (fcport->disc_state == DSC_DELETE_PEND) {
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
		return;
	}

	if (!sess->se_sess) {
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);

		mutex_lock(&vha->vha_tgt.tgt_mutex);
		sess = qlt_create_sess(vha, fcport, false);
		mutex_unlock(&vha->vha_tgt.tgt_mutex);

		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
	} else {
		if (fcport->fw_login_state == DSC_LS_PRLI_COMP) {
			spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
			return;
		}

		if (!kref_get_unless_zero(&sess->sess_kref)) {
748
			ql_dbg(ql_dbg_disc, vha, 0x2107,
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
			    "%s: kref_get fail sess %8phC \n",
			    __func__, sess->port_name);
			spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
			return;
		}

		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04c,
		    "qla_target(%u): %ssession for port %8phC "
		    "(loop ID %d) reappeared\n", vha->vp_idx,
		    sess->local ? "local " : "", sess->port_name, sess->loop_id);

		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf007,
		    "Reappeared sess %p\n", sess);

		ha->tgt.tgt_ops->update_sess(sess, fcport->d_id,
		    fcport->loop_id,
		    (fcport->flags & FCF_CONF_COMP_SUPPORTED));
	}

	if (sess && sess->local) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04d,
		    "qla_target(%u): local session for "
		    "port %8phC (loop ID %d) became global\n", vha->vp_idx,
		    fcport->port_name, sess->loop_id);
		sess->local = 0;
	}
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
776 777

	ha->tgt.tgt_ops->put_sess(sess);
778
}
779 780 781 782 783 784

/*
 * This is a zero-base ref-counting solution, since hardware_lock
 * guarantees that ref_count is not modified concurrently.
 * Upon successful return content of iocb is undefined
 */
785
static struct qlt_plogi_ack_t *
786 787 788
qlt_plogi_ack_find_add(struct scsi_qla_host *vha, port_id_t *id,
		       struct imm_ntfy_from_isp *iocb)
{
789
	struct qlt_plogi_ack_t *pla;
790

791 792
	lockdep_assert_held(&vha->hw->hardware_lock);

793 794
	list_for_each_entry(pla, &vha->plogi_ack_list, list) {
		if (pla->id.b24 == id->b24) {
795 796 797 798
			ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0x210d,
			    "%s %d %8phC Term INOT due to new INOT",
			    __func__, __LINE__,
			    pla->iocb.u.isp24.port_name);
799
			qlt_send_term_imm_notif(vha, &pla->iocb, 1);
800
			memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
801 802 803 804 805 806 807 808 809 810 811 812
			return pla;
		}
	}

	pla = kmem_cache_zalloc(qla_tgt_plogi_cachep, GFP_ATOMIC);
	if (!pla) {
		ql_dbg(ql_dbg_async, vha, 0x5088,
		       "qla_target(%d): Allocation of plogi_ack failed\n",
		       vha->vp_idx);
		return NULL;
	}

813
	memcpy(&pla->iocb, iocb, sizeof(pla->iocb));
814 815 816 817 818 819
	pla->id = *id;
	list_add_tail(&pla->list, &vha->plogi_ack_list);

	return pla;
}

820
void qlt_plogi_ack_unref(struct scsi_qla_host *vha,
821
    struct qlt_plogi_ack_t *pla)
822
{
823
	struct imm_ntfy_from_isp *iocb = &pla->iocb;
824 825 826 827
	port_id_t port_id;
	uint16_t loop_id;
	fc_port_t *fcport = pla->fcport;

828 829 830 831 832 833
	BUG_ON(!pla->ref_count);
	pla->ref_count--;

	if (pla->ref_count)
		return;

834
	ql_dbg(ql_dbg_disc, vha, 0x5089,
835
	    "Sending PLOGI ACK to wwn %8phC s_id %02x:%02x:%02x loop_id %#04x"
836 837 838 839 840
	    " exch %#x ox_id %#x\n", iocb->u.isp24.port_name,
	    iocb->u.isp24.port_id[2], iocb->u.isp24.port_id[1],
	    iocb->u.isp24.port_id[0],
	    le16_to_cpu(iocb->u.isp24.nport_handle),
	    iocb->u.isp24.exchange_address, iocb->ox_id);
841 842 843 844 845 846 847 848 849 850

	port_id.b.domain = iocb->u.isp24.port_id[2];
	port_id.b.area   = iocb->u.isp24.port_id[1];
	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
	port_id.b.rsvd_1 = 0;

	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);

	fcport->loop_id = loop_id;
	fcport->d_id = port_id;
851 852 853 854
	if (iocb->u.isp24.status_subcode == ELS_PLOGI)
		qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PLOGI);
	else
		qla24xx_post_nack_work(vha, fcport, iocb, SRB_NACK_PRLI);
855 856 857 858 859 860 861

	list_for_each_entry(fcport, &vha->vp_fcports, list) {
		if (fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] == pla)
			fcport->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
		if (fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] == pla)
			fcport->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
	}
862 863 864 865 866

	list_del(&pla->list);
	kmem_cache_free(qla_tgt_plogi_cachep, pla);
}

867
void
868 869
qlt_plogi_ack_link(struct scsi_qla_host *vha, struct qlt_plogi_ack_t *pla,
    struct fc_port *sess, enum qlt_plogi_link_t link)
870
{
871
	struct imm_ntfy_from_isp *iocb = &pla->iocb;
872 873 874
	/* Inc ref_count first because link might already be pointing at pla */
	pla->ref_count++;

875 876 877 878 879 880 881 882
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf097,
		"Linking sess %p [%d] wwn %8phC with PLOGI ACK to wwn %8phC"
		" s_id %02x:%02x:%02x, ref=%d pla %p link %d\n",
		sess, link, sess->port_name,
		iocb->u.isp24.port_name, iocb->u.isp24.port_id[2],
		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
		pla->ref_count, pla, link);

883 884 885 886 887 888 889 890 891 892 893
	if (link == QLT_PLOGI_LINK_CONFLICT) {
		switch (sess->disc_state) {
		case DSC_DELETED:
		case DSC_DELETE_PEND:
			pla->ref_count--;
			return;
		default:
			break;
		}
	}

894 895 896
	if (sess->plogi_link[link])
		qlt_plogi_ack_unref(vha, sess->plogi_link[link]);

897 898
	if (link == QLT_PLOGI_LINK_SAME_WWN)
		pla->fcport = sess;
899 900 901 902

	sess->plogi_link[link] = pla;
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942
typedef struct {
	/* These fields must be initialized by the caller */
	port_id_t id;
	/*
	 * number of cmds dropped while we were waiting for
	 * initiator to ack LOGO initialize to 1 if LOGO is
	 * triggered by a command, otherwise, to 0
	 */
	int cmd_count;

	/* These fields are used by callee */
	struct list_head list;
} qlt_port_logo_t;

static void
qlt_send_first_logo(struct scsi_qla_host *vha, qlt_port_logo_t *logo)
{
	qlt_port_logo_t *tmp;
	int res;

	mutex_lock(&vha->vha_tgt.tgt_mutex);

	list_for_each_entry(tmp, &vha->logo_list, list) {
		if (tmp->id.b24 == logo->id.b24) {
			tmp->cmd_count += logo->cmd_count;
			mutex_unlock(&vha->vha_tgt.tgt_mutex);
			return;
		}
	}

	list_add_tail(&logo->list, &vha->logo_list);

	mutex_unlock(&vha->vha_tgt.tgt_mutex);

	res = qla24xx_els_dcmd_iocb(vha, ELS_DCMD_LOGO, logo->id);

	mutex_lock(&vha->vha_tgt.tgt_mutex);
	list_del(&logo->list);
	mutex_unlock(&vha->vha_tgt.tgt_mutex);

943 944 945 946
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf098,
	    "Finished LOGO to %02x:%02x:%02x, dropped %d cmds, res = %#x\n",
	    logo->id.b.domain, logo->id.b.area, logo->id.b.al_pa,
	    logo->cmd_count, res);
947 948
}

949
void qlt_free_session_done(struct work_struct *work)
950
{
951
	struct fc_port *sess = container_of(work, struct fc_port,
952 953 954 955
	    free_work);
	struct qla_tgt *tgt = sess->tgt;
	struct scsi_qla_host *vha = sess->vha;
	struct qla_hw_data *ha = vha->hw;
956 957
	unsigned long flags;
	bool logout_started = false;
958
	scsi_qla_host_t *base_vha;
959 960
	struct qlt_plogi_ack_t *own =
		sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
961 962 963

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf084,
		"%s: se_sess %p / sess %p from port %8phC loop_id %#04x"
964
		" s_id %02x:%02x:%02x logout %d keep %d els_logo %d\n",
965
		__func__, sess->se_sess, sess, sess->port_name, sess->loop_id,
966
		sess->d_id.b.domain, sess->d_id.b.area, sess->d_id.b.al_pa,
967
		sess->logout_on_delete, sess->keep_nport_handle,
968
		sess->send_els_logo);
969

970
	if (!IS_SW_RESV_ADDR(sess->d_id)) {
971 972
		qla2x00_mark_device_lost(vha, sess, 0, 0);

973 974
		if (sess->send_els_logo) {
			qlt_port_logo_t logo;
975

976 977
			logo.id = sess->d_id;
			logo.cmd_count = 0;
978 979
			if (!own)
				qlt_send_first_logo(vha, &logo);
980
			sess->send_els_logo = 0;
981 982
		}

983
		if (sess->logout_on_delete && sess->loop_id != FC_NO_LOOP_ID) {
984 985
			int rc;

986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
			if (!own ||
			    (own &&
			     (own->iocb.u.isp24.status_subcode == ELS_PLOGI))) {
				rc = qla2x00_post_async_logout_work(vha, sess,
				    NULL);
				if (rc != QLA_SUCCESS)
					ql_log(ql_log_warn, vha, 0xf085,
					    "Schedule logo failed sess %p rc %d\n",
					    sess, rc);
				else
					logout_started = true;
			} else if (own && (own->iocb.u.isp24.status_subcode ==
				ELS_PRLI) && ha->flags.rida_fmt2) {
				rc = qla2x00_post_async_prlo_work(vha, sess,
				    NULL);
				if (rc != QLA_SUCCESS)
					ql_log(ql_log_warn, vha, 0xf085,
					    "Schedule PRLO failed sess %p rc %d\n",
					    sess, rc);
				else
					logout_started = true;
			}
1008 1009 1010 1011 1012 1013
		} /* if sess->logout_on_delete */

		if (sess->nvme_flag & NVME_FLAG_REGISTERED &&
		    !(sess->nvme_flag & NVME_FLAG_DELETING)) {
			sess->nvme_flag |= NVME_FLAG_DELETING;
			qla_nvme_unregister_remote_port(sess);
1014
		}
1015
	}
1016

1017 1018 1019 1020 1021 1022
	/*
	 * Release the target session for FC Nexus from fabric module code.
	 */
	if (sess->se_sess != NULL)
		ha->tgt.tgt_ops->free_session(sess);

1023 1024 1025
	if (logout_started) {
		bool traced = false;

1026
		while (!READ_ONCE(sess->logout_completed)) {
1027 1028 1029 1030 1031 1032 1033 1034 1035
			if (!traced) {
				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf086,
					"%s: waiting for sess %p logout\n",
					__func__, sess);
				traced = true;
			}
			msleep(100);
		}

1036
		ql_dbg(ql_dbg_disc, vha, 0xf087,
1037
		    "%s: sess %p logout completed\n", __func__, sess);
1038 1039 1040 1041 1042 1043
	}

	if (sess->logo_ack_needed) {
		sess->logo_ack_needed = 0;
		qla24xx_async_notify_ack(vha, sess,
			(struct imm_ntfy_from_isp *)sess->iocb, SRB_NACK_LOGO);
1044 1045
	}

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
	if (sess->se_sess) {
		sess->se_sess = NULL;
		if (tgt && !IS_SW_RESV_ADDR(sess->d_id))
			tgt->sess_count--;
	}

	sess->disc_state = DSC_DELETED;
	sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
	sess->deleted = QLA_SESS_DELETED;

	if (sess->login_succ && !IS_SW_RESV_ADDR(sess->d_id)) {
		vha->fcport_count--;
		sess->login_succ = 0;
	}

1062
	qla2x00_clear_loop_id(sess);
1063 1064 1065 1066 1067 1068 1069 1070

	if (sess->conflict) {
		sess->conflict->login_pause = 0;
		sess->conflict = NULL;
		if (!test_bit(UNLOADING, &vha->dpc_flags))
			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
	}

1071
	{
1072
		struct qlt_plogi_ack_t *con =
1073
		    sess->plogi_link[QLT_PLOGI_LINK_CONFLICT];
1074
		struct imm_ntfy_from_isp *iocb;
1075

1076
		own = sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN];
1077 1078

		if (con) {
1079
			iocb = &con->iocb;
1080
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf099,
1081 1082 1083 1084 1085 1086
				 "se_sess %p / sess %p port %8phC is gone,"
				 " %s (ref=%d), releasing PLOGI for %8phC (ref=%d)\n",
				 sess->se_sess, sess, sess->port_name,
				 own ? "releasing own PLOGI" : "no own PLOGI pending",
				 own ? own->ref_count : -1,
				 iocb->u.isp24.port_name, con->ref_count);
1087
			qlt_plogi_ack_unref(vha, con);
1088
			sess->plogi_link[QLT_PLOGI_LINK_CONFLICT] = NULL;
1089 1090 1091 1092 1093 1094 1095 1096 1097
		} else {
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09a,
			    "se_sess %p / sess %p port %8phC is gone, %s (ref=%d)\n",
			    sess->se_sess, sess, sess->port_name,
			    own ? "releasing own PLOGI" :
			    "no own PLOGI pending",
			    own ? own->ref_count : -1);
		}

1098 1099
		if (own) {
			sess->fw_login_state = DSC_LS_PLOGI_PEND;
1100
			qlt_plogi_ack_unref(vha, own);
1101 1102
			sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] = NULL;
		}
1103
	}
1104

1105 1106
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);

1107
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf001,
1108 1109
	    "Unregistration of sess %p %8phC finished fcp_cnt %d\n",
		sess, sess->port_name, vha->fcport_count);
1110

1111
	if (tgt && (tgt->sess_count == 0))
1112
		wake_up_all(&tgt->waitQ);
1113 1114 1115 1116

	if (vha->fcport_count == 0)
		wake_up_all(&vha->fcport_waitQ);

1117
	base_vha = pci_get_drvdata(ha->pdev);
1118 1119 1120

	sess->free_pending = 0;

1121 1122 1123
	if (test_bit(PFLG_DRIVER_REMOVING, &base_vha->pci_flags))
		return;

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	if ((!tgt || !tgt->tgt_stop) && !LOOP_TRANSITION(vha)) {
		switch (vha->host->active_mode) {
		case MODE_INITIATOR:
		case MODE_DUAL:
			set_bit(RELOGIN_NEEDED, &vha->dpc_flags);
			qla2xxx_wake_dpc(vha);
			break;
		case MODE_TARGET:
		default:
			/* no-op */
			break;
		}
1136
	}
1137 1138
}

1139
/* ha->tgt.sess_lock supposed to be held on entry */
1140
void qlt_unreg_sess(struct fc_port *sess)
1141 1142
{
	struct scsi_qla_host *vha = sess->vha;
1143
	unsigned long flags;
1144

1145
	ql_dbg(ql_dbg_disc, sess->vha, 0x210a,
1146 1147 1148
	    "%s sess %p for deletion %8phC\n",
	    __func__, sess, sess->port_name);

1149 1150 1151 1152 1153 1154 1155 1156
	spin_lock_irqsave(&sess->vha->work_lock, flags);
	if (sess->free_pending) {
		spin_unlock_irqrestore(&sess->vha->work_lock, flags);
		return;
	}
	sess->free_pending = 1;
	spin_unlock_irqrestore(&sess->vha->work_lock, flags);

1157 1158
	if (sess->se_sess)
		vha->hw->tgt.tgt_ops->clear_nacl_from_fcport_map(sess);
1159

1160
	sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1161 1162 1163
	sess->disc_state = DSC_DELETE_PEND;
	sess->last_rscn_gen = sess->rscn_gen;
	sess->last_login_gen = sess->login_gen;
1164

1165 1166
	INIT_WORK(&sess->free_work, qlt_free_session_done);
	schedule_work(&sess->free_work);
1167
}
1168
EXPORT_SYMBOL(qlt_unreg_sess);
1169

1170 1171 1172
static int qlt_reset(struct scsi_qla_host *vha, void *iocb, int mcmd)
{
	struct qla_hw_data *ha = vha->hw;
1173
	struct fc_port *sess = NULL;
1174 1175 1176
	uint16_t loop_id;
	int res = 0;
	struct imm_ntfy_from_isp *n = (struct imm_ntfy_from_isp *)iocb;
1177
	unsigned long flags;
1178 1179 1180 1181

	loop_id = le16_to_cpu(n->u.isp24.nport_handle);
	if (loop_id == 0xFFFF) {
		/* Global event */
1182
		atomic_inc(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
1183
		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1184
		qlt_clear_tgt_db(vha->vha_tgt.qla_tgt);
1185
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1186
	} else {
1187
		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1188
		sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
1189
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	}

	ql_dbg(ql_dbg_tgt, vha, 0xe000,
	    "Using sess for qla_tgt_reset: %p\n", sess);
	if (!sess) {
		res = -ESRCH;
		return res;
	}

	ql_dbg(ql_dbg_tgt, vha, 0xe047,
1200 1201
	    "scsi(%ld): resetting (session %p from port %8phC mcmd %x, "
	    "loop_id %d)\n", vha->host_no, sess, sess->port_name,
1202 1203
	    mcmd, loop_id);

1204
	return qlt_issue_task_mgmt(sess, 0, mcmd, iocb, QLA24XX_MGMT_SEND_NACK);
1205 1206
}

1207 1208
static void qla24xx_chk_fcp_state(struct fc_port *sess)
{
1209
	if (sess->chip_reset != sess->vha->hw->base_qpair->chip_reset) {
1210 1211 1212 1213 1214 1215
		sess->logout_on_delete = 0;
		sess->logo_ack_needed = 0;
		sess->fw_login_state = DSC_LS_PORT_UNAVAIL;
	}
}

1216
void qlt_schedule_sess_for_deletion(struct fc_port *sess)
1217 1218
{
	struct qla_tgt *tgt = sess->tgt;
1219
	unsigned long flags;
1220
	u16 sec;
1221

1222 1223
	switch (sess->disc_state) {
	case DSC_DELETE_PEND:
1224
		return;
1225
	case DSC_DELETED:
1226 1227 1228 1229 1230 1231 1232
		if (tgt && tgt->tgt_stop && (tgt->sess_count == 0))
			wake_up_all(&tgt->waitQ);
		if (sess->vha->fcport_count == 0)
			wake_up_all(&sess->vha->fcport_waitQ);

		if (!sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN] &&
			!sess->plogi_link[QLT_PLOGI_LINK_CONFLICT])
1233
			return;
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
		break;
	case DSC_UPD_FCPORT:
		/*
		 * This port is not done reporting to upper layer.
		 * let it finish
		 */
		sess->next_disc_state = DSC_DELETE_PEND;
		sec = jiffies_to_msecs(jiffies -
		    sess->jiffies_at_registration)/1000;
		if (sess->sec_since_registration < sec && sec && !(sec % 5)) {
			sess->sec_since_registration = sec;
			ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
			    "%s %8phC : Slow Rport registration(%d Sec)\n",
			    __func__, sess->port_name, sec);
		}
		return;
	default:
		break;
1252
	}
1253

1254
	spin_lock_irqsave(&sess->vha->work_lock, flags);
1255
	if (sess->deleted == QLA_SESS_DELETION_IN_PROGRESS) {
1256
		spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1257 1258
		return;
	}
1259
	sess->deleted = QLA_SESS_DELETION_IN_PROGRESS;
1260
	spin_unlock_irqrestore(&sess->vha->work_lock, flags);
1261 1262 1263

	sess->disc_state = DSC_DELETE_PEND;

1264
	qla24xx_chk_fcp_state(sess);
1265

1266
	ql_dbg(ql_dbg_tgt, sess->vha, 0xe001,
1267 1268
	    "Scheduling sess %p for deletion %8phC\n",
	    sess, sess->port_name);
1269

1270
	INIT_WORK(&sess->del_work, qla24xx_delete_sess_fn);
1271
	WARN_ON(!queue_work(sess->vha->hw->wq, &sess->del_work));
1272
}
1273

J
Joern Engel 已提交
1274
static void qlt_clear_tgt_db(struct qla_tgt *tgt)
1275
{
1276 1277
	struct fc_port *sess;
	scsi_qla_host_t *vha = tgt->vha;
1278

1279 1280
	list_for_each_entry(sess, &vha->vp_fcports, list) {
		if (sess->se_sess)
1281
			qlt_schedule_sess_for_deletion(sess);
1282
	}
1283 1284 1285 1286

	/* At this point tgt could be already dead */
}

1287
static int qla24xx_get_loop_id(struct scsi_qla_host *vha, be_id_t s_id,
1288 1289 1290 1291
	uint16_t *loop_id)
{
	struct qla_hw_data *ha = vha->hw;
	dma_addr_t gid_list_dma;
1292
	struct gid_list_info *gid_list, *gid;
1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	int res, rc, i;
	uint16_t entries;

	gid_list = dma_alloc_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
	    &gid_list_dma, GFP_KERNEL);
	if (!gid_list) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf044,
		    "qla_target(%d): DMA Alloc failed of %u\n",
		    vha->vp_idx, qla2x00_gid_list_size(ha));
		return -ENOMEM;
	}

	/* Get list of logged in devices */
1306
	rc = qla24xx_gidlist_wait(vha, gid_list, gid_list_dma, &entries);
1307 1308 1309 1310
	if (rc != QLA_SUCCESS) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf045,
		    "qla_target(%d): get_id_list() failed: %x\n",
		    vha->vp_idx, rc);
1311
		res = -EBUSY;
1312 1313 1314
		goto out_free_id_list;
	}

1315
	gid = gid_list;
1316
	res = -ENOENT;
1317
	for (i = 0; i < entries; i++) {
1318 1319 1320
		if (gid->al_pa == s_id.al_pa &&
		    gid->area == s_id.area &&
		    gid->domain == s_id.domain) {
1321 1322 1323 1324
			*loop_id = le16_to_cpu(gid->loop_id);
			res = 0;
			break;
		}
1325
		gid = (void *)gid + ha->gid_list_info_size;
1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
	}

out_free_id_list:
	dma_free_coherent(&ha->pdev->dev, qla2x00_gid_list_size(ha),
	    gid_list, gid_list_dma);
	return res;
}

/*
 * Adds an extra ref to allow to drop hw lock after adding sess to the list.
 * Caller must put it.
 */
1338
static struct fc_port *qlt_create_sess(
1339 1340 1341 1342 1343
	struct scsi_qla_host *vha,
	fc_port_t *fcport,
	bool local)
{
	struct qla_hw_data *ha = vha->hw;
1344
	struct fc_port *sess = fcport;
1345 1346
	unsigned long flags;

1347 1348
	if (vha->vha_tgt.qla_tgt->tgt_stop)
		return NULL;
1349

1350 1351
	if (fcport->se_sess) {
		if (!kref_get_unless_zero(&sess->sess_kref)) {
1352
			ql_dbg(ql_dbg_disc, vha, 0x20f6,
1353 1354 1355
			    "%s: kref_get_unless_zero failed for %8phC\n",
			    __func__, sess->port_name);
			return NULL;
1356
		}
1357
		return fcport;
1358
	}
1359
	sess->tgt = vha->vha_tgt.qla_tgt;
1360
	sess->local = local;
1361

1362 1363
	/*
	 * Under normal circumstances we want to logout from firmware when
1364 1365
	 * session eventually ends and release corresponding nport handle.
	 * In the exception cases (e.g. when new PLOGI is waiting) corresponding
1366 1367
	 * code will adjust these flags as necessary.
	 */
1368 1369
	sess->logout_on_delete = 1;
	sess->keep_nport_handle = 0;
1370
	sess->logout_completed = 0;
1371

1372 1373
	if (ha->tgt.tgt_ops->check_initiator_node_acl(vha,
	    &fcport->port_name[0], sess) < 0) {
1374
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf015,
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
		    "(%d) %8phC check_initiator_node_acl failed\n",
		    vha->vp_idx, fcport->port_name);
		return NULL;
	} else {
		kref_init(&fcport->sess_kref);
		/*
		 * Take an extra reference to ->sess_kref here to handle
		 * fc_port access across ->tgt.sess_lock reaquire.
		 */
		if (!kref_get_unless_zero(&sess->sess_kref)) {
1385
			ql_dbg(ql_dbg_disc, vha, 0x20f7,
1386 1387 1388 1389
			    "%s: kref_get_unless_zero failed for %8phC\n",
			    __func__, sess->port_name);
			return NULL;
		}
1390

1391 1392 1393
		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
		if (!IS_SW_RESV_ADDR(sess->d_id))
			vha->vha_tgt.qla_tgt->sess_count++;
1394

1395 1396 1397 1398 1399 1400 1401 1402
		qlt_do_generation_tick(vha, &sess->generation);
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
	}

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf006,
	    "Adding sess %p se_sess %p  to tgt %p sess_count %d\n",
	    sess, sess->se_sess, vha->vha_tgt.qla_tgt,
	    vha->vha_tgt.qla_tgt->sess_count);
1403 1404

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04b,
1405 1406 1407
	    "qla_target(%d): %ssession for wwn %8phC (loop_id %d, "
	    "s_id %x:%x:%x, confirmed completion %ssupported) added\n",
	    vha->vp_idx, local ?  "local " : "", fcport->port_name,
1408 1409
	    fcport->loop_id, sess->d_id.b.domain, sess->d_id.b.area,
	    sess->d_id.b.al_pa, sess->conf_compl_supported ?  "" : "not ");
1410 1411 1412 1413

	return sess;
}

1414 1415 1416 1417 1418 1419
/*
 * max_gen - specifies maximum session generation
 * at which this deletion requestion is still valid
 */
void
qlt_fc_port_deleted(struct scsi_qla_host *vha, fc_port_t *fcport, int max_gen)
1420
{
1421
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
1422
	struct fc_port *sess = fcport;
1423
	unsigned long flags;
1424 1425 1426 1427

	if (!vha->hw->tgt.tgt_ops)
		return;

1428
	if (!tgt)
1429 1430
		return;

1431
	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
1432
	if (tgt->tgt_stop) {
1433
		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1434 1435
		return;
	}
1436
	if (!sess->se_sess) {
1437
		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1438 1439 1440
		return;
	}

1441
	if (max_gen - sess->generation < 0) {
1442
		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1443 1444 1445 1446 1447 1448 1449 1450
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf092,
		    "Ignoring stale deletion request for se_sess %p / sess %p"
		    " for port %8phC, req_gen %d, sess_gen %d\n",
		    sess->se_sess, sess, sess->port_name, max_gen,
		    sess->generation);
		return;
	}

1451 1452 1453
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf008, "qla_tgt_fc_port_deleted %p", sess);

	sess->local = 1;
1454
	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
1455
	qlt_schedule_sess_for_deletion(sess);
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
}

static inline int test_tgt_sess_count(struct qla_tgt *tgt)
{
	struct qla_hw_data *ha = tgt->ha;
	unsigned long flags;
	int res;
	/*
	 * We need to protect against race, when tgt is freed before or
	 * inside wake_up()
	 */
1467
	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1468
	ql_dbg(ql_dbg_tgt, tgt->vha, 0xe002,
1469 1470
	    "tgt %p, sess_count=%d\n",
	    tgt, tgt->sess_count);
1471
	res = (tgt->sess_count == 0);
1472
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1473 1474 1475 1476 1477

	return res;
}

/* Called by tcm_qla2xxx configfs code */
1478
int qlt_stop_phase1(struct qla_tgt *tgt)
1479 1480 1481 1482 1483
{
	struct scsi_qla_host *vha = tgt->vha;
	struct qla_hw_data *ha = tgt->ha;
	unsigned long flags;

1484
	mutex_lock(&ha->optrom_mutex);
1485
	mutex_lock(&qla_tgt_mutex);
1486

1487 1488 1489
	if (tgt->tgt_stop || tgt->tgt_stopped) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04e,
		    "Already in tgt->tgt_stop or tgt_stopped state\n");
1490
		mutex_unlock(&qla_tgt_mutex);
1491
		mutex_unlock(&ha->optrom_mutex);
1492
		return -EPERM;
1493 1494
	}

1495
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xe003, "Stopping target for host %ld(%p)\n",
1496 1497 1498 1499 1500
	    vha->host_no, vha);
	/*
	 * Mutex needed to sync with qla_tgt_fc_port_[added,deleted].
	 * Lock is needed, because we still can get an incoming packet.
	 */
1501
	mutex_lock(&vha->vha_tgt.tgt_mutex);
1502
	tgt->tgt_stop = 1;
J
Joern Engel 已提交
1503
	qlt_clear_tgt_db(tgt);
1504
	mutex_unlock(&vha->vha_tgt.tgt_mutex);
1505
	mutex_unlock(&qla_tgt_mutex);
1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf009,
	    "Waiting for sess works (tgt %p)", tgt);
	spin_lock_irqsave(&tgt->sess_work_lock, flags);
	while (!list_empty(&tgt->sess_works_list)) {
		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
		flush_scheduled_work();
		spin_lock_irqsave(&tgt->sess_work_lock, flags);
	}
	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00a,
1518
	    "Waiting for tgt %p: sess_count=%d\n", tgt, tgt->sess_count);
1519

1520
	wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1521 1522

	/* Big hammer */
1523 1524
	if (!ha->flags.host_shutting_down &&
	    (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)))
1525 1526 1527
		qlt_disable_vha(vha);

	/* Wait for sessions to clear out (just in case) */
1528
	wait_event_timeout(tgt->waitQ, test_tgt_sess_count(tgt), 10*HZ);
1529 1530
	mutex_unlock(&ha->optrom_mutex);

1531
	return 0;
1532 1533 1534 1535 1536 1537
}
EXPORT_SYMBOL(qlt_stop_phase1);

/* Called by tcm_qla2xxx configfs code */
void qlt_stop_phase2(struct qla_tgt *tgt)
{
1538
	scsi_qla_host_t *vha = tgt->vha;
1539 1540

	if (tgt->tgt_stopped) {
1541
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf04f,
1542 1543 1544 1545
		    "Already in tgt->tgt_stopped state\n");
		dump_stack();
		return;
	}
1546 1547 1548 1549 1550 1551
	if (!tgt->tgt_stop) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00b,
		    "%s: phase1 stop is not completed\n", __func__);
		dump_stack();
		return;
	}
1552

1553
	mutex_lock(&vha->vha_tgt.tgt_mutex);
1554 1555
	tgt->tgt_stop = 0;
	tgt->tgt_stopped = 1;
1556
	mutex_unlock(&vha->vha_tgt.tgt_mutex);
1557

1558
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00c, "Stop of tgt %p finished\n",
1559
	    tgt);
1560 1561 1562 1563 1564 1565 1566 1567 1568

	switch (vha->qlini_mode) {
	case QLA2XXX_INI_MODE_EXCLUSIVE:
		vha->flags.online = 1;
		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
		break;
	default:
		break;
	}
1569 1570 1571 1572
}
EXPORT_SYMBOL(qlt_stop_phase2);

/* Called from qlt_remove_target() -> qla2x00_remove_one() */
1573
static void qlt_release(struct qla_tgt *tgt)
1574
{
1575
	scsi_qla_host_t *vha = tgt->vha;
1576 1577 1578 1579
	void *node;
	u64 key = 0;
	u16 i;
	struct qla_qpair_hint *h;
1580
	struct qla_hw_data *ha = vha->hw;
1581

1582
	if (!tgt->tgt_stop && !tgt->tgt_stopped)
1583 1584
		qlt_stop_phase1(tgt);

1585
	if (!tgt->tgt_stopped)
1586 1587
		qlt_stop_phase2(tgt);

1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
	for (i = 0; i < vha->hw->max_qpairs + 1; i++) {
		unsigned long flags;

		h = &tgt->qphints[i];
		if (h->qpair) {
			spin_lock_irqsave(h->qpair->qp_lock_ptr, flags);
			list_del(&h->hint_elem);
			spin_unlock_irqrestore(h->qpair->qp_lock_ptr, flags);
			h->qpair = NULL;
		}
	}
	kfree(tgt->qphints);
1600 1601 1602
	mutex_lock(&qla_tgt_mutex);
	list_del(&vha->vha_tgt.qla_tgt->tgt_list_entry);
	mutex_unlock(&qla_tgt_mutex);
1603 1604 1605 1606 1607 1608

	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
		btree_remove64(&tgt->lun_qpair_map, key);

	btree_destroy64(&tgt->lun_qpair_map);

1609 1610 1611 1612 1613
	if (vha->vp_idx)
		if (ha->tgt.tgt_ops &&
		    ha->tgt.tgt_ops->remove_target &&
		    vha->vha_tgt.target_lport_ptr)
			ha->tgt.tgt_ops->remove_target(vha);
1614

1615
	vha->vha_tgt.qla_tgt = NULL;
1616

1617
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00d,
1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
	    "Release of tgt %p finished\n", tgt);

	kfree(tgt);
}

/* ha->hardware_lock supposed to be held on entry */
static int qlt_sched_sess_work(struct qla_tgt *tgt, int type,
	const void *param, unsigned int param_size)
{
	struct qla_tgt_sess_work_param *prm;
	unsigned long flags;

	prm = kzalloc(sizeof(*prm), GFP_ATOMIC);
	if (!prm) {
		ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf050,
		    "qla_target(%d): Unable to create session "
		    "work, command will be refused", 0);
		return -ENOMEM;
	}

	ql_dbg(ql_dbg_tgt_mgt, tgt->vha, 0xf00e,
	    "Scheduling work (type %d, prm %p)"
	    " to find session for param %p (size %d, tgt %p)\n",
	    type, prm, param, param_size, tgt);

	prm->type = type;
	memcpy(&prm->tm_iocb, param, param_size);

	spin_lock_irqsave(&tgt->sess_work_lock, flags);
	list_add_tail(&prm->sess_works_list_entry, &tgt->sess_works_list);
	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);

	schedule_work(&tgt->sess_work);

	return 0;
}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
1658
static void qlt_send_notify_ack(struct qla_qpair *qpair,
1659 1660 1661 1662
	struct imm_ntfy_from_isp *ntfy,
	uint32_t add_flags, uint16_t resp_code, int resp_code_valid,
	uint16_t srr_flags, uint16_t srr_reject_code, uint8_t srr_explan)
{
1663
	struct scsi_qla_host *vha = qpair->vha;
1664 1665 1666 1667
	struct qla_hw_data *ha = vha->hw;
	request_t *pkt;
	struct nack_to_isp *nack;

1668 1669 1670
	if (!ha->flags.fw_started)
		return;

1671 1672
	ql_dbg(ql_dbg_tgt, vha, 0xe004, "Sending NOTIFY_ACK (ha=%p)\n", ha);

1673
	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
1674 1675 1676 1677 1678 1679 1680
	if (!pkt) {
		ql_dbg(ql_dbg_tgt, vha, 0xe049,
		    "qla_target(%d): %s failed: unable to allocate "
		    "request packet\n", vha->vp_idx, __func__);
		return;
	}

1681 1682
	if (vha->vha_tgt.qla_tgt != NULL)
		vha->vha_tgt.qla_tgt->notify_ack_expected++;
1683 1684 1685 1686 1687 1688 1689

	pkt->entry_type = NOTIFY_ACK_TYPE;
	pkt->entry_count = 1;

	nack = (struct nack_to_isp *)pkt;
	nack->ox_id = ntfy->ox_id;

1690
	nack->u.isp24.handle = QLA_TGT_SKIP_HANDLE;
1691 1692 1693
	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
		nack->u.isp24.flags = ntfy->u.isp24.flags &
1694
			cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
1695 1696 1697 1698
	}
	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
	nack->u.isp24.status = ntfy->u.isp24.status;
	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
1699
	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
	nack->u.isp24.srr_flags = cpu_to_le16(srr_flags);
	nack->u.isp24.srr_reject_code = srr_reject_code;
	nack->u.isp24.srr_reject_code_expl = srr_explan;
	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;

	ql_dbg(ql_dbg_tgt, vha, 0xe005,
	    "qla_target(%d): Sending 24xx Notify Ack %d\n",
	    vha->vp_idx, nack->u.isp24.status);

1712 1713
	/* Memory Barrier */
	wmb();
1714
	qla2x00_start_iocbs(vha, qpair->req);
1715 1716
}

1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
static int qlt_build_abts_resp_iocb(struct qla_tgt_mgmt_cmd *mcmd)
{
	struct scsi_qla_host *vha = mcmd->vha;
	struct qla_hw_data *ha = vha->hw;
	struct abts_resp_to_24xx *resp;
	uint32_t f_ctl, h;
	uint8_t *p;
	int rc;
	struct abts_recv_from_24xx *abts = &mcmd->orig_iocb.abts;
	struct qla_qpair *qpair = mcmd->qpair;

	ql_dbg(ql_dbg_tgt, vha, 0xe006,
	    "Sending task mgmt ABTS response (ha=%p, status=%x)\n",
	    ha, mcmd->fc_tm_rsp);

	rc = qlt_check_reserve_free_req(qpair, 1);
	if (rc) {
		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
		    "qla_target(%d): %s failed: unable to allocate request packet\n",
		    vha->vp_idx, __func__);
		return -EAGAIN;
	}

	resp = (struct abts_resp_to_24xx *)qpair->req->ring_ptr;
	memset(resp, 0, sizeof(*resp));

	h = qlt_make_handle(qpair);
	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
		/*
		 * CTIO type 7 from the firmware doesn't provide a way to
		 * know the initiator's LOOP ID, hence we can't find
		 * the session and, so, the command.
		 */
		return -EAGAIN;
	} else {
		qpair->req->outstanding_cmds[h] = (srb_t *)mcmd;
	}

	resp->handle = MAKE_HANDLE(qpair->req->id, h);
	resp->entry_type = ABTS_RESP_24XX;
	resp->entry_count = 1;
	resp->nport_handle = abts->nport_handle;
	resp->vp_index = vha->vp_idx;
	resp->sof_type = abts->sof_type;
	resp->exchange_address = abts->exchange_address;
	resp->fcp_hdr_le = abts->fcp_hdr_le;
	f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
	    F_CTL_SEQ_INITIATIVE);
	p = (uint8_t *)&f_ctl;
	resp->fcp_hdr_le.f_ctl[0] = *p++;
	resp->fcp_hdr_le.f_ctl[1] = *p++;
	resp->fcp_hdr_le.f_ctl[2] = *p;

1771 1772
	resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
	resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800

	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
	if (mcmd->fc_tm_rsp == FCP_TMF_CMPL) {
		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
		resp->payload.ba_acct.low_seq_cnt = 0x0000;
		resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
	} else {
		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
		resp->payload.ba_rjt.reason_code =
			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
		/* Other bytes are zero */
	}

	vha->vha_tgt.qla_tgt->abts_resp_expected++;

	/* Memory Barrier */
	wmb();
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(vha, qpair->req);

	return rc;
}

1801 1802 1803
/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
1804
static void qlt_24xx_send_abts_resp(struct qla_qpair *qpair,
1805 1806 1807
	struct abts_recv_from_24xx *abts, uint32_t status,
	bool ids_reversed)
{
1808
	struct scsi_qla_host *vha = qpair->vha;
1809 1810 1811 1812 1813 1814 1815 1816 1817
	struct qla_hw_data *ha = vha->hw;
	struct abts_resp_to_24xx *resp;
	uint32_t f_ctl;
	uint8_t *p;

	ql_dbg(ql_dbg_tgt, vha, 0xe006,
	    "Sending task mgmt ABTS response (ha=%p, atio=%p, status=%x\n",
	    ha, abts, status);

1818 1819
	resp = (struct abts_resp_to_24xx *)qla2x00_alloc_iocbs_ready(qpair,
	    NULL);
1820 1821 1822 1823 1824 1825 1826 1827
	if (!resp) {
		ql_dbg(ql_dbg_tgt, vha, 0xe04a,
		    "qla_target(%d): %s failed: unable to allocate "
		    "request packet", vha->vp_idx, __func__);
		return;
	}

	resp->entry_type = ABTS_RESP_24XX;
1828
	resp->handle = QLA_TGT_SKIP_HANDLE;
1829 1830 1831 1832 1833 1834
	resp->entry_count = 1;
	resp->nport_handle = abts->nport_handle;
	resp->vp_index = vha->vp_idx;
	resp->sof_type = abts->sof_type;
	resp->exchange_address = abts->exchange_address;
	resp->fcp_hdr_le = abts->fcp_hdr_le;
1835
	f_ctl = cpu_to_le32(F_CTL_EXCH_CONTEXT_RESP |
1836 1837 1838 1839 1840 1841 1842
	    F_CTL_LAST_SEQ | F_CTL_END_SEQ |
	    F_CTL_SEQ_INITIATIVE);
	p = (uint8_t *)&f_ctl;
	resp->fcp_hdr_le.f_ctl[0] = *p++;
	resp->fcp_hdr_le.f_ctl[1] = *p++;
	resp->fcp_hdr_le.f_ctl[2] = *p;
	if (ids_reversed) {
1843 1844
		resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.d_id;
		resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.s_id;
1845
	} else {
1846 1847
		resp->fcp_hdr_le.d_id = abts->fcp_hdr_le.s_id;
		resp->fcp_hdr_le.s_id = abts->fcp_hdr_le.d_id;
1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
	}
	resp->exchange_addr_to_abort = abts->exchange_addr_to_abort;
	if (status == FCP_TMF_CMPL) {
		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_ACC;
		resp->payload.ba_acct.seq_id_valid = SEQ_ID_INVALID;
		resp->payload.ba_acct.low_seq_cnt = 0x0000;
		resp->payload.ba_acct.high_seq_cnt = 0xFFFF;
		resp->payload.ba_acct.ox_id = abts->fcp_hdr_le.ox_id;
		resp->payload.ba_acct.rx_id = abts->fcp_hdr_le.rx_id;
	} else {
		resp->fcp_hdr_le.r_ctl = R_CTL_BASIC_LINK_SERV | R_CTL_B_RJT;
		resp->payload.ba_rjt.reason_code =
			BA_RJT_REASON_CODE_UNABLE_TO_PERFORM;
		/* Other bytes are zero */
	}

1864
	vha->vha_tgt.qla_tgt->abts_resp_expected++;
1865

1866 1867
	/* Memory Barrier */
	wmb();
1868 1869 1870 1871
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(vha, qpair->req);
1872 1873 1874 1875 1876 1877
}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
static void qlt_24xx_retry_term_exchange(struct scsi_qla_host *vha,
1878
    struct qla_qpair *qpair, response_t *pkt, struct qla_tgt_mgmt_cmd *mcmd)
1879 1880
{
	struct ctio7_to_24xx *ctio;
1881
	u16 tmp;
1882
	struct abts_recv_from_24xx *entry;
1883

1884
	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs_ready(qpair, NULL);
1885 1886 1887 1888 1889 1890 1891
	if (ctio == NULL) {
		ql_dbg(ql_dbg_tgt, vha, 0xe04b,
		    "qla_target(%d): %s failed: unable to allocate "
		    "request packet\n", vha->vp_idx, __func__);
		return;
	}

1892 1893 1894 1895 1896 1897 1898
	if (mcmd)
		/* abts from remote port */
		entry = &mcmd->orig_iocb.abts;
	else
		/* abts from this driver.  */
		entry = (struct abts_recv_from_24xx *)pkt;

1899 1900 1901 1902 1903 1904 1905 1906 1907
	/*
	 * We've got on entrance firmware's response on by us generated
	 * ABTS response. So, in it ID fields are reversed.
	 */

	ctio->entry_type = CTIO_TYPE7;
	ctio->entry_count = 1;
	ctio->nport_handle = entry->nport_handle;
	ctio->handle = QLA_TGT_SKIP_HANDLE |	CTIO_COMPLETION_HANDLE_MARK;
1908
	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
1909 1910
	ctio->vp_index = vha->vp_idx;
	ctio->exchange_addr = entry->exchange_addr_to_abort;
1911
	tmp = (CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_TERMINATE);
1912 1913

	if (mcmd) {
1914
		ctio->initiator_id = entry->fcp_hdr_le.s_id;
1915 1916 1917 1918 1919 1920

		if (mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID)
			tmp |= (mcmd->abort_io_attr << 9);
		else if (qpair->retry_term_cnt & 1)
			tmp |= (0x4 << 9);
	} else {
1921
		ctio->initiator_id = entry->fcp_hdr_le.d_id;
1922 1923 1924 1925

		if (qpair->retry_term_cnt & 1)
			tmp |= (0x4 << 9);
	}
1926
	ctio->u.status1.flags = cpu_to_le16(tmp);
1927
	ctio->u.status1.ox_id = entry->fcp_hdr_le.ox_id;
1928

1929 1930 1931 1932 1933
	ql_dbg(ql_dbg_tgt, vha, 0xe007,
	    "Sending retry TERM EXCH CTIO7 flags %04xh oxid %04xh attr valid %x\n",
	    le16_to_cpu(ctio->u.status1.flags),
	    le16_to_cpu(ctio->u.status1.ox_id),
	    (mcmd && mcmd->flags & QLA24XX_MGMT_ABORT_IO_ATTR_VALID) ? 1 : 0);
1934

1935 1936
	/* Memory Barrier */
	wmb();
1937 1938 1939 1940
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(vha, qpair->req);
1941

1942 1943 1944 1945 1946
	if (mcmd)
		qlt_build_abts_resp_iocb(mcmd);
	else
		qlt_24xx_send_abts_resp(qpair,
		    (struct abts_recv_from_24xx *)entry, FCP_TMF_CMPL, true);
1947 1948 1949 1950 1951 1952 1953 1954

}

/* drop cmds for the given lun
 * XXX only looks for cmds on the port through which lun reset was recieved
 * XXX does not go through the list of other port (which may have cmds
 *     for the same lun)
 */
1955
static void abort_cmds_for_lun(struct scsi_qla_host *vha, u64 lun, be_id_t s_id)
1956 1957 1958 1959
{
	struct qla_tgt_sess_op *op;
	struct qla_tgt_cmd *cmd;
	uint32_t key;
1960
	unsigned long flags;
1961 1962

	key = sid_to_key(s_id);
1963
	spin_lock_irqsave(&vha->cmd_list_lock, flags);
1964 1965
	list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
		uint32_t op_key;
1966
		u64 op_lun;
1967 1968 1969 1970 1971 1972 1973

		op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
		op_lun = scsilun_to_int(
			(struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
		if (op_key == key && op_lun == lun)
			op->aborted = true;
	}
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985

	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
		uint32_t op_key;
		u64 op_lun;

		op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
		op_lun = scsilun_to_int(
			(struct scsi_lun *)&op->atio.u.isp24.fcp_cmnd.lun);
		if (op_key == key && op_lun == lun)
			op->aborted = true;
	}

1986 1987
	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
		uint32_t cmd_key;
1988
		u64 cmd_lun;
1989 1990 1991 1992 1993

		cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
		cmd_lun = scsilun_to_int(
			(struct scsi_lun *)&cmd->atio.u.isp24.fcp_cmnd.lun);
		if (cmd_key == key && cmd_lun == lun)
1994
			cmd->aborted = 1;
1995
	}
1996
	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
1997 1998
}

1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
static struct qla_qpair_hint *qlt_find_qphint(struct scsi_qla_host *vha,
    uint64_t unpacked_lun)
{
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
	struct qla_qpair_hint *h = NULL;

	if (vha->flags.qpairs_available) {
		h = btree_lookup64(&tgt->lun_qpair_map, unpacked_lun);
		if (!h)
			h = &tgt->qphints[0];
	} else {
		h = &tgt->qphints[0];
	}

	return h;
}

static void qlt_do_tmr_work(struct work_struct *work)
{
	struct qla_tgt_mgmt_cmd *mcmd =
		container_of(work, struct qla_tgt_mgmt_cmd, work);
	struct qla_hw_data *ha = mcmd->vha->hw;
	int rc = EIO;
	uint32_t tag;
	unsigned long flags;

	switch (mcmd->tmr_func) {
	case QLA_TGT_ABTS:
		tag = mcmd->orig_iocb.abts.exchange_addr_to_abort;
		break;
	default:
		tag = 0;
		break;
	}

	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, mcmd->unpacked_lun,
	    mcmd->tmr_func, tag);

	if (rc != 0) {
		spin_lock_irqsave(mcmd->qpair->qp_lock_ptr, flags);
		switch (mcmd->tmr_func) {
		case QLA_TGT_ABTS:
2041 2042
			mcmd->fc_tm_rsp = FCP_TMF_REJECTED;
			qlt_build_abts_resp_iocb(mcmd);
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068
			break;
		case QLA_TGT_LUN_RESET:
		case QLA_TGT_CLEAR_TS:
		case QLA_TGT_ABORT_TS:
		case QLA_TGT_CLEAR_ACA:
		case QLA_TGT_TARGET_RESET:
			qlt_send_busy(mcmd->qpair, &mcmd->orig_iocb.atio,
			    qla_sam_status);
			break;

		case QLA_TGT_ABORT_ALL:
		case QLA_TGT_NEXUS_LOSS_SESS:
		case QLA_TGT_NEXUS_LOSS:
			qlt_send_notify_ack(mcmd->qpair,
			    &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
			break;
		}
		spin_unlock_irqrestore(mcmd->qpair->qp_lock_ptr, flags);

		ql_dbg(ql_dbg_tgt_mgt, mcmd->vha, 0xf052,
		    "qla_target(%d):  tgt_ops->handle_tmr() failed: %d\n",
		    mcmd->vha->vp_idx, rc);
		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
	}
}

2069 2070
/* ha->hardware_lock supposed to be held on entry */
static int __qlt_24xx_handle_abts(struct scsi_qla_host *vha,
2071
	struct abts_recv_from_24xx *abts, struct fc_port *sess)
2072 2073 2074
{
	struct qla_hw_data *ha = vha->hw;
	struct qla_tgt_mgmt_cmd *mcmd;
2075
	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
2076

2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf00f,
	    "qla_target(%d): task abort (tag=%d)\n",
	    vha->vp_idx, abts->exchange_addr_to_abort);

	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
	if (mcmd == NULL) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf051,
		    "qla_target(%d): %s: Allocation of ABORT cmd failed",
		    vha->vp_idx, __func__);
		return -ENOMEM;
	}
	memset(mcmd, 0, sizeof(*mcmd));
2089
	mcmd->cmd_type = TYPE_TGT_TMCMD;
2090 2091
	mcmd->sess = sess;
	memcpy(&mcmd->orig_iocb.abts, abts, sizeof(mcmd->orig_iocb.abts));
2092
	mcmd->reset_count = ha->base_qpair->chip_reset;
2093
	mcmd->tmr_func = QLA_TGT_ABTS;
2094
	mcmd->qpair = h->qpair;
2095
	mcmd->vha = vha;
2096

2097 2098 2099 2100
	/*
	 * LUN is looked up by target-core internally based on the passed
	 * abts->exchange_addr_to_abort tag.
	 */
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110
	mcmd->se_cmd.cpuid = h->cpuid;

	if (ha->tgt.tgt_ops->find_cmd_by_tag) {
		struct qla_tgt_cmd *abort_cmd;

		abort_cmd = ha->tgt.tgt_ops->find_cmd_by_tag(sess,
		    abts->exchange_addr_to_abort);
		if (abort_cmd && abort_cmd->qpair) {
			mcmd->qpair = abort_cmd->qpair;
			mcmd->se_cmd.cpuid = abort_cmd->se_cmd.cpuid;
2111 2112
			mcmd->abort_io_attr = abort_cmd->atio.u.isp24.attr;
			mcmd->flags = QLA24XX_MGMT_ABORT_IO_ATTR_VALID;
2113
		}
2114 2115
	}

2116 2117 2118
	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq, &mcmd->work);

2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
	return 0;
}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
static void qlt_24xx_handle_abts(struct scsi_qla_host *vha,
	struct abts_recv_from_24xx *abts)
{
	struct qla_hw_data *ha = vha->hw;
2129
	struct fc_port *sess;
2130
	uint32_t tag = abts->exchange_addr_to_abort;
2131
	be_id_t s_id;
2132
	int rc;
2133
	unsigned long flags;
2134 2135 2136 2137 2138

	if (le32_to_cpu(abts->fcp_hdr_le.parameter) & ABTS_PARAM_ABORT_SEQ) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf053,
		    "qla_target(%d): ABTS: Abort Sequence not "
		    "supported\n", vha->vp_idx);
2139 2140
		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
		    false);
2141 2142 2143 2144 2145 2146 2147
		return;
	}

	if (tag == ATIO_EXCHANGE_ADDRESS_UNKNOWN) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf010,
		    "qla_target(%d): ABTS: Unknown Exchange "
		    "Address received\n", vha->vp_idx);
2148 2149
		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
		    false);
2150 2151 2152 2153 2154
		return;
	}

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf011,
	    "qla_target(%d): task abort (s_id=%x:%x:%x, "
2155 2156
	    "tag=%d, param=%x)\n", vha->vp_idx, abts->fcp_hdr_le.s_id.domain,
	    abts->fcp_hdr_le.s_id.area, abts->fcp_hdr_le.s_id.al_pa, tag,
2157 2158
	    le32_to_cpu(abts->fcp_hdr_le.parameter));

2159
	s_id = le_id_to_be(abts->fcp_hdr_le.s_id);
2160

2161
	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
2162 2163 2164
	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
	if (!sess) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf012,
2165
		    "qla_target(%d): task abort for non-existent session\n",
2166
		    vha->vp_idx);
2167 2168
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);

2169 2170
		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
			    false);
2171 2172
		return;
	}
2173 2174
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);

2175

2176
	if (sess->deleted) {
2177 2178
		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
		    false);
2179 2180 2181
		return;
	}

2182 2183 2184 2185 2186
	rc = __qlt_24xx_handle_abts(vha, abts, sess);
	if (rc != 0) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf054,
		    "qla_target(%d): __qlt_24xx_handle_abts() failed: %d\n",
		    vha->vp_idx, rc);
2187 2188
		qlt_24xx_send_abts_resp(ha->base_qpair, abts, FCP_TMF_REJECTED,
		    false);
2189 2190 2191 2192 2193 2194 2195
		return;
	}
}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
2196
static void qlt_24xx_send_task_mgmt_ctio(struct qla_qpair *qpair,
2197 2198
	struct qla_tgt_mgmt_cmd *mcmd, uint32_t resp_code)
{
2199
	struct scsi_qla_host *ha = mcmd->vha;
2200 2201
	struct atio_from_isp *atio = &mcmd->orig_iocb.atio;
	struct ctio7_to_24xx *ctio;
2202
	uint16_t temp;
2203 2204 2205 2206 2207 2208

	ql_dbg(ql_dbg_tgt, ha, 0xe008,
	    "Sending task mgmt CTIO7 (ha=%p, atio=%p, resp_code=%x\n",
	    ha, atio, resp_code);


2209
	ctio = (struct ctio7_to_24xx *)__qla2x00_alloc_iocbs(qpair, NULL);
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
	if (ctio == NULL) {
		ql_dbg(ql_dbg_tgt, ha, 0xe04c,
		    "qla_target(%d): %s failed: unable to allocate "
		    "request packet\n", ha->vp_idx, __func__);
		return;
	}

	ctio->entry_type = CTIO_TYPE7;
	ctio->entry_count = 1;
	ctio->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;
	ctio->nport_handle = mcmd->sess->loop_id;
2221
	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2222
	ctio->vp_index = ha->vp_idx;
2223
	ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2224
	ctio->exchange_addr = atio->u.isp24.exchange_addr;
2225 2226 2227
	temp = (atio->u.isp24.attr << 9)|
		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
	ctio->u.status1.flags = cpu_to_le16(temp);
2228 2229
	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
	ctio->u.status1.ox_id = cpu_to_le16(temp);
2230
	ctio->u.status1.scsi_status =
2231 2232
	    cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID);
	ctio->u.status1.response_len = cpu_to_le16(8);
2233
	ctio->u.status1.sense_data[0] = resp_code;
2234

2235 2236
	/* Memory Barrier */
	wmb();
2237 2238 2239 2240
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(ha, qpair->req);
2241 2242 2243 2244 2245 2246 2247 2248
}

void qlt_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
{
	mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
}
EXPORT_SYMBOL(qlt_free_mcmd);

2249 2250 2251 2252
/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then
 * reacquire
 */
2253
void qlt_send_resp_ctio(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
2254 2255 2256 2257 2258
    uint8_t scsi_status, uint8_t sense_key, uint8_t asc, uint8_t ascq)
{
	struct atio_from_isp *atio = &cmd->atio;
	struct ctio7_to_24xx *ctio;
	uint16_t temp;
2259
	struct scsi_qla_host *vha = cmd->vha;
2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279

	ql_dbg(ql_dbg_tgt_dif, vha, 0x3066,
	    "Sending response CTIO7 (vha=%p, atio=%p, scsi_status=%02x, "
	    "sense_key=%02x, asc=%02x, ascq=%02x",
	    vha, atio, scsi_status, sense_key, asc, ascq);

	ctio = (struct ctio7_to_24xx *)qla2x00_alloc_iocbs(vha, NULL);
	if (!ctio) {
		ql_dbg(ql_dbg_async, vha, 0x3067,
		    "qla2x00t(%ld): %s failed: unable to allocate request packet",
		    vha->host_no, __func__);
		goto out;
	}

	ctio->entry_type = CTIO_TYPE7;
	ctio->entry_count = 1;
	ctio->handle = QLA_TGT_SKIP_HANDLE;
	ctio->nport_handle = cmd->sess->loop_id;
	ctio->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
	ctio->vp_index = vha->vp_idx;
2280
	ctio->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2281
	ctio->exchange_addr = atio->u.isp24.exchange_addr;
2282 2283 2284
	temp = (atio->u.isp24.attr << 9) |
	    CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS;
	ctio->u.status1.flags = cpu_to_le16(temp);
2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295
	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
	ctio->u.status1.ox_id = cpu_to_le16(temp);
	ctio->u.status1.scsi_status =
	    cpu_to_le16(SS_RESPONSE_INFO_LEN_VALID | scsi_status);
	ctio->u.status1.response_len = cpu_to_le16(18);
	ctio->u.status1.residual = cpu_to_le32(get_datalen_for_atio(atio));

	if (ctio->u.status1.residual != 0)
		ctio->u.status1.scsi_status |=
		    cpu_to_le16(SS_RESIDUAL_UNDER);

2296 2297 2298
	/* Fixed format sense data. */
	ctio->u.status1.sense_data[0] = 0x70;
	ctio->u.status1.sense_data[2] = sense_key;
2299
	/* Additional sense length */
2300
	ctio->u.status1.sense_data[7] = 0xa;
2301
	/* ASC and ASCQ */
2302 2303
	ctio->u.status1.sense_data[12] = asc;
	ctio->u.status1.sense_data[13] = ascq;
2304 2305 2306 2307

	/* Memory Barrier */
	wmb();

2308 2309 2310 2311 2312
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(vha, qpair->req);

2313 2314 2315 2316
out:
	return;
}

2317 2318 2319 2320 2321 2322
/* callback from target fabric module code */
void qlt_xmit_tm_rsp(struct qla_tgt_mgmt_cmd *mcmd)
{
	struct scsi_qla_host *vha = mcmd->sess->vha;
	struct qla_hw_data *ha = vha->hw;
	unsigned long flags;
2323
	struct qla_qpair *qpair = mcmd->qpair;
2324
	bool free_mcmd = true;
2325 2326 2327 2328 2329

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf013,
	    "TM response mcmd (%p) status %#x state %#x",
	    mcmd, mcmd->fc_tm_rsp, mcmd->flags);

2330
	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
2331

2332
	if (!vha->flags.online || mcmd->reset_count != qpair->chip_reset) {
2333
		/*
2334
		 * Either the port is not online or this request was from
2335 2336 2337
		 * previous life, just abort the processing.
		 */
		ql_dbg(ql_dbg_async, vha, 0xe100,
2338 2339
			"RESET-TMR online/active/old-count/new-count = %d/%d/%d/%d.\n",
			vha->flags.online, qla2x00_reset_active(vha),
2340
			mcmd->reset_count, qpair->chip_reset);
2341
		ha->tgt.tgt_ops->free_mcmd(mcmd);
2342
		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2343 2344 2345
		return;
	}

2346
	if (mcmd->flags == QLA24XX_MGMT_SEND_NACK) {
2347 2348 2349 2350
		switch (mcmd->orig_iocb.imm_ntfy.u.isp24.status_subcode) {
		case ELS_LOGO:
		case ELS_PRLO:
		case ELS_TPRLO:
2351
			ql_dbg(ql_dbg_disc, vha, 0x2106,
2352
			    "TM response logo %8phC status %#x state %#x",
2353 2354
			    mcmd->sess->port_name, mcmd->fc_tm_rsp,
			    mcmd->flags);
2355
			qlt_schedule_sess_for_deletion(mcmd->sess);
2356 2357
			break;
		default:
2358 2359
			qlt_send_notify_ack(vha->hw->base_qpair,
			    &mcmd->orig_iocb.imm_ntfy, 0, 0, 0, 0, 0, 0);
2360
			break;
2361 2362
		}
	} else {
2363 2364 2365 2366
		if (mcmd->orig_iocb.atio.u.raw.entry_type == ABTS_RECV_24XX) {
			qlt_build_abts_resp_iocb(mcmd);
			free_mcmd = false;
		} else
2367
			qlt_24xx_send_task_mgmt_ctio(qpair, mcmd,
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377
			    mcmd->fc_tm_rsp);
	}
	/*
	 * Make the callback for ->free_mcmd() to queue_work() and invoke
	 * target_put_sess_cmd() to drop cmd_kref to 1.  The final
	 * target_put_sess_cmd() call will be made from TFO->check_stop_free()
	 * -> tcm_qla2xxx_check_stop_free() to release the TMR associated se_cmd
	 * descriptor after TFO->queue_tm_rsp() -> tcm_qla2xxx_queue_tm_rsp() ->
	 * qlt_xmit_tm_rsp() returns here..
	 */
2378 2379 2380
	if (free_mcmd)
		ha->tgt.tgt_ops->free_mcmd(mcmd);

2381
	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392
}
EXPORT_SYMBOL(qlt_xmit_tm_rsp);

/* No locks */
static int qlt_pci_map_calc_cnt(struct qla_tgt_prm *prm)
{
	struct qla_tgt_cmd *cmd = prm->cmd;

	BUG_ON(cmd->sg_cnt == 0);

	prm->sg = (struct scatterlist *)cmd->sg;
2393
	prm->seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev, cmd->sg,
2394 2395 2396 2397 2398 2399
	    cmd->sg_cnt, cmd->dma_data_direction);
	if (unlikely(prm->seg_cnt == 0))
		goto out_err;

	prm->cmd->sg_mapped = 1;

2400 2401 2402 2403 2404
	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL) {
		/*
		 * If greater than four sg entries then we need to allocate
		 * the continuation entries
		 */
2405
		if (prm->seg_cnt > QLA_TGT_DATASEGS_PER_CMD_24XX)
2406
			prm->req_cnt += DIV_ROUND_UP(prm->seg_cnt -
2407 2408
			QLA_TGT_DATASEGS_PER_CMD_24XX,
			QLA_TGT_DATASEGS_PER_CONT_24XX);
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
	} else {
		/* DIF */
		if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
		    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
			prm->seg_cnt = DIV_ROUND_UP(cmd->bufflen, cmd->blk_sz);
			prm->tot_dsds = prm->seg_cnt;
		} else
			prm->tot_dsds = prm->seg_cnt;

		if (cmd->prot_sg_cnt) {
			prm->prot_sg      = cmd->prot_sg;
2420
			prm->prot_seg_cnt = dma_map_sg(&cmd->qpair->pdev->dev,
2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435
				cmd->prot_sg, cmd->prot_sg_cnt,
				cmd->dma_data_direction);
			if (unlikely(prm->prot_seg_cnt == 0))
				goto out_err;

			if ((cmd->se_cmd.prot_op == TARGET_PROT_DIN_INSERT) ||
			    (cmd->se_cmd.prot_op == TARGET_PROT_DOUT_STRIP)) {
				/* Dif Bundling not support here */
				prm->prot_seg_cnt = DIV_ROUND_UP(cmd->bufflen,
								cmd->blk_sz);
				prm->tot_dsds += prm->prot_seg_cnt;
			} else
				prm->tot_dsds += prm->prot_seg_cnt;
		}
	}
2436 2437 2438 2439

	return 0;

out_err:
2440
	ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe04d,
2441 2442 2443 2444 2445
	    "qla_target(%d): PCI mapping failed: sg_cnt=%d",
	    0, prm->cmd->sg_cnt);
	return -1;
}

J
Joern Engel 已提交
2446
static void qlt_unmap_sg(struct scsi_qla_host *vha, struct qla_tgt_cmd *cmd)
2447
{
2448 2449
	struct qla_hw_data *ha;
	struct qla_qpair *qpair;
2450

J
Joern Engel 已提交
2451 2452 2453
	if (!cmd->sg_mapped)
		return;

2454 2455
	qpair = cmd->qpair;

2456
	dma_unmap_sg(&qpair->pdev->dev, cmd->sg, cmd->sg_cnt,
2457
	    cmd->dma_data_direction);
2458
	cmd->sg_mapped = 0;
2459 2460

	if (cmd->prot_sg_cnt)
2461
		dma_unmap_sg(&qpair->pdev->dev, cmd->prot_sg, cmd->prot_sg_cnt,
2462 2463
			cmd->dma_data_direction);

2464 2465
	if (!cmd->ctx)
		return;
2466
	ha = vha->hw;
2467
	if (cmd->ctx_dsd_alloced)
2468
		qla2x00_clean_dsd_pool(ha, cmd->ctx);
2469

2470
	dma_pool_free(ha->dl_dma_pool, cmd->ctx, cmd->ctx->crc_ctx_dma);
2471 2472
}

2473
static int qlt_check_reserve_free_req(struct qla_qpair *qpair,
2474 2475
	uint32_t req_cnt)
{
2476
	uint32_t cnt;
2477
	struct req_que *req = qpair->req;
2478

2479
	if (req->cnt < (req_cnt + 2)) {
2480 2481
		cnt = (uint16_t)(qpair->use_shadow_reg ? *req->out_ptr :
		    RD_REG_DWORD_RELAXED(req->req_q_out));
2482

2483 2484
		if  (req->ring_index < cnt)
			req->cnt = cnt - req->ring_index;
2485
		else
2486
			req->cnt = req->length - (req->ring_index - cnt);
2487

2488
		if (unlikely(req->cnt < (req_cnt + 2)))
2489
			return -EAGAIN;
2490
	}
2491

2492
	req->cnt -= req_cnt;
2493 2494 2495 2496 2497 2498 2499

	return 0;
}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
2500
static inline void *qlt_get_req_pkt(struct req_que *req)
2501 2502
{
	/* Adjust ring index. */
2503 2504 2505 2506
	req->ring_index++;
	if (req->ring_index == req->length) {
		req->ring_index = 0;
		req->ring_ptr = req->ring;
2507
	} else {
2508
		req->ring_ptr++;
2509
	}
2510
	return (cont_entry_t *)req->ring_ptr;
2511 2512 2513
}

/* ha->hardware_lock supposed to be held on entry */
2514
static inline uint32_t qlt_make_handle(struct qla_qpair *qpair)
2515 2516
{
	uint32_t h;
2517 2518
	int index;
	uint8_t found = 0;
2519
	struct req_que *req = qpair->req;
2520 2521

	h = req->current_outstanding_cmd;
2522

2523 2524 2525 2526 2527 2528 2529 2530 2531 2532
	for (index = 1; index < req->num_outstanding_cmds; index++) {
		h++;
		if (h == req->num_outstanding_cmds)
			h = 1;

		if (h == QLA_TGT_SKIP_HANDLE)
			continue;

		if (!req->outstanding_cmds[h]) {
			found = 1;
2533 2534
			break;
		}
2535
	}
2536

2537 2538 2539
	if (found) {
		req->current_outstanding_cmd = h;
	} else {
2540 2541 2542
		ql_dbg(ql_dbg_io, qpair->vha, 0x305b,
		    "qla_target(%d): Ran out of empty cmd slots\n",
		    qpair->vha->vp_idx);
2543 2544
		h = QLA_TGT_NULL_HANDLE;
	}
2545 2546 2547 2548 2549

	return h;
}

/* ha->hardware_lock supposed to be held on entry */
2550 2551
static int qlt_24xx_build_ctio_pkt(struct qla_qpair *qpair,
	struct qla_tgt_prm *prm)
2552 2553 2554 2555
{
	uint32_t h;
	struct ctio7_to_24xx *pkt;
	struct atio_from_isp *atio = &prm->cmd->atio;
2556
	uint16_t temp;
2557

2558
	pkt = (struct ctio7_to_24xx *)qpair->req->ring_ptr;
2559 2560 2561 2562 2563
	prm->pkt = pkt;
	memset(pkt, 0, sizeof(*pkt));

	pkt->entry_type = CTIO_TYPE7;
	pkt->entry_count = (uint8_t)prm->req_cnt;
2564
	pkt->vp_index = prm->cmd->vp_idx;
2565

2566
	h = qlt_make_handle(qpair);
2567 2568 2569 2570 2571 2572 2573
	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
		/*
		 * CTIO type 7 from the firmware doesn't provide a way to
		 * know the initiator's LOOP ID, hence we can't find
		 * the session and, so, the command.
		 */
		return -EAGAIN;
2574 2575
	} else
		qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
2576

2577 2578 2579
	pkt->handle = MAKE_HANDLE(qpair->req->id, h);
	pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
	pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
2580
	pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
2581
	pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
2582
	pkt->exchange_addr = atio->u.isp24.exchange_addr;
2583 2584
	temp = atio->u.isp24.attr << 9;
	pkt->u.status0.flags |= cpu_to_le16(temp);
2585 2586
	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
	pkt->u.status0.ox_id = cpu_to_le16(temp);
2587 2588 2589 2590 2591 2592 2593 2594 2595
	pkt->u.status0.relative_offset = cpu_to_le32(prm->cmd->offset);

	return 0;
}

/*
 * ha->hardware_lock supposed to be held on entry. We have already made sure
 * that there is sufficient amount of request entries to not drop it.
 */
2596
static void qlt_load_cont_data_segments(struct qla_tgt_prm *prm)
2597 2598
{
	int cnt;
2599
	struct dsd64 *cur_dsd;
2600 2601 2602 2603

	/* Build continuation packets */
	while (prm->seg_cnt > 0) {
		cont_a64_entry_t *cont_pkt64 =
2604 2605
			(cont_a64_entry_t *)qlt_get_req_pkt(
			   prm->cmd->qpair->req);
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618

		/*
		 * Make sure that from cont_pkt64 none of
		 * 64-bit specific fields used for 32-bit
		 * addressing. Cast to (cont_entry_t *) for
		 * that.
		 */

		memset(cont_pkt64, 0, sizeof(*cont_pkt64));

		cont_pkt64->entry_count = 1;
		cont_pkt64->sys_define = 0;

2619
		cont_pkt64->entry_type = CONTINUE_A64_TYPE;
2620
		cur_dsd = cont_pkt64->dsd;
2621 2622 2623

		/* Load continuation entry data segments */
		for (cnt = 0;
2624
		    cnt < QLA_TGT_DATASEGS_PER_CONT_24XX && prm->seg_cnt;
2625
		    cnt++, prm->seg_cnt--) {
2626
			append_dsd64(&cur_dsd, prm->sg);
2627 2628 2629 2630 2631 2632 2633 2634 2635
			prm->sg = sg_next(prm->sg);
		}
	}
}

/*
 * ha->hardware_lock supposed to be held on entry. We have already made sure
 * that there is sufficient amount of request entries to not drop it.
 */
2636
static void qlt_load_data_segments(struct qla_tgt_prm *prm)
2637 2638
{
	int cnt;
2639
	struct dsd64 *cur_dsd;
2640 2641 2642 2643 2644
	struct ctio7_to_24xx *pkt24 = (struct ctio7_to_24xx *)prm->pkt;

	pkt24->u.status0.transfer_length = cpu_to_le32(prm->cmd->bufflen);

	/* Setup packet address segment pointer */
2645
	cur_dsd = &pkt24->u.status0.dsd;
2646 2647 2648 2649 2650 2651 2652

	/* Set total data segment count */
	if (prm->seg_cnt)
		pkt24->dseg_count = cpu_to_le16(prm->seg_cnt);

	if (prm->seg_cnt == 0) {
		/* No data transfer */
2653 2654
		cur_dsd->address = 0;
		cur_dsd->length = 0;
2655 2656 2657 2658 2659 2660 2661
		return;
	}

	/* If scatter gather */

	/* Load command entry data segments */
	for (cnt = 0;
2662
	    (cnt < QLA_TGT_DATASEGS_PER_CMD_24XX) && prm->seg_cnt;
2663
	    cnt++, prm->seg_cnt--) {
2664
		append_dsd64(&cur_dsd, prm->sg);
2665 2666 2667
		prm->sg = sg_next(prm->sg);
	}

2668
	qlt_load_cont_data_segments(prm);
2669 2670 2671 2672 2673 2674 2675
}

static inline int qlt_has_data(struct qla_tgt_cmd *cmd)
{
	return cmd->bufflen > 0;
}

2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687
static void qlt_print_dif_err(struct qla_tgt_prm *prm)
{
	struct qla_tgt_cmd *cmd;
	struct scsi_qla_host *vha;

	/* asc 0x10=dif error */
	if (prm->sense_buffer && (prm->sense_buffer[12] == 0x10)) {
		cmd = prm->cmd;
		vha = cmd->vha;
		/* ASCQ */
		switch (prm->sense_buffer[13]) {
		case 1:
2688
			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00b,
2689 2690 2691 2692 2693 2694
			    "BE detected Guard TAG ERR: lba[0x%llx|%lld] len[0x%x] "
			    "se_cmd=%p tag[%x]",
			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
			    cmd->atio.u.isp24.exchange_addr);
			break;
		case 2:
2695
			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00c,
2696 2697 2698 2699 2700 2701
			    "BE detected APP TAG ERR: lba[0x%llx|%lld] len[0x%x] "
			    "se_cmd=%p tag[%x]",
			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
			    cmd->atio.u.isp24.exchange_addr);
			break;
		case 3:
2702
			ql_dbg(ql_dbg_tgt_dif, vha, 0xe00f,
2703 2704 2705 2706 2707 2708
			    "BE detected REF TAG ERR: lba[0x%llx|%lld] len[0x%x] "
			    "se_cmd=%p tag[%x]",
			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
			    cmd->atio.u.isp24.exchange_addr);
			break;
		default:
2709
			ql_dbg(ql_dbg_tgt_dif, vha, 0xe010,
2710 2711 2712 2713 2714 2715
			    "BE detected Dif ERR: lba[%llx|%lld] len[%x] "
			    "se_cmd=%p tag[%x]",
			    cmd->lba, cmd->lba, cmd->num_blks, &cmd->se_cmd,
			    cmd->atio.u.isp24.exchange_addr);
			break;
		}
2716
		ql_dump_buffer(ql_dbg_tgt_dif, vha, 0xe011, cmd->cdb, 16);
2717 2718 2719
	}
}

2720 2721 2722 2723 2724 2725 2726 2727
/*
 * Called without ha->hardware_lock held
 */
static int qlt_pre_xmit_response(struct qla_tgt_cmd *cmd,
	struct qla_tgt_prm *prm, int xmit_type, uint8_t scsi_status,
	uint32_t *full_req_cnt)
{
	struct se_cmd *se_cmd = &cmd->se_cmd;
2728
	struct qla_qpair *qpair = cmd->qpair;
2729 2730

	prm->cmd = cmd;
2731 2732
	prm->tgt = cmd->tgt;
	prm->pkt = NULL;
2733 2734 2735 2736 2737 2738
	prm->rq_result = scsi_status;
	prm->sense_buffer = &cmd->sense_buffer[0];
	prm->sense_buffer_len = TRANSPORT_SENSE_BUFFER;
	prm->sg = NULL;
	prm->seg_cnt = -1;
	prm->req_cnt = 1;
2739
	prm->residual = 0;
2740
	prm->add_status_pkt = 0;
2741 2742 2743
	prm->prot_sg = NULL;
	prm->prot_seg_cnt = 0;
	prm->tot_dsds = 0;
2744 2745 2746 2747 2748 2749 2750 2751 2752 2753

	if ((xmit_type & QLA_TGT_XMIT_DATA) && qlt_has_data(cmd)) {
		if  (qlt_pci_map_calc_cnt(prm) != 0)
			return -EAGAIN;
	}

	*full_req_cnt = prm->req_cnt;

	if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
		prm->residual = se_cmd->residual_count;
2754
		ql_dbg_qp(ql_dbg_io + ql_dbg_verbose, qpair, 0x305c,
2755 2756 2757 2758
		    "Residual underflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
		       prm->residual, se_cmd->tag,
		       se_cmd->t_task_cdb ? se_cmd->t_task_cdb[0] : 0,
		       cmd->bufflen, prm->rq_result);
2759 2760 2761
		prm->rq_result |= SS_RESIDUAL_UNDER;
	} else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
		prm->residual = se_cmd->residual_count;
2762
		ql_dbg_qp(ql_dbg_io, qpair, 0x305d,
2763 2764 2765
		    "Residual overflow: %d (tag %lld, op %x, bufflen %d, rq_result %x)\n",
		       prm->residual, se_cmd->tag, se_cmd->t_task_cdb ?
		       se_cmd->t_task_cdb[0] : 0, cmd->bufflen, prm->rq_result);
2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
		prm->rq_result |= SS_RESIDUAL_OVER;
	}

	if (xmit_type & QLA_TGT_XMIT_STATUS) {
		/*
		 * If QLA_TGT_XMIT_DATA is not set, add_status_pkt will be
		 * ignored in *xmit_response() below
		 */
		if (qlt_has_data(cmd)) {
			if (QLA_TGT_SENSE_VALID(prm->sense_buffer) ||
2776
			    (IS_FWI2_CAPABLE(cmd->vha->hw) &&
2777 2778 2779 2780 2781 2782 2783 2784 2785 2786
			    (prm->rq_result != 0))) {
				prm->add_status_pkt = 1;
				(*full_req_cnt)++;
			}
		}
	}

	return 0;
}

2787 2788
static inline int qlt_need_explicit_conf(struct qla_tgt_cmd *cmd,
    int sending_sense)
2789
{
2790
	if (cmd->qpair->enable_class_2)
2791 2792 2793 2794 2795
		return 0;

	if (sending_sense)
		return cmd->conf_compl_supported;
	else
2796 2797
		return cmd->qpair->enable_explicit_conf &&
                    cmd->conf_compl_supported;
2798 2799 2800 2801 2802 2803 2804
}

static void qlt_24xx_init_ctio_to_isp(struct ctio7_to_24xx *ctio,
	struct qla_tgt_prm *prm)
{
	prm->sense_buffer_len = min_t(uint32_t, prm->sense_buffer_len,
	    (uint32_t)sizeof(ctio->u.status1.sense_data));
2805
	ctio->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_SEND_STATUS);
2806
	if (qlt_need_explicit_conf(prm->cmd, 0)) {
2807
		ctio->u.status0.flags |= cpu_to_le16(
2808 2809 2810 2811 2812 2813 2814 2815
		    CTIO7_FLAGS_EXPLICIT_CONFORM |
		    CTIO7_FLAGS_CONFORM_REQ);
	}
	ctio->u.status0.residual = cpu_to_le32(prm->residual);
	ctio->u.status0.scsi_status = cpu_to_le16(prm->rq_result);
	if (QLA_TGT_SENSE_VALID(prm->sense_buffer)) {
		int i;

2816
		if (qlt_need_explicit_conf(prm->cmd, 1)) {
2817
			if ((prm->rq_result & SS_SCSI_STATUS_BYTE) != 0) {
2818
				ql_dbg_qp(ql_dbg_tgt, prm->cmd->qpair, 0xe017,
2819 2820 2821 2822 2823
				    "Skipping EXPLICIT_CONFORM and "
				    "CTIO7_FLAGS_CONFORM_REQ for FCP READ w/ "
				    "non GOOD status\n");
				goto skip_explict_conf;
			}
2824
			ctio->u.status1.flags |= cpu_to_le16(
2825 2826 2827 2828 2829
			    CTIO7_FLAGS_EXPLICIT_CONFORM |
			    CTIO7_FLAGS_CONFORM_REQ);
		}
skip_explict_conf:
		ctio->u.status1.flags &=
2830
		    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2831
		ctio->u.status1.flags |=
2832
		    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2833
		ctio->u.status1.scsi_status |=
2834
		    cpu_to_le16(SS_SENSE_LEN_VALID);
2835 2836 2837 2838 2839
		ctio->u.status1.sense_length =
		    cpu_to_le16(prm->sense_buffer_len);
		for (i = 0; i < prm->sense_buffer_len/4; i++)
			((uint32_t *)ctio->u.status1.sense_data)[i] =
				cpu_to_be32(((uint32_t *)prm->sense_buffer)[i]);
2840 2841 2842

		qlt_print_dif_err(prm);

2843 2844
	} else {
		ctio->u.status1.flags &=
2845
		    ~cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_0);
2846
		ctio->u.status1.flags |=
2847
		    cpu_to_le16(CTIO7_FLAGS_STATUS_MODE_1);
2848 2849 2850 2851 2852 2853 2854 2855
		ctio->u.status1.sense_length = 0;
		memset(ctio->u.status1.sense_data, 0,
		    sizeof(ctio->u.status1.sense_data));
	}

	/* Sense with len > 24, is it possible ??? */
}

2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878
static inline int
qlt_hba_err_chk_enabled(struct se_cmd *se_cmd)
{
	switch (se_cmd->prot_op) {
	case TARGET_PROT_DOUT_INSERT:
	case TARGET_PROT_DIN_STRIP:
		if (ql2xenablehba_err_chk >= 1)
			return 1;
		break;
	case TARGET_PROT_DOUT_PASS:
	case TARGET_PROT_DIN_PASS:
		if (ql2xenablehba_err_chk >= 2)
			return 1;
		break;
	case TARGET_PROT_DIN_INSERT:
	case TARGET_PROT_DOUT_STRIP:
		return 1;
	default:
		break;
	}
	return 0;
}

2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
static inline int
qla_tgt_ref_mask_check(struct se_cmd *se_cmd)
{
	switch (se_cmd->prot_op) {
	case TARGET_PROT_DIN_INSERT:
	case TARGET_PROT_DOUT_INSERT:
	case TARGET_PROT_DIN_STRIP:
	case TARGET_PROT_DOUT_STRIP:
	case TARGET_PROT_DIN_PASS:
	case TARGET_PROT_DOUT_PASS:
	    return 1;
	default:
	    return 0;
	}
	return 0;
}

2896
/*
2897
 * qla_tgt_set_dif_tags - Extract Ref and App tags from SCSI command
2898
 */
2899 2900 2901
static void
qla_tgt_set_dif_tags(struct qla_tgt_cmd *cmd, struct crc_context *ctx,
    uint16_t *pfw_prot_opts)
2902
{
2903
	struct se_cmd *se_cmd = &cmd->se_cmd;
2904
	uint32_t lba = 0xffffffff & se_cmd->t_task_lba;
2905 2906 2907
	scsi_qla_host_t *vha = cmd->tgt->vha;
	struct qla_hw_data *ha = vha->hw;
	uint32_t t32 = 0;
2908

2909 2910
	/*
	 * wait till Mode Sense/Select cmd, modepage Ah, subpage 2
2911 2912 2913
	 * have been immplemented by TCM, before AppTag is avail.
	 * Look for modesense_handlers[]
	 */
2914
	ctx->app_tag = 0;
2915 2916 2917
	ctx->app_tag_mask[0] = 0x0;
	ctx->app_tag_mask[1] = 0x0;

2918 2919 2920 2921 2922 2923 2924 2925 2926 2927
	if (IS_PI_UNINIT_CAPABLE(ha)) {
		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
			*pfw_prot_opts |= PO_DIS_VALD_APP_ESC;
		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
			*pfw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
	}

	t32 = ha->tgt.tgt_ops->get_dif_tags(cmd, pfw_prot_opts);

2928 2929 2930
	switch (se_cmd->prot_type) {
	case TARGET_DIF_TYPE0_PROT:
		/*
2931 2932 2933
		 * No check for ql2xenablehba_err_chk, as it
		 * would be an I/O error if hba tag generation
		 * is not done.
2934 2935 2936 2937 2938 2939 2940 2941 2942
		 */
		ctx->ref_tag = cpu_to_le32(lba);
		/* enable ALL bytes of the ref tag */
		ctx->ref_tag_mask[0] = 0xff;
		ctx->ref_tag_mask[1] = 0xff;
		ctx->ref_tag_mask[2] = 0xff;
		ctx->ref_tag_mask[3] = 0xff;
		break;
	case TARGET_DIF_TYPE1_PROT:
2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958
	    /*
	     * For TYPE 1 protection: 16 bit GUARD tag, 32 bit
	     * REF tag, and 16 bit app tag.
	     */
	    ctx->ref_tag = cpu_to_le32(lba);
	    if (!qla_tgt_ref_mask_check(se_cmd) ||
		!(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
		    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
		    break;
	    }
	    /* enable ALL bytes of the ref tag */
	    ctx->ref_tag_mask[0] = 0xff;
	    ctx->ref_tag_mask[1] = 0xff;
	    ctx->ref_tag_mask[2] = 0xff;
	    ctx->ref_tag_mask[3] = 0xff;
	    break;
2959
	case TARGET_DIF_TYPE2_PROT:
2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975
	    /*
	     * For TYPE 2 protection: 16 bit GUARD + 32 bit REF
	     * tag has to match LBA in CDB + N
	     */
	    ctx->ref_tag = cpu_to_le32(lba);
	    if (!qla_tgt_ref_mask_check(se_cmd) ||
		!(ha->tgt.tgt_ops->chk_dif_tags(t32))) {
		    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
		    break;
	    }
	    /* enable ALL bytes of the ref tag */
	    ctx->ref_tag_mask[0] = 0xff;
	    ctx->ref_tag_mask[1] = 0xff;
	    ctx->ref_tag_mask[2] = 0xff;
	    ctx->ref_tag_mask[3] = 0xff;
	    break;
2976
	case TARGET_DIF_TYPE3_PROT:
2977 2978 2979 2980 2981
	    /* For TYPE 3 protection: 16 bit GUARD only */
	    *pfw_prot_opts |= PO_DIS_REF_TAG_VALD;
	    ctx->ref_tag_mask[0] = ctx->ref_tag_mask[1] =
		ctx->ref_tag_mask[2] = ctx->ref_tag_mask[3] = 0x00;
	    break;
2982 2983 2984 2985
	}
}

static inline int
2986
qlt_build_ctio_crc2_pkt(struct qla_qpair *qpair, struct qla_tgt_prm *prm)
2987
{
2988
	struct dsd64		*cur_dsd;
2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001
	uint32_t		transfer_length = 0;
	uint32_t		data_bytes;
	uint32_t		dif_bytes;
	uint8_t			bundling = 1;
	struct crc_context	*crc_ctx_pkt = NULL;
	struct qla_hw_data	*ha;
	struct ctio_crc2_to_fw	*pkt;
	dma_addr_t		crc_ctx_dma;
	uint16_t		fw_prot_opts = 0;
	struct qla_tgt_cmd	*cmd = prm->cmd;
	struct se_cmd		*se_cmd = &cmd->se_cmd;
	uint32_t h;
	struct atio_from_isp *atio = &prm->cmd->atio;
3002
	struct qla_tc_param	tc;
3003
	uint16_t t16;
3004
	scsi_qla_host_t *vha = cmd->vha;
3005 3006 3007

	ha = vha->hw;

3008
	pkt = (struct ctio_crc2_to_fw *)qpair->req->ring_ptr;
3009 3010 3011
	prm->pkt = pkt;
	memset(pkt, 0, sizeof(*pkt));

3012
	ql_dbg_qp(ql_dbg_tgt, cmd->qpair, 0xe071,
3013
		"qla_target(%d):%s: se_cmd[%p] CRC2 prot_op[0x%x] cmd prot sg:cnt[%p:%x] lba[%llu]\n",
3014
		cmd->vp_idx, __func__, se_cmd, se_cmd->prot_op,
3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028
		prm->prot_sg, prm->prot_seg_cnt, se_cmd->t_task_lba);

	if ((se_cmd->prot_op == TARGET_PROT_DIN_INSERT) ||
	    (se_cmd->prot_op == TARGET_PROT_DOUT_STRIP))
		bundling = 0;

	/* Compute dif len and adjust data len to incude protection */
	data_bytes = cmd->bufflen;
	dif_bytes  = (data_bytes / cmd->blk_sz) * 8;

	switch (se_cmd->prot_op) {
	case TARGET_PROT_DIN_INSERT:
	case TARGET_PROT_DOUT_STRIP:
		transfer_length = data_bytes;
3029 3030
		if (cmd->prot_sg_cnt)
			data_bytes += dif_bytes;
3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076
		break;
	case TARGET_PROT_DIN_STRIP:
	case TARGET_PROT_DOUT_INSERT:
	case TARGET_PROT_DIN_PASS:
	case TARGET_PROT_DOUT_PASS:
		transfer_length = data_bytes + dif_bytes;
		break;
	default:
		BUG();
		break;
	}

	if (!qlt_hba_err_chk_enabled(se_cmd))
		fw_prot_opts |= 0x10; /* Disable Guard tag checking */
	/* HBA error checking enabled */
	else if (IS_PI_UNINIT_CAPABLE(ha)) {
		if ((se_cmd->prot_type == TARGET_DIF_TYPE1_PROT) ||
		    (se_cmd->prot_type == TARGET_DIF_TYPE2_PROT))
			fw_prot_opts |= PO_DIS_VALD_APP_ESC;
		else if (se_cmd->prot_type == TARGET_DIF_TYPE3_PROT)
			fw_prot_opts |= PO_DIS_VALD_APP_REF_ESC;
	}

	switch (se_cmd->prot_op) {
	case TARGET_PROT_DIN_INSERT:
	case TARGET_PROT_DOUT_INSERT:
		fw_prot_opts |= PO_MODE_DIF_INSERT;
		break;
	case TARGET_PROT_DIN_STRIP:
	case TARGET_PROT_DOUT_STRIP:
		fw_prot_opts |= PO_MODE_DIF_REMOVE;
		break;
	case TARGET_PROT_DIN_PASS:
	case TARGET_PROT_DOUT_PASS:
		fw_prot_opts |= PO_MODE_DIF_PASS;
		/* FUTURE: does tcm require T10CRC<->IPCKSUM conversion? */
		break;
	default:/* Normal Request */
		fw_prot_opts |= PO_MODE_DIF_PASS;
		break;
	}

	/* ---- PKT ---- */
	/* Update entry type to indicate Command Type CRC_2 IOCB */
	pkt->entry_type  = CTIO_CRC2;
	pkt->entry_count = 1;
3077
	pkt->vp_index = cmd->vp_idx;
3078

3079
	h = qlt_make_handle(qpair);
3080 3081 3082 3083 3084 3085 3086 3087
	if (unlikely(h == QLA_TGT_NULL_HANDLE)) {
		/*
		 * CTIO type 7 from the firmware doesn't provide a way to
		 * know the initiator's LOOP ID, hence we can't find
		 * the session and, so, the command.
		 */
		return -EAGAIN;
	} else
3088
		qpair->req->outstanding_cmds[h] = (srb_t *)prm->cmd;
3089

3090 3091
	pkt->handle  = MAKE_HANDLE(qpair->req->id, h);
	pkt->handle |= CTIO_COMPLETION_HANDLE_MARK;
3092
	pkt->nport_handle = cpu_to_le16(prm->cmd->loop_id);
3093
	pkt->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3094
	pkt->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3095
	pkt->exchange_addr   = atio->u.isp24.exchange_addr;
3096 3097 3098 3099 3100 3101 3102

	/* silence compile warning */
	t16 = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
	pkt->ox_id  = cpu_to_le16(t16);

	t16 = (atio->u.isp24.attr << 9);
	pkt->flags |= cpu_to_le16(t16);
3103 3104 3105 3106
	pkt->relative_offset = cpu_to_le32(prm->cmd->offset);

	/* Set transfer direction */
	if (cmd->dma_data_direction == DMA_TO_DEVICE)
3107
		pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_IN);
3108
	else if (cmd->dma_data_direction == DMA_FROM_DEVICE)
3109
		pkt->flags = cpu_to_le16(CTIO7_FLAGS_DATA_OUT);
3110 3111 3112 3113 3114 3115 3116 3117 3118

	pkt->dseg_count = prm->tot_dsds;
	/* Fibre channel byte count */
	pkt->transfer_length = cpu_to_le32(transfer_length);

	/* ----- CRC context -------- */

	/* Allocate CRC context from global pool */
	crc_ctx_pkt = cmd->ctx =
3119
	    dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
3120 3121 3122 3123 3124 3125 3126 3127 3128 3129

	if (!crc_ctx_pkt)
		goto crc_queuing_error;

	crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
	INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);

	/* Set handle */
	crc_ctx_pkt->handle = pkt->handle;

3130
	qla_tgt_set_dif_tags(cmd, crc_ctx_pkt, &fw_prot_opts);
3131

3132
	put_unaligned_le64(crc_ctx_dma, &pkt->crc_context_address);
3133 3134 3135
	pkt->crc_context_len = CRC_CONTEXT_LEN_FW;

	if (!bundling) {
3136
		cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
3137 3138 3139 3140 3141 3142 3143 3144 3145
	} else {
		/*
		 * Configure Bundling if we need to fetch interlaving
		 * protection PCI accesses
		 */
		fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
		crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
		crc_ctx_pkt->u.bundling.dseg_count =
			cpu_to_le16(prm->tot_dsds - prm->prot_seg_cnt);
3146
		cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
3147 3148 3149 3150 3151 3152
	}

	/* Finish the common fields of CRC pkt */
	crc_ctx_pkt->blk_size   = cpu_to_le16(cmd->blk_sz);
	crc_ctx_pkt->prot_opts  = cpu_to_le16(fw_prot_opts);
	crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
3153
	crc_ctx_pkt->guard_seed = cpu_to_le16(0);
3154

3155 3156 3157 3158 3159 3160 3161 3162
	memset((uint8_t *)&tc, 0 , sizeof(tc));
	tc.vha = vha;
	tc.blk_sz = cmd->blk_sz;
	tc.bufflen = cmd->bufflen;
	tc.sg = cmd->sg;
	tc.prot_sg = cmd->prot_sg;
	tc.ctx = crc_ctx_pkt;
	tc.ctx_dsd_alloced = &cmd->ctx_dsd_alloced;
3163 3164

	/* Walks data segments */
3165
	pkt->flags |= cpu_to_le16(CTIO7_FLAGS_DSD_PTR);
3166 3167 3168

	if (!bundling && prm->prot_seg_cnt) {
		if (qla24xx_walk_and_build_sglist_no_difb(ha, NULL, cur_dsd,
3169
			prm->tot_dsds, &tc))
3170 3171
			goto crc_queuing_error;
	} else if (qla24xx_walk_and_build_sglist(ha, NULL, cur_dsd,
3172
		(prm->tot_dsds - prm->prot_seg_cnt), &tc))
3173 3174 3175 3176
		goto crc_queuing_error;

	if (bundling && prm->prot_seg_cnt) {
		/* Walks dif segments */
3177
		pkt->add_flags |= CTIO_CRC2_AF_DIF_DSD_ENA;
3178

3179
		cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
3180
		if (qla24xx_walk_and_build_prot_sglist(ha, NULL, cur_dsd,
3181
			prm->prot_seg_cnt, cmd))
3182 3183 3184 3185 3186 3187
			goto crc_queuing_error;
	}
	return QLA_SUCCESS;

crc_queuing_error:
	/* Cleanup will be performed by the caller */
3188
	qpair->req->outstanding_cmds[h] = NULL;
3189 3190 3191 3192

	return QLA_FUNCTION_FAILED;
}

3193 3194 3195 3196 3197 3198 3199 3200
/*
 * Callback to setup response of xmit_type of QLA_TGT_XMIT_DATA and *
 * QLA_TGT_XMIT_STATUS for >= 24xx silicon
 */
int qlt_xmit_response(struct qla_tgt_cmd *cmd, int xmit_type,
	uint8_t scsi_status)
{
	struct scsi_qla_host *vha = cmd->vha;
3201
	struct qla_qpair *qpair = cmd->qpair;
3202 3203 3204 3205 3206 3207
	struct ctio7_to_24xx *pkt;
	struct qla_tgt_prm prm;
	uint32_t full_req_cnt = 0;
	unsigned long flags = 0;
	int res;

3208 3209
	if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
	    (cmd->sess && cmd->sess->deleted)) {
3210 3211 3212 3213
		cmd->state = QLA_TGT_STATE_PROCESSED;
		return 0;
	}

3214
	ql_dbg_qp(ql_dbg_tgt, qpair, 0xe018,
3215
	    "is_send_status=%d, cmd->bufflen=%d, cmd->sg_cnt=%d, cmd->dma_data_direction=%d se_cmd[%p] qp %d\n",
3216 3217
	    (xmit_type & QLA_TGT_XMIT_STATUS) ?
	    1 : 0, cmd->bufflen, cmd->sg_cnt, cmd->dma_data_direction,
3218
	    &cmd->se_cmd, qpair->id);
3219 3220 3221 3222 3223 3224 3225

	res = qlt_pre_xmit_response(cmd, &prm, xmit_type, scsi_status,
	    &full_req_cnt);
	if (unlikely(res != 0)) {
		return res;
	}

3226
	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3227

3228
	if (xmit_type == QLA_TGT_XMIT_STATUS)
3229
		qpair->tgt_counters.core_qla_snd_status++;
3230
	else
3231
		qpair->tgt_counters.core_qla_que_buf++;
3232

3233
	if (!qpair->fw_started || cmd->reset_count != qpair->chip_reset) {
3234
		/*
3235
		 * Either the port is not online or this request was from
3236 3237 3238
		 * previous life, just abort the processing.
		 */
		cmd->state = QLA_TGT_STATE_PROCESSED;
3239
		ql_dbg_qp(ql_dbg_async, qpair, 0xe101,
3240 3241
			"RESET-RSP online/active/old-count/new-count = %d/%d/%d/%d.\n",
			vha->flags.online, qla2x00_reset_active(vha),
3242
			cmd->reset_count, qpair->chip_reset);
3243
		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3244 3245 3246
		return 0;
	}

3247
	/* Does F/W have an IOCBs for this request */
3248
	res = qlt_check_reserve_free_req(qpair, full_req_cnt);
3249 3250 3251
	if (unlikely(res))
		goto out_unmap_unlock;

3252
	if (cmd->se_cmd.prot_op && (xmit_type & QLA_TGT_XMIT_DATA))
3253
		res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3254
	else
3255
		res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3256
	if (unlikely(res != 0)) {
3257
		qpair->req->cnt += full_req_cnt;
3258
		goto out_unmap_unlock;
3259
	}
3260 3261 3262 3263 3264

	pkt = (struct ctio7_to_24xx *)prm.pkt;

	if (qlt_has_data(cmd) && (xmit_type & QLA_TGT_XMIT_DATA)) {
		pkt->u.status0.flags |=
3265
		    cpu_to_le16(CTIO7_FLAGS_DATA_IN |
3266 3267
			CTIO7_FLAGS_STATUS_MODE_0);

3268
		if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3269
			qlt_load_data_segments(&prm);
3270 3271 3272 3273 3274 3275 3276

		if (prm.add_status_pkt == 0) {
			if (xmit_type & QLA_TGT_XMIT_STATUS) {
				pkt->u.status0.scsi_status =
				    cpu_to_le16(prm.rq_result);
				pkt->u.status0.residual =
				    cpu_to_le32(prm.residual);
3277
				pkt->u.status0.flags |= cpu_to_le16(
3278
				    CTIO7_FLAGS_SEND_STATUS);
3279
				if (qlt_need_explicit_conf(cmd, 0)) {
3280
					pkt->u.status0.flags |=
3281
					    cpu_to_le16(
3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293
						CTIO7_FLAGS_EXPLICIT_CONFORM |
						CTIO7_FLAGS_CONFORM_REQ);
				}
			}

		} else {
			/*
			 * We have already made sure that there is sufficient
			 * amount of request entries to not drop HW lock in
			 * req_pkt().
			 */
			struct ctio7_to_24xx *ctio =
3294 3295
				(struct ctio7_to_24xx *)qlt_get_req_pkt(
				    qpair->req);
3296

3297
			ql_dbg_qp(ql_dbg_tgt, qpair, 0x305e,
3298 3299
			    "Building additional status packet 0x%p.\n",
			    ctio);
3300

3301 3302 3303 3304
			/*
			 * T10Dif: ctio_crc2_to_fw overlay ontop of
			 * ctio7_to_24xx
			 */
3305
			memcpy(ctio, pkt, sizeof(*ctio));
3306
			/* reset back to CTIO7 */
3307
			ctio->entry_count = 1;
3308
			ctio->entry_type = CTIO_TYPE7;
3309
			ctio->dseg_count = 0;
3310
			ctio->u.status1.flags &= ~cpu_to_le16(
3311 3312 3313 3314
			    CTIO7_FLAGS_DATA_IN);

			/* Real finish is ctio_m1's finish */
			pkt->handle |= CTIO_INTERMEDIATE_HANDLE_MARK;
3315
			pkt->u.status0.flags |= cpu_to_le16(
3316
			    CTIO7_FLAGS_DONT_RET_CTIO);
3317 3318 3319 3320 3321

			/* qlt_24xx_init_ctio_to_isp will correct
			 * all neccessary fields that's part of CTIO7.
			 * There should be no residual of CTIO-CRC2 data.
			 */
3322 3323 3324 3325 3326 3327 3328 3329
			qlt_24xx_init_ctio_to_isp((struct ctio7_to_24xx *)ctio,
			    &prm);
		}
	} else
		qlt_24xx_init_ctio_to_isp(pkt, &prm);


	cmd->state = QLA_TGT_STATE_PROCESSED; /* Mid-level is done processing */
3330
	cmd->cmd_sent_to_fw = 1;
3331
	cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3332

3333 3334
	/* Memory Barrier */
	wmb();
3335 3336 3337 3338
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(vha, qpair->req);
3339
	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3340 3341 3342 3343

	return 0;

out_unmap_unlock:
J
Joern Engel 已提交
3344
	qlt_unmap_sg(vha, cmd);
3345
	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356

	return res;
}
EXPORT_SYMBOL(qlt_xmit_response);

int qlt_rdy_to_xfer(struct qla_tgt_cmd *cmd)
{
	struct ctio7_to_24xx *pkt;
	struct scsi_qla_host *vha = cmd->vha;
	struct qla_tgt *tgt = cmd->tgt;
	struct qla_tgt_prm prm;
3357
	unsigned long flags = 0;
3358
	int res = 0;
3359
	struct qla_qpair *qpair = cmd->qpair;
3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370

	memset(&prm, 0, sizeof(prm));
	prm.cmd = cmd;
	prm.tgt = tgt;
	prm.sg = NULL;
	prm.req_cnt = 1;

	/* Calculate number of entries and segments required */
	if (qlt_pci_map_calc_cnt(&prm) != 0)
		return -EAGAIN;

3371
	if (!qpair->fw_started || (cmd->reset_count != qpair->chip_reset) ||
3372
	    (cmd->sess && cmd->sess->deleted)) {
3373
		/*
3374
		 * Either the port is not online or this request was from
3375 3376
		 * previous life, just abort the processing.
		 */
3377 3378 3379 3380
		cmd->aborted = 1;
		cmd->write_data_transferred = 0;
		cmd->state = QLA_TGT_STATE_DATA_IN;
		vha->hw->tgt.tgt_ops->handle_data(cmd);
3381
		ql_dbg_qp(ql_dbg_async, qpair, 0xe102,
3382 3383
			"RESET-XFR online/active/old-count/new-count = %d/%d/%d/%d.\n",
			vha->flags.online, qla2x00_reset_active(vha),
3384
			cmd->reset_count, qpair->chip_reset);
3385 3386 3387
		return 0;
	}

3388
	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
3389
	/* Does F/W have an IOCBs for this request */
3390
	res = qlt_check_reserve_free_req(qpair, prm.req_cnt);
3391 3392
	if (res != 0)
		goto out_unlock_free_unmap;
3393
	if (cmd->se_cmd.prot_op)
3394
		res = qlt_build_ctio_crc2_pkt(qpair, &prm);
3395
	else
3396
		res = qlt_24xx_build_ctio_pkt(qpair, &prm);
3397

3398
	if (unlikely(res != 0)) {
3399
		qpair->req->cnt += prm.req_cnt;
3400
		goto out_unlock_free_unmap;
3401 3402
	}

3403
	pkt = (struct ctio7_to_24xx *)prm.pkt;
3404
	pkt->u.status0.flags |= cpu_to_le16(CTIO7_FLAGS_DATA_OUT |
3405
	    CTIO7_FLAGS_STATUS_MODE_0);
3406 3407

	if (cmd->se_cmd.prot_op == TARGET_PROT_NORMAL)
3408
		qlt_load_data_segments(&prm);
3409 3410

	cmd->state = QLA_TGT_STATE_NEED_DATA;
3411
	cmd->cmd_sent_to_fw = 1;
3412
	cmd->ctio_flags = le16_to_cpu(pkt->u.status0.flags);
3413

3414 3415
	/* Memory Barrier */
	wmb();
3416 3417 3418 3419
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(vha, qpair->req);
3420
	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3421 3422 3423 3424

	return res;

out_unlock_free_unmap:
J
Joern Engel 已提交
3425
	qlt_unmap_sg(vha, cmd);
3426
	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3427 3428 3429 3430 3431

	return res;
}
EXPORT_SYMBOL(qlt_rdy_to_xfer);

3432 3433

/*
3434
 * it is assumed either hardware_lock or qpair lock is held.
3435
 */
3436
static void
3437
qlt_handle_dif_error(struct qla_qpair *qpair, struct qla_tgt_cmd *cmd,
3438
	struct ctio_crc_from_fw *sts)
3439 3440 3441 3442
{
	uint8_t		*ap = &sts->actual_dif[0];
	uint8_t		*ep = &sts->expected_dif[0];
	uint64_t	lba = cmd->se_cmd.t_task_lba;
3443 3444
	uint8_t scsi_status, sense_key, asc, ascq;
	unsigned long flags;
3445
	struct scsi_qla_host *vha = cmd->vha;
3446

3447
	cmd->trc_flags |= TRC_DIF_ERR;
3448

3449 3450 3451
	cmd->a_guard   = be16_to_cpu(*(uint16_t *)(ap + 0));
	cmd->a_app_tag = be16_to_cpu(*(uint16_t *)(ap + 2));
	cmd->a_ref_tag = be32_to_cpu(*(uint32_t *)(ap + 4));
3452

3453 3454 3455
	cmd->e_guard   = be16_to_cpu(*(uint16_t *)(ep + 0));
	cmd->e_app_tag = be16_to_cpu(*(uint16_t *)(ep + 2));
	cmd->e_ref_tag = be32_to_cpu(*(uint32_t *)(ep + 4));
3456

3457 3458
	ql_dbg(ql_dbg_tgt_dif, vha, 0xf075,
	    "%s: aborted %d state %d\n", __func__, cmd->aborted, cmd->state);
3459

3460
	scsi_status = sense_key = asc = ascq = 0;
3461

3462 3463
	/* check appl tag */
	if (cmd->e_app_tag != cmd->a_app_tag) {
3464 3465 3466 3467 3468 3469
		ql_dbg(ql_dbg_tgt_dif, vha, 0xe00d,
		    "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]",
		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3470 3471 3472 3473 3474 3475

		cmd->dif_err_code = DIF_ERR_APP;
		scsi_status = SAM_STAT_CHECK_CONDITION;
		sense_key = ABORTED_COMMAND;
		asc = 0x10;
		ascq = 0x2;
3476 3477 3478
	}

	/* check ref tag */
3479
	if (cmd->e_ref_tag != cmd->a_ref_tag) {
3480 3481 3482 3483 3484 3485
		ql_dbg(ql_dbg_tgt_dif, vha, 0xe00e,
		    "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] ",
		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
		    cmd->atio.u.isp24.fcp_hdr.ox_id);
3486 3487 3488 3489 3490 3491

		cmd->dif_err_code = DIF_ERR_REF;
		scsi_status = SAM_STAT_CHECK_CONDITION;
		sense_key = ABORTED_COMMAND;
		asc = 0x10;
		ascq = 0x3;
3492 3493 3494
		goto out;
	}

3495 3496
	/* check guard */
	if (cmd->e_guard != cmd->a_guard) {
3497 3498 3499 3500 3501 3502 3503
		ql_dbg(ql_dbg_tgt_dif, vha, 0xe012,
		    "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]",
		    cmd->cdb[0], lba, (lba+cmd->num_blks), cmd->num_blks,
		    cmd->a_ref_tag, cmd->e_ref_tag, cmd->a_app_tag,
		    cmd->e_app_tag, cmd->a_guard, cmd->e_guard, cmd,
		    cmd->atio.u.isp24.fcp_hdr.ox_id);

3504 3505 3506 3507 3508
		cmd->dif_err_code = DIF_ERR_GRD;
		scsi_status = SAM_STAT_CHECK_CONDITION;
		sense_key = ABORTED_COMMAND;
		asc = 0x10;
		ascq = 0x1;
3509 3510
	}
out:
3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524
	switch (cmd->state) {
	case QLA_TGT_STATE_NEED_DATA:
		/* handle_data will load DIF error code  */
		cmd->state = QLA_TGT_STATE_DATA_IN;
		vha->hw->tgt.tgt_ops->handle_data(cmd);
		break;
	default:
		spin_lock_irqsave(&cmd->cmd_lock, flags);
		if (cmd->aborted) {
			spin_unlock_irqrestore(&cmd->cmd_lock, flags);
			vha->hw->tgt.tgt_ops->free_cmd(cmd);
			break;
		}
		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3525

3526 3527
		qlt_send_resp_ctio(qpair, cmd, scsi_status, sense_key, asc,
		    ascq);
3528 3529 3530 3531 3532 3533 3534
		/* assume scsi status gets out on the wire.
		 * Will not wait for completion.
		 */
		vha->hw->tgt.tgt_ops->free_cmd(cmd);
		break;
	}
}
3535

3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548
/* If hardware_lock held on entry, might drop it, then reaquire */
/* This function sends the appropriate CTIO to ISP 2xxx or 24xx */
static int __qlt_send_term_imm_notif(struct scsi_qla_host *vha,
	struct imm_ntfy_from_isp *ntfy)
{
	struct nack_to_isp *nack;
	struct qla_hw_data *ha = vha->hw;
	request_t *pkt;
	int ret = 0;

	ql_dbg(ql_dbg_tgt_tmr, vha, 0xe01c,
	    "Sending TERM ELS CTIO (ha=%p)\n", ha);

3549
	pkt = (request_t *)qla2x00_alloc_iocbs(vha, NULL);
3550 3551 3552 3553 3554 3555 3556 3557 3558
	if (pkt == NULL) {
		ql_dbg(ql_dbg_tgt, vha, 0xe080,
		    "qla_target(%d): %s failed: unable to allocate "
		    "request packet\n", vha->vp_idx, __func__);
		return -ENOMEM;
	}

	pkt->entry_type = NOTIFY_ACK_TYPE;
	pkt->entry_count = 1;
3559
	pkt->handle = QLA_TGT_SKIP_HANDLE;
3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591

	nack = (struct nack_to_isp *)pkt;
	nack->ox_id = ntfy->ox_id;

	nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
	if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
		nack->u.isp24.flags = ntfy->u.isp24.flags &
			__constant_cpu_to_le32(NOTIFY24XX_FLAGS_PUREX_IOCB);
	}

	/* terminate */
	nack->u.isp24.flags |=
		__constant_cpu_to_le16(NOTIFY_ACK_FLAGS_TERMINATE);

	nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
	nack->u.isp24.status = ntfy->u.isp24.status;
	nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
	nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
	nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
	nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
	nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
	nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;

	qla2x00_start_iocbs(vha, vha->req);
	return ret;
}

static void qlt_send_term_imm_notif(struct scsi_qla_host *vha,
	struct imm_ntfy_from_isp *imm, int ha_locked)
{
	int rc;

3592
	WARN_ON_ONCE(!ha_locked);
3593
	rc = __qlt_send_term_imm_notif(vha, imm);
3594
	pr_debug("rc = %d\n", rc);
3595 3596
}

3597 3598 3599 3600
/*
 * If hardware_lock held on entry, might drop it, then reaquire
 * This function sends the appropriate CTIO to ISP 2xxx or 24xx
 */
3601
static int __qlt_send_term_exchange(struct qla_qpair *qpair,
3602 3603 3604
	struct qla_tgt_cmd *cmd,
	struct atio_from_isp *atio)
{
3605
	struct scsi_qla_host *vha = qpair->vha;
3606 3607 3608 3609
	struct ctio7_to_24xx *ctio24;
	struct qla_hw_data *ha = vha->hw;
	request_t *pkt;
	int ret = 0;
3610
	uint16_t temp;
3611

3612
	ql_dbg(ql_dbg_tgt, vha, 0xe009, "Sending TERM EXCH CTIO (ha=%p)\n", ha);
3613

3614 3615 3616
	if (cmd)
		vha = cmd->vha;

3617
	pkt = (request_t *)qla2x00_alloc_iocbs_ready(qpair, NULL);
3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634
	if (pkt == NULL) {
		ql_dbg(ql_dbg_tgt, vha, 0xe050,
		    "qla_target(%d): %s failed: unable to allocate "
		    "request packet\n", vha->vp_idx, __func__);
		return -ENOMEM;
	}

	if (cmd != NULL) {
		if (cmd->state < QLA_TGT_STATE_PROCESSED) {
			ql_dbg(ql_dbg_tgt, vha, 0xe051,
			    "qla_target(%d): Terminating cmd %p with "
			    "incorrect state %d\n", vha->vp_idx, cmd,
			    cmd->state);
		} else
			ret = 1;
	}

3635
	qpair->tgt_counters.num_term_xchg_sent++;
3636 3637 3638 3639 3640
	pkt->entry_count = 1;
	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;

	ctio24 = (struct ctio7_to_24xx *)pkt;
	ctio24->entry_type = CTIO_TYPE7;
3641
	ctio24->nport_handle = CTIO7_NHANDLE_UNRECOGNIZED;
3642
	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
3643
	ctio24->vp_index = vha->vp_idx;
3644
	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
3645
	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
3646 3647 3648
	temp = (atio->u.isp24.attr << 9) | CTIO7_FLAGS_STATUS_MODE_1 |
		CTIO7_FLAGS_TERMINATE;
	ctio24->u.status1.flags = cpu_to_le16(temp);
3649 3650
	temp = be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id);
	ctio24->u.status1.ox_id = cpu_to_le16(temp);
3651

3652 3653
	/* Memory Barrier */
	wmb();
3654 3655 3656 3657
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(vha, qpair->req);
3658 3659 3660
	return ret;
}

3661
static void qlt_send_term_exchange(struct qla_qpair *qpair,
3662 3663
	struct qla_tgt_cmd *cmd, struct atio_from_isp *atio, int ha_locked,
	int ul_abort)
3664
{
3665
	struct scsi_qla_host *vha;
3666
	unsigned long flags = 0;
3667 3668
	int rc;

3669 3670 3671 3672 3673
	/* why use different vha? NPIV */
	if (cmd)
		vha = cmd->vha;
	else
		vha = qpair->vha;
3674 3675

	if (ha_locked) {
3676
		rc = __qlt_send_term_exchange(qpair, cmd, atio);
3677 3678
		if (rc == -ENOMEM)
			qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3679 3680
		goto done;
	}
3681 3682
	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
	rc = __qlt_send_term_exchange(qpair, cmd, atio);
3683 3684
	if (rc == -ENOMEM)
		qlt_alloc_qfull_cmd(vha, atio, 0, 0);
3685

3686
done:
3687
	if (cmd && !ul_abort && !cmd->aborted) {
3688 3689
		if (cmd->sg_mapped)
			qlt_unmap_sg(vha, cmd);
3690 3691
		vha->hw->tgt.tgt_ops->free_cmd(cmd);
	}
3692 3693

	if (!ha_locked)
3694
		spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
3695

3696
	return;
3697 3698
}

3699 3700 3701 3702 3703 3704
static void qlt_init_term_exchange(struct scsi_qla_host *vha)
{
	struct list_head free_list;
	struct qla_tgt_cmd *cmd, *tcmd;

	vha->hw->tgt.leak_exchg_thresh_hold =
3705
	    (vha->hw->cur_fw_xcb_count/100) * LEAK_EXCHG_THRESH_HOLD_PERCENT;
3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734

	cmd = tcmd = NULL;
	if (!list_empty(&vha->hw->tgt.q_full_list)) {
		INIT_LIST_HEAD(&free_list);
		list_splice_init(&vha->hw->tgt.q_full_list, &free_list);

		list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
			list_del(&cmd->cmd_list);
			/* This cmd was never sent to TCM.  There is no need
			 * to schedule free or call free_cmd
			 */
			qlt_free_cmd(cmd);
			vha->hw->tgt.num_qfull_cmds_alloc--;
		}
	}
	vha->hw->tgt.num_qfull_cmds_dropped = 0;
}

static void qlt_chk_exch_leak_thresh_hold(struct scsi_qla_host *vha)
{
	uint32_t total_leaked;

	total_leaked = vha->hw->tgt.num_qfull_cmds_dropped;

	if (vha->hw->tgt.leak_exchg_thresh_hold &&
	    (total_leaked > vha->hw->tgt.leak_exchg_thresh_hold)) {

		ql_dbg(ql_dbg_tgt, vha, 0xe079,
		    "Chip reset due to exchange starvation: %d/%d.\n",
3735
		    total_leaked, vha->hw->cur_fw_xcb_count);
3736 3737 3738 3739 3740 3741 3742 3743 3744 3745

		if (IS_P3P_TYPE(vha->hw))
			set_bit(FCOE_CTX_RESET_NEEDED, &vha->dpc_flags);
		else
			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
		qla2xxx_wake_dpc(vha);
	}

}

3746
int qlt_abort_cmd(struct qla_tgt_cmd *cmd)
3747 3748 3749 3750
{
	struct qla_tgt *tgt = cmd->tgt;
	struct scsi_qla_host *vha = tgt->vha;
	struct se_cmd *se_cmd = &cmd->se_cmd;
3751
	unsigned long flags;
3752 3753 3754 3755 3756 3757

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf014,
	    "qla_target(%d): terminating exchange for aborted cmd=%p "
	    "(se_cmd=%p, tag=%llu)", vha->vp_idx, cmd, &cmd->se_cmd,
	    se_cmd->tag);

3758 3759 3760 3761 3762 3763 3764 3765
	spin_lock_irqsave(&cmd->cmd_lock, flags);
	if (cmd->aborted) {
		spin_unlock_irqrestore(&cmd->cmd_lock, flags);
		/*
		 * It's normal to see 2 calls in this path:
		 *  1) XFER Rdy completion + CMD_T_ABORT
		 *  2) TCM TMR - drain_state_list
		 */
3766 3767 3768 3769
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf016,
		    "multiple abort. %p transport_state %x, t_state %x, "
		    "se_cmd_flags %x\n", cmd, cmd->se_cmd.transport_state,
		    cmd->se_cmd.t_state, cmd->se_cmd.se_cmd_flags);
3770 3771
		return EIO;
	}
3772
	cmd->aborted = 1;
3773
	cmd->trc_flags |= TRC_ABORT;
3774
	spin_unlock_irqrestore(&cmd->cmd_lock, flags);
3775

3776
	qlt_send_term_exchange(cmd->qpair, cmd, &cmd->atio, 0, 1);
3777
	return 0;
3778 3779 3780
}
EXPORT_SYMBOL(qlt_abort_cmd);

3781 3782
void qlt_free_cmd(struct qla_tgt_cmd *cmd)
{
3783
	struct fc_port *sess = cmd->sess;
3784

3785 3786 3787 3788
	ql_dbg(ql_dbg_tgt, cmd->vha, 0xe074,
	    "%s: se_cmd[%p] ox_id %04x\n",
	    __func__, &cmd->se_cmd,
	    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
3789

3790 3791
	BUG_ON(cmd->cmd_in_wq);

3792 3793 3794
	if (cmd->sg_mapped)
		qlt_unmap_sg(cmd->vha, cmd);

3795 3796 3797
	if (!cmd->q_full)
		qlt_decr_num_pend_cmds(cmd->vha);

3798
	BUG_ON(cmd->sg_mapped);
3799
	cmd->jiffies_at_free = get_jiffies_64();
3800 3801
	if (unlikely(cmd->free_sg))
		kfree(cmd->sg);
3802 3803 3804 3805 3806

	if (!sess || !sess->se_sess) {
		WARN_ON(1);
		return;
	}
3807
	cmd->jiffies_at_free = get_jiffies_64();
3808
	target_free_tag(sess->se_sess, &cmd->se_cmd);
3809 3810 3811 3812 3813 3814
}
EXPORT_SYMBOL(qlt_free_cmd);

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
3815
static int qlt_term_ctio_exchange(struct qla_qpair *qpair, void *ctio,
3816 3817 3818
	struct qla_tgt_cmd *cmd, uint32_t status)
{
	int term = 0;
3819
	struct scsi_qla_host *vha = qpair->vha;
3820

3821
	if (cmd->se_cmd.prot_op)
3822
		ql_dbg(ql_dbg_tgt_dif, vha, 0xe013,
3823 3824 3825 3826 3827 3828 3829 3830
		    "Term DIF cmd: lba[0x%llx|%lld] len[0x%x] "
		    "se_cmd=%p tag[%x] op %#x/%s",
		     cmd->lba, cmd->lba,
		     cmd->num_blks, &cmd->se_cmd,
		     cmd->atio.u.isp24.exchange_addr,
		     cmd->se_cmd.prot_op,
		     prot_op_str(cmd->se_cmd.prot_op));

3831 3832
	if (ctio != NULL) {
		struct ctio7_from_24xx *c = (struct ctio7_from_24xx *)ctio;
3833

3834
		term = !(c->flags &
3835
		    cpu_to_le16(OF_TERM_EXCH));
3836 3837 3838 3839
	} else
		term = 1;

	if (term)
3840
		qlt_send_term_exchange(qpair, cmd, &cmd->atio, 1, 0);
3841 3842 3843 3844 3845 3846

	return term;
}


/* ha->hardware_lock supposed to be held on entry */
3847
static void *qlt_ctio_to_cmd(struct scsi_qla_host *vha,
3848
	struct rsp_que *rsp, uint32_t handle, void *ctio)
3849
{
3850
	void *cmd = NULL;
3851 3852 3853
	struct req_que *req;
	int qid = GET_QID(handle);
	uint32_t h = handle & ~QLA_TGT_HANDLE_MASK;
3854

3855 3856
	if (unlikely(h == QLA_TGT_SKIP_HANDLE))
		return NULL;
3857

3858 3859 3860 3861 3862 3863 3864 3865 3866 3867
	if (qid == rsp->req->id) {
		req = rsp->req;
	} else if (vha->hw->req_q_map[qid]) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0x1000a,
		    "qla_target(%d): CTIO completion with different QID %d handle %x\n",
		    vha->vp_idx, rsp->id, handle);
		req = vha->hw->req_q_map[qid];
	} else {
		return NULL;
	}
3868

3869
	h &= QLA_CMD_HANDLE_MASK;
3870

3871
	if (h != QLA_TGT_NULL_HANDLE) {
3872
		if (unlikely(h >= req->num_outstanding_cmds)) {
3873 3874 3875 3876 3877
			ql_dbg(ql_dbg_tgt, vha, 0xe052,
			    "qla_target(%d): Wrong handle %x received\n",
			    vha->vp_idx, handle);
			return NULL;
		}
3878

3879
		cmd = (void *) req->outstanding_cmds[h];
3880
		if (unlikely(cmd == NULL)) {
3881
			ql_dbg(ql_dbg_async, vha, 0xe053,
3882 3883
			    "qla_target(%d): Suspicious: unable to find the command with handle %x req->id %d rsp->id %d\n",
				vha->vp_idx, handle, req->id, rsp->id);
3884 3885
			return NULL;
		}
3886
		req->outstanding_cmds[h] = NULL;
3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900
	} else if (ctio != NULL) {
		/* We can't get loop ID from CTIO7 */
		ql_dbg(ql_dbg_tgt, vha, 0xe054,
		    "qla_target(%d): Wrong CTIO received: QLA24xx doesn't "
		    "support NULL handles\n", vha->vp_idx);
		return NULL;
	}

	return cmd;
}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
3901 3902
static void qlt_do_ctio_completion(struct scsi_qla_host *vha,
    struct rsp_que *rsp, uint32_t handle, uint32_t status, void *ctio)
3903 3904 3905 3906
{
	struct qla_hw_data *ha = vha->hw;
	struct se_cmd *se_cmd;
	struct qla_tgt_cmd *cmd;
3907
	struct qla_qpair *qpair = rsp->qpair;
3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918

	if (handle & CTIO_INTERMEDIATE_HANDLE_MARK) {
		/* That could happen only in case of an error/reset/abort */
		if (status != CTIO_SUCCESS) {
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01d,
			    "Intermediate CTIO received"
			    " (status %x)\n", status);
		}
		return;
	}

3919
	cmd = qlt_ctio_to_cmd(vha, rsp, handle, ctio);
3920
	if (cmd == NULL)
3921
		return;
3922

3923
	se_cmd = &cmd->se_cmd;
3924
	cmd->cmd_sent_to_fw = 0;
3925

J
Joern Engel 已提交
3926
	qlt_unmap_sg(vha, cmd);
3927 3928 3929

	if (unlikely(status != CTIO_SUCCESS)) {
		switch (status & 0xFFFF) {
3930 3931 3932 3933 3934 3935 3936 3937 3938
		case CTIO_INVALID_RX_ID:
			if (printk_ratelimit())
				dev_info(&vha->hw->pdev->dev,
				    "qla_target(%d): CTIO with INVALID_RX_ID ATIO attr %x CTIO Flags %x|%x\n",
				    vha->vp_idx, cmd->atio.u.isp24.attr,
				    ((cmd->ctio_flags >> 9) & 0xf),
				    cmd->ctio_flags);

			break;
3939 3940 3941
		case CTIO_LIP_RESET:
		case CTIO_TARGET_RESET:
		case CTIO_ABORTED:
3942
			/* driver request abort via Terminate exchange */
3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954
		case CTIO_TIMEOUT:
			/* They are OK */
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf058,
			    "qla_target(%d): CTIO with "
			    "status %#x received, state %x, se_cmd %p, "
			    "(LIP_RESET=e, ABORTED=2, TARGET_RESET=17, "
			    "TIMEOUT=b, INVALID_RX_ID=8)\n", vha->vp_idx,
			    status, cmd->state, se_cmd);
			break;

		case CTIO_PORT_LOGGED_OUT:
		case CTIO_PORT_UNAVAILABLE:
3955
		{
3956 3957 3958
			int logged_out =
				(status & 0xFFFF) == CTIO_PORT_LOGGED_OUT;

3959
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf059,
3960
			    "qla_target(%d): CTIO with %s status %x "
3961
			    "received (state %x, se_cmd %p)\n", vha->vp_idx,
3962
			    logged_out ? "PORT LOGGED OUT" : "PORT UNAVAILABLE",
3963 3964
			    status, cmd->state, se_cmd);

3965 3966 3967 3968 3969 3970
			if (logged_out && cmd->sess) {
				/*
				 * Session is already logged out, but we need
				 * to notify initiator, who's not aware of this
				 */
				cmd->sess->send_els_logo = 1;
3971
				ql_dbg(ql_dbg_disc, vha, 0x20f8,
3972 3973 3974
				    "%s %d %8phC post del sess\n",
				    __func__, __LINE__, cmd->sess->port_name);

3975
				qlt_schedule_sess_for_deletion(cmd->sess);
3976 3977 3978
			}
			break;
		}
3979 3980 3981 3982
		case CTIO_DIF_ERROR: {
			struct ctio_crc_from_fw *crc =
				(struct ctio_crc_from_fw *)ctio;
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf073,
3983 3984 3985
			    "qla_target(%d): CTIO with DIF_ERROR status %x "
			    "received (state %x, ulp_cmd %p) actual_dif[0x%llx] "
			    "expect_dif[0x%llx]\n",
3986 3987 3988 3989
			    vha->vp_idx, status, cmd->state, se_cmd,
			    *((u64 *)&crc->actual_dif[0]),
			    *((u64 *)&crc->expected_dif[0]));

3990
			qlt_handle_dif_error(qpair, cmd, ctio);
3991
			return;
3992
		}
3993 3994
		default:
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05b,
3995
			    "qla_target(%d): CTIO with error status 0x%x received (state %x, se_cmd %p\n",
3996 3997 3998 3999
			    vha->vp_idx, status, cmd->state, se_cmd);
			break;
		}

4000

4001
		/* "cmd->aborted" means
4002 4003 4004 4005 4006 4007
		 * cmd is already aborted/terminated, we don't
		 * need to terminate again.  The exchange is already
		 * cleaned up/freed at FW level.  Just cleanup at driver
		 * level.
		 */
		if ((cmd->state != QLA_TGT_STATE_NEED_DATA) &&
4008
		    (!cmd->aborted)) {
4009
			cmd->trc_flags |= TRC_CTIO_ERR;
4010
			if (qlt_term_ctio_exchange(qpair, ctio, cmd, status))
4011
				return;
4012
		}
4013 4014 4015
	}

	if (cmd->state == QLA_TGT_STATE_PROCESSED) {
4016
		cmd->trc_flags |= TRC_CTIO_DONE;
4017 4018 4019
	} else if (cmd->state == QLA_TGT_STATE_NEED_DATA) {
		cmd->state = QLA_TGT_STATE_DATA_IN;

4020
		if (status == CTIO_SUCCESS)
4021 4022 4023 4024
			cmd->write_data_transferred = 1;

		ha->tgt.tgt_ops->handle_data(cmd);
		return;
4025
	} else if (cmd->aborted) {
4026
		cmd->trc_flags |= TRC_CTIO_ABORTED;
4027
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01e,
4028
		  "Aborted command %p (tag %lld) finished\n", cmd, se_cmd->tag);
4029
	} else {
4030
		cmd->trc_flags |= TRC_CTIO_STRANGE;
4031 4032 4033 4034 4035
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05c,
		    "qla_target(%d): A command in state (%d) should "
		    "not return a CTIO complete\n", vha->vp_idx, cmd->state);
	}

4036
	if (unlikely(status != CTIO_SUCCESS) &&
4037
		!cmd->aborted) {
4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01f, "Finishing failed CTIO\n");
		dump_stack();
	}

	ha->tgt.tgt_ops->free_cmd(cmd);
}

static inline int qlt_get_fcp_task_attr(struct scsi_qla_host *vha,
	uint8_t task_codes)
{
	int fcp_task_attr;

	switch (task_codes) {
	case ATIO_SIMPLE_QUEUE:
C
Christoph Hellwig 已提交
4052
		fcp_task_attr = TCM_SIMPLE_TAG;
4053 4054
		break;
	case ATIO_HEAD_OF_QUEUE:
C
Christoph Hellwig 已提交
4055
		fcp_task_attr = TCM_HEAD_TAG;
4056 4057
		break;
	case ATIO_ORDERED_QUEUE:
C
Christoph Hellwig 已提交
4058
		fcp_task_attr = TCM_ORDERED_TAG;
4059 4060
		break;
	case ATIO_ACA_QUEUE:
C
Christoph Hellwig 已提交
4061
		fcp_task_attr = TCM_ACA_TAG;
4062 4063
		break;
	case ATIO_UNTAGGED:
C
Christoph Hellwig 已提交
4064
		fcp_task_attr = TCM_SIMPLE_TAG;
4065 4066 4067 4068 4069
		break;
	default:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05d,
		    "qla_target: unknown task code %x, use ORDERED instead\n",
		    task_codes);
C
Christoph Hellwig 已提交
4070
		fcp_task_attr = TCM_ORDERED_TAG;
4071 4072 4073 4074 4075 4076 4077 4078 4079
		break;
	}

	return fcp_task_attr;
}

/*
 * Process context for I/O path into tcm_qla2xxx code
 */
4080
static void __qlt_do_work(struct qla_tgt_cmd *cmd)
4081 4082 4083
{
	scsi_qla_host_t *vha = cmd->vha;
	struct qla_hw_data *ha = vha->hw;
4084
	struct fc_port *sess = cmd->sess;
4085 4086 4087 4088 4089
	struct atio_from_isp *atio = &cmd->atio;
	unsigned char *cdb;
	unsigned long flags;
	uint32_t data_length;
	int ret, fcp_task_attr, data_dir, bidi = 0;
4090
	struct qla_qpair *qpair = cmd->qpair;
4091

4092
	cmd->cmd_in_wq = 0;
4093
	cmd->trc_flags |= TRC_DO_WORK;
4094

4095
	if (cmd->aborted) {
4096 4097 4098 4099 4100 4101
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf082,
		    "cmd with tag %u is aborted\n",
		    cmd->atio.u.isp24.exchange_addr);
		goto out_term;
	}

4102
	spin_lock_init(&cmd->cmd_lock);
4103
	cdb = &atio->u.isp24.fcp_cmnd.cdb[0];
4104
	cmd->se_cmd.tag = atio->u.isp24.exchange_addr;
4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118

	if (atio->u.isp24.fcp_cmnd.rddata &&
	    atio->u.isp24.fcp_cmnd.wrdata) {
		bidi = 1;
		data_dir = DMA_TO_DEVICE;
	} else if (atio->u.isp24.fcp_cmnd.rddata)
		data_dir = DMA_FROM_DEVICE;
	else if (atio->u.isp24.fcp_cmnd.wrdata)
		data_dir = DMA_TO_DEVICE;
	else
		data_dir = DMA_NONE;

	fcp_task_attr = qlt_get_fcp_task_attr(vha,
	    atio->u.isp24.fcp_cmnd.task_attr);
4119
	data_length = get_datalen_for_atio(atio);
4120

4121 4122
	ret = ha->tgt.tgt_ops->handle_cmd(vha, cmd, cdb, data_length,
				          fcp_task_attr, data_dir, bidi);
4123 4124 4125
	if (ret != 0)
		goto out_term;
	/*
4126
	 * Drop extra session reference from qlt_handle_cmd_for_atio().
4127
	 */
4128
	ha->tgt.tgt_ops->put_sess(sess);
4129 4130 4131
	return;

out_term:
4132
	ql_dbg(ql_dbg_io, vha, 0x3060, "Terminating work cmd %p", cmd);
4133
	/*
4134 4135
	 * cmd has not sent to target yet, so pass NULL as the second
	 * argument to qlt_send_term_exchange() and free the memory here.
4136
	 */
4137
	cmd->trc_flags |= TRC_DO_WORK_ERR;
4138 4139
	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
	qlt_send_term_exchange(qpair, NULL, &cmd->atio, 1, 0);
4140 4141

	qlt_decr_num_pend_cmds(vha);
4142
	target_free_tag(sess->se_sess, &cmd->se_cmd);
4143
	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
4144

4145
	ha->tgt.tgt_ops->put_sess(sess);
4146 4147 4148 4149 4150
}

static void qlt_do_work(struct work_struct *work)
{
	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
4151 4152 4153 4154 4155 4156
	scsi_qla_host_t *vha = cmd->vha;
	unsigned long flags;

	spin_lock_irqsave(&vha->cmd_list_lock, flags);
	list_del(&cmd->cmd_list);
	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4157 4158 4159 4160

	__qlt_do_work(cmd);
}

4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185
void qlt_clr_qp_table(struct scsi_qla_host *vha)
{
	unsigned long flags;
	struct qla_hw_data *ha = vha->hw;
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
	void *node;
	u64 key = 0;

	ql_log(ql_log_info, vha, 0x706c,
	    "User update Number of Active Qpairs %d\n",
	    ha->tgt.num_act_qpairs);

	spin_lock_irqsave(&ha->tgt.atio_lock, flags);

	btree_for_each_safe64(&tgt->lun_qpair_map, key, node)
		btree_remove64(&tgt->lun_qpair_map, key);

	ha->base_qpair->lun_cnt = 0;
	for (key = 0; key < ha->max_qpairs; key++)
		if (ha->queue_pair_map[key])
			ha->queue_pair_map[key]->lun_cnt = 0;

	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
}

4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264
static void qlt_assign_qpair(struct scsi_qla_host *vha,
	struct qla_tgt_cmd *cmd)
{
	struct qla_qpair *qpair, *qp;
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
	struct qla_qpair_hint *h;

	if (vha->flags.qpairs_available) {
		h = btree_lookup64(&tgt->lun_qpair_map, cmd->unpacked_lun);
		if (unlikely(!h)) {
			/* spread lun to qpair ratio evently */
			int lcnt = 0, rc;
			struct scsi_qla_host *base_vha =
				pci_get_drvdata(vha->hw->pdev);

			qpair = vha->hw->base_qpair;
			if (qpair->lun_cnt == 0) {
				qpair->lun_cnt++;
				h = qla_qpair_to_hint(tgt, qpair);
				BUG_ON(!h);
				rc = btree_insert64(&tgt->lun_qpair_map,
					cmd->unpacked_lun, h, GFP_ATOMIC);
				if (rc) {
					qpair->lun_cnt--;
					ql_log(ql_log_info, vha, 0xd037,
					    "Unable to insert lun %llx into lun_qpair_map\n",
					    cmd->unpacked_lun);
				}
				goto out;
			} else {
				lcnt = qpair->lun_cnt;
			}

			h = NULL;
			list_for_each_entry(qp, &base_vha->qp_list,
			    qp_list_elem) {
				if (qp->lun_cnt == 0) {
					qp->lun_cnt++;
					h = qla_qpair_to_hint(tgt, qp);
					BUG_ON(!h);
					rc = btree_insert64(&tgt->lun_qpair_map,
					    cmd->unpacked_lun, h, GFP_ATOMIC);
					if (rc) {
						qp->lun_cnt--;
						ql_log(ql_log_info, vha, 0xd038,
							"Unable to insert lun %llx into lun_qpair_map\n",
							cmd->unpacked_lun);
					}
					qpair = qp;
					goto out;
				} else {
					if (qp->lun_cnt < lcnt) {
						lcnt = qp->lun_cnt;
						qpair = qp;
						continue;
					}
				}
			}
			BUG_ON(!qpair);
			qpair->lun_cnt++;
			h = qla_qpair_to_hint(tgt, qpair);
			BUG_ON(!h);
			rc = btree_insert64(&tgt->lun_qpair_map,
				cmd->unpacked_lun, h, GFP_ATOMIC);
			if (rc) {
				qpair->lun_cnt--;
				ql_log(ql_log_info, vha, 0xd039,
				   "Unable to insert lun %llx into lun_qpair_map\n",
				   cmd->unpacked_lun);
			}
		}
	} else {
		h = &tgt->qphints[0];
	}
out:
	cmd->qpair = h->qpair;
	cmd->se_cmd.cpuid = h->cpuid;
}

4265
static struct qla_tgt_cmd *qlt_get_tag(scsi_qla_host_t *vha,
4266
				       struct fc_port *sess,
4267 4268 4269 4270
				       struct atio_from_isp *atio)
{
	struct se_session *se_sess = sess->se_sess;
	struct qla_tgt_cmd *cmd;
4271
	int tag, cpu;
4272

4273
	tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
4274 4275 4276 4277 4278
	if (tag < 0)
		return NULL;

	cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
	memset(cmd, 0, sizeof(struct qla_tgt_cmd));
4279
	cmd->cmd_type = TYPE_TGT_CMD;
4280 4281 4282
	memcpy(&cmd->atio, atio, sizeof(*atio));
	cmd->state = QLA_TGT_STATE_NEW;
	cmd->tgt = vha->vha_tgt.qla_tgt;
4283
	qlt_incr_num_pend_cmds(vha);
4284 4285
	cmd->vha = vha;
	cmd->se_cmd.map_tag = tag;
4286
	cmd->se_cmd.map_cpu = cpu;
4287 4288 4289 4290
	cmd->sess = sess;
	cmd->loop_id = sess->loop_id;
	cmd->conf_compl_supported = sess->conf_compl_supported;

4291
	cmd->trc_flags = 0;
4292 4293
	cmd->jiffies_at_alloc = get_jiffies_64();

4294 4295 4296
	cmd->unpacked_lun = scsilun_to_int(
	    (struct scsi_lun *)&atio->u.isp24.fcp_cmnd.lun);
	qlt_assign_qpair(vha, cmd);
4297
	cmd->reset_count = vha->hw->base_qpair->chip_reset;
4298
	cmd->vp_idx = vha->vp_idx;
4299

4300 4301 4302
	return cmd;
}

4303 4304 4305 4306
/* ha->hardware_lock supposed to be held on entry */
static int qlt_handle_cmd_for_atio(struct scsi_qla_host *vha,
	struct atio_from_isp *atio)
{
4307
	struct qla_hw_data *ha = vha->hw;
4308
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4309
	struct fc_port *sess;
4310
	struct qla_tgt_cmd *cmd;
4311
	unsigned long flags;
4312
	port_id_t id;
4313 4314

	if (unlikely(tgt->tgt_stop)) {
4315
		ql_dbg(ql_dbg_io, vha, 0x3061,
4316
		    "New command while device %p is shutting down\n", tgt);
4317
		return -ENODEV;
4318 4319
	}

4320
	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
4321 4322 4323
	if (IS_SW_RESV_ADDR(id))
		return -EBUSY;

4324
	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, atio->u.isp24.fcp_hdr.s_id);
4325 4326
	if (unlikely(!sess))
		return -EFAULT;
4327 4328 4329

	/* Another WWN used to have our s_id. Our PLOGI scheduled its
	 * session deletion, but it's still in sess_del_work wq */
4330
	if (sess->deleted) {
4331
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf002,
4332 4333 4334 4335 4336
		    "New command while old session %p is being deleted\n",
		    sess);
		return -EFAULT;
	}

4337 4338 4339
	/*
	 * Do kref_get() before returning + dropping qla_hw_data->hardware_lock.
	 */
4340
	if (!kref_get_unless_zero(&sess->sess_kref)) {
4341
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf004,
4342 4343 4344 4345 4346
		    "%s: kref_get fail, %8phC oxid %x \n",
		    __func__, sess->port_name,
		     be16_to_cpu(atio->u.isp24.fcp_hdr.ox_id));
		return -EFAULT;
	}
4347 4348

	cmd = qlt_get_tag(vha, sess, atio);
4349
	if (!cmd) {
4350
		ql_dbg(ql_dbg_io, vha, 0x3062,
4351
		    "qla_target(%d): Allocation of cmd failed\n", vha->vp_idx);
4352
		ha->tgt.tgt_ops->put_sess(sess);
4353
		return -EBUSY;
4354 4355
	}

4356
	cmd->cmd_in_wq = 1;
4357
	cmd->trc_flags |= TRC_NEW_CMD;
4358

4359
	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4360
	list_add_tail(&cmd->cmd_list, &vha->qla_cmd_list);
4361
	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4362

4363
	INIT_WORK(&cmd->work, qlt_do_work);
4364 4365 4366
	if (vha->flags.qpairs_available) {
		queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq, &cmd->work);
	} else if (ha->msix_count) {
4367 4368 4369 4370 4371 4372 4373 4374 4375
		if (cmd->atio.u.isp24.fcp_cmnd.rddata)
			queue_work_on(smp_processor_id(), qla_tgt_wq,
			    &cmd->work);
		else
			queue_work_on(cmd->se_cmd.cpuid, qla_tgt_wq,
			    &cmd->work);
	} else {
		queue_work(qla_tgt_wq, &cmd->work);
	}
4376

4377
	return 0;
4378 4379 4380
}

/* ha->hardware_lock supposed to be held on entry */
4381
static int qlt_issue_task_mgmt(struct fc_port *sess, u64 lun,
4382 4383 4384 4385 4386
	int fn, void *iocb, int flags)
{
	struct scsi_qla_host *vha = sess->vha;
	struct qla_hw_data *ha = vha->hw;
	struct qla_tgt_mgmt_cmd *mcmd;
4387
	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
4388
	struct qla_qpair_hint *h = &vha->vha_tgt.qla_tgt->qphints[0];
4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406

	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
	if (!mcmd) {
		ql_dbg(ql_dbg_tgt_tmr, vha, 0x10009,
		    "qla_target(%d): Allocation of management "
		    "command failed, some commands and their data could "
		    "leak\n", vha->vp_idx);
		return -ENOMEM;
	}
	memset(mcmd, 0, sizeof(*mcmd));
	mcmd->sess = sess;

	if (iocb) {
		memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
		    sizeof(mcmd->orig_iocb.imm_ntfy));
	}
	mcmd->tmr_func = fn;
	mcmd->flags = flags;
4407
	mcmd->reset_count = ha->base_qpair->chip_reset;
4408
	mcmd->qpair = h->qpair;
4409
	mcmd->vha = vha;
4410 4411
	mcmd->se_cmd.cpuid = h->cpuid;
	mcmd->unpacked_lun = lun;
4412 4413 4414

	switch (fn) {
	case QLA_TGT_LUN_RESET:
4415 4416 4417
	case QLA_TGT_CLEAR_TS:
	case QLA_TGT_ABORT_TS:
		abort_cmds_for_lun(vha, lun, a->u.isp24.fcp_hdr.s_id);
4418
		/* fall through */
4419 4420 4421 4422 4423
	case QLA_TGT_CLEAR_ACA:
		h = qlt_find_qphint(vha, mcmd->unpacked_lun);
		mcmd->qpair = h->qpair;
		mcmd->se_cmd.cpuid = h->cpuid;
		break;
4424

4425 4426 4427 4428 4429 4430 4431
	case QLA_TGT_TARGET_RESET:
	case QLA_TGT_NEXUS_LOSS_SESS:
	case QLA_TGT_NEXUS_LOSS:
	case QLA_TGT_ABORT_ALL:
	default:
		/* no-op */
		break;
4432 4433
	}

4434 4435 4436 4437
	INIT_WORK(&mcmd->work, qlt_do_tmr_work);
	queue_work_on(mcmd->se_cmd.cpuid, qla_tgt_wq,
	    &mcmd->work);

4438 4439 4440 4441 4442 4443 4444 4445
	return 0;
}

/* ha->hardware_lock supposed to be held on entry */
static int qlt_handle_task_mgmt(struct scsi_qla_host *vha, void *iocb)
{
	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
	struct qla_hw_data *ha = vha->hw;
4446
	struct fc_port *sess;
4447
	u64 unpacked_lun;
4448
	int fn;
4449
	unsigned long flags;
4450 4451

	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
4452 4453

	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4454 4455
	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha,
	    a->u.isp24.fcp_hdr.s_id);
4456 4457
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);

4458 4459
	unpacked_lun =
	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4460

4461
	if (sess == NULL || sess->deleted)
4462 4463
		return -EFAULT;

4464 4465 4466 4467 4468
	return qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
}

/* ha->hardware_lock supposed to be held on entry */
static int __qlt_abort_task(struct scsi_qla_host *vha,
4469
	struct imm_ntfy_from_isp *iocb, struct fc_port *sess)
4470 4471 4472 4473
{
	struct atio_from_isp *a = (struct atio_from_isp *)iocb;
	struct qla_hw_data *ha = vha->hw;
	struct qla_tgt_mgmt_cmd *mcmd;
4474
	u64 unpacked_lun;
4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489
	int rc;

	mcmd = mempool_alloc(qla_tgt_mgmt_cmd_mempool, GFP_ATOMIC);
	if (mcmd == NULL) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf05f,
		    "qla_target(%d): %s: Allocation of ABORT cmd failed\n",
		    vha->vp_idx, __func__);
		return -ENOMEM;
	}
	memset(mcmd, 0, sizeof(*mcmd));

	mcmd->sess = sess;
	memcpy(&mcmd->orig_iocb.imm_ntfy, iocb,
	    sizeof(mcmd->orig_iocb.imm_ntfy));

4490 4491
	unpacked_lun =
	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
4492
	mcmd->reset_count = ha->base_qpair->chip_reset;
4493
	mcmd->tmr_func = QLA_TGT_2G_ABORT_TASK;
4494
	mcmd->qpair = ha->base_qpair;
4495

4496
	rc = ha->tgt.tgt_ops->handle_tmr(mcmd, unpacked_lun, mcmd->tmr_func,
4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513
	    le16_to_cpu(iocb->u.isp2x.seq_id));
	if (rc != 0) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf060,
		    "qla_target(%d): tgt_ops->handle_tmr() failed: %d\n",
		    vha->vp_idx, rc);
		mempool_free(mcmd, qla_tgt_mgmt_cmd_mempool);
		return -EFAULT;
	}

	return 0;
}

/* ha->hardware_lock supposed to be held on entry */
static int qlt_abort_task(struct scsi_qla_host *vha,
	struct imm_ntfy_from_isp *iocb)
{
	struct qla_hw_data *ha = vha->hw;
4514
	struct fc_port *sess;
4515
	int loop_id;
4516
	unsigned long flags;
4517 4518 4519

	loop_id = GET_TARGET_ID(ha, (struct atio_from_isp *)iocb);

4520
	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
4521
	sess = ha->tgt.tgt_ops->find_sess_by_loop_id(vha, loop_id);
4522 4523
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);

4524 4525 4526 4527
	if (sess == NULL) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf025,
		    "qla_target(%d): task abort for unexisting "
		    "session\n", vha->vp_idx);
4528
		return qlt_sched_sess_work(vha->vha_tgt.qla_tgt,
4529 4530 4531 4532 4533 4534
		    QLA_TGT_SESS_WORK_ABORT, iocb, sizeof(*iocb));
	}

	return __qlt_abort_task(vha, iocb, sess);
}

4535 4536
void qlt_logo_completion_handler(fc_port_t *fcport, int rc)
{
4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550
	if (rc != MBS_COMMAND_COMPLETE) {
		ql_dbg(ql_dbg_tgt_mgt, fcport->vha, 0xf093,
			"%s: se_sess %p / sess %p from"
			" port %8phC loop_id %#04x s_id %02x:%02x:%02x"
			" LOGO failed: %#x\n",
			__func__,
			fcport->se_sess,
			fcport,
			fcport->port_name, fcport->loop_id,
			fcport->d_id.b.domain, fcport->d_id.b.area,
			fcport->d_id.b.al_pa, rc);
	}

	fcport->logout_completed = 1;
4551 4552 4553 4554 4555 4556 4557 4558 4559
}

/*
* ha->hardware_lock supposed to be held on entry (to protect tgt->sess_list)
*
* Schedules sessions with matching port_id/loop_id but different wwn for
* deletion. Returns existing session with matching wwn if present.
* Null otherwise.
*/
4560 4561
struct fc_port *
qlt_find_sess_invalidate_other(scsi_qla_host_t *vha, uint64_t wwn,
4562
    port_id_t port_id, uint16_t loop_id, struct fc_port **conflict_sess)
4563
{
4564
	struct fc_port *sess = NULL, *other_sess;
4565 4566
	uint64_t other_wwn;

4567 4568
	*conflict_sess = NULL;

4569
	list_for_each_entry(other_sess, &vha->vp_fcports, list) {
4570 4571 4572 4573 4574 4575 4576 4577 4578 4579

		other_wwn = wwn_to_u64(other_sess->port_name);

		if (wwn == other_wwn) {
			WARN_ON(sess);
			sess = other_sess;
			continue;
		}

		/* find other sess with nport_id collision */
4580
		if (port_id.b24 == other_sess->d_id.b24) {
4581
			if (loop_id != other_sess->loop_id) {
4582
				ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000c,
4583 4584 4585 4586 4587 4588 4589 4590 4591
				    "Invalidating sess %p loop_id %d wwn %llx.\n",
				    other_sess, other_sess->loop_id, other_wwn);

				/*
				 * logout_on_delete is set by default, but another
				 * session that has the same s_id/loop_id combo
				 * might have cleared it when requested this session
				 * deletion, so don't touch it
				 */
4592
				qlt_schedule_sess_for_deletion(other_sess);
4593 4594 4595
			} else {
				/*
				 * Another wwn used to have our s_id/loop_id
4596
				 * kill the session, but don't free the loop_id
4597
				 */
4598
				ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01b,
4599 4600 4601
				    "Invalidating sess %p loop_id %d wwn %llx.\n",
				    other_sess, other_sess->loop_id, other_wwn);

4602
				other_sess->keep_nport_handle = 1;
4603 4604
				if (other_sess->disc_state != DSC_DELETED)
					*conflict_sess = other_sess;
4605
				qlt_schedule_sess_for_deletion(other_sess);
4606 4607 4608 4609 4610
			}
			continue;
		}

		/* find other sess with nport handle collision */
4611 4612 4613
		if ((loop_id == other_sess->loop_id) &&
			(loop_id != FC_NO_LOOP_ID)) {
			ql_dbg(ql_dbg_tgt_tmr, vha, 0x1000d,
4614 4615 4616 4617 4618
			       "Invalidating sess %p loop_id %d wwn %llx.\n",
			       other_sess, other_sess->loop_id, other_wwn);

			/* Same loop_id but different s_id
			 * Ok to kill and logout */
4619
			qlt_schedule_sess_for_deletion(other_sess);
4620 4621 4622 4623 4624 4625
		}
	}

	return sess;
}

4626 4627 4628 4629 4630 4631 4632
/* Abort any commands for this s_id waiting on qla_tgt_wq workqueue */
static int abort_cmds_for_s_id(struct scsi_qla_host *vha, port_id_t *s_id)
{
	struct qla_tgt_sess_op *op;
	struct qla_tgt_cmd *cmd;
	uint32_t key;
	int count = 0;
4633
	unsigned long flags;
4634 4635 4636 4637 4638

	key = (((u32)s_id->b.domain << 16) |
	       ((u32)s_id->b.area   <<  8) |
	       ((u32)s_id->b.al_pa));

4639
	spin_lock_irqsave(&vha->cmd_list_lock, flags);
4640 4641
	list_for_each_entry(op, &vha->qla_sess_op_cmd_list, cmd_list) {
		uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4642

4643 4644 4645 4646 4647
		if (op_key == key) {
			op->aborted = true;
			count++;
		}
	}
4648 4649 4650

	list_for_each_entry(op, &vha->unknown_atio_list, cmd_list) {
		uint32_t op_key = sid_to_key(op->atio.u.isp24.fcp_hdr.s_id);
4651

4652 4653 4654 4655 4656 4657
		if (op_key == key) {
			op->aborted = true;
			count++;
		}
	}

4658 4659
	list_for_each_entry(cmd, &vha->qla_cmd_list, cmd_list) {
		uint32_t cmd_key = sid_to_key(cmd->atio.u.isp24.fcp_hdr.s_id);
4660

4661
		if (cmd_key == key) {
4662
			cmd->aborted = 1;
4663 4664 4665
			count++;
		}
	}
4666
	spin_unlock_irqrestore(&vha->cmd_list_lock, flags);
4667 4668 4669 4670

	return count;
}

4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681
static int qlt_handle_login(struct scsi_qla_host *vha,
    struct imm_ntfy_from_isp *iocb)
{
	struct fc_port *sess = NULL, *conflict_sess = NULL;
	uint64_t wwn;
	port_id_t port_id;
	uint16_t loop_id, wd3_lo;
	int res = 0;
	struct qlt_plogi_ack_t *pla;
	unsigned long flags;

4682 4683
	lockdep_assert_held(&vha->hw->hardware_lock);

4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700
	wwn = wwn_to_u64(iocb->u.isp24.port_name);

	port_id.b.domain = iocb->u.isp24.port_id[2];
	port_id.b.area   = iocb->u.isp24.port_id[1];
	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
	port_id.b.rsvd_1 = 0;

	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);

	/* Mark all stale commands sitting in qla_tgt_wq for deletion */
	abort_cmds_for_s_id(vha, &port_id);

	if (wwn) {
		spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
		sess = qlt_find_sess_invalidate_other(vha, wwn,
		    port_id, loop_id, &conflict_sess);
		spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
4701 4702 4703 4704 4705 4706
	} else {
		ql_dbg(ql_dbg_disc, vha, 0xffff,
		    "%s %d Term INOT due to WWN=0 lid=%d, NportID %06X ",
		    __func__, __LINE__, loop_id, port_id.b24);
		qlt_send_term_imm_notif(vha, iocb, 1);
		goto out;
4707 4708 4709 4710 4711 4712 4713 4714 4715
	}

	if (IS_SW_RESV_ADDR(port_id)) {
		res = 1;
		goto out;
	}

	pla = qlt_plogi_ack_find_add(vha, &port_id, iocb);
	if (!pla) {
4716 4717 4718 4719
		ql_dbg(ql_dbg_disc + ql_dbg_verbose, vha, 0xffff,
		    "%s %d %8phC Term INOT due to mem alloc fail",
		    __func__, __LINE__,
		    iocb->u.isp24.port_name);
4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734
		qlt_send_term_imm_notif(vha, iocb, 1);
		goto out;
	}

	if (conflict_sess) {
		conflict_sess->login_gen++;
		qlt_plogi_ack_link(vha, pla, conflict_sess,
		    QLT_PLOGI_LINK_CONFLICT);
	}

	if (!sess) {
		pla->ref_count++;
		ql_dbg(ql_dbg_disc, vha, 0xffff,
		    "%s %d %8phC post new sess\n",
		    __func__, __LINE__, iocb->u.isp24.port_name);
4735 4736 4737 4738 4739 4740 4741 4742 4743 4744
		if (iocb->u.isp24.status_subcode == ELS_PLOGI)
			qla24xx_post_newsess_work(vha, &port_id,
			    iocb->u.isp24.port_name,
			    iocb->u.isp24.u.plogi.node_name,
			    pla, FC4_TYPE_UNKNOWN);
		else
			qla24xx_post_newsess_work(vha, &port_id,
			    iocb->u.isp24.port_name, NULL,
			    pla, FC4_TYPE_UNKNOWN);

4745 4746 4747
		goto out;
	}

4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766
	if (sess->disc_state == DSC_UPD_FCPORT) {
		u16 sec;

		/*
		 * Remote port registration is still going on from
		 * previous login. Allow it to finish before we
		 * accept the new login.
		 */
		sess->next_disc_state = DSC_DELETE_PEND;
		sec = jiffies_to_msecs(jiffies -
		    sess->jiffies_at_registration) / 1000;
		if (sess->sec_since_registration < sec && sec &&
		    !(sec % 5)) {
			sess->sec_since_registration = sec;
			ql_dbg(ql_dbg_disc, vha, 0xffff,
			    "%s %8phC - Slow Rport registration (%d Sec)\n",
			    __func__, sess->port_name, sec);
		}

4767 4768
		if (!conflict_sess) {
			list_del(&pla->list);
4769
			kmem_cache_free(qla_tgt_plogi_cachep, pla);
4770
		}
4771 4772 4773 4774 4775

		qlt_send_term_imm_notif(vha, iocb, 1);
		goto out;
	}

4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835
	qlt_plogi_ack_link(vha, pla, sess, QLT_PLOGI_LINK_SAME_WWN);
	sess->d_id = port_id;
	sess->login_gen++;

	if (iocb->u.isp24.status_subcode == ELS_PRLI) {
		sess->fw_login_state = DSC_LS_PRLI_PEND;
		sess->local = 0;
		sess->loop_id = loop_id;
		sess->d_id = port_id;
		sess->fw_login_state = DSC_LS_PRLI_PEND;
		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);

		if (wd3_lo & BIT_7)
			sess->conf_compl_supported = 1;

		if ((wd3_lo & BIT_4) == 0)
			sess->port_type = FCT_INITIATOR;
		else
			sess->port_type = FCT_TARGET;

	} else
		sess->fw_login_state = DSC_LS_PLOGI_PEND;


	ql_dbg(ql_dbg_disc, vha, 0x20f9,
	    "%s %d %8phC  DS %d\n",
	    __func__, __LINE__, sess->port_name, sess->disc_state);

	switch (sess->disc_state) {
	case DSC_DELETED:
		qlt_plogi_ack_unref(vha, pla);
		break;

	default:
		/*
		 * Under normal circumstances we want to release nport handle
		 * during LOGO process to avoid nport handle leaks inside FW.
		 * The exception is when LOGO is done while another PLOGI with
		 * the same nport handle is waiting as might be the case here.
		 * Note: there is always a possibily of a race where session
		 * deletion has already started for other reasons (e.g. ACL
		 * removal) and now PLOGI arrives:
		 * 1. if PLOGI arrived in FW after nport handle has been freed,
		 *    FW must have assigned this PLOGI a new/same handle and we
		 *    can proceed ACK'ing it as usual when session deletion
		 *    completes.
		 * 2. if PLOGI arrived in FW before LOGO with LCF_FREE_NPORT
		 *    bit reached it, the handle has now been released. We'll
		 *    get an error when we ACK this PLOGI. Nothing will be sent
		 *    back to initiator. Initiator should eventually retry
		 *    PLOGI and situation will correct itself.
		 */
		sess->keep_nport_handle = ((sess->loop_id == loop_id) &&
		    (sess->d_id.b24 == port_id.b24));

		ql_dbg(ql_dbg_disc, vha, 0x20f9,
		    "%s %d %8phC post del sess\n",
		    __func__, __LINE__, sess->port_name);


4836
		qlt_schedule_sess_for_deletion(sess);
4837 4838 4839 4840 4841 4842
		break;
	}
out:
	return res;
}

4843 4844 4845 4846 4847 4848
/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
static int qlt_24xx_handle_els(struct scsi_qla_host *vha,
	struct imm_ntfy_from_isp *iocb)
{
4849
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
4850
	struct qla_hw_data *ha = vha->hw;
4851
	struct fc_port *sess = NULL, *conflict_sess = NULL;
4852 4853 4854 4855
	uint64_t wwn;
	port_id_t port_id;
	uint16_t loop_id;
	uint16_t wd3_lo;
4856
	int res = 0;
4857
	unsigned long flags;
4858

4859 4860
	lockdep_assert_held(&ha->hardware_lock);

4861 4862 4863 4864 4865 4866 4867 4868 4869
	wwn = wwn_to_u64(iocb->u.isp24.port_name);

	port_id.b.domain = iocb->u.isp24.port_id[2];
	port_id.b.area   = iocb->u.isp24.port_id[1];
	port_id.b.al_pa  = iocb->u.isp24.port_id[0];
	port_id.b.rsvd_1 = 0;

	loop_id = le16_to_cpu(iocb->u.isp24.nport_handle);

4870 4871 4872 4873 4874 4875
	ql_dbg(ql_dbg_disc, vha, 0xf026,
	    "qla_target(%d): Port ID: %02x:%02x:%02x ELS opcode: 0x%02x lid %d %8phC\n",
	    vha->vp_idx, iocb->u.isp24.port_id[2],
		iocb->u.isp24.port_id[1], iocb->u.isp24.port_id[0],
		   iocb->u.isp24.status_subcode, loop_id,
		iocb->u.isp24.port_name);
4876

4877 4878 4879
	/* res = 1 means ack at the end of thread
	 * res = 0 means ack async/later.
	 */
4880 4881
	switch (iocb->u.isp24.status_subcode) {
	case ELS_PLOGI:
4882 4883
		res = qlt_handle_login(vha, iocb);
		break;
4884

4885 4886 4887 4888
	case ELS_PRLI:
		if (N2N_TOPO(ha)) {
			sess = qla2x00_find_fcport_by_wwpn(vha,
			    iocb->u.isp24.port_name, 1);
4889

4890 4891 4892 4893 4894 4895 4896 4897
			if (sess && sess->plogi_link[QLT_PLOGI_LINK_SAME_WWN]) {
				ql_dbg(ql_dbg_disc, vha, 0xffff,
				    "%s %d %8phC Term PRLI due to PLOGI ACK not completed\n",
				    __func__, __LINE__,
				    iocb->u.isp24.port_name);
				qlt_send_term_imm_notif(vha, iocb, 1);
				break;
			}
4898

4899
			res = qlt_handle_login(vha, iocb);
4900 4901 4902
			break;
		}

4903 4904 4905 4906 4907
		if (IS_SW_RESV_ADDR(port_id)) {
			res = 1;
			break;
		}

4908 4909
		wd3_lo = le16_to_cpu(iocb->u.isp24.u.prli.wd3_lo);

4910 4911
		if (wwn) {
			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
4912 4913
			sess = qlt_find_sess_invalidate_other(vha, wwn, port_id,
				loop_id, &conflict_sess);
4914 4915
			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
		}
4916 4917

		if (conflict_sess) {
4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931
			switch (conflict_sess->disc_state) {
			case DSC_DELETED:
			case DSC_DELETE_PEND:
				break;
			default:
				ql_dbg(ql_dbg_tgt_mgt, vha, 0xf09b,
				    "PRLI with conflicting sess %p port %8phC\n",
				    conflict_sess, conflict_sess->port_name);
				conflict_sess->fw_login_state =
				    DSC_LS_PORT_UNAVAIL;
				qlt_send_term_imm_notif(vha, iocb, 1);
				res = 0;
				break;
			}
4932
		}
4933 4934

		if (sess != NULL) {
4935
			bool delete = false;
4936
			int sec;
4937

4938 4939
			spin_lock_irqsave(&tgt->ha->tgt.sess_lock, flags);
			switch (sess->fw_login_state) {
4940
			case DSC_LS_PLOGI_PEND:
4941 4942 4943 4944
			case DSC_LS_PLOGI_COMP:
			case DSC_LS_PRLI_COMP:
				break;
			default:
4945 4946 4947 4948 4949
				delete = true;
				break;
			}

			switch (sess->disc_state) {
4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965
			case DSC_UPD_FCPORT:
				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
				    flags);

				sec = jiffies_to_msecs(jiffies -
				    sess->jiffies_at_registration)/1000;
				if (sess->sec_since_registration < sec && sec &&
				    !(sec % 5)) {
					sess->sec_since_registration = sec;
					ql_dbg(ql_dbg_disc, sess->vha, 0xffff,
					    "%s %8phC : Slow Rport registration(%d Sec)\n",
					    __func__, sess->port_name, sec);
				}
				qlt_send_term_imm_notif(vha, iocb, 1);
				return 0;

4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976
			case DSC_LOGIN_PEND:
			case DSC_GPDB:
			case DSC_LOGIN_COMPLETE:
			case DSC_ADISC:
				delete = false;
				break;
			default:
				break;
			}

			if (delete) {
4977 4978
				spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock,
				    flags);
4979 4980 4981 4982 4983
				/*
				 * Impatient initiator sent PRLI before last
				 * PLOGI could finish. Will force him to re-try,
				 * while last one finishes.
				 */
4984
				ql_log(ql_log_warn, sess->vha, 0xf095,
4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995
				    "sess %p PRLI received, before plogi ack.\n",
				    sess);
				qlt_send_term_imm_notif(vha, iocb, 1);
				res = 0;
				break;
			}

			/*
			 * This shouldn't happen under normal circumstances,
			 * since we have deleted the old session during PLOGI
			 */
4996
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf096,
4997 4998 4999 5000 5001
			    "PRLI (loop_id %#04x) for existing sess %p (loop_id %#04x)\n",
			    sess->loop_id, sess, iocb->u.isp24.nport_handle);

			sess->local = 0;
			sess->loop_id = loop_id;
5002
			sess->d_id = port_id;
5003
			sess->fw_login_state = DSC_LS_PRLI_PEND;
5004 5005 5006 5007

			if (wd3_lo & BIT_7)
				sess->conf_compl_supported = 1;

5008 5009 5010 5011
			if ((wd3_lo & BIT_4) == 0)
				sess->port_type = FCT_INITIATOR;
			else
				sess->port_type = FCT_TARGET;
5012 5013

			spin_unlock_irqrestore(&tgt->ha->tgt.sess_lock, flags);
5014 5015 5016 5017 5018
		}
		res = 1; /* send notify ack */

		/* Make session global (not used in fabric mode) */
		if (ha->current_topology != ISP_CFG_F) {
5019
			if (sess) {
5020
				ql_dbg(ql_dbg_disc, vha, 0x20fa,
5021 5022 5023 5024 5025 5026 5027 5028 5029 5030
				    "%s %d %8phC post nack\n",
				    __func__, __LINE__, sess->port_name);
				qla24xx_post_nack_work(vha, sess, iocb,
					SRB_NACK_PRLI);
				res = 0;
			} else {
				set_bit(LOOP_RESYNC_NEEDED, &vha->dpc_flags);
				set_bit(LOCAL_LOOP_UPDATE, &vha->dpc_flags);
				qla2xxx_wake_dpc(vha);
			}
5031
		} else {
5032
			if (sess) {
5033
				ql_dbg(ql_dbg_disc, vha, 0x20fb,
5034 5035
				    "%s %d %8phC post nack\n",
				    __func__, __LINE__, sess->port_name);
5036 5037 5038 5039 5040
				qla24xx_post_nack_work(vha, sess, iocb,
					SRB_NACK_PRLI);
				res = 0;
			}
		}
5041 5042
		break;

5043 5044 5045 5046 5047 5048 5049 5050
	case ELS_TPRLO:
		if (le16_to_cpu(iocb->u.isp24.flags) &
			NOTIFY24XX_FLAGS_GLOBAL_TPRLO) {
			loop_id = 0xFFFF;
			qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS);
			res = 1;
			break;
		}
5051
		/* fall through */
5052 5053
	case ELS_LOGO:
	case ELS_PRLO:
5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064
		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
		sess = qla2x00_find_fcport_by_loopid(vha, loop_id);
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);

		if (sess) {
			sess->login_gen++;
			sess->fw_login_state = DSC_LS_LOGO_PEND;
			sess->logo_ack_needed = 1;
			memcpy(sess->iocb, iocb, IOCB_SIZE);
		}

5065
		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
5066

5067
		ql_dbg(ql_dbg_disc, vha, 0x20fc,
5068 5069 5070
		    "%s: logo %llx res %d sess %p ",
		    __func__, wwn, res, sess);
		if (res == 0) {
5071 5072 5073 5074
			/*
			 * cmd went upper layer, look for qlt_xmit_tm_rsp()
			 * for LOGO_ACK & sess delete
			 */
5075 5076 5077
			BUG_ON(!sess);
			res = 0;
		} else {
5078
			/* cmd did not go to upper layer. */
5079
			if (sess) {
5080
				qlt_schedule_sess_for_deletion(sess);
5081 5082 5083 5084
				res = 0;
			}
			/* else logo will be ack */
		}
5085 5086 5087 5088
		break;
	case ELS_PDISC:
	case ELS_ADISC:
	{
5089
		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5090

5091
		if (tgt->link_reinit_iocb_pending) {
5092 5093
			qlt_send_notify_ack(ha->base_qpair,
			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5094 5095
			tgt->link_reinit_iocb_pending = 0;
		}
5096 5097 5098 5099

		sess = qla2x00_find_fcport_by_wwpn(vha,
		    iocb->u.isp24.port_name, 1);
		if (sess) {
5100
			ql_dbg(ql_dbg_disc, vha, 0x20fd,
5101 5102 5103 5104 5105
				"sess %p lid %d|%d DS %d LS %d\n",
				sess, sess->loop_id, loop_id,
				sess->disc_state, sess->fw_login_state);
		}

5106 5107 5108 5109
		res = 1; /* send notify ack */
		break;
	}

5110
	case ELS_FLOGI:	/* should never happen */
5111 5112 5113 5114 5115 5116 5117 5118
	default:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf061,
		    "qla_target(%d): Unsupported ELS command %x "
		    "received\n", vha->vp_idx, iocb->u.isp24.status_subcode);
		res = qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS);
		break;
	}

5119 5120 5121 5122
	ql_dbg(ql_dbg_disc, vha, 0xf026,
	    "qla_target(%d): Exit ELS opcode: 0x%02x res %d\n",
	    vha->vp_idx, iocb->u.isp24.status_subcode, res);

5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136
	return res;
}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
static void qlt_handle_imm_notify(struct scsi_qla_host *vha,
	struct imm_ntfy_from_isp *iocb)
{
	struct qla_hw_data *ha = vha->hw;
	uint32_t add_flags = 0;
	int send_notify_ack = 1;
	uint16_t status;

5137 5138
	lockdep_assert_held(&ha->hardware_lock);

5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154
	status = le16_to_cpu(iocb->u.isp2x.status);
	switch (status) {
	case IMM_NTFY_LIP_RESET:
	{
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf032,
		    "qla_target(%d): LIP reset (loop %#x), subcode %x\n",
		    vha->vp_idx, le16_to_cpu(iocb->u.isp24.nport_handle),
		    iocb->u.isp24.status_subcode);

		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
			send_notify_ack = 0;
		break;
	}

	case IMM_NTFY_LIP_LINK_REINIT:
	{
5155
		struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5156

5157 5158 5159 5160 5161 5162
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf033,
		    "qla_target(%d): LINK REINIT (loop %#x, "
		    "subcode %x)\n", vha->vp_idx,
		    le16_to_cpu(iocb->u.isp24.nport_handle),
		    iocb->u.isp24.status_subcode);
		if (tgt->link_reinit_iocb_pending) {
5163 5164
			qlt_send_notify_ack(ha->base_qpair,
			    &tgt->link_reinit_iocb, 0, 0, 0, 0, 0, 0);
5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255
		}
		memcpy(&tgt->link_reinit_iocb, iocb, sizeof(*iocb));
		tgt->link_reinit_iocb_pending = 1;
		/*
		 * QLogic requires to wait after LINK REINIT for possible
		 * PDISC or ADISC ELS commands
		 */
		send_notify_ack = 0;
		break;
	}

	case IMM_NTFY_PORT_LOGOUT:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf034,
		    "qla_target(%d): Port logout (loop "
		    "%#x, subcode %x)\n", vha->vp_idx,
		    le16_to_cpu(iocb->u.isp24.nport_handle),
		    iocb->u.isp24.status_subcode);

		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS_SESS) == 0)
			send_notify_ack = 0;
		/* The sessions will be cleared in the callback, if needed */
		break;

	case IMM_NTFY_GLBL_TPRLO:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf035,
		    "qla_target(%d): Global TPRLO (%x)\n", vha->vp_idx, status);
		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
			send_notify_ack = 0;
		/* The sessions will be cleared in the callback, if needed */
		break;

	case IMM_NTFY_PORT_CONFIG:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf036,
		    "qla_target(%d): Port config changed (%x)\n", vha->vp_idx,
		    status);
		if (qlt_reset(vha, iocb, QLA_TGT_ABORT_ALL) == 0)
			send_notify_ack = 0;
		/* The sessions will be cleared in the callback, if needed */
		break;

	case IMM_NTFY_GLBL_LOGO:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06a,
		    "qla_target(%d): Link failure detected\n",
		    vha->vp_idx);
		/* I_T nexus loss */
		if (qlt_reset(vha, iocb, QLA_TGT_NEXUS_LOSS) == 0)
			send_notify_ack = 0;
		break;

	case IMM_NTFY_IOCB_OVERFLOW:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06b,
		    "qla_target(%d): Cannot provide requested "
		    "capability (IOCB overflowed the immediate notify "
		    "resource count)\n", vha->vp_idx);
		break;

	case IMM_NTFY_ABORT_TASK:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf037,
		    "qla_target(%d): Abort Task (S %08x I %#x -> "
		    "L %#x)\n", vha->vp_idx,
		    le16_to_cpu(iocb->u.isp2x.seq_id),
		    GET_TARGET_ID(ha, (struct atio_from_isp *)iocb),
		    le16_to_cpu(iocb->u.isp2x.lun));
		if (qlt_abort_task(vha, iocb) == 0)
			send_notify_ack = 0;
		break;

	case IMM_NTFY_RESOURCE:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06c,
		    "qla_target(%d): Out of resources, host %ld\n",
		    vha->vp_idx, vha->host_no);
		break;

	case IMM_NTFY_MSG_RX:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf038,
		    "qla_target(%d): Immediate notify task %x\n",
		    vha->vp_idx, iocb->u.isp2x.task_flags);
		break;

	case IMM_NTFY_ELS:
		if (qlt_24xx_handle_els(vha, iocb) == 0)
			send_notify_ack = 0;
		break;
	default:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06d,
		    "qla_target(%d): Received unknown immediate "
		    "notify status %x\n", vha->vp_idx, status);
		break;
	}

	if (send_notify_ack)
5256 5257
		qlt_send_notify_ack(ha->base_qpair, iocb, add_flags, 0, 0, 0,
		    0, 0);
5258 5259 5260 5261 5262 5263
}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 * This function sends busy to ISP 2xxx or 24xx.
 */
5264
static int __qlt_send_busy(struct qla_qpair *qpair,
5265 5266
	struct atio_from_isp *atio, uint16_t status)
{
5267
	struct scsi_qla_host *vha = qpair->vha;
5268 5269 5270
	struct ctio7_to_24xx *ctio24;
	struct qla_hw_data *ha = vha->hw;
	request_t *pkt;
5271
	struct fc_port *sess = NULL;
5272
	unsigned long flags;
5273
	u16 temp;
5274 5275
	port_id_t id;

5276
	id = be_to_port_id(atio->u.isp24.fcp_hdr.s_id);
5277

5278
	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
5279
	sess = qla2x00_find_fcport_by_nportid(vha, &id, 1);
5280
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
5281
	if (!sess) {
5282
		qlt_send_term_exchange(qpair, NULL, atio, 1, 0);
5283
		return 0;
5284 5285 5286
	}
	/* Sending marker isn't necessary, since we called from ISR */

5287
	pkt = (request_t *)__qla2x00_alloc_iocbs(qpair, NULL);
5288
	if (!pkt) {
5289
		ql_dbg(ql_dbg_io, vha, 0x3063,
5290 5291
		    "qla_target(%d): %s failed: unable to allocate "
		    "request packet", vha->vp_idx, __func__);
5292
		return -ENOMEM;
5293 5294
	}

5295
	qpair->tgt_counters.num_q_full_sent++;
5296 5297 5298 5299 5300 5301
	pkt->entry_count = 1;
	pkt->handle = QLA_TGT_SKIP_HANDLE | CTIO_COMPLETION_HANDLE_MARK;

	ctio24 = (struct ctio7_to_24xx *)pkt;
	ctio24->entry_type = CTIO_TYPE7;
	ctio24->nport_handle = sess->loop_id;
5302
	ctio24->timeout = cpu_to_le16(QLA_TGT_TIMEOUT);
5303
	ctio24->vp_index = vha->vp_idx;
5304
	ctio24->initiator_id = be_id_to_le(atio->u.isp24.fcp_hdr.s_id);
5305
	ctio24->exchange_addr = atio->u.isp24.exchange_addr;
5306
	temp = (atio->u.isp24.attr << 9) |
5307
		CTIO7_FLAGS_STATUS_MODE_1 | CTIO7_FLAGS_SEND_STATUS |
5308 5309
		CTIO7_FLAGS_DONT_RET_CTIO;
	ctio24->u.status1.flags = cpu_to_le16(temp);
5310 5311 5312 5313 5314 5315
	/*
	 * CTIO from fw w/o se_cmd doesn't provide enough info to retry it,
	 * if the explicit conformation is used.
	 */
	ctio24->u.status1.ox_id = swab16(atio->u.isp24.fcp_hdr.ox_id);
	ctio24->u.status1.scsi_status = cpu_to_le16(status);
5316 5317 5318 5319 5320 5321

	ctio24->u.status1.residual = get_datalen_for_atio(atio);

	if (ctio24->u.status1.residual != 0)
		ctio24->u.status1.scsi_status |= SS_RESIDUAL_UNDER;

5322 5323
	/* Memory Barrier */
	wmb();
5324 5325 5326 5327
	if (qpair->reqq_start_iocbs)
		qpair->reqq_start_iocbs(qpair);
	else
		qla2x00_start_iocbs(vha, qpair->req);
5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341
	return 0;
}

/*
 * This routine is used to allocate a command for either a QFull condition
 * (ie reply SAM_STAT_BUSY) or to terminate an exchange that did not go
 * out previously.
 */
static void
qlt_alloc_qfull_cmd(struct scsi_qla_host *vha,
	struct atio_from_isp *atio, uint16_t status, int qfull)
{
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
	struct qla_hw_data *ha = vha->hw;
5342
	struct fc_port *sess;
5343 5344
	struct se_session *se_sess;
	struct qla_tgt_cmd *cmd;
5345
	int tag, cpu;
5346
	unsigned long flags;
5347 5348 5349 5350 5351 5352 5353 5354 5355 5356

	if (unlikely(tgt->tgt_stop)) {
		ql_dbg(ql_dbg_io, vha, 0x300a,
			"New command while device %p is shutting down\n", tgt);
		return;
	}

	if ((vha->hw->tgt.num_qfull_cmds_alloc + 1) > MAX_QFULL_CMDS_ALLOC) {
		vha->hw->tgt.num_qfull_cmds_dropped++;
		if (vha->hw->tgt.num_qfull_cmds_dropped >
5357 5358
			vha->qla_stats.stat_max_qfull_cmds_dropped)
			vha->qla_stats.stat_max_qfull_cmds_dropped =
5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376
				vha->hw->tgt.num_qfull_cmds_dropped;

		ql_dbg(ql_dbg_io, vha, 0x3068,
			"qla_target(%d): %s: QFull CMD dropped[%d]\n",
			vha->vp_idx, __func__,
			vha->hw->tgt.num_qfull_cmds_dropped);

		qlt_chk_exch_leak_thresh_hold(vha);
		return;
	}

	sess = ha->tgt.tgt_ops->find_sess_by_s_id
		(vha, atio->u.isp24.fcp_hdr.s_id);
	if (!sess)
		return;

	se_sess = sess->se_sess;

5377
	tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
5378
	if (tag < 0) {
5379 5380 5381 5382 5383 5384
		ql_dbg(ql_dbg_io, vha, 0x3009,
			"qla_target(%d): %s: Allocation of cmd failed\n",
			vha->vp_idx, __func__);

		vha->hw->tgt.num_qfull_cmds_dropped++;
		if (vha->hw->tgt.num_qfull_cmds_dropped >
5385 5386
			vha->qla_stats.stat_max_qfull_cmds_dropped)
			vha->qla_stats.stat_max_qfull_cmds_dropped =
5387 5388 5389 5390 5391 5392
				vha->hw->tgt.num_qfull_cmds_dropped;

		qlt_chk_exch_leak_thresh_hold(vha);
		return;
	}

5393
	cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
5394 5395 5396 5397 5398 5399 5400 5401
	memset(cmd, 0, sizeof(struct qla_tgt_cmd));

	qlt_incr_num_pend_cmds(vha);
	INIT_LIST_HEAD(&cmd->cmd_list);
	memcpy(&cmd->atio, atio, sizeof(*atio));

	cmd->tgt = vha->vha_tgt.qla_tgt;
	cmd->vha = vha;
5402
	cmd->reset_count = ha->base_qpair->chip_reset;
5403
	cmd->q_full = 1;
5404
	cmd->qpair = ha->base_qpair;
5405
	cmd->se_cmd.map_cpu = cpu;
5406 5407 5408 5409 5410 5411 5412 5413

	if (qfull) {
		cmd->q_full = 1;
		/* NOTE: borrowing the state field to carry the status */
		cmd->state = status;
	} else
		cmd->term_exchg = 1;

5414
	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5415 5416 5417 5418
	list_add_tail(&cmd->cmd_list, &vha->hw->tgt.q_full_list);

	vha->hw->tgt.num_qfull_cmds_alloc++;
	if (vha->hw->tgt.num_qfull_cmds_alloc >
5419 5420
		vha->qla_stats.stat_max_qfull_cmds_alloc)
		vha->qla_stats.stat_max_qfull_cmds_alloc =
5421
			vha->hw->tgt.num_qfull_cmds_alloc;
5422
	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5423 5424 5425
}

int
5426
qlt_free_qfull_cmds(struct qla_qpair *qpair)
5427
{
5428
	struct scsi_qla_host *vha = qpair->vha;
5429 5430 5431
	struct qla_hw_data *ha = vha->hw;
	unsigned long flags;
	struct qla_tgt_cmd *cmd, *tcmd;
5432
	struct list_head free_list, q_full_list;
5433 5434 5435 5436 5437 5438
	int rc = 0;

	if (list_empty(&ha->tgt.q_full_list))
		return 0;

	INIT_LIST_HEAD(&free_list);
5439
	INIT_LIST_HEAD(&q_full_list);
5440

5441
	spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
5442
	if (list_empty(&ha->tgt.q_full_list)) {
5443
		spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
5444 5445 5446
		return 0;
	}

5447 5448 5449 5450 5451
	list_splice_init(&vha->hw->tgt.q_full_list, &q_full_list);
	spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);

	spin_lock_irqsave(qpair->qp_lock_ptr, flags);
	list_for_each_entry_safe(cmd, tcmd, &q_full_list, cmd_list) {
5452 5453
		if (cmd->q_full)
			/* cmd->state is a borrowed field to hold status */
5454
			rc = __qlt_send_busy(qpair, &cmd->atio, cmd->state);
5455
		else if (cmd->term_exchg)
5456
			rc = __qlt_send_term_exchange(qpair, NULL, &cmd->atio);
5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479

		if (rc == -ENOMEM)
			break;

		if (cmd->q_full)
			ql_dbg(ql_dbg_io, vha, 0x3006,
			    "%s: busy sent for ox_id[%04x]\n", __func__,
			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
		else if (cmd->term_exchg)
			ql_dbg(ql_dbg_io, vha, 0x3007,
			    "%s: Term exchg sent for ox_id[%04x]\n", __func__,
			    be16_to_cpu(cmd->atio.u.isp24.fcp_hdr.ox_id));
		else
			ql_dbg(ql_dbg_io, vha, 0x3008,
			    "%s: Unexpected cmd in QFull list %p\n", __func__,
			    cmd);

		list_del(&cmd->cmd_list);
		list_add_tail(&cmd->cmd_list, &free_list);

		/* piggy back on hardware_lock for protection */
		vha->hw->tgt.num_qfull_cmds_alloc--;
	}
5480
	spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
5481 5482 5483 5484 5485 5486 5487 5488 5489 5490

	cmd = NULL;

	list_for_each_entry_safe(cmd, tcmd, &free_list, cmd_list) {
		list_del(&cmd->cmd_list);
		/* This cmd was never sent to TCM.  There is no need
		 * to schedule free or call free_cmd
		 */
		qlt_free_cmd(cmd);
	}
5491 5492 5493 5494 5495 5496 5497

	if (!list_empty(&q_full_list)) {
		spin_lock_irqsave(&vha->hw->tgt.q_full_lock, flags);
		list_splice(&q_full_list, &vha->hw->tgt.q_full_list);
		spin_unlock_irqrestore(&vha->hw->tgt.q_full_lock, flags);
	}

5498 5499 5500 5501
	return rc;
}

static void
5502 5503
qlt_send_busy(struct qla_qpair *qpair, struct atio_from_isp *atio,
    uint16_t status)
5504 5505
{
	int rc = 0;
5506
	struct scsi_qla_host *vha = qpair->vha;
5507

5508
	rc = __qlt_send_busy(qpair, atio, status);
5509 5510 5511 5512 5513
	if (rc == -ENOMEM)
		qlt_alloc_qfull_cmd(vha, atio, status, 1);
}

static int
5514 5515
qlt_chk_qfull_thresh_hold(struct scsi_qla_host *vha, struct qla_qpair *qpair,
	struct atio_from_isp *atio, uint8_t ha_locked)
5516 5517
{
	struct qla_hw_data *ha = vha->hw;
5518
	unsigned long flags;
5519 5520 5521 5522

	if (ha->tgt.num_pend_cmds < Q_FULL_THRESH_HOLD(ha))
		return 0;

5523 5524
	if (!ha_locked)
		spin_lock_irqsave(&ha->hardware_lock, flags);
5525
	qlt_send_busy(qpair, atio, qla_sam_status);
5526 5527 5528
	if (!ha_locked)
		spin_unlock_irqrestore(&ha->hardware_lock, flags);

5529
	return 1;
5530 5531 5532 5533 5534
}

/* ha->hardware_lock supposed to be held on entry */
/* called via callback from qla2xxx */
static void qlt_24xx_atio_pkt(struct scsi_qla_host *vha,
5535
	struct atio_from_isp *atio, uint8_t ha_locked)
5536 5537
{
	struct qla_hw_data *ha = vha->hw;
5538
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5539
	int rc;
5540
	unsigned long flags = 0;
5541 5542

	if (unlikely(tgt == NULL)) {
5543
		ql_dbg(ql_dbg_tgt, vha, 0x3064,
5544 5545 5546 5547 5548 5549 5550 5551
		    "ATIO pkt, but no tgt (ha %p)", ha);
		return;
	}
	/*
	 * In tgt_stop mode we also should allow all requests to pass.
	 * Otherwise, some commands can stuck.
	 */

5552
	tgt->atio_irq_cmd_count++;
5553 5554 5555 5556 5557

	switch (atio->u.raw.entry_type) {
	case ATIO_TYPE7:
		if (unlikely(atio->u.isp24.exchange_addr ==
		    ATIO_EXCHANGE_ADDRESS_UNKNOWN)) {
5558
			ql_dbg(ql_dbg_io, vha, 0x3065,
5559 5560 5561
			    "qla_target(%d): ATIO_TYPE7 "
			    "received with UNKNOWN exchange address, "
			    "sending QUEUE_FULL\n", vha->vp_idx);
5562 5563
			if (!ha_locked)
				spin_lock_irqsave(&ha->hardware_lock, flags);
5564
			qlt_send_busy(ha->base_qpair, atio, qla_sam_status);
5565
			if (!ha_locked)
5566 5567
				spin_unlock_irqrestore(&ha->hardware_lock,
				    flags);
5568 5569
			break;
		}
5570 5571

		if (likely(atio->u.isp24.fcp_cmnd.task_mgmt_flags == 0)) {
5572 5573
			rc = qlt_chk_qfull_thresh_hold(vha, ha->base_qpair,
			    atio, ha_locked);
5574
			if (rc != 0) {
5575
				tgt->atio_irq_cmd_count--;
5576 5577
				return;
			}
5578
			rc = qlt_handle_cmd_for_atio(vha, atio);
5579
		} else {
5580
			rc = qlt_handle_task_mgmt(vha, atio);
5581
		}
5582
		if (unlikely(rc != 0)) {
5583 5584 5585 5586 5587 5588 5589 5590 5591 5592
			if (!ha_locked)
				spin_lock_irqsave(&ha->hardware_lock, flags);
			switch (rc) {
			case -ENODEV:
				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
				    "qla_target: Unable to send command to target\n");
				break;
			case -EBADF:
				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
5593 5594
				qlt_send_term_exchange(ha->base_qpair, NULL,
				    atio, 1, 0);
5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609
				break;
			case -EBUSY:
				ql_dbg(ql_dbg_tgt, vha, 0xe060,
				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
				    vha->vp_idx);
				qlt_send_busy(ha->base_qpair, atio,
				    tc_sam_status);
				break;
			default:
				ql_dbg(ql_dbg_tgt, vha, 0xe060,
				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
				    vha->vp_idx);
				qlt_send_busy(ha->base_qpair, atio,
				    qla_sam_status);
				break;
5610
			}
5611 5612 5613
			if (!ha_locked)
				spin_unlock_irqrestore(&ha->hardware_lock,
				    flags);
5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627
		}
		break;

	case IMMED_NOTIFY_TYPE:
	{
		if (unlikely(atio->u.isp2x.entry_status != 0)) {
			ql_dbg(ql_dbg_tgt, vha, 0xe05b,
			    "qla_target(%d): Received ATIO packet %x "
			    "with error status %x\n", vha->vp_idx,
			    atio->u.raw.entry_type,
			    atio->u.isp2x.entry_status);
			break;
		}
		ql_dbg(ql_dbg_tgt, vha, 0xe02e, "%s", "IMMED_NOTIFY ATIO");
5628 5629 5630

		if (!ha_locked)
			spin_lock_irqsave(&ha->hardware_lock, flags);
5631
		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)atio);
5632 5633
		if (!ha_locked)
			spin_unlock_irqrestore(&ha->hardware_lock, flags);
5634 5635 5636 5637 5638 5639 5640 5641 5642 5643
		break;
	}

	default:
		ql_dbg(ql_dbg_tgt, vha, 0xe05c,
		    "qla_target(%d): Received unknown ATIO atio "
		    "type %x\n", vha->vp_idx, atio->u.raw.entry_type);
		break;
	}

5644
	tgt->atio_irq_cmd_count--;
5645 5646
}

5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692
/*
 * qpair lock is assume to be held
 * rc = 0 : send terminate & abts respond
 * rc != 0: do not send term & abts respond
 */
static int qlt_chk_unresolv_exchg(struct scsi_qla_host *vha,
    struct qla_qpair *qpair, struct abts_resp_from_24xx_fw *entry)
{
	struct qla_hw_data *ha = vha->hw;
	int rc = 0;

	/*
	 * Detect unresolved exchange. If the same ABTS is unable
	 * to terminate an existing command and the same ABTS loops
	 * between FW & Driver, then force FW dump. Under 1 jiff,
	 * we should see multiple loops.
	 */
	if (qpair->retry_term_exchg_addr == entry->exchange_addr_to_abort &&
	    qpair->retry_term_jiff == jiffies) {
		/* found existing exchange */
		qpair->retry_term_cnt++;
		if (qpair->retry_term_cnt >= 5) {
			rc = EIO;
			qpair->retry_term_cnt = 0;
			ql_log(ql_log_warn, vha, 0xffff,
			    "Unable to send ABTS Respond. Dumping firmware.\n");
			ql_dump_buffer(ql_dbg_tgt_mgt + ql_dbg_buffer,
			    vha, 0xffff, (uint8_t *)entry, sizeof(*entry));

			if (qpair == ha->base_qpair)
				ha->isp_ops->fw_dump(vha, 1);
			else
				ha->isp_ops->fw_dump(vha, 0);

			set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
			qla2xxx_wake_dpc(vha);
		}
	} else if (qpair->retry_term_jiff != jiffies) {
		qpair->retry_term_exchg_addr = entry->exchange_addr_to_abort;
		qpair->retry_term_cnt = 0;
		qpair->retry_term_jiff = jiffies;
	}

	return rc;
}

5693 5694 5695 5696 5697 5698 5699 5700 5701 5702

static void qlt_handle_abts_completion(struct scsi_qla_host *vha,
	struct rsp_que *rsp, response_t *pkt)
{
	struct abts_resp_from_24xx_fw *entry =
		(struct abts_resp_from_24xx_fw *)pkt;
	u32 h = pkt->handle & ~QLA_TGT_HANDLE_MASK;
	struct qla_tgt_mgmt_cmd *mcmd;
	struct qla_hw_data *ha = vha->hw;

5703
	mcmd = qlt_ctio_to_cmd(vha, rsp, pkt->handle, pkt);
5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740
	if (mcmd == NULL && h != QLA_TGT_SKIP_HANDLE) {
		ql_dbg(ql_dbg_async, vha, 0xe064,
		    "qla_target(%d): ABTS Comp without mcmd\n",
		    vha->vp_idx);
		return;
	}

	if (mcmd)
		vha  = mcmd->vha;
	vha->vha_tgt.qla_tgt->abts_resp_expected--;

	ql_dbg(ql_dbg_tgt, vha, 0xe038,
	    "ABTS_RESP_24XX: compl_status %x\n",
	    entry->compl_status);

	if (le16_to_cpu(entry->compl_status) != ABTS_RESP_COMPL_SUCCESS) {
		if ((entry->error_subcode1 == 0x1E) &&
		    (entry->error_subcode2 == 0)) {
			if (qlt_chk_unresolv_exchg(vha, rsp->qpair, entry)) {
				ha->tgt.tgt_ops->free_mcmd(mcmd);
				return;
			}
			qlt_24xx_retry_term_exchange(vha, rsp->qpair,
			    pkt, mcmd);
		} else {
			ql_dbg(ql_dbg_tgt, vha, 0xe063,
			    "qla_target(%d): ABTS_RESP_24XX failed %x (subcode %x:%x)",
			    vha->vp_idx, entry->compl_status,
			    entry->error_subcode1,
			    entry->error_subcode2);
			ha->tgt.tgt_ops->free_mcmd(mcmd);
		}
	} else {
		ha->tgt.tgt_ops->free_mcmd(mcmd);
	}
}

5741 5742
/* ha->hardware_lock supposed to be held on entry */
/* called via callback from qla2xxx */
5743 5744
static void qlt_response_pkt(struct scsi_qla_host *vha,
	struct rsp_que *rsp, response_t *pkt)
5745
{
5746
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5747 5748 5749

	if (unlikely(tgt == NULL)) {
		ql_dbg(ql_dbg_tgt, vha, 0xe05d,
5750 5751
		    "qla_target(%d): Response pkt %x received, but no tgt (ha %p)\n",
		    vha->vp_idx, pkt->entry_type, vha->hw);
5752 5753 5754 5755 5756 5757 5758 5759 5760
		return;
	}

	/*
	 * In tgt_stop mode we also should allow all requests to pass.
	 * Otherwise, some commands can stuck.
	 */

	switch (pkt->entry_type) {
5761
	case CTIO_CRC2:
5762 5763 5764
	case CTIO_TYPE7:
	{
		struct ctio7_from_24xx *entry = (struct ctio7_from_24xx *)pkt;
5765

5766
		qlt_do_ctio_completion(vha, rsp, entry->handle,
5767 5768 5769 5770 5771 5772 5773 5774 5775
		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
		    entry);
		break;
	}

	case ACCEPT_TGT_IO_TYPE:
	{
		struct atio_from_isp *atio = (struct atio_from_isp *)pkt;
		int rc;
5776

5777
		if (atio->u.isp2x.status !=
5778
		    cpu_to_le16(ATIO_CDB_VALID)) {
5779 5780 5781 5782 5783 5784 5785
			ql_dbg(ql_dbg_tgt, vha, 0xe05e,
			    "qla_target(%d): ATIO with error "
			    "status %x received\n", vha->vp_idx,
			    le16_to_cpu(atio->u.isp2x.status));
			break;
		}

5786
		rc = qlt_chk_qfull_thresh_hold(vha, rsp->qpair, atio, 1);
5787
		if (rc != 0)
5788 5789
			return;

5790 5791
		rc = qlt_handle_cmd_for_atio(vha, atio);
		if (unlikely(rc != 0)) {
5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816
			switch (rc) {
			case -ENODEV:
				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
				    "qla_target: Unable to send command to target\n");
				break;
			case -EBADF:
				ql_dbg(ql_dbg_tgt, vha, 0xe05f,
				    "qla_target: Unable to send command to target, sending TERM EXCHANGE for rsp\n");
				qlt_send_term_exchange(rsp->qpair, NULL,
				    atio, 1, 0);
				break;
			case -EBUSY:
				ql_dbg(ql_dbg_tgt, vha, 0xe060,
				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
				    vha->vp_idx);
				qlt_send_busy(rsp->qpair, atio,
				    tc_sam_status);
				break;
			default:
				ql_dbg(ql_dbg_tgt, vha, 0xe060,
				    "qla_target(%d): Unable to send command to target, sending BUSY status\n",
				    vha->vp_idx);
				qlt_send_busy(rsp->qpair, atio,
				    qla_sam_status);
				break;
5817 5818 5819 5820 5821 5822 5823 5824
			}
		}
	}
	break;

	case CONTINUE_TGT_IO_TYPE:
	{
		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5825

5826
		qlt_do_ctio_completion(vha, rsp, entry->handle,
5827 5828 5829 5830 5831 5832 5833 5834
		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
		    entry);
		break;
	}

	case CTIO_A64_TYPE:
	{
		struct ctio_to_2xxx *entry = (struct ctio_to_2xxx *)pkt;
5835

5836
		qlt_do_ctio_completion(vha, rsp, entry->handle,
5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849
		    le16_to_cpu(entry->status)|(pkt->entry_status << 16),
		    entry);
		break;
	}

	case IMMED_NOTIFY_TYPE:
		ql_dbg(ql_dbg_tgt, vha, 0xe035, "%s", "IMMED_NOTIFY\n");
		qlt_handle_imm_notify(vha, (struct imm_ntfy_from_isp *)pkt);
		break;

	case NOTIFY_ACK_TYPE:
		if (tgt->notify_ack_expected > 0) {
			struct nack_to_isp *entry = (struct nack_to_isp *)pkt;
5850

5851 5852 5853 5854 5855 5856
			ql_dbg(ql_dbg_tgt, vha, 0xe036,
			    "NOTIFY_ACK seq %08x status %x\n",
			    le16_to_cpu(entry->u.isp2x.seq_id),
			    le16_to_cpu(entry->u.isp2x.status));
			tgt->notify_ack_expected--;
			if (entry->u.isp2x.status !=
5857
			    cpu_to_le16(NOTIFY_ACK_SUCCESS)) {
5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877
				ql_dbg(ql_dbg_tgt, vha, 0xe061,
				    "qla_target(%d): NOTIFY_ACK "
				    "failed %x\n", vha->vp_idx,
				    le16_to_cpu(entry->u.isp2x.status));
			}
		} else {
			ql_dbg(ql_dbg_tgt, vha, 0xe062,
			    "qla_target(%d): Unexpected NOTIFY_ACK received\n",
			    vha->vp_idx);
		}
		break;

	case ABTS_RECV_24XX:
		ql_dbg(ql_dbg_tgt, vha, 0xe037,
		    "ABTS_RECV_24XX: instance %d\n", vha->vp_idx);
		qlt_24xx_handle_abts(vha, (struct abts_recv_from_24xx *)pkt);
		break;

	case ABTS_RESP_24XX:
		if (tgt->abts_resp_expected > 0) {
5878
			qlt_handle_abts_completion(vha, rsp, pkt);
5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901
		} else {
			ql_dbg(ql_dbg_tgt, vha, 0xe064,
			    "qla_target(%d): Unexpected ABTS_RESP_24XX "
			    "received\n", vha->vp_idx);
		}
		break;

	default:
		ql_dbg(ql_dbg_tgt, vha, 0xe065,
		    "qla_target(%d): Received unknown response pkt "
		    "type %x\n", vha->vp_idx, pkt->entry_type);
		break;
	}

}

/*
 * ha->hardware_lock supposed to be held on entry. Might drop it, then reaquire
 */
void qlt_async_event(uint16_t code, struct scsi_qla_host *vha,
	uint16_t *mailbox)
{
	struct qla_hw_data *ha = vha->hw;
5902
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
5903
	int login_code;
5904

5905
	if (!tgt || tgt->tgt_stop || tgt->tgt_stopped)
5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923
		return;

	if (((code == MBA_POINT_TO_POINT) || (code == MBA_CHG_IN_CONNECTION)) &&
	    IS_QLA2100(ha))
		return;
	/*
	 * In tgt_stop mode we also should allow all requests to pass.
	 * Otherwise, some commands can stuck.
	 */


	switch (code) {
	case MBA_RESET:			/* Reset */
	case MBA_SYSTEM_ERR:		/* System Error */
	case MBA_REQ_TRANSFER_ERR:	/* Request Transfer Error */
	case MBA_RSP_TRANSFER_ERR:	/* Response Transfer Error */
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03a,
		    "qla_target(%d): System error async event %#x "
5924
		    "occurred", vha->vp_idx, code);
5925 5926 5927 5928 5929 5930 5931 5932
		break;
	case MBA_WAKEUP_THRES:		/* Request Queue Wake-up. */
		set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
		break;

	case MBA_LOOP_UP:
	{
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03b,
5933
		    "qla_target(%d): Async LOOP_UP occurred "
5934 5935 5936
		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx,
		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5937
		if (tgt->link_reinit_iocb_pending) {
5938 5939
			qlt_send_notify_ack(ha->base_qpair,
			    (void *)&tgt->link_reinit_iocb,
5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950
			    0, 0, 0, 0, 0, 0);
			tgt->link_reinit_iocb_pending = 0;
		}
		break;
	}

	case MBA_LIP_OCCURRED:
	case MBA_LOOP_DOWN:
	case MBA_LIP_RESET:
	case MBA_RSCN_UPDATE:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03c,
5951
		    "qla_target(%d): Async event %#x occurred "
5952 5953 5954
		    "(m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)", vha->vp_idx, code,
		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5955 5956
		break;

5957
	case MBA_REJECTED_FCP_CMD:
5958 5959 5960 5961 5962
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf017,
		    "qla_target(%d): Async event LS_REJECT occurred (m[0]=%x, m[1]=%x, m[2]=%x, m[3]=%x)",
		    vha->vp_idx,
		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));
5963 5964 5965 5966 5967

		if (le16_to_cpu(mailbox[3]) == 1) {
			/* exchange starvation. */
			vha->hw->exch_starvation++;
			if (vha->hw->exch_starvation > 5) {
5968
				ql_log(ql_log_warn, vha, 0xd03a,
5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982
				    "Exchange starvation-. Resetting RISC\n");

				vha->hw->exch_starvation = 0;
				if (IS_P3P_TYPE(vha->hw))
					set_bit(FCOE_CTX_RESET_NEEDED,
					    &vha->dpc_flags);
				else
					set_bit(ISP_ABORT_NEEDED,
					    &vha->dpc_flags);
				qla2xxx_wake_dpc(vha);
			}
		}
		break;

5983 5984 5985
	case MBA_PORT_UPDATE:
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03d,
		    "qla_target(%d): Port update async event %#x "
5986
		    "occurred: updating the ports database (m[0]=%x, m[1]=%x, "
5987 5988 5989 5990 5991
		    "m[2]=%x, m[3]=%x)", vha->vp_idx, code,
		    le16_to_cpu(mailbox[0]), le16_to_cpu(mailbox[1]),
		    le16_to_cpu(mailbox[2]), le16_to_cpu(mailbox[3]));

		login_code = le16_to_cpu(mailbox[2]);
5992
		if (login_code == 0x4) {
5993 5994
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03e,
			    "Async MB 2: Got PLOGI Complete\n");
5995 5996
			vha->hw->exch_starvation = 0;
		} else if (login_code == 0x7)
5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf03f,
			    "Async MB 2: Port Logged Out\n");
		break;
	default:
		break;
	}

}

static fc_port_t *qlt_get_port_database(struct scsi_qla_host *vha,
	uint16_t loop_id)
{
6009
	fc_port_t *fcport, *tfcp, *del;
6010
	int rc;
6011 6012
	unsigned long flags;
	u8 newfcport = 0;
6013

6014
	fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
6015 6016 6017 6018 6019 6020 6021 6022 6023
	if (!fcport) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf06f,
		    "qla_target(%d): Allocation of tmp FC port failed",
		    vha->vp_idx);
		return NULL;
	}

	fcport->loop_id = loop_id;

6024
	rc = qla24xx_gpdb_wait(vha, fcport, 0);
6025 6026 6027 6028 6029 6030 6031 6032 6033
	if (rc != QLA_SUCCESS) {
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf070,
		    "qla_target(%d): Failed to retrieve fcport "
		    "information -- get_port_database() returned %x "
		    "(loop_id=0x%04x)", vha->vp_idx, rc, loop_id);
		kfree(fcport);
		return NULL;
	}

6034 6035 6036 6037 6038 6039 6040 6041 6042
	del = NULL;
	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
	tfcp = qla2x00_find_fcport_by_wwpn(vha, fcport->port_name, 1);

	if (tfcp) {
		tfcp->d_id = fcport->d_id;
		tfcp->port_type = fcport->port_type;
		tfcp->supported_classes = fcport->supported_classes;
		tfcp->flags |= fcport->flags;
6043
		tfcp->scan_state = QLA_FCPORT_FOUND;
6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067

		del = fcport;
		fcport = tfcp;
	} else {
		if (vha->hw->current_topology == ISP_CFG_F)
			fcport->flags |= FCF_FABRIC_DEVICE;

		list_add_tail(&fcport->list, &vha->vp_fcports);
		if (!IS_SW_RESV_ADDR(fcport->d_id))
		   vha->fcport_count++;
		fcport->login_gen++;
		fcport->disc_state = DSC_LOGIN_COMPLETE;
		fcport->login_succ = 1;
		newfcport = 1;
	}

	fcport->deleted = 0;
	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);

	switch (vha->host->active_mode) {
	case MODE_INITIATOR:
	case MODE_DUAL:
		if (newfcport) {
			if (!IS_IIDMA_CAPABLE(vha->hw) || !vha->hw->flags.gpsc_supported) {
6068
				qla24xx_sched_upd_fcport(fcport);
6069
			} else {
6070
				ql_dbg(ql_dbg_disc, vha, 0x20ff,
6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084
				   "%s %d %8phC post gpsc fcp_cnt %d\n",
				   __func__, __LINE__, fcport->port_name, vha->fcport_count);
				qla24xx_post_gpsc_work(vha, fcport);
			}
		}
		break;

	case MODE_TARGET:
	default:
		break;
	}
	if (del)
		qla2x00_free_fcport(del);

6085 6086 6087 6088
	return fcport;
}

/* Must be called under tgt_mutex */
6089
static struct fc_port *qlt_make_local_sess(struct scsi_qla_host *vha,
6090
					   be_id_t s_id)
6091
{
6092
	struct fc_port *sess = NULL;
6093 6094 6095 6096
	fc_port_t *fcport = NULL;
	int rc, global_resets;
	uint16_t loop_id = 0;

6097
	if (s_id.domain == 0xFF && s_id.area == 0xFC) {
6098 6099 6100 6101 6102 6103
		/*
		 * This is Domain Controller, so it should be
		 * OK to drop SCSI commands from it.
		 */
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf042,
		    "Unable to find initiator with S_ID %x:%x:%x",
6104
		    s_id.domain, s_id.area, s_id.al_pa);
6105 6106 6107
		return NULL;
	}

6108 6109
	mutex_lock(&vha->vha_tgt.tgt_mutex);

6110
retry:
6111 6112
	global_resets =
	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count);
6113 6114 6115

	rc = qla24xx_get_loop_id(vha, s_id, &loop_id);
	if (rc != 0) {
6116 6117
		mutex_unlock(&vha->vha_tgt.tgt_mutex);

6118 6119 6120
		ql_log(ql_log_info, vha, 0xf071,
		    "qla_target(%d): Unable to find "
		    "initiator with S_ID %x:%x:%x",
6121
		    vha->vp_idx, s_id.domain, s_id.area, s_id.al_pa);
6122 6123 6124

		if (rc == -ENOENT) {
			qlt_port_logo_t logo;
6125

6126
			logo.id = be_to_port_id(s_id);
6127 6128 6129 6130
			logo.cmd_count = 1;
			qlt_send_first_logo(vha, &logo);
		}

6131 6132 6133 6134
		return NULL;
	}

	fcport = qlt_get_port_database(vha, loop_id);
6135 6136
	if (!fcport) {
		mutex_unlock(&vha->vha_tgt.tgt_mutex);
6137
		return NULL;
6138
	}
6139 6140

	if (global_resets !=
6141
	    atomic_read(&vha->vha_tgt.qla_tgt->tgt_global_resets_count)) {
6142 6143 6144 6145
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf043,
		    "qla_target(%d): global reset during session discovery "
		    "(counter was %d, new %d), retrying", vha->vp_idx,
		    global_resets,
6146 6147
		    atomic_read(&vha->vha_tgt.
			qla_tgt->tgt_global_resets_count));
6148 6149 6150 6151 6152
		goto retry;
	}

	sess = qlt_create_sess(vha, fcport, true);

6153 6154
	mutex_unlock(&vha->vha_tgt.tgt_mutex);

6155 6156 6157 6158 6159 6160 6161 6162
	return sess;
}

static void qlt_abort_work(struct qla_tgt *tgt,
	struct qla_tgt_sess_work_param *prm)
{
	struct scsi_qla_host *vha = tgt->vha;
	struct qla_hw_data *ha = vha->hw;
6163
	struct fc_port *sess = NULL;
6164
	unsigned long flags = 0, flags2 = 0;
6165
	be_id_t s_id;
6166 6167
	int rc;

6168
	spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6169 6170

	if (tgt->tgt_stop)
6171
		goto out_term2;
6172

6173
	s_id = le_id_to_be(prm->abts.fcp_hdr_le.s_id);
6174

6175
	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
6176
	if (!sess) {
6177
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);
6178 6179 6180 6181

		sess = qlt_make_local_sess(vha, s_id);
		/* sess has got an extra creation ref */

6182
		spin_lock_irqsave(&ha->tgt.sess_lock, flags2);
6183
		if (!sess)
6184
			goto out_term2;
6185
	} else {
6186
		if (sess->deleted) {
6187
			sess = NULL;
6188
			goto out_term2;
6189 6190
		}

6191
		if (!kref_get_unless_zero(&sess->sess_kref)) {
6192
			ql_dbg(ql_dbg_tgt_tmr, vha, 0xf01c,
6193 6194 6195 6196 6197
			    "%s: kref_get fail %8phC \n",
			     __func__, sess->port_name);
			sess = NULL;
			goto out_term2;
		}
6198 6199 6200
	}

	rc = __qlt_24xx_handle_abts(vha, &prm->abts, sess);
6201 6202
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);

6203 6204
	ha->tgt.tgt_ops->put_sess(sess);

6205 6206 6207 6208
	if (rc != 0)
		goto out_term;
	return;

6209
out_term2:
6210 6211
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags2);

6212
out_term:
6213
	spin_lock_irqsave(&ha->hardware_lock, flags);
6214 6215
	qlt_24xx_send_abts_resp(ha->base_qpair, &prm->abts,
	    FCP_TMF_REJECTED, false);
6216
	spin_unlock_irqrestore(&ha->hardware_lock, flags);
6217 6218 6219 6220 6221 6222 6223 6224
}

static void qlt_tmr_work(struct qla_tgt *tgt,
	struct qla_tgt_sess_work_param *prm)
{
	struct atio_from_isp *a = &prm->tm_iocb2;
	struct scsi_qla_host *vha = tgt->vha;
	struct qla_hw_data *ha = vha->hw;
6225
	struct fc_port *sess;
6226
	unsigned long flags;
6227
	be_id_t s_id;
6228
	int rc;
6229
	u64 unpacked_lun;
6230
	int fn;
6231 6232
	void *iocb;

6233
	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6234 6235

	if (tgt->tgt_stop)
6236
		goto out_term2;
6237 6238 6239 6240

	s_id = prm->tm_iocb2.u.isp24.fcp_hdr.s_id;
	sess = ha->tgt.tgt_ops->find_sess_by_s_id(vha, s_id);
	if (!sess) {
6241
		spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6242 6243 6244 6245

		sess = qlt_make_local_sess(vha, s_id);
		/* sess has got an extra creation ref */

6246
		spin_lock_irqsave(&ha->tgt.sess_lock, flags);
6247
		if (!sess)
6248
			goto out_term2;
6249
	} else {
6250
		if (sess->deleted) {
6251
			goto out_term2;
6252 6253
		}

6254
		if (!kref_get_unless_zero(&sess->sess_kref)) {
6255
			ql_dbg(ql_dbg_tgt_tmr, vha, 0xf020,
6256 6257
			    "%s: kref_get fail %8phC\n",
			     __func__, sess->port_name);
6258
			goto out_term2;
6259
		}
6260 6261 6262 6263
	}

	iocb = a;
	fn = a->u.isp24.fcp_cmnd.task_mgmt_flags;
6264 6265
	unpacked_lun =
	    scsilun_to_int((struct scsi_lun *)&a->u.isp24.fcp_cmnd.lun);
6266 6267

	rc = qlt_issue_task_mgmt(sess, unpacked_lun, fn, iocb, 0);
6268
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6269

6270 6271
	ha->tgt.tgt_ops->put_sess(sess);

6272 6273
	if (rc != 0)
		goto out_term;
6274 6275
	return;

6276 6277
out_term2:
	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
6278
out_term:
6279
	qlt_send_term_exchange(ha->base_qpair, NULL, &prm->tm_iocb2, 1, 0);
6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326
}

static void qlt_sess_work_fn(struct work_struct *work)
{
	struct qla_tgt *tgt = container_of(work, struct qla_tgt, sess_work);
	struct scsi_qla_host *vha = tgt->vha;
	unsigned long flags;

	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf000, "Sess work (tgt %p)", tgt);

	spin_lock_irqsave(&tgt->sess_work_lock, flags);
	while (!list_empty(&tgt->sess_works_list)) {
		struct qla_tgt_sess_work_param *prm = list_entry(
		    tgt->sess_works_list.next, typeof(*prm),
		    sess_works_list_entry);

		/*
		 * This work can be scheduled on several CPUs at time, so we
		 * must delete the entry to eliminate double processing
		 */
		list_del(&prm->sess_works_list_entry);

		spin_unlock_irqrestore(&tgt->sess_work_lock, flags);

		switch (prm->type) {
		case QLA_TGT_SESS_WORK_ABORT:
			qlt_abort_work(tgt, prm);
			break;
		case QLA_TGT_SESS_WORK_TM:
			qlt_tmr_work(tgt, prm);
			break;
		default:
			BUG_ON(1);
			break;
		}

		spin_lock_irqsave(&tgt->sess_work_lock, flags);

		kfree(prm);
	}
	spin_unlock_irqrestore(&tgt->sess_work_lock, flags);
}

/* Must be called under tgt_host_action_mutex */
int qlt_add_target(struct qla_hw_data *ha, struct scsi_qla_host *base_vha)
{
	struct qla_tgt *tgt;
6327 6328
	int rc, i;
	struct qla_qpair_hint *h;
6329 6330 6331 6332

	if (!QLA_TGT_MODE_ENABLED())
		return 0;

6333 6334 6335 6336 6337 6338
	if (!IS_TGT_MODE_CAPABLE(ha)) {
		ql_log(ql_log_warn, base_vha, 0xe070,
		    "This adapter does not support target mode.\n");
		return 0;
	}

6339
	ql_dbg(ql_dbg_tgt, base_vha, 0xe03b,
6340
	    "Registering target for host %ld(%p).\n", base_vha->host_no, ha);
6341

6342
	BUG_ON(base_vha->vha_tgt.qla_tgt != NULL);
6343 6344 6345 6346 6347 6348 6349 6350

	tgt = kzalloc(sizeof(struct qla_tgt), GFP_KERNEL);
	if (!tgt) {
		ql_dbg(ql_dbg_tgt, base_vha, 0xe066,
		    "Unable to allocate struct qla_tgt\n");
		return -ENOMEM;
	}

K
Kees Cook 已提交
6351 6352 6353
	tgt->qphints = kcalloc(ha->max_qpairs + 1,
			       sizeof(struct qla_qpair_hint),
			       GFP_KERNEL);
6354 6355 6356 6357 6358 6359 6360
	if (!tgt->qphints) {
		kfree(tgt);
		ql_log(ql_log_warn, base_vha, 0x0197,
		    "Unable to allocate qpair hints.\n");
		return -ENOMEM;
	}

6361 6362 6363
	if (!(base_vha->host->hostt->supported_mode & MODE_TARGET))
		base_vha->host->hostt->supported_mode |= MODE_TARGET;

6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381
	rc = btree_init64(&tgt->lun_qpair_map);
	if (rc) {
		kfree(tgt->qphints);
		kfree(tgt);
		ql_log(ql_log_info, base_vha, 0x0198,
			"Unable to initialize lun_qpair_map btree\n");
		return -EIO;
	}
	h = &tgt->qphints[0];
	h->qpair = ha->base_qpair;
	INIT_LIST_HEAD(&h->hint_elem);
	h->cpuid = ha->base_qpair->cpuid;
	list_add_tail(&h->hint_elem, &ha->base_qpair->hints_list);

	for (i = 0; i < ha->max_qpairs; i++) {
		unsigned long flags;

		struct qla_qpair *qpair = ha->queue_pair_map[i];
6382

6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393
		h = &tgt->qphints[i + 1];
		INIT_LIST_HEAD(&h->hint_elem);
		if (qpair) {
			h->qpair = qpair;
			spin_lock_irqsave(qpair->qp_lock_ptr, flags);
			list_add_tail(&h->hint_elem, &qpair->hints_list);
			spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
			h->cpuid = qpair->cpuid;
		}
	}

6394 6395 6396 6397 6398 6399 6400 6401 6402
	tgt->ha = ha;
	tgt->vha = base_vha;
	init_waitqueue_head(&tgt->waitQ);
	INIT_LIST_HEAD(&tgt->del_sess_list);
	spin_lock_init(&tgt->sess_work_lock);
	INIT_WORK(&tgt->sess_work, qlt_sess_work_fn);
	INIT_LIST_HEAD(&tgt->sess_works_list);
	atomic_set(&tgt->tgt_global_resets_count, 0);

6403
	base_vha->vha_tgt.qla_tgt = tgt;
6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414

	ql_dbg(ql_dbg_tgt, base_vha, 0xe067,
		"qla_target(%d): using 64 Bit PCI addressing",
		base_vha->vp_idx);
	/* 3 is reserved */
	tgt->sg_tablesize = QLA_TGT_MAX_SG_24XX(base_vha->req->length - 3);

	mutex_lock(&qla_tgt_mutex);
	list_add_tail(&tgt->tgt_list_entry, &qla_tgt_glist);
	mutex_unlock(&qla_tgt_mutex);

6415 6416 6417
	if (ha->tgt.tgt_ops && ha->tgt.tgt_ops->add_target)
		ha->tgt.tgt_ops->add_target(base_vha);

6418 6419 6420 6421 6422 6423
	return 0;
}

/* Must be called under tgt_host_action_mutex */
int qlt_remove_target(struct qla_hw_data *ha, struct scsi_qla_host *vha)
{
6424
	if (!vha->vha_tgt.qla_tgt)
6425 6426
		return 0;

6427 6428 6429 6430
	if (vha->fc_vport) {
		qlt_release(vha->vha_tgt.qla_tgt);
		return 0;
	}
6431 6432 6433 6434

	/* free left over qfull cmds */
	qlt_init_term_exchange(vha);

6435 6436
	ql_dbg(ql_dbg_tgt, vha, 0xe03c, "Unregistering target for host %ld(%p)",
	    vha->host_no, ha);
6437
	qlt_release(vha->vha_tgt.qla_tgt);
6438 6439 6440 6441

	return 0;
}

6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452
void qlt_remove_target_resources(struct qla_hw_data *ha)
{
	struct scsi_qla_host *node;
	u32 key = 0;

	btree_for_each_safe32(&ha->tgt.host_map, key, node)
		btree_remove32(&ha->tgt.host_map, key);

	btree_destroy32(&ha->tgt.host_map);
}

6453 6454 6455
static void qlt_lport_dump(struct scsi_qla_host *vha, u64 wwpn,
	unsigned char *b)
{
6456 6457
	pr_debug("qla2xxx HW vha->node_name: %8phC\n", vha->node_name);
	pr_debug("qla2xxx HW vha->port_name: %8phC\n", vha->port_name);
6458
	put_unaligned_be64(wwpn, b);
6459
	pr_debug("qla2xxx passed configfs WWPN: %8phC\n", b);
6460 6461 6462 6463 6464 6465
}

/**
 * qla_tgt_lport_register - register lport with external module
 *
 * @target_lport_ptr: pointer for tcm_qla2xxx specific lport data
6466 6467 6468
 * @phys_wwpn: physical port WWPN
 * @npiv_wwpn: NPIV WWPN
 * @npiv_wwnn: NPIV WWNN
6469
 * @callback:  lport initialization callback for tcm_qla2xxx code
6470
 */
6471 6472 6473
int qlt_lport_register(void *target_lport_ptr, u64 phys_wwpn,
		       u64 npiv_wwpn, u64 npiv_wwnn,
		       int (*callback)(struct scsi_qla_host *, void *, u64, u64))
6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494
{
	struct qla_tgt *tgt;
	struct scsi_qla_host *vha;
	struct qla_hw_data *ha;
	struct Scsi_Host *host;
	unsigned long flags;
	int rc;
	u8 b[WWN_SIZE];

	mutex_lock(&qla_tgt_mutex);
	list_for_each_entry(tgt, &qla_tgt_glist, tgt_list_entry) {
		vha = tgt->vha;
		ha = vha->hw;

		host = vha->host;
		if (!host)
			continue;

		if (!(host->hostt->supported_mode & MODE_TARGET))
			continue;

6495 6496 6497
		if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
			continue;

6498
		spin_lock_irqsave(&ha->hardware_lock, flags);
6499
		if ((!npiv_wwpn || !npiv_wwnn) && host->active_mode & MODE_TARGET) {
6500 6501 6502 6503 6504
			pr_debug("MODE_TARGET already active on qla2xxx(%d)\n",
			    host->host_no);
			spin_unlock_irqrestore(&ha->hardware_lock, flags);
			continue;
		}
6505 6506 6507 6508 6509 6510
		if (tgt->tgt_stop) {
			pr_debug("MODE_TARGET in shutdown on qla2xxx(%d)\n",
				 host->host_no);
			spin_unlock_irqrestore(&ha->hardware_lock, flags);
			continue;
		}
6511 6512 6513 6514 6515 6516 6517 6518
		spin_unlock_irqrestore(&ha->hardware_lock, flags);

		if (!scsi_host_get(host)) {
			ql_dbg(ql_dbg_tgt, vha, 0xe068,
			    "Unable to scsi_host_get() for"
			    " qla2xxx scsi_host\n");
			continue;
		}
6519
		qlt_lport_dump(vha, phys_wwpn, b);
6520 6521 6522 6523 6524

		if (memcmp(vha->port_name, b, WWN_SIZE)) {
			scsi_host_put(host);
			continue;
		}
6525 6526 6527 6528
		rc = (*callback)(vha, target_lport_ptr, npiv_wwpn, npiv_wwnn);
		if (rc != 0)
			scsi_host_put(host);

6529
		mutex_unlock(&qla_tgt_mutex);
6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549
		return rc;
	}
	mutex_unlock(&qla_tgt_mutex);

	return -ENODEV;
}
EXPORT_SYMBOL(qlt_lport_register);

/**
 * qla_tgt_lport_deregister - Degister lport
 *
 * @vha:  Registered scsi_qla_host pointer
 */
void qlt_lport_deregister(struct scsi_qla_host *vha)
{
	struct qla_hw_data *ha = vha->hw;
	struct Scsi_Host *sh = vha->host;
	/*
	 * Clear the target_lport_ptr qla_target_template pointer in qla_hw_data
	 */
6550
	vha->vha_tgt.target_lport_ptr = NULL;
6551 6552 6553 6554 6555 6556 6557 6558 6559
	ha->tgt.tgt_ops = NULL;
	/*
	 * Release the Scsi_Host reference for the underlying qla2xxx host
	 */
	scsi_host_put(sh);
}
EXPORT_SYMBOL(qlt_lport_deregister);

/* Must be called under HW lock */
6560
void qlt_set_mode(struct scsi_qla_host *vha)
6561
{
6562
	switch (vha->qlini_mode) {
6563 6564 6565 6566 6567
	case QLA2XXX_INI_MODE_DISABLED:
	case QLA2XXX_INI_MODE_EXCLUSIVE:
		vha->host->active_mode = MODE_TARGET;
		break;
	case QLA2XXX_INI_MODE_ENABLED:
6568
		vha->host->active_mode = MODE_INITIATOR;
6569 6570 6571
		break;
	case QLA2XXX_INI_MODE_DUAL:
		vha->host->active_mode = MODE_DUAL;
6572 6573 6574 6575 6576 6577 6578
		break;
	default:
		break;
	}
}

/* Must be called under HW lock */
6579
static void qlt_clear_mode(struct scsi_qla_host *vha)
6580
{
6581
	switch (vha->qlini_mode) {
6582 6583 6584 6585 6586 6587 6588
	case QLA2XXX_INI_MODE_DISABLED:
		vha->host->active_mode = MODE_UNKNOWN;
		break;
	case QLA2XXX_INI_MODE_EXCLUSIVE:
		vha->host->active_mode = MODE_INITIATOR;
		break;
	case QLA2XXX_INI_MODE_ENABLED:
6589 6590
	case QLA2XXX_INI_MODE_DUAL:
		vha->host->active_mode = MODE_INITIATOR;
6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605
		break;
	default:
		break;
	}
}

/*
 * qla_tgt_enable_vha - NO LOCK HELD
 *
 * host_reset, bring up w/ Target Mode Enabled
 */
void
qlt_enable_vha(struct scsi_qla_host *vha)
{
	struct qla_hw_data *ha = vha->hw;
6606
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6607
	unsigned long flags;
6608
	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
6609 6610 6611 6612 6613 6614 6615 6616

	if (!tgt) {
		ql_dbg(ql_dbg_tgt, vha, 0xe069,
		    "Unable to locate qla_tgt pointer from"
		    " struct qla_hw_data\n");
		dump_stack();
		return;
	}
6617 6618
	if (vha->qlini_mode == QLA2XXX_INI_MODE_ENABLED)
		return;
6619

6620 6621
	if (ha->tgt.num_act_qpairs > ha->max_qpairs)
		ha->tgt.num_act_qpairs = ha->max_qpairs;
6622 6623 6624 6625 6626
	spin_lock_irqsave(&ha->hardware_lock, flags);
	tgt->tgt_stopped = 0;
	qlt_set_mode(vha);
	spin_unlock_irqrestore(&ha->hardware_lock, flags);

6627 6628 6629
	mutex_lock(&ha->optrom_mutex);
	ql_dbg(ql_dbg_tgt_mgt, vha, 0xf021,
	    "%s.\n", __func__);
6630 6631 6632 6633 6634 6635
	if (vha->vp_idx) {
		qla24xx_disable_vp(vha);
		qla24xx_enable_vp(vha);
	} else {
		set_bit(ISP_ABORT_NEEDED, &base_vha->dpc_flags);
		qla2xxx_wake_dpc(base_vha);
6636 6637
		WARN_ON_ONCE(qla2x00_wait_for_hba_online(base_vha) !=
			     QLA_SUCCESS);
6638
	}
6639
	mutex_unlock(&ha->optrom_mutex);
6640 6641 6642 6643 6644 6645 6646 6647
}
EXPORT_SYMBOL(qlt_enable_vha);

/*
 * qla_tgt_disable_vha - NO LOCK HELD
 *
 * Disable Target Mode and reset the adapter
 */
6648
static void qlt_disable_vha(struct scsi_qla_host *vha)
6649 6650
{
	struct qla_hw_data *ha = vha->hw;
6651
	struct qla_tgt *tgt = vha->vha_tgt.qla_tgt;
6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667
	unsigned long flags;

	if (!tgt) {
		ql_dbg(ql_dbg_tgt, vha, 0xe06a,
		    "Unable to locate qla_tgt pointer from"
		    " struct qla_hw_data\n");
		dump_stack();
		return;
	}

	spin_lock_irqsave(&ha->hardware_lock, flags);
	qlt_clear_mode(vha);
	spin_unlock_irqrestore(&ha->hardware_lock, flags);

	set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
	qla2xxx_wake_dpc(vha);
6668 6669 6670
	if (qla2x00_wait_for_hba_online(vha) != QLA_SUCCESS)
		ql_dbg(ql_dbg_tgt, vha, 0xe081,
		       "qla2x00_wait_for_hba_online() failed\n");
6671 6672 6673 6674 6675 6676 6677 6678 6679 6680
}

/*
 * Called from qla_init.c:qla24xx_vport_create() contex to setup
 * the target mode specific struct scsi_qla_host and struct qla_hw_data
 * members.
 */
void
qlt_vport_create(struct scsi_qla_host *vha, struct qla_hw_data *ha)
{
6681 6682 6683 6684
	vha->vha_tgt.qla_tgt = NULL;

	mutex_init(&vha->vha_tgt.tgt_mutex);
	mutex_init(&vha->vha_tgt.tgt_host_action_mutex);
6685 6686 6687 6688 6689 6690 6691 6692 6693 6694

	qlt_clear_mode(vha);

	/*
	 * NOTE: Currently the value is kept the same for <24xx and
	 * >=24xx ISPs. If it is necessary to change it,
	 * the check should be added for specific ISPs,
	 * assigning the value appropriately.
	 */
	ha->tgt.atio_q_length = ATIO_ENTRY_CNT_24XX;
6695 6696

	qlt_add_target(ha, vha);
6697 6698
}

6699 6700
u8
qlt_rff_id(struct scsi_qla_host *vha)
6701
{
6702
	u8 fc4_feature = 0;
6703 6704 6705 6706
	/*
	 * FC-4 Feature bit 0 indicates target functionality to the name server.
	 */
	if (qla_tgt_mode_enabled(vha)) {
6707
		fc4_feature = BIT_0;
6708
	} else if (qla_ini_mode_enabled(vha)) {
6709
		fc4_feature = BIT_1;
6710
	} else if (qla_dual_mode_enabled(vha))
6711 6712 6713
		fc4_feature = BIT_0 | BIT_1;

	return fc4_feature;
6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731
}

/*
 * qlt_init_atio_q_entries() - Initializes ATIO queue entries.
 * @ha: HA context
 *
 * Beginning of ATIO ring has initialization control block already built
 * by nvram config routine.
 *
 * Returns 0 on success.
 */
void
qlt_init_atio_q_entries(struct scsi_qla_host *vha)
{
	struct qla_hw_data *ha = vha->hw;
	uint16_t cnt;
	struct atio_from_isp *pkt = (struct atio_from_isp *)ha->tgt.atio_ring;

6732
	if (qla_ini_mode_enabled(vha))
6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746
		return;

	for (cnt = 0; cnt < ha->tgt.atio_q_length; cnt++) {
		pkt->u.raw.signature = ATIO_PROCESSED;
		pkt++;
	}

}

/*
 * qlt_24xx_process_atio_queue() - Process ATIO queue entries.
 * @ha: SCSI driver HA context
 */
void
6747
qlt_24xx_process_atio_queue(struct scsi_qla_host *vha, uint8_t ha_locked)
6748 6749 6750 6751 6752
{
	struct qla_hw_data *ha = vha->hw;
	struct atio_from_isp *pkt;
	int cnt, i;

6753
	if (!ha->flags.fw_started)
6754 6755
		return;

6756 6757
	while ((ha->tgt.atio_ring_ptr->signature != ATIO_PROCESSED) ||
	    fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr)) {
6758 6759 6760
		pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
		cnt = pkt->u.raw.entry_count;

6761 6762 6763 6764 6765 6766
		if (unlikely(fcpcmd_is_corrupted(ha->tgt.atio_ring_ptr))) {
			/*
			 * This packet is corrupted. The header + payload
			 * can not be trusted. There is no point in passing
			 * it further up.
			 */
6767
			ql_log(ql_log_warn, vha, 0xd03c,
6768
			    "corrupted fcp frame SID[%3phN] OXID[%04x] EXCG[%x] %64phN\n",
6769
			    &pkt->u.isp24.fcp_hdr.s_id,
6770 6771 6772 6773
			    be16_to_cpu(pkt->u.isp24.fcp_hdr.ox_id),
			    le32_to_cpu(pkt->u.isp24.exchange_addr), pkt);

			adjust_corrupted_atio(pkt);
6774 6775
			qlt_send_term_exchange(ha->base_qpair, NULL, pkt,
			    ha_locked, 0);
6776 6777 6778 6779
		} else {
			qlt_24xx_atio_pkt_all_vps(vha,
			    (struct atio_from_isp *)pkt, ha_locked);
		}
6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795

		for (i = 0; i < cnt; i++) {
			ha->tgt.atio_ring_index++;
			if (ha->tgt.atio_ring_index == ha->tgt.atio_q_length) {
				ha->tgt.atio_ring_index = 0;
				ha->tgt.atio_ring_ptr = ha->tgt.atio_ring;
			} else
				ha->tgt.atio_ring_ptr++;

			pkt->u.raw.signature = ATIO_PROCESSED;
			pkt = (struct atio_from_isp *)ha->tgt.atio_ring_ptr;
		}
		wmb();
	}

	/* Adjust ring index */
6796
	WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), ha->tgt.atio_ring_index);
6797 6798 6799
}

void
6800
qlt_24xx_config_rings(struct scsi_qla_host *vha)
6801 6802
{
	struct qla_hw_data *ha = vha->hw;
6803 6804 6805
	struct qla_msix_entry *msix = &ha->msix_entries[2];
	struct init_cb_24xx *icb = (struct init_cb_24xx *)ha->init_cb;

6806 6807
	if (!QLA_TGT_MODE_ENABLED())
		return;
6808

6809 6810 6811 6812
	WRT_REG_DWORD(ISP_ATIO_Q_IN(vha), 0);
	WRT_REG_DWORD(ISP_ATIO_Q_OUT(vha), 0);
	RD_REG_DWORD(ISP_ATIO_Q_OUT(vha));

6813
	if (ha->flags.msix_enabled) {
6814
		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828
			if (IS_QLA2071(ha)) {
				/* 4 ports Baker: Enable Interrupt Handshake */
				icb->msix_atio = 0;
				icb->firmware_options_2 |= BIT_26;
			} else {
				icb->msix_atio = cpu_to_le16(msix->entry);
				icb->firmware_options_2 &= ~BIT_26;
			}
			ql_dbg(ql_dbg_init, vha, 0xf072,
			    "Registering ICB vector 0x%x for atio que.\n",
			    msix->entry);
		}
	} else {
		/* INTx|MSI */
6829
		if (IS_QLA83XX(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
6830 6831 6832 6833 6834
			icb->msix_atio = 0;
			icb->firmware_options_2 |= BIT_26;
			ql_dbg(ql_dbg_init, vha, 0xf072,
			    "%s: Use INTx for ATIOQ.\n", __func__);
		}
6835 6836 6837 6838 6839 6840 6841
	}
}

void
qlt_24xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_24xx *nv)
{
	struct qla_hw_data *ha = vha->hw;
6842
	u32 tmp;
6843 6844 6845

	if (!QLA_TGT_MODE_ENABLED())
		return;
6846

6847
	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859
		if (!ha->tgt.saved_set) {
			/* We save only once */
			ha->tgt.saved_exchange_count = nv->exchange_count;
			ha->tgt.saved_firmware_options_1 =
			    nv->firmware_options_1;
			ha->tgt.saved_firmware_options_2 =
			    nv->firmware_options_2;
			ha->tgt.saved_firmware_options_3 =
			    nv->firmware_options_3;
			ha->tgt.saved_set = 1;
		}

6860
		if (qla_tgt_mode_enabled(vha))
6861
			nv->exchange_count = cpu_to_le16(0xFFFF);
6862
		else			/* dual */
6863
			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6864 6865

		/* Enable target mode */
6866
		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6867 6868

		/* Disable ini mode, if requested */
6869
		if (qla_tgt_mode_enabled(vha))
6870
			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6871 6872

		/* Disable Full Login after LIP */
6873
		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6874
		/* Enable initial LIP */
6875
		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6876 6877 6878 6879 6880 6881 6882
		if (ql2xtgt_tape_enable)
			/* Enable FC Tape support */
			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
		else
			/* Disable FC Tape support */
			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);

6883
		/* Disable Full Login after LIP */
6884
		nv->host_p &= cpu_to_le32(~BIT_10);
6885 6886 6887 6888 6889 6890 6891

		/*
		 * clear BIT 15 explicitly as we have seen at least
		 * a couple of instances where this was set and this
		 * was causing the firmware to not be initialized.
		 */
		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6892
		/* Enable target PRLI control */
6893
		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6894 6895 6896 6897 6898 6899 6900 6901

		if (IS_QLA25XX(ha)) {
			/* Change Loop-prefer to Pt-Pt */
			tmp = ~(BIT_4|BIT_5|BIT_6);
			nv->firmware_options_2 &= cpu_to_le32(tmp);
			tmp = P2P << 4;
			nv->firmware_options_2 |= cpu_to_le32(tmp);
		}
6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914
	} else {
		if (ha->tgt.saved_set) {
			nv->exchange_count = ha->tgt.saved_exchange_count;
			nv->firmware_options_1 =
			    ha->tgt.saved_firmware_options_1;
			nv->firmware_options_2 =
			    ha->tgt.saved_firmware_options_2;
			nv->firmware_options_3 =
			    ha->tgt.saved_firmware_options_3;
		}
		return;
	}

6915
	if (ha->base_qpair->enable_class_2) {
6916 6917 6918 6919
		if (vha->flags.init_done)
			fc_host_supported_classes(vha->host) =
				FC_COS_CLASS2 | FC_COS_CLASS3;

6920
		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
6921 6922 6923 6924
	} else {
		if (vha->flags.init_done)
			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;

6925
		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
6926 6927 6928 6929 6930 6931 6932 6933 6934
	}
}

void
qlt_24xx_config_nvram_stage2(struct scsi_qla_host *vha,
	struct init_cb_24xx *icb)
{
	struct qla_hw_data *ha = vha->hw;

Q
Quinn Tran 已提交
6935 6936 6937
	if (!QLA_TGT_MODE_ENABLED())
		return;

6938 6939
	if (ha->tgt.node_name_set) {
		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
6940
		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
6941 6942 6943
	}
}

6944 6945 6946 6947
void
qlt_81xx_config_nvram_stage1(struct scsi_qla_host *vha, struct nvram_81xx *nv)
{
	struct qla_hw_data *ha = vha->hw;
6948
	u32 tmp;
6949 6950 6951 6952

	if (!QLA_TGT_MODE_ENABLED())
		return;

6953
	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha)) {
6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965
		if (!ha->tgt.saved_set) {
			/* We save only once */
			ha->tgt.saved_exchange_count = nv->exchange_count;
			ha->tgt.saved_firmware_options_1 =
			    nv->firmware_options_1;
			ha->tgt.saved_firmware_options_2 =
			    nv->firmware_options_2;
			ha->tgt.saved_firmware_options_3 =
			    nv->firmware_options_3;
			ha->tgt.saved_set = 1;
		}

6966
		if (qla_tgt_mode_enabled(vha))
6967
			nv->exchange_count = cpu_to_le16(0xFFFF);
6968
		else			/* dual */
6969
			nv->exchange_count = cpu_to_le16(vha->ql2xexchoffld);
6970 6971

		/* Enable target mode */
6972
		nv->firmware_options_1 |= cpu_to_le32(BIT_4);
6973 6974

		/* Disable ini mode, if requested */
6975
		if (qla_tgt_mode_enabled(vha))
6976
			nv->firmware_options_1 |= cpu_to_le32(BIT_5);
6977
		/* Disable Full Login after LIP */
6978
		nv->firmware_options_1 &= cpu_to_le32(~BIT_13);
6979
		/* Enable initial LIP */
6980
		nv->firmware_options_1 &= cpu_to_le32(~BIT_9);
6981 6982 6983 6984 6985 6986 6987
		/*
		 * clear BIT 15 explicitly as we have seen at
		 * least a couple of instances where this was set
		 * and this was causing the firmware to not be
		 * initialized.
		 */
		nv->firmware_options_1 &= cpu_to_le32(~BIT_15);
6988 6989 6990 6991 6992 6993 6994
		if (ql2xtgt_tape_enable)
			/* Enable FC tape support */
			nv->firmware_options_2 |= cpu_to_le32(BIT_12);
		else
			/* Disable FC tape support */
			nv->firmware_options_2 &= cpu_to_le32(~BIT_12);

6995
		/* Disable Full Login after LIP */
6996
		nv->host_p &= cpu_to_le32(~BIT_10);
6997
		/* Enable target PRLI control */
6998
		nv->firmware_options_2 |= cpu_to_le32(BIT_14);
6999 7000 7001 7002 7003 7004

		/* Change Loop-prefer to Pt-Pt */
		tmp = ~(BIT_4|BIT_5|BIT_6);
		nv->firmware_options_2 &= cpu_to_le32(tmp);
		tmp = P2P << 4;
		nv->firmware_options_2 |= cpu_to_le32(tmp);
7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017
	} else {
		if (ha->tgt.saved_set) {
			nv->exchange_count = ha->tgt.saved_exchange_count;
			nv->firmware_options_1 =
			    ha->tgt.saved_firmware_options_1;
			nv->firmware_options_2 =
			    ha->tgt.saved_firmware_options_2;
			nv->firmware_options_3 =
			    ha->tgt.saved_firmware_options_3;
		}
		return;
	}

7018
	if (ha->base_qpair->enable_class_2) {
7019 7020 7021 7022
		if (vha->flags.init_done)
			fc_host_supported_classes(vha->host) =
				FC_COS_CLASS2 | FC_COS_CLASS3;

7023
		nv->firmware_options_2 |= cpu_to_le32(BIT_8);
7024 7025 7026 7027
	} else {
		if (vha->flags.init_done)
			fc_host_supported_classes(vha->host) = FC_COS_CLASS3;

7028
		nv->firmware_options_2 &= ~cpu_to_le32(BIT_8);
7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042
	}
}

void
qlt_81xx_config_nvram_stage2(struct scsi_qla_host *vha,
	struct init_cb_81xx *icb)
{
	struct qla_hw_data *ha = vha->hw;

	if (!QLA_TGT_MODE_ENABLED())
		return;

	if (ha->tgt.node_name_set) {
		memcpy(icb->node_name, ha->tgt.tgt_node_name, WWN_SIZE);
7043
		icb->firmware_options_1 |= cpu_to_le32(BIT_14);
7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055
	}
}

void
qlt_83xx_iospace_config(struct qla_hw_data *ha)
{
	if (!QLA_TGT_MODE_ENABLED())
		return;

	ha->msix_count += 1; /* For ATIO Q */
}

7056 7057 7058 7059 7060

void
qlt_modify_vp_config(struct scsi_qla_host *vha,
	struct vp_config_entry_24xx *vpmod)
{
7061 7062
	/* enable target mode.  Bit5 = 1 => disable */
	if (qla_tgt_mode_enabled(vha) || qla_dual_mode_enabled(vha))
7063
		vpmod->options_idx1 &= ~BIT_5;
7064

7065
	/* Disable ini mode, if requested.  bit4 = 1 => disable */
7066
	if (qla_tgt_mode_enabled(vha))
7067 7068 7069 7070 7071 7072
		vpmod->options_idx1 &= ~BIT_4;
}

void
qlt_probe_one_stage1(struct scsi_qla_host *base_vha, struct qla_hw_data *ha)
{
7073 7074
	int rc;

7075 7076 7077
	if (!QLA_TGT_MODE_ENABLED())
		return;

7078 7079
	if  ((ql2xenablemsix == 0) || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
	    IS_QLA28XX(ha)) {
7080 7081 7082 7083 7084 7085 7086
		ISP_ATIO_Q_IN(base_vha) = &ha->mqiobase->isp25mq.atio_q_in;
		ISP_ATIO_Q_OUT(base_vha) = &ha->mqiobase->isp25mq.atio_q_out;
	} else {
		ISP_ATIO_Q_IN(base_vha) = &ha->iobase->isp24.atio_q_in;
		ISP_ATIO_Q_OUT(base_vha) = &ha->iobase->isp24.atio_q_out;
	}

7087 7088
	mutex_init(&base_vha->vha_tgt.tgt_mutex);
	mutex_init(&base_vha->vha_tgt.tgt_host_action_mutex);
7089 7090 7091 7092 7093

	INIT_LIST_HEAD(&base_vha->unknown_atio_list);
	INIT_DELAYED_WORK(&base_vha->unknown_atio_work,
	    qlt_unknown_atio_work_fn);

7094
	qlt_clear_mode(base_vha);
7095 7096 7097

	rc = btree_init32(&ha->tgt.host_map);
	if (rc)
7098
		ql_log(ql_log_info, base_vha, 0xd03d,
7099 7100 7101
		    "Unable to initialize ha->host_map btree\n");

	qlt_update_vp_map(base_vha, SET_VP_IDX);
7102 7103
}

7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115
irqreturn_t
qla83xx_msix_atio_q(int irq, void *dev_id)
{
	struct rsp_que *rsp;
	scsi_qla_host_t	*vha;
	struct qla_hw_data *ha;
	unsigned long flags;

	rsp = (struct rsp_que *) dev_id;
	ha = rsp->hw;
	vha = pci_get_drvdata(ha->pdev);

7116
	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
7117

7118
	qlt_24xx_process_atio_queue(vha, 0);
7119

7120
	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);
7121 7122 7123 7124

	return IRQ_HANDLED;
}

7125 7126 7127 7128 7129 7130 7131 7132 7133
static void
qlt_handle_abts_recv_work(struct work_struct *work)
{
	struct qla_tgt_sess_op *op = container_of(work,
		struct qla_tgt_sess_op, work);
	scsi_qla_host_t *vha = op->vha;
	struct qla_hw_data *ha = vha->hw;
	unsigned long flags;

7134 7135
	if (qla2x00_reset_active(vha) ||
	    (op->chip_reset != ha->base_qpair->chip_reset))
7136 7137 7138 7139 7140 7141 7142
		return;

	spin_lock_irqsave(&ha->tgt.atio_lock, flags);
	qlt_24xx_process_atio_queue(vha, 0);
	spin_unlock_irqrestore(&ha->tgt.atio_lock, flags);

	spin_lock_irqsave(&ha->hardware_lock, flags);
7143
	qlt_response_pkt_all_vps(vha, op->rsp, (response_t *)&op->atio);
7144
	spin_unlock_irqrestore(&ha->hardware_lock, flags);
7145 7146

	kfree(op);
7147 7148 7149
}

void
7150 7151
qlt_handle_abts_recv(struct scsi_qla_host *vha, struct rsp_que *rsp,
    response_t *pkt)
7152 7153 7154 7155 7156 7157 7158 7159 7160
{
	struct qla_tgt_sess_op *op;

	op = kzalloc(sizeof(*op), GFP_ATOMIC);

	if (!op) {
		/* do not reach for ATIO queue here.  This is best effort err
		 * recovery at this point.
		 */
7161
		qlt_response_pkt_all_vps(vha, rsp, pkt);
7162 7163 7164 7165 7166
		return;
	}

	memcpy(&op->atio, pkt, sizeof(*pkt));
	op->vha = vha;
7167
	op->chip_reset = vha->hw->base_qpair->chip_reset;
7168
	op->rsp = rsp;
7169 7170 7171 7172 7173
	INIT_WORK(&op->work, qlt_handle_abts_recv_work);
	queue_work(qla_tgt_wq, &op->work);
	return;
}

7174 7175 7176 7177 7178 7179
int
qlt_mem_alloc(struct qla_hw_data *ha)
{
	if (!QLA_TGT_MODE_ENABLED())
		return 0;

K
Kees Cook 已提交
7180 7181 7182
	ha->tgt.tgt_vp_map = kcalloc(MAX_MULTI_ID_FABRIC,
				     sizeof(struct qla_tgt_vp_map),
				     GFP_KERNEL);
7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206
	if (!ha->tgt.tgt_vp_map)
		return -ENOMEM;

	ha->tgt.atio_ring = dma_alloc_coherent(&ha->pdev->dev,
	    (ha->tgt.atio_q_length + 1) * sizeof(struct atio_from_isp),
	    &ha->tgt.atio_dma, GFP_KERNEL);
	if (!ha->tgt.atio_ring) {
		kfree(ha->tgt.tgt_vp_map);
		return -ENOMEM;
	}
	return 0;
}

void
qlt_mem_free(struct qla_hw_data *ha)
{
	if (!QLA_TGT_MODE_ENABLED())
		return;

	if (ha->tgt.atio_ring) {
		dma_free_coherent(&ha->pdev->dev, (ha->tgt.atio_q_length + 1) *
		    sizeof(struct atio_from_isp), ha->tgt.atio_ring,
		    ha->tgt.atio_dma);
	}
7207 7208
	ha->tgt.atio_ring = NULL;
	ha->tgt.atio_dma = 0;
7209
	kfree(ha->tgt.tgt_vp_map);
7210
	ha->tgt.tgt_vp_map = NULL;
7211 7212 7213 7214 7215 7216
}

/* vport_slock to be held by the caller */
void
qlt_update_vp_map(struct scsi_qla_host *vha, int cmd)
{
7217 7218 7219 7220
	void *slot;
	u32 key;
	int rc;

7221 7222 7223
	if (!QLA_TGT_MODE_ENABLED())
		return;

7224 7225
	key = vha->d_id.b24;

7226 7227 7228 7229 7230
	switch (cmd) {
	case SET_VP_IDX:
		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = vha;
		break;
	case SET_AL_PA:
7231 7232
		slot = btree_lookup32(&vha->hw->tgt.host_map, key);
		if (!slot) {
7233
			ql_dbg(ql_dbg_tgt_mgt, vha, 0xf018,
7234 7235 7236 7237
			    "Save vha in host_map %p %06x\n", vha, key);
			rc = btree_insert32(&vha->hw->tgt.host_map,
				key, vha, GFP_ATOMIC);
			if (rc)
7238
				ql_log(ql_log_info, vha, 0xd03e,
7239 7240 7241 7242
				    "Unable to insert s_id into host_map: %06x\n",
				    key);
			return;
		}
7243 7244
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf019,
		    "replace existing vha in host_map %p %06x\n", vha, key);
7245
		btree_update32(&vha->hw->tgt.host_map, key, vha);
7246 7247 7248 7249 7250
		break;
	case RESET_VP_IDX:
		vha->hw->tgt.tgt_vp_map[vha->vp_idx].vha = NULL;
		break;
	case RESET_AL_PA:
7251
		ql_dbg(ql_dbg_tgt_mgt, vha, 0xf01a,
7252 7253 7254 7255 7256
		   "clear vha in host_map %p %06x\n", vha, key);
		slot = btree_lookup32(&vha->hw->tgt.host_map, key);
		if (slot)
			btree_remove32(&vha->hw->tgt.host_map, key);
		vha->d_id.b24 = 0;
7257 7258 7259 7260
		break;
	}
}

7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273
void qlt_update_host_map(struct scsi_qla_host *vha, port_id_t id)
{

	if (!vha->d_id.b24) {
		vha->d_id = id;
		qlt_update_vp_map(vha, SET_AL_PA);
	} else if (vha->d_id.b24 != id.b24) {
		qlt_update_vp_map(vha, RESET_AL_PA);
		vha->d_id = id;
		qlt_update_vp_map(vha, SET_AL_PA);
	}
}

7274 7275 7276 7277 7278 7279 7280 7281
static int __init qlt_parse_ini_mode(void)
{
	if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_EXCLUSIVE) == 0)
		ql2x_ini_mode = QLA2XXX_INI_MODE_EXCLUSIVE;
	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DISABLED) == 0)
		ql2x_ini_mode = QLA2XXX_INI_MODE_DISABLED;
	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_ENABLED) == 0)
		ql2x_ini_mode = QLA2XXX_INI_MODE_ENABLED;
7282 7283
	else if (strcasecmp(qlini_mode, QLA2XXX_INI_MODE_STR_DUAL) == 0)
		ql2x_ini_mode = QLA2XXX_INI_MODE_DUAL;
7284 7285 7286 7287 7288 7289 7290 7291 7292 7293
	else
		return false;

	return true;
}

int __init qlt_init(void)
{
	int ret;

7294 7295 7296
	BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
	BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);

7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309
	if (!qlt_parse_ini_mode()) {
		ql_log(ql_log_fatal, NULL, 0xe06b,
		    "qlt_parse_ini_mode() failed\n");
		return -EINVAL;
	}

	if (!QLA_TGT_MODE_ENABLED())
		return 0;

	qla_tgt_mgmt_cmd_cachep = kmem_cache_create("qla_tgt_mgmt_cmd_cachep",
	    sizeof(struct qla_tgt_mgmt_cmd), __alignof__(struct
	    qla_tgt_mgmt_cmd), 0, NULL);
	if (!qla_tgt_mgmt_cmd_cachep) {
7310
		ql_log(ql_log_fatal, NULL, 0xd04b,
7311
		    "kmem_cache_create for qla_tgt_mgmt_cmd_cachep failed\n");
7312
		return -ENOMEM;
7313 7314
	}

7315
	qla_tgt_plogi_cachep = kmem_cache_create("qla_tgt_plogi_cachep",
7316 7317
	    sizeof(struct qlt_plogi_ack_t), __alignof__(struct qlt_plogi_ack_t),
	    0, NULL);
7318 7319 7320 7321 7322 7323 7324 7325

	if (!qla_tgt_plogi_cachep) {
		ql_log(ql_log_fatal, NULL, 0xe06d,
		    "kmem_cache_create for qla_tgt_plogi_cachep failed\n");
		ret = -ENOMEM;
		goto out_mgmt_cmd_cachep;
	}

7326 7327 7328 7329 7330 7331
	qla_tgt_mgmt_cmd_mempool = mempool_create(25, mempool_alloc_slab,
	    mempool_free_slab, qla_tgt_mgmt_cmd_cachep);
	if (!qla_tgt_mgmt_cmd_mempool) {
		ql_log(ql_log_fatal, NULL, 0xe06e,
		    "mempool_create for qla_tgt_mgmt_cmd_mempool failed\n");
		ret = -ENOMEM;
7332
		goto out_plogi_cachep;
7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348
	}

	qla_tgt_wq = alloc_workqueue("qla_tgt_wq", 0, 0);
	if (!qla_tgt_wq) {
		ql_log(ql_log_fatal, NULL, 0xe06f,
		    "alloc_workqueue for qla_tgt_wq failed\n");
		ret = -ENOMEM;
		goto out_cmd_mempool;
	}
	/*
	 * Return 1 to signal that initiator-mode is being disabled
	 */
	return (ql2x_ini_mode == QLA2XXX_INI_MODE_DISABLED) ? 1 : 0;

out_cmd_mempool:
	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7349 7350
out_plogi_cachep:
	kmem_cache_destroy(qla_tgt_plogi_cachep);
7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362
out_mgmt_cmd_cachep:
	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
	return ret;
}

void qlt_exit(void)
{
	if (!QLA_TGT_MODE_ENABLED())
		return;

	destroy_workqueue(qla_tgt_wq);
	mempool_destroy(qla_tgt_mgmt_cmd_mempool);
7363
	kmem_cache_destroy(qla_tgt_plogi_cachep);
7364 7365
	kmem_cache_destroy(qla_tgt_mgmt_cmd_cachep);
}