blob: 68fb66fdb757fbc98784fad4ff7a5d03c49ee73d [file] [log] [blame]
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001/*******************************************************************************
2 * This file contains tcm implementation using v4 configfs fabric infrastructure
3 * for QLogic target mode HBAs
4 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07005 * (c) Copyright 2010-2013 Datera, Inc.
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04006 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07007 * Author: Nicholas A. Bellinger <nab@daterainc.com>
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04008 *
9 * tcm_qla2xxx_parse_wwn() and tcm_qla2xxx_format_wwn() contains code from
10 * the TCM_FC / Open-FCoE.org fabric module.
11 *
12 * Copyright (c) 2010 Cisco Systems, Inc
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 ****************************************************************************/
24
25
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <generated/utsrelease.h>
29#include <linux/utsname.h>
30#include <linux/init.h>
31#include <linux/list.h>
32#include <linux/slab.h>
33#include <linux/kthread.h>
34#include <linux/types.h>
35#include <linux/string.h>
36#include <linux/configfs.h>
37#include <linux/ctype.h>
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -040038#include <asm/unaligned.h>
39#include <scsi/scsi.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi_device.h>
42#include <scsi/scsi_cmnd.h>
43#include <target/target_core_base.h>
44#include <target/target_core_fabric.h>
45#include <target/target_core_fabric_configfs.h>
46#include <target/target_core_configfs.h>
47#include <target/configfs_macros.h>
48
49#include "qla_def.h"
50#include "qla_target.h"
51#include "tcm_qla2xxx.h"
52
53struct workqueue_struct *tcm_qla2xxx_free_wq;
54struct workqueue_struct *tcm_qla2xxx_cmd_wq;
55
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -040056/*
57 * Parse WWN.
58 * If strict, we require lower-case hex and colon separators to be sure
59 * the name is the same as what would be generated by ft_format_wwn()
60 * so the name and wwn are mapped one-to-one.
61 */
62static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
63{
64 const char *cp;
65 char c;
66 u32 nibble;
67 u32 byte = 0;
68 u32 pos = 0;
69 u32 err;
70
71 *wwn = 0;
72 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
73 c = *cp;
74 if (c == '\n' && cp[1] == '\0')
75 continue;
76 if (strict && pos++ == 2 && byte++ < 7) {
77 pos = 0;
78 if (c == ':')
79 continue;
80 err = 1;
81 goto fail;
82 }
83 if (c == '\0') {
84 err = 2;
85 if (strict && byte != 8)
86 goto fail;
87 return cp - name;
88 }
89 err = 3;
90 if (isdigit(c))
91 nibble = c - '0';
92 else if (isxdigit(c) && (islower(c) || !strict))
93 nibble = tolower(c) - 'a' + 10;
94 else
95 goto fail;
96 *wwn = (*wwn << 4) | nibble;
97 }
98 err = 4;
99fail:
100 pr_debug("err %u len %zu pos %u byte %u\n",
101 err, cp - name, pos, byte);
102 return -1;
103}
104
105static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
106{
107 u8 b[8];
108
109 put_unaligned_be64(wwn, b);
110 return snprintf(buf, len,
111 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
112 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
113}
114
115static char *tcm_qla2xxx_get_fabric_name(void)
116{
117 return "qla2xxx";
118}
119
120/*
121 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
122 */
123static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
124{
Roland Dreierd4f75b52012-06-11 18:31:31 -0700125 unsigned int i, j;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400126 u8 wwn[8];
127
128 memset(wwn, 0, sizeof(wwn));
129
130 /* Validate and store the new name */
131 for (i = 0, j = 0; i < 16; i++) {
Roland Dreierd4f75b52012-06-11 18:31:31 -0700132 int value;
133
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400134 value = hex_to_bin(*ns++);
135 if (value >= 0)
136 j = (j << 4) | value;
137 else
138 return -EINVAL;
139
140 if (i % 2) {
141 wwn[i/2] = j & 0xff;
142 j = 0;
143 }
144 }
145
146 *nm = wwn_to_u64(wwn);
147 return 0;
148}
149
150/*
151 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
152 * store_fc_host_vport_create()
153 */
154static int tcm_qla2xxx_npiv_parse_wwn(
155 const char *name,
156 size_t count,
157 u64 *wwpn,
158 u64 *wwnn)
159{
160 unsigned int cnt = count;
161 int rc;
162
163 *wwpn = 0;
164 *wwnn = 0;
165
166 /* count may include a LF at end of string */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800167 if (name[cnt-1] == '\n' || name[cnt-1] == 0)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400168 cnt--;
169
170 /* validate we have enough characters for WWPN */
171 if ((cnt != (16+1+16)) || (name[16] != ':'))
172 return -EINVAL;
173
174 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
175 if (rc != 0)
176 return rc;
177
178 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
179 if (rc != 0)
180 return rc;
181
182 return 0;
183}
184
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400185static char *tcm_qla2xxx_npiv_get_fabric_name(void)
186{
187 return "qla2xxx_npiv";
188}
189
190static u8 tcm_qla2xxx_get_fabric_proto_ident(struct se_portal_group *se_tpg)
191{
192 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
193 struct tcm_qla2xxx_tpg, se_tpg);
194 struct tcm_qla2xxx_lport *lport = tpg->lport;
195 u8 proto_id;
196
197 switch (lport->lport_proto_id) {
198 case SCSI_PROTOCOL_FCP:
199 default:
200 proto_id = fc_get_fabric_proto_ident(se_tpg);
201 break;
202 }
203
204 return proto_id;
205}
206
207static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
208{
209 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
210 struct tcm_qla2xxx_tpg, se_tpg);
211 struct tcm_qla2xxx_lport *lport = tpg->lport;
212
Roland Dreierc046aa02012-10-11 13:41:31 -0700213 return lport->lport_naa_name;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400214}
215
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400216static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
217{
218 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
219 struct tcm_qla2xxx_tpg, se_tpg);
220 return tpg->lport_tpgt;
221}
222
223static u32 tcm_qla2xxx_get_default_depth(struct se_portal_group *se_tpg)
224{
225 return 1;
226}
227
228static u32 tcm_qla2xxx_get_pr_transport_id(
229 struct se_portal_group *se_tpg,
230 struct se_node_acl *se_nacl,
231 struct t10_pr_registration *pr_reg,
232 int *format_code,
233 unsigned char *buf)
234{
235 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
236 struct tcm_qla2xxx_tpg, se_tpg);
237 struct tcm_qla2xxx_lport *lport = tpg->lport;
238 int ret = 0;
239
240 switch (lport->lport_proto_id) {
241 case SCSI_PROTOCOL_FCP:
242 default:
243 ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
244 format_code, buf);
245 break;
246 }
247
248 return ret;
249}
250
251static u32 tcm_qla2xxx_get_pr_transport_id_len(
252 struct se_portal_group *se_tpg,
253 struct se_node_acl *se_nacl,
254 struct t10_pr_registration *pr_reg,
255 int *format_code)
256{
257 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
258 struct tcm_qla2xxx_tpg, se_tpg);
259 struct tcm_qla2xxx_lport *lport = tpg->lport;
260 int ret = 0;
261
262 switch (lport->lport_proto_id) {
263 case SCSI_PROTOCOL_FCP:
264 default:
265 ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
266 format_code);
267 break;
268 }
269
270 return ret;
271}
272
273static char *tcm_qla2xxx_parse_pr_out_transport_id(
274 struct se_portal_group *se_tpg,
275 const char *buf,
276 u32 *out_tid_len,
277 char **port_nexus_ptr)
278{
279 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
280 struct tcm_qla2xxx_tpg, se_tpg);
281 struct tcm_qla2xxx_lport *lport = tpg->lport;
282 char *tid = NULL;
283
284 switch (lport->lport_proto_id) {
285 case SCSI_PROTOCOL_FCP:
286 default:
287 tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
288 port_nexus_ptr);
289 break;
290 }
291
292 return tid;
293}
294
295static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
296{
297 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
298 struct tcm_qla2xxx_tpg, se_tpg);
299
Andy Grovera309f482013-10-09 11:05:59 -0700300 return tpg->tpg_attrib.generate_node_acls;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400301}
302
303static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
304{
305 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
306 struct tcm_qla2xxx_tpg, se_tpg);
307
Andy Grovera309f482013-10-09 11:05:59 -0700308 return tpg->tpg_attrib.cache_dynamic_acls;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400309}
310
311static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
312{
313 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
314 struct tcm_qla2xxx_tpg, se_tpg);
315
Andy Grovera309f482013-10-09 11:05:59 -0700316 return tpg->tpg_attrib.demo_mode_write_protect;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400317}
318
319static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
320{
321 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
322 struct tcm_qla2xxx_tpg, se_tpg);
323
Andy Grovera309f482013-10-09 11:05:59 -0700324 return tpg->tpg_attrib.prod_mode_write_protect;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400325}
326
Andy Groverde04a8a2013-07-19 15:06:38 -0700327static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
328{
329 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
330 struct tcm_qla2xxx_tpg, se_tpg);
331
Andy Grovera309f482013-10-09 11:05:59 -0700332 return tpg->tpg_attrib.demo_mode_login_only;
Andy Groverde04a8a2013-07-19 15:06:38 -0700333}
334
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400335static struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
336 struct se_portal_group *se_tpg)
337{
338 struct tcm_qla2xxx_nacl *nacl;
339
340 nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
341 if (!nacl) {
Masanari Iida6efb3c0a2012-10-26 22:10:54 +0900342 pr_err("Unable to allocate struct tcm_qla2xxx_nacl\n");
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400343 return NULL;
344 }
345
346 return &nacl->se_node_acl;
347}
348
349static void tcm_qla2xxx_release_fabric_acl(
350 struct se_portal_group *se_tpg,
351 struct se_node_acl *se_nacl)
352{
353 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
354 struct tcm_qla2xxx_nacl, se_node_acl);
355 kfree(nacl);
356}
357
358static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
359{
360 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
361 struct tcm_qla2xxx_tpg, se_tpg);
362
363 return tpg->lport_tpgt;
364}
365
366static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
367{
368 struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
369 struct qla_tgt_mgmt_cmd, free_work);
370
371 transport_generic_free_cmd(&mcmd->se_cmd, 0);
372}
373
374/*
375 * Called from qla_target_template->free_mcmd(), and will call
376 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
377 * release callback. qla_hw_data->hardware_lock is expected to be held
378 */
379static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
380{
381 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
382 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
383}
384
385static void tcm_qla2xxx_complete_free(struct work_struct *work)
386{
387 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
388
389 transport_generic_free_cmd(&cmd->se_cmd, 0);
390}
391
392/*
393 * Called from qla_target_template->free_cmd(), and will call
394 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
395 * release callback. qla_hw_data->hardware_lock is expected to be held
396 */
397static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
398{
399 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
400 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
401}
402
403/*
404 * Called from struct target_core_fabric_ops->check_stop_free() context
405 */
406static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
407{
408 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
409}
410
411/* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
412 * fabric descriptor @se_cmd command to release
413 */
414static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
415{
416 struct qla_tgt_cmd *cmd;
417
418 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
419 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
420 struct qla_tgt_mgmt_cmd, se_cmd);
421 qlt_free_mcmd(mcmd);
422 return;
423 }
424
425 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
426 qlt_free_cmd(cmd);
427}
428
429static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
430{
431 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
432 struct scsi_qla_host *vha;
433 unsigned long flags;
434
435 BUG_ON(!sess);
436 vha = sess->vha;
437
438 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
Roland Dreier1c7b13f2012-07-16 11:04:42 -0700439 target_sess_cmd_list_set_waiting(se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400440 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
441
442 return 1;
443}
444
445static void tcm_qla2xxx_close_session(struct se_session *se_sess)
446{
447 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
448 struct scsi_qla_host *vha;
449 unsigned long flags;
450
451 BUG_ON(!sess);
452 vha = sess->vha;
453
454 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
455 qlt_unreg_sess(sess);
456 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
457}
458
459static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
460{
461 return 0;
462}
463
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400464static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
465{
466 struct qla_tgt_cmd *cmd = container_of(se_cmd,
467 struct qla_tgt_cmd, se_cmd);
468
469 cmd->bufflen = se_cmd->data_length;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700470 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400471
472 cmd->sg_cnt = se_cmd->t_data_nents;
473 cmd->sg = se_cmd->t_data_sg;
474
475 /*
476 * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
477 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
478 */
479 return qlt_rdy_to_xfer(cmd);
480}
481
482static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
483{
484 unsigned long flags;
485 /*
486 * Check for WRITE_PENDING status to determine if we need to wait for
487 * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
488 */
489 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
490 if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
491 se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
492 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
493 wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
494 3000);
495 return 0;
496 }
497 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
498
499 return 0;
500}
501
502static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
503{
504 return;
505}
506
507static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
508{
509 struct qla_tgt_cmd *cmd = container_of(se_cmd,
510 struct qla_tgt_cmd, se_cmd);
511
512 return cmd->tag;
513}
514
515static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
516{
517 return 0;
518}
519
520/*
521 * Called from process context in qla_target.c:qlt_do_work() code
522 */
523static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
524 unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
525 int data_dir, int bidi)
526{
527 struct se_cmd *se_cmd = &cmd->se_cmd;
528 struct se_session *se_sess;
529 struct qla_tgt_sess *sess;
530 int flags = TARGET_SCF_ACK_KREF;
531
532 if (bidi)
533 flags |= TARGET_SCF_BIDI_OP;
534
535 sess = cmd->sess;
536 if (!sess) {
537 pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
538 return -EINVAL;
539 }
540
541 se_sess = sess->se_sess;
542 if (!se_sess) {
543 pr_err("Unable to locate active struct se_session\n");
544 return -EINVAL;
545 }
546
Roland Dreierd6dfc862012-07-16 11:04:39 -0700547 return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400548 cmd->unpacked_lun, data_length, fcp_task_attr,
549 data_dir, flags);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400550}
551
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400552static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400553{
554 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400555
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400556 /*
557 * Ensure that the complete FCP WRITE payload has been received.
558 * Otherwise return an exception via CHECK_CONDITION status.
559 */
560 if (!cmd->write_data_transferred) {
561 /*
562 * Check if se_cmd has already been aborted via LUN_RESET, and
563 * waiting upon completion in tcm_qla2xxx_write_pending_status()
564 */
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400565 if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
566 complete(&cmd->se_cmd.t_transport_stop_comp);
567 return;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400568 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400569
Christoph Hellwigde103c92012-11-06 12:24:09 -0800570 transport_generic_request_failure(&cmd->se_cmd,
571 TCM_CHECK_CONDITION_ABORT_CMD);
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400572 return;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400573 }
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400574
575 return target_execute_cmd(&cmd->se_cmd);
576}
577
578/*
579 * Called from qla_target.c:qlt_do_ctio_completion()
580 */
581static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
582{
583 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
584 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400585}
586
587/*
588 * Called from qla_target.c:qlt_issue_task_mgmt()
589 */
Roland Dreier9389c3c2012-06-11 18:31:30 -0700590static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
591 uint8_t tmr_func, uint32_t tag)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400592{
593 struct qla_tgt_sess *sess = mcmd->sess;
594 struct se_cmd *se_cmd = &mcmd->se_cmd;
595
596 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
597 tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
598}
599
600static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
601{
602 struct qla_tgt_cmd *cmd = container_of(se_cmd,
603 struct qla_tgt_cmd, se_cmd);
604
605 cmd->bufflen = se_cmd->data_length;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700606 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400607 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
608
609 cmd->sg_cnt = se_cmd->t_data_nents;
610 cmd->sg = se_cmd->t_data_sg;
611 cmd->offset = 0;
612
613 /*
614 * Now queue completed DATA_IN the qla2xxx LLD and response ring
615 */
616 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
617 se_cmd->scsi_status);
618}
619
620static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
621{
622 struct qla_tgt_cmd *cmd = container_of(se_cmd,
623 struct qla_tgt_cmd, se_cmd);
624 int xmit_type = QLA_TGT_XMIT_STATUS;
625
626 cmd->bufflen = se_cmd->data_length;
627 cmd->sg = NULL;
628 cmd->sg_cnt = 0;
629 cmd->offset = 0;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700630 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400631 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
632
633 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
634 /*
635 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
636 * for qla_tgt_xmit_response LLD code
637 */
Roland Dreierb5aff3d2013-06-05 09:54:17 -0700638 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
639 se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
640 se_cmd->residual_count = 0;
641 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400642 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
Roland Dreierb5aff3d2013-06-05 09:54:17 -0700643 se_cmd->residual_count += se_cmd->data_length;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400644
645 cmd->bufflen = 0;
646 }
647 /*
648 * Now queue status response to qla2xxx LLD code and response ring
649 */
650 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
651}
652
Joern Engelb79fafa2013-07-03 11:22:17 -0400653static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400654{
655 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
656 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
657 struct qla_tgt_mgmt_cmd, se_cmd);
658
659 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
660 mcmd, se_tmr->function, se_tmr->response);
661 /*
662 * Do translation between TCM TM response codes and
663 * QLA2xxx FC TM response codes.
664 */
665 switch (se_tmr->response) {
666 case TMR_FUNCTION_COMPLETE:
667 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
668 break;
669 case TMR_TASK_DOES_NOT_EXIST:
670 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
671 break;
672 case TMR_FUNCTION_REJECTED:
673 mcmd->fc_tm_rsp = FC_TM_REJECT;
674 break;
675 case TMR_LUN_DOES_NOT_EXIST:
676 default:
677 mcmd->fc_tm_rsp = FC_TM_FAILED;
678 break;
679 }
680 /*
681 * Queue the TM response to QLA2xxx LLD to build a
682 * CTIO response packet.
683 */
684 qlt_xmit_tm_rsp(mcmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400685}
686
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700687static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
688{
689 struct qla_tgt_cmd *cmd = container_of(se_cmd,
690 struct qla_tgt_cmd, se_cmd);
691 struct scsi_qla_host *vha = cmd->vha;
692 struct qla_hw_data *ha = vha->hw;
693
694 if (!cmd->sg_mapped)
695 return;
696
697 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
698 cmd->sg_mapped = 0;
699}
700
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400701/* Local pointer to allocated TCM configfs fabric module */
702struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
703struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
704
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -0700705static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
706 struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400707/*
708 * Expected to be called with struct qla_hw_data->hardware_lock held
709 */
710static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
711{
712 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
713 struct se_portal_group *se_tpg = se_nacl->se_tpg;
714 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
715 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
716 struct tcm_qla2xxx_lport, lport_wwn);
717 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
718 struct tcm_qla2xxx_nacl, se_node_acl);
719 void *node;
720
721 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
722
723 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
724 WARN_ON(node && (node != se_nacl));
725
726 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
727 se_nacl, nacl->nport_wwnn, nacl->nport_id);
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -0700728 /*
729 * Now clear the se_nacl and session pointers from our HW lport lookup
730 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
731 *
732 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
733 * target_wait_for_sess_cmds() before the session waits for outstanding
734 * I/O to complete, to avoid a race between session shutdown execution
735 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
736 */
737 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400738}
739
Joern Engelaaf68b72012-05-18 13:58:23 -0700740static void tcm_qla2xxx_release_session(struct kref *kref)
741{
742 struct se_session *se_sess = container_of(kref,
743 struct se_session, sess_kref);
744
745 qlt_unreg_sess(se_sess->fabric_sess_ptr);
746}
747
748static void tcm_qla2xxx_put_session(struct se_session *se_sess)
749{
750 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
751 struct qla_hw_data *ha = sess->vha->hw;
752 unsigned long flags;
753
754 spin_lock_irqsave(&ha->hardware_lock, flags);
755 kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
756 spin_unlock_irqrestore(&ha->hardware_lock, flags);
757}
758
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400759static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
760{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800761 if (!sess)
762 return;
763
Jörn Engel08234e32013-06-12 16:27:54 -0400764 assert_spin_locked(&sess->vha->hw->hardware_lock);
765 kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400766}
767
768static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
769{
Jörn Engel08234e32013-06-12 16:27:54 -0400770 assert_spin_locked(&sess->vha->hw->hardware_lock);
771 target_sess_cmd_list_set_waiting(sess->se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400772}
773
774static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
775 struct se_portal_group *se_tpg,
776 struct config_group *group,
777 const char *name)
778{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400779 struct se_node_acl *se_nacl, *se_nacl_new;
780 struct tcm_qla2xxx_nacl *nacl;
781 u64 wwnn;
782 u32 qla2xxx_nexus_depth;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400783
784 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
785 return ERR_PTR(-EINVAL);
786
787 se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
788 if (!se_nacl_new)
789 return ERR_PTR(-ENOMEM);
790/* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
791 qla2xxx_nexus_depth = 1;
792
793 /*
794 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
795 * when converting a NodeACL from demo mode -> explict
796 */
797 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
798 name, qla2xxx_nexus_depth);
799 if (IS_ERR(se_nacl)) {
800 tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
801 return se_nacl;
802 }
803 /*
804 * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
805 */
806 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
807 nacl->nport_wwnn = wwnn;
808 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400809
810 return se_nacl;
811}
812
813static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
814{
815 struct se_portal_group *se_tpg = se_acl->se_tpg;
816 struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
817 struct tcm_qla2xxx_nacl, se_node_acl);
818
819 core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
820 kfree(nacl);
821}
822
823/* Start items for tcm_qla2xxx_tpg_attrib_cit */
824
825#define DEF_QLA_TPG_ATTRIB(name) \
826 \
827static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
828 struct se_portal_group *se_tpg, \
829 char *page) \
830{ \
831 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
832 struct tcm_qla2xxx_tpg, se_tpg); \
833 \
Andy Grovera309f482013-10-09 11:05:59 -0700834 return sprintf(page, "%u\n", tpg->tpg_attrib.name); \
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400835} \
836 \
837static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
838 struct se_portal_group *se_tpg, \
839 const char *page, \
840 size_t count) \
841{ \
842 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
843 struct tcm_qla2xxx_tpg, se_tpg); \
844 unsigned long val; \
845 int ret; \
846 \
847 ret = kstrtoul(page, 0, &val); \
848 if (ret < 0) { \
849 pr_err("kstrtoul() failed with" \
850 " ret: %d\n", ret); \
851 return -EINVAL; \
852 } \
853 ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
854 \
855 return (!ret) ? count : -EINVAL; \
856}
857
858#define DEF_QLA_TPG_ATTR_BOOL(_name) \
859 \
860static int tcm_qla2xxx_set_attrib_##_name( \
861 struct tcm_qla2xxx_tpg *tpg, \
862 unsigned long val) \
863{ \
864 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
865 \
866 if ((val != 0) && (val != 1)) { \
867 pr_err("Illegal boolean value %lu\n", val); \
868 return -EINVAL; \
869 } \
870 \
871 a->_name = val; \
872 return 0; \
873}
874
875#define QLA_TPG_ATTR(_name, _mode) \
876 TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
877
878/*
879 * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
880 */
881DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
882DEF_QLA_TPG_ATTRIB(generate_node_acls);
883QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
884
885/*
886 Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
887 */
888DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
889DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
890QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
891
892/*
893 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
894 */
895DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
896DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
897QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
898
899/*
900 * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
901 */
902DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
903DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
904QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
905
Andy Groverde04a8a2013-07-19 15:06:38 -0700906/*
907 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_login_only
908 */
909DEF_QLA_TPG_ATTR_BOOL(demo_mode_login_only);
910DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
911QLA_TPG_ATTR(demo_mode_login_only, S_IRUGO | S_IWUSR);
912
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400913static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
914 &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
915 &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
916 &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
917 &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
Andy Groverde04a8a2013-07-19 15:06:38 -0700918 &tcm_qla2xxx_tpg_attrib_demo_mode_login_only.attr,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400919 NULL,
920};
921
922/* End items for tcm_qla2xxx_tpg_attrib_cit */
923
924static ssize_t tcm_qla2xxx_tpg_show_enable(
925 struct se_portal_group *se_tpg,
926 char *page)
927{
928 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
929 struct tcm_qla2xxx_tpg, se_tpg);
930
931 return snprintf(page, PAGE_SIZE, "%d\n",
932 atomic_read(&tpg->lport_tpg_enabled));
933}
934
Nicholas Bellinger7474f522014-02-19 16:53:07 -0800935static void tcm_qla2xxx_depend_tpg(struct work_struct *work)
936{
937 struct tcm_qla2xxx_tpg *base_tpg = container_of(work,
938 struct tcm_qla2xxx_tpg, tpg_base_work);
939 struct se_portal_group *se_tpg = &base_tpg->se_tpg;
940 struct scsi_qla_host *base_vha = base_tpg->lport->qla_vha;
941
942 if (!configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
943 &se_tpg->tpg_group.cg_item)) {
944 atomic_set(&base_tpg->lport_tpg_enabled, 1);
945 qlt_enable_vha(base_vha);
946 }
947 complete(&base_tpg->tpg_base_comp);
948}
949
950static void tcm_qla2xxx_undepend_tpg(struct work_struct *work)
951{
952 struct tcm_qla2xxx_tpg *base_tpg = container_of(work,
953 struct tcm_qla2xxx_tpg, tpg_base_work);
954 struct se_portal_group *se_tpg = &base_tpg->se_tpg;
955 struct scsi_qla_host *base_vha = base_tpg->lport->qla_vha;
956
957 if (!qlt_stop_phase1(base_vha->vha_tgt.qla_tgt)) {
958 atomic_set(&base_tpg->lport_tpg_enabled, 0);
959 configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
960 &se_tpg->tpg_group.cg_item);
961 }
962 complete(&base_tpg->tpg_base_comp);
963}
964
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400965static ssize_t tcm_qla2xxx_tpg_store_enable(
966 struct se_portal_group *se_tpg,
967 const char *page,
968 size_t count)
969{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400970 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
971 struct tcm_qla2xxx_tpg, se_tpg);
972 unsigned long op;
973 int rc;
974
975 rc = kstrtoul(page, 0, &op);
976 if (rc < 0) {
977 pr_err("kstrtoul() returned %d\n", rc);
978 return -EINVAL;
979 }
980 if ((op != 1) && (op != 0)) {
981 pr_err("Illegal value for tpg_enable: %lu\n", op);
982 return -EINVAL;
983 }
Nicholas Bellinger7474f522014-02-19 16:53:07 -0800984 if (op) {
985 if (atomic_read(&tpg->lport_tpg_enabled))
986 return -EEXIST;
987
988 INIT_WORK(&tpg->tpg_base_work, tcm_qla2xxx_depend_tpg);
989 } else {
990 if (!atomic_read(&tpg->lport_tpg_enabled))
991 return count;
992
993 INIT_WORK(&tpg->tpg_base_work, tcm_qla2xxx_undepend_tpg);
994 }
995 init_completion(&tpg->tpg_base_comp);
996 schedule_work(&tpg->tpg_base_work);
997 wait_for_completion(&tpg->tpg_base_comp);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400998
999 if (op) {
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001000 if (!atomic_read(&tpg->lport_tpg_enabled))
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001001 return -ENODEV;
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001002 } else {
1003 if (atomic_read(&tpg->lport_tpg_enabled))
1004 return -EPERM;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001005 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001006 return count;
1007}
1008
1009TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
1010
1011static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
1012 &tcm_qla2xxx_tpg_enable.attr,
1013 NULL,
1014};
1015
1016static struct se_portal_group *tcm_qla2xxx_make_tpg(
1017 struct se_wwn *wwn,
1018 struct config_group *group,
1019 const char *name)
1020{
1021 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1022 struct tcm_qla2xxx_lport, lport_wwn);
1023 struct tcm_qla2xxx_tpg *tpg;
1024 unsigned long tpgt;
1025 int ret;
1026
1027 if (strstr(name, "tpgt_") != name)
1028 return ERR_PTR(-EINVAL);
1029 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1030 return ERR_PTR(-EINVAL);
1031
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001032 if ((tpgt != 1)) {
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001033 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1034 return ERR_PTR(-ENOSYS);
1035 }
1036
1037 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1038 if (!tpg) {
1039 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1040 return ERR_PTR(-ENOMEM);
1041 }
1042 tpg->lport = lport;
1043 tpg->lport_tpgt = tpgt;
1044 /*
1045 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1046 * NodeACLs
1047 */
Andy Grovera309f482013-10-09 11:05:59 -07001048 tpg->tpg_attrib.generate_node_acls = 1;
1049 tpg->tpg_attrib.demo_mode_write_protect = 1;
1050 tpg->tpg_attrib.cache_dynamic_acls = 1;
1051 tpg->tpg_attrib.demo_mode_login_only = 1;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001052
1053 ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
1054 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1055 if (ret < 0) {
1056 kfree(tpg);
1057 return NULL;
1058 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001059
1060 lport->tpg_1 = tpg;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001061
1062 return &tpg->se_tpg;
1063}
1064
1065static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1066{
1067 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1068 struct tcm_qla2xxx_tpg, se_tpg);
1069 struct tcm_qla2xxx_lport *lport = tpg->lport;
1070 struct scsi_qla_host *vha = lport->qla_vha;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001071 /*
1072 * Call into qla2x_target.c LLD logic to shutdown the active
1073 * FC Nexuses and disable target mode operation for this qla_hw_data
1074 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001075 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
1076 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001077
1078 core_tpg_deregister(se_tpg);
1079 /*
1080 * Clear local TPG=1 pointer for non NPIV mode.
1081 */
Nicholas Bellinger394d62b2014-02-19 16:52:15 -08001082 lport->tpg_1 = NULL;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001083 kfree(tpg);
1084}
1085
Nicholas Bellinger394d62b2014-02-19 16:52:15 -08001086static ssize_t tcm_qla2xxx_npiv_tpg_show_enable(
1087 struct se_portal_group *se_tpg,
1088 char *page)
1089{
1090 return tcm_qla2xxx_tpg_show_enable(se_tpg, page);
1091}
1092
1093static ssize_t tcm_qla2xxx_npiv_tpg_store_enable(
1094 struct se_portal_group *se_tpg,
1095 const char *page,
1096 size_t count)
1097{
1098 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1099 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1100 struct tcm_qla2xxx_lport, lport_wwn);
1101 struct scsi_qla_host *vha = lport->qla_vha;
1102 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1103 struct tcm_qla2xxx_tpg, se_tpg);
1104 unsigned long op;
1105 int rc;
1106
1107 rc = kstrtoul(page, 0, &op);
1108 if (rc < 0) {
1109 pr_err("kstrtoul() returned %d\n", rc);
1110 return -EINVAL;
1111 }
1112 if ((op != 1) && (op != 0)) {
1113 pr_err("Illegal value for tpg_enable: %lu\n", op);
1114 return -EINVAL;
1115 }
1116 if (op) {
1117 if (atomic_read(&tpg->lport_tpg_enabled))
1118 return -EEXIST;
1119
1120 atomic_set(&tpg->lport_tpg_enabled, 1);
1121 qlt_enable_vha(vha);
1122 } else {
1123 if (!atomic_read(&tpg->lport_tpg_enabled))
1124 return count;
1125
1126 atomic_set(&tpg->lport_tpg_enabled, 0);
1127 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1128 }
1129
1130 return count;
1131}
1132
1133TF_TPG_BASE_ATTR(tcm_qla2xxx_npiv, enable, S_IRUGO | S_IWUSR);
1134
1135static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
1136 &tcm_qla2xxx_npiv_tpg_enable.attr,
1137 NULL,
1138};
1139
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001140static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1141 struct se_wwn *wwn,
1142 struct config_group *group,
1143 const char *name)
1144{
1145 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1146 struct tcm_qla2xxx_lport, lport_wwn);
1147 struct tcm_qla2xxx_tpg *tpg;
1148 unsigned long tpgt;
1149 int ret;
1150
1151 if (strstr(name, "tpgt_") != name)
1152 return ERR_PTR(-EINVAL);
1153 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1154 return ERR_PTR(-EINVAL);
1155
1156 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1157 if (!tpg) {
1158 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1159 return ERR_PTR(-ENOMEM);
1160 }
1161 tpg->lport = lport;
1162 tpg->lport_tpgt = tpgt;
1163
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001164 /*
1165 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1166 * NodeACLs
1167 */
1168 tpg->tpg_attrib.generate_node_acls = 1;
1169 tpg->tpg_attrib.demo_mode_write_protect = 1;
1170 tpg->tpg_attrib.cache_dynamic_acls = 1;
1171 tpg->tpg_attrib.demo_mode_login_only = 1;
1172
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001173 ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
1174 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1175 if (ret < 0) {
1176 kfree(tpg);
1177 return NULL;
1178 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001179 lport->tpg_1 = tpg;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001180 return &tpg->se_tpg;
1181}
1182
1183/*
1184 * Expected to be called with struct qla_hw_data->hardware_lock held
1185 */
1186static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
1187 scsi_qla_host_t *vha,
1188 const uint8_t *s_id)
1189{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001190 struct tcm_qla2xxx_lport *lport;
1191 struct se_node_acl *se_nacl;
1192 struct tcm_qla2xxx_nacl *nacl;
1193 u32 key;
1194
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001195 lport = vha->vha_tgt.target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001196 if (!lport) {
1197 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1198 dump_stack();
1199 return NULL;
1200 }
1201
1202 key = (((unsigned long)s_id[0] << 16) |
1203 ((unsigned long)s_id[1] << 8) |
1204 (unsigned long)s_id[2]);
1205 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1206
1207 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1208 if (!se_nacl) {
1209 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1210 return NULL;
1211 }
1212 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1213 se_nacl, se_nacl->initiatorname);
1214
1215 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1216 if (!nacl->qla_tgt_sess) {
1217 pr_err("Unable to locate struct qla_tgt_sess\n");
1218 return NULL;
1219 }
1220
1221 return nacl->qla_tgt_sess;
1222}
1223
1224/*
1225 * Expected to be called with struct qla_hw_data->hardware_lock held
1226 */
1227static void tcm_qla2xxx_set_sess_by_s_id(
1228 struct tcm_qla2xxx_lport *lport,
1229 struct se_node_acl *new_se_nacl,
1230 struct tcm_qla2xxx_nacl *nacl,
1231 struct se_session *se_sess,
1232 struct qla_tgt_sess *qla_tgt_sess,
1233 uint8_t *s_id)
1234{
1235 u32 key;
1236 void *slot;
1237 int rc;
1238
1239 key = (((unsigned long)s_id[0] << 16) |
1240 ((unsigned long)s_id[1] << 8) |
1241 (unsigned long)s_id[2]);
1242 pr_debug("set_sess_by_s_id: %06x\n", key);
1243
1244 slot = btree_lookup32(&lport->lport_fcport_map, key);
1245 if (!slot) {
1246 if (new_se_nacl) {
1247 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1248 nacl->nport_id = key;
1249 rc = btree_insert32(&lport->lport_fcport_map, key,
1250 new_se_nacl, GFP_ATOMIC);
1251 if (rc)
1252 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1253 (int)key);
1254 } else {
1255 pr_debug("Wiping nonexisting fc_port entry\n");
1256 }
1257
1258 qla_tgt_sess->se_sess = se_sess;
1259 nacl->qla_tgt_sess = qla_tgt_sess;
1260 return;
1261 }
1262
1263 if (nacl->qla_tgt_sess) {
1264 if (new_se_nacl == NULL) {
1265 pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
1266 btree_remove32(&lport->lport_fcport_map, key);
1267 nacl->qla_tgt_sess = NULL;
1268 return;
1269 }
1270 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
1271 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1272 qla_tgt_sess->se_sess = se_sess;
1273 nacl->qla_tgt_sess = qla_tgt_sess;
1274 return;
1275 }
1276
1277 if (new_se_nacl == NULL) {
1278 pr_debug("Clearing existing fc_port entry\n");
1279 btree_remove32(&lport->lport_fcport_map, key);
1280 return;
1281 }
1282
1283 pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
1284 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1285 qla_tgt_sess->se_sess = se_sess;
1286 nacl->qla_tgt_sess = qla_tgt_sess;
1287
1288 pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
1289 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1290}
1291
1292/*
1293 * Expected to be called with struct qla_hw_data->hardware_lock held
1294 */
1295static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
1296 scsi_qla_host_t *vha,
1297 const uint16_t loop_id)
1298{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001299 struct tcm_qla2xxx_lport *lport;
1300 struct se_node_acl *se_nacl;
1301 struct tcm_qla2xxx_nacl *nacl;
1302 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1303
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001304 lport = vha->vha_tgt.target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001305 if (!lport) {
1306 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1307 dump_stack();
1308 return NULL;
1309 }
1310
1311 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1312
1313 fc_loopid = lport->lport_loopid_map + loop_id;
1314 se_nacl = fc_loopid->se_nacl;
1315 if (!se_nacl) {
1316 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1317 loop_id);
1318 return NULL;
1319 }
1320
1321 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1322
1323 if (!nacl->qla_tgt_sess) {
1324 pr_err("Unable to locate struct qla_tgt_sess\n");
1325 return NULL;
1326 }
1327
1328 return nacl->qla_tgt_sess;
1329}
1330
1331/*
1332 * Expected to be called with struct qla_hw_data->hardware_lock held
1333 */
1334static void tcm_qla2xxx_set_sess_by_loop_id(
1335 struct tcm_qla2xxx_lport *lport,
1336 struct se_node_acl *new_se_nacl,
1337 struct tcm_qla2xxx_nacl *nacl,
1338 struct se_session *se_sess,
1339 struct qla_tgt_sess *qla_tgt_sess,
1340 uint16_t loop_id)
1341{
1342 struct se_node_acl *saved_nacl;
1343 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1344
1345 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1346
1347 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1348 lport->lport_loopid_map)[loop_id];
1349
1350 saved_nacl = fc_loopid->se_nacl;
1351 if (!saved_nacl) {
1352 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1353 fc_loopid->se_nacl = new_se_nacl;
1354 if (qla_tgt_sess->se_sess != se_sess)
1355 qla_tgt_sess->se_sess = se_sess;
1356 if (nacl->qla_tgt_sess != qla_tgt_sess)
1357 nacl->qla_tgt_sess = qla_tgt_sess;
1358 return;
1359 }
1360
1361 if (nacl->qla_tgt_sess) {
1362 if (new_se_nacl == NULL) {
1363 pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1364 fc_loopid->se_nacl = NULL;
1365 nacl->qla_tgt_sess = NULL;
1366 return;
1367 }
1368
1369 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1370 fc_loopid->se_nacl = new_se_nacl;
1371 if (qla_tgt_sess->se_sess != se_sess)
1372 qla_tgt_sess->se_sess = se_sess;
1373 if (nacl->qla_tgt_sess != qla_tgt_sess)
1374 nacl->qla_tgt_sess = qla_tgt_sess;
1375 return;
1376 }
1377
1378 if (new_se_nacl == NULL) {
1379 pr_debug("Clearing fc_loopid->se_nacl\n");
1380 fc_loopid->se_nacl = NULL;
1381 return;
1382 }
1383
1384 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
1385 fc_loopid->se_nacl = new_se_nacl;
1386 if (qla_tgt_sess->se_sess != se_sess)
1387 qla_tgt_sess->se_sess = se_sess;
1388 if (nacl->qla_tgt_sess != qla_tgt_sess)
1389 nacl->qla_tgt_sess = qla_tgt_sess;
1390
1391 pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1392 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1393}
1394
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -07001395/*
1396 * Should always be called with qla_hw_data->hardware_lock held.
1397 */
1398static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1399 struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
1400{
1401 struct se_session *se_sess = sess->se_sess;
1402 unsigned char be_sid[3];
1403
1404 be_sid[0] = sess->s_id.b.domain;
1405 be_sid[1] = sess->s_id.b.area;
1406 be_sid[2] = sess->s_id.b.al_pa;
1407
1408 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1409 sess, be_sid);
1410 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1411 sess, sess->loop_id);
1412}
1413
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001414static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
1415{
1416 struct qla_tgt *tgt = sess->tgt;
1417 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001418 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001419 struct se_session *se_sess;
1420 struct se_node_acl *se_nacl;
1421 struct tcm_qla2xxx_lport *lport;
1422 struct tcm_qla2xxx_nacl *nacl;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001423
1424 BUG_ON(in_interrupt());
1425
1426 se_sess = sess->se_sess;
1427 if (!se_sess) {
1428 pr_err("struct qla_tgt_sess->se_sess is NULL\n");
1429 dump_stack();
1430 return;
1431 }
1432 se_nacl = se_sess->se_node_acl;
1433 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1434
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001435 lport = vha->vha_tgt.target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001436 if (!lport) {
1437 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1438 dump_stack();
1439 return;
1440 }
Joern Engelbe646c2d2013-05-15 00:44:07 -07001441 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001442
1443 transport_deregister_session_configfs(sess->se_sess);
1444 transport_deregister_session(sess->se_sess);
1445}
1446
1447/*
1448 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1449 * to locate struct se_node_acl
1450 */
1451static int tcm_qla2xxx_check_initiator_node_acl(
1452 scsi_qla_host_t *vha,
1453 unsigned char *fc_wwpn,
1454 void *qla_tgt_sess,
1455 uint8_t *s_id,
1456 uint16_t loop_id)
1457{
1458 struct qla_hw_data *ha = vha->hw;
1459 struct tcm_qla2xxx_lport *lport;
1460 struct tcm_qla2xxx_tpg *tpg;
1461 struct tcm_qla2xxx_nacl *nacl;
1462 struct se_portal_group *se_tpg;
1463 struct se_node_acl *se_nacl;
1464 struct se_session *se_sess;
1465 struct qla_tgt_sess *sess = qla_tgt_sess;
1466 unsigned char port_name[36];
1467 unsigned long flags;
1468
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001469 lport = vha->vha_tgt.target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001470 if (!lport) {
1471 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1472 dump_stack();
1473 return -EINVAL;
1474 }
1475 /*
1476 * Locate the TPG=1 reference..
1477 */
1478 tpg = lport->tpg_1;
1479 if (!tpg) {
1480 pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1481 return -EINVAL;
1482 }
1483 se_tpg = &tpg->se_tpg;
1484
Nicholas Bellingere70beee2014-04-02 12:52:38 -07001485 se_sess = transport_init_session(TARGET_PROT_NORMAL);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001486 if (IS_ERR(se_sess)) {
1487 pr_err("Unable to initialize struct se_session\n");
1488 return PTR_ERR(se_sess);
1489 }
1490 /*
1491 * Format the FCP Initiator port_name into colon seperated values to
1492 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1493 */
1494 memset(&port_name, 0, 36);
1495 snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1496 fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
1497 fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
1498 /*
1499 * Locate our struct se_node_acl either from an explict NodeACL created
1500 * via ConfigFS, or via running in TPG demo mode.
1501 */
1502 se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
1503 port_name);
1504 if (!se_sess->se_node_acl) {
1505 transport_free_session(se_sess);
1506 return -EINVAL;
1507 }
1508 se_nacl = se_sess->se_node_acl;
1509 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1510 /*
1511 * And now setup the new se_nacl and session pointers into our HW lport
1512 * mappings for fabric S_ID and LOOP_ID.
1513 */
1514 spin_lock_irqsave(&ha->hardware_lock, flags);
1515 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
1516 qla_tgt_sess, s_id);
1517 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
1518 qla_tgt_sess, loop_id);
1519 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1520 /*
1521 * Finally register the new FC Nexus with TCM
1522 */
1523 __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
1524
1525 return 0;
1526}
1527
Roland Dreierc8292d12012-10-11 13:41:32 -07001528static void tcm_qla2xxx_update_sess(struct qla_tgt_sess *sess, port_id_t s_id,
1529 uint16_t loop_id, bool conf_compl_supported)
1530{
1531 struct qla_tgt *tgt = sess->tgt;
1532 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001533 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1534 struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
Roland Dreierc8292d12012-10-11 13:41:32 -07001535 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1536 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1537 struct tcm_qla2xxx_nacl, se_node_acl);
1538 u32 key;
1539
1540
1541 if (sess->loop_id != loop_id || sess->s_id.b24 != s_id.b24)
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001542 pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1543 sess, sess->port_name,
1544 sess->loop_id, loop_id, sess->s_id.b.domain,
1545 sess->s_id.b.area, sess->s_id.b.al_pa, s_id.b.domain,
1546 s_id.b.area, s_id.b.al_pa);
Roland Dreierc8292d12012-10-11 13:41:32 -07001547
1548 if (sess->loop_id != loop_id) {
1549 /*
1550 * Because we can shuffle loop IDs around and we
1551 * update different sessions non-atomically, we might
1552 * have overwritten this session's old loop ID
1553 * already, and we might end up overwriting some other
1554 * session that will be updated later. So we have to
1555 * be extra careful and we can't warn about those things...
1556 */
1557 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1558 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1559
1560 lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1561
1562 sess->loop_id = loop_id;
1563 }
1564
1565 if (sess->s_id.b24 != s_id.b24) {
1566 key = (((u32) sess->s_id.b.domain << 16) |
1567 ((u32) sess->s_id.b.area << 8) |
1568 ((u32) sess->s_id.b.al_pa));
1569
1570 if (btree_lookup32(&lport->lport_fcport_map, key))
1571 WARN(btree_remove32(&lport->lport_fcport_map, key) != se_nacl,
1572 "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1573 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1574 else
1575 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1576 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1577
1578 key = (((u32) s_id.b.domain << 16) |
1579 ((u32) s_id.b.area << 8) |
1580 ((u32) s_id.b.al_pa));
1581
1582 if (btree_lookup32(&lport->lport_fcport_map, key)) {
1583 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1584 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1585 btree_update32(&lport->lport_fcport_map, key, se_nacl);
1586 } else {
1587 btree_insert32(&lport->lport_fcport_map, key, se_nacl, GFP_ATOMIC);
1588 }
1589
1590 sess->s_id = s_id;
1591 nacl->nport_id = key;
1592 }
1593
1594 sess->conf_compl_supported = conf_compl_supported;
1595}
1596
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001597/*
1598 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1599 */
1600static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1601 .handle_cmd = tcm_qla2xxx_handle_cmd,
1602 .handle_data = tcm_qla2xxx_handle_data,
1603 .handle_tmr = tcm_qla2xxx_handle_tmr,
1604 .free_cmd = tcm_qla2xxx_free_cmd,
1605 .free_mcmd = tcm_qla2xxx_free_mcmd,
1606 .free_session = tcm_qla2xxx_free_session,
Roland Dreierc8292d12012-10-11 13:41:32 -07001607 .update_sess = tcm_qla2xxx_update_sess,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001608 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1609 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1610 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1611 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1612 .put_sess = tcm_qla2xxx_put_sess,
1613 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1614};
1615
1616static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1617{
1618 int rc;
1619
1620 rc = btree_init32(&lport->lport_fcport_map);
1621 if (rc) {
1622 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1623 return rc;
1624 }
1625
1626 lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
1627 65536);
1628 if (!lport->lport_loopid_map) {
1629 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1630 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1631 btree_destroy32(&lport->lport_fcport_map);
1632 return -ENOMEM;
1633 }
1634 memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
1635 * 65536);
1636 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1637 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1638 return 0;
1639}
1640
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001641static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
1642 void *target_lport_ptr,
1643 u64 npiv_wwpn, u64 npiv_wwnn)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001644{
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001645 struct qla_hw_data *ha = vha->hw;
1646 struct tcm_qla2xxx_lport *lport =
1647 (struct tcm_qla2xxx_lport *)target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001648 /*
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001649 * Setup tgt_ops, local pointer to vha and target_lport_ptr
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001650 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001651 ha->tgt.tgt_ops = &tcm_qla2xxx_template;
1652 vha->vha_tgt.target_lport_ptr = target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001653 lport->qla_vha = vha;
1654
1655 return 0;
1656}
1657
1658static struct se_wwn *tcm_qla2xxx_make_lport(
1659 struct target_fabric_configfs *tf,
1660 struct config_group *group,
1661 const char *name)
1662{
1663 struct tcm_qla2xxx_lport *lport;
1664 u64 wwpn;
1665 int ret = -ENODEV;
1666
1667 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1668 return ERR_PTR(-EINVAL);
1669
1670 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1671 if (!lport) {
1672 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1673 return ERR_PTR(-ENOMEM);
1674 }
1675 lport->lport_wwpn = wwpn;
1676 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1677 wwpn);
Roland Dreierc046aa02012-10-11 13:41:31 -07001678 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001679
1680 ret = tcm_qla2xxx_init_lport(lport);
1681 if (ret != 0)
1682 goto out;
1683
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001684 ret = qlt_lport_register(lport, wwpn, 0, 0,
1685 tcm_qla2xxx_lport_register_cb);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001686 if (ret != 0)
1687 goto out_lport;
1688
1689 return &lport->lport_wwn;
1690out_lport:
1691 vfree(lport->lport_loopid_map);
1692 btree_destroy32(&lport->lport_fcport_map);
1693out:
1694 kfree(lport);
1695 return ERR_PTR(ret);
1696}
1697
1698static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1699{
1700 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1701 struct tcm_qla2xxx_lport, lport_wwn);
1702 struct scsi_qla_host *vha = lport->qla_vha;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001703 struct se_node_acl *node;
1704 u32 key = 0;
1705
1706 /*
1707 * Call into qla2x_target.c LLD logic to complete the
1708 * shutdown of struct qla_tgt after the call to
1709 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1710 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001711 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
1712 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001713
1714 qlt_lport_deregister(vha);
1715
1716 vfree(lport->lport_loopid_map);
1717 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1718 btree_remove32(&lport->lport_fcport_map, key);
1719 btree_destroy32(&lport->lport_fcport_map);
1720 kfree(lport);
1721}
1722
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001723static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
1724 void *target_lport_ptr,
1725 u64 npiv_wwpn, u64 npiv_wwnn)
1726{
1727 struct fc_vport *vport;
1728 struct Scsi_Host *sh = base_vha->host;
1729 struct scsi_qla_host *npiv_vha;
1730 struct tcm_qla2xxx_lport *lport =
1731 (struct tcm_qla2xxx_lport *)target_lport_ptr;
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001732 struct tcm_qla2xxx_lport *base_lport =
1733 (struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
1734 struct tcm_qla2xxx_tpg *base_tpg;
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001735 struct fc_vport_identifiers vport_id;
1736
1737 if (!qla_tgt_mode_enabled(base_vha)) {
1738 pr_err("qla2xxx base_vha not enabled for target mode\n");
1739 return -EPERM;
1740 }
1741
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001742 if (!base_lport || !base_lport->tpg_1 ||
1743 !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
1744 pr_err("qla2xxx base_lport or tpg_1 not available\n");
1745 return -EPERM;
1746 }
1747 base_tpg = base_lport->tpg_1;
1748
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001749 memset(&vport_id, 0, sizeof(vport_id));
1750 vport_id.port_name = npiv_wwpn;
1751 vport_id.node_name = npiv_wwnn;
1752 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
1753 vport_id.vport_type = FC_PORTTYPE_NPIV;
1754 vport_id.disable = false;
1755
1756 vport = fc_vport_create(sh, 0, &vport_id);
1757 if (!vport) {
1758 pr_err("fc_vport_create failed for qla2xxx_npiv\n");
1759 return -ENODEV;
1760 }
1761 /*
1762 * Setup local pointer to NPIV vhba + target_lport_ptr
1763 */
1764 npiv_vha = (struct scsi_qla_host *)vport->dd_data;
1765 npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1766 lport->qla_vha = npiv_vha;
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001767 scsi_host_get(npiv_vha->host);
1768 return 0;
1769}
1770
1771
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001772static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1773 struct target_fabric_configfs *tf,
1774 struct config_group *group,
1775 const char *name)
1776{
1777 struct tcm_qla2xxx_lport *lport;
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001778 u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
1779 char *p, tmp[128];
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001780 int ret;
1781
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001782 snprintf(tmp, 128, "%s", name);
1783
1784 p = strchr(tmp, '@');
1785 if (!p) {
1786 pr_err("Unable to locate NPIV '@' seperator\n");
1787 return ERR_PTR(-EINVAL);
1788 }
1789 *p++ = '\0';
1790
1791 if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
1792 return ERR_PTR(-EINVAL);
1793
1794 if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
1795 &npiv_wwpn, &npiv_wwnn) < 0)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001796 return ERR_PTR(-EINVAL);
1797
1798 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1799 if (!lport) {
1800 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1801 return ERR_PTR(-ENOMEM);
1802 }
1803 lport->lport_npiv_wwpn = npiv_wwpn;
1804 lport->lport_npiv_wwnn = npiv_wwnn;
Roland Dreierc046aa02012-10-11 13:41:31 -07001805 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001806
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001807 ret = tcm_qla2xxx_init_lport(lport);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001808 if (ret != 0)
1809 goto out;
1810
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001811 ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
1812 tcm_qla2xxx_lport_register_npiv_cb);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001813 if (ret != 0)
1814 goto out_lport;
1815
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001816 return &lport->lport_wwn;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001817out_lport:
1818 vfree(lport->lport_loopid_map);
1819 btree_destroy32(&lport->lport_fcport_map);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001820out:
1821 kfree(lport);
1822 return ERR_PTR(ret);
1823}
1824
1825static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1826{
1827 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1828 struct tcm_qla2xxx_lport, lport_wwn);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001829 struct scsi_qla_host *npiv_vha = lport->qla_vha;
1830 struct qla_hw_data *ha = npiv_vha->hw;
1831 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1832
1833 scsi_host_put(npiv_vha->host);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001834 /*
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001835 * Notify libfc that we want to release the vha->fc_vport
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001836 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001837 fc_vport_terminate(npiv_vha->fc_vport);
1838 scsi_host_put(base_vha->host);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001839 kfree(lport);
1840}
1841
1842
1843static ssize_t tcm_qla2xxx_wwn_show_attr_version(
1844 struct target_fabric_configfs *tf,
1845 char *page)
1846{
1847 return sprintf(page,
1848 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
1849 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1850 utsname()->machine);
1851}
1852
1853TF_WWN_ATTR_RO(tcm_qla2xxx, version);
1854
1855static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1856 &tcm_qla2xxx_wwn_version.attr,
1857 NULL,
1858};
1859
1860static struct target_core_fabric_ops tcm_qla2xxx_ops = {
1861 .get_fabric_name = tcm_qla2xxx_get_fabric_name,
1862 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1863 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1864 .tpg_get_tag = tcm_qla2xxx_get_tag,
1865 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1866 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1867 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1868 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1869 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1870 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1871 .tpg_check_demo_mode_write_protect =
1872 tcm_qla2xxx_check_demo_write_protect,
1873 .tpg_check_prod_mode_write_protect =
1874 tcm_qla2xxx_check_prod_write_protect,
Andy Groverde04a8a2013-07-19 15:06:38 -07001875 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001876 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1877 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1878 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001879 .check_stop_free = tcm_qla2xxx_check_stop_free,
1880 .release_cmd = tcm_qla2xxx_release_cmd,
Joern Engelaaf68b72012-05-18 13:58:23 -07001881 .put_session = tcm_qla2xxx_put_session,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001882 .shutdown_session = tcm_qla2xxx_shutdown_session,
1883 .close_session = tcm_qla2xxx_close_session,
1884 .sess_get_index = tcm_qla2xxx_sess_get_index,
1885 .sess_get_initiator_sid = NULL,
1886 .write_pending = tcm_qla2xxx_write_pending,
1887 .write_pending_status = tcm_qla2xxx_write_pending_status,
1888 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1889 .get_task_tag = tcm_qla2xxx_get_task_tag,
1890 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1891 .queue_data_in = tcm_qla2xxx_queue_data_in,
1892 .queue_status = tcm_qla2xxx_queue_status,
1893 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001894 .aborted_task = tcm_qla2xxx_aborted_task,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001895 /*
1896 * Setup function pointers for generic logic in
1897 * target_core_fabric_configfs.c
1898 */
1899 .fabric_make_wwn = tcm_qla2xxx_make_lport,
1900 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
1901 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
1902 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1903 .fabric_post_link = NULL,
1904 .fabric_pre_unlink = NULL,
1905 .fabric_make_np = NULL,
1906 .fabric_drop_np = NULL,
1907 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1908 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1909};
1910
1911static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1912 .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
1913 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
Nicholas Bellinger84197a32014-01-30 09:52:21 -08001914 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001915 .tpg_get_tag = tcm_qla2xxx_get_tag,
1916 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1917 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1918 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1919 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001920 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1921 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1922 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
1923 .tpg_check_prod_mode_write_protect =
1924 tcm_qla2xxx_check_prod_write_protect,
Andy Groverde04a8a2013-07-19 15:06:38 -07001925 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001926 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1927 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1928 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001929 .check_stop_free = tcm_qla2xxx_check_stop_free,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001930 .release_cmd = tcm_qla2xxx_release_cmd,
Joern Engelaaf68b72012-05-18 13:58:23 -07001931 .put_session = tcm_qla2xxx_put_session,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001932 .shutdown_session = tcm_qla2xxx_shutdown_session,
1933 .close_session = tcm_qla2xxx_close_session,
1934 .sess_get_index = tcm_qla2xxx_sess_get_index,
1935 .sess_get_initiator_sid = NULL,
1936 .write_pending = tcm_qla2xxx_write_pending,
1937 .write_pending_status = tcm_qla2xxx_write_pending_status,
1938 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1939 .get_task_tag = tcm_qla2xxx_get_task_tag,
1940 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1941 .queue_data_in = tcm_qla2xxx_queue_data_in,
1942 .queue_status = tcm_qla2xxx_queue_status,
1943 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07001944 .aborted_task = tcm_qla2xxx_aborted_task,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001945 /*
1946 * Setup function pointers for generic logic in
1947 * target_core_fabric_configfs.c
1948 */
1949 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
1950 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
1951 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
1952 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1953 .fabric_post_link = NULL,
1954 .fabric_pre_unlink = NULL,
1955 .fabric_make_np = NULL,
1956 .fabric_drop_np = NULL,
1957 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1958 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1959};
1960
1961static int tcm_qla2xxx_register_configfs(void)
1962{
1963 struct target_fabric_configfs *fabric, *npiv_fabric;
1964 int ret;
1965
1966 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
1967 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1968 utsname()->machine);
1969 /*
1970 * Register the top level struct config_item_type with TCM core
1971 */
1972 fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
1973 if (IS_ERR(fabric)) {
1974 pr_err("target_fabric_configfs_init() failed\n");
1975 return PTR_ERR(fabric);
1976 }
1977 /*
1978 * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
1979 */
1980 fabric->tf_ops = tcm_qla2xxx_ops;
1981 /*
1982 * Setup default attribute lists for various fabric->tf_cit_tmpl
1983 */
Andy Groverd80e224d2013-10-09 11:05:56 -07001984 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1985 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
1986 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs =
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001987 tcm_qla2xxx_tpg_attrib_attrs;
Andy Groverd80e224d2013-10-09 11:05:56 -07001988 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
1989 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
1990 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1991 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1992 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1993 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001994 /*
1995 * Register the fabric for use within TCM
1996 */
1997 ret = target_fabric_configfs_register(fabric);
1998 if (ret < 0) {
1999 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
2000 return ret;
2001 }
2002 /*
2003 * Setup our local pointer to *fabric
2004 */
2005 tcm_qla2xxx_fabric_configfs = fabric;
2006 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
2007
2008 /*
2009 * Register the top level struct config_item_type for NPIV with TCM core
2010 */
2011 npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
2012 if (IS_ERR(npiv_fabric)) {
2013 pr_err("target_fabric_configfs_init() failed\n");
2014 ret = PTR_ERR(npiv_fabric);
2015 goto out_fabric;
2016 }
2017 /*
2018 * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
2019 */
2020 npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
2021 /*
2022 * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
2023 */
Andy Groverd80e224d2013-10-09 11:05:56 -07002024 npiv_fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002025 npiv_fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs =
Nicholas Bellinger394d62b2014-02-19 16:52:15 -08002026 tcm_qla2xxx_npiv_tpg_attrs;
Andy Groverd80e224d2013-10-09 11:05:56 -07002027 npiv_fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
2028 npiv_fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
2029 npiv_fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2030 npiv_fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2031 npiv_fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2032 npiv_fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2033 npiv_fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002034 /*
2035 * Register the npiv_fabric for use within TCM
2036 */
2037 ret = target_fabric_configfs_register(npiv_fabric);
2038 if (ret < 0) {
2039 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
2040 goto out_fabric;
2041 }
2042 /*
2043 * Setup our local pointer to *npiv_fabric
2044 */
2045 tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
2046 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
2047
2048 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
2049 WQ_MEM_RECLAIM, 0);
2050 if (!tcm_qla2xxx_free_wq) {
2051 ret = -ENOMEM;
2052 goto out_fabric_npiv;
2053 }
2054
2055 tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
2056 if (!tcm_qla2xxx_cmd_wq) {
2057 ret = -ENOMEM;
2058 goto out_free_wq;
2059 }
2060
2061 return 0;
2062
2063out_free_wq:
2064 destroy_workqueue(tcm_qla2xxx_free_wq);
2065out_fabric_npiv:
2066 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
2067out_fabric:
2068 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
2069 return ret;
2070}
2071
2072static void tcm_qla2xxx_deregister_configfs(void)
2073{
2074 destroy_workqueue(tcm_qla2xxx_cmd_wq);
2075 destroy_workqueue(tcm_qla2xxx_free_wq);
2076
2077 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
2078 tcm_qla2xxx_fabric_configfs = NULL;
2079 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
2080
2081 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
2082 tcm_qla2xxx_npiv_fabric_configfs = NULL;
2083 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
2084}
2085
2086static int __init tcm_qla2xxx_init(void)
2087{
2088 int ret;
2089
2090 ret = tcm_qla2xxx_register_configfs();
2091 if (ret < 0)
2092 return ret;
2093
2094 return 0;
2095}
2096
2097static void __exit tcm_qla2xxx_exit(void)
2098{
2099 tcm_qla2xxx_deregister_configfs();
2100}
2101
2102MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
2103MODULE_LICENSE("GPL");
2104module_init(tcm_qla2xxx_init);
2105module_exit(tcm_qla2xxx_exit);