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