1 /*******************************************************************************
2 * This file contains tcm implementation using v4 configfs fabric infrastructure
3 * for QLogic target mode HBAs
5 * ?? Copyright 2010-2011 RisingTide Systems LLC.
7 * Licensed to the Linux Foundation under the General Public License (GPL)
10 * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
12 * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
13 * the TCM_FC / Open-FCoE.org fabric module.
15 * Copyright (c) 2010 Cisco Systems, Inc
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.
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 ****************************************************************************/
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>
41 #include <linux/string.h>
42 #include <linux/ctype.h>
43 #include <asm/unaligned.h>
44 #include <scsi/scsi.h>
45 #include <scsi/scsi_host.h>
46 #include <scsi/scsi_device.h>
47 #include <scsi/scsi_cmnd.h>
48 #include <target/target_core_base.h>
49 #include <target/target_core_fabric.h>
50 #include <target/target_core_fabric_configfs.h>
51 #include <target/target_core_configfs.h>
52 #include <target/configfs_macros.h>
55 #include "qla_target.h"
56 #include "tcm_qla2xxx.h"
58 struct workqueue_struct *tcm_qla2xxx_free_wq;
59 struct workqueue_struct *tcm_qla2xxx_cmd_wq;
61 static int tcm_qla2xxx_check_true(struct se_portal_group *se_tpg)
66 static int tcm_qla2xxx_check_false(struct se_portal_group *se_tpg)
73 * If strict, we require lower-case hex and colon separators to be sure
74 * the name is the same as what would be generated by ft_format_wwn()
75 * so the name and wwn are mapped one-to-one.
77 static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
87 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
89 if (c == '\n' && cp[1] == '\0')
91 if (strict && pos++ == 2 && byte++ < 7) {
100 if (strict && byte != 8)
107 else if (isxdigit(c) && (islower(c) || !strict))
108 nibble = tolower(c) - 'a' + 10;
111 *wwn = (*wwn << 4) | nibble;
115 pr_debug("err %u len %zu pos %u byte %u\n",
116 err, cp - name, pos, byte);
120 static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
124 put_unaligned_be64(wwn, b);
125 return snprintf(buf, len,
126 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
127 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
130 static char *tcm_qla2xxx_get_fabric_name(void)
136 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
138 static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
140 unsigned int i, j, value;
143 memset(wwn, 0, sizeof(wwn));
145 /* Validate and store the new name */
146 for (i = 0, j = 0; i < 16; i++) {
147 value = hex_to_bin(*ns++);
149 j = (j << 4) | value;
159 *nm = wwn_to_u64(wwn);
164 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
165 * store_fc_host_vport_create()
167 static int tcm_qla2xxx_npiv_parse_wwn(
173 unsigned int cnt = count;
179 /* count may include a LF at end of string */
180 if (name[cnt-1] == '\n')
183 /* validate we have enough characters for WWPN */
184 if ((cnt != (16+1+16)) || (name[16] != ':'))
187 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
191 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
198 static ssize_t tcm_qla2xxx_npiv_format_wwn(char *buf, size_t len,
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]);
212 static char *tcm_qla2xxx_npiv_get_fabric_name(void)
214 return "qla2xxx_npiv";
217 static u8 tcm_qla2xxx_get_fabric_proto_ident(struct se_portal_group *se_tpg)
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;
224 switch (lport->lport_proto_id) {
225 case SCSI_PROTOCOL_FCP:
227 proto_id = fc_get_fabric_proto_ident(se_tpg);
234 static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
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;
240 return &lport->lport_name[0];
243 static char *tcm_qla2xxx_npiv_get_fabric_wwn(struct se_portal_group *se_tpg)
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;
249 return &lport->lport_npiv_name[0];
252 static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
254 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
255 struct tcm_qla2xxx_tpg, se_tpg);
256 return tpg->lport_tpgt;
259 static u32 tcm_qla2xxx_get_default_depth(struct se_portal_group *se_tpg)
264 static 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,
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;
276 switch (lport->lport_proto_id) {
277 case SCSI_PROTOCOL_FCP:
279 ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
287 static 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,
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;
298 switch (lport->lport_proto_id) {
299 case SCSI_PROTOCOL_FCP:
301 ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
309 static char *tcm_qla2xxx_parse_pr_out_transport_id(
310 struct se_portal_group *se_tpg,
313 char **port_nexus_ptr)
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;
320 switch (lport->lport_proto_id) {
321 case SCSI_PROTOCOL_FCP:
323 tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
331 static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
333 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
334 struct tcm_qla2xxx_tpg, se_tpg);
336 return QLA_TPG_ATTRIB(tpg)->generate_node_acls;
339 static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
341 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
342 struct tcm_qla2xxx_tpg, se_tpg);
344 return QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls;
347 static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
349 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
350 struct tcm_qla2xxx_tpg, se_tpg);
352 return QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect;
355 static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
357 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
358 struct tcm_qla2xxx_tpg, se_tpg);
360 return QLA_TPG_ATTRIB(tpg)->prod_mode_write_protect;
363 static struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
364 struct se_portal_group *se_tpg)
366 struct tcm_qla2xxx_nacl *nacl;
368 nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
370 pr_err("Unable to alocate struct tcm_qla2xxx_nacl\n");
374 return &nacl->se_node_acl;
377 static void tcm_qla2xxx_release_fabric_acl(
378 struct se_portal_group *se_tpg,
379 struct se_node_acl *se_nacl)
381 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
382 struct tcm_qla2xxx_nacl, se_node_acl);
386 static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
388 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
389 struct tcm_qla2xxx_tpg, se_tpg);
391 return tpg->lport_tpgt;
394 static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
396 struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
397 struct qla_tgt_mgmt_cmd, free_work);
399 transport_generic_free_cmd(&mcmd->se_cmd, 0);
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
407 static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
409 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
410 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
413 static void tcm_qla2xxx_complete_free(struct work_struct *work)
415 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
417 transport_generic_free_cmd(&cmd->se_cmd, 0);
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
425 static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
427 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
428 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
432 * Called from struct target_core_fabric_ops->check_stop_free() context
434 static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
436 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
439 /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
440 * fabric descriptor @se_cmd command to release
442 static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
444 struct qla_tgt_cmd *cmd;
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);
453 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
457 static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
459 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
460 struct scsi_qla_host *vha;
466 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
467 sess->tearing_down = 1;
468 target_splice_sess_cmd_list(se_sess);
469 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
474 static void tcm_qla2xxx_close_session(struct se_session *se_sess)
476 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
477 struct scsi_qla_host *vha;
483 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
484 qlt_unreg_sess(sess);
485 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
488 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
494 * The LIO target core uses DMA_TO_DEVICE to mean that data is going
495 * to the target (eg handling a WRITE) and DMA_FROM_DEVICE to mean
496 * that data is coming from the target (eg handling a READ). However,
497 * this is just the opposite of what we have to tell the DMA mapping
498 * layer -- eg when handling a READ, the HBA will have to DMA the data
499 * out of memory so it can send it to the initiator, which means we
500 * need to use DMA_TO_DEVICE when we map the data.
502 static enum dma_data_direction tcm_qla2xxx_mapping_dir(struct se_cmd *se_cmd)
504 if (se_cmd->se_cmd_flags & SCF_BIDI)
505 return DMA_BIDIRECTIONAL;
507 switch (se_cmd->data_direction) {
509 return DMA_FROM_DEVICE;
510 case DMA_FROM_DEVICE:
511 return DMA_TO_DEVICE;
518 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
520 struct qla_tgt_cmd *cmd = container_of(se_cmd,
521 struct qla_tgt_cmd, se_cmd);
523 cmd->bufflen = se_cmd->data_length;
524 cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
526 cmd->sg_cnt = se_cmd->t_data_nents;
527 cmd->sg = se_cmd->t_data_sg;
530 * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
531 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
533 return qlt_rdy_to_xfer(cmd);
536 static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
540 * Check for WRITE_PENDING status to determine if we need to wait for
541 * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
543 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
544 if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
545 se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
546 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
547 wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
551 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
556 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
561 static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
563 struct qla_tgt_cmd *cmd = container_of(se_cmd,
564 struct qla_tgt_cmd, se_cmd);
569 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
575 * Called from process context in qla_target.c:qlt_do_work() code
577 static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
578 unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
579 int data_dir, int bidi)
581 struct se_cmd *se_cmd = &cmd->se_cmd;
582 struct se_session *se_sess;
583 struct qla_tgt_sess *sess;
584 int flags = TARGET_SCF_ACK_KREF;
587 flags |= TARGET_SCF_BIDI_OP;
591 pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
595 se_sess = sess->se_sess;
597 pr_err("Unable to locate active struct se_session\n");
601 target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
602 cmd->unpacked_lun, data_length, fcp_task_attr,
607 static void tcm_qla2xxx_do_rsp(struct work_struct *work)
609 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
611 * Dispatch ->queue_status from workqueue process context
613 transport_generic_request_failure(&cmd->se_cmd);
617 * Called from qla_target.c:qlt_do_ctio_completion()
619 static int tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
621 struct se_cmd *se_cmd = &cmd->se_cmd;
624 * Ensure that the complete FCP WRITE payload has been received.
625 * Otherwise return an exception via CHECK_CONDITION status.
627 if (!cmd->write_data_transferred) {
629 * Check if se_cmd has already been aborted via LUN_RESET, and
630 * waiting upon completion in tcm_qla2xxx_write_pending_status()
632 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
633 if (se_cmd->transport_state & CMD_T_ABORTED) {
634 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
635 complete(&se_cmd->t_transport_stop_comp);
638 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
640 se_cmd->scsi_sense_reason = TCM_CHECK_CONDITION_ABORT_CMD;
641 INIT_WORK(&cmd->work, tcm_qla2xxx_do_rsp);
642 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
646 * We now tell TCM to queue this WRITE CDB with TRANSPORT_PROCESS_WRITE
647 * status to the backstore processing thread.
649 return transport_generic_handle_data(&cmd->se_cmd);
653 * Called from qla_target.c:qlt_issue_task_mgmt()
655 int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
656 uint8_t tmr_func, uint32_t tag)
658 struct qla_tgt_sess *sess = mcmd->sess;
659 struct se_cmd *se_cmd = &mcmd->se_cmd;
661 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
662 tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
665 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
667 struct qla_tgt_cmd *cmd = container_of(se_cmd,
668 struct qla_tgt_cmd, se_cmd);
670 cmd->bufflen = se_cmd->data_length;
671 cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
672 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
674 cmd->sg_cnt = se_cmd->t_data_nents;
675 cmd->sg = se_cmd->t_data_sg;
679 * Now queue completed DATA_IN the qla2xxx LLD and response ring
681 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
682 se_cmd->scsi_status);
685 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
687 struct qla_tgt_cmd *cmd = container_of(se_cmd,
688 struct qla_tgt_cmd, se_cmd);
689 int xmit_type = QLA_TGT_XMIT_STATUS;
691 cmd->bufflen = se_cmd->data_length;
695 cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
696 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
698 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
700 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
701 * for qla_tgt_xmit_response LLD code
703 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
704 se_cmd->residual_count = se_cmd->data_length;
709 * Now queue status response to qla2xxx LLD code and response ring
711 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
714 static int tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
716 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
717 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
718 struct qla_tgt_mgmt_cmd, se_cmd);
720 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
721 mcmd, se_tmr->function, se_tmr->response);
723 * Do translation between TCM TM response codes and
724 * QLA2xxx FC TM response codes.
726 switch (se_tmr->response) {
727 case TMR_FUNCTION_COMPLETE:
728 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
730 case TMR_TASK_DOES_NOT_EXIST:
731 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
733 case TMR_FUNCTION_REJECTED:
734 mcmd->fc_tm_rsp = FC_TM_REJECT;
736 case TMR_LUN_DOES_NOT_EXIST:
738 mcmd->fc_tm_rsp = FC_TM_FAILED;
742 * Queue the TM response to QLA2xxx LLD to build a
743 * CTIO response packet.
745 qlt_xmit_tm_rsp(mcmd);
750 static u16 tcm_qla2xxx_get_fabric_sense_len(void)
755 static u16 tcm_qla2xxx_set_fabric_sense_len(struct se_cmd *se_cmd,
761 /* Local pointer to allocated TCM configfs fabric module */
762 struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
763 struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
765 static int tcm_qla2xxx_setup_nacl_from_rport(
766 struct se_portal_group *se_tpg,
767 struct se_node_acl *se_nacl,
768 struct tcm_qla2xxx_lport *lport,
769 struct tcm_qla2xxx_nacl *nacl,
772 struct scsi_qla_host *vha = lport->qla_vha;
773 struct Scsi_Host *sh = vha->host;
774 struct fc_host_attrs *fc_host = shost_to_fc_host(sh);
775 struct fc_rport *rport;
781 * Scan the existing rports, and create a session for the
782 * explict NodeACL is an matching rport->node_name already
785 spin_lock_irqsave(sh->host_lock, flags);
786 list_for_each_entry(rport, &fc_host->rports, peers) {
787 if (rport_wwnn != rport->node_name)
790 pr_debug("Located existing rport_wwpn and rport->node_name: 0x%016LX, port_id: 0x%04x\n",
791 rport->node_name, rport->port_id);
792 nacl->nport_id = rport->port_id;
794 spin_unlock_irqrestore(sh->host_lock, flags);
796 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
797 node = btree_lookup32(&lport->lport_fcport_map, rport->port_id);
799 rc = btree_update32(&lport->lport_fcport_map,
800 rport->port_id, se_nacl);
802 rc = btree_insert32(&lport->lport_fcport_map,
803 rport->port_id, se_nacl,
806 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
809 pr_err("Unable to insert se_nacl into fcport_map");
814 pr_debug("Inserted into fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%08x\n",
815 se_nacl, rport_wwnn, nacl->nport_id);
819 spin_unlock_irqrestore(sh->host_lock, flags);
825 * Expected to be called with struct qla_hw_data->hardware_lock held
827 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
829 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
830 struct se_portal_group *se_tpg = se_nacl->se_tpg;
831 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
832 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
833 struct tcm_qla2xxx_lport, lport_wwn);
834 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
835 struct tcm_qla2xxx_nacl, se_node_acl);
838 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
840 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
841 WARN_ON(node && (node != se_nacl));
843 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
844 se_nacl, nacl->nport_wwnn, nacl->nport_id);
847 static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
849 target_put_session(sess->se_sess);
852 static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
854 tcm_qla2xxx_shutdown_session(sess->se_sess);
857 static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
858 struct se_portal_group *se_tpg,
859 struct config_group *group,
862 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
863 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
864 struct tcm_qla2xxx_lport, lport_wwn);
865 struct se_node_acl *se_nacl, *se_nacl_new;
866 struct tcm_qla2xxx_nacl *nacl;
868 u32 qla2xxx_nexus_depth;
871 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
872 return ERR_PTR(-EINVAL);
874 se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
876 return ERR_PTR(-ENOMEM);
877 /* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
878 qla2xxx_nexus_depth = 1;
881 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
882 * when converting a NodeACL from demo mode -> explict
884 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
885 name, qla2xxx_nexus_depth);
886 if (IS_ERR(se_nacl)) {
887 tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
891 * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
893 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
894 nacl->nport_wwnn = wwnn;
895 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
897 * Setup a se_nacl handle based on an a matching struct fc_rport setup
898 * via drivers/scsi/qla2xxx/qla_init.c:qla2x00_reg_remote_port()
900 rc = tcm_qla2xxx_setup_nacl_from_rport(se_tpg, se_nacl, lport,
903 tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
910 static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
912 struct se_portal_group *se_tpg = se_acl->se_tpg;
913 struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
914 struct tcm_qla2xxx_nacl, se_node_acl);
916 core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
920 /* Start items for tcm_qla2xxx_tpg_attrib_cit */
922 #define DEF_QLA_TPG_ATTRIB(name) \
924 static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
925 struct se_portal_group *se_tpg, \
928 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
929 struct tcm_qla2xxx_tpg, se_tpg); \
931 return sprintf(page, "%u\n", QLA_TPG_ATTRIB(tpg)->name); \
934 static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
935 struct se_portal_group *se_tpg, \
939 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
940 struct tcm_qla2xxx_tpg, se_tpg); \
944 ret = kstrtoul(page, 0, &val); \
946 pr_err("kstrtoul() failed with" \
947 " ret: %d\n", ret); \
950 ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
952 return (!ret) ? count : -EINVAL; \
955 #define DEF_QLA_TPG_ATTR_BOOL(_name) \
957 static int tcm_qla2xxx_set_attrib_##_name( \
958 struct tcm_qla2xxx_tpg *tpg, \
961 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
963 if ((val != 0) && (val != 1)) { \
964 pr_err("Illegal boolean value %lu\n", val); \
972 #define QLA_TPG_ATTR(_name, _mode) \
973 TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
976 * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
978 DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
979 DEF_QLA_TPG_ATTRIB(generate_node_acls);
980 QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
983 Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
985 DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
986 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
987 QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
990 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
992 DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
993 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
994 QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
997 * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
999 DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
1000 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
1001 QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
1003 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
1004 &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
1005 &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
1006 &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
1007 &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
1011 /* End items for tcm_qla2xxx_tpg_attrib_cit */
1013 static ssize_t tcm_qla2xxx_tpg_show_enable(
1014 struct se_portal_group *se_tpg,
1017 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1018 struct tcm_qla2xxx_tpg, se_tpg);
1020 return snprintf(page, PAGE_SIZE, "%d\n",
1021 atomic_read(&tpg->lport_tpg_enabled));
1024 static ssize_t tcm_qla2xxx_tpg_store_enable(
1025 struct se_portal_group *se_tpg,
1029 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1030 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1031 struct tcm_qla2xxx_lport, lport_wwn);
1032 struct scsi_qla_host *vha = lport->qla_vha;
1033 struct qla_hw_data *ha = vha->hw;
1034 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1035 struct tcm_qla2xxx_tpg, se_tpg);
1039 rc = kstrtoul(page, 0, &op);
1041 pr_err("kstrtoul() returned %d\n", rc);
1044 if ((op != 1) && (op != 0)) {
1045 pr_err("Illegal value for tpg_enable: %lu\n", op);
1050 atomic_set(&tpg->lport_tpg_enabled, 1);
1051 qlt_enable_vha(vha);
1053 if (!ha->tgt.qla_tgt) {
1054 pr_err("truct qla_hw_data *ha->tgt.qla_tgt is NULL\n");
1057 atomic_set(&tpg->lport_tpg_enabled, 0);
1058 qlt_stop_phase1(ha->tgt.qla_tgt);
1064 TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
1066 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
1067 &tcm_qla2xxx_tpg_enable.attr,
1071 static struct se_portal_group *tcm_qla2xxx_make_tpg(
1073 struct config_group *group,
1076 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1077 struct tcm_qla2xxx_lport, lport_wwn);
1078 struct tcm_qla2xxx_tpg *tpg;
1082 if (strstr(name, "tpgt_") != name)
1083 return ERR_PTR(-EINVAL);
1084 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1085 return ERR_PTR(-EINVAL);
1087 if (!lport->qla_npiv_vp && (tpgt != 1)) {
1088 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1089 return ERR_PTR(-ENOSYS);
1092 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1094 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1095 return ERR_PTR(-ENOMEM);
1098 tpg->lport_tpgt = tpgt;
1100 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1103 QLA_TPG_ATTRIB(tpg)->generate_node_acls = 1;
1104 QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect = 1;
1105 QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls = 1;
1107 ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
1108 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1114 * Setup local TPG=1 pointer for non NPIV mode.
1116 if (lport->qla_npiv_vp == NULL)
1119 return &tpg->se_tpg;
1122 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1124 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1125 struct tcm_qla2xxx_tpg, se_tpg);
1126 struct tcm_qla2xxx_lport *lport = tpg->lport;
1127 struct scsi_qla_host *vha = lport->qla_vha;
1128 struct qla_hw_data *ha = vha->hw;
1130 * Call into qla2x_target.c LLD logic to shutdown the active
1131 * FC Nexuses and disable target mode operation for this qla_hw_data
1133 if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stop)
1134 qlt_stop_phase1(ha->tgt.qla_tgt);
1136 core_tpg_deregister(se_tpg);
1138 * Clear local TPG=1 pointer for non NPIV mode.
1140 if (lport->qla_npiv_vp == NULL)
1141 lport->tpg_1 = NULL;
1146 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1148 struct config_group *group,
1151 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1152 struct tcm_qla2xxx_lport, lport_wwn);
1153 struct tcm_qla2xxx_tpg *tpg;
1157 if (strstr(name, "tpgt_") != name)
1158 return ERR_PTR(-EINVAL);
1159 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1160 return ERR_PTR(-EINVAL);
1162 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1164 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1165 return ERR_PTR(-ENOMEM);
1168 tpg->lport_tpgt = tpgt;
1170 ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
1171 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1176 return &tpg->se_tpg;
1180 * Expected to be called with struct qla_hw_data->hardware_lock held
1182 static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
1183 scsi_qla_host_t *vha,
1184 const uint8_t *s_id)
1186 struct qla_hw_data *ha = vha->hw;
1187 struct tcm_qla2xxx_lport *lport;
1188 struct se_node_acl *se_nacl;
1189 struct tcm_qla2xxx_nacl *nacl;
1192 lport = ha->tgt.target_lport_ptr;
1194 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1199 key = (((unsigned long)s_id[0] << 16) |
1200 ((unsigned long)s_id[1] << 8) |
1201 (unsigned long)s_id[2]);
1202 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1204 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1206 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1209 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1210 se_nacl, se_nacl->initiatorname);
1212 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1213 if (!nacl->qla_tgt_sess) {
1214 pr_err("Unable to locate struct qla_tgt_sess\n");
1218 return nacl->qla_tgt_sess;
1222 * Expected to be called with struct qla_hw_data->hardware_lock held
1224 static void tcm_qla2xxx_set_sess_by_s_id(
1225 struct tcm_qla2xxx_lport *lport,
1226 struct se_node_acl *new_se_nacl,
1227 struct tcm_qla2xxx_nacl *nacl,
1228 struct se_session *se_sess,
1229 struct qla_tgt_sess *qla_tgt_sess,
1236 key = (((unsigned long)s_id[0] << 16) |
1237 ((unsigned long)s_id[1] << 8) |
1238 (unsigned long)s_id[2]);
1239 pr_debug("set_sess_by_s_id: %06x\n", key);
1241 slot = btree_lookup32(&lport->lport_fcport_map, key);
1244 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1245 nacl->nport_id = key;
1246 rc = btree_insert32(&lport->lport_fcport_map, key,
1247 new_se_nacl, GFP_ATOMIC);
1249 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1252 pr_debug("Wiping nonexisting fc_port entry\n");
1255 qla_tgt_sess->se_sess = se_sess;
1256 nacl->qla_tgt_sess = qla_tgt_sess;
1260 if (nacl->qla_tgt_sess) {
1261 if (new_se_nacl == NULL) {
1262 pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
1263 btree_remove32(&lport->lport_fcport_map, key);
1264 nacl->qla_tgt_sess = NULL;
1267 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
1268 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1269 qla_tgt_sess->se_sess = se_sess;
1270 nacl->qla_tgt_sess = qla_tgt_sess;
1274 if (new_se_nacl == NULL) {
1275 pr_debug("Clearing existing fc_port entry\n");
1276 btree_remove32(&lport->lport_fcport_map, key);
1280 pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
1281 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1282 qla_tgt_sess->se_sess = se_sess;
1283 nacl->qla_tgt_sess = qla_tgt_sess;
1285 pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
1286 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1290 * Expected to be called with struct qla_hw_data->hardware_lock held
1292 static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
1293 scsi_qla_host_t *vha,
1294 const uint16_t loop_id)
1296 struct qla_hw_data *ha = vha->hw;
1297 struct tcm_qla2xxx_lport *lport;
1298 struct se_node_acl *se_nacl;
1299 struct tcm_qla2xxx_nacl *nacl;
1300 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1302 lport = ha->tgt.target_lport_ptr;
1304 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1309 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1311 fc_loopid = lport->lport_loopid_map + loop_id;
1312 se_nacl = fc_loopid->se_nacl;
1314 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1319 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1321 if (!nacl->qla_tgt_sess) {
1322 pr_err("Unable to locate struct qla_tgt_sess\n");
1326 return nacl->qla_tgt_sess;
1330 * Expected to be called with struct qla_hw_data->hardware_lock held
1332 static void tcm_qla2xxx_set_sess_by_loop_id(
1333 struct tcm_qla2xxx_lport *lport,
1334 struct se_node_acl *new_se_nacl,
1335 struct tcm_qla2xxx_nacl *nacl,
1336 struct se_session *se_sess,
1337 struct qla_tgt_sess *qla_tgt_sess,
1340 struct se_node_acl *saved_nacl;
1341 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1343 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1345 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1346 lport->lport_loopid_map)[loop_id];
1348 saved_nacl = fc_loopid->se_nacl;
1350 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1351 fc_loopid->se_nacl = new_se_nacl;
1352 if (qla_tgt_sess->se_sess != se_sess)
1353 qla_tgt_sess->se_sess = se_sess;
1354 if (nacl->qla_tgt_sess != qla_tgt_sess)
1355 nacl->qla_tgt_sess = qla_tgt_sess;
1359 if (nacl->qla_tgt_sess) {
1360 if (new_se_nacl == NULL) {
1361 pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1362 fc_loopid->se_nacl = NULL;
1363 nacl->qla_tgt_sess = NULL;
1367 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1368 fc_loopid->se_nacl = new_se_nacl;
1369 if (qla_tgt_sess->se_sess != se_sess)
1370 qla_tgt_sess->se_sess = se_sess;
1371 if (nacl->qla_tgt_sess != qla_tgt_sess)
1372 nacl->qla_tgt_sess = qla_tgt_sess;
1376 if (new_se_nacl == NULL) {
1377 pr_debug("Clearing fc_loopid->se_nacl\n");
1378 fc_loopid->se_nacl = NULL;
1382 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
1383 fc_loopid->se_nacl = new_se_nacl;
1384 if (qla_tgt_sess->se_sess != se_sess)
1385 qla_tgt_sess->se_sess = se_sess;
1386 if (nacl->qla_tgt_sess != qla_tgt_sess)
1387 nacl->qla_tgt_sess = qla_tgt_sess;
1389 pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1390 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1393 static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
1395 struct qla_tgt *tgt = sess->tgt;
1396 struct qla_hw_data *ha = tgt->ha;
1397 struct se_session *se_sess;
1398 struct se_node_acl *se_nacl;
1399 struct tcm_qla2xxx_lport *lport;
1400 struct tcm_qla2xxx_nacl *nacl;
1401 unsigned char be_sid[3];
1402 unsigned long flags;
1404 BUG_ON(in_interrupt());
1406 se_sess = sess->se_sess;
1408 pr_err("struct qla_tgt_sess->se_sess is NULL\n");
1412 se_nacl = se_sess->se_node_acl;
1413 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1415 lport = ha->tgt.target_lport_ptr;
1417 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1421 target_wait_for_sess_cmds(se_sess, 0);
1423 * And now clear the se_nacl and session pointers from our HW lport
1424 * mappings for fabric S_ID and LOOP_ID.
1426 memset(&be_sid, 0, 3);
1427 be_sid[0] = sess->s_id.b.domain;
1428 be_sid[1] = sess->s_id.b.area;
1429 be_sid[2] = sess->s_id.b.al_pa;
1431 spin_lock_irqsave(&ha->hardware_lock, flags);
1432 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1434 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1435 sess, sess->loop_id);
1436 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1438 transport_deregister_session_configfs(sess->se_sess);
1439 transport_deregister_session(sess->se_sess);
1443 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1444 * to locate struct se_node_acl
1446 static int tcm_qla2xxx_check_initiator_node_acl(
1447 scsi_qla_host_t *vha,
1448 unsigned char *fc_wwpn,
1453 struct qla_hw_data *ha = vha->hw;
1454 struct tcm_qla2xxx_lport *lport;
1455 struct tcm_qla2xxx_tpg *tpg;
1456 struct tcm_qla2xxx_nacl *nacl;
1457 struct se_portal_group *se_tpg;
1458 struct se_node_acl *se_nacl;
1459 struct se_session *se_sess;
1460 struct qla_tgt_sess *sess = qla_tgt_sess;
1461 unsigned char port_name[36];
1462 unsigned long flags;
1464 lport = ha->tgt.target_lport_ptr;
1466 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1471 * Locate the TPG=1 reference..
1475 pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1478 se_tpg = &tpg->se_tpg;
1480 se_sess = transport_init_session();
1481 if (IS_ERR(se_sess)) {
1482 pr_err("Unable to initialize struct se_session\n");
1483 return PTR_ERR(se_sess);
1486 * Format the FCP Initiator port_name into colon seperated values to
1487 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1489 memset(&port_name, 0, 36);
1490 snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1491 fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
1492 fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
1494 * Locate our struct se_node_acl either from an explict NodeACL created
1495 * via ConfigFS, or via running in TPG demo mode.
1497 se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
1499 if (!se_sess->se_node_acl) {
1500 transport_free_session(se_sess);
1503 se_nacl = se_sess->se_node_acl;
1504 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1506 * And now setup the new se_nacl and session pointers into our HW lport
1507 * mappings for fabric S_ID and LOOP_ID.
1509 spin_lock_irqsave(&ha->hardware_lock, flags);
1510 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
1511 qla_tgt_sess, s_id);
1512 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
1513 qla_tgt_sess, loop_id);
1514 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1516 * Finally register the new FC Nexus with TCM
1518 __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
1524 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1526 static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1527 .handle_cmd = tcm_qla2xxx_handle_cmd,
1528 .handle_data = tcm_qla2xxx_handle_data,
1529 .handle_tmr = tcm_qla2xxx_handle_tmr,
1530 .free_cmd = tcm_qla2xxx_free_cmd,
1531 .free_mcmd = tcm_qla2xxx_free_mcmd,
1532 .free_session = tcm_qla2xxx_free_session,
1533 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1534 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1535 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1536 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1537 .put_sess = tcm_qla2xxx_put_sess,
1538 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1541 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1545 rc = btree_init32(&lport->lport_fcport_map);
1547 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1551 lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
1553 if (!lport->lport_loopid_map) {
1554 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1555 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1556 btree_destroy32(&lport->lport_fcport_map);
1559 memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
1561 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1562 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1566 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha)
1568 struct qla_hw_data *ha = vha->hw;
1569 struct tcm_qla2xxx_lport *lport;
1571 * Setup local pointer to vha, NPIV VP pointer (if present) and
1572 * vha->tcm_lport pointer
1574 lport = (struct tcm_qla2xxx_lport *)ha->tgt.target_lport_ptr;
1575 lport->qla_vha = vha;
1580 static struct se_wwn *tcm_qla2xxx_make_lport(
1581 struct target_fabric_configfs *tf,
1582 struct config_group *group,
1585 struct tcm_qla2xxx_lport *lport;
1589 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1590 return ERR_PTR(-EINVAL);
1592 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1594 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1595 return ERR_PTR(-ENOMEM);
1597 lport->lport_wwpn = wwpn;
1598 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1601 ret = tcm_qla2xxx_init_lport(lport);
1605 ret = qlt_lport_register(&tcm_qla2xxx_template, wwpn,
1606 tcm_qla2xxx_lport_register_cb, lport);
1610 return &lport->lport_wwn;
1612 vfree(lport->lport_loopid_map);
1613 btree_destroy32(&lport->lport_fcport_map);
1616 return ERR_PTR(ret);
1619 static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1621 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1622 struct tcm_qla2xxx_lport, lport_wwn);
1623 struct scsi_qla_host *vha = lport->qla_vha;
1624 struct qla_hw_data *ha = vha->hw;
1625 struct se_node_acl *node;
1629 * Call into qla2x_target.c LLD logic to complete the
1630 * shutdown of struct qla_tgt after the call to
1631 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1633 if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stopped)
1634 qlt_stop_phase2(ha->tgt.qla_tgt);
1636 qlt_lport_deregister(vha);
1638 vfree(lport->lport_loopid_map);
1639 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1640 btree_remove32(&lport->lport_fcport_map, key);
1641 btree_destroy32(&lport->lport_fcport_map);
1645 static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1646 struct target_fabric_configfs *tf,
1647 struct config_group *group,
1650 struct tcm_qla2xxx_lport *lport;
1651 u64 npiv_wwpn, npiv_wwnn;
1654 if (tcm_qla2xxx_npiv_parse_wwn(name, strlen(name)+1,
1655 &npiv_wwpn, &npiv_wwnn) < 0)
1656 return ERR_PTR(-EINVAL);
1658 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1660 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1661 return ERR_PTR(-ENOMEM);
1663 lport->lport_npiv_wwpn = npiv_wwpn;
1664 lport->lport_npiv_wwnn = npiv_wwnn;
1665 tcm_qla2xxx_npiv_format_wwn(&lport->lport_npiv_name[0],
1666 TCM_QLA2XXX_NAMELEN, npiv_wwpn, npiv_wwnn);
1668 /* FIXME: tcm_qla2xxx_npiv_make_lport */
1673 return &lport->lport_wwn;
1676 return ERR_PTR(ret);
1679 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1681 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1682 struct tcm_qla2xxx_lport, lport_wwn);
1683 struct scsi_qla_host *vha = lport->qla_vha;
1684 struct Scsi_Host *sh = vha->host;
1686 * Notify libfc that we want to release the lport->npiv_vport
1688 fc_vport_terminate(lport->npiv_vport);
1695 static ssize_t tcm_qla2xxx_wwn_show_attr_version(
1696 struct target_fabric_configfs *tf,
1699 return sprintf(page,
1700 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
1701 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1702 utsname()->machine);
1705 TF_WWN_ATTR_RO(tcm_qla2xxx, version);
1707 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1708 &tcm_qla2xxx_wwn_version.attr,
1712 static struct target_core_fabric_ops tcm_qla2xxx_ops = {
1713 .get_fabric_name = tcm_qla2xxx_get_fabric_name,
1714 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1715 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1716 .tpg_get_tag = tcm_qla2xxx_get_tag,
1717 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1718 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1719 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1720 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1721 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1722 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1723 .tpg_check_demo_mode_write_protect =
1724 tcm_qla2xxx_check_demo_write_protect,
1725 .tpg_check_prod_mode_write_protect =
1726 tcm_qla2xxx_check_prod_write_protect,
1727 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
1728 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1729 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1730 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1731 .new_cmd_map = NULL,
1732 .check_stop_free = tcm_qla2xxx_check_stop_free,
1733 .release_cmd = tcm_qla2xxx_release_cmd,
1734 .shutdown_session = tcm_qla2xxx_shutdown_session,
1735 .close_session = tcm_qla2xxx_close_session,
1736 .sess_get_index = tcm_qla2xxx_sess_get_index,
1737 .sess_get_initiator_sid = NULL,
1738 .write_pending = tcm_qla2xxx_write_pending,
1739 .write_pending_status = tcm_qla2xxx_write_pending_status,
1740 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1741 .get_task_tag = tcm_qla2xxx_get_task_tag,
1742 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1743 .queue_data_in = tcm_qla2xxx_queue_data_in,
1744 .queue_status = tcm_qla2xxx_queue_status,
1745 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1746 .get_fabric_sense_len = tcm_qla2xxx_get_fabric_sense_len,
1747 .set_fabric_sense_len = tcm_qla2xxx_set_fabric_sense_len,
1749 * Setup function pointers for generic logic in
1750 * target_core_fabric_configfs.c
1752 .fabric_make_wwn = tcm_qla2xxx_make_lport,
1753 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
1754 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
1755 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1756 .fabric_post_link = NULL,
1757 .fabric_pre_unlink = NULL,
1758 .fabric_make_np = NULL,
1759 .fabric_drop_np = NULL,
1760 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1761 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1764 static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1765 .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
1766 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1767 .tpg_get_wwn = tcm_qla2xxx_npiv_get_fabric_wwn,
1768 .tpg_get_tag = tcm_qla2xxx_get_tag,
1769 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1770 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1771 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1772 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1773 .tpg_check_demo_mode = tcm_qla2xxx_check_false,
1774 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_true,
1775 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_true,
1776 .tpg_check_prod_mode_write_protect = tcm_qla2xxx_check_false,
1777 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
1778 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1779 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1780 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1781 .release_cmd = tcm_qla2xxx_release_cmd,
1782 .shutdown_session = tcm_qla2xxx_shutdown_session,
1783 .close_session = tcm_qla2xxx_close_session,
1784 .sess_get_index = tcm_qla2xxx_sess_get_index,
1785 .sess_get_initiator_sid = NULL,
1786 .write_pending = tcm_qla2xxx_write_pending,
1787 .write_pending_status = tcm_qla2xxx_write_pending_status,
1788 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1789 .get_task_tag = tcm_qla2xxx_get_task_tag,
1790 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1791 .queue_data_in = tcm_qla2xxx_queue_data_in,
1792 .queue_status = tcm_qla2xxx_queue_status,
1793 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1794 .get_fabric_sense_len = tcm_qla2xxx_get_fabric_sense_len,
1795 .set_fabric_sense_len = tcm_qla2xxx_set_fabric_sense_len,
1797 * Setup function pointers for generic logic in
1798 * target_core_fabric_configfs.c
1800 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
1801 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
1802 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
1803 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1804 .fabric_post_link = NULL,
1805 .fabric_pre_unlink = NULL,
1806 .fabric_make_np = NULL,
1807 .fabric_drop_np = NULL,
1808 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1809 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1812 static int tcm_qla2xxx_register_configfs(void)
1814 struct target_fabric_configfs *fabric, *npiv_fabric;
1817 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
1818 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1819 utsname()->machine);
1821 * Register the top level struct config_item_type with TCM core
1823 fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
1824 if (IS_ERR(fabric)) {
1825 pr_err("target_fabric_configfs_init() failed\n");
1826 return PTR_ERR(fabric);
1829 * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
1831 fabric->tf_ops = tcm_qla2xxx_ops;
1833 * Setup default attribute lists for various fabric->tf_cit_tmpl
1835 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1836 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
1837 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs =
1838 tcm_qla2xxx_tpg_attrib_attrs;
1839 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1840 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1841 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1842 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1843 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1844 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1846 * Register the fabric for use within TCM
1848 ret = target_fabric_configfs_register(fabric);
1850 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1854 * Setup our local pointer to *fabric
1856 tcm_qla2xxx_fabric_configfs = fabric;
1857 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
1860 * Register the top level struct config_item_type for NPIV with TCM core
1862 npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
1863 if (IS_ERR(npiv_fabric)) {
1864 pr_err("target_fabric_configfs_init() failed\n");
1865 ret = PTR_ERR(npiv_fabric);
1869 * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
1871 npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
1873 * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
1875 TF_CIT_TMPL(npiv_fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1876 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
1877 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1878 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1879 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1880 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1881 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1882 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1883 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1885 * Register the npiv_fabric for use within TCM
1887 ret = target_fabric_configfs_register(npiv_fabric);
1889 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1893 * Setup our local pointer to *npiv_fabric
1895 tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
1896 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
1898 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1900 if (!tcm_qla2xxx_free_wq) {
1902 goto out_fabric_npiv;
1905 tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
1906 if (!tcm_qla2xxx_cmd_wq) {
1914 destroy_workqueue(tcm_qla2xxx_free_wq);
1916 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1918 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1922 static void tcm_qla2xxx_deregister_configfs(void)
1924 destroy_workqueue(tcm_qla2xxx_cmd_wq);
1925 destroy_workqueue(tcm_qla2xxx_free_wq);
1927 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1928 tcm_qla2xxx_fabric_configfs = NULL;
1929 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
1931 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1932 tcm_qla2xxx_npiv_fabric_configfs = NULL;
1933 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
1936 static int __init tcm_qla2xxx_init(void)
1940 ret = tcm_qla2xxx_register_configfs();
1947 static void __exit tcm_qla2xxx_exit(void)
1949 tcm_qla2xxx_deregister_configfs();
1952 MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
1953 MODULE_LICENSE("GPL");
1954 module_init(tcm_qla2xxx_init);
1955 module_exit(tcm_qla2xxx_exit);