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