1 /*******************************************************************************
2  * This file contains tcm implementation using v4 configfs fabric infrastructure
3  * for QLogic target mode HBAs
4  *
5  * (c) Copyright 2010-2013 Datera, Inc.
6  *
7  * Author: Nicholas A. Bellinger <nab@daterainc.com>
8  *
9  * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
10  * the TCM_FC / Open-FCoE.org fabric module.
11  *
12  * Copyright (c) 2010 Cisco Systems, Inc
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  ****************************************************************************/
24 
25 
26 #include <linux/module.h>
27 #include <linux/utsname.h>
28 #include <linux/vmalloc.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/configfs.h>
34 #include <linux/ctype.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi_host.h>
37 #include <target/target_core_base.h>
38 #include <target/target_core_fabric.h>
39 
40 #include "qla_def.h"
41 #include "qla_target.h"
42 #include "tcm_qla2xxx.h"
43 
44 static struct workqueue_struct *tcm_qla2xxx_free_wq;
45 
46 /*
47  * Parse WWN.
48  * If strict, we require lower-case hex and colon separators to be sure
49  * the name is the same as what would be generated by ft_format_wwn()
50  * so the name and wwn are mapped one-to-one.
51  */
52 static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
53 {
54 	const char *cp;
55 	char c;
56 	u32 nibble;
57 	u32 byte = 0;
58 	u32 pos = 0;
59 	u32 err;
60 
61 	*wwn = 0;
62 	for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
63 		c = *cp;
64 		if (c == '\n' && cp[1] == '\0')
65 			continue;
66 		if (strict && pos++ == 2 && byte++ < 7) {
67 			pos = 0;
68 			if (c == ':')
69 				continue;
70 			err = 1;
71 			goto fail;
72 		}
73 		if (c == '\0') {
74 			err = 2;
75 			if (strict && byte != 8)
76 				goto fail;
77 			return cp - name;
78 		}
79 		err = 3;
80 		if (isdigit(c))
81 			nibble = c - '0';
82 		else if (isxdigit(c) && (islower(c) || !strict))
83 			nibble = tolower(c) - 'a' + 10;
84 		else
85 			goto fail;
86 		*wwn = (*wwn << 4) | nibble;
87 	}
88 	err = 4;
89 fail:
90 	pr_debug("err %u len %zu pos %u byte %u\n",
91 			err, cp - name, pos, byte);
92 	return -1;
93 }
94 
95 static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
96 {
97 	u8 b[8];
98 
99 	put_unaligned_be64(wwn, b);
100 	return snprintf(buf, len,
101 		"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
102 		b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
103 }
104 
105 /*
106  * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
107  */
108 static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
109 {
110 	unsigned int i, j;
111 	u8 wwn[8];
112 
113 	memset(wwn, 0, sizeof(wwn));
114 
115 	/* Validate and store the new name */
116 	for (i = 0, j = 0; i < 16; i++) {
117 		int value;
118 
119 		value = hex_to_bin(*ns++);
120 		if (value >= 0)
121 			j = (j << 4) | value;
122 		else
123 			return -EINVAL;
124 
125 		if (i % 2) {
126 			wwn[i/2] = j & 0xff;
127 			j = 0;
128 		}
129 	}
130 
131 	*nm = wwn_to_u64(wwn);
132 	return 0;
133 }
134 
135 /*
136  * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
137  * store_fc_host_vport_create()
138  */
139 static int tcm_qla2xxx_npiv_parse_wwn(
140 	const char *name,
141 	size_t count,
142 	u64 *wwpn,
143 	u64 *wwnn)
144 {
145 	unsigned int cnt = count;
146 	int rc;
147 
148 	*wwpn = 0;
149 	*wwnn = 0;
150 
151 	/* count may include a LF at end of string */
152 	if (name[cnt-1] == '\n' || name[cnt-1] == 0)
153 		cnt--;
154 
155 	/* validate we have enough characters for WWPN */
156 	if ((cnt != (16+1+16)) || (name[16] != ':'))
157 		return -EINVAL;
158 
159 	rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
160 	if (rc != 0)
161 		return rc;
162 
163 	rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
164 	if (rc != 0)
165 		return rc;
166 
167 	return 0;
168 }
169 
170 static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
171 {
172 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
173 				struct tcm_qla2xxx_tpg, se_tpg);
174 	struct tcm_qla2xxx_lport *lport = tpg->lport;
175 
176 	return lport->lport_naa_name;
177 }
178 
179 static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
180 {
181 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
182 				struct tcm_qla2xxx_tpg, se_tpg);
183 	return tpg->lport_tpgt;
184 }
185 
186 static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
187 {
188 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
189 				struct tcm_qla2xxx_tpg, se_tpg);
190 
191 	return tpg->tpg_attrib.generate_node_acls;
192 }
193 
194 static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
195 {
196 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
197 				struct tcm_qla2xxx_tpg, se_tpg);
198 
199 	return tpg->tpg_attrib.cache_dynamic_acls;
200 }
201 
202 static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
203 {
204 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
205 				struct tcm_qla2xxx_tpg, se_tpg);
206 
207 	return tpg->tpg_attrib.demo_mode_write_protect;
208 }
209 
210 static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
211 {
212 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
213 				struct tcm_qla2xxx_tpg, se_tpg);
214 
215 	return tpg->tpg_attrib.prod_mode_write_protect;
216 }
217 
218 static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
219 {
220 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
221 				struct tcm_qla2xxx_tpg, se_tpg);
222 
223 	return tpg->tpg_attrib.demo_mode_login_only;
224 }
225 
226 static int tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group *se_tpg)
227 {
228 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
229 				struct tcm_qla2xxx_tpg, se_tpg);
230 
231 	return tpg->tpg_attrib.fabric_prot_type;
232 }
233 
234 static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
235 {
236 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
237 				struct tcm_qla2xxx_tpg, se_tpg);
238 
239 	return tpg->lport_tpgt;
240 }
241 
242 static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
243 {
244 	struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
245 			struct qla_tgt_mgmt_cmd, free_work);
246 
247 	transport_generic_free_cmd(&mcmd->se_cmd, 0);
248 }
249 
250 /*
251  * Called from qla_target_template->free_mcmd(), and will call
252  * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
253  * release callback.  qla_hw_data->hardware_lock is expected to be held
254  */
255 static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
256 {
257 	INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
258 	queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
259 }
260 
261 static void tcm_qla2xxx_complete_free(struct work_struct *work)
262 {
263 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
264 
265 	cmd->cmd_in_wq = 0;
266 
267 	WARN_ON(cmd->trc_flags & TRC_CMD_FREE);
268 
269 	/* To do: protect all tgt_counters manipulations with proper locking. */
270 	cmd->qpair->tgt_counters.qla_core_ret_sta_ctio++;
271 	cmd->trc_flags |= TRC_CMD_FREE;
272 	cmd->cmd_sent_to_fw = 0;
273 
274 	transport_generic_free_cmd(&cmd->se_cmd, 0);
275 }
276 
277 /*
278  * Called from qla_target_template->free_cmd(), and will call
279  * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
280  * release callback.  qla_hw_data->hardware_lock is expected to be held
281  */
282 static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
283 {
284 	cmd->qpair->tgt_counters.core_qla_free_cmd++;
285 	cmd->cmd_in_wq = 1;
286 
287 	WARN_ON(cmd->trc_flags & TRC_CMD_DONE);
288 	cmd->trc_flags |= TRC_CMD_DONE;
289 
290 	INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
291 	queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
292 }
293 
294 /*
295  * Called from struct target_core_fabric_ops->check_stop_free() context
296  */
297 static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
298 {
299 	struct qla_tgt_cmd *cmd;
300 
301 	if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
302 		cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
303 		cmd->trc_flags |= TRC_CMD_CHK_STOP;
304 	}
305 
306 	return target_put_sess_cmd(se_cmd);
307 }
308 
309 /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
310  * fabric descriptor @se_cmd command to release
311  */
312 static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
313 {
314 	struct qla_tgt_cmd *cmd;
315 
316 	if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
317 		struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
318 				struct qla_tgt_mgmt_cmd, se_cmd);
319 		qlt_free_mcmd(mcmd);
320 		return;
321 	}
322 	cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
323 
324 	if (WARN_ON(cmd->cmd_sent_to_fw))
325 		return;
326 
327 	qlt_free_cmd(cmd);
328 }
329 
330 static void tcm_qla2xxx_release_session(struct kref *kref)
331 {
332 	struct fc_port  *sess = container_of(kref,
333 	    struct fc_port, sess_kref);
334 
335 	qlt_unreg_sess(sess);
336 }
337 
338 static void tcm_qla2xxx_put_sess(struct fc_port *sess)
339 {
340 	if (!sess)
341 		return;
342 
343 	kref_put(&sess->sess_kref, tcm_qla2xxx_release_session);
344 }
345 
346 static void tcm_qla2xxx_close_session(struct se_session *se_sess)
347 {
348 	struct fc_port *sess = se_sess->fabric_sess_ptr;
349 	struct scsi_qla_host *vha;
350 	unsigned long flags;
351 
352 	BUG_ON(!sess);
353 	vha = sess->vha;
354 
355 	spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
356 	target_sess_cmd_list_set_waiting(se_sess);
357 	spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
358 
359 	tcm_qla2xxx_put_sess(sess);
360 }
361 
362 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
363 {
364 	return 0;
365 }
366 
367 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
368 {
369 	struct qla_tgt_cmd *cmd = container_of(se_cmd,
370 				struct qla_tgt_cmd, se_cmd);
371 
372 	if (cmd->aborted) {
373 		/* Cmd can loop during Q-full.  tcm_qla2xxx_aborted_task
374 		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
375 		 * already kick start the free.
376 		 */
377 		pr_debug("write_pending aborted cmd[%p] refcount %d "
378 			"transport_state %x, t_state %x, se_cmd_flags %x\n",
379 			cmd, kref_read(&cmd->se_cmd.cmd_kref),
380 			cmd->se_cmd.transport_state,
381 			cmd->se_cmd.t_state,
382 			cmd->se_cmd.se_cmd_flags);
383 		transport_generic_request_failure(&cmd->se_cmd,
384 			TCM_CHECK_CONDITION_ABORT_CMD);
385 		return 0;
386 	}
387 	cmd->trc_flags |= TRC_XFR_RDY;
388 	cmd->bufflen = se_cmd->data_length;
389 	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
390 
391 	cmd->sg_cnt = se_cmd->t_data_nents;
392 	cmd->sg = se_cmd->t_data_sg;
393 
394 	cmd->prot_sg_cnt = se_cmd->t_prot_nents;
395 	cmd->prot_sg = se_cmd->t_prot_sg;
396 	cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
397 	se_cmd->pi_err = 0;
398 
399 	/*
400 	 * qla_target.c:qlt_rdy_to_xfer() will call dma_map_sg() to setup
401 	 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
402 	 */
403 	return qlt_rdy_to_xfer(cmd);
404 }
405 
406 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
407 {
408 	return;
409 }
410 
411 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
412 {
413 	if (!(se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
414 		struct qla_tgt_cmd *cmd = container_of(se_cmd,
415 				struct qla_tgt_cmd, se_cmd);
416 		return cmd->state;
417 	}
418 
419 	return 0;
420 }
421 
422 /*
423  * Called from process context in qla_target.c:qlt_do_work() code
424  */
425 static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
426 	unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
427 	int data_dir, int bidi)
428 {
429 	struct se_cmd *se_cmd = &cmd->se_cmd;
430 	struct se_session *se_sess;
431 	struct fc_port *sess;
432 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
433 	struct se_portal_group *se_tpg;
434 	struct tcm_qla2xxx_tpg *tpg;
435 #endif
436 	int flags = TARGET_SCF_ACK_KREF;
437 
438 	if (bidi)
439 		flags |= TARGET_SCF_BIDI_OP;
440 
441 	if (se_cmd->cpuid != WORK_CPU_UNBOUND)
442 		flags |= TARGET_SCF_USE_CPUID;
443 
444 	sess = cmd->sess;
445 	if (!sess) {
446 		pr_err("Unable to locate struct fc_port from qla_tgt_cmd\n");
447 		return -EINVAL;
448 	}
449 
450 	se_sess = sess->se_sess;
451 	if (!se_sess) {
452 		pr_err("Unable to locate active struct se_session\n");
453 		return -EINVAL;
454 	}
455 
456 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
457 	se_tpg = se_sess->se_tpg;
458 	tpg = container_of(se_tpg, struct tcm_qla2xxx_tpg, se_tpg);
459 	if (unlikely(tpg->tpg_attrib.jam_host)) {
460 		/* return, and dont run target_submit_cmd,discarding command */
461 		return 0;
462 	}
463 #endif
464 
465 	cmd->qpair->tgt_counters.qla_core_sbt_cmd++;
466 	return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
467 				cmd->unpacked_lun, data_length, fcp_task_attr,
468 				data_dir, flags);
469 }
470 
471 static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
472 {
473 	struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
474 
475 	/*
476 	 * Ensure that the complete FCP WRITE payload has been received.
477 	 * Otherwise return an exception via CHECK_CONDITION status.
478 	 */
479 	cmd->cmd_in_wq = 0;
480 	cmd->cmd_sent_to_fw = 0;
481 	if (cmd->aborted) {
482 		transport_generic_request_failure(&cmd->se_cmd,
483 			TCM_CHECK_CONDITION_ABORT_CMD);
484 		return;
485 	}
486 
487 	cmd->qpair->tgt_counters.qla_core_ret_ctio++;
488 	if (!cmd->write_data_transferred) {
489 		switch (cmd->dif_err_code) {
490 		case DIF_ERR_GRD:
491 			cmd->se_cmd.pi_err =
492 			    TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
493 			break;
494 		case DIF_ERR_REF:
495 			cmd->se_cmd.pi_err =
496 			    TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
497 			break;
498 		case DIF_ERR_APP:
499 			cmd->se_cmd.pi_err =
500 			    TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
501 			break;
502 		case DIF_ERR_NONE:
503 		default:
504 			break;
505 		}
506 
507 		if (cmd->se_cmd.pi_err)
508 			transport_generic_request_failure(&cmd->se_cmd,
509 				cmd->se_cmd.pi_err);
510 		else
511 			transport_generic_request_failure(&cmd->se_cmd,
512 				TCM_CHECK_CONDITION_ABORT_CMD);
513 
514 		return;
515 	}
516 
517 	return target_execute_cmd(&cmd->se_cmd);
518 }
519 
520 /*
521  * Called from qla_target.c:qlt_do_ctio_completion()
522  */
523 static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
524 {
525 	cmd->trc_flags |= TRC_DATA_IN;
526 	cmd->cmd_in_wq = 1;
527 	INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
528 	queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
529 }
530 
531 static int tcm_qla2xxx_chk_dif_tags(uint32_t tag)
532 {
533 	return 0;
534 }
535 
536 static int tcm_qla2xxx_dif_tags(struct qla_tgt_cmd *cmd,
537     uint16_t *pfw_prot_opts)
538 {
539 	struct se_cmd *se_cmd = &cmd->se_cmd;
540 
541 	if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
542 		*pfw_prot_opts |= PO_DISABLE_GUARD_CHECK;
543 
544 	if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG))
545 		*pfw_prot_opts |= PO_DIS_APP_TAG_VALD;
546 
547 	return 0;
548 }
549 
550 /*
551  * Called from qla_target.c:qlt_issue_task_mgmt()
552  */
553 static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, u64 lun,
554 	uint16_t tmr_func, uint32_t tag)
555 {
556 	struct fc_port *sess = mcmd->sess;
557 	struct se_cmd *se_cmd = &mcmd->se_cmd;
558 	int transl_tmr_func = 0;
559 	int flags = TARGET_SCF_ACK_KREF;
560 
561 	switch (tmr_func) {
562 	case QLA_TGT_ABTS:
563 		pr_debug("%ld: ABTS received\n", sess->vha->host_no);
564 		transl_tmr_func = TMR_ABORT_TASK;
565 		flags |= TARGET_SCF_LOOKUP_LUN_FROM_TAG;
566 		break;
567 	case QLA_TGT_2G_ABORT_TASK:
568 		pr_debug("%ld: 2G Abort Task received\n", sess->vha->host_no);
569 		transl_tmr_func = TMR_ABORT_TASK;
570 		break;
571 	case QLA_TGT_CLEAR_ACA:
572 		pr_debug("%ld: CLEAR_ACA received\n", sess->vha->host_no);
573 		transl_tmr_func = TMR_CLEAR_ACA;
574 		break;
575 	case QLA_TGT_TARGET_RESET:
576 		pr_debug("%ld: TARGET_RESET received\n", sess->vha->host_no);
577 		transl_tmr_func = TMR_TARGET_WARM_RESET;
578 		break;
579 	case QLA_TGT_LUN_RESET:
580 		pr_debug("%ld: LUN_RESET received\n", sess->vha->host_no);
581 		transl_tmr_func = TMR_LUN_RESET;
582 		break;
583 	case QLA_TGT_CLEAR_TS:
584 		pr_debug("%ld: CLEAR_TS received\n", sess->vha->host_no);
585 		transl_tmr_func = TMR_CLEAR_TASK_SET;
586 		break;
587 	case QLA_TGT_ABORT_TS:
588 		pr_debug("%ld: ABORT_TS received\n", sess->vha->host_no);
589 		transl_tmr_func = TMR_ABORT_TASK_SET;
590 		break;
591 	default:
592 		pr_debug("%ld: Unknown task mgmt fn 0x%x\n",
593 		    sess->vha->host_no, tmr_func);
594 		return -ENOSYS;
595 	}
596 
597 	return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
598 	    transl_tmr_func, GFP_ATOMIC, tag, flags);
599 }
600 
601 static struct qla_tgt_cmd *tcm_qla2xxx_find_cmd_by_tag(struct fc_port *sess,
602     uint64_t tag)
603 {
604 	struct qla_tgt_cmd *cmd = NULL;
605 	struct se_cmd *secmd;
606 	unsigned long flags;
607 
608 	if (!sess->se_sess)
609 		return NULL;
610 
611 	spin_lock_irqsave(&sess->se_sess->sess_cmd_lock, flags);
612 	list_for_each_entry(secmd, &sess->se_sess->sess_cmd_list, se_cmd_list) {
613 		/* skip task management functions, including tmr->task_cmd */
614 		if (secmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
615 			continue;
616 
617 		if (secmd->tag == tag) {
618 			cmd = container_of(secmd, struct qla_tgt_cmd, se_cmd);
619 			break;
620 		}
621 	}
622 	spin_unlock_irqrestore(&sess->se_sess->sess_cmd_lock, flags);
623 
624 	return cmd;
625 }
626 
627 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
628 {
629 	struct qla_tgt_cmd *cmd = container_of(se_cmd,
630 				struct qla_tgt_cmd, se_cmd);
631 
632 	if (cmd->aborted) {
633 		/* Cmd can loop during Q-full.  tcm_qla2xxx_aborted_task
634 		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
635 		 * already kick start the free.
636 		 */
637 		pr_debug("queue_data_in aborted cmd[%p] refcount %d "
638 			"transport_state %x, t_state %x, se_cmd_flags %x\n",
639 			cmd, kref_read(&cmd->se_cmd.cmd_kref),
640 			cmd->se_cmd.transport_state,
641 			cmd->se_cmd.t_state,
642 			cmd->se_cmd.se_cmd_flags);
643 		return 0;
644 	}
645 
646 	cmd->trc_flags |= TRC_XMIT_DATA;
647 	cmd->bufflen = se_cmd->data_length;
648 	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
649 
650 	cmd->sg_cnt = se_cmd->t_data_nents;
651 	cmd->sg = se_cmd->t_data_sg;
652 	cmd->offset = 0;
653 
654 	cmd->prot_sg_cnt = se_cmd->t_prot_nents;
655 	cmd->prot_sg = se_cmd->t_prot_sg;
656 	cmd->blk_sz  = se_cmd->se_dev->dev_attrib.block_size;
657 	se_cmd->pi_err = 0;
658 
659 	/*
660 	 * Now queue completed DATA_IN the qla2xxx LLD and response ring
661 	 */
662 	return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
663 				se_cmd->scsi_status);
664 }
665 
666 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
667 {
668 	struct qla_tgt_cmd *cmd = container_of(se_cmd,
669 				struct qla_tgt_cmd, se_cmd);
670 	int xmit_type = QLA_TGT_XMIT_STATUS;
671 
672 	if (cmd->aborted) {
673 		/*
674 		 * Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
675 		 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
676 		 * already kick start the free.
677 		 */
678 		pr_debug(
679 		    "queue_data_in aborted cmd[%p] refcount %d transport_state %x, t_state %x, se_cmd_flags %x\n",
680 		    cmd, kref_read(&cmd->se_cmd.cmd_kref),
681 		    cmd->se_cmd.transport_state, cmd->se_cmd.t_state,
682 		    cmd->se_cmd.se_cmd_flags);
683 		return 0;
684 	}
685 	cmd->bufflen = se_cmd->data_length;
686 	cmd->sg = NULL;
687 	cmd->sg_cnt = 0;
688 	cmd->offset = 0;
689 	cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
690 	cmd->trc_flags |= TRC_XMIT_STATUS;
691 
692 	if (se_cmd->data_direction == DMA_FROM_DEVICE) {
693 		/*
694 		 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
695 		 * for qla_tgt_xmit_response LLD code
696 		 */
697 		if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
698 			se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
699 			se_cmd->residual_count = 0;
700 		}
701 		se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
702 		se_cmd->residual_count += se_cmd->data_length;
703 
704 		cmd->bufflen = 0;
705 	}
706 	/*
707 	 * Now queue status response to qla2xxx LLD code and response ring
708 	 */
709 	return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
710 }
711 
712 static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
713 {
714 	struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
715 	struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
716 				struct qla_tgt_mgmt_cmd, se_cmd);
717 
718 	pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
719 			mcmd, se_tmr->function, se_tmr->response);
720 	/*
721 	 * Do translation between TCM TM response codes and
722 	 * QLA2xxx FC TM response codes.
723 	 */
724 	switch (se_tmr->response) {
725 	case TMR_FUNCTION_COMPLETE:
726 		mcmd->fc_tm_rsp = FC_TM_SUCCESS;
727 		break;
728 	case TMR_TASK_DOES_NOT_EXIST:
729 		mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
730 		break;
731 	case TMR_FUNCTION_REJECTED:
732 		mcmd->fc_tm_rsp = FC_TM_REJECT;
733 		break;
734 	case TMR_LUN_DOES_NOT_EXIST:
735 	default:
736 		mcmd->fc_tm_rsp = FC_TM_FAILED;
737 		break;
738 	}
739 	/*
740 	 * Queue the TM response to QLA2xxx LLD to build a
741 	 * CTIO response packet.
742 	 */
743 	qlt_xmit_tm_rsp(mcmd);
744 }
745 
746 static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
747 {
748 	struct qla_tgt_cmd *cmd = container_of(se_cmd,
749 				struct qla_tgt_cmd, se_cmd);
750 
751 	if (qlt_abort_cmd(cmd))
752 		return;
753 }
754 
755 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
756 			struct tcm_qla2xxx_nacl *, struct fc_port *);
757 /*
758  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
759  */
760 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port *sess)
761 {
762 	struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
763 	struct se_portal_group *se_tpg = se_nacl->se_tpg;
764 	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
765 	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
766 				struct tcm_qla2xxx_lport, lport_wwn);
767 	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
768 				struct tcm_qla2xxx_nacl, se_node_acl);
769 	void *node;
770 
771 	pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
772 
773 	node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
774 	if (WARN_ON(node && (node != se_nacl))) {
775 		/*
776 		 * The nacl no longer matches what we think it should be.
777 		 * Most likely a new dynamic acl has been added while
778 		 * someone dropped the hardware lock.  It clearly is a
779 		 * bug elsewhere, but this bit can't make things worse.
780 		 */
781 		btree_insert32(&lport->lport_fcport_map, nacl->nport_id,
782 			       node, GFP_ATOMIC);
783 	}
784 
785 	pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
786 	    se_nacl, nacl->nport_wwnn, nacl->nport_id);
787 	/*
788 	 * Now clear the se_nacl and session pointers from our HW lport lookup
789 	 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
790 	 *
791 	 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
792 	 * target_wait_for_sess_cmds() before the session waits for outstanding
793 	 * I/O to complete, to avoid a race between session shutdown execution
794 	 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
795 	 */
796 	tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
797 }
798 
799 static void tcm_qla2xxx_shutdown_sess(struct fc_port *sess)
800 {
801 	target_sess_cmd_list_set_waiting(sess->se_sess);
802 }
803 
804 static int tcm_qla2xxx_init_nodeacl(struct se_node_acl *se_nacl,
805 		const char *name)
806 {
807 	struct tcm_qla2xxx_nacl *nacl =
808 		container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
809 	u64 wwnn;
810 
811 	if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
812 		return -EINVAL;
813 
814 	nacl->nport_wwnn = wwnn;
815 	tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
816 
817 	return 0;
818 }
819 
820 /* Start items for tcm_qla2xxx_tpg_attrib_cit */
821 
822 #define DEF_QLA_TPG_ATTRIB(name)					\
823 									\
824 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_show(			\
825 		struct config_item *item, char *page)			\
826 {									\
827 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
828 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,		\
829 			struct tcm_qla2xxx_tpg, se_tpg);		\
830 									\
831 	return sprintf(page, "%u\n", tpg->tpg_attrib.name);	\
832 }									\
833 									\
834 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_store(			\
835 		struct config_item *item, const char *page, size_t count) \
836 {									\
837 	struct se_portal_group *se_tpg = attrib_to_tpg(item);		\
838 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,		\
839 			struct tcm_qla2xxx_tpg, se_tpg);		\
840 	struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib;		\
841 	unsigned long val;						\
842 	int ret;							\
843 									\
844 	ret = kstrtoul(page, 0, &val);					\
845 	if (ret < 0) {							\
846 		pr_err("kstrtoul() failed with"				\
847 				" ret: %d\n", ret);			\
848 		return -EINVAL;						\
849 	}								\
850 									\
851 	if ((val != 0) && (val != 1)) {					\
852 		pr_err("Illegal boolean value %lu\n", val);		\
853 		return -EINVAL;						\
854 	}								\
855 									\
856 	a->name = val;							\
857 									\
858 	return count;							\
859 }									\
860 CONFIGFS_ATTR(tcm_qla2xxx_tpg_attrib_, name)
861 
862 DEF_QLA_TPG_ATTRIB(generate_node_acls);
863 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
864 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
865 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
866 DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
867 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
868 DEF_QLA_TPG_ATTRIB(jam_host);
869 #endif
870 
871 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
872 	&tcm_qla2xxx_tpg_attrib_attr_generate_node_acls,
873 	&tcm_qla2xxx_tpg_attrib_attr_cache_dynamic_acls,
874 	&tcm_qla2xxx_tpg_attrib_attr_demo_mode_write_protect,
875 	&tcm_qla2xxx_tpg_attrib_attr_prod_mode_write_protect,
876 	&tcm_qla2xxx_tpg_attrib_attr_demo_mode_login_only,
877 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
878 	&tcm_qla2xxx_tpg_attrib_attr_jam_host,
879 #endif
880 	NULL,
881 };
882 
883 /* End items for tcm_qla2xxx_tpg_attrib_cit */
884 
885 static ssize_t tcm_qla2xxx_tpg_enable_show(struct config_item *item,
886 		char *page)
887 {
888 	struct se_portal_group *se_tpg = to_tpg(item);
889 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
890 			struct tcm_qla2xxx_tpg, se_tpg);
891 
892 	return snprintf(page, PAGE_SIZE, "%d\n",
893 			atomic_read(&tpg->lport_tpg_enabled));
894 }
895 
896 static ssize_t tcm_qla2xxx_tpg_enable_store(struct config_item *item,
897 		const char *page, size_t count)
898 {
899 	struct se_portal_group *se_tpg = to_tpg(item);
900 	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
901 	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
902 			struct tcm_qla2xxx_lport, lport_wwn);
903 	struct scsi_qla_host *vha = lport->qla_vha;
904 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
905 			struct tcm_qla2xxx_tpg, se_tpg);
906 	unsigned long op;
907 	int rc;
908 
909 	rc = kstrtoul(page, 0, &op);
910 	if (rc < 0) {
911 		pr_err("kstrtoul() returned %d\n", rc);
912 		return -EINVAL;
913 	}
914 	if ((op != 1) && (op != 0)) {
915 		pr_err("Illegal value for tpg_enable: %lu\n", op);
916 		return -EINVAL;
917 	}
918 	if (op) {
919 		if (atomic_read(&tpg->lport_tpg_enabled))
920 			return -EEXIST;
921 
922 		atomic_set(&tpg->lport_tpg_enabled, 1);
923 		qlt_enable_vha(vha);
924 	} else {
925 		if (!atomic_read(&tpg->lport_tpg_enabled))
926 			return count;
927 
928 		atomic_set(&tpg->lport_tpg_enabled, 0);
929 		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
930 	}
931 
932 	return count;
933 }
934 
935 static ssize_t tcm_qla2xxx_tpg_dynamic_sessions_show(struct config_item *item,
936 		char *page)
937 {
938 	return target_show_dynamic_sessions(to_tpg(item), page);
939 }
940 
941 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_store(struct config_item *item,
942 		const char *page, size_t count)
943 {
944 	struct se_portal_group *se_tpg = to_tpg(item);
945 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
946 				struct tcm_qla2xxx_tpg, se_tpg);
947 	unsigned long val;
948 	int ret = kstrtoul(page, 0, &val);
949 
950 	if (ret) {
951 		pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
952 		return ret;
953 	}
954 	if (val != 0 && val != 1 && val != 3) {
955 		pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
956 		return -EINVAL;
957 	}
958 	tpg->tpg_attrib.fabric_prot_type = val;
959 
960 	return count;
961 }
962 
963 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_show(struct config_item *item,
964 		char *page)
965 {
966 	struct se_portal_group *se_tpg = to_tpg(item);
967 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
968 				struct tcm_qla2xxx_tpg, se_tpg);
969 
970 	return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
971 }
972 
973 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, enable);
974 CONFIGFS_ATTR_RO(tcm_qla2xxx_tpg_, dynamic_sessions);
975 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, fabric_prot_type);
976 
977 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
978 	&tcm_qla2xxx_tpg_attr_enable,
979 	&tcm_qla2xxx_tpg_attr_dynamic_sessions,
980 	&tcm_qla2xxx_tpg_attr_fabric_prot_type,
981 	NULL,
982 };
983 
984 static struct se_portal_group *tcm_qla2xxx_make_tpg(struct se_wwn *wwn,
985 						    const char *name)
986 {
987 	struct tcm_qla2xxx_lport *lport = container_of(wwn,
988 			struct tcm_qla2xxx_lport, lport_wwn);
989 	struct tcm_qla2xxx_tpg *tpg;
990 	unsigned long tpgt;
991 	int ret;
992 
993 	if (strstr(name, "tpgt_") != name)
994 		return ERR_PTR(-EINVAL);
995 	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
996 		return ERR_PTR(-EINVAL);
997 
998 	if ((tpgt != 1)) {
999 		pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1000 		return ERR_PTR(-ENOSYS);
1001 	}
1002 
1003 	tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1004 	if (!tpg) {
1005 		pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1006 		return ERR_PTR(-ENOMEM);
1007 	}
1008 	tpg->lport = lport;
1009 	tpg->lport_tpgt = tpgt;
1010 	/*
1011 	 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1012 	 * NodeACLs
1013 	 */
1014 	tpg->tpg_attrib.generate_node_acls = 1;
1015 	tpg->tpg_attrib.demo_mode_write_protect = 1;
1016 	tpg->tpg_attrib.cache_dynamic_acls = 1;
1017 	tpg->tpg_attrib.demo_mode_login_only = 1;
1018 	tpg->tpg_attrib.jam_host = 0;
1019 
1020 	ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1021 	if (ret < 0) {
1022 		kfree(tpg);
1023 		return NULL;
1024 	}
1025 
1026 	lport->tpg_1 = tpg;
1027 
1028 	return &tpg->se_tpg;
1029 }
1030 
1031 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1032 {
1033 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1034 			struct tcm_qla2xxx_tpg, se_tpg);
1035 	struct tcm_qla2xxx_lport *lport = tpg->lport;
1036 	struct scsi_qla_host *vha = lport->qla_vha;
1037 	/*
1038 	 * Call into qla2x_target.c LLD logic to shutdown the active
1039 	 * FC Nexuses and disable target mode operation for this qla_hw_data
1040 	 */
1041 	if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
1042 		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1043 
1044 	core_tpg_deregister(se_tpg);
1045 	/*
1046 	 * Clear local TPG=1 pointer for non NPIV mode.
1047 	 */
1048 	lport->tpg_1 = NULL;
1049 	kfree(tpg);
1050 }
1051 
1052 static ssize_t tcm_qla2xxx_npiv_tpg_enable_show(struct config_item *item,
1053 		char *page)
1054 {
1055 	return tcm_qla2xxx_tpg_enable_show(item, page);
1056 }
1057 
1058 static ssize_t tcm_qla2xxx_npiv_tpg_enable_store(struct config_item *item,
1059 		const char *page, size_t count)
1060 {
1061 	struct se_portal_group *se_tpg = to_tpg(item);
1062 	struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1063 	struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1064 			struct tcm_qla2xxx_lport, lport_wwn);
1065 	struct scsi_qla_host *vha = lport->qla_vha;
1066 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1067 			struct tcm_qla2xxx_tpg, se_tpg);
1068 	unsigned long op;
1069 	int rc;
1070 
1071 	rc = kstrtoul(page, 0, &op);
1072 	if (rc < 0) {
1073 		pr_err("kstrtoul() returned %d\n", rc);
1074 		return -EINVAL;
1075 	}
1076 	if ((op != 1) && (op != 0)) {
1077 		pr_err("Illegal value for tpg_enable: %lu\n", op);
1078 		return -EINVAL;
1079 	}
1080 	if (op) {
1081 		if (atomic_read(&tpg->lport_tpg_enabled))
1082 			return -EEXIST;
1083 
1084 		atomic_set(&tpg->lport_tpg_enabled, 1);
1085 		qlt_enable_vha(vha);
1086 	} else {
1087 		if (!atomic_read(&tpg->lport_tpg_enabled))
1088 			return count;
1089 
1090 		atomic_set(&tpg->lport_tpg_enabled, 0);
1091 		qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1092 	}
1093 
1094 	return count;
1095 }
1096 
1097 CONFIGFS_ATTR(tcm_qla2xxx_npiv_tpg_, enable);
1098 
1099 static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
1100         &tcm_qla2xxx_npiv_tpg_attr_enable,
1101         NULL,
1102 };
1103 
1104 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(struct se_wwn *wwn,
1105 							 const char *name)
1106 {
1107 	struct tcm_qla2xxx_lport *lport = container_of(wwn,
1108 			struct tcm_qla2xxx_lport, lport_wwn);
1109 	struct tcm_qla2xxx_tpg *tpg;
1110 	unsigned long tpgt;
1111 	int ret;
1112 
1113 	if (strstr(name, "tpgt_") != name)
1114 		return ERR_PTR(-EINVAL);
1115 	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1116 		return ERR_PTR(-EINVAL);
1117 
1118 	tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1119 	if (!tpg) {
1120 		pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1121 		return ERR_PTR(-ENOMEM);
1122 	}
1123 	tpg->lport = lport;
1124 	tpg->lport_tpgt = tpgt;
1125 
1126 	/*
1127 	 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1128 	 * NodeACLs
1129 	 */
1130 	tpg->tpg_attrib.generate_node_acls = 1;
1131 	tpg->tpg_attrib.demo_mode_write_protect = 1;
1132 	tpg->tpg_attrib.cache_dynamic_acls = 1;
1133 	tpg->tpg_attrib.demo_mode_login_only = 1;
1134 
1135 	ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1136 	if (ret < 0) {
1137 		kfree(tpg);
1138 		return NULL;
1139 	}
1140 	lport->tpg_1 = tpg;
1141 	return &tpg->se_tpg;
1142 }
1143 
1144 /*
1145  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1146  */
1147 static struct fc_port *tcm_qla2xxx_find_sess_by_s_id(
1148 	scsi_qla_host_t *vha,
1149 	const uint8_t *s_id)
1150 {
1151 	struct tcm_qla2xxx_lport *lport;
1152 	struct se_node_acl *se_nacl;
1153 	struct tcm_qla2xxx_nacl *nacl;
1154 	u32 key;
1155 
1156 	lport = vha->vha_tgt.target_lport_ptr;
1157 	if (!lport) {
1158 		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1159 		dump_stack();
1160 		return NULL;
1161 	}
1162 
1163 	key = sid_to_key(s_id);
1164 	pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1165 
1166 	se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1167 	if (!se_nacl) {
1168 		pr_debug("Unable to locate s_id: 0x%06x\n", key);
1169 		return NULL;
1170 	}
1171 	pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1172 	    se_nacl, se_nacl->initiatorname);
1173 
1174 	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1175 	if (!nacl->fc_port) {
1176 		pr_err("Unable to locate struct fc_port\n");
1177 		return NULL;
1178 	}
1179 
1180 	return nacl->fc_port;
1181 }
1182 
1183 /*
1184  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1185  */
1186 static void tcm_qla2xxx_set_sess_by_s_id(
1187 	struct tcm_qla2xxx_lport *lport,
1188 	struct se_node_acl *new_se_nacl,
1189 	struct tcm_qla2xxx_nacl *nacl,
1190 	struct se_session *se_sess,
1191 	struct fc_port *fc_port,
1192 	uint8_t *s_id)
1193 {
1194 	u32 key;
1195 	void *slot;
1196 	int rc;
1197 
1198 	key = sid_to_key(s_id);
1199 	pr_debug("set_sess_by_s_id: %06x\n", key);
1200 
1201 	slot = btree_lookup32(&lport->lport_fcport_map, key);
1202 	if (!slot) {
1203 		if (new_se_nacl) {
1204 			pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1205 			nacl->nport_id = key;
1206 			rc = btree_insert32(&lport->lport_fcport_map, key,
1207 					new_se_nacl, GFP_ATOMIC);
1208 			if (rc)
1209 				printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1210 				    (int)key);
1211 		} else {
1212 			pr_debug("Wiping nonexisting fc_port entry\n");
1213 		}
1214 
1215 		fc_port->se_sess = se_sess;
1216 		nacl->fc_port = fc_port;
1217 		return;
1218 	}
1219 
1220 	if (nacl->fc_port) {
1221 		if (new_se_nacl == NULL) {
1222 			pr_debug("Clearing existing nacl->fc_port and fc_port entry\n");
1223 			btree_remove32(&lport->lport_fcport_map, key);
1224 			nacl->fc_port = NULL;
1225 			return;
1226 		}
1227 		pr_debug("Replacing existing nacl->fc_port and fc_port entry\n");
1228 		btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1229 		fc_port->se_sess = se_sess;
1230 		nacl->fc_port = fc_port;
1231 		return;
1232 	}
1233 
1234 	if (new_se_nacl == NULL) {
1235 		pr_debug("Clearing existing fc_port entry\n");
1236 		btree_remove32(&lport->lport_fcport_map, key);
1237 		return;
1238 	}
1239 
1240 	pr_debug("Replacing existing fc_port entry w/o active nacl->fc_port\n");
1241 	btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1242 	fc_port->se_sess = se_sess;
1243 	nacl->fc_port = fc_port;
1244 
1245 	pr_debug("Setup nacl->fc_port %p by s_id for se_nacl: %p, initiatorname: %s\n",
1246 	    nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1247 }
1248 
1249 /*
1250  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1251  */
1252 static struct fc_port *tcm_qla2xxx_find_sess_by_loop_id(
1253 	scsi_qla_host_t *vha,
1254 	const uint16_t loop_id)
1255 {
1256 	struct tcm_qla2xxx_lport *lport;
1257 	struct se_node_acl *se_nacl;
1258 	struct tcm_qla2xxx_nacl *nacl;
1259 	struct tcm_qla2xxx_fc_loopid *fc_loopid;
1260 
1261 	lport = vha->vha_tgt.target_lport_ptr;
1262 	if (!lport) {
1263 		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1264 		dump_stack();
1265 		return NULL;
1266 	}
1267 
1268 	pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1269 
1270 	fc_loopid = lport->lport_loopid_map + loop_id;
1271 	se_nacl = fc_loopid->se_nacl;
1272 	if (!se_nacl) {
1273 		pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1274 		    loop_id);
1275 		return NULL;
1276 	}
1277 
1278 	nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1279 
1280 	if (!nacl->fc_port) {
1281 		pr_err("Unable to locate struct fc_port\n");
1282 		return NULL;
1283 	}
1284 
1285 	return nacl->fc_port;
1286 }
1287 
1288 /*
1289  * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1290  */
1291 static void tcm_qla2xxx_set_sess_by_loop_id(
1292 	struct tcm_qla2xxx_lport *lport,
1293 	struct se_node_acl *new_se_nacl,
1294 	struct tcm_qla2xxx_nacl *nacl,
1295 	struct se_session *se_sess,
1296 	struct fc_port *fc_port,
1297 	uint16_t loop_id)
1298 {
1299 	struct se_node_acl *saved_nacl;
1300 	struct tcm_qla2xxx_fc_loopid *fc_loopid;
1301 
1302 	pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1303 
1304 	fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1305 			lport->lport_loopid_map)[loop_id];
1306 
1307 	saved_nacl = fc_loopid->se_nacl;
1308 	if (!saved_nacl) {
1309 		pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1310 		fc_loopid->se_nacl = new_se_nacl;
1311 		if (fc_port->se_sess != se_sess)
1312 			fc_port->se_sess = se_sess;
1313 		if (nacl->fc_port != fc_port)
1314 			nacl->fc_port = fc_port;
1315 		return;
1316 	}
1317 
1318 	if (nacl->fc_port) {
1319 		if (new_se_nacl == NULL) {
1320 			pr_debug("Clearing nacl->fc_port and fc_loopid->se_nacl\n");
1321 			fc_loopid->se_nacl = NULL;
1322 			nacl->fc_port = NULL;
1323 			return;
1324 		}
1325 
1326 		pr_debug("Replacing existing nacl->fc_port and fc_loopid->se_nacl\n");
1327 		fc_loopid->se_nacl = new_se_nacl;
1328 		if (fc_port->se_sess != se_sess)
1329 			fc_port->se_sess = se_sess;
1330 		if (nacl->fc_port != fc_port)
1331 			nacl->fc_port = fc_port;
1332 		return;
1333 	}
1334 
1335 	if (new_se_nacl == NULL) {
1336 		pr_debug("Clearing fc_loopid->se_nacl\n");
1337 		fc_loopid->se_nacl = NULL;
1338 		return;
1339 	}
1340 
1341 	pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->fc_port\n");
1342 	fc_loopid->se_nacl = new_se_nacl;
1343 	if (fc_port->se_sess != se_sess)
1344 		fc_port->se_sess = se_sess;
1345 	if (nacl->fc_port != fc_port)
1346 		nacl->fc_port = fc_port;
1347 
1348 	pr_debug("Setup nacl->fc_port %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1349 	    nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1350 }
1351 
1352 /*
1353  * Should always be called with qla_hw_data->tgt.sess_lock held.
1354  */
1355 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1356 		struct tcm_qla2xxx_nacl *nacl, struct fc_port *sess)
1357 {
1358 	struct se_session *se_sess = sess->se_sess;
1359 	unsigned char be_sid[3];
1360 
1361 	be_sid[0] = sess->d_id.b.domain;
1362 	be_sid[1] = sess->d_id.b.area;
1363 	be_sid[2] = sess->d_id.b.al_pa;
1364 
1365 	tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1366 				sess, be_sid);
1367 	tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1368 				sess, sess->loop_id);
1369 }
1370 
1371 static void tcm_qla2xxx_free_session(struct fc_port *sess)
1372 {
1373 	struct qla_tgt *tgt = sess->tgt;
1374 	struct qla_hw_data *ha = tgt->ha;
1375 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1376 	struct se_session *se_sess;
1377 	struct tcm_qla2xxx_lport *lport;
1378 
1379 	BUG_ON(in_interrupt());
1380 
1381 	se_sess = sess->se_sess;
1382 	if (!se_sess) {
1383 		pr_err("struct fc_port->se_sess is NULL\n");
1384 		dump_stack();
1385 		return;
1386 	}
1387 
1388 	lport = vha->vha_tgt.target_lport_ptr;
1389 	if (!lport) {
1390 		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1391 		dump_stack();
1392 		return;
1393 	}
1394 	target_wait_for_sess_cmds(se_sess);
1395 
1396 	target_remove_session(se_sess);
1397 }
1398 
1399 static int tcm_qla2xxx_session_cb(struct se_portal_group *se_tpg,
1400 				  struct se_session *se_sess, void *p)
1401 {
1402 	struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1403 				struct tcm_qla2xxx_tpg, se_tpg);
1404 	struct tcm_qla2xxx_lport *lport = tpg->lport;
1405 	struct qla_hw_data *ha = lport->qla_vha->hw;
1406 	struct se_node_acl *se_nacl = se_sess->se_node_acl;
1407 	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1408 				struct tcm_qla2xxx_nacl, se_node_acl);
1409 	struct fc_port *qlat_sess = p;
1410 	uint16_t loop_id = qlat_sess->loop_id;
1411 	unsigned long flags;
1412 	unsigned char be_sid[3];
1413 
1414 	be_sid[0] = qlat_sess->d_id.b.domain;
1415 	be_sid[1] = qlat_sess->d_id.b.area;
1416 	be_sid[2] = qlat_sess->d_id.b.al_pa;
1417 
1418 	/*
1419 	 * And now setup se_nacl and session pointers into HW lport internal
1420 	 * mappings for fabric S_ID and LOOP_ID.
1421 	 */
1422 	spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1423 	tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl,
1424 				     se_sess, qlat_sess, be_sid);
1425 	tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl,
1426 					se_sess, qlat_sess, loop_id);
1427 	spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1428 
1429 	return 0;
1430 }
1431 
1432 /*
1433  * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1434  * to locate struct se_node_acl
1435  */
1436 static int tcm_qla2xxx_check_initiator_node_acl(
1437 	scsi_qla_host_t *vha,
1438 	unsigned char *fc_wwpn,
1439 	struct fc_port *qlat_sess)
1440 {
1441 	struct qla_hw_data *ha = vha->hw;
1442 	struct tcm_qla2xxx_lport *lport;
1443 	struct tcm_qla2xxx_tpg *tpg;
1444 	struct se_session *se_sess;
1445 	unsigned char port_name[36];
1446 	int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count :
1447 		       TCM_QLA2XXX_DEFAULT_TAGS;
1448 
1449 	lport = vha->vha_tgt.target_lport_ptr;
1450 	if (!lport) {
1451 		pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1452 		dump_stack();
1453 		return -EINVAL;
1454 	}
1455 	/*
1456 	 * Locate the TPG=1 reference..
1457 	 */
1458 	tpg = lport->tpg_1;
1459 	if (!tpg) {
1460 		pr_err("Unable to locate struct tcm_qla2xxx_lport->tpg_1\n");
1461 		return -EINVAL;
1462 	}
1463 	/*
1464 	 * Format the FCP Initiator port_name into colon seperated values to
1465 	 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1466 	 */
1467 	memset(&port_name, 0, 36);
1468 	snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn);
1469 	/*
1470 	 * Locate our struct se_node_acl either from an explict NodeACL created
1471 	 * via ConfigFS, or via running in TPG demo mode.
1472 	 */
1473 	se_sess = target_setup_session(&tpg->se_tpg, num_tags,
1474 				       sizeof(struct qla_tgt_cmd),
1475 				       TARGET_PROT_ALL, port_name,
1476 				       qlat_sess, tcm_qla2xxx_session_cb);
1477 	if (IS_ERR(se_sess))
1478 		return PTR_ERR(se_sess);
1479 
1480 	return 0;
1481 }
1482 
1483 static void tcm_qla2xxx_update_sess(struct fc_port *sess, port_id_t s_id,
1484 				    uint16_t loop_id, bool conf_compl_supported)
1485 {
1486 	struct qla_tgt *tgt = sess->tgt;
1487 	struct qla_hw_data *ha = tgt->ha;
1488 	scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1489 	struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
1490 	struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1491 	struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1492 			struct tcm_qla2xxx_nacl, se_node_acl);
1493 	u32 key;
1494 
1495 
1496 	if (sess->loop_id != loop_id || sess->d_id.b24 != s_id.b24)
1497 		pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1498 		    sess, sess->port_name,
1499 		    sess->loop_id, loop_id, sess->d_id.b.domain,
1500 		    sess->d_id.b.area, sess->d_id.b.al_pa, s_id.b.domain,
1501 		    s_id.b.area, s_id.b.al_pa);
1502 
1503 	if (sess->loop_id != loop_id) {
1504 		/*
1505 		 * Because we can shuffle loop IDs around and we
1506 		 * update different sessions non-atomically, we might
1507 		 * have overwritten this session's old loop ID
1508 		 * already, and we might end up overwriting some other
1509 		 * session that will be updated later.  So we have to
1510 		 * be extra careful and we can't warn about those things...
1511 		 */
1512 		if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1513 			lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1514 
1515 		lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1516 
1517 		sess->loop_id = loop_id;
1518 	}
1519 
1520 	if (sess->d_id.b24 != s_id.b24) {
1521 		key = (((u32) sess->d_id.b.domain << 16) |
1522 		       ((u32) sess->d_id.b.area   <<  8) |
1523 		       ((u32) sess->d_id.b.al_pa));
1524 
1525 		if (btree_lookup32(&lport->lport_fcport_map, key))
1526 			WARN(btree_remove32(&lport->lport_fcport_map, key) !=
1527 			    se_nacl, "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1528 			    sess->d_id.b.domain, sess->d_id.b.area,
1529 			    sess->d_id.b.al_pa);
1530 		else
1531 			WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1532 			     sess->d_id.b.domain, sess->d_id.b.area,
1533 			     sess->d_id.b.al_pa);
1534 
1535 		key = (((u32) s_id.b.domain << 16) |
1536 		       ((u32) s_id.b.area   <<  8) |
1537 		       ((u32) s_id.b.al_pa));
1538 
1539 		if (btree_lookup32(&lport->lport_fcport_map, key)) {
1540 			WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1541 			     s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1542 			btree_update32(&lport->lport_fcport_map, key, se_nacl);
1543 		} else {
1544 			btree_insert32(&lport->lport_fcport_map, key, se_nacl,
1545 			    GFP_ATOMIC);
1546 		}
1547 
1548 		sess->d_id = s_id;
1549 		nacl->nport_id = key;
1550 	}
1551 
1552 	sess->conf_compl_supported = conf_compl_supported;
1553 
1554 }
1555 
1556 /*
1557  * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1558  */
1559 static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1560 	.find_cmd_by_tag	= tcm_qla2xxx_find_cmd_by_tag,
1561 	.handle_cmd		= tcm_qla2xxx_handle_cmd,
1562 	.handle_data		= tcm_qla2xxx_handle_data,
1563 	.handle_tmr		= tcm_qla2xxx_handle_tmr,
1564 	.free_cmd		= tcm_qla2xxx_free_cmd,
1565 	.free_mcmd		= tcm_qla2xxx_free_mcmd,
1566 	.free_session		= tcm_qla2xxx_free_session,
1567 	.update_sess		= tcm_qla2xxx_update_sess,
1568 	.check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1569 	.find_sess_by_s_id	= tcm_qla2xxx_find_sess_by_s_id,
1570 	.find_sess_by_loop_id	= tcm_qla2xxx_find_sess_by_loop_id,
1571 	.clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1572 	.put_sess		= tcm_qla2xxx_put_sess,
1573 	.shutdown_sess		= tcm_qla2xxx_shutdown_sess,
1574 	.get_dif_tags		= tcm_qla2xxx_dif_tags,
1575 	.chk_dif_tags		= tcm_qla2xxx_chk_dif_tags,
1576 };
1577 
1578 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1579 {
1580 	int rc;
1581 
1582 	rc = btree_init32(&lport->lport_fcport_map);
1583 	if (rc) {
1584 		pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1585 		return rc;
1586 	}
1587 
1588 	lport->lport_loopid_map =
1589 		vzalloc(array_size(65536,
1590 				   sizeof(struct tcm_qla2xxx_fc_loopid)));
1591 	if (!lport->lport_loopid_map) {
1592 		pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1593 		    sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1594 		btree_destroy32(&lport->lport_fcport_map);
1595 		return -ENOMEM;
1596 	}
1597 	pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1598 	       sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1599 	return 0;
1600 }
1601 
1602 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
1603 					 void *target_lport_ptr,
1604 					 u64 npiv_wwpn, u64 npiv_wwnn)
1605 {
1606 	struct qla_hw_data *ha = vha->hw;
1607 	struct tcm_qla2xxx_lport *lport =
1608 			(struct tcm_qla2xxx_lport *)target_lport_ptr;
1609 	/*
1610 	 * Setup tgt_ops, local pointer to vha and target_lport_ptr
1611 	 */
1612 	ha->tgt.tgt_ops = &tcm_qla2xxx_template;
1613 	vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1614 	lport->qla_vha = vha;
1615 
1616 	return 0;
1617 }
1618 
1619 static struct se_wwn *tcm_qla2xxx_make_lport(
1620 	struct target_fabric_configfs *tf,
1621 	struct config_group *group,
1622 	const char *name)
1623 {
1624 	struct tcm_qla2xxx_lport *lport;
1625 	u64 wwpn;
1626 	int ret = -ENODEV;
1627 
1628 	if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1629 		return ERR_PTR(-EINVAL);
1630 
1631 	lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1632 	if (!lport) {
1633 		pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1634 		return ERR_PTR(-ENOMEM);
1635 	}
1636 	lport->lport_wwpn = wwpn;
1637 	tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1638 				wwpn);
1639 	sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
1640 
1641 	ret = tcm_qla2xxx_init_lport(lport);
1642 	if (ret != 0)
1643 		goto out;
1644 
1645 	ret = qlt_lport_register(lport, wwpn, 0, 0,
1646 				 tcm_qla2xxx_lport_register_cb);
1647 	if (ret != 0)
1648 		goto out_lport;
1649 
1650 	return &lport->lport_wwn;
1651 out_lport:
1652 	vfree(lport->lport_loopid_map);
1653 	btree_destroy32(&lport->lport_fcport_map);
1654 out:
1655 	kfree(lport);
1656 	return ERR_PTR(ret);
1657 }
1658 
1659 static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1660 {
1661 	struct tcm_qla2xxx_lport *lport = container_of(wwn,
1662 			struct tcm_qla2xxx_lport, lport_wwn);
1663 	struct scsi_qla_host *vha = lport->qla_vha;
1664 	struct se_node_acl *node;
1665 	u32 key = 0;
1666 
1667 	/*
1668 	 * Call into qla2x_target.c LLD logic to complete the
1669 	 * shutdown of struct qla_tgt after the call to
1670 	 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1671 	 */
1672 	if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
1673 		qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1674 
1675 	qlt_lport_deregister(vha);
1676 
1677 	vfree(lport->lport_loopid_map);
1678 	btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1679 		btree_remove32(&lport->lport_fcport_map, key);
1680 	btree_destroy32(&lport->lport_fcport_map);
1681 	kfree(lport);
1682 }
1683 
1684 static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
1685 					      void *target_lport_ptr,
1686 					      u64 npiv_wwpn, u64 npiv_wwnn)
1687 {
1688 	struct fc_vport *vport;
1689 	struct Scsi_Host *sh = base_vha->host;
1690 	struct scsi_qla_host *npiv_vha;
1691 	struct tcm_qla2xxx_lport *lport =
1692 			(struct tcm_qla2xxx_lport *)target_lport_ptr;
1693 	struct tcm_qla2xxx_lport *base_lport =
1694 			(struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
1695 	struct fc_vport_identifiers vport_id;
1696 
1697 	if (qla_ini_mode_enabled(base_vha)) {
1698 		pr_err("qla2xxx base_vha not enabled for target mode\n");
1699 		return -EPERM;
1700 	}
1701 
1702 	if (!base_lport || !base_lport->tpg_1 ||
1703 	    !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
1704 		pr_err("qla2xxx base_lport or tpg_1 not available\n");
1705 		return -EPERM;
1706 	}
1707 
1708 	memset(&vport_id, 0, sizeof(vport_id));
1709 	vport_id.port_name = npiv_wwpn;
1710 	vport_id.node_name = npiv_wwnn;
1711 	vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
1712 	vport_id.vport_type = FC_PORTTYPE_NPIV;
1713 	vport_id.disable = false;
1714 
1715 	vport = fc_vport_create(sh, 0, &vport_id);
1716 	if (!vport) {
1717 		pr_err("fc_vport_create failed for qla2xxx_npiv\n");
1718 		return -ENODEV;
1719 	}
1720 	/*
1721 	 * Setup local pointer to NPIV vhba + target_lport_ptr
1722 	 */
1723 	npiv_vha = (struct scsi_qla_host *)vport->dd_data;
1724 	npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1725 	lport->qla_vha = npiv_vha;
1726 	scsi_host_get(npiv_vha->host);
1727 	return 0;
1728 }
1729 
1730 
1731 static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1732 	struct target_fabric_configfs *tf,
1733 	struct config_group *group,
1734 	const char *name)
1735 {
1736 	struct tcm_qla2xxx_lport *lport;
1737 	u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
1738 	char *p, tmp[128];
1739 	int ret;
1740 
1741 	snprintf(tmp, 128, "%s", name);
1742 
1743 	p = strchr(tmp, '@');
1744 	if (!p) {
1745 		pr_err("Unable to locate NPIV '@' separator\n");
1746 		return ERR_PTR(-EINVAL);
1747 	}
1748 	*p++ = '\0';
1749 
1750 	if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
1751 		return ERR_PTR(-EINVAL);
1752 
1753 	if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
1754 				       &npiv_wwpn, &npiv_wwnn) < 0)
1755 		return ERR_PTR(-EINVAL);
1756 
1757 	lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1758 	if (!lport) {
1759 		pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1760 		return ERR_PTR(-ENOMEM);
1761 	}
1762 	lport->lport_npiv_wwpn = npiv_wwpn;
1763 	lport->lport_npiv_wwnn = npiv_wwnn;
1764 	sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
1765 
1766 	ret = tcm_qla2xxx_init_lport(lport);
1767 	if (ret != 0)
1768 		goto out;
1769 
1770 	ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
1771 				 tcm_qla2xxx_lport_register_npiv_cb);
1772 	if (ret != 0)
1773 		goto out_lport;
1774 
1775 	return &lport->lport_wwn;
1776 out_lport:
1777 	vfree(lport->lport_loopid_map);
1778 	btree_destroy32(&lport->lport_fcport_map);
1779 out:
1780 	kfree(lport);
1781 	return ERR_PTR(ret);
1782 }
1783 
1784 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1785 {
1786 	struct tcm_qla2xxx_lport *lport = container_of(wwn,
1787 			struct tcm_qla2xxx_lport, lport_wwn);
1788 	struct scsi_qla_host *npiv_vha = lport->qla_vha;
1789 	struct qla_hw_data *ha = npiv_vha->hw;
1790 	scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1791 
1792 	scsi_host_put(npiv_vha->host);
1793 	/*
1794 	 * Notify libfc that we want to release the vha->fc_vport
1795 	 */
1796 	fc_vport_terminate(npiv_vha->fc_vport);
1797 	scsi_host_put(base_vha->host);
1798 	kfree(lport);
1799 }
1800 
1801 
1802 static ssize_t tcm_qla2xxx_wwn_version_show(struct config_item *item,
1803 		char *page)
1804 {
1805 	return sprintf(page,
1806 	    "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on %s\n",
1807 	    QLA2XXX_VERSION, utsname()->sysname,
1808 	    utsname()->machine, utsname()->release);
1809 }
1810 
1811 CONFIGFS_ATTR_RO(tcm_qla2xxx_wwn_, version);
1812 
1813 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1814 	&tcm_qla2xxx_wwn_attr_version,
1815 	NULL,
1816 };
1817 
1818 static const struct target_core_fabric_ops tcm_qla2xxx_ops = {
1819 	.module				= THIS_MODULE,
1820 	.fabric_name			= "qla2xxx",
1821 	.node_acl_size			= sizeof(struct tcm_qla2xxx_nacl),
1822 	/*
1823 	 * XXX: Limit assumes single page per scatter-gather-list entry.
1824 	 * Current maximum is ~4.9 MB per se_cmd->t_data_sg with PAGE_SIZE=4096
1825 	 */
1826 	.max_data_sg_nents		= 1200,
1827 	.tpg_get_wwn			= tcm_qla2xxx_get_fabric_wwn,
1828 	.tpg_get_tag			= tcm_qla2xxx_get_tag,
1829 	.tpg_check_demo_mode		= tcm_qla2xxx_check_demo_mode,
1830 	.tpg_check_demo_mode_cache	= tcm_qla2xxx_check_demo_mode_cache,
1831 	.tpg_check_demo_mode_write_protect =
1832 					tcm_qla2xxx_check_demo_write_protect,
1833 	.tpg_check_prod_mode_write_protect =
1834 					tcm_qla2xxx_check_prod_write_protect,
1835 	.tpg_check_prot_fabric_only	= tcm_qla2xxx_check_prot_fabric_only,
1836 	.tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1837 	.tpg_get_inst_index		= tcm_qla2xxx_tpg_get_inst_index,
1838 	.check_stop_free		= tcm_qla2xxx_check_stop_free,
1839 	.release_cmd			= tcm_qla2xxx_release_cmd,
1840 	.close_session			= tcm_qla2xxx_close_session,
1841 	.sess_get_index			= tcm_qla2xxx_sess_get_index,
1842 	.sess_get_initiator_sid		= NULL,
1843 	.write_pending			= tcm_qla2xxx_write_pending,
1844 	.set_default_node_attributes	= tcm_qla2xxx_set_default_node_attrs,
1845 	.get_cmd_state			= tcm_qla2xxx_get_cmd_state,
1846 	.queue_data_in			= tcm_qla2xxx_queue_data_in,
1847 	.queue_status			= tcm_qla2xxx_queue_status,
1848 	.queue_tm_rsp			= tcm_qla2xxx_queue_tm_rsp,
1849 	.aborted_task			= tcm_qla2xxx_aborted_task,
1850 	/*
1851 	 * Setup function pointers for generic logic in
1852 	 * target_core_fabric_configfs.c
1853 	 */
1854 	.fabric_make_wwn		= tcm_qla2xxx_make_lport,
1855 	.fabric_drop_wwn		= tcm_qla2xxx_drop_lport,
1856 	.fabric_make_tpg		= tcm_qla2xxx_make_tpg,
1857 	.fabric_drop_tpg		= tcm_qla2xxx_drop_tpg,
1858 	.fabric_init_nodeacl		= tcm_qla2xxx_init_nodeacl,
1859 
1860 	.tfc_wwn_attrs			= tcm_qla2xxx_wwn_attrs,
1861 	.tfc_tpg_base_attrs		= tcm_qla2xxx_tpg_attrs,
1862 	.tfc_tpg_attrib_attrs		= tcm_qla2xxx_tpg_attrib_attrs,
1863 };
1864 
1865 static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1866 	.module				= THIS_MODULE,
1867 	.fabric_name			= "qla2xxx_npiv",
1868 	.node_acl_size			= sizeof(struct tcm_qla2xxx_nacl),
1869 	.tpg_get_wwn			= tcm_qla2xxx_get_fabric_wwn,
1870 	.tpg_get_tag			= tcm_qla2xxx_get_tag,
1871 	.tpg_check_demo_mode		= tcm_qla2xxx_check_demo_mode,
1872 	.tpg_check_demo_mode_cache	= tcm_qla2xxx_check_demo_mode_cache,
1873 	.tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
1874 	.tpg_check_prod_mode_write_protect =
1875 	    tcm_qla2xxx_check_prod_write_protect,
1876 	.tpg_check_demo_mode_login_only	= tcm_qla2xxx_check_demo_mode_login_only,
1877 	.tpg_get_inst_index		= tcm_qla2xxx_tpg_get_inst_index,
1878 	.check_stop_free                = tcm_qla2xxx_check_stop_free,
1879 	.release_cmd			= tcm_qla2xxx_release_cmd,
1880 	.close_session			= tcm_qla2xxx_close_session,
1881 	.sess_get_index			= tcm_qla2xxx_sess_get_index,
1882 	.sess_get_initiator_sid		= NULL,
1883 	.write_pending			= tcm_qla2xxx_write_pending,
1884 	.set_default_node_attributes	= tcm_qla2xxx_set_default_node_attrs,
1885 	.get_cmd_state			= tcm_qla2xxx_get_cmd_state,
1886 	.queue_data_in			= tcm_qla2xxx_queue_data_in,
1887 	.queue_status			= tcm_qla2xxx_queue_status,
1888 	.queue_tm_rsp			= tcm_qla2xxx_queue_tm_rsp,
1889 	.aborted_task			= tcm_qla2xxx_aborted_task,
1890 	/*
1891 	 * Setup function pointers for generic logic in
1892 	 * target_core_fabric_configfs.c
1893 	 */
1894 	.fabric_make_wwn		= tcm_qla2xxx_npiv_make_lport,
1895 	.fabric_drop_wwn		= tcm_qla2xxx_npiv_drop_lport,
1896 	.fabric_make_tpg		= tcm_qla2xxx_npiv_make_tpg,
1897 	.fabric_drop_tpg		= tcm_qla2xxx_drop_tpg,
1898 	.fabric_init_nodeacl		= tcm_qla2xxx_init_nodeacl,
1899 
1900 	.tfc_wwn_attrs			= tcm_qla2xxx_wwn_attrs,
1901 	.tfc_tpg_base_attrs		= tcm_qla2xxx_npiv_tpg_attrs,
1902 };
1903 
1904 static int tcm_qla2xxx_register_configfs(void)
1905 {
1906 	int ret;
1907 
1908 	pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on %s\n",
1909 	    QLA2XXX_VERSION, utsname()->sysname,
1910 	    utsname()->machine, utsname()->release);
1911 
1912 	ret = target_register_template(&tcm_qla2xxx_ops);
1913 	if (ret)
1914 		return ret;
1915 
1916 	ret = target_register_template(&tcm_qla2xxx_npiv_ops);
1917 	if (ret)
1918 		goto out_fabric;
1919 
1920 	tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1921 						WQ_MEM_RECLAIM, 0);
1922 	if (!tcm_qla2xxx_free_wq) {
1923 		ret = -ENOMEM;
1924 		goto out_fabric_npiv;
1925 	}
1926 
1927 	return 0;
1928 
1929 out_fabric_npiv:
1930 	target_unregister_template(&tcm_qla2xxx_npiv_ops);
1931 out_fabric:
1932 	target_unregister_template(&tcm_qla2xxx_ops);
1933 	return ret;
1934 }
1935 
1936 static void tcm_qla2xxx_deregister_configfs(void)
1937 {
1938 	destroy_workqueue(tcm_qla2xxx_free_wq);
1939 
1940 	target_unregister_template(&tcm_qla2xxx_ops);
1941 	target_unregister_template(&tcm_qla2xxx_npiv_ops);
1942 }
1943 
1944 static int __init tcm_qla2xxx_init(void)
1945 {
1946 	int ret;
1947 
1948 	ret = tcm_qla2xxx_register_configfs();
1949 	if (ret < 0)
1950 		return ret;
1951 
1952 	return 0;
1953 }
1954 
1955 static void __exit tcm_qla2xxx_exit(void)
1956 {
1957 	tcm_qla2xxx_deregister_configfs();
1958 }
1959 
1960 MODULE_DESCRIPTION("TCM QLA24XX+ series NPIV enabled fabric driver");
1961 MODULE_LICENSE("GPL");
1962 module_init(tcm_qla2xxx_init);
1963 module_exit(tcm_qla2xxx_exit);
1964