xref: /linux/drivers/scsi/qla2xxx/tcm_qla2xxx.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
275f8c1f6SNicholas Bellinger /*******************************************************************************
375f8c1f6SNicholas Bellinger  * This file contains tcm implementation using v4 configfs fabric infrastructure
475f8c1f6SNicholas Bellinger  * for QLogic target mode HBAs
575f8c1f6SNicholas Bellinger  *
64c76251eSNicholas Bellinger  * (c) Copyright 2010-2013 Datera, Inc.
775f8c1f6SNicholas Bellinger  *
84c76251eSNicholas Bellinger  * Author: Nicholas A. Bellinger <nab@daterainc.com>
975f8c1f6SNicholas Bellinger  *
1075f8c1f6SNicholas Bellinger  * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
1175f8c1f6SNicholas Bellinger  * the TCM_FC / Open-FCoE.org fabric module.
1275f8c1f6SNicholas Bellinger  *
1375f8c1f6SNicholas Bellinger  * Copyright (c) 2010 Cisco Systems, Inc
1475f8c1f6SNicholas Bellinger  *
1575f8c1f6SNicholas Bellinger  ****************************************************************************/
1675f8c1f6SNicholas Bellinger 
1775f8c1f6SNicholas Bellinger 
1875f8c1f6SNicholas Bellinger #include <linux/module.h>
1975f8c1f6SNicholas Bellinger #include <linux/utsname.h>
205538d294SDavid S. Miller #include <linux/vmalloc.h>
2175f8c1f6SNicholas Bellinger #include <linux/list.h>
2275f8c1f6SNicholas Bellinger #include <linux/slab.h>
2375f8c1f6SNicholas Bellinger #include <linux/types.h>
2475f8c1f6SNicholas Bellinger #include <linux/string.h>
2575f8c1f6SNicholas Bellinger #include <linux/configfs.h>
2675f8c1f6SNicholas Bellinger #include <linux/ctype.h>
2775f8c1f6SNicholas Bellinger #include <asm/unaligned.h>
2875f8c1f6SNicholas Bellinger #include <scsi/scsi_host.h>
2975f8c1f6SNicholas Bellinger #include <target/target_core_base.h>
3075f8c1f6SNicholas Bellinger #include <target/target_core_fabric.h>
3175f8c1f6SNicholas Bellinger 
3275f8c1f6SNicholas Bellinger #include "qla_def.h"
3375f8c1f6SNicholas Bellinger #include "qla_target.h"
3475f8c1f6SNicholas Bellinger #include "tcm_qla2xxx.h"
3575f8c1f6SNicholas Bellinger 
364d6609c4SHimanshu Madhani static struct workqueue_struct *tcm_qla2xxx_free_wq;
374d6609c4SHimanshu Madhani 
3875f8c1f6SNicholas Bellinger /*
3975f8c1f6SNicholas Bellinger  * Parse WWN.
4075f8c1f6SNicholas Bellinger  * If strict, we require lower-case hex and colon separators to be sure
4175f8c1f6SNicholas Bellinger  * the name is the same as what would be generated by ft_format_wwn()
4275f8c1f6SNicholas Bellinger  * so the name and wwn are mapped one-to-one.
4375f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_parse_wwn(const char * name,u64 * wwn,int strict)4475f8c1f6SNicholas Bellinger static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
4575f8c1f6SNicholas Bellinger {
4675f8c1f6SNicholas Bellinger 	const char *cp;
4775f8c1f6SNicholas Bellinger 	char c;
4875f8c1f6SNicholas Bellinger 	u32 nibble;
4975f8c1f6SNicholas Bellinger 	u32 byte = 0;
5075f8c1f6SNicholas Bellinger 	u32 pos = 0;
5175f8c1f6SNicholas Bellinger 	u32 err;
5275f8c1f6SNicholas Bellinger 
5375f8c1f6SNicholas Bellinger 	*wwn = 0;
5475f8c1f6SNicholas Bellinger 	for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
5575f8c1f6SNicholas Bellinger 		c = *cp;
5675f8c1f6SNicholas Bellinger 		if (c == '\n' && cp[1] == '\0')
5775f8c1f6SNicholas Bellinger 			continue;
5875f8c1f6SNicholas Bellinger 		if (strict && pos++ == 2 && byte++ < 7) {
5975f8c1f6SNicholas Bellinger 			pos = 0;
6075f8c1f6SNicholas Bellinger 			if (c == ':')
6175f8c1f6SNicholas Bellinger 				continue;
6275f8c1f6SNicholas Bellinger 			err = 1;
6375f8c1f6SNicholas Bellinger 			goto fail;
6475f8c1f6SNicholas Bellinger 		}
6575f8c1f6SNicholas Bellinger 		if (c == '\0') {
6675f8c1f6SNicholas Bellinger 			err = 2;
6775f8c1f6SNicholas Bellinger 			if (strict && byte != 8)
6875f8c1f6SNicholas Bellinger 				goto fail;
6975f8c1f6SNicholas Bellinger 			return cp - name;
7075f8c1f6SNicholas Bellinger 		}
7175f8c1f6SNicholas Bellinger 		err = 3;
7275f8c1f6SNicholas Bellinger 		if (isdigit(c))
7375f8c1f6SNicholas Bellinger 			nibble = c - '0';
7475f8c1f6SNicholas Bellinger 		else if (isxdigit(c) && (islower(c) || !strict))
7575f8c1f6SNicholas Bellinger 			nibble = tolower(c) - 'a' + 10;
7675f8c1f6SNicholas Bellinger 		else
7775f8c1f6SNicholas Bellinger 			goto fail;
7875f8c1f6SNicholas Bellinger 		*wwn = (*wwn << 4) | nibble;
7975f8c1f6SNicholas Bellinger 	}
8075f8c1f6SNicholas Bellinger 	err = 4;
8175f8c1f6SNicholas Bellinger fail:
8275f8c1f6SNicholas Bellinger 	pr_debug("err %u len %zu pos %u byte %u\n",
8375f8c1f6SNicholas Bellinger 			err, cp - name, pos, byte);
8475f8c1f6SNicholas Bellinger 	return -1;
8575f8c1f6SNicholas Bellinger }
8675f8c1f6SNicholas Bellinger 
tcm_qla2xxx_format_wwn(char * buf,size_t len,u64 wwn)8775f8c1f6SNicholas Bellinger static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
8875f8c1f6SNicholas Bellinger {
8975f8c1f6SNicholas Bellinger 	u8 b[8];
9075f8c1f6SNicholas Bellinger 
9175f8c1f6SNicholas Bellinger 	put_unaligned_be64(wwn, b);
9275f8c1f6SNicholas Bellinger 	return snprintf(buf, len,
9375f8c1f6SNicholas Bellinger 		"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
9475f8c1f6SNicholas Bellinger 		b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
9575f8c1f6SNicholas Bellinger }
9675f8c1f6SNicholas Bellinger 
9775f8c1f6SNicholas Bellinger /*
9875f8c1f6SNicholas Bellinger  * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
9975f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_npiv_extract_wwn(const char * ns,u64 * nm)10075f8c1f6SNicholas Bellinger static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
10175f8c1f6SNicholas Bellinger {
102d4f75b56SRoland Dreier 	unsigned int i, j;
10375f8c1f6SNicholas Bellinger 	u8 wwn[8];
10475f8c1f6SNicholas Bellinger 
10575f8c1f6SNicholas Bellinger 	memset(wwn, 0, sizeof(wwn));
10675f8c1f6SNicholas Bellinger 
10775f8c1f6SNicholas Bellinger 	/* Validate and store the new name */
10875f8c1f6SNicholas Bellinger 	for (i = 0, j = 0; i < 16; i++) {
109d4f75b56SRoland Dreier 		int value;
110d4f75b56SRoland Dreier 
11175f8c1f6SNicholas Bellinger 		value = hex_to_bin(*ns++);
11275f8c1f6SNicholas Bellinger 		if (value >= 0)
11375f8c1f6SNicholas Bellinger 			j = (j << 4) | value;
11475f8c1f6SNicholas Bellinger 		else
11575f8c1f6SNicholas Bellinger 			return -EINVAL;
11675f8c1f6SNicholas Bellinger 
11775f8c1f6SNicholas Bellinger 		if (i % 2) {
11875f8c1f6SNicholas Bellinger 			wwn[i/2] = j & 0xff;
11975f8c1f6SNicholas Bellinger 			j = 0;
12075f8c1f6SNicholas Bellinger 		}
12175f8c1f6SNicholas Bellinger 	}
12275f8c1f6SNicholas Bellinger 
12375f8c1f6SNicholas Bellinger 	*nm = wwn_to_u64(wwn);
12475f8c1f6SNicholas Bellinger 	return 0;
12575f8c1f6SNicholas Bellinger }
12675f8c1f6SNicholas Bellinger 
12775f8c1f6SNicholas Bellinger /*
12875f8c1f6SNicholas Bellinger  * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
12975f8c1f6SNicholas Bellinger  * store_fc_host_vport_create()
13075f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_npiv_parse_wwn(const char * name,size_t count,u64 * wwpn,u64 * wwnn)13175f8c1f6SNicholas Bellinger static int tcm_qla2xxx_npiv_parse_wwn(
13275f8c1f6SNicholas Bellinger 	const char *name,
13375f8c1f6SNicholas Bellinger 	size_t count,
13475f8c1f6SNicholas Bellinger 	u64 *wwpn,
13575f8c1f6SNicholas Bellinger 	u64 *wwnn)
13675f8c1f6SNicholas Bellinger {
13775f8c1f6SNicholas Bellinger 	unsigned int cnt = count;
13875f8c1f6SNicholas Bellinger 	int rc;
13975f8c1f6SNicholas Bellinger 
14075f8c1f6SNicholas Bellinger 	*wwpn = 0;
14175f8c1f6SNicholas Bellinger 	*wwnn = 0;
14275f8c1f6SNicholas Bellinger 
14375f8c1f6SNicholas Bellinger 	/* count may include a LF at end of string */
1440e8cd71cSSaurav Kashyap 	if (name[cnt-1] == '\n' || name[cnt-1] == 0)
14575f8c1f6SNicholas Bellinger 		cnt--;
14675f8c1f6SNicholas Bellinger 
14775f8c1f6SNicholas Bellinger 	/* validate we have enough characters for WWPN */
14875f8c1f6SNicholas Bellinger 	if ((cnt != (16+1+16)) || (name[16] != ':'))
14975f8c1f6SNicholas Bellinger 		return -EINVAL;
15075f8c1f6SNicholas Bellinger 
15175f8c1f6SNicholas Bellinger 	rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
15275f8c1f6SNicholas Bellinger 	if (rc != 0)
15375f8c1f6SNicholas Bellinger 		return rc;
15475f8c1f6SNicholas Bellinger 
15575f8c1f6SNicholas Bellinger 	rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
15675f8c1f6SNicholas Bellinger 	if (rc != 0)
15775f8c1f6SNicholas Bellinger 		return rc;
15875f8c1f6SNicholas Bellinger 
15975f8c1f6SNicholas Bellinger 	return 0;
16075f8c1f6SNicholas Bellinger }
16175f8c1f6SNicholas Bellinger 
tcm_qla2xxx_get_fabric_wwn(struct se_portal_group * se_tpg)16275f8c1f6SNicholas Bellinger static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
16375f8c1f6SNicholas Bellinger {
16475f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
16575f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
16675f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = tpg->lport;
16775f8c1f6SNicholas Bellinger 
168c046aa0fSRoland Dreier 	return lport->lport_naa_name;
16975f8c1f6SNicholas Bellinger }
17075f8c1f6SNicholas Bellinger 
tcm_qla2xxx_get_tag(struct se_portal_group * se_tpg)17175f8c1f6SNicholas Bellinger static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
17275f8c1f6SNicholas Bellinger {
17375f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
17475f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
17575f8c1f6SNicholas Bellinger 	return tpg->lport_tpgt;
17675f8c1f6SNicholas Bellinger }
17775f8c1f6SNicholas Bellinger 
tcm_qla2xxx_check_demo_mode(struct se_portal_group * se_tpg)17875f8c1f6SNicholas Bellinger static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
17975f8c1f6SNicholas Bellinger {
18075f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
18175f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
18275f8c1f6SNicholas Bellinger 
183a309f489SAndy Grover 	return tpg->tpg_attrib.generate_node_acls;
18475f8c1f6SNicholas Bellinger }
18575f8c1f6SNicholas Bellinger 
tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group * se_tpg)18675f8c1f6SNicholas Bellinger static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
18775f8c1f6SNicholas Bellinger {
18875f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
18975f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
19075f8c1f6SNicholas Bellinger 
191a309f489SAndy Grover 	return tpg->tpg_attrib.cache_dynamic_acls;
19275f8c1f6SNicholas Bellinger }
19375f8c1f6SNicholas Bellinger 
tcm_qla2xxx_check_demo_write_protect(struct se_portal_group * se_tpg)19475f8c1f6SNicholas Bellinger static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
19575f8c1f6SNicholas Bellinger {
19675f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
19775f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
19875f8c1f6SNicholas Bellinger 
199a309f489SAndy Grover 	return tpg->tpg_attrib.demo_mode_write_protect;
20075f8c1f6SNicholas Bellinger }
20175f8c1f6SNicholas Bellinger 
tcm_qla2xxx_check_prod_write_protect(struct se_portal_group * se_tpg)20275f8c1f6SNicholas Bellinger static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
20375f8c1f6SNicholas Bellinger {
20475f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
20575f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
20675f8c1f6SNicholas Bellinger 
207a309f489SAndy Grover 	return tpg->tpg_attrib.prod_mode_write_protect;
20875f8c1f6SNicholas Bellinger }
20975f8c1f6SNicholas Bellinger 
tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group * se_tpg)210de04a8aaSAndy Grover static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
211de04a8aaSAndy Grover {
212de04a8aaSAndy Grover 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
213de04a8aaSAndy Grover 				struct tcm_qla2xxx_tpg, se_tpg);
214de04a8aaSAndy Grover 
215a309f489SAndy Grover 	return tpg->tpg_attrib.demo_mode_login_only;
216de04a8aaSAndy Grover }
217de04a8aaSAndy Grover 
tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group * se_tpg)21864b16887SNicholas Bellinger static int tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group *se_tpg)
21964b16887SNicholas Bellinger {
22064b16887SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
22164b16887SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
22264b16887SNicholas Bellinger 
22364b16887SNicholas Bellinger 	return tpg->tpg_attrib.fabric_prot_type;
22464b16887SNicholas Bellinger }
22564b16887SNicholas Bellinger 
tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group * se_tpg)22675f8c1f6SNicholas Bellinger static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
22775f8c1f6SNicholas Bellinger {
22875f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
22975f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
23075f8c1f6SNicholas Bellinger 
23175f8c1f6SNicholas Bellinger 	return tpg->lport_tpgt;
23275f8c1f6SNicholas Bellinger }
23375f8c1f6SNicholas Bellinger 
tcm_qla2xxx_complete_mcmd(struct work_struct * work)23475f8c1f6SNicholas Bellinger static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
23575f8c1f6SNicholas Bellinger {
23675f8c1f6SNicholas Bellinger 	struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
23775f8c1f6SNicholas Bellinger 			struct qla_tgt_mgmt_cmd, free_work);
23875f8c1f6SNicholas Bellinger 
23975f8c1f6SNicholas Bellinger 	transport_generic_free_cmd(&mcmd->se_cmd, 0);
24075f8c1f6SNicholas Bellinger }
24175f8c1f6SNicholas Bellinger 
24275f8c1f6SNicholas Bellinger /*
24375f8c1f6SNicholas Bellinger  * Called from qla_target_template->free_mcmd(), and will call
24475f8c1f6SNicholas Bellinger  * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
24575f8c1f6SNicholas Bellinger  * release callback.  qla_hw_data->hardware_lock is expected to be held
24675f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd * mcmd)24775f8c1f6SNicholas Bellinger static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
24875f8c1f6SNicholas Bellinger {
249f2c9ee54SRoman Bolshakov 	if (!mcmd)
250f2c9ee54SRoman Bolshakov 		return;
25175f8c1f6SNicholas Bellinger 	INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
25275f8c1f6SNicholas Bellinger 	queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
25375f8c1f6SNicholas Bellinger }
25475f8c1f6SNicholas Bellinger 
tcm_qla2xxx_complete_free(struct work_struct * work)25575f8c1f6SNicholas Bellinger static void tcm_qla2xxx_complete_free(struct work_struct *work)
25675f8c1f6SNicholas Bellinger {
25775f8c1f6SNicholas Bellinger 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
258605e7402SMike Christie 	unsigned long flags;
25975f8c1f6SNicholas Bellinger 
260e07f8f65SSaurav Kashyap 	cmd->cmd_in_wq = 0;
261e07f8f65SSaurav Kashyap 
2621eb42f96SQuinn Tran 	WARN_ON(cmd->trc_flags & TRC_CMD_FREE);
263e07f8f65SSaurav Kashyap 
2641b1e68d2SBart Van Assche 	/* To do: protect all tgt_counters manipulations with proper locking. */
26560a9eadbSQuinn Tran 	cmd->qpair->tgt_counters.qla_core_ret_sta_ctio++;
2661eb42f96SQuinn Tran 	cmd->trc_flags |= TRC_CMD_FREE;
267d594db01SQuinn Tran 	cmd->cmd_sent_to_fw = 0;
268d594db01SQuinn Tran 
269605e7402SMike Christie 	spin_lock_irqsave(&cmd->sess->sess_cmd_lock, flags);
270605e7402SMike Christie 	list_del_init(&cmd->sess_cmd_list);
271605e7402SMike Christie 	spin_unlock_irqrestore(&cmd->sess->sess_cmd_lock, flags);
272605e7402SMike Christie 
27375f8c1f6SNicholas Bellinger 	transport_generic_free_cmd(&cmd->se_cmd, 0);
27475f8c1f6SNicholas Bellinger }
27575f8c1f6SNicholas Bellinger 
tcm_qla2xxx_get_cmd(struct fc_port * sess)27680363e1bSBart Van Assche static struct qla_tgt_cmd *tcm_qla2xxx_get_cmd(struct fc_port *sess)
27780363e1bSBart Van Assche {
27880363e1bSBart Van Assche 	struct se_session *se_sess = sess->se_sess;
27980363e1bSBart Van Assche 	struct qla_tgt_cmd *cmd;
28080363e1bSBart Van Assche 	int tag, cpu;
28180363e1bSBart Van Assche 
28280363e1bSBart Van Assche 	tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
28380363e1bSBart Van Assche 	if (tag < 0)
28480363e1bSBart Van Assche 		return NULL;
28580363e1bSBart Van Assche 
28680363e1bSBart Van Assche 	cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
28780363e1bSBart Van Assche 	memset(cmd, 0, sizeof(struct qla_tgt_cmd));
28880363e1bSBart Van Assche 	cmd->se_cmd.map_tag = tag;
28980363e1bSBart Van Assche 	cmd->se_cmd.map_cpu = cpu;
29080363e1bSBart Van Assche 
29180363e1bSBart Van Assche 	return cmd;
29280363e1bSBart Van Assche }
29380363e1bSBart Van Assche 
tcm_qla2xxx_rel_cmd(struct qla_tgt_cmd * cmd)29480363e1bSBart Van Assche static void tcm_qla2xxx_rel_cmd(struct qla_tgt_cmd *cmd)
29580363e1bSBart Van Assche {
29680363e1bSBart Van Assche 	target_free_tag(cmd->sess->se_sess, &cmd->se_cmd);
29780363e1bSBart Van Assche }
29880363e1bSBart Van Assche 
29975f8c1f6SNicholas Bellinger /*
30075f8c1f6SNicholas Bellinger  * Called from qla_target_template->free_cmd(), and will call
30175f8c1f6SNicholas Bellinger  * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
30275f8c1f6SNicholas Bellinger  * release callback.  qla_hw_data->hardware_lock is expected to be held
30375f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_free_cmd(struct qla_tgt_cmd * cmd)30475f8c1f6SNicholas Bellinger static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
30575f8c1f6SNicholas Bellinger {
30660a9eadbSQuinn Tran 	cmd->qpair->tgt_counters.core_qla_free_cmd++;
307e07f8f65SSaurav Kashyap 	cmd->cmd_in_wq = 1;
308a07100e0SQuinn Tran 
3091eb42f96SQuinn Tran 	WARN_ON(cmd->trc_flags & TRC_CMD_DONE);
3101eb42f96SQuinn Tran 	cmd->trc_flags |= TRC_CMD_DONE;
311a07100e0SQuinn Tran 
31275f8c1f6SNicholas Bellinger 	INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
31359f10a05SNilesh Javali 	queue_work(tcm_qla2xxx_free_wq, &cmd->work);
31475f8c1f6SNicholas Bellinger }
31575f8c1f6SNicholas Bellinger 
31675f8c1f6SNicholas Bellinger /*
31775f8c1f6SNicholas Bellinger  * Called from struct target_core_fabric_ops->check_stop_free() context
31875f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_check_stop_free(struct se_cmd * se_cmd)31975f8c1f6SNicholas Bellinger static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
32075f8c1f6SNicholas Bellinger {
321e07f8f65SSaurav Kashyap 	struct qla_tgt_cmd *cmd;
322e07f8f65SSaurav Kashyap 
323e07f8f65SSaurav Kashyap 	if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
324e07f8f65SSaurav Kashyap 		cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
3251eb42f96SQuinn Tran 		cmd->trc_flags |= TRC_CMD_CHK_STOP;
326e07f8f65SSaurav Kashyap 	}
327e07f8f65SSaurav Kashyap 
328afc16604SBart Van Assche 	return target_put_sess_cmd(se_cmd);
32975f8c1f6SNicholas Bellinger }
33075f8c1f6SNicholas Bellinger 
33175f8c1f6SNicholas Bellinger /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
33275f8c1f6SNicholas Bellinger  * fabric descriptor @se_cmd command to release
33375f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_release_cmd(struct se_cmd * se_cmd)33475f8c1f6SNicholas Bellinger static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
33575f8c1f6SNicholas Bellinger {
33675f8c1f6SNicholas Bellinger 	struct qla_tgt_cmd *cmd;
33775f8c1f6SNicholas Bellinger 
33875f8c1f6SNicholas Bellinger 	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
33975f8c1f6SNicholas Bellinger 		struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
34075f8c1f6SNicholas Bellinger 				struct qla_tgt_mgmt_cmd, se_cmd);
34175f8c1f6SNicholas Bellinger 		qlt_free_mcmd(mcmd);
34275f8c1f6SNicholas Bellinger 		return;
34375f8c1f6SNicholas Bellinger 	}
34475f8c1f6SNicholas Bellinger 	cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
345d594db01SQuinn Tran 
346b1e261d4SBart Van Assche 	if (WARN_ON(cmd->cmd_sent_to_fw))
347b1e261d4SBart Van Assche 		return;
348b1e261d4SBart Van Assche 
34975f8c1f6SNicholas Bellinger 	qlt_free_cmd(cmd);
35075f8c1f6SNicholas Bellinger }
35175f8c1f6SNicholas Bellinger 
tcm_qla2xxx_release_session(struct kref * kref)3525d964837SQuinn Tran static void tcm_qla2xxx_release_session(struct kref *kref)
3535d964837SQuinn Tran {
3545d964837SQuinn Tran 	struct fc_port  *sess = container_of(kref,
3555d964837SQuinn Tran 	    struct fc_port, sess_kref);
3565d964837SQuinn Tran 
3575d964837SQuinn Tran 	qlt_unreg_sess(sess);
3585d964837SQuinn Tran }
3595d964837SQuinn Tran 
tcm_qla2xxx_put_sess(struct fc_port * sess)3605d964837SQuinn Tran static void tcm_qla2xxx_put_sess(struct fc_port *sess)
3615d964837SQuinn Tran {
3625d964837SQuinn Tran 	if (!sess)
3635d964837SQuinn Tran 		return;
3645d964837SQuinn Tran 
3655d964837SQuinn Tran 	kref_put(&sess->sess_kref, tcm_qla2xxx_release_session);
3665d964837SQuinn Tran }
3675d964837SQuinn Tran 
tcm_qla2xxx_close_session(struct se_session * se_sess)36875f8c1f6SNicholas Bellinger static void tcm_qla2xxx_close_session(struct se_session *se_sess)
36975f8c1f6SNicholas Bellinger {
3705d964837SQuinn Tran 	struct fc_port *sess = se_sess->fabric_sess_ptr;
37175f8c1f6SNicholas Bellinger 
37275f8c1f6SNicholas Bellinger 	BUG_ON(!sess);
37375f8c1f6SNicholas Bellinger 
3746f55b06fSMike Christie 	target_stop_session(se_sess);
375d4023db7SBart Van Assche 
37686196a8fSQuinn Tran 	sess->explicit_logout = 1;
377d4023db7SBart Van Assche 	tcm_qla2xxx_put_sess(sess);
37875f8c1f6SNicholas Bellinger }
37975f8c1f6SNicholas Bellinger 
tcm_qla2xxx_write_pending(struct se_cmd * se_cmd)38075f8c1f6SNicholas Bellinger static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
38175f8c1f6SNicholas Bellinger {
38275f8c1f6SNicholas Bellinger 	struct qla_tgt_cmd *cmd = container_of(se_cmd,
38375f8c1f6SNicholas Bellinger 				struct qla_tgt_cmd, se_cmd);
384a07100e0SQuinn Tran 
385a07100e0SQuinn Tran 	if (cmd->aborted) {
386a07100e0SQuinn Tran 		/* Cmd can loop during Q-full.  tcm_qla2xxx_aborted_task
387a07100e0SQuinn Tran 		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
388a07100e0SQuinn Tran 		 * already kick start the free.
389a07100e0SQuinn Tran 		 */
390a07100e0SQuinn Tran 		pr_debug("write_pending aborted cmd[%p] refcount %d "
391a07100e0SQuinn Tran 			"transport_state %x, t_state %x, se_cmd_flags %x\n",
3922c935bc5SPeter Zijlstra 			cmd, kref_read(&cmd->se_cmd.cmd_kref),
393a07100e0SQuinn Tran 			cmd->se_cmd.transport_state,
394a07100e0SQuinn Tran 			cmd->se_cmd.t_state,
395a07100e0SQuinn Tran 			cmd->se_cmd.se_cmd_flags);
396e209783dSBart Van Assche 		transport_generic_request_failure(&cmd->se_cmd,
397e209783dSBart Van Assche 			TCM_CHECK_CONDITION_ABORT_CMD);
398a07100e0SQuinn Tran 		return 0;
399a07100e0SQuinn Tran 	}
4001eb42f96SQuinn Tran 	cmd->trc_flags |= TRC_XFR_RDY;
40175f8c1f6SNicholas Bellinger 	cmd->bufflen = se_cmd->data_length;
402b3faa2e8SNicholas Bellinger 	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
40375f8c1f6SNicholas Bellinger 
40475f8c1f6SNicholas Bellinger 	cmd->sg_cnt = se_cmd->t_data_nents;
40575f8c1f6SNicholas Bellinger 	cmd->sg = se_cmd->t_data_sg;
40675f8c1f6SNicholas Bellinger 
407f83adb61SQuinn Tran 	cmd->prot_sg_cnt = se_cmd->t_prot_nents;
408f83adb61SQuinn Tran 	cmd->prot_sg = se_cmd->t_prot_sg;
409f83adb61SQuinn Tran 	cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
410f83adb61SQuinn Tran 	se_cmd->pi_err = 0;
411f83adb61SQuinn Tran 
41275f8c1f6SNicholas Bellinger 	/*
413e7d0bb77SChristoph Hellwig 	 * qla_target.c:qlt_rdy_to_xfer() will call dma_map_sg() to setup
41475f8c1f6SNicholas Bellinger 	 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
41575f8c1f6SNicholas Bellinger 	 */
41675f8c1f6SNicholas Bellinger 	return qlt_rdy_to_xfer(cmd);
41775f8c1f6SNicholas Bellinger }
41875f8c1f6SNicholas Bellinger 
tcm_qla2xxx_get_cmd_state(struct se_cmd * se_cmd)41975f8c1f6SNicholas Bellinger static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
42075f8c1f6SNicholas Bellinger {
4215155ce5fSDilip Kumar Uppugandla 	if (!(se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
4225155ce5fSDilip Kumar Uppugandla 		struct qla_tgt_cmd *cmd = container_of(se_cmd,
4235155ce5fSDilip Kumar Uppugandla 				struct qla_tgt_cmd, se_cmd);
4245155ce5fSDilip Kumar Uppugandla 		return cmd->state;
4255155ce5fSDilip Kumar Uppugandla 	}
4265155ce5fSDilip Kumar Uppugandla 
42775f8c1f6SNicholas Bellinger 	return 0;
42875f8c1f6SNicholas Bellinger }
42975f8c1f6SNicholas Bellinger 
43075f8c1f6SNicholas Bellinger /*
43175f8c1f6SNicholas Bellinger  * Called from process context in qla_target.c:qlt_do_work() code
43275f8c1f6SNicholas Bellinger  */
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)43375f8c1f6SNicholas Bellinger static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
43475f8c1f6SNicholas Bellinger 	unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
43575f8c1f6SNicholas Bellinger 	int data_dir, int bidi)
43675f8c1f6SNicholas Bellinger {
43775f8c1f6SNicholas Bellinger 	struct se_cmd *se_cmd = &cmd->se_cmd;
43875f8c1f6SNicholas Bellinger 	struct se_session *se_sess;
4395d964837SQuinn Tran 	struct fc_port *sess;
44054a5e73fSLaurence Oberman #ifdef CONFIG_TCM_QLA2XXX_DEBUG
44154a5e73fSLaurence Oberman 	struct se_portal_group *se_tpg;
44254a5e73fSLaurence Oberman 	struct tcm_qla2xxx_tpg *tpg;
44354a5e73fSLaurence Oberman #endif
444919ba0adSMike Christie 	int rc, target_flags = TARGET_SCF_ACK_KREF;
445605e7402SMike Christie 	unsigned long flags;
44675f8c1f6SNicholas Bellinger 
44775f8c1f6SNicholas Bellinger 	if (bidi)
448605e7402SMike Christie 		target_flags |= TARGET_SCF_BIDI_OP;
44975f8c1f6SNicholas Bellinger 
4505327c7dbSQuinn Tran 	if (se_cmd->cpuid != WORK_CPU_UNBOUND)
451605e7402SMike Christie 		target_flags |= TARGET_SCF_USE_CPUID;
4525327c7dbSQuinn Tran 
45375f8c1f6SNicholas Bellinger 	sess = cmd->sess;
45475f8c1f6SNicholas Bellinger 	if (!sess) {
4555d964837SQuinn Tran 		pr_err("Unable to locate struct fc_port from qla_tgt_cmd\n");
45675f8c1f6SNicholas Bellinger 		return -EINVAL;
45775f8c1f6SNicholas Bellinger 	}
45875f8c1f6SNicholas Bellinger 
45975f8c1f6SNicholas Bellinger 	se_sess = sess->se_sess;
46075f8c1f6SNicholas Bellinger 	if (!se_sess) {
46175f8c1f6SNicholas Bellinger 		pr_err("Unable to locate active struct se_session\n");
46275f8c1f6SNicholas Bellinger 		return -EINVAL;
46375f8c1f6SNicholas Bellinger 	}
46475f8c1f6SNicholas Bellinger 
46554a5e73fSLaurence Oberman #ifdef CONFIG_TCM_QLA2XXX_DEBUG
46654a5e73fSLaurence Oberman 	se_tpg = se_sess->se_tpg;
46754a5e73fSLaurence Oberman 	tpg = container_of(se_tpg, struct tcm_qla2xxx_tpg, se_tpg);
46854a5e73fSLaurence Oberman 	if (unlikely(tpg->tpg_attrib.jam_host)) {
46954a5e73fSLaurence Oberman 		/* return, and dont run target_submit_cmd,discarding command */
47054a5e73fSLaurence Oberman 		return 0;
47154a5e73fSLaurence Oberman 	}
47254a5e73fSLaurence Oberman #endif
47360a9eadbSQuinn Tran 	cmd->qpair->tgt_counters.qla_core_sbt_cmd++;
474605e7402SMike Christie 
475605e7402SMike Christie 	spin_lock_irqsave(&sess->sess_cmd_lock, flags);
476605e7402SMike Christie 	list_add_tail(&cmd->sess_cmd_list, &sess->sess_cmd_list);
477605e7402SMike Christie 	spin_unlock_irqrestore(&sess->sess_cmd_lock, flags);
478605e7402SMike Christie 
479919ba0adSMike Christie 	rc = target_init_cmd(se_cmd, se_sess, &cmd->sense_buffer[0],
48075f8c1f6SNicholas Bellinger 			     cmd->unpacked_lun, data_length, fcp_task_attr,
481605e7402SMike Christie 			     data_dir, target_flags);
482919ba0adSMike Christie 	if (rc)
483919ba0adSMike Christie 		return rc;
484919ba0adSMike Christie 
48508694199SMike Christie 	if (target_submit_prep(se_cmd, cdb, NULL, 0, NULL, 0, NULL, 0,
48608694199SMike Christie 			       GFP_KERNEL))
487919ba0adSMike Christie 		return 0;
488919ba0adSMike Christie 
489919ba0adSMike Christie 	target_submit(se_cmd);
490919ba0adSMike Christie 	return 0;
49175f8c1f6SNicholas Bellinger }
49275f8c1f6SNicholas Bellinger 
tcm_qla2xxx_handle_data_work(struct work_struct * work)49343381ce8SChristoph Hellwig static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
49475f8c1f6SNicholas Bellinger {
49575f8c1f6SNicholas Bellinger 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
49675f8c1f6SNicholas Bellinger 
49775f8c1f6SNicholas Bellinger 	/*
49875f8c1f6SNicholas Bellinger 	 * Ensure that the complete FCP WRITE payload has been received.
49975f8c1f6SNicholas Bellinger 	 * Otherwise return an exception via CHECK_CONDITION status.
50075f8c1f6SNicholas Bellinger 	 */
501e07f8f65SSaurav Kashyap 	cmd->cmd_in_wq = 0;
502d594db01SQuinn Tran 	cmd->cmd_sent_to_fw = 0;
503d594db01SQuinn Tran 	if (cmd->aborted) {
504aefed3e5SBart Van Assche 		transport_generic_request_failure(&cmd->se_cmd,
505aefed3e5SBart Van Assche 			TCM_CHECK_CONDITION_ABORT_CMD);
506d594db01SQuinn Tran 		return;
507d594db01SQuinn Tran 	}
508d594db01SQuinn Tran 
50960a9eadbSQuinn Tran 	cmd->qpair->tgt_counters.qla_core_ret_ctio++;
51075f8c1f6SNicholas Bellinger 	if (!cmd->write_data_transferred) {
511be25152cSQuinn Tran 		switch (cmd->dif_err_code) {
512be25152cSQuinn Tran 		case DIF_ERR_GRD:
513be25152cSQuinn Tran 			cmd->se_cmd.pi_err =
514be25152cSQuinn Tran 			    TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
515be25152cSQuinn Tran 			break;
516be25152cSQuinn Tran 		case DIF_ERR_REF:
517be25152cSQuinn Tran 			cmd->se_cmd.pi_err =
518be25152cSQuinn Tran 			    TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
519be25152cSQuinn Tran 			break;
520be25152cSQuinn Tran 		case DIF_ERR_APP:
521be25152cSQuinn Tran 			cmd->se_cmd.pi_err =
522be25152cSQuinn Tran 			    TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
523be25152cSQuinn Tran 			break;
524be25152cSQuinn Tran 		case DIF_ERR_NONE:
525be25152cSQuinn Tran 		default:
526be25152cSQuinn Tran 			break;
527be25152cSQuinn Tran 		}
528be25152cSQuinn Tran 
529f83adb61SQuinn Tran 		if (cmd->se_cmd.pi_err)
530f83adb61SQuinn Tran 			transport_generic_request_failure(&cmd->se_cmd,
531f83adb61SQuinn Tran 				cmd->se_cmd.pi_err);
532f83adb61SQuinn Tran 		else
533de103c93SChristoph Hellwig 			transport_generic_request_failure(&cmd->se_cmd,
534de103c93SChristoph Hellwig 				TCM_CHECK_CONDITION_ABORT_CMD);
535f83adb61SQuinn Tran 
53643381ce8SChristoph Hellwig 		return;
53775f8c1f6SNicholas Bellinger 	}
53843381ce8SChristoph Hellwig 
53943381ce8SChristoph Hellwig 	return target_execute_cmd(&cmd->se_cmd);
54043381ce8SChristoph Hellwig }
54143381ce8SChristoph Hellwig 
54275f8c1f6SNicholas Bellinger /*
54343381ce8SChristoph Hellwig  * Called from qla_target.c:qlt_do_ctio_completion()
54475f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_handle_data(struct qla_tgt_cmd * cmd)54543381ce8SChristoph Hellwig static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
54643381ce8SChristoph Hellwig {
5471eb42f96SQuinn Tran 	cmd->trc_flags |= TRC_DATA_IN;
548e07f8f65SSaurav Kashyap 	cmd->cmd_in_wq = 1;
54943381ce8SChristoph Hellwig 	INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
55059f10a05SNilesh Javali 	queue_work(tcm_qla2xxx_free_wq, &cmd->work);
55175f8c1f6SNicholas Bellinger }
55275f8c1f6SNicholas Bellinger 
tcm_qla2xxx_chk_dif_tags(uint32_t tag)553be25152cSQuinn Tran static int tcm_qla2xxx_chk_dif_tags(uint32_t tag)
554f83adb61SQuinn Tran {
555be25152cSQuinn Tran 	return 0;
556f83adb61SQuinn Tran }
557f83adb61SQuinn Tran 
tcm_qla2xxx_dif_tags(struct qla_tgt_cmd * cmd,uint16_t * pfw_prot_opts)558be25152cSQuinn Tran static int tcm_qla2xxx_dif_tags(struct qla_tgt_cmd *cmd,
559be25152cSQuinn Tran     uint16_t *pfw_prot_opts)
560f83adb61SQuinn Tran {
561be25152cSQuinn Tran 	struct se_cmd *se_cmd = &cmd->se_cmd;
562be25152cSQuinn Tran 
563be25152cSQuinn Tran 	if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
564be25152cSQuinn Tran 		*pfw_prot_opts |= PO_DISABLE_GUARD_CHECK;
565be25152cSQuinn Tran 
566be25152cSQuinn Tran 	if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG))
567be25152cSQuinn Tran 		*pfw_prot_opts |= PO_DIS_APP_TAG_VALD;
568be25152cSQuinn Tran 
569be25152cSQuinn Tran 	return 0;
570f83adb61SQuinn Tran }
571f83adb61SQuinn Tran 
57275f8c1f6SNicholas Bellinger /*
57375f8c1f6SNicholas Bellinger  * Called from qla_target.c:qlt_issue_task_mgmt()
57475f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd * mcmd,u64 lun,uint16_t tmr_func,uint32_t tag)575f775bd14SQuinn Tran static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, u64 lun,
576be92fc3fSQuinn Tran 	uint16_t tmr_func, uint32_t tag)
57775f8c1f6SNicholas Bellinger {
5785d964837SQuinn Tran 	struct fc_port *sess = mcmd->sess;
57975f8c1f6SNicholas Bellinger 	struct se_cmd *se_cmd = &mcmd->se_cmd;
580be92fc3fSQuinn Tran 	int transl_tmr_func = 0;
581be92fc3fSQuinn Tran 
582be92fc3fSQuinn Tran 	switch (tmr_func) {
583be92fc3fSQuinn Tran 	case QLA_TGT_ABTS:
584be92fc3fSQuinn Tran 		pr_debug("%ld: ABTS received\n", sess->vha->host_no);
585be92fc3fSQuinn Tran 		transl_tmr_func = TMR_ABORT_TASK;
586be92fc3fSQuinn Tran 		break;
587be92fc3fSQuinn Tran 	case QLA_TGT_2G_ABORT_TASK:
588be92fc3fSQuinn Tran 		pr_debug("%ld: 2G Abort Task received\n", sess->vha->host_no);
589be92fc3fSQuinn Tran 		transl_tmr_func = TMR_ABORT_TASK;
590be92fc3fSQuinn Tran 		break;
591be92fc3fSQuinn Tran 	case QLA_TGT_CLEAR_ACA:
592be92fc3fSQuinn Tran 		pr_debug("%ld: CLEAR_ACA received\n", sess->vha->host_no);
593be92fc3fSQuinn Tran 		transl_tmr_func = TMR_CLEAR_ACA;
594be92fc3fSQuinn Tran 		break;
595be92fc3fSQuinn Tran 	case QLA_TGT_TARGET_RESET:
596be92fc3fSQuinn Tran 		pr_debug("%ld: TARGET_RESET received\n", sess->vha->host_no);
597be92fc3fSQuinn Tran 		transl_tmr_func = TMR_TARGET_WARM_RESET;
598be92fc3fSQuinn Tran 		break;
599be92fc3fSQuinn Tran 	case QLA_TGT_LUN_RESET:
600be92fc3fSQuinn Tran 		pr_debug("%ld: LUN_RESET received\n", sess->vha->host_no);
601be92fc3fSQuinn Tran 		transl_tmr_func = TMR_LUN_RESET;
602be92fc3fSQuinn Tran 		break;
603be92fc3fSQuinn Tran 	case QLA_TGT_CLEAR_TS:
604be92fc3fSQuinn Tran 		pr_debug("%ld: CLEAR_TS received\n", sess->vha->host_no);
605be92fc3fSQuinn Tran 		transl_tmr_func = TMR_CLEAR_TASK_SET;
606be92fc3fSQuinn Tran 		break;
607be92fc3fSQuinn Tran 	case QLA_TGT_ABORT_TS:
608be92fc3fSQuinn Tran 		pr_debug("%ld: ABORT_TS received\n", sess->vha->host_no);
609be92fc3fSQuinn Tran 		transl_tmr_func = TMR_ABORT_TASK_SET;
610be92fc3fSQuinn Tran 		break;
611be92fc3fSQuinn Tran 	default:
612be92fc3fSQuinn Tran 		pr_debug("%ld: Unknown task mgmt fn 0x%x\n",
613be92fc3fSQuinn Tran 		    sess->vha->host_no, tmr_func);
614be92fc3fSQuinn Tran 		return -ENOSYS;
615be92fc3fSQuinn Tran 	}
61675f8c1f6SNicholas Bellinger 
61775f8c1f6SNicholas Bellinger 	return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
6188f394da3SMike Christie 	    transl_tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
61975f8c1f6SNicholas Bellinger }
62075f8c1f6SNicholas Bellinger 
tcm_qla2xxx_find_cmd_by_tag(struct fc_port * sess,uint64_t tag)62184905dfeSQuinn Tran static struct qla_tgt_cmd *tcm_qla2xxx_find_cmd_by_tag(struct fc_port *sess,
62284905dfeSQuinn Tran     uint64_t tag)
62384905dfeSQuinn Tran {
624605e7402SMike Christie 	struct qla_tgt_cmd *cmd;
62584905dfeSQuinn Tran 	unsigned long flags;
62684905dfeSQuinn Tran 
62784905dfeSQuinn Tran 	if (!sess->se_sess)
62884905dfeSQuinn Tran 		return NULL;
62984905dfeSQuinn Tran 
630605e7402SMike Christie 	spin_lock_irqsave(&sess->sess_cmd_lock, flags);
631605e7402SMike Christie 	list_for_each_entry(cmd, &sess->sess_cmd_list, sess_cmd_list) {
632605e7402SMike Christie 		if (cmd->se_cmd.tag == tag)
633605e7402SMike Christie 			goto done;
63484905dfeSQuinn Tran 	}
635605e7402SMike Christie 	cmd = NULL;
636605e7402SMike Christie done:
637605e7402SMike Christie 	spin_unlock_irqrestore(&sess->sess_cmd_lock, flags);
63884905dfeSQuinn Tran 
63984905dfeSQuinn Tran 	return cmd;
64084905dfeSQuinn Tran }
64184905dfeSQuinn Tran 
tcm_qla2xxx_queue_data_in(struct se_cmd * se_cmd)64275f8c1f6SNicholas Bellinger static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
64375f8c1f6SNicholas Bellinger {
64475f8c1f6SNicholas Bellinger 	struct qla_tgt_cmd *cmd = container_of(se_cmd,
64575f8c1f6SNicholas Bellinger 				struct qla_tgt_cmd, se_cmd);
64675f8c1f6SNicholas Bellinger 
647a07100e0SQuinn Tran 	if (cmd->aborted) {
648a07100e0SQuinn Tran 		/* Cmd can loop during Q-full.  tcm_qla2xxx_aborted_task
649a07100e0SQuinn Tran 		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
650a07100e0SQuinn Tran 		 * already kick start the free.
651a07100e0SQuinn Tran 		 */
652a07100e0SQuinn Tran 		pr_debug("queue_data_in aborted cmd[%p] refcount %d "
653a07100e0SQuinn Tran 			"transport_state %x, t_state %x, se_cmd_flags %x\n",
6542c935bc5SPeter Zijlstra 			cmd, kref_read(&cmd->se_cmd.cmd_kref),
655a07100e0SQuinn Tran 			cmd->se_cmd.transport_state,
656a07100e0SQuinn Tran 			cmd->se_cmd.t_state,
657a07100e0SQuinn Tran 			cmd->se_cmd.se_cmd_flags);
658a07100e0SQuinn Tran 		return 0;
659a07100e0SQuinn Tran 	}
660a07100e0SQuinn Tran 
6611eb42f96SQuinn Tran 	cmd->trc_flags |= TRC_XMIT_DATA;
66275f8c1f6SNicholas Bellinger 	cmd->bufflen = se_cmd->data_length;
663b3faa2e8SNicholas Bellinger 	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
66475f8c1f6SNicholas Bellinger 
66575f8c1f6SNicholas Bellinger 	cmd->sg_cnt = se_cmd->t_data_nents;
66675f8c1f6SNicholas Bellinger 	cmd->sg = se_cmd->t_data_sg;
66775f8c1f6SNicholas Bellinger 	cmd->offset = 0;
66875f8c1f6SNicholas Bellinger 
669f83adb61SQuinn Tran 	cmd->prot_sg_cnt = se_cmd->t_prot_nents;
670f83adb61SQuinn Tran 	cmd->prot_sg = se_cmd->t_prot_sg;
671f83adb61SQuinn Tran 	cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
672f83adb61SQuinn Tran 	se_cmd->pi_err = 0;
673f83adb61SQuinn Tran 
67475f8c1f6SNicholas Bellinger 	/*
67575f8c1f6SNicholas Bellinger 	 * Now queue completed DATA_IN the qla2xxx LLD and response ring
67675f8c1f6SNicholas Bellinger 	 */
67775f8c1f6SNicholas Bellinger 	return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
67875f8c1f6SNicholas Bellinger 				se_cmd->scsi_status);
67975f8c1f6SNicholas Bellinger }
68075f8c1f6SNicholas Bellinger 
tcm_qla2xxx_queue_status(struct se_cmd * se_cmd)68175f8c1f6SNicholas Bellinger static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
68275f8c1f6SNicholas Bellinger {
68375f8c1f6SNicholas Bellinger 	struct qla_tgt_cmd *cmd = container_of(se_cmd,
68475f8c1f6SNicholas Bellinger 				struct qla_tgt_cmd, se_cmd);
68575f8c1f6SNicholas Bellinger 	int xmit_type = QLA_TGT_XMIT_STATUS;
68675f8c1f6SNicholas Bellinger 
687694833eeSQuinn Tran 	if (cmd->aborted) {
688694833eeSQuinn Tran 		/*
689694833eeSQuinn Tran 		 * Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
690694833eeSQuinn Tran 		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
691694833eeSQuinn Tran 		 * already kick start the free.
692694833eeSQuinn Tran 		 */
693694833eeSQuinn Tran 		pr_debug(
694694833eeSQuinn Tran 		    "queue_data_in aborted cmd[%p] refcount %d transport_state %x, t_state %x, se_cmd_flags %x\n",
695694833eeSQuinn Tran 		    cmd, kref_read(&cmd->se_cmd.cmd_kref),
696694833eeSQuinn Tran 		    cmd->se_cmd.transport_state, cmd->se_cmd.t_state,
697694833eeSQuinn Tran 		    cmd->se_cmd.se_cmd_flags);
698694833eeSQuinn Tran 		return 0;
699694833eeSQuinn Tran 	}
70075f8c1f6SNicholas Bellinger 	cmd->bufflen = se_cmd->data_length;
70175f8c1f6SNicholas Bellinger 	cmd->sg = NULL;
70275f8c1f6SNicholas Bellinger 	cmd->sg_cnt = 0;
70375f8c1f6SNicholas Bellinger 	cmd->offset = 0;
704b3faa2e8SNicholas Bellinger 	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
7051eb42f96SQuinn Tran 	cmd->trc_flags |= TRC_XMIT_STATUS;
70675f8c1f6SNicholas Bellinger 
70775f8c1f6SNicholas Bellinger 	if (se_cmd->data_direction == DMA_FROM_DEVICE) {
70875f8c1f6SNicholas Bellinger 		/*
70975f8c1f6SNicholas Bellinger 		 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
71075f8c1f6SNicholas Bellinger 		 * for qla_tgt_xmit_response LLD code
71175f8c1f6SNicholas Bellinger 		 */
712b5aff3d2SRoland Dreier 		if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
713b5aff3d2SRoland Dreier 			se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
714b5aff3d2SRoland Dreier 			se_cmd->residual_count = 0;
715b5aff3d2SRoland Dreier 		}
71675f8c1f6SNicholas Bellinger 		se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
717b5aff3d2SRoland Dreier 		se_cmd->residual_count += se_cmd->data_length;
71875f8c1f6SNicholas Bellinger 
71975f8c1f6SNicholas Bellinger 		cmd->bufflen = 0;
72075f8c1f6SNicholas Bellinger 	}
72175f8c1f6SNicholas Bellinger 	/*
72275f8c1f6SNicholas Bellinger 	 * Now queue status response to qla2xxx LLD code and response ring
72375f8c1f6SNicholas Bellinger 	 */
72475f8c1f6SNicholas Bellinger 	return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
72575f8c1f6SNicholas Bellinger }
72675f8c1f6SNicholas Bellinger 
tcm_qla2xxx_queue_tm_rsp(struct se_cmd * se_cmd)727b79fafacSJoern Engel static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
72875f8c1f6SNicholas Bellinger {
72975f8c1f6SNicholas Bellinger 	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
73075f8c1f6SNicholas Bellinger 	struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
73175f8c1f6SNicholas Bellinger 				struct qla_tgt_mgmt_cmd, se_cmd);
73275f8c1f6SNicholas Bellinger 
73375f8c1f6SNicholas Bellinger 	pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
73475f8c1f6SNicholas Bellinger 			mcmd, se_tmr->function, se_tmr->response);
73575f8c1f6SNicholas Bellinger 	/*
73675f8c1f6SNicholas Bellinger 	 * Do translation between TCM TM response codes and
73775f8c1f6SNicholas Bellinger 	 * QLA2xxx FC TM response codes.
73875f8c1f6SNicholas Bellinger 	 */
73975f8c1f6SNicholas Bellinger 	switch (se_tmr->response) {
74075f8c1f6SNicholas Bellinger 	case TMR_FUNCTION_COMPLETE:
74175f8c1f6SNicholas Bellinger 		mcmd->fc_tm_rsp = FC_TM_SUCCESS;
74275f8c1f6SNicholas Bellinger 		break;
74375f8c1f6SNicholas Bellinger 	case TMR_TASK_DOES_NOT_EXIST:
74475f8c1f6SNicholas Bellinger 		mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
74575f8c1f6SNicholas Bellinger 		break;
74675f8c1f6SNicholas Bellinger 	case TMR_FUNCTION_REJECTED:
74775f8c1f6SNicholas Bellinger 		mcmd->fc_tm_rsp = FC_TM_REJECT;
74875f8c1f6SNicholas Bellinger 		break;
74975f8c1f6SNicholas Bellinger 	case TMR_LUN_DOES_NOT_EXIST:
75075f8c1f6SNicholas Bellinger 	default:
75175f8c1f6SNicholas Bellinger 		mcmd->fc_tm_rsp = FC_TM_FAILED;
75275f8c1f6SNicholas Bellinger 		break;
75375f8c1f6SNicholas Bellinger 	}
75475f8c1f6SNicholas Bellinger 	/*
75575f8c1f6SNicholas Bellinger 	 * Queue the TM response to QLA2xxx LLD to build a
75675f8c1f6SNicholas Bellinger 	 * CTIO response packet.
75775f8c1f6SNicholas Bellinger 	 */
75875f8c1f6SNicholas Bellinger 	qlt_xmit_tm_rsp(mcmd);
75975f8c1f6SNicholas Bellinger }
76075f8c1f6SNicholas Bellinger 
tcm_qla2xxx_aborted_task(struct se_cmd * se_cmd)761131e6abcSNicholas Bellinger static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
762131e6abcSNicholas Bellinger {
763605e7402SMike Christie 	struct qla_tgt_cmd *cmd;
764605e7402SMike Christie 	unsigned long flags;
765a07100e0SQuinn Tran 
766605e7402SMike Christie 	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
767a07100e0SQuinn Tran 		return;
768605e7402SMike Christie 
769605e7402SMike Christie 	cmd  = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
770605e7402SMike Christie 
771605e7402SMike Christie 	spin_lock_irqsave(&cmd->sess->sess_cmd_lock, flags);
772605e7402SMike Christie 	list_del_init(&cmd->sess_cmd_list);
773605e7402SMike Christie 	spin_unlock_irqrestore(&cmd->sess->sess_cmd_lock, flags);
774605e7402SMike Christie 
775605e7402SMike Christie 	qlt_abort_cmd(cmd);
776131e6abcSNicholas Bellinger }
777131e6abcSNicholas Bellinger 
778f2d5d9b9SNicholas Bellinger static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
7795d964837SQuinn Tran 			struct tcm_qla2xxx_nacl *, struct fc_port *);
78075f8c1f6SNicholas Bellinger /*
7817560151bSQuinn Tran  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
78275f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port * sess)7835d964837SQuinn Tran static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port *sess)
78475f8c1f6SNicholas Bellinger {
78575f8c1f6SNicholas Bellinger 	struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
78675f8c1f6SNicholas Bellinger 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
78775f8c1f6SNicholas Bellinger 	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
78875f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
78975f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_lport, lport_wwn);
79075f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
79175f8c1f6SNicholas Bellinger 				struct tcm_qla2xxx_nacl, se_node_acl);
79275f8c1f6SNicholas Bellinger 	void *node;
79375f8c1f6SNicholas Bellinger 
79475f8c1f6SNicholas Bellinger 	pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
79575f8c1f6SNicholas Bellinger 
79675f8c1f6SNicholas Bellinger 	node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
797f4c24db1SJoern Engel 	if (WARN_ON(node && (node != se_nacl))) {
798f4c24db1SJoern Engel 		/*
799f4c24db1SJoern Engel 		 * The nacl no longer matches what we think it should be.
800f4c24db1SJoern Engel 		 * Most likely a new dynamic acl has been added while
801f4c24db1SJoern Engel 		 * someone dropped the hardware lock.  It clearly is a
802f4c24db1SJoern Engel 		 * bug elsewhere, but this bit can't make things worse.
803f4c24db1SJoern Engel 		 */
804f4c24db1SJoern Engel 		btree_insert32(&lport->lport_fcport_map, nacl->nport_id,
805f4c24db1SJoern Engel 			       node, GFP_ATOMIC);
806f4c24db1SJoern Engel 	}
80775f8c1f6SNicholas Bellinger 
80875f8c1f6SNicholas Bellinger 	pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
80975f8c1f6SNicholas Bellinger 	    se_nacl, nacl->nport_wwnn, nacl->nport_id);
810f2d5d9b9SNicholas Bellinger 	/*
811f2d5d9b9SNicholas Bellinger 	 * Now clear the se_nacl and session pointers from our HW lport lookup
812f2d5d9b9SNicholas Bellinger 	 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
813f2d5d9b9SNicholas Bellinger 	 *
814f2d5d9b9SNicholas Bellinger 	 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
815f2d5d9b9SNicholas Bellinger 	 * target_wait_for_sess_cmds() before the session waits for outstanding
816f2d5d9b9SNicholas Bellinger 	 * I/O to complete, to avoid a race between session shutdown execution
817f2d5d9b9SNicholas Bellinger 	 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
818f2d5d9b9SNicholas Bellinger 	 */
819f2d5d9b9SNicholas Bellinger 	tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
82075f8c1f6SNicholas Bellinger }
82175f8c1f6SNicholas Bellinger 
tcm_qla2xxx_shutdown_sess(struct fc_port * sess)8225d964837SQuinn Tran static void tcm_qla2xxx_shutdown_sess(struct fc_port *sess)
82375f8c1f6SNicholas Bellinger {
8246f55b06fSMike Christie 	target_stop_session(sess->se_sess);
82575f8c1f6SNicholas Bellinger }
82675f8c1f6SNicholas Bellinger 
tcm_qla2xxx_init_nodeacl(struct se_node_acl * se_nacl,const char * name)827c7d6a803SChristoph Hellwig static int tcm_qla2xxx_init_nodeacl(struct se_node_acl *se_nacl,
82875f8c1f6SNicholas Bellinger 		const char *name)
82975f8c1f6SNicholas Bellinger {
830c7d6a803SChristoph Hellwig 	struct tcm_qla2xxx_nacl *nacl =
831c7d6a803SChristoph Hellwig 		container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
83275f8c1f6SNicholas Bellinger 	u64 wwnn;
83375f8c1f6SNicholas Bellinger 
83475f8c1f6SNicholas Bellinger 	if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
835c7d6a803SChristoph Hellwig 		return -EINVAL;
83675f8c1f6SNicholas Bellinger 
83775f8c1f6SNicholas Bellinger 	nacl->nport_wwnn = wwnn;
83875f8c1f6SNicholas Bellinger 	tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
83975f8c1f6SNicholas Bellinger 
840c7d6a803SChristoph Hellwig 	return 0;
84175f8c1f6SNicholas Bellinger }
84275f8c1f6SNicholas Bellinger 
84375f8c1f6SNicholas Bellinger /* Start items for tcm_qla2xxx_tpg_attrib_cit */
84475f8c1f6SNicholas Bellinger 
84575f8c1f6SNicholas Bellinger #define DEF_QLA_TPG_ATTRIB(name)					\
84675f8c1f6SNicholas Bellinger 									\
8472eafd729SChristoph Hellwig static ssize_t tcm_qla2xxx_tpg_attrib_##name##_show(			\
8482eafd729SChristoph Hellwig 		struct config_item *item, char *page)			\
84975f8c1f6SNicholas Bellinger {									\
8502eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
85175f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,		\
85275f8c1f6SNicholas Bellinger 			struct tcm_qla2xxx_tpg, se_tpg);		\
85375f8c1f6SNicholas Bellinger 									\
8547f5523f6SYe Bin 	return sprintf(page, "%d\n", tpg->tpg_attrib.name);	\
85575f8c1f6SNicholas Bellinger }									\
85675f8c1f6SNicholas Bellinger 									\
8572eafd729SChristoph Hellwig static ssize_t tcm_qla2xxx_tpg_attrib_##name##_store(			\
8582eafd729SChristoph Hellwig 		struct config_item *item, const char *page, size_t count) \
85975f8c1f6SNicholas Bellinger {									\
8602eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
86175f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,		\
86275f8c1f6SNicholas Bellinger 			struct tcm_qla2xxx_tpg, se_tpg);		\
8632eafd729SChristoph Hellwig 	struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib;		\
86475f8c1f6SNicholas Bellinger 	unsigned long val;						\
86575f8c1f6SNicholas Bellinger 	int ret;							\
86675f8c1f6SNicholas Bellinger 									\
86775f8c1f6SNicholas Bellinger 	ret = kstrtoul(page, 0, &val);					\
86875f8c1f6SNicholas Bellinger 	if (ret < 0) {							\
86975f8c1f6SNicholas Bellinger 		pr_err("kstrtoul() failed with"				\
87075f8c1f6SNicholas Bellinger 				" ret: %d\n", ret);			\
87175f8c1f6SNicholas Bellinger 		return -EINVAL;						\
87275f8c1f6SNicholas Bellinger 	}								\
87375f8c1f6SNicholas Bellinger 									\
87475f8c1f6SNicholas Bellinger 	if ((val != 0) && (val != 1)) {					\
87575f8c1f6SNicholas Bellinger 		pr_err("Illegal boolean value %lu\n", val);		\
87675f8c1f6SNicholas Bellinger 		return -EINVAL;						\
87775f8c1f6SNicholas Bellinger 	}								\
87875f8c1f6SNicholas Bellinger 									\
8792eafd729SChristoph Hellwig 	a->name = val;							\
8802eafd729SChristoph Hellwig 									\
8812eafd729SChristoph Hellwig 	return count;							\
8822eafd729SChristoph Hellwig }									\
8832eafd729SChristoph Hellwig CONFIGFS_ATTR(tcm_qla2xxx_tpg_attrib_, name)
88475f8c1f6SNicholas Bellinger 
88575f8c1f6SNicholas Bellinger DEF_QLA_TPG_ATTRIB(generate_node_acls);
88675f8c1f6SNicholas Bellinger DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
88775f8c1f6SNicholas Bellinger DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
88875f8c1f6SNicholas Bellinger DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
889de04a8aaSAndy Grover DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
89054a5e73fSLaurence Oberman #ifdef CONFIG_TCM_QLA2XXX_DEBUG
89154a5e73fSLaurence Oberman DEF_QLA_TPG_ATTRIB(jam_host);
89254a5e73fSLaurence Oberman #endif
893de04a8aaSAndy Grover 
89475f8c1f6SNicholas Bellinger static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
8952eafd729SChristoph Hellwig 	&tcm_qla2xxx_tpg_attrib_attr_generate_node_acls,
8962eafd729SChristoph Hellwig 	&tcm_qla2xxx_tpg_attrib_attr_cache_dynamic_acls,
8972eafd729SChristoph Hellwig 	&tcm_qla2xxx_tpg_attrib_attr_demo_mode_write_protect,
8982eafd729SChristoph Hellwig 	&tcm_qla2xxx_tpg_attrib_attr_prod_mode_write_protect,
8992eafd729SChristoph Hellwig 	&tcm_qla2xxx_tpg_attrib_attr_demo_mode_login_only,
90054a5e73fSLaurence Oberman #ifdef CONFIG_TCM_QLA2XXX_DEBUG
90154a5e73fSLaurence Oberman 	&tcm_qla2xxx_tpg_attrib_attr_jam_host,
90254a5e73fSLaurence Oberman #endif
90375f8c1f6SNicholas Bellinger 	NULL,
90475f8c1f6SNicholas Bellinger };
90575f8c1f6SNicholas Bellinger 
90675f8c1f6SNicholas Bellinger /* End items for tcm_qla2xxx_tpg_attrib_cit */
90775f8c1f6SNicholas Bellinger 
tcm_qla2xxx_enable_tpg(struct se_portal_group * se_tpg,bool enable)908cb8717a7SDmitry Bogdanov static int tcm_qla2xxx_enable_tpg(struct se_portal_group *se_tpg,
909cb8717a7SDmitry Bogdanov 				  bool enable)
91075f8c1f6SNicholas Bellinger {
91117b18eaaSAnatoliy Glagolev 	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
91217b18eaaSAnatoliy Glagolev 	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
91317b18eaaSAnatoliy Glagolev 			struct tcm_qla2xxx_lport, lport_wwn);
91417b18eaaSAnatoliy Glagolev 	struct scsi_qla_host *vha = lport->qla_vha;
91575f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
91675f8c1f6SNicholas Bellinger 			struct tcm_qla2xxx_tpg, se_tpg);
91775f8c1f6SNicholas Bellinger 
918cb8717a7SDmitry Bogdanov 	if (enable) {
9197474f52aSNicholas Bellinger 		if (atomic_read(&tpg->lport_tpg_enabled))
9207474f52aSNicholas Bellinger 			return -EEXIST;
9217474f52aSNicholas Bellinger 
92217b18eaaSAnatoliy Glagolev 		atomic_set(&tpg->lport_tpg_enabled, 1);
92317b18eaaSAnatoliy Glagolev 		qlt_enable_vha(vha);
9247474f52aSNicholas Bellinger 	} else {
9257474f52aSNicholas Bellinger 		if (!atomic_read(&tpg->lport_tpg_enabled))
926cb8717a7SDmitry Bogdanov 			return 0;
9277474f52aSNicholas Bellinger 
92817b18eaaSAnatoliy Glagolev 		atomic_set(&tpg->lport_tpg_enabled, 0);
92917b18eaaSAnatoliy Glagolev 		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
930803e4555SViacheslav Dubeyko 		qlt_stop_phase2(vha->vha_tgt.qla_tgt);
9317474f52aSNicholas Bellinger 	}
93275f8c1f6SNicholas Bellinger 
933cb8717a7SDmitry Bogdanov 	return 0;
93475f8c1f6SNicholas Bellinger }
93575f8c1f6SNicholas Bellinger 
tcm_qla2xxx_tpg_dynamic_sessions_show(struct config_item * item,char * page)9362eafd729SChristoph Hellwig static ssize_t tcm_qla2xxx_tpg_dynamic_sessions_show(struct config_item *item,
937d23dbaaaSNicholas Bellinger 		char *page)
938d23dbaaaSNicholas Bellinger {
9392eafd729SChristoph Hellwig 	return target_show_dynamic_sessions(to_tpg(item), page);
940d23dbaaaSNicholas Bellinger }
941d23dbaaaSNicholas Bellinger 
tcm_qla2xxx_tpg_fabric_prot_type_store(struct config_item * item,const char * page,size_t count)9422eafd729SChristoph Hellwig static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_store(struct config_item *item,
9432eafd729SChristoph Hellwig 		const char *page, size_t count)
94464b16887SNicholas Bellinger {
9452eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = to_tpg(item);
94664b16887SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
94764b16887SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
94864b16887SNicholas Bellinger 	unsigned long val;
94964b16887SNicholas Bellinger 	int ret = kstrtoul(page, 0, &val);
95064b16887SNicholas Bellinger 
95164b16887SNicholas Bellinger 	if (ret) {
95264b16887SNicholas Bellinger 		pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
95364b16887SNicholas Bellinger 		return ret;
95464b16887SNicholas Bellinger 	}
95564b16887SNicholas Bellinger 	if (val != 0 && val != 1 && val != 3) {
95664b16887SNicholas Bellinger 		pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
95764b16887SNicholas Bellinger 		return -EINVAL;
95864b16887SNicholas Bellinger 	}
95964b16887SNicholas Bellinger 	tpg->tpg_attrib.fabric_prot_type = val;
96064b16887SNicholas Bellinger 
96164b16887SNicholas Bellinger 	return count;
96264b16887SNicholas Bellinger }
96364b16887SNicholas Bellinger 
tcm_qla2xxx_tpg_fabric_prot_type_show(struct config_item * item,char * page)9642eafd729SChristoph Hellwig static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_show(struct config_item *item,
96564b16887SNicholas Bellinger 		char *page)
96664b16887SNicholas Bellinger {
9672eafd729SChristoph Hellwig 	struct se_portal_group *se_tpg = to_tpg(item);
96864b16887SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
96964b16887SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
97064b16887SNicholas Bellinger 
97164b16887SNicholas Bellinger 	return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
97264b16887SNicholas Bellinger }
9732eafd729SChristoph Hellwig 
9742eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(tcm_qla2xxx_tpg_, dynamic_sessions);
9752eafd729SChristoph Hellwig CONFIGFS_ATTR(tcm_qla2xxx_tpg_, fabric_prot_type);
97664b16887SNicholas Bellinger 
97775f8c1f6SNicholas Bellinger static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
9782eafd729SChristoph Hellwig 	&tcm_qla2xxx_tpg_attr_dynamic_sessions,
9792eafd729SChristoph Hellwig 	&tcm_qla2xxx_tpg_attr_fabric_prot_type,
98075f8c1f6SNicholas Bellinger 	NULL,
98175f8c1f6SNicholas Bellinger };
98275f8c1f6SNicholas Bellinger 
tcm_qla2xxx_make_tpg(struct se_wwn * wwn,const char * name)983aa090eabSBart Van Assche static struct se_portal_group *tcm_qla2xxx_make_tpg(struct se_wwn *wwn,
98475f8c1f6SNicholas Bellinger 						    const char *name)
98575f8c1f6SNicholas Bellinger {
98675f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = container_of(wwn,
98775f8c1f6SNicholas Bellinger 			struct tcm_qla2xxx_lport, lport_wwn);
98875f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg;
98975f8c1f6SNicholas Bellinger 	unsigned long tpgt;
99075f8c1f6SNicholas Bellinger 	int ret;
99175f8c1f6SNicholas Bellinger 
99275f8c1f6SNicholas Bellinger 	if (strstr(name, "tpgt_") != name)
99375f8c1f6SNicholas Bellinger 		return ERR_PTR(-EINVAL);
99475f8c1f6SNicholas Bellinger 	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
99575f8c1f6SNicholas Bellinger 		return ERR_PTR(-EINVAL);
99675f8c1f6SNicholas Bellinger 
9970e8cd71cSSaurav Kashyap 	if ((tpgt != 1)) {
99875f8c1f6SNicholas Bellinger 		pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
99975f8c1f6SNicholas Bellinger 		return ERR_PTR(-ENOSYS);
100075f8c1f6SNicholas Bellinger 	}
100175f8c1f6SNicholas Bellinger 
100275f8c1f6SNicholas Bellinger 	tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
100375f8c1f6SNicholas Bellinger 	if (!tpg) {
100475f8c1f6SNicholas Bellinger 		pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
100575f8c1f6SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
100675f8c1f6SNicholas Bellinger 	}
100775f8c1f6SNicholas Bellinger 	tpg->lport = lport;
100875f8c1f6SNicholas Bellinger 	tpg->lport_tpgt = tpgt;
100975f8c1f6SNicholas Bellinger 	/*
101075f8c1f6SNicholas Bellinger 	 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
101175f8c1f6SNicholas Bellinger 	 * NodeACLs
101275f8c1f6SNicholas Bellinger 	 */
1013a309f489SAndy Grover 	tpg->tpg_attrib.generate_node_acls = 1;
1014a309f489SAndy Grover 	tpg->tpg_attrib.demo_mode_write_protect = 1;
1015a309f489SAndy Grover 	tpg->tpg_attrib.cache_dynamic_acls = 1;
1016a309f489SAndy Grover 	tpg->tpg_attrib.demo_mode_login_only = 1;
101754a5e73fSLaurence Oberman 	tpg->tpg_attrib.jam_host = 0;
101875f8c1f6SNicholas Bellinger 
1019bc0c94b1SNicholas Bellinger 	ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
102075f8c1f6SNicholas Bellinger 	if (ret < 0) {
102175f8c1f6SNicholas Bellinger 		kfree(tpg);
102275f8c1f6SNicholas Bellinger 		return NULL;
102375f8c1f6SNicholas Bellinger 	}
10240e8cd71cSSaurav Kashyap 
102575f8c1f6SNicholas Bellinger 	lport->tpg_1 = tpg;
102675f8c1f6SNicholas Bellinger 
102775f8c1f6SNicholas Bellinger 	return &tpg->se_tpg;
102875f8c1f6SNicholas Bellinger }
102975f8c1f6SNicholas Bellinger 
tcm_qla2xxx_drop_tpg(struct se_portal_group * se_tpg)103075f8c1f6SNicholas Bellinger static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
103175f8c1f6SNicholas Bellinger {
103275f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
103375f8c1f6SNicholas Bellinger 			struct tcm_qla2xxx_tpg, se_tpg);
103475f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = tpg->lport;
103575f8c1f6SNicholas Bellinger 	struct scsi_qla_host *vha = lport->qla_vha;
103675f8c1f6SNicholas Bellinger 	/*
103775f8c1f6SNicholas Bellinger 	 * Call into qla2x_target.c LLD logic to shutdown the active
103875f8c1f6SNicholas Bellinger 	 * FC Nexuses and disable target mode operation for this qla_hw_data
103975f8c1f6SNicholas Bellinger 	 */
10400e8cd71cSSaurav Kashyap 	if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
10410e8cd71cSSaurav Kashyap 		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
104275f8c1f6SNicholas Bellinger 
104375f8c1f6SNicholas Bellinger 	core_tpg_deregister(se_tpg);
104475f8c1f6SNicholas Bellinger 	/*
104575f8c1f6SNicholas Bellinger 	 * Clear local TPG=1 pointer for non NPIV mode.
104675f8c1f6SNicholas Bellinger 	 */
104775f8c1f6SNicholas Bellinger 	lport->tpg_1 = NULL;
104875f8c1f6SNicholas Bellinger 	kfree(tpg);
104975f8c1f6SNicholas Bellinger }
105075f8c1f6SNicholas Bellinger 
tcm_qla2xxx_npiv_enable_tpg(struct se_portal_group * se_tpg,bool enable)1051cb8717a7SDmitry Bogdanov static int tcm_qla2xxx_npiv_enable_tpg(struct se_portal_group *se_tpg,
1052cb8717a7SDmitry Bogdanov 				    bool enable)
1053394d62baSNicholas Bellinger {
1054394d62baSNicholas Bellinger 	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1055394d62baSNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1056394d62baSNicholas Bellinger 			struct tcm_qla2xxx_lport, lport_wwn);
1057394d62baSNicholas Bellinger 	struct scsi_qla_host *vha = lport->qla_vha;
1058394d62baSNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1059394d62baSNicholas Bellinger 			struct tcm_qla2xxx_tpg, se_tpg);
1060394d62baSNicholas Bellinger 
1061cb8717a7SDmitry Bogdanov 	if (enable) {
1062394d62baSNicholas Bellinger 		if (atomic_read(&tpg->lport_tpg_enabled))
1063394d62baSNicholas Bellinger 			return -EEXIST;
1064394d62baSNicholas Bellinger 
1065394d62baSNicholas Bellinger 		atomic_set(&tpg->lport_tpg_enabled, 1);
1066394d62baSNicholas Bellinger 		qlt_enable_vha(vha);
1067394d62baSNicholas Bellinger 	} else {
1068394d62baSNicholas Bellinger 		if (!atomic_read(&tpg->lport_tpg_enabled))
1069cb8717a7SDmitry Bogdanov 			return 0;
1070394d62baSNicholas Bellinger 
1071394d62baSNicholas Bellinger 		atomic_set(&tpg->lport_tpg_enabled, 0);
1072394d62baSNicholas Bellinger 		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1073803e4555SViacheslav Dubeyko 		qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1074394d62baSNicholas Bellinger 	}
1075394d62baSNicholas Bellinger 
1076cb8717a7SDmitry Bogdanov 	return 0;
1077394d62baSNicholas Bellinger }
1078394d62baSNicholas Bellinger 
tcm_qla2xxx_npiv_make_tpg(struct se_wwn * wwn,const char * name)1079aa090eabSBart Van Assche static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(struct se_wwn *wwn,
108075f8c1f6SNicholas Bellinger 							 const char *name)
108175f8c1f6SNicholas Bellinger {
108275f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = container_of(wwn,
108375f8c1f6SNicholas Bellinger 			struct tcm_qla2xxx_lport, lport_wwn);
108475f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg;
108575f8c1f6SNicholas Bellinger 	unsigned long tpgt;
108675f8c1f6SNicholas Bellinger 	int ret;
108775f8c1f6SNicholas Bellinger 
108875f8c1f6SNicholas Bellinger 	if (strstr(name, "tpgt_") != name)
108975f8c1f6SNicholas Bellinger 		return ERR_PTR(-EINVAL);
109075f8c1f6SNicholas Bellinger 	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
109175f8c1f6SNicholas Bellinger 		return ERR_PTR(-EINVAL);
109275f8c1f6SNicholas Bellinger 
109375f8c1f6SNicholas Bellinger 	tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
109475f8c1f6SNicholas Bellinger 	if (!tpg) {
109575f8c1f6SNicholas Bellinger 		pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
109675f8c1f6SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
109775f8c1f6SNicholas Bellinger 	}
109875f8c1f6SNicholas Bellinger 	tpg->lport = lport;
109975f8c1f6SNicholas Bellinger 	tpg->lport_tpgt = tpgt;
110075f8c1f6SNicholas Bellinger 
11010e8cd71cSSaurav Kashyap 	/*
11020e8cd71cSSaurav Kashyap 	 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
11030e8cd71cSSaurav Kashyap 	 * NodeACLs
11040e8cd71cSSaurav Kashyap 	 */
11050e8cd71cSSaurav Kashyap 	tpg->tpg_attrib.generate_node_acls = 1;
11060e8cd71cSSaurav Kashyap 	tpg->tpg_attrib.demo_mode_write_protect = 1;
11070e8cd71cSSaurav Kashyap 	tpg->tpg_attrib.cache_dynamic_acls = 1;
11080e8cd71cSSaurav Kashyap 	tpg->tpg_attrib.demo_mode_login_only = 1;
11090e8cd71cSSaurav Kashyap 
1110bc0c94b1SNicholas Bellinger 	ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
111175f8c1f6SNicholas Bellinger 	if (ret < 0) {
111275f8c1f6SNicholas Bellinger 		kfree(tpg);
111375f8c1f6SNicholas Bellinger 		return NULL;
111475f8c1f6SNicholas Bellinger 	}
11150e8cd71cSSaurav Kashyap 	lport->tpg_1 = tpg;
111675f8c1f6SNicholas Bellinger 	return &tpg->se_tpg;
111775f8c1f6SNicholas Bellinger }
111875f8c1f6SNicholas Bellinger 
111975f8c1f6SNicholas Bellinger /*
11207560151bSQuinn Tran  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
112175f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_find_sess_by_s_id(scsi_qla_host_t * vha,const be_id_t s_id)1122df95f39aSBart Van Assche static struct fc_port *tcm_qla2xxx_find_sess_by_s_id(scsi_qla_host_t *vha,
1123df95f39aSBart Van Assche 						     const be_id_t s_id)
112475f8c1f6SNicholas Bellinger {
112575f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport;
112675f8c1f6SNicholas Bellinger 	struct se_node_acl *se_nacl;
112775f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_nacl *nacl;
112875f8c1f6SNicholas Bellinger 	u32 key;
112975f8c1f6SNicholas Bellinger 
11300e8cd71cSSaurav Kashyap 	lport = vha->vha_tgt.target_lport_ptr;
113175f8c1f6SNicholas Bellinger 	if (!lport) {
113275f8c1f6SNicholas Bellinger 		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
113375f8c1f6SNicholas Bellinger 		dump_stack();
113475f8c1f6SNicholas Bellinger 		return NULL;
113575f8c1f6SNicholas Bellinger 	}
113675f8c1f6SNicholas Bellinger 
11378b2f5ff3SSwapnil Nagle 	key = sid_to_key(s_id);
113875f8c1f6SNicholas Bellinger 	pr_debug("find_sess_by_s_id: 0x%06x\n", key);
113975f8c1f6SNicholas Bellinger 
114075f8c1f6SNicholas Bellinger 	se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
114175f8c1f6SNicholas Bellinger 	if (!se_nacl) {
114275f8c1f6SNicholas Bellinger 		pr_debug("Unable to locate s_id: 0x%06x\n", key);
114375f8c1f6SNicholas Bellinger 		return NULL;
114475f8c1f6SNicholas Bellinger 	}
114575f8c1f6SNicholas Bellinger 	pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
114675f8c1f6SNicholas Bellinger 	    se_nacl, se_nacl->initiatorname);
114775f8c1f6SNicholas Bellinger 
114875f8c1f6SNicholas Bellinger 	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
11495d964837SQuinn Tran 	if (!nacl->fc_port) {
11505d964837SQuinn Tran 		pr_err("Unable to locate struct fc_port\n");
115175f8c1f6SNicholas Bellinger 		return NULL;
115275f8c1f6SNicholas Bellinger 	}
115375f8c1f6SNicholas Bellinger 
11545d964837SQuinn Tran 	return nacl->fc_port;
115575f8c1f6SNicholas Bellinger }
115675f8c1f6SNicholas Bellinger 
115775f8c1f6SNicholas Bellinger /*
11587560151bSQuinn Tran  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
115975f8c1f6SNicholas Bellinger  */
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 fc_port * fc_port,be_id_t s_id)116075f8c1f6SNicholas Bellinger static void tcm_qla2xxx_set_sess_by_s_id(
116175f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport,
116275f8c1f6SNicholas Bellinger 	struct se_node_acl *new_se_nacl,
116375f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_nacl *nacl,
116475f8c1f6SNicholas Bellinger 	struct se_session *se_sess,
11655d964837SQuinn Tran 	struct fc_port *fc_port,
1166df95f39aSBart Van Assche 	be_id_t s_id)
116775f8c1f6SNicholas Bellinger {
116875f8c1f6SNicholas Bellinger 	u32 key;
116975f8c1f6SNicholas Bellinger 	void *slot;
117075f8c1f6SNicholas Bellinger 	int rc;
117175f8c1f6SNicholas Bellinger 
11728b2f5ff3SSwapnil Nagle 	key = sid_to_key(s_id);
117375f8c1f6SNicholas Bellinger 	pr_debug("set_sess_by_s_id: %06x\n", key);
117475f8c1f6SNicholas Bellinger 
117575f8c1f6SNicholas Bellinger 	slot = btree_lookup32(&lport->lport_fcport_map, key);
117675f8c1f6SNicholas Bellinger 	if (!slot) {
117775f8c1f6SNicholas Bellinger 		if (new_se_nacl) {
117875f8c1f6SNicholas Bellinger 			pr_debug("Setting up new fc_port entry to new_se_nacl\n");
117975f8c1f6SNicholas Bellinger 			nacl->nport_id = key;
118075f8c1f6SNicholas Bellinger 			rc = btree_insert32(&lport->lport_fcport_map, key,
118175f8c1f6SNicholas Bellinger 					new_se_nacl, GFP_ATOMIC);
118275f8c1f6SNicholas Bellinger 			if (rc)
118375f8c1f6SNicholas Bellinger 				printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
118475f8c1f6SNicholas Bellinger 				    (int)key);
118575f8c1f6SNicholas Bellinger 		} else {
118675f8c1f6SNicholas Bellinger 			pr_debug("Wiping nonexisting fc_port entry\n");
118775f8c1f6SNicholas Bellinger 		}
118875f8c1f6SNicholas Bellinger 
11895d964837SQuinn Tran 		fc_port->se_sess = se_sess;
11905d964837SQuinn Tran 		nacl->fc_port = fc_port;
119175f8c1f6SNicholas Bellinger 		return;
119275f8c1f6SNicholas Bellinger 	}
119375f8c1f6SNicholas Bellinger 
11945d964837SQuinn Tran 	if (nacl->fc_port) {
119575f8c1f6SNicholas Bellinger 		if (new_se_nacl == NULL) {
11965d964837SQuinn Tran 			pr_debug("Clearing existing nacl->fc_port and fc_port entry\n");
119775f8c1f6SNicholas Bellinger 			btree_remove32(&lport->lport_fcport_map, key);
11985d964837SQuinn Tran 			nacl->fc_port = NULL;
119975f8c1f6SNicholas Bellinger 			return;
120075f8c1f6SNicholas Bellinger 		}
12015d964837SQuinn Tran 		pr_debug("Replacing existing nacl->fc_port and fc_port entry\n");
120275f8c1f6SNicholas Bellinger 		btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
12035d964837SQuinn Tran 		fc_port->se_sess = se_sess;
12045d964837SQuinn Tran 		nacl->fc_port = fc_port;
120575f8c1f6SNicholas Bellinger 		return;
120675f8c1f6SNicholas Bellinger 	}
120775f8c1f6SNicholas Bellinger 
120875f8c1f6SNicholas Bellinger 	if (new_se_nacl == NULL) {
120975f8c1f6SNicholas Bellinger 		pr_debug("Clearing existing fc_port entry\n");
121075f8c1f6SNicholas Bellinger 		btree_remove32(&lport->lport_fcport_map, key);
121175f8c1f6SNicholas Bellinger 		return;
121275f8c1f6SNicholas Bellinger 	}
121375f8c1f6SNicholas Bellinger 
12145d964837SQuinn Tran 	pr_debug("Replacing existing fc_port entry w/o active nacl->fc_port\n");
121575f8c1f6SNicholas Bellinger 	btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
12165d964837SQuinn Tran 	fc_port->se_sess = se_sess;
12175d964837SQuinn Tran 	nacl->fc_port = fc_port;
121875f8c1f6SNicholas Bellinger 
12195d964837SQuinn Tran 	pr_debug("Setup nacl->fc_port %p by s_id for se_nacl: %p, initiatorname: %s\n",
12205d964837SQuinn Tran 	    nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
122175f8c1f6SNicholas Bellinger }
122275f8c1f6SNicholas Bellinger 
122375f8c1f6SNicholas Bellinger /*
12247560151bSQuinn Tran  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
122575f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_find_sess_by_loop_id(scsi_qla_host_t * vha,const uint16_t loop_id)12265d964837SQuinn Tran static struct fc_port *tcm_qla2xxx_find_sess_by_loop_id(
122775f8c1f6SNicholas Bellinger 	scsi_qla_host_t *vha,
122875f8c1f6SNicholas Bellinger 	const uint16_t loop_id)
122975f8c1f6SNicholas Bellinger {
123075f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport;
123175f8c1f6SNicholas Bellinger 	struct se_node_acl *se_nacl;
123275f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_nacl *nacl;
123375f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_fc_loopid *fc_loopid;
123475f8c1f6SNicholas Bellinger 
12350e8cd71cSSaurav Kashyap 	lport = vha->vha_tgt.target_lport_ptr;
123675f8c1f6SNicholas Bellinger 	if (!lport) {
123775f8c1f6SNicholas Bellinger 		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
123875f8c1f6SNicholas Bellinger 		dump_stack();
123975f8c1f6SNicholas Bellinger 		return NULL;
124075f8c1f6SNicholas Bellinger 	}
124175f8c1f6SNicholas Bellinger 
124275f8c1f6SNicholas Bellinger 	pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
124375f8c1f6SNicholas Bellinger 
124475f8c1f6SNicholas Bellinger 	fc_loopid = lport->lport_loopid_map + loop_id;
124575f8c1f6SNicholas Bellinger 	se_nacl = fc_loopid->se_nacl;
124675f8c1f6SNicholas Bellinger 	if (!se_nacl) {
124775f8c1f6SNicholas Bellinger 		pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
124875f8c1f6SNicholas Bellinger 		    loop_id);
124975f8c1f6SNicholas Bellinger 		return NULL;
125075f8c1f6SNicholas Bellinger 	}
125175f8c1f6SNicholas Bellinger 
125275f8c1f6SNicholas Bellinger 	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
125375f8c1f6SNicholas Bellinger 
12545d964837SQuinn Tran 	if (!nacl->fc_port) {
12555d964837SQuinn Tran 		pr_err("Unable to locate struct fc_port\n");
125675f8c1f6SNicholas Bellinger 		return NULL;
125775f8c1f6SNicholas Bellinger 	}
125875f8c1f6SNicholas Bellinger 
12595d964837SQuinn Tran 	return nacl->fc_port;
126075f8c1f6SNicholas Bellinger }
126175f8c1f6SNicholas Bellinger 
126275f8c1f6SNicholas Bellinger /*
12637560151bSQuinn Tran  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
126475f8c1f6SNicholas Bellinger  */
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 fc_port * fc_port,uint16_t loop_id)126575f8c1f6SNicholas Bellinger static void tcm_qla2xxx_set_sess_by_loop_id(
126675f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport,
126775f8c1f6SNicholas Bellinger 	struct se_node_acl *new_se_nacl,
126875f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_nacl *nacl,
126975f8c1f6SNicholas Bellinger 	struct se_session *se_sess,
12705d964837SQuinn Tran 	struct fc_port *fc_port,
127175f8c1f6SNicholas Bellinger 	uint16_t loop_id)
127275f8c1f6SNicholas Bellinger {
127375f8c1f6SNicholas Bellinger 	struct se_node_acl *saved_nacl;
127475f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_fc_loopid *fc_loopid;
127575f8c1f6SNicholas Bellinger 
127675f8c1f6SNicholas Bellinger 	pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
127775f8c1f6SNicholas Bellinger 
127875f8c1f6SNicholas Bellinger 	fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
127975f8c1f6SNicholas Bellinger 			lport->lport_loopid_map)[loop_id];
128075f8c1f6SNicholas Bellinger 
128175f8c1f6SNicholas Bellinger 	saved_nacl = fc_loopid->se_nacl;
128275f8c1f6SNicholas Bellinger 	if (!saved_nacl) {
128375f8c1f6SNicholas Bellinger 		pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
128475f8c1f6SNicholas Bellinger 		fc_loopid->se_nacl = new_se_nacl;
12855d964837SQuinn Tran 		if (fc_port->se_sess != se_sess)
12865d964837SQuinn Tran 			fc_port->se_sess = se_sess;
12875d964837SQuinn Tran 		if (nacl->fc_port != fc_port)
12885d964837SQuinn Tran 			nacl->fc_port = fc_port;
128975f8c1f6SNicholas Bellinger 		return;
129075f8c1f6SNicholas Bellinger 	}
129175f8c1f6SNicholas Bellinger 
12925d964837SQuinn Tran 	if (nacl->fc_port) {
129375f8c1f6SNicholas Bellinger 		if (new_se_nacl == NULL) {
12945d964837SQuinn Tran 			pr_debug("Clearing nacl->fc_port and fc_loopid->se_nacl\n");
129575f8c1f6SNicholas Bellinger 			fc_loopid->se_nacl = NULL;
12965d964837SQuinn Tran 			nacl->fc_port = NULL;
129775f8c1f6SNicholas Bellinger 			return;
129875f8c1f6SNicholas Bellinger 		}
129975f8c1f6SNicholas Bellinger 
13005d964837SQuinn Tran 		pr_debug("Replacing existing nacl->fc_port and fc_loopid->se_nacl\n");
130175f8c1f6SNicholas Bellinger 		fc_loopid->se_nacl = new_se_nacl;
13025d964837SQuinn Tran 		if (fc_port->se_sess != se_sess)
13035d964837SQuinn Tran 			fc_port->se_sess = se_sess;
13045d964837SQuinn Tran 		if (nacl->fc_port != fc_port)
13055d964837SQuinn Tran 			nacl->fc_port = fc_port;
130675f8c1f6SNicholas Bellinger 		return;
130775f8c1f6SNicholas Bellinger 	}
130875f8c1f6SNicholas Bellinger 
130975f8c1f6SNicholas Bellinger 	if (new_se_nacl == NULL) {
131075f8c1f6SNicholas Bellinger 		pr_debug("Clearing fc_loopid->se_nacl\n");
131175f8c1f6SNicholas Bellinger 		fc_loopid->se_nacl = NULL;
131275f8c1f6SNicholas Bellinger 		return;
131375f8c1f6SNicholas Bellinger 	}
131475f8c1f6SNicholas Bellinger 
13155d964837SQuinn Tran 	pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->fc_port\n");
131675f8c1f6SNicholas Bellinger 	fc_loopid->se_nacl = new_se_nacl;
13175d964837SQuinn Tran 	if (fc_port->se_sess != se_sess)
13185d964837SQuinn Tran 		fc_port->se_sess = se_sess;
13195d964837SQuinn Tran 	if (nacl->fc_port != fc_port)
13205d964837SQuinn Tran 		nacl->fc_port = fc_port;
132175f8c1f6SNicholas Bellinger 
13225d964837SQuinn Tran 	pr_debug("Setup nacl->fc_port %p by loop_id for se_nacl: %p, initiatorname: %s\n",
13235d964837SQuinn Tran 	    nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
132475f8c1f6SNicholas Bellinger }
132575f8c1f6SNicholas Bellinger 
1326f2d5d9b9SNicholas Bellinger /*
13277560151bSQuinn Tran  * Should always be called with qla_hw_data->tgt.sess_lock held.
1328f2d5d9b9SNicholas Bellinger  */
tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport * lport,struct tcm_qla2xxx_nacl * nacl,struct fc_port * sess)1329f2d5d9b9SNicholas Bellinger static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
13305d964837SQuinn Tran 		struct tcm_qla2xxx_nacl *nacl, struct fc_port *sess)
1331f2d5d9b9SNicholas Bellinger {
1332f2d5d9b9SNicholas Bellinger 	struct se_session *se_sess = sess->se_sess;
1333f2d5d9b9SNicholas Bellinger 
1334f2d5d9b9SNicholas Bellinger 	tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1335df95f39aSBart Van Assche 				     sess, port_id_to_be_id(sess->d_id));
1336f2d5d9b9SNicholas Bellinger 	tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1337f2d5d9b9SNicholas Bellinger 				sess, sess->loop_id);
1338f2d5d9b9SNicholas Bellinger }
1339f2d5d9b9SNicholas Bellinger 
tcm_qla2xxx_free_session(struct fc_port * sess)13405d964837SQuinn Tran static void tcm_qla2xxx_free_session(struct fc_port *sess)
134175f8c1f6SNicholas Bellinger {
134275f8c1f6SNicholas Bellinger 	struct qla_tgt *tgt = sess->tgt;
134375f8c1f6SNicholas Bellinger 	struct qla_hw_data *ha = tgt->ha;
13440e8cd71cSSaurav Kashyap 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
134575f8c1f6SNicholas Bellinger 	struct se_session *se_sess;
134675f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport;
134775f8c1f6SNicholas Bellinger 
134875f8c1f6SNicholas Bellinger 	se_sess = sess->se_sess;
134975f8c1f6SNicholas Bellinger 	if (!se_sess) {
13505d964837SQuinn Tran 		pr_err("struct fc_port->se_sess is NULL\n");
135175f8c1f6SNicholas Bellinger 		dump_stack();
135275f8c1f6SNicholas Bellinger 		return;
135375f8c1f6SNicholas Bellinger 	}
135475f8c1f6SNicholas Bellinger 
13550e8cd71cSSaurav Kashyap 	lport = vha->vha_tgt.target_lport_ptr;
135675f8c1f6SNicholas Bellinger 	if (!lport) {
135775f8c1f6SNicholas Bellinger 		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
135875f8c1f6SNicholas Bellinger 		dump_stack();
135975f8c1f6SNicholas Bellinger 		return;
136075f8c1f6SNicholas Bellinger 	}
1361be646c2dSJoern Engel 	target_wait_for_sess_cmds(se_sess);
136275f8c1f6SNicholas Bellinger 
1363b287e351SMike Christie 	target_remove_session(se_sess);
136475f8c1f6SNicholas Bellinger }
136575f8c1f6SNicholas Bellinger 
tcm_qla2xxx_session_cb(struct se_portal_group * se_tpg,struct se_session * se_sess,void * p)13661b655b19SNicholas Bellinger static int tcm_qla2xxx_session_cb(struct se_portal_group *se_tpg,
13671b655b19SNicholas Bellinger 				  struct se_session *se_sess, void *p)
13681b655b19SNicholas Bellinger {
13691b655b19SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
13701b655b19SNicholas Bellinger 				struct tcm_qla2xxx_tpg, se_tpg);
13711b655b19SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = tpg->lport;
13721b655b19SNicholas Bellinger 	struct qla_hw_data *ha = lport->qla_vha->hw;
13731b655b19SNicholas Bellinger 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
13741b655b19SNicholas Bellinger 	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
13751b655b19SNicholas Bellinger 				struct tcm_qla2xxx_nacl, se_node_acl);
13765d964837SQuinn Tran 	struct fc_port *qlat_sess = p;
13771b655b19SNicholas Bellinger 	uint16_t loop_id = qlat_sess->loop_id;
13781b655b19SNicholas Bellinger 	unsigned long flags;
13791b655b19SNicholas Bellinger 
13801b655b19SNicholas Bellinger 	/*
13811b655b19SNicholas Bellinger 	 * And now setup se_nacl and session pointers into HW lport internal
13821b655b19SNicholas Bellinger 	 * mappings for fabric S_ID and LOOP_ID.
13831b655b19SNicholas Bellinger 	 */
13841b655b19SNicholas Bellinger 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1385df95f39aSBart Van Assche 	tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess, qlat_sess,
1386df95f39aSBart Van Assche 				     port_id_to_be_id(qlat_sess->d_id));
13871b655b19SNicholas Bellinger 	tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl,
13881b655b19SNicholas Bellinger 					se_sess, qlat_sess, loop_id);
13891b655b19SNicholas Bellinger 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
13901b655b19SNicholas Bellinger 
13911b655b19SNicholas Bellinger 	return 0;
13921b655b19SNicholas Bellinger }
13931b655b19SNicholas Bellinger 
139475f8c1f6SNicholas Bellinger /*
139575f8c1f6SNicholas Bellinger  * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
139675f8c1f6SNicholas Bellinger  * to locate struct se_node_acl
139775f8c1f6SNicholas Bellinger  */
tcm_qla2xxx_check_initiator_node_acl(scsi_qla_host_t * vha,unsigned char * fc_wwpn,struct fc_port * qlat_sess)139875f8c1f6SNicholas Bellinger static int tcm_qla2xxx_check_initiator_node_acl(
139975f8c1f6SNicholas Bellinger 	scsi_qla_host_t *vha,
140075f8c1f6SNicholas Bellinger 	unsigned char *fc_wwpn,
14015d964837SQuinn Tran 	struct fc_port *qlat_sess)
140275f8c1f6SNicholas Bellinger {
140375f8c1f6SNicholas Bellinger 	struct qla_hw_data *ha = vha->hw;
140475f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport;
140575f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_tpg *tpg;
140675f8c1f6SNicholas Bellinger 	struct se_session *se_sess;
140775f8c1f6SNicholas Bellinger 	unsigned char port_name[36];
140803e8c680SQuinn Tran 	int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count :
140951a07f84SNicholas Bellinger 		       TCM_QLA2XXX_DEFAULT_TAGS;
141075f8c1f6SNicholas Bellinger 
14110e8cd71cSSaurav Kashyap 	lport = vha->vha_tgt.target_lport_ptr;
141275f8c1f6SNicholas Bellinger 	if (!lport) {
141375f8c1f6SNicholas Bellinger 		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
141475f8c1f6SNicholas Bellinger 		dump_stack();
141575f8c1f6SNicholas Bellinger 		return -EINVAL;
141675f8c1f6SNicholas Bellinger 	}
141775f8c1f6SNicholas Bellinger 	/*
141875f8c1f6SNicholas Bellinger 	 * Locate the TPG=1 reference..
141975f8c1f6SNicholas Bellinger 	 */
142075f8c1f6SNicholas Bellinger 	tpg = lport->tpg_1;
142175f8c1f6SNicholas Bellinger 	if (!tpg) {
142267eb4a60SBart Van Assche 		pr_err("Unable to locate struct tcm_qla2xxx_lport->tpg_1\n");
142375f8c1f6SNicholas Bellinger 		return -EINVAL;
142475f8c1f6SNicholas Bellinger 	}
142575f8c1f6SNicholas Bellinger 	/*
142675f8c1f6SNicholas Bellinger 	 * Format the FCP Initiator port_name into colon seperated values to
142775f8c1f6SNicholas Bellinger 	 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
142875f8c1f6SNicholas Bellinger 	 */
142975f8c1f6SNicholas Bellinger 	memset(&port_name, 0, 36);
14303c9786e5SAndy Shevchenko 	snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn);
143175f8c1f6SNicholas Bellinger 	/*
143275f8c1f6SNicholas Bellinger 	 * Locate our struct se_node_acl either from an explict NodeACL created
143375f8c1f6SNicholas Bellinger 	 * via ConfigFS, or via running in TPG demo mode.
143475f8c1f6SNicholas Bellinger 	 */
1435fa834287SMike Christie 	se_sess = target_setup_session(&tpg->se_tpg, num_tags,
14361b655b19SNicholas Bellinger 				       sizeof(struct qla_tgt_cmd),
14371b655b19SNicholas Bellinger 				       TARGET_PROT_ALL, port_name,
14381b655b19SNicholas Bellinger 				       qlat_sess, tcm_qla2xxx_session_cb);
14391b655b19SNicholas Bellinger 	if (IS_ERR(se_sess))
14401b655b19SNicholas Bellinger 		return PTR_ERR(se_sess);
144175f8c1f6SNicholas Bellinger 
144275f8c1f6SNicholas Bellinger 	return 0;
144375f8c1f6SNicholas Bellinger }
144475f8c1f6SNicholas Bellinger 
tcm_qla2xxx_update_sess(struct fc_port * sess,port_id_t s_id,uint16_t loop_id,bool conf_compl_supported)14455d964837SQuinn Tran static void tcm_qla2xxx_update_sess(struct fc_port *sess, port_id_t s_id,
1446c8292d1dSRoland Dreier 				    uint16_t loop_id, bool conf_compl_supported)
1447c8292d1dSRoland Dreier {
1448c8292d1dSRoland Dreier 	struct qla_tgt *tgt = sess->tgt;
1449c8292d1dSRoland Dreier 	struct qla_hw_data *ha = tgt->ha;
14500e8cd71cSSaurav Kashyap 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
14510e8cd71cSSaurav Kashyap 	struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
1452c8292d1dSRoland Dreier 	struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1453c8292d1dSRoland Dreier 	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1454c8292d1dSRoland Dreier 			struct tcm_qla2xxx_nacl, se_node_acl);
1455c8292d1dSRoland Dreier 	u32 key;
1456c8292d1dSRoland Dreier 
1457c8292d1dSRoland Dreier 
145837cacc0aSQuinn Tran 	if (sess->loop_id != loop_id || sess->d_id.b24 != s_id.b24)
14597b833558SOleksandr Khoshaba 		pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
14607b833558SOleksandr Khoshaba 		    sess, sess->port_name,
146137cacc0aSQuinn Tran 		    sess->loop_id, loop_id, sess->d_id.b.domain,
146237cacc0aSQuinn Tran 		    sess->d_id.b.area, sess->d_id.b.al_pa, s_id.b.domain,
14637b833558SOleksandr Khoshaba 		    s_id.b.area, s_id.b.al_pa);
1464c8292d1dSRoland Dreier 
1465c8292d1dSRoland Dreier 	if (sess->loop_id != loop_id) {
1466c8292d1dSRoland Dreier 		/*
1467c8292d1dSRoland Dreier 		 * Because we can shuffle loop IDs around and we
1468c8292d1dSRoland Dreier 		 * update different sessions non-atomically, we might
1469c8292d1dSRoland Dreier 		 * have overwritten this session's old loop ID
1470c8292d1dSRoland Dreier 		 * already, and we might end up overwriting some other
1471c8292d1dSRoland Dreier 		 * session that will be updated later.  So we have to
1472c8292d1dSRoland Dreier 		 * be extra careful and we can't warn about those things...
1473c8292d1dSRoland Dreier 		 */
1474c8292d1dSRoland Dreier 		if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1475c8292d1dSRoland Dreier 			lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1476c8292d1dSRoland Dreier 
1477c8292d1dSRoland Dreier 		lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1478c8292d1dSRoland Dreier 
1479c8292d1dSRoland Dreier 		sess->loop_id = loop_id;
1480c8292d1dSRoland Dreier 	}
1481c8292d1dSRoland Dreier 
148237cacc0aSQuinn Tran 	if (sess->d_id.b24 != s_id.b24) {
148337cacc0aSQuinn Tran 		key = (((u32) sess->d_id.b.domain << 16) |
148437cacc0aSQuinn Tran 		       ((u32) sess->d_id.b.area   <<  8) |
148537cacc0aSQuinn Tran 		       ((u32) sess->d_id.b.al_pa));
1486c8292d1dSRoland Dreier 
1487c8292d1dSRoland Dreier 		if (btree_lookup32(&lport->lport_fcport_map, key))
148837cacc0aSQuinn Tran 			WARN(btree_remove32(&lport->lport_fcport_map, key) !=
148937cacc0aSQuinn Tran 			    se_nacl, "Found wrong se_nacl when updating s_id %x:%x:%x\n",
149037cacc0aSQuinn Tran 			    sess->d_id.b.domain, sess->d_id.b.area,
149137cacc0aSQuinn Tran 			    sess->d_id.b.al_pa);
1492c8292d1dSRoland Dreier 		else
1493c8292d1dSRoland Dreier 			WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
149437cacc0aSQuinn Tran 			     sess->d_id.b.domain, sess->d_id.b.area,
149537cacc0aSQuinn Tran 			     sess->d_id.b.al_pa);
1496c8292d1dSRoland Dreier 
1497c8292d1dSRoland Dreier 		key = (((u32) s_id.b.domain << 16) |
1498c8292d1dSRoland Dreier 		       ((u32) s_id.b.area   <<  8) |
1499c8292d1dSRoland Dreier 		       ((u32) s_id.b.al_pa));
1500c8292d1dSRoland Dreier 
1501c8292d1dSRoland Dreier 		if (btree_lookup32(&lport->lport_fcport_map, key)) {
1502c8292d1dSRoland Dreier 			WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1503c8292d1dSRoland Dreier 			     s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1504c8292d1dSRoland Dreier 			btree_update32(&lport->lport_fcport_map, key, se_nacl);
1505c8292d1dSRoland Dreier 		} else {
150637cacc0aSQuinn Tran 			btree_insert32(&lport->lport_fcport_map, key, se_nacl,
150737cacc0aSQuinn Tran 			    GFP_ATOMIC);
1508c8292d1dSRoland Dreier 		}
1509c8292d1dSRoland Dreier 
151037cacc0aSQuinn Tran 		sess->d_id = s_id;
1511c8292d1dSRoland Dreier 		nacl->nport_id = key;
1512c8292d1dSRoland Dreier 	}
1513c8292d1dSRoland Dreier 
1514c8292d1dSRoland Dreier 	sess->conf_compl_supported = conf_compl_supported;
1515a6ca8878SAlexei Potashnik 
1516c8292d1dSRoland Dreier }
1517c8292d1dSRoland Dreier 
151875f8c1f6SNicholas Bellinger /*
151975f8c1f6SNicholas Bellinger  * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
152075f8c1f6SNicholas Bellinger  */
1521634b9774SBart Van Assche static const struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
152284905dfeSQuinn Tran 	.find_cmd_by_tag	= tcm_qla2xxx_find_cmd_by_tag,
152375f8c1f6SNicholas Bellinger 	.handle_cmd		= tcm_qla2xxx_handle_cmd,
152475f8c1f6SNicholas Bellinger 	.handle_data		= tcm_qla2xxx_handle_data,
152575f8c1f6SNicholas Bellinger 	.handle_tmr		= tcm_qla2xxx_handle_tmr,
152680363e1bSBart Van Assche 	.get_cmd		= tcm_qla2xxx_get_cmd,
152780363e1bSBart Van Assche 	.rel_cmd		= tcm_qla2xxx_rel_cmd,
152875f8c1f6SNicholas Bellinger 	.free_cmd		= tcm_qla2xxx_free_cmd,
152975f8c1f6SNicholas Bellinger 	.free_mcmd		= tcm_qla2xxx_free_mcmd,
153075f8c1f6SNicholas Bellinger 	.free_session		= tcm_qla2xxx_free_session,
1531c8292d1dSRoland Dreier 	.update_sess		= tcm_qla2xxx_update_sess,
153275f8c1f6SNicholas Bellinger 	.check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
153375f8c1f6SNicholas Bellinger 	.find_sess_by_s_id	= tcm_qla2xxx_find_sess_by_s_id,
153475f8c1f6SNicholas Bellinger 	.find_sess_by_loop_id	= tcm_qla2xxx_find_sess_by_loop_id,
153575f8c1f6SNicholas Bellinger 	.clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
15365d964837SQuinn Tran 	.put_sess		= tcm_qla2xxx_put_sess,
153775f8c1f6SNicholas Bellinger 	.shutdown_sess		= tcm_qla2xxx_shutdown_sess,
1538be25152cSQuinn Tran 	.get_dif_tags		= tcm_qla2xxx_dif_tags,
1539be25152cSQuinn Tran 	.chk_dif_tags		= tcm_qla2xxx_chk_dif_tags,
154075f8c1f6SNicholas Bellinger };
154175f8c1f6SNicholas Bellinger 
tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport * lport)154275f8c1f6SNicholas Bellinger static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
154375f8c1f6SNicholas Bellinger {
154475f8c1f6SNicholas Bellinger 	int rc;
15454fd62973SDeepak R Varma 	size_t map_sz;
154675f8c1f6SNicholas Bellinger 
154775f8c1f6SNicholas Bellinger 	rc = btree_init32(&lport->lport_fcport_map);
154875f8c1f6SNicholas Bellinger 	if (rc) {
154975f8c1f6SNicholas Bellinger 		pr_err("Unable to initialize lport->lport_fcport_map btree\n");
155075f8c1f6SNicholas Bellinger 		return rc;
155175f8c1f6SNicholas Bellinger 	}
155275f8c1f6SNicholas Bellinger 
15534fd62973SDeepak R Varma 	map_sz = array_size(65536, sizeof(struct tcm_qla2xxx_fc_loopid));
15544fd62973SDeepak R Varma 
15554fd62973SDeepak R Varma 	lport->lport_loopid_map = vzalloc(map_sz);
155675f8c1f6SNicholas Bellinger 	if (!lport->lport_loopid_map) {
15574fd62973SDeepak R Varma 		pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n", map_sz);
155875f8c1f6SNicholas Bellinger 		btree_destroy32(&lport->lport_fcport_map);
155975f8c1f6SNicholas Bellinger 		return -ENOMEM;
156075f8c1f6SNicholas Bellinger 	}
15614fd62973SDeepak R Varma 	pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n", map_sz);
156275f8c1f6SNicholas Bellinger 	return 0;
156375f8c1f6SNicholas Bellinger }
156475f8c1f6SNicholas Bellinger 
tcm_qla2xxx_lport_register_cb(struct scsi_qla_host * vha,void * target_lport_ptr,u64 npiv_wwpn,u64 npiv_wwnn)156549a47f2cSNicholas Bellinger static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
156649a47f2cSNicholas Bellinger 					 void *target_lport_ptr,
156749a47f2cSNicholas Bellinger 					 u64 npiv_wwpn, u64 npiv_wwnn)
156875f8c1f6SNicholas Bellinger {
156949a47f2cSNicholas Bellinger 	struct qla_hw_data *ha = vha->hw;
157049a47f2cSNicholas Bellinger 	struct tcm_qla2xxx_lport *lport =
157149a47f2cSNicholas Bellinger 			(struct tcm_qla2xxx_lport *)target_lport_ptr;
157275f8c1f6SNicholas Bellinger 	/*
157349a47f2cSNicholas Bellinger 	 * Setup tgt_ops, local pointer to vha and target_lport_ptr
157475f8c1f6SNicholas Bellinger 	 */
157549a47f2cSNicholas Bellinger 	ha->tgt.tgt_ops = &tcm_qla2xxx_template;
157649a47f2cSNicholas Bellinger 	vha->vha_tgt.target_lport_ptr = target_lport_ptr;
157775f8c1f6SNicholas Bellinger 	lport->qla_vha = vha;
157875f8c1f6SNicholas Bellinger 
157975f8c1f6SNicholas Bellinger 	return 0;
158075f8c1f6SNicholas Bellinger }
158175f8c1f6SNicholas Bellinger 
tcm_qla2xxx_make_lport(struct target_fabric_configfs * tf,struct config_group * group,const char * name)158275f8c1f6SNicholas Bellinger static struct se_wwn *tcm_qla2xxx_make_lport(
158375f8c1f6SNicholas Bellinger 	struct target_fabric_configfs *tf,
158475f8c1f6SNicholas Bellinger 	struct config_group *group,
158575f8c1f6SNicholas Bellinger 	const char *name)
158675f8c1f6SNicholas Bellinger {
158775f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport;
158875f8c1f6SNicholas Bellinger 	u64 wwpn;
158975f8c1f6SNicholas Bellinger 	int ret = -ENODEV;
159075f8c1f6SNicholas Bellinger 
159175f8c1f6SNicholas Bellinger 	if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
159275f8c1f6SNicholas Bellinger 		return ERR_PTR(-EINVAL);
159375f8c1f6SNicholas Bellinger 
159475f8c1f6SNicholas Bellinger 	lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
159575f8c1f6SNicholas Bellinger 	if (!lport) {
159675f8c1f6SNicholas Bellinger 		pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
159775f8c1f6SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
159875f8c1f6SNicholas Bellinger 	}
159975f8c1f6SNicholas Bellinger 	lport->lport_wwpn = wwpn;
160075f8c1f6SNicholas Bellinger 	tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
160175f8c1f6SNicholas Bellinger 				wwpn);
1602c046aa0fSRoland Dreier 	sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
160375f8c1f6SNicholas Bellinger 
160475f8c1f6SNicholas Bellinger 	ret = tcm_qla2xxx_init_lport(lport);
160575f8c1f6SNicholas Bellinger 	if (ret != 0)
160675f8c1f6SNicholas Bellinger 		goto out;
160775f8c1f6SNicholas Bellinger 
160849a47f2cSNicholas Bellinger 	ret = qlt_lport_register(lport, wwpn, 0, 0,
160949a47f2cSNicholas Bellinger 				 tcm_qla2xxx_lport_register_cb);
161075f8c1f6SNicholas Bellinger 	if (ret != 0)
161175f8c1f6SNicholas Bellinger 		goto out_lport;
161275f8c1f6SNicholas Bellinger 
161375f8c1f6SNicholas Bellinger 	return &lport->lport_wwn;
161475f8c1f6SNicholas Bellinger out_lport:
161575f8c1f6SNicholas Bellinger 	vfree(lport->lport_loopid_map);
161675f8c1f6SNicholas Bellinger 	btree_destroy32(&lport->lport_fcport_map);
161775f8c1f6SNicholas Bellinger out:
161875f8c1f6SNicholas Bellinger 	kfree(lport);
161975f8c1f6SNicholas Bellinger 	return ERR_PTR(ret);
162075f8c1f6SNicholas Bellinger }
162175f8c1f6SNicholas Bellinger 
tcm_qla2xxx_drop_lport(struct se_wwn * wwn)162275f8c1f6SNicholas Bellinger static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
162375f8c1f6SNicholas Bellinger {
162475f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = container_of(wwn,
162575f8c1f6SNicholas Bellinger 			struct tcm_qla2xxx_lport, lport_wwn);
162675f8c1f6SNicholas Bellinger 	struct scsi_qla_host *vha = lport->qla_vha;
162775f8c1f6SNicholas Bellinger 	struct se_node_acl *node;
162875f8c1f6SNicholas Bellinger 	u32 key = 0;
162975f8c1f6SNicholas Bellinger 
163075f8c1f6SNicholas Bellinger 	/*
163175f8c1f6SNicholas Bellinger 	 * Call into qla2x_target.c LLD logic to complete the
163275f8c1f6SNicholas Bellinger 	 * shutdown of struct qla_tgt after the call to
163375f8c1f6SNicholas Bellinger 	 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
163475f8c1f6SNicholas Bellinger 	 */
16350e8cd71cSSaurav Kashyap 	if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
16360e8cd71cSSaurav Kashyap 		qlt_stop_phase2(vha->vha_tgt.qla_tgt);
163775f8c1f6SNicholas Bellinger 
163875f8c1f6SNicholas Bellinger 	qlt_lport_deregister(vha);
163975f8c1f6SNicholas Bellinger 
164075f8c1f6SNicholas Bellinger 	vfree(lport->lport_loopid_map);
164175f8c1f6SNicholas Bellinger 	btree_for_each_safe32(&lport->lport_fcport_map, key, node)
164275f8c1f6SNicholas Bellinger 		btree_remove32(&lport->lport_fcport_map, key);
164375f8c1f6SNicholas Bellinger 	btree_destroy32(&lport->lport_fcport_map);
164475f8c1f6SNicholas Bellinger 	kfree(lport);
164575f8c1f6SNicholas Bellinger }
164675f8c1f6SNicholas Bellinger 
tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host * base_vha,void * target_lport_ptr,u64 npiv_wwpn,u64 npiv_wwnn)164749a47f2cSNicholas Bellinger static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
164849a47f2cSNicholas Bellinger 					      void *target_lport_ptr,
164949a47f2cSNicholas Bellinger 					      u64 npiv_wwpn, u64 npiv_wwnn)
165049a47f2cSNicholas Bellinger {
165149a47f2cSNicholas Bellinger 	struct fc_vport *vport;
165249a47f2cSNicholas Bellinger 	struct Scsi_Host *sh = base_vha->host;
165349a47f2cSNicholas Bellinger 	struct scsi_qla_host *npiv_vha;
165449a47f2cSNicholas Bellinger 	struct tcm_qla2xxx_lport *lport =
165549a47f2cSNicholas Bellinger 			(struct tcm_qla2xxx_lport *)target_lport_ptr;
16567474f52aSNicholas Bellinger 	struct tcm_qla2xxx_lport *base_lport =
16577474f52aSNicholas Bellinger 			(struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
165849a47f2cSNicholas Bellinger 	struct fc_vport_identifiers vport_id;
165949a47f2cSNicholas Bellinger 
1660ead03855SQuinn Tran 	if (qla_ini_mode_enabled(base_vha)) {
166149a47f2cSNicholas Bellinger 		pr_err("qla2xxx base_vha not enabled for target mode\n");
166249a47f2cSNicholas Bellinger 		return -EPERM;
166349a47f2cSNicholas Bellinger 	}
166449a47f2cSNicholas Bellinger 
16657474f52aSNicholas Bellinger 	if (!base_lport || !base_lport->tpg_1 ||
16667474f52aSNicholas Bellinger 	    !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
16677474f52aSNicholas Bellinger 		pr_err("qla2xxx base_lport or tpg_1 not available\n");
16687474f52aSNicholas Bellinger 		return -EPERM;
16697474f52aSNicholas Bellinger 	}
16707474f52aSNicholas Bellinger 
167149a47f2cSNicholas Bellinger 	memset(&vport_id, 0, sizeof(vport_id));
167249a47f2cSNicholas Bellinger 	vport_id.port_name = npiv_wwpn;
167349a47f2cSNicholas Bellinger 	vport_id.node_name = npiv_wwnn;
167449a47f2cSNicholas Bellinger 	vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
167549a47f2cSNicholas Bellinger 	vport_id.vport_type = FC_PORTTYPE_NPIV;
167649a47f2cSNicholas Bellinger 	vport_id.disable = false;
167749a47f2cSNicholas Bellinger 
167849a47f2cSNicholas Bellinger 	vport = fc_vport_create(sh, 0, &vport_id);
167949a47f2cSNicholas Bellinger 	if (!vport) {
168049a47f2cSNicholas Bellinger 		pr_err("fc_vport_create failed for qla2xxx_npiv\n");
168149a47f2cSNicholas Bellinger 		return -ENODEV;
168249a47f2cSNicholas Bellinger 	}
168349a47f2cSNicholas Bellinger 	/*
168449a47f2cSNicholas Bellinger 	 * Setup local pointer to NPIV vhba + target_lport_ptr
168549a47f2cSNicholas Bellinger 	 */
168649a47f2cSNicholas Bellinger 	npiv_vha = (struct scsi_qla_host *)vport->dd_data;
168749a47f2cSNicholas Bellinger 	npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
168849a47f2cSNicholas Bellinger 	lport->qla_vha = npiv_vha;
168949a47f2cSNicholas Bellinger 	scsi_host_get(npiv_vha->host);
169049a47f2cSNicholas Bellinger 	return 0;
169149a47f2cSNicholas Bellinger }
169249a47f2cSNicholas Bellinger 
169349a47f2cSNicholas Bellinger 
tcm_qla2xxx_npiv_make_lport(struct target_fabric_configfs * tf,struct config_group * group,const char * name)169475f8c1f6SNicholas Bellinger static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
169575f8c1f6SNicholas Bellinger 	struct target_fabric_configfs *tf,
169675f8c1f6SNicholas Bellinger 	struct config_group *group,
169775f8c1f6SNicholas Bellinger 	const char *name)
169875f8c1f6SNicholas Bellinger {
169975f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport;
170049a47f2cSNicholas Bellinger 	u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
170149a47f2cSNicholas Bellinger 	char *p, tmp[128];
170275f8c1f6SNicholas Bellinger 	int ret;
170375f8c1f6SNicholas Bellinger 
170449a47f2cSNicholas Bellinger 	snprintf(tmp, 128, "%s", name);
170549a47f2cSNicholas Bellinger 
170649a47f2cSNicholas Bellinger 	p = strchr(tmp, '@');
170749a47f2cSNicholas Bellinger 	if (!p) {
1708669a311bSColin Ian King 		pr_err("Unable to locate NPIV '@' separator\n");
170949a47f2cSNicholas Bellinger 		return ERR_PTR(-EINVAL);
171049a47f2cSNicholas Bellinger 	}
171149a47f2cSNicholas Bellinger 	*p++ = '\0';
171249a47f2cSNicholas Bellinger 
171349a47f2cSNicholas Bellinger 	if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
171449a47f2cSNicholas Bellinger 		return ERR_PTR(-EINVAL);
171549a47f2cSNicholas Bellinger 
171649a47f2cSNicholas Bellinger 	if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
171775f8c1f6SNicholas Bellinger 				       &npiv_wwpn, &npiv_wwnn) < 0)
171875f8c1f6SNicholas Bellinger 		return ERR_PTR(-EINVAL);
171975f8c1f6SNicholas Bellinger 
172075f8c1f6SNicholas Bellinger 	lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
172175f8c1f6SNicholas Bellinger 	if (!lport) {
172275f8c1f6SNicholas Bellinger 		pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
172375f8c1f6SNicholas Bellinger 		return ERR_PTR(-ENOMEM);
172475f8c1f6SNicholas Bellinger 	}
172575f8c1f6SNicholas Bellinger 	lport->lport_npiv_wwpn = npiv_wwpn;
172675f8c1f6SNicholas Bellinger 	lport->lport_npiv_wwnn = npiv_wwnn;
1727c046aa0fSRoland Dreier 	sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
172875f8c1f6SNicholas Bellinger 
17290e8cd71cSSaurav Kashyap 	ret = tcm_qla2xxx_init_lport(lport);
173075f8c1f6SNicholas Bellinger 	if (ret != 0)
173175f8c1f6SNicholas Bellinger 		goto out;
173275f8c1f6SNicholas Bellinger 
173349a47f2cSNicholas Bellinger 	ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
173449a47f2cSNicholas Bellinger 				 tcm_qla2xxx_lport_register_npiv_cb);
17350e8cd71cSSaurav Kashyap 	if (ret != 0)
17360e8cd71cSSaurav Kashyap 		goto out_lport;
17370e8cd71cSSaurav Kashyap 
173875f8c1f6SNicholas Bellinger 	return &lport->lport_wwn;
17390e8cd71cSSaurav Kashyap out_lport:
17400e8cd71cSSaurav Kashyap 	vfree(lport->lport_loopid_map);
17410e8cd71cSSaurav Kashyap 	btree_destroy32(&lport->lport_fcport_map);
174275f8c1f6SNicholas Bellinger out:
174375f8c1f6SNicholas Bellinger 	kfree(lport);
174475f8c1f6SNicholas Bellinger 	return ERR_PTR(ret);
174575f8c1f6SNicholas Bellinger }
174675f8c1f6SNicholas Bellinger 
tcm_qla2xxx_npiv_drop_lport(struct se_wwn * wwn)174775f8c1f6SNicholas Bellinger static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
174875f8c1f6SNicholas Bellinger {
174975f8c1f6SNicholas Bellinger 	struct tcm_qla2xxx_lport *lport = container_of(wwn,
175075f8c1f6SNicholas Bellinger 			struct tcm_qla2xxx_lport, lport_wwn);
175149a47f2cSNicholas Bellinger 	struct scsi_qla_host *npiv_vha = lport->qla_vha;
175249a47f2cSNicholas Bellinger 	struct qla_hw_data *ha = npiv_vha->hw;
175349a47f2cSNicholas Bellinger 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
175449a47f2cSNicholas Bellinger 
175549a47f2cSNicholas Bellinger 	scsi_host_put(npiv_vha->host);
175675f8c1f6SNicholas Bellinger 	/*
17570e8cd71cSSaurav Kashyap 	 * Notify libfc that we want to release the vha->fc_vport
175875f8c1f6SNicholas Bellinger 	 */
175949a47f2cSNicholas Bellinger 	fc_vport_terminate(npiv_vha->fc_vport);
176049a47f2cSNicholas Bellinger 	scsi_host_put(base_vha->host);
176175f8c1f6SNicholas Bellinger 	kfree(lport);
176275f8c1f6SNicholas Bellinger }
176375f8c1f6SNicholas Bellinger 
176475f8c1f6SNicholas Bellinger 
tcm_qla2xxx_wwn_version_show(struct config_item * item,char * page)17652eafd729SChristoph Hellwig static ssize_t tcm_qla2xxx_wwn_version_show(struct config_item *item,
176675f8c1f6SNicholas Bellinger 		char *page)
176775f8c1f6SNicholas Bellinger {
176875f8c1f6SNicholas Bellinger 	return sprintf(page,
176949541a04SJohannes Berg 	    "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on %s\n",
177049541a04SJohannes Berg 	    QLA2XXX_VERSION, utsname()->sysname,
177149541a04SJohannes Berg 	    utsname()->machine, utsname()->release);
177275f8c1f6SNicholas Bellinger }
177375f8c1f6SNicholas Bellinger 
17742eafd729SChristoph Hellwig CONFIGFS_ATTR_RO(tcm_qla2xxx_wwn_, version);
177575f8c1f6SNicholas Bellinger 
177675f8c1f6SNicholas Bellinger static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
17772eafd729SChristoph Hellwig 	&tcm_qla2xxx_wwn_attr_version,
177875f8c1f6SNicholas Bellinger 	NULL,
177975f8c1f6SNicholas Bellinger };
178075f8c1f6SNicholas Bellinger 
17819ac8928eSChristoph Hellwig static const struct target_core_fabric_ops tcm_qla2xxx_ops = {
17829ac8928eSChristoph Hellwig 	.module				= THIS_MODULE,
178330c7ca93SDavid Disseldorp 	.fabric_name			= "qla2xxx",
1784144bc4c2SChristoph Hellwig 	.node_acl_size			= sizeof(struct tcm_qla2xxx_nacl),
17858f9b5654SNicholas Bellinger 	/*
17868f9b5654SNicholas Bellinger 	 * XXX: Limit assumes single page per scatter-gather-list entry.
17878f9b5654SNicholas Bellinger 	 * Current maximum is ~4.9 MB per se_cmd->t_data_sg with PAGE_SIZE=4096
17888f9b5654SNicholas Bellinger 	 */
17898f9b5654SNicholas Bellinger 	.max_data_sg_nents		= 1200,
179075f8c1f6SNicholas Bellinger 	.tpg_get_wwn			= tcm_qla2xxx_get_fabric_wwn,
179175f8c1f6SNicholas Bellinger 	.tpg_get_tag			= tcm_qla2xxx_get_tag,
179275f8c1f6SNicholas Bellinger 	.tpg_check_demo_mode		= tcm_qla2xxx_check_demo_mode,
179375f8c1f6SNicholas Bellinger 	.tpg_check_demo_mode_cache	= tcm_qla2xxx_check_demo_mode_cache,
179475f8c1f6SNicholas Bellinger 	.tpg_check_demo_mode_write_protect =
179575f8c1f6SNicholas Bellinger 					tcm_qla2xxx_check_demo_write_protect,
179675f8c1f6SNicholas Bellinger 	.tpg_check_prod_mode_write_protect =
179775f8c1f6SNicholas Bellinger 					tcm_qla2xxx_check_prod_write_protect,
179864b16887SNicholas Bellinger 	.tpg_check_prot_fabric_only	= tcm_qla2xxx_check_prot_fabric_only,
1799de04a8aaSAndy Grover 	.tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
180075f8c1f6SNicholas Bellinger 	.tpg_get_inst_index		= tcm_qla2xxx_tpg_get_inst_index,
180175f8c1f6SNicholas Bellinger 	.check_stop_free		= tcm_qla2xxx_check_stop_free,
180275f8c1f6SNicholas Bellinger 	.release_cmd			= tcm_qla2xxx_release_cmd,
180375f8c1f6SNicholas Bellinger 	.close_session			= tcm_qla2xxx_close_session,
180475f8c1f6SNicholas Bellinger 	.sess_get_initiator_sid		= NULL,
180575f8c1f6SNicholas Bellinger 	.write_pending			= tcm_qla2xxx_write_pending,
180675f8c1f6SNicholas Bellinger 	.get_cmd_state			= tcm_qla2xxx_get_cmd_state,
180775f8c1f6SNicholas Bellinger 	.queue_data_in			= tcm_qla2xxx_queue_data_in,
180875f8c1f6SNicholas Bellinger 	.queue_status			= tcm_qla2xxx_queue_status,
180975f8c1f6SNicholas Bellinger 	.queue_tm_rsp			= tcm_qla2xxx_queue_tm_rsp,
1810131e6abcSNicholas Bellinger 	.aborted_task			= tcm_qla2xxx_aborted_task,
181175f8c1f6SNicholas Bellinger 	/*
181275f8c1f6SNicholas Bellinger 	 * Setup function pointers for generic logic in
181375f8c1f6SNicholas Bellinger 	 * target_core_fabric_configfs.c
181475f8c1f6SNicholas Bellinger 	 */
181575f8c1f6SNicholas Bellinger 	.fabric_make_wwn		= tcm_qla2xxx_make_lport,
181675f8c1f6SNicholas Bellinger 	.fabric_drop_wwn		= tcm_qla2xxx_drop_lport,
181775f8c1f6SNicholas Bellinger 	.fabric_make_tpg		= tcm_qla2xxx_make_tpg,
1818cb8717a7SDmitry Bogdanov 	.fabric_enable_tpg		= tcm_qla2xxx_enable_tpg,
181975f8c1f6SNicholas Bellinger 	.fabric_drop_tpg		= tcm_qla2xxx_drop_tpg,
1820c7d6a803SChristoph Hellwig 	.fabric_init_nodeacl		= tcm_qla2xxx_init_nodeacl,
18219ac8928eSChristoph Hellwig 
18229ac8928eSChristoph Hellwig 	.tfc_wwn_attrs			= tcm_qla2xxx_wwn_attrs,
18239ac8928eSChristoph Hellwig 	.tfc_tpg_base_attrs		= tcm_qla2xxx_tpg_attrs,
18249ac8928eSChristoph Hellwig 	.tfc_tpg_attrib_attrs		= tcm_qla2xxx_tpg_attrib_attrs,
1825*194605d4SMike Christie 
1826*194605d4SMike Christie 	.default_submit_type		= TARGET_DIRECT_SUBMIT,
1827*194605d4SMike Christie 	.direct_submit_supp		= 1,
182875f8c1f6SNicholas Bellinger };
182975f8c1f6SNicholas Bellinger 
18309ac8928eSChristoph Hellwig static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
18319ac8928eSChristoph Hellwig 	.module				= THIS_MODULE,
183230c7ca93SDavid Disseldorp 	.fabric_name			= "qla2xxx_npiv",
1833144bc4c2SChristoph Hellwig 	.node_acl_size			= sizeof(struct tcm_qla2xxx_nacl),
183484197a36SNicholas Bellinger 	.tpg_get_wwn			= tcm_qla2xxx_get_fabric_wwn,
183575f8c1f6SNicholas Bellinger 	.tpg_get_tag			= tcm_qla2xxx_get_tag,
18360e8cd71cSSaurav Kashyap 	.tpg_check_demo_mode		= tcm_qla2xxx_check_demo_mode,
18370e8cd71cSSaurav Kashyap 	.tpg_check_demo_mode_cache	= tcm_qla2xxx_check_demo_mode_cache,
18380e8cd71cSSaurav Kashyap 	.tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
18390e8cd71cSSaurav Kashyap 	.tpg_check_prod_mode_write_protect =
18400e8cd71cSSaurav Kashyap 	    tcm_qla2xxx_check_prod_write_protect,
1841de04a8aaSAndy Grover 	.tpg_check_demo_mode_login_only	= tcm_qla2xxx_check_demo_mode_login_only,
184275f8c1f6SNicholas Bellinger 	.tpg_get_inst_index		= tcm_qla2xxx_tpg_get_inst_index,
18430e8cd71cSSaurav Kashyap 	.check_stop_free                = tcm_qla2xxx_check_stop_free,
184475f8c1f6SNicholas Bellinger 	.release_cmd			= tcm_qla2xxx_release_cmd,
184575f8c1f6SNicholas Bellinger 	.close_session			= tcm_qla2xxx_close_session,
184675f8c1f6SNicholas Bellinger 	.sess_get_initiator_sid		= NULL,
184775f8c1f6SNicholas Bellinger 	.write_pending			= tcm_qla2xxx_write_pending,
184875f8c1f6SNicholas Bellinger 	.get_cmd_state			= tcm_qla2xxx_get_cmd_state,
184975f8c1f6SNicholas Bellinger 	.queue_data_in			= tcm_qla2xxx_queue_data_in,
185075f8c1f6SNicholas Bellinger 	.queue_status			= tcm_qla2xxx_queue_status,
185175f8c1f6SNicholas Bellinger 	.queue_tm_rsp			= tcm_qla2xxx_queue_tm_rsp,
1852131e6abcSNicholas Bellinger 	.aborted_task			= tcm_qla2xxx_aborted_task,
185375f8c1f6SNicholas Bellinger 	/*
185475f8c1f6SNicholas Bellinger 	 * Setup function pointers for generic logic in
185575f8c1f6SNicholas Bellinger 	 * target_core_fabric_configfs.c
185675f8c1f6SNicholas Bellinger 	 */
185775f8c1f6SNicholas Bellinger 	.fabric_make_wwn		= tcm_qla2xxx_npiv_make_lport,
185875f8c1f6SNicholas Bellinger 	.fabric_drop_wwn		= tcm_qla2xxx_npiv_drop_lport,
185975f8c1f6SNicholas Bellinger 	.fabric_make_tpg		= tcm_qla2xxx_npiv_make_tpg,
1860cb8717a7SDmitry Bogdanov 	.fabric_enable_tpg		= tcm_qla2xxx_npiv_enable_tpg,
186175f8c1f6SNicholas Bellinger 	.fabric_drop_tpg		= tcm_qla2xxx_drop_tpg,
1862c7d6a803SChristoph Hellwig 	.fabric_init_nodeacl		= tcm_qla2xxx_init_nodeacl,
18639ac8928eSChristoph Hellwig 
18649ac8928eSChristoph Hellwig 	.tfc_wwn_attrs			= tcm_qla2xxx_wwn_attrs,
1865*194605d4SMike Christie 
1866*194605d4SMike Christie 	.default_submit_type		= TARGET_DIRECT_SUBMIT,
1867*194605d4SMike Christie 	.direct_submit_supp		= 1,
186875f8c1f6SNicholas Bellinger };
186975f8c1f6SNicholas Bellinger 
tcm_qla2xxx_register_configfs(void)187075f8c1f6SNicholas Bellinger static int tcm_qla2xxx_register_configfs(void)
187175f8c1f6SNicholas Bellinger {
187275f8c1f6SNicholas Bellinger 	int ret;
187375f8c1f6SNicholas Bellinger 
187449541a04SJohannes Berg 	pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on %s\n",
187549541a04SJohannes Berg 	    QLA2XXX_VERSION, utsname()->sysname,
187649541a04SJohannes Berg 	    utsname()->machine, utsname()->release);
187775f8c1f6SNicholas Bellinger 
18789ac8928eSChristoph Hellwig 	ret = target_register_template(&tcm_qla2xxx_ops);
18799ac8928eSChristoph Hellwig 	if (ret)
18809ac8928eSChristoph Hellwig 		return ret;
18819ac8928eSChristoph Hellwig 
18829ac8928eSChristoph Hellwig 	ret = target_register_template(&tcm_qla2xxx_npiv_ops);
18839ac8928eSChristoph Hellwig 	if (ret)
188475f8c1f6SNicholas Bellinger 		goto out_fabric;
188575f8c1f6SNicholas Bellinger 
188675f8c1f6SNicholas Bellinger 	tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
188775f8c1f6SNicholas Bellinger 						WQ_MEM_RECLAIM, 0);
188875f8c1f6SNicholas Bellinger 	if (!tcm_qla2xxx_free_wq) {
188975f8c1f6SNicholas Bellinger 		ret = -ENOMEM;
189075f8c1f6SNicholas Bellinger 		goto out_fabric_npiv;
189175f8c1f6SNicholas Bellinger 	}
189275f8c1f6SNicholas Bellinger 
189375f8c1f6SNicholas Bellinger 	return 0;
189475f8c1f6SNicholas Bellinger 
189575f8c1f6SNicholas Bellinger out_fabric_npiv:
18969ac8928eSChristoph Hellwig 	target_unregister_template(&tcm_qla2xxx_npiv_ops);
189775f8c1f6SNicholas Bellinger out_fabric:
18989ac8928eSChristoph Hellwig 	target_unregister_template(&tcm_qla2xxx_ops);
189975f8c1f6SNicholas Bellinger 	return ret;
190075f8c1f6SNicholas Bellinger }
190175f8c1f6SNicholas Bellinger 
tcm_qla2xxx_deregister_configfs(void)190275f8c1f6SNicholas Bellinger static void tcm_qla2xxx_deregister_configfs(void)
190375f8c1f6SNicholas Bellinger {
190475f8c1f6SNicholas Bellinger 	destroy_workqueue(tcm_qla2xxx_free_wq);
190575f8c1f6SNicholas Bellinger 
19069ac8928eSChristoph Hellwig 	target_unregister_template(&tcm_qla2xxx_ops);
19079ac8928eSChristoph Hellwig 	target_unregister_template(&tcm_qla2xxx_npiv_ops);
190875f8c1f6SNicholas Bellinger }
190975f8c1f6SNicholas Bellinger 
tcm_qla2xxx_init(void)191075f8c1f6SNicholas Bellinger static int __init tcm_qla2xxx_init(void)
191175f8c1f6SNicholas Bellinger {
191275f8c1f6SNicholas Bellinger 	int ret;
191375f8c1f6SNicholas Bellinger 
19148a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct abts_recv_from_24xx) != 64);
19158a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct abts_resp_from_24xx_fw) != 64);
19168a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct atio7_fcp_cmnd) != 32);
19178a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct atio_from_isp) != 64);
19188a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct ba_acc_le) != 12);
19198a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct ba_rjt_le) != 4);
19208a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct ctio7_from_24xx) != 64);
19218a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
19228a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct ctio_crc2_to_fw) != 64);
19238a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct ctio_crc_from_fw) != 64);
19248a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
1925a7f47454SBart Van Assche 	BUILD_BUG_ON(sizeof(struct fcp_hdr) != 24);
19268a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct fcp_hdr_le) != 24);
19278a73a0e0SBart Van Assche 	BUILD_BUG_ON(sizeof(struct nack_to_isp) != 64);
19288a73a0e0SBart Van Assche 
192975f8c1f6SNicholas Bellinger 	ret = tcm_qla2xxx_register_configfs();
193075f8c1f6SNicholas Bellinger 	if (ret < 0)
193175f8c1f6SNicholas Bellinger 		return ret;
193275f8c1f6SNicholas Bellinger 
193375f8c1f6SNicholas Bellinger 	return 0;
193475f8c1f6SNicholas Bellinger }
193575f8c1f6SNicholas Bellinger 
tcm_qla2xxx_exit(void)193675f8c1f6SNicholas Bellinger static void __exit tcm_qla2xxx_exit(void)
193775f8c1f6SNicholas Bellinger {
193875f8c1f6SNicholas Bellinger 	tcm_qla2xxx_deregister_configfs();
193975f8c1f6SNicholas Bellinger }
194075f8c1f6SNicholas Bellinger 
194124c7d6c7SSebastian Herbszt MODULE_DESCRIPTION("TCM QLA24XX+ series NPIV enabled fabric driver");
194275f8c1f6SNicholas Bellinger MODULE_LICENSE("GPL");
194375f8c1f6SNicholas Bellinger module_init(tcm_qla2xxx_init);
194475f8c1f6SNicholas Bellinger module_exit(tcm_qla2xxx_exit);
1945