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