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