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