Debian Codegraph @

drivers/scsi/qla2xxx/tcm_qla2xxx.c

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