Loading...
1/*******************************************************************************
2 * Filename: target_core_alua.c
3 *
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
5 *
6 * Copyright (c) 2009-2010 Rising Tide Systems
7 * Copyright (c) 2009-2010 Linux-iSCSI.org
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 ******************************************************************************/
26
27#include <linux/version.h>
28#include <linux/slab.h>
29#include <linux/spinlock.h>
30#include <linux/configfs.h>
31#include <scsi/scsi.h>
32#include <scsi/scsi_cmnd.h>
33
34#include <target/target_core_base.h>
35#include <target/target_core_device.h>
36#include <target/target_core_transport.h>
37#include <target/target_core_fabric_ops.h>
38#include <target/target_core_configfs.h>
39
40#include "target_core_alua.h"
41#include "target_core_hba.h"
42#include "target_core_ua.h"
43
44static int core_alua_check_transition(int state, int *primary);
45static int core_alua_set_tg_pt_secondary_state(
46 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
47 struct se_port *port, int explict, int offline);
48
49static u16 alua_lu_gps_counter;
50static u32 alua_lu_gps_count;
51
52static DEFINE_SPINLOCK(lu_gps_lock);
53static LIST_HEAD(lu_gps_list);
54
55struct t10_alua_lu_gp *default_lu_gp;
56
57/*
58 * REPORT_TARGET_PORT_GROUPS
59 *
60 * See spc4r17 section 6.27
61 */
62int core_emulate_report_target_port_groups(struct se_cmd *cmd)
63{
64 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
65 struct se_port *port;
66 struct t10_alua_tg_pt_gp *tg_pt_gp;
67 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
68 unsigned char *buf;
69 u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
70 Target port group descriptor */
71
72 buf = transport_kmap_first_data_page(cmd);
73
74 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
75 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
76 tg_pt_gp_list) {
77 /*
78 * PREF: Preferred target port bit, determine if this
79 * bit should be set for port group.
80 */
81 if (tg_pt_gp->tg_pt_gp_pref)
82 buf[off] = 0x80;
83 /*
84 * Set the ASYMMETRIC ACCESS State
85 */
86 buf[off++] |= (atomic_read(
87 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
88 /*
89 * Set supported ASYMMETRIC ACCESS State bits
90 */
91 buf[off] = 0x80; /* T_SUP */
92 buf[off] |= 0x40; /* O_SUP */
93 buf[off] |= 0x8; /* U_SUP */
94 buf[off] |= 0x4; /* S_SUP */
95 buf[off] |= 0x2; /* AN_SUP */
96 buf[off++] |= 0x1; /* AO_SUP */
97 /*
98 * TARGET PORT GROUP
99 */
100 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
101 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
102
103 off++; /* Skip over Reserved */
104 /*
105 * STATUS CODE
106 */
107 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
108 /*
109 * Vendor Specific field
110 */
111 buf[off++] = 0x00;
112 /*
113 * TARGET PORT COUNT
114 */
115 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
116 rd_len += 8;
117
118 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
119 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
120 tg_pt_gp_mem_list) {
121 port = tg_pt_gp_mem->tg_pt;
122 /*
123 * Start Target Port descriptor format
124 *
125 * See spc4r17 section 6.2.7 Table 247
126 */
127 off += 2; /* Skip over Obsolete */
128 /*
129 * Set RELATIVE TARGET PORT IDENTIFIER
130 */
131 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
132 buf[off++] = (port->sep_rtpi & 0xff);
133 rd_len += 4;
134 }
135 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
136 }
137 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
138 /*
139 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
140 */
141 buf[0] = ((rd_len >> 24) & 0xff);
142 buf[1] = ((rd_len >> 16) & 0xff);
143 buf[2] = ((rd_len >> 8) & 0xff);
144 buf[3] = (rd_len & 0xff);
145
146 transport_kunmap_first_data_page(cmd);
147
148 return 0;
149}
150
151/*
152 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
153 *
154 * See spc4r17 section 6.35
155 */
156int core_emulate_set_target_port_groups(struct se_cmd *cmd)
157{
158 struct se_device *dev = cmd->se_dev;
159 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
160 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
161 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
162 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
163 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
164 unsigned char *buf;
165 unsigned char *ptr;
166 u32 len = 4; /* Skip over RESERVED area in header */
167 int alua_access_state, primary = 0, rc;
168 u16 tg_pt_id, rtpi;
169
170 if (!l_port)
171 return PYX_TRANSPORT_LU_COMM_FAILURE;
172
173 buf = transport_kmap_first_data_page(cmd);
174
175 /*
176 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
177 * for the local tg_pt_gp.
178 */
179 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
180 if (!l_tg_pt_gp_mem) {
181 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
182 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
183 goto out;
184 }
185 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
186 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
187 if (!l_tg_pt_gp) {
188 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
189 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
190 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
191 goto out;
192 }
193 rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
194 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
195
196 if (!rc) {
197 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
198 " while TPGS_EXPLICT_ALUA is disabled\n");
199 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
200 goto out;
201 }
202
203 ptr = &buf[4]; /* Skip over RESERVED area in header */
204
205 while (len < cmd->data_length) {
206 alua_access_state = (ptr[0] & 0x0f);
207 /*
208 * Check the received ALUA access state, and determine if
209 * the state is a primary or secondary target port asymmetric
210 * access state.
211 */
212 rc = core_alua_check_transition(alua_access_state, &primary);
213 if (rc != 0) {
214 /*
215 * If the SET TARGET PORT GROUPS attempts to establish
216 * an invalid combination of target port asymmetric
217 * access states or attempts to establish an
218 * unsupported target port asymmetric access state,
219 * then the command shall be terminated with CHECK
220 * CONDITION status, with the sense key set to ILLEGAL
221 * REQUEST, and the additional sense code set to INVALID
222 * FIELD IN PARAMETER LIST.
223 */
224 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
225 goto out;
226 }
227 rc = -1;
228 /*
229 * If the ASYMMETRIC ACCESS STATE field (see table 267)
230 * specifies a primary target port asymmetric access state,
231 * then the TARGET PORT GROUP OR TARGET PORT field specifies
232 * a primary target port group for which the primary target
233 * port asymmetric access state shall be changed. If the
234 * ASYMMETRIC ACCESS STATE field specifies a secondary target
235 * port asymmetric access state, then the TARGET PORT GROUP OR
236 * TARGET PORT field specifies the relative target port
237 * identifier (see 3.1.120) of the target port for which the
238 * secondary target port asymmetric access state shall be
239 * changed.
240 */
241 if (primary) {
242 tg_pt_id = ((ptr[2] << 8) & 0xff);
243 tg_pt_id |= (ptr[3] & 0xff);
244 /*
245 * Locate the matching target port group ID from
246 * the global tg_pt_gp list
247 */
248 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
249 list_for_each_entry(tg_pt_gp,
250 &su_dev->t10_alua.tg_pt_gps_list,
251 tg_pt_gp_list) {
252 if (!tg_pt_gp->tg_pt_gp_valid_id)
253 continue;
254
255 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
256 continue;
257
258 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
259 smp_mb__after_atomic_inc();
260 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
261
262 rc = core_alua_do_port_transition(tg_pt_gp,
263 dev, l_port, nacl,
264 alua_access_state, 1);
265
266 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
267 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
268 smp_mb__after_atomic_dec();
269 break;
270 }
271 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
272 /*
273 * If not matching target port group ID can be located
274 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
275 */
276 if (rc != 0) {
277 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
278 goto out;
279 }
280 } else {
281 /*
282 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
283 * the Target Port in question for the the incoming
284 * SET_TARGET_PORT_GROUPS op.
285 */
286 rtpi = ((ptr[2] << 8) & 0xff);
287 rtpi |= (ptr[3] & 0xff);
288 /*
289 * Locate the matching relative target port identifer
290 * for the struct se_device storage object.
291 */
292 spin_lock(&dev->se_port_lock);
293 list_for_each_entry(port, &dev->dev_sep_list,
294 sep_list) {
295 if (port->sep_rtpi != rtpi)
296 continue;
297
298 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
299 spin_unlock(&dev->se_port_lock);
300
301 rc = core_alua_set_tg_pt_secondary_state(
302 tg_pt_gp_mem, port, 1, 1);
303
304 spin_lock(&dev->se_port_lock);
305 break;
306 }
307 spin_unlock(&dev->se_port_lock);
308 /*
309 * If not matching relative target port identifier can
310 * be located, throw an exception with ASCQ:
311 * INVALID_PARAMETER_LIST
312 */
313 if (rc != 0) {
314 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
315 goto out;
316 }
317 }
318
319 ptr += 4;
320 len += 4;
321 }
322
323out:
324 transport_kunmap_first_data_page(cmd);
325
326 return 0;
327}
328
329static inline int core_alua_state_nonoptimized(
330 struct se_cmd *cmd,
331 unsigned char *cdb,
332 int nonop_delay_msecs,
333 u8 *alua_ascq)
334{
335 /*
336 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
337 * later to determine if processing of this cmd needs to be
338 * temporarily delayed for the Active/NonOptimized primary access state.
339 */
340 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
341 cmd->alua_nonop_delay = nonop_delay_msecs;
342 return 0;
343}
344
345static inline int core_alua_state_standby(
346 struct se_cmd *cmd,
347 unsigned char *cdb,
348 u8 *alua_ascq)
349{
350 /*
351 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
352 * spc4r17 section 5.9.2.4.4
353 */
354 switch (cdb[0]) {
355 case INQUIRY:
356 case LOG_SELECT:
357 case LOG_SENSE:
358 case MODE_SELECT:
359 case MODE_SENSE:
360 case REPORT_LUNS:
361 case RECEIVE_DIAGNOSTIC:
362 case SEND_DIAGNOSTIC:
363 case MAINTENANCE_IN:
364 switch (cdb[1]) {
365 case MI_REPORT_TARGET_PGS:
366 return 0;
367 default:
368 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
369 return 1;
370 }
371 case MAINTENANCE_OUT:
372 switch (cdb[1]) {
373 case MO_SET_TARGET_PGS:
374 return 0;
375 default:
376 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
377 return 1;
378 }
379 case REQUEST_SENSE:
380 case PERSISTENT_RESERVE_IN:
381 case PERSISTENT_RESERVE_OUT:
382 case READ_BUFFER:
383 case WRITE_BUFFER:
384 return 0;
385 default:
386 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
387 return 1;
388 }
389
390 return 0;
391}
392
393static inline int core_alua_state_unavailable(
394 struct se_cmd *cmd,
395 unsigned char *cdb,
396 u8 *alua_ascq)
397{
398 /*
399 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
400 * spc4r17 section 5.9.2.4.5
401 */
402 switch (cdb[0]) {
403 case INQUIRY:
404 case REPORT_LUNS:
405 case MAINTENANCE_IN:
406 switch (cdb[1]) {
407 case MI_REPORT_TARGET_PGS:
408 return 0;
409 default:
410 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
411 return 1;
412 }
413 case MAINTENANCE_OUT:
414 switch (cdb[1]) {
415 case MO_SET_TARGET_PGS:
416 return 0;
417 default:
418 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
419 return 1;
420 }
421 case REQUEST_SENSE:
422 case READ_BUFFER:
423 case WRITE_BUFFER:
424 return 0;
425 default:
426 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
427 return 1;
428 }
429
430 return 0;
431}
432
433static inline int core_alua_state_transition(
434 struct se_cmd *cmd,
435 unsigned char *cdb,
436 u8 *alua_ascq)
437{
438 /*
439 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
440 * spc4r17 section 5.9.2.5
441 */
442 switch (cdb[0]) {
443 case INQUIRY:
444 case REPORT_LUNS:
445 case MAINTENANCE_IN:
446 switch (cdb[1]) {
447 case MI_REPORT_TARGET_PGS:
448 return 0;
449 default:
450 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
451 return 1;
452 }
453 case REQUEST_SENSE:
454 case READ_BUFFER:
455 case WRITE_BUFFER:
456 return 0;
457 default:
458 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
459 return 1;
460 }
461
462 return 0;
463}
464
465/*
466 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
467 * in transport_cmd_sequencer(). This function is assigned to
468 * struct t10_alua *->state_check() in core_setup_alua()
469 */
470static int core_alua_state_check_nop(
471 struct se_cmd *cmd,
472 unsigned char *cdb,
473 u8 *alua_ascq)
474{
475 return 0;
476}
477
478/*
479 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
480 * This function is assigned to struct t10_alua *->state_check() in
481 * core_setup_alua()
482 *
483 * Also, this function can return three different return codes to
484 * signal transport_generic_cmd_sequencer()
485 *
486 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
487 * return 0: Used to signal success
488 * reutrn -1: Used to signal failure, and invalid cdb field
489 */
490static int core_alua_state_check(
491 struct se_cmd *cmd,
492 unsigned char *cdb,
493 u8 *alua_ascq)
494{
495 struct se_lun *lun = cmd->se_lun;
496 struct se_port *port = lun->lun_sep;
497 struct t10_alua_tg_pt_gp *tg_pt_gp;
498 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
499 int out_alua_state, nonop_delay_msecs;
500
501 if (!port)
502 return 0;
503 /*
504 * First, check for a struct se_port specific secondary ALUA target port
505 * access state: OFFLINE
506 */
507 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
508 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
509 pr_debug("ALUA: Got secondary offline status for local"
510 " target port\n");
511 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
512 return 1;
513 }
514 /*
515 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
516 * ALUA target port group, to obtain current ALUA access state.
517 * Otherwise look for the underlying struct se_device association with
518 * a ALUA logical unit group.
519 */
520 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
521 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
522 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
523 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
524 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
525 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
526 /*
527 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
528 * statement so the compiler knows explicitly to check this case first.
529 * For the Optimized ALUA access state case, we want to process the
530 * incoming fabric cmd ASAP..
531 */
532 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
533 return 0;
534
535 switch (out_alua_state) {
536 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
537 return core_alua_state_nonoptimized(cmd, cdb,
538 nonop_delay_msecs, alua_ascq);
539 case ALUA_ACCESS_STATE_STANDBY:
540 return core_alua_state_standby(cmd, cdb, alua_ascq);
541 case ALUA_ACCESS_STATE_UNAVAILABLE:
542 return core_alua_state_unavailable(cmd, cdb, alua_ascq);
543 case ALUA_ACCESS_STATE_TRANSITION:
544 return core_alua_state_transition(cmd, cdb, alua_ascq);
545 /*
546 * OFFLINE is a secondary ALUA target port group access state, that is
547 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
548 */
549 case ALUA_ACCESS_STATE_OFFLINE:
550 default:
551 pr_err("Unknown ALUA access state: 0x%02x\n",
552 out_alua_state);
553 return -EINVAL;
554 }
555
556 return 0;
557}
558
559/*
560 * Check implict and explict ALUA state change request.
561 */
562static int core_alua_check_transition(int state, int *primary)
563{
564 switch (state) {
565 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
566 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
567 case ALUA_ACCESS_STATE_STANDBY:
568 case ALUA_ACCESS_STATE_UNAVAILABLE:
569 /*
570 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
571 * defined as primary target port asymmetric access states.
572 */
573 *primary = 1;
574 break;
575 case ALUA_ACCESS_STATE_OFFLINE:
576 /*
577 * OFFLINE state is defined as a secondary target port
578 * asymmetric access state.
579 */
580 *primary = 0;
581 break;
582 default:
583 pr_err("Unknown ALUA access state: 0x%02x\n", state);
584 return -EINVAL;
585 }
586
587 return 0;
588}
589
590static char *core_alua_dump_state(int state)
591{
592 switch (state) {
593 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
594 return "Active/Optimized";
595 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
596 return "Active/NonOptimized";
597 case ALUA_ACCESS_STATE_STANDBY:
598 return "Standby";
599 case ALUA_ACCESS_STATE_UNAVAILABLE:
600 return "Unavailable";
601 case ALUA_ACCESS_STATE_OFFLINE:
602 return "Offline";
603 default:
604 return "Unknown";
605 }
606
607 return NULL;
608}
609
610char *core_alua_dump_status(int status)
611{
612 switch (status) {
613 case ALUA_STATUS_NONE:
614 return "None";
615 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
616 return "Altered by Explict STPG";
617 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
618 return "Altered by Implict ALUA";
619 default:
620 return "Unknown";
621 }
622
623 return NULL;
624}
625
626/*
627 * Used by fabric modules to determine when we need to delay processing
628 * for the Active/NonOptimized paths..
629 */
630int core_alua_check_nonop_delay(
631 struct se_cmd *cmd)
632{
633 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
634 return 0;
635 if (in_interrupt())
636 return 0;
637 /*
638 * The ALUA Active/NonOptimized access state delay can be disabled
639 * in via configfs with a value of zero
640 */
641 if (!cmd->alua_nonop_delay)
642 return 0;
643 /*
644 * struct se_cmd->alua_nonop_delay gets set by a target port group
645 * defined interval in core_alua_state_nonoptimized()
646 */
647 msleep_interruptible(cmd->alua_nonop_delay);
648 return 0;
649}
650EXPORT_SYMBOL(core_alua_check_nonop_delay);
651
652/*
653 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
654 *
655 */
656static int core_alua_write_tpg_metadata(
657 const char *path,
658 unsigned char *md_buf,
659 u32 md_buf_len)
660{
661 mm_segment_t old_fs;
662 struct file *file;
663 struct iovec iov[1];
664 int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
665
666 memset(iov, 0, sizeof(struct iovec));
667
668 file = filp_open(path, flags, 0600);
669 if (IS_ERR(file) || !file || !file->f_dentry) {
670 pr_err("filp_open(%s) for ALUA metadata failed\n",
671 path);
672 return -ENODEV;
673 }
674
675 iov[0].iov_base = &md_buf[0];
676 iov[0].iov_len = md_buf_len;
677
678 old_fs = get_fs();
679 set_fs(get_ds());
680 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
681 set_fs(old_fs);
682
683 if (ret < 0) {
684 pr_err("Error writing ALUA metadata file: %s\n", path);
685 filp_close(file, NULL);
686 return -EIO;
687 }
688 filp_close(file, NULL);
689
690 return 0;
691}
692
693/*
694 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
695 */
696static int core_alua_update_tpg_primary_metadata(
697 struct t10_alua_tg_pt_gp *tg_pt_gp,
698 int primary_state,
699 unsigned char *md_buf)
700{
701 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
702 struct t10_wwn *wwn = &su_dev->t10_wwn;
703 char path[ALUA_METADATA_PATH_LEN];
704 int len;
705
706 memset(path, 0, ALUA_METADATA_PATH_LEN);
707
708 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
709 "tg_pt_gp_id=%hu\n"
710 "alua_access_state=0x%02x\n"
711 "alua_access_status=0x%02x\n",
712 tg_pt_gp->tg_pt_gp_id, primary_state,
713 tg_pt_gp->tg_pt_gp_alua_access_status);
714
715 snprintf(path, ALUA_METADATA_PATH_LEN,
716 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
717 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
718
719 return core_alua_write_tpg_metadata(path, md_buf, len);
720}
721
722static int core_alua_do_transition_tg_pt(
723 struct t10_alua_tg_pt_gp *tg_pt_gp,
724 struct se_port *l_port,
725 struct se_node_acl *nacl,
726 unsigned char *md_buf,
727 int new_state,
728 int explict)
729{
730 struct se_dev_entry *se_deve;
731 struct se_lun_acl *lacl;
732 struct se_port *port;
733 struct t10_alua_tg_pt_gp_member *mem;
734 int old_state = 0;
735 /*
736 * Save the old primary ALUA access state, and set the current state
737 * to ALUA_ACCESS_STATE_TRANSITION.
738 */
739 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
740 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
741 ALUA_ACCESS_STATE_TRANSITION);
742 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
743 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
744 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
745 /*
746 * Check for the optional ALUA primary state transition delay
747 */
748 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
749 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
750
751 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
752 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
753 tg_pt_gp_mem_list) {
754 port = mem->tg_pt;
755 /*
756 * After an implicit target port asymmetric access state
757 * change, a device server shall establish a unit attention
758 * condition for the initiator port associated with every I_T
759 * nexus with the additional sense code set to ASYMMETRIC
760 * ACCESS STATE CHAGED.
761 *
762 * After an explicit target port asymmetric access state
763 * change, a device server shall establish a unit attention
764 * condition with the additional sense code set to ASYMMETRIC
765 * ACCESS STATE CHANGED for the initiator port associated with
766 * every I_T nexus other than the I_T nexus on which the SET
767 * TARGET PORT GROUPS command
768 */
769 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
770 smp_mb__after_atomic_inc();
771 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
772
773 spin_lock_bh(&port->sep_alua_lock);
774 list_for_each_entry(se_deve, &port->sep_alua_list,
775 alua_port_list) {
776 lacl = se_deve->se_lun_acl;
777 /*
778 * se_deve->se_lun_acl pointer may be NULL for a
779 * entry created without explict Node+MappedLUN ACLs
780 */
781 if (!lacl)
782 continue;
783
784 if (explict &&
785 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
786 (l_port != NULL) && (l_port == port))
787 continue;
788
789 core_scsi3_ua_allocate(lacl->se_lun_nacl,
790 se_deve->mapped_lun, 0x2A,
791 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
792 }
793 spin_unlock_bh(&port->sep_alua_lock);
794
795 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
796 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
797 smp_mb__after_atomic_dec();
798 }
799 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
800 /*
801 * Update the ALUA metadata buf that has been allocated in
802 * core_alua_do_port_transition(), this metadata will be written
803 * to struct file.
804 *
805 * Note that there is the case where we do not want to update the
806 * metadata when the saved metadata is being parsed in userspace
807 * when setting the existing port access state and access status.
808 *
809 * Also note that the failure to write out the ALUA metadata to
810 * struct file does NOT affect the actual ALUA transition.
811 */
812 if (tg_pt_gp->tg_pt_gp_write_metadata) {
813 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
814 core_alua_update_tpg_primary_metadata(tg_pt_gp,
815 new_state, md_buf);
816 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
817 }
818 /*
819 * Set the current primary ALUA access state to the requested new state
820 */
821 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
822
823 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
824 " from primary access state %s to %s\n", (explict) ? "explict" :
825 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
826 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
827 core_alua_dump_state(new_state));
828
829 return 0;
830}
831
832int core_alua_do_port_transition(
833 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
834 struct se_device *l_dev,
835 struct se_port *l_port,
836 struct se_node_acl *l_nacl,
837 int new_state,
838 int explict)
839{
840 struct se_device *dev;
841 struct se_port *port;
842 struct se_subsystem_dev *su_dev;
843 struct se_node_acl *nacl;
844 struct t10_alua_lu_gp *lu_gp;
845 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
846 struct t10_alua_tg_pt_gp *tg_pt_gp;
847 unsigned char *md_buf;
848 int primary;
849
850 if (core_alua_check_transition(new_state, &primary) != 0)
851 return -EINVAL;
852
853 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
854 if (!md_buf) {
855 pr_err("Unable to allocate buf for ALUA metadata\n");
856 return -ENOMEM;
857 }
858
859 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
860 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
861 lu_gp = local_lu_gp_mem->lu_gp;
862 atomic_inc(&lu_gp->lu_gp_ref_cnt);
863 smp_mb__after_atomic_inc();
864 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
865 /*
866 * For storage objects that are members of the 'default_lu_gp',
867 * we only do transition on the passed *l_tp_pt_gp, and not
868 * on all of the matching target port groups IDs in default_lu_gp.
869 */
870 if (!lu_gp->lu_gp_id) {
871 /*
872 * core_alua_do_transition_tg_pt() will always return
873 * success.
874 */
875 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
876 md_buf, new_state, explict);
877 atomic_dec(&lu_gp->lu_gp_ref_cnt);
878 smp_mb__after_atomic_dec();
879 kfree(md_buf);
880 return 0;
881 }
882 /*
883 * For all other LU groups aside from 'default_lu_gp', walk all of
884 * the associated storage objects looking for a matching target port
885 * group ID from the local target port group.
886 */
887 spin_lock(&lu_gp->lu_gp_lock);
888 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
889 lu_gp_mem_list) {
890
891 dev = lu_gp_mem->lu_gp_mem_dev;
892 su_dev = dev->se_sub_dev;
893 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
894 smp_mb__after_atomic_inc();
895 spin_unlock(&lu_gp->lu_gp_lock);
896
897 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
898 list_for_each_entry(tg_pt_gp,
899 &su_dev->t10_alua.tg_pt_gps_list,
900 tg_pt_gp_list) {
901
902 if (!tg_pt_gp->tg_pt_gp_valid_id)
903 continue;
904 /*
905 * If the target behavior port asymmetric access state
906 * is changed for any target port group accessiable via
907 * a logical unit within a LU group, the target port
908 * behavior group asymmetric access states for the same
909 * target port group accessible via other logical units
910 * in that LU group will also change.
911 */
912 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
913 continue;
914
915 if (l_tg_pt_gp == tg_pt_gp) {
916 port = l_port;
917 nacl = l_nacl;
918 } else {
919 port = NULL;
920 nacl = NULL;
921 }
922 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
923 smp_mb__after_atomic_inc();
924 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
925 /*
926 * core_alua_do_transition_tg_pt() will always return
927 * success.
928 */
929 core_alua_do_transition_tg_pt(tg_pt_gp, port,
930 nacl, md_buf, new_state, explict);
931
932 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
933 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
934 smp_mb__after_atomic_dec();
935 }
936 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
937
938 spin_lock(&lu_gp->lu_gp_lock);
939 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
940 smp_mb__after_atomic_dec();
941 }
942 spin_unlock(&lu_gp->lu_gp_lock);
943
944 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
945 " Group IDs: %hu %s transition to primary state: %s\n",
946 config_item_name(&lu_gp->lu_gp_group.cg_item),
947 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
948 core_alua_dump_state(new_state));
949
950 atomic_dec(&lu_gp->lu_gp_ref_cnt);
951 smp_mb__after_atomic_dec();
952 kfree(md_buf);
953 return 0;
954}
955
956/*
957 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
958 */
959static int core_alua_update_tpg_secondary_metadata(
960 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
961 struct se_port *port,
962 unsigned char *md_buf,
963 u32 md_buf_len)
964{
965 struct se_portal_group *se_tpg = port->sep_tpg;
966 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
967 int len;
968
969 memset(path, 0, ALUA_METADATA_PATH_LEN);
970 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
971
972 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
973 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
974
975 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
976 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
977 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
978
979 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
980 "alua_tg_pt_status=0x%02x\n",
981 atomic_read(&port->sep_tg_pt_secondary_offline),
982 port->sep_tg_pt_secondary_stat);
983
984 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
985 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
986 port->sep_lun->unpacked_lun);
987
988 return core_alua_write_tpg_metadata(path, md_buf, len);
989}
990
991static int core_alua_set_tg_pt_secondary_state(
992 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
993 struct se_port *port,
994 int explict,
995 int offline)
996{
997 struct t10_alua_tg_pt_gp *tg_pt_gp;
998 unsigned char *md_buf;
999 u32 md_buf_len;
1000 int trans_delay_msecs;
1001
1002 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1003 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1004 if (!tg_pt_gp) {
1005 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1006 pr_err("Unable to complete secondary state"
1007 " transition\n");
1008 return -EINVAL;
1009 }
1010 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1011 /*
1012 * Set the secondary ALUA target port access state to OFFLINE
1013 * or release the previously secondary state for struct se_port
1014 */
1015 if (offline)
1016 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1017 else
1018 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1019
1020 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1021 port->sep_tg_pt_secondary_stat = (explict) ?
1022 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1023 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1024
1025 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1026 " to secondary access state: %s\n", (explict) ? "explict" :
1027 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1028 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1029
1030 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1031 /*
1032 * Do the optional transition delay after we set the secondary
1033 * ALUA access state.
1034 */
1035 if (trans_delay_msecs != 0)
1036 msleep_interruptible(trans_delay_msecs);
1037 /*
1038 * See if we need to update the ALUA fabric port metadata for
1039 * secondary state and status
1040 */
1041 if (port->sep_tg_pt_secondary_write_md) {
1042 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1043 if (!md_buf) {
1044 pr_err("Unable to allocate md_buf for"
1045 " secondary ALUA access metadata\n");
1046 return -ENOMEM;
1047 }
1048 mutex_lock(&port->sep_tg_pt_md_mutex);
1049 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1050 md_buf, md_buf_len);
1051 mutex_unlock(&port->sep_tg_pt_md_mutex);
1052
1053 kfree(md_buf);
1054 }
1055
1056 return 0;
1057}
1058
1059struct t10_alua_lu_gp *
1060core_alua_allocate_lu_gp(const char *name, int def_group)
1061{
1062 struct t10_alua_lu_gp *lu_gp;
1063
1064 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1065 if (!lu_gp) {
1066 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1067 return ERR_PTR(-ENOMEM);
1068 }
1069 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1070 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1071 spin_lock_init(&lu_gp->lu_gp_lock);
1072 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1073
1074 if (def_group) {
1075 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1076 lu_gp->lu_gp_valid_id = 1;
1077 alua_lu_gps_count++;
1078 }
1079
1080 return lu_gp;
1081}
1082
1083int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1084{
1085 struct t10_alua_lu_gp *lu_gp_tmp;
1086 u16 lu_gp_id_tmp;
1087 /*
1088 * The lu_gp->lu_gp_id may only be set once..
1089 */
1090 if (lu_gp->lu_gp_valid_id) {
1091 pr_warn("ALUA LU Group already has a valid ID,"
1092 " ignoring request\n");
1093 return -EINVAL;
1094 }
1095
1096 spin_lock(&lu_gps_lock);
1097 if (alua_lu_gps_count == 0x0000ffff) {
1098 pr_err("Maximum ALUA alua_lu_gps_count:"
1099 " 0x0000ffff reached\n");
1100 spin_unlock(&lu_gps_lock);
1101 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1102 return -ENOSPC;
1103 }
1104again:
1105 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1106 alua_lu_gps_counter++;
1107
1108 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1109 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1110 if (!lu_gp_id)
1111 goto again;
1112
1113 pr_warn("ALUA Logical Unit Group ID: %hu"
1114 " already exists, ignoring request\n",
1115 lu_gp_id);
1116 spin_unlock(&lu_gps_lock);
1117 return -EINVAL;
1118 }
1119 }
1120
1121 lu_gp->lu_gp_id = lu_gp_id_tmp;
1122 lu_gp->lu_gp_valid_id = 1;
1123 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1124 alua_lu_gps_count++;
1125 spin_unlock(&lu_gps_lock);
1126
1127 return 0;
1128}
1129
1130static struct t10_alua_lu_gp_member *
1131core_alua_allocate_lu_gp_mem(struct se_device *dev)
1132{
1133 struct t10_alua_lu_gp_member *lu_gp_mem;
1134
1135 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1136 if (!lu_gp_mem) {
1137 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1138 return ERR_PTR(-ENOMEM);
1139 }
1140 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1141 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1142 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1143
1144 lu_gp_mem->lu_gp_mem_dev = dev;
1145 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1146
1147 return lu_gp_mem;
1148}
1149
1150void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1151{
1152 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1153 /*
1154 * Once we have reached this point, config_item_put() has
1155 * already been called from target_core_alua_drop_lu_gp().
1156 *
1157 * Here, we remove the *lu_gp from the global list so that
1158 * no associations can be made while we are releasing
1159 * struct t10_alua_lu_gp.
1160 */
1161 spin_lock(&lu_gps_lock);
1162 atomic_set(&lu_gp->lu_gp_shutdown, 1);
1163 list_del(&lu_gp->lu_gp_node);
1164 alua_lu_gps_count--;
1165 spin_unlock(&lu_gps_lock);
1166 /*
1167 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1168 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1169 * released with core_alua_put_lu_gp_from_name()
1170 */
1171 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1172 cpu_relax();
1173 /*
1174 * Release reference to struct t10_alua_lu_gp * from all associated
1175 * struct se_device.
1176 */
1177 spin_lock(&lu_gp->lu_gp_lock);
1178 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1179 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1180 if (lu_gp_mem->lu_gp_assoc) {
1181 list_del(&lu_gp_mem->lu_gp_mem_list);
1182 lu_gp->lu_gp_members--;
1183 lu_gp_mem->lu_gp_assoc = 0;
1184 }
1185 spin_unlock(&lu_gp->lu_gp_lock);
1186 /*
1187 *
1188 * lu_gp_mem is associated with a single
1189 * struct se_device->dev_alua_lu_gp_mem, and is released when
1190 * struct se_device is released via core_alua_free_lu_gp_mem().
1191 *
1192 * If the passed lu_gp does NOT match the default_lu_gp, assume
1193 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1194 */
1195 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1196 if (lu_gp != default_lu_gp)
1197 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1198 default_lu_gp);
1199 else
1200 lu_gp_mem->lu_gp = NULL;
1201 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1202
1203 spin_lock(&lu_gp->lu_gp_lock);
1204 }
1205 spin_unlock(&lu_gp->lu_gp_lock);
1206
1207 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1208}
1209
1210void core_alua_free_lu_gp_mem(struct se_device *dev)
1211{
1212 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1213 struct t10_alua *alua = &su_dev->t10_alua;
1214 struct t10_alua_lu_gp *lu_gp;
1215 struct t10_alua_lu_gp_member *lu_gp_mem;
1216
1217 if (alua->alua_type != SPC3_ALUA_EMULATED)
1218 return;
1219
1220 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1221 if (!lu_gp_mem)
1222 return;
1223
1224 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1225 cpu_relax();
1226
1227 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1228 lu_gp = lu_gp_mem->lu_gp;
1229 if (lu_gp) {
1230 spin_lock(&lu_gp->lu_gp_lock);
1231 if (lu_gp_mem->lu_gp_assoc) {
1232 list_del(&lu_gp_mem->lu_gp_mem_list);
1233 lu_gp->lu_gp_members--;
1234 lu_gp_mem->lu_gp_assoc = 0;
1235 }
1236 spin_unlock(&lu_gp->lu_gp_lock);
1237 lu_gp_mem->lu_gp = NULL;
1238 }
1239 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1240
1241 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1242}
1243
1244struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1245{
1246 struct t10_alua_lu_gp *lu_gp;
1247 struct config_item *ci;
1248
1249 spin_lock(&lu_gps_lock);
1250 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1251 if (!lu_gp->lu_gp_valid_id)
1252 continue;
1253 ci = &lu_gp->lu_gp_group.cg_item;
1254 if (!strcmp(config_item_name(ci), name)) {
1255 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1256 spin_unlock(&lu_gps_lock);
1257 return lu_gp;
1258 }
1259 }
1260 spin_unlock(&lu_gps_lock);
1261
1262 return NULL;
1263}
1264
1265void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1266{
1267 spin_lock(&lu_gps_lock);
1268 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1269 spin_unlock(&lu_gps_lock);
1270}
1271
1272/*
1273 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1274 */
1275void __core_alua_attach_lu_gp_mem(
1276 struct t10_alua_lu_gp_member *lu_gp_mem,
1277 struct t10_alua_lu_gp *lu_gp)
1278{
1279 spin_lock(&lu_gp->lu_gp_lock);
1280 lu_gp_mem->lu_gp = lu_gp;
1281 lu_gp_mem->lu_gp_assoc = 1;
1282 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1283 lu_gp->lu_gp_members++;
1284 spin_unlock(&lu_gp->lu_gp_lock);
1285}
1286
1287/*
1288 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1289 */
1290void __core_alua_drop_lu_gp_mem(
1291 struct t10_alua_lu_gp_member *lu_gp_mem,
1292 struct t10_alua_lu_gp *lu_gp)
1293{
1294 spin_lock(&lu_gp->lu_gp_lock);
1295 list_del(&lu_gp_mem->lu_gp_mem_list);
1296 lu_gp_mem->lu_gp = NULL;
1297 lu_gp_mem->lu_gp_assoc = 0;
1298 lu_gp->lu_gp_members--;
1299 spin_unlock(&lu_gp->lu_gp_lock);
1300}
1301
1302struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
1303 struct se_subsystem_dev *su_dev,
1304 const char *name,
1305 int def_group)
1306{
1307 struct t10_alua_tg_pt_gp *tg_pt_gp;
1308
1309 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1310 if (!tg_pt_gp) {
1311 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1312 return NULL;
1313 }
1314 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1315 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1316 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1317 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1318 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1319 tg_pt_gp->tg_pt_gp_su_dev = su_dev;
1320 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1321 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1322 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1323 /*
1324 * Enable both explict and implict ALUA support by default
1325 */
1326 tg_pt_gp->tg_pt_gp_alua_access_type =
1327 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1328 /*
1329 * Set the default Active/NonOptimized Delay in milliseconds
1330 */
1331 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1332 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1333
1334 if (def_group) {
1335 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1336 tg_pt_gp->tg_pt_gp_id =
1337 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1338 tg_pt_gp->tg_pt_gp_valid_id = 1;
1339 su_dev->t10_alua.alua_tg_pt_gps_count++;
1340 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1341 &su_dev->t10_alua.tg_pt_gps_list);
1342 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1343 }
1344
1345 return tg_pt_gp;
1346}
1347
1348int core_alua_set_tg_pt_gp_id(
1349 struct t10_alua_tg_pt_gp *tg_pt_gp,
1350 u16 tg_pt_gp_id)
1351{
1352 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1353 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1354 u16 tg_pt_gp_id_tmp;
1355 /*
1356 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1357 */
1358 if (tg_pt_gp->tg_pt_gp_valid_id) {
1359 pr_warn("ALUA TG PT Group already has a valid ID,"
1360 " ignoring request\n");
1361 return -EINVAL;
1362 }
1363
1364 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1365 if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1366 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1367 " 0x0000ffff reached\n");
1368 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1369 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1370 return -ENOSPC;
1371 }
1372again:
1373 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1374 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1375
1376 list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
1377 tg_pt_gp_list) {
1378 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1379 if (!tg_pt_gp_id)
1380 goto again;
1381
1382 pr_err("ALUA Target Port Group ID: %hu already"
1383 " exists, ignoring request\n", tg_pt_gp_id);
1384 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1385 return -EINVAL;
1386 }
1387 }
1388
1389 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1390 tg_pt_gp->tg_pt_gp_valid_id = 1;
1391 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1392 &su_dev->t10_alua.tg_pt_gps_list);
1393 su_dev->t10_alua.alua_tg_pt_gps_count++;
1394 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1395
1396 return 0;
1397}
1398
1399struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1400 struct se_port *port)
1401{
1402 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1403
1404 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1405 GFP_KERNEL);
1406 if (!tg_pt_gp_mem) {
1407 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1408 return ERR_PTR(-ENOMEM);
1409 }
1410 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1411 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1412 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1413
1414 tg_pt_gp_mem->tg_pt = port;
1415 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1416 atomic_set(&port->sep_tg_pt_gp_active, 1);
1417
1418 return tg_pt_gp_mem;
1419}
1420
1421void core_alua_free_tg_pt_gp(
1422 struct t10_alua_tg_pt_gp *tg_pt_gp)
1423{
1424 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1425 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1426 /*
1427 * Once we have reached this point, config_item_put() has already
1428 * been called from target_core_alua_drop_tg_pt_gp().
1429 *
1430 * Here we remove *tg_pt_gp from the global list so that
1431 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1432 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1433 */
1434 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1435 list_del(&tg_pt_gp->tg_pt_gp_list);
1436 su_dev->t10_alua.alua_tg_pt_gps_counter--;
1437 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1438 /*
1439 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1440 * core_alua_get_tg_pt_gp_by_name() in
1441 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1442 * to be released with core_alua_put_tg_pt_gp_from_name().
1443 */
1444 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1445 cpu_relax();
1446 /*
1447 * Release reference to struct t10_alua_tg_pt_gp from all associated
1448 * struct se_port.
1449 */
1450 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1451 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1452 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1453 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1454 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1455 tg_pt_gp->tg_pt_gp_members--;
1456 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1457 }
1458 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1459 /*
1460 * tg_pt_gp_mem is associated with a single
1461 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1462 * core_alua_free_tg_pt_gp_mem().
1463 *
1464 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1465 * assume we want to re-assocate a given tg_pt_gp_mem with
1466 * default_tg_pt_gp.
1467 */
1468 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1469 if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
1470 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1471 su_dev->t10_alua.default_tg_pt_gp);
1472 } else
1473 tg_pt_gp_mem->tg_pt_gp = NULL;
1474 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1475
1476 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1477 }
1478 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1479
1480 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1481}
1482
1483void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1484{
1485 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1486 struct t10_alua *alua = &su_dev->t10_alua;
1487 struct t10_alua_tg_pt_gp *tg_pt_gp;
1488 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1489
1490 if (alua->alua_type != SPC3_ALUA_EMULATED)
1491 return;
1492
1493 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1494 if (!tg_pt_gp_mem)
1495 return;
1496
1497 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1498 cpu_relax();
1499
1500 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1501 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1502 if (tg_pt_gp) {
1503 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1504 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1505 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1506 tg_pt_gp->tg_pt_gp_members--;
1507 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1508 }
1509 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1510 tg_pt_gp_mem->tg_pt_gp = NULL;
1511 }
1512 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1513
1514 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1515}
1516
1517static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1518 struct se_subsystem_dev *su_dev,
1519 const char *name)
1520{
1521 struct t10_alua_tg_pt_gp *tg_pt_gp;
1522 struct config_item *ci;
1523
1524 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1525 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
1526 tg_pt_gp_list) {
1527 if (!tg_pt_gp->tg_pt_gp_valid_id)
1528 continue;
1529 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1530 if (!strcmp(config_item_name(ci), name)) {
1531 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1532 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1533 return tg_pt_gp;
1534 }
1535 }
1536 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1537
1538 return NULL;
1539}
1540
1541static void core_alua_put_tg_pt_gp_from_name(
1542 struct t10_alua_tg_pt_gp *tg_pt_gp)
1543{
1544 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1545
1546 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1547 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1548 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1549}
1550
1551/*
1552 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1553 */
1554void __core_alua_attach_tg_pt_gp_mem(
1555 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1556 struct t10_alua_tg_pt_gp *tg_pt_gp)
1557{
1558 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1559 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1560 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1561 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1562 &tg_pt_gp->tg_pt_gp_mem_list);
1563 tg_pt_gp->tg_pt_gp_members++;
1564 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1565}
1566
1567/*
1568 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1569 */
1570static void __core_alua_drop_tg_pt_gp_mem(
1571 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1572 struct t10_alua_tg_pt_gp *tg_pt_gp)
1573{
1574 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1575 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1576 tg_pt_gp_mem->tg_pt_gp = NULL;
1577 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1578 tg_pt_gp->tg_pt_gp_members--;
1579 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1580}
1581
1582ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1583{
1584 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1585 struct config_item *tg_pt_ci;
1586 struct t10_alua *alua = &su_dev->t10_alua;
1587 struct t10_alua_tg_pt_gp *tg_pt_gp;
1588 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1589 ssize_t len = 0;
1590
1591 if (alua->alua_type != SPC3_ALUA_EMULATED)
1592 return len;
1593
1594 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1595 if (!tg_pt_gp_mem)
1596 return len;
1597
1598 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1599 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1600 if (tg_pt_gp) {
1601 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1602 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1603 " %hu\nTG Port Primary Access State: %s\nTG Port "
1604 "Primary Access Status: %s\nTG Port Secondary Access"
1605 " State: %s\nTG Port Secondary Access Status: %s\n",
1606 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1607 core_alua_dump_state(atomic_read(
1608 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1609 core_alua_dump_status(
1610 tg_pt_gp->tg_pt_gp_alua_access_status),
1611 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1612 "Offline" : "None",
1613 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1614 }
1615 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1616
1617 return len;
1618}
1619
1620ssize_t core_alua_store_tg_pt_gp_info(
1621 struct se_port *port,
1622 const char *page,
1623 size_t count)
1624{
1625 struct se_portal_group *tpg;
1626 struct se_lun *lun;
1627 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1628 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1629 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1630 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1631 int move = 0;
1632
1633 tpg = port->sep_tpg;
1634 lun = port->sep_lun;
1635
1636 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1637 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1638 " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1639 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1640 config_item_name(&lun->lun_group.cg_item));
1641 return -EINVAL;
1642 }
1643
1644 if (count > TG_PT_GROUP_NAME_BUF) {
1645 pr_err("ALUA Target Port Group alias too large!\n");
1646 return -EINVAL;
1647 }
1648 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1649 memcpy(buf, page, count);
1650 /*
1651 * Any ALUA target port group alias besides "NULL" means we will be
1652 * making a new group association.
1653 */
1654 if (strcmp(strstrip(buf), "NULL")) {
1655 /*
1656 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1657 * struct t10_alua_tg_pt_gp. This reference is released with
1658 * core_alua_put_tg_pt_gp_from_name() below.
1659 */
1660 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
1661 strstrip(buf));
1662 if (!tg_pt_gp_new)
1663 return -ENODEV;
1664 }
1665 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1666 if (!tg_pt_gp_mem) {
1667 if (tg_pt_gp_new)
1668 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1669 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1670 return -EINVAL;
1671 }
1672
1673 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1674 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1675 if (tg_pt_gp) {
1676 /*
1677 * Clearing an existing tg_pt_gp association, and replacing
1678 * with the default_tg_pt_gp.
1679 */
1680 if (!tg_pt_gp_new) {
1681 pr_debug("Target_Core_ConfigFS: Moving"
1682 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1683 " alua/%s, ID: %hu back to"
1684 " default_tg_pt_gp\n",
1685 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1686 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1687 config_item_name(&lun->lun_group.cg_item),
1688 config_item_name(
1689 &tg_pt_gp->tg_pt_gp_group.cg_item),
1690 tg_pt_gp->tg_pt_gp_id);
1691
1692 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1693 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1694 su_dev->t10_alua.default_tg_pt_gp);
1695 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1696
1697 return count;
1698 }
1699 /*
1700 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1701 */
1702 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1703 move = 1;
1704 }
1705 /*
1706 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1707 */
1708 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1709 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1710 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1711 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1712 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1713 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1714 config_item_name(&lun->lun_group.cg_item),
1715 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1716 tg_pt_gp_new->tg_pt_gp_id);
1717
1718 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1719 return count;
1720}
1721
1722ssize_t core_alua_show_access_type(
1723 struct t10_alua_tg_pt_gp *tg_pt_gp,
1724 char *page)
1725{
1726 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1727 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1728 return sprintf(page, "Implict and Explict\n");
1729 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1730 return sprintf(page, "Implict\n");
1731 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1732 return sprintf(page, "Explict\n");
1733 else
1734 return sprintf(page, "None\n");
1735}
1736
1737ssize_t core_alua_store_access_type(
1738 struct t10_alua_tg_pt_gp *tg_pt_gp,
1739 const char *page,
1740 size_t count)
1741{
1742 unsigned long tmp;
1743 int ret;
1744
1745 ret = strict_strtoul(page, 0, &tmp);
1746 if (ret < 0) {
1747 pr_err("Unable to extract alua_access_type\n");
1748 return -EINVAL;
1749 }
1750 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1751 pr_err("Illegal value for alua_access_type:"
1752 " %lu\n", tmp);
1753 return -EINVAL;
1754 }
1755 if (tmp == 3)
1756 tg_pt_gp->tg_pt_gp_alua_access_type =
1757 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1758 else if (tmp == 2)
1759 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1760 else if (tmp == 1)
1761 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1762 else
1763 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1764
1765 return count;
1766}
1767
1768ssize_t core_alua_show_nonop_delay_msecs(
1769 struct t10_alua_tg_pt_gp *tg_pt_gp,
1770 char *page)
1771{
1772 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1773}
1774
1775ssize_t core_alua_store_nonop_delay_msecs(
1776 struct t10_alua_tg_pt_gp *tg_pt_gp,
1777 const char *page,
1778 size_t count)
1779{
1780 unsigned long tmp;
1781 int ret;
1782
1783 ret = strict_strtoul(page, 0, &tmp);
1784 if (ret < 0) {
1785 pr_err("Unable to extract nonop_delay_msecs\n");
1786 return -EINVAL;
1787 }
1788 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1789 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1790 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1791 ALUA_MAX_NONOP_DELAY_MSECS);
1792 return -EINVAL;
1793 }
1794 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1795
1796 return count;
1797}
1798
1799ssize_t core_alua_show_trans_delay_msecs(
1800 struct t10_alua_tg_pt_gp *tg_pt_gp,
1801 char *page)
1802{
1803 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1804}
1805
1806ssize_t core_alua_store_trans_delay_msecs(
1807 struct t10_alua_tg_pt_gp *tg_pt_gp,
1808 const char *page,
1809 size_t count)
1810{
1811 unsigned long tmp;
1812 int ret;
1813
1814 ret = strict_strtoul(page, 0, &tmp);
1815 if (ret < 0) {
1816 pr_err("Unable to extract trans_delay_msecs\n");
1817 return -EINVAL;
1818 }
1819 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1820 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1821 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1822 ALUA_MAX_TRANS_DELAY_MSECS);
1823 return -EINVAL;
1824 }
1825 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1826
1827 return count;
1828}
1829
1830ssize_t core_alua_show_preferred_bit(
1831 struct t10_alua_tg_pt_gp *tg_pt_gp,
1832 char *page)
1833{
1834 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1835}
1836
1837ssize_t core_alua_store_preferred_bit(
1838 struct t10_alua_tg_pt_gp *tg_pt_gp,
1839 const char *page,
1840 size_t count)
1841{
1842 unsigned long tmp;
1843 int ret;
1844
1845 ret = strict_strtoul(page, 0, &tmp);
1846 if (ret < 0) {
1847 pr_err("Unable to extract preferred ALUA value\n");
1848 return -EINVAL;
1849 }
1850 if ((tmp != 0) && (tmp != 1)) {
1851 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1852 return -EINVAL;
1853 }
1854 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1855
1856 return count;
1857}
1858
1859ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1860{
1861 if (!lun->lun_sep)
1862 return -ENODEV;
1863
1864 return sprintf(page, "%d\n",
1865 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1866}
1867
1868ssize_t core_alua_store_offline_bit(
1869 struct se_lun *lun,
1870 const char *page,
1871 size_t count)
1872{
1873 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1874 unsigned long tmp;
1875 int ret;
1876
1877 if (!lun->lun_sep)
1878 return -ENODEV;
1879
1880 ret = strict_strtoul(page, 0, &tmp);
1881 if (ret < 0) {
1882 pr_err("Unable to extract alua_tg_pt_offline value\n");
1883 return -EINVAL;
1884 }
1885 if ((tmp != 0) && (tmp != 1)) {
1886 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1887 tmp);
1888 return -EINVAL;
1889 }
1890 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1891 if (!tg_pt_gp_mem) {
1892 pr_err("Unable to locate *tg_pt_gp_mem\n");
1893 return -EINVAL;
1894 }
1895
1896 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1897 lun->lun_sep, 0, (int)tmp);
1898 if (ret < 0)
1899 return -EINVAL;
1900
1901 return count;
1902}
1903
1904ssize_t core_alua_show_secondary_status(
1905 struct se_lun *lun,
1906 char *page)
1907{
1908 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1909}
1910
1911ssize_t core_alua_store_secondary_status(
1912 struct se_lun *lun,
1913 const char *page,
1914 size_t count)
1915{
1916 unsigned long tmp;
1917 int ret;
1918
1919 ret = strict_strtoul(page, 0, &tmp);
1920 if (ret < 0) {
1921 pr_err("Unable to extract alua_tg_pt_status\n");
1922 return -EINVAL;
1923 }
1924 if ((tmp != ALUA_STATUS_NONE) &&
1925 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1926 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1927 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1928 tmp);
1929 return -EINVAL;
1930 }
1931 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1932
1933 return count;
1934}
1935
1936ssize_t core_alua_show_secondary_write_metadata(
1937 struct se_lun *lun,
1938 char *page)
1939{
1940 return sprintf(page, "%d\n",
1941 lun->lun_sep->sep_tg_pt_secondary_write_md);
1942}
1943
1944ssize_t core_alua_store_secondary_write_metadata(
1945 struct se_lun *lun,
1946 const char *page,
1947 size_t count)
1948{
1949 unsigned long tmp;
1950 int ret;
1951
1952 ret = strict_strtoul(page, 0, &tmp);
1953 if (ret < 0) {
1954 pr_err("Unable to extract alua_tg_pt_write_md\n");
1955 return -EINVAL;
1956 }
1957 if ((tmp != 0) && (tmp != 1)) {
1958 pr_err("Illegal value for alua_tg_pt_write_md:"
1959 " %lu\n", tmp);
1960 return -EINVAL;
1961 }
1962 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
1963
1964 return count;
1965}
1966
1967int core_setup_alua(struct se_device *dev, int force_pt)
1968{
1969 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1970 struct t10_alua *alua = &su_dev->t10_alua;
1971 struct t10_alua_lu_gp_member *lu_gp_mem;
1972 /*
1973 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
1974 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
1975 * cause a problem because libata and some SATA RAID HBAs appear
1976 * under Linux/SCSI, but emulate SCSI logic themselves.
1977 */
1978 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
1979 !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
1980 alua->alua_type = SPC_ALUA_PASSTHROUGH;
1981 alua->alua_state_check = &core_alua_state_check_nop;
1982 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
1983 " emulation\n", dev->transport->name);
1984 return 0;
1985 }
1986 /*
1987 * If SPC-3 or above is reported by real or emulated struct se_device,
1988 * use emulated ALUA.
1989 */
1990 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
1991 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
1992 " device\n", dev->transport->name);
1993 /*
1994 * Associate this struct se_device with the default ALUA
1995 * LUN Group.
1996 */
1997 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
1998 if (IS_ERR(lu_gp_mem))
1999 return PTR_ERR(lu_gp_mem);
2000
2001 alua->alua_type = SPC3_ALUA_EMULATED;
2002 alua->alua_state_check = &core_alua_state_check;
2003 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2004 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2005 default_lu_gp);
2006 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2007
2008 pr_debug("%s: Adding to default ALUA LU Group:"
2009 " core/alua/lu_gps/default_lu_gp\n",
2010 dev->transport->name);
2011 } else {
2012 alua->alua_type = SPC2_ALUA_DISABLED;
2013 alua->alua_state_check = &core_alua_state_check_nop;
2014 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2015 " device\n", dev->transport->name);
2016 }
2017
2018 return 0;
2019}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*******************************************************************************
3 * Filename: target_core_alua.c
4 *
5 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 *
7 * (c) Copyright 2009-2013 Datera, Inc.
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 ******************************************************************************/
12
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/configfs.h>
16#include <linux/delay.h>
17#include <linux/export.h>
18#include <linux/fcntl.h>
19#include <linux/file.h>
20#include <linux/fs.h>
21#include <scsi/scsi_proto.h>
22#include <asm/unaligned.h>
23
24#include <target/target_core_base.h>
25#include <target/target_core_backend.h>
26#include <target/target_core_fabric.h>
27
28#include "target_core_internal.h"
29#include "target_core_alua.h"
30#include "target_core_ua.h"
31
32static sense_reason_t core_alua_check_transition(int state, int valid,
33 int *primary, int explicit);
34static int core_alua_set_tg_pt_secondary_state(
35 struct se_lun *lun, int explicit, int offline);
36
37static char *core_alua_dump_state(int state);
38
39static void __target_attach_tg_pt_gp(struct se_lun *lun,
40 struct t10_alua_tg_pt_gp *tg_pt_gp);
41
42static u16 alua_lu_gps_counter;
43static u32 alua_lu_gps_count;
44
45static DEFINE_SPINLOCK(lu_gps_lock);
46static LIST_HEAD(lu_gps_list);
47
48struct t10_alua_lu_gp *default_lu_gp;
49
50/*
51 * REPORT REFERRALS
52 *
53 * See sbc3r35 section 5.23
54 */
55sense_reason_t
56target_emulate_report_referrals(struct se_cmd *cmd)
57{
58 struct se_device *dev = cmd->se_dev;
59 struct t10_alua_lba_map *map;
60 struct t10_alua_lba_map_member *map_mem;
61 unsigned char *buf;
62 u32 rd_len = 0, off;
63
64 if (cmd->data_length < 4) {
65 pr_warn("REPORT REFERRALS allocation length %u too"
66 " small\n", cmd->data_length);
67 return TCM_INVALID_CDB_FIELD;
68 }
69
70 buf = transport_kmap_data_sg(cmd);
71 if (!buf)
72 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
73
74 off = 4;
75 spin_lock(&dev->t10_alua.lba_map_lock);
76 if (list_empty(&dev->t10_alua.lba_map_list)) {
77 spin_unlock(&dev->t10_alua.lba_map_lock);
78 transport_kunmap_data_sg(cmd);
79
80 return TCM_UNSUPPORTED_SCSI_OPCODE;
81 }
82
83 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
84 lba_map_list) {
85 int desc_num = off + 3;
86 int pg_num;
87
88 off += 4;
89 if (cmd->data_length > off)
90 put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
91 off += 8;
92 if (cmd->data_length > off)
93 put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
94 off += 8;
95 rd_len += 20;
96 pg_num = 0;
97 list_for_each_entry(map_mem, &map->lba_map_mem_list,
98 lba_map_mem_list) {
99 int alua_state = map_mem->lba_map_mem_alua_state;
100 int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;
101
102 if (cmd->data_length > off)
103 buf[off] = alua_state & 0x0f;
104 off += 2;
105 if (cmd->data_length > off)
106 buf[off] = (alua_pg_id >> 8) & 0xff;
107 off++;
108 if (cmd->data_length > off)
109 buf[off] = (alua_pg_id & 0xff);
110 off++;
111 rd_len += 4;
112 pg_num++;
113 }
114 if (cmd->data_length > desc_num)
115 buf[desc_num] = pg_num;
116 }
117 spin_unlock(&dev->t10_alua.lba_map_lock);
118
119 /*
120 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
121 */
122 put_unaligned_be16(rd_len, &buf[2]);
123
124 transport_kunmap_data_sg(cmd);
125
126 target_complete_cmd(cmd, SAM_STAT_GOOD);
127 return 0;
128}
129
130/*
131 * REPORT_TARGET_PORT_GROUPS
132 *
133 * See spc4r17 section 6.27
134 */
135sense_reason_t
136target_emulate_report_target_port_groups(struct se_cmd *cmd)
137{
138 struct se_device *dev = cmd->se_dev;
139 struct t10_alua_tg_pt_gp *tg_pt_gp;
140 struct se_lun *lun;
141 unsigned char *buf;
142 u32 rd_len = 0, off;
143 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
144
145 /*
146 * Skip over RESERVED area to first Target port group descriptor
147 * depending on the PARAMETER DATA FORMAT type..
148 */
149 if (ext_hdr != 0)
150 off = 8;
151 else
152 off = 4;
153
154 if (cmd->data_length < off) {
155 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
156 " small for %s header\n", cmd->data_length,
157 (ext_hdr) ? "extended" : "normal");
158 return TCM_INVALID_CDB_FIELD;
159 }
160 buf = transport_kmap_data_sg(cmd);
161 if (!buf)
162 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
163
164 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
165 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
166 tg_pt_gp_list) {
167 /* Skip empty port groups */
168 if (!tg_pt_gp->tg_pt_gp_members)
169 continue;
170 /*
171 * Check if the Target port group and Target port descriptor list
172 * based on tg_pt_gp_members count will fit into the response payload.
173 * Otherwise, bump rd_len to let the initiator know we have exceeded
174 * the allocation length and the response is truncated.
175 */
176 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
177 cmd->data_length) {
178 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
179 continue;
180 }
181 /*
182 * PREF: Preferred target port bit, determine if this
183 * bit should be set for port group.
184 */
185 if (tg_pt_gp->tg_pt_gp_pref)
186 buf[off] = 0x80;
187 /*
188 * Set the ASYMMETRIC ACCESS State
189 */
190 buf[off++] |= tg_pt_gp->tg_pt_gp_alua_access_state & 0xff;
191 /*
192 * Set supported ASYMMETRIC ACCESS State bits
193 */
194 buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
195 /*
196 * TARGET PORT GROUP
197 */
198 put_unaligned_be16(tg_pt_gp->tg_pt_gp_id, &buf[off]);
199 off += 2;
200
201 off++; /* Skip over Reserved */
202 /*
203 * STATUS CODE
204 */
205 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
206 /*
207 * Vendor Specific field
208 */
209 buf[off++] = 0x00;
210 /*
211 * TARGET PORT COUNT
212 */
213 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
214 rd_len += 8;
215
216 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
217 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
218 lun_tg_pt_gp_link) {
219 /*
220 * Start Target Port descriptor format
221 *
222 * See spc4r17 section 6.2.7 Table 247
223 */
224 off += 2; /* Skip over Obsolete */
225 /*
226 * Set RELATIVE TARGET PORT IDENTIFIER
227 */
228 put_unaligned_be16(lun->lun_rtpi, &buf[off]);
229 off += 2;
230 rd_len += 4;
231 }
232 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
233 }
234 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
235 /*
236 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
237 */
238 put_unaligned_be32(rd_len, &buf[0]);
239
240 /*
241 * Fill in the Extended header parameter data format if requested
242 */
243 if (ext_hdr != 0) {
244 buf[4] = 0x10;
245 /*
246 * Set the implicit transition time (in seconds) for the application
247 * client to use as a base for it's transition timeout value.
248 *
249 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
250 * this CDB was received upon to determine this value individually
251 * for ALUA target port group.
252 */
253 rcu_read_lock();
254 tg_pt_gp = rcu_dereference(cmd->se_lun->lun_tg_pt_gp);
255 if (tg_pt_gp)
256 buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
257 rcu_read_unlock();
258 }
259 transport_kunmap_data_sg(cmd);
260
261 target_complete_cmd_with_length(cmd, SAM_STAT_GOOD, rd_len + 4);
262 return 0;
263}
264
265/*
266 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
267 *
268 * See spc4r17 section 6.35
269 */
270sense_reason_t
271target_emulate_set_target_port_groups(struct se_cmd *cmd)
272{
273 struct se_device *dev = cmd->se_dev;
274 struct se_lun *l_lun = cmd->se_lun;
275 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
276 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
277 unsigned char *buf;
278 unsigned char *ptr;
279 sense_reason_t rc = TCM_NO_SENSE;
280 u32 len = 4; /* Skip over RESERVED area in header */
281 int alua_access_state, primary = 0, valid_states;
282 u16 tg_pt_id, rtpi;
283
284 if (cmd->data_length < 4) {
285 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
286 " small\n", cmd->data_length);
287 return TCM_INVALID_PARAMETER_LIST;
288 }
289
290 buf = transport_kmap_data_sg(cmd);
291 if (!buf)
292 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
293
294 /*
295 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
296 * for the local tg_pt_gp.
297 */
298 rcu_read_lock();
299 l_tg_pt_gp = rcu_dereference(l_lun->lun_tg_pt_gp);
300 if (!l_tg_pt_gp) {
301 rcu_read_unlock();
302 pr_err("Unable to access l_lun->tg_pt_gp\n");
303 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
304 goto out;
305 }
306
307 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
308 rcu_read_unlock();
309 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
310 " while TPGS_EXPLICIT_ALUA is disabled\n");
311 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
312 goto out;
313 }
314 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
315 rcu_read_unlock();
316
317 ptr = &buf[4]; /* Skip over RESERVED area in header */
318
319 while (len < cmd->data_length) {
320 bool found = false;
321 alua_access_state = (ptr[0] & 0x0f);
322 /*
323 * Check the received ALUA access state, and determine if
324 * the state is a primary or secondary target port asymmetric
325 * access state.
326 */
327 rc = core_alua_check_transition(alua_access_state, valid_states,
328 &primary, 1);
329 if (rc) {
330 /*
331 * If the SET TARGET PORT GROUPS attempts to establish
332 * an invalid combination of target port asymmetric
333 * access states or attempts to establish an
334 * unsupported target port asymmetric access state,
335 * then the command shall be terminated with CHECK
336 * CONDITION status, with the sense key set to ILLEGAL
337 * REQUEST, and the additional sense code set to INVALID
338 * FIELD IN PARAMETER LIST.
339 */
340 goto out;
341 }
342
343 /*
344 * If the ASYMMETRIC ACCESS STATE field (see table 267)
345 * specifies a primary target port asymmetric access state,
346 * then the TARGET PORT GROUP OR TARGET PORT field specifies
347 * a primary target port group for which the primary target
348 * port asymmetric access state shall be changed. If the
349 * ASYMMETRIC ACCESS STATE field specifies a secondary target
350 * port asymmetric access state, then the TARGET PORT GROUP OR
351 * TARGET PORT field specifies the relative target port
352 * identifier (see 3.1.120) of the target port for which the
353 * secondary target port asymmetric access state shall be
354 * changed.
355 */
356 if (primary) {
357 tg_pt_id = get_unaligned_be16(ptr + 2);
358 /*
359 * Locate the matching target port group ID from
360 * the global tg_pt_gp list
361 */
362 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
363 list_for_each_entry(tg_pt_gp,
364 &dev->t10_alua.tg_pt_gps_list,
365 tg_pt_gp_list) {
366 if (!tg_pt_gp->tg_pt_gp_valid_id)
367 continue;
368
369 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
370 continue;
371
372 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
373
374 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
375
376 if (!core_alua_do_port_transition(tg_pt_gp,
377 dev, l_lun, nacl,
378 alua_access_state, 1))
379 found = true;
380
381 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
382 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
383 break;
384 }
385 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
386 } else {
387 struct se_lun *lun;
388
389 /*
390 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
391 * the Target Port in question for the incoming
392 * SET_TARGET_PORT_GROUPS op.
393 */
394 rtpi = get_unaligned_be16(ptr + 2);
395 /*
396 * Locate the matching relative target port identifier
397 * for the struct se_device storage object.
398 */
399 spin_lock(&dev->se_port_lock);
400 list_for_each_entry(lun, &dev->dev_sep_list,
401 lun_dev_link) {
402 if (lun->lun_rtpi != rtpi)
403 continue;
404
405 // XXX: racy unlock
406 spin_unlock(&dev->se_port_lock);
407
408 if (!core_alua_set_tg_pt_secondary_state(
409 lun, 1, 1))
410 found = true;
411
412 spin_lock(&dev->se_port_lock);
413 break;
414 }
415 spin_unlock(&dev->se_port_lock);
416 }
417
418 if (!found) {
419 rc = TCM_INVALID_PARAMETER_LIST;
420 goto out;
421 }
422
423 ptr += 4;
424 len += 4;
425 }
426
427out:
428 transport_kunmap_data_sg(cmd);
429 if (!rc)
430 target_complete_cmd(cmd, SAM_STAT_GOOD);
431 return rc;
432}
433
434static inline void core_alua_state_nonoptimized(
435 struct se_cmd *cmd,
436 unsigned char *cdb,
437 int nonop_delay_msecs)
438{
439 /*
440 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
441 * later to determine if processing of this cmd needs to be
442 * temporarily delayed for the Active/NonOptimized primary access state.
443 */
444 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
445 cmd->alua_nonop_delay = nonop_delay_msecs;
446}
447
448static inline sense_reason_t core_alua_state_lba_dependent(
449 struct se_cmd *cmd,
450 u16 tg_pt_gp_id)
451{
452 struct se_device *dev = cmd->se_dev;
453 u64 segment_size, segment_mult, sectors, lba;
454
455 /* Only need to check for cdb actually containing LBAs */
456 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
457 return 0;
458
459 spin_lock(&dev->t10_alua.lba_map_lock);
460 segment_size = dev->t10_alua.lba_map_segment_size;
461 segment_mult = dev->t10_alua.lba_map_segment_multiplier;
462 sectors = cmd->data_length / dev->dev_attrib.block_size;
463
464 lba = cmd->t_task_lba;
465 while (lba < cmd->t_task_lba + sectors) {
466 struct t10_alua_lba_map *cur_map = NULL, *map;
467 struct t10_alua_lba_map_member *map_mem;
468
469 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
470 lba_map_list) {
471 u64 start_lba, last_lba;
472 u64 first_lba = map->lba_map_first_lba;
473
474 if (segment_mult) {
475 u64 tmp = lba;
476 start_lba = do_div(tmp, segment_size * segment_mult);
477
478 last_lba = first_lba + segment_size - 1;
479 if (start_lba >= first_lba &&
480 start_lba <= last_lba) {
481 lba += segment_size;
482 cur_map = map;
483 break;
484 }
485 } else {
486 last_lba = map->lba_map_last_lba;
487 if (lba >= first_lba && lba <= last_lba) {
488 lba = last_lba + 1;
489 cur_map = map;
490 break;
491 }
492 }
493 }
494 if (!cur_map) {
495 spin_unlock(&dev->t10_alua.lba_map_lock);
496 return TCM_ALUA_TG_PT_UNAVAILABLE;
497 }
498 list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
499 lba_map_mem_list) {
500 if (map_mem->lba_map_mem_alua_pg_id != tg_pt_gp_id)
501 continue;
502 switch(map_mem->lba_map_mem_alua_state) {
503 case ALUA_ACCESS_STATE_STANDBY:
504 spin_unlock(&dev->t10_alua.lba_map_lock);
505 return TCM_ALUA_TG_PT_STANDBY;
506 case ALUA_ACCESS_STATE_UNAVAILABLE:
507 spin_unlock(&dev->t10_alua.lba_map_lock);
508 return TCM_ALUA_TG_PT_UNAVAILABLE;
509 default:
510 break;
511 }
512 }
513 }
514 spin_unlock(&dev->t10_alua.lba_map_lock);
515 return 0;
516}
517
518static inline sense_reason_t core_alua_state_standby(
519 struct se_cmd *cmd,
520 unsigned char *cdb)
521{
522 /*
523 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
524 * spc4r17 section 5.9.2.4.4
525 */
526 switch (cdb[0]) {
527 case INQUIRY:
528 case LOG_SELECT:
529 case LOG_SENSE:
530 case MODE_SELECT:
531 case MODE_SENSE:
532 case REPORT_LUNS:
533 case RECEIVE_DIAGNOSTIC:
534 case SEND_DIAGNOSTIC:
535 case READ_CAPACITY:
536 return 0;
537 case SERVICE_ACTION_IN_16:
538 switch (cdb[1] & 0x1f) {
539 case SAI_READ_CAPACITY_16:
540 return 0;
541 default:
542 return TCM_ALUA_TG_PT_STANDBY;
543 }
544 case MAINTENANCE_IN:
545 switch (cdb[1] & 0x1f) {
546 case MI_REPORT_TARGET_PGS:
547 return 0;
548 default:
549 return TCM_ALUA_TG_PT_STANDBY;
550 }
551 case MAINTENANCE_OUT:
552 switch (cdb[1]) {
553 case MO_SET_TARGET_PGS:
554 return 0;
555 default:
556 return TCM_ALUA_TG_PT_STANDBY;
557 }
558 case REQUEST_SENSE:
559 case PERSISTENT_RESERVE_IN:
560 case PERSISTENT_RESERVE_OUT:
561 case READ_BUFFER:
562 case WRITE_BUFFER:
563 return 0;
564 default:
565 return TCM_ALUA_TG_PT_STANDBY;
566 }
567
568 return 0;
569}
570
571static inline sense_reason_t core_alua_state_unavailable(
572 struct se_cmd *cmd,
573 unsigned char *cdb)
574{
575 /*
576 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
577 * spc4r17 section 5.9.2.4.5
578 */
579 switch (cdb[0]) {
580 case INQUIRY:
581 case REPORT_LUNS:
582 return 0;
583 case MAINTENANCE_IN:
584 switch (cdb[1] & 0x1f) {
585 case MI_REPORT_TARGET_PGS:
586 return 0;
587 default:
588 return TCM_ALUA_TG_PT_UNAVAILABLE;
589 }
590 case MAINTENANCE_OUT:
591 switch (cdb[1]) {
592 case MO_SET_TARGET_PGS:
593 return 0;
594 default:
595 return TCM_ALUA_TG_PT_UNAVAILABLE;
596 }
597 case REQUEST_SENSE:
598 case READ_BUFFER:
599 case WRITE_BUFFER:
600 return 0;
601 default:
602 return TCM_ALUA_TG_PT_UNAVAILABLE;
603 }
604
605 return 0;
606}
607
608static inline sense_reason_t core_alua_state_transition(
609 struct se_cmd *cmd,
610 unsigned char *cdb)
611{
612 /*
613 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
614 * spc4r17 section 5.9.2.5
615 */
616 switch (cdb[0]) {
617 case INQUIRY:
618 case REPORT_LUNS:
619 return 0;
620 case MAINTENANCE_IN:
621 switch (cdb[1] & 0x1f) {
622 case MI_REPORT_TARGET_PGS:
623 return 0;
624 default:
625 return TCM_ALUA_STATE_TRANSITION;
626 }
627 case REQUEST_SENSE:
628 case READ_BUFFER:
629 case WRITE_BUFFER:
630 return 0;
631 default:
632 return TCM_ALUA_STATE_TRANSITION;
633 }
634
635 return 0;
636}
637
638/*
639 * return 1: Is used to signal LUN not accessible, and check condition/not ready
640 * return 0: Used to signal success
641 * return -1: Used to signal failure, and invalid cdb field
642 */
643sense_reason_t
644target_alua_state_check(struct se_cmd *cmd)
645{
646 struct se_device *dev = cmd->se_dev;
647 unsigned char *cdb = cmd->t_task_cdb;
648 struct se_lun *lun = cmd->se_lun;
649 struct t10_alua_tg_pt_gp *tg_pt_gp;
650 int out_alua_state, nonop_delay_msecs;
651 u16 tg_pt_gp_id;
652 sense_reason_t rc = TCM_NO_SENSE;
653
654 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
655 return 0;
656 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
657 return 0;
658
659 /*
660 * First, check for a struct se_port specific secondary ALUA target port
661 * access state: OFFLINE
662 */
663 if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
664 pr_debug("ALUA: Got secondary offline status for local"
665 " target port\n");
666 return TCM_ALUA_OFFLINE;
667 }
668 rcu_read_lock();
669 tg_pt_gp = rcu_dereference(lun->lun_tg_pt_gp);
670 if (!tg_pt_gp) {
671 rcu_read_unlock();
672 return 0;
673 }
674
675 out_alua_state = tg_pt_gp->tg_pt_gp_alua_access_state;
676 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
677 tg_pt_gp_id = tg_pt_gp->tg_pt_gp_id;
678 rcu_read_unlock();
679 /*
680 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
681 * statement so the compiler knows explicitly to check this case first.
682 * For the Optimized ALUA access state case, we want to process the
683 * incoming fabric cmd ASAP..
684 */
685 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
686 return 0;
687
688 switch (out_alua_state) {
689 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
690 core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
691 break;
692 case ALUA_ACCESS_STATE_STANDBY:
693 rc = core_alua_state_standby(cmd, cdb);
694 break;
695 case ALUA_ACCESS_STATE_UNAVAILABLE:
696 rc = core_alua_state_unavailable(cmd, cdb);
697 break;
698 case ALUA_ACCESS_STATE_TRANSITION:
699 rc = core_alua_state_transition(cmd, cdb);
700 break;
701 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
702 rc = core_alua_state_lba_dependent(cmd, tg_pt_gp_id);
703 break;
704 /*
705 * OFFLINE is a secondary ALUA target port group access state, that is
706 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
707 */
708 case ALUA_ACCESS_STATE_OFFLINE:
709 default:
710 pr_err("Unknown ALUA access state: 0x%02x\n",
711 out_alua_state);
712 rc = TCM_INVALID_CDB_FIELD;
713 }
714
715 if (rc && rc != TCM_INVALID_CDB_FIELD) {
716 pr_debug("[%s]: ALUA TG Port not available, "
717 "SenseKey: NOT_READY, ASC/rc: 0x04/%d\n",
718 cmd->se_tfo->fabric_name, rc);
719 }
720
721 return rc;
722}
723
724/*
725 * Check implicit and explicit ALUA state change request.
726 */
727static sense_reason_t
728core_alua_check_transition(int state, int valid, int *primary, int explicit)
729{
730 /*
731 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
732 * defined as primary target port asymmetric access states.
733 */
734 switch (state) {
735 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
736 if (!(valid & ALUA_AO_SUP))
737 goto not_supported;
738 *primary = 1;
739 break;
740 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
741 if (!(valid & ALUA_AN_SUP))
742 goto not_supported;
743 *primary = 1;
744 break;
745 case ALUA_ACCESS_STATE_STANDBY:
746 if (!(valid & ALUA_S_SUP))
747 goto not_supported;
748 *primary = 1;
749 break;
750 case ALUA_ACCESS_STATE_UNAVAILABLE:
751 if (!(valid & ALUA_U_SUP))
752 goto not_supported;
753 *primary = 1;
754 break;
755 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
756 if (!(valid & ALUA_LBD_SUP))
757 goto not_supported;
758 *primary = 1;
759 break;
760 case ALUA_ACCESS_STATE_OFFLINE:
761 /*
762 * OFFLINE state is defined as a secondary target port
763 * asymmetric access state.
764 */
765 if (!(valid & ALUA_O_SUP))
766 goto not_supported;
767 *primary = 0;
768 break;
769 case ALUA_ACCESS_STATE_TRANSITION:
770 if (!(valid & ALUA_T_SUP) || explicit)
771 /*
772 * Transitioning is set internally and by tcmu daemon,
773 * and cannot be selected through a STPG.
774 */
775 goto not_supported;
776 *primary = 0;
777 break;
778 default:
779 pr_err("Unknown ALUA access state: 0x%02x\n", state);
780 return TCM_INVALID_PARAMETER_LIST;
781 }
782
783 return 0;
784
785not_supported:
786 pr_err("ALUA access state %s not supported",
787 core_alua_dump_state(state));
788 return TCM_INVALID_PARAMETER_LIST;
789}
790
791static char *core_alua_dump_state(int state)
792{
793 switch (state) {
794 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
795 return "Active/Optimized";
796 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
797 return "Active/NonOptimized";
798 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
799 return "LBA Dependent";
800 case ALUA_ACCESS_STATE_STANDBY:
801 return "Standby";
802 case ALUA_ACCESS_STATE_UNAVAILABLE:
803 return "Unavailable";
804 case ALUA_ACCESS_STATE_OFFLINE:
805 return "Offline";
806 case ALUA_ACCESS_STATE_TRANSITION:
807 return "Transitioning";
808 default:
809 return "Unknown";
810 }
811
812 return NULL;
813}
814
815char *core_alua_dump_status(int status)
816{
817 switch (status) {
818 case ALUA_STATUS_NONE:
819 return "None";
820 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
821 return "Altered by Explicit STPG";
822 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
823 return "Altered by Implicit ALUA";
824 default:
825 return "Unknown";
826 }
827
828 return NULL;
829}
830
831/*
832 * Used by fabric modules to determine when we need to delay processing
833 * for the Active/NonOptimized paths..
834 */
835int core_alua_check_nonop_delay(
836 struct se_cmd *cmd)
837{
838 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
839 return 0;
840 /*
841 * The ALUA Active/NonOptimized access state delay can be disabled
842 * in via configfs with a value of zero
843 */
844 if (!cmd->alua_nonop_delay)
845 return 0;
846 /*
847 * struct se_cmd->alua_nonop_delay gets set by a target port group
848 * defined interval in core_alua_state_nonoptimized()
849 */
850 msleep_interruptible(cmd->alua_nonop_delay);
851 return 0;
852}
853EXPORT_SYMBOL(core_alua_check_nonop_delay);
854
855static int core_alua_write_tpg_metadata(
856 const char *path,
857 unsigned char *md_buf,
858 u32 md_buf_len)
859{
860 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
861 loff_t pos = 0;
862 int ret;
863
864 if (IS_ERR(file)) {
865 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
866 return -ENODEV;
867 }
868 ret = kernel_write(file, md_buf, md_buf_len, &pos);
869 if (ret < 0)
870 pr_err("Error writing ALUA metadata file: %s\n", path);
871 fput(file);
872 return (ret < 0) ? -EIO : 0;
873}
874
875static int core_alua_update_tpg_primary_metadata(
876 struct t10_alua_tg_pt_gp *tg_pt_gp)
877{
878 unsigned char *md_buf;
879 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
880 char *path;
881 int len, rc;
882
883 lockdep_assert_held(&tg_pt_gp->tg_pt_gp_transition_mutex);
884
885 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
886 if (!md_buf) {
887 pr_err("Unable to allocate buf for ALUA metadata\n");
888 return -ENOMEM;
889 }
890
891 len = snprintf(md_buf, ALUA_MD_BUF_LEN,
892 "tg_pt_gp_id=%hu\n"
893 "alua_access_state=0x%02x\n"
894 "alua_access_status=0x%02x\n",
895 tg_pt_gp->tg_pt_gp_id,
896 tg_pt_gp->tg_pt_gp_alua_access_state,
897 tg_pt_gp->tg_pt_gp_alua_access_status);
898
899 rc = -ENOMEM;
900 path = kasprintf(GFP_KERNEL, "%s/alua/tpgs_%s/%s", db_root,
901 &wwn->unit_serial[0],
902 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
903 if (path) {
904 rc = core_alua_write_tpg_metadata(path, md_buf, len);
905 kfree(path);
906 }
907 kfree(md_buf);
908 return rc;
909}
910
911static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp)
912{
913 struct se_dev_entry *se_deve;
914 struct se_lun *lun;
915 struct se_lun_acl *lacl;
916
917 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
918 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
919 lun_tg_pt_gp_link) {
920 /*
921 * After an implicit target port asymmetric access state
922 * change, a device server shall establish a unit attention
923 * condition for the initiator port associated with every I_T
924 * nexus with the additional sense code set to ASYMMETRIC
925 * ACCESS STATE CHANGED.
926 *
927 * After an explicit target port asymmetric access state
928 * change, a device server shall establish a unit attention
929 * condition with the additional sense code set to ASYMMETRIC
930 * ACCESS STATE CHANGED for the initiator port associated with
931 * every I_T nexus other than the I_T nexus on which the SET
932 * TARGET PORT GROUPS command
933 */
934 if (!percpu_ref_tryget_live(&lun->lun_ref))
935 continue;
936 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
937
938 spin_lock(&lun->lun_deve_lock);
939 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
940 lacl = se_deve->se_lun_acl;
941
942 /*
943 * spc4r37 p.242:
944 * After an explicit target port asymmetric access
945 * state change, a device server shall establish a
946 * unit attention condition with the additional sense
947 * code set to ASYMMETRIC ACCESS STATE CHANGED for
948 * the initiator port associated with every I_T nexus
949 * other than the I_T nexus on which the SET TARGET
950 * PORT GROUPS command was received.
951 */
952 if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
953 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
954 (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
955 (tg_pt_gp->tg_pt_gp_alua_lun == lun))
956 continue;
957
958 /*
959 * se_deve->se_lun_acl pointer may be NULL for a
960 * entry created without explicit Node+MappedLUN ACLs
961 */
962 if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
963 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl))
964 continue;
965
966 core_scsi3_ua_allocate(se_deve, 0x2A,
967 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
968 }
969 spin_unlock(&lun->lun_deve_lock);
970
971 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
972 percpu_ref_put(&lun->lun_ref);
973 }
974 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
975}
976
977static int core_alua_do_transition_tg_pt(
978 struct t10_alua_tg_pt_gp *tg_pt_gp,
979 int new_state,
980 int explicit)
981{
982 int prev_state;
983
984 mutex_lock(&tg_pt_gp->tg_pt_gp_transition_mutex);
985 /* Nothing to be done here */
986 if (tg_pt_gp->tg_pt_gp_alua_access_state == new_state) {
987 mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
988 return 0;
989 }
990
991 if (explicit && new_state == ALUA_ACCESS_STATE_TRANSITION) {
992 mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
993 return -EAGAIN;
994 }
995
996 /*
997 * Save the old primary ALUA access state, and set the current state
998 * to ALUA_ACCESS_STATE_TRANSITION.
999 */
1000 prev_state = tg_pt_gp->tg_pt_gp_alua_access_state;
1001 tg_pt_gp->tg_pt_gp_alua_access_state = ALUA_ACCESS_STATE_TRANSITION;
1002 tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1003 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1004 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1005
1006 core_alua_queue_state_change_ua(tg_pt_gp);
1007
1008 if (new_state == ALUA_ACCESS_STATE_TRANSITION) {
1009 mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
1010 return 0;
1011 }
1012
1013 /*
1014 * Check for the optional ALUA primary state transition delay
1015 */
1016 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
1017 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1018
1019 /*
1020 * Set the current primary ALUA access state to the requested new state
1021 */
1022 tg_pt_gp->tg_pt_gp_alua_access_state = new_state;
1023
1024 /*
1025 * Update the ALUA metadata buf that has been allocated in
1026 * core_alua_do_port_transition(), this metadata will be written
1027 * to struct file.
1028 *
1029 * Note that there is the case where we do not want to update the
1030 * metadata when the saved metadata is being parsed in userspace
1031 * when setting the existing port access state and access status.
1032 *
1033 * Also note that the failure to write out the ALUA metadata to
1034 * struct file does NOT affect the actual ALUA transition.
1035 */
1036 if (tg_pt_gp->tg_pt_gp_write_metadata) {
1037 core_alua_update_tpg_primary_metadata(tg_pt_gp);
1038 }
1039
1040 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1041 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1042 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1043 tg_pt_gp->tg_pt_gp_id,
1044 core_alua_dump_state(prev_state),
1045 core_alua_dump_state(new_state));
1046
1047 core_alua_queue_state_change_ua(tg_pt_gp);
1048
1049 mutex_unlock(&tg_pt_gp->tg_pt_gp_transition_mutex);
1050 return 0;
1051}
1052
1053int core_alua_do_port_transition(
1054 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1055 struct se_device *l_dev,
1056 struct se_lun *l_lun,
1057 struct se_node_acl *l_nacl,
1058 int new_state,
1059 int explicit)
1060{
1061 struct se_device *dev;
1062 struct t10_alua_lu_gp *lu_gp;
1063 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1064 struct t10_alua_tg_pt_gp *tg_pt_gp;
1065 int primary, valid_states, rc = 0;
1066
1067 if (l_dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
1068 return -ENODEV;
1069
1070 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1071 if (core_alua_check_transition(new_state, valid_states, &primary,
1072 explicit) != 0)
1073 return -EINVAL;
1074
1075 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1076 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1077 lu_gp = local_lu_gp_mem->lu_gp;
1078 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1079 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1080 /*
1081 * For storage objects that are members of the 'default_lu_gp',
1082 * we only do transition on the passed *l_tp_pt_gp, and not
1083 * on all of the matching target port groups IDs in default_lu_gp.
1084 */
1085 if (!lu_gp->lu_gp_id) {
1086 /*
1087 * core_alua_do_transition_tg_pt() will always return
1088 * success.
1089 */
1090 l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1091 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1092 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1093 new_state, explicit);
1094 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1095 return rc;
1096 }
1097 /*
1098 * For all other LU groups aside from 'default_lu_gp', walk all of
1099 * the associated storage objects looking for a matching target port
1100 * group ID from the local target port group.
1101 */
1102 spin_lock(&lu_gp->lu_gp_lock);
1103 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1104 lu_gp_mem_list) {
1105
1106 dev = lu_gp_mem->lu_gp_mem_dev;
1107 atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1108 spin_unlock(&lu_gp->lu_gp_lock);
1109
1110 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1111 list_for_each_entry(tg_pt_gp,
1112 &dev->t10_alua.tg_pt_gps_list,
1113 tg_pt_gp_list) {
1114
1115 if (!tg_pt_gp->tg_pt_gp_valid_id)
1116 continue;
1117 /*
1118 * If the target behavior port asymmetric access state
1119 * is changed for any target port group accessible via
1120 * a logical unit within a LU group, the target port
1121 * behavior group asymmetric access states for the same
1122 * target port group accessible via other logical units
1123 * in that LU group will also change.
1124 */
1125 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1126 continue;
1127
1128 if (l_tg_pt_gp == tg_pt_gp) {
1129 tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
1130 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1131 } else {
1132 tg_pt_gp->tg_pt_gp_alua_lun = NULL;
1133 tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
1134 }
1135 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1136 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1137 /*
1138 * core_alua_do_transition_tg_pt() will always return
1139 * success.
1140 */
1141 rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1142 new_state, explicit);
1143
1144 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1145 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
1146 if (rc)
1147 break;
1148 }
1149 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1150
1151 spin_lock(&lu_gp->lu_gp_lock);
1152 atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
1153 }
1154 spin_unlock(&lu_gp->lu_gp_lock);
1155
1156 if (!rc) {
1157 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1158 " Group IDs: %hu %s transition to primary state: %s\n",
1159 config_item_name(&lu_gp->lu_gp_group.cg_item),
1160 l_tg_pt_gp->tg_pt_gp_id,
1161 (explicit) ? "explicit" : "implicit",
1162 core_alua_dump_state(new_state));
1163 }
1164
1165 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
1166 return rc;
1167}
1168
1169static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
1170{
1171 struct se_portal_group *se_tpg = lun->lun_tpg;
1172 unsigned char *md_buf;
1173 char *path;
1174 int len, rc;
1175
1176 mutex_lock(&lun->lun_tg_pt_md_mutex);
1177
1178 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1179 if (!md_buf) {
1180 pr_err("Unable to allocate buf for ALUA metadata\n");
1181 rc = -ENOMEM;
1182 goto out_unlock;
1183 }
1184
1185 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
1186 "alua_tg_pt_status=0x%02x\n",
1187 atomic_read(&lun->lun_tg_pt_secondary_offline),
1188 lun->lun_tg_pt_secondary_stat);
1189
1190 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL) {
1191 path = kasprintf(GFP_KERNEL, "%s/alua/%s/%s+%hu/lun_%llu",
1192 db_root, se_tpg->se_tpg_tfo->fabric_name,
1193 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
1194 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg),
1195 lun->unpacked_lun);
1196 } else {
1197 path = kasprintf(GFP_KERNEL, "%s/alua/%s/%s/lun_%llu",
1198 db_root, se_tpg->se_tpg_tfo->fabric_name,
1199 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg),
1200 lun->unpacked_lun);
1201 }
1202 if (!path) {
1203 rc = -ENOMEM;
1204 goto out_free;
1205 }
1206
1207 rc = core_alua_write_tpg_metadata(path, md_buf, len);
1208 kfree(path);
1209out_free:
1210 kfree(md_buf);
1211out_unlock:
1212 mutex_unlock(&lun->lun_tg_pt_md_mutex);
1213 return rc;
1214}
1215
1216static int core_alua_set_tg_pt_secondary_state(
1217 struct se_lun *lun,
1218 int explicit,
1219 int offline)
1220{
1221 struct t10_alua_tg_pt_gp *tg_pt_gp;
1222 int trans_delay_msecs;
1223
1224 rcu_read_lock();
1225 tg_pt_gp = rcu_dereference(lun->lun_tg_pt_gp);
1226 if (!tg_pt_gp) {
1227 rcu_read_unlock();
1228 pr_err("Unable to complete secondary state"
1229 " transition\n");
1230 return -EINVAL;
1231 }
1232 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1233 /*
1234 * Set the secondary ALUA target port access state to OFFLINE
1235 * or release the previously secondary state for struct se_lun
1236 */
1237 if (offline)
1238 atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
1239 else
1240 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
1241
1242 lun->lun_tg_pt_secondary_stat = (explicit) ?
1243 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1244 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1245
1246 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1247 " to secondary access state: %s\n", (explicit) ? "explicit" :
1248 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1249 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1250
1251 rcu_read_unlock();
1252 /*
1253 * Do the optional transition delay after we set the secondary
1254 * ALUA access state.
1255 */
1256 if (trans_delay_msecs != 0)
1257 msleep_interruptible(trans_delay_msecs);
1258 /*
1259 * See if we need to update the ALUA fabric port metadata for
1260 * secondary state and status
1261 */
1262 if (lun->lun_tg_pt_secondary_write_md)
1263 core_alua_update_tpg_secondary_metadata(lun);
1264
1265 return 0;
1266}
1267
1268struct t10_alua_lba_map *
1269core_alua_allocate_lba_map(struct list_head *list,
1270 u64 first_lba, u64 last_lba)
1271{
1272 struct t10_alua_lba_map *lba_map;
1273
1274 lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1275 if (!lba_map) {
1276 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1277 return ERR_PTR(-ENOMEM);
1278 }
1279 INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1280 lba_map->lba_map_first_lba = first_lba;
1281 lba_map->lba_map_last_lba = last_lba;
1282
1283 list_add_tail(&lba_map->lba_map_list, list);
1284 return lba_map;
1285}
1286
1287int
1288core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1289 int pg_id, int state)
1290{
1291 struct t10_alua_lba_map_member *lba_map_mem;
1292
1293 list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1294 lba_map_mem_list) {
1295 if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1296 pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1297 return -EINVAL;
1298 }
1299 }
1300
1301 lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1302 if (!lba_map_mem) {
1303 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1304 return -ENOMEM;
1305 }
1306 lba_map_mem->lba_map_mem_alua_state = state;
1307 lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1308
1309 list_add_tail(&lba_map_mem->lba_map_mem_list,
1310 &lba_map->lba_map_mem_list);
1311 return 0;
1312}
1313
1314void
1315core_alua_free_lba_map(struct list_head *lba_list)
1316{
1317 struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1318 struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1319
1320 list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1321 lba_map_list) {
1322 list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1323 &lba_map->lba_map_mem_list,
1324 lba_map_mem_list) {
1325 list_del(&lba_map_mem->lba_map_mem_list);
1326 kmem_cache_free(t10_alua_lba_map_mem_cache,
1327 lba_map_mem);
1328 }
1329 list_del(&lba_map->lba_map_list);
1330 kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1331 }
1332}
1333
1334void
1335core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1336 int segment_size, int segment_mult)
1337{
1338 struct list_head old_lba_map_list;
1339 struct t10_alua_tg_pt_gp *tg_pt_gp;
1340 int activate = 0, supported;
1341
1342 INIT_LIST_HEAD(&old_lba_map_list);
1343 spin_lock(&dev->t10_alua.lba_map_lock);
1344 dev->t10_alua.lba_map_segment_size = segment_size;
1345 dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1346 list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1347 if (lba_map_list) {
1348 list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1349 activate = 1;
1350 }
1351 spin_unlock(&dev->t10_alua.lba_map_lock);
1352 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1353 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1354 tg_pt_gp_list) {
1355
1356 if (!tg_pt_gp->tg_pt_gp_valid_id)
1357 continue;
1358 supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1359 if (activate)
1360 supported |= ALUA_LBD_SUP;
1361 else
1362 supported &= ~ALUA_LBD_SUP;
1363 tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1364 }
1365 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1366 core_alua_free_lba_map(&old_lba_map_list);
1367}
1368
1369struct t10_alua_lu_gp *
1370core_alua_allocate_lu_gp(const char *name, int def_group)
1371{
1372 struct t10_alua_lu_gp *lu_gp;
1373
1374 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1375 if (!lu_gp) {
1376 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1377 return ERR_PTR(-ENOMEM);
1378 }
1379 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1380 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1381 spin_lock_init(&lu_gp->lu_gp_lock);
1382 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1383
1384 if (def_group) {
1385 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1386 lu_gp->lu_gp_valid_id = 1;
1387 alua_lu_gps_count++;
1388 }
1389
1390 return lu_gp;
1391}
1392
1393int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1394{
1395 struct t10_alua_lu_gp *lu_gp_tmp;
1396 u16 lu_gp_id_tmp;
1397 /*
1398 * The lu_gp->lu_gp_id may only be set once..
1399 */
1400 if (lu_gp->lu_gp_valid_id) {
1401 pr_warn("ALUA LU Group already has a valid ID,"
1402 " ignoring request\n");
1403 return -EINVAL;
1404 }
1405
1406 spin_lock(&lu_gps_lock);
1407 if (alua_lu_gps_count == 0x0000ffff) {
1408 pr_err("Maximum ALUA alua_lu_gps_count:"
1409 " 0x0000ffff reached\n");
1410 spin_unlock(&lu_gps_lock);
1411 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1412 return -ENOSPC;
1413 }
1414again:
1415 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1416 alua_lu_gps_counter++;
1417
1418 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1419 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1420 if (!lu_gp_id)
1421 goto again;
1422
1423 pr_warn("ALUA Logical Unit Group ID: %hu"
1424 " already exists, ignoring request\n",
1425 lu_gp_id);
1426 spin_unlock(&lu_gps_lock);
1427 return -EINVAL;
1428 }
1429 }
1430
1431 lu_gp->lu_gp_id = lu_gp_id_tmp;
1432 lu_gp->lu_gp_valid_id = 1;
1433 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1434 alua_lu_gps_count++;
1435 spin_unlock(&lu_gps_lock);
1436
1437 return 0;
1438}
1439
1440static struct t10_alua_lu_gp_member *
1441core_alua_allocate_lu_gp_mem(struct se_device *dev)
1442{
1443 struct t10_alua_lu_gp_member *lu_gp_mem;
1444
1445 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1446 if (!lu_gp_mem) {
1447 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1448 return ERR_PTR(-ENOMEM);
1449 }
1450 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1451 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1452 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1453
1454 lu_gp_mem->lu_gp_mem_dev = dev;
1455 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1456
1457 return lu_gp_mem;
1458}
1459
1460void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1461{
1462 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1463 /*
1464 * Once we have reached this point, config_item_put() has
1465 * already been called from target_core_alua_drop_lu_gp().
1466 *
1467 * Here, we remove the *lu_gp from the global list so that
1468 * no associations can be made while we are releasing
1469 * struct t10_alua_lu_gp.
1470 */
1471 spin_lock(&lu_gps_lock);
1472 list_del(&lu_gp->lu_gp_node);
1473 alua_lu_gps_count--;
1474 spin_unlock(&lu_gps_lock);
1475 /*
1476 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1477 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1478 * released with core_alua_put_lu_gp_from_name()
1479 */
1480 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1481 cpu_relax();
1482 /*
1483 * Release reference to struct t10_alua_lu_gp * from all associated
1484 * struct se_device.
1485 */
1486 spin_lock(&lu_gp->lu_gp_lock);
1487 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1488 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1489 if (lu_gp_mem->lu_gp_assoc) {
1490 list_del(&lu_gp_mem->lu_gp_mem_list);
1491 lu_gp->lu_gp_members--;
1492 lu_gp_mem->lu_gp_assoc = 0;
1493 }
1494 spin_unlock(&lu_gp->lu_gp_lock);
1495 /*
1496 *
1497 * lu_gp_mem is associated with a single
1498 * struct se_device->dev_alua_lu_gp_mem, and is released when
1499 * struct se_device is released via core_alua_free_lu_gp_mem().
1500 *
1501 * If the passed lu_gp does NOT match the default_lu_gp, assume
1502 * we want to re-associate a given lu_gp_mem with default_lu_gp.
1503 */
1504 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1505 if (lu_gp != default_lu_gp)
1506 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1507 default_lu_gp);
1508 else
1509 lu_gp_mem->lu_gp = NULL;
1510 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1511
1512 spin_lock(&lu_gp->lu_gp_lock);
1513 }
1514 spin_unlock(&lu_gp->lu_gp_lock);
1515
1516 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1517}
1518
1519void core_alua_free_lu_gp_mem(struct se_device *dev)
1520{
1521 struct t10_alua_lu_gp *lu_gp;
1522 struct t10_alua_lu_gp_member *lu_gp_mem;
1523
1524 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1525 if (!lu_gp_mem)
1526 return;
1527
1528 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1529 cpu_relax();
1530
1531 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1532 lu_gp = lu_gp_mem->lu_gp;
1533 if (lu_gp) {
1534 spin_lock(&lu_gp->lu_gp_lock);
1535 if (lu_gp_mem->lu_gp_assoc) {
1536 list_del(&lu_gp_mem->lu_gp_mem_list);
1537 lu_gp->lu_gp_members--;
1538 lu_gp_mem->lu_gp_assoc = 0;
1539 }
1540 spin_unlock(&lu_gp->lu_gp_lock);
1541 lu_gp_mem->lu_gp = NULL;
1542 }
1543 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1544
1545 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1546}
1547
1548struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1549{
1550 struct t10_alua_lu_gp *lu_gp;
1551 struct config_item *ci;
1552
1553 spin_lock(&lu_gps_lock);
1554 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1555 if (!lu_gp->lu_gp_valid_id)
1556 continue;
1557 ci = &lu_gp->lu_gp_group.cg_item;
1558 if (!strcmp(config_item_name(ci), name)) {
1559 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1560 spin_unlock(&lu_gps_lock);
1561 return lu_gp;
1562 }
1563 }
1564 spin_unlock(&lu_gps_lock);
1565
1566 return NULL;
1567}
1568
1569void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1570{
1571 spin_lock(&lu_gps_lock);
1572 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1573 spin_unlock(&lu_gps_lock);
1574}
1575
1576/*
1577 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1578 */
1579void __core_alua_attach_lu_gp_mem(
1580 struct t10_alua_lu_gp_member *lu_gp_mem,
1581 struct t10_alua_lu_gp *lu_gp)
1582{
1583 spin_lock(&lu_gp->lu_gp_lock);
1584 lu_gp_mem->lu_gp = lu_gp;
1585 lu_gp_mem->lu_gp_assoc = 1;
1586 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1587 lu_gp->lu_gp_members++;
1588 spin_unlock(&lu_gp->lu_gp_lock);
1589}
1590
1591/*
1592 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1593 */
1594void __core_alua_drop_lu_gp_mem(
1595 struct t10_alua_lu_gp_member *lu_gp_mem,
1596 struct t10_alua_lu_gp *lu_gp)
1597{
1598 spin_lock(&lu_gp->lu_gp_lock);
1599 list_del(&lu_gp_mem->lu_gp_mem_list);
1600 lu_gp_mem->lu_gp = NULL;
1601 lu_gp_mem->lu_gp_assoc = 0;
1602 lu_gp->lu_gp_members--;
1603 spin_unlock(&lu_gp->lu_gp_lock);
1604}
1605
1606struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1607 const char *name, int def_group)
1608{
1609 struct t10_alua_tg_pt_gp *tg_pt_gp;
1610
1611 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1612 if (!tg_pt_gp) {
1613 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1614 return NULL;
1615 }
1616 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1617 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
1618 mutex_init(&tg_pt_gp->tg_pt_gp_transition_mutex);
1619 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1620 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1621 tg_pt_gp->tg_pt_gp_dev = dev;
1622 tg_pt_gp->tg_pt_gp_alua_access_state =
1623 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED;
1624 /*
1625 * Enable both explicit and implicit ALUA support by default
1626 */
1627 tg_pt_gp->tg_pt_gp_alua_access_type =
1628 TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
1629 /*
1630 * Set the default Active/NonOptimized Delay in milliseconds
1631 */
1632 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1633 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1634 tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
1635
1636 /*
1637 * Enable all supported states
1638 */
1639 tg_pt_gp->tg_pt_gp_alua_supported_states =
1640 ALUA_T_SUP | ALUA_O_SUP |
1641 ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1642
1643 if (def_group) {
1644 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1645 tg_pt_gp->tg_pt_gp_id =
1646 dev->t10_alua.alua_tg_pt_gps_counter++;
1647 tg_pt_gp->tg_pt_gp_valid_id = 1;
1648 dev->t10_alua.alua_tg_pt_gps_count++;
1649 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1650 &dev->t10_alua.tg_pt_gps_list);
1651 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1652 }
1653
1654 return tg_pt_gp;
1655}
1656
1657int core_alua_set_tg_pt_gp_id(
1658 struct t10_alua_tg_pt_gp *tg_pt_gp,
1659 u16 tg_pt_gp_id)
1660{
1661 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1662 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1663 u16 tg_pt_gp_id_tmp;
1664
1665 /*
1666 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1667 */
1668 if (tg_pt_gp->tg_pt_gp_valid_id) {
1669 pr_warn("ALUA TG PT Group already has a valid ID,"
1670 " ignoring request\n");
1671 return -EINVAL;
1672 }
1673
1674 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1675 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1676 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1677 " 0x0000ffff reached\n");
1678 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1679 return -ENOSPC;
1680 }
1681again:
1682 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1683 dev->t10_alua.alua_tg_pt_gps_counter++;
1684
1685 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1686 tg_pt_gp_list) {
1687 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1688 if (!tg_pt_gp_id)
1689 goto again;
1690
1691 pr_err("ALUA Target Port Group ID: %hu already"
1692 " exists, ignoring request\n", tg_pt_gp_id);
1693 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1694 return -EINVAL;
1695 }
1696 }
1697
1698 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1699 tg_pt_gp->tg_pt_gp_valid_id = 1;
1700 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1701 &dev->t10_alua.tg_pt_gps_list);
1702 dev->t10_alua.alua_tg_pt_gps_count++;
1703 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1704
1705 return 0;
1706}
1707
1708void core_alua_free_tg_pt_gp(
1709 struct t10_alua_tg_pt_gp *tg_pt_gp)
1710{
1711 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1712 struct se_lun *lun, *next;
1713
1714 /*
1715 * Once we have reached this point, config_item_put() has already
1716 * been called from target_core_alua_drop_tg_pt_gp().
1717 *
1718 * Here we remove *tg_pt_gp from the global list so that
1719 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
1720 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1721 */
1722 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1723 if (tg_pt_gp->tg_pt_gp_valid_id) {
1724 list_del(&tg_pt_gp->tg_pt_gp_list);
1725 dev->t10_alua.alua_tg_pt_gps_count--;
1726 }
1727 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1728
1729 /*
1730 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1731 * core_alua_get_tg_pt_gp_by_name() in
1732 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1733 * to be released with core_alua_put_tg_pt_gp_from_name().
1734 */
1735 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1736 cpu_relax();
1737
1738 /*
1739 * Release reference to struct t10_alua_tg_pt_gp from all associated
1740 * struct se_port.
1741 */
1742 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1743 list_for_each_entry_safe(lun, next,
1744 &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1745 list_del_init(&lun->lun_tg_pt_gp_link);
1746 tg_pt_gp->tg_pt_gp_members--;
1747
1748 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1749 /*
1750 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1751 * assume we want to re-associate a given tg_pt_gp_mem with
1752 * default_tg_pt_gp.
1753 */
1754 spin_lock(&lun->lun_tg_pt_gp_lock);
1755 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1756 __target_attach_tg_pt_gp(lun,
1757 dev->t10_alua.default_tg_pt_gp);
1758 } else
1759 rcu_assign_pointer(lun->lun_tg_pt_gp, NULL);
1760 spin_unlock(&lun->lun_tg_pt_gp_lock);
1761
1762 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1763 }
1764 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1765
1766 synchronize_rcu();
1767 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1768}
1769
1770static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1771 struct se_device *dev, const char *name)
1772{
1773 struct t10_alua_tg_pt_gp *tg_pt_gp;
1774 struct config_item *ci;
1775
1776 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1777 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1778 tg_pt_gp_list) {
1779 if (!tg_pt_gp->tg_pt_gp_valid_id)
1780 continue;
1781 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1782 if (!strcmp(config_item_name(ci), name)) {
1783 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1784 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1785 return tg_pt_gp;
1786 }
1787 }
1788 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1789
1790 return NULL;
1791}
1792
1793static void core_alua_put_tg_pt_gp_from_name(
1794 struct t10_alua_tg_pt_gp *tg_pt_gp)
1795{
1796 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1797
1798 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1799 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1800 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1801}
1802
1803static void __target_attach_tg_pt_gp(struct se_lun *lun,
1804 struct t10_alua_tg_pt_gp *tg_pt_gp)
1805{
1806 struct se_dev_entry *se_deve;
1807
1808 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1809
1810 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1811 rcu_assign_pointer(lun->lun_tg_pt_gp, tg_pt_gp);
1812 list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
1813 tg_pt_gp->tg_pt_gp_members++;
1814 spin_lock(&lun->lun_deve_lock);
1815 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
1816 core_scsi3_ua_allocate(se_deve, 0x3f,
1817 ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED);
1818 spin_unlock(&lun->lun_deve_lock);
1819 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1820}
1821
1822void target_attach_tg_pt_gp(struct se_lun *lun,
1823 struct t10_alua_tg_pt_gp *tg_pt_gp)
1824{
1825 spin_lock(&lun->lun_tg_pt_gp_lock);
1826 __target_attach_tg_pt_gp(lun, tg_pt_gp);
1827 spin_unlock(&lun->lun_tg_pt_gp_lock);
1828 synchronize_rcu();
1829}
1830
1831static void __target_detach_tg_pt_gp(struct se_lun *lun,
1832 struct t10_alua_tg_pt_gp *tg_pt_gp)
1833{
1834 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1835
1836 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1837 list_del_init(&lun->lun_tg_pt_gp_link);
1838 tg_pt_gp->tg_pt_gp_members--;
1839 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1840}
1841
1842void target_detach_tg_pt_gp(struct se_lun *lun)
1843{
1844 struct t10_alua_tg_pt_gp *tg_pt_gp;
1845
1846 spin_lock(&lun->lun_tg_pt_gp_lock);
1847 tg_pt_gp = rcu_dereference_check(lun->lun_tg_pt_gp,
1848 lockdep_is_held(&lun->lun_tg_pt_gp_lock));
1849 if (tg_pt_gp) {
1850 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1851 rcu_assign_pointer(lun->lun_tg_pt_gp, NULL);
1852 }
1853 spin_unlock(&lun->lun_tg_pt_gp_lock);
1854 synchronize_rcu();
1855}
1856
1857static void target_swap_tg_pt_gp(struct se_lun *lun,
1858 struct t10_alua_tg_pt_gp *old_tg_pt_gp,
1859 struct t10_alua_tg_pt_gp *new_tg_pt_gp)
1860{
1861 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1862
1863 if (old_tg_pt_gp)
1864 __target_detach_tg_pt_gp(lun, old_tg_pt_gp);
1865 __target_attach_tg_pt_gp(lun, new_tg_pt_gp);
1866}
1867
1868ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
1869{
1870 struct config_item *tg_pt_ci;
1871 struct t10_alua_tg_pt_gp *tg_pt_gp;
1872 ssize_t len = 0;
1873
1874 rcu_read_lock();
1875 tg_pt_gp = rcu_dereference(lun->lun_tg_pt_gp);
1876 if (tg_pt_gp) {
1877 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1878 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1879 " %hu\nTG Port Primary Access State: %s\nTG Port "
1880 "Primary Access Status: %s\nTG Port Secondary Access"
1881 " State: %s\nTG Port Secondary Access Status: %s\n",
1882 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1883 core_alua_dump_state(
1884 tg_pt_gp->tg_pt_gp_alua_access_state),
1885 core_alua_dump_status(
1886 tg_pt_gp->tg_pt_gp_alua_access_status),
1887 atomic_read(&lun->lun_tg_pt_secondary_offline) ?
1888 "Offline" : "None",
1889 core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
1890 }
1891 rcu_read_unlock();
1892
1893 return len;
1894}
1895
1896ssize_t core_alua_store_tg_pt_gp_info(
1897 struct se_lun *lun,
1898 const char *page,
1899 size_t count)
1900{
1901 struct se_portal_group *tpg = lun->lun_tpg;
1902 /*
1903 * rcu_dereference_raw protected by se_lun->lun_group symlink
1904 * reference to se_device->dev_group.
1905 */
1906 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
1907 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1908 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1909 int move = 0;
1910
1911 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
1912 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1913 return -ENODEV;
1914
1915 if (count > TG_PT_GROUP_NAME_BUF) {
1916 pr_err("ALUA Target Port Group alias too large!\n");
1917 return -EINVAL;
1918 }
1919 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1920 memcpy(buf, page, count);
1921 /*
1922 * Any ALUA target port group alias besides "NULL" means we will be
1923 * making a new group association.
1924 */
1925 if (strcmp(strstrip(buf), "NULL")) {
1926 /*
1927 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1928 * struct t10_alua_tg_pt_gp. This reference is released with
1929 * core_alua_put_tg_pt_gp_from_name() below.
1930 */
1931 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
1932 strstrip(buf));
1933 if (!tg_pt_gp_new)
1934 return -ENODEV;
1935 }
1936
1937 spin_lock(&lun->lun_tg_pt_gp_lock);
1938 tg_pt_gp = rcu_dereference_check(lun->lun_tg_pt_gp,
1939 lockdep_is_held(&lun->lun_tg_pt_gp_lock));
1940 if (tg_pt_gp) {
1941 /*
1942 * Clearing an existing tg_pt_gp association, and replacing
1943 * with the default_tg_pt_gp.
1944 */
1945 if (!tg_pt_gp_new) {
1946 pr_debug("Target_Core_ConfigFS: Moving"
1947 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1948 " alua/%s, ID: %hu back to"
1949 " default_tg_pt_gp\n",
1950 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1951 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1952 config_item_name(&lun->lun_group.cg_item),
1953 config_item_name(
1954 &tg_pt_gp->tg_pt_gp_group.cg_item),
1955 tg_pt_gp->tg_pt_gp_id);
1956
1957 target_swap_tg_pt_gp(lun, tg_pt_gp,
1958 dev->t10_alua.default_tg_pt_gp);
1959 spin_unlock(&lun->lun_tg_pt_gp_lock);
1960
1961 goto sync_rcu;
1962 }
1963 move = 1;
1964 }
1965
1966 target_swap_tg_pt_gp(lun, tg_pt_gp, tg_pt_gp_new);
1967 spin_unlock(&lun->lun_tg_pt_gp_lock);
1968 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1969 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1970 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1971 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1972 config_item_name(&lun->lun_group.cg_item),
1973 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1974 tg_pt_gp_new->tg_pt_gp_id);
1975
1976 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1977sync_rcu:
1978 synchronize_rcu();
1979 return count;
1980}
1981
1982ssize_t core_alua_show_access_type(
1983 struct t10_alua_tg_pt_gp *tg_pt_gp,
1984 char *page)
1985{
1986 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
1987 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
1988 return sprintf(page, "Implicit and Explicit\n");
1989 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
1990 return sprintf(page, "Implicit\n");
1991 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
1992 return sprintf(page, "Explicit\n");
1993 else
1994 return sprintf(page, "None\n");
1995}
1996
1997ssize_t core_alua_store_access_type(
1998 struct t10_alua_tg_pt_gp *tg_pt_gp,
1999 const char *page,
2000 size_t count)
2001{
2002 unsigned long tmp;
2003 int ret;
2004
2005 ret = kstrtoul(page, 0, &tmp);
2006 if (ret < 0) {
2007 pr_err("Unable to extract alua_access_type\n");
2008 return ret;
2009 }
2010 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
2011 pr_err("Illegal value for alua_access_type:"
2012 " %lu\n", tmp);
2013 return -EINVAL;
2014 }
2015 if (tmp == 3)
2016 tg_pt_gp->tg_pt_gp_alua_access_type =
2017 TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
2018 else if (tmp == 2)
2019 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
2020 else if (tmp == 1)
2021 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
2022 else
2023 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2024
2025 return count;
2026}
2027
2028ssize_t core_alua_show_nonop_delay_msecs(
2029 struct t10_alua_tg_pt_gp *tg_pt_gp,
2030 char *page)
2031{
2032 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2033}
2034
2035ssize_t core_alua_store_nonop_delay_msecs(
2036 struct t10_alua_tg_pt_gp *tg_pt_gp,
2037 const char *page,
2038 size_t count)
2039{
2040 unsigned long tmp;
2041 int ret;
2042
2043 ret = kstrtoul(page, 0, &tmp);
2044 if (ret < 0) {
2045 pr_err("Unable to extract nonop_delay_msecs\n");
2046 return ret;
2047 }
2048 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
2049 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
2050 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2051 ALUA_MAX_NONOP_DELAY_MSECS);
2052 return -EINVAL;
2053 }
2054 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2055
2056 return count;
2057}
2058
2059ssize_t core_alua_show_trans_delay_msecs(
2060 struct t10_alua_tg_pt_gp *tg_pt_gp,
2061 char *page)
2062{
2063 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2064}
2065
2066ssize_t core_alua_store_trans_delay_msecs(
2067 struct t10_alua_tg_pt_gp *tg_pt_gp,
2068 const char *page,
2069 size_t count)
2070{
2071 unsigned long tmp;
2072 int ret;
2073
2074 ret = kstrtoul(page, 0, &tmp);
2075 if (ret < 0) {
2076 pr_err("Unable to extract trans_delay_msecs\n");
2077 return ret;
2078 }
2079 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
2080 pr_err("Passed trans_delay_msecs: %lu, exceeds"
2081 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2082 ALUA_MAX_TRANS_DELAY_MSECS);
2083 return -EINVAL;
2084 }
2085 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2086
2087 return count;
2088}
2089
2090ssize_t core_alua_show_implicit_trans_secs(
2091 struct t10_alua_tg_pt_gp *tg_pt_gp,
2092 char *page)
2093{
2094 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
2095}
2096
2097ssize_t core_alua_store_implicit_trans_secs(
2098 struct t10_alua_tg_pt_gp *tg_pt_gp,
2099 const char *page,
2100 size_t count)
2101{
2102 unsigned long tmp;
2103 int ret;
2104
2105 ret = kstrtoul(page, 0, &tmp);
2106 if (ret < 0) {
2107 pr_err("Unable to extract implicit_trans_secs\n");
2108 return ret;
2109 }
2110 if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2111 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2112 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2113 ALUA_MAX_IMPLICIT_TRANS_SECS);
2114 return -EINVAL;
2115 }
2116 tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
2117
2118 return count;
2119}
2120
2121ssize_t core_alua_show_preferred_bit(
2122 struct t10_alua_tg_pt_gp *tg_pt_gp,
2123 char *page)
2124{
2125 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2126}
2127
2128ssize_t core_alua_store_preferred_bit(
2129 struct t10_alua_tg_pt_gp *tg_pt_gp,
2130 const char *page,
2131 size_t count)
2132{
2133 unsigned long tmp;
2134 int ret;
2135
2136 ret = kstrtoul(page, 0, &tmp);
2137 if (ret < 0) {
2138 pr_err("Unable to extract preferred ALUA value\n");
2139 return ret;
2140 }
2141 if ((tmp != 0) && (tmp != 1)) {
2142 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
2143 return -EINVAL;
2144 }
2145 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2146
2147 return count;
2148}
2149
2150ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2151{
2152 return sprintf(page, "%d\n",
2153 atomic_read(&lun->lun_tg_pt_secondary_offline));
2154}
2155
2156ssize_t core_alua_store_offline_bit(
2157 struct se_lun *lun,
2158 const char *page,
2159 size_t count)
2160{
2161 /*
2162 * rcu_dereference_raw protected by se_lun->lun_group symlink
2163 * reference to se_device->dev_group.
2164 */
2165 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
2166 unsigned long tmp;
2167 int ret;
2168
2169 if (dev->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
2170 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
2171 return -ENODEV;
2172
2173 ret = kstrtoul(page, 0, &tmp);
2174 if (ret < 0) {
2175 pr_err("Unable to extract alua_tg_pt_offline value\n");
2176 return ret;
2177 }
2178 if ((tmp != 0) && (tmp != 1)) {
2179 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
2180 tmp);
2181 return -EINVAL;
2182 }
2183
2184 ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
2185 if (ret < 0)
2186 return -EINVAL;
2187
2188 return count;
2189}
2190
2191ssize_t core_alua_show_secondary_status(
2192 struct se_lun *lun,
2193 char *page)
2194{
2195 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
2196}
2197
2198ssize_t core_alua_store_secondary_status(
2199 struct se_lun *lun,
2200 const char *page,
2201 size_t count)
2202{
2203 unsigned long tmp;
2204 int ret;
2205
2206 ret = kstrtoul(page, 0, &tmp);
2207 if (ret < 0) {
2208 pr_err("Unable to extract alua_tg_pt_status\n");
2209 return ret;
2210 }
2211 if ((tmp != ALUA_STATUS_NONE) &&
2212 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2213 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
2214 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
2215 tmp);
2216 return -EINVAL;
2217 }
2218 lun->lun_tg_pt_secondary_stat = (int)tmp;
2219
2220 return count;
2221}
2222
2223ssize_t core_alua_show_secondary_write_metadata(
2224 struct se_lun *lun,
2225 char *page)
2226{
2227 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
2228}
2229
2230ssize_t core_alua_store_secondary_write_metadata(
2231 struct se_lun *lun,
2232 const char *page,
2233 size_t count)
2234{
2235 unsigned long tmp;
2236 int ret;
2237
2238 ret = kstrtoul(page, 0, &tmp);
2239 if (ret < 0) {
2240 pr_err("Unable to extract alua_tg_pt_write_md\n");
2241 return ret;
2242 }
2243 if ((tmp != 0) && (tmp != 1)) {
2244 pr_err("Illegal value for alua_tg_pt_write_md:"
2245 " %lu\n", tmp);
2246 return -EINVAL;
2247 }
2248 lun->lun_tg_pt_secondary_write_md = (int)tmp;
2249
2250 return count;
2251}
2252
2253int core_setup_alua(struct se_device *dev)
2254{
2255 if (!(dev->transport_flags &
2256 TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
2257 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2258 struct t10_alua_lu_gp_member *lu_gp_mem;
2259
2260 /*
2261 * Associate this struct se_device with the default ALUA
2262 * LUN Group.
2263 */
2264 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2265 if (IS_ERR(lu_gp_mem))
2266 return PTR_ERR(lu_gp_mem);
2267
2268 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2269 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2270 default_lu_gp);
2271 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2272
2273 pr_debug("%s: Adding to default ALUA LU Group:"
2274 " core/alua/lu_gps/default_lu_gp\n",
2275 dev->transport->name);
2276 }
2277
2278 return 0;
2279}