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