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