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