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