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