1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * This file contains tcm implementation using v4 configfs fabric infrastructure
4 * for QLogic target mode HBAs
6 * (c) Copyright 2010-2013 Datera, Inc.
8 * Author: Nicholas A. Bellinger <nab@daterainc.com>
10 * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
11 * the TCM_FC / Open-FCoE.org fabric module.
13 * Copyright (c) 2010 Cisco Systems, Inc
15 ****************************************************************************/
18 #include <linux/module.h>
19 #include <linux/utsname.h>
20 #include <linux/vmalloc.h>
21 #include <linux/list.h>
22 #include <linux/slab.h>
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/configfs.h>
26 #include <linux/ctype.h>
27 #include <asm/unaligned.h>
28 #include <scsi/scsi_host.h>
29 #include <target/target_core_base.h>
30 #include <target/target_core_fabric.h>
33 #include "qla_target.h"
34 #include "tcm_qla2xxx.h"
36 static struct workqueue_struct *tcm_qla2xxx_free_wq;
40 * If strict, we require lower-case hex and colon separators to be sure
41 * the name is the same as what would be generated by ft_format_wwn()
42 * so the name and wwn are mapped one-to-one.
44 static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
54 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
56 if (c == '\n' && cp[1] == '\0')
58 if (strict && pos++ == 2 && byte++ < 7) {
67 if (strict && byte != 8)
74 else if (isxdigit(c) && (islower(c) || !strict))
75 nibble = tolower(c) - 'a' + 10;
78 *wwn = (*wwn << 4) | nibble;
82 pr_debug("err %u len %zu pos %u byte %u\n",
83 err, cp - name, pos, byte);
87 static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
91 put_unaligned_be64(wwn, b);
92 return snprintf(buf, len,
93 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
94 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
98 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
100 static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
105 memset(wwn, 0, sizeof(wwn));
107 /* Validate and store the new name */
108 for (i = 0, j = 0; i < 16; i++) {
111 value = hex_to_bin(*ns++);
113 j = (j << 4) | value;
123 *nm = wwn_to_u64(wwn);
128 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
129 * store_fc_host_vport_create()
131 static int tcm_qla2xxx_npiv_parse_wwn(
137 unsigned int cnt = count;
143 /* count may include a LF at end of string */
144 if (name[cnt-1] == '\n' || name[cnt-1] == 0)
147 /* validate we have enough characters for WWPN */
148 if ((cnt != (16+1+16)) || (name[16] != ':'))
151 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
155 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
162 static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
164 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
165 struct tcm_qla2xxx_tpg, se_tpg);
166 struct tcm_qla2xxx_lport *lport = tpg->lport;
168 return lport->lport_naa_name;
171 static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
173 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
174 struct tcm_qla2xxx_tpg, se_tpg);
175 return tpg->lport_tpgt;
178 static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
180 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
181 struct tcm_qla2xxx_tpg, se_tpg);
183 return tpg->tpg_attrib.generate_node_acls;
186 static int tcm_qla2xxx_check_demo_mode_cache(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.cache_dynamic_acls;
194 static int tcm_qla2xxx_check_demo_write_protect(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.demo_mode_write_protect;
202 static int tcm_qla2xxx_check_prod_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.prod_mode_write_protect;
210 static int tcm_qla2xxx_check_demo_mode_login_only(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.demo_mode_login_only;
218 static int tcm_qla2xxx_check_prot_fabric_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.fabric_prot_type;
226 static u32 tcm_qla2xxx_tpg_get_inst_index(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->lport_tpgt;
234 static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
236 struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
237 struct qla_tgt_mgmt_cmd, free_work);
239 transport_generic_free_cmd(&mcmd->se_cmd, 0);
243 * Called from qla_target_template->free_mcmd(), and will call
244 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
245 * release callback. qla_hw_data->hardware_lock is expected to be held
247 static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
251 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
252 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
255 static void tcm_qla2xxx_complete_free(struct work_struct *work)
257 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
261 WARN_ON(cmd->trc_flags & TRC_CMD_FREE);
263 /* To do: protect all tgt_counters manipulations with proper locking. */
264 cmd->qpair->tgt_counters.qla_core_ret_sta_ctio++;
265 cmd->trc_flags |= TRC_CMD_FREE;
266 cmd->cmd_sent_to_fw = 0;
268 transport_generic_free_cmd(&cmd->se_cmd, 0);
271 static struct qla_tgt_cmd *tcm_qla2xxx_get_cmd(struct fc_port *sess)
273 struct se_session *se_sess = sess->se_sess;
274 struct qla_tgt_cmd *cmd;
277 tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
281 cmd = &((struct qla_tgt_cmd *)se_sess->sess_cmd_map)[tag];
282 memset(cmd, 0, sizeof(struct qla_tgt_cmd));
283 cmd->se_cmd.map_tag = tag;
284 cmd->se_cmd.map_cpu = cpu;
289 static void tcm_qla2xxx_rel_cmd(struct qla_tgt_cmd *cmd)
291 target_free_tag(cmd->sess->se_sess, &cmd->se_cmd);
295 * Called from qla_target_template->free_cmd(), and will call
296 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
297 * release callback. qla_hw_data->hardware_lock is expected to be held
299 static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
301 cmd->qpair->tgt_counters.core_qla_free_cmd++;
304 WARN_ON(cmd->trc_flags & TRC_CMD_DONE);
305 cmd->trc_flags |= TRC_CMD_DONE;
307 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
308 queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
312 * Called from struct target_core_fabric_ops->check_stop_free() context
314 static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
316 struct qla_tgt_cmd *cmd;
318 if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
319 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
320 cmd->trc_flags |= TRC_CMD_CHK_STOP;
323 return target_put_sess_cmd(se_cmd);
326 /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
327 * fabric descriptor @se_cmd command to release
329 static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
331 struct qla_tgt_cmd *cmd;
333 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
334 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
335 struct qla_tgt_mgmt_cmd, se_cmd);
339 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
341 if (WARN_ON(cmd->cmd_sent_to_fw))
347 static void tcm_qla2xxx_release_session(struct kref *kref)
349 struct fc_port *sess = container_of(kref,
350 struct fc_port, sess_kref);
352 qlt_unreg_sess(sess);
355 static void tcm_qla2xxx_put_sess(struct fc_port *sess)
360 kref_put(&sess->sess_kref, tcm_qla2xxx_release_session);
363 static void tcm_qla2xxx_close_session(struct se_session *se_sess)
365 struct fc_port *sess = se_sess->fabric_sess_ptr;
366 struct scsi_qla_host *vha;
372 spin_lock_irqsave(&vha->hw->tgt.sess_lock, flags);
373 target_sess_cmd_list_set_waiting(se_sess);
374 spin_unlock_irqrestore(&vha->hw->tgt.sess_lock, flags);
376 sess->explicit_logout = 1;
377 tcm_qla2xxx_put_sess(sess);
380 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
385 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
387 struct qla_tgt_cmd *cmd = container_of(se_cmd,
388 struct qla_tgt_cmd, se_cmd);
391 /* Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
392 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
393 * already kick start the free.
395 pr_debug("write_pending aborted cmd[%p] refcount %d "
396 "transport_state %x, t_state %x, se_cmd_flags %x\n",
397 cmd, kref_read(&cmd->se_cmd.cmd_kref),
398 cmd->se_cmd.transport_state,
400 cmd->se_cmd.se_cmd_flags);
401 transport_generic_request_failure(&cmd->se_cmd,
402 TCM_CHECK_CONDITION_ABORT_CMD);
405 cmd->trc_flags |= TRC_XFR_RDY;
406 cmd->bufflen = se_cmd->data_length;
407 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
409 cmd->sg_cnt = se_cmd->t_data_nents;
410 cmd->sg = se_cmd->t_data_sg;
412 cmd->prot_sg_cnt = se_cmd->t_prot_nents;
413 cmd->prot_sg = se_cmd->t_prot_sg;
414 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size;
418 * qla_target.c:qlt_rdy_to_xfer() will call dma_map_sg() to setup
419 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
421 return qlt_rdy_to_xfer(cmd);
424 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
429 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
431 if (!(se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)) {
432 struct qla_tgt_cmd *cmd = container_of(se_cmd,
433 struct qla_tgt_cmd, se_cmd);
441 * Called from process context in qla_target.c:qlt_do_work() code
443 static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
444 unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
445 int data_dir, int bidi)
447 struct se_cmd *se_cmd = &cmd->se_cmd;
448 struct se_session *se_sess;
449 struct fc_port *sess;
450 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
451 struct se_portal_group *se_tpg;
452 struct tcm_qla2xxx_tpg *tpg;
454 int flags = TARGET_SCF_ACK_KREF;
457 flags |= TARGET_SCF_BIDI_OP;
459 if (se_cmd->cpuid != WORK_CPU_UNBOUND)
460 flags |= TARGET_SCF_USE_CPUID;
464 pr_err("Unable to locate struct fc_port from qla_tgt_cmd\n");
468 se_sess = sess->se_sess;
470 pr_err("Unable to locate active struct se_session\n");
474 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
475 se_tpg = se_sess->se_tpg;
476 tpg = container_of(se_tpg, struct tcm_qla2xxx_tpg, se_tpg);
477 if (unlikely(tpg->tpg_attrib.jam_host)) {
478 /* return, and dont run target_submit_cmd,discarding command */
483 cmd->qpair->tgt_counters.qla_core_sbt_cmd++;
484 return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
485 cmd->unpacked_lun, data_length, fcp_task_attr,
489 static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
491 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
494 * Ensure that the complete FCP WRITE payload has been received.
495 * Otherwise return an exception via CHECK_CONDITION status.
498 cmd->cmd_sent_to_fw = 0;
500 transport_generic_request_failure(&cmd->se_cmd,
501 TCM_CHECK_CONDITION_ABORT_CMD);
505 cmd->qpair->tgt_counters.qla_core_ret_ctio++;
506 if (!cmd->write_data_transferred) {
507 switch (cmd->dif_err_code) {
510 TCM_LOGICAL_BLOCK_GUARD_CHECK_FAILED;
514 TCM_LOGICAL_BLOCK_REF_TAG_CHECK_FAILED;
518 TCM_LOGICAL_BLOCK_APP_TAG_CHECK_FAILED;
525 if (cmd->se_cmd.pi_err)
526 transport_generic_request_failure(&cmd->se_cmd,
529 transport_generic_request_failure(&cmd->se_cmd,
530 TCM_CHECK_CONDITION_ABORT_CMD);
535 return target_execute_cmd(&cmd->se_cmd);
539 * Called from qla_target.c:qlt_do_ctio_completion()
541 static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
543 cmd->trc_flags |= TRC_DATA_IN;
545 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
546 queue_work_on(smp_processor_id(), tcm_qla2xxx_free_wq, &cmd->work);
549 static int tcm_qla2xxx_chk_dif_tags(uint32_t tag)
554 static int tcm_qla2xxx_dif_tags(struct qla_tgt_cmd *cmd,
555 uint16_t *pfw_prot_opts)
557 struct se_cmd *se_cmd = &cmd->se_cmd;
559 if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_GUARD))
560 *pfw_prot_opts |= PO_DISABLE_GUARD_CHECK;
562 if (!(se_cmd->prot_checks & TARGET_DIF_CHECK_APPTAG))
563 *pfw_prot_opts |= PO_DIS_APP_TAG_VALD;
569 * Called from qla_target.c:qlt_issue_task_mgmt()
571 static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, u64 lun,
572 uint16_t tmr_func, uint32_t tag)
574 struct fc_port *sess = mcmd->sess;
575 struct se_cmd *se_cmd = &mcmd->se_cmd;
576 int transl_tmr_func = 0;
577 int flags = TARGET_SCF_ACK_KREF;
581 pr_debug("%ld: ABTS received\n", sess->vha->host_no);
582 transl_tmr_func = TMR_ABORT_TASK;
583 flags |= TARGET_SCF_LOOKUP_LUN_FROM_TAG;
585 case QLA_TGT_2G_ABORT_TASK:
586 pr_debug("%ld: 2G Abort Task received\n", sess->vha->host_no);
587 transl_tmr_func = TMR_ABORT_TASK;
589 case QLA_TGT_CLEAR_ACA:
590 pr_debug("%ld: CLEAR_ACA received\n", sess->vha->host_no);
591 transl_tmr_func = TMR_CLEAR_ACA;
593 case QLA_TGT_TARGET_RESET:
594 pr_debug("%ld: TARGET_RESET received\n", sess->vha->host_no);
595 transl_tmr_func = TMR_TARGET_WARM_RESET;
597 case QLA_TGT_LUN_RESET:
598 pr_debug("%ld: LUN_RESET received\n", sess->vha->host_no);
599 transl_tmr_func = TMR_LUN_RESET;
601 case QLA_TGT_CLEAR_TS:
602 pr_debug("%ld: CLEAR_TS received\n", sess->vha->host_no);
603 transl_tmr_func = TMR_CLEAR_TASK_SET;
605 case QLA_TGT_ABORT_TS:
606 pr_debug("%ld: ABORT_TS received\n", sess->vha->host_no);
607 transl_tmr_func = TMR_ABORT_TASK_SET;
610 pr_debug("%ld: Unknown task mgmt fn 0x%x\n",
611 sess->vha->host_no, tmr_func);
615 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
616 transl_tmr_func, GFP_ATOMIC, tag, flags);
619 static struct qla_tgt_cmd *tcm_qla2xxx_find_cmd_by_tag(struct fc_port *sess,
622 struct qla_tgt_cmd *cmd = NULL;
623 struct se_cmd *secmd;
629 spin_lock_irqsave(&sess->se_sess->sess_cmd_lock, flags);
630 list_for_each_entry(secmd, &sess->se_sess->sess_cmd_list, se_cmd_list) {
631 /* skip task management functions, including tmr->task_cmd */
632 if (secmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
635 if (secmd->tag == tag) {
636 cmd = container_of(secmd, struct qla_tgt_cmd, se_cmd);
640 spin_unlock_irqrestore(&sess->se_sess->sess_cmd_lock, flags);
645 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
647 struct qla_tgt_cmd *cmd = container_of(se_cmd,
648 struct qla_tgt_cmd, se_cmd);
651 /* Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
652 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
653 * already kick start the free.
655 pr_debug("queue_data_in aborted cmd[%p] refcount %d "
656 "transport_state %x, t_state %x, se_cmd_flags %x\n",
657 cmd, kref_read(&cmd->se_cmd.cmd_kref),
658 cmd->se_cmd.transport_state,
660 cmd->se_cmd.se_cmd_flags);
664 cmd->trc_flags |= TRC_XMIT_DATA;
665 cmd->bufflen = se_cmd->data_length;
666 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
668 cmd->sg_cnt = se_cmd->t_data_nents;
669 cmd->sg = se_cmd->t_data_sg;
672 cmd->prot_sg_cnt = se_cmd->t_prot_nents;
673 cmd->prot_sg = se_cmd->t_prot_sg;
674 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size;
678 * Now queue completed DATA_IN the qla2xxx LLD and response ring
680 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
681 se_cmd->scsi_status);
684 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
686 struct qla_tgt_cmd *cmd = container_of(se_cmd,
687 struct qla_tgt_cmd, se_cmd);
688 int xmit_type = QLA_TGT_XMIT_STATUS;
692 * Cmd can loop during Q-full. tcm_qla2xxx_aborted_task
693 * can get ahead of this cmd. tcm_qla2xxx_aborted_task
694 * already kick start the free.
697 "queue_data_in aborted cmd[%p] refcount %d transport_state %x, t_state %x, se_cmd_flags %x\n",
698 cmd, kref_read(&cmd->se_cmd.cmd_kref),
699 cmd->se_cmd.transport_state, cmd->se_cmd.t_state,
700 cmd->se_cmd.se_cmd_flags);
703 cmd->bufflen = se_cmd->data_length;
707 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
708 cmd->trc_flags |= TRC_XMIT_STATUS;
710 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
712 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
713 * for qla_tgt_xmit_response LLD code
715 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
716 se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
717 se_cmd->residual_count = 0;
719 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
720 se_cmd->residual_count += se_cmd->data_length;
725 * Now queue status response to qla2xxx LLD code and response ring
727 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
730 static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
732 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
733 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
734 struct qla_tgt_mgmt_cmd, se_cmd);
736 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
737 mcmd, se_tmr->function, se_tmr->response);
739 * Do translation between TCM TM response codes and
740 * QLA2xxx FC TM response codes.
742 switch (se_tmr->response) {
743 case TMR_FUNCTION_COMPLETE:
744 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
746 case TMR_TASK_DOES_NOT_EXIST:
747 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
749 case TMR_FUNCTION_REJECTED:
750 mcmd->fc_tm_rsp = FC_TM_REJECT;
752 case TMR_LUN_DOES_NOT_EXIST:
754 mcmd->fc_tm_rsp = FC_TM_FAILED;
758 * Queue the TM response to QLA2xxx LLD to build a
759 * CTIO response packet.
761 qlt_xmit_tm_rsp(mcmd);
764 static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
766 struct qla_tgt_cmd *cmd = container_of(se_cmd,
767 struct qla_tgt_cmd, se_cmd);
769 if (qlt_abort_cmd(cmd))
773 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
774 struct tcm_qla2xxx_nacl *, struct fc_port *);
776 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
778 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct fc_port *sess)
780 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
781 struct se_portal_group *se_tpg = se_nacl->se_tpg;
782 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
783 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
784 struct tcm_qla2xxx_lport, lport_wwn);
785 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
786 struct tcm_qla2xxx_nacl, se_node_acl);
789 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
791 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
792 if (WARN_ON(node && (node != se_nacl))) {
794 * The nacl no longer matches what we think it should be.
795 * Most likely a new dynamic acl has been added while
796 * someone dropped the hardware lock. It clearly is a
797 * bug elsewhere, but this bit can't make things worse.
799 btree_insert32(&lport->lport_fcport_map, nacl->nport_id,
803 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
804 se_nacl, nacl->nport_wwnn, nacl->nport_id);
806 * Now clear the se_nacl and session pointers from our HW lport lookup
807 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
809 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
810 * target_wait_for_sess_cmds() before the session waits for outstanding
811 * I/O to complete, to avoid a race between session shutdown execution
812 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
814 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
817 static void tcm_qla2xxx_shutdown_sess(struct fc_port *sess)
819 target_sess_cmd_list_set_waiting(sess->se_sess);
822 static int tcm_qla2xxx_init_nodeacl(struct se_node_acl *se_nacl,
825 struct tcm_qla2xxx_nacl *nacl =
826 container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
829 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
832 nacl->nport_wwnn = wwnn;
833 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
838 /* Start items for tcm_qla2xxx_tpg_attrib_cit */
840 #define DEF_QLA_TPG_ATTRIB(name) \
842 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_show( \
843 struct config_item *item, char *page) \
845 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
846 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
847 struct tcm_qla2xxx_tpg, se_tpg); \
849 return sprintf(page, "%d\n", tpg->tpg_attrib.name); \
852 static ssize_t tcm_qla2xxx_tpg_attrib_##name##_store( \
853 struct config_item *item, const char *page, size_t count) \
855 struct se_portal_group *se_tpg = attrib_to_tpg(item); \
856 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
857 struct tcm_qla2xxx_tpg, se_tpg); \
858 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
862 ret = kstrtoul(page, 0, &val); \
864 pr_err("kstrtoul() failed with" \
865 " ret: %d\n", ret); \
869 if ((val != 0) && (val != 1)) { \
870 pr_err("Illegal boolean value %lu\n", val); \
878 CONFIGFS_ATTR(tcm_qla2xxx_tpg_attrib_, name)
880 DEF_QLA_TPG_ATTRIB(generate_node_acls);
881 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
882 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
883 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
884 DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
885 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
886 DEF_QLA_TPG_ATTRIB(jam_host);
889 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
890 &tcm_qla2xxx_tpg_attrib_attr_generate_node_acls,
891 &tcm_qla2xxx_tpg_attrib_attr_cache_dynamic_acls,
892 &tcm_qla2xxx_tpg_attrib_attr_demo_mode_write_protect,
893 &tcm_qla2xxx_tpg_attrib_attr_prod_mode_write_protect,
894 &tcm_qla2xxx_tpg_attrib_attr_demo_mode_login_only,
895 #ifdef CONFIG_TCM_QLA2XXX_DEBUG
896 &tcm_qla2xxx_tpg_attrib_attr_jam_host,
901 /* End items for tcm_qla2xxx_tpg_attrib_cit */
903 static ssize_t tcm_qla2xxx_tpg_enable_show(struct config_item *item,
906 struct se_portal_group *se_tpg = to_tpg(item);
907 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
908 struct tcm_qla2xxx_tpg, se_tpg);
910 return snprintf(page, PAGE_SIZE, "%d\n",
911 atomic_read(&tpg->lport_tpg_enabled));
914 static ssize_t tcm_qla2xxx_tpg_enable_store(struct config_item *item,
915 const char *page, size_t count)
917 struct se_portal_group *se_tpg = to_tpg(item);
918 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
919 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
920 struct tcm_qla2xxx_lport, lport_wwn);
921 struct scsi_qla_host *vha = lport->qla_vha;
922 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
923 struct tcm_qla2xxx_tpg, se_tpg);
927 rc = kstrtoul(page, 0, &op);
929 pr_err("kstrtoul() returned %d\n", rc);
932 if ((op != 1) && (op != 0)) {
933 pr_err("Illegal value for tpg_enable: %lu\n", op);
937 if (atomic_read(&tpg->lport_tpg_enabled))
940 atomic_set(&tpg->lport_tpg_enabled, 1);
943 if (!atomic_read(&tpg->lport_tpg_enabled))
946 atomic_set(&tpg->lport_tpg_enabled, 0);
947 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
948 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
954 static ssize_t tcm_qla2xxx_tpg_dynamic_sessions_show(struct config_item *item,
957 return target_show_dynamic_sessions(to_tpg(item), page);
960 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_store(struct config_item *item,
961 const char *page, size_t count)
963 struct se_portal_group *se_tpg = to_tpg(item);
964 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
965 struct tcm_qla2xxx_tpg, se_tpg);
967 int ret = kstrtoul(page, 0, &val);
970 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
973 if (val != 0 && val != 1 && val != 3) {
974 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
977 tpg->tpg_attrib.fabric_prot_type = val;
982 static ssize_t tcm_qla2xxx_tpg_fabric_prot_type_show(struct config_item *item,
985 struct se_portal_group *se_tpg = to_tpg(item);
986 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
987 struct tcm_qla2xxx_tpg, se_tpg);
989 return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
992 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, enable);
993 CONFIGFS_ATTR_RO(tcm_qla2xxx_tpg_, dynamic_sessions);
994 CONFIGFS_ATTR(tcm_qla2xxx_tpg_, fabric_prot_type);
996 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
997 &tcm_qla2xxx_tpg_attr_enable,
998 &tcm_qla2xxx_tpg_attr_dynamic_sessions,
999 &tcm_qla2xxx_tpg_attr_fabric_prot_type,
1003 static struct se_portal_group *tcm_qla2xxx_make_tpg(struct se_wwn *wwn,
1006 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1007 struct tcm_qla2xxx_lport, lport_wwn);
1008 struct tcm_qla2xxx_tpg *tpg;
1012 if (strstr(name, "tpgt_") != name)
1013 return ERR_PTR(-EINVAL);
1014 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1015 return ERR_PTR(-EINVAL);
1018 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1019 return ERR_PTR(-ENOSYS);
1022 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1024 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1025 return ERR_PTR(-ENOMEM);
1028 tpg->lport_tpgt = tpgt;
1030 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1033 tpg->tpg_attrib.generate_node_acls = 1;
1034 tpg->tpg_attrib.demo_mode_write_protect = 1;
1035 tpg->tpg_attrib.cache_dynamic_acls = 1;
1036 tpg->tpg_attrib.demo_mode_login_only = 1;
1037 tpg->tpg_attrib.jam_host = 0;
1039 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1047 return &tpg->se_tpg;
1050 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1052 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1053 struct tcm_qla2xxx_tpg, se_tpg);
1054 struct tcm_qla2xxx_lport *lport = tpg->lport;
1055 struct scsi_qla_host *vha = lport->qla_vha;
1057 * Call into qla2x_target.c LLD logic to shutdown the active
1058 * FC Nexuses and disable target mode operation for this qla_hw_data
1060 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
1061 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1063 core_tpg_deregister(se_tpg);
1065 * Clear local TPG=1 pointer for non NPIV mode.
1067 lport->tpg_1 = NULL;
1071 static ssize_t tcm_qla2xxx_npiv_tpg_enable_show(struct config_item *item,
1074 return tcm_qla2xxx_tpg_enable_show(item, page);
1077 static ssize_t tcm_qla2xxx_npiv_tpg_enable_store(struct config_item *item,
1078 const char *page, size_t count)
1080 struct se_portal_group *se_tpg = to_tpg(item);
1081 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1082 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1083 struct tcm_qla2xxx_lport, lport_wwn);
1084 struct scsi_qla_host *vha = lport->qla_vha;
1085 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1086 struct tcm_qla2xxx_tpg, se_tpg);
1090 rc = kstrtoul(page, 0, &op);
1092 pr_err("kstrtoul() returned %d\n", rc);
1095 if ((op != 1) && (op != 0)) {
1096 pr_err("Illegal value for tpg_enable: %lu\n", op);
1100 if (atomic_read(&tpg->lport_tpg_enabled))
1103 atomic_set(&tpg->lport_tpg_enabled, 1);
1104 qlt_enable_vha(vha);
1106 if (!atomic_read(&tpg->lport_tpg_enabled))
1109 atomic_set(&tpg->lport_tpg_enabled, 0);
1110 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1111 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1117 CONFIGFS_ATTR(tcm_qla2xxx_npiv_tpg_, enable);
1119 static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
1120 &tcm_qla2xxx_npiv_tpg_attr_enable,
1124 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(struct se_wwn *wwn,
1127 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1128 struct tcm_qla2xxx_lport, lport_wwn);
1129 struct tcm_qla2xxx_tpg *tpg;
1133 if (strstr(name, "tpgt_") != name)
1134 return ERR_PTR(-EINVAL);
1135 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1136 return ERR_PTR(-EINVAL);
1138 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1140 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1141 return ERR_PTR(-ENOMEM);
1144 tpg->lport_tpgt = tpgt;
1147 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1150 tpg->tpg_attrib.generate_node_acls = 1;
1151 tpg->tpg_attrib.demo_mode_write_protect = 1;
1152 tpg->tpg_attrib.cache_dynamic_acls = 1;
1153 tpg->tpg_attrib.demo_mode_login_only = 1;
1155 ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_FCP);
1161 return &tpg->se_tpg;
1165 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1167 static struct fc_port *tcm_qla2xxx_find_sess_by_s_id(scsi_qla_host_t *vha,
1170 struct tcm_qla2xxx_lport *lport;
1171 struct se_node_acl *se_nacl;
1172 struct tcm_qla2xxx_nacl *nacl;
1175 lport = vha->vha_tgt.target_lport_ptr;
1177 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1182 key = sid_to_key(s_id);
1183 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1185 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1187 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1190 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1191 se_nacl, se_nacl->initiatorname);
1193 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1194 if (!nacl->fc_port) {
1195 pr_err("Unable to locate struct fc_port\n");
1199 return nacl->fc_port;
1203 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1205 static void tcm_qla2xxx_set_sess_by_s_id(
1206 struct tcm_qla2xxx_lport *lport,
1207 struct se_node_acl *new_se_nacl,
1208 struct tcm_qla2xxx_nacl *nacl,
1209 struct se_session *se_sess,
1210 struct fc_port *fc_port,
1217 key = sid_to_key(s_id);
1218 pr_debug("set_sess_by_s_id: %06x\n", key);
1220 slot = btree_lookup32(&lport->lport_fcport_map, key);
1223 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1224 nacl->nport_id = key;
1225 rc = btree_insert32(&lport->lport_fcport_map, key,
1226 new_se_nacl, GFP_ATOMIC);
1228 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1231 pr_debug("Wiping nonexisting fc_port entry\n");
1234 fc_port->se_sess = se_sess;
1235 nacl->fc_port = fc_port;
1239 if (nacl->fc_port) {
1240 if (new_se_nacl == NULL) {
1241 pr_debug("Clearing existing nacl->fc_port and fc_port entry\n");
1242 btree_remove32(&lport->lport_fcport_map, key);
1243 nacl->fc_port = NULL;
1246 pr_debug("Replacing existing nacl->fc_port and fc_port entry\n");
1247 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1248 fc_port->se_sess = se_sess;
1249 nacl->fc_port = fc_port;
1253 if (new_se_nacl == NULL) {
1254 pr_debug("Clearing existing fc_port entry\n");
1255 btree_remove32(&lport->lport_fcport_map, key);
1259 pr_debug("Replacing existing fc_port entry w/o active nacl->fc_port\n");
1260 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1261 fc_port->se_sess = se_sess;
1262 nacl->fc_port = fc_port;
1264 pr_debug("Setup nacl->fc_port %p by s_id for se_nacl: %p, initiatorname: %s\n",
1265 nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1269 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1271 static struct fc_port *tcm_qla2xxx_find_sess_by_loop_id(
1272 scsi_qla_host_t *vha,
1273 const uint16_t loop_id)
1275 struct tcm_qla2xxx_lport *lport;
1276 struct se_node_acl *se_nacl;
1277 struct tcm_qla2xxx_nacl *nacl;
1278 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1280 lport = vha->vha_tgt.target_lport_ptr;
1282 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1287 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1289 fc_loopid = lport->lport_loopid_map + loop_id;
1290 se_nacl = fc_loopid->se_nacl;
1292 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1297 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1299 if (!nacl->fc_port) {
1300 pr_err("Unable to locate struct fc_port\n");
1304 return nacl->fc_port;
1308 * Expected to be called with struct qla_hw_data->tgt.sess_lock held
1310 static void tcm_qla2xxx_set_sess_by_loop_id(
1311 struct tcm_qla2xxx_lport *lport,
1312 struct se_node_acl *new_se_nacl,
1313 struct tcm_qla2xxx_nacl *nacl,
1314 struct se_session *se_sess,
1315 struct fc_port *fc_port,
1318 struct se_node_acl *saved_nacl;
1319 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1321 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1323 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1324 lport->lport_loopid_map)[loop_id];
1326 saved_nacl = fc_loopid->se_nacl;
1328 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1329 fc_loopid->se_nacl = new_se_nacl;
1330 if (fc_port->se_sess != se_sess)
1331 fc_port->se_sess = se_sess;
1332 if (nacl->fc_port != fc_port)
1333 nacl->fc_port = fc_port;
1337 if (nacl->fc_port) {
1338 if (new_se_nacl == NULL) {
1339 pr_debug("Clearing nacl->fc_port and fc_loopid->se_nacl\n");
1340 fc_loopid->se_nacl = NULL;
1341 nacl->fc_port = NULL;
1345 pr_debug("Replacing existing nacl->fc_port and fc_loopid->se_nacl\n");
1346 fc_loopid->se_nacl = new_se_nacl;
1347 if (fc_port->se_sess != se_sess)
1348 fc_port->se_sess = se_sess;
1349 if (nacl->fc_port != fc_port)
1350 nacl->fc_port = fc_port;
1354 if (new_se_nacl == NULL) {
1355 pr_debug("Clearing fc_loopid->se_nacl\n");
1356 fc_loopid->se_nacl = NULL;
1360 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->fc_port\n");
1361 fc_loopid->se_nacl = new_se_nacl;
1362 if (fc_port->se_sess != se_sess)
1363 fc_port->se_sess = se_sess;
1364 if (nacl->fc_port != fc_port)
1365 nacl->fc_port = fc_port;
1367 pr_debug("Setup nacl->fc_port %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1368 nacl->fc_port, new_se_nacl, new_se_nacl->initiatorname);
1372 * Should always be called with qla_hw_data->tgt.sess_lock held.
1374 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1375 struct tcm_qla2xxx_nacl *nacl, struct fc_port *sess)
1377 struct se_session *se_sess = sess->se_sess;
1379 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1380 sess, port_id_to_be_id(sess->d_id));
1381 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1382 sess, sess->loop_id);
1385 static void tcm_qla2xxx_free_session(struct fc_port *sess)
1387 struct qla_tgt *tgt = sess->tgt;
1388 struct qla_hw_data *ha = tgt->ha;
1389 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1390 struct se_session *se_sess;
1391 struct tcm_qla2xxx_lport *lport;
1393 BUG_ON(in_interrupt());
1395 se_sess = sess->se_sess;
1397 pr_err("struct fc_port->se_sess is NULL\n");
1402 lport = vha->vha_tgt.target_lport_ptr;
1404 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1408 target_wait_for_sess_cmds(se_sess);
1410 target_remove_session(se_sess);
1413 static int tcm_qla2xxx_session_cb(struct se_portal_group *se_tpg,
1414 struct se_session *se_sess, void *p)
1416 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1417 struct tcm_qla2xxx_tpg, se_tpg);
1418 struct tcm_qla2xxx_lport *lport = tpg->lport;
1419 struct qla_hw_data *ha = lport->qla_vha->hw;
1420 struct se_node_acl *se_nacl = se_sess->se_node_acl;
1421 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1422 struct tcm_qla2xxx_nacl, se_node_acl);
1423 struct fc_port *qlat_sess = p;
1424 uint16_t loop_id = qlat_sess->loop_id;
1425 unsigned long flags;
1428 * And now setup se_nacl and session pointers into HW lport internal
1429 * mappings for fabric S_ID and LOOP_ID.
1431 spin_lock_irqsave(&ha->tgt.sess_lock, flags);
1432 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess, qlat_sess,
1433 port_id_to_be_id(qlat_sess->d_id));
1434 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl,
1435 se_sess, qlat_sess, loop_id);
1436 spin_unlock_irqrestore(&ha->tgt.sess_lock, flags);
1442 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1443 * to locate struct se_node_acl
1445 static int tcm_qla2xxx_check_initiator_node_acl(
1446 scsi_qla_host_t *vha,
1447 unsigned char *fc_wwpn,
1448 struct fc_port *qlat_sess)
1450 struct qla_hw_data *ha = vha->hw;
1451 struct tcm_qla2xxx_lport *lport;
1452 struct tcm_qla2xxx_tpg *tpg;
1453 struct se_session *se_sess;
1454 unsigned char port_name[36];
1455 int num_tags = (ha->cur_fw_xcb_count) ? ha->cur_fw_xcb_count :
1456 TCM_QLA2XXX_DEFAULT_TAGS;
1458 lport = vha->vha_tgt.target_lport_ptr;
1460 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1465 * Locate the TPG=1 reference..
1469 pr_err("Unable to locate struct tcm_qla2xxx_lport->tpg_1\n");
1473 * Format the FCP Initiator port_name into colon seperated values to
1474 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1476 memset(&port_name, 0, 36);
1477 snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn);
1479 * Locate our struct se_node_acl either from an explict NodeACL created
1480 * via ConfigFS, or via running in TPG demo mode.
1482 se_sess = target_setup_session(&tpg->se_tpg, num_tags,
1483 sizeof(struct qla_tgt_cmd),
1484 TARGET_PROT_ALL, port_name,
1485 qlat_sess, tcm_qla2xxx_session_cb);
1486 if (IS_ERR(se_sess))
1487 return PTR_ERR(se_sess);
1492 static void tcm_qla2xxx_update_sess(struct fc_port *sess, port_id_t s_id,
1493 uint16_t loop_id, bool conf_compl_supported)
1495 struct qla_tgt *tgt = sess->tgt;
1496 struct qla_hw_data *ha = tgt->ha;
1497 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1498 struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
1499 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1500 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1501 struct tcm_qla2xxx_nacl, se_node_acl);
1505 if (sess->loop_id != loop_id || sess->d_id.b24 != s_id.b24)
1506 pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1507 sess, sess->port_name,
1508 sess->loop_id, loop_id, sess->d_id.b.domain,
1509 sess->d_id.b.area, sess->d_id.b.al_pa, s_id.b.domain,
1510 s_id.b.area, s_id.b.al_pa);
1512 if (sess->loop_id != loop_id) {
1514 * Because we can shuffle loop IDs around and we
1515 * update different sessions non-atomically, we might
1516 * have overwritten this session's old loop ID
1517 * already, and we might end up overwriting some other
1518 * session that will be updated later. So we have to
1519 * be extra careful and we can't warn about those things...
1521 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1522 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1524 lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1526 sess->loop_id = loop_id;
1529 if (sess->d_id.b24 != s_id.b24) {
1530 key = (((u32) sess->d_id.b.domain << 16) |
1531 ((u32) sess->d_id.b.area << 8) |
1532 ((u32) sess->d_id.b.al_pa));
1534 if (btree_lookup32(&lport->lport_fcport_map, key))
1535 WARN(btree_remove32(&lport->lport_fcport_map, key) !=
1536 se_nacl, "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1537 sess->d_id.b.domain, sess->d_id.b.area,
1538 sess->d_id.b.al_pa);
1540 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1541 sess->d_id.b.domain, sess->d_id.b.area,
1542 sess->d_id.b.al_pa);
1544 key = (((u32) s_id.b.domain << 16) |
1545 ((u32) s_id.b.area << 8) |
1546 ((u32) s_id.b.al_pa));
1548 if (btree_lookup32(&lport->lport_fcport_map, key)) {
1549 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1550 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1551 btree_update32(&lport->lport_fcport_map, key, se_nacl);
1553 btree_insert32(&lport->lport_fcport_map, key, se_nacl,
1558 nacl->nport_id = key;
1561 sess->conf_compl_supported = conf_compl_supported;
1566 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1568 static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1569 .find_cmd_by_tag = tcm_qla2xxx_find_cmd_by_tag,
1570 .handle_cmd = tcm_qla2xxx_handle_cmd,
1571 .handle_data = tcm_qla2xxx_handle_data,
1572 .handle_tmr = tcm_qla2xxx_handle_tmr,
1573 .get_cmd = tcm_qla2xxx_get_cmd,
1574 .rel_cmd = tcm_qla2xxx_rel_cmd,
1575 .free_cmd = tcm_qla2xxx_free_cmd,
1576 .free_mcmd = tcm_qla2xxx_free_mcmd,
1577 .free_session = tcm_qla2xxx_free_session,
1578 .update_sess = tcm_qla2xxx_update_sess,
1579 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1580 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1581 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1582 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1583 .put_sess = tcm_qla2xxx_put_sess,
1584 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1585 .get_dif_tags = tcm_qla2xxx_dif_tags,
1586 .chk_dif_tags = tcm_qla2xxx_chk_dif_tags,
1589 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1593 rc = btree_init32(&lport->lport_fcport_map);
1595 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1599 lport->lport_loopid_map =
1600 vzalloc(array_size(65536,
1601 sizeof(struct tcm_qla2xxx_fc_loopid)));
1602 if (!lport->lport_loopid_map) {
1603 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1604 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1605 btree_destroy32(&lport->lport_fcport_map);
1608 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1609 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1613 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
1614 void *target_lport_ptr,
1615 u64 npiv_wwpn, u64 npiv_wwnn)
1617 struct qla_hw_data *ha = vha->hw;
1618 struct tcm_qla2xxx_lport *lport =
1619 (struct tcm_qla2xxx_lport *)target_lport_ptr;
1621 * Setup tgt_ops, local pointer to vha and target_lport_ptr
1623 ha->tgt.tgt_ops = &tcm_qla2xxx_template;
1624 vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1625 lport->qla_vha = vha;
1630 static struct se_wwn *tcm_qla2xxx_make_lport(
1631 struct target_fabric_configfs *tf,
1632 struct config_group *group,
1635 struct tcm_qla2xxx_lport *lport;
1639 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1640 return ERR_PTR(-EINVAL);
1642 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1644 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1645 return ERR_PTR(-ENOMEM);
1647 lport->lport_wwpn = wwpn;
1648 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1650 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
1652 ret = tcm_qla2xxx_init_lport(lport);
1656 ret = qlt_lport_register(lport, wwpn, 0, 0,
1657 tcm_qla2xxx_lport_register_cb);
1661 return &lport->lport_wwn;
1663 vfree(lport->lport_loopid_map);
1664 btree_destroy32(&lport->lport_fcport_map);
1667 return ERR_PTR(ret);
1670 static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1672 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1673 struct tcm_qla2xxx_lport, lport_wwn);
1674 struct scsi_qla_host *vha = lport->qla_vha;
1675 struct se_node_acl *node;
1679 * Call into qla2x_target.c LLD logic to complete the
1680 * shutdown of struct qla_tgt after the call to
1681 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1683 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
1684 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
1686 qlt_lport_deregister(vha);
1688 vfree(lport->lport_loopid_map);
1689 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1690 btree_remove32(&lport->lport_fcport_map, key);
1691 btree_destroy32(&lport->lport_fcport_map);
1695 static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
1696 void *target_lport_ptr,
1697 u64 npiv_wwpn, u64 npiv_wwnn)
1699 struct fc_vport *vport;
1700 struct Scsi_Host *sh = base_vha->host;
1701 struct scsi_qla_host *npiv_vha;
1702 struct tcm_qla2xxx_lport *lport =
1703 (struct tcm_qla2xxx_lport *)target_lport_ptr;
1704 struct tcm_qla2xxx_lport *base_lport =
1705 (struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
1706 struct fc_vport_identifiers vport_id;
1708 if (qla_ini_mode_enabled(base_vha)) {
1709 pr_err("qla2xxx base_vha not enabled for target mode\n");
1713 if (!base_lport || !base_lport->tpg_1 ||
1714 !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
1715 pr_err("qla2xxx base_lport or tpg_1 not available\n");
1719 memset(&vport_id, 0, sizeof(vport_id));
1720 vport_id.port_name = npiv_wwpn;
1721 vport_id.node_name = npiv_wwnn;
1722 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
1723 vport_id.vport_type = FC_PORTTYPE_NPIV;
1724 vport_id.disable = false;
1726 vport = fc_vport_create(sh, 0, &vport_id);
1728 pr_err("fc_vport_create failed for qla2xxx_npiv\n");
1732 * Setup local pointer to NPIV vhba + target_lport_ptr
1734 npiv_vha = (struct scsi_qla_host *)vport->dd_data;
1735 npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1736 lport->qla_vha = npiv_vha;
1737 scsi_host_get(npiv_vha->host);
1742 static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1743 struct target_fabric_configfs *tf,
1744 struct config_group *group,
1747 struct tcm_qla2xxx_lport *lport;
1748 u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
1752 snprintf(tmp, 128, "%s", name);
1754 p = strchr(tmp, '@');
1756 pr_err("Unable to locate NPIV '@' separator\n");
1757 return ERR_PTR(-EINVAL);
1761 if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
1762 return ERR_PTR(-EINVAL);
1764 if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
1765 &npiv_wwpn, &npiv_wwnn) < 0)
1766 return ERR_PTR(-EINVAL);
1768 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1770 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1771 return ERR_PTR(-ENOMEM);
1773 lport->lport_npiv_wwpn = npiv_wwpn;
1774 lport->lport_npiv_wwnn = npiv_wwnn;
1775 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
1777 ret = tcm_qla2xxx_init_lport(lport);
1781 ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
1782 tcm_qla2xxx_lport_register_npiv_cb);
1786 return &lport->lport_wwn;
1788 vfree(lport->lport_loopid_map);
1789 btree_destroy32(&lport->lport_fcport_map);
1792 return ERR_PTR(ret);
1795 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1797 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1798 struct tcm_qla2xxx_lport, lport_wwn);
1799 struct scsi_qla_host *npiv_vha = lport->qla_vha;
1800 struct qla_hw_data *ha = npiv_vha->hw;
1801 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1803 scsi_host_put(npiv_vha->host);
1805 * Notify libfc that we want to release the vha->fc_vport
1807 fc_vport_terminate(npiv_vha->fc_vport);
1808 scsi_host_put(base_vha->host);
1813 static ssize_t tcm_qla2xxx_wwn_version_show(struct config_item *item,
1816 return sprintf(page,
1817 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on %s\n",
1818 QLA2XXX_VERSION, utsname()->sysname,
1819 utsname()->machine, utsname()->release);
1822 CONFIGFS_ATTR_RO(tcm_qla2xxx_wwn_, version);
1824 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1825 &tcm_qla2xxx_wwn_attr_version,
1829 static const struct target_core_fabric_ops tcm_qla2xxx_ops = {
1830 .module = THIS_MODULE,
1831 .fabric_name = "qla2xxx",
1832 .node_acl_size = sizeof(struct tcm_qla2xxx_nacl),
1834 * XXX: Limit assumes single page per scatter-gather-list entry.
1835 * Current maximum is ~4.9 MB per se_cmd->t_data_sg with PAGE_SIZE=4096
1837 .max_data_sg_nents = 1200,
1838 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1839 .tpg_get_tag = tcm_qla2xxx_get_tag,
1840 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1841 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1842 .tpg_check_demo_mode_write_protect =
1843 tcm_qla2xxx_check_demo_write_protect,
1844 .tpg_check_prod_mode_write_protect =
1845 tcm_qla2xxx_check_prod_write_protect,
1846 .tpg_check_prot_fabric_only = tcm_qla2xxx_check_prot_fabric_only,
1847 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1848 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1849 .check_stop_free = tcm_qla2xxx_check_stop_free,
1850 .release_cmd = tcm_qla2xxx_release_cmd,
1851 .close_session = tcm_qla2xxx_close_session,
1852 .sess_get_index = tcm_qla2xxx_sess_get_index,
1853 .sess_get_initiator_sid = NULL,
1854 .write_pending = tcm_qla2xxx_write_pending,
1855 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1856 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1857 .queue_data_in = tcm_qla2xxx_queue_data_in,
1858 .queue_status = tcm_qla2xxx_queue_status,
1859 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1860 .aborted_task = tcm_qla2xxx_aborted_task,
1862 * Setup function pointers for generic logic in
1863 * target_core_fabric_configfs.c
1865 .fabric_make_wwn = tcm_qla2xxx_make_lport,
1866 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
1867 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
1868 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1869 .fabric_init_nodeacl = tcm_qla2xxx_init_nodeacl,
1871 .tfc_wwn_attrs = tcm_qla2xxx_wwn_attrs,
1872 .tfc_tpg_base_attrs = tcm_qla2xxx_tpg_attrs,
1873 .tfc_tpg_attrib_attrs = tcm_qla2xxx_tpg_attrib_attrs,
1876 static const struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1877 .module = THIS_MODULE,
1878 .fabric_name = "qla2xxx_npiv",
1879 .node_acl_size = sizeof(struct tcm_qla2xxx_nacl),
1880 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1881 .tpg_get_tag = tcm_qla2xxx_get_tag,
1882 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1883 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1884 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
1885 .tpg_check_prod_mode_write_protect =
1886 tcm_qla2xxx_check_prod_write_protect,
1887 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
1888 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1889 .check_stop_free = tcm_qla2xxx_check_stop_free,
1890 .release_cmd = tcm_qla2xxx_release_cmd,
1891 .close_session = tcm_qla2xxx_close_session,
1892 .sess_get_index = tcm_qla2xxx_sess_get_index,
1893 .sess_get_initiator_sid = NULL,
1894 .write_pending = tcm_qla2xxx_write_pending,
1895 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1896 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1897 .queue_data_in = tcm_qla2xxx_queue_data_in,
1898 .queue_status = tcm_qla2xxx_queue_status,
1899 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1900 .aborted_task = tcm_qla2xxx_aborted_task,
1902 * Setup function pointers for generic logic in
1903 * target_core_fabric_configfs.c
1905 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
1906 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
1907 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
1908 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1909 .fabric_init_nodeacl = tcm_qla2xxx_init_nodeacl,
1911 .tfc_wwn_attrs = tcm_qla2xxx_wwn_attrs,
1912 .tfc_tpg_base_attrs = tcm_qla2xxx_npiv_tpg_attrs,
1915 static int tcm_qla2xxx_register_configfs(void)
1919 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on %s\n",
1920 QLA2XXX_VERSION, utsname()->sysname,
1921 utsname()->machine, utsname()->release);
1923 ret = target_register_template(&tcm_qla2xxx_ops);
1927 ret = target_register_template(&tcm_qla2xxx_npiv_ops);
1931 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1933 if (!tcm_qla2xxx_free_wq) {
1935 goto out_fabric_npiv;
1941 target_unregister_template(&tcm_qla2xxx_npiv_ops);
1943 target_unregister_template(&tcm_qla2xxx_ops);
1947 static void tcm_qla2xxx_deregister_configfs(void)
1949 destroy_workqueue(tcm_qla2xxx_free_wq);
1951 target_unregister_template(&tcm_qla2xxx_ops);
1952 target_unregister_template(&tcm_qla2xxx_npiv_ops);
1955 static int __init tcm_qla2xxx_init(void)
1959 BUILD_BUG_ON(sizeof(struct abts_recv_from_24xx) != 64);
1960 BUILD_BUG_ON(sizeof(struct abts_resp_from_24xx_fw) != 64);
1961 BUILD_BUG_ON(sizeof(struct atio7_fcp_cmnd) != 32);
1962 BUILD_BUG_ON(sizeof(struct atio_from_isp) != 64);
1963 BUILD_BUG_ON(sizeof(struct ba_acc_le) != 12);
1964 BUILD_BUG_ON(sizeof(struct ba_rjt_le) != 4);
1965 BUILD_BUG_ON(sizeof(struct ctio7_from_24xx) != 64);
1966 BUILD_BUG_ON(sizeof(struct ctio7_to_24xx) != 64);
1967 BUILD_BUG_ON(sizeof(struct ctio_crc2_to_fw) != 64);
1968 BUILD_BUG_ON(sizeof(struct ctio_crc_from_fw) != 64);
1969 BUILD_BUG_ON(sizeof(struct ctio_to_2xxx) != 64);
1970 BUILD_BUG_ON(sizeof(struct fcp_hdr) != 24);
1971 BUILD_BUG_ON(sizeof(struct fcp_hdr_le) != 24);
1972 BUILD_BUG_ON(sizeof(struct nack_to_isp) != 64);
1974 ret = tcm_qla2xxx_register_configfs();
1981 static void __exit tcm_qla2xxx_exit(void)
1983 tcm_qla2xxx_deregister_configfs();
1986 MODULE_DESCRIPTION("TCM QLA24XX+ series NPIV enabled fabric driver");
1987 MODULE_LICENSE("GPL");
1988 module_init(tcm_qla2xxx_init);
1989 module_exit(tcm_qla2xxx_exit);