blob: 843b53b0e9f2c97879b2965c1e373e25ceddf2bd [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
Himanshu Madhani4d6609c2014-09-25 06:14:43 -040053static struct workqueue_struct *tcm_qla2xxx_free_wq;
54static struct workqueue_struct *tcm_qla2xxx_cmd_wq;
55
56/* Local pointer to allocated TCM configfs fabric module */
57static struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
58static struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -040059
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -040060/*
61 * Parse WWN.
62 * If strict, we require lower-case hex and colon separators to be sure
63 * the name is the same as what would be generated by ft_format_wwn()
64 * so the name and wwn are mapped one-to-one.
65 */
66static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
67{
68 const char *cp;
69 char c;
70 u32 nibble;
71 u32 byte = 0;
72 u32 pos = 0;
73 u32 err;
74
75 *wwn = 0;
76 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
77 c = *cp;
78 if (c == '\n' && cp[1] == '\0')
79 continue;
80 if (strict && pos++ == 2 && byte++ < 7) {
81 pos = 0;
82 if (c == ':')
83 continue;
84 err = 1;
85 goto fail;
86 }
87 if (c == '\0') {
88 err = 2;
89 if (strict && byte != 8)
90 goto fail;
91 return cp - name;
92 }
93 err = 3;
94 if (isdigit(c))
95 nibble = c - '0';
96 else if (isxdigit(c) && (islower(c) || !strict))
97 nibble = tolower(c) - 'a' + 10;
98 else
99 goto fail;
100 *wwn = (*wwn << 4) | nibble;
101 }
102 err = 4;
103fail:
104 pr_debug("err %u len %zu pos %u byte %u\n",
105 err, cp - name, pos, byte);
106 return -1;
107}
108
109static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
110{
111 u8 b[8];
112
113 put_unaligned_be64(wwn, b);
114 return snprintf(buf, len,
115 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
116 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
117}
118
119static char *tcm_qla2xxx_get_fabric_name(void)
120{
121 return "qla2xxx";
122}
123
124/*
125 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
126 */
127static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
128{
Roland Dreierd4f75b52012-06-11 18:31:31 -0700129 unsigned int i, j;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400130 u8 wwn[8];
131
132 memset(wwn, 0, sizeof(wwn));
133
134 /* Validate and store the new name */
135 for (i = 0, j = 0; i < 16; i++) {
Roland Dreierd4f75b52012-06-11 18:31:31 -0700136 int value;
137
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400138 value = hex_to_bin(*ns++);
139 if (value >= 0)
140 j = (j << 4) | value;
141 else
142 return -EINVAL;
143
144 if (i % 2) {
145 wwn[i/2] = j & 0xff;
146 j = 0;
147 }
148 }
149
150 *nm = wwn_to_u64(wwn);
151 return 0;
152}
153
154/*
155 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
156 * store_fc_host_vport_create()
157 */
158static int tcm_qla2xxx_npiv_parse_wwn(
159 const char *name,
160 size_t count,
161 u64 *wwpn,
162 u64 *wwnn)
163{
164 unsigned int cnt = count;
165 int rc;
166
167 *wwpn = 0;
168 *wwnn = 0;
169
170 /* count may include a LF at end of string */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800171 if (name[cnt-1] == '\n' || name[cnt-1] == 0)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400172 cnt--;
173
174 /* validate we have enough characters for WWPN */
175 if ((cnt != (16+1+16)) || (name[16] != ':'))
176 return -EINVAL;
177
178 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
179 if (rc != 0)
180 return rc;
181
182 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
183 if (rc != 0)
184 return rc;
185
186 return 0;
187}
188
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400189static char *tcm_qla2xxx_npiv_get_fabric_name(void)
190{
191 return "qla2xxx_npiv";
192}
193
194static u8 tcm_qla2xxx_get_fabric_proto_ident(struct se_portal_group *se_tpg)
195{
196 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
197 struct tcm_qla2xxx_tpg, se_tpg);
198 struct tcm_qla2xxx_lport *lport = tpg->lport;
199 u8 proto_id;
200
201 switch (lport->lport_proto_id) {
202 case SCSI_PROTOCOL_FCP:
203 default:
204 proto_id = fc_get_fabric_proto_ident(se_tpg);
205 break;
206 }
207
208 return proto_id;
209}
210
211static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
212{
213 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
214 struct tcm_qla2xxx_tpg, se_tpg);
215 struct tcm_qla2xxx_lport *lport = tpg->lport;
216
Roland Dreierc046aa02012-10-11 13:41:31 -0700217 return lport->lport_naa_name;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400218}
219
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400220static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
221{
222 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
223 struct tcm_qla2xxx_tpg, se_tpg);
224 return tpg->lport_tpgt;
225}
226
227static u32 tcm_qla2xxx_get_default_depth(struct se_portal_group *se_tpg)
228{
229 return 1;
230}
231
232static u32 tcm_qla2xxx_get_pr_transport_id(
233 struct se_portal_group *se_tpg,
234 struct se_node_acl *se_nacl,
235 struct t10_pr_registration *pr_reg,
236 int *format_code,
237 unsigned char *buf)
238{
239 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
240 struct tcm_qla2xxx_tpg, se_tpg);
241 struct tcm_qla2xxx_lport *lport = tpg->lport;
242 int ret = 0;
243
244 switch (lport->lport_proto_id) {
245 case SCSI_PROTOCOL_FCP:
246 default:
247 ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
248 format_code, buf);
249 break;
250 }
251
252 return ret;
253}
254
255static u32 tcm_qla2xxx_get_pr_transport_id_len(
256 struct se_portal_group *se_tpg,
257 struct se_node_acl *se_nacl,
258 struct t10_pr_registration *pr_reg,
259 int *format_code)
260{
261 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
262 struct tcm_qla2xxx_tpg, se_tpg);
263 struct tcm_qla2xxx_lport *lport = tpg->lport;
264 int ret = 0;
265
266 switch (lport->lport_proto_id) {
267 case SCSI_PROTOCOL_FCP:
268 default:
269 ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
270 format_code);
271 break;
272 }
273
274 return ret;
275}
276
277static char *tcm_qla2xxx_parse_pr_out_transport_id(
278 struct se_portal_group *se_tpg,
279 const char *buf,
280 u32 *out_tid_len,
281 char **port_nexus_ptr)
282{
283 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
284 struct tcm_qla2xxx_tpg, se_tpg);
285 struct tcm_qla2xxx_lport *lport = tpg->lport;
286 char *tid = NULL;
287
288 switch (lport->lport_proto_id) {
289 case SCSI_PROTOCOL_FCP:
290 default:
291 tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
292 port_nexus_ptr);
293 break;
294 }
295
296 return tid;
297}
298
299static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
300{
301 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
302 struct tcm_qla2xxx_tpg, se_tpg);
303
Andy Grovera309f482013-10-09 11:05:59 -0700304 return tpg->tpg_attrib.generate_node_acls;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400305}
306
307static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
308{
309 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
310 struct tcm_qla2xxx_tpg, se_tpg);
311
Andy Grovera309f482013-10-09 11:05:59 -0700312 return tpg->tpg_attrib.cache_dynamic_acls;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400313}
314
315static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
316{
317 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
318 struct tcm_qla2xxx_tpg, se_tpg);
319
Andy Grovera309f482013-10-09 11:05:59 -0700320 return tpg->tpg_attrib.demo_mode_write_protect;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400321}
322
323static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
324{
325 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
326 struct tcm_qla2xxx_tpg, se_tpg);
327
Andy Grovera309f482013-10-09 11:05:59 -0700328 return tpg->tpg_attrib.prod_mode_write_protect;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400329}
330
Andy Groverde04a8a2013-07-19 15:06:38 -0700331static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
332{
333 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
334 struct tcm_qla2xxx_tpg, se_tpg);
335
Andy Grovera309f482013-10-09 11:05:59 -0700336 return tpg->tpg_attrib.demo_mode_login_only;
Andy Groverde04a8a2013-07-19 15:06:38 -0700337}
338
Nicholas Bellinger64b16882015-03-27 23:30:57 -0700339static int tcm_qla2xxx_check_prot_fabric_only(struct se_portal_group *se_tpg)
340{
341 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
342 struct tcm_qla2xxx_tpg, se_tpg);
343
344 return tpg->tpg_attrib.fabric_prot_type;
345}
346
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400347static struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
348 struct se_portal_group *se_tpg)
349{
350 struct tcm_qla2xxx_nacl *nacl;
351
352 nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
353 if (!nacl) {
Masanari Iida6efb3c0a2012-10-26 22:10:54 +0900354 pr_err("Unable to allocate struct tcm_qla2xxx_nacl\n");
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400355 return NULL;
356 }
357
358 return &nacl->se_node_acl;
359}
360
361static void tcm_qla2xxx_release_fabric_acl(
362 struct se_portal_group *se_tpg,
363 struct se_node_acl *se_nacl)
364{
365 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
366 struct tcm_qla2xxx_nacl, se_node_acl);
367 kfree(nacl);
368}
369
370static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
371{
372 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
373 struct tcm_qla2xxx_tpg, se_tpg);
374
375 return tpg->lport_tpgt;
376}
377
378static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
379{
380 struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
381 struct qla_tgt_mgmt_cmd, free_work);
382
383 transport_generic_free_cmd(&mcmd->se_cmd, 0);
384}
385
386/*
387 * Called from qla_target_template->free_mcmd(), and will call
388 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
389 * release callback. qla_hw_data->hardware_lock is expected to be held
390 */
391static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
392{
393 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
394 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
395}
396
397static void tcm_qla2xxx_complete_free(struct work_struct *work)
398{
399 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
400
Saurav Kashyape07f8f62014-09-25 06:14:58 -0400401 cmd->cmd_in_wq = 0;
402
403 WARN_ON(cmd->cmd_flags & BIT_16);
404
405 cmd->cmd_flags |= BIT_16;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400406 transport_generic_free_cmd(&cmd->se_cmd, 0);
407}
408
409/*
410 * Called from qla_target_template->free_cmd(), and will call
411 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
412 * release callback. qla_hw_data->hardware_lock is expected to be held
413 */
414static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
415{
Saurav Kashyape07f8f62014-09-25 06:14:58 -0400416 cmd->cmd_in_wq = 1;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400417 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
418 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
419}
420
421/*
422 * Called from struct target_core_fabric_ops->check_stop_free() context
423 */
424static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
425{
Saurav Kashyape07f8f62014-09-25 06:14:58 -0400426 struct qla_tgt_cmd *cmd;
427
428 if ((se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) == 0) {
429 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
430 cmd->cmd_flags |= BIT_14;
431 }
432
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400433 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
434}
435
436/* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
437 * fabric descriptor @se_cmd command to release
438 */
439static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
440{
441 struct qla_tgt_cmd *cmd;
442
443 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
444 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
445 struct qla_tgt_mgmt_cmd, se_cmd);
446 qlt_free_mcmd(mcmd);
447 return;
448 }
449
450 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
451 qlt_free_cmd(cmd);
452}
453
454static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
455{
456 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
457 struct scsi_qla_host *vha;
458 unsigned long flags;
459
460 BUG_ON(!sess);
461 vha = sess->vha;
462
463 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
Roland Dreier1c7b13f2012-07-16 11:04:42 -0700464 target_sess_cmd_list_set_waiting(se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400465 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
466
467 return 1;
468}
469
470static void tcm_qla2xxx_close_session(struct se_session *se_sess)
471{
472 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
473 struct scsi_qla_host *vha;
474 unsigned long flags;
475
476 BUG_ON(!sess);
477 vha = sess->vha;
478
479 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
480 qlt_unreg_sess(sess);
481 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
482}
483
484static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
485{
486 return 0;
487}
488
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400489static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
490{
491 struct qla_tgt_cmd *cmd = container_of(se_cmd,
492 struct qla_tgt_cmd, se_cmd);
493
494 cmd->bufflen = se_cmd->data_length;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700495 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400496
497 cmd->sg_cnt = se_cmd->t_data_nents;
498 cmd->sg = se_cmd->t_data_sg;
499
Quinn Tranf83adb62014-04-11 16:54:43 -0400500 cmd->prot_sg_cnt = se_cmd->t_prot_nents;
501 cmd->prot_sg = se_cmd->t_prot_sg;
502 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size;
503 se_cmd->pi_err = 0;
504
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400505 /*
506 * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
507 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
508 */
509 return qlt_rdy_to_xfer(cmd);
510}
511
512static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
513{
514 unsigned long flags;
515 /*
516 * Check for WRITE_PENDING status to determine if we need to wait for
517 * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
518 */
519 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
520 if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
521 se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
522 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
523 wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
524 3000);
525 return 0;
526 }
527 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
528
529 return 0;
530}
531
532static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
533{
534 return;
535}
536
537static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
538{
Himanshu Madhanidd9c4ef2014-09-25 06:14:50 -0400539 struct qla_tgt_cmd *cmd;
540
541 /* check for task mgmt cmd */
542 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB)
543 return 0xffffffff;
544
545 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400546
547 return cmd->tag;
548}
549
550static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
551{
552 return 0;
553}
554
555/*
556 * Called from process context in qla_target.c:qlt_do_work() code
557 */
558static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
559 unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
560 int data_dir, int bidi)
561{
562 struct se_cmd *se_cmd = &cmd->se_cmd;
563 struct se_session *se_sess;
564 struct qla_tgt_sess *sess;
565 int flags = TARGET_SCF_ACK_KREF;
566
567 if (bidi)
568 flags |= TARGET_SCF_BIDI_OP;
569
570 sess = cmd->sess;
571 if (!sess) {
572 pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
573 return -EINVAL;
574 }
575
576 se_sess = sess->se_sess;
577 if (!se_sess) {
578 pr_err("Unable to locate active struct se_session\n");
579 return -EINVAL;
580 }
581
Roland Dreierd6dfc862012-07-16 11:04:39 -0700582 return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400583 cmd->unpacked_lun, data_length, fcp_task_attr,
584 data_dir, flags);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400585}
586
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400587static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400588{
589 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400590
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400591 /*
592 * Ensure that the complete FCP WRITE payload has been received.
593 * Otherwise return an exception via CHECK_CONDITION status.
594 */
Saurav Kashyape07f8f62014-09-25 06:14:58 -0400595 cmd->cmd_in_wq = 0;
596 cmd->cmd_flags |= BIT_11;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400597 if (!cmd->write_data_transferred) {
598 /*
599 * Check if se_cmd has already been aborted via LUN_RESET, and
600 * waiting upon completion in tcm_qla2xxx_write_pending_status()
601 */
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400602 if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
603 complete(&cmd->se_cmd.t_transport_stop_comp);
604 return;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400605 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400606
Quinn Tranf83adb62014-04-11 16:54:43 -0400607 if (cmd->se_cmd.pi_err)
608 transport_generic_request_failure(&cmd->se_cmd,
609 cmd->se_cmd.pi_err);
610 else
611 transport_generic_request_failure(&cmd->se_cmd,
612 TCM_CHECK_CONDITION_ABORT_CMD);
613
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400614 return;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400615 }
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400616
617 return target_execute_cmd(&cmd->se_cmd);
618}
619
620/*
621 * Called from qla_target.c:qlt_do_ctio_completion()
622 */
623static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
624{
Saurav Kashyape07f8f62014-09-25 06:14:58 -0400625 cmd->cmd_flags |= BIT_10;
626 cmd->cmd_in_wq = 1;
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400627 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
628 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400629}
630
Quinn Tranf83adb62014-04-11 16:54:43 -0400631static void tcm_qla2xxx_handle_dif_work(struct work_struct *work)
632{
633 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
634
635 /* take an extra kref to prevent cmd free too early.
636 * need to wait for SCSI status/check condition to
637 * finish responding generate by transport_generic_request_failure.
638 */
639 kref_get(&cmd->se_cmd.cmd_kref);
640 transport_generic_request_failure(&cmd->se_cmd, cmd->se_cmd.pi_err);
641}
642
643/*
644 * Called from qla_target.c:qlt_do_ctio_completion()
645 */
646static void tcm_qla2xxx_handle_dif_err(struct qla_tgt_cmd *cmd)
647{
648 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_dif_work);
649 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
650}
651
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400652/*
653 * Called from qla_target.c:qlt_issue_task_mgmt()
654 */
Roland Dreier9389c3c2012-06-11 18:31:30 -0700655static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
656 uint8_t tmr_func, uint32_t tag)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400657{
658 struct qla_tgt_sess *sess = mcmd->sess;
659 struct se_cmd *se_cmd = &mcmd->se_cmd;
660
661 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
662 tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
663}
664
665static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
666{
667 struct qla_tgt_cmd *cmd = container_of(se_cmd,
668 struct qla_tgt_cmd, se_cmd);
669
Saurav Kashyape07f8f62014-09-25 06:14:58 -0400670 cmd->cmd_flags |= BIT_4;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400671 cmd->bufflen = se_cmd->data_length;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700672 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400673 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
674
675 cmd->sg_cnt = se_cmd->t_data_nents;
676 cmd->sg = se_cmd->t_data_sg;
677 cmd->offset = 0;
Saurav Kashyape07f8f62014-09-25 06:14:58 -0400678 cmd->cmd_flags |= BIT_3;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400679
Quinn Tranf83adb62014-04-11 16:54:43 -0400680 cmd->prot_sg_cnt = se_cmd->t_prot_nents;
681 cmd->prot_sg = se_cmd->t_prot_sg;
682 cmd->blk_sz = se_cmd->se_dev->dev_attrib.block_size;
683 se_cmd->pi_err = 0;
684
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400685 /*
686 * Now queue completed DATA_IN the qla2xxx LLD and response ring
687 */
688 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
689 se_cmd->scsi_status);
690}
691
692static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
693{
694 struct qla_tgt_cmd *cmd = container_of(se_cmd,
695 struct qla_tgt_cmd, se_cmd);
696 int xmit_type = QLA_TGT_XMIT_STATUS;
697
698 cmd->bufflen = se_cmd->data_length;
699 cmd->sg = NULL;
700 cmd->sg_cnt = 0;
701 cmd->offset = 0;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700702 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400703 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
Saurav Kashyape07f8f62014-09-25 06:14:58 -0400704 if (cmd->cmd_flags & BIT_5) {
705 pr_crit("Bit_5 already set for cmd = %p.\n", cmd);
706 dump_stack();
707 }
708 cmd->cmd_flags |= BIT_5;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400709
710 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
711 /*
712 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
713 * for qla_tgt_xmit_response LLD code
714 */
Roland Dreierb5aff3d2013-06-05 09:54:17 -0700715 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
716 se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
717 se_cmd->residual_count = 0;
718 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400719 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
Roland Dreierb5aff3d2013-06-05 09:54:17 -0700720 se_cmd->residual_count += se_cmd->data_length;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400721
722 cmd->bufflen = 0;
723 }
724 /*
725 * Now queue status response to qla2xxx LLD code and response ring
726 */
727 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
728}
729
Joern Engelb79fafa2013-07-03 11:22:17 -0400730static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400731{
732 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
733 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
734 struct qla_tgt_mgmt_cmd, se_cmd);
735
736 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
737 mcmd, se_tmr->function, se_tmr->response);
738 /*
739 * Do translation between TCM TM response codes and
740 * QLA2xxx FC TM response codes.
741 */
742 switch (se_tmr->response) {
743 case TMR_FUNCTION_COMPLETE:
744 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
745 break;
746 case TMR_TASK_DOES_NOT_EXIST:
747 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
748 break;
749 case TMR_FUNCTION_REJECTED:
750 mcmd->fc_tm_rsp = FC_TM_REJECT;
751 break;
752 case TMR_LUN_DOES_NOT_EXIST:
753 default:
754 mcmd->fc_tm_rsp = FC_TM_FAILED;
755 break;
756 }
757 /*
758 * Queue the TM response to QLA2xxx LLD to build a
759 * CTIO response packet.
760 */
761 qlt_xmit_tm_rsp(mcmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400762}
763
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -0700764static void tcm_qla2xxx_aborted_task(struct se_cmd *se_cmd)
765{
766 struct qla_tgt_cmd *cmd = container_of(se_cmd,
767 struct qla_tgt_cmd, se_cmd);
768 struct scsi_qla_host *vha = cmd->vha;
769 struct qla_hw_data *ha = vha->hw;
770
771 if (!cmd->sg_mapped)
772 return;
773
774 pci_unmap_sg(ha->pdev, cmd->sg, cmd->sg_cnt, cmd->dma_data_direction);
775 cmd->sg_mapped = 0;
776}
777
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -0700778static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
779 struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400780/*
781 * Expected to be called with struct qla_hw_data->hardware_lock held
782 */
783static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
784{
785 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
786 struct se_portal_group *se_tpg = se_nacl->se_tpg;
787 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
788 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
789 struct tcm_qla2xxx_lport, lport_wwn);
790 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
791 struct tcm_qla2xxx_nacl, se_node_acl);
792 void *node;
793
794 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
795
796 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
Joern Engelf4c24db12014-10-03 14:35:56 -0700797 if (WARN_ON(node && (node != se_nacl))) {
798 /*
799 * The nacl no longer matches what we think it should be.
800 * Most likely a new dynamic acl has been added while
801 * someone dropped the hardware lock. It clearly is a
802 * bug elsewhere, but this bit can't make things worse.
803 */
804 btree_insert32(&lport->lport_fcport_map, nacl->nport_id,
805 node, GFP_ATOMIC);
806 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400807
808 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
809 se_nacl, nacl->nport_wwnn, nacl->nport_id);
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -0700810 /*
811 * Now clear the se_nacl and session pointers from our HW lport lookup
812 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
813 *
814 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
815 * target_wait_for_sess_cmds() before the session waits for outstanding
816 * I/O to complete, to avoid a race between session shutdown execution
817 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
818 */
819 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400820}
821
Joern Engelaaf68b72012-05-18 13:58:23 -0700822static void tcm_qla2xxx_release_session(struct kref *kref)
823{
824 struct se_session *se_sess = container_of(kref,
825 struct se_session, sess_kref);
826
827 qlt_unreg_sess(se_sess->fabric_sess_ptr);
828}
829
830static void tcm_qla2xxx_put_session(struct se_session *se_sess)
831{
832 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
833 struct qla_hw_data *ha = sess->vha->hw;
834 unsigned long flags;
835
836 spin_lock_irqsave(&ha->hardware_lock, flags);
837 kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
838 spin_unlock_irqrestore(&ha->hardware_lock, flags);
839}
840
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400841static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
842{
Saurav Kashyap0e8cd712014-01-14 20:40:38 -0800843 if (!sess)
844 return;
845
Jörn Engel08234e32013-06-12 16:27:54 -0400846 assert_spin_locked(&sess->vha->hw->hardware_lock);
847 kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400848}
849
850static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
851{
Jörn Engel08234e32013-06-12 16:27:54 -0400852 assert_spin_locked(&sess->vha->hw->hardware_lock);
853 target_sess_cmd_list_set_waiting(sess->se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400854}
855
856static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
857 struct se_portal_group *se_tpg,
858 struct config_group *group,
859 const char *name)
860{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400861 struct se_node_acl *se_nacl, *se_nacl_new;
862 struct tcm_qla2xxx_nacl *nacl;
863 u64 wwnn;
864 u32 qla2xxx_nexus_depth;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400865
866 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
867 return ERR_PTR(-EINVAL);
868
869 se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
870 if (!se_nacl_new)
871 return ERR_PTR(-ENOMEM);
872/* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
873 qla2xxx_nexus_depth = 1;
874
875 /*
876 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
877 * when converting a NodeACL from demo mode -> explict
878 */
879 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
880 name, qla2xxx_nexus_depth);
881 if (IS_ERR(se_nacl)) {
882 tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
883 return se_nacl;
884 }
885 /*
886 * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
887 */
888 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
889 nacl->nport_wwnn = wwnn;
890 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400891
892 return se_nacl;
893}
894
895static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
896{
897 struct se_portal_group *se_tpg = se_acl->se_tpg;
898 struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
899 struct tcm_qla2xxx_nacl, se_node_acl);
900
901 core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
902 kfree(nacl);
903}
904
905/* Start items for tcm_qla2xxx_tpg_attrib_cit */
906
907#define DEF_QLA_TPG_ATTRIB(name) \
908 \
909static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
910 struct se_portal_group *se_tpg, \
911 char *page) \
912{ \
913 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
914 struct tcm_qla2xxx_tpg, se_tpg); \
915 \
Andy Grovera309f482013-10-09 11:05:59 -0700916 return sprintf(page, "%u\n", tpg->tpg_attrib.name); \
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400917} \
918 \
919static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
920 struct se_portal_group *se_tpg, \
921 const char *page, \
922 size_t count) \
923{ \
924 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
925 struct tcm_qla2xxx_tpg, se_tpg); \
926 unsigned long val; \
927 int ret; \
928 \
929 ret = kstrtoul(page, 0, &val); \
930 if (ret < 0) { \
931 pr_err("kstrtoul() failed with" \
932 " ret: %d\n", ret); \
933 return -EINVAL; \
934 } \
935 ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
936 \
937 return (!ret) ? count : -EINVAL; \
938}
939
940#define DEF_QLA_TPG_ATTR_BOOL(_name) \
941 \
942static int tcm_qla2xxx_set_attrib_##_name( \
943 struct tcm_qla2xxx_tpg *tpg, \
944 unsigned long val) \
945{ \
946 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
947 \
948 if ((val != 0) && (val != 1)) { \
949 pr_err("Illegal boolean value %lu\n", val); \
950 return -EINVAL; \
951 } \
952 \
953 a->_name = val; \
954 return 0; \
955}
956
957#define QLA_TPG_ATTR(_name, _mode) \
958 TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
959
960/*
961 * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
962 */
963DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
964DEF_QLA_TPG_ATTRIB(generate_node_acls);
965QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
966
967/*
968 Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
969 */
970DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
971DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
972QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
973
974/*
975 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
976 */
977DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
978DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
979QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
980
981/*
982 * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
983 */
984DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
985DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
986QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
987
Andy Groverde04a8a2013-07-19 15:06:38 -0700988/*
989 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_login_only
990 */
991DEF_QLA_TPG_ATTR_BOOL(demo_mode_login_only);
992DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
993QLA_TPG_ATTR(demo_mode_login_only, S_IRUGO | S_IWUSR);
994
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400995static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
996 &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
997 &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
998 &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
999 &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
Andy Groverde04a8a2013-07-19 15:06:38 -07001000 &tcm_qla2xxx_tpg_attrib_demo_mode_login_only.attr,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001001 NULL,
1002};
1003
1004/* End items for tcm_qla2xxx_tpg_attrib_cit */
1005
1006static ssize_t tcm_qla2xxx_tpg_show_enable(
1007 struct se_portal_group *se_tpg,
1008 char *page)
1009{
1010 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1011 struct tcm_qla2xxx_tpg, se_tpg);
1012
1013 return snprintf(page, PAGE_SIZE, "%d\n",
1014 atomic_read(&tpg->lport_tpg_enabled));
1015}
1016
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001017static void tcm_qla2xxx_depend_tpg(struct work_struct *work)
1018{
1019 struct tcm_qla2xxx_tpg *base_tpg = container_of(work,
1020 struct tcm_qla2xxx_tpg, tpg_base_work);
1021 struct se_portal_group *se_tpg = &base_tpg->se_tpg;
1022 struct scsi_qla_host *base_vha = base_tpg->lport->qla_vha;
1023
1024 if (!configfs_depend_item(se_tpg->se_tpg_tfo->tf_subsys,
1025 &se_tpg->tpg_group.cg_item)) {
1026 atomic_set(&base_tpg->lport_tpg_enabled, 1);
1027 qlt_enable_vha(base_vha);
1028 }
1029 complete(&base_tpg->tpg_base_comp);
1030}
1031
1032static void tcm_qla2xxx_undepend_tpg(struct work_struct *work)
1033{
1034 struct tcm_qla2xxx_tpg *base_tpg = container_of(work,
1035 struct tcm_qla2xxx_tpg, tpg_base_work);
1036 struct se_portal_group *se_tpg = &base_tpg->se_tpg;
1037 struct scsi_qla_host *base_vha = base_tpg->lport->qla_vha;
1038
1039 if (!qlt_stop_phase1(base_vha->vha_tgt.qla_tgt)) {
1040 atomic_set(&base_tpg->lport_tpg_enabled, 0);
1041 configfs_undepend_item(se_tpg->se_tpg_tfo->tf_subsys,
1042 &se_tpg->tpg_group.cg_item);
1043 }
1044 complete(&base_tpg->tpg_base_comp);
1045}
1046
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001047static ssize_t tcm_qla2xxx_tpg_store_enable(
1048 struct se_portal_group *se_tpg,
1049 const char *page,
1050 size_t count)
1051{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001052 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1053 struct tcm_qla2xxx_tpg, se_tpg);
1054 unsigned long op;
1055 int rc;
1056
1057 rc = kstrtoul(page, 0, &op);
1058 if (rc < 0) {
1059 pr_err("kstrtoul() returned %d\n", rc);
1060 return -EINVAL;
1061 }
1062 if ((op != 1) && (op != 0)) {
1063 pr_err("Illegal value for tpg_enable: %lu\n", op);
1064 return -EINVAL;
1065 }
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001066 if (op) {
1067 if (atomic_read(&tpg->lport_tpg_enabled))
1068 return -EEXIST;
1069
1070 INIT_WORK(&tpg->tpg_base_work, tcm_qla2xxx_depend_tpg);
1071 } else {
1072 if (!atomic_read(&tpg->lport_tpg_enabled))
1073 return count;
1074
1075 INIT_WORK(&tpg->tpg_base_work, tcm_qla2xxx_undepend_tpg);
1076 }
1077 init_completion(&tpg->tpg_base_comp);
1078 schedule_work(&tpg->tpg_base_work);
1079 wait_for_completion(&tpg->tpg_base_comp);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001080
1081 if (op) {
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001082 if (!atomic_read(&tpg->lport_tpg_enabled))
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001083 return -ENODEV;
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001084 } else {
1085 if (atomic_read(&tpg->lport_tpg_enabled))
1086 return -EPERM;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001087 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001088 return count;
1089}
1090
1091TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
1092
Nicholas Bellingerd23dbaa2015-03-06 20:43:34 -08001093static ssize_t tcm_qla2xxx_tpg_show_dynamic_sessions(
1094 struct se_portal_group *se_tpg,
1095 char *page)
1096{
1097 return target_show_dynamic_sessions(se_tpg, page);
1098}
1099
1100TF_TPG_BASE_ATTR_RO(tcm_qla2xxx, dynamic_sessions);
1101
Nicholas Bellinger64b16882015-03-27 23:30:57 -07001102static ssize_t tcm_qla2xxx_tpg_store_fabric_prot_type(
1103 struct se_portal_group *se_tpg,
1104 const char *page,
1105 size_t count)
1106{
1107 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1108 struct tcm_qla2xxx_tpg, se_tpg);
1109 unsigned long val;
1110 int ret = kstrtoul(page, 0, &val);
1111
1112 if (ret) {
1113 pr_err("kstrtoul() returned %d for fabric_prot_type\n", ret);
1114 return ret;
1115 }
1116 if (val != 0 && val != 1 && val != 3) {
1117 pr_err("Invalid qla2xxx fabric_prot_type: %lu\n", val);
1118 return -EINVAL;
1119 }
1120 tpg->tpg_attrib.fabric_prot_type = val;
1121
1122 return count;
1123}
1124
1125static ssize_t tcm_qla2xxx_tpg_show_fabric_prot_type(
1126 struct se_portal_group *se_tpg,
1127 char *page)
1128{
1129 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1130 struct tcm_qla2xxx_tpg, se_tpg);
1131
1132 return sprintf(page, "%d\n", tpg->tpg_attrib.fabric_prot_type);
1133}
1134TF_TPG_BASE_ATTR(tcm_qla2xxx, fabric_prot_type, S_IRUGO | S_IWUSR);
1135
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001136static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
1137 &tcm_qla2xxx_tpg_enable.attr,
Nicholas Bellingerd23dbaa2015-03-06 20:43:34 -08001138 &tcm_qla2xxx_tpg_dynamic_sessions.attr,
Nicholas Bellinger64b16882015-03-27 23:30:57 -07001139 &tcm_qla2xxx_tpg_fabric_prot_type.attr,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001140 NULL,
1141};
1142
1143static struct se_portal_group *tcm_qla2xxx_make_tpg(
1144 struct se_wwn *wwn,
1145 struct config_group *group,
1146 const char *name)
1147{
1148 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1149 struct tcm_qla2xxx_lport, lport_wwn);
1150 struct tcm_qla2xxx_tpg *tpg;
1151 unsigned long tpgt;
1152 int ret;
1153
1154 if (strstr(name, "tpgt_") != name)
1155 return ERR_PTR(-EINVAL);
1156 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1157 return ERR_PTR(-EINVAL);
1158
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001159 if ((tpgt != 1)) {
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001160 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1161 return ERR_PTR(-ENOSYS);
1162 }
1163
1164 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1165 if (!tpg) {
1166 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1167 return ERR_PTR(-ENOMEM);
1168 }
1169 tpg->lport = lport;
1170 tpg->lport_tpgt = tpgt;
1171 /*
1172 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1173 * NodeACLs
1174 */
Andy Grovera309f482013-10-09 11:05:59 -07001175 tpg->tpg_attrib.generate_node_acls = 1;
1176 tpg->tpg_attrib.demo_mode_write_protect = 1;
1177 tpg->tpg_attrib.cache_dynamic_acls = 1;
1178 tpg->tpg_attrib.demo_mode_login_only = 1;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001179
1180 ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
1181 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1182 if (ret < 0) {
1183 kfree(tpg);
1184 return NULL;
1185 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001186
1187 lport->tpg_1 = tpg;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001188
1189 return &tpg->se_tpg;
1190}
1191
1192static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1193{
1194 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1195 struct tcm_qla2xxx_tpg, se_tpg);
1196 struct tcm_qla2xxx_lport *lport = tpg->lport;
1197 struct scsi_qla_host *vha = lport->qla_vha;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001198 /*
1199 * Call into qla2x_target.c LLD logic to shutdown the active
1200 * FC Nexuses and disable target mode operation for this qla_hw_data
1201 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001202 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stop)
1203 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001204
1205 core_tpg_deregister(se_tpg);
1206 /*
1207 * Clear local TPG=1 pointer for non NPIV mode.
1208 */
Nicholas Bellinger394d62b2014-02-19 16:52:15 -08001209 lport->tpg_1 = NULL;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001210 kfree(tpg);
1211}
1212
Nicholas Bellinger394d62b2014-02-19 16:52:15 -08001213static ssize_t tcm_qla2xxx_npiv_tpg_show_enable(
1214 struct se_portal_group *se_tpg,
1215 char *page)
1216{
1217 return tcm_qla2xxx_tpg_show_enable(se_tpg, page);
1218}
1219
1220static ssize_t tcm_qla2xxx_npiv_tpg_store_enable(
1221 struct se_portal_group *se_tpg,
1222 const char *page,
1223 size_t count)
1224{
1225 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
1226 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
1227 struct tcm_qla2xxx_lport, lport_wwn);
1228 struct scsi_qla_host *vha = lport->qla_vha;
1229 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1230 struct tcm_qla2xxx_tpg, se_tpg);
1231 unsigned long op;
1232 int rc;
1233
1234 rc = kstrtoul(page, 0, &op);
1235 if (rc < 0) {
1236 pr_err("kstrtoul() returned %d\n", rc);
1237 return -EINVAL;
1238 }
1239 if ((op != 1) && (op != 0)) {
1240 pr_err("Illegal value for tpg_enable: %lu\n", op);
1241 return -EINVAL;
1242 }
1243 if (op) {
1244 if (atomic_read(&tpg->lport_tpg_enabled))
1245 return -EEXIST;
1246
1247 atomic_set(&tpg->lport_tpg_enabled, 1);
1248 qlt_enable_vha(vha);
1249 } else {
1250 if (!atomic_read(&tpg->lport_tpg_enabled))
1251 return count;
1252
1253 atomic_set(&tpg->lport_tpg_enabled, 0);
1254 qlt_stop_phase1(vha->vha_tgt.qla_tgt);
1255 }
1256
1257 return count;
1258}
1259
1260TF_TPG_BASE_ATTR(tcm_qla2xxx_npiv, enable, S_IRUGO | S_IWUSR);
1261
1262static struct configfs_attribute *tcm_qla2xxx_npiv_tpg_attrs[] = {
1263 &tcm_qla2xxx_npiv_tpg_enable.attr,
1264 NULL,
1265};
1266
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001267static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1268 struct se_wwn *wwn,
1269 struct config_group *group,
1270 const char *name)
1271{
1272 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1273 struct tcm_qla2xxx_lport, lport_wwn);
1274 struct tcm_qla2xxx_tpg *tpg;
1275 unsigned long tpgt;
1276 int ret;
1277
1278 if (strstr(name, "tpgt_") != name)
1279 return ERR_PTR(-EINVAL);
1280 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1281 return ERR_PTR(-EINVAL);
1282
1283 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1284 if (!tpg) {
1285 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1286 return ERR_PTR(-ENOMEM);
1287 }
1288 tpg->lport = lport;
1289 tpg->lport_tpgt = tpgt;
1290
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001291 /*
1292 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1293 * NodeACLs
1294 */
1295 tpg->tpg_attrib.generate_node_acls = 1;
1296 tpg->tpg_attrib.demo_mode_write_protect = 1;
1297 tpg->tpg_attrib.cache_dynamic_acls = 1;
1298 tpg->tpg_attrib.demo_mode_login_only = 1;
1299
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001300 ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
1301 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1302 if (ret < 0) {
1303 kfree(tpg);
1304 return NULL;
1305 }
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001306 lport->tpg_1 = tpg;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001307 return &tpg->se_tpg;
1308}
1309
1310/*
1311 * Expected to be called with struct qla_hw_data->hardware_lock held
1312 */
1313static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
1314 scsi_qla_host_t *vha,
1315 const uint8_t *s_id)
1316{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001317 struct tcm_qla2xxx_lport *lport;
1318 struct se_node_acl *se_nacl;
1319 struct tcm_qla2xxx_nacl *nacl;
1320 u32 key;
1321
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001322 lport = vha->vha_tgt.target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001323 if (!lport) {
1324 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1325 dump_stack();
1326 return NULL;
1327 }
1328
1329 key = (((unsigned long)s_id[0] << 16) |
1330 ((unsigned long)s_id[1] << 8) |
1331 (unsigned long)s_id[2]);
1332 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1333
1334 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1335 if (!se_nacl) {
1336 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1337 return NULL;
1338 }
1339 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1340 se_nacl, se_nacl->initiatorname);
1341
1342 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1343 if (!nacl->qla_tgt_sess) {
1344 pr_err("Unable to locate struct qla_tgt_sess\n");
1345 return NULL;
1346 }
1347
1348 return nacl->qla_tgt_sess;
1349}
1350
1351/*
1352 * Expected to be called with struct qla_hw_data->hardware_lock held
1353 */
1354static void tcm_qla2xxx_set_sess_by_s_id(
1355 struct tcm_qla2xxx_lport *lport,
1356 struct se_node_acl *new_se_nacl,
1357 struct tcm_qla2xxx_nacl *nacl,
1358 struct se_session *se_sess,
1359 struct qla_tgt_sess *qla_tgt_sess,
1360 uint8_t *s_id)
1361{
1362 u32 key;
1363 void *slot;
1364 int rc;
1365
1366 key = (((unsigned long)s_id[0] << 16) |
1367 ((unsigned long)s_id[1] << 8) |
1368 (unsigned long)s_id[2]);
1369 pr_debug("set_sess_by_s_id: %06x\n", key);
1370
1371 slot = btree_lookup32(&lport->lport_fcport_map, key);
1372 if (!slot) {
1373 if (new_se_nacl) {
1374 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1375 nacl->nport_id = key;
1376 rc = btree_insert32(&lport->lport_fcport_map, key,
1377 new_se_nacl, GFP_ATOMIC);
1378 if (rc)
1379 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1380 (int)key);
1381 } else {
1382 pr_debug("Wiping nonexisting fc_port entry\n");
1383 }
1384
1385 qla_tgt_sess->se_sess = se_sess;
1386 nacl->qla_tgt_sess = qla_tgt_sess;
1387 return;
1388 }
1389
1390 if (nacl->qla_tgt_sess) {
1391 if (new_se_nacl == NULL) {
1392 pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
1393 btree_remove32(&lport->lport_fcport_map, key);
1394 nacl->qla_tgt_sess = NULL;
1395 return;
1396 }
1397 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
1398 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1399 qla_tgt_sess->se_sess = se_sess;
1400 nacl->qla_tgt_sess = qla_tgt_sess;
1401 return;
1402 }
1403
1404 if (new_se_nacl == NULL) {
1405 pr_debug("Clearing existing fc_port entry\n");
1406 btree_remove32(&lport->lport_fcport_map, key);
1407 return;
1408 }
1409
1410 pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
1411 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1412 qla_tgt_sess->se_sess = se_sess;
1413 nacl->qla_tgt_sess = qla_tgt_sess;
1414
1415 pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
1416 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1417}
1418
1419/*
1420 * Expected to be called with struct qla_hw_data->hardware_lock held
1421 */
1422static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
1423 scsi_qla_host_t *vha,
1424 const uint16_t loop_id)
1425{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001426 struct tcm_qla2xxx_lport *lport;
1427 struct se_node_acl *se_nacl;
1428 struct tcm_qla2xxx_nacl *nacl;
1429 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1430
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001431 lport = vha->vha_tgt.target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001432 if (!lport) {
1433 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1434 dump_stack();
1435 return NULL;
1436 }
1437
1438 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1439
1440 fc_loopid = lport->lport_loopid_map + loop_id;
1441 se_nacl = fc_loopid->se_nacl;
1442 if (!se_nacl) {
1443 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1444 loop_id);
1445 return NULL;
1446 }
1447
1448 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1449
1450 if (!nacl->qla_tgt_sess) {
1451 pr_err("Unable to locate struct qla_tgt_sess\n");
1452 return NULL;
1453 }
1454
1455 return nacl->qla_tgt_sess;
1456}
1457
1458/*
1459 * Expected to be called with struct qla_hw_data->hardware_lock held
1460 */
1461static void tcm_qla2xxx_set_sess_by_loop_id(
1462 struct tcm_qla2xxx_lport *lport,
1463 struct se_node_acl *new_se_nacl,
1464 struct tcm_qla2xxx_nacl *nacl,
1465 struct se_session *se_sess,
1466 struct qla_tgt_sess *qla_tgt_sess,
1467 uint16_t loop_id)
1468{
1469 struct se_node_acl *saved_nacl;
1470 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1471
1472 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1473
1474 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1475 lport->lport_loopid_map)[loop_id];
1476
1477 saved_nacl = fc_loopid->se_nacl;
1478 if (!saved_nacl) {
1479 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1480 fc_loopid->se_nacl = new_se_nacl;
1481 if (qla_tgt_sess->se_sess != se_sess)
1482 qla_tgt_sess->se_sess = se_sess;
1483 if (nacl->qla_tgt_sess != qla_tgt_sess)
1484 nacl->qla_tgt_sess = qla_tgt_sess;
1485 return;
1486 }
1487
1488 if (nacl->qla_tgt_sess) {
1489 if (new_se_nacl == NULL) {
1490 pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1491 fc_loopid->se_nacl = NULL;
1492 nacl->qla_tgt_sess = NULL;
1493 return;
1494 }
1495
1496 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1497 fc_loopid->se_nacl = new_se_nacl;
1498 if (qla_tgt_sess->se_sess != se_sess)
1499 qla_tgt_sess->se_sess = se_sess;
1500 if (nacl->qla_tgt_sess != qla_tgt_sess)
1501 nacl->qla_tgt_sess = qla_tgt_sess;
1502 return;
1503 }
1504
1505 if (new_se_nacl == NULL) {
1506 pr_debug("Clearing fc_loopid->se_nacl\n");
1507 fc_loopid->se_nacl = NULL;
1508 return;
1509 }
1510
1511 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
1512 fc_loopid->se_nacl = new_se_nacl;
1513 if (qla_tgt_sess->se_sess != se_sess)
1514 qla_tgt_sess->se_sess = se_sess;
1515 if (nacl->qla_tgt_sess != qla_tgt_sess)
1516 nacl->qla_tgt_sess = qla_tgt_sess;
1517
1518 pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1519 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1520}
1521
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -07001522/*
1523 * Should always be called with qla_hw_data->hardware_lock held.
1524 */
1525static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1526 struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
1527{
1528 struct se_session *se_sess = sess->se_sess;
1529 unsigned char be_sid[3];
1530
1531 be_sid[0] = sess->s_id.b.domain;
1532 be_sid[1] = sess->s_id.b.area;
1533 be_sid[2] = sess->s_id.b.al_pa;
1534
1535 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1536 sess, be_sid);
1537 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1538 sess, sess->loop_id);
1539}
1540
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001541static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
1542{
1543 struct qla_tgt *tgt = sess->tgt;
1544 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001545 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001546 struct se_session *se_sess;
1547 struct se_node_acl *se_nacl;
1548 struct tcm_qla2xxx_lport *lport;
1549 struct tcm_qla2xxx_nacl *nacl;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001550
1551 BUG_ON(in_interrupt());
1552
1553 se_sess = sess->se_sess;
1554 if (!se_sess) {
1555 pr_err("struct qla_tgt_sess->se_sess is NULL\n");
1556 dump_stack();
1557 return;
1558 }
1559 se_nacl = se_sess->se_node_acl;
1560 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1561
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001562 lport = vha->vha_tgt.target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001563 if (!lport) {
1564 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1565 dump_stack();
1566 return;
1567 }
Joern Engelbe646c2d2013-05-15 00:44:07 -07001568 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001569
1570 transport_deregister_session_configfs(sess->se_sess);
1571 transport_deregister_session(sess->se_sess);
1572}
1573
1574/*
1575 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1576 * to locate struct se_node_acl
1577 */
1578static int tcm_qla2xxx_check_initiator_node_acl(
1579 scsi_qla_host_t *vha,
1580 unsigned char *fc_wwpn,
1581 void *qla_tgt_sess,
1582 uint8_t *s_id,
1583 uint16_t loop_id)
1584{
1585 struct qla_hw_data *ha = vha->hw;
1586 struct tcm_qla2xxx_lport *lport;
1587 struct tcm_qla2xxx_tpg *tpg;
1588 struct tcm_qla2xxx_nacl *nacl;
1589 struct se_portal_group *se_tpg;
1590 struct se_node_acl *se_nacl;
1591 struct se_session *se_sess;
1592 struct qla_tgt_sess *sess = qla_tgt_sess;
1593 unsigned char port_name[36];
1594 unsigned long flags;
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07001595 int num_tags = (ha->fw_xcb_count) ? ha->fw_xcb_count :
1596 TCM_QLA2XXX_DEFAULT_TAGS;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001597
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001598 lport = vha->vha_tgt.target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001599 if (!lport) {
1600 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1601 dump_stack();
1602 return -EINVAL;
1603 }
1604 /*
1605 * Locate the TPG=1 reference..
1606 */
1607 tpg = lport->tpg_1;
1608 if (!tpg) {
1609 pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1610 return -EINVAL;
1611 }
1612 se_tpg = &tpg->se_tpg;
1613
Nicholas Bellinger51a07f82014-05-23 02:00:56 -07001614 se_sess = transport_init_session_tags(num_tags,
1615 sizeof(struct qla_tgt_cmd),
Nicholas Bellinger59bb0ff2015-02-08 12:49:46 -08001616 TARGET_PROT_ALL);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001617 if (IS_ERR(se_sess)) {
1618 pr_err("Unable to initialize struct se_session\n");
1619 return PTR_ERR(se_sess);
1620 }
1621 /*
1622 * Format the FCP Initiator port_name into colon seperated values to
1623 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1624 */
1625 memset(&port_name, 0, 36);
Andy Shevchenko3c9786e2015-01-15 13:28:07 +02001626 snprintf(port_name, sizeof(port_name), "%8phC", fc_wwpn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001627 /*
1628 * Locate our struct se_node_acl either from an explict NodeACL created
1629 * via ConfigFS, or via running in TPG demo mode.
1630 */
1631 se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
1632 port_name);
1633 if (!se_sess->se_node_acl) {
1634 transport_free_session(se_sess);
1635 return -EINVAL;
1636 }
1637 se_nacl = se_sess->se_node_acl;
1638 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1639 /*
1640 * And now setup the new se_nacl and session pointers into our HW lport
1641 * mappings for fabric S_ID and LOOP_ID.
1642 */
1643 spin_lock_irqsave(&ha->hardware_lock, flags);
1644 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
1645 qla_tgt_sess, s_id);
1646 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
1647 qla_tgt_sess, loop_id);
1648 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1649 /*
1650 * Finally register the new FC Nexus with TCM
1651 */
1652 __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
1653
1654 return 0;
1655}
1656
Roland Dreierc8292d12012-10-11 13:41:32 -07001657static void tcm_qla2xxx_update_sess(struct qla_tgt_sess *sess, port_id_t s_id,
1658 uint16_t loop_id, bool conf_compl_supported)
1659{
1660 struct qla_tgt *tgt = sess->tgt;
1661 struct qla_hw_data *ha = tgt->ha;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001662 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
1663 struct tcm_qla2xxx_lport *lport = vha->vha_tgt.target_lport_ptr;
Roland Dreierc8292d12012-10-11 13:41:32 -07001664 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1665 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1666 struct tcm_qla2xxx_nacl, se_node_acl);
1667 u32 key;
1668
1669
1670 if (sess->loop_id != loop_id || sess->s_id.b24 != s_id.b24)
Oleksandr Khoshaba7b8335582013-08-27 01:37:27 -04001671 pr_info("Updating session %p from port %8phC loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1672 sess, sess->port_name,
1673 sess->loop_id, loop_id, sess->s_id.b.domain,
1674 sess->s_id.b.area, sess->s_id.b.al_pa, s_id.b.domain,
1675 s_id.b.area, s_id.b.al_pa);
Roland Dreierc8292d12012-10-11 13:41:32 -07001676
1677 if (sess->loop_id != loop_id) {
1678 /*
1679 * Because we can shuffle loop IDs around and we
1680 * update different sessions non-atomically, we might
1681 * have overwritten this session's old loop ID
1682 * already, and we might end up overwriting some other
1683 * session that will be updated later. So we have to
1684 * be extra careful and we can't warn about those things...
1685 */
1686 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1687 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1688
1689 lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1690
1691 sess->loop_id = loop_id;
1692 }
1693
1694 if (sess->s_id.b24 != s_id.b24) {
1695 key = (((u32) sess->s_id.b.domain << 16) |
1696 ((u32) sess->s_id.b.area << 8) |
1697 ((u32) sess->s_id.b.al_pa));
1698
1699 if (btree_lookup32(&lport->lport_fcport_map, key))
1700 WARN(btree_remove32(&lport->lport_fcport_map, key) != se_nacl,
1701 "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1702 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1703 else
1704 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1705 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1706
1707 key = (((u32) s_id.b.domain << 16) |
1708 ((u32) s_id.b.area << 8) |
1709 ((u32) s_id.b.al_pa));
1710
1711 if (btree_lookup32(&lport->lport_fcport_map, key)) {
1712 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1713 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1714 btree_update32(&lport->lport_fcport_map, key, se_nacl);
1715 } else {
1716 btree_insert32(&lport->lport_fcport_map, key, se_nacl, GFP_ATOMIC);
1717 }
1718
1719 sess->s_id = s_id;
1720 nacl->nport_id = key;
1721 }
1722
1723 sess->conf_compl_supported = conf_compl_supported;
1724}
1725
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001726/*
1727 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1728 */
1729static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1730 .handle_cmd = tcm_qla2xxx_handle_cmd,
1731 .handle_data = tcm_qla2xxx_handle_data,
Quinn Tranf83adb62014-04-11 16:54:43 -04001732 .handle_dif_err = tcm_qla2xxx_handle_dif_err,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001733 .handle_tmr = tcm_qla2xxx_handle_tmr,
1734 .free_cmd = tcm_qla2xxx_free_cmd,
1735 .free_mcmd = tcm_qla2xxx_free_mcmd,
1736 .free_session = tcm_qla2xxx_free_session,
Roland Dreierc8292d12012-10-11 13:41:32 -07001737 .update_sess = tcm_qla2xxx_update_sess,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001738 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1739 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1740 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1741 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1742 .put_sess = tcm_qla2xxx_put_sess,
1743 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1744};
1745
1746static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1747{
1748 int rc;
1749
1750 rc = btree_init32(&lport->lport_fcport_map);
1751 if (rc) {
1752 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1753 return rc;
1754 }
1755
1756 lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
1757 65536);
1758 if (!lport->lport_loopid_map) {
1759 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1760 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1761 btree_destroy32(&lport->lport_fcport_map);
1762 return -ENOMEM;
1763 }
1764 memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
1765 * 65536);
1766 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1767 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1768 return 0;
1769}
1770
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001771static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha,
1772 void *target_lport_ptr,
1773 u64 npiv_wwpn, u64 npiv_wwnn)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001774{
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001775 struct qla_hw_data *ha = vha->hw;
1776 struct tcm_qla2xxx_lport *lport =
1777 (struct tcm_qla2xxx_lport *)target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001778 /*
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001779 * Setup tgt_ops, local pointer to vha and target_lport_ptr
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001780 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001781 ha->tgt.tgt_ops = &tcm_qla2xxx_template;
1782 vha->vha_tgt.target_lport_ptr = target_lport_ptr;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001783 lport->qla_vha = vha;
1784
1785 return 0;
1786}
1787
1788static struct se_wwn *tcm_qla2xxx_make_lport(
1789 struct target_fabric_configfs *tf,
1790 struct config_group *group,
1791 const char *name)
1792{
1793 struct tcm_qla2xxx_lport *lport;
1794 u64 wwpn;
1795 int ret = -ENODEV;
1796
1797 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1798 return ERR_PTR(-EINVAL);
1799
1800 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1801 if (!lport) {
1802 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1803 return ERR_PTR(-ENOMEM);
1804 }
1805 lport->lport_wwpn = wwpn;
1806 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1807 wwpn);
Roland Dreierc046aa02012-10-11 13:41:31 -07001808 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001809
1810 ret = tcm_qla2xxx_init_lport(lport);
1811 if (ret != 0)
1812 goto out;
1813
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001814 ret = qlt_lport_register(lport, wwpn, 0, 0,
1815 tcm_qla2xxx_lport_register_cb);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001816 if (ret != 0)
1817 goto out_lport;
1818
1819 return &lport->lport_wwn;
1820out_lport:
1821 vfree(lport->lport_loopid_map);
1822 btree_destroy32(&lport->lport_fcport_map);
1823out:
1824 kfree(lport);
1825 return ERR_PTR(ret);
1826}
1827
1828static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1829{
1830 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1831 struct tcm_qla2xxx_lport, lport_wwn);
1832 struct scsi_qla_host *vha = lport->qla_vha;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001833 struct se_node_acl *node;
1834 u32 key = 0;
1835
1836 /*
1837 * Call into qla2x_target.c LLD logic to complete the
1838 * shutdown of struct qla_tgt after the call to
1839 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1840 */
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001841 if (vha->vha_tgt.qla_tgt && !vha->vha_tgt.qla_tgt->tgt_stopped)
1842 qlt_stop_phase2(vha->vha_tgt.qla_tgt);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001843
1844 qlt_lport_deregister(vha);
1845
1846 vfree(lport->lport_loopid_map);
1847 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1848 btree_remove32(&lport->lport_fcport_map, key);
1849 btree_destroy32(&lport->lport_fcport_map);
1850 kfree(lport);
1851}
1852
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001853static int tcm_qla2xxx_lport_register_npiv_cb(struct scsi_qla_host *base_vha,
1854 void *target_lport_ptr,
1855 u64 npiv_wwpn, u64 npiv_wwnn)
1856{
1857 struct fc_vport *vport;
1858 struct Scsi_Host *sh = base_vha->host;
1859 struct scsi_qla_host *npiv_vha;
1860 struct tcm_qla2xxx_lport *lport =
1861 (struct tcm_qla2xxx_lport *)target_lport_ptr;
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001862 struct tcm_qla2xxx_lport *base_lport =
1863 (struct tcm_qla2xxx_lport *)base_vha->vha_tgt.target_lport_ptr;
1864 struct tcm_qla2xxx_tpg *base_tpg;
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001865 struct fc_vport_identifiers vport_id;
1866
1867 if (!qla_tgt_mode_enabled(base_vha)) {
1868 pr_err("qla2xxx base_vha not enabled for target mode\n");
1869 return -EPERM;
1870 }
1871
Nicholas Bellinger7474f522014-02-19 16:53:07 -08001872 if (!base_lport || !base_lport->tpg_1 ||
1873 !atomic_read(&base_lport->tpg_1->lport_tpg_enabled)) {
1874 pr_err("qla2xxx base_lport or tpg_1 not available\n");
1875 return -EPERM;
1876 }
1877 base_tpg = base_lport->tpg_1;
1878
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001879 memset(&vport_id, 0, sizeof(vport_id));
1880 vport_id.port_name = npiv_wwpn;
1881 vport_id.node_name = npiv_wwnn;
1882 vport_id.roles = FC_PORT_ROLE_FCP_INITIATOR;
1883 vport_id.vport_type = FC_PORTTYPE_NPIV;
1884 vport_id.disable = false;
1885
1886 vport = fc_vport_create(sh, 0, &vport_id);
1887 if (!vport) {
1888 pr_err("fc_vport_create failed for qla2xxx_npiv\n");
1889 return -ENODEV;
1890 }
1891 /*
1892 * Setup local pointer to NPIV vhba + target_lport_ptr
1893 */
1894 npiv_vha = (struct scsi_qla_host *)vport->dd_data;
1895 npiv_vha->vha_tgt.target_lport_ptr = target_lport_ptr;
1896 lport->qla_vha = npiv_vha;
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001897 scsi_host_get(npiv_vha->host);
1898 return 0;
1899}
1900
1901
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001902static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1903 struct target_fabric_configfs *tf,
1904 struct config_group *group,
1905 const char *name)
1906{
1907 struct tcm_qla2xxx_lport *lport;
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001908 u64 phys_wwpn, npiv_wwpn, npiv_wwnn;
1909 char *p, tmp[128];
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001910 int ret;
1911
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001912 snprintf(tmp, 128, "%s", name);
1913
1914 p = strchr(tmp, '@');
1915 if (!p) {
1916 pr_err("Unable to locate NPIV '@' seperator\n");
1917 return ERR_PTR(-EINVAL);
1918 }
1919 *p++ = '\0';
1920
1921 if (tcm_qla2xxx_parse_wwn(tmp, &phys_wwpn, 1) < 0)
1922 return ERR_PTR(-EINVAL);
1923
1924 if (tcm_qla2xxx_npiv_parse_wwn(p, strlen(p)+1,
1925 &npiv_wwpn, &npiv_wwnn) < 0)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001926 return ERR_PTR(-EINVAL);
1927
1928 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1929 if (!lport) {
1930 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1931 return ERR_PTR(-ENOMEM);
1932 }
1933 lport->lport_npiv_wwpn = npiv_wwpn;
1934 lport->lport_npiv_wwnn = npiv_wwnn;
Roland Dreierc046aa02012-10-11 13:41:31 -07001935 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001936
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001937 ret = tcm_qla2xxx_init_lport(lport);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001938 if (ret != 0)
1939 goto out;
1940
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001941 ret = qlt_lport_register(lport, phys_wwpn, npiv_wwpn, npiv_wwnn,
1942 tcm_qla2xxx_lport_register_npiv_cb);
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001943 if (ret != 0)
1944 goto out_lport;
1945
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001946 return &lport->lport_wwn;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001947out_lport:
1948 vfree(lport->lport_loopid_map);
1949 btree_destroy32(&lport->lport_fcport_map);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001950out:
1951 kfree(lport);
1952 return ERR_PTR(ret);
1953}
1954
1955static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1956{
1957 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1958 struct tcm_qla2xxx_lport, lport_wwn);
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001959 struct scsi_qla_host *npiv_vha = lport->qla_vha;
1960 struct qla_hw_data *ha = npiv_vha->hw;
1961 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1962
1963 scsi_host_put(npiv_vha->host);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001964 /*
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08001965 * Notify libfc that we want to release the vha->fc_vport
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001966 */
Nicholas Bellinger49a47f22014-01-14 20:38:58 -08001967 fc_vport_terminate(npiv_vha->fc_vport);
1968 scsi_host_put(base_vha->host);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001969 kfree(lport);
1970}
1971
1972
1973static ssize_t tcm_qla2xxx_wwn_show_attr_version(
1974 struct target_fabric_configfs *tf,
1975 char *page)
1976{
1977 return sprintf(page,
1978 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
1979 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1980 utsname()->machine);
1981}
1982
1983TF_WWN_ATTR_RO(tcm_qla2xxx, version);
1984
1985static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1986 &tcm_qla2xxx_wwn_version.attr,
1987 NULL,
1988};
1989
1990static struct target_core_fabric_ops tcm_qla2xxx_ops = {
1991 .get_fabric_name = tcm_qla2xxx_get_fabric_name,
1992 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1993 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1994 .tpg_get_tag = tcm_qla2xxx_get_tag,
1995 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1996 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1997 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1998 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1999 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
2000 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
2001 .tpg_check_demo_mode_write_protect =
2002 tcm_qla2xxx_check_demo_write_protect,
2003 .tpg_check_prod_mode_write_protect =
2004 tcm_qla2xxx_check_prod_write_protect,
Nicholas Bellinger64b16882015-03-27 23:30:57 -07002005 .tpg_check_prot_fabric_only = tcm_qla2xxx_check_prot_fabric_only,
Andy Groverde04a8a2013-07-19 15:06:38 -07002006 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002007 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
2008 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
2009 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002010 .check_stop_free = tcm_qla2xxx_check_stop_free,
2011 .release_cmd = tcm_qla2xxx_release_cmd,
Joern Engelaaf68b72012-05-18 13:58:23 -07002012 .put_session = tcm_qla2xxx_put_session,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002013 .shutdown_session = tcm_qla2xxx_shutdown_session,
2014 .close_session = tcm_qla2xxx_close_session,
2015 .sess_get_index = tcm_qla2xxx_sess_get_index,
2016 .sess_get_initiator_sid = NULL,
2017 .write_pending = tcm_qla2xxx_write_pending,
2018 .write_pending_status = tcm_qla2xxx_write_pending_status,
2019 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
2020 .get_task_tag = tcm_qla2xxx_get_task_tag,
2021 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
2022 .queue_data_in = tcm_qla2xxx_queue_data_in,
2023 .queue_status = tcm_qla2xxx_queue_status,
2024 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002025 .aborted_task = tcm_qla2xxx_aborted_task,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002026 /*
2027 * Setup function pointers for generic logic in
2028 * target_core_fabric_configfs.c
2029 */
2030 .fabric_make_wwn = tcm_qla2xxx_make_lport,
2031 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
2032 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
2033 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
2034 .fabric_post_link = NULL,
2035 .fabric_pre_unlink = NULL,
2036 .fabric_make_np = NULL,
2037 .fabric_drop_np = NULL,
2038 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
2039 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
2040};
2041
2042static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
2043 .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
2044 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
Nicholas Bellinger84197a32014-01-30 09:52:21 -08002045 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002046 .tpg_get_tag = tcm_qla2xxx_get_tag,
2047 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
2048 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
2049 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
2050 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002051 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
2052 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
2053 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_demo_mode,
2054 .tpg_check_prod_mode_write_protect =
2055 tcm_qla2xxx_check_prod_write_protect,
Andy Groverde04a8a2013-07-19 15:06:38 -07002056 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002057 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
2058 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
2059 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002060 .check_stop_free = tcm_qla2xxx_check_stop_free,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002061 .release_cmd = tcm_qla2xxx_release_cmd,
Joern Engelaaf68b72012-05-18 13:58:23 -07002062 .put_session = tcm_qla2xxx_put_session,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002063 .shutdown_session = tcm_qla2xxx_shutdown_session,
2064 .close_session = tcm_qla2xxx_close_session,
2065 .sess_get_index = tcm_qla2xxx_sess_get_index,
2066 .sess_get_initiator_sid = NULL,
2067 .write_pending = tcm_qla2xxx_write_pending,
2068 .write_pending_status = tcm_qla2xxx_write_pending_status,
2069 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
2070 .get_task_tag = tcm_qla2xxx_get_task_tag,
2071 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
2072 .queue_data_in = tcm_qla2xxx_queue_data_in,
2073 .queue_status = tcm_qla2xxx_queue_status,
2074 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
Nicholas Bellinger131e6ab2014-03-22 14:55:56 -07002075 .aborted_task = tcm_qla2xxx_aborted_task,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002076 /*
2077 * Setup function pointers for generic logic in
2078 * target_core_fabric_configfs.c
2079 */
2080 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
2081 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
2082 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
2083 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
2084 .fabric_post_link = NULL,
2085 .fabric_pre_unlink = NULL,
2086 .fabric_make_np = NULL,
2087 .fabric_drop_np = NULL,
2088 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
2089 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
2090};
2091
2092static int tcm_qla2xxx_register_configfs(void)
2093{
2094 struct target_fabric_configfs *fabric, *npiv_fabric;
2095 int ret;
2096
2097 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
2098 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
2099 utsname()->machine);
2100 /*
2101 * Register the top level struct config_item_type with TCM core
2102 */
2103 fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
2104 if (IS_ERR(fabric)) {
2105 pr_err("target_fabric_configfs_init() failed\n");
2106 return PTR_ERR(fabric);
2107 }
2108 /*
2109 * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
2110 */
2111 fabric->tf_ops = tcm_qla2xxx_ops;
2112 /*
2113 * Setup default attribute lists for various fabric->tf_cit_tmpl
2114 */
Andy Groverd80e224d2013-10-09 11:05:56 -07002115 fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
2116 fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
2117 fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs =
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002118 tcm_qla2xxx_tpg_attrib_attrs;
Andy Groverd80e224d2013-10-09 11:05:56 -07002119 fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
2120 fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2121 fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2122 fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2123 fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2124 fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002125 /*
2126 * Register the fabric for use within TCM
2127 */
2128 ret = target_fabric_configfs_register(fabric);
2129 if (ret < 0) {
2130 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
2131 return ret;
2132 }
2133 /*
2134 * Setup our local pointer to *fabric
2135 */
2136 tcm_qla2xxx_fabric_configfs = fabric;
2137 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
2138
2139 /*
2140 * Register the top level struct config_item_type for NPIV with TCM core
2141 */
2142 npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
2143 if (IS_ERR(npiv_fabric)) {
2144 pr_err("target_fabric_configfs_init() failed\n");
2145 ret = PTR_ERR(npiv_fabric);
2146 goto out_fabric;
2147 }
2148 /*
2149 * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
2150 */
2151 npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
2152 /*
2153 * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
2154 */
Andy Groverd80e224d2013-10-09 11:05:56 -07002155 npiv_fabric->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
Saurav Kashyap0e8cd712014-01-14 20:40:38 -08002156 npiv_fabric->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs =
Nicholas Bellinger394d62b2014-02-19 16:52:15 -08002157 tcm_qla2xxx_npiv_tpg_attrs;
Andy Groverd80e224d2013-10-09 11:05:56 -07002158 npiv_fabric->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = NULL;
2159 npiv_fabric->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
2160 npiv_fabric->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
2161 npiv_fabric->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
2162 npiv_fabric->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
2163 npiv_fabric->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
2164 npiv_fabric->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04002165 /*
2166 * Register the npiv_fabric for use within TCM
2167 */
2168 ret = target_fabric_configfs_register(npiv_fabric);
2169 if (ret < 0) {
2170 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
2171 goto out_fabric;
2172 }
2173 /*
2174 * Setup our local pointer to *npiv_fabric
2175 */
2176 tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
2177 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
2178
2179 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
2180 WQ_MEM_RECLAIM, 0);
2181 if (!tcm_qla2xxx_free_wq) {
2182 ret = -ENOMEM;
2183 goto out_fabric_npiv;
2184 }
2185
2186 tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
2187 if (!tcm_qla2xxx_cmd_wq) {
2188 ret = -ENOMEM;
2189 goto out_free_wq;
2190 }
2191
2192 return 0;
2193
2194out_free_wq:
2195 destroy_workqueue(tcm_qla2xxx_free_wq);
2196out_fabric_npiv:
2197 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
2198out_fabric:
2199 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
2200 return ret;
2201}
2202
2203static void tcm_qla2xxx_deregister_configfs(void)
2204{
2205 destroy_workqueue(tcm_qla2xxx_cmd_wq);
2206 destroy_workqueue(tcm_qla2xxx_free_wq);
2207
2208 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
2209 tcm_qla2xxx_fabric_configfs = NULL;
2210 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
2211
2212 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
2213 tcm_qla2xxx_npiv_fabric_configfs = NULL;
2214 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
2215}
2216
2217static int __init tcm_qla2xxx_init(void)
2218{
2219 int ret;
2220
2221 ret = tcm_qla2xxx_register_configfs();
2222 if (ret < 0)
2223 return ret;
2224
2225 return 0;
2226}
2227
2228static void __exit tcm_qla2xxx_exit(void)
2229{
2230 tcm_qla2xxx_deregister_configfs();
2231}
2232
2233MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
2234MODULE_LICENSE("GPL");
2235module_init(tcm_qla2xxx_init);
2236module_exit(tcm_qla2xxx_exit);