blob: 69225bd0aebba99af20b1ad7da97d5aeb1524a53 [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
56static int tcm_qla2xxx_check_true(struct se_portal_group *se_tpg)
57{
58 return 1;
59}
60
61static int tcm_qla2xxx_check_false(struct se_portal_group *se_tpg)
62{
63 return 0;
64}
65
66/*
67 * Parse WWN.
68 * If strict, we require lower-case hex and colon separators to be sure
69 * the name is the same as what would be generated by ft_format_wwn()
70 * so the name and wwn are mapped one-to-one.
71 */
72static ssize_t tcm_qla2xxx_parse_wwn(const char *name, u64 *wwn, int strict)
73{
74 const char *cp;
75 char c;
76 u32 nibble;
77 u32 byte = 0;
78 u32 pos = 0;
79 u32 err;
80
81 *wwn = 0;
82 for (cp = name; cp < &name[TCM_QLA2XXX_NAMELEN - 1]; cp++) {
83 c = *cp;
84 if (c == '\n' && cp[1] == '\0')
85 continue;
86 if (strict && pos++ == 2 && byte++ < 7) {
87 pos = 0;
88 if (c == ':')
89 continue;
90 err = 1;
91 goto fail;
92 }
93 if (c == '\0') {
94 err = 2;
95 if (strict && byte != 8)
96 goto fail;
97 return cp - name;
98 }
99 err = 3;
100 if (isdigit(c))
101 nibble = c - '0';
102 else if (isxdigit(c) && (islower(c) || !strict))
103 nibble = tolower(c) - 'a' + 10;
104 else
105 goto fail;
106 *wwn = (*wwn << 4) | nibble;
107 }
108 err = 4;
109fail:
110 pr_debug("err %u len %zu pos %u byte %u\n",
111 err, cp - name, pos, byte);
112 return -1;
113}
114
115static ssize_t tcm_qla2xxx_format_wwn(char *buf, size_t len, u64 wwn)
116{
117 u8 b[8];
118
119 put_unaligned_be64(wwn, b);
120 return snprintf(buf, len,
121 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
122 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
123}
124
125static char *tcm_qla2xxx_get_fabric_name(void)
126{
127 return "qla2xxx";
128}
129
130/*
131 * From drivers/scsi/scsi_transport_fc.c:fc_parse_wwn
132 */
133static int tcm_qla2xxx_npiv_extract_wwn(const char *ns, u64 *nm)
134{
Roland Dreierd4f75b52012-06-11 18:31:31 -0700135 unsigned int i, j;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400136 u8 wwn[8];
137
138 memset(wwn, 0, sizeof(wwn));
139
140 /* Validate and store the new name */
141 for (i = 0, j = 0; i < 16; i++) {
Roland Dreierd4f75b52012-06-11 18:31:31 -0700142 int value;
143
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400144 value = hex_to_bin(*ns++);
145 if (value >= 0)
146 j = (j << 4) | value;
147 else
148 return -EINVAL;
149
150 if (i % 2) {
151 wwn[i/2] = j & 0xff;
152 j = 0;
153 }
154 }
155
156 *nm = wwn_to_u64(wwn);
157 return 0;
158}
159
160/*
161 * This parsing logic follows drivers/scsi/scsi_transport_fc.c:
162 * store_fc_host_vport_create()
163 */
164static int tcm_qla2xxx_npiv_parse_wwn(
165 const char *name,
166 size_t count,
167 u64 *wwpn,
168 u64 *wwnn)
169{
170 unsigned int cnt = count;
171 int rc;
172
173 *wwpn = 0;
174 *wwnn = 0;
175
176 /* count may include a LF at end of string */
177 if (name[cnt-1] == '\n')
178 cnt--;
179
180 /* validate we have enough characters for WWPN */
181 if ((cnt != (16+1+16)) || (name[16] != ':'))
182 return -EINVAL;
183
184 rc = tcm_qla2xxx_npiv_extract_wwn(&name[0], wwpn);
185 if (rc != 0)
186 return rc;
187
188 rc = tcm_qla2xxx_npiv_extract_wwn(&name[17], wwnn);
189 if (rc != 0)
190 return rc;
191
192 return 0;
193}
194
195static ssize_t tcm_qla2xxx_npiv_format_wwn(char *buf, size_t len,
196 u64 wwpn, u64 wwnn)
197{
198 u8 b[8], b2[8];
199
200 put_unaligned_be64(wwpn, b);
201 put_unaligned_be64(wwnn, b2);
202 return snprintf(buf, len,
203 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x,"
204 "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x",
205 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
206 b2[0], b2[1], b2[2], b2[3], b2[4], b2[5], b2[6], b2[7]);
207}
208
209static char *tcm_qla2xxx_npiv_get_fabric_name(void)
210{
211 return "qla2xxx_npiv";
212}
213
214static u8 tcm_qla2xxx_get_fabric_proto_ident(struct se_portal_group *se_tpg)
215{
216 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
217 struct tcm_qla2xxx_tpg, se_tpg);
218 struct tcm_qla2xxx_lport *lport = tpg->lport;
219 u8 proto_id;
220
221 switch (lport->lport_proto_id) {
222 case SCSI_PROTOCOL_FCP:
223 default:
224 proto_id = fc_get_fabric_proto_ident(se_tpg);
225 break;
226 }
227
228 return proto_id;
229}
230
231static char *tcm_qla2xxx_get_fabric_wwn(struct se_portal_group *se_tpg)
232{
233 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
234 struct tcm_qla2xxx_tpg, se_tpg);
235 struct tcm_qla2xxx_lport *lport = tpg->lport;
236
Roland Dreierc046aa02012-10-11 13:41:31 -0700237 return lport->lport_naa_name;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400238}
239
240static char *tcm_qla2xxx_npiv_get_fabric_wwn(struct se_portal_group *se_tpg)
241{
242 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
243 struct tcm_qla2xxx_tpg, se_tpg);
244 struct tcm_qla2xxx_lport *lport = tpg->lport;
245
246 return &lport->lport_npiv_name[0];
247}
248
249static u16 tcm_qla2xxx_get_tag(struct se_portal_group *se_tpg)
250{
251 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
252 struct tcm_qla2xxx_tpg, se_tpg);
253 return tpg->lport_tpgt;
254}
255
256static u32 tcm_qla2xxx_get_default_depth(struct se_portal_group *se_tpg)
257{
258 return 1;
259}
260
261static u32 tcm_qla2xxx_get_pr_transport_id(
262 struct se_portal_group *se_tpg,
263 struct se_node_acl *se_nacl,
264 struct t10_pr_registration *pr_reg,
265 int *format_code,
266 unsigned char *buf)
267{
268 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
269 struct tcm_qla2xxx_tpg, se_tpg);
270 struct tcm_qla2xxx_lport *lport = tpg->lport;
271 int ret = 0;
272
273 switch (lport->lport_proto_id) {
274 case SCSI_PROTOCOL_FCP:
275 default:
276 ret = fc_get_pr_transport_id(se_tpg, se_nacl, pr_reg,
277 format_code, buf);
278 break;
279 }
280
281 return ret;
282}
283
284static u32 tcm_qla2xxx_get_pr_transport_id_len(
285 struct se_portal_group *se_tpg,
286 struct se_node_acl *se_nacl,
287 struct t10_pr_registration *pr_reg,
288 int *format_code)
289{
290 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
291 struct tcm_qla2xxx_tpg, se_tpg);
292 struct tcm_qla2xxx_lport *lport = tpg->lport;
293 int ret = 0;
294
295 switch (lport->lport_proto_id) {
296 case SCSI_PROTOCOL_FCP:
297 default:
298 ret = fc_get_pr_transport_id_len(se_tpg, se_nacl, pr_reg,
299 format_code);
300 break;
301 }
302
303 return ret;
304}
305
306static char *tcm_qla2xxx_parse_pr_out_transport_id(
307 struct se_portal_group *se_tpg,
308 const char *buf,
309 u32 *out_tid_len,
310 char **port_nexus_ptr)
311{
312 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
313 struct tcm_qla2xxx_tpg, se_tpg);
314 struct tcm_qla2xxx_lport *lport = tpg->lport;
315 char *tid = NULL;
316
317 switch (lport->lport_proto_id) {
318 case SCSI_PROTOCOL_FCP:
319 default:
320 tid = fc_parse_pr_out_transport_id(se_tpg, buf, out_tid_len,
321 port_nexus_ptr);
322 break;
323 }
324
325 return tid;
326}
327
328static int tcm_qla2xxx_check_demo_mode(struct se_portal_group *se_tpg)
329{
330 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
331 struct tcm_qla2xxx_tpg, se_tpg);
332
333 return QLA_TPG_ATTRIB(tpg)->generate_node_acls;
334}
335
336static int tcm_qla2xxx_check_demo_mode_cache(struct se_portal_group *se_tpg)
337{
338 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
339 struct tcm_qla2xxx_tpg, se_tpg);
340
341 return QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls;
342}
343
344static int tcm_qla2xxx_check_demo_write_protect(struct se_portal_group *se_tpg)
345{
346 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
347 struct tcm_qla2xxx_tpg, se_tpg);
348
349 return QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect;
350}
351
352static int tcm_qla2xxx_check_prod_write_protect(struct se_portal_group *se_tpg)
353{
354 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
355 struct tcm_qla2xxx_tpg, se_tpg);
356
357 return QLA_TPG_ATTRIB(tpg)->prod_mode_write_protect;
358}
359
Andy Groverde04a8a2013-07-19 15:06:38 -0700360static int tcm_qla2xxx_check_demo_mode_login_only(struct se_portal_group *se_tpg)
361{
362 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
363 struct tcm_qla2xxx_tpg, se_tpg);
364
365 return QLA_TPG_ATTRIB(tpg)->demo_mode_login_only;
366}
367
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400368static struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
369 struct se_portal_group *se_tpg)
370{
371 struct tcm_qla2xxx_nacl *nacl;
372
373 nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
374 if (!nacl) {
Masanari Iida6efb3c02012-10-26 22:10:54 +0900375 pr_err("Unable to allocate struct tcm_qla2xxx_nacl\n");
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400376 return NULL;
377 }
378
379 return &nacl->se_node_acl;
380}
381
382static void tcm_qla2xxx_release_fabric_acl(
383 struct se_portal_group *se_tpg,
384 struct se_node_acl *se_nacl)
385{
386 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
387 struct tcm_qla2xxx_nacl, se_node_acl);
388 kfree(nacl);
389}
390
391static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
392{
393 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
394 struct tcm_qla2xxx_tpg, se_tpg);
395
396 return tpg->lport_tpgt;
397}
398
399static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
400{
401 struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
402 struct qla_tgt_mgmt_cmd, free_work);
403
404 transport_generic_free_cmd(&mcmd->se_cmd, 0);
405}
406
407/*
408 * Called from qla_target_template->free_mcmd(), and will call
409 * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
410 * release callback. qla_hw_data->hardware_lock is expected to be held
411 */
412static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
413{
414 INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
415 queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
416}
417
418static void tcm_qla2xxx_complete_free(struct work_struct *work)
419{
420 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
421
422 transport_generic_free_cmd(&cmd->se_cmd, 0);
423}
424
425/*
426 * Called from qla_target_template->free_cmd(), and will call
427 * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
428 * release callback. qla_hw_data->hardware_lock is expected to be held
429 */
430static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
431{
432 INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
433 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
434}
435
436/*
437 * Called from struct target_core_fabric_ops->check_stop_free() context
438 */
439static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
440{
441 return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
442}
443
444/* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
445 * fabric descriptor @se_cmd command to release
446 */
447static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
448{
449 struct qla_tgt_cmd *cmd;
450
451 if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
452 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
453 struct qla_tgt_mgmt_cmd, se_cmd);
454 qlt_free_mcmd(mcmd);
455 return;
456 }
457
458 cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
459 qlt_free_cmd(cmd);
460}
461
462static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
463{
464 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
465 struct scsi_qla_host *vha;
466 unsigned long flags;
467
468 BUG_ON(!sess);
469 vha = sess->vha;
470
471 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
Roland Dreier1c7b13f2012-07-16 11:04:42 -0700472 target_sess_cmd_list_set_waiting(se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400473 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
474
475 return 1;
476}
477
478static void tcm_qla2xxx_close_session(struct se_session *se_sess)
479{
480 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
481 struct scsi_qla_host *vha;
482 unsigned long flags;
483
484 BUG_ON(!sess);
485 vha = sess->vha;
486
487 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
488 qlt_unreg_sess(sess);
489 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
490}
491
492static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
493{
494 return 0;
495}
496
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400497static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
498{
499 struct qla_tgt_cmd *cmd = container_of(se_cmd,
500 struct qla_tgt_cmd, se_cmd);
501
502 cmd->bufflen = se_cmd->data_length;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700503 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400504
505 cmd->sg_cnt = se_cmd->t_data_nents;
506 cmd->sg = se_cmd->t_data_sg;
507
508 /*
509 * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
510 * the SGL mappings into PCIe memory for incoming FCP WRITE data.
511 */
512 return qlt_rdy_to_xfer(cmd);
513}
514
515static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
516{
517 unsigned long flags;
518 /*
519 * Check for WRITE_PENDING status to determine if we need to wait for
520 * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
521 */
522 spin_lock_irqsave(&se_cmd->t_state_lock, flags);
523 if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
524 se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
525 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
526 wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
527 3000);
528 return 0;
529 }
530 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
531
532 return 0;
533}
534
535static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
536{
537 return;
538}
539
540static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
541{
542 struct qla_tgt_cmd *cmd = container_of(se_cmd,
543 struct qla_tgt_cmd, se_cmd);
544
545 return cmd->tag;
546}
547
548static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
549{
550 return 0;
551}
552
553/*
554 * Called from process context in qla_target.c:qlt_do_work() code
555 */
556static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
557 unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
558 int data_dir, int bidi)
559{
560 struct se_cmd *se_cmd = &cmd->se_cmd;
561 struct se_session *se_sess;
562 struct qla_tgt_sess *sess;
563 int flags = TARGET_SCF_ACK_KREF;
564
565 if (bidi)
566 flags |= TARGET_SCF_BIDI_OP;
567
568 sess = cmd->sess;
569 if (!sess) {
570 pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
571 return -EINVAL;
572 }
573
574 se_sess = sess->se_sess;
575 if (!se_sess) {
576 pr_err("Unable to locate active struct se_session\n");
577 return -EINVAL;
578 }
579
Roland Dreierd6dfc862012-07-16 11:04:39 -0700580 return target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400581 cmd->unpacked_lun, data_length, fcp_task_attr,
582 data_dir, flags);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400583}
584
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400585static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400586{
587 struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400588
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400589 /*
590 * Ensure that the complete FCP WRITE payload has been received.
591 * Otherwise return an exception via CHECK_CONDITION status.
592 */
593 if (!cmd->write_data_transferred) {
594 /*
595 * Check if se_cmd has already been aborted via LUN_RESET, and
596 * waiting upon completion in tcm_qla2xxx_write_pending_status()
597 */
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400598 if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
599 complete(&cmd->se_cmd.t_transport_stop_comp);
600 return;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400601 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400602
Christoph Hellwigde103c92012-11-06 12:24:09 -0800603 transport_generic_request_failure(&cmd->se_cmd,
604 TCM_CHECK_CONDITION_ABORT_CMD);
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400605 return;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400606 }
Christoph Hellwig43381ce2012-07-08 15:58:44 -0400607
608 return target_execute_cmd(&cmd->se_cmd);
609}
610
611/*
612 * Called from qla_target.c:qlt_do_ctio_completion()
613 */
614static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
615{
616 INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
617 queue_work(tcm_qla2xxx_free_wq, &cmd->work);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400618}
619
620/*
621 * Called from qla_target.c:qlt_issue_task_mgmt()
622 */
Roland Dreier9389c3c2012-06-11 18:31:30 -0700623static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
624 uint8_t tmr_func, uint32_t tag)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400625{
626 struct qla_tgt_sess *sess = mcmd->sess;
627 struct se_cmd *se_cmd = &mcmd->se_cmd;
628
629 return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
630 tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
631}
632
633static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
634{
635 struct qla_tgt_cmd *cmd = container_of(se_cmd,
636 struct qla_tgt_cmd, se_cmd);
637
638 cmd->bufflen = se_cmd->data_length;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700639 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400640 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
641
642 cmd->sg_cnt = se_cmd->t_data_nents;
643 cmd->sg = se_cmd->t_data_sg;
644 cmd->offset = 0;
645
646 /*
647 * Now queue completed DATA_IN the qla2xxx LLD and response ring
648 */
649 return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
650 se_cmd->scsi_status);
651}
652
653static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
654{
655 struct qla_tgt_cmd *cmd = container_of(se_cmd,
656 struct qla_tgt_cmd, se_cmd);
657 int xmit_type = QLA_TGT_XMIT_STATUS;
658
659 cmd->bufflen = se_cmd->data_length;
660 cmd->sg = NULL;
661 cmd->sg_cnt = 0;
662 cmd->offset = 0;
Nicholas Bellingerb3faa2e2013-08-21 14:54:54 -0700663 cmd->dma_data_direction = target_reverse_dma_direction(se_cmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400664 cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
665
666 if (se_cmd->data_direction == DMA_FROM_DEVICE) {
667 /*
668 * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
669 * for qla_tgt_xmit_response LLD code
670 */
Roland Dreierb5aff3d2013-06-05 09:54:17 -0700671 if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
672 se_cmd->se_cmd_flags &= ~SCF_OVERFLOW_BIT;
673 se_cmd->residual_count = 0;
674 }
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400675 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
Roland Dreierb5aff3d2013-06-05 09:54:17 -0700676 se_cmd->residual_count += se_cmd->data_length;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400677
678 cmd->bufflen = 0;
679 }
680 /*
681 * Now queue status response to qla2xxx LLD code and response ring
682 */
683 return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
684}
685
Joern Engelb79fafa2013-07-03 11:22:17 -0400686static void tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400687{
688 struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
689 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
690 struct qla_tgt_mgmt_cmd, se_cmd);
691
692 pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
693 mcmd, se_tmr->function, se_tmr->response);
694 /*
695 * Do translation between TCM TM response codes and
696 * QLA2xxx FC TM response codes.
697 */
698 switch (se_tmr->response) {
699 case TMR_FUNCTION_COMPLETE:
700 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
701 break;
702 case TMR_TASK_DOES_NOT_EXIST:
703 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
704 break;
705 case TMR_FUNCTION_REJECTED:
706 mcmd->fc_tm_rsp = FC_TM_REJECT;
707 break;
708 case TMR_LUN_DOES_NOT_EXIST:
709 default:
710 mcmd->fc_tm_rsp = FC_TM_FAILED;
711 break;
712 }
713 /*
714 * Queue the TM response to QLA2xxx LLD to build a
715 * CTIO response packet.
716 */
717 qlt_xmit_tm_rsp(mcmd);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400718}
719
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400720/* Local pointer to allocated TCM configfs fabric module */
721struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
722struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
723
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -0700724static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
725 struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400726/*
727 * Expected to be called with struct qla_hw_data->hardware_lock held
728 */
729static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
730{
731 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
732 struct se_portal_group *se_tpg = se_nacl->se_tpg;
733 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
734 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
735 struct tcm_qla2xxx_lport, lport_wwn);
736 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
737 struct tcm_qla2xxx_nacl, se_node_acl);
738 void *node;
739
740 pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
741
742 node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
743 WARN_ON(node && (node != se_nacl));
744
745 pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
746 se_nacl, nacl->nport_wwnn, nacl->nport_id);
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -0700747 /*
748 * Now clear the se_nacl and session pointers from our HW lport lookup
749 * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
750 *
751 * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
752 * target_wait_for_sess_cmds() before the session waits for outstanding
753 * I/O to complete, to avoid a race between session shutdown execution
754 * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
755 */
756 tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400757}
758
Joern Engelaaf68b72012-05-18 13:58:23 -0700759static void tcm_qla2xxx_release_session(struct kref *kref)
760{
761 struct se_session *se_sess = container_of(kref,
762 struct se_session, sess_kref);
763
764 qlt_unreg_sess(se_sess->fabric_sess_ptr);
765}
766
767static void tcm_qla2xxx_put_session(struct se_session *se_sess)
768{
769 struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
770 struct qla_hw_data *ha = sess->vha->hw;
771 unsigned long flags;
772
773 spin_lock_irqsave(&ha->hardware_lock, flags);
774 kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
775 spin_unlock_irqrestore(&ha->hardware_lock, flags);
776}
777
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400778static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
779{
Jörn Engel08234e32013-06-12 16:27:54 -0400780 assert_spin_locked(&sess->vha->hw->hardware_lock);
781 kref_put(&sess->se_sess->sess_kref, tcm_qla2xxx_release_session);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400782}
783
784static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
785{
Jörn Engel08234e32013-06-12 16:27:54 -0400786 assert_spin_locked(&sess->vha->hw->hardware_lock);
787 target_sess_cmd_list_set_waiting(sess->se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400788}
789
790static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
791 struct se_portal_group *se_tpg,
792 struct config_group *group,
793 const char *name)
794{
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400795 struct se_node_acl *se_nacl, *se_nacl_new;
796 struct tcm_qla2xxx_nacl *nacl;
797 u64 wwnn;
798 u32 qla2xxx_nexus_depth;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400799
800 if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
801 return ERR_PTR(-EINVAL);
802
803 se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
804 if (!se_nacl_new)
805 return ERR_PTR(-ENOMEM);
806/* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
807 qla2xxx_nexus_depth = 1;
808
809 /*
810 * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
811 * when converting a NodeACL from demo mode -> explict
812 */
813 se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
814 name, qla2xxx_nexus_depth);
815 if (IS_ERR(se_nacl)) {
816 tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
817 return se_nacl;
818 }
819 /*
820 * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
821 */
822 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
823 nacl->nport_wwnn = wwnn;
824 tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400825
826 return se_nacl;
827}
828
829static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
830{
831 struct se_portal_group *se_tpg = se_acl->se_tpg;
832 struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
833 struct tcm_qla2xxx_nacl, se_node_acl);
834
835 core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
836 kfree(nacl);
837}
838
839/* Start items for tcm_qla2xxx_tpg_attrib_cit */
840
841#define DEF_QLA_TPG_ATTRIB(name) \
842 \
843static ssize_t tcm_qla2xxx_tpg_attrib_show_##name( \
844 struct se_portal_group *se_tpg, \
845 char *page) \
846{ \
847 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
848 struct tcm_qla2xxx_tpg, se_tpg); \
849 \
850 return sprintf(page, "%u\n", QLA_TPG_ATTRIB(tpg)->name); \
851} \
852 \
853static ssize_t tcm_qla2xxx_tpg_attrib_store_##name( \
854 struct se_portal_group *se_tpg, \
855 const char *page, \
856 size_t count) \
857{ \
858 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg, \
859 struct tcm_qla2xxx_tpg, se_tpg); \
860 unsigned long val; \
861 int ret; \
862 \
863 ret = kstrtoul(page, 0, &val); \
864 if (ret < 0) { \
865 pr_err("kstrtoul() failed with" \
866 " ret: %d\n", ret); \
867 return -EINVAL; \
868 } \
869 ret = tcm_qla2xxx_set_attrib_##name(tpg, val); \
870 \
871 return (!ret) ? count : -EINVAL; \
872}
873
874#define DEF_QLA_TPG_ATTR_BOOL(_name) \
875 \
876static int tcm_qla2xxx_set_attrib_##_name( \
877 struct tcm_qla2xxx_tpg *tpg, \
878 unsigned long val) \
879{ \
880 struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib; \
881 \
882 if ((val != 0) && (val != 1)) { \
883 pr_err("Illegal boolean value %lu\n", val); \
884 return -EINVAL; \
885 } \
886 \
887 a->_name = val; \
888 return 0; \
889}
890
891#define QLA_TPG_ATTR(_name, _mode) \
892 TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
893
894/*
895 * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
896 */
897DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
898DEF_QLA_TPG_ATTRIB(generate_node_acls);
899QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
900
901/*
902 Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
903 */
904DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
905DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
906QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
907
908/*
909 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
910 */
911DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
912DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
913QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
914
915/*
916 * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
917 */
918DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
919DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
920QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
921
Andy Groverde04a8a2013-07-19 15:06:38 -0700922/*
923 * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_login_only
924 */
925DEF_QLA_TPG_ATTR_BOOL(demo_mode_login_only);
926DEF_QLA_TPG_ATTRIB(demo_mode_login_only);
927QLA_TPG_ATTR(demo_mode_login_only, S_IRUGO | S_IWUSR);
928
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400929static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
930 &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
931 &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
932 &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
933 &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
Andy Groverde04a8a2013-07-19 15:06:38 -0700934 &tcm_qla2xxx_tpg_attrib_demo_mode_login_only.attr,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -0400935 NULL,
936};
937
938/* End items for tcm_qla2xxx_tpg_attrib_cit */
939
940static ssize_t tcm_qla2xxx_tpg_show_enable(
941 struct se_portal_group *se_tpg,
942 char *page)
943{
944 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
945 struct tcm_qla2xxx_tpg, se_tpg);
946
947 return snprintf(page, PAGE_SIZE, "%d\n",
948 atomic_read(&tpg->lport_tpg_enabled));
949}
950
951static ssize_t tcm_qla2xxx_tpg_store_enable(
952 struct se_portal_group *se_tpg,
953 const char *page,
954 size_t count)
955{
956 struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
957 struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
958 struct tcm_qla2xxx_lport, lport_wwn);
959 struct scsi_qla_host *vha = lport->qla_vha;
960 struct qla_hw_data *ha = vha->hw;
961 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
962 struct tcm_qla2xxx_tpg, se_tpg);
963 unsigned long op;
964 int rc;
965
966 rc = kstrtoul(page, 0, &op);
967 if (rc < 0) {
968 pr_err("kstrtoul() returned %d\n", rc);
969 return -EINVAL;
970 }
971 if ((op != 1) && (op != 0)) {
972 pr_err("Illegal value for tpg_enable: %lu\n", op);
973 return -EINVAL;
974 }
975
976 if (op) {
977 atomic_set(&tpg->lport_tpg_enabled, 1);
978 qlt_enable_vha(vha);
979 } else {
980 if (!ha->tgt.qla_tgt) {
981 pr_err("truct qla_hw_data *ha->tgt.qla_tgt is NULL\n");
982 return -ENODEV;
983 }
984 atomic_set(&tpg->lport_tpg_enabled, 0);
985 qlt_stop_phase1(ha->tgt.qla_tgt);
986 }
987
988 return count;
989}
990
991TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
992
993static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
994 &tcm_qla2xxx_tpg_enable.attr,
995 NULL,
996};
997
998static struct se_portal_group *tcm_qla2xxx_make_tpg(
999 struct se_wwn *wwn,
1000 struct config_group *group,
1001 const char *name)
1002{
1003 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1004 struct tcm_qla2xxx_lport, lport_wwn);
1005 struct tcm_qla2xxx_tpg *tpg;
1006 unsigned long tpgt;
1007 int ret;
1008
1009 if (strstr(name, "tpgt_") != name)
1010 return ERR_PTR(-EINVAL);
1011 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1012 return ERR_PTR(-EINVAL);
1013
1014 if (!lport->qla_npiv_vp && (tpgt != 1)) {
1015 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1016 return ERR_PTR(-ENOSYS);
1017 }
1018
1019 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1020 if (!tpg) {
1021 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1022 return ERR_PTR(-ENOMEM);
1023 }
1024 tpg->lport = lport;
1025 tpg->lport_tpgt = tpgt;
1026 /*
1027 * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1028 * NodeACLs
1029 */
1030 QLA_TPG_ATTRIB(tpg)->generate_node_acls = 1;
1031 QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect = 1;
1032 QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls = 1;
Andy Groverde04a8a2013-07-19 15:06:38 -07001033 QLA_TPG_ATTRIB(tpg)->demo_mode_login_only = 1;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001034
1035 ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
1036 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1037 if (ret < 0) {
1038 kfree(tpg);
1039 return NULL;
1040 }
1041 /*
1042 * Setup local TPG=1 pointer for non NPIV mode.
1043 */
1044 if (lport->qla_npiv_vp == NULL)
1045 lport->tpg_1 = tpg;
1046
1047 return &tpg->se_tpg;
1048}
1049
1050static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1051{
1052 struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1053 struct tcm_qla2xxx_tpg, se_tpg);
1054 struct tcm_qla2xxx_lport *lport = tpg->lport;
1055 struct scsi_qla_host *vha = lport->qla_vha;
1056 struct qla_hw_data *ha = vha->hw;
1057 /*
1058 * Call into qla2x_target.c LLD logic to shutdown the active
1059 * FC Nexuses and disable target mode operation for this qla_hw_data
1060 */
1061 if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stop)
1062 qlt_stop_phase1(ha->tgt.qla_tgt);
1063
1064 core_tpg_deregister(se_tpg);
1065 /*
1066 * Clear local TPG=1 pointer for non NPIV mode.
1067 */
1068 if (lport->qla_npiv_vp == NULL)
1069 lport->tpg_1 = NULL;
1070
1071 kfree(tpg);
1072}
1073
1074static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1075 struct se_wwn *wwn,
1076 struct config_group *group,
1077 const char *name)
1078{
1079 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1080 struct tcm_qla2xxx_lport, lport_wwn);
1081 struct tcm_qla2xxx_tpg *tpg;
1082 unsigned long tpgt;
1083 int ret;
1084
1085 if (strstr(name, "tpgt_") != name)
1086 return ERR_PTR(-EINVAL);
1087 if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1088 return ERR_PTR(-EINVAL);
1089
1090 tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1091 if (!tpg) {
1092 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1093 return ERR_PTR(-ENOMEM);
1094 }
1095 tpg->lport = lport;
1096 tpg->lport_tpgt = tpgt;
1097
1098 ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
1099 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1100 if (ret < 0) {
1101 kfree(tpg);
1102 return NULL;
1103 }
1104 return &tpg->se_tpg;
1105}
1106
1107/*
1108 * Expected to be called with struct qla_hw_data->hardware_lock held
1109 */
1110static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
1111 scsi_qla_host_t *vha,
1112 const uint8_t *s_id)
1113{
1114 struct qla_hw_data *ha = vha->hw;
1115 struct tcm_qla2xxx_lport *lport;
1116 struct se_node_acl *se_nacl;
1117 struct tcm_qla2xxx_nacl *nacl;
1118 u32 key;
1119
1120 lport = ha->tgt.target_lport_ptr;
1121 if (!lport) {
1122 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1123 dump_stack();
1124 return NULL;
1125 }
1126
1127 key = (((unsigned long)s_id[0] << 16) |
1128 ((unsigned long)s_id[1] << 8) |
1129 (unsigned long)s_id[2]);
1130 pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1131
1132 se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1133 if (!se_nacl) {
1134 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1135 return NULL;
1136 }
1137 pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1138 se_nacl, se_nacl->initiatorname);
1139
1140 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1141 if (!nacl->qla_tgt_sess) {
1142 pr_err("Unable to locate struct qla_tgt_sess\n");
1143 return NULL;
1144 }
1145
1146 return nacl->qla_tgt_sess;
1147}
1148
1149/*
1150 * Expected to be called with struct qla_hw_data->hardware_lock held
1151 */
1152static void tcm_qla2xxx_set_sess_by_s_id(
1153 struct tcm_qla2xxx_lport *lport,
1154 struct se_node_acl *new_se_nacl,
1155 struct tcm_qla2xxx_nacl *nacl,
1156 struct se_session *se_sess,
1157 struct qla_tgt_sess *qla_tgt_sess,
1158 uint8_t *s_id)
1159{
1160 u32 key;
1161 void *slot;
1162 int rc;
1163
1164 key = (((unsigned long)s_id[0] << 16) |
1165 ((unsigned long)s_id[1] << 8) |
1166 (unsigned long)s_id[2]);
1167 pr_debug("set_sess_by_s_id: %06x\n", key);
1168
1169 slot = btree_lookup32(&lport->lport_fcport_map, key);
1170 if (!slot) {
1171 if (new_se_nacl) {
1172 pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1173 nacl->nport_id = key;
1174 rc = btree_insert32(&lport->lport_fcport_map, key,
1175 new_se_nacl, GFP_ATOMIC);
1176 if (rc)
1177 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1178 (int)key);
1179 } else {
1180 pr_debug("Wiping nonexisting fc_port entry\n");
1181 }
1182
1183 qla_tgt_sess->se_sess = se_sess;
1184 nacl->qla_tgt_sess = qla_tgt_sess;
1185 return;
1186 }
1187
1188 if (nacl->qla_tgt_sess) {
1189 if (new_se_nacl == NULL) {
1190 pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
1191 btree_remove32(&lport->lport_fcport_map, key);
1192 nacl->qla_tgt_sess = NULL;
1193 return;
1194 }
1195 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
1196 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1197 qla_tgt_sess->se_sess = se_sess;
1198 nacl->qla_tgt_sess = qla_tgt_sess;
1199 return;
1200 }
1201
1202 if (new_se_nacl == NULL) {
1203 pr_debug("Clearing existing fc_port entry\n");
1204 btree_remove32(&lport->lport_fcport_map, key);
1205 return;
1206 }
1207
1208 pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
1209 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1210 qla_tgt_sess->se_sess = se_sess;
1211 nacl->qla_tgt_sess = qla_tgt_sess;
1212
1213 pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
1214 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1215}
1216
1217/*
1218 * Expected to be called with struct qla_hw_data->hardware_lock held
1219 */
1220static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
1221 scsi_qla_host_t *vha,
1222 const uint16_t loop_id)
1223{
1224 struct qla_hw_data *ha = vha->hw;
1225 struct tcm_qla2xxx_lport *lport;
1226 struct se_node_acl *se_nacl;
1227 struct tcm_qla2xxx_nacl *nacl;
1228 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1229
1230 lport = ha->tgt.target_lport_ptr;
1231 if (!lport) {
1232 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1233 dump_stack();
1234 return NULL;
1235 }
1236
1237 pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1238
1239 fc_loopid = lport->lport_loopid_map + loop_id;
1240 se_nacl = fc_loopid->se_nacl;
1241 if (!se_nacl) {
1242 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1243 loop_id);
1244 return NULL;
1245 }
1246
1247 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1248
1249 if (!nacl->qla_tgt_sess) {
1250 pr_err("Unable to locate struct qla_tgt_sess\n");
1251 return NULL;
1252 }
1253
1254 return nacl->qla_tgt_sess;
1255}
1256
1257/*
1258 * Expected to be called with struct qla_hw_data->hardware_lock held
1259 */
1260static void tcm_qla2xxx_set_sess_by_loop_id(
1261 struct tcm_qla2xxx_lport *lport,
1262 struct se_node_acl *new_se_nacl,
1263 struct tcm_qla2xxx_nacl *nacl,
1264 struct se_session *se_sess,
1265 struct qla_tgt_sess *qla_tgt_sess,
1266 uint16_t loop_id)
1267{
1268 struct se_node_acl *saved_nacl;
1269 struct tcm_qla2xxx_fc_loopid *fc_loopid;
1270
1271 pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1272
1273 fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1274 lport->lport_loopid_map)[loop_id];
1275
1276 saved_nacl = fc_loopid->se_nacl;
1277 if (!saved_nacl) {
1278 pr_debug("Setting up new fc_loopid->se_nacl to new_se_nacl\n");
1279 fc_loopid->se_nacl = new_se_nacl;
1280 if (qla_tgt_sess->se_sess != se_sess)
1281 qla_tgt_sess->se_sess = se_sess;
1282 if (nacl->qla_tgt_sess != qla_tgt_sess)
1283 nacl->qla_tgt_sess = qla_tgt_sess;
1284 return;
1285 }
1286
1287 if (nacl->qla_tgt_sess) {
1288 if (new_se_nacl == NULL) {
1289 pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1290 fc_loopid->se_nacl = NULL;
1291 nacl->qla_tgt_sess = NULL;
1292 return;
1293 }
1294
1295 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1296 fc_loopid->se_nacl = new_se_nacl;
1297 if (qla_tgt_sess->se_sess != se_sess)
1298 qla_tgt_sess->se_sess = se_sess;
1299 if (nacl->qla_tgt_sess != qla_tgt_sess)
1300 nacl->qla_tgt_sess = qla_tgt_sess;
1301 return;
1302 }
1303
1304 if (new_se_nacl == NULL) {
1305 pr_debug("Clearing fc_loopid->se_nacl\n");
1306 fc_loopid->se_nacl = NULL;
1307 return;
1308 }
1309
1310 pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
1311 fc_loopid->se_nacl = new_se_nacl;
1312 if (qla_tgt_sess->se_sess != se_sess)
1313 qla_tgt_sess->se_sess = se_sess;
1314 if (nacl->qla_tgt_sess != qla_tgt_sess)
1315 nacl->qla_tgt_sess = qla_tgt_sess;
1316
1317 pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1318 nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1319}
1320
Nicholas Bellingerf2d5d9b2012-05-18 15:37:53 -07001321/*
1322 * Should always be called with qla_hw_data->hardware_lock held.
1323 */
1324static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1325 struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
1326{
1327 struct se_session *se_sess = sess->se_sess;
1328 unsigned char be_sid[3];
1329
1330 be_sid[0] = sess->s_id.b.domain;
1331 be_sid[1] = sess->s_id.b.area;
1332 be_sid[2] = sess->s_id.b.al_pa;
1333
1334 tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1335 sess, be_sid);
1336 tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1337 sess, sess->loop_id);
1338}
1339
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001340static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
1341{
1342 struct qla_tgt *tgt = sess->tgt;
1343 struct qla_hw_data *ha = tgt->ha;
1344 struct se_session *se_sess;
1345 struct se_node_acl *se_nacl;
1346 struct tcm_qla2xxx_lport *lport;
1347 struct tcm_qla2xxx_nacl *nacl;
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001348
1349 BUG_ON(in_interrupt());
1350
1351 se_sess = sess->se_sess;
1352 if (!se_sess) {
1353 pr_err("struct qla_tgt_sess->se_sess is NULL\n");
1354 dump_stack();
1355 return;
1356 }
1357 se_nacl = se_sess->se_node_acl;
1358 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1359
1360 lport = ha->tgt.target_lport_ptr;
1361 if (!lport) {
1362 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1363 dump_stack();
1364 return;
1365 }
Joern Engelbe646c2d2013-05-15 00:44:07 -07001366 target_wait_for_sess_cmds(se_sess);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001367
1368 transport_deregister_session_configfs(sess->se_sess);
1369 transport_deregister_session(sess->se_sess);
1370}
1371
1372/*
1373 * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1374 * to locate struct se_node_acl
1375 */
1376static int tcm_qla2xxx_check_initiator_node_acl(
1377 scsi_qla_host_t *vha,
1378 unsigned char *fc_wwpn,
1379 void *qla_tgt_sess,
1380 uint8_t *s_id,
1381 uint16_t loop_id)
1382{
1383 struct qla_hw_data *ha = vha->hw;
1384 struct tcm_qla2xxx_lport *lport;
1385 struct tcm_qla2xxx_tpg *tpg;
1386 struct tcm_qla2xxx_nacl *nacl;
1387 struct se_portal_group *se_tpg;
1388 struct se_node_acl *se_nacl;
1389 struct se_session *se_sess;
1390 struct qla_tgt_sess *sess = qla_tgt_sess;
1391 unsigned char port_name[36];
1392 unsigned long flags;
1393
1394 lport = ha->tgt.target_lport_ptr;
1395 if (!lport) {
1396 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1397 dump_stack();
1398 return -EINVAL;
1399 }
1400 /*
1401 * Locate the TPG=1 reference..
1402 */
1403 tpg = lport->tpg_1;
1404 if (!tpg) {
1405 pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1406 return -EINVAL;
1407 }
1408 se_tpg = &tpg->se_tpg;
1409
1410 se_sess = transport_init_session();
1411 if (IS_ERR(se_sess)) {
1412 pr_err("Unable to initialize struct se_session\n");
1413 return PTR_ERR(se_sess);
1414 }
1415 /*
1416 * Format the FCP Initiator port_name into colon seperated values to
1417 * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1418 */
1419 memset(&port_name, 0, 36);
1420 snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1421 fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
1422 fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
1423 /*
1424 * Locate our struct se_node_acl either from an explict NodeACL created
1425 * via ConfigFS, or via running in TPG demo mode.
1426 */
1427 se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
1428 port_name);
1429 if (!se_sess->se_node_acl) {
1430 transport_free_session(se_sess);
1431 return -EINVAL;
1432 }
1433 se_nacl = se_sess->se_node_acl;
1434 nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1435 /*
1436 * And now setup the new se_nacl and session pointers into our HW lport
1437 * mappings for fabric S_ID and LOOP_ID.
1438 */
1439 spin_lock_irqsave(&ha->hardware_lock, flags);
1440 tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
1441 qla_tgt_sess, s_id);
1442 tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
1443 qla_tgt_sess, loop_id);
1444 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1445 /*
1446 * Finally register the new FC Nexus with TCM
1447 */
1448 __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
1449
1450 return 0;
1451}
1452
Roland Dreierc8292d12012-10-11 13:41:32 -07001453static void tcm_qla2xxx_update_sess(struct qla_tgt_sess *sess, port_id_t s_id,
1454 uint16_t loop_id, bool conf_compl_supported)
1455{
1456 struct qla_tgt *tgt = sess->tgt;
1457 struct qla_hw_data *ha = tgt->ha;
1458 struct tcm_qla2xxx_lport *lport = ha->tgt.target_lport_ptr;
1459 struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
1460 struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
1461 struct tcm_qla2xxx_nacl, se_node_acl);
1462 u32 key;
1463
1464
1465 if (sess->loop_id != loop_id || sess->s_id.b24 != s_id.b24)
1466 pr_info("Updating session %p from port %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x loop_id %d -> %d s_id %x:%x:%x -> %x:%x:%x\n",
1467 sess,
1468 sess->port_name[0], sess->port_name[1],
1469 sess->port_name[2], sess->port_name[3],
1470 sess->port_name[4], sess->port_name[5],
1471 sess->port_name[6], sess->port_name[7],
1472 sess->loop_id, loop_id,
1473 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa,
1474 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1475
1476 if (sess->loop_id != loop_id) {
1477 /*
1478 * Because we can shuffle loop IDs around and we
1479 * update different sessions non-atomically, we might
1480 * have overwritten this session's old loop ID
1481 * already, and we might end up overwriting some other
1482 * session that will be updated later. So we have to
1483 * be extra careful and we can't warn about those things...
1484 */
1485 if (lport->lport_loopid_map[sess->loop_id].se_nacl == se_nacl)
1486 lport->lport_loopid_map[sess->loop_id].se_nacl = NULL;
1487
1488 lport->lport_loopid_map[loop_id].se_nacl = se_nacl;
1489
1490 sess->loop_id = loop_id;
1491 }
1492
1493 if (sess->s_id.b24 != s_id.b24) {
1494 key = (((u32) sess->s_id.b.domain << 16) |
1495 ((u32) sess->s_id.b.area << 8) |
1496 ((u32) sess->s_id.b.al_pa));
1497
1498 if (btree_lookup32(&lport->lport_fcport_map, key))
1499 WARN(btree_remove32(&lport->lport_fcport_map, key) != se_nacl,
1500 "Found wrong se_nacl when updating s_id %x:%x:%x\n",
1501 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1502 else
1503 WARN(1, "No lport_fcport_map entry for s_id %x:%x:%x\n",
1504 sess->s_id.b.domain, sess->s_id.b.area, sess->s_id.b.al_pa);
1505
1506 key = (((u32) s_id.b.domain << 16) |
1507 ((u32) s_id.b.area << 8) |
1508 ((u32) s_id.b.al_pa));
1509
1510 if (btree_lookup32(&lport->lport_fcport_map, key)) {
1511 WARN(1, "Already have lport_fcport_map entry for s_id %x:%x:%x\n",
1512 s_id.b.domain, s_id.b.area, s_id.b.al_pa);
1513 btree_update32(&lport->lport_fcport_map, key, se_nacl);
1514 } else {
1515 btree_insert32(&lport->lport_fcport_map, key, se_nacl, GFP_ATOMIC);
1516 }
1517
1518 sess->s_id = s_id;
1519 nacl->nport_id = key;
1520 }
1521
1522 sess->conf_compl_supported = conf_compl_supported;
1523}
1524
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001525/*
1526 * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1527 */
1528static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1529 .handle_cmd = tcm_qla2xxx_handle_cmd,
1530 .handle_data = tcm_qla2xxx_handle_data,
1531 .handle_tmr = tcm_qla2xxx_handle_tmr,
1532 .free_cmd = tcm_qla2xxx_free_cmd,
1533 .free_mcmd = tcm_qla2xxx_free_mcmd,
1534 .free_session = tcm_qla2xxx_free_session,
Roland Dreierc8292d12012-10-11 13:41:32 -07001535 .update_sess = tcm_qla2xxx_update_sess,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001536 .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1537 .find_sess_by_s_id = tcm_qla2xxx_find_sess_by_s_id,
1538 .find_sess_by_loop_id = tcm_qla2xxx_find_sess_by_loop_id,
1539 .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1540 .put_sess = tcm_qla2xxx_put_sess,
1541 .shutdown_sess = tcm_qla2xxx_shutdown_sess,
1542};
1543
1544static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1545{
1546 int rc;
1547
1548 rc = btree_init32(&lport->lport_fcport_map);
1549 if (rc) {
1550 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1551 return rc;
1552 }
1553
1554 lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
1555 65536);
1556 if (!lport->lport_loopid_map) {
1557 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1558 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1559 btree_destroy32(&lport->lport_fcport_map);
1560 return -ENOMEM;
1561 }
1562 memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
1563 * 65536);
1564 pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1565 sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1566 return 0;
1567}
1568
1569static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha)
1570{
1571 struct qla_hw_data *ha = vha->hw;
1572 struct tcm_qla2xxx_lport *lport;
1573 /*
1574 * Setup local pointer to vha, NPIV VP pointer (if present) and
1575 * vha->tcm_lport pointer
1576 */
1577 lport = (struct tcm_qla2xxx_lport *)ha->tgt.target_lport_ptr;
1578 lport->qla_vha = vha;
1579
1580 return 0;
1581}
1582
1583static struct se_wwn *tcm_qla2xxx_make_lport(
1584 struct target_fabric_configfs *tf,
1585 struct config_group *group,
1586 const char *name)
1587{
1588 struct tcm_qla2xxx_lport *lport;
1589 u64 wwpn;
1590 int ret = -ENODEV;
1591
1592 if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1593 return ERR_PTR(-EINVAL);
1594
1595 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1596 if (!lport) {
1597 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1598 return ERR_PTR(-ENOMEM);
1599 }
1600 lport->lport_wwpn = wwpn;
1601 tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1602 wwpn);
Roland Dreierc046aa02012-10-11 13:41:31 -07001603 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) wwpn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001604
1605 ret = tcm_qla2xxx_init_lport(lport);
1606 if (ret != 0)
1607 goto out;
1608
1609 ret = qlt_lport_register(&tcm_qla2xxx_template, wwpn,
1610 tcm_qla2xxx_lport_register_cb, lport);
1611 if (ret != 0)
1612 goto out_lport;
1613
1614 return &lport->lport_wwn;
1615out_lport:
1616 vfree(lport->lport_loopid_map);
1617 btree_destroy32(&lport->lport_fcport_map);
1618out:
1619 kfree(lport);
1620 return ERR_PTR(ret);
1621}
1622
1623static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1624{
1625 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1626 struct tcm_qla2xxx_lport, lport_wwn);
1627 struct scsi_qla_host *vha = lport->qla_vha;
1628 struct qla_hw_data *ha = vha->hw;
1629 struct se_node_acl *node;
1630 u32 key = 0;
1631
1632 /*
1633 * Call into qla2x_target.c LLD logic to complete the
1634 * shutdown of struct qla_tgt after the call to
1635 * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1636 */
1637 if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stopped)
1638 qlt_stop_phase2(ha->tgt.qla_tgt);
1639
1640 qlt_lport_deregister(vha);
1641
1642 vfree(lport->lport_loopid_map);
1643 btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1644 btree_remove32(&lport->lport_fcport_map, key);
1645 btree_destroy32(&lport->lport_fcport_map);
1646 kfree(lport);
1647}
1648
1649static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1650 struct target_fabric_configfs *tf,
1651 struct config_group *group,
1652 const char *name)
1653{
1654 struct tcm_qla2xxx_lport *lport;
1655 u64 npiv_wwpn, npiv_wwnn;
1656 int ret;
1657
1658 if (tcm_qla2xxx_npiv_parse_wwn(name, strlen(name)+1,
1659 &npiv_wwpn, &npiv_wwnn) < 0)
1660 return ERR_PTR(-EINVAL);
1661
1662 lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1663 if (!lport) {
1664 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1665 return ERR_PTR(-ENOMEM);
1666 }
1667 lport->lport_npiv_wwpn = npiv_wwpn;
1668 lport->lport_npiv_wwnn = npiv_wwnn;
1669 tcm_qla2xxx_npiv_format_wwn(&lport->lport_npiv_name[0],
1670 TCM_QLA2XXX_NAMELEN, npiv_wwpn, npiv_wwnn);
Roland Dreierc046aa02012-10-11 13:41:31 -07001671 sprintf(lport->lport_naa_name, "naa.%016llx", (unsigned long long) npiv_wwpn);
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001672
1673/* FIXME: tcm_qla2xxx_npiv_make_lport */
1674 ret = -ENOSYS;
1675 if (ret != 0)
1676 goto out;
1677
1678 return &lport->lport_wwn;
1679out:
1680 kfree(lport);
1681 return ERR_PTR(ret);
1682}
1683
1684static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1685{
1686 struct tcm_qla2xxx_lport *lport = container_of(wwn,
1687 struct tcm_qla2xxx_lport, lport_wwn);
1688 struct scsi_qla_host *vha = lport->qla_vha;
1689 struct Scsi_Host *sh = vha->host;
1690 /*
1691 * Notify libfc that we want to release the lport->npiv_vport
1692 */
1693 fc_vport_terminate(lport->npiv_vport);
1694
1695 scsi_host_put(sh);
1696 kfree(lport);
1697}
1698
1699
1700static ssize_t tcm_qla2xxx_wwn_show_attr_version(
1701 struct target_fabric_configfs *tf,
1702 char *page)
1703{
1704 return sprintf(page,
1705 "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
1706 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1707 utsname()->machine);
1708}
1709
1710TF_WWN_ATTR_RO(tcm_qla2xxx, version);
1711
1712static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1713 &tcm_qla2xxx_wwn_version.attr,
1714 NULL,
1715};
1716
1717static struct target_core_fabric_ops tcm_qla2xxx_ops = {
1718 .get_fabric_name = tcm_qla2xxx_get_fabric_name,
1719 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1720 .tpg_get_wwn = tcm_qla2xxx_get_fabric_wwn,
1721 .tpg_get_tag = tcm_qla2xxx_get_tag,
1722 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1723 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1724 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1725 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1726 .tpg_check_demo_mode = tcm_qla2xxx_check_demo_mode,
1727 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_demo_mode_cache,
1728 .tpg_check_demo_mode_write_protect =
1729 tcm_qla2xxx_check_demo_write_protect,
1730 .tpg_check_prod_mode_write_protect =
1731 tcm_qla2xxx_check_prod_write_protect,
Andy Groverde04a8a2013-07-19 15:06:38 -07001732 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001733 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1734 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1735 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001736 .check_stop_free = tcm_qla2xxx_check_stop_free,
1737 .release_cmd = tcm_qla2xxx_release_cmd,
Joern Engelaaf68b72012-05-18 13:58:23 -07001738 .put_session = tcm_qla2xxx_put_session,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001739 .shutdown_session = tcm_qla2xxx_shutdown_session,
1740 .close_session = tcm_qla2xxx_close_session,
1741 .sess_get_index = tcm_qla2xxx_sess_get_index,
1742 .sess_get_initiator_sid = NULL,
1743 .write_pending = tcm_qla2xxx_write_pending,
1744 .write_pending_status = tcm_qla2xxx_write_pending_status,
1745 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1746 .get_task_tag = tcm_qla2xxx_get_task_tag,
1747 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1748 .queue_data_in = tcm_qla2xxx_queue_data_in,
1749 .queue_status = tcm_qla2xxx_queue_status,
1750 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001751 /*
1752 * Setup function pointers for generic logic in
1753 * target_core_fabric_configfs.c
1754 */
1755 .fabric_make_wwn = tcm_qla2xxx_make_lport,
1756 .fabric_drop_wwn = tcm_qla2xxx_drop_lport,
1757 .fabric_make_tpg = tcm_qla2xxx_make_tpg,
1758 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1759 .fabric_post_link = NULL,
1760 .fabric_pre_unlink = NULL,
1761 .fabric_make_np = NULL,
1762 .fabric_drop_np = NULL,
1763 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1764 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1765};
1766
1767static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1768 .get_fabric_name = tcm_qla2xxx_npiv_get_fabric_name,
1769 .get_fabric_proto_ident = tcm_qla2xxx_get_fabric_proto_ident,
1770 .tpg_get_wwn = tcm_qla2xxx_npiv_get_fabric_wwn,
1771 .tpg_get_tag = tcm_qla2xxx_get_tag,
1772 .tpg_get_default_depth = tcm_qla2xxx_get_default_depth,
1773 .tpg_get_pr_transport_id = tcm_qla2xxx_get_pr_transport_id,
1774 .tpg_get_pr_transport_id_len = tcm_qla2xxx_get_pr_transport_id_len,
1775 .tpg_parse_pr_out_transport_id = tcm_qla2xxx_parse_pr_out_transport_id,
1776 .tpg_check_demo_mode = tcm_qla2xxx_check_false,
1777 .tpg_check_demo_mode_cache = tcm_qla2xxx_check_true,
1778 .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_true,
1779 .tpg_check_prod_mode_write_protect = tcm_qla2xxx_check_false,
Andy Groverde04a8a2013-07-19 15:06:38 -07001780 .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_demo_mode_login_only,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001781 .tpg_alloc_fabric_acl = tcm_qla2xxx_alloc_fabric_acl,
1782 .tpg_release_fabric_acl = tcm_qla2xxx_release_fabric_acl,
1783 .tpg_get_inst_index = tcm_qla2xxx_tpg_get_inst_index,
1784 .release_cmd = tcm_qla2xxx_release_cmd,
Joern Engelaaf68b72012-05-18 13:58:23 -07001785 .put_session = tcm_qla2xxx_put_session,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001786 .shutdown_session = tcm_qla2xxx_shutdown_session,
1787 .close_session = tcm_qla2xxx_close_session,
1788 .sess_get_index = tcm_qla2xxx_sess_get_index,
1789 .sess_get_initiator_sid = NULL,
1790 .write_pending = tcm_qla2xxx_write_pending,
1791 .write_pending_status = tcm_qla2xxx_write_pending_status,
1792 .set_default_node_attributes = tcm_qla2xxx_set_default_node_attrs,
1793 .get_task_tag = tcm_qla2xxx_get_task_tag,
1794 .get_cmd_state = tcm_qla2xxx_get_cmd_state,
1795 .queue_data_in = tcm_qla2xxx_queue_data_in,
1796 .queue_status = tcm_qla2xxx_queue_status,
1797 .queue_tm_rsp = tcm_qla2xxx_queue_tm_rsp,
Nicholas Bellinger75f8c1f2012-05-15 14:34:29 -04001798 /*
1799 * Setup function pointers for generic logic in
1800 * target_core_fabric_configfs.c
1801 */
1802 .fabric_make_wwn = tcm_qla2xxx_npiv_make_lport,
1803 .fabric_drop_wwn = tcm_qla2xxx_npiv_drop_lport,
1804 .fabric_make_tpg = tcm_qla2xxx_npiv_make_tpg,
1805 .fabric_drop_tpg = tcm_qla2xxx_drop_tpg,
1806 .fabric_post_link = NULL,
1807 .fabric_pre_unlink = NULL,
1808 .fabric_make_np = NULL,
1809 .fabric_drop_np = NULL,
1810 .fabric_make_nodeacl = tcm_qla2xxx_make_nodeacl,
1811 .fabric_drop_nodeacl = tcm_qla2xxx_drop_nodeacl,
1812};
1813
1814static int tcm_qla2xxx_register_configfs(void)
1815{
1816 struct target_fabric_configfs *fabric, *npiv_fabric;
1817 int ret;
1818
1819 pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
1820 UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1821 utsname()->machine);
1822 /*
1823 * Register the top level struct config_item_type with TCM core
1824 */
1825 fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
1826 if (IS_ERR(fabric)) {
1827 pr_err("target_fabric_configfs_init() failed\n");
1828 return PTR_ERR(fabric);
1829 }
1830 /*
1831 * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
1832 */
1833 fabric->tf_ops = tcm_qla2xxx_ops;
1834 /*
1835 * Setup default attribute lists for various fabric->tf_cit_tmpl
1836 */
1837 TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1838 TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
1839 TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs =
1840 tcm_qla2xxx_tpg_attrib_attrs;
1841 TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1842 TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1843 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1844 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1845 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1846 TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1847 /*
1848 * Register the fabric for use within TCM
1849 */
1850 ret = target_fabric_configfs_register(fabric);
1851 if (ret < 0) {
1852 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1853 return ret;
1854 }
1855 /*
1856 * Setup our local pointer to *fabric
1857 */
1858 tcm_qla2xxx_fabric_configfs = fabric;
1859 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
1860
1861 /*
1862 * Register the top level struct config_item_type for NPIV with TCM core
1863 */
1864 npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
1865 if (IS_ERR(npiv_fabric)) {
1866 pr_err("target_fabric_configfs_init() failed\n");
1867 ret = PTR_ERR(npiv_fabric);
1868 goto out_fabric;
1869 }
1870 /*
1871 * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
1872 */
1873 npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
1874 /*
1875 * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
1876 */
1877 TF_CIT_TMPL(npiv_fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1878 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
1879 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1880 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1881 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1882 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1883 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1884 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1885 TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1886 /*
1887 * Register the npiv_fabric for use within TCM
1888 */
1889 ret = target_fabric_configfs_register(npiv_fabric);
1890 if (ret < 0) {
1891 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1892 goto out_fabric;
1893 }
1894 /*
1895 * Setup our local pointer to *npiv_fabric
1896 */
1897 tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
1898 pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
1899
1900 tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1901 WQ_MEM_RECLAIM, 0);
1902 if (!tcm_qla2xxx_free_wq) {
1903 ret = -ENOMEM;
1904 goto out_fabric_npiv;
1905 }
1906
1907 tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
1908 if (!tcm_qla2xxx_cmd_wq) {
1909 ret = -ENOMEM;
1910 goto out_free_wq;
1911 }
1912
1913 return 0;
1914
1915out_free_wq:
1916 destroy_workqueue(tcm_qla2xxx_free_wq);
1917out_fabric_npiv:
1918 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1919out_fabric:
1920 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1921 return ret;
1922}
1923
1924static void tcm_qla2xxx_deregister_configfs(void)
1925{
1926 destroy_workqueue(tcm_qla2xxx_cmd_wq);
1927 destroy_workqueue(tcm_qla2xxx_free_wq);
1928
1929 target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1930 tcm_qla2xxx_fabric_configfs = NULL;
1931 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
1932
1933 target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1934 tcm_qla2xxx_npiv_fabric_configfs = NULL;
1935 pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
1936}
1937
1938static int __init tcm_qla2xxx_init(void)
1939{
1940 int ret;
1941
1942 ret = tcm_qla2xxx_register_configfs();
1943 if (ret < 0)
1944 return ret;
1945
1946 return 0;
1947}
1948
1949static void __exit tcm_qla2xxx_exit(void)
1950{
1951 tcm_qla2xxx_deregister_configfs();
1952}
1953
1954MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
1955MODULE_LICENSE("GPL");
1956module_init(tcm_qla2xxx_init);
1957module_exit(tcm_qla2xxx_exit);