tcm_qla2xxx.c 59.2 KB
Newer Older
1 2 3 4
/*******************************************************************************
 * This file contains tcm implementation using v4 configfs fabric infrastructure
 * for QLogic target mode HBAs
 *
5
 * (c) Copyright 2010-2013 Datera, Inc.
6
 *
7
 * Author: Nicholas A. Bellinger <nab@daterainc.com>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 *
 * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
 * the TCM_FC / Open-FCoE.org fabric module.
 *
 * Copyright (c) 2010 Cisco Systems, Inc
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 ****************************************************************************/


#include <linux/module.h>
#include <linux/moduleparam.h>
#include <generated/utsrelease.h>
#include <linux/utsname.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/kthread.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/configfs.h>
#include <linux/ctype.h>
#include <asm/unaligned.h>
#include <scsi/scsi.h>
#include <scsi/scsi_host.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_cmnd.h>
#include <target/target_core_base.h>
#include <target/target_core_fabric.h>
#include <target/target_core_fabric_configfs.h>
#include <target/target_core_configfs.h>
#include <target/configfs_macros.h>

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

53 54 55
static struct workqueue_struct *tcm_qla2xxx_free_wq;
static struct workqueue_struct *tcm_qla2xxx_cmd_wq;

56 57
static const struct target_core_fabric_ops tcm_qla2xxx_ops;
static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops;
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

/*
 * Parse WWN.
 * If strict, we require lower-case hex and colon separators to be sure
 * the name is the same as what would be generated by ft_format_wwn()
 * so the name and wwn are mapped one-to-one.
 */
static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
{
	const char *cp;
	char c;
	u32 nibble;
	u32 byte = 0;
	u32 pos = 0;
	u32 err;

	*wwn = 0;
	for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
		c = *cp;
		if (c == '\n' && cp[1] == '\0')
			continue;
		if (strict && pos++ == 2 && byte++ < 7) {
			pos = 0;
			if (c == ':')
				continue;
			err = 1;
			goto fail;
		}
		if (c == '\0') {
			err = 2;
			if (strict && byte != 8)
				goto fail;
			return cp - name;
		}
		err = 3;
		if (isdigit(c))
			nibble = c - '0';
		else if (isxdigit(c) && (islower(c) || !strict))
			nibble = tolower(c) - 'a' + 10;
		else
			goto fail;
		*wwn = (*wwn << 4) | nibble;
	}
	err = 4;
fail:
	pr_debug("err %u len %zu pos %u byte %u\n",
			err, cp - name, pos, byte);
	return -1;
}

static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
{
	u8 b[8];

	put_unaligned_be64(wwn, b);
	return snprintf(buf, len,
		"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
		b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
}

static char *tcm_qla2xxx_get_fabric_name(void)
{
	return "qla2xxx";
}

/*
 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
 */
static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
{
128
	unsigned int i, j;
129 130 131 132 133 134
	u8 wwn[8];

	memset(wwn, 0, sizeof(wwn));

	/* Validate and store the new name */
	for (i = 0, j = 0; i < 16; i++) {
135 136
		int value;

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
		value = hex_to_bin(*ns++);
		if (value >= 0)
			j = (j << 4) | value;
		else
			return -EINVAL;

		if (i % 2) {
			wwn[i/2] = j & 0xff;
			j = 0;
		}
	}

	*nm = wwn_to_u64(wwn);
	return 0;
}

/*
 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
 * store_fc_host_vport_create()
 */
static int tcm_qla2xxx_npiv_parse_wwn(
	const char *name,
	size_t count,
	u64 *wwpn,
	u64 *wwnn)
{
	unsigned int cnt = count;
	int rc;

	*wwpn = 0;
	*wwnn = 0;

	/* count may include a LF at end of string */
170
	if (name[cnt-1] == '\n' || name[cnt-1] == 0)
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		cnt--;

	/* validate we have enough characters for WWPN */
	if ((cnt != (16+1+16)) || (name[16] != ':'))
		return -EINVAL;

	rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
	if (rc != 0)
		return rc;

	rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
	if (rc != 0)
		return rc;

	return 0;
}

static char *tcm_qla2xxx_npiv_get_fabric_name(void)
{
	return "qla2xxx_npiv";
}

static u8 tcm_qla2xxx_get_fabric_proto_ident(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);
	struct tcm_qla2xxx_lport *lport = tpg->lport;
	u8 proto_id;

	switch (lport->lport_proto_id) {
	case SCSI_PROTOCOL_FCP:
	default:
		proto_id = fc_get_fabric_proto_ident(se_tpg);
		break;
	}

	return proto_id;
}

static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);
	struct tcm_qla2xxx_lport *lport = tpg->lport;

216
	return lport->lport_naa_name;
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
}

static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);
	return tpg->lport_tpgt;
}

static u32 tcm_qla2xxx_get_default_depth(struct se_portal_group *se_tpg)
{
	return 1;
}

static u32 tcm_qla2xxx_get_pr_transport_id(
	struct se_portal_group *se_tpg,
	struct se_node_acl *se_nacl,
	struct t10_pr_registration *pr_reg,
	int *format_code,
	unsigned char *buf)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);
	struct tcm_qla2xxx_lport *lport = tpg->lport;
	int ret = 0;

	switch (lport->lport_proto_id) {
	case SCSI_PROTOCOL_FCP:
	default:
		ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
					format_code, buf);
		break;
	}

	return ret;
}

static u32 tcm_qla2xxx_get_pr_transport_id_len(
	struct se_portal_group *se_tpg,
	struct se_node_acl *se_nacl,
	struct t10_pr_registration *pr_reg,
	int *format_code)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);
	struct tcm_qla2xxx_lport *lport = tpg->lport;
	int ret = 0;

	switch (lport->lport_proto_id) {
	case SCSI_PROTOCOL_FCP:
	default:
		ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
					format_code);
		break;
	}

	return ret;
}

static char *tcm_qla2xxx_parse_pr_out_transport_id(
	struct se_portal_group *se_tpg,
	const char *buf,
	u32 *out_tid_len,
	char **port_nexus_ptr)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);
	struct tcm_qla2xxx_lport *lport = tpg->lport;
	char *tid = NULL;

	switch (lport->lport_proto_id) {
	case SCSI_PROTOCOL_FCP:
	default:
		tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
					port_nexus_ptr);
		break;
	}

	return tid;
}

static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);

303
	return tpg->tpg_attrib.generate_node_acls;
304 305 306 307 308 309 310
}

static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);

311
	return tpg->tpg_attrib.cache_dynamic_acls;
312 313 314 315 316 317 318
}

static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);

319
	return tpg->tpg_attrib.demo_mode_write_protect;
320 321 322 323 324 325 326
}

static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);

327
	return tpg->tpg_attrib.prod_mode_write_protect;
328 329
}

330 331 332 333 334
static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);

335
	return tpg->tpg_attrib.demo_mode_login_only;
336 337
}

338 339 340 341 342 343 344 345
static int tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);

	return tpg->tpg_attrib.fabric_prot_type;
}

346 347 348 349 350 351 352
static struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
	struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_nacl *nacl;

	nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
	if (!nacl) {
353
		pr_err("Unable to allocate struct tcm_qla2xxx_nacl\n");
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
		return NULL;
	}

	return &nacl->se_node_acl;
}

static void tcm_qla2xxx_release_fabric_acl(
	struct se_portal_group *se_tpg,
	struct se_node_acl *se_nacl)
{
	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
			struct tcm_qla2xxx_nacl, se_node_acl);
	kfree(nacl);
}

static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);

	return tpg->lport_tpgt;
}

static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
{
	struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
			struct qla_tgt_mgmt_cmd, free_work);

	transport_generic_free_cmd(&mcmd->se_cmd, 0);
}

/*
 * Called from qla_target_template->free_mcmd(), and will call
 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
 * release callback.  qla_hw_data->hardware_lock is expected to be held
 */
static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
{
	INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
	queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
}

static void tcm_qla2xxx_complete_free(struct work_struct *work)
{
	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);

400 401 402 403 404
	cmd->cmd_in_wq = 0;

	WARN_ON(cmd->cmd_flags &  BIT_16);

	cmd->cmd_flags |= BIT_16;
405 406 407 408 409 410 411 412 413 414
	transport_generic_free_cmd(&cmd->se_cmd, 0);
}

/*
 * Called from qla_target_template->free_cmd(), and will call
 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
 * release callback.  qla_hw_data->hardware_lock is expected to be held
 */
static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
{
415
	cmd->cmd_in_wq = 1;
416 417 418 419 420 421 422 423 424
	INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
	queue_work(tcm_qla2xxx_free_wq, &cmd->work);
}

/*
 * Called from struct target_core_fabric_ops->check_stop_free() context
 */
static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
{
425 426 427 428 429 430 431
	struct qla_tgt_cmd *cmd;

	if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
		cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
		cmd->cmd_flags |= BIT_14;
	}

432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
	return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
}

/* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
 * fabric descriptor @se_cmd command to release
 */
static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
{
	struct qla_tgt_cmd *cmd;

	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
		struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
				struct qla_tgt_mgmt_cmd, se_cmd);
		qlt_free_mcmd(mcmd);
		return;
	}

	cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
	qlt_free_cmd(cmd);
}

static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
{
	struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
	struct scsi_qla_host *vha;
	unsigned long flags;

	BUG_ON(!sess);
	vha = sess->vha;

	spin_lock_irqsave(&vha->hw->hardware_lock, flags);
463
	target_sess_cmd_list_set_waiting(se_sess);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
	spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);

	return 1;
}

static void tcm_qla2xxx_close_session(struct se_session *se_sess)
{
	struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
	struct scsi_qla_host *vha;
	unsigned long flags;

	BUG_ON(!sess);
	vha = sess->vha;

	spin_lock_irqsave(&vha->hw->hardware_lock, flags);
	qlt_unreg_sess(sess);
	spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
}

static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
{
	return 0;
}

static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
{
	struct qla_tgt_cmd *cmd = container_of(se_cmd,
				struct qla_tgt_cmd, se_cmd);

	cmd->bufflen = se_cmd->data_length;
494
	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
495 496 497 498

	cmd->sg_cnt = se_cmd->t_data_nents;
	cmd->sg = se_cmd->t_data_sg;

499 500 501 502 503
	cmd->prot_sg_cnt = se_cmd->t_prot_nents;
	cmd->prot_sg = se_cmd->t_prot_sg;
	cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
	se_cmd->pi_err = 0;

504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
	/*
	 * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
	 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
	 */
	return qlt_rdy_to_xfer(cmd);
}

static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
{
	unsigned long flags;
	/*
	 * Check for WRITE_PENDING status to determine if we need to wait for
	 * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
	 */
	spin_lock_irqsave(&se_cmd->t_state_lock, flags);
	if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
	    se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
		spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
		wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
						3000);
		return 0;
	}
	spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);

	return 0;
}

static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
{
	return;
}

static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
{
538 539 540 541 542 543 544
	struct qla_tgt_cmd *cmd;

	/* check for task mgmt cmd */
	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
		return 0xffffffff;

	cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580

	return cmd->tag;
}

static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
{
	return 0;
}

/*
 * Called from process context in qla_target.c:qlt_do_work() code
 */
static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
	unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
	int data_dir, int bidi)
{
	struct se_cmd *se_cmd = &cmd->se_cmd;
	struct se_session *se_sess;
	struct qla_tgt_sess *sess;
	int flags = TARGET_SCF_ACK_KREF;

	if (bidi)
		flags |= TARGET_SCF_BIDI_OP;

	sess = cmd->sess;
	if (!sess) {
		pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
		return -EINVAL;
	}

	se_sess = sess->se_sess;
	if (!se_sess) {
		pr_err("Unable to locate active struct se_session\n");
		return -EINVAL;
	}

581
	return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
582 583 584 585
				cmd->unpacked_lun, data_length, fcp_task_attr,
				data_dir, flags);
}

586
static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
587 588 589 590 591 592 593
{
	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);

	/*
	 * Ensure that the complete FCP WRITE payload has been received.
	 * Otherwise return an exception via CHECK_CONDITION status.
	 */
594 595
	cmd->cmd_in_wq = 0;
	cmd->cmd_flags |= BIT_11;
596 597 598 599 600
	if (!cmd->write_data_transferred) {
		/*
		 * Check if se_cmd has already been aborted via LUN_RESET, and
		 * waiting upon completion in tcm_qla2xxx_write_pending_status()
		 */
601 602 603
		if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
			complete(&cmd->se_cmd.t_transport_stop_comp);
			return;
604 605
		}

606 607 608 609 610 611 612
		if (cmd->se_cmd.pi_err)
			transport_generic_request_failure(&cmd->se_cmd,
				cmd->se_cmd.pi_err);
		else
			transport_generic_request_failure(&cmd->se_cmd,
				TCM_CHECK_CONDITION_ABORT_CMD);

613
		return;
614
	}
615 616 617 618 619 620 621 622 623

	return target_execute_cmd(&cmd->se_cmd);
}

/*
 * Called from qla_target.c:qlt_do_ctio_completion()
 */
static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
{
624 625
	cmd->cmd_flags |= BIT_10;
	cmd->cmd_in_wq = 1;
626 627
	INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
	queue_work(tcm_qla2xxx_free_wq, &cmd->work);
628 629
}

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
static void tcm_qla2xxx_handle_dif_work(struct work_struct *work)
{
	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);

	/* take an extra kref to prevent cmd free too early.
	 * need to wait for SCSI status/check condition to
	 * finish responding generate by transport_generic_request_failure.
	 */
	kref_get(&cmd->se_cmd.cmd_kref);
	transport_generic_request_failure(&cmd->se_cmd, cmd->se_cmd.pi_err);
}

/*
 * Called from qla_target.c:qlt_do_ctio_completion()
 */
static void tcm_qla2xxx_handle_dif_err(struct qla_tgt_cmd *cmd)
{
	INIT_WORK(&cmd->work, tcm_qla2xxx_handle_dif_work);
	queue_work(tcm_qla2xxx_free_wq, &cmd->work);
}

651 652 653
/*
 * Called from qla_target.c:qlt_issue_task_mgmt()
 */
654 655
static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
	uint8_t tmr_func, uint32_t tag)
656 657 658 659 660 661 662 663 664 665 666 667 668
{
	struct qla_tgt_sess *sess = mcmd->sess;
	struct se_cmd *se_cmd = &mcmd->se_cmd;

	return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
			tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
}

static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
{
	struct qla_tgt_cmd *cmd = container_of(se_cmd,
				struct qla_tgt_cmd, se_cmd);

669
	cmd->cmd_flags |= BIT_4;
670
	cmd->bufflen = se_cmd->data_length;
671
	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
672 673 674 675 676
	cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);

	cmd->sg_cnt = se_cmd->t_data_nents;
	cmd->sg = se_cmd->t_data_sg;
	cmd->offset = 0;
677
	cmd->cmd_flags |= BIT_3;
678

679 680 681 682 683
	cmd->prot_sg_cnt = se_cmd->t_prot_nents;
	cmd->prot_sg = se_cmd->t_prot_sg;
	cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
	se_cmd->pi_err = 0;

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	/*
	 * Now queue completed DATA_IN the qla2xxx LLD and response ring
	 */
	return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
				se_cmd->scsi_status);
}

static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
{
	struct qla_tgt_cmd *cmd = container_of(se_cmd,
				struct qla_tgt_cmd, se_cmd);
	int xmit_type = QLA_TGT_XMIT_STATUS;

	cmd->bufflen = se_cmd->data_length;
	cmd->sg = NULL;
	cmd->sg_cnt = 0;
	cmd->offset = 0;
701
	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
702
	cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
703 704 705 706 707
	if (cmd->cmd_flags &  BIT_5) {
		pr_crit("Bit_5 already set for cmd = %p.\n", cmd);
		dump_stack();
	}
	cmd->cmd_flags |= BIT_5;
708 709 710 711 712 713

	if (se_cmd->data_direction == DMA_FROM_DEVICE) {
		/*
		 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
		 * for qla_tgt_xmit_response LLD code
		 */
714 715 716 717
		if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
			se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
			se_cmd->residual_count = 0;
		}
718
		se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
719
		se_cmd->residual_count += se_cmd->data_length;
720 721 722 723 724 725 726 727 728

		cmd->bufflen = 0;
	}
	/*
	 * Now queue status response to qla2xxx LLD code and response ring
	 */
	return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
}

729
static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
{
	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
	struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
				struct qla_tgt_mgmt_cmd, se_cmd);

	pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
			mcmd, se_tmr->function, se_tmr->response);
	/*
	 * Do translation between TCM TM response codes and
	 * QLA2xxx FC TM response codes.
	 */
	switch (se_tmr->response) {
	case TMR_FUNCTION_COMPLETE:
		mcmd->fc_tm_rsp = FC_TM_SUCCESS;
		break;
	case TMR_TASK_DOES_NOT_EXIST:
		mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
		break;
	case TMR_FUNCTION_REJECTED:
		mcmd->fc_tm_rsp = FC_TM_REJECT;
		break;
	case TMR_LUN_DOES_NOT_EXIST:
	default:
		mcmd->fc_tm_rsp = FC_TM_FAILED;
		break;
	}
	/*
	 * Queue the TM response to QLA2xxx LLD to build a
	 * CTIO response packet.
	 */
	qlt_xmit_tm_rsp(mcmd);
}

763 764 765 766 767 768 769 770 771 772 773 774 775 776
static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
{
	struct qla_tgt_cmd *cmd = container_of(se_cmd,
				struct qla_tgt_cmd, se_cmd);
	struct scsi_qla_host *vha = cmd->vha;
	struct qla_hw_data *ha = vha->hw;

	if (!cmd->sg_mapped)
		return;

	pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
	cmd->sg_mapped = 0;
}

777 778
static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
			struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
/*
 * Expected to be called with struct qla_hw_data->hardware_lock held
 */
static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
{
	struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
	struct se_portal_group *se_tpg = se_nacl->se_tpg;
	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
				struct tcm_qla2xxx_lport, lport_wwn);
	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
				struct tcm_qla2xxx_nacl, se_node_acl);
	void *node;

	pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);

	node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
796 797 798 799 800 801 802 803 804 805
	if (WARN_ON(node && (node != se_nacl))) {
		/*
		 * The nacl no longer matches what we think it should be.
		 * Most likely a new dynamic acl has been added while
		 * someone dropped the hardware lock.  It clearly is a
		 * bug elsewhere, but this bit can't make things worse.
		 */
		btree_insert32(&lport->lport_fcport_map, nacl->nport_id,
			       node, GFP_ATOMIC);
	}
806 807 808

	pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
	    se_nacl, nacl->nport_wwnn, nacl->nport_id);
809 810 811 812 813 814 815 816 817 818
	/*
	 * Now clear the se_nacl and session pointers from our HW lport lookup
	 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
	 *
	 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
	 * target_wait_for_sess_cmds() before the session waits for outstanding
	 * I/O to complete, to avoid a race between session shutdown execution
	 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
	 */
	tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
819 820
}

821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839
static void tcm_qla2xxx_release_session(struct kref *kref)
{
	struct se_session *se_sess = container_of(kref,
			struct se_session, sess_kref);

	qlt_unreg_sess(se_sess->fabric_sess_ptr);
}

static void tcm_qla2xxx_put_session(struct se_session *se_sess)
{
	struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
	struct qla_hw_data *ha = sess->vha->hw;
	unsigned long flags;

	spin_lock_irqsave(&ha->hardware_lock, flags);
	kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
	spin_unlock_irqrestore(&ha->hardware_lock, flags);
}

840 841
static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
{
842 843 844
	if (!sess)
		return;

845 846
	assert_spin_locked(&sess->vha->hw->hardware_lock);
	kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
847 848 849 850
}

static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
{
851 852
	assert_spin_locked(&sess->vha->hw->hardware_lock);
	target_sess_cmd_list_set_waiting(sess->se_sess);
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
}

static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
	struct se_portal_group *se_tpg,
	struct config_group *group,
	const char *name)
{
	struct se_node_acl *se_nacl, *se_nacl_new;
	struct tcm_qla2xxx_nacl *nacl;
	u64 wwnn;
	u32 qla2xxx_nexus_depth;

	if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
		return ERR_PTR(-EINVAL);

	se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
	if (!se_nacl_new)
		return ERR_PTR(-ENOMEM);
/* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
	qla2xxx_nexus_depth = 1;

	/*
	 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
	 * when converting a NodeACL from demo mode -> explict
	 */
	se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
				name, qla2xxx_nexus_depth);
	if (IS_ERR(se_nacl)) {
		tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
		return se_nacl;
	}
	/*
	 * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
	 */
	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
	nacl->nport_wwnn = wwnn;
	tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);

	return se_nacl;
}

static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
{
	struct se_portal_group *se_tpg = se_acl->se_tpg;
	struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
				struct tcm_qla2xxx_nacl, se_node_acl);

	core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
	kfree(nacl);
}

/* Start items for tcm_qla2xxx_tpg_attrib_cit */

#define DEF_QLA_TPG_ATTRIB(name)					\
									\
static ssize_t tcm_qla2xxx_tpg_attrib_show_##name(			\
	struct se_portal_group *se_tpg,					\
	char *page)							\
{									\
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,		\
			struct tcm_qla2xxx_tpg, se_tpg);		\
									\
915
	return sprintf(page, "%u\n", tpg->tpg_attrib.name);	\
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 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986
}									\
									\
static ssize_t tcm_qla2xxx_tpg_attrib_store_##name(			\
	struct se_portal_group *se_tpg,					\
	const char *page,						\
	size_t count)							\
{									\
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,		\
			struct tcm_qla2xxx_tpg, se_tpg);		\
	unsigned long val;						\
	int ret;							\
									\
	ret = kstrtoul(page, 0, &val);					\
	if (ret < 0) {							\
		pr_err("kstrtoul() failed with"				\
				" ret: %d\n", ret);			\
		return -EINVAL;						\
	}								\
	ret = tcm_qla2xxx_set_attrib_##name(tpg, val);			\
									\
	return (!ret) ? count : -EINVAL;				\
}

#define DEF_QLA_TPG_ATTR_BOOL(_name)					\
									\
static int tcm_qla2xxx_set_attrib_##_name(				\
	struct tcm_qla2xxx_tpg *tpg,					\
	unsigned long val)						\
{									\
	struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib;		\
									\
	if ((val != 0) && (val != 1)) {					\
		pr_err("Illegal boolean value %lu\n", val);		\
		return -EINVAL;						\
	}								\
									\
	a->_name = val;							\
	return 0;							\
}

#define QLA_TPG_ATTR(_name, _mode) \
	TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);

/*
 * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
 */
DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
DEF_QLA_TPG_ATTRIB(generate_node_acls);
QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);

/*
 Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
 */
DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);

/*
 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
 */
DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);

/*
 * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
 */
DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);

987 988 989 990 991 992 993
/*
 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_login_only
 */
DEF_QLA_TPG_ATTR_BOOL(demo_mode_login_only);
DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
QLA_TPG_ATTR(demo_mode_login_only, S_IRUGO | S_IWUSR);

994 995 996 997 998
static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
	&tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
	&tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
	&tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
	&tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
999
	&tcm_qla2xxx_tpg_attrib_demo_mode_login_only.attr,
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
	NULL,
};

/* End items for tcm_qla2xxx_tpg_attrib_cit */

static ssize_t tcm_qla2xxx_tpg_show_enable(
	struct se_portal_group *se_tpg,
	char *page)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
			struct tcm_qla2xxx_tpg, se_tpg);

	return snprintf(page, PAGE_SIZE, "%d\n",
			atomic_read(&tpg->lport_tpg_enabled));
}

1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
static void tcm_qla2xxx_depend_tpg(struct work_struct *work)
{
	struct tcm_qla2xxx_tpg *base_tpg = container_of(work,
				struct tcm_qla2xxx_tpg, tpg_base_work);
	struct se_portal_group *se_tpg = &base_tpg->se_tpg;
	struct scsi_qla_host *base_vha = base_tpg->lport->qla_vha;

	if (!configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
				  &se_tpg->tpg_group.cg_item)) {
		atomic_set(&base_tpg->lport_tpg_enabled, 1);
		qlt_enable_vha(base_vha);
	}
	complete(&base_tpg->tpg_base_comp);
}

static void tcm_qla2xxx_undepend_tpg(struct work_struct *work)
{
	struct tcm_qla2xxx_tpg *base_tpg = container_of(work,
				struct tcm_qla2xxx_tpg, tpg_base_work);
	struct se_portal_group *se_tpg = &base_tpg->se_tpg;
	struct scsi_qla_host *base_vha = base_tpg->lport->qla_vha;

	if (!qlt_stop_phase1(base_vha->vha_tgt.qla_tgt)) {
		atomic_set(&base_tpg->lport_tpg_enabled, 0);
		configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
				       &se_tpg->tpg_group.cg_item);
	}
	complete(&base_tpg->tpg_base_comp);
}

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
static ssize_t tcm_qla2xxx_tpg_store_enable(
	struct se_portal_group *se_tpg,
	const char *page,
	size_t count)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
			struct tcm_qla2xxx_tpg, se_tpg);
	unsigned long op;
	int rc;

	rc = kstrtoul(page, 0, &op);
	if (rc < 0) {
		pr_err("kstrtoul() returned %d\n", rc);
		return -EINVAL;
	}
	if ((op != 1) && (op != 0)) {
		pr_err("Illegal value for tpg_enable: %lu\n", op);
		return -EINVAL;
	}
	if (op) {
1066 1067 1068 1069
		if (atomic_read(&tpg->lport_tpg_enabled))
			return -EEXIST;

		INIT_WORK(&tpg->tpg_base_work, tcm_qla2xxx_depend_tpg);
1070
	} else {
1071 1072 1073 1074
		if (!atomic_read(&tpg->lport_tpg_enabled))
			return count;

		INIT_WORK(&tpg->tpg_base_work, tcm_qla2xxx_undepend_tpg);
1075
	}
1076 1077 1078
	init_completion(&tpg->tpg_base_comp);
	schedule_work(&tpg->tpg_base_work);
	wait_for_completion(&tpg->tpg_base_comp);
1079

1080 1081 1082 1083 1084 1085 1086
	if (op) {
		if (!atomic_read(&tpg->lport_tpg_enabled))
			return -ENODEV;
	} else {
		if (atomic_read(&tpg->lport_tpg_enabled))
			return -EPERM;
	}
1087 1088 1089 1090 1091
	return count;
}

TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);

1092 1093 1094 1095 1096 1097 1098 1099 1100
static ssize_t tcm_qla2xxx_tpg_show_dynamic_sessions(
	struct se_portal_group *se_tpg,
	char *page)
{
	return target_show_dynamic_sessions(se_tpg, page);
}

TF_TPG_BASE_ATTR_RO(tcm_qla2xxx, dynamic_sessions);

1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
static ssize_t tcm_qla2xxx_tpg_store_fabric_prot_type(
	struct se_portal_group *se_tpg,
	const char *page,
	size_t count)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);
	unsigned long val;
	int ret = kstrtoul(page, 0, &val);

	if (ret) {
		pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
		return ret;
	}
	if (val != 0 && val != 1 && val != 3) {
		pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
		return -EINVAL;
	}
	tpg->tpg_attrib.fabric_prot_type = val;

	return count;
}

static ssize_t tcm_qla2xxx_tpg_show_fabric_prot_type(
	struct se_portal_group *se_tpg,
	char *page)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
				struct tcm_qla2xxx_tpg, se_tpg);

	return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
}
TF_TPG_BASE_ATTR(tcm_qla2xxx, fabric_prot_type, S_IRUGO | S_IWUSR);

1135 1136
static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
	&tcm_qla2xxx_tpg_enable.attr,
1137
	&tcm_qla2xxx_tpg_dynamic_sessions.attr,
1138
	&tcm_qla2xxx_tpg_fabric_prot_type.attr,
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
	NULL,
};

static struct se_portal_group *tcm_qla2xxx_make_tpg(
	struct se_wwn *wwn,
	struct config_group *group,
	const char *name)
{
	struct tcm_qla2xxx_lport *lport = container_of(wwn,
			struct tcm_qla2xxx_lport, lport_wwn);
	struct tcm_qla2xxx_tpg *tpg;
	unsigned long tpgt;
	int ret;

	if (strstr(name, "tpgt_") != name)
		return ERR_PTR(-EINVAL);
	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
		return ERR_PTR(-EINVAL);

1158
	if ((tpgt != 1)) {
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
		pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
		return ERR_PTR(-ENOSYS);
	}

	tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
	if (!tpg) {
		pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
		return ERR_PTR(-ENOMEM);
	}
	tpg->lport = lport;
	tpg->lport_tpgt = tpgt;
	/*
	 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
	 * NodeACLs
	 */
1174 1175 1176 1177
	tpg->tpg_attrib.generate_node_acls = 1;
	tpg->tpg_attrib.demo_mode_write_protect = 1;
	tpg->tpg_attrib.cache_dynamic_acls = 1;
	tpg->tpg_attrib.demo_mode_login_only = 1;
1178

1179
	ret = core_tpg_register(&tcm_qla2xxx_ops, wwn,
1180 1181 1182 1183 1184
				&tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
	if (ret < 0) {
		kfree(tpg);
		return NULL;
	}
1185 1186

	lport->tpg_1 = tpg;
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200

	return &tpg->se_tpg;
}

static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
{
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
			struct tcm_qla2xxx_tpg, se_tpg);
	struct tcm_qla2xxx_lport *lport = tpg->lport;
	struct scsi_qla_host *vha = lport->qla_vha;
	/*
	 * Call into qla2x_target.c LLD logic to shutdown the active
	 * FC Nexuses and disable target mode operation for this qla_hw_data
	 */
1201 1202
	if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1203 1204 1205 1206 1207

	core_tpg_deregister(se_tpg);
	/*
	 * Clear local TPG=1 pointer for non NPIV mode.
	 */
1208
	lport->tpg_1 = NULL;
1209 1210 1211
	kfree(tpg);
}

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265
static ssize_t tcm_qla2xxx_npiv_tpg_show_enable(
	struct se_portal_group *se_tpg,
	char *page)
{
	return tcm_qla2xxx_tpg_show_enable(se_tpg, page);
}

static ssize_t tcm_qla2xxx_npiv_tpg_store_enable(
	struct se_portal_group *se_tpg,
	const char *page,
	size_t count)
{
	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
			struct tcm_qla2xxx_lport, lport_wwn);
	struct scsi_qla_host *vha = lport->qla_vha;
	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
			struct tcm_qla2xxx_tpg, se_tpg);
	unsigned long op;
	int rc;

	rc = kstrtoul(page, 0, &op);
	if (rc < 0) {
		pr_err("kstrtoul() returned %d\n", rc);
		return -EINVAL;
	}
	if ((op != 1) && (op != 0)) {
		pr_err("Illegal value for tpg_enable: %lu\n", op);
		return -EINVAL;
	}
	if (op) {
		if (atomic_read(&tpg->lport_tpg_enabled))
			return -EEXIST;

		atomic_set(&tpg->lport_tpg_enabled, 1);
		qlt_enable_vha(vha);
	} else {
		if (!atomic_read(&tpg->lport_tpg_enabled))
			return count;

		atomic_set(&tpg->lport_tpg_enabled, 0);
		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
	}

	return count;
}

TF_TPG_BASE_ATTR(tcm_qla2xxx_npiv, enable, S_IRUGO | S_IWUSR);

static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
        &tcm_qla2xxx_npiv_tpg_enable.attr,
        NULL,
};

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
	struct se_wwn *wwn,
	struct config_group *group,
	const char *name)
{
	struct tcm_qla2xxx_lport *lport = container_of(wwn,
			struct tcm_qla2xxx_lport, lport_wwn);
	struct tcm_qla2xxx_tpg *tpg;
	unsigned long tpgt;
	int ret;

	if (strstr(name, "tpgt_") != name)
		return ERR_PTR(-EINVAL);
	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
		return ERR_PTR(-EINVAL);

	tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
	if (!tpg) {
		pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
		return ERR_PTR(-ENOMEM);
	}
	tpg->lport = lport;
	tpg->lport_tpgt = tpgt;

1290 1291 1292 1293 1294 1295 1296 1297 1298
	/*
	 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
	 * NodeACLs
	 */
	tpg->tpg_attrib.generate_node_acls = 1;
	tpg->tpg_attrib.demo_mode_write_protect = 1;
	tpg->tpg_attrib.cache_dynamic_acls = 1;
	tpg->tpg_attrib.demo_mode_login_only = 1;

1299
	ret = core_tpg_register(&tcm_qla2xxx_npiv_ops, wwn,
1300 1301 1302 1303 1304
				&tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
	if (ret < 0) {
		kfree(tpg);
		return NULL;
	}
1305
	lport->tpg_1 = tpg;
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
	return &tpg->se_tpg;
}

/*
 * Expected to be called with struct qla_hw_data->hardware_lock held
 */
static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
	scsi_qla_host_t *vha,
	const uint8_t *s_id)
{
	struct tcm_qla2xxx_lport *lport;
	struct se_node_acl *se_nacl;
	struct tcm_qla2xxx_nacl *nacl;
	u32 key;

1321
	lport = vha->vha_tgt.target_lport_ptr;
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
	if (!lport) {
		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
		dump_stack();
		return NULL;
	}

	key = (((unsigned long)s_id[0] << 16) |
	       ((unsigned long)s_id[1] << 8) |
	       (unsigned long)s_id[2]);
	pr_debug("find_sess_by_s_id: 0x%06x\n", key);

	se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
	if (!se_nacl) {
		pr_debug("Unable to locate s_id: 0x%06x\n", key);
		return NULL;
	}
	pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
	    se_nacl, se_nacl->initiatorname);

	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
	if (!nacl->qla_tgt_sess) {
		pr_err("Unable to locate struct qla_tgt_sess\n");
		return NULL;
	}

	return nacl->qla_tgt_sess;
}

/*
 * Expected to be called with struct qla_hw_data->hardware_lock held
 */
static void tcm_qla2xxx_set_sess_by_s_id(
	struct tcm_qla2xxx_lport *lport,
	struct se_node_acl *new_se_nacl,
	struct tcm_qla2xxx_nacl *nacl,
	struct se_session *se_sess,
	struct qla_tgt_sess *qla_tgt_sess,
	uint8_t *s_id)
{
	u32 key;
	void *slot;
	int rc;

	key = (((unsigned long)s_id[0] << 16) |
	       ((unsigned long)s_id[1] << 8) |
	       (unsigned long)s_id[2]);
	pr_debug("set_sess_by_s_id: %06x\n", key);

	slot = btree_lookup32(&lport->lport_fcport_map, key);
	if (!slot) {
		if (new_se_nacl) {
			pr_debug("Setting up new fc_port entry to new_se_nacl\n");
			nacl->nport_id = key;
			rc = btree_insert32(&lport->lport_fcport_map, key,
					new_se_nacl, GFP_ATOMIC);
			if (rc)
				printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
				    (int)key);
		} else {
			pr_debug("Wiping nonexisting fc_port entry\n");
		}

		qla_tgt_sess->se_sess = se_sess;
		nacl->qla_tgt_sess = qla_tgt_sess;
		return;
	}

	if (nacl->qla_tgt_sess) {
		if (new_se_nacl == NULL) {
			pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
			btree_remove32(&lport->lport_fcport_map, key);
			nacl->qla_tgt_sess = NULL;
			return;
		}
		pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
		btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
		qla_tgt_sess->se_sess = se_sess;
		nacl->qla_tgt_sess = qla_tgt_sess;
		return;
	}

	if (new_se_nacl == NULL) {
		pr_debug("Clearing existing fc_port entry\n");
		btree_remove32(&lport->lport_fcport_map, key);
		return;
	}

	pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
	btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
	qla_tgt_sess->se_sess = se_sess;
	nacl->qla_tgt_sess = qla_tgt_sess;

	pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
	    nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
}

/*
 * Expected to be called with struct qla_hw_data->hardware_lock held
 */
static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
	scsi_qla_host_t *vha,
	const uint16_t loop_id)
{
	struct tcm_qla2xxx_lport *lport;
	struct se_node_acl *se_nacl;
	struct tcm_qla2xxx_nacl *nacl;
	struct tcm_qla2xxx_fc_loopid *fc_loopid;

1430
	lport = vha->vha_tgt.target_lport_ptr;
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
	if (!lport) {
		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
		dump_stack();
		return NULL;
	}

	pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);

	fc_loopid = lport->lport_loopid_map + loop_id;
	se_nacl = fc_loopid->se_nacl;
	if (!se_nacl) {
		pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
		    loop_id);
		return NULL;
	}

	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);

	if (!nacl->qla_tgt_sess) {
		pr_err("Unable to locate struct qla_tgt_sess\n");
		return NULL;
	}

	return nacl->qla_tgt_sess;
}

/*
 * Expected to be called with struct qla_hw_data->hardware_lock held
 */
static void tcm_qla2xxx_set_sess_by_loop_id(
	struct tcm_qla2xxx_lport *lport,
	struct se_node_acl *new_se_nacl,
	struct tcm_qla2xxx_nacl *nacl,
	struct se_session *se_sess,
	struct qla_tgt_sess *qla_tgt_sess,
	uint16_t loop_id)
{
	struct se_node_acl *saved_nacl;
	struct tcm_qla2xxx_fc_loopid *fc_loopid;

	pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);

	fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
			lport->lport_loopid_map)[loop_id];

	saved_nacl = fc_loopid->se_nacl;
	if (!saved_nacl) {
		pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
		fc_loopid->se_nacl = new_se_nacl;
		if (qla_tgt_sess->se_sess != se_sess)
			qla_tgt_sess->se_sess = se_sess;
		if (nacl->qla_tgt_sess != qla_tgt_sess)
			nacl->qla_tgt_sess = qla_tgt_sess;
		return;
	}

	if (nacl->qla_tgt_sess) {
		if (new_se_nacl == NULL) {
			pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
			fc_loopid->se_nacl = NULL;
			nacl->qla_tgt_sess = NULL;
			return;
		}

		pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
		fc_loopid->se_nacl = new_se_nacl;
		if (qla_tgt_sess->se_sess != se_sess)
			qla_tgt_sess->se_sess = se_sess;
		if (nacl->qla_tgt_sess != qla_tgt_sess)
			nacl->qla_tgt_sess = qla_tgt_sess;
		return;
	}

	if (new_se_nacl == NULL) {
		pr_debug("Clearing fc_loopid->se_nacl\n");
		fc_loopid->se_nacl = NULL;
		return;
	}

	pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
	fc_loopid->se_nacl = new_se_nacl;
	if (qla_tgt_sess->se_sess != se_sess)
		qla_tgt_sess->se_sess = se_sess;
	if (nacl->qla_tgt_sess != qla_tgt_sess)
		nacl->qla_tgt_sess = qla_tgt_sess;

	pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
	    nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
}

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
/*
 * Should always be called with qla_hw_data->hardware_lock held.
 */
static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
		struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
{
	struct se_session *se_sess = sess->se_sess;
	unsigned char be_sid[3];

	be_sid[0] = sess->s_id.b.domain;
	be_sid[1] = sess->s_id.b.area;
	be_sid[2] = sess->s_id.b.al_pa;

	tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
				sess, be_sid);
	tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
				sess, sess->loop_id);
}

1540 1541 1542 1543
static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
{
	struct qla_tgt *tgt = sess->tgt;
	struct qla_hw_data *ha = tgt->ha;
1544
	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560
	struct se_session *se_sess;
	struct se_node_acl *se_nacl;
	struct tcm_qla2xxx_lport *lport;
	struct tcm_qla2xxx_nacl *nacl;

	BUG_ON(in_interrupt());

	se_sess = sess->se_sess;
	if (!se_sess) {
		pr_err("struct qla_tgt_sess->se_sess is NULL\n");
		dump_stack();
		return;
	}
	se_nacl = se_sess->se_node_acl;
	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);

1561
	lport = vha->vha_tgt.target_lport_ptr;
1562 1563 1564 1565 1566
	if (!lport) {
		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
		dump_stack();
		return;
	}
1567
	target_wait_for_sess_cmds(se_sess);
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593

	transport_deregister_session_configfs(sess->se_sess);
	transport_deregister_session(sess->se_sess);
}

/*
 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
 * to locate struct se_node_acl
 */
static int tcm_qla2xxx_check_initiator_node_acl(
	scsi_qla_host_t *vha,
	unsigned char *fc_wwpn,
	void *qla_tgt_sess,
	uint8_t *s_id,
	uint16_t loop_id)
{
	struct qla_hw_data *ha = vha->hw;
	struct tcm_qla2xxx_lport *lport;
	struct tcm_qla2xxx_tpg *tpg;
	struct tcm_qla2xxx_nacl *nacl;
	struct se_portal_group *se_tpg;
	struct se_node_acl *se_nacl;
	struct se_session *se_sess;
	struct qla_tgt_sess *sess = qla_tgt_sess;
	unsigned char port_name[36];
	unsigned long flags;
1594 1595
	int num_tags = (ha->fw_xcb_count) ? ha->fw_xcb_count :
		       TCM_QLA2XXX_DEFAULT_TAGS;
1596

1597
	lport = vha->vha_tgt.target_lport_ptr;
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
	if (!lport) {
		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
		dump_stack();
		return -EINVAL;
	}
	/*
	 * Locate the TPG=1 reference..
	 */
	tpg = lport->tpg_1;
	if (!tpg) {
		pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
		return -EINVAL;
	}
	se_tpg = &tpg->se_tpg;

1613 1614
	se_sess = transport_init_session_tags(num_tags,
					      sizeof(struct qla_tgt_cmd),
1615
					      TARGET_PROT_ALL);
1616 1617 1618 1619 1620 1621 1622 1623 1624
	if (IS_ERR(se_sess)) {
		pr_err("Unable to initialize struct se_session\n");
		return PTR_ERR(se_sess);
	}
	/*
	 * Format the FCP Initiator port_name into colon seperated values to
	 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
	 */
	memset(&port_name, 0, 36);
1625
	snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn);
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
	/*
	 * Locate our struct se_node_acl either from an explict NodeACL created
	 * via ConfigFS, or via running in TPG demo mode.
	 */
	se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
					port_name);
	if (!se_sess->se_node_acl) {
		transport_free_session(se_sess);
		return -EINVAL;
	}
	se_nacl = se_sess->se_node_acl;
	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
	/*
	 * And now setup the new se_nacl and session pointers into our HW lport
	 * mappings for fabric S_ID and LOOP_ID.
	 */
	spin_lock_irqsave(&ha->hardware_lock, flags);
	tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
			qla_tgt_sess, s_id);
	tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
			qla_tgt_sess, loop_id);
	spin_unlock_irqrestore(&ha->hardware_lock, flags);
	/*
	 * Finally register the new FC Nexus with TCM
	 */
1651
	transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
1652 1653 1654 1655

	return 0;
}

1656 1657 1658 1659 1660
static void tcm_qla2xxx_update_sess(struct qla_tgt_sess *sess, port_id_t s_id,
				    uint16_t loop_id, bool conf_compl_supported)
{
	struct qla_tgt *tgt = sess->tgt;
	struct qla_hw_data *ha = tgt->ha;
1661 1662
	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
	struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
1663 1664 1665 1666 1667 1668 1669
	struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
			struct tcm_qla2xxx_nacl, se_node_acl);
	u32 key;


	if (sess->loop_id != loop_id || sess->s_id.b24 != s_id.b24)
1670 1671 1672 1673 1674
		pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
		    sess, sess->port_name,
		    sess->loop_id, loop_id, sess->s_id.b.domain,
		    sess->s_id.b.area, sess->s_id.b.al_pa, s_id.b.domain,
		    s_id.b.area, s_id.b.al_pa);
1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724

	if (sess->loop_id != loop_id) {
		/*
		 * Because we can shuffle loop IDs around and we
		 * update different sessions non-atomically, we might
		 * have overwritten this session's old loop ID
		 * already, and we might end up overwriting some other
		 * session that will be updated later.  So we have to
		 * be extra careful and we can't warn about those things...
		 */
		if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
			lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;

		lport->lport_loopid_map[loop_id].se_nacl = se_nacl;

		sess->loop_id = loop_id;
	}

	if (sess->s_id.b24 != s_id.b24) {
		key = (((u32) sess->s_id.b.domain << 16) |
		       ((u32) sess->s_id.b.area   <<  8) |
		       ((u32) sess->s_id.b.al_pa));

		if (btree_lookup32(&lport->lport_fcport_map, key))
			WARN(btree_remove32(&lport->lport_fcport_map, key) != se_nacl,
			     "Found wrong se_nacl when updating s_id %x:%x:%x\n",
			     sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
		else
			WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
			     sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);

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

		if (btree_lookup32(&lport->lport_fcport_map, key)) {
			WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
			     s_id.b.domain, s_id.b.area, s_id.b.al_pa);
			btree_update32(&lport->lport_fcport_map, key, se_nacl);
		} else {
			btree_insert32(&lport->lport_fcport_map, key, se_nacl, GFP_ATOMIC);
		}

		sess->s_id = s_id;
		nacl->nport_id = key;
	}

	sess->conf_compl_supported = conf_compl_supported;
}

1725 1726 1727 1728 1729 1730
/*
 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
 */
static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
	.handle_cmd		= tcm_qla2xxx_handle_cmd,
	.handle_data		= tcm_qla2xxx_handle_data,
1731
	.handle_dif_err		= tcm_qla2xxx_handle_dif_err,
1732 1733 1734 1735
	.handle_tmr		= tcm_qla2xxx_handle_tmr,
	.free_cmd		= tcm_qla2xxx_free_cmd,
	.free_mcmd		= tcm_qla2xxx_free_mcmd,
	.free_session		= tcm_qla2xxx_free_session,
1736
	.update_sess		= tcm_qla2xxx_update_sess,
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
	.check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
	.find_sess_by_s_id	= tcm_qla2xxx_find_sess_by_s_id,
	.find_sess_by_loop_id	= tcm_qla2xxx_find_sess_by_loop_id,
	.clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
	.put_sess		= tcm_qla2xxx_put_sess,
	.shutdown_sess		= tcm_qla2xxx_shutdown_sess,
};

static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
{
	int rc;

	rc = btree_init32(&lport->lport_fcport_map);
	if (rc) {
		pr_err("Unable to initialize lport->lport_fcport_map btree\n");
		return rc;
	}

	lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
				65536);
	if (!lport->lport_loopid_map) {
		pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
		    sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
		btree_destroy32(&lport->lport_fcport_map);
		return -ENOMEM;
	}
	memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
	       * 65536);
	pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
	       sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
	return 0;
}

1770 1771 1772
static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
					 void *target_lport_ptr,
					 u64 npiv_wwpn, u64 npiv_wwnn)
1773
{
1774 1775 1776
	struct qla_hw_data *ha = vha->hw;
	struct tcm_qla2xxx_lport *lport =
			(struct tcm_qla2xxx_lport *)target_lport_ptr;
1777
	/*
1778
	 * Setup tgt_ops, local pointer to vha and target_lport_ptr
1779
	 */
1780 1781
	ha->tgt.tgt_ops = &tcm_qla2xxx_template;
	vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
	lport->qla_vha = vha;

	return 0;
}

static struct se_wwn *tcm_qla2xxx_make_lport(
	struct target_fabric_configfs *tf,
	struct config_group *group,
	const char *name)
{
	struct tcm_qla2xxx_lport *lport;
	u64 wwpn;
	int ret = -ENODEV;

	if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
		return ERR_PTR(-EINVAL);

	lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
	if (!lport) {
		pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
		return ERR_PTR(-ENOMEM);
	}
	lport->lport_wwpn = wwpn;
	tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
				wwpn);
1807
	sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
1808 1809 1810 1811 1812

	ret = tcm_qla2xxx_init_lport(lport);
	if (ret != 0)
		goto out;

1813 1814
	ret = qlt_lport_register(lport, wwpn, 0, 0,
				 tcm_qla2xxx_lport_register_cb);
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839
	if (ret != 0)
		goto out_lport;

	return &lport->lport_wwn;
out_lport:
	vfree(lport->lport_loopid_map);
	btree_destroy32(&lport->lport_fcport_map);
out:
	kfree(lport);
	return ERR_PTR(ret);
}

static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
{
	struct tcm_qla2xxx_lport *lport = container_of(wwn,
			struct tcm_qla2xxx_lport, lport_wwn);
	struct scsi_qla_host *vha = lport->qla_vha;
	struct se_node_acl *node;
	u32 key = 0;

	/*
	 * Call into qla2x_target.c LLD logic to complete the
	 * shutdown of struct qla_tgt after the call to
	 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
	 */
1840 1841
	if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
		qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1842 1843 1844 1845 1846 1847 1848 1849 1850 1851

	qlt_lport_deregister(vha);

	vfree(lport->lport_loopid_map);
	btree_for_each_safe32(&lport->lport_fcport_map, key, node)
		btree_remove32(&lport->lport_fcport_map, key);
	btree_destroy32(&lport->lport_fcport_map);
	kfree(lport);
}

1852 1853 1854 1855 1856 1857 1858 1859 1860
static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
					      void *target_lport_ptr,
					      u64 npiv_wwpn, u64 npiv_wwnn)
{
	struct fc_vport *vport;
	struct Scsi_Host *sh = base_vha->host;
	struct scsi_qla_host *npiv_vha;
	struct tcm_qla2xxx_lport *lport =
			(struct tcm_qla2xxx_lport *)target_lport_ptr;
1861 1862 1863
	struct tcm_qla2xxx_lport *base_lport =
			(struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
	struct tcm_qla2xxx_tpg *base_tpg;
1864 1865 1866 1867 1868 1869 1870
	struct fc_vport_identifiers vport_id;

	if (!qla_tgt_mode_enabled(base_vha)) {
		pr_err("qla2xxx base_vha not enabled for target mode\n");
		return -EPERM;
	}

1871 1872 1873 1874 1875 1876 1877
	if (!base_lport || !base_lport->tpg_1 ||
	    !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
		pr_err("qla2xxx base_lport or tpg_1 not available\n");
		return -EPERM;
	}
	base_tpg = base_lport->tpg_1;

1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900
	memset(&vport_id, 0, sizeof(vport_id));
	vport_id.port_name = npiv_wwpn;
	vport_id.node_name = npiv_wwnn;
	vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
	vport_id.vport_type = FC_PORTTYPE_NPIV;
	vport_id.disable = false;

	vport = fc_vport_create(sh, 0, &vport_id);
	if (!vport) {
		pr_err("fc_vport_create failed for qla2xxx_npiv\n");
		return -ENODEV;
	}
	/*
	 * Setup local pointer to NPIV vhba + target_lport_ptr
	 */
	npiv_vha = (struct scsi_qla_host *)vport->dd_data;
	npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
	lport->qla_vha = npiv_vha;
	scsi_host_get(npiv_vha->host);
	return 0;
}


1901 1902 1903 1904 1905 1906
static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
	struct target_fabric_configfs *tf,
	struct config_group *group,
	const char *name)
{
	struct tcm_qla2xxx_lport *lport;
1907 1908
	u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
	char *p, tmp[128];
1909 1910
	int ret;

1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924
	snprintf(tmp, 128, "%s", name);

	p = strchr(tmp, '@');
	if (!p) {
		pr_err("Unable to locate NPIV '@' seperator\n");
		return ERR_PTR(-EINVAL);
	}
	*p++ = '\0';

	if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
		return ERR_PTR(-EINVAL);

	if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
				       &npiv_wwpn, &npiv_wwnn) < 0)
1925 1926 1927 1928 1929 1930 1931 1932 1933
		return ERR_PTR(-EINVAL);

	lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
	if (!lport) {
		pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
		return ERR_PTR(-ENOMEM);
	}
	lport->lport_npiv_wwpn = npiv_wwpn;
	lport->lport_npiv_wwnn = npiv_wwnn;
1934
	sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
1935

1936
	ret = tcm_qla2xxx_init_lport(lport);
1937 1938 1939
	if (ret != 0)
		goto out;

1940 1941
	ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
				 tcm_qla2xxx_lport_register_npiv_cb);
1942 1943 1944
	if (ret != 0)
		goto out_lport;

1945
	return &lport->lport_wwn;
1946 1947 1948
out_lport:
	vfree(lport->lport_loopid_map);
	btree_destroy32(&lport->lport_fcport_map);
1949 1950 1951 1952 1953 1954 1955 1956 1957
out:
	kfree(lport);
	return ERR_PTR(ret);
}

static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
{
	struct tcm_qla2xxx_lport *lport = container_of(wwn,
			struct tcm_qla2xxx_lport, lport_wwn);
1958 1959 1960 1961 1962
	struct scsi_qla_host *npiv_vha = lport->qla_vha;
	struct qla_hw_data *ha = npiv_vha->hw;
	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);

	scsi_host_put(npiv_vha->host);
1963
	/*
1964
	 * Notify libfc that we want to release the vha->fc_vport
1965
	 */
1966 1967
	fc_vport_terminate(npiv_vha->fc_vport);
	scsi_host_put(base_vha->host);
1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988
	kfree(lport);
}


static ssize_t tcm_qla2xxx_wwn_show_attr_version(
	struct target_fabric_configfs *tf,
	char *page)
{
	return sprintf(page,
	    "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
	    UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
	    utsname()->machine);
}

TF_WWN_ATTR_RO(tcm_qla2xxx, version);

static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
	&tcm_qla2xxx_wwn_version.attr,
	NULL,
};

1989 1990 1991
static const struct target_core_fabric_ops tcm_qla2xxx_ops = {
	.module				= THIS_MODULE,
	.name				= "qla2xxx",
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005
	.get_fabric_name		= tcm_qla2xxx_get_fabric_name,
	.get_fabric_proto_ident		= tcm_qla2xxx_get_fabric_proto_ident,
	.tpg_get_wwn			= tcm_qla2xxx_get_fabric_wwn,
	.tpg_get_tag			= tcm_qla2xxx_get_tag,
	.tpg_get_default_depth		= tcm_qla2xxx_get_default_depth,
	.tpg_get_pr_transport_id	= tcm_qla2xxx_get_pr_transport_id,
	.tpg_get_pr_transport_id_len	= tcm_qla2xxx_get_pr_transport_id_len,
	.tpg_parse_pr_out_transport_id	= tcm_qla2xxx_parse_pr_out_transport_id,
	.tpg_check_demo_mode		= tcm_qla2xxx_check_demo_mode,
	.tpg_check_demo_mode_cache	= tcm_qla2xxx_check_demo_mode_cache,
	.tpg_check_demo_mode_write_protect =
					tcm_qla2xxx_check_demo_write_protect,
	.tpg_check_prod_mode_write_protect =
					tcm_qla2xxx_check_prod_write_protect,
2006
	.tpg_check_prot_fabric_only	= tcm_qla2xxx_check_prot_fabric_only,
2007
	.tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
2008 2009 2010 2011 2012
	.tpg_alloc_fabric_acl		= tcm_qla2xxx_alloc_fabric_acl,
	.tpg_release_fabric_acl		= tcm_qla2xxx_release_fabric_acl,
	.tpg_get_inst_index		= tcm_qla2xxx_tpg_get_inst_index,
	.check_stop_free		= tcm_qla2xxx_check_stop_free,
	.release_cmd			= tcm_qla2xxx_release_cmd,
2013
	.put_session			= tcm_qla2xxx_put_session,
2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025
	.shutdown_session		= tcm_qla2xxx_shutdown_session,
	.close_session			= tcm_qla2xxx_close_session,
	.sess_get_index			= tcm_qla2xxx_sess_get_index,
	.sess_get_initiator_sid		= NULL,
	.write_pending			= tcm_qla2xxx_write_pending,
	.write_pending_status		= tcm_qla2xxx_write_pending_status,
	.set_default_node_attributes	= tcm_qla2xxx_set_default_node_attrs,
	.get_task_tag			= tcm_qla2xxx_get_task_tag,
	.get_cmd_state			= tcm_qla2xxx_get_cmd_state,
	.queue_data_in			= tcm_qla2xxx_queue_data_in,
	.queue_status			= tcm_qla2xxx_queue_status,
	.queue_tm_rsp			= tcm_qla2xxx_queue_tm_rsp,
2026
	.aborted_task			= tcm_qla2xxx_aborted_task,
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040
	/*
	 * Setup function pointers for generic logic in
	 * target_core_fabric_configfs.c
	 */
	.fabric_make_wwn		= tcm_qla2xxx_make_lport,
	.fabric_drop_wwn		= tcm_qla2xxx_drop_lport,
	.fabric_make_tpg		= tcm_qla2xxx_make_tpg,
	.fabric_drop_tpg		= tcm_qla2xxx_drop_tpg,
	.fabric_post_link		= NULL,
	.fabric_pre_unlink		= NULL,
	.fabric_make_np			= NULL,
	.fabric_drop_np			= NULL,
	.fabric_make_nodeacl		= tcm_qla2xxx_make_nodeacl,
	.fabric_drop_nodeacl		= tcm_qla2xxx_drop_nodeacl,
2041 2042 2043 2044

	.tfc_wwn_attrs			= tcm_qla2xxx_wwn_attrs,
	.tfc_tpg_base_attrs		= tcm_qla2xxx_tpg_attrs,
	.tfc_tpg_attrib_attrs		= tcm_qla2xxx_tpg_attrib_attrs,
2045 2046
};

2047 2048 2049
static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
	.module				= THIS_MODULE,
	.name				= "qla2xxx_npiv",
2050 2051
	.get_fabric_name		= tcm_qla2xxx_npiv_get_fabric_name,
	.get_fabric_proto_ident		= tcm_qla2xxx_get_fabric_proto_ident,
2052
	.tpg_get_wwn			= tcm_qla2xxx_get_fabric_wwn,
2053 2054 2055 2056 2057
	.tpg_get_tag			= tcm_qla2xxx_get_tag,
	.tpg_get_default_depth		= tcm_qla2xxx_get_default_depth,
	.tpg_get_pr_transport_id	= tcm_qla2xxx_get_pr_transport_id,
	.tpg_get_pr_transport_id_len	= tcm_qla2xxx_get_pr_transport_id_len,
	.tpg_parse_pr_out_transport_id	= tcm_qla2xxx_parse_pr_out_transport_id,
2058 2059 2060 2061 2062
	.tpg_check_demo_mode		= tcm_qla2xxx_check_demo_mode,
	.tpg_check_demo_mode_cache	= tcm_qla2xxx_check_demo_mode_cache,
	.tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
	.tpg_check_prod_mode_write_protect =
	    tcm_qla2xxx_check_prod_write_protect,
2063
	.tpg_check_demo_mode_login_only	= tcm_qla2xxx_check_demo_mode_login_only,
2064 2065 2066
	.tpg_alloc_fabric_acl		= tcm_qla2xxx_alloc_fabric_acl,
	.tpg_release_fabric_acl		= tcm_qla2xxx_release_fabric_acl,
	.tpg_get_inst_index		= tcm_qla2xxx_tpg_get_inst_index,
2067
	.check_stop_free                = tcm_qla2xxx_check_stop_free,
2068
	.release_cmd			= tcm_qla2xxx_release_cmd,
2069
	.put_session			= tcm_qla2xxx_put_session,
2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081
	.shutdown_session		= tcm_qla2xxx_shutdown_session,
	.close_session			= tcm_qla2xxx_close_session,
	.sess_get_index			= tcm_qla2xxx_sess_get_index,
	.sess_get_initiator_sid		= NULL,
	.write_pending			= tcm_qla2xxx_write_pending,
	.write_pending_status		= tcm_qla2xxx_write_pending_status,
	.set_default_node_attributes	= tcm_qla2xxx_set_default_node_attrs,
	.get_task_tag			= tcm_qla2xxx_get_task_tag,
	.get_cmd_state			= tcm_qla2xxx_get_cmd_state,
	.queue_data_in			= tcm_qla2xxx_queue_data_in,
	.queue_status			= tcm_qla2xxx_queue_status,
	.queue_tm_rsp			= tcm_qla2xxx_queue_tm_rsp,
2082
	.aborted_task			= tcm_qla2xxx_aborted_task,
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
	/*
	 * Setup function pointers for generic logic in
	 * target_core_fabric_configfs.c
	 */
	.fabric_make_wwn		= tcm_qla2xxx_npiv_make_lport,
	.fabric_drop_wwn		= tcm_qla2xxx_npiv_drop_lport,
	.fabric_make_tpg		= tcm_qla2xxx_npiv_make_tpg,
	.fabric_drop_tpg		= tcm_qla2xxx_drop_tpg,
	.fabric_post_link		= NULL,
	.fabric_pre_unlink		= NULL,
	.fabric_make_np			= NULL,
	.fabric_drop_np			= NULL,
	.fabric_make_nodeacl		= tcm_qla2xxx_make_nodeacl,
	.fabric_drop_nodeacl		= tcm_qla2xxx_drop_nodeacl,
2097 2098 2099

	.tfc_wwn_attrs			= tcm_qla2xxx_wwn_attrs,
	.tfc_tpg_base_attrs		= tcm_qla2xxx_npiv_tpg_attrs,
2100 2101 2102 2103 2104 2105 2106 2107 2108
};

static int tcm_qla2xxx_register_configfs(void)
{
	int ret;

	pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
	    UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
	    utsname()->machine);
2109 2110 2111

	ret = target_register_template(&tcm_qla2xxx_ops);
	if (ret)
2112 2113
		return ret;

2114 2115
	ret = target_register_template(&tcm_qla2xxx_npiv_ops);
	if (ret)
2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
		goto out_fabric;

	tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
						WQ_MEM_RECLAIM, 0);
	if (!tcm_qla2xxx_free_wq) {
		ret = -ENOMEM;
		goto out_fabric_npiv;
	}

	tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
	if (!tcm_qla2xxx_cmd_wq) {
		ret = -ENOMEM;
		goto out_free_wq;
	}

	return 0;

out_free_wq:
	destroy_workqueue(tcm_qla2xxx_free_wq);
out_fabric_npiv:
2136
	target_unregister_template(&tcm_qla2xxx_npiv_ops);
2137
out_fabric:
2138
	target_unregister_template(&tcm_qla2xxx_ops);
2139 2140 2141 2142 2143 2144 2145 2146
	return ret;
}

static void tcm_qla2xxx_deregister_configfs(void)
{
	destroy_workqueue(tcm_qla2xxx_cmd_wq);
	destroy_workqueue(tcm_qla2xxx_free_wq);

2147 2148
	target_unregister_template(&tcm_qla2xxx_ops);
	target_unregister_template(&tcm_qla2xxx_npiv_ops);
2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
}

static int __init tcm_qla2xxx_init(void)
{
	int ret;

	ret = tcm_qla2xxx_register_configfs();
	if (ret < 0)
		return ret;

	return 0;
}

static void __exit tcm_qla2xxx_exit(void)
{
	tcm_qla2xxx_deregister_configfs();
}

MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
MODULE_LICENSE("GPL");
module_init(tcm_qla2xxx_init);
module_exit(tcm_qla2xxx_exit);