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