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