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 <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>
53 #include "qla_target.h"
54 #include "tcm_qla2xxx.h"
56 struct workqueue_struct *tcm_qla2xxx_free_wq;
57 struct workqueue_struct *tcm_qla2xxx_cmd_wq;
59 static int tcm_qla2xxx_check_true(struct se_portal_group *se_tpg)
64 static int tcm_qla2xxx_check_false(struct se_portal_group *se_tpg)
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.
75 static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
85 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
87 if (c == '\n' && cp[1] == '\0')
89 if (strict && pos++ == 2 && byte++ < 7) {
98 if (strict && byte != 8)
105 else if (isxdigit(c) && (islower(c) || !strict))
106 nibble = tolower(c) - 'a' + 10;
109 *wwn = (*wwn << 4) | nibble;
113 pr_debug("err %u len %zu pos %u byte %u\n",
114 err, cp - name, pos, byte);
118 static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
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]);
128 static char *tcm_qla2xxx_get_fabric_name(void)
134 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
136 static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
141 memset(wwn, 0, sizeof(wwn));
143 /* Validate and store the new name */
144 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_naa_name;
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 allocate 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 target_sess_cmd_list_set_waiting(se_sess);
468 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
473 static void tcm_qla2xxx_close_session(struct se_session *se_sess)
475 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
476 struct scsi_qla_host *vha;
482 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
483 qlt_unreg_sess(sess);
484 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
487 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
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.
501 static enum dma_data_direction tcm_qla2xxx_mapping_dir(struct se_cmd *se_cmd)
503 if (se_cmd->se_cmd_flags & SCF_BIDI)
504 return DMA_BIDIRECTIONAL;
506 switch (se_cmd->data_direction) {
508 return DMA_FROM_DEVICE;
509 case DMA_FROM_DEVICE:
510 return DMA_TO_DEVICE;
517 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
519 struct qla_tgt_cmd *cmd = container_of(se_cmd,
520 struct qla_tgt_cmd, se_cmd);
522 cmd->bufflen = se_cmd->data_length;
523 cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
525 cmd->sg_cnt = se_cmd->t_data_nents;
526 cmd->sg = se_cmd->t_data_sg;
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.
532 return qlt_rdy_to_xfer(cmd);
535 static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
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().
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,
550 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
555 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
560 static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
562 struct qla_tgt_cmd *cmd = container_of(se_cmd,
563 struct qla_tgt_cmd, se_cmd);
568 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
574 * Called from process context in qla_target.c:qlt_do_work() code
576 static 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)
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;
586 flags |= TARGET_SCF_BIDI_OP;
590 pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
594 se_sess = sess->se_sess;
596 pr_err("Unable to locate active struct se_session\n");
600 return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
601 cmd->unpacked_lun, data_length, fcp_task_attr,
605 static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
607 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
610 * Ensure that the complete FCP WRITE payload has been received.
611 * Otherwise return an exception via CHECK_CONDITION status.
613 if (!cmd->write_data_transferred) {
615 * Check if se_cmd has already been aborted via LUN_RESET, and
616 * waiting upon completion in tcm_qla2xxx_write_pending_status()
618 if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
619 complete(&cmd->se_cmd.t_transport_stop_comp);
623 transport_generic_request_failure(&cmd->se_cmd,
624 TCM_CHECK_CONDITION_ABORT_CMD);
628 return target_execute_cmd(&cmd->se_cmd);
632 * Called from qla_target.c:qlt_do_ctio_completion()
634 static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
636 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
637 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
641 * Called from qla_target.c:qlt_issue_task_mgmt()
643 static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
644 uint8_t tmr_func, uint32_t tag)
646 struct qla_tgt_sess *sess = mcmd->sess;
647 struct se_cmd *se_cmd = &mcmd->se_cmd;
649 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
650 tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
653 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
655 struct qla_tgt_cmd *cmd = container_of(se_cmd,
656 struct qla_tgt_cmd, se_cmd);
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);
662 cmd->sg_cnt = se_cmd->t_data_nents;
663 cmd->sg = se_cmd->t_data_sg;
667 * Now queue completed DATA_IN the qla2xxx LLD and response ring
669 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
670 se_cmd->scsi_status);
673 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
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;
679 cmd->bufflen = se_cmd->data_length;
683 cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
684 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
686 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
688 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
689 * for qla_tgt_xmit_response LLD code
691 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
692 se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
693 se_cmd->residual_count = 0;
695 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
696 se_cmd->residual_count += se_cmd->data_length;
701 * Now queue status response to qla2xxx LLD code and response ring
703 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
706 static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
708 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
709 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
710 struct qla_tgt_mgmt_cmd, se_cmd);
712 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
713 mcmd, se_tmr->function, se_tmr->response);
715 * Do translation between TCM TM response codes and
716 * QLA2xxx FC TM response codes.
718 switch (se_tmr->response) {
719 case TMR_FUNCTION_COMPLETE:
720 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
722 case TMR_TASK_DOES_NOT_EXIST:
723 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
725 case TMR_FUNCTION_REJECTED:
726 mcmd->fc_tm_rsp = FC_TM_REJECT;
728 case TMR_LUN_DOES_NOT_EXIST:
730 mcmd->fc_tm_rsp = FC_TM_FAILED;
734 * Queue the TM response to QLA2xxx LLD to build a
735 * CTIO response packet.
737 qlt_xmit_tm_rsp(mcmd);
740 /* Local pointer to allocated TCM configfs fabric module */
741 struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
742 struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
744 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
745 struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
747 * Expected to be called with struct qla_hw_data->hardware_lock held
749 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
751 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
752 struct se_portal_group *se_tpg = se_nacl->se_tpg;
753 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
754 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
755 struct tcm_qla2xxx_lport, lport_wwn);
756 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
757 struct tcm_qla2xxx_nacl, se_node_acl);
760 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
762 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
763 WARN_ON(node && (node != se_nacl));
765 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
766 se_nacl, nacl->nport_wwnn, nacl->nport_id);
768 * Now clear the se_nacl and session pointers from our HW lport lookup
769 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
771 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
772 * target_wait_for_sess_cmds() before the session waits for outstanding
773 * I/O to complete, to avoid a race between session shutdown execution
774 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
776 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
779 static void tcm_qla2xxx_release_session(struct kref *kref)
781 struct se_session *se_sess = container_of(kref,
782 struct se_session, sess_kref);
784 qlt_unreg_sess(se_sess->fabric_sess_ptr);
787 static void tcm_qla2xxx_put_session(struct se_session *se_sess)
789 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
790 struct qla_hw_data *ha = sess->vha->hw;
793 spin_lock_irqsave(&ha->hardware_lock, flags);
794 kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
795 spin_unlock_irqrestore(&ha->hardware_lock, flags);
798 static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
800 assert_spin_locked(&sess->vha->hw->hardware_lock);
801 kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
804 static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
806 assert_spin_locked(&sess->vha->hw->hardware_lock);
807 target_sess_cmd_list_set_waiting(sess->se_sess);
810 static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
811 struct se_portal_group *se_tpg,
812 struct config_group *group,
815 struct se_node_acl *se_nacl, *se_nacl_new;
816 struct tcm_qla2xxx_nacl *nacl;
818 u32 qla2xxx_nexus_depth;
820 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
821 return ERR_PTR(-EINVAL);
823 se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
825 return ERR_PTR(-ENOMEM);
826 /* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
827 qla2xxx_nexus_depth = 1;
830 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
831 * when converting a NodeACL from demo mode -> explict
833 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
834 name, qla2xxx_nexus_depth);
835 if (IS_ERR(se_nacl)) {
836 tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
840 * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
842 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
843 nacl->nport_wwnn = wwnn;
844 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
849 static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
851 struct se_portal_group *se_tpg = se_acl->se_tpg;
852 struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
853 struct tcm_qla2xxx_nacl, se_node_acl);
855 core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
859 /* Start items for tcm_qla2xxx_tpg_attrib_cit */
861 #define DEF_QLA_TPG_ATTRIB(name) \
863 static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
864 struct se_portal_group *se_tpg, \
867 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
868 struct tcm_qla2xxx_tpg, se_tpg); \
870 return sprintf(page, "%u\n", QLA_TPG_ATTRIB(tpg)->name); \
873 static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
874 struct se_portal_group *se_tpg, \
878 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
879 struct tcm_qla2xxx_tpg, se_tpg); \
883 ret = kstrtoul(page, 0, &val); \
885 pr_err("kstrtoul() failed with" \
886 " ret: %d\n", ret); \
889 ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
891 return (!ret) ? count : -EINVAL; \
894 #define DEF_QLA_TPG_ATTR_BOOL(_name) \
896 static int tcm_qla2xxx_set_attrib_##_name( \
897 struct tcm_qla2xxx_tpg *tpg, \
900 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
902 if ((val != 0) && (val != 1)) { \
903 pr_err("Illegal boolean value %lu\n", val); \
911 #define QLA_TPG_ATTR(_name, _mode) \
912 TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
915 * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
917 DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
918 DEF_QLA_TPG_ATTRIB(generate_node_acls);
919 QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
922 Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
924 DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
925 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
926 QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
929 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
931 DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
932 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
933 QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
936 * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
938 DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
939 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
940 QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
942 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
943 &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
944 &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
945 &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
946 &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
950 /* End items for tcm_qla2xxx_tpg_attrib_cit */
952 static ssize_t tcm_qla2xxx_tpg_show_enable(
953 struct se_portal_group *se_tpg,
956 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
957 struct tcm_qla2xxx_tpg, se_tpg);
959 return snprintf(page, PAGE_SIZE, "%d\n",
960 atomic_read(&tpg->lport_tpg_enabled));
963 static ssize_t tcm_qla2xxx_tpg_store_enable(
964 struct se_portal_group *se_tpg,
968 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
969 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
970 struct tcm_qla2xxx_lport, lport_wwn);
971 struct scsi_qla_host *vha = lport->qla_vha;
972 struct qla_hw_data *ha = vha->hw;
973 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
974 struct tcm_qla2xxx_tpg, se_tpg);
978 rc = kstrtoul(page, 0, &op);
980 pr_err("kstrtoul() returned %d\n", rc);
983 if ((op != 1) && (op != 0)) {
984 pr_err("Illegal value for tpg_enable: %lu\n", op);
989 atomic_set(&tpg->lport_tpg_enabled, 1);
992 if (!ha->tgt.qla_tgt) {
993 pr_err("truct qla_hw_data *ha->tgt.qla_tgt is NULL\n");
996 atomic_set(&tpg->lport_tpg_enabled, 0);
997 qlt_stop_phase1(ha->tgt.qla_tgt);
1003 TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
1005 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
1006 &tcm_qla2xxx_tpg_enable.attr,
1010 static struct se_portal_group *tcm_qla2xxx_make_tpg(
1012 struct config_group *group,
1015 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1016 struct tcm_qla2xxx_lport, lport_wwn);
1017 struct tcm_qla2xxx_tpg *tpg;
1021 if (strstr(name, "tpgt_") != name)
1022 return ERR_PTR(-EINVAL);
1023 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1024 return ERR_PTR(-EINVAL);
1026 if (!lport->qla_npiv_vp && (tpgt != 1)) {
1027 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1028 return ERR_PTR(-ENOSYS);
1031 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1033 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1034 return ERR_PTR(-ENOMEM);
1037 tpg->lport_tpgt = tpgt;
1039 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1042 QLA_TPG_ATTRIB(tpg)->generate_node_acls = 1;
1043 QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect = 1;
1044 QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls = 1;
1046 ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
1047 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1053 * Setup local TPG=1 pointer for non NPIV mode.
1055 if (lport->qla_npiv_vp == NULL)
1058 return &tpg->se_tpg;
1061 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1063 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1064 struct tcm_qla2xxx_tpg, se_tpg);
1065 struct tcm_qla2xxx_lport *lport = tpg->lport;
1066 struct scsi_qla_host *vha = lport->qla_vha;
1067 struct qla_hw_data *ha = vha->hw;
1069 * Call into qla2x_target.c LLD logic to shutdown the active
1070 * FC Nexuses and disable target mode operation for this qla_hw_data
1072 if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stop)
1073 qlt_stop_phase1(ha->tgt.qla_tgt);
1075 core_tpg_deregister(se_tpg);
1077 * Clear local TPG=1 pointer for non NPIV mode.
1079 if (lport->qla_npiv_vp == NULL)
1080 lport->tpg_1 = NULL;
1085 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1087 struct config_group *group,
1090 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1091 struct tcm_qla2xxx_lport, lport_wwn);
1092 struct tcm_qla2xxx_tpg *tpg;
1096 if (strstr(name, "tpgt_") != name)
1097 return ERR_PTR(-EINVAL);
1098 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1099 return ERR_PTR(-EINVAL);
1101 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1103 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1104 return ERR_PTR(-ENOMEM);
1107 tpg->lport_tpgt = tpgt;
1109 ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
1110 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1115 return &tpg->se_tpg;
1119 * Expected to be called with struct qla_hw_data->hardware_lock held
1121 static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
1122 scsi_qla_host_t *vha,
1123 const uint8_t *s_id)
1125 struct qla_hw_data *ha = vha->hw;
1126 struct tcm_qla2xxx_lport *lport;
1127 struct se_node_acl *se_nacl;
1128 struct tcm_qla2xxx_nacl *nacl;
1131 lport = ha->tgt.target_lport_ptr;
1133 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1138 key = (((unsigned long)s_id[0] << 16) |
1139 ((unsigned long)s_id[1] << 8) |
1140 (unsigned long)s_id[2]);
1141 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1143 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1145 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1148 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1149 se_nacl, se_nacl->initiatorname);
1151 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1152 if (!nacl->qla_tgt_sess) {
1153 pr_err("Unable to locate struct qla_tgt_sess\n");
1157 return nacl->qla_tgt_sess;
1161 * Expected to be called with struct qla_hw_data->hardware_lock held
1163 static void tcm_qla2xxx_set_sess_by_s_id(
1164 struct tcm_qla2xxx_lport *lport,
1165 struct se_node_acl *new_se_nacl,
1166 struct tcm_qla2xxx_nacl *nacl,
1167 struct se_session *se_sess,
1168 struct qla_tgt_sess *qla_tgt_sess,
1175 key = (((unsigned long)s_id[0] << 16) |
1176 ((unsigned long)s_id[1] << 8) |
1177 (unsigned long)s_id[2]);
1178 pr_debug("set_sess_by_s_id: %06x\n", key);
1180 slot = btree_lookup32(&lport->lport_fcport_map, key);
1183 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1184 nacl->nport_id = key;
1185 rc = btree_insert32(&lport->lport_fcport_map, key,
1186 new_se_nacl, GFP_ATOMIC);
1188 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1191 pr_debug("Wiping nonexisting fc_port entry\n");
1194 qla_tgt_sess->se_sess = se_sess;
1195 nacl->qla_tgt_sess = qla_tgt_sess;
1199 if (nacl->qla_tgt_sess) {
1200 if (new_se_nacl == NULL) {
1201 pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
1202 btree_remove32(&lport->lport_fcport_map, key);
1203 nacl->qla_tgt_sess = NULL;
1206 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
1207 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1208 qla_tgt_sess->se_sess = se_sess;
1209 nacl->qla_tgt_sess = qla_tgt_sess;
1213 if (new_se_nacl == NULL) {
1214 pr_debug("Clearing existing fc_port entry\n");
1215 btree_remove32(&lport->lport_fcport_map, key);
1219 pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
1220 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1221 qla_tgt_sess->se_sess = se_sess;
1222 nacl->qla_tgt_sess = qla_tgt_sess;
1224 pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
1225 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1229 * Expected to be called with struct qla_hw_data->hardware_lock held
1231 static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
1232 scsi_qla_host_t *vha,
1233 const uint16_t loop_id)
1235 struct qla_hw_data *ha = vha->hw;
1236 struct tcm_qla2xxx_lport *lport;
1237 struct se_node_acl *se_nacl;
1238 struct tcm_qla2xxx_nacl *nacl;
1239 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1241 lport = ha->tgt.target_lport_ptr;
1243 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1248 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1250 fc_loopid = lport->lport_loopid_map + loop_id;
1251 se_nacl = fc_loopid->se_nacl;
1253 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1258 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1260 if (!nacl->qla_tgt_sess) {
1261 pr_err("Unable to locate struct qla_tgt_sess\n");
1265 return nacl->qla_tgt_sess;
1269 * Expected to be called with struct qla_hw_data->hardware_lock held
1271 static void tcm_qla2xxx_set_sess_by_loop_id(
1272 struct tcm_qla2xxx_lport *lport,
1273 struct se_node_acl *new_se_nacl,
1274 struct tcm_qla2xxx_nacl *nacl,
1275 struct se_session *se_sess,
1276 struct qla_tgt_sess *qla_tgt_sess,
1279 struct se_node_acl *saved_nacl;
1280 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1282 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1284 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1285 lport->lport_loopid_map)[loop_id];
1287 saved_nacl = fc_loopid->se_nacl;
1289 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1290 fc_loopid->se_nacl = new_se_nacl;
1291 if (qla_tgt_sess->se_sess != se_sess)
1292 qla_tgt_sess->se_sess = se_sess;
1293 if (nacl->qla_tgt_sess != qla_tgt_sess)
1294 nacl->qla_tgt_sess = qla_tgt_sess;
1298 if (nacl->qla_tgt_sess) {
1299 if (new_se_nacl == NULL) {
1300 pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1301 fc_loopid->se_nacl = NULL;
1302 nacl->qla_tgt_sess = NULL;
1306 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1307 fc_loopid->se_nacl = new_se_nacl;
1308 if (qla_tgt_sess->se_sess != se_sess)
1309 qla_tgt_sess->se_sess = se_sess;
1310 if (nacl->qla_tgt_sess != qla_tgt_sess)
1311 nacl->qla_tgt_sess = qla_tgt_sess;
1315 if (new_se_nacl == NULL) {
1316 pr_debug("Clearing fc_loopid->se_nacl\n");
1317 fc_loopid->se_nacl = NULL;
1321 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
1322 fc_loopid->se_nacl = new_se_nacl;
1323 if (qla_tgt_sess->se_sess != se_sess)
1324 qla_tgt_sess->se_sess = se_sess;
1325 if (nacl->qla_tgt_sess != qla_tgt_sess)
1326 nacl->qla_tgt_sess = qla_tgt_sess;
1328 pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1329 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1333 * Should always be called with qla_hw_data->hardware_lock held.
1335 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1336 struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
1338 struct se_session *se_sess = sess->se_sess;
1339 unsigned char be_sid[3];
1341 be_sid[0] = sess->s_id.b.domain;
1342 be_sid[1] = sess->s_id.b.area;
1343 be_sid[2] = sess->s_id.b.al_pa;
1345 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1347 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1348 sess, sess->loop_id);
1351 static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
1353 struct qla_tgt *tgt = sess->tgt;
1354 struct qla_hw_data *ha = tgt->ha;
1355 struct se_session *se_sess;
1356 struct se_node_acl *se_nacl;
1357 struct tcm_qla2xxx_lport *lport;
1358 struct tcm_qla2xxx_nacl *nacl;
1360 BUG_ON(in_interrupt());
1362 se_sess = sess->se_sess;
1364 pr_err("struct qla_tgt_sess->se_sess is NULL\n");
1368 se_nacl = se_sess->se_node_acl;
1369 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1371 lport = ha->tgt.target_lport_ptr;
1373 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1377 target_wait_for_sess_cmds(se_sess);
1379 transport_deregister_session_configfs(sess->se_sess);
1380 transport_deregister_session(sess->se_sess);
1384 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1385 * to locate struct se_node_acl
1387 static int tcm_qla2xxx_check_initiator_node_acl(
1388 scsi_qla_host_t *vha,
1389 unsigned char *fc_wwpn,
1394 struct qla_hw_data *ha = vha->hw;
1395 struct tcm_qla2xxx_lport *lport;
1396 struct tcm_qla2xxx_tpg *tpg;
1397 struct tcm_qla2xxx_nacl *nacl;
1398 struct se_portal_group *se_tpg;
1399 struct se_node_acl *se_nacl;
1400 struct se_session *se_sess;
1401 struct qla_tgt_sess *sess = qla_tgt_sess;
1402 unsigned char port_name[36];
1403 unsigned long flags;
1405 lport = ha->tgt.target_lport_ptr;
1407 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1412 * Locate the TPG=1 reference..
1416 pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1419 se_tpg = &tpg->se_tpg;
1421 se_sess = transport_init_session();
1422 if (IS_ERR(se_sess)) {
1423 pr_err("Unable to initialize struct se_session\n");
1424 return PTR_ERR(se_sess);
1427 * Format the FCP Initiator port_name into colon seperated values to
1428 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1430 memset(&port_name, 0, 36);
1431 snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1432 fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
1433 fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
1435 * Locate our struct se_node_acl either from an explict NodeACL created
1436 * via ConfigFS, or via running in TPG demo mode.
1438 se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
1440 if (!se_sess->se_node_acl) {
1441 transport_free_session(se_sess);
1444 se_nacl = se_sess->se_node_acl;
1445 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1447 * And now setup the new se_nacl and session pointers into our HW lport
1448 * mappings for fabric S_ID and LOOP_ID.
1450 spin_lock_irqsave(&ha->hardware_lock, flags);
1451 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
1452 qla_tgt_sess, s_id);
1453 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
1454 qla_tgt_sess, loop_id);
1455 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1457 * Finally register the new FC Nexus with TCM
1459 __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
1464 static void tcm_qla2xxx_update_sess(struct qla_tgt_sess *sess, port_id_t s_id,
1465 uint16_t loop_id, bool conf_compl_supported)
1467 struct qla_tgt *tgt = sess->tgt;
1468 struct qla_hw_data *ha = tgt->ha;
1469 struct tcm_qla2xxx_lport *lport = ha->tgt.target_lport_ptr;
1470 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1471 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1472 struct tcm_qla2xxx_nacl, se_node_acl);
1476 if (sess->loop_id != loop_id || sess->s_id.b24 != s_id.b24)
1477 pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1478 sess, sess->port_name,
1479 sess->loop_id, loop_id, sess->s_id.b.domain,
1480 sess->s_id.b.area, sess->s_id.b.al_pa, s_id.b.domain,
1481 s_id.b.area, s_id.b.al_pa);
1483 if (sess->loop_id != loop_id) {
1485 * Because we can shuffle loop IDs around and we
1486 * update different sessions non-atomically, we might
1487 * have overwritten this session's old loop ID
1488 * already, and we might end up overwriting some other
1489 * session that will be updated later. So we have to
1490 * be extra careful and we can't warn about those things...
1492 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1493 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1495 lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1497 sess->loop_id = loop_id;
1500 if (sess->s_id.b24 != s_id.b24) {
1501 key = (((u32) sess->s_id.b.domain << 16) |
1502 ((u32) sess->s_id.b.area << 8) |
1503 ((u32) sess->s_id.b.al_pa));
1505 if (btree_lookup32(&lport->lport_fcport_map, key))
1506 WARN(btree_remove32(&lport->lport_fcport_map, key) != se_nacl,
1507 "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1508 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1510 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1511 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1513 key = (((u32) s_id.b.domain << 16) |
1514 ((u32) s_id.b.area << 8) |
1515 ((u32) s_id.b.al_pa));
1517 if (btree_lookup32(&lport->lport_fcport_map, key)) {
1518 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1519 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1520 btree_update32(&lport->lport_fcport_map, key, se_nacl);
1522 btree_insert32(&lport->lport_fcport_map, key, se_nacl, GFP_ATOMIC);
1526 nacl->nport_id = key;
1529 sess->conf_compl_supported = conf_compl_supported;
1533 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1535 static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1536 .handle_cmd = tcm_qla2xxx_handle_cmd,
1537 .handle_data = tcm_qla2xxx_handle_data,
1538 .handle_tmr = tcm_qla2xxx_handle_tmr,
1539 .free_cmd = tcm_qla2xxx_free_cmd,
1540 .free_mcmd = tcm_qla2xxx_free_mcmd,
1541 .free_session = tcm_qla2xxx_free_session,
1542 .update_sess = tcm_qla2xxx_update_sess,
1543 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1544 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1545 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1546 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1547 .put_sess = tcm_qla2xxx_put_sess,
1548 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1551 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1555 rc = btree_init32(&lport->lport_fcport_map);
1557 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1561 lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
1563 if (!lport->lport_loopid_map) {
1564 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1565 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1566 btree_destroy32(&lport->lport_fcport_map);
1569 memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
1571 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1572 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1576 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha)
1578 struct qla_hw_data *ha = vha->hw;
1579 struct tcm_qla2xxx_lport *lport;
1581 * Setup local pointer to vha, NPIV VP pointer (if present) and
1582 * vha->tcm_lport pointer
1584 lport = (struct tcm_qla2xxx_lport *)ha->tgt.target_lport_ptr;
1585 lport->qla_vha = vha;
1590 static struct se_wwn *tcm_qla2xxx_make_lport(
1591 struct target_fabric_configfs *tf,
1592 struct config_group *group,
1595 struct tcm_qla2xxx_lport *lport;
1599 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1600 return ERR_PTR(-EINVAL);
1602 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1604 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1605 return ERR_PTR(-ENOMEM);
1607 lport->lport_wwpn = wwpn;
1608 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1610 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
1612 ret = tcm_qla2xxx_init_lport(lport);
1616 ret = qlt_lport_register(&tcm_qla2xxx_template, wwpn,
1617 tcm_qla2xxx_lport_register_cb, lport);
1621 return &lport->lport_wwn;
1623 vfree(lport->lport_loopid_map);
1624 btree_destroy32(&lport->lport_fcport_map);
1627 return ERR_PTR(ret);
1630 static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1632 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1633 struct tcm_qla2xxx_lport, lport_wwn);
1634 struct scsi_qla_host *vha = lport->qla_vha;
1635 struct qla_hw_data *ha = vha->hw;
1636 struct se_node_acl *node;
1640 * Call into qla2x_target.c LLD logic to complete the
1641 * shutdown of struct qla_tgt after the call to
1642 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1644 if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stopped)
1645 qlt_stop_phase2(ha->tgt.qla_tgt);
1647 qlt_lport_deregister(vha);
1649 vfree(lport->lport_loopid_map);
1650 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1651 btree_remove32(&lport->lport_fcport_map, key);
1652 btree_destroy32(&lport->lport_fcport_map);
1656 static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1657 struct target_fabric_configfs *tf,
1658 struct config_group *group,
1661 struct tcm_qla2xxx_lport *lport;
1662 u64 npiv_wwpn, npiv_wwnn;
1665 if (tcm_qla2xxx_npiv_parse_wwn(name, strlen(name)+1,
1666 &npiv_wwpn, &npiv_wwnn) < 0)
1667 return ERR_PTR(-EINVAL);
1669 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1671 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1672 return ERR_PTR(-ENOMEM);
1674 lport->lport_npiv_wwpn = npiv_wwpn;
1675 lport->lport_npiv_wwnn = npiv_wwnn;
1676 tcm_qla2xxx_npiv_format_wwn(&lport->lport_npiv_name[0],
1677 TCM_QLA2XXX_NAMELEN, npiv_wwpn, npiv_wwnn);
1678 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
1680 /* FIXME: tcm_qla2xxx_npiv_make_lport */
1685 return &lport->lport_wwn;
1688 return ERR_PTR(ret);
1691 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1693 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1694 struct tcm_qla2xxx_lport, lport_wwn);
1695 struct scsi_qla_host *vha = lport->qla_vha;
1696 struct Scsi_Host *sh = vha->host;
1698 * Notify libfc that we want to release the lport->npiv_vport
1700 fc_vport_terminate(lport->npiv_vport);
1707 static ssize_t tcm_qla2xxx_wwn_show_attr_version(
1708 struct target_fabric_configfs *tf,
1711 return sprintf(page,
1712 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
1713 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1714 utsname()->machine);
1717 TF_WWN_ATTR_RO(tcm_qla2xxx, version);
1719 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1720 &tcm_qla2xxx_wwn_version.attr,
1724 static struct target_core_fabric_ops tcm_qla2xxx_ops = {
1725 .get_fabric_name = tcm_qla2xxx_get_fabric_name,
1726 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1727 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1728 .tpg_get_tag = tcm_qla2xxx_get_tag,
1729 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1730 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1731 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1732 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1733 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1734 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1735 .tpg_check_demo_mode_write_protect =
1736 tcm_qla2xxx_check_demo_write_protect,
1737 .tpg_check_prod_mode_write_protect =
1738 tcm_qla2xxx_check_prod_write_protect,
1739 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
1740 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1741 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1742 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1743 .check_stop_free = tcm_qla2xxx_check_stop_free,
1744 .release_cmd = tcm_qla2xxx_release_cmd,
1745 .put_session = tcm_qla2xxx_put_session,
1746 .shutdown_session = tcm_qla2xxx_shutdown_session,
1747 .close_session = tcm_qla2xxx_close_session,
1748 .sess_get_index = tcm_qla2xxx_sess_get_index,
1749 .sess_get_initiator_sid = NULL,
1750 .write_pending = tcm_qla2xxx_write_pending,
1751 .write_pending_status = tcm_qla2xxx_write_pending_status,
1752 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1753 .get_task_tag = tcm_qla2xxx_get_task_tag,
1754 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1755 .queue_data_in = tcm_qla2xxx_queue_data_in,
1756 .queue_status = tcm_qla2xxx_queue_status,
1757 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1759 * Setup function pointers for generic logic in
1760 * target_core_fabric_configfs.c
1762 .fabric_make_wwn = tcm_qla2xxx_make_lport,
1763 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
1764 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
1765 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1766 .fabric_post_link = NULL,
1767 .fabric_pre_unlink = NULL,
1768 .fabric_make_np = NULL,
1769 .fabric_drop_np = NULL,
1770 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1771 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1774 static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1775 .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
1776 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1777 .tpg_get_wwn = tcm_qla2xxx_npiv_get_fabric_wwn,
1778 .tpg_get_tag = tcm_qla2xxx_get_tag,
1779 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1780 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1781 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1782 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1783 .tpg_check_demo_mode = tcm_qla2xxx_check_false,
1784 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_true,
1785 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_true,
1786 .tpg_check_prod_mode_write_protect = tcm_qla2xxx_check_false,
1787 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
1788 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1789 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1790 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1791 .release_cmd = tcm_qla2xxx_release_cmd,
1792 .put_session = tcm_qla2xxx_put_session,
1793 .shutdown_session = tcm_qla2xxx_shutdown_session,
1794 .close_session = tcm_qla2xxx_close_session,
1795 .sess_get_index = tcm_qla2xxx_sess_get_index,
1796 .sess_get_initiator_sid = NULL,
1797 .write_pending = tcm_qla2xxx_write_pending,
1798 .write_pending_status = tcm_qla2xxx_write_pending_status,
1799 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1800 .get_task_tag = tcm_qla2xxx_get_task_tag,
1801 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1802 .queue_data_in = tcm_qla2xxx_queue_data_in,
1803 .queue_status = tcm_qla2xxx_queue_status,
1804 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1806 * Setup function pointers for generic logic in
1807 * target_core_fabric_configfs.c
1809 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
1810 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
1811 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
1812 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1813 .fabric_post_link = NULL,
1814 .fabric_pre_unlink = NULL,
1815 .fabric_make_np = NULL,
1816 .fabric_drop_np = NULL,
1817 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1818 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1821 static int tcm_qla2xxx_register_configfs(void)
1823 struct target_fabric_configfs *fabric, *npiv_fabric;
1826 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
1827 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1828 utsname()->machine);
1830 * Register the top level struct config_item_type with TCM core
1832 fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
1833 if (IS_ERR(fabric)) {
1834 pr_err("target_fabric_configfs_init() failed\n");
1835 return PTR_ERR(fabric);
1838 * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
1840 fabric->tf_ops = tcm_qla2xxx_ops;
1842 * Setup default attribute lists for various fabric->tf_cit_tmpl
1844 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1845 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
1846 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs =
1847 tcm_qla2xxx_tpg_attrib_attrs;
1848 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1849 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1850 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1851 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1852 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1853 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1855 * Register the fabric for use within TCM
1857 ret = target_fabric_configfs_register(fabric);
1859 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1863 * Setup our local pointer to *fabric
1865 tcm_qla2xxx_fabric_configfs = fabric;
1866 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
1869 * Register the top level struct config_item_type for NPIV with TCM core
1871 npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
1872 if (IS_ERR(npiv_fabric)) {
1873 pr_err("target_fabric_configfs_init() failed\n");
1874 ret = PTR_ERR(npiv_fabric);
1878 * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
1880 npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
1882 * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
1884 TF_CIT_TMPL(npiv_fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1885 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
1886 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1887 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1888 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1889 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1890 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1891 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1892 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1894 * Register the npiv_fabric for use within TCM
1896 ret = target_fabric_configfs_register(npiv_fabric);
1898 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1902 * Setup our local pointer to *npiv_fabric
1904 tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
1905 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
1907 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1909 if (!tcm_qla2xxx_free_wq) {
1911 goto out_fabric_npiv;
1914 tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
1915 if (!tcm_qla2xxx_cmd_wq) {
1923 destroy_workqueue(tcm_qla2xxx_free_wq);
1925 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1927 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1931 static void tcm_qla2xxx_deregister_configfs(void)
1933 destroy_workqueue(tcm_qla2xxx_cmd_wq);
1934 destroy_workqueue(tcm_qla2xxx_free_wq);
1936 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1937 tcm_qla2xxx_fabric_configfs = NULL;
1938 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
1940 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1941 tcm_qla2xxx_npiv_fabric_configfs = NULL;
1942 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
1945 static int __init tcm_qla2xxx_init(void)
1949 ret = tcm_qla2xxx_register_configfs();
1956 static void __exit tcm_qla2xxx_exit(void)
1958 tcm_qla2xxx_deregister_configfs();
1961 MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
1962 MODULE_LICENSE("GPL");
1963 module_init(tcm_qla2xxx_init);
1964 module_exit(tcm_qla2xxx_exit);