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