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