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/utsname.h>
28 #include <linux/vmalloc.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/configfs.h>
34 #include <linux/ctype.h>
35 #include <asm/unaligned.h>
36 #include <scsi/scsi_host.h>
37 #include <target/target_core_base.h>
38 #include <target/target_core_fabric.h>
41 #include "qla_target.h"
42 #include "tcm_qla2xxx.h"
44 static struct workqueue_struct *tcm_qla2xxx_free_wq;
48 * If strict, we require lower-case hex and colon separators to be sure
49 * the name is the same as what would be generated by ft_format_wwn()
50 * so the name and wwn are mapped one-to-one.
52 static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
62 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
64 if (c == '\n' && cp[1] == '\0')
66 if (strict && pos++ == 2 && byte++ < 7) {
75 if (strict && byte != 8)
82 else if (isxdigit(c) && (islower(c) || !strict))
83 nibble = tolower(c) - 'a' + 10;
86 *wwn = (*wwn << 4) | nibble;
90 pr_debug("err %u len %zu pos %u byte %u\n",
91 err, cp - name, pos, byte);
95 static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
99 put_unaligned_be64(wwn, b);
100 return snprintf(buf, len,
101 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
102 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
106 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
108 static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
113 memset(wwn, 0, sizeof(wwn));
115 /* Validate and store the new name */
116 for (i = 0, j = 0; i < 16; i++) {
119 value = hex_to_bin(*ns++);
121 j = (j << 4) | value;
131 *nm = wwn_to_u64(wwn);
136 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
137 * store_fc_host_vport_create()
139 static int tcm_qla2xxx_npiv_parse_wwn(
145 unsigned int cnt = count;
151 /* count may include a LF at end of string */
152 if (name[cnt-1] == '\n' || name[cnt-1] == 0)
155 /* validate we have enough characters for WWPN */
156 if ((cnt != (16+1+16)) || (name[16] != ':'))
159 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
163 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
170 static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
172 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
173 struct tcm_qla2xxx_tpg, se_tpg);
174 struct tcm_qla2xxx_lport *lport = tpg->lport;
176 return lport->lport_naa_name;
179 static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
181 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
182 struct tcm_qla2xxx_tpg, se_tpg);
183 return tpg->lport_tpgt;
186 static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
188 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
189 struct tcm_qla2xxx_tpg, se_tpg);
191 return tpg->tpg_attrib.generate_node_acls;
194 static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
196 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
197 struct tcm_qla2xxx_tpg, se_tpg);
199 return tpg->tpg_attrib.cache_dynamic_acls;
202 static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
204 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
205 struct tcm_qla2xxx_tpg, se_tpg);
207 return tpg->tpg_attrib.demo_mode_write_protect;
210 static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
212 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
213 struct tcm_qla2xxx_tpg, se_tpg);
215 return tpg->tpg_attrib.prod_mode_write_protect;
218 static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
220 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
221 struct tcm_qla2xxx_tpg, se_tpg);
223 return tpg->tpg_attrib.demo_mode_login_only;
226 static int tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group *se_tpg)
228 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
229 struct tcm_qla2xxx_tpg, se_tpg);
231 return tpg->tpg_attrib.fabric_prot_type;
234 static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
236 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
237 struct tcm_qla2xxx_tpg, se_tpg);
239 return tpg->lport_tpgt;
242 static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
244 struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
245 struct qla_tgt_mgmt_cmd, free_work);
247 transport_generic_free_cmd(&mcmd->se_cmd, 0);
251 * Called from qla_target_template->free_mcmd(), and will call
252 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
253 * release callback. qla_hw_data->hardware_lock is expected to be held
255 static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
257 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
258 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
261 static void tcm_qla2xxx_complete_free(struct work_struct *work)
263 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
267 WARN_ON(cmd->trc_flags & TRC_CMD_FREE);
269 /* To do: protect all tgt_counters manipulations with proper locking. */
270 cmd->qpair->tgt_counters.qla_core_ret_sta_ctio++;
271 cmd->trc_flags |= TRC_CMD_FREE;
272 cmd->cmd_sent_to_fw = 0;
274 transport_generic_free_cmd(&cmd->se_cmd, 0);
278 * Called from qla_target_template->free_cmd(), and will call
279 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
280 * release callback. qla_hw_data->hardware_lock is expected to be held
282 static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
284 cmd->qpair->tgt_counters.core_qla_free_cmd++;
287 WARN_ON(cmd->trc_flags & TRC_CMD_DONE);
288 cmd->trc_flags |= TRC_CMD_DONE;
290 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
291 queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
295 * Called from struct target_core_fabric_ops->check_stop_free() context
297 static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
299 struct qla_tgt_cmd *cmd;
301 if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
302 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
303 cmd->trc_flags |= TRC_CMD_CHK_STOP;
306 return target_put_sess_cmd(se_cmd);
309 /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
310 * fabric descriptor @se_cmd command to release
312 static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
314 struct qla_tgt_cmd *cmd;
316 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
317 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
318 struct qla_tgt_mgmt_cmd, se_cmd);
322 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
324 if (WARN_ON(cmd->cmd_sent_to_fw))
330 static void tcm_qla2xxx_release_session(struct kref *kref)
332 struct fc_port *sess = container_of(kref,
333 struct fc_port, sess_kref);
335 qlt_unreg_sess(sess);
338 static void tcm_qla2xxx_put_sess(struct fc_port *sess)
343 kref_put(&sess->sess_kref, tcm_qla2xxx_release_session);
346 static void tcm_qla2xxx_close_session(struct se_session *se_sess)
348 struct fc_port *sess = se_sess->fabric_sess_ptr;
349 struct scsi_qla_host *vha;
355 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
356 target_sess_cmd_list_set_waiting(se_sess);
357 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
359 tcm_qla2xxx_put_sess(sess);
362 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
367 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
369 struct qla_tgt_cmd *cmd = container_of(se_cmd,
370 struct qla_tgt_cmd, se_cmd);
373 /* Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
374 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
375 * already kick start the free.
377 pr_debug("write_pending aborted cmd[%p] refcount %d "
378 "transport_state %x, t_state %x, se_cmd_flags %x\n",
379 cmd, kref_read(&cmd->se_cmd.cmd_kref),
380 cmd->se_cmd.transport_state,
382 cmd->se_cmd.se_cmd_flags);
383 transport_generic_request_failure(&cmd->se_cmd,
384 TCM_CHECK_CONDITION_ABORT_CMD);
387 cmd->trc_flags |= TRC_XFR_RDY;
388 cmd->bufflen = se_cmd->data_length;
389 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
391 cmd->sg_cnt = se_cmd->t_data_nents;
392 cmd->sg = se_cmd->t_data_sg;
394 cmd->prot_sg_cnt = se_cmd->t_prot_nents;
395 cmd->prot_sg = se_cmd->t_prot_sg;
396 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size;
400 * qla_target.c:qlt_rdy_to_xfer() will call dma_map_sg() to setup
401 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
403 return qlt_rdy_to_xfer(cmd);
406 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
411 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
413 if (!(se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
414 struct qla_tgt_cmd *cmd = container_of(se_cmd,
415 struct qla_tgt_cmd, se_cmd);
423 * Called from process context in qla_target.c:qlt_do_work() code
425 static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
426 unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
427 int data_dir, int bidi)
429 struct se_cmd *se_cmd = &cmd->se_cmd;
430 struct se_session *se_sess;
431 struct fc_port *sess;
432 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
433 struct se_portal_group *se_tpg;
434 struct tcm_qla2xxx_tpg *tpg;
436 int flags = TARGET_SCF_ACK_KREF;
439 flags |= TARGET_SCF_BIDI_OP;
441 if (se_cmd->cpuid != WORK_CPU_UNBOUND)
442 flags |= TARGET_SCF_USE_CPUID;
446 pr_err("Unable to locate struct fc_port from qla_tgt_cmd\n");
450 se_sess = sess->se_sess;
452 pr_err("Unable to locate active struct se_session\n");
456 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
457 se_tpg = se_sess->se_tpg;
458 tpg = container_of(se_tpg, struct tcm_qla2xxx_tpg, se_tpg);
459 if (unlikely(tpg->tpg_attrib.jam_host)) {
460 /* return, and dont run target_submit_cmd,discarding command */
465 cmd->qpair->tgt_counters.qla_core_sbt_cmd++;
466 return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
467 cmd->unpacked_lun, data_length, fcp_task_attr,
471 static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
473 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
476 * Ensure that the complete FCP WRITE payload has been received.
477 * Otherwise return an exception via CHECK_CONDITION status.
480 cmd->cmd_sent_to_fw = 0;
482 transport_generic_request_failure(&cmd->se_cmd,
483 TCM_CHECK_CONDITION_ABORT_CMD);
487 cmd->qpair->tgt_counters.qla_core_ret_ctio++;
488 if (!cmd->write_data_transferred) {
489 switch (cmd->dif_err_code) {
492 TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
496 TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
500 TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
507 if (cmd->se_cmd.pi_err)
508 transport_generic_request_failure(&cmd->se_cmd,
511 transport_generic_request_failure(&cmd->se_cmd,
512 TCM_CHECK_CONDITION_ABORT_CMD);
517 return target_execute_cmd(&cmd->se_cmd);
521 * Called from qla_target.c:qlt_do_ctio_completion()
523 static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
525 cmd->trc_flags |= TRC_DATA_IN;
527 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
528 queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
531 static int tcm_qla2xxx_chk_dif_tags(uint32_t tag)
536 static int tcm_qla2xxx_dif_tags(struct qla_tgt_cmd *cmd,
537 uint16_t *pfw_prot_opts)
539 struct se_cmd *se_cmd = &cmd->se_cmd;
541 if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
542 *pfw_prot_opts |= PO_DISABLE_GUARD_CHECK;
544 if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG))
545 *pfw_prot_opts |= PO_DIS_APP_TAG_VALD;
551 * Called from qla_target.c:qlt_issue_task_mgmt()
553 static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, u64 lun,
554 uint16_t tmr_func, uint32_t tag)
556 struct fc_port *sess = mcmd->sess;
557 struct se_cmd *se_cmd = &mcmd->se_cmd;
558 int transl_tmr_func = 0;
559 int flags = TARGET_SCF_ACK_KREF;
563 pr_debug("%ld: ABTS received\n", sess->vha->host_no);
564 transl_tmr_func = TMR_ABORT_TASK;
565 flags |= TARGET_SCF_LOOKUP_LUN_FROM_TAG;
567 case QLA_TGT_2G_ABORT_TASK:
568 pr_debug("%ld: 2G Abort Task received\n", sess->vha->host_no);
569 transl_tmr_func = TMR_ABORT_TASK;
571 case QLA_TGT_CLEAR_ACA:
572 pr_debug("%ld: CLEAR_ACA received\n", sess->vha->host_no);
573 transl_tmr_func = TMR_CLEAR_ACA;
575 case QLA_TGT_TARGET_RESET:
576 pr_debug("%ld: TARGET_RESET received\n", sess->vha->host_no);
577 transl_tmr_func = TMR_TARGET_WARM_RESET;
579 case QLA_TGT_LUN_RESET:
580 pr_debug("%ld: LUN_RESET received\n", sess->vha->host_no);
581 transl_tmr_func = TMR_LUN_RESET;
583 case QLA_TGT_CLEAR_TS:
584 pr_debug("%ld: CLEAR_TS received\n", sess->vha->host_no);
585 transl_tmr_func = TMR_CLEAR_TASK_SET;
587 case QLA_TGT_ABORT_TS:
588 pr_debug("%ld: ABORT_TS received\n", sess->vha->host_no);
589 transl_tmr_func = TMR_ABORT_TASK_SET;
592 pr_debug("%ld: Unknown task mgmt fn 0x%x\n",
593 sess->vha->host_no, tmr_func);
597 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
598 transl_tmr_func, GFP_ATOMIC, tag, flags);
601 static struct qla_tgt_cmd *tcm_qla2xxx_find_cmd_by_tag(struct fc_port *sess,
604 struct qla_tgt_cmd *cmd = NULL;
605 struct se_cmd *secmd;
611 spin_lock_irqsave(&sess->se_sess->sess_cmd_lock, flags);
612 list_for_each_entry(secmd, &sess->se_sess->sess_cmd_list, se_cmd_list) {
613 /* skip task management functions, including tmr->task_cmd */
614 if (secmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
617 if (secmd->tag == tag) {
618 cmd = container_of(secmd, struct qla_tgt_cmd, se_cmd);
622 spin_unlock_irqrestore(&sess->se_sess->sess_cmd_lock, flags);
627 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
629 struct qla_tgt_cmd *cmd = container_of(se_cmd,
630 struct qla_tgt_cmd, se_cmd);
633 /* Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
634 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
635 * already kick start the free.
637 pr_debug("queue_data_in aborted cmd[%p] refcount %d "
638 "transport_state %x, t_state %x, se_cmd_flags %x\n",
639 cmd, kref_read(&cmd->se_cmd.cmd_kref),
640 cmd->se_cmd.transport_state,
642 cmd->se_cmd.se_cmd_flags);
646 cmd->trc_flags |= TRC_XMIT_DATA;
647 cmd->bufflen = se_cmd->data_length;
648 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
650 cmd->sg_cnt = se_cmd->t_data_nents;
651 cmd->sg = se_cmd->t_data_sg;
654 cmd->prot_sg_cnt = se_cmd->t_prot_nents;
655 cmd->prot_sg = se_cmd->t_prot_sg;
656 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size;
660 * Now queue completed DATA_IN the qla2xxx LLD and response ring
662 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
663 se_cmd->scsi_status);
666 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
668 struct qla_tgt_cmd *cmd = container_of(se_cmd,
669 struct qla_tgt_cmd, se_cmd);
670 int xmit_type = QLA_TGT_XMIT_STATUS;
674 * Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
675 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
676 * already kick start the free.
679 "queue_data_in aborted cmd[%p] refcount %d transport_state %x, t_state %x, se_cmd_flags %x\n",
680 cmd, kref_read(&cmd->se_cmd.cmd_kref),
681 cmd->se_cmd.transport_state, cmd->se_cmd.t_state,
682 cmd->se_cmd.se_cmd_flags);
685 cmd->bufflen = se_cmd->data_length;
689 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
690 cmd->trc_flags |= TRC_XMIT_STATUS;
692 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
694 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
695 * for qla_tgt_xmit_response LLD code
697 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
698 se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
699 se_cmd->residual_count = 0;
701 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
702 se_cmd->residual_count += se_cmd->data_length;
707 * Now queue status response to qla2xxx LLD code and response ring
709 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
712 static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
714 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
715 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
716 struct qla_tgt_mgmt_cmd, se_cmd);
718 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
719 mcmd, se_tmr->function, se_tmr->response);
721 * Do translation between TCM TM response codes and
722 * QLA2xxx FC TM response codes.
724 switch (se_tmr->response) {
725 case TMR_FUNCTION_COMPLETE:
726 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
728 case TMR_TASK_DOES_NOT_EXIST:
729 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
731 case TMR_FUNCTION_REJECTED:
732 mcmd->fc_tm_rsp = FC_TM_REJECT;
734 case TMR_LUN_DOES_NOT_EXIST:
736 mcmd->fc_tm_rsp = FC_TM_FAILED;
740 * Queue the TM response to QLA2xxx LLD to build a
741 * CTIO response packet.
743 qlt_xmit_tm_rsp(mcmd);
746 static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
748 struct qla_tgt_cmd *cmd = container_of(se_cmd,
749 struct qla_tgt_cmd, se_cmd);
751 if (qlt_abort_cmd(cmd))
755 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
756 struct tcm_qla2xxx_nacl *, struct fc_port *);
758 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
760 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port *sess)
762 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
763 struct se_portal_group *se_tpg = se_nacl->se_tpg;
764 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
765 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
766 struct tcm_qla2xxx_lport, lport_wwn);
767 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
768 struct tcm_qla2xxx_nacl, se_node_acl);
771 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
773 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
774 if (WARN_ON(node && (node != se_nacl))) {
776 * The nacl no longer matches what we think it should be.
777 * Most likely a new dynamic acl has been added while
778 * someone dropped the hardware lock. It clearly is a
779 * bug elsewhere, but this bit can't make things worse.
781 btree_insert32(&lport->lport_fcport_map, nacl->nport_id,
785 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
786 se_nacl, nacl->nport_wwnn, nacl->nport_id);
788 * Now clear the se_nacl and session pointers from our HW lport lookup
789 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
791 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
792 * target_wait_for_sess_cmds() before the session waits for outstanding
793 * I/O to complete, to avoid a race between session shutdown execution
794 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
796 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
799 static void tcm_qla2xxx_shutdown_sess(struct fc_port *sess)
801 target_sess_cmd_list_set_waiting(sess->se_sess);
804 static int tcm_qla2xxx_init_nodeacl(struct se_node_acl *se_nacl,
807 struct tcm_qla2xxx_nacl *nacl =
808 container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
811 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
814 nacl->nport_wwnn = wwnn;
815 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
820 /* Start items for tcm_qla2xxx_tpg_attrib_cit */
822 #define DEF_QLA_TPG_ATTRIB(name) \
824 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_show( \
825 struct config_item *item, char *page) \
827 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
828 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
829 struct tcm_qla2xxx_tpg, se_tpg); \
831 return sprintf(page, "%u\n", tpg->tpg_attrib.name); \
834 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_store( \
835 struct config_item *item, const char *page, size_t count) \
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); \
840 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
844 ret = kstrtoul(page, 0, &val); \
846 pr_err("kstrtoul() failed with" \
847 " ret: %d\n", ret); \
851 if ((val != 0) && (val != 1)) { \
852 pr_err("Illegal boolean value %lu\n", val); \
860 CONFIGFS_ATTR(tcm_qla2xxx_tpg_attrib_, name)
862 DEF_QLA_TPG_ATTRIB(generate_node_acls);
863 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
864 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
865 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
866 DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
867 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
868 DEF_QLA_TPG_ATTRIB(jam_host);
871 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
872 &tcm_qla2xxx_tpg_attrib_attr_generate_node_acls,
873 &tcm_qla2xxx_tpg_attrib_attr_cache_dynamic_acls,
874 &tcm_qla2xxx_tpg_attrib_attr_demo_mode_write_protect,
875 &tcm_qla2xxx_tpg_attrib_attr_prod_mode_write_protect,
876 &tcm_qla2xxx_tpg_attrib_attr_demo_mode_login_only,
877 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
878 &tcm_qla2xxx_tpg_attrib_attr_jam_host,
883 /* End items for tcm_qla2xxx_tpg_attrib_cit */
885 static ssize_t tcm_qla2xxx_tpg_enable_show(struct config_item *item,
888 struct se_portal_group *se_tpg = to_tpg(item);
889 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
890 struct tcm_qla2xxx_tpg, se_tpg);
892 return snprintf(page, PAGE_SIZE, "%d\n",
893 atomic_read(&tpg->lport_tpg_enabled));
896 static ssize_t tcm_qla2xxx_tpg_enable_store(struct config_item *item,
897 const char *page, size_t count)
899 struct se_portal_group *se_tpg = to_tpg(item);
900 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
901 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
902 struct tcm_qla2xxx_lport, lport_wwn);
903 struct scsi_qla_host *vha = lport->qla_vha;
904 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
905 struct tcm_qla2xxx_tpg, se_tpg);
909 rc = kstrtoul(page, 0, &op);
911 pr_err("kstrtoul() returned %d\n", rc);
914 if ((op != 1) && (op != 0)) {
915 pr_err("Illegal value for tpg_enable: %lu\n", op);
919 if (atomic_read(&tpg->lport_tpg_enabled))
922 atomic_set(&tpg->lport_tpg_enabled, 1);
925 if (!atomic_read(&tpg->lport_tpg_enabled))
928 atomic_set(&tpg->lport_tpg_enabled, 0);
929 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
935 static ssize_t tcm_qla2xxx_tpg_dynamic_sessions_show(struct config_item *item,
938 return target_show_dynamic_sessions(to_tpg(item), page);
941 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_store(struct config_item *item,
942 const char *page, size_t count)
944 struct se_portal_group *se_tpg = to_tpg(item);
945 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
946 struct tcm_qla2xxx_tpg, se_tpg);
948 int ret = kstrtoul(page, 0, &val);
951 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
954 if (val != 0 && val != 1 && val != 3) {
955 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
958 tpg->tpg_attrib.fabric_prot_type = val;
963 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_show(struct config_item *item,
966 struct se_portal_group *se_tpg = to_tpg(item);
967 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
968 struct tcm_qla2xxx_tpg, se_tpg);
970 return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
973 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, enable);
974 CONFIGFS_ATTR_RO(tcm_qla2xxx_tpg_, dynamic_sessions);
975 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, fabric_prot_type);
977 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
978 &tcm_qla2xxx_tpg_attr_enable,
979 &tcm_qla2xxx_tpg_attr_dynamic_sessions,
980 &tcm_qla2xxx_tpg_attr_fabric_prot_type,
984 static struct se_portal_group *tcm_qla2xxx_make_tpg(struct se_wwn *wwn,
987 struct tcm_qla2xxx_lport *lport = container_of(wwn,
988 struct tcm_qla2xxx_lport, lport_wwn);
989 struct tcm_qla2xxx_tpg *tpg;
993 if (strstr(name, "tpgt_") != name)
994 return ERR_PTR(-EINVAL);
995 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
996 return ERR_PTR(-EINVAL);
999 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1000 return ERR_PTR(-ENOSYS);
1003 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1005 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1006 return ERR_PTR(-ENOMEM);
1009 tpg->lport_tpgt = tpgt;
1011 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1014 tpg->tpg_attrib.generate_node_acls = 1;
1015 tpg->tpg_attrib.demo_mode_write_protect = 1;
1016 tpg->tpg_attrib.cache_dynamic_acls = 1;
1017 tpg->tpg_attrib.demo_mode_login_only = 1;
1018 tpg->tpg_attrib.jam_host = 0;
1020 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1028 return &tpg->se_tpg;
1031 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1033 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1034 struct tcm_qla2xxx_tpg, se_tpg);
1035 struct tcm_qla2xxx_lport *lport = tpg->lport;
1036 struct scsi_qla_host *vha = lport->qla_vha;
1038 * Call into qla2x_target.c LLD logic to shutdown the active
1039 * FC Nexuses and disable target mode operation for this qla_hw_data
1041 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
1042 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1044 core_tpg_deregister(se_tpg);
1046 * Clear local TPG=1 pointer for non NPIV mode.
1048 lport->tpg_1 = NULL;
1052 static ssize_t tcm_qla2xxx_npiv_tpg_enable_show(struct config_item *item,
1055 return tcm_qla2xxx_tpg_enable_show(item, page);
1058 static ssize_t tcm_qla2xxx_npiv_tpg_enable_store(struct config_item *item,
1059 const char *page, size_t count)
1061 struct se_portal_group *se_tpg = to_tpg(item);
1062 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1063 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1064 struct tcm_qla2xxx_lport, lport_wwn);
1065 struct scsi_qla_host *vha = lport->qla_vha;
1066 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1067 struct tcm_qla2xxx_tpg, se_tpg);
1071 rc = kstrtoul(page, 0, &op);
1073 pr_err("kstrtoul() returned %d\n", rc);
1076 if ((op != 1) && (op != 0)) {
1077 pr_err("Illegal value for tpg_enable: %lu\n", op);
1081 if (atomic_read(&tpg->lport_tpg_enabled))
1084 atomic_set(&tpg->lport_tpg_enabled, 1);
1085 qlt_enable_vha(vha);
1087 if (!atomic_read(&tpg->lport_tpg_enabled))
1090 atomic_set(&tpg->lport_tpg_enabled, 0);
1091 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1097 CONFIGFS_ATTR(tcm_qla2xxx_npiv_tpg_, enable);
1099 static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
1100 &tcm_qla2xxx_npiv_tpg_attr_enable,
1104 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(struct se_wwn *wwn,
1107 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1108 struct tcm_qla2xxx_lport, lport_wwn);
1109 struct tcm_qla2xxx_tpg *tpg;
1113 if (strstr(name, "tpgt_") != name)
1114 return ERR_PTR(-EINVAL);
1115 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1116 return ERR_PTR(-EINVAL);
1118 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1120 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1121 return ERR_PTR(-ENOMEM);
1124 tpg->lport_tpgt = tpgt;
1127 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1130 tpg->tpg_attrib.generate_node_acls = 1;
1131 tpg->tpg_attrib.demo_mode_write_protect = 1;
1132 tpg->tpg_attrib.cache_dynamic_acls = 1;
1133 tpg->tpg_attrib.demo_mode_login_only = 1;
1135 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1141 return &tpg->se_tpg;
1145 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1147 static struct fc_port *tcm_qla2xxx_find_sess_by_s_id(
1148 scsi_qla_host_t *vha,
1149 const uint8_t *s_id)
1151 struct tcm_qla2xxx_lport *lport;
1152 struct se_node_acl *se_nacl;
1153 struct tcm_qla2xxx_nacl *nacl;
1156 lport = vha->vha_tgt.target_lport_ptr;
1158 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1163 key = sid_to_key(s_id);
1164 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1166 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1168 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1171 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1172 se_nacl, se_nacl->initiatorname);
1174 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1175 if (!nacl->fc_port) {
1176 pr_err("Unable to locate struct fc_port\n");
1180 return nacl->fc_port;
1184 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1186 static void tcm_qla2xxx_set_sess_by_s_id(
1187 struct tcm_qla2xxx_lport *lport,
1188 struct se_node_acl *new_se_nacl,
1189 struct tcm_qla2xxx_nacl *nacl,
1190 struct se_session *se_sess,
1191 struct fc_port *fc_port,
1198 key = sid_to_key(s_id);
1199 pr_debug("set_sess_by_s_id: %06x\n", key);
1201 slot = btree_lookup32(&lport->lport_fcport_map, key);
1204 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1205 nacl->nport_id = key;
1206 rc = btree_insert32(&lport->lport_fcport_map, key,
1207 new_se_nacl, GFP_ATOMIC);
1209 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1212 pr_debug("Wiping nonexisting fc_port entry\n");
1215 fc_port->se_sess = se_sess;
1216 nacl->fc_port = fc_port;
1220 if (nacl->fc_port) {
1221 if (new_se_nacl == NULL) {
1222 pr_debug("Clearing existing nacl->fc_port and fc_port entry\n");
1223 btree_remove32(&lport->lport_fcport_map, key);
1224 nacl->fc_port = NULL;
1227 pr_debug("Replacing existing nacl->fc_port and fc_port entry\n");
1228 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1229 fc_port->se_sess = se_sess;
1230 nacl->fc_port = fc_port;
1234 if (new_se_nacl == NULL) {
1235 pr_debug("Clearing existing fc_port entry\n");
1236 btree_remove32(&lport->lport_fcport_map, key);
1240 pr_debug("Replacing existing fc_port entry w/o active nacl->fc_port\n");
1241 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1242 fc_port->se_sess = se_sess;
1243 nacl->fc_port = fc_port;
1245 pr_debug("Setup nacl->fc_port %p by s_id for se_nacl: %p, initiatorname: %s\n",
1246 nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1250 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1252 static struct fc_port *tcm_qla2xxx_find_sess_by_loop_id(
1253 scsi_qla_host_t *vha,
1254 const uint16_t loop_id)
1256 struct tcm_qla2xxx_lport *lport;
1257 struct se_node_acl *se_nacl;
1258 struct tcm_qla2xxx_nacl *nacl;
1259 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1261 lport = vha->vha_tgt.target_lport_ptr;
1263 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1268 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1270 fc_loopid = lport->lport_loopid_map + loop_id;
1271 se_nacl = fc_loopid->se_nacl;
1273 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1278 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1280 if (!nacl->fc_port) {
1281 pr_err("Unable to locate struct fc_port\n");
1285 return nacl->fc_port;
1289 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1291 static void tcm_qla2xxx_set_sess_by_loop_id(
1292 struct tcm_qla2xxx_lport *lport,
1293 struct se_node_acl *new_se_nacl,
1294 struct tcm_qla2xxx_nacl *nacl,
1295 struct se_session *se_sess,
1296 struct fc_port *fc_port,
1299 struct se_node_acl *saved_nacl;
1300 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1302 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1304 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1305 lport->lport_loopid_map)[loop_id];
1307 saved_nacl = fc_loopid->se_nacl;
1309 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1310 fc_loopid->se_nacl = new_se_nacl;
1311 if (fc_port->se_sess != se_sess)
1312 fc_port->se_sess = se_sess;
1313 if (nacl->fc_port != fc_port)
1314 nacl->fc_port = fc_port;
1318 if (nacl->fc_port) {
1319 if (new_se_nacl == NULL) {
1320 pr_debug("Clearing nacl->fc_port and fc_loopid->se_nacl\n");
1321 fc_loopid->se_nacl = NULL;
1322 nacl->fc_port = NULL;
1326 pr_debug("Replacing existing nacl->fc_port and fc_loopid->se_nacl\n");
1327 fc_loopid->se_nacl = new_se_nacl;
1328 if (fc_port->se_sess != se_sess)
1329 fc_port->se_sess = se_sess;
1330 if (nacl->fc_port != fc_port)
1331 nacl->fc_port = fc_port;
1335 if (new_se_nacl == NULL) {
1336 pr_debug("Clearing fc_loopid->se_nacl\n");
1337 fc_loopid->se_nacl = NULL;
1341 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->fc_port\n");
1342 fc_loopid->se_nacl = new_se_nacl;
1343 if (fc_port->se_sess != se_sess)
1344 fc_port->se_sess = se_sess;
1345 if (nacl->fc_port != fc_port)
1346 nacl->fc_port = fc_port;
1348 pr_debug("Setup nacl->fc_port %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1349 nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1353 * Should always be called with qla_hw_data->tgt.sess_lock held.
1355 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1356 struct tcm_qla2xxx_nacl *nacl, struct fc_port *sess)
1358 struct se_session *se_sess = sess->se_sess;
1359 unsigned char be_sid[3];
1361 be_sid[0] = sess->d_id.b.domain;
1362 be_sid[1] = sess->d_id.b.area;
1363 be_sid[2] = sess->d_id.b.al_pa;
1365 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1367 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1368 sess, sess->loop_id);
1371 static void tcm_qla2xxx_free_session(struct fc_port *sess)
1373 struct qla_tgt *tgt = sess->tgt;
1374 struct qla_hw_data *ha = tgt->ha;
1375 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1376 struct se_session *se_sess;
1377 struct tcm_qla2xxx_lport *lport;
1379 BUG_ON(in_interrupt());
1381 se_sess = sess->se_sess;
1383 pr_err("struct fc_port->se_sess is NULL\n");
1388 lport = vha->vha_tgt.target_lport_ptr;
1390 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1394 target_wait_for_sess_cmds(se_sess);
1396 target_remove_session(se_sess);
1399 static int tcm_qla2xxx_session_cb(struct se_portal_group *se_tpg,
1400 struct se_session *se_sess, void *p)
1402 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1403 struct tcm_qla2xxx_tpg, se_tpg);
1404 struct tcm_qla2xxx_lport *lport = tpg->lport;
1405 struct qla_hw_data *ha = lport->qla_vha->hw;
1406 struct se_node_acl *se_nacl = se_sess->se_node_acl;
1407 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1408 struct tcm_qla2xxx_nacl, se_node_acl);
1409 struct fc_port *qlat_sess = p;
1410 uint16_t loop_id = qlat_sess->loop_id;
1411 unsigned long flags;
1412 unsigned char be_sid[3];
1414 be_sid[0] = qlat_sess->d_id.b.domain;
1415 be_sid[1] = qlat_sess->d_id.b.area;
1416 be_sid[2] = qlat_sess->d_id.b.al_pa;
1419 * And now setup se_nacl and session pointers into HW lport internal
1420 * mappings for fabric S_ID and LOOP_ID.
1422 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1423 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl,
1424 se_sess, qlat_sess, be_sid);
1425 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl,
1426 se_sess, qlat_sess, loop_id);
1427 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1433 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1434 * to locate struct se_node_acl
1436 static int tcm_qla2xxx_check_initiator_node_acl(
1437 scsi_qla_host_t *vha,
1438 unsigned char *fc_wwpn,
1439 struct fc_port *qlat_sess)
1441 struct qla_hw_data *ha = vha->hw;
1442 struct tcm_qla2xxx_lport *lport;
1443 struct tcm_qla2xxx_tpg *tpg;
1444 struct se_session *se_sess;
1445 unsigned char port_name[36];
1446 int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count :
1447 TCM_QLA2XXX_DEFAULT_TAGS;
1449 lport = vha->vha_tgt.target_lport_ptr;
1451 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1456 * Locate the TPG=1 reference..
1460 pr_err("Unable to locate struct tcm_qla2xxx_lport->tpg_1\n");
1464 * Format the FCP Initiator port_name into colon seperated values to
1465 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1467 memset(&port_name, 0, 36);
1468 snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn);
1470 * Locate our struct se_node_acl either from an explict NodeACL created
1471 * via ConfigFS, or via running in TPG demo mode.
1473 se_sess = target_setup_session(&tpg->se_tpg, num_tags,
1474 sizeof(struct qla_tgt_cmd),
1475 TARGET_PROT_ALL, port_name,
1476 qlat_sess, tcm_qla2xxx_session_cb);
1477 if (IS_ERR(se_sess))
1478 return PTR_ERR(se_sess);
1483 static void tcm_qla2xxx_update_sess(struct fc_port *sess, port_id_t s_id,
1484 uint16_t loop_id, bool conf_compl_supported)
1486 struct qla_tgt *tgt = sess->tgt;
1487 struct qla_hw_data *ha = tgt->ha;
1488 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1489 struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
1490 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1491 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1492 struct tcm_qla2xxx_nacl, se_node_acl);
1496 if (sess->loop_id != loop_id || sess->d_id.b24 != s_id.b24)
1497 pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1498 sess, sess->port_name,
1499 sess->loop_id, loop_id, sess->d_id.b.domain,
1500 sess->d_id.b.area, sess->d_id.b.al_pa, s_id.b.domain,
1501 s_id.b.area, s_id.b.al_pa);
1503 if (sess->loop_id != loop_id) {
1505 * Because we can shuffle loop IDs around and we
1506 * update different sessions non-atomically, we might
1507 * have overwritten this session's old loop ID
1508 * already, and we might end up overwriting some other
1509 * session that will be updated later. So we have to
1510 * be extra careful and we can't warn about those things...
1512 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1513 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1515 lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1517 sess->loop_id = loop_id;
1520 if (sess->d_id.b24 != s_id.b24) {
1521 key = (((u32) sess->d_id.b.domain << 16) |
1522 ((u32) sess->d_id.b.area << 8) |
1523 ((u32) sess->d_id.b.al_pa));
1525 if (btree_lookup32(&lport->lport_fcport_map, key))
1526 WARN(btree_remove32(&lport->lport_fcport_map, key) !=
1527 se_nacl, "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1528 sess->d_id.b.domain, sess->d_id.b.area,
1529 sess->d_id.b.al_pa);
1531 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1532 sess->d_id.b.domain, sess->d_id.b.area,
1533 sess->d_id.b.al_pa);
1535 key = (((u32) s_id.b.domain << 16) |
1536 ((u32) s_id.b.area << 8) |
1537 ((u32) s_id.b.al_pa));
1539 if (btree_lookup32(&lport->lport_fcport_map, key)) {
1540 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1541 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1542 btree_update32(&lport->lport_fcport_map, key, se_nacl);
1544 btree_insert32(&lport->lport_fcport_map, key, se_nacl,
1549 nacl->nport_id = key;
1552 sess->conf_compl_supported = conf_compl_supported;
1557 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1559 static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1560 .find_cmd_by_tag = tcm_qla2xxx_find_cmd_by_tag,
1561 .handle_cmd = tcm_qla2xxx_handle_cmd,
1562 .handle_data = tcm_qla2xxx_handle_data,
1563 .handle_tmr = tcm_qla2xxx_handle_tmr,
1564 .free_cmd = tcm_qla2xxx_free_cmd,
1565 .free_mcmd = tcm_qla2xxx_free_mcmd,
1566 .free_session = tcm_qla2xxx_free_session,
1567 .update_sess = tcm_qla2xxx_update_sess,
1568 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1569 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1570 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1571 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1572 .put_sess = tcm_qla2xxx_put_sess,
1573 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1574 .get_dif_tags = tcm_qla2xxx_dif_tags,
1575 .chk_dif_tags = tcm_qla2xxx_chk_dif_tags,
1578 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1582 rc = btree_init32(&lport->lport_fcport_map);
1584 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1588 lport->lport_loopid_map =
1589 vzalloc(array_size(65536,
1590 sizeof(struct tcm_qla2xxx_fc_loopid)));
1591 if (!lport->lport_loopid_map) {
1592 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1593 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1594 btree_destroy32(&lport->lport_fcport_map);
1597 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1598 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1602 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
1603 void *target_lport_ptr,
1604 u64 npiv_wwpn, u64 npiv_wwnn)
1606 struct qla_hw_data *ha = vha->hw;
1607 struct tcm_qla2xxx_lport *lport =
1608 (struct tcm_qla2xxx_lport *)target_lport_ptr;
1610 * Setup tgt_ops, local pointer to vha and target_lport_ptr
1612 ha->tgt.tgt_ops = &tcm_qla2xxx_template;
1613 vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1614 lport->qla_vha = vha;
1619 static struct se_wwn *tcm_qla2xxx_make_lport(
1620 struct target_fabric_configfs *tf,
1621 struct config_group *group,
1624 struct tcm_qla2xxx_lport *lport;
1628 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1629 return ERR_PTR(-EINVAL);
1631 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1633 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1634 return ERR_PTR(-ENOMEM);
1636 lport->lport_wwpn = wwpn;
1637 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1639 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
1641 ret = tcm_qla2xxx_init_lport(lport);
1645 ret = qlt_lport_register(lport, wwpn, 0, 0,
1646 tcm_qla2xxx_lport_register_cb);
1650 return &lport->lport_wwn;
1652 vfree(lport->lport_loopid_map);
1653 btree_destroy32(&lport->lport_fcport_map);
1656 return ERR_PTR(ret);
1659 static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1661 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1662 struct tcm_qla2xxx_lport, lport_wwn);
1663 struct scsi_qla_host *vha = lport->qla_vha;
1664 struct se_node_acl *node;
1668 * Call into qla2x_target.c LLD logic to complete the
1669 * shutdown of struct qla_tgt after the call to
1670 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1672 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
1673 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1675 qlt_lport_deregister(vha);
1677 vfree(lport->lport_loopid_map);
1678 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1679 btree_remove32(&lport->lport_fcport_map, key);
1680 btree_destroy32(&lport->lport_fcport_map);
1684 static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
1685 void *target_lport_ptr,
1686 u64 npiv_wwpn, u64 npiv_wwnn)
1688 struct fc_vport *vport;
1689 struct Scsi_Host *sh = base_vha->host;
1690 struct scsi_qla_host *npiv_vha;
1691 struct tcm_qla2xxx_lport *lport =
1692 (struct tcm_qla2xxx_lport *)target_lport_ptr;
1693 struct tcm_qla2xxx_lport *base_lport =
1694 (struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
1695 struct fc_vport_identifiers vport_id;
1697 if (qla_ini_mode_enabled(base_vha)) {
1698 pr_err("qla2xxx base_vha not enabled for target mode\n");
1702 if (!base_lport || !base_lport->tpg_1 ||
1703 !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
1704 pr_err("qla2xxx base_lport or tpg_1 not available\n");
1708 memset(&vport_id, 0, sizeof(vport_id));
1709 vport_id.port_name = npiv_wwpn;
1710 vport_id.node_name = npiv_wwnn;
1711 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
1712 vport_id.vport_type = FC_PORTTYPE_NPIV;
1713 vport_id.disable = false;
1715 vport = fc_vport_create(sh, 0, &vport_id);
1717 pr_err("fc_vport_create failed for qla2xxx_npiv\n");
1721 * Setup local pointer to NPIV vhba + target_lport_ptr
1723 npiv_vha = (struct scsi_qla_host *)vport->dd_data;
1724 npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1725 lport->qla_vha = npiv_vha;
1726 scsi_host_get(npiv_vha->host);
1731 static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1732 struct target_fabric_configfs *tf,
1733 struct config_group *group,
1736 struct tcm_qla2xxx_lport *lport;
1737 u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
1741 snprintf(tmp, 128, "%s", name);
1743 p = strchr(tmp, '@');
1745 pr_err("Unable to locate NPIV '@' separator\n");
1746 return ERR_PTR(-EINVAL);
1750 if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
1751 return ERR_PTR(-EINVAL);
1753 if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
1754 &npiv_wwpn, &npiv_wwnn) < 0)
1755 return ERR_PTR(-EINVAL);
1757 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1759 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1760 return ERR_PTR(-ENOMEM);
1762 lport->lport_npiv_wwpn = npiv_wwpn;
1763 lport->lport_npiv_wwnn = npiv_wwnn;
1764 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
1766 ret = tcm_qla2xxx_init_lport(lport);
1770 ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
1771 tcm_qla2xxx_lport_register_npiv_cb);
1775 return &lport->lport_wwn;
1777 vfree(lport->lport_loopid_map);
1778 btree_destroy32(&lport->lport_fcport_map);
1781 return ERR_PTR(ret);
1784 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1786 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1787 struct tcm_qla2xxx_lport, lport_wwn);
1788 struct scsi_qla_host *npiv_vha = lport->qla_vha;
1789 struct qla_hw_data *ha = npiv_vha->hw;
1790 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1792 scsi_host_put(npiv_vha->host);
1794 * Notify libfc that we want to release the vha->fc_vport
1796 fc_vport_terminate(npiv_vha->fc_vport);
1797 scsi_host_put(base_vha->host);
1802 static ssize_t tcm_qla2xxx_wwn_version_show(struct config_item *item,
1805 return sprintf(page,
1806 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on %s\n",
1807 QLA2XXX_VERSION, utsname()->sysname,
1808 utsname()->machine, utsname()->release);
1811 CONFIGFS_ATTR_RO(tcm_qla2xxx_wwn_, version);
1813 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1814 &tcm_qla2xxx_wwn_attr_version,
1818 static const struct target_core_fabric_ops tcm_qla2xxx_ops = {
1819 .module = THIS_MODULE,
1820 .fabric_name = "qla2xxx",
1821 .node_acl_size = sizeof(struct tcm_qla2xxx_nacl),
1823 * XXX: Limit assumes single page per scatter-gather-list entry.
1824 * Current maximum is ~4.9 MB per se_cmd->t_data_sg with PAGE_SIZE=4096
1826 .max_data_sg_nents = 1200,
1827 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1828 .tpg_get_tag = tcm_qla2xxx_get_tag,
1829 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1830 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1831 .tpg_check_demo_mode_write_protect =
1832 tcm_qla2xxx_check_demo_write_protect,
1833 .tpg_check_prod_mode_write_protect =
1834 tcm_qla2xxx_check_prod_write_protect,
1835 .tpg_check_prot_fabric_only = tcm_qla2xxx_check_prot_fabric_only,
1836 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1837 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1838 .check_stop_free = tcm_qla2xxx_check_stop_free,
1839 .release_cmd = tcm_qla2xxx_release_cmd,
1840 .close_session = tcm_qla2xxx_close_session,
1841 .sess_get_index = tcm_qla2xxx_sess_get_index,
1842 .sess_get_initiator_sid = NULL,
1843 .write_pending = tcm_qla2xxx_write_pending,
1844 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1845 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1846 .queue_data_in = tcm_qla2xxx_queue_data_in,
1847 .queue_status = tcm_qla2xxx_queue_status,
1848 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1849 .aborted_task = tcm_qla2xxx_aborted_task,
1851 * Setup function pointers for generic logic in
1852 * target_core_fabric_configfs.c
1854 .fabric_make_wwn = tcm_qla2xxx_make_lport,
1855 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
1856 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
1857 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1858 .fabric_init_nodeacl = tcm_qla2xxx_init_nodeacl,
1860 .tfc_wwn_attrs = tcm_qla2xxx_wwn_attrs,
1861 .tfc_tpg_base_attrs = tcm_qla2xxx_tpg_attrs,
1862 .tfc_tpg_attrib_attrs = tcm_qla2xxx_tpg_attrib_attrs,
1865 static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1866 .module = THIS_MODULE,
1867 .fabric_name = "qla2xxx_npiv",
1868 .node_acl_size = sizeof(struct tcm_qla2xxx_nacl),
1869 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1870 .tpg_get_tag = tcm_qla2xxx_get_tag,
1871 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1872 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1873 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
1874 .tpg_check_prod_mode_write_protect =
1875 tcm_qla2xxx_check_prod_write_protect,
1876 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1877 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1878 .check_stop_free = tcm_qla2xxx_check_stop_free,
1879 .release_cmd = tcm_qla2xxx_release_cmd,
1880 .close_session = tcm_qla2xxx_close_session,
1881 .sess_get_index = tcm_qla2xxx_sess_get_index,
1882 .sess_get_initiator_sid = NULL,
1883 .write_pending = tcm_qla2xxx_write_pending,
1884 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1885 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1886 .queue_data_in = tcm_qla2xxx_queue_data_in,
1887 .queue_status = tcm_qla2xxx_queue_status,
1888 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1889 .aborted_task = tcm_qla2xxx_aborted_task,
1891 * Setup function pointers for generic logic in
1892 * target_core_fabric_configfs.c
1894 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
1895 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
1896 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
1897 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1898 .fabric_init_nodeacl = tcm_qla2xxx_init_nodeacl,
1900 .tfc_wwn_attrs = tcm_qla2xxx_wwn_attrs,
1901 .tfc_tpg_base_attrs = tcm_qla2xxx_npiv_tpg_attrs,
1904 static int tcm_qla2xxx_register_configfs(void)
1908 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on %s\n",
1909 QLA2XXX_VERSION, utsname()->sysname,
1910 utsname()->machine, utsname()->release);
1912 ret = target_register_template(&tcm_qla2xxx_ops);
1916 ret = target_register_template(&tcm_qla2xxx_npiv_ops);
1920 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1922 if (!tcm_qla2xxx_free_wq) {
1924 goto out_fabric_npiv;
1930 target_unregister_template(&tcm_qla2xxx_npiv_ops);
1932 target_unregister_template(&tcm_qla2xxx_ops);
1936 static void tcm_qla2xxx_deregister_configfs(void)
1938 destroy_workqueue(tcm_qla2xxx_free_wq);
1940 target_unregister_template(&tcm_qla2xxx_ops);
1941 target_unregister_template(&tcm_qla2xxx_npiv_ops);
1944 static int __init tcm_qla2xxx_init(void)
1948 ret = tcm_qla2xxx_register_configfs();
1955 static void __exit tcm_qla2xxx_exit(void)
1957 tcm_qla2xxx_deregister_configfs();
1960 MODULE_DESCRIPTION("TCM QLA24XX+ series NPIV enabled fabric driver");
1961 MODULE_LICENSE("GPL");
1962 module_init(tcm_qla2xxx_init);
1963 module_exit(tcm_qla2xxx_exit);