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