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