Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-btrfs-devel.git] / drivers / target / target_core_alua.c
blob8f4447749c7141f245137cddca5bc44ca4d9da48
1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * Copyright (c) 2009-2010 Rising Tide Systems
7 * Copyright (c) 2009-2010 Linux-iSCSI.org
9 * Nicholas A. Bellinger <nab@kernel.org>
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.
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.
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.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/configfs.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_cmnd.h>
33 #include <target/target_core_base.h>
34 #include <target/target_core_device.h>
35 #include <target/target_core_transport.h>
36 #include <target/target_core_fabric_ops.h>
37 #include <target/target_core_configfs.h>
39 #include "target_core_alua.h"
40 #include "target_core_hba.h"
41 #include "target_core_ua.h"
43 static int core_alua_check_transition(int state, int *primary);
44 static int core_alua_set_tg_pt_secondary_state(
45 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
46 struct se_port *port, int explict, int offline);
48 static u16 alua_lu_gps_counter;
49 static u32 alua_lu_gps_count;
51 static DEFINE_SPINLOCK(lu_gps_lock);
52 static LIST_HEAD(lu_gps_list);
54 struct t10_alua_lu_gp *default_lu_gp;
57 * REPORT_TARGET_PORT_GROUPS
59 * See spc4r17 section 6.27
61 int core_emulate_report_target_port_groups(struct se_cmd *cmd)
63 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
64 struct se_port *port;
65 struct t10_alua_tg_pt_gp *tg_pt_gp;
66 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
67 unsigned char *buf;
68 u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
69 Target port group descriptor */
71 * Need at least 4 bytes of response data or else we can't
72 * even fit the return data length.
74 if (cmd->data_length < 4) {
75 pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
76 " too small\n", cmd->data_length);
77 return -EINVAL;
80 buf = transport_kmap_first_data_page(cmd);
82 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
83 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
84 tg_pt_gp_list) {
86 * Check if the Target port group and Target port descriptor list
87 * based on tg_pt_gp_members count will fit into the response payload.
88 * Otherwise, bump rd_len to let the initiator know we have exceeded
89 * the allocation length and the response is truncated.
91 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
92 cmd->data_length) {
93 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
94 continue;
97 * PREF: Preferred target port bit, determine if this
98 * bit should be set for port group.
100 if (tg_pt_gp->tg_pt_gp_pref)
101 buf[off] = 0x80;
103 * Set the ASYMMETRIC ACCESS State
105 buf[off++] |= (atomic_read(
106 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
108 * Set supported ASYMMETRIC ACCESS State bits
110 buf[off] = 0x80; /* T_SUP */
111 buf[off] |= 0x40; /* O_SUP */
112 buf[off] |= 0x8; /* U_SUP */
113 buf[off] |= 0x4; /* S_SUP */
114 buf[off] |= 0x2; /* AN_SUP */
115 buf[off++] |= 0x1; /* AO_SUP */
117 * TARGET PORT GROUP
119 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
120 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
122 off++; /* Skip over Reserved */
124 * STATUS CODE
126 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
128 * Vendor Specific field
130 buf[off++] = 0x00;
132 * TARGET PORT COUNT
134 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
135 rd_len += 8;
137 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
138 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
139 tg_pt_gp_mem_list) {
140 port = tg_pt_gp_mem->tg_pt;
142 * Start Target Port descriptor format
144 * See spc4r17 section 6.2.7 Table 247
146 off += 2; /* Skip over Obsolete */
148 * Set RELATIVE TARGET PORT IDENTIFIER
150 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
151 buf[off++] = (port->sep_rtpi & 0xff);
152 rd_len += 4;
154 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
156 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
158 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
160 buf[0] = ((rd_len >> 24) & 0xff);
161 buf[1] = ((rd_len >> 16) & 0xff);
162 buf[2] = ((rd_len >> 8) & 0xff);
163 buf[3] = (rd_len & 0xff);
165 transport_kunmap_first_data_page(cmd);
167 return 0;
171 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
173 * See spc4r17 section 6.35
175 int core_emulate_set_target_port_groups(struct se_cmd *cmd)
177 struct se_device *dev = cmd->se_dev;
178 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
179 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
180 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
181 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
182 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
183 unsigned char *buf;
184 unsigned char *ptr;
185 u32 len = 4; /* Skip over RESERVED area in header */
186 int alua_access_state, primary = 0, rc;
187 u16 tg_pt_id, rtpi;
189 if (!l_port)
190 return PYX_TRANSPORT_LU_COMM_FAILURE;
192 buf = transport_kmap_first_data_page(cmd);
195 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
196 * for the local tg_pt_gp.
198 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
199 if (!l_tg_pt_gp_mem) {
200 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
201 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
202 goto out;
204 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
205 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
206 if (!l_tg_pt_gp) {
207 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
208 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
209 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
210 goto out;
212 rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
213 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
215 if (!rc) {
216 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
217 " while TPGS_EXPLICT_ALUA is disabled\n");
218 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
219 goto out;
222 ptr = &buf[4]; /* Skip over RESERVED area in header */
224 while (len < cmd->data_length) {
225 alua_access_state = (ptr[0] & 0x0f);
227 * Check the received ALUA access state, and determine if
228 * the state is a primary or secondary target port asymmetric
229 * access state.
231 rc = core_alua_check_transition(alua_access_state, &primary);
232 if (rc != 0) {
234 * If the SET TARGET PORT GROUPS attempts to establish
235 * an invalid combination of target port asymmetric
236 * access states or attempts to establish an
237 * unsupported target port asymmetric access state,
238 * then the command shall be terminated with CHECK
239 * CONDITION status, with the sense key set to ILLEGAL
240 * REQUEST, and the additional sense code set to INVALID
241 * FIELD IN PARAMETER LIST.
243 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
244 goto out;
246 rc = -1;
248 * If the ASYMMETRIC ACCESS STATE field (see table 267)
249 * specifies a primary target port asymmetric access state,
250 * then the TARGET PORT GROUP OR TARGET PORT field specifies
251 * a primary target port group for which the primary target
252 * port asymmetric access state shall be changed. If the
253 * ASYMMETRIC ACCESS STATE field specifies a secondary target
254 * port asymmetric access state, then the TARGET PORT GROUP OR
255 * TARGET PORT field specifies the relative target port
256 * identifier (see 3.1.120) of the target port for which the
257 * secondary target port asymmetric access state shall be
258 * changed.
260 if (primary) {
261 tg_pt_id = ((ptr[2] << 8) & 0xff);
262 tg_pt_id |= (ptr[3] & 0xff);
264 * Locate the matching target port group ID from
265 * the global tg_pt_gp list
267 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
268 list_for_each_entry(tg_pt_gp,
269 &su_dev->t10_alua.tg_pt_gps_list,
270 tg_pt_gp_list) {
271 if (!tg_pt_gp->tg_pt_gp_valid_id)
272 continue;
274 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
275 continue;
277 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
278 smp_mb__after_atomic_inc();
279 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
281 rc = core_alua_do_port_transition(tg_pt_gp,
282 dev, l_port, nacl,
283 alua_access_state, 1);
285 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
286 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
287 smp_mb__after_atomic_dec();
288 break;
290 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
292 * If not matching target port group ID can be located
293 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
295 if (rc != 0) {
296 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
297 goto out;
299 } else {
301 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
302 * the Target Port in question for the the incoming
303 * SET_TARGET_PORT_GROUPS op.
305 rtpi = ((ptr[2] << 8) & 0xff);
306 rtpi |= (ptr[3] & 0xff);
308 * Locate the matching relative target port identifer
309 * for the struct se_device storage object.
311 spin_lock(&dev->se_port_lock);
312 list_for_each_entry(port, &dev->dev_sep_list,
313 sep_list) {
314 if (port->sep_rtpi != rtpi)
315 continue;
317 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
318 spin_unlock(&dev->se_port_lock);
320 rc = core_alua_set_tg_pt_secondary_state(
321 tg_pt_gp_mem, port, 1, 1);
323 spin_lock(&dev->se_port_lock);
324 break;
326 spin_unlock(&dev->se_port_lock);
328 * If not matching relative target port identifier can
329 * be located, throw an exception with ASCQ:
330 * INVALID_PARAMETER_LIST
332 if (rc != 0) {
333 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
334 goto out;
338 ptr += 4;
339 len += 4;
342 out:
343 transport_kunmap_first_data_page(cmd);
345 return 0;
348 static inline int core_alua_state_nonoptimized(
349 struct se_cmd *cmd,
350 unsigned char *cdb,
351 int nonop_delay_msecs,
352 u8 *alua_ascq)
355 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
356 * later to determine if processing of this cmd needs to be
357 * temporarily delayed for the Active/NonOptimized primary access state.
359 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
360 cmd->alua_nonop_delay = nonop_delay_msecs;
361 return 0;
364 static inline int core_alua_state_standby(
365 struct se_cmd *cmd,
366 unsigned char *cdb,
367 u8 *alua_ascq)
370 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
371 * spc4r17 section 5.9.2.4.4
373 switch (cdb[0]) {
374 case INQUIRY:
375 case LOG_SELECT:
376 case LOG_SENSE:
377 case MODE_SELECT:
378 case MODE_SENSE:
379 case REPORT_LUNS:
380 case RECEIVE_DIAGNOSTIC:
381 case SEND_DIAGNOSTIC:
382 case MAINTENANCE_IN:
383 switch (cdb[1]) {
384 case MI_REPORT_TARGET_PGS:
385 return 0;
386 default:
387 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
388 return 1;
390 case MAINTENANCE_OUT:
391 switch (cdb[1]) {
392 case MO_SET_TARGET_PGS:
393 return 0;
394 default:
395 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
396 return 1;
398 case REQUEST_SENSE:
399 case PERSISTENT_RESERVE_IN:
400 case PERSISTENT_RESERVE_OUT:
401 case READ_BUFFER:
402 case WRITE_BUFFER:
403 return 0;
404 default:
405 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
406 return 1;
409 return 0;
412 static inline int core_alua_state_unavailable(
413 struct se_cmd *cmd,
414 unsigned char *cdb,
415 u8 *alua_ascq)
418 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
419 * spc4r17 section 5.9.2.4.5
421 switch (cdb[0]) {
422 case INQUIRY:
423 case REPORT_LUNS:
424 case MAINTENANCE_IN:
425 switch (cdb[1]) {
426 case MI_REPORT_TARGET_PGS:
427 return 0;
428 default:
429 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
430 return 1;
432 case MAINTENANCE_OUT:
433 switch (cdb[1]) {
434 case MO_SET_TARGET_PGS:
435 return 0;
436 default:
437 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
438 return 1;
440 case REQUEST_SENSE:
441 case READ_BUFFER:
442 case WRITE_BUFFER:
443 return 0;
444 default:
445 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
446 return 1;
449 return 0;
452 static inline int core_alua_state_transition(
453 struct se_cmd *cmd,
454 unsigned char *cdb,
455 u8 *alua_ascq)
458 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
459 * spc4r17 section 5.9.2.5
461 switch (cdb[0]) {
462 case INQUIRY:
463 case REPORT_LUNS:
464 case MAINTENANCE_IN:
465 switch (cdb[1]) {
466 case MI_REPORT_TARGET_PGS:
467 return 0;
468 default:
469 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
470 return 1;
472 case REQUEST_SENSE:
473 case READ_BUFFER:
474 case WRITE_BUFFER:
475 return 0;
476 default:
477 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
478 return 1;
481 return 0;
485 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
486 * in transport_cmd_sequencer(). This function is assigned to
487 * struct t10_alua *->state_check() in core_setup_alua()
489 static int core_alua_state_check_nop(
490 struct se_cmd *cmd,
491 unsigned char *cdb,
492 u8 *alua_ascq)
494 return 0;
498 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
499 * This function is assigned to struct t10_alua *->state_check() in
500 * core_setup_alua()
502 * Also, this function can return three different return codes to
503 * signal transport_generic_cmd_sequencer()
505 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
506 * return 0: Used to signal success
507 * reutrn -1: Used to signal failure, and invalid cdb field
509 static int core_alua_state_check(
510 struct se_cmd *cmd,
511 unsigned char *cdb,
512 u8 *alua_ascq)
514 struct se_lun *lun = cmd->se_lun;
515 struct se_port *port = lun->lun_sep;
516 struct t10_alua_tg_pt_gp *tg_pt_gp;
517 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
518 int out_alua_state, nonop_delay_msecs;
520 if (!port)
521 return 0;
523 * First, check for a struct se_port specific secondary ALUA target port
524 * access state: OFFLINE
526 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
527 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
528 pr_debug("ALUA: Got secondary offline status for local"
529 " target port\n");
530 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
531 return 1;
534 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
535 * ALUA target port group, to obtain current ALUA access state.
536 * Otherwise look for the underlying struct se_device association with
537 * a ALUA logical unit group.
539 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
540 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
541 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
542 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
543 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
544 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
546 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
547 * statement so the compiler knows explicitly to check this case first.
548 * For the Optimized ALUA access state case, we want to process the
549 * incoming fabric cmd ASAP..
551 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
552 return 0;
554 switch (out_alua_state) {
555 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
556 return core_alua_state_nonoptimized(cmd, cdb,
557 nonop_delay_msecs, alua_ascq);
558 case ALUA_ACCESS_STATE_STANDBY:
559 return core_alua_state_standby(cmd, cdb, alua_ascq);
560 case ALUA_ACCESS_STATE_UNAVAILABLE:
561 return core_alua_state_unavailable(cmd, cdb, alua_ascq);
562 case ALUA_ACCESS_STATE_TRANSITION:
563 return core_alua_state_transition(cmd, cdb, alua_ascq);
565 * OFFLINE is a secondary ALUA target port group access state, that is
566 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
568 case ALUA_ACCESS_STATE_OFFLINE:
569 default:
570 pr_err("Unknown ALUA access state: 0x%02x\n",
571 out_alua_state);
572 return -EINVAL;
575 return 0;
579 * Check implict and explict ALUA state change request.
581 static int core_alua_check_transition(int state, int *primary)
583 switch (state) {
584 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
585 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
586 case ALUA_ACCESS_STATE_STANDBY:
587 case ALUA_ACCESS_STATE_UNAVAILABLE:
589 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
590 * defined as primary target port asymmetric access states.
592 *primary = 1;
593 break;
594 case ALUA_ACCESS_STATE_OFFLINE:
596 * OFFLINE state is defined as a secondary target port
597 * asymmetric access state.
599 *primary = 0;
600 break;
601 default:
602 pr_err("Unknown ALUA access state: 0x%02x\n", state);
603 return -EINVAL;
606 return 0;
609 static char *core_alua_dump_state(int state)
611 switch (state) {
612 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
613 return "Active/Optimized";
614 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
615 return "Active/NonOptimized";
616 case ALUA_ACCESS_STATE_STANDBY:
617 return "Standby";
618 case ALUA_ACCESS_STATE_UNAVAILABLE:
619 return "Unavailable";
620 case ALUA_ACCESS_STATE_OFFLINE:
621 return "Offline";
622 default:
623 return "Unknown";
626 return NULL;
629 char *core_alua_dump_status(int status)
631 switch (status) {
632 case ALUA_STATUS_NONE:
633 return "None";
634 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
635 return "Altered by Explict STPG";
636 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
637 return "Altered by Implict ALUA";
638 default:
639 return "Unknown";
642 return NULL;
646 * Used by fabric modules to determine when we need to delay processing
647 * for the Active/NonOptimized paths..
649 int core_alua_check_nonop_delay(
650 struct se_cmd *cmd)
652 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
653 return 0;
654 if (in_interrupt())
655 return 0;
657 * The ALUA Active/NonOptimized access state delay can be disabled
658 * in via configfs with a value of zero
660 if (!cmd->alua_nonop_delay)
661 return 0;
663 * struct se_cmd->alua_nonop_delay gets set by a target port group
664 * defined interval in core_alua_state_nonoptimized()
666 msleep_interruptible(cmd->alua_nonop_delay);
667 return 0;
669 EXPORT_SYMBOL(core_alua_check_nonop_delay);
672 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
675 static int core_alua_write_tpg_metadata(
676 const char *path,
677 unsigned char *md_buf,
678 u32 md_buf_len)
680 mm_segment_t old_fs;
681 struct file *file;
682 struct iovec iov[1];
683 int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
685 memset(iov, 0, sizeof(struct iovec));
687 file = filp_open(path, flags, 0600);
688 if (IS_ERR(file) || !file || !file->f_dentry) {
689 pr_err("filp_open(%s) for ALUA metadata failed\n",
690 path);
691 return -ENODEV;
694 iov[0].iov_base = &md_buf[0];
695 iov[0].iov_len = md_buf_len;
697 old_fs = get_fs();
698 set_fs(get_ds());
699 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
700 set_fs(old_fs);
702 if (ret < 0) {
703 pr_err("Error writing ALUA metadata file: %s\n", path);
704 filp_close(file, NULL);
705 return -EIO;
707 filp_close(file, NULL);
709 return 0;
713 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
715 static int core_alua_update_tpg_primary_metadata(
716 struct t10_alua_tg_pt_gp *tg_pt_gp,
717 int primary_state,
718 unsigned char *md_buf)
720 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
721 struct t10_wwn *wwn = &su_dev->t10_wwn;
722 char path[ALUA_METADATA_PATH_LEN];
723 int len;
725 memset(path, 0, ALUA_METADATA_PATH_LEN);
727 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
728 "tg_pt_gp_id=%hu\n"
729 "alua_access_state=0x%02x\n"
730 "alua_access_status=0x%02x\n",
731 tg_pt_gp->tg_pt_gp_id, primary_state,
732 tg_pt_gp->tg_pt_gp_alua_access_status);
734 snprintf(path, ALUA_METADATA_PATH_LEN,
735 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
736 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
738 return core_alua_write_tpg_metadata(path, md_buf, len);
741 static int core_alua_do_transition_tg_pt(
742 struct t10_alua_tg_pt_gp *tg_pt_gp,
743 struct se_port *l_port,
744 struct se_node_acl *nacl,
745 unsigned char *md_buf,
746 int new_state,
747 int explict)
749 struct se_dev_entry *se_deve;
750 struct se_lun_acl *lacl;
751 struct se_port *port;
752 struct t10_alua_tg_pt_gp_member *mem;
753 int old_state = 0;
755 * Save the old primary ALUA access state, and set the current state
756 * to ALUA_ACCESS_STATE_TRANSITION.
758 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
759 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
760 ALUA_ACCESS_STATE_TRANSITION);
761 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
762 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
763 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
765 * Check for the optional ALUA primary state transition delay
767 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
768 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
770 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
771 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
772 tg_pt_gp_mem_list) {
773 port = mem->tg_pt;
775 * After an implicit target port asymmetric access state
776 * change, a device server shall establish a unit attention
777 * condition for the initiator port associated with every I_T
778 * nexus with the additional sense code set to ASYMMETRIC
779 * ACCESS STATE CHAGED.
781 * After an explicit target port asymmetric access state
782 * change, a device server shall establish a unit attention
783 * condition with the additional sense code set to ASYMMETRIC
784 * ACCESS STATE CHANGED for the initiator port associated with
785 * every I_T nexus other than the I_T nexus on which the SET
786 * TARGET PORT GROUPS command
788 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
789 smp_mb__after_atomic_inc();
790 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
792 spin_lock_bh(&port->sep_alua_lock);
793 list_for_each_entry(se_deve, &port->sep_alua_list,
794 alua_port_list) {
795 lacl = se_deve->se_lun_acl;
797 * se_deve->se_lun_acl pointer may be NULL for a
798 * entry created without explict Node+MappedLUN ACLs
800 if (!lacl)
801 continue;
803 if (explict &&
804 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
805 (l_port != NULL) && (l_port == port))
806 continue;
808 core_scsi3_ua_allocate(lacl->se_lun_nacl,
809 se_deve->mapped_lun, 0x2A,
810 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
812 spin_unlock_bh(&port->sep_alua_lock);
814 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
815 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
816 smp_mb__after_atomic_dec();
818 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
820 * Update the ALUA metadata buf that has been allocated in
821 * core_alua_do_port_transition(), this metadata will be written
822 * to struct file.
824 * Note that there is the case where we do not want to update the
825 * metadata when the saved metadata is being parsed in userspace
826 * when setting the existing port access state and access status.
828 * Also note that the failure to write out the ALUA metadata to
829 * struct file does NOT affect the actual ALUA transition.
831 if (tg_pt_gp->tg_pt_gp_write_metadata) {
832 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
833 core_alua_update_tpg_primary_metadata(tg_pt_gp,
834 new_state, md_buf);
835 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
838 * Set the current primary ALUA access state to the requested new state
840 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
842 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
843 " from primary access state %s to %s\n", (explict) ? "explict" :
844 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
845 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
846 core_alua_dump_state(new_state));
848 return 0;
851 int core_alua_do_port_transition(
852 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
853 struct se_device *l_dev,
854 struct se_port *l_port,
855 struct se_node_acl *l_nacl,
856 int new_state,
857 int explict)
859 struct se_device *dev;
860 struct se_port *port;
861 struct se_subsystem_dev *su_dev;
862 struct se_node_acl *nacl;
863 struct t10_alua_lu_gp *lu_gp;
864 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
865 struct t10_alua_tg_pt_gp *tg_pt_gp;
866 unsigned char *md_buf;
867 int primary;
869 if (core_alua_check_transition(new_state, &primary) != 0)
870 return -EINVAL;
872 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
873 if (!md_buf) {
874 pr_err("Unable to allocate buf for ALUA metadata\n");
875 return -ENOMEM;
878 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
879 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
880 lu_gp = local_lu_gp_mem->lu_gp;
881 atomic_inc(&lu_gp->lu_gp_ref_cnt);
882 smp_mb__after_atomic_inc();
883 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
885 * For storage objects that are members of the 'default_lu_gp',
886 * we only do transition on the passed *l_tp_pt_gp, and not
887 * on all of the matching target port groups IDs in default_lu_gp.
889 if (!lu_gp->lu_gp_id) {
891 * core_alua_do_transition_tg_pt() will always return
892 * success.
894 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
895 md_buf, new_state, explict);
896 atomic_dec(&lu_gp->lu_gp_ref_cnt);
897 smp_mb__after_atomic_dec();
898 kfree(md_buf);
899 return 0;
902 * For all other LU groups aside from 'default_lu_gp', walk all of
903 * the associated storage objects looking for a matching target port
904 * group ID from the local target port group.
906 spin_lock(&lu_gp->lu_gp_lock);
907 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
908 lu_gp_mem_list) {
910 dev = lu_gp_mem->lu_gp_mem_dev;
911 su_dev = dev->se_sub_dev;
912 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
913 smp_mb__after_atomic_inc();
914 spin_unlock(&lu_gp->lu_gp_lock);
916 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
917 list_for_each_entry(tg_pt_gp,
918 &su_dev->t10_alua.tg_pt_gps_list,
919 tg_pt_gp_list) {
921 if (!tg_pt_gp->tg_pt_gp_valid_id)
922 continue;
924 * If the target behavior port asymmetric access state
925 * is changed for any target port group accessiable via
926 * a logical unit within a LU group, the target port
927 * behavior group asymmetric access states for the same
928 * target port group accessible via other logical units
929 * in that LU group will also change.
931 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
932 continue;
934 if (l_tg_pt_gp == tg_pt_gp) {
935 port = l_port;
936 nacl = l_nacl;
937 } else {
938 port = NULL;
939 nacl = NULL;
941 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
942 smp_mb__after_atomic_inc();
943 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
945 * core_alua_do_transition_tg_pt() will always return
946 * success.
948 core_alua_do_transition_tg_pt(tg_pt_gp, port,
949 nacl, md_buf, new_state, explict);
951 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
952 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
953 smp_mb__after_atomic_dec();
955 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
957 spin_lock(&lu_gp->lu_gp_lock);
958 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
959 smp_mb__after_atomic_dec();
961 spin_unlock(&lu_gp->lu_gp_lock);
963 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
964 " Group IDs: %hu %s transition to primary state: %s\n",
965 config_item_name(&lu_gp->lu_gp_group.cg_item),
966 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
967 core_alua_dump_state(new_state));
969 atomic_dec(&lu_gp->lu_gp_ref_cnt);
970 smp_mb__after_atomic_dec();
971 kfree(md_buf);
972 return 0;
976 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
978 static int core_alua_update_tpg_secondary_metadata(
979 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
980 struct se_port *port,
981 unsigned char *md_buf,
982 u32 md_buf_len)
984 struct se_portal_group *se_tpg = port->sep_tpg;
985 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
986 int len;
988 memset(path, 0, ALUA_METADATA_PATH_LEN);
989 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
991 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
992 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
994 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
995 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
996 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
998 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
999 "alua_tg_pt_status=0x%02x\n",
1000 atomic_read(&port->sep_tg_pt_secondary_offline),
1001 port->sep_tg_pt_secondary_stat);
1003 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1004 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1005 port->sep_lun->unpacked_lun);
1007 return core_alua_write_tpg_metadata(path, md_buf, len);
1010 static int core_alua_set_tg_pt_secondary_state(
1011 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1012 struct se_port *port,
1013 int explict,
1014 int offline)
1016 struct t10_alua_tg_pt_gp *tg_pt_gp;
1017 unsigned char *md_buf;
1018 u32 md_buf_len;
1019 int trans_delay_msecs;
1021 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1022 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1023 if (!tg_pt_gp) {
1024 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1025 pr_err("Unable to complete secondary state"
1026 " transition\n");
1027 return -EINVAL;
1029 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1031 * Set the secondary ALUA target port access state to OFFLINE
1032 * or release the previously secondary state for struct se_port
1034 if (offline)
1035 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1036 else
1037 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1039 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1040 port->sep_tg_pt_secondary_stat = (explict) ?
1041 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1042 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1044 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1045 " to secondary access state: %s\n", (explict) ? "explict" :
1046 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1047 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1049 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1051 * Do the optional transition delay after we set the secondary
1052 * ALUA access state.
1054 if (trans_delay_msecs != 0)
1055 msleep_interruptible(trans_delay_msecs);
1057 * See if we need to update the ALUA fabric port metadata for
1058 * secondary state and status
1060 if (port->sep_tg_pt_secondary_write_md) {
1061 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1062 if (!md_buf) {
1063 pr_err("Unable to allocate md_buf for"
1064 " secondary ALUA access metadata\n");
1065 return -ENOMEM;
1067 mutex_lock(&port->sep_tg_pt_md_mutex);
1068 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1069 md_buf, md_buf_len);
1070 mutex_unlock(&port->sep_tg_pt_md_mutex);
1072 kfree(md_buf);
1075 return 0;
1078 struct t10_alua_lu_gp *
1079 core_alua_allocate_lu_gp(const char *name, int def_group)
1081 struct t10_alua_lu_gp *lu_gp;
1083 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1084 if (!lu_gp) {
1085 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1086 return ERR_PTR(-ENOMEM);
1088 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1089 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1090 spin_lock_init(&lu_gp->lu_gp_lock);
1091 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1093 if (def_group) {
1094 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1095 lu_gp->lu_gp_valid_id = 1;
1096 alua_lu_gps_count++;
1099 return lu_gp;
1102 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1104 struct t10_alua_lu_gp *lu_gp_tmp;
1105 u16 lu_gp_id_tmp;
1107 * The lu_gp->lu_gp_id may only be set once..
1109 if (lu_gp->lu_gp_valid_id) {
1110 pr_warn("ALUA LU Group already has a valid ID,"
1111 " ignoring request\n");
1112 return -EINVAL;
1115 spin_lock(&lu_gps_lock);
1116 if (alua_lu_gps_count == 0x0000ffff) {
1117 pr_err("Maximum ALUA alua_lu_gps_count:"
1118 " 0x0000ffff reached\n");
1119 spin_unlock(&lu_gps_lock);
1120 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1121 return -ENOSPC;
1123 again:
1124 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1125 alua_lu_gps_counter++;
1127 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1128 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1129 if (!lu_gp_id)
1130 goto again;
1132 pr_warn("ALUA Logical Unit Group ID: %hu"
1133 " already exists, ignoring request\n",
1134 lu_gp_id);
1135 spin_unlock(&lu_gps_lock);
1136 return -EINVAL;
1140 lu_gp->lu_gp_id = lu_gp_id_tmp;
1141 lu_gp->lu_gp_valid_id = 1;
1142 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1143 alua_lu_gps_count++;
1144 spin_unlock(&lu_gps_lock);
1146 return 0;
1149 static struct t10_alua_lu_gp_member *
1150 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1152 struct t10_alua_lu_gp_member *lu_gp_mem;
1154 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1155 if (!lu_gp_mem) {
1156 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1157 return ERR_PTR(-ENOMEM);
1159 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1160 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1161 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1163 lu_gp_mem->lu_gp_mem_dev = dev;
1164 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1166 return lu_gp_mem;
1169 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1171 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1173 * Once we have reached this point, config_item_put() has
1174 * already been called from target_core_alua_drop_lu_gp().
1176 * Here, we remove the *lu_gp from the global list so that
1177 * no associations can be made while we are releasing
1178 * struct t10_alua_lu_gp.
1180 spin_lock(&lu_gps_lock);
1181 atomic_set(&lu_gp->lu_gp_shutdown, 1);
1182 list_del(&lu_gp->lu_gp_node);
1183 alua_lu_gps_count--;
1184 spin_unlock(&lu_gps_lock);
1186 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1187 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1188 * released with core_alua_put_lu_gp_from_name()
1190 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1191 cpu_relax();
1193 * Release reference to struct t10_alua_lu_gp * from all associated
1194 * struct se_device.
1196 spin_lock(&lu_gp->lu_gp_lock);
1197 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1198 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1199 if (lu_gp_mem->lu_gp_assoc) {
1200 list_del(&lu_gp_mem->lu_gp_mem_list);
1201 lu_gp->lu_gp_members--;
1202 lu_gp_mem->lu_gp_assoc = 0;
1204 spin_unlock(&lu_gp->lu_gp_lock);
1207 * lu_gp_mem is associated with a single
1208 * struct se_device->dev_alua_lu_gp_mem, and is released when
1209 * struct se_device is released via core_alua_free_lu_gp_mem().
1211 * If the passed lu_gp does NOT match the default_lu_gp, assume
1212 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1214 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1215 if (lu_gp != default_lu_gp)
1216 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1217 default_lu_gp);
1218 else
1219 lu_gp_mem->lu_gp = NULL;
1220 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1222 spin_lock(&lu_gp->lu_gp_lock);
1224 spin_unlock(&lu_gp->lu_gp_lock);
1226 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1229 void core_alua_free_lu_gp_mem(struct se_device *dev)
1231 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1232 struct t10_alua *alua = &su_dev->t10_alua;
1233 struct t10_alua_lu_gp *lu_gp;
1234 struct t10_alua_lu_gp_member *lu_gp_mem;
1236 if (alua->alua_type != SPC3_ALUA_EMULATED)
1237 return;
1239 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1240 if (!lu_gp_mem)
1241 return;
1243 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1244 cpu_relax();
1246 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1247 lu_gp = lu_gp_mem->lu_gp;
1248 if (lu_gp) {
1249 spin_lock(&lu_gp->lu_gp_lock);
1250 if (lu_gp_mem->lu_gp_assoc) {
1251 list_del(&lu_gp_mem->lu_gp_mem_list);
1252 lu_gp->lu_gp_members--;
1253 lu_gp_mem->lu_gp_assoc = 0;
1255 spin_unlock(&lu_gp->lu_gp_lock);
1256 lu_gp_mem->lu_gp = NULL;
1258 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1260 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1263 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1265 struct t10_alua_lu_gp *lu_gp;
1266 struct config_item *ci;
1268 spin_lock(&lu_gps_lock);
1269 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1270 if (!lu_gp->lu_gp_valid_id)
1271 continue;
1272 ci = &lu_gp->lu_gp_group.cg_item;
1273 if (!strcmp(config_item_name(ci), name)) {
1274 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1275 spin_unlock(&lu_gps_lock);
1276 return lu_gp;
1279 spin_unlock(&lu_gps_lock);
1281 return NULL;
1284 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1286 spin_lock(&lu_gps_lock);
1287 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1288 spin_unlock(&lu_gps_lock);
1292 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1294 void __core_alua_attach_lu_gp_mem(
1295 struct t10_alua_lu_gp_member *lu_gp_mem,
1296 struct t10_alua_lu_gp *lu_gp)
1298 spin_lock(&lu_gp->lu_gp_lock);
1299 lu_gp_mem->lu_gp = lu_gp;
1300 lu_gp_mem->lu_gp_assoc = 1;
1301 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1302 lu_gp->lu_gp_members++;
1303 spin_unlock(&lu_gp->lu_gp_lock);
1307 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1309 void __core_alua_drop_lu_gp_mem(
1310 struct t10_alua_lu_gp_member *lu_gp_mem,
1311 struct t10_alua_lu_gp *lu_gp)
1313 spin_lock(&lu_gp->lu_gp_lock);
1314 list_del(&lu_gp_mem->lu_gp_mem_list);
1315 lu_gp_mem->lu_gp = NULL;
1316 lu_gp_mem->lu_gp_assoc = 0;
1317 lu_gp->lu_gp_members--;
1318 spin_unlock(&lu_gp->lu_gp_lock);
1321 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
1322 struct se_subsystem_dev *su_dev,
1323 const char *name,
1324 int def_group)
1326 struct t10_alua_tg_pt_gp *tg_pt_gp;
1328 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1329 if (!tg_pt_gp) {
1330 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1331 return NULL;
1333 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1334 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1335 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1336 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1337 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1338 tg_pt_gp->tg_pt_gp_su_dev = su_dev;
1339 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1340 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1341 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1343 * Enable both explict and implict ALUA support by default
1345 tg_pt_gp->tg_pt_gp_alua_access_type =
1346 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1348 * Set the default Active/NonOptimized Delay in milliseconds
1350 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1351 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1353 if (def_group) {
1354 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1355 tg_pt_gp->tg_pt_gp_id =
1356 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1357 tg_pt_gp->tg_pt_gp_valid_id = 1;
1358 su_dev->t10_alua.alua_tg_pt_gps_count++;
1359 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1360 &su_dev->t10_alua.tg_pt_gps_list);
1361 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1364 return tg_pt_gp;
1367 int core_alua_set_tg_pt_gp_id(
1368 struct t10_alua_tg_pt_gp *tg_pt_gp,
1369 u16 tg_pt_gp_id)
1371 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1372 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1373 u16 tg_pt_gp_id_tmp;
1375 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1377 if (tg_pt_gp->tg_pt_gp_valid_id) {
1378 pr_warn("ALUA TG PT Group already has a valid ID,"
1379 " ignoring request\n");
1380 return -EINVAL;
1383 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1384 if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1385 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1386 " 0x0000ffff reached\n");
1387 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1388 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1389 return -ENOSPC;
1391 again:
1392 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1393 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1395 list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
1396 tg_pt_gp_list) {
1397 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1398 if (!tg_pt_gp_id)
1399 goto again;
1401 pr_err("ALUA Target Port Group ID: %hu already"
1402 " exists, ignoring request\n", tg_pt_gp_id);
1403 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1404 return -EINVAL;
1408 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1409 tg_pt_gp->tg_pt_gp_valid_id = 1;
1410 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1411 &su_dev->t10_alua.tg_pt_gps_list);
1412 su_dev->t10_alua.alua_tg_pt_gps_count++;
1413 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1415 return 0;
1418 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1419 struct se_port *port)
1421 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1423 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1424 GFP_KERNEL);
1425 if (!tg_pt_gp_mem) {
1426 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1427 return ERR_PTR(-ENOMEM);
1429 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1430 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1431 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1433 tg_pt_gp_mem->tg_pt = port;
1434 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1435 atomic_set(&port->sep_tg_pt_gp_active, 1);
1437 return tg_pt_gp_mem;
1440 void core_alua_free_tg_pt_gp(
1441 struct t10_alua_tg_pt_gp *tg_pt_gp)
1443 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1444 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1446 * Once we have reached this point, config_item_put() has already
1447 * been called from target_core_alua_drop_tg_pt_gp().
1449 * Here we remove *tg_pt_gp from the global list so that
1450 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1451 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1453 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1454 list_del(&tg_pt_gp->tg_pt_gp_list);
1455 su_dev->t10_alua.alua_tg_pt_gps_counter--;
1456 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1458 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1459 * core_alua_get_tg_pt_gp_by_name() in
1460 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1461 * to be released with core_alua_put_tg_pt_gp_from_name().
1463 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1464 cpu_relax();
1466 * Release reference to struct t10_alua_tg_pt_gp from all associated
1467 * struct se_port.
1469 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1470 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1471 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1472 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1473 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1474 tg_pt_gp->tg_pt_gp_members--;
1475 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1477 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1479 * tg_pt_gp_mem is associated with a single
1480 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1481 * core_alua_free_tg_pt_gp_mem().
1483 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1484 * assume we want to re-assocate a given tg_pt_gp_mem with
1485 * default_tg_pt_gp.
1487 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1488 if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
1489 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1490 su_dev->t10_alua.default_tg_pt_gp);
1491 } else
1492 tg_pt_gp_mem->tg_pt_gp = NULL;
1493 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1495 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1497 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1499 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1502 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1504 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1505 struct t10_alua *alua = &su_dev->t10_alua;
1506 struct t10_alua_tg_pt_gp *tg_pt_gp;
1507 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1509 if (alua->alua_type != SPC3_ALUA_EMULATED)
1510 return;
1512 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1513 if (!tg_pt_gp_mem)
1514 return;
1516 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1517 cpu_relax();
1519 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1520 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1521 if (tg_pt_gp) {
1522 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1523 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1524 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1525 tg_pt_gp->tg_pt_gp_members--;
1526 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1528 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1529 tg_pt_gp_mem->tg_pt_gp = NULL;
1531 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1533 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1536 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1537 struct se_subsystem_dev *su_dev,
1538 const char *name)
1540 struct t10_alua_tg_pt_gp *tg_pt_gp;
1541 struct config_item *ci;
1543 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1544 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
1545 tg_pt_gp_list) {
1546 if (!tg_pt_gp->tg_pt_gp_valid_id)
1547 continue;
1548 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1549 if (!strcmp(config_item_name(ci), name)) {
1550 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1551 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1552 return tg_pt_gp;
1555 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1557 return NULL;
1560 static void core_alua_put_tg_pt_gp_from_name(
1561 struct t10_alua_tg_pt_gp *tg_pt_gp)
1563 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1565 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1566 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1567 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1571 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1573 void __core_alua_attach_tg_pt_gp_mem(
1574 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1575 struct t10_alua_tg_pt_gp *tg_pt_gp)
1577 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1578 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1579 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1580 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1581 &tg_pt_gp->tg_pt_gp_mem_list);
1582 tg_pt_gp->tg_pt_gp_members++;
1583 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1587 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1589 static void __core_alua_drop_tg_pt_gp_mem(
1590 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1591 struct t10_alua_tg_pt_gp *tg_pt_gp)
1593 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1594 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1595 tg_pt_gp_mem->tg_pt_gp = NULL;
1596 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1597 tg_pt_gp->tg_pt_gp_members--;
1598 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1601 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1603 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1604 struct config_item *tg_pt_ci;
1605 struct t10_alua *alua = &su_dev->t10_alua;
1606 struct t10_alua_tg_pt_gp *tg_pt_gp;
1607 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1608 ssize_t len = 0;
1610 if (alua->alua_type != SPC3_ALUA_EMULATED)
1611 return len;
1613 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1614 if (!tg_pt_gp_mem)
1615 return len;
1617 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1618 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1619 if (tg_pt_gp) {
1620 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1621 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1622 " %hu\nTG Port Primary Access State: %s\nTG Port "
1623 "Primary Access Status: %s\nTG Port Secondary Access"
1624 " State: %s\nTG Port Secondary Access Status: %s\n",
1625 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1626 core_alua_dump_state(atomic_read(
1627 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1628 core_alua_dump_status(
1629 tg_pt_gp->tg_pt_gp_alua_access_status),
1630 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1631 "Offline" : "None",
1632 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1634 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1636 return len;
1639 ssize_t core_alua_store_tg_pt_gp_info(
1640 struct se_port *port,
1641 const char *page,
1642 size_t count)
1644 struct se_portal_group *tpg;
1645 struct se_lun *lun;
1646 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1647 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1648 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1649 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1650 int move = 0;
1652 tpg = port->sep_tpg;
1653 lun = port->sep_lun;
1655 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1656 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1657 " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1658 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1659 config_item_name(&lun->lun_group.cg_item));
1660 return -EINVAL;
1663 if (count > TG_PT_GROUP_NAME_BUF) {
1664 pr_err("ALUA Target Port Group alias too large!\n");
1665 return -EINVAL;
1667 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1668 memcpy(buf, page, count);
1670 * Any ALUA target port group alias besides "NULL" means we will be
1671 * making a new group association.
1673 if (strcmp(strstrip(buf), "NULL")) {
1675 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1676 * struct t10_alua_tg_pt_gp. This reference is released with
1677 * core_alua_put_tg_pt_gp_from_name() below.
1679 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
1680 strstrip(buf));
1681 if (!tg_pt_gp_new)
1682 return -ENODEV;
1684 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1685 if (!tg_pt_gp_mem) {
1686 if (tg_pt_gp_new)
1687 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1688 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1689 return -EINVAL;
1692 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1693 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1694 if (tg_pt_gp) {
1696 * Clearing an existing tg_pt_gp association, and replacing
1697 * with the default_tg_pt_gp.
1699 if (!tg_pt_gp_new) {
1700 pr_debug("Target_Core_ConfigFS: Moving"
1701 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1702 " alua/%s, ID: %hu back to"
1703 " default_tg_pt_gp\n",
1704 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1705 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1706 config_item_name(&lun->lun_group.cg_item),
1707 config_item_name(
1708 &tg_pt_gp->tg_pt_gp_group.cg_item),
1709 tg_pt_gp->tg_pt_gp_id);
1711 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1712 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1713 su_dev->t10_alua.default_tg_pt_gp);
1714 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1716 return count;
1719 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1721 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1722 move = 1;
1725 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1727 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1728 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1729 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1730 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1731 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1732 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1733 config_item_name(&lun->lun_group.cg_item),
1734 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1735 tg_pt_gp_new->tg_pt_gp_id);
1737 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1738 return count;
1741 ssize_t core_alua_show_access_type(
1742 struct t10_alua_tg_pt_gp *tg_pt_gp,
1743 char *page)
1745 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1746 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1747 return sprintf(page, "Implict and Explict\n");
1748 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1749 return sprintf(page, "Implict\n");
1750 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1751 return sprintf(page, "Explict\n");
1752 else
1753 return sprintf(page, "None\n");
1756 ssize_t core_alua_store_access_type(
1757 struct t10_alua_tg_pt_gp *tg_pt_gp,
1758 const char *page,
1759 size_t count)
1761 unsigned long tmp;
1762 int ret;
1764 ret = strict_strtoul(page, 0, &tmp);
1765 if (ret < 0) {
1766 pr_err("Unable to extract alua_access_type\n");
1767 return -EINVAL;
1769 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1770 pr_err("Illegal value for alua_access_type:"
1771 " %lu\n", tmp);
1772 return -EINVAL;
1774 if (tmp == 3)
1775 tg_pt_gp->tg_pt_gp_alua_access_type =
1776 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1777 else if (tmp == 2)
1778 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1779 else if (tmp == 1)
1780 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1781 else
1782 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1784 return count;
1787 ssize_t core_alua_show_nonop_delay_msecs(
1788 struct t10_alua_tg_pt_gp *tg_pt_gp,
1789 char *page)
1791 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1794 ssize_t core_alua_store_nonop_delay_msecs(
1795 struct t10_alua_tg_pt_gp *tg_pt_gp,
1796 const char *page,
1797 size_t count)
1799 unsigned long tmp;
1800 int ret;
1802 ret = strict_strtoul(page, 0, &tmp);
1803 if (ret < 0) {
1804 pr_err("Unable to extract nonop_delay_msecs\n");
1805 return -EINVAL;
1807 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1808 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1809 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1810 ALUA_MAX_NONOP_DELAY_MSECS);
1811 return -EINVAL;
1813 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1815 return count;
1818 ssize_t core_alua_show_trans_delay_msecs(
1819 struct t10_alua_tg_pt_gp *tg_pt_gp,
1820 char *page)
1822 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1825 ssize_t core_alua_store_trans_delay_msecs(
1826 struct t10_alua_tg_pt_gp *tg_pt_gp,
1827 const char *page,
1828 size_t count)
1830 unsigned long tmp;
1831 int ret;
1833 ret = strict_strtoul(page, 0, &tmp);
1834 if (ret < 0) {
1835 pr_err("Unable to extract trans_delay_msecs\n");
1836 return -EINVAL;
1838 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1839 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1840 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1841 ALUA_MAX_TRANS_DELAY_MSECS);
1842 return -EINVAL;
1844 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1846 return count;
1849 ssize_t core_alua_show_preferred_bit(
1850 struct t10_alua_tg_pt_gp *tg_pt_gp,
1851 char *page)
1853 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1856 ssize_t core_alua_store_preferred_bit(
1857 struct t10_alua_tg_pt_gp *tg_pt_gp,
1858 const char *page,
1859 size_t count)
1861 unsigned long tmp;
1862 int ret;
1864 ret = strict_strtoul(page, 0, &tmp);
1865 if (ret < 0) {
1866 pr_err("Unable to extract preferred ALUA value\n");
1867 return -EINVAL;
1869 if ((tmp != 0) && (tmp != 1)) {
1870 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1871 return -EINVAL;
1873 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1875 return count;
1878 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1880 if (!lun->lun_sep)
1881 return -ENODEV;
1883 return sprintf(page, "%d\n",
1884 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1887 ssize_t core_alua_store_offline_bit(
1888 struct se_lun *lun,
1889 const char *page,
1890 size_t count)
1892 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1893 unsigned long tmp;
1894 int ret;
1896 if (!lun->lun_sep)
1897 return -ENODEV;
1899 ret = strict_strtoul(page, 0, &tmp);
1900 if (ret < 0) {
1901 pr_err("Unable to extract alua_tg_pt_offline value\n");
1902 return -EINVAL;
1904 if ((tmp != 0) && (tmp != 1)) {
1905 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1906 tmp);
1907 return -EINVAL;
1909 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1910 if (!tg_pt_gp_mem) {
1911 pr_err("Unable to locate *tg_pt_gp_mem\n");
1912 return -EINVAL;
1915 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1916 lun->lun_sep, 0, (int)tmp);
1917 if (ret < 0)
1918 return -EINVAL;
1920 return count;
1923 ssize_t core_alua_show_secondary_status(
1924 struct se_lun *lun,
1925 char *page)
1927 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1930 ssize_t core_alua_store_secondary_status(
1931 struct se_lun *lun,
1932 const char *page,
1933 size_t count)
1935 unsigned long tmp;
1936 int ret;
1938 ret = strict_strtoul(page, 0, &tmp);
1939 if (ret < 0) {
1940 pr_err("Unable to extract alua_tg_pt_status\n");
1941 return -EINVAL;
1943 if ((tmp != ALUA_STATUS_NONE) &&
1944 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1945 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1946 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1947 tmp);
1948 return -EINVAL;
1950 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1952 return count;
1955 ssize_t core_alua_show_secondary_write_metadata(
1956 struct se_lun *lun,
1957 char *page)
1959 return sprintf(page, "%d\n",
1960 lun->lun_sep->sep_tg_pt_secondary_write_md);
1963 ssize_t core_alua_store_secondary_write_metadata(
1964 struct se_lun *lun,
1965 const char *page,
1966 size_t count)
1968 unsigned long tmp;
1969 int ret;
1971 ret = strict_strtoul(page, 0, &tmp);
1972 if (ret < 0) {
1973 pr_err("Unable to extract alua_tg_pt_write_md\n");
1974 return -EINVAL;
1976 if ((tmp != 0) && (tmp != 1)) {
1977 pr_err("Illegal value for alua_tg_pt_write_md:"
1978 " %lu\n", tmp);
1979 return -EINVAL;
1981 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
1983 return count;
1986 int core_setup_alua(struct se_device *dev, int force_pt)
1988 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1989 struct t10_alua *alua = &su_dev->t10_alua;
1990 struct t10_alua_lu_gp_member *lu_gp_mem;
1992 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
1993 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
1994 * cause a problem because libata and some SATA RAID HBAs appear
1995 * under Linux/SCSI, but emulate SCSI logic themselves.
1997 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
1998 !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
1999 alua->alua_type = SPC_ALUA_PASSTHROUGH;
2000 alua->alua_state_check = &core_alua_state_check_nop;
2001 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2002 " emulation\n", dev->transport->name);
2003 return 0;
2006 * If SPC-3 or above is reported by real or emulated struct se_device,
2007 * use emulated ALUA.
2009 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
2010 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2011 " device\n", dev->transport->name);
2013 * Associate this struct se_device with the default ALUA
2014 * LUN Group.
2016 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2017 if (IS_ERR(lu_gp_mem))
2018 return PTR_ERR(lu_gp_mem);
2020 alua->alua_type = SPC3_ALUA_EMULATED;
2021 alua->alua_state_check = &core_alua_state_check;
2022 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2023 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2024 default_lu_gp);
2025 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2027 pr_debug("%s: Adding to default ALUA LU Group:"
2028 " core/alua/lu_gps/default_lu_gp\n",
2029 dev->transport->name);
2030 } else {
2031 alua->alua_type = SPC2_ALUA_DISABLED;
2032 alua->alua_state_check = &core_alua_state_check_nop;
2033 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2034 " device\n", dev->transport->name);
2037 return 0;