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