target: pass sense_reason as a return value
[linux-2.6.git] / drivers / scsi / qla2xxx / tcm_qla2xxx.c
blobc5696240d8f66b2a859441716a48c1c13a59edbc
1 /*******************************************************************************
2 * This file contains tcm implementation using v4 configfs fabric infrastructure
3 * for QLogic target mode HBAs
5 * ?? Copyright 2010-2011 RisingTide Systems LLC.
7 * Licensed to the Linux Foundation under the General Public License (GPL)
8 * version 2.
10 * Author: Nicholas A. Bellinger <nab@risingtidesystems.com>
12 * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
13 * the TCM_FC / Open-FCoE.org fabric module.
15 * Copyright (c) 2010 Cisco Systems, Inc
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 ****************************************************************************/
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <generated/utsrelease.h>
32 #include <linux/utsname.h>
33 #include <linux/init.h>
34 #include <linux/list.h>
35 #include <linux/slab.h>
36 #include <linux/kthread.h>
37 #include <linux/types.h>
38 #include <linux/string.h>
39 #include <linux/configfs.h>
40 #include <linux/ctype.h>
41 #include <asm/unaligned.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_cmnd.h>
46 #include <target/target_core_base.h>
47 #include <target/target_core_fabric.h>
48 #include <target/target_core_fabric_configfs.h>
49 #include <target/target_core_configfs.h>
50 #include <target/configfs_macros.h>
52 #include "qla_def.h"
53 #include "qla_target.h"
54 #include "tcm_qla2xxx.h"
56 struct workqueue_struct *tcm_qla2xxx_free_wq;
57 struct workqueue_struct *tcm_qla2xxx_cmd_wq;
59 static int tcm_qla2xxx_check_true(struct se_portal_group *se_tpg)
61 return 1;
64 static int tcm_qla2xxx_check_false(struct se_portal_group *se_tpg)
66 return 0;
70 * Parse WWN.
71 * If strict, we require lower-case hex and colon separators to be sure
72 * the name is the same as what would be generated by ft_format_wwn()
73 * so the name and wwn are mapped one-to-one.
75 static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
77 const char *cp;
78 char c;
79 u32 nibble;
80 u32 byte = 0;
81 u32 pos = 0;
82 u32 err;
84 *wwn = 0;
85 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
86 c = *cp;
87 if (c == '\n' && cp[1] == '\0')
88 continue;
89 if (strict && pos++ == 2 && byte++ < 7) {
90 pos = 0;
91 if (c == ':')
92 continue;
93 err = 1;
94 goto fail;
96 if (c == '\0') {
97 err = 2;
98 if (strict && byte != 8)
99 goto fail;
100 return cp - name;
102 err = 3;
103 if (isdigit(c))
104 nibble = c - '0';
105 else if (isxdigit(c) && (islower(c) || !strict))
106 nibble = tolower(c) - 'a' + 10;
107 else
108 goto fail;
109 *wwn = (*wwn << 4) | nibble;
111 err = 4;
112 fail:
113 pr_debug("err %u len %zu pos %u byte %u\n",
114 err, cp - name, pos, byte);
115 return -1;
118 static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
120 u8 b[8];
122 put_unaligned_be64(wwn, b);
123 return snprintf(buf, len,
124 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
125 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
128 static char *tcm_qla2xxx_get_fabric_name(void)
130 return "qla2xxx";
134 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
136 static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
138 unsigned int i, j;
139 u8 wwn[8];
141 memset(wwn, 0, sizeof(wwn));
143 /* Validate and store the new name */
144 for (i = 0, j = 0; i < 16; i++) {
145 int value;
147 value = hex_to_bin(*ns++);
148 if (value >= 0)
149 j = (j << 4) | value;
150 else
151 return -EINVAL;
153 if (i % 2) {
154 wwn[i/2] = j & 0xff;
155 j = 0;
159 *nm = wwn_to_u64(wwn);
160 return 0;
164 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
165 * store_fc_host_vport_create()
167 static int tcm_qla2xxx_npiv_parse_wwn(
168 const char *name,
169 size_t count,
170 u64 *wwpn,
171 u64 *wwnn)
173 unsigned int cnt = count;
174 int rc;
176 *wwpn = 0;
177 *wwnn = 0;
179 /* count may include a LF at end of string */
180 if (name[cnt-1] == '\n')
181 cnt--;
183 /* validate we have enough characters for WWPN */
184 if ((cnt != (16+1+16)) || (name[16] != ':'))
185 return -EINVAL;
187 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
188 if (rc != 0)
189 return rc;
191 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
192 if (rc != 0)
193 return rc;
195 return 0;
198 static ssize_t tcm_qla2xxx_npiv_format_wwn(char *buf, size_t len,
199 u64 wwpn, u64 wwnn)
201 u8 b[8], b2[8];
203 put_unaligned_be64(wwpn, b);
204 put_unaligned_be64(wwnn, b2);
205 return snprintf(buf, len,
206 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x,"
207 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
208 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
209 b2[0], b2[1], b2[2], b2[3], b2[4], b2[5], b2[6], b2[7]);
212 static char *tcm_qla2xxx_npiv_get_fabric_name(void)
214 return "qla2xxx_npiv";
217 static u8 tcm_qla2xxx_get_fabric_proto_ident(struct se_portal_group *se_tpg)
219 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
220 struct tcm_qla2xxx_tpg, se_tpg);
221 struct tcm_qla2xxx_lport *lport = tpg->lport;
222 u8 proto_id;
224 switch (lport->lport_proto_id) {
225 case SCSI_PROTOCOL_FCP:
226 default:
227 proto_id = fc_get_fabric_proto_ident(se_tpg);
228 break;
231 return proto_id;
234 static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
236 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
237 struct tcm_qla2xxx_tpg, se_tpg);
238 struct tcm_qla2xxx_lport *lport = tpg->lport;
240 return lport->lport_naa_name;
243 static char *tcm_qla2xxx_npiv_get_fabric_wwn(struct se_portal_group *se_tpg)
245 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
246 struct tcm_qla2xxx_tpg, se_tpg);
247 struct tcm_qla2xxx_lport *lport = tpg->lport;
249 return &lport->lport_npiv_name[0];
252 static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
254 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
255 struct tcm_qla2xxx_tpg, se_tpg);
256 return tpg->lport_tpgt;
259 static u32 tcm_qla2xxx_get_default_depth(struct se_portal_group *se_tpg)
261 return 1;
264 static u32 tcm_qla2xxx_get_pr_transport_id(
265 struct se_portal_group *se_tpg,
266 struct se_node_acl *se_nacl,
267 struct t10_pr_registration *pr_reg,
268 int *format_code,
269 unsigned char *buf)
271 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
272 struct tcm_qla2xxx_tpg, se_tpg);
273 struct tcm_qla2xxx_lport *lport = tpg->lport;
274 int ret = 0;
276 switch (lport->lport_proto_id) {
277 case SCSI_PROTOCOL_FCP:
278 default:
279 ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
280 format_code, buf);
281 break;
284 return ret;
287 static u32 tcm_qla2xxx_get_pr_transport_id_len(
288 struct se_portal_group *se_tpg,
289 struct se_node_acl *se_nacl,
290 struct t10_pr_registration *pr_reg,
291 int *format_code)
293 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
294 struct tcm_qla2xxx_tpg, se_tpg);
295 struct tcm_qla2xxx_lport *lport = tpg->lport;
296 int ret = 0;
298 switch (lport->lport_proto_id) {
299 case SCSI_PROTOCOL_FCP:
300 default:
301 ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
302 format_code);
303 break;
306 return ret;
309 static char *tcm_qla2xxx_parse_pr_out_transport_id(
310 struct se_portal_group *se_tpg,
311 const char *buf,
312 u32 *out_tid_len,
313 char **port_nexus_ptr)
315 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
316 struct tcm_qla2xxx_tpg, se_tpg);
317 struct tcm_qla2xxx_lport *lport = tpg->lport;
318 char *tid = NULL;
320 switch (lport->lport_proto_id) {
321 case SCSI_PROTOCOL_FCP:
322 default:
323 tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
324 port_nexus_ptr);
325 break;
328 return tid;
331 static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
333 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
334 struct tcm_qla2xxx_tpg, se_tpg);
336 return QLA_TPG_ATTRIB(tpg)->generate_node_acls;
339 static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
341 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
342 struct tcm_qla2xxx_tpg, se_tpg);
344 return QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls;
347 static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
349 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
350 struct tcm_qla2xxx_tpg, se_tpg);
352 return QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect;
355 static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
357 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
358 struct tcm_qla2xxx_tpg, se_tpg);
360 return QLA_TPG_ATTRIB(tpg)->prod_mode_write_protect;
363 static struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
364 struct se_portal_group *se_tpg)
366 struct tcm_qla2xxx_nacl *nacl;
368 nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
369 if (!nacl) {
370 pr_err("Unable to alocate struct tcm_qla2xxx_nacl\n");
371 return NULL;
374 return &nacl->se_node_acl;
377 static void tcm_qla2xxx_release_fabric_acl(
378 struct se_portal_group *se_tpg,
379 struct se_node_acl *se_nacl)
381 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
382 struct tcm_qla2xxx_nacl, se_node_acl);
383 kfree(nacl);
386 static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
388 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
389 struct tcm_qla2xxx_tpg, se_tpg);
391 return tpg->lport_tpgt;
394 static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
396 struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
397 struct qla_tgt_mgmt_cmd, free_work);
399 transport_generic_free_cmd(&mcmd->se_cmd, 0);
403 * Called from qla_target_template->free_mcmd(), and will call
404 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
405 * release callback. qla_hw_data->hardware_lock is expected to be held
407 static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
409 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
410 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
413 static void tcm_qla2xxx_complete_free(struct work_struct *work)
415 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
417 transport_generic_free_cmd(&cmd->se_cmd, 0);
421 * Called from qla_target_template->free_cmd(), and will call
422 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
423 * release callback. qla_hw_data->hardware_lock is expected to be held
425 static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
427 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
428 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
432 * Called from struct target_core_fabric_ops->check_stop_free() context
434 static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
436 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
439 /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
440 * fabric descriptor @se_cmd command to release
442 static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
444 struct qla_tgt_cmd *cmd;
446 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
447 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
448 struct qla_tgt_mgmt_cmd, se_cmd);
449 qlt_free_mcmd(mcmd);
450 return;
453 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
454 qlt_free_cmd(cmd);
457 static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
459 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
460 struct scsi_qla_host *vha;
461 unsigned long flags;
463 BUG_ON(!sess);
464 vha = sess->vha;
466 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
467 target_sess_cmd_list_set_waiting(se_sess);
468 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
470 return 1;
473 static void tcm_qla2xxx_close_session(struct se_session *se_sess)
475 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
476 struct scsi_qla_host *vha;
477 unsigned long flags;
479 BUG_ON(!sess);
480 vha = sess->vha;
482 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
483 qlt_unreg_sess(sess);
484 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
487 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
489 return 0;
493 * The LIO target core uses DMA_TO_DEVICE to mean that data is going
494 * to the target (eg handling a WRITE) and DMA_FROM_DEVICE to mean
495 * that data is coming from the target (eg handling a READ). However,
496 * this is just the opposite of what we have to tell the DMA mapping
497 * layer -- eg when handling a READ, the HBA will have to DMA the data
498 * out of memory so it can send it to the initiator, which means we
499 * need to use DMA_TO_DEVICE when we map the data.
501 static enum dma_data_direction tcm_qla2xxx_mapping_dir(struct se_cmd *se_cmd)
503 if (se_cmd->se_cmd_flags & SCF_BIDI)
504 return DMA_BIDIRECTIONAL;
506 switch (se_cmd->data_direction) {
507 case DMA_TO_DEVICE:
508 return DMA_FROM_DEVICE;
509 case DMA_FROM_DEVICE:
510 return DMA_TO_DEVICE;
511 case DMA_NONE:
512 default:
513 return DMA_NONE;
517 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
519 struct qla_tgt_cmd *cmd = container_of(se_cmd,
520 struct qla_tgt_cmd, se_cmd);
522 cmd->bufflen = se_cmd->data_length;
523 cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
525 cmd->sg_cnt = se_cmd->t_data_nents;
526 cmd->sg = se_cmd->t_data_sg;
529 * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
530 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
532 return qlt_rdy_to_xfer(cmd);
535 static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
537 unsigned long flags;
539 * Check for WRITE_PENDING status to determine if we need to wait for
540 * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
542 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
543 if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
544 se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
545 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
546 wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
547 3000);
548 return 0;
550 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
552 return 0;
555 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
557 return;
560 static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
562 struct qla_tgt_cmd *cmd = container_of(se_cmd,
563 struct qla_tgt_cmd, se_cmd);
565 return cmd->tag;
568 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
570 return 0;
574 * Called from process context in qla_target.c:qlt_do_work() code
576 static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
577 unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
578 int data_dir, int bidi)
580 struct se_cmd *se_cmd = &cmd->se_cmd;
581 struct se_session *se_sess;
582 struct qla_tgt_sess *sess;
583 int flags = TARGET_SCF_ACK_KREF;
585 if (bidi)
586 flags |= TARGET_SCF_BIDI_OP;
588 sess = cmd->sess;
589 if (!sess) {
590 pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
591 return -EINVAL;
594 se_sess = sess->se_sess;
595 if (!se_sess) {
596 pr_err("Unable to locate active struct se_session\n");
597 return -EINVAL;
600 return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
601 cmd->unpacked_lun, data_length, fcp_task_attr,
602 data_dir, flags);
605 static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
607 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
610 * Ensure that the complete FCP WRITE payload has been received.
611 * Otherwise return an exception via CHECK_CONDITION status.
613 if (!cmd->write_data_transferred) {
615 * Check if se_cmd has already been aborted via LUN_RESET, and
616 * waiting upon completion in tcm_qla2xxx_write_pending_status()
618 if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
619 complete(&cmd->se_cmd.t_transport_stop_comp);
620 return;
623 transport_generic_request_failure(&cmd->se_cmd,
624 TCM_CHECK_CONDITION_ABORT_CMD);
625 return;
628 return target_execute_cmd(&cmd->se_cmd);
632 * Called from qla_target.c:qlt_do_ctio_completion()
634 static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
636 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
637 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
641 * Called from qla_target.c:qlt_issue_task_mgmt()
643 static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
644 uint8_t tmr_func, uint32_t tag)
646 struct qla_tgt_sess *sess = mcmd->sess;
647 struct se_cmd *se_cmd = &mcmd->se_cmd;
649 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
650 tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
653 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
655 struct qla_tgt_cmd *cmd = container_of(se_cmd,
656 struct qla_tgt_cmd, se_cmd);
658 cmd->bufflen = se_cmd->data_length;
659 cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
660 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
662 cmd->sg_cnt = se_cmd->t_data_nents;
663 cmd->sg = se_cmd->t_data_sg;
664 cmd->offset = 0;
667 * Now queue completed DATA_IN the qla2xxx LLD and response ring
669 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
670 se_cmd->scsi_status);
673 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
675 struct qla_tgt_cmd *cmd = container_of(se_cmd,
676 struct qla_tgt_cmd, se_cmd);
677 int xmit_type = QLA_TGT_XMIT_STATUS;
679 cmd->bufflen = se_cmd->data_length;
680 cmd->sg = NULL;
681 cmd->sg_cnt = 0;
682 cmd->offset = 0;
683 cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
684 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
686 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
688 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
689 * for qla_tgt_xmit_response LLD code
691 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
692 se_cmd->residual_count = se_cmd->data_length;
694 cmd->bufflen = 0;
697 * Now queue status response to qla2xxx LLD code and response ring
699 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
702 static int tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
704 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
705 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
706 struct qla_tgt_mgmt_cmd, se_cmd);
708 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
709 mcmd, se_tmr->function, se_tmr->response);
711 * Do translation between TCM TM response codes and
712 * QLA2xxx FC TM response codes.
714 switch (se_tmr->response) {
715 case TMR_FUNCTION_COMPLETE:
716 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
717 break;
718 case TMR_TASK_DOES_NOT_EXIST:
719 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
720 break;
721 case TMR_FUNCTION_REJECTED:
722 mcmd->fc_tm_rsp = FC_TM_REJECT;
723 break;
724 case TMR_LUN_DOES_NOT_EXIST:
725 default:
726 mcmd->fc_tm_rsp = FC_TM_FAILED;
727 break;
730 * Queue the TM response to QLA2xxx LLD to build a
731 * CTIO response packet.
733 qlt_xmit_tm_rsp(mcmd);
735 return 0;
738 /* Local pointer to allocated TCM configfs fabric module */
739 struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
740 struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
742 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
743 struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
745 * Expected to be called with struct qla_hw_data->hardware_lock held
747 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
749 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
750 struct se_portal_group *se_tpg = se_nacl->se_tpg;
751 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
752 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
753 struct tcm_qla2xxx_lport, lport_wwn);
754 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
755 struct tcm_qla2xxx_nacl, se_node_acl);
756 void *node;
758 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
760 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
761 WARN_ON(node && (node != se_nacl));
763 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
764 se_nacl, nacl->nport_wwnn, nacl->nport_id);
766 * Now clear the se_nacl and session pointers from our HW lport lookup
767 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
769 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
770 * target_wait_for_sess_cmds() before the session waits for outstanding
771 * I/O to complete, to avoid a race between session shutdown execution
772 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
774 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
777 static void tcm_qla2xxx_release_session(struct kref *kref)
779 struct se_session *se_sess = container_of(kref,
780 struct se_session, sess_kref);
782 qlt_unreg_sess(se_sess->fabric_sess_ptr);
785 static void tcm_qla2xxx_put_session(struct se_session *se_sess)
787 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
788 struct qla_hw_data *ha = sess->vha->hw;
789 unsigned long flags;
791 spin_lock_irqsave(&ha->hardware_lock, flags);
792 kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
793 spin_unlock_irqrestore(&ha->hardware_lock, flags);
796 static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
798 tcm_qla2xxx_put_session(sess->se_sess);
801 static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
803 tcm_qla2xxx_shutdown_session(sess->se_sess);
806 static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
807 struct se_portal_group *se_tpg,
808 struct config_group *group,
809 const char *name)
811 struct se_node_acl *se_nacl, *se_nacl_new;
812 struct tcm_qla2xxx_nacl *nacl;
813 u64 wwnn;
814 u32 qla2xxx_nexus_depth;
816 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
817 return ERR_PTR(-EINVAL);
819 se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
820 if (!se_nacl_new)
821 return ERR_PTR(-ENOMEM);
822 /* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
823 qla2xxx_nexus_depth = 1;
826 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
827 * when converting a NodeACL from demo mode -> explict
829 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
830 name, qla2xxx_nexus_depth);
831 if (IS_ERR(se_nacl)) {
832 tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
833 return se_nacl;
836 * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
838 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
839 nacl->nport_wwnn = wwnn;
840 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
842 return se_nacl;
845 static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
847 struct se_portal_group *se_tpg = se_acl->se_tpg;
848 struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
849 struct tcm_qla2xxx_nacl, se_node_acl);
851 core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
852 kfree(nacl);
855 /* Start items for tcm_qla2xxx_tpg_attrib_cit */
857 #define DEF_QLA_TPG_ATTRIB(name) \
859 static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
860 struct se_portal_group *se_tpg, \
861 char *page) \
863 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
864 struct tcm_qla2xxx_tpg, se_tpg); \
866 return sprintf(page, "%u\n", QLA_TPG_ATTRIB(tpg)->name); \
869 static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
870 struct se_portal_group *se_tpg, \
871 const char *page, \
872 size_t count) \
874 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
875 struct tcm_qla2xxx_tpg, se_tpg); \
876 unsigned long val; \
877 int ret; \
879 ret = kstrtoul(page, 0, &val); \
880 if (ret < 0) { \
881 pr_err("kstrtoul() failed with" \
882 " ret: %d\n", ret); \
883 return -EINVAL; \
885 ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
887 return (!ret) ? count : -EINVAL; \
890 #define DEF_QLA_TPG_ATTR_BOOL(_name) \
892 static int tcm_qla2xxx_set_attrib_##_name( \
893 struct tcm_qla2xxx_tpg *tpg, \
894 unsigned long val) \
896 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
898 if ((val != 0) && (val != 1)) { \
899 pr_err("Illegal boolean value %lu\n", val); \
900 return -EINVAL; \
903 a->_name = val; \
904 return 0; \
907 #define QLA_TPG_ATTR(_name, _mode) \
908 TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
911 * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
913 DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
914 DEF_QLA_TPG_ATTRIB(generate_node_acls);
915 QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
918 Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
920 DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
921 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
922 QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
925 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
927 DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
928 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
929 QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
932 * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
934 DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
935 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
936 QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
938 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
939 &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
940 &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
941 &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
942 &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
943 NULL,
946 /* End items for tcm_qla2xxx_tpg_attrib_cit */
948 static ssize_t tcm_qla2xxx_tpg_show_enable(
949 struct se_portal_group *se_tpg,
950 char *page)
952 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
953 struct tcm_qla2xxx_tpg, se_tpg);
955 return snprintf(page, PAGE_SIZE, "%d\n",
956 atomic_read(&tpg->lport_tpg_enabled));
959 static ssize_t tcm_qla2xxx_tpg_store_enable(
960 struct se_portal_group *se_tpg,
961 const char *page,
962 size_t count)
964 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
965 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
966 struct tcm_qla2xxx_lport, lport_wwn);
967 struct scsi_qla_host *vha = lport->qla_vha;
968 struct qla_hw_data *ha = vha->hw;
969 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
970 struct tcm_qla2xxx_tpg, se_tpg);
971 unsigned long op;
972 int rc;
974 rc = kstrtoul(page, 0, &op);
975 if (rc < 0) {
976 pr_err("kstrtoul() returned %d\n", rc);
977 return -EINVAL;
979 if ((op != 1) && (op != 0)) {
980 pr_err("Illegal value for tpg_enable: %lu\n", op);
981 return -EINVAL;
984 if (op) {
985 atomic_set(&tpg->lport_tpg_enabled, 1);
986 qlt_enable_vha(vha);
987 } else {
988 if (!ha->tgt.qla_tgt) {
989 pr_err("truct qla_hw_data *ha->tgt.qla_tgt is NULL\n");
990 return -ENODEV;
992 atomic_set(&tpg->lport_tpg_enabled, 0);
993 qlt_stop_phase1(ha->tgt.qla_tgt);
996 return count;
999 TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
1001 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
1002 &tcm_qla2xxx_tpg_enable.attr,
1003 NULL,
1006 static struct se_portal_group *tcm_qla2xxx_make_tpg(
1007 struct se_wwn *wwn,
1008 struct config_group *group,
1009 const char *name)
1011 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1012 struct tcm_qla2xxx_lport, lport_wwn);
1013 struct tcm_qla2xxx_tpg *tpg;
1014 unsigned long tpgt;
1015 int ret;
1017 if (strstr(name, "tpgt_") != name)
1018 return ERR_PTR(-EINVAL);
1019 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1020 return ERR_PTR(-EINVAL);
1022 if (!lport->qla_npiv_vp && (tpgt != 1)) {
1023 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1024 return ERR_PTR(-ENOSYS);
1027 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1028 if (!tpg) {
1029 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1030 return ERR_PTR(-ENOMEM);
1032 tpg->lport = lport;
1033 tpg->lport_tpgt = tpgt;
1035 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1036 * NodeACLs
1038 QLA_TPG_ATTRIB(tpg)->generate_node_acls = 1;
1039 QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect = 1;
1040 QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls = 1;
1042 ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
1043 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1044 if (ret < 0) {
1045 kfree(tpg);
1046 return NULL;
1049 * Setup local TPG=1 pointer for non NPIV mode.
1051 if (lport->qla_npiv_vp == NULL)
1052 lport->tpg_1 = tpg;
1054 return &tpg->se_tpg;
1057 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1059 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1060 struct tcm_qla2xxx_tpg, se_tpg);
1061 struct tcm_qla2xxx_lport *lport = tpg->lport;
1062 struct scsi_qla_host *vha = lport->qla_vha;
1063 struct qla_hw_data *ha = vha->hw;
1065 * Call into qla2x_target.c LLD logic to shutdown the active
1066 * FC Nexuses and disable target mode operation for this qla_hw_data
1068 if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stop)
1069 qlt_stop_phase1(ha->tgt.qla_tgt);
1071 core_tpg_deregister(se_tpg);
1073 * Clear local TPG=1 pointer for non NPIV mode.
1075 if (lport->qla_npiv_vp == NULL)
1076 lport->tpg_1 = NULL;
1078 kfree(tpg);
1081 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1082 struct se_wwn *wwn,
1083 struct config_group *group,
1084 const char *name)
1086 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1087 struct tcm_qla2xxx_lport, lport_wwn);
1088 struct tcm_qla2xxx_tpg *tpg;
1089 unsigned long tpgt;
1090 int ret;
1092 if (strstr(name, "tpgt_") != name)
1093 return ERR_PTR(-EINVAL);
1094 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1095 return ERR_PTR(-EINVAL);
1097 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1098 if (!tpg) {
1099 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1100 return ERR_PTR(-ENOMEM);
1102 tpg->lport = lport;
1103 tpg->lport_tpgt = tpgt;
1105 ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
1106 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1107 if (ret < 0) {
1108 kfree(tpg);
1109 return NULL;
1111 return &tpg->se_tpg;
1115 * Expected to be called with struct qla_hw_data->hardware_lock held
1117 static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
1118 scsi_qla_host_t *vha,
1119 const uint8_t *s_id)
1121 struct qla_hw_data *ha = vha->hw;
1122 struct tcm_qla2xxx_lport *lport;
1123 struct se_node_acl *se_nacl;
1124 struct tcm_qla2xxx_nacl *nacl;
1125 u32 key;
1127 lport = ha->tgt.target_lport_ptr;
1128 if (!lport) {
1129 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1130 dump_stack();
1131 return NULL;
1134 key = (((unsigned long)s_id[0] << 16) |
1135 ((unsigned long)s_id[1] << 8) |
1136 (unsigned long)s_id[2]);
1137 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1139 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1140 if (!se_nacl) {
1141 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1142 return NULL;
1144 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1145 se_nacl, se_nacl->initiatorname);
1147 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1148 if (!nacl->qla_tgt_sess) {
1149 pr_err("Unable to locate struct qla_tgt_sess\n");
1150 return NULL;
1153 return nacl->qla_tgt_sess;
1157 * Expected to be called with struct qla_hw_data->hardware_lock held
1159 static void tcm_qla2xxx_set_sess_by_s_id(
1160 struct tcm_qla2xxx_lport *lport,
1161 struct se_node_acl *new_se_nacl,
1162 struct tcm_qla2xxx_nacl *nacl,
1163 struct se_session *se_sess,
1164 struct qla_tgt_sess *qla_tgt_sess,
1165 uint8_t *s_id)
1167 u32 key;
1168 void *slot;
1169 int rc;
1171 key = (((unsigned long)s_id[0] << 16) |
1172 ((unsigned long)s_id[1] << 8) |
1173 (unsigned long)s_id[2]);
1174 pr_debug("set_sess_by_s_id: %06x\n", key);
1176 slot = btree_lookup32(&lport->lport_fcport_map, key);
1177 if (!slot) {
1178 if (new_se_nacl) {
1179 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1180 nacl->nport_id = key;
1181 rc = btree_insert32(&lport->lport_fcport_map, key,
1182 new_se_nacl, GFP_ATOMIC);
1183 if (rc)
1184 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1185 (int)key);
1186 } else {
1187 pr_debug("Wiping nonexisting fc_port entry\n");
1190 qla_tgt_sess->se_sess = se_sess;
1191 nacl->qla_tgt_sess = qla_tgt_sess;
1192 return;
1195 if (nacl->qla_tgt_sess) {
1196 if (new_se_nacl == NULL) {
1197 pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
1198 btree_remove32(&lport->lport_fcport_map, key);
1199 nacl->qla_tgt_sess = NULL;
1200 return;
1202 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
1203 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1204 qla_tgt_sess->se_sess = se_sess;
1205 nacl->qla_tgt_sess = qla_tgt_sess;
1206 return;
1209 if (new_se_nacl == NULL) {
1210 pr_debug("Clearing existing fc_port entry\n");
1211 btree_remove32(&lport->lport_fcport_map, key);
1212 return;
1215 pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
1216 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1217 qla_tgt_sess->se_sess = se_sess;
1218 nacl->qla_tgt_sess = qla_tgt_sess;
1220 pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
1221 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1225 * Expected to be called with struct qla_hw_data->hardware_lock held
1227 static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
1228 scsi_qla_host_t *vha,
1229 const uint16_t loop_id)
1231 struct qla_hw_data *ha = vha->hw;
1232 struct tcm_qla2xxx_lport *lport;
1233 struct se_node_acl *se_nacl;
1234 struct tcm_qla2xxx_nacl *nacl;
1235 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1237 lport = ha->tgt.target_lport_ptr;
1238 if (!lport) {
1239 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1240 dump_stack();
1241 return NULL;
1244 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1246 fc_loopid = lport->lport_loopid_map + loop_id;
1247 se_nacl = fc_loopid->se_nacl;
1248 if (!se_nacl) {
1249 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1250 loop_id);
1251 return NULL;
1254 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1256 if (!nacl->qla_tgt_sess) {
1257 pr_err("Unable to locate struct qla_tgt_sess\n");
1258 return NULL;
1261 return nacl->qla_tgt_sess;
1265 * Expected to be called with struct qla_hw_data->hardware_lock held
1267 static void tcm_qla2xxx_set_sess_by_loop_id(
1268 struct tcm_qla2xxx_lport *lport,
1269 struct se_node_acl *new_se_nacl,
1270 struct tcm_qla2xxx_nacl *nacl,
1271 struct se_session *se_sess,
1272 struct qla_tgt_sess *qla_tgt_sess,
1273 uint16_t loop_id)
1275 struct se_node_acl *saved_nacl;
1276 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1278 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1280 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1281 lport->lport_loopid_map)[loop_id];
1283 saved_nacl = fc_loopid->se_nacl;
1284 if (!saved_nacl) {
1285 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1286 fc_loopid->se_nacl = new_se_nacl;
1287 if (qla_tgt_sess->se_sess != se_sess)
1288 qla_tgt_sess->se_sess = se_sess;
1289 if (nacl->qla_tgt_sess != qla_tgt_sess)
1290 nacl->qla_tgt_sess = qla_tgt_sess;
1291 return;
1294 if (nacl->qla_tgt_sess) {
1295 if (new_se_nacl == NULL) {
1296 pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1297 fc_loopid->se_nacl = NULL;
1298 nacl->qla_tgt_sess = NULL;
1299 return;
1302 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1303 fc_loopid->se_nacl = new_se_nacl;
1304 if (qla_tgt_sess->se_sess != se_sess)
1305 qla_tgt_sess->se_sess = se_sess;
1306 if (nacl->qla_tgt_sess != qla_tgt_sess)
1307 nacl->qla_tgt_sess = qla_tgt_sess;
1308 return;
1311 if (new_se_nacl == NULL) {
1312 pr_debug("Clearing fc_loopid->se_nacl\n");
1313 fc_loopid->se_nacl = NULL;
1314 return;
1317 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
1318 fc_loopid->se_nacl = new_se_nacl;
1319 if (qla_tgt_sess->se_sess != se_sess)
1320 qla_tgt_sess->se_sess = se_sess;
1321 if (nacl->qla_tgt_sess != qla_tgt_sess)
1322 nacl->qla_tgt_sess = qla_tgt_sess;
1324 pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1325 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1329 * Should always be called with qla_hw_data->hardware_lock held.
1331 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1332 struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
1334 struct se_session *se_sess = sess->se_sess;
1335 unsigned char be_sid[3];
1337 be_sid[0] = sess->s_id.b.domain;
1338 be_sid[1] = sess->s_id.b.area;
1339 be_sid[2] = sess->s_id.b.al_pa;
1341 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1342 sess, be_sid);
1343 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1344 sess, sess->loop_id);
1347 static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
1349 struct qla_tgt *tgt = sess->tgt;
1350 struct qla_hw_data *ha = tgt->ha;
1351 struct se_session *se_sess;
1352 struct se_node_acl *se_nacl;
1353 struct tcm_qla2xxx_lport *lport;
1354 struct tcm_qla2xxx_nacl *nacl;
1356 BUG_ON(in_interrupt());
1358 se_sess = sess->se_sess;
1359 if (!se_sess) {
1360 pr_err("struct qla_tgt_sess->se_sess is NULL\n");
1361 dump_stack();
1362 return;
1364 se_nacl = se_sess->se_node_acl;
1365 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1367 lport = ha->tgt.target_lport_ptr;
1368 if (!lport) {
1369 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1370 dump_stack();
1371 return;
1373 target_wait_for_sess_cmds(se_sess, 0);
1375 transport_deregister_session_configfs(sess->se_sess);
1376 transport_deregister_session(sess->se_sess);
1380 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1381 * to locate struct se_node_acl
1383 static int tcm_qla2xxx_check_initiator_node_acl(
1384 scsi_qla_host_t *vha,
1385 unsigned char *fc_wwpn,
1386 void *qla_tgt_sess,
1387 uint8_t *s_id,
1388 uint16_t loop_id)
1390 struct qla_hw_data *ha = vha->hw;
1391 struct tcm_qla2xxx_lport *lport;
1392 struct tcm_qla2xxx_tpg *tpg;
1393 struct tcm_qla2xxx_nacl *nacl;
1394 struct se_portal_group *se_tpg;
1395 struct se_node_acl *se_nacl;
1396 struct se_session *se_sess;
1397 struct qla_tgt_sess *sess = qla_tgt_sess;
1398 unsigned char port_name[36];
1399 unsigned long flags;
1401 lport = ha->tgt.target_lport_ptr;
1402 if (!lport) {
1403 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1404 dump_stack();
1405 return -EINVAL;
1408 * Locate the TPG=1 reference..
1410 tpg = lport->tpg_1;
1411 if (!tpg) {
1412 pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1413 return -EINVAL;
1415 se_tpg = &tpg->se_tpg;
1417 se_sess = transport_init_session();
1418 if (IS_ERR(se_sess)) {
1419 pr_err("Unable to initialize struct se_session\n");
1420 return PTR_ERR(se_sess);
1423 * Format the FCP Initiator port_name into colon seperated values to
1424 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1426 memset(&port_name, 0, 36);
1427 snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1428 fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
1429 fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
1431 * Locate our struct se_node_acl either from an explict NodeACL created
1432 * via ConfigFS, or via running in TPG demo mode.
1434 se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
1435 port_name);
1436 if (!se_sess->se_node_acl) {
1437 transport_free_session(se_sess);
1438 return -EINVAL;
1440 se_nacl = se_sess->se_node_acl;
1441 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1443 * And now setup the new se_nacl and session pointers into our HW lport
1444 * mappings for fabric S_ID and LOOP_ID.
1446 spin_lock_irqsave(&ha->hardware_lock, flags);
1447 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
1448 qla_tgt_sess, s_id);
1449 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
1450 qla_tgt_sess, loop_id);
1451 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1453 * Finally register the new FC Nexus with TCM
1455 __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
1457 return 0;
1460 static void tcm_qla2xxx_update_sess(struct qla_tgt_sess *sess, port_id_t s_id,
1461 uint16_t loop_id, bool conf_compl_supported)
1463 struct qla_tgt *tgt = sess->tgt;
1464 struct qla_hw_data *ha = tgt->ha;
1465 struct tcm_qla2xxx_lport *lport = ha->tgt.target_lport_ptr;
1466 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1467 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1468 struct tcm_qla2xxx_nacl, se_node_acl);
1469 u32 key;
1472 if (sess->loop_id != loop_id || sess->s_id.b24 != s_id.b24)
1473 pr_info("Updating session %p from port %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1474 sess,
1475 sess->port_name[0], sess->port_name[1],
1476 sess->port_name[2], sess->port_name[3],
1477 sess->port_name[4], sess->port_name[5],
1478 sess->port_name[6], sess->port_name[7],
1479 sess->loop_id, loop_id,
1480 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
1481 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1483 if (sess->loop_id != loop_id) {
1485 * Because we can shuffle loop IDs around and we
1486 * update different sessions non-atomically, we might
1487 * have overwritten this session's old loop ID
1488 * already, and we might end up overwriting some other
1489 * session that will be updated later. So we have to
1490 * be extra careful and we can't warn about those things...
1492 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1493 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1495 lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1497 sess->loop_id = loop_id;
1500 if (sess->s_id.b24 != s_id.b24) {
1501 key = (((u32) sess->s_id.b.domain << 16) |
1502 ((u32) sess->s_id.b.area << 8) |
1503 ((u32) sess->s_id.b.al_pa));
1505 if (btree_lookup32(&lport->lport_fcport_map, key))
1506 WARN(btree_remove32(&lport->lport_fcport_map, key) != se_nacl,
1507 "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1508 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1509 else
1510 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1511 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1513 key = (((u32) s_id.b.domain << 16) |
1514 ((u32) s_id.b.area << 8) |
1515 ((u32) s_id.b.al_pa));
1517 if (btree_lookup32(&lport->lport_fcport_map, key)) {
1518 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1519 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1520 btree_update32(&lport->lport_fcport_map, key, se_nacl);
1521 } else {
1522 btree_insert32(&lport->lport_fcport_map, key, se_nacl, GFP_ATOMIC);
1525 sess->s_id = s_id;
1526 nacl->nport_id = key;
1529 sess->conf_compl_supported = conf_compl_supported;
1533 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1535 static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1536 .handle_cmd = tcm_qla2xxx_handle_cmd,
1537 .handle_data = tcm_qla2xxx_handle_data,
1538 .handle_tmr = tcm_qla2xxx_handle_tmr,
1539 .free_cmd = tcm_qla2xxx_free_cmd,
1540 .free_mcmd = tcm_qla2xxx_free_mcmd,
1541 .free_session = tcm_qla2xxx_free_session,
1542 .update_sess = tcm_qla2xxx_update_sess,
1543 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1544 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1545 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1546 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1547 .put_sess = tcm_qla2xxx_put_sess,
1548 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1551 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1553 int rc;
1555 rc = btree_init32(&lport->lport_fcport_map);
1556 if (rc) {
1557 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1558 return rc;
1561 lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
1562 65536);
1563 if (!lport->lport_loopid_map) {
1564 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1565 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1566 btree_destroy32(&lport->lport_fcport_map);
1567 return -ENOMEM;
1569 memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
1570 * 65536);
1571 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1572 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1573 return 0;
1576 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha)
1578 struct qla_hw_data *ha = vha->hw;
1579 struct tcm_qla2xxx_lport *lport;
1581 * Setup local pointer to vha, NPIV VP pointer (if present) and
1582 * vha->tcm_lport pointer
1584 lport = (struct tcm_qla2xxx_lport *)ha->tgt.target_lport_ptr;
1585 lport->qla_vha = vha;
1587 return 0;
1590 static struct se_wwn *tcm_qla2xxx_make_lport(
1591 struct target_fabric_configfs *tf,
1592 struct config_group *group,
1593 const char *name)
1595 struct tcm_qla2xxx_lport *lport;
1596 u64 wwpn;
1597 int ret = -ENODEV;
1599 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1600 return ERR_PTR(-EINVAL);
1602 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1603 if (!lport) {
1604 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1605 return ERR_PTR(-ENOMEM);
1607 lport->lport_wwpn = wwpn;
1608 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1609 wwpn);
1610 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
1612 ret = tcm_qla2xxx_init_lport(lport);
1613 if (ret != 0)
1614 goto out;
1616 ret = qlt_lport_register(&tcm_qla2xxx_template, wwpn,
1617 tcm_qla2xxx_lport_register_cb, lport);
1618 if (ret != 0)
1619 goto out_lport;
1621 return &lport->lport_wwn;
1622 out_lport:
1623 vfree(lport->lport_loopid_map);
1624 btree_destroy32(&lport->lport_fcport_map);
1625 out:
1626 kfree(lport);
1627 return ERR_PTR(ret);
1630 static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1632 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1633 struct tcm_qla2xxx_lport, lport_wwn);
1634 struct scsi_qla_host *vha = lport->qla_vha;
1635 struct qla_hw_data *ha = vha->hw;
1636 struct se_node_acl *node;
1637 u32 key = 0;
1640 * Call into qla2x_target.c LLD logic to complete the
1641 * shutdown of struct qla_tgt after the call to
1642 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1644 if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stopped)
1645 qlt_stop_phase2(ha->tgt.qla_tgt);
1647 qlt_lport_deregister(vha);
1649 vfree(lport->lport_loopid_map);
1650 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1651 btree_remove32(&lport->lport_fcport_map, key);
1652 btree_destroy32(&lport->lport_fcport_map);
1653 kfree(lport);
1656 static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1657 struct target_fabric_configfs *tf,
1658 struct config_group *group,
1659 const char *name)
1661 struct tcm_qla2xxx_lport *lport;
1662 u64 npiv_wwpn, npiv_wwnn;
1663 int ret;
1665 if (tcm_qla2xxx_npiv_parse_wwn(name, strlen(name)+1,
1666 &npiv_wwpn, &npiv_wwnn) < 0)
1667 return ERR_PTR(-EINVAL);
1669 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1670 if (!lport) {
1671 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1672 return ERR_PTR(-ENOMEM);
1674 lport->lport_npiv_wwpn = npiv_wwpn;
1675 lport->lport_npiv_wwnn = npiv_wwnn;
1676 tcm_qla2xxx_npiv_format_wwn(&lport->lport_npiv_name[0],
1677 TCM_QLA2XXX_NAMELEN, npiv_wwpn, npiv_wwnn);
1678 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
1680 /* FIXME: tcm_qla2xxx_npiv_make_lport */
1681 ret = -ENOSYS;
1682 if (ret != 0)
1683 goto out;
1685 return &lport->lport_wwn;
1686 out:
1687 kfree(lport);
1688 return ERR_PTR(ret);
1691 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1693 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1694 struct tcm_qla2xxx_lport, lport_wwn);
1695 struct scsi_qla_host *vha = lport->qla_vha;
1696 struct Scsi_Host *sh = vha->host;
1698 * Notify libfc that we want to release the lport->npiv_vport
1700 fc_vport_terminate(lport->npiv_vport);
1702 scsi_host_put(sh);
1703 kfree(lport);
1707 static ssize_t tcm_qla2xxx_wwn_show_attr_version(
1708 struct target_fabric_configfs *tf,
1709 char *page)
1711 return sprintf(page,
1712 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
1713 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1714 utsname()->machine);
1717 TF_WWN_ATTR_RO(tcm_qla2xxx, version);
1719 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1720 &tcm_qla2xxx_wwn_version.attr,
1721 NULL,
1724 static struct target_core_fabric_ops tcm_qla2xxx_ops = {
1725 .get_fabric_name = tcm_qla2xxx_get_fabric_name,
1726 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1727 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1728 .tpg_get_tag = tcm_qla2xxx_get_tag,
1729 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1730 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1731 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1732 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1733 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1734 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1735 .tpg_check_demo_mode_write_protect =
1736 tcm_qla2xxx_check_demo_write_protect,
1737 .tpg_check_prod_mode_write_protect =
1738 tcm_qla2xxx_check_prod_write_protect,
1739 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
1740 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1741 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1742 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1743 .check_stop_free = tcm_qla2xxx_check_stop_free,
1744 .release_cmd = tcm_qla2xxx_release_cmd,
1745 .put_session = tcm_qla2xxx_put_session,
1746 .shutdown_session = tcm_qla2xxx_shutdown_session,
1747 .close_session = tcm_qla2xxx_close_session,
1748 .sess_get_index = tcm_qla2xxx_sess_get_index,
1749 .sess_get_initiator_sid = NULL,
1750 .write_pending = tcm_qla2xxx_write_pending,
1751 .write_pending_status = tcm_qla2xxx_write_pending_status,
1752 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1753 .get_task_tag = tcm_qla2xxx_get_task_tag,
1754 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1755 .queue_data_in = tcm_qla2xxx_queue_data_in,
1756 .queue_status = tcm_qla2xxx_queue_status,
1757 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1759 * Setup function pointers for generic logic in
1760 * target_core_fabric_configfs.c
1762 .fabric_make_wwn = tcm_qla2xxx_make_lport,
1763 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
1764 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
1765 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1766 .fabric_post_link = NULL,
1767 .fabric_pre_unlink = NULL,
1768 .fabric_make_np = NULL,
1769 .fabric_drop_np = NULL,
1770 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1771 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1774 static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1775 .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
1776 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1777 .tpg_get_wwn = tcm_qla2xxx_npiv_get_fabric_wwn,
1778 .tpg_get_tag = tcm_qla2xxx_get_tag,
1779 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1780 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1781 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1782 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1783 .tpg_check_demo_mode = tcm_qla2xxx_check_false,
1784 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_true,
1785 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_true,
1786 .tpg_check_prod_mode_write_protect = tcm_qla2xxx_check_false,
1787 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
1788 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1789 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1790 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1791 .release_cmd = tcm_qla2xxx_release_cmd,
1792 .put_session = tcm_qla2xxx_put_session,
1793 .shutdown_session = tcm_qla2xxx_shutdown_session,
1794 .close_session = tcm_qla2xxx_close_session,
1795 .sess_get_index = tcm_qla2xxx_sess_get_index,
1796 .sess_get_initiator_sid = NULL,
1797 .write_pending = tcm_qla2xxx_write_pending,
1798 .write_pending_status = tcm_qla2xxx_write_pending_status,
1799 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1800 .get_task_tag = tcm_qla2xxx_get_task_tag,
1801 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1802 .queue_data_in = tcm_qla2xxx_queue_data_in,
1803 .queue_status = tcm_qla2xxx_queue_status,
1804 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
1806 * Setup function pointers for generic logic in
1807 * target_core_fabric_configfs.c
1809 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
1810 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
1811 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
1812 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1813 .fabric_post_link = NULL,
1814 .fabric_pre_unlink = NULL,
1815 .fabric_make_np = NULL,
1816 .fabric_drop_np = NULL,
1817 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1818 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1821 static int tcm_qla2xxx_register_configfs(void)
1823 struct target_fabric_configfs *fabric, *npiv_fabric;
1824 int ret;
1826 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
1827 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1828 utsname()->machine);
1830 * Register the top level struct config_item_type with TCM core
1832 fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
1833 if (IS_ERR(fabric)) {
1834 pr_err("target_fabric_configfs_init() failed\n");
1835 return PTR_ERR(fabric);
1838 * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
1840 fabric->tf_ops = tcm_qla2xxx_ops;
1842 * Setup default attribute lists for various fabric->tf_cit_tmpl
1844 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1845 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
1846 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs =
1847 tcm_qla2xxx_tpg_attrib_attrs;
1848 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1849 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1850 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1851 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1852 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1853 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1855 * Register the fabric for use within TCM
1857 ret = target_fabric_configfs_register(fabric);
1858 if (ret < 0) {
1859 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1860 return ret;
1863 * Setup our local pointer to *fabric
1865 tcm_qla2xxx_fabric_configfs = fabric;
1866 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
1869 * Register the top level struct config_item_type for NPIV with TCM core
1871 npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
1872 if (IS_ERR(npiv_fabric)) {
1873 pr_err("target_fabric_configfs_init() failed\n");
1874 ret = PTR_ERR(npiv_fabric);
1875 goto out_fabric;
1878 * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
1880 npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
1882 * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
1884 TF_CIT_TMPL(npiv_fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1885 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
1886 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1887 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1888 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1889 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1890 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1891 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1892 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1894 * Register the npiv_fabric for use within TCM
1896 ret = target_fabric_configfs_register(npiv_fabric);
1897 if (ret < 0) {
1898 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1899 goto out_fabric;
1902 * Setup our local pointer to *npiv_fabric
1904 tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
1905 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
1907 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1908 WQ_MEM_RECLAIM, 0);
1909 if (!tcm_qla2xxx_free_wq) {
1910 ret = -ENOMEM;
1911 goto out_fabric_npiv;
1914 tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
1915 if (!tcm_qla2xxx_cmd_wq) {
1916 ret = -ENOMEM;
1917 goto out_free_wq;
1920 return 0;
1922 out_free_wq:
1923 destroy_workqueue(tcm_qla2xxx_free_wq);
1924 out_fabric_npiv:
1925 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1926 out_fabric:
1927 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1928 return ret;
1931 static void tcm_qla2xxx_deregister_configfs(void)
1933 destroy_workqueue(tcm_qla2xxx_cmd_wq);
1934 destroy_workqueue(tcm_qla2xxx_free_wq);
1936 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1937 tcm_qla2xxx_fabric_configfs = NULL;
1938 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
1940 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1941 tcm_qla2xxx_npiv_fabric_configfs = NULL;
1942 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
1945 static int __init tcm_qla2xxx_init(void)
1947 int ret;
1949 ret = tcm_qla2xxx_register_configfs();
1950 if (ret < 0)
1951 return ret;
1953 return 0;
1956 static void __exit tcm_qla2xxx_exit(void)
1958 tcm_qla2xxx_deregister_configfs();
1961 MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
1962 MODULE_LICENSE("GPL");
1963 module_init(tcm_qla2xxx_init);
1964 module_exit(tcm_qla2xxx_exit);