977cb8b1e42bf5705f6f312fcfcfb867b4e122b0
[firefly-linux-kernel-4.4.55.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_name[0];
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 struct se_node_acl *tcm_qla2xxx_alloc_fabric_acl(
364         struct se_portal_group *se_tpg)
365 {
366         struct tcm_qla2xxx_nacl *nacl;
367
368         nacl = kzalloc(sizeof(struct tcm_qla2xxx_nacl), GFP_KERNEL);
369         if (!nacl) {
370                 pr_err("Unable to alocate struct tcm_qla2xxx_nacl\n");
371                 return NULL;
372         }
373
374         return &nacl->se_node_acl;
375 }
376
377 static void tcm_qla2xxx_release_fabric_acl(
378         struct se_portal_group *se_tpg,
379         struct se_node_acl *se_nacl)
380 {
381         struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
382                         struct tcm_qla2xxx_nacl, se_node_acl);
383         kfree(nacl);
384 }
385
386 static u32 tcm_qla2xxx_tpg_get_inst_index(struct se_portal_group *se_tpg)
387 {
388         struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
389                                 struct tcm_qla2xxx_tpg, se_tpg);
390
391         return tpg->lport_tpgt;
392 }
393
394 static void tcm_qla2xxx_complete_mcmd(struct work_struct *work)
395 {
396         struct qla_tgt_mgmt_cmd *mcmd = container_of(work,
397                         struct qla_tgt_mgmt_cmd, free_work);
398
399         transport_generic_free_cmd(&mcmd->se_cmd, 0);
400 }
401
402 /*
403  * Called from qla_target_template->free_mcmd(), and will call
404  * tcm_qla2xxx_release_cmd() via normal struct target_core_fabric_ops
405  * release callback.  qla_hw_data->hardware_lock is expected to be held
406  */
407 static void tcm_qla2xxx_free_mcmd(struct qla_tgt_mgmt_cmd *mcmd)
408 {
409         INIT_WORK(&mcmd->free_work, tcm_qla2xxx_complete_mcmd);
410         queue_work(tcm_qla2xxx_free_wq, &mcmd->free_work);
411 }
412
413 static void tcm_qla2xxx_complete_free(struct work_struct *work)
414 {
415         struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
416
417         transport_generic_free_cmd(&cmd->se_cmd, 0);
418 }
419
420 /*
421  * Called from qla_target_template->free_cmd(), and will call
422  * tcm_qla2xxx_release_cmd via normal struct target_core_fabric_ops
423  * release callback.  qla_hw_data->hardware_lock is expected to be held
424  */
425 static void tcm_qla2xxx_free_cmd(struct qla_tgt_cmd *cmd)
426 {
427         INIT_WORK(&cmd->work, tcm_qla2xxx_complete_free);
428         queue_work(tcm_qla2xxx_free_wq, &cmd->work);
429 }
430
431 /*
432  * Called from struct target_core_fabric_ops->check_stop_free() context
433  */
434 static int tcm_qla2xxx_check_stop_free(struct se_cmd *se_cmd)
435 {
436         return target_put_sess_cmd(se_cmd->se_sess, se_cmd);
437 }
438
439 /* tcm_qla2xxx_release_cmd - Callback from TCM Core to release underlying
440  * fabric descriptor @se_cmd command to release
441  */
442 static void tcm_qla2xxx_release_cmd(struct se_cmd *se_cmd)
443 {
444         struct qla_tgt_cmd *cmd;
445
446         if (se_cmd->se_cmd_flags & SCF_SCSI_TMR_CDB) {
447                 struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
448                                 struct qla_tgt_mgmt_cmd, se_cmd);
449                 qlt_free_mcmd(mcmd);
450                 return;
451         }
452
453         cmd = container_of(se_cmd, struct qla_tgt_cmd, se_cmd);
454         qlt_free_cmd(cmd);
455 }
456
457 static int tcm_qla2xxx_shutdown_session(struct se_session *se_sess)
458 {
459         struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
460         struct scsi_qla_host *vha;
461         unsigned long flags;
462
463         BUG_ON(!sess);
464         vha = sess->vha;
465
466         spin_lock_irqsave(&vha->hw->hardware_lock, flags);
467         sess->tearing_down = 1;
468         target_splice_sess_cmd_list(se_sess);
469         spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
470
471         return 1;
472 }
473
474 static void tcm_qla2xxx_close_session(struct se_session *se_sess)
475 {
476         struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
477         struct scsi_qla_host *vha;
478         unsigned long flags;
479
480         BUG_ON(!sess);
481         vha = sess->vha;
482
483         spin_lock_irqsave(&vha->hw->hardware_lock, flags);
484         qlt_unreg_sess(sess);
485         spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
486 }
487
488 static u32 tcm_qla2xxx_sess_get_index(struct se_session *se_sess)
489 {
490         return 0;
491 }
492
493 /*
494  * The LIO target core uses DMA_TO_DEVICE to mean that data is going
495  * to the target (eg handling a WRITE) and DMA_FROM_DEVICE to mean
496  * that data is coming from the target (eg handling a READ).  However,
497  * this is just the opposite of what we have to tell the DMA mapping
498  * layer -- eg when handling a READ, the HBA will have to DMA the data
499  * out of memory so it can send it to the initiator, which means we
500  * need to use DMA_TO_DEVICE when we map the data.
501  */
502 static enum dma_data_direction tcm_qla2xxx_mapping_dir(struct se_cmd *se_cmd)
503 {
504         if (se_cmd->se_cmd_flags & SCF_BIDI)
505                 return DMA_BIDIRECTIONAL;
506
507         switch (se_cmd->data_direction) {
508         case DMA_TO_DEVICE:
509                 return DMA_FROM_DEVICE;
510         case DMA_FROM_DEVICE:
511                 return DMA_TO_DEVICE;
512         case DMA_NONE:
513         default:
514                 return DMA_NONE;
515         }
516 }
517
518 static int tcm_qla2xxx_write_pending(struct se_cmd *se_cmd)
519 {
520         struct qla_tgt_cmd *cmd = container_of(se_cmd,
521                                 struct qla_tgt_cmd, se_cmd);
522
523         cmd->bufflen = se_cmd->data_length;
524         cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
525
526         cmd->sg_cnt = se_cmd->t_data_nents;
527         cmd->sg = se_cmd->t_data_sg;
528
529         /*
530          * qla_target.c:qlt_rdy_to_xfer() will call pci_map_sg() to setup
531          * the SGL mappings into PCIe memory for incoming FCP WRITE data.
532          */
533         return qlt_rdy_to_xfer(cmd);
534 }
535
536 static int tcm_qla2xxx_write_pending_status(struct se_cmd *se_cmd)
537 {
538         unsigned long flags;
539         /*
540          * Check for WRITE_PENDING status to determine if we need to wait for
541          * CTIO aborts to be posted via hardware in tcm_qla2xxx_handle_data().
542          */
543         spin_lock_irqsave(&se_cmd->t_state_lock, flags);
544         if (se_cmd->t_state == TRANSPORT_WRITE_PENDING ||
545             se_cmd->t_state == TRANSPORT_COMPLETE_QF_WP) {
546                 spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
547                 wait_for_completion_timeout(&se_cmd->t_transport_stop_comp,
548                                                 3000);
549                 return 0;
550         }
551         spin_unlock_irqrestore(&se_cmd->t_state_lock, flags);
552
553         return 0;
554 }
555
556 static void tcm_qla2xxx_set_default_node_attrs(struct se_node_acl *nacl)
557 {
558         return;
559 }
560
561 static u32 tcm_qla2xxx_get_task_tag(struct se_cmd *se_cmd)
562 {
563         struct qla_tgt_cmd *cmd = container_of(se_cmd,
564                                 struct qla_tgt_cmd, se_cmd);
565
566         return cmd->tag;
567 }
568
569 static int tcm_qla2xxx_get_cmd_state(struct se_cmd *se_cmd)
570 {
571         return 0;
572 }
573
574 /*
575  * Called from process context in qla_target.c:qlt_do_work() code
576  */
577 static int tcm_qla2xxx_handle_cmd(scsi_qla_host_t *vha, struct qla_tgt_cmd *cmd,
578         unsigned char *cdb, uint32_t data_length, int fcp_task_attr,
579         int data_dir, int bidi)
580 {
581         struct se_cmd *se_cmd = &cmd->se_cmd;
582         struct se_session *se_sess;
583         struct qla_tgt_sess *sess;
584         int flags = TARGET_SCF_ACK_KREF;
585
586         if (bidi)
587                 flags |= TARGET_SCF_BIDI_OP;
588
589         sess = cmd->sess;
590         if (!sess) {
591                 pr_err("Unable to locate struct qla_tgt_sess from qla_tgt_cmd\n");
592                 return -EINVAL;
593         }
594
595         se_sess = sess->se_sess;
596         if (!se_sess) {
597                 pr_err("Unable to locate active struct se_session\n");
598                 return -EINVAL;
599         }
600
601         target_submit_cmd(se_cmd, se_sess, cdb, &cmd->sense_buffer[0],
602                                 cmd->unpacked_lun, data_length, fcp_task_attr,
603                                 data_dir, flags);
604         return 0;
605 }
606
607 static void tcm_qla2xxx_handle_data_work(struct work_struct *work)
608 {
609         struct qla_tgt_cmd *cmd = container_of(work, struct qla_tgt_cmd, work);
610
611         /*
612          * Ensure that the complete FCP WRITE payload has been received.
613          * Otherwise return an exception via CHECK_CONDITION status.
614          */
615         if (!cmd->write_data_transferred) {
616                 /*
617                  * Check if se_cmd has already been aborted via LUN_RESET, and
618                  * waiting upon completion in tcm_qla2xxx_write_pending_status()
619                  */
620                 if (cmd->se_cmd.transport_state & CMD_T_ABORTED) {
621                         complete(&cmd->se_cmd.t_transport_stop_comp);
622                         return;
623                 }
624
625                 cmd->se_cmd.scsi_sense_reason = TCM_CHECK_CONDITION_ABORT_CMD;
626                 transport_generic_request_failure(&cmd->se_cmd);
627                 return;
628         }
629
630         return target_execute_cmd(&cmd->se_cmd);
631 }
632
633 /*
634  * Called from qla_target.c:qlt_do_ctio_completion()
635  */
636 static void tcm_qla2xxx_handle_data(struct qla_tgt_cmd *cmd)
637 {
638         INIT_WORK(&cmd->work, tcm_qla2xxx_handle_data_work);
639         queue_work(tcm_qla2xxx_free_wq, &cmd->work);
640 }
641
642 /*
643  * Called from qla_target.c:qlt_issue_task_mgmt()
644  */
645 static int tcm_qla2xxx_handle_tmr(struct qla_tgt_mgmt_cmd *mcmd, uint32_t lun,
646         uint8_t tmr_func, uint32_t tag)
647 {
648         struct qla_tgt_sess *sess = mcmd->sess;
649         struct se_cmd *se_cmd = &mcmd->se_cmd;
650
651         return target_submit_tmr(se_cmd, sess->se_sess, NULL, lun, mcmd,
652                         tmr_func, GFP_ATOMIC, tag, TARGET_SCF_ACK_KREF);
653 }
654
655 static int tcm_qla2xxx_queue_data_in(struct se_cmd *se_cmd)
656 {
657         struct qla_tgt_cmd *cmd = container_of(se_cmd,
658                                 struct qla_tgt_cmd, se_cmd);
659
660         cmd->bufflen = se_cmd->data_length;
661         cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
662         cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
663
664         cmd->sg_cnt = se_cmd->t_data_nents;
665         cmd->sg = se_cmd->t_data_sg;
666         cmd->offset = 0;
667
668         /*
669          * Now queue completed DATA_IN the qla2xxx LLD and response ring
670          */
671         return qlt_xmit_response(cmd, QLA_TGT_XMIT_DATA|QLA_TGT_XMIT_STATUS,
672                                 se_cmd->scsi_status);
673 }
674
675 static int tcm_qla2xxx_queue_status(struct se_cmd *se_cmd)
676 {
677         struct qla_tgt_cmd *cmd = container_of(se_cmd,
678                                 struct qla_tgt_cmd, se_cmd);
679         int xmit_type = QLA_TGT_XMIT_STATUS;
680
681         cmd->bufflen = se_cmd->data_length;
682         cmd->sg = NULL;
683         cmd->sg_cnt = 0;
684         cmd->offset = 0;
685         cmd->dma_data_direction = tcm_qla2xxx_mapping_dir(se_cmd);
686         cmd->aborted = (se_cmd->transport_state & CMD_T_ABORTED);
687
688         if (se_cmd->data_direction == DMA_FROM_DEVICE) {
689                 /*
690                  * For FCP_READ with CHECK_CONDITION status, clear cmd->bufflen
691                  * for qla_tgt_xmit_response LLD code
692                  */
693                 se_cmd->se_cmd_flags |= SCF_UNDERFLOW_BIT;
694                 se_cmd->residual_count = se_cmd->data_length;
695
696                 cmd->bufflen = 0;
697         }
698         /*
699          * Now queue status response to qla2xxx LLD code and response ring
700          */
701         return qlt_xmit_response(cmd, xmit_type, se_cmd->scsi_status);
702 }
703
704 static int tcm_qla2xxx_queue_tm_rsp(struct se_cmd *se_cmd)
705 {
706         struct se_tmr_req *se_tmr = se_cmd->se_tmr_req;
707         struct qla_tgt_mgmt_cmd *mcmd = container_of(se_cmd,
708                                 struct qla_tgt_mgmt_cmd, se_cmd);
709
710         pr_debug("queue_tm_rsp: mcmd: %p func: 0x%02x response: 0x%02x\n",
711                         mcmd, se_tmr->function, se_tmr->response);
712         /*
713          * Do translation between TCM TM response codes and
714          * QLA2xxx FC TM response codes.
715          */
716         switch (se_tmr->response) {
717         case TMR_FUNCTION_COMPLETE:
718                 mcmd->fc_tm_rsp = FC_TM_SUCCESS;
719                 break;
720         case TMR_TASK_DOES_NOT_EXIST:
721                 mcmd->fc_tm_rsp = FC_TM_BAD_CMD;
722                 break;
723         case TMR_FUNCTION_REJECTED:
724                 mcmd->fc_tm_rsp = FC_TM_REJECT;
725                 break;
726         case TMR_LUN_DOES_NOT_EXIST:
727         default:
728                 mcmd->fc_tm_rsp = FC_TM_FAILED;
729                 break;
730         }
731         /*
732          * Queue the TM response to QLA2xxx LLD to build a
733          * CTIO response packet.
734          */
735         qlt_xmit_tm_rsp(mcmd);
736
737         return 0;
738 }
739
740 static u16 tcm_qla2xxx_get_fabric_sense_len(void)
741 {
742         return 0;
743 }
744
745 static u16 tcm_qla2xxx_set_fabric_sense_len(struct se_cmd *se_cmd,
746                                         u32 sense_length)
747 {
748         return 0;
749 }
750
751 /* Local pointer to allocated TCM configfs fabric module */
752 struct target_fabric_configfs *tcm_qla2xxx_fabric_configfs;
753 struct target_fabric_configfs *tcm_qla2xxx_npiv_fabric_configfs;
754
755 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *,
756                         struct tcm_qla2xxx_nacl *, struct qla_tgt_sess *);
757 /*
758  * Expected to be called with struct qla_hw_data->hardware_lock held
759  */
760 static void tcm_qla2xxx_clear_nacl_from_fcport_map(struct qla_tgt_sess *sess)
761 {
762         struct se_node_acl *se_nacl = sess->se_sess->se_node_acl;
763         struct se_portal_group *se_tpg = se_nacl->se_tpg;
764         struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
765         struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
766                                 struct tcm_qla2xxx_lport, lport_wwn);
767         struct tcm_qla2xxx_nacl *nacl = container_of(se_nacl,
768                                 struct tcm_qla2xxx_nacl, se_node_acl);
769         void *node;
770
771         pr_debug("fc_rport domain: port_id 0x%06x\n", nacl->nport_id);
772
773         node = btree_remove32(&lport->lport_fcport_map, nacl->nport_id);
774         WARN_ON(node && (node != se_nacl));
775
776         pr_debug("Removed from fcport_map: %p for WWNN: 0x%016LX, port_id: 0x%06x\n",
777             se_nacl, nacl->nport_wwnn, nacl->nport_id);
778         /*
779          * Now clear the se_nacl and session pointers from our HW lport lookup
780          * table mapping for this initiator's fabric S_ID and LOOP_ID entries.
781          *
782          * This is done ahead of callbacks into tcm_qla2xxx_free_session() ->
783          * target_wait_for_sess_cmds() before the session waits for outstanding
784          * I/O to complete, to avoid a race between session shutdown execution
785          * and incoming ATIOs or TMRs picking up a stale se_node_act reference.
786          */
787         tcm_qla2xxx_clear_sess_lookup(lport, nacl, sess);
788 }
789
790 static void tcm_qla2xxx_release_session(struct kref *kref)
791 {
792         struct se_session *se_sess = container_of(kref,
793                         struct se_session, sess_kref);
794
795         qlt_unreg_sess(se_sess->fabric_sess_ptr);
796 }
797
798 static void tcm_qla2xxx_put_session(struct se_session *se_sess)
799 {
800         struct qla_tgt_sess *sess = se_sess->fabric_sess_ptr;
801         struct qla_hw_data *ha = sess->vha->hw;
802         unsigned long flags;
803
804         spin_lock_irqsave(&ha->hardware_lock, flags);
805         kref_put(&se_sess->sess_kref, tcm_qla2xxx_release_session);
806         spin_unlock_irqrestore(&ha->hardware_lock, flags);
807 }
808
809 static void tcm_qla2xxx_put_sess(struct qla_tgt_sess *sess)
810 {
811         tcm_qla2xxx_put_session(sess->se_sess);
812 }
813
814 static void tcm_qla2xxx_shutdown_sess(struct qla_tgt_sess *sess)
815 {
816         tcm_qla2xxx_shutdown_session(sess->se_sess);
817 }
818
819 static struct se_node_acl *tcm_qla2xxx_make_nodeacl(
820         struct se_portal_group *se_tpg,
821         struct config_group *group,
822         const char *name)
823 {
824         struct se_node_acl *se_nacl, *se_nacl_new;
825         struct tcm_qla2xxx_nacl *nacl;
826         u64 wwnn;
827         u32 qla2xxx_nexus_depth;
828
829         if (tcm_qla2xxx_parse_wwn(name, &wwnn, 1) < 0)
830                 return ERR_PTR(-EINVAL);
831
832         se_nacl_new = tcm_qla2xxx_alloc_fabric_acl(se_tpg);
833         if (!se_nacl_new)
834                 return ERR_PTR(-ENOMEM);
835 /* #warning FIXME: Hardcoded qla2xxx_nexus depth in tcm_qla2xxx_make_nodeacl */
836         qla2xxx_nexus_depth = 1;
837
838         /*
839          * se_nacl_new may be released by core_tpg_add_initiator_node_acl()
840          * when converting a NodeACL from demo mode -> explict
841          */
842         se_nacl = core_tpg_add_initiator_node_acl(se_tpg, se_nacl_new,
843                                 name, qla2xxx_nexus_depth);
844         if (IS_ERR(se_nacl)) {
845                 tcm_qla2xxx_release_fabric_acl(se_tpg, se_nacl_new);
846                 return se_nacl;
847         }
848         /*
849          * Locate our struct tcm_qla2xxx_nacl and set the FC Nport WWPN
850          */
851         nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
852         nacl->nport_wwnn = wwnn;
853         tcm_qla2xxx_format_wwn(&nacl->nport_name[0], TCM_QLA2XXX_NAMELEN, wwnn);
854
855         return se_nacl;
856 }
857
858 static void tcm_qla2xxx_drop_nodeacl(struct se_node_acl *se_acl)
859 {
860         struct se_portal_group *se_tpg = se_acl->se_tpg;
861         struct tcm_qla2xxx_nacl *nacl = container_of(se_acl,
862                                 struct tcm_qla2xxx_nacl, se_node_acl);
863
864         core_tpg_del_initiator_node_acl(se_tpg, se_acl, 1);
865         kfree(nacl);
866 }
867
868 /* Start items for tcm_qla2xxx_tpg_attrib_cit */
869
870 #define DEF_QLA_TPG_ATTRIB(name)                                        \
871                                                                         \
872 static ssize_t tcm_qla2xxx_tpg_attrib_show_##name(                      \
873         struct se_portal_group *se_tpg,                                 \
874         char *page)                                                     \
875 {                                                                       \
876         struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,              \
877                         struct tcm_qla2xxx_tpg, se_tpg);                \
878                                                                         \
879         return sprintf(page, "%u\n", QLA_TPG_ATTRIB(tpg)->name);        \
880 }                                                                       \
881                                                                         \
882 static ssize_t tcm_qla2xxx_tpg_attrib_store_##name(                     \
883         struct se_portal_group *se_tpg,                                 \
884         const char *page,                                               \
885         size_t count)                                                   \
886 {                                                                       \
887         struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,              \
888                         struct tcm_qla2xxx_tpg, se_tpg);                \
889         unsigned long val;                                              \
890         int ret;                                                        \
891                                                                         \
892         ret = kstrtoul(page, 0, &val);                                  \
893         if (ret < 0) {                                                  \
894                 pr_err("kstrtoul() failed with"                         \
895                                 " ret: %d\n", ret);                     \
896                 return -EINVAL;                                         \
897         }                                                               \
898         ret = tcm_qla2xxx_set_attrib_##name(tpg, val);                  \
899                                                                         \
900         return (!ret) ? count : -EINVAL;                                \
901 }
902
903 #define DEF_QLA_TPG_ATTR_BOOL(_name)                                    \
904                                                                         \
905 static int tcm_qla2xxx_set_attrib_##_name(                              \
906         struct tcm_qla2xxx_tpg *tpg,                                    \
907         unsigned long val)                                              \
908 {                                                                       \
909         struct tcm_qla2xxx_tpg_attrib *a = &tpg->tpg_attrib;            \
910                                                                         \
911         if ((val != 0) && (val != 1)) {                                 \
912                 pr_err("Illegal boolean value %lu\n", val);             \
913                 return -EINVAL;                                         \
914         }                                                               \
915                                                                         \
916         a->_name = val;                                                 \
917         return 0;                                                       \
918 }
919
920 #define QLA_TPG_ATTR(_name, _mode) \
921         TF_TPG_ATTRIB_ATTR(tcm_qla2xxx, _name, _mode);
922
923 /*
924  * Define tcm_qla2xxx_tpg_attrib_s_generate_node_acls
925  */
926 DEF_QLA_TPG_ATTR_BOOL(generate_node_acls);
927 DEF_QLA_TPG_ATTRIB(generate_node_acls);
928 QLA_TPG_ATTR(generate_node_acls, S_IRUGO | S_IWUSR);
929
930 /*
931  Define tcm_qla2xxx_attrib_s_cache_dynamic_acls
932  */
933 DEF_QLA_TPG_ATTR_BOOL(cache_dynamic_acls);
934 DEF_QLA_TPG_ATTRIB(cache_dynamic_acls);
935 QLA_TPG_ATTR(cache_dynamic_acls, S_IRUGO | S_IWUSR);
936
937 /*
938  * Define tcm_qla2xxx_tpg_attrib_s_demo_mode_write_protect
939  */
940 DEF_QLA_TPG_ATTR_BOOL(demo_mode_write_protect);
941 DEF_QLA_TPG_ATTRIB(demo_mode_write_protect);
942 QLA_TPG_ATTR(demo_mode_write_protect, S_IRUGO | S_IWUSR);
943
944 /*
945  * Define tcm_qla2xxx_tpg_attrib_s_prod_mode_write_protect
946  */
947 DEF_QLA_TPG_ATTR_BOOL(prod_mode_write_protect);
948 DEF_QLA_TPG_ATTRIB(prod_mode_write_protect);
949 QLA_TPG_ATTR(prod_mode_write_protect, S_IRUGO | S_IWUSR);
950
951 static struct configfs_attribute *tcm_qla2xxx_tpg_attrib_attrs[] = {
952         &tcm_qla2xxx_tpg_attrib_generate_node_acls.attr,
953         &tcm_qla2xxx_tpg_attrib_cache_dynamic_acls.attr,
954         &tcm_qla2xxx_tpg_attrib_demo_mode_write_protect.attr,
955         &tcm_qla2xxx_tpg_attrib_prod_mode_write_protect.attr,
956         NULL,
957 };
958
959 /* End items for tcm_qla2xxx_tpg_attrib_cit */
960
961 static ssize_t tcm_qla2xxx_tpg_show_enable(
962         struct se_portal_group *se_tpg,
963         char *page)
964 {
965         struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
966                         struct tcm_qla2xxx_tpg, se_tpg);
967
968         return snprintf(page, PAGE_SIZE, "%d\n",
969                         atomic_read(&tpg->lport_tpg_enabled));
970 }
971
972 static ssize_t tcm_qla2xxx_tpg_store_enable(
973         struct se_portal_group *se_tpg,
974         const char *page,
975         size_t count)
976 {
977         struct se_wwn *se_wwn = se_tpg->se_tpg_wwn;
978         struct tcm_qla2xxx_lport *lport = container_of(se_wwn,
979                         struct tcm_qla2xxx_lport, lport_wwn);
980         struct scsi_qla_host *vha = lport->qla_vha;
981         struct qla_hw_data *ha = vha->hw;
982         struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
983                         struct tcm_qla2xxx_tpg, se_tpg);
984         unsigned long op;
985         int rc;
986
987         rc = kstrtoul(page, 0, &op);
988         if (rc < 0) {
989                 pr_err("kstrtoul() returned %d\n", rc);
990                 return -EINVAL;
991         }
992         if ((op != 1) && (op != 0)) {
993                 pr_err("Illegal value for tpg_enable: %lu\n", op);
994                 return -EINVAL;
995         }
996
997         if (op) {
998                 atomic_set(&tpg->lport_tpg_enabled, 1);
999                 qlt_enable_vha(vha);
1000         } else {
1001                 if (!ha->tgt.qla_tgt) {
1002                         pr_err("truct qla_hw_data *ha->tgt.qla_tgt is NULL\n");
1003                         return -ENODEV;
1004                 }
1005                 atomic_set(&tpg->lport_tpg_enabled, 0);
1006                 qlt_stop_phase1(ha->tgt.qla_tgt);
1007         }
1008
1009         return count;
1010 }
1011
1012 TF_TPG_BASE_ATTR(tcm_qla2xxx, enable, S_IRUGO | S_IWUSR);
1013
1014 static struct configfs_attribute *tcm_qla2xxx_tpg_attrs[] = {
1015         &tcm_qla2xxx_tpg_enable.attr,
1016         NULL,
1017 };
1018
1019 static struct se_portal_group *tcm_qla2xxx_make_tpg(
1020         struct se_wwn *wwn,
1021         struct config_group *group,
1022         const char *name)
1023 {
1024         struct tcm_qla2xxx_lport *lport = container_of(wwn,
1025                         struct tcm_qla2xxx_lport, lport_wwn);
1026         struct tcm_qla2xxx_tpg *tpg;
1027         unsigned long tpgt;
1028         int ret;
1029
1030         if (strstr(name, "tpgt_") != name)
1031                 return ERR_PTR(-EINVAL);
1032         if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1033                 return ERR_PTR(-EINVAL);
1034
1035         if (!lport->qla_npiv_vp && (tpgt != 1)) {
1036                 pr_err("In non NPIV mode, a single TPG=1 is used for HW port mappings\n");
1037                 return ERR_PTR(-ENOSYS);
1038         }
1039
1040         tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1041         if (!tpg) {
1042                 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1043                 return ERR_PTR(-ENOMEM);
1044         }
1045         tpg->lport = lport;
1046         tpg->lport_tpgt = tpgt;
1047         /*
1048          * By default allow READ-ONLY TPG demo-mode access w/ cached dynamic
1049          * NodeACLs
1050          */
1051         QLA_TPG_ATTRIB(tpg)->generate_node_acls = 1;
1052         QLA_TPG_ATTRIB(tpg)->demo_mode_write_protect = 1;
1053         QLA_TPG_ATTRIB(tpg)->cache_dynamic_acls = 1;
1054
1055         ret = core_tpg_register(&tcm_qla2xxx_fabric_configfs->tf_ops, wwn,
1056                                 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1057         if (ret < 0) {
1058                 kfree(tpg);
1059                 return NULL;
1060         }
1061         /*
1062          * Setup local TPG=1 pointer for non NPIV mode.
1063          */
1064         if (lport->qla_npiv_vp == NULL)
1065                 lport->tpg_1 = tpg;
1066
1067         return &tpg->se_tpg;
1068 }
1069
1070 static void tcm_qla2xxx_drop_tpg(struct se_portal_group *se_tpg)
1071 {
1072         struct tcm_qla2xxx_tpg *tpg = container_of(se_tpg,
1073                         struct tcm_qla2xxx_tpg, se_tpg);
1074         struct tcm_qla2xxx_lport *lport = tpg->lport;
1075         struct scsi_qla_host *vha = lport->qla_vha;
1076         struct qla_hw_data *ha = vha->hw;
1077         /*
1078          * Call into qla2x_target.c LLD logic to shutdown the active
1079          * FC Nexuses and disable target mode operation for this qla_hw_data
1080          */
1081         if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stop)
1082                 qlt_stop_phase1(ha->tgt.qla_tgt);
1083
1084         core_tpg_deregister(se_tpg);
1085         /*
1086          * Clear local TPG=1 pointer for non NPIV mode.
1087          */
1088         if (lport->qla_npiv_vp == NULL)
1089                 lport->tpg_1 = NULL;
1090
1091         kfree(tpg);
1092 }
1093
1094 static struct se_portal_group *tcm_qla2xxx_npiv_make_tpg(
1095         struct se_wwn *wwn,
1096         struct config_group *group,
1097         const char *name)
1098 {
1099         struct tcm_qla2xxx_lport *lport = container_of(wwn,
1100                         struct tcm_qla2xxx_lport, lport_wwn);
1101         struct tcm_qla2xxx_tpg *tpg;
1102         unsigned long tpgt;
1103         int ret;
1104
1105         if (strstr(name, "tpgt_") != name)
1106                 return ERR_PTR(-EINVAL);
1107         if (kstrtoul(name + 5, 10, &tpgt) || tpgt > USHRT_MAX)
1108                 return ERR_PTR(-EINVAL);
1109
1110         tpg = kzalloc(sizeof(struct tcm_qla2xxx_tpg), GFP_KERNEL);
1111         if (!tpg) {
1112                 pr_err("Unable to allocate struct tcm_qla2xxx_tpg\n");
1113                 return ERR_PTR(-ENOMEM);
1114         }
1115         tpg->lport = lport;
1116         tpg->lport_tpgt = tpgt;
1117
1118         ret = core_tpg_register(&tcm_qla2xxx_npiv_fabric_configfs->tf_ops, wwn,
1119                                 &tpg->se_tpg, tpg, TRANSPORT_TPG_TYPE_NORMAL);
1120         if (ret < 0) {
1121                 kfree(tpg);
1122                 return NULL;
1123         }
1124         return &tpg->se_tpg;
1125 }
1126
1127 /*
1128  * Expected to be called with struct qla_hw_data->hardware_lock held
1129  */
1130 static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_s_id(
1131         scsi_qla_host_t *vha,
1132         const uint8_t *s_id)
1133 {
1134         struct qla_hw_data *ha = vha->hw;
1135         struct tcm_qla2xxx_lport *lport;
1136         struct se_node_acl *se_nacl;
1137         struct tcm_qla2xxx_nacl *nacl;
1138         u32 key;
1139
1140         lport = ha->tgt.target_lport_ptr;
1141         if (!lport) {
1142                 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1143                 dump_stack();
1144                 return NULL;
1145         }
1146
1147         key = (((unsigned long)s_id[0] << 16) |
1148                ((unsigned long)s_id[1] << 8) |
1149                (unsigned long)s_id[2]);
1150         pr_debug("find_sess_by_s_id: 0x%06x\n", key);
1151
1152         se_nacl = btree_lookup32(&lport->lport_fcport_map, key);
1153         if (!se_nacl) {
1154                 pr_debug("Unable to locate s_id: 0x%06x\n", key);
1155                 return NULL;
1156         }
1157         pr_debug("find_sess_by_s_id: located se_nacl: %p, initiatorname: %s\n",
1158             se_nacl, se_nacl->initiatorname);
1159
1160         nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1161         if (!nacl->qla_tgt_sess) {
1162                 pr_err("Unable to locate struct qla_tgt_sess\n");
1163                 return NULL;
1164         }
1165
1166         return nacl->qla_tgt_sess;
1167 }
1168
1169 /*
1170  * Expected to be called with struct qla_hw_data->hardware_lock held
1171  */
1172 static void tcm_qla2xxx_set_sess_by_s_id(
1173         struct tcm_qla2xxx_lport *lport,
1174         struct se_node_acl *new_se_nacl,
1175         struct tcm_qla2xxx_nacl *nacl,
1176         struct se_session *se_sess,
1177         struct qla_tgt_sess *qla_tgt_sess,
1178         uint8_t *s_id)
1179 {
1180         u32 key;
1181         void *slot;
1182         int rc;
1183
1184         key = (((unsigned long)s_id[0] << 16) |
1185                ((unsigned long)s_id[1] << 8) |
1186                (unsigned long)s_id[2]);
1187         pr_debug("set_sess_by_s_id: %06x\n", key);
1188
1189         slot = btree_lookup32(&lport->lport_fcport_map, key);
1190         if (!slot) {
1191                 if (new_se_nacl) {
1192                         pr_debug("Setting up new fc_port entry to new_se_nacl\n");
1193                         nacl->nport_id = key;
1194                         rc = btree_insert32(&lport->lport_fcport_map, key,
1195                                         new_se_nacl, GFP_ATOMIC);
1196                         if (rc)
1197                                 printk(KERN_ERR "Unable to insert s_id into fcport_map: %06x\n",
1198                                     (int)key);
1199                 } else {
1200                         pr_debug("Wiping nonexisting fc_port entry\n");
1201                 }
1202
1203                 qla_tgt_sess->se_sess = se_sess;
1204                 nacl->qla_tgt_sess = qla_tgt_sess;
1205                 return;
1206         }
1207
1208         if (nacl->qla_tgt_sess) {
1209                 if (new_se_nacl == NULL) {
1210                         pr_debug("Clearing existing nacl->qla_tgt_sess and fc_port entry\n");
1211                         btree_remove32(&lport->lport_fcport_map, key);
1212                         nacl->qla_tgt_sess = NULL;
1213                         return;
1214                 }
1215                 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_port entry\n");
1216                 btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1217                 qla_tgt_sess->se_sess = se_sess;
1218                 nacl->qla_tgt_sess = qla_tgt_sess;
1219                 return;
1220         }
1221
1222         if (new_se_nacl == NULL) {
1223                 pr_debug("Clearing existing fc_port entry\n");
1224                 btree_remove32(&lport->lport_fcport_map, key);
1225                 return;
1226         }
1227
1228         pr_debug("Replacing existing fc_port entry w/o active nacl->qla_tgt_sess\n");
1229         btree_update32(&lport->lport_fcport_map, key, new_se_nacl);
1230         qla_tgt_sess->se_sess = se_sess;
1231         nacl->qla_tgt_sess = qla_tgt_sess;
1232
1233         pr_debug("Setup nacl->qla_tgt_sess %p by s_id for se_nacl: %p, initiatorname: %s\n",
1234             nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1235 }
1236
1237 /*
1238  * Expected to be called with struct qla_hw_data->hardware_lock held
1239  */
1240 static struct qla_tgt_sess *tcm_qla2xxx_find_sess_by_loop_id(
1241         scsi_qla_host_t *vha,
1242         const uint16_t loop_id)
1243 {
1244         struct qla_hw_data *ha = vha->hw;
1245         struct tcm_qla2xxx_lport *lport;
1246         struct se_node_acl *se_nacl;
1247         struct tcm_qla2xxx_nacl *nacl;
1248         struct tcm_qla2xxx_fc_loopid *fc_loopid;
1249
1250         lport = ha->tgt.target_lport_ptr;
1251         if (!lport) {
1252                 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1253                 dump_stack();
1254                 return NULL;
1255         }
1256
1257         pr_debug("find_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1258
1259         fc_loopid = lport->lport_loopid_map + loop_id;
1260         se_nacl = fc_loopid->se_nacl;
1261         if (!se_nacl) {
1262                 pr_debug("Unable to locate se_nacl by loop_id: 0x%04x\n",
1263                     loop_id);
1264                 return NULL;
1265         }
1266
1267         nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1268
1269         if (!nacl->qla_tgt_sess) {
1270                 pr_err("Unable to locate struct qla_tgt_sess\n");
1271                 return NULL;
1272         }
1273
1274         return nacl->qla_tgt_sess;
1275 }
1276
1277 /*
1278  * Expected to be called with struct qla_hw_data->hardware_lock held
1279  */
1280 static void tcm_qla2xxx_set_sess_by_loop_id(
1281         struct tcm_qla2xxx_lport *lport,
1282         struct se_node_acl *new_se_nacl,
1283         struct tcm_qla2xxx_nacl *nacl,
1284         struct se_session *se_sess,
1285         struct qla_tgt_sess *qla_tgt_sess,
1286         uint16_t loop_id)
1287 {
1288         struct se_node_acl *saved_nacl;
1289         struct tcm_qla2xxx_fc_loopid *fc_loopid;
1290
1291         pr_debug("set_sess_by_loop_id: Using loop_id: 0x%04x\n", loop_id);
1292
1293         fc_loopid = &((struct tcm_qla2xxx_fc_loopid *)
1294                         lport->lport_loopid_map)[loop_id];
1295
1296         saved_nacl = fc_loopid->se_nacl;
1297         if (!saved_nacl) {
1298                 pr_debug("Setting up new fc_loopid->se_nacl to new_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 (nacl->qla_tgt_sess) {
1308                 if (new_se_nacl == NULL) {
1309                         pr_debug("Clearing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1310                         fc_loopid->se_nacl = NULL;
1311                         nacl->qla_tgt_sess = NULL;
1312                         return;
1313                 }
1314
1315                 pr_debug("Replacing existing nacl->qla_tgt_sess and fc_loopid->se_nacl\n");
1316                 fc_loopid->se_nacl = new_se_nacl;
1317                 if (qla_tgt_sess->se_sess != se_sess)
1318                         qla_tgt_sess->se_sess = se_sess;
1319                 if (nacl->qla_tgt_sess != qla_tgt_sess)
1320                         nacl->qla_tgt_sess = qla_tgt_sess;
1321                 return;
1322         }
1323
1324         if (new_se_nacl == NULL) {
1325                 pr_debug("Clearing fc_loopid->se_nacl\n");
1326                 fc_loopid->se_nacl = NULL;
1327                 return;
1328         }
1329
1330         pr_debug("Replacing existing fc_loopid->se_nacl w/o active nacl->qla_tgt_sess\n");
1331         fc_loopid->se_nacl = new_se_nacl;
1332         if (qla_tgt_sess->se_sess != se_sess)
1333                 qla_tgt_sess->se_sess = se_sess;
1334         if (nacl->qla_tgt_sess != qla_tgt_sess)
1335                 nacl->qla_tgt_sess = qla_tgt_sess;
1336
1337         pr_debug("Setup nacl->qla_tgt_sess %p by loop_id for se_nacl: %p, initiatorname: %s\n",
1338             nacl->qla_tgt_sess, new_se_nacl, new_se_nacl->initiatorname);
1339 }
1340
1341 /*
1342  * Should always be called with qla_hw_data->hardware_lock held.
1343  */
1344 static void tcm_qla2xxx_clear_sess_lookup(struct tcm_qla2xxx_lport *lport,
1345                 struct tcm_qla2xxx_nacl *nacl, struct qla_tgt_sess *sess)
1346 {
1347         struct se_session *se_sess = sess->se_sess;
1348         unsigned char be_sid[3];
1349
1350         be_sid[0] = sess->s_id.b.domain;
1351         be_sid[1] = sess->s_id.b.area;
1352         be_sid[2] = sess->s_id.b.al_pa;
1353
1354         tcm_qla2xxx_set_sess_by_s_id(lport, NULL, nacl, se_sess,
1355                                 sess, be_sid);
1356         tcm_qla2xxx_set_sess_by_loop_id(lport, NULL, nacl, se_sess,
1357                                 sess, sess->loop_id);
1358 }
1359
1360 static void tcm_qla2xxx_free_session(struct qla_tgt_sess *sess)
1361 {
1362         struct qla_tgt *tgt = sess->tgt;
1363         struct qla_hw_data *ha = tgt->ha;
1364         struct se_session *se_sess;
1365         struct se_node_acl *se_nacl;
1366         struct tcm_qla2xxx_lport *lport;
1367         struct tcm_qla2xxx_nacl *nacl;
1368
1369         BUG_ON(in_interrupt());
1370
1371         se_sess = sess->se_sess;
1372         if (!se_sess) {
1373                 pr_err("struct qla_tgt_sess->se_sess is NULL\n");
1374                 dump_stack();
1375                 return;
1376         }
1377         se_nacl = se_sess->se_node_acl;
1378         nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1379
1380         lport = ha->tgt.target_lport_ptr;
1381         if (!lport) {
1382                 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1383                 dump_stack();
1384                 return;
1385         }
1386         target_wait_for_sess_cmds(se_sess, 0);
1387
1388         transport_deregister_session_configfs(sess->se_sess);
1389         transport_deregister_session(sess->se_sess);
1390 }
1391
1392 /*
1393  * Called via qlt_create_sess():ha->qla2x_tmpl->check_initiator_node_acl()
1394  * to locate struct se_node_acl
1395  */
1396 static int tcm_qla2xxx_check_initiator_node_acl(
1397         scsi_qla_host_t *vha,
1398         unsigned char *fc_wwpn,
1399         void *qla_tgt_sess,
1400         uint8_t *s_id,
1401         uint16_t loop_id)
1402 {
1403         struct qla_hw_data *ha = vha->hw;
1404         struct tcm_qla2xxx_lport *lport;
1405         struct tcm_qla2xxx_tpg *tpg;
1406         struct tcm_qla2xxx_nacl *nacl;
1407         struct se_portal_group *se_tpg;
1408         struct se_node_acl *se_nacl;
1409         struct se_session *se_sess;
1410         struct qla_tgt_sess *sess = qla_tgt_sess;
1411         unsigned char port_name[36];
1412         unsigned long flags;
1413
1414         lport = ha->tgt.target_lport_ptr;
1415         if (!lport) {
1416                 pr_err("Unable to locate struct tcm_qla2xxx_lport\n");
1417                 dump_stack();
1418                 return -EINVAL;
1419         }
1420         /*
1421          * Locate the TPG=1 reference..
1422          */
1423         tpg = lport->tpg_1;
1424         if (!tpg) {
1425                 pr_err("Unable to lcoate struct tcm_qla2xxx_lport->tpg_1\n");
1426                 return -EINVAL;
1427         }
1428         se_tpg = &tpg->se_tpg;
1429
1430         se_sess = transport_init_session();
1431         if (IS_ERR(se_sess)) {
1432                 pr_err("Unable to initialize struct se_session\n");
1433                 return PTR_ERR(se_sess);
1434         }
1435         /*
1436          * Format the FCP Initiator port_name into colon seperated values to
1437          * match the format by tcm_qla2xxx explict ConfigFS NodeACLs.
1438          */
1439         memset(&port_name, 0, 36);
1440         snprintf(port_name, 36, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
1441                 fc_wwpn[0], fc_wwpn[1], fc_wwpn[2], fc_wwpn[3], fc_wwpn[4],
1442                 fc_wwpn[5], fc_wwpn[6], fc_wwpn[7]);
1443         /*
1444          * Locate our struct se_node_acl either from an explict NodeACL created
1445          * via ConfigFS, or via running in TPG demo mode.
1446          */
1447         se_sess->se_node_acl = core_tpg_check_initiator_node_acl(se_tpg,
1448                                         port_name);
1449         if (!se_sess->se_node_acl) {
1450                 transport_free_session(se_sess);
1451                 return -EINVAL;
1452         }
1453         se_nacl = se_sess->se_node_acl;
1454         nacl = container_of(se_nacl, struct tcm_qla2xxx_nacl, se_node_acl);
1455         /*
1456          * And now setup the new se_nacl and session pointers into our HW lport
1457          * mappings for fabric S_ID and LOOP_ID.
1458          */
1459         spin_lock_irqsave(&ha->hardware_lock, flags);
1460         tcm_qla2xxx_set_sess_by_s_id(lport, se_nacl, nacl, se_sess,
1461                         qla_tgt_sess, s_id);
1462         tcm_qla2xxx_set_sess_by_loop_id(lport, se_nacl, nacl, se_sess,
1463                         qla_tgt_sess, loop_id);
1464         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1465         /*
1466          * Finally register the new FC Nexus with TCM
1467          */
1468         __transport_register_session(se_nacl->se_tpg, se_nacl, se_sess, sess);
1469
1470         return 0;
1471 }
1472
1473 /*
1474  * Calls into tcm_qla2xxx used by qla2xxx LLD I/O path.
1475  */
1476 static struct qla_tgt_func_tmpl tcm_qla2xxx_template = {
1477         .handle_cmd             = tcm_qla2xxx_handle_cmd,
1478         .handle_data            = tcm_qla2xxx_handle_data,
1479         .handle_tmr             = tcm_qla2xxx_handle_tmr,
1480         .free_cmd               = tcm_qla2xxx_free_cmd,
1481         .free_mcmd              = tcm_qla2xxx_free_mcmd,
1482         .free_session           = tcm_qla2xxx_free_session,
1483         .check_initiator_node_acl = tcm_qla2xxx_check_initiator_node_acl,
1484         .find_sess_by_s_id      = tcm_qla2xxx_find_sess_by_s_id,
1485         .find_sess_by_loop_id   = tcm_qla2xxx_find_sess_by_loop_id,
1486         .clear_nacl_from_fcport_map = tcm_qla2xxx_clear_nacl_from_fcport_map,
1487         .put_sess               = tcm_qla2xxx_put_sess,
1488         .shutdown_sess          = tcm_qla2xxx_shutdown_sess,
1489 };
1490
1491 static int tcm_qla2xxx_init_lport(struct tcm_qla2xxx_lport *lport)
1492 {
1493         int rc;
1494
1495         rc = btree_init32(&lport->lport_fcport_map);
1496         if (rc) {
1497                 pr_err("Unable to initialize lport->lport_fcport_map btree\n");
1498                 return rc;
1499         }
1500
1501         lport->lport_loopid_map = vmalloc(sizeof(struct tcm_qla2xxx_fc_loopid) *
1502                                 65536);
1503         if (!lport->lport_loopid_map) {
1504                 pr_err("Unable to allocate lport->lport_loopid_map of %zu bytes\n",
1505                     sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1506                 btree_destroy32(&lport->lport_fcport_map);
1507                 return -ENOMEM;
1508         }
1509         memset(lport->lport_loopid_map, 0, sizeof(struct tcm_qla2xxx_fc_loopid)
1510                * 65536);
1511         pr_debug("qla2xxx: Allocated lport_loopid_map of %zu bytes\n",
1512                sizeof(struct tcm_qla2xxx_fc_loopid) * 65536);
1513         return 0;
1514 }
1515
1516 static int tcm_qla2xxx_lport_register_cb(struct scsi_qla_host *vha)
1517 {
1518         struct qla_hw_data *ha = vha->hw;
1519         struct tcm_qla2xxx_lport *lport;
1520         /*
1521          * Setup local pointer to vha, NPIV VP pointer (if present) and
1522          * vha->tcm_lport pointer
1523          */
1524         lport = (struct tcm_qla2xxx_lport *)ha->tgt.target_lport_ptr;
1525         lport->qla_vha = vha;
1526
1527         return 0;
1528 }
1529
1530 static struct se_wwn *tcm_qla2xxx_make_lport(
1531         struct target_fabric_configfs *tf,
1532         struct config_group *group,
1533         const char *name)
1534 {
1535         struct tcm_qla2xxx_lport *lport;
1536         u64 wwpn;
1537         int ret = -ENODEV;
1538
1539         if (tcm_qla2xxx_parse_wwn(name, &wwpn, 1) < 0)
1540                 return ERR_PTR(-EINVAL);
1541
1542         lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1543         if (!lport) {
1544                 pr_err("Unable to allocate struct tcm_qla2xxx_lport\n");
1545                 return ERR_PTR(-ENOMEM);
1546         }
1547         lport->lport_wwpn = wwpn;
1548         tcm_qla2xxx_format_wwn(&lport->lport_name[0], TCM_QLA2XXX_NAMELEN,
1549                                 wwpn);
1550
1551         ret = tcm_qla2xxx_init_lport(lport);
1552         if (ret != 0)
1553                 goto out;
1554
1555         ret = qlt_lport_register(&tcm_qla2xxx_template, wwpn,
1556                                 tcm_qla2xxx_lport_register_cb, lport);
1557         if (ret != 0)
1558                 goto out_lport;
1559
1560         return &lport->lport_wwn;
1561 out_lport:
1562         vfree(lport->lport_loopid_map);
1563         btree_destroy32(&lport->lport_fcport_map);
1564 out:
1565         kfree(lport);
1566         return ERR_PTR(ret);
1567 }
1568
1569 static void tcm_qla2xxx_drop_lport(struct se_wwn *wwn)
1570 {
1571         struct tcm_qla2xxx_lport *lport = container_of(wwn,
1572                         struct tcm_qla2xxx_lport, lport_wwn);
1573         struct scsi_qla_host *vha = lport->qla_vha;
1574         struct qla_hw_data *ha = vha->hw;
1575         struct se_node_acl *node;
1576         u32 key = 0;
1577
1578         /*
1579          * Call into qla2x_target.c LLD logic to complete the
1580          * shutdown of struct qla_tgt after the call to
1581          * qlt_stop_phase1() from tcm_qla2xxx_drop_tpg() above..
1582          */
1583         if (ha->tgt.qla_tgt && !ha->tgt.qla_tgt->tgt_stopped)
1584                 qlt_stop_phase2(ha->tgt.qla_tgt);
1585
1586         qlt_lport_deregister(vha);
1587
1588         vfree(lport->lport_loopid_map);
1589         btree_for_each_safe32(&lport->lport_fcport_map, key, node)
1590                 btree_remove32(&lport->lport_fcport_map, key);
1591         btree_destroy32(&lport->lport_fcport_map);
1592         kfree(lport);
1593 }
1594
1595 static struct se_wwn *tcm_qla2xxx_npiv_make_lport(
1596         struct target_fabric_configfs *tf,
1597         struct config_group *group,
1598         const char *name)
1599 {
1600         struct tcm_qla2xxx_lport *lport;
1601         u64 npiv_wwpn, npiv_wwnn;
1602         int ret;
1603
1604         if (tcm_qla2xxx_npiv_parse_wwn(name, strlen(name)+1,
1605                                 &npiv_wwpn, &npiv_wwnn) < 0)
1606                 return ERR_PTR(-EINVAL);
1607
1608         lport = kzalloc(sizeof(struct tcm_qla2xxx_lport), GFP_KERNEL);
1609         if (!lport) {
1610                 pr_err("Unable to allocate struct tcm_qla2xxx_lport for NPIV\n");
1611                 return ERR_PTR(-ENOMEM);
1612         }
1613         lport->lport_npiv_wwpn = npiv_wwpn;
1614         lport->lport_npiv_wwnn = npiv_wwnn;
1615         tcm_qla2xxx_npiv_format_wwn(&lport->lport_npiv_name[0],
1616                         TCM_QLA2XXX_NAMELEN, npiv_wwpn, npiv_wwnn);
1617
1618 /* FIXME: tcm_qla2xxx_npiv_make_lport */
1619         ret = -ENOSYS;
1620         if (ret != 0)
1621                 goto out;
1622
1623         return &lport->lport_wwn;
1624 out:
1625         kfree(lport);
1626         return ERR_PTR(ret);
1627 }
1628
1629 static void tcm_qla2xxx_npiv_drop_lport(struct se_wwn *wwn)
1630 {
1631         struct tcm_qla2xxx_lport *lport = container_of(wwn,
1632                         struct tcm_qla2xxx_lport, lport_wwn);
1633         struct scsi_qla_host *vha = lport->qla_vha;
1634         struct Scsi_Host *sh = vha->host;
1635         /*
1636          * Notify libfc that we want to release the lport->npiv_vport
1637          */
1638         fc_vport_terminate(lport->npiv_vport);
1639
1640         scsi_host_put(sh);
1641         kfree(lport);
1642 }
1643
1644
1645 static ssize_t tcm_qla2xxx_wwn_show_attr_version(
1646         struct target_fabric_configfs *tf,
1647         char *page)
1648 {
1649         return sprintf(page,
1650             "TCM QLOGIC QLA2XXX NPIV capable fabric module %s on %s/%s on "
1651             UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1652             utsname()->machine);
1653 }
1654
1655 TF_WWN_ATTR_RO(tcm_qla2xxx, version);
1656
1657 static struct configfs_attribute *tcm_qla2xxx_wwn_attrs[] = {
1658         &tcm_qla2xxx_wwn_version.attr,
1659         NULL,
1660 };
1661
1662 static struct target_core_fabric_ops tcm_qla2xxx_ops = {
1663         .get_fabric_name                = tcm_qla2xxx_get_fabric_name,
1664         .get_fabric_proto_ident         = tcm_qla2xxx_get_fabric_proto_ident,
1665         .tpg_get_wwn                    = tcm_qla2xxx_get_fabric_wwn,
1666         .tpg_get_tag                    = tcm_qla2xxx_get_tag,
1667         .tpg_get_default_depth          = tcm_qla2xxx_get_default_depth,
1668         .tpg_get_pr_transport_id        = tcm_qla2xxx_get_pr_transport_id,
1669         .tpg_get_pr_transport_id_len    = tcm_qla2xxx_get_pr_transport_id_len,
1670         .tpg_parse_pr_out_transport_id  = tcm_qla2xxx_parse_pr_out_transport_id,
1671         .tpg_check_demo_mode            = tcm_qla2xxx_check_demo_mode,
1672         .tpg_check_demo_mode_cache      = tcm_qla2xxx_check_demo_mode_cache,
1673         .tpg_check_demo_mode_write_protect =
1674                                         tcm_qla2xxx_check_demo_write_protect,
1675         .tpg_check_prod_mode_write_protect =
1676                                         tcm_qla2xxx_check_prod_write_protect,
1677         .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
1678         .tpg_alloc_fabric_acl           = tcm_qla2xxx_alloc_fabric_acl,
1679         .tpg_release_fabric_acl         = tcm_qla2xxx_release_fabric_acl,
1680         .tpg_get_inst_index             = tcm_qla2xxx_tpg_get_inst_index,
1681         .new_cmd_map                    = NULL,
1682         .check_stop_free                = tcm_qla2xxx_check_stop_free,
1683         .release_cmd                    = tcm_qla2xxx_release_cmd,
1684         .put_session                    = tcm_qla2xxx_put_session,
1685         .shutdown_session               = tcm_qla2xxx_shutdown_session,
1686         .close_session                  = tcm_qla2xxx_close_session,
1687         .sess_get_index                 = tcm_qla2xxx_sess_get_index,
1688         .sess_get_initiator_sid         = NULL,
1689         .write_pending                  = tcm_qla2xxx_write_pending,
1690         .write_pending_status           = tcm_qla2xxx_write_pending_status,
1691         .set_default_node_attributes    = tcm_qla2xxx_set_default_node_attrs,
1692         .get_task_tag                   = tcm_qla2xxx_get_task_tag,
1693         .get_cmd_state                  = tcm_qla2xxx_get_cmd_state,
1694         .queue_data_in                  = tcm_qla2xxx_queue_data_in,
1695         .queue_status                   = tcm_qla2xxx_queue_status,
1696         .queue_tm_rsp                   = tcm_qla2xxx_queue_tm_rsp,
1697         .get_fabric_sense_len           = tcm_qla2xxx_get_fabric_sense_len,
1698         .set_fabric_sense_len           = tcm_qla2xxx_set_fabric_sense_len,
1699         /*
1700          * Setup function pointers for generic logic in
1701          * target_core_fabric_configfs.c
1702          */
1703         .fabric_make_wwn                = tcm_qla2xxx_make_lport,
1704         .fabric_drop_wwn                = tcm_qla2xxx_drop_lport,
1705         .fabric_make_tpg                = tcm_qla2xxx_make_tpg,
1706         .fabric_drop_tpg                = tcm_qla2xxx_drop_tpg,
1707         .fabric_post_link               = NULL,
1708         .fabric_pre_unlink              = NULL,
1709         .fabric_make_np                 = NULL,
1710         .fabric_drop_np                 = NULL,
1711         .fabric_make_nodeacl            = tcm_qla2xxx_make_nodeacl,
1712         .fabric_drop_nodeacl            = tcm_qla2xxx_drop_nodeacl,
1713 };
1714
1715 static struct target_core_fabric_ops tcm_qla2xxx_npiv_ops = {
1716         .get_fabric_name                = tcm_qla2xxx_npiv_get_fabric_name,
1717         .get_fabric_proto_ident         = tcm_qla2xxx_get_fabric_proto_ident,
1718         .tpg_get_wwn                    = tcm_qla2xxx_npiv_get_fabric_wwn,
1719         .tpg_get_tag                    = tcm_qla2xxx_get_tag,
1720         .tpg_get_default_depth          = tcm_qla2xxx_get_default_depth,
1721         .tpg_get_pr_transport_id        = tcm_qla2xxx_get_pr_transport_id,
1722         .tpg_get_pr_transport_id_len    = tcm_qla2xxx_get_pr_transport_id_len,
1723         .tpg_parse_pr_out_transport_id  = tcm_qla2xxx_parse_pr_out_transport_id,
1724         .tpg_check_demo_mode            = tcm_qla2xxx_check_false,
1725         .tpg_check_demo_mode_cache      = tcm_qla2xxx_check_true,
1726         .tpg_check_demo_mode_write_protect = tcm_qla2xxx_check_true,
1727         .tpg_check_prod_mode_write_protect = tcm_qla2xxx_check_false,
1728         .tpg_check_demo_mode_login_only = tcm_qla2xxx_check_true,
1729         .tpg_alloc_fabric_acl           = tcm_qla2xxx_alloc_fabric_acl,
1730         .tpg_release_fabric_acl         = tcm_qla2xxx_release_fabric_acl,
1731         .tpg_get_inst_index             = tcm_qla2xxx_tpg_get_inst_index,
1732         .release_cmd                    = tcm_qla2xxx_release_cmd,
1733         .put_session                    = tcm_qla2xxx_put_session,
1734         .shutdown_session               = tcm_qla2xxx_shutdown_session,
1735         .close_session                  = tcm_qla2xxx_close_session,
1736         .sess_get_index                 = tcm_qla2xxx_sess_get_index,
1737         .sess_get_initiator_sid         = NULL,
1738         .write_pending                  = tcm_qla2xxx_write_pending,
1739         .write_pending_status           = tcm_qla2xxx_write_pending_status,
1740         .set_default_node_attributes    = tcm_qla2xxx_set_default_node_attrs,
1741         .get_task_tag                   = tcm_qla2xxx_get_task_tag,
1742         .get_cmd_state                  = tcm_qla2xxx_get_cmd_state,
1743         .queue_data_in                  = tcm_qla2xxx_queue_data_in,
1744         .queue_status                   = tcm_qla2xxx_queue_status,
1745         .queue_tm_rsp                   = tcm_qla2xxx_queue_tm_rsp,
1746         .get_fabric_sense_len           = tcm_qla2xxx_get_fabric_sense_len,
1747         .set_fabric_sense_len           = tcm_qla2xxx_set_fabric_sense_len,
1748         /*
1749          * Setup function pointers for generic logic in
1750          * target_core_fabric_configfs.c
1751          */
1752         .fabric_make_wwn                = tcm_qla2xxx_npiv_make_lport,
1753         .fabric_drop_wwn                = tcm_qla2xxx_npiv_drop_lport,
1754         .fabric_make_tpg                = tcm_qla2xxx_npiv_make_tpg,
1755         .fabric_drop_tpg                = tcm_qla2xxx_drop_tpg,
1756         .fabric_post_link               = NULL,
1757         .fabric_pre_unlink              = NULL,
1758         .fabric_make_np                 = NULL,
1759         .fabric_drop_np                 = NULL,
1760         .fabric_make_nodeacl            = tcm_qla2xxx_make_nodeacl,
1761         .fabric_drop_nodeacl            = tcm_qla2xxx_drop_nodeacl,
1762 };
1763
1764 static int tcm_qla2xxx_register_configfs(void)
1765 {
1766         struct target_fabric_configfs *fabric, *npiv_fabric;
1767         int ret;
1768
1769         pr_debug("TCM QLOGIC QLA2XXX fabric module %s on %s/%s on "
1770             UTS_RELEASE"\n", TCM_QLA2XXX_VERSION, utsname()->sysname,
1771             utsname()->machine);
1772         /*
1773          * Register the top level struct config_item_type with TCM core
1774          */
1775         fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx");
1776         if (IS_ERR(fabric)) {
1777                 pr_err("target_fabric_configfs_init() failed\n");
1778                 return PTR_ERR(fabric);
1779         }
1780         /*
1781          * Setup fabric->tf_ops from our local tcm_qla2xxx_ops
1782          */
1783         fabric->tf_ops = tcm_qla2xxx_ops;
1784         /*
1785          * Setup default attribute lists for various fabric->tf_cit_tmpl
1786          */
1787         TF_CIT_TMPL(fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1788         TF_CIT_TMPL(fabric)->tfc_tpg_base_cit.ct_attrs = tcm_qla2xxx_tpg_attrs;
1789         TF_CIT_TMPL(fabric)->tfc_tpg_attrib_cit.ct_attrs =
1790                                                 tcm_qla2xxx_tpg_attrib_attrs;
1791         TF_CIT_TMPL(fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1792         TF_CIT_TMPL(fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1793         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1794         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1795         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1796         TF_CIT_TMPL(fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1797         /*
1798          * Register the fabric for use within TCM
1799          */
1800         ret = target_fabric_configfs_register(fabric);
1801         if (ret < 0) {
1802                 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1803                 return ret;
1804         }
1805         /*
1806          * Setup our local pointer to *fabric
1807          */
1808         tcm_qla2xxx_fabric_configfs = fabric;
1809         pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_fabric_configfs\n");
1810
1811         /*
1812          * Register the top level struct config_item_type for NPIV with TCM core
1813          */
1814         npiv_fabric = target_fabric_configfs_init(THIS_MODULE, "qla2xxx_npiv");
1815         if (IS_ERR(npiv_fabric)) {
1816                 pr_err("target_fabric_configfs_init() failed\n");
1817                 ret = PTR_ERR(npiv_fabric);
1818                 goto out_fabric;
1819         }
1820         /*
1821          * Setup fabric->tf_ops from our local tcm_qla2xxx_npiv_ops
1822          */
1823         npiv_fabric->tf_ops = tcm_qla2xxx_npiv_ops;
1824         /*
1825          * Setup default attribute lists for various npiv_fabric->tf_cit_tmpl
1826          */
1827         TF_CIT_TMPL(npiv_fabric)->tfc_wwn_cit.ct_attrs = tcm_qla2xxx_wwn_attrs;
1828         TF_CIT_TMPL(npiv_fabric)->tfc_tpg_base_cit.ct_attrs = NULL;
1829         TF_CIT_TMPL(npiv_fabric)->tfc_tpg_attrib_cit.ct_attrs = NULL;
1830         TF_CIT_TMPL(npiv_fabric)->tfc_tpg_param_cit.ct_attrs = NULL;
1831         TF_CIT_TMPL(npiv_fabric)->tfc_tpg_np_base_cit.ct_attrs = NULL;
1832         TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_base_cit.ct_attrs = NULL;
1833         TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
1834         TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
1835         TF_CIT_TMPL(npiv_fabric)->tfc_tpg_nacl_param_cit.ct_attrs = NULL;
1836         /*
1837          * Register the npiv_fabric for use within TCM
1838          */
1839         ret = target_fabric_configfs_register(npiv_fabric);
1840         if (ret < 0) {
1841                 pr_err("target_fabric_configfs_register() failed for TCM_QLA2XXX\n");
1842                 goto out_fabric;
1843         }
1844         /*
1845          * Setup our local pointer to *npiv_fabric
1846          */
1847         tcm_qla2xxx_npiv_fabric_configfs = npiv_fabric;
1848         pr_debug("TCM_QLA2XXX[0] - Set fabric -> tcm_qla2xxx_npiv_fabric_configfs\n");
1849
1850         tcm_qla2xxx_free_wq = alloc_workqueue("tcm_qla2xxx_free",
1851                                                 WQ_MEM_RECLAIM, 0);
1852         if (!tcm_qla2xxx_free_wq) {
1853                 ret = -ENOMEM;
1854                 goto out_fabric_npiv;
1855         }
1856
1857         tcm_qla2xxx_cmd_wq = alloc_workqueue("tcm_qla2xxx_cmd", 0, 0);
1858         if (!tcm_qla2xxx_cmd_wq) {
1859                 ret = -ENOMEM;
1860                 goto out_free_wq;
1861         }
1862
1863         return 0;
1864
1865 out_free_wq:
1866         destroy_workqueue(tcm_qla2xxx_free_wq);
1867 out_fabric_npiv:
1868         target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1869 out_fabric:
1870         target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1871         return ret;
1872 }
1873
1874 static void tcm_qla2xxx_deregister_configfs(void)
1875 {
1876         destroy_workqueue(tcm_qla2xxx_cmd_wq);
1877         destroy_workqueue(tcm_qla2xxx_free_wq);
1878
1879         target_fabric_configfs_deregister(tcm_qla2xxx_fabric_configfs);
1880         tcm_qla2xxx_fabric_configfs = NULL;
1881         pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_fabric_configfs\n");
1882
1883         target_fabric_configfs_deregister(tcm_qla2xxx_npiv_fabric_configfs);
1884         tcm_qla2xxx_npiv_fabric_configfs = NULL;
1885         pr_debug("TCM_QLA2XXX[0] - Cleared tcm_qla2xxx_npiv_fabric_configfs\n");
1886 }
1887
1888 static int __init tcm_qla2xxx_init(void)
1889 {
1890         int ret;
1891
1892         ret = tcm_qla2xxx_register_configfs();
1893         if (ret < 0)
1894                 return ret;
1895
1896         return 0;
1897 }
1898
1899 static void __exit tcm_qla2xxx_exit(void)
1900 {
1901         tcm_qla2xxx_deregister_configfs();
1902 }
1903
1904 MODULE_DESCRIPTION("TCM QLA2XXX series NPIV enabled fabric driver");
1905 MODULE_LICENSE("GPL");
1906 module_init(tcm_qla2xxx_init);
1907 module_exit(tcm_qla2xxx_exit);