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