1 /*******************************************************************************
2 * This file contains tcm implementation using v4 configfs fabric infrastructure
3 * for QLogic target mode HBAs
5 * (c) Copyright 2010-2013 Datera, Inc.
7 * Author: Nicholas A. Bellinger <nab@daterainc.com>
9 * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
10 * the TCM_FC / Open-FCoE.org fabric module.
12 * Copyright (c) 2010 Cisco Systems, Inc
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 ****************************************************************************/
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/utsname.h>
29 #include <linux/vmalloc.h>
30 #include <linux/init.h>
31 #include <linux/list.h>
32 #include <linux/slab.h>
33 #include <linux/kthread.h>
34 #include <linux/types.h>
35 #include <linux/string.h>
36 #include <linux/configfs.h>
37 #include <linux/ctype.h>
38 #include <asm/unaligned.h>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_host.h>
41 #include <scsi/scsi_device.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <target/target_core_base.h>
44 #include <target/target_core_fabric.h>
47 #include "qla_target.h"
48 #include "tcm_qla2xxx.h"
50 static struct workqueue_struct *tcm_qla2xxx_free_wq;
51 static struct workqueue_struct *tcm_qla2xxx_cmd_wq;
55 * If strict, we require lower-case hex and colon separators to be sure
56 * the name is the same as what would be generated by ft_format_wwn()
57 * so the name and wwn are mapped one-to-one.
59 static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
69 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
71 if (c == '\n' && cp[1] == '\0')
73 if (strict && pos++ == 2 && byte++ < 7) {
82 if (strict && byte != 8)
89 else if (isxdigit(c) && (islower(c) || !strict))
90 nibble = tolower(c) - 'a' + 10;
93 *wwn = (*wwn << 4) | nibble;
97 pr_debug("err %u len %zu pos %u byte %u\n",
98 err, cp - name, pos, byte);
102 static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
106 put_unaligned_be64(wwn, b);
107 return snprintf(buf, len,
108 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
109 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
112 static char *tcm_qla2xxx_get_fabric_name(void)
118 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
120 static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
125 memset(wwn, 0, sizeof(wwn));
127 /* Validate and store the new name */
128 for (i = 0, j = 0; i < 16; i++) {
131 value = hex_to_bin(*ns++);
133 j = (j << 4) | value;
143 *nm = wwn_to_u64(wwn);
148 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
149 * store_fc_host_vport_create()
151 static int tcm_qla2xxx_npiv_parse_wwn(
157 unsigned int cnt = count;
163 /* count may include a LF at end of string */
164 if (name[cnt-1] == '\n' || name[cnt-1] == 0)
167 /* validate we have enough characters for WWPN */
168 if ((cnt != (16+1+16)) || (name[16] != ':'))
171 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
175 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
182 static char *tcm_qla2xxx_npiv_get_fabric_name(void)
184 return "qla2xxx_npiv";
187 static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
189 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
190 struct tcm_qla2xxx_tpg, se_tpg);
191 struct tcm_qla2xxx_lport *lport = tpg->lport;
193 return lport->lport_naa_name;
196 static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
198 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
199 struct tcm_qla2xxx_tpg, se_tpg);
200 return tpg->lport_tpgt;
203 static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
205 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
206 struct tcm_qla2xxx_tpg, se_tpg);
208 return tpg->tpg_attrib.generate_node_acls;
211 static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
213 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
214 struct tcm_qla2xxx_tpg, se_tpg);
216 return tpg->tpg_attrib.cache_dynamic_acls;
219 static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
221 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
222 struct tcm_qla2xxx_tpg, se_tpg);
224 return tpg->tpg_attrib.demo_mode_write_protect;
227 static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
229 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
230 struct tcm_qla2xxx_tpg, se_tpg);
232 return tpg->tpg_attrib.prod_mode_write_protect;
235 static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
237 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
238 struct tcm_qla2xxx_tpg, se_tpg);
240 return tpg->tpg_attrib.demo_mode_login_only;
243 static int tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group *se_tpg)
245 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
246 struct tcm_qla2xxx_tpg, se_tpg);
248 return tpg->tpg_attrib.fabric_prot_type;
251 static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
253 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
254 struct tcm_qla2xxx_tpg, se_tpg);
256 return tpg->lport_tpgt;
259 static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
261 struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
262 struct qla_tgt_mgmt_cmd, free_work);
264 transport_generic_free_cmd(&mcmd->se_cmd, 0);
268 * Called from qla_target_template->free_mcmd(), and will call
269 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
270 * release callback. qla_hw_data->hardware_lock is expected to be held
272 static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
274 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
275 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
278 static void tcm_qla2xxx_complete_free(struct work_struct *work)
280 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
284 WARN_ON(cmd->trc_flags & TRC_CMD_FREE);
286 cmd->qpair->tgt_counters.qla_core_ret_sta_ctio++;
287 cmd->trc_flags |= TRC_CMD_FREE;
288 transport_generic_free_cmd(&cmd->se_cmd, 0);
292 * Called from qla_target_template->free_cmd(), and will call
293 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
294 * release callback. qla_hw_data->hardware_lock is expected to be held
296 static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
298 cmd->qpair->tgt_counters.core_qla_free_cmd++;
301 WARN_ON(cmd->trc_flags & TRC_CMD_DONE);
302 cmd->trc_flags |= TRC_CMD_DONE;
304 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
305 queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
309 * Called from struct target_core_fabric_ops->check_stop_free() context
311 static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
313 struct qla_tgt_cmd *cmd;
315 if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
316 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
317 cmd->trc_flags |= TRC_CMD_CHK_STOP;
320 return target_put_sess_cmd(se_cmd);
323 /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
324 * fabric descriptor @se_cmd command to release
326 static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
328 struct qla_tgt_cmd *cmd;
330 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
331 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
332 struct qla_tgt_mgmt_cmd, se_cmd);
337 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
341 static void tcm_qla2xxx_release_session(struct kref *kref)
343 struct fc_port *sess = container_of(kref,
344 struct fc_port, sess_kref);
346 qlt_unreg_sess(sess);
349 static void tcm_qla2xxx_put_sess(struct fc_port *sess)
354 assert_spin_locked(&sess->vha->hw->tgt.sess_lock);
355 kref_put(&sess->sess_kref, tcm_qla2xxx_release_session);
358 static void tcm_qla2xxx_close_session(struct se_session *se_sess)
360 struct fc_port *sess = se_sess->fabric_sess_ptr;
361 struct scsi_qla_host *vha;
367 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
368 target_sess_cmd_list_set_waiting(se_sess);
369 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
371 tcm_qla2xxx_put_sess(sess);
374 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
379 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
381 struct qla_tgt_cmd *cmd = container_of(se_cmd,
382 struct qla_tgt_cmd, se_cmd);
385 /* Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
386 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
387 * already kick start the free.
389 pr_debug("write_pending aborted cmd[%p] refcount %d "
390 "transport_state %x, t_state %x, se_cmd_flags %x\n",
391 cmd, kref_read(&cmd->se_cmd.cmd_kref),
392 cmd->se_cmd.transport_state,
394 cmd->se_cmd.se_cmd_flags);
395 transport_generic_request_failure(&cmd->se_cmd,
396 TCM_CHECK_CONDITION_ABORT_CMD);
399 cmd->trc_flags |= TRC_XFR_RDY;
400 cmd->bufflen = se_cmd->data_length;
401 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
403 cmd->sg_cnt = se_cmd->t_data_nents;
404 cmd->sg = se_cmd->t_data_sg;
406 cmd->prot_sg_cnt = se_cmd->t_prot_nents;
407 cmd->prot_sg = se_cmd->t_prot_sg;
408 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size;
412 * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
413 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
415 return qlt_rdy_to_xfer(cmd);
418 static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
422 * Check for WRITE_PENDING status to determine if we need to wait for
423 * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
425 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
426 if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
427 se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
428 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
429 wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
433 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
438 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
443 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
445 if (!(se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
446 struct qla_tgt_cmd *cmd = container_of(se_cmd,
447 struct qla_tgt_cmd, se_cmd);
455 * Called from process context in qla_target.c:qlt_do_work() code
457 static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
458 unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
459 int data_dir, int bidi)
461 struct se_cmd *se_cmd = &cmd->se_cmd;
462 struct se_session *se_sess;
463 struct fc_port *sess;
464 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
465 struct se_portal_group *se_tpg;
466 struct tcm_qla2xxx_tpg *tpg;
468 int flags = TARGET_SCF_ACK_KREF;
471 flags |= TARGET_SCF_BIDI_OP;
473 if (se_cmd->cpuid != WORK_CPU_UNBOUND)
474 flags |= TARGET_SCF_USE_CPUID;
478 pr_err("Unable to locate struct fc_port from qla_tgt_cmd\n");
482 se_sess = sess->se_sess;
484 pr_err("Unable to locate active struct se_session\n");
488 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
489 se_tpg = se_sess->se_tpg;
490 tpg = container_of(se_tpg, struct tcm_qla2xxx_tpg, se_tpg);
491 if (unlikely(tpg->tpg_attrib.jam_host)) {
492 /* return, and dont run target_submit_cmd,discarding command */
497 cmd->qpair->tgt_counters.qla_core_sbt_cmd++;
498 return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
499 cmd->unpacked_lun, data_length, fcp_task_attr,
503 static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
505 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
508 * Ensure that the complete FCP WRITE payload has been received.
509 * Otherwise return an exception via CHECK_CONDITION status.
513 cmd->qpair->tgt_counters.qla_core_ret_ctio++;
514 if (!cmd->write_data_transferred) {
516 * Check if se_cmd has already been aborted via LUN_RESET, and
517 * waiting upon completion in tcm_qla2xxx_write_pending_status()
519 if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
520 complete(&cmd->se_cmd.t_transport_stop_comp);
524 switch (cmd->dif_err_code) {
527 TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
531 TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
535 TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
542 if (cmd->se_cmd.pi_err)
543 transport_generic_request_failure(&cmd->se_cmd,
546 transport_generic_request_failure(&cmd->se_cmd,
547 TCM_CHECK_CONDITION_ABORT_CMD);
552 return target_execute_cmd(&cmd->se_cmd);
556 * Called from qla_target.c:qlt_do_ctio_completion()
558 static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
560 cmd->trc_flags |= TRC_DATA_IN;
562 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
563 queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
566 static int tcm_qla2xxx_chk_dif_tags(uint32_t tag)
571 static int tcm_qla2xxx_dif_tags(struct qla_tgt_cmd *cmd,
572 uint16_t *pfw_prot_opts)
574 struct se_cmd *se_cmd = &cmd->se_cmd;
576 if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
577 *pfw_prot_opts |= PO_DISABLE_GUARD_CHECK;
579 if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG))
580 *pfw_prot_opts |= PO_DIS_APP_TAG_VALD;
586 * Called from qla_target.c:qlt_issue_task_mgmt()
588 static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, u64 lun,
589 uint16_t tmr_func, uint32_t tag)
591 struct fc_port *sess = mcmd->sess;
592 struct se_cmd *se_cmd = &mcmd->se_cmd;
593 int transl_tmr_func = 0;
594 int flags = TARGET_SCF_ACK_KREF;
598 pr_debug("%ld: ABTS received\n", sess->vha->host_no);
599 transl_tmr_func = TMR_ABORT_TASK;
600 flags |= TARGET_SCF_LOOKUP_LUN_FROM_TAG;
602 case QLA_TGT_2G_ABORT_TASK:
603 pr_debug("%ld: 2G Abort Task received\n", sess->vha->host_no);
604 transl_tmr_func = TMR_ABORT_TASK;
606 case QLA_TGT_CLEAR_ACA:
607 pr_debug("%ld: CLEAR_ACA received\n", sess->vha->host_no);
608 transl_tmr_func = TMR_CLEAR_ACA;
610 case QLA_TGT_TARGET_RESET:
611 pr_debug("%ld: TARGET_RESET received\n", sess->vha->host_no);
612 transl_tmr_func = TMR_TARGET_WARM_RESET;
614 case QLA_TGT_LUN_RESET:
615 pr_debug("%ld: LUN_RESET received\n", sess->vha->host_no);
616 transl_tmr_func = TMR_LUN_RESET;
618 case QLA_TGT_CLEAR_TS:
619 pr_debug("%ld: CLEAR_TS received\n", sess->vha->host_no);
620 transl_tmr_func = TMR_CLEAR_TASK_SET;
622 case QLA_TGT_ABORT_TS:
623 pr_debug("%ld: ABORT_TS received\n", sess->vha->host_no);
624 transl_tmr_func = TMR_ABORT_TASK_SET;
627 pr_debug("%ld: Unknown task mgmt fn 0x%x\n",
628 sess->vha->host_no, tmr_func);
632 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
633 transl_tmr_func, GFP_ATOMIC, tag, flags);
636 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
638 struct qla_tgt_cmd *cmd = container_of(se_cmd,
639 struct qla_tgt_cmd, se_cmd);
642 /* Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
643 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
644 * already kick start the free.
646 pr_debug("queue_data_in aborted cmd[%p] refcount %d "
647 "transport_state %x, t_state %x, se_cmd_flags %x\n",
648 cmd, kref_read(&cmd->se_cmd.cmd_kref),
649 cmd->se_cmd.transport_state,
651 cmd->se_cmd.se_cmd_flags);
655 cmd->trc_flags |= TRC_XMIT_DATA;
656 cmd->bufflen = se_cmd->data_length;
657 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
659 cmd->sg_cnt = se_cmd->t_data_nents;
660 cmd->sg = se_cmd->t_data_sg;
663 cmd->prot_sg_cnt = se_cmd->t_prot_nents;
664 cmd->prot_sg = se_cmd->t_prot_sg;
665 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size;
669 * Now queue completed DATA_IN the qla2xxx LLD and response ring
671 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
672 se_cmd->scsi_status);
675 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
677 struct qla_tgt_cmd *cmd = container_of(se_cmd,
678 struct qla_tgt_cmd, se_cmd);
679 int xmit_type = QLA_TGT_XMIT_STATUS;
683 * Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
684 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
685 * already kick start the free.
688 "queue_data_in aborted cmd[%p] refcount %d transport_state %x, t_state %x, se_cmd_flags %x\n",
689 cmd, kref_read(&cmd->se_cmd.cmd_kref),
690 cmd->se_cmd.transport_state, cmd->se_cmd.t_state,
691 cmd->se_cmd.se_cmd_flags);
694 cmd->bufflen = se_cmd->data_length;
698 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
699 cmd->trc_flags |= TRC_XMIT_STATUS;
701 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
703 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
704 * for qla_tgt_xmit_response LLD code
706 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
707 se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
708 se_cmd->residual_count = 0;
710 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
711 se_cmd->residual_count += se_cmd->data_length;
716 * Now queue status response to qla2xxx LLD code and response ring
718 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
721 static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
723 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
724 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
725 struct qla_tgt_mgmt_cmd, se_cmd);
727 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
728 mcmd, se_tmr->function, se_tmr->response);
730 * Do translation between TCM TM response codes and
731 * QLA2xxx FC TM response codes.
733 switch (se_tmr->response) {
734 case TMR_FUNCTION_COMPLETE:
735 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
737 case TMR_TASK_DOES_NOT_EXIST:
738 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
740 case TMR_FUNCTION_REJECTED:
741 mcmd->fc_tm_rsp = FC_TM_REJECT;
743 case TMR_LUN_DOES_NOT_EXIST:
745 mcmd->fc_tm_rsp = FC_TM_FAILED;
749 * Queue the TM response to QLA2xxx LLD to build a
750 * CTIO response packet.
752 qlt_xmit_tm_rsp(mcmd);
755 static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
757 struct qla_tgt_cmd *cmd = container_of(se_cmd,
758 struct qla_tgt_cmd, se_cmd);
760 if (qlt_abort_cmd(cmd))
764 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
765 struct tcm_qla2xxx_nacl *, struct fc_port *);
767 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
769 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port *sess)
771 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
772 struct se_portal_group *se_tpg = se_nacl->se_tpg;
773 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
774 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
775 struct tcm_qla2xxx_lport, lport_wwn);
776 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
777 struct tcm_qla2xxx_nacl, se_node_acl);
780 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
782 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
783 if (WARN_ON(node && (node != se_nacl))) {
785 * The nacl no longer matches what we think it should be.
786 * Most likely a new dynamic acl has been added while
787 * someone dropped the hardware lock. It clearly is a
788 * bug elsewhere, but this bit can't make things worse.
790 btree_insert32(&lport->lport_fcport_map, nacl->nport_id,
794 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
795 se_nacl, nacl->nport_wwnn, nacl->nport_id);
797 * Now clear the se_nacl and session pointers from our HW lport lookup
798 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
800 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
801 * target_wait_for_sess_cmds() before the session waits for outstanding
802 * I/O to complete, to avoid a race between session shutdown execution
803 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
805 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
808 static void tcm_qla2xxx_shutdown_sess(struct fc_port *sess)
810 assert_spin_locked(&sess->vha->hw->tgt.sess_lock);
811 target_sess_cmd_list_set_waiting(sess->se_sess);
814 static int tcm_qla2xxx_init_nodeacl(struct se_node_acl *se_nacl,
817 struct tcm_qla2xxx_nacl *nacl =
818 container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
821 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
824 nacl->nport_wwnn = wwnn;
825 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
830 /* Start items for tcm_qla2xxx_tpg_attrib_cit */
832 #define DEF_QLA_TPG_ATTRIB(name) \
834 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_show( \
835 struct config_item *item, char *page) \
837 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
838 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
839 struct tcm_qla2xxx_tpg, se_tpg); \
841 return sprintf(page, "%u\n", tpg->tpg_attrib.name); \
844 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_store( \
845 struct config_item *item, const char *page, size_t count) \
847 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
848 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
849 struct tcm_qla2xxx_tpg, se_tpg); \
850 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
854 ret = kstrtoul(page, 0, &val); \
856 pr_err("kstrtoul() failed with" \
857 " ret: %d\n", ret); \
861 if ((val != 0) && (val != 1)) { \
862 pr_err("Illegal boolean value %lu\n", val); \
870 CONFIGFS_ATTR(tcm_qla2xxx_tpg_attrib_, name)
872 DEF_QLA_TPG_ATTRIB(generate_node_acls);
873 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
874 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
875 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
876 DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
877 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
878 DEF_QLA_TPG_ATTRIB(jam_host);
881 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
882 &tcm_qla2xxx_tpg_attrib_attr_generate_node_acls,
883 &tcm_qla2xxx_tpg_attrib_attr_cache_dynamic_acls,
884 &tcm_qla2xxx_tpg_attrib_attr_demo_mode_write_protect,
885 &tcm_qla2xxx_tpg_attrib_attr_prod_mode_write_protect,
886 &tcm_qla2xxx_tpg_attrib_attr_demo_mode_login_only,
887 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
888 &tcm_qla2xxx_tpg_attrib_attr_jam_host,
893 /* End items for tcm_qla2xxx_tpg_attrib_cit */
895 static ssize_t tcm_qla2xxx_tpg_enable_show(struct config_item *item,
898 struct se_portal_group *se_tpg = to_tpg(item);
899 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
900 struct tcm_qla2xxx_tpg, se_tpg);
902 return snprintf(page, PAGE_SIZE, "%d\n",
903 atomic_read(&tpg->lport_tpg_enabled));
906 static ssize_t tcm_qla2xxx_tpg_enable_store(struct config_item *item,
907 const char *page, size_t count)
909 struct se_portal_group *se_tpg = to_tpg(item);
910 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
911 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
912 struct tcm_qla2xxx_lport, lport_wwn);
913 struct scsi_qla_host *vha = lport->qla_vha;
914 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
915 struct tcm_qla2xxx_tpg, se_tpg);
919 rc = kstrtoul(page, 0, &op);
921 pr_err("kstrtoul() returned %d\n", rc);
924 if ((op != 1) && (op != 0)) {
925 pr_err("Illegal value for tpg_enable: %lu\n", op);
929 if (atomic_read(&tpg->lport_tpg_enabled))
932 atomic_set(&tpg->lport_tpg_enabled, 1);
935 if (!atomic_read(&tpg->lport_tpg_enabled))
938 atomic_set(&tpg->lport_tpg_enabled, 0);
939 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
940 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
946 static ssize_t tcm_qla2xxx_tpg_dynamic_sessions_show(struct config_item *item,
949 return target_show_dynamic_sessions(to_tpg(item), page);
952 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_store(struct config_item *item,
953 const char *page, size_t count)
955 struct se_portal_group *se_tpg = to_tpg(item);
956 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
957 struct tcm_qla2xxx_tpg, se_tpg);
959 int ret = kstrtoul(page, 0, &val);
962 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
965 if (val != 0 && val != 1 && val != 3) {
966 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
969 tpg->tpg_attrib.fabric_prot_type = val;
974 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_show(struct config_item *item,
977 struct se_portal_group *se_tpg = to_tpg(item);
978 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
979 struct tcm_qla2xxx_tpg, se_tpg);
981 return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
984 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, enable);
985 CONFIGFS_ATTR_RO(tcm_qla2xxx_tpg_, dynamic_sessions);
986 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, fabric_prot_type);
988 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
989 &tcm_qla2xxx_tpg_attr_enable,
990 &tcm_qla2xxx_tpg_attr_dynamic_sessions,
991 &tcm_qla2xxx_tpg_attr_fabric_prot_type,
995 static struct se_portal_group *tcm_qla2xxx_make_tpg(
997 struct config_group *group,
1000 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1001 struct tcm_qla2xxx_lport, lport_wwn);
1002 struct tcm_qla2xxx_tpg *tpg;
1006 if (strstr(name, "tpgt_") != name)
1007 return ERR_PTR(-EINVAL);
1008 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1009 return ERR_PTR(-EINVAL);
1012 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1013 return ERR_PTR(-ENOSYS);
1016 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1018 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1019 return ERR_PTR(-ENOMEM);
1022 tpg->lport_tpgt = tpgt;
1024 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1027 tpg->tpg_attrib.generate_node_acls = 1;
1028 tpg->tpg_attrib.demo_mode_write_protect = 1;
1029 tpg->tpg_attrib.cache_dynamic_acls = 1;
1030 tpg->tpg_attrib.demo_mode_login_only = 1;
1031 tpg->tpg_attrib.jam_host = 0;
1033 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1041 return &tpg->se_tpg;
1044 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1046 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1047 struct tcm_qla2xxx_tpg, se_tpg);
1048 struct tcm_qla2xxx_lport *lport = tpg->lport;
1049 struct scsi_qla_host *vha = lport->qla_vha;
1051 * Call into qla2x_target.c LLD logic to shutdown the active
1052 * FC Nexuses and disable target mode operation for this qla_hw_data
1054 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
1055 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1057 core_tpg_deregister(se_tpg);
1059 * Clear local TPG=1 pointer for non NPIV mode.
1061 lport->tpg_1 = NULL;
1065 static ssize_t tcm_qla2xxx_npiv_tpg_enable_show(struct config_item *item,
1068 return tcm_qla2xxx_tpg_enable_show(item, page);
1071 static ssize_t tcm_qla2xxx_npiv_tpg_enable_store(struct config_item *item,
1072 const char *page, size_t count)
1074 struct se_portal_group *se_tpg = to_tpg(item);
1075 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1076 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1077 struct tcm_qla2xxx_lport, lport_wwn);
1078 struct scsi_qla_host *vha = lport->qla_vha;
1079 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1080 struct tcm_qla2xxx_tpg, se_tpg);
1084 rc = kstrtoul(page, 0, &op);
1086 pr_err("kstrtoul() returned %d\n", rc);
1089 if ((op != 1) && (op != 0)) {
1090 pr_err("Illegal value for tpg_enable: %lu\n", op);
1094 if (atomic_read(&tpg->lport_tpg_enabled))
1097 atomic_set(&tpg->lport_tpg_enabled, 1);
1098 qlt_enable_vha(vha);
1100 if (!atomic_read(&tpg->lport_tpg_enabled))
1103 atomic_set(&tpg->lport_tpg_enabled, 0);
1104 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1105 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1111 CONFIGFS_ATTR(tcm_qla2xxx_npiv_tpg_, enable);
1113 static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
1114 &tcm_qla2xxx_npiv_tpg_attr_enable,
1118 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1120 struct config_group *group,
1123 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1124 struct tcm_qla2xxx_lport, lport_wwn);
1125 struct tcm_qla2xxx_tpg *tpg;
1129 if (strstr(name, "tpgt_") != name)
1130 return ERR_PTR(-EINVAL);
1131 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1132 return ERR_PTR(-EINVAL);
1134 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1136 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1137 return ERR_PTR(-ENOMEM);
1140 tpg->lport_tpgt = tpgt;
1143 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1146 tpg->tpg_attrib.generate_node_acls = 1;
1147 tpg->tpg_attrib.demo_mode_write_protect = 1;
1148 tpg->tpg_attrib.cache_dynamic_acls = 1;
1149 tpg->tpg_attrib.demo_mode_login_only = 1;
1151 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1157 return &tpg->se_tpg;
1161 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1163 static struct fc_port *tcm_qla2xxx_find_sess_by_s_id(
1164 scsi_qla_host_t *vha,
1165 const uint8_t *s_id)
1167 struct tcm_qla2xxx_lport *lport;
1168 struct se_node_acl *se_nacl;
1169 struct tcm_qla2xxx_nacl *nacl;
1172 lport = vha->vha_tgt.target_lport_ptr;
1174 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1179 key = sid_to_key(s_id);
1180 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1182 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1184 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1187 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1188 se_nacl, se_nacl->initiatorname);
1190 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1191 if (!nacl->fc_port) {
1192 pr_err("Unable to locate struct fc_port\n");
1196 return nacl->fc_port;
1200 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1202 static void tcm_qla2xxx_set_sess_by_s_id(
1203 struct tcm_qla2xxx_lport *lport,
1204 struct se_node_acl *new_se_nacl,
1205 struct tcm_qla2xxx_nacl *nacl,
1206 struct se_session *se_sess,
1207 struct fc_port *fc_port,
1214 key = sid_to_key(s_id);
1215 pr_debug("set_sess_by_s_id: %06x\n", key);
1217 slot = btree_lookup32(&lport->lport_fcport_map, key);
1220 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1221 nacl->nport_id = key;
1222 rc = btree_insert32(&lport->lport_fcport_map, key,
1223 new_se_nacl, GFP_ATOMIC);
1225 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1228 pr_debug("Wiping nonexisting fc_port entry\n");
1231 fc_port->se_sess = se_sess;
1232 nacl->fc_port = fc_port;
1236 if (nacl->fc_port) {
1237 if (new_se_nacl == NULL) {
1238 pr_debug("Clearing existing nacl->fc_port and fc_port entry\n");
1239 btree_remove32(&lport->lport_fcport_map, key);
1240 nacl->fc_port = NULL;
1243 pr_debug("Replacing existing nacl->fc_port and fc_port entry\n");
1244 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1245 fc_port->se_sess = se_sess;
1246 nacl->fc_port = fc_port;
1250 if (new_se_nacl == NULL) {
1251 pr_debug("Clearing existing fc_port entry\n");
1252 btree_remove32(&lport->lport_fcport_map, key);
1256 pr_debug("Replacing existing fc_port entry w/o active nacl->fc_port\n");
1257 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1258 fc_port->se_sess = se_sess;
1259 nacl->fc_port = fc_port;
1261 pr_debug("Setup nacl->fc_port %p by s_id for se_nacl: %p, initiatorname: %s\n",
1262 nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1266 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1268 static struct fc_port *tcm_qla2xxx_find_sess_by_loop_id(
1269 scsi_qla_host_t *vha,
1270 const uint16_t loop_id)
1272 struct tcm_qla2xxx_lport *lport;
1273 struct se_node_acl *se_nacl;
1274 struct tcm_qla2xxx_nacl *nacl;
1275 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1277 lport = vha->vha_tgt.target_lport_ptr;
1279 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1284 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1286 fc_loopid = lport->lport_loopid_map + loop_id;
1287 se_nacl = fc_loopid->se_nacl;
1289 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1294 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1296 if (!nacl->fc_port) {
1297 pr_err("Unable to locate struct fc_port\n");
1301 return nacl->fc_port;
1305 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1307 static void tcm_qla2xxx_set_sess_by_loop_id(
1308 struct tcm_qla2xxx_lport *lport,
1309 struct se_node_acl *new_se_nacl,
1310 struct tcm_qla2xxx_nacl *nacl,
1311 struct se_session *se_sess,
1312 struct fc_port *fc_port,
1315 struct se_node_acl *saved_nacl;
1316 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1318 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1320 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1321 lport->lport_loopid_map)[loop_id];
1323 saved_nacl = fc_loopid->se_nacl;
1325 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1326 fc_loopid->se_nacl = new_se_nacl;
1327 if (fc_port->se_sess != se_sess)
1328 fc_port->se_sess = se_sess;
1329 if (nacl->fc_port != fc_port)
1330 nacl->fc_port = fc_port;
1334 if (nacl->fc_port) {
1335 if (new_se_nacl == NULL) {
1336 pr_debug("Clearing nacl->fc_port and fc_loopid->se_nacl\n");
1337 fc_loopid->se_nacl = NULL;
1338 nacl->fc_port = NULL;
1342 pr_debug("Replacing existing nacl->fc_port and fc_loopid->se_nacl\n");
1343 fc_loopid->se_nacl = new_se_nacl;
1344 if (fc_port->se_sess != se_sess)
1345 fc_port->se_sess = se_sess;
1346 if (nacl->fc_port != fc_port)
1347 nacl->fc_port = fc_port;
1351 if (new_se_nacl == NULL) {
1352 pr_debug("Clearing fc_loopid->se_nacl\n");
1353 fc_loopid->se_nacl = NULL;
1357 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->fc_port\n");
1358 fc_loopid->se_nacl = new_se_nacl;
1359 if (fc_port->se_sess != se_sess)
1360 fc_port->se_sess = se_sess;
1361 if (nacl->fc_port != fc_port)
1362 nacl->fc_port = fc_port;
1364 pr_debug("Setup nacl->fc_port %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1365 nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1369 * Should always be called with qla_hw_data->tgt.sess_lock held.
1371 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1372 struct tcm_qla2xxx_nacl *nacl, struct fc_port *sess)
1374 struct se_session *se_sess = sess->se_sess;
1375 unsigned char be_sid[3];
1377 be_sid[0] = sess->d_id.b.domain;
1378 be_sid[1] = sess->d_id.b.area;
1379 be_sid[2] = sess->d_id.b.al_pa;
1381 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1383 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1384 sess, sess->loop_id);
1387 static void tcm_qla2xxx_free_session(struct fc_port *sess)
1389 struct qla_tgt *tgt = sess->tgt;
1390 struct qla_hw_data *ha = tgt->ha;
1391 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1392 struct se_session *se_sess;
1393 struct tcm_qla2xxx_lport *lport;
1395 BUG_ON(in_interrupt());
1397 se_sess = sess->se_sess;
1399 pr_err("struct fc_port->se_sess is NULL\n");
1404 lport = vha->vha_tgt.target_lport_ptr;
1406 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1410 target_wait_for_sess_cmds(se_sess);
1412 transport_deregister_session_configfs(sess->se_sess);
1413 transport_deregister_session(sess->se_sess);
1416 static int tcm_qla2xxx_session_cb(struct se_portal_group *se_tpg,
1417 struct se_session *se_sess, void *p)
1419 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1420 struct tcm_qla2xxx_tpg, se_tpg);
1421 struct tcm_qla2xxx_lport *lport = tpg->lport;
1422 struct qla_hw_data *ha = lport->qla_vha->hw;
1423 struct se_node_acl *se_nacl = se_sess->se_node_acl;
1424 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1425 struct tcm_qla2xxx_nacl, se_node_acl);
1426 struct fc_port *qlat_sess = p;
1427 uint16_t loop_id = qlat_sess->loop_id;
1428 unsigned long flags;
1429 unsigned char be_sid[3];
1431 be_sid[0] = qlat_sess->d_id.b.domain;
1432 be_sid[1] = qlat_sess->d_id.b.area;
1433 be_sid[2] = qlat_sess->d_id.b.al_pa;
1436 * And now setup se_nacl and session pointers into HW lport internal
1437 * mappings for fabric S_ID and LOOP_ID.
1439 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1440 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl,
1441 se_sess, qlat_sess, be_sid);
1442 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl,
1443 se_sess, qlat_sess, loop_id);
1444 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1450 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1451 * to locate struct se_node_acl
1453 static int tcm_qla2xxx_check_initiator_node_acl(
1454 scsi_qla_host_t *vha,
1455 unsigned char *fc_wwpn,
1456 struct fc_port *qlat_sess)
1458 struct qla_hw_data *ha = vha->hw;
1459 struct tcm_qla2xxx_lport *lport;
1460 struct tcm_qla2xxx_tpg *tpg;
1461 struct se_session *se_sess;
1462 unsigned char port_name[36];
1463 int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count :
1464 TCM_QLA2XXX_DEFAULT_TAGS;
1466 lport = vha->vha_tgt.target_lport_ptr;
1468 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1473 * Locate the TPG=1 reference..
1477 pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1481 * Format the FCP Initiator port_name into colon seperated values to
1482 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1484 memset(&port_name, 0, 36);
1485 snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn);
1487 * Locate our struct se_node_acl either from an explict NodeACL created
1488 * via ConfigFS, or via running in TPG demo mode.
1490 se_sess = target_alloc_session(&tpg->se_tpg, num_tags,
1491 sizeof(struct qla_tgt_cmd),
1492 TARGET_PROT_ALL, port_name,
1493 qlat_sess, tcm_qla2xxx_session_cb);
1494 if (IS_ERR(se_sess))
1495 return PTR_ERR(se_sess);
1500 static void tcm_qla2xxx_update_sess(struct fc_port *sess, port_id_t s_id,
1501 uint16_t loop_id, bool conf_compl_supported)
1503 struct qla_tgt *tgt = sess->tgt;
1504 struct qla_hw_data *ha = tgt->ha;
1505 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1506 struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
1507 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1508 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1509 struct tcm_qla2xxx_nacl, se_node_acl);
1513 if (sess->loop_id != loop_id || sess->d_id.b24 != s_id.b24)
1514 pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1515 sess, sess->port_name,
1516 sess->loop_id, loop_id, sess->d_id.b.domain,
1517 sess->d_id.b.area, sess->d_id.b.al_pa, s_id.b.domain,
1518 s_id.b.area, s_id.b.al_pa);
1520 if (sess->loop_id != loop_id) {
1522 * Because we can shuffle loop IDs around and we
1523 * update different sessions non-atomically, we might
1524 * have overwritten this session's old loop ID
1525 * already, and we might end up overwriting some other
1526 * session that will be updated later. So we have to
1527 * be extra careful and we can't warn about those things...
1529 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1530 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1532 lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1534 sess->loop_id = loop_id;
1537 if (sess->d_id.b24 != s_id.b24) {
1538 key = (((u32) sess->d_id.b.domain << 16) |
1539 ((u32) sess->d_id.b.area << 8) |
1540 ((u32) sess->d_id.b.al_pa));
1542 if (btree_lookup32(&lport->lport_fcport_map, key))
1543 WARN(btree_remove32(&lport->lport_fcport_map, key) !=
1544 se_nacl, "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1545 sess->d_id.b.domain, sess->d_id.b.area,
1546 sess->d_id.b.al_pa);
1548 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1549 sess->d_id.b.domain, sess->d_id.b.area,
1550 sess->d_id.b.al_pa);
1552 key = (((u32) s_id.b.domain << 16) |
1553 ((u32) s_id.b.area << 8) |
1554 ((u32) s_id.b.al_pa));
1556 if (btree_lookup32(&lport->lport_fcport_map, key)) {
1557 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1558 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1559 btree_update32(&lport->lport_fcport_map, key, se_nacl);
1561 btree_insert32(&lport->lport_fcport_map, key, se_nacl,
1566 nacl->nport_id = key;
1569 sess->conf_compl_supported = conf_compl_supported;
1571 /* Reset logout parameters to default */
1572 sess->logout_on_delete = 1;
1573 sess->keep_nport_handle = 0;
1577 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1579 static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1580 .handle_cmd = tcm_qla2xxx_handle_cmd,
1581 .handle_data = tcm_qla2xxx_handle_data,
1582 .handle_tmr = tcm_qla2xxx_handle_tmr,
1583 .free_cmd = tcm_qla2xxx_free_cmd,
1584 .free_mcmd = tcm_qla2xxx_free_mcmd,
1585 .free_session = tcm_qla2xxx_free_session,
1586 .update_sess = tcm_qla2xxx_update_sess,
1587 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1588 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1589 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1590 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1591 .put_sess = tcm_qla2xxx_put_sess,
1592 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1593 .get_dif_tags = tcm_qla2xxx_dif_tags,
1594 .chk_dif_tags = tcm_qla2xxx_chk_dif_tags,
1597 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1601 rc = btree_init32(&lport->lport_fcport_map);
1603 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1607 lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
1609 if (!lport->lport_loopid_map) {
1610 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1611 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1612 btree_destroy32(&lport->lport_fcport_map);
1615 memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
1617 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1618 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1622 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
1623 void *target_lport_ptr,
1624 u64 npiv_wwpn, u64 npiv_wwnn)
1626 struct qla_hw_data *ha = vha->hw;
1627 struct tcm_qla2xxx_lport *lport =
1628 (struct tcm_qla2xxx_lport *)target_lport_ptr;
1630 * Setup tgt_ops, local pointer to vha and target_lport_ptr
1632 ha->tgt.tgt_ops = &tcm_qla2xxx_template;
1633 vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1634 lport->qla_vha = vha;
1639 static struct se_wwn *tcm_qla2xxx_make_lport(
1640 struct target_fabric_configfs *tf,
1641 struct config_group *group,
1644 struct tcm_qla2xxx_lport *lport;
1648 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1649 return ERR_PTR(-EINVAL);
1651 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1653 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1654 return ERR_PTR(-ENOMEM);
1656 lport->lport_wwpn = wwpn;
1657 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1659 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
1661 ret = tcm_qla2xxx_init_lport(lport);
1665 ret = qlt_lport_register(lport, wwpn, 0, 0,
1666 tcm_qla2xxx_lport_register_cb);
1670 return &lport->lport_wwn;
1672 vfree(lport->lport_loopid_map);
1673 btree_destroy32(&lport->lport_fcport_map);
1676 return ERR_PTR(ret);
1679 static void tcm_qla2xxx_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 se_node_acl *node;
1688 * Call into qla2x_target.c LLD logic to complete the
1689 * shutdown of struct qla_tgt after the call to
1690 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1692 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
1693 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1695 qlt_lport_deregister(vha);
1697 vfree(lport->lport_loopid_map);
1698 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1699 btree_remove32(&lport->lport_fcport_map, key);
1700 btree_destroy32(&lport->lport_fcport_map);
1704 static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
1705 void *target_lport_ptr,
1706 u64 npiv_wwpn, u64 npiv_wwnn)
1708 struct fc_vport *vport;
1709 struct Scsi_Host *sh = base_vha->host;
1710 struct scsi_qla_host *npiv_vha;
1711 struct tcm_qla2xxx_lport *lport =
1712 (struct tcm_qla2xxx_lport *)target_lport_ptr;
1713 struct tcm_qla2xxx_lport *base_lport =
1714 (struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
1715 struct fc_vport_identifiers vport_id;
1717 if (qla_ini_mode_enabled(base_vha)) {
1718 pr_err("qla2xxx base_vha not enabled for target mode\n");
1722 if (!base_lport || !base_lport->tpg_1 ||
1723 !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
1724 pr_err("qla2xxx base_lport or tpg_1 not available\n");
1728 memset(&vport_id, 0, sizeof(vport_id));
1729 vport_id.port_name = npiv_wwpn;
1730 vport_id.node_name = npiv_wwnn;
1731 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
1732 vport_id.vport_type = FC_PORTTYPE_NPIV;
1733 vport_id.disable = false;
1735 vport = fc_vport_create(sh, 0, &vport_id);
1737 pr_err("fc_vport_create failed for qla2xxx_npiv\n");
1741 * Setup local pointer to NPIV vhba + target_lport_ptr
1743 npiv_vha = (struct scsi_qla_host *)vport->dd_data;
1744 npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1745 lport->qla_vha = npiv_vha;
1746 scsi_host_get(npiv_vha->host);
1751 static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1752 struct target_fabric_configfs *tf,
1753 struct config_group *group,
1756 struct tcm_qla2xxx_lport *lport;
1757 u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
1761 snprintf(tmp, 128, "%s", name);
1763 p = strchr(tmp, '@');
1765 pr_err("Unable to locate NPIV '@' separator\n");
1766 return ERR_PTR(-EINVAL);
1770 if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
1771 return ERR_PTR(-EINVAL);
1773 if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
1774 &npiv_wwpn, &npiv_wwnn) < 0)
1775 return ERR_PTR(-EINVAL);
1777 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1779 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1780 return ERR_PTR(-ENOMEM);
1782 lport->lport_npiv_wwpn = npiv_wwpn;
1783 lport->lport_npiv_wwnn = npiv_wwnn;
1784 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
1786 ret = tcm_qla2xxx_init_lport(lport);
1790 ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
1791 tcm_qla2xxx_lport_register_npiv_cb);
1795 return &lport->lport_wwn;
1797 vfree(lport->lport_loopid_map);
1798 btree_destroy32(&lport->lport_fcport_map);
1801 return ERR_PTR(ret);
1804 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1806 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1807 struct tcm_qla2xxx_lport, lport_wwn);
1808 struct scsi_qla_host *npiv_vha = lport->qla_vha;
1809 struct qla_hw_data *ha = npiv_vha->hw;
1810 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1812 scsi_host_put(npiv_vha->host);
1814 * Notify libfc that we want to release the vha->fc_vport
1816 fc_vport_terminate(npiv_vha->fc_vport);
1817 scsi_host_put(base_vha->host);
1822 static ssize_t tcm_qla2xxx_wwn_version_show(struct config_item *item,
1825 return sprintf(page,
1826 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on %s\n",
1827 QLA2XXX_VERSION, utsname()->sysname,
1828 utsname()->machine, utsname()->release);
1831 CONFIGFS_ATTR_RO(tcm_qla2xxx_wwn_, version);
1833 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1834 &tcm_qla2xxx_wwn_attr_version,
1838 static const struct target_core_fabric_ops tcm_qla2xxx_ops = {
1839 .module = THIS_MODULE,
1841 .node_acl_size = sizeof(struct tcm_qla2xxx_nacl),
1843 * XXX: Limit assumes single page per scatter-gather-list entry.
1844 * Current maximum is ~4.9 MB per se_cmd->t_data_sg with PAGE_SIZE=4096
1846 .max_data_sg_nents = 1200,
1847 .get_fabric_name = tcm_qla2xxx_get_fabric_name,
1848 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1849 .tpg_get_tag = tcm_qla2xxx_get_tag,
1850 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1851 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1852 .tpg_check_demo_mode_write_protect =
1853 tcm_qla2xxx_check_demo_write_protect,
1854 .tpg_check_prod_mode_write_protect =
1855 tcm_qla2xxx_check_prod_write_protect,
1856 .tpg_check_prot_fabric_only = tcm_qla2xxx_check_prot_fabric_only,
1857 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1858 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1859 .check_stop_free = tcm_qla2xxx_check_stop_free,
1860 .release_cmd = tcm_qla2xxx_release_cmd,
1861 .close_session = tcm_qla2xxx_close_session,
1862 .sess_get_index = tcm_qla2xxx_sess_get_index,
1863 .sess_get_initiator_sid = NULL,
1864 .write_pending = tcm_qla2xxx_write_pending,
1865 .write_pending_status = tcm_qla2xxx_write_pending_status,
1866 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1867 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1868 .queue_data_in = tcm_qla2xxx_queue_data_in,
1869 .queue_status = tcm_qla2xxx_queue_status,
1870 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1871 .aborted_task = tcm_qla2xxx_aborted_task,
1873 * Setup function pointers for generic logic in
1874 * target_core_fabric_configfs.c
1876 .fabric_make_wwn = tcm_qla2xxx_make_lport,
1877 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
1878 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
1879 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1880 .fabric_init_nodeacl = tcm_qla2xxx_init_nodeacl,
1882 .tfc_wwn_attrs = tcm_qla2xxx_wwn_attrs,
1883 .tfc_tpg_base_attrs = tcm_qla2xxx_tpg_attrs,
1884 .tfc_tpg_attrib_attrs = tcm_qla2xxx_tpg_attrib_attrs,
1887 static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1888 .module = THIS_MODULE,
1889 .name = "qla2xxx_npiv",
1890 .node_acl_size = sizeof(struct tcm_qla2xxx_nacl),
1891 .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
1892 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1893 .tpg_get_tag = tcm_qla2xxx_get_tag,
1894 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1895 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1896 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
1897 .tpg_check_prod_mode_write_protect =
1898 tcm_qla2xxx_check_prod_write_protect,
1899 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1900 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1901 .check_stop_free = tcm_qla2xxx_check_stop_free,
1902 .release_cmd = tcm_qla2xxx_release_cmd,
1903 .close_session = tcm_qla2xxx_close_session,
1904 .sess_get_index = tcm_qla2xxx_sess_get_index,
1905 .sess_get_initiator_sid = NULL,
1906 .write_pending = tcm_qla2xxx_write_pending,
1907 .write_pending_status = tcm_qla2xxx_write_pending_status,
1908 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1909 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1910 .queue_data_in = tcm_qla2xxx_queue_data_in,
1911 .queue_status = tcm_qla2xxx_queue_status,
1912 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1913 .aborted_task = tcm_qla2xxx_aborted_task,
1915 * Setup function pointers for generic logic in
1916 * target_core_fabric_configfs.c
1918 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
1919 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
1920 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
1921 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1922 .fabric_init_nodeacl = tcm_qla2xxx_init_nodeacl,
1924 .tfc_wwn_attrs = tcm_qla2xxx_wwn_attrs,
1925 .tfc_tpg_base_attrs = tcm_qla2xxx_npiv_tpg_attrs,
1928 static int tcm_qla2xxx_register_configfs(void)
1932 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on %s\n",
1933 QLA2XXX_VERSION, utsname()->sysname,
1934 utsname()->machine, utsname()->release);
1936 ret = target_register_template(&tcm_qla2xxx_ops);
1940 ret = target_register_template(&tcm_qla2xxx_npiv_ops);
1944 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1946 if (!tcm_qla2xxx_free_wq) {
1948 goto out_fabric_npiv;
1951 tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
1952 if (!tcm_qla2xxx_cmd_wq) {
1960 destroy_workqueue(tcm_qla2xxx_free_wq);
1962 target_unregister_template(&tcm_qla2xxx_npiv_ops);
1964 target_unregister_template(&tcm_qla2xxx_ops);
1968 static void tcm_qla2xxx_deregister_configfs(void)
1970 destroy_workqueue(tcm_qla2xxx_cmd_wq);
1971 destroy_workqueue(tcm_qla2xxx_free_wq);
1973 target_unregister_template(&tcm_qla2xxx_ops);
1974 target_unregister_template(&tcm_qla2xxx_npiv_ops);
1977 static int __init tcm_qla2xxx_init(void)
1981 ret = tcm_qla2xxx_register_configfs();
1988 static void __exit tcm_qla2xxx_exit(void)
1990 tcm_qla2xxx_deregister_configfs();
1993 MODULE_DESCRIPTION("TCM QLA24XX+ series NPIV enabled fabric driver");
1994 MODULE_LICENSE("GPL");
1995 module_init(tcm_qla2xxx_init);
1996 module_exit(tcm_qla2xxx_exit);