Linux 3.4.102
[linux/fpc-iii.git] / drivers / target / target_core_alua.c
blobb10f585cd11ab8b7acddff35c44297f3933c7b4a
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 <linux/export.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
33 #include <asm/unaligned.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_backend.h>
37 #include <target/target_core_fabric.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_internal.h"
41 #include "target_core_alua.h"
42 #include "target_core_ua.h"
44 static int core_alua_check_transition(int state, int *primary);
45 static 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);
49 static u16 alua_lu_gps_counter;
50 static u32 alua_lu_gps_count;
52 static DEFINE_SPINLOCK(lu_gps_lock);
53 static LIST_HEAD(lu_gps_list);
55 struct t10_alua_lu_gp *default_lu_gp;
58 * REPORT_TARGET_PORT_GROUPS
60 * See spc4r17 section 6.27
62 int target_emulate_report_target_port_groups(struct se_task *task)
64 struct se_cmd *cmd = task->task_se_cmd;
65 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
66 struct se_port *port;
67 struct t10_alua_tg_pt_gp *tg_pt_gp;
68 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
69 unsigned char *buf;
70 u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
71 Target port group descriptor */
73 * Need at least 4 bytes of response data or else we can't
74 * even fit the return data length.
76 if (cmd->data_length < 4) {
77 pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
78 " too small\n", cmd->data_length);
79 return -EINVAL;
82 buf = transport_kmap_data_sg(cmd);
84 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
85 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
86 tg_pt_gp_list) {
88 * Check if the Target port group and Target port descriptor list
89 * based on tg_pt_gp_members count will fit into the response payload.
90 * Otherwise, bump rd_len to let the initiator know we have exceeded
91 * the allocation length and the response is truncated.
93 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
94 cmd->data_length) {
95 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
96 continue;
99 * PREF: Preferred target port bit, determine if this
100 * bit should be set for port group.
102 if (tg_pt_gp->tg_pt_gp_pref)
103 buf[off] = 0x80;
105 * Set the ASYMMETRIC ACCESS State
107 buf[off++] |= (atomic_read(
108 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
110 * Set supported ASYMMETRIC ACCESS State bits
112 buf[off] = 0x80; /* T_SUP */
113 buf[off] |= 0x40; /* O_SUP */
114 buf[off] |= 0x8; /* U_SUP */
115 buf[off] |= 0x4; /* S_SUP */
116 buf[off] |= 0x2; /* AN_SUP */
117 buf[off++] |= 0x1; /* AO_SUP */
119 * TARGET PORT GROUP
121 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
122 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
124 off++; /* Skip over Reserved */
126 * STATUS CODE
128 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
130 * Vendor Specific field
132 buf[off++] = 0x00;
134 * TARGET PORT COUNT
136 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
137 rd_len += 8;
139 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
140 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
141 tg_pt_gp_mem_list) {
142 port = tg_pt_gp_mem->tg_pt;
144 * Start Target Port descriptor format
146 * See spc4r17 section 6.2.7 Table 247
148 off += 2; /* Skip over Obsolete */
150 * Set RELATIVE TARGET PORT IDENTIFIER
152 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
153 buf[off++] = (port->sep_rtpi & 0xff);
154 rd_len += 4;
156 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
158 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
160 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
162 buf[0] = ((rd_len >> 24) & 0xff);
163 buf[1] = ((rd_len >> 16) & 0xff);
164 buf[2] = ((rd_len >> 8) & 0xff);
165 buf[3] = (rd_len & 0xff);
167 transport_kunmap_data_sg(cmd);
169 task->task_scsi_status = GOOD;
170 transport_complete_task(task, 1);
171 return 0;
175 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
177 * See spc4r17 section 6.35
179 int target_emulate_set_target_port_groups(struct se_task *task)
181 struct se_cmd *cmd = task->task_se_cmd;
182 struct se_device *dev = cmd->se_dev;
183 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
184 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
185 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
186 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
187 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
188 unsigned char *buf;
189 unsigned char *ptr;
190 u32 len = 4; /* Skip over RESERVED area in header */
191 int alua_access_state, primary = 0, rc;
192 u16 tg_pt_id, rtpi;
194 if (!l_port) {
195 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
196 return -EINVAL;
198 buf = transport_kmap_data_sg(cmd);
201 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
202 * for the local tg_pt_gp.
204 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
205 if (!l_tg_pt_gp_mem) {
206 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
207 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
208 rc = -EINVAL;
209 goto out;
211 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
212 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
213 if (!l_tg_pt_gp) {
214 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
215 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
216 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
217 rc = -EINVAL;
218 goto out;
220 rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
221 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
223 if (!rc) {
224 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
225 " while TPGS_EXPLICT_ALUA is disabled\n");
226 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
227 rc = -EINVAL;
228 goto out;
231 ptr = &buf[4]; /* Skip over RESERVED area in header */
233 while (len < cmd->data_length) {
234 alua_access_state = (ptr[0] & 0x0f);
236 * Check the received ALUA access state, and determine if
237 * the state is a primary or secondary target port asymmetric
238 * access state.
240 rc = core_alua_check_transition(alua_access_state, &primary);
241 if (rc != 0) {
243 * If the SET TARGET PORT GROUPS attempts to establish
244 * an invalid combination of target port asymmetric
245 * access states or attempts to establish an
246 * unsupported target port asymmetric access state,
247 * then the command shall be terminated with CHECK
248 * CONDITION status, with the sense key set to ILLEGAL
249 * REQUEST, and the additional sense code set to INVALID
250 * FIELD IN PARAMETER LIST.
252 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
253 rc = -EINVAL;
254 goto out;
256 rc = -1;
258 * If the ASYMMETRIC ACCESS STATE field (see table 267)
259 * specifies a primary target port asymmetric access state,
260 * then the TARGET PORT GROUP OR TARGET PORT field specifies
261 * a primary target port group for which the primary target
262 * port asymmetric access state shall be changed. If the
263 * ASYMMETRIC ACCESS STATE field specifies a secondary target
264 * port asymmetric access state, then the TARGET PORT GROUP OR
265 * TARGET PORT field specifies the relative target port
266 * identifier (see 3.1.120) of the target port for which the
267 * secondary target port asymmetric access state shall be
268 * changed.
270 if (primary) {
271 tg_pt_id = get_unaligned_be16(ptr + 2);
273 * Locate the matching target port group ID from
274 * the global tg_pt_gp list
276 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
277 list_for_each_entry(tg_pt_gp,
278 &su_dev->t10_alua.tg_pt_gps_list,
279 tg_pt_gp_list) {
280 if (!tg_pt_gp->tg_pt_gp_valid_id)
281 continue;
283 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
284 continue;
286 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
287 smp_mb__after_atomic_inc();
288 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
290 rc = core_alua_do_port_transition(tg_pt_gp,
291 dev, l_port, nacl,
292 alua_access_state, 1);
294 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
295 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
296 smp_mb__after_atomic_dec();
297 break;
299 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
301 * If not matching target port group ID can be located
302 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
304 if (rc != 0) {
305 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
306 rc = -EINVAL;
307 goto out;
309 } else {
311 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
312 * the Target Port in question for the the incoming
313 * SET_TARGET_PORT_GROUPS op.
315 rtpi = get_unaligned_be16(ptr + 2);
317 * Locate the matching relative target port identifer
318 * for the struct se_device storage object.
320 spin_lock(&dev->se_port_lock);
321 list_for_each_entry(port, &dev->dev_sep_list,
322 sep_list) {
323 if (port->sep_rtpi != rtpi)
324 continue;
326 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
327 spin_unlock(&dev->se_port_lock);
329 rc = core_alua_set_tg_pt_secondary_state(
330 tg_pt_gp_mem, port, 1, 1);
332 spin_lock(&dev->se_port_lock);
333 break;
335 spin_unlock(&dev->se_port_lock);
337 * If not matching relative target port identifier can
338 * be located, throw an exception with ASCQ:
339 * INVALID_PARAMETER_LIST
341 if (rc != 0) {
342 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
343 rc = -EINVAL;
344 goto out;
348 ptr += 4;
349 len += 4;
352 out:
353 transport_kunmap_data_sg(cmd);
354 if (!rc) {
355 task->task_scsi_status = GOOD;
356 transport_complete_task(task, 1);
358 return rc;
361 static inline int core_alua_state_nonoptimized(
362 struct se_cmd *cmd,
363 unsigned char *cdb,
364 int nonop_delay_msecs,
365 u8 *alua_ascq)
368 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
369 * later to determine if processing of this cmd needs to be
370 * temporarily delayed for the Active/NonOptimized primary access state.
372 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
373 cmd->alua_nonop_delay = nonop_delay_msecs;
374 return 0;
377 static inline int core_alua_state_standby(
378 struct se_cmd *cmd,
379 unsigned char *cdb,
380 u8 *alua_ascq)
383 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
384 * spc4r17 section 5.9.2.4.4
386 switch (cdb[0]) {
387 case INQUIRY:
388 case LOG_SELECT:
389 case LOG_SENSE:
390 case MODE_SELECT:
391 case MODE_SENSE:
392 case REPORT_LUNS:
393 case RECEIVE_DIAGNOSTIC:
394 case SEND_DIAGNOSTIC:
395 return 0;
396 case MAINTENANCE_IN:
397 switch (cdb[1] & 0x1f) {
398 case MI_REPORT_TARGET_PGS:
399 return 0;
400 default:
401 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
402 return 1;
404 case MAINTENANCE_OUT:
405 switch (cdb[1]) {
406 case MO_SET_TARGET_PGS:
407 return 0;
408 default:
409 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
410 return 1;
412 case REQUEST_SENSE:
413 case PERSISTENT_RESERVE_IN:
414 case PERSISTENT_RESERVE_OUT:
415 case READ_BUFFER:
416 case WRITE_BUFFER:
417 return 0;
418 default:
419 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
420 return 1;
423 return 0;
426 static inline int core_alua_state_unavailable(
427 struct se_cmd *cmd,
428 unsigned char *cdb,
429 u8 *alua_ascq)
432 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
433 * spc4r17 section 5.9.2.4.5
435 switch (cdb[0]) {
436 case INQUIRY:
437 case REPORT_LUNS:
438 return 0;
439 case MAINTENANCE_IN:
440 switch (cdb[1] & 0x1f) {
441 case MI_REPORT_TARGET_PGS:
442 return 0;
443 default:
444 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
445 return 1;
447 case MAINTENANCE_OUT:
448 switch (cdb[1]) {
449 case MO_SET_TARGET_PGS:
450 return 0;
451 default:
452 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
453 return 1;
455 case REQUEST_SENSE:
456 case READ_BUFFER:
457 case WRITE_BUFFER:
458 return 0;
459 default:
460 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
461 return 1;
464 return 0;
467 static inline int core_alua_state_transition(
468 struct se_cmd *cmd,
469 unsigned char *cdb,
470 u8 *alua_ascq)
473 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
474 * spc4r17 section 5.9.2.5
476 switch (cdb[0]) {
477 case INQUIRY:
478 case REPORT_LUNS:
479 return 0;
480 case MAINTENANCE_IN:
481 switch (cdb[1] & 0x1f) {
482 case MI_REPORT_TARGET_PGS:
483 return 0;
484 default:
485 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
486 return 1;
488 case REQUEST_SENSE:
489 case READ_BUFFER:
490 case WRITE_BUFFER:
491 return 0;
492 default:
493 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
494 return 1;
497 return 0;
501 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
502 * in transport_cmd_sequencer(). This function is assigned to
503 * struct t10_alua *->state_check() in core_setup_alua()
505 static int core_alua_state_check_nop(
506 struct se_cmd *cmd,
507 unsigned char *cdb,
508 u8 *alua_ascq)
510 return 0;
514 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
515 * This function is assigned to struct t10_alua *->state_check() in
516 * core_setup_alua()
518 * Also, this function can return three different return codes to
519 * signal transport_generic_cmd_sequencer()
521 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
522 * return 0: Used to signal success
523 * reutrn -1: Used to signal failure, and invalid cdb field
525 static int core_alua_state_check(
526 struct se_cmd *cmd,
527 unsigned char *cdb,
528 u8 *alua_ascq)
530 struct se_lun *lun = cmd->se_lun;
531 struct se_port *port = lun->lun_sep;
532 struct t10_alua_tg_pt_gp *tg_pt_gp;
533 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
534 int out_alua_state, nonop_delay_msecs;
536 if (!port)
537 return 0;
539 * First, check for a struct se_port specific secondary ALUA target port
540 * access state: OFFLINE
542 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
543 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
544 pr_debug("ALUA: Got secondary offline status for local"
545 " target port\n");
546 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
547 return 1;
550 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
551 * ALUA target port group, to obtain current ALUA access state.
552 * Otherwise look for the underlying struct se_device association with
553 * a ALUA logical unit group.
555 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
556 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
557 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
558 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
559 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
560 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
562 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
563 * statement so the compiler knows explicitly to check this case first.
564 * For the Optimized ALUA access state case, we want to process the
565 * incoming fabric cmd ASAP..
567 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
568 return 0;
570 switch (out_alua_state) {
571 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
572 return core_alua_state_nonoptimized(cmd, cdb,
573 nonop_delay_msecs, alua_ascq);
574 case ALUA_ACCESS_STATE_STANDBY:
575 return core_alua_state_standby(cmd, cdb, alua_ascq);
576 case ALUA_ACCESS_STATE_UNAVAILABLE:
577 return core_alua_state_unavailable(cmd, cdb, alua_ascq);
578 case ALUA_ACCESS_STATE_TRANSITION:
579 return core_alua_state_transition(cmd, cdb, alua_ascq);
581 * OFFLINE is a secondary ALUA target port group access state, that is
582 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
584 case ALUA_ACCESS_STATE_OFFLINE:
585 default:
586 pr_err("Unknown ALUA access state: 0x%02x\n",
587 out_alua_state);
588 return -EINVAL;
591 return 0;
595 * Check implict and explict ALUA state change request.
597 static int core_alua_check_transition(int state, int *primary)
599 switch (state) {
600 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
601 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
602 case ALUA_ACCESS_STATE_STANDBY:
603 case ALUA_ACCESS_STATE_UNAVAILABLE:
605 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
606 * defined as primary target port asymmetric access states.
608 *primary = 1;
609 break;
610 case ALUA_ACCESS_STATE_OFFLINE:
612 * OFFLINE state is defined as a secondary target port
613 * asymmetric access state.
615 *primary = 0;
616 break;
617 default:
618 pr_err("Unknown ALUA access state: 0x%02x\n", state);
619 return -EINVAL;
622 return 0;
625 static char *core_alua_dump_state(int state)
627 switch (state) {
628 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
629 return "Active/Optimized";
630 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
631 return "Active/NonOptimized";
632 case ALUA_ACCESS_STATE_STANDBY:
633 return "Standby";
634 case ALUA_ACCESS_STATE_UNAVAILABLE:
635 return "Unavailable";
636 case ALUA_ACCESS_STATE_OFFLINE:
637 return "Offline";
638 default:
639 return "Unknown";
642 return NULL;
645 char *core_alua_dump_status(int status)
647 switch (status) {
648 case ALUA_STATUS_NONE:
649 return "None";
650 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
651 return "Altered by Explict STPG";
652 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
653 return "Altered by Implict ALUA";
654 default:
655 return "Unknown";
658 return NULL;
662 * Used by fabric modules to determine when we need to delay processing
663 * for the Active/NonOptimized paths..
665 int core_alua_check_nonop_delay(
666 struct se_cmd *cmd)
668 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
669 return 0;
670 if (in_interrupt())
671 return 0;
673 * The ALUA Active/NonOptimized access state delay can be disabled
674 * in via configfs with a value of zero
676 if (!cmd->alua_nonop_delay)
677 return 0;
679 * struct se_cmd->alua_nonop_delay gets set by a target port group
680 * defined interval in core_alua_state_nonoptimized()
682 msleep_interruptible(cmd->alua_nonop_delay);
683 return 0;
685 EXPORT_SYMBOL(core_alua_check_nonop_delay);
688 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
691 static int core_alua_write_tpg_metadata(
692 const char *path,
693 unsigned char *md_buf,
694 u32 md_buf_len)
696 mm_segment_t old_fs;
697 struct file *file;
698 struct iovec iov[1];
699 int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
701 memset(iov, 0, sizeof(struct iovec));
703 file = filp_open(path, flags, 0600);
704 if (IS_ERR(file) || !file || !file->f_dentry) {
705 pr_err("filp_open(%s) for ALUA metadata failed\n",
706 path);
707 return -ENODEV;
710 iov[0].iov_base = &md_buf[0];
711 iov[0].iov_len = md_buf_len;
713 old_fs = get_fs();
714 set_fs(get_ds());
715 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
716 set_fs(old_fs);
718 if (ret < 0) {
719 pr_err("Error writing ALUA metadata file: %s\n", path);
720 filp_close(file, NULL);
721 return -EIO;
723 filp_close(file, NULL);
725 return 0;
729 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
731 static int core_alua_update_tpg_primary_metadata(
732 struct t10_alua_tg_pt_gp *tg_pt_gp,
733 int primary_state,
734 unsigned char *md_buf)
736 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
737 struct t10_wwn *wwn = &su_dev->t10_wwn;
738 char path[ALUA_METADATA_PATH_LEN];
739 int len;
741 memset(path, 0, ALUA_METADATA_PATH_LEN);
743 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
744 "tg_pt_gp_id=%hu\n"
745 "alua_access_state=0x%02x\n"
746 "alua_access_status=0x%02x\n",
747 tg_pt_gp->tg_pt_gp_id, primary_state,
748 tg_pt_gp->tg_pt_gp_alua_access_status);
750 snprintf(path, ALUA_METADATA_PATH_LEN,
751 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
752 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
754 return core_alua_write_tpg_metadata(path, md_buf, len);
757 static int core_alua_do_transition_tg_pt(
758 struct t10_alua_tg_pt_gp *tg_pt_gp,
759 struct se_port *l_port,
760 struct se_node_acl *nacl,
761 unsigned char *md_buf,
762 int new_state,
763 int explict)
765 struct se_dev_entry *se_deve;
766 struct se_lun_acl *lacl;
767 struct se_port *port;
768 struct t10_alua_tg_pt_gp_member *mem;
769 int old_state = 0;
771 * Save the old primary ALUA access state, and set the current state
772 * to ALUA_ACCESS_STATE_TRANSITION.
774 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
775 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
776 ALUA_ACCESS_STATE_TRANSITION);
777 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
778 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
779 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
781 * Check for the optional ALUA primary state transition delay
783 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
784 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
786 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
787 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
788 tg_pt_gp_mem_list) {
789 port = mem->tg_pt;
791 * After an implicit target port asymmetric access state
792 * change, a device server shall establish a unit attention
793 * condition for the initiator port associated with every I_T
794 * nexus with the additional sense code set to ASYMMETRIC
795 * ACCESS STATE CHAGED.
797 * After an explicit target port asymmetric access state
798 * change, a device server shall establish a unit attention
799 * condition with the additional sense code set to ASYMMETRIC
800 * ACCESS STATE CHANGED for the initiator port associated with
801 * every I_T nexus other than the I_T nexus on which the SET
802 * TARGET PORT GROUPS command
804 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
805 smp_mb__after_atomic_inc();
806 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
808 spin_lock_bh(&port->sep_alua_lock);
809 list_for_each_entry(se_deve, &port->sep_alua_list,
810 alua_port_list) {
811 lacl = se_deve->se_lun_acl;
813 * se_deve->se_lun_acl pointer may be NULL for a
814 * entry created without explict Node+MappedLUN ACLs
816 if (!lacl)
817 continue;
819 if (explict &&
820 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
821 (l_port != NULL) && (l_port == port))
822 continue;
824 core_scsi3_ua_allocate(lacl->se_lun_nacl,
825 se_deve->mapped_lun, 0x2A,
826 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
828 spin_unlock_bh(&port->sep_alua_lock);
830 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
831 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
832 smp_mb__after_atomic_dec();
834 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
836 * Update the ALUA metadata buf that has been allocated in
837 * core_alua_do_port_transition(), this metadata will be written
838 * to struct file.
840 * Note that there is the case where we do not want to update the
841 * metadata when the saved metadata is being parsed in userspace
842 * when setting the existing port access state and access status.
844 * Also note that the failure to write out the ALUA metadata to
845 * struct file does NOT affect the actual ALUA transition.
847 if (tg_pt_gp->tg_pt_gp_write_metadata) {
848 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
849 core_alua_update_tpg_primary_metadata(tg_pt_gp,
850 new_state, md_buf);
851 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
854 * Set the current primary ALUA access state to the requested new state
856 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
858 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
859 " from primary access state %s to %s\n", (explict) ? "explict" :
860 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
861 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
862 core_alua_dump_state(new_state));
864 return 0;
867 int core_alua_do_port_transition(
868 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
869 struct se_device *l_dev,
870 struct se_port *l_port,
871 struct se_node_acl *l_nacl,
872 int new_state,
873 int explict)
875 struct se_device *dev;
876 struct se_port *port;
877 struct se_subsystem_dev *su_dev;
878 struct se_node_acl *nacl;
879 struct t10_alua_lu_gp *lu_gp;
880 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
881 struct t10_alua_tg_pt_gp *tg_pt_gp;
882 unsigned char *md_buf;
883 int primary;
885 if (core_alua_check_transition(new_state, &primary) != 0)
886 return -EINVAL;
888 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
889 if (!md_buf) {
890 pr_err("Unable to allocate buf for ALUA metadata\n");
891 return -ENOMEM;
894 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
895 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
896 lu_gp = local_lu_gp_mem->lu_gp;
897 atomic_inc(&lu_gp->lu_gp_ref_cnt);
898 smp_mb__after_atomic_inc();
899 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
901 * For storage objects that are members of the 'default_lu_gp',
902 * we only do transition on the passed *l_tp_pt_gp, and not
903 * on all of the matching target port groups IDs in default_lu_gp.
905 if (!lu_gp->lu_gp_id) {
907 * core_alua_do_transition_tg_pt() will always return
908 * success.
910 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
911 md_buf, new_state, explict);
912 atomic_dec(&lu_gp->lu_gp_ref_cnt);
913 smp_mb__after_atomic_dec();
914 kfree(md_buf);
915 return 0;
918 * For all other LU groups aside from 'default_lu_gp', walk all of
919 * the associated storage objects looking for a matching target port
920 * group ID from the local target port group.
922 spin_lock(&lu_gp->lu_gp_lock);
923 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
924 lu_gp_mem_list) {
926 dev = lu_gp_mem->lu_gp_mem_dev;
927 su_dev = dev->se_sub_dev;
928 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
929 smp_mb__after_atomic_inc();
930 spin_unlock(&lu_gp->lu_gp_lock);
932 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
933 list_for_each_entry(tg_pt_gp,
934 &su_dev->t10_alua.tg_pt_gps_list,
935 tg_pt_gp_list) {
937 if (!tg_pt_gp->tg_pt_gp_valid_id)
938 continue;
940 * If the target behavior port asymmetric access state
941 * is changed for any target port group accessiable via
942 * a logical unit within a LU group, the target port
943 * behavior group asymmetric access states for the same
944 * target port group accessible via other logical units
945 * in that LU group will also change.
947 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
948 continue;
950 if (l_tg_pt_gp == tg_pt_gp) {
951 port = l_port;
952 nacl = l_nacl;
953 } else {
954 port = NULL;
955 nacl = NULL;
957 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
958 smp_mb__after_atomic_inc();
959 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
961 * core_alua_do_transition_tg_pt() will always return
962 * success.
964 core_alua_do_transition_tg_pt(tg_pt_gp, port,
965 nacl, md_buf, new_state, explict);
967 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
968 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
969 smp_mb__after_atomic_dec();
971 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
973 spin_lock(&lu_gp->lu_gp_lock);
974 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
975 smp_mb__after_atomic_dec();
977 spin_unlock(&lu_gp->lu_gp_lock);
979 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
980 " Group IDs: %hu %s transition to primary state: %s\n",
981 config_item_name(&lu_gp->lu_gp_group.cg_item),
982 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
983 core_alua_dump_state(new_state));
985 atomic_dec(&lu_gp->lu_gp_ref_cnt);
986 smp_mb__after_atomic_dec();
987 kfree(md_buf);
988 return 0;
992 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
994 static int core_alua_update_tpg_secondary_metadata(
995 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
996 struct se_port *port,
997 unsigned char *md_buf,
998 u32 md_buf_len)
1000 struct se_portal_group *se_tpg = port->sep_tpg;
1001 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1002 int len;
1004 memset(path, 0, ALUA_METADATA_PATH_LEN);
1005 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1007 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1008 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1010 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1011 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1012 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1014 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1015 "alua_tg_pt_status=0x%02x\n",
1016 atomic_read(&port->sep_tg_pt_secondary_offline),
1017 port->sep_tg_pt_secondary_stat);
1019 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1020 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1021 port->sep_lun->unpacked_lun);
1023 return core_alua_write_tpg_metadata(path, md_buf, len);
1026 static int core_alua_set_tg_pt_secondary_state(
1027 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1028 struct se_port *port,
1029 int explict,
1030 int offline)
1032 struct t10_alua_tg_pt_gp *tg_pt_gp;
1033 unsigned char *md_buf;
1034 u32 md_buf_len;
1035 int trans_delay_msecs;
1037 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1038 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1039 if (!tg_pt_gp) {
1040 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1041 pr_err("Unable to complete secondary state"
1042 " transition\n");
1043 return -EINVAL;
1045 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1047 * Set the secondary ALUA target port access state to OFFLINE
1048 * or release the previously secondary state for struct se_port
1050 if (offline)
1051 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1052 else
1053 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1055 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1056 port->sep_tg_pt_secondary_stat = (explict) ?
1057 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1058 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1060 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1061 " to secondary access state: %s\n", (explict) ? "explict" :
1062 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1063 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1065 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1067 * Do the optional transition delay after we set the secondary
1068 * ALUA access state.
1070 if (trans_delay_msecs != 0)
1071 msleep_interruptible(trans_delay_msecs);
1073 * See if we need to update the ALUA fabric port metadata for
1074 * secondary state and status
1076 if (port->sep_tg_pt_secondary_write_md) {
1077 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1078 if (!md_buf) {
1079 pr_err("Unable to allocate md_buf for"
1080 " secondary ALUA access metadata\n");
1081 return -ENOMEM;
1083 mutex_lock(&port->sep_tg_pt_md_mutex);
1084 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1085 md_buf, md_buf_len);
1086 mutex_unlock(&port->sep_tg_pt_md_mutex);
1088 kfree(md_buf);
1091 return 0;
1094 struct t10_alua_lu_gp *
1095 core_alua_allocate_lu_gp(const char *name, int def_group)
1097 struct t10_alua_lu_gp *lu_gp;
1099 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1100 if (!lu_gp) {
1101 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1102 return ERR_PTR(-ENOMEM);
1104 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1105 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1106 spin_lock_init(&lu_gp->lu_gp_lock);
1107 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1109 if (def_group) {
1110 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1111 lu_gp->lu_gp_valid_id = 1;
1112 alua_lu_gps_count++;
1115 return lu_gp;
1118 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1120 struct t10_alua_lu_gp *lu_gp_tmp;
1121 u16 lu_gp_id_tmp;
1123 * The lu_gp->lu_gp_id may only be set once..
1125 if (lu_gp->lu_gp_valid_id) {
1126 pr_warn("ALUA LU Group already has a valid ID,"
1127 " ignoring request\n");
1128 return -EINVAL;
1131 spin_lock(&lu_gps_lock);
1132 if (alua_lu_gps_count == 0x0000ffff) {
1133 pr_err("Maximum ALUA alua_lu_gps_count:"
1134 " 0x0000ffff reached\n");
1135 spin_unlock(&lu_gps_lock);
1136 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1137 return -ENOSPC;
1139 again:
1140 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1141 alua_lu_gps_counter++;
1143 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1144 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1145 if (!lu_gp_id)
1146 goto again;
1148 pr_warn("ALUA Logical Unit Group ID: %hu"
1149 " already exists, ignoring request\n",
1150 lu_gp_id);
1151 spin_unlock(&lu_gps_lock);
1152 return -EINVAL;
1156 lu_gp->lu_gp_id = lu_gp_id_tmp;
1157 lu_gp->lu_gp_valid_id = 1;
1158 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1159 alua_lu_gps_count++;
1160 spin_unlock(&lu_gps_lock);
1162 return 0;
1165 static struct t10_alua_lu_gp_member *
1166 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1168 struct t10_alua_lu_gp_member *lu_gp_mem;
1170 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1171 if (!lu_gp_mem) {
1172 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1173 return ERR_PTR(-ENOMEM);
1175 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1176 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1177 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1179 lu_gp_mem->lu_gp_mem_dev = dev;
1180 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1182 return lu_gp_mem;
1185 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1187 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1189 * Once we have reached this point, config_item_put() has
1190 * already been called from target_core_alua_drop_lu_gp().
1192 * Here, we remove the *lu_gp from the global list so that
1193 * no associations can be made while we are releasing
1194 * struct t10_alua_lu_gp.
1196 spin_lock(&lu_gps_lock);
1197 list_del(&lu_gp->lu_gp_node);
1198 alua_lu_gps_count--;
1199 spin_unlock(&lu_gps_lock);
1201 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1202 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1203 * released with core_alua_put_lu_gp_from_name()
1205 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1206 cpu_relax();
1208 * Release reference to struct t10_alua_lu_gp * from all associated
1209 * struct se_device.
1211 spin_lock(&lu_gp->lu_gp_lock);
1212 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1213 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1214 if (lu_gp_mem->lu_gp_assoc) {
1215 list_del(&lu_gp_mem->lu_gp_mem_list);
1216 lu_gp->lu_gp_members--;
1217 lu_gp_mem->lu_gp_assoc = 0;
1219 spin_unlock(&lu_gp->lu_gp_lock);
1222 * lu_gp_mem is associated with a single
1223 * struct se_device->dev_alua_lu_gp_mem, and is released when
1224 * struct se_device is released via core_alua_free_lu_gp_mem().
1226 * If the passed lu_gp does NOT match the default_lu_gp, assume
1227 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1229 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1230 if (lu_gp != default_lu_gp)
1231 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1232 default_lu_gp);
1233 else
1234 lu_gp_mem->lu_gp = NULL;
1235 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1237 spin_lock(&lu_gp->lu_gp_lock);
1239 spin_unlock(&lu_gp->lu_gp_lock);
1241 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1244 void core_alua_free_lu_gp_mem(struct se_device *dev)
1246 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1247 struct t10_alua *alua = &su_dev->t10_alua;
1248 struct t10_alua_lu_gp *lu_gp;
1249 struct t10_alua_lu_gp_member *lu_gp_mem;
1251 if (alua->alua_type != SPC3_ALUA_EMULATED)
1252 return;
1254 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1255 if (!lu_gp_mem)
1256 return;
1258 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1259 cpu_relax();
1261 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1262 lu_gp = lu_gp_mem->lu_gp;
1263 if (lu_gp) {
1264 spin_lock(&lu_gp->lu_gp_lock);
1265 if (lu_gp_mem->lu_gp_assoc) {
1266 list_del(&lu_gp_mem->lu_gp_mem_list);
1267 lu_gp->lu_gp_members--;
1268 lu_gp_mem->lu_gp_assoc = 0;
1270 spin_unlock(&lu_gp->lu_gp_lock);
1271 lu_gp_mem->lu_gp = NULL;
1273 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1275 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1278 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1280 struct t10_alua_lu_gp *lu_gp;
1281 struct config_item *ci;
1283 spin_lock(&lu_gps_lock);
1284 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1285 if (!lu_gp->lu_gp_valid_id)
1286 continue;
1287 ci = &lu_gp->lu_gp_group.cg_item;
1288 if (!strcmp(config_item_name(ci), name)) {
1289 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1290 spin_unlock(&lu_gps_lock);
1291 return lu_gp;
1294 spin_unlock(&lu_gps_lock);
1296 return NULL;
1299 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1301 spin_lock(&lu_gps_lock);
1302 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1303 spin_unlock(&lu_gps_lock);
1307 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1309 void __core_alua_attach_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 lu_gp_mem->lu_gp = lu_gp;
1315 lu_gp_mem->lu_gp_assoc = 1;
1316 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1317 lu_gp->lu_gp_members++;
1318 spin_unlock(&lu_gp->lu_gp_lock);
1322 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1324 void __core_alua_drop_lu_gp_mem(
1325 struct t10_alua_lu_gp_member *lu_gp_mem,
1326 struct t10_alua_lu_gp *lu_gp)
1328 spin_lock(&lu_gp->lu_gp_lock);
1329 list_del(&lu_gp_mem->lu_gp_mem_list);
1330 lu_gp_mem->lu_gp = NULL;
1331 lu_gp_mem->lu_gp_assoc = 0;
1332 lu_gp->lu_gp_members--;
1333 spin_unlock(&lu_gp->lu_gp_lock);
1336 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
1337 struct se_subsystem_dev *su_dev,
1338 const char *name,
1339 int def_group)
1341 struct t10_alua_tg_pt_gp *tg_pt_gp;
1343 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1344 if (!tg_pt_gp) {
1345 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1346 return NULL;
1348 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1349 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1350 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1351 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1352 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1353 tg_pt_gp->tg_pt_gp_su_dev = su_dev;
1354 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1355 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1356 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1358 * Enable both explict and implict ALUA support by default
1360 tg_pt_gp->tg_pt_gp_alua_access_type =
1361 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1363 * Set the default Active/NonOptimized Delay in milliseconds
1365 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1366 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1368 if (def_group) {
1369 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1370 tg_pt_gp->tg_pt_gp_id =
1371 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1372 tg_pt_gp->tg_pt_gp_valid_id = 1;
1373 su_dev->t10_alua.alua_tg_pt_gps_count++;
1374 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1375 &su_dev->t10_alua.tg_pt_gps_list);
1376 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1379 return tg_pt_gp;
1382 int core_alua_set_tg_pt_gp_id(
1383 struct t10_alua_tg_pt_gp *tg_pt_gp,
1384 u16 tg_pt_gp_id)
1386 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1387 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1388 u16 tg_pt_gp_id_tmp;
1390 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1392 if (tg_pt_gp->tg_pt_gp_valid_id) {
1393 pr_warn("ALUA TG PT Group already has a valid ID,"
1394 " ignoring request\n");
1395 return -EINVAL;
1398 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1399 if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1400 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1401 " 0x0000ffff reached\n");
1402 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1403 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1404 return -ENOSPC;
1406 again:
1407 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1408 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1410 list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
1411 tg_pt_gp_list) {
1412 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1413 if (!tg_pt_gp_id)
1414 goto again;
1416 pr_err("ALUA Target Port Group ID: %hu already"
1417 " exists, ignoring request\n", tg_pt_gp_id);
1418 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1419 return -EINVAL;
1423 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1424 tg_pt_gp->tg_pt_gp_valid_id = 1;
1425 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1426 &su_dev->t10_alua.tg_pt_gps_list);
1427 su_dev->t10_alua.alua_tg_pt_gps_count++;
1428 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1430 return 0;
1433 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1434 struct se_port *port)
1436 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1438 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1439 GFP_KERNEL);
1440 if (!tg_pt_gp_mem) {
1441 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1442 return ERR_PTR(-ENOMEM);
1444 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1445 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1446 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1448 tg_pt_gp_mem->tg_pt = port;
1449 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1451 return tg_pt_gp_mem;
1454 void core_alua_free_tg_pt_gp(
1455 struct t10_alua_tg_pt_gp *tg_pt_gp)
1457 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1458 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1460 * Once we have reached this point, config_item_put() has already
1461 * been called from target_core_alua_drop_tg_pt_gp().
1463 * Here we remove *tg_pt_gp from the global list so that
1464 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1465 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1467 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1468 list_del(&tg_pt_gp->tg_pt_gp_list);
1469 su_dev->t10_alua.alua_tg_pt_gps_counter--;
1470 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1472 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1473 * core_alua_get_tg_pt_gp_by_name() in
1474 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1475 * to be released with core_alua_put_tg_pt_gp_from_name().
1477 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1478 cpu_relax();
1480 * Release reference to struct t10_alua_tg_pt_gp from all associated
1481 * struct se_port.
1483 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1484 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1485 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1486 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1487 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1488 tg_pt_gp->tg_pt_gp_members--;
1489 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1491 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1493 * tg_pt_gp_mem is associated with a single
1494 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1495 * core_alua_free_tg_pt_gp_mem().
1497 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1498 * assume we want to re-assocate a given tg_pt_gp_mem with
1499 * default_tg_pt_gp.
1501 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1502 if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
1503 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1504 su_dev->t10_alua.default_tg_pt_gp);
1505 } else
1506 tg_pt_gp_mem->tg_pt_gp = NULL;
1507 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1509 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1511 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1513 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1516 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1518 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1519 struct t10_alua *alua = &su_dev->t10_alua;
1520 struct t10_alua_tg_pt_gp *tg_pt_gp;
1521 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1523 if (alua->alua_type != SPC3_ALUA_EMULATED)
1524 return;
1526 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1527 if (!tg_pt_gp_mem)
1528 return;
1530 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1531 cpu_relax();
1533 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1534 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1535 if (tg_pt_gp) {
1536 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1537 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1538 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1539 tg_pt_gp->tg_pt_gp_members--;
1540 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1542 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1543 tg_pt_gp_mem->tg_pt_gp = NULL;
1545 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1547 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1550 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1551 struct se_subsystem_dev *su_dev,
1552 const char *name)
1554 struct t10_alua_tg_pt_gp *tg_pt_gp;
1555 struct config_item *ci;
1557 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1558 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
1559 tg_pt_gp_list) {
1560 if (!tg_pt_gp->tg_pt_gp_valid_id)
1561 continue;
1562 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1563 if (!strcmp(config_item_name(ci), name)) {
1564 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1565 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1566 return tg_pt_gp;
1569 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1571 return NULL;
1574 static void core_alua_put_tg_pt_gp_from_name(
1575 struct t10_alua_tg_pt_gp *tg_pt_gp)
1577 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1579 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1580 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1581 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1585 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1587 void __core_alua_attach_tg_pt_gp_mem(
1588 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1589 struct t10_alua_tg_pt_gp *tg_pt_gp)
1591 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1592 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1593 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1594 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1595 &tg_pt_gp->tg_pt_gp_mem_list);
1596 tg_pt_gp->tg_pt_gp_members++;
1597 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1601 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1603 static void __core_alua_drop_tg_pt_gp_mem(
1604 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1605 struct t10_alua_tg_pt_gp *tg_pt_gp)
1607 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1608 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1609 tg_pt_gp_mem->tg_pt_gp = NULL;
1610 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1611 tg_pt_gp->tg_pt_gp_members--;
1612 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1615 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1617 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1618 struct config_item *tg_pt_ci;
1619 struct t10_alua *alua = &su_dev->t10_alua;
1620 struct t10_alua_tg_pt_gp *tg_pt_gp;
1621 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1622 ssize_t len = 0;
1624 if (alua->alua_type != SPC3_ALUA_EMULATED)
1625 return len;
1627 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1628 if (!tg_pt_gp_mem)
1629 return len;
1631 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1632 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1633 if (tg_pt_gp) {
1634 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1635 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1636 " %hu\nTG Port Primary Access State: %s\nTG Port "
1637 "Primary Access Status: %s\nTG Port Secondary Access"
1638 " State: %s\nTG Port Secondary Access Status: %s\n",
1639 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1640 core_alua_dump_state(atomic_read(
1641 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1642 core_alua_dump_status(
1643 tg_pt_gp->tg_pt_gp_alua_access_status),
1644 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1645 "Offline" : "None",
1646 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1648 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1650 return len;
1653 ssize_t core_alua_store_tg_pt_gp_info(
1654 struct se_port *port,
1655 const char *page,
1656 size_t count)
1658 struct se_portal_group *tpg;
1659 struct se_lun *lun;
1660 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1661 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1662 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1663 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1664 int move = 0;
1666 tpg = port->sep_tpg;
1667 lun = port->sep_lun;
1669 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1670 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1671 " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1672 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1673 config_item_name(&lun->lun_group.cg_item));
1674 return -EINVAL;
1677 if (count > TG_PT_GROUP_NAME_BUF) {
1678 pr_err("ALUA Target Port Group alias too large!\n");
1679 return -EINVAL;
1681 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1682 memcpy(buf, page, count);
1684 * Any ALUA target port group alias besides "NULL" means we will be
1685 * making a new group association.
1687 if (strcmp(strstrip(buf), "NULL")) {
1689 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1690 * struct t10_alua_tg_pt_gp. This reference is released with
1691 * core_alua_put_tg_pt_gp_from_name() below.
1693 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
1694 strstrip(buf));
1695 if (!tg_pt_gp_new)
1696 return -ENODEV;
1698 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1699 if (!tg_pt_gp_mem) {
1700 if (tg_pt_gp_new)
1701 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1702 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1703 return -EINVAL;
1706 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1707 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1708 if (tg_pt_gp) {
1710 * Clearing an existing tg_pt_gp association, and replacing
1711 * with the default_tg_pt_gp.
1713 if (!tg_pt_gp_new) {
1714 pr_debug("Target_Core_ConfigFS: Moving"
1715 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1716 " alua/%s, ID: %hu back to"
1717 " default_tg_pt_gp\n",
1718 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1719 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1720 config_item_name(&lun->lun_group.cg_item),
1721 config_item_name(
1722 &tg_pt_gp->tg_pt_gp_group.cg_item),
1723 tg_pt_gp->tg_pt_gp_id);
1725 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1726 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1727 su_dev->t10_alua.default_tg_pt_gp);
1728 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1730 return count;
1733 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1735 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1736 move = 1;
1739 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1741 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1742 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1743 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1744 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1745 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1746 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1747 config_item_name(&lun->lun_group.cg_item),
1748 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1749 tg_pt_gp_new->tg_pt_gp_id);
1751 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1752 return count;
1755 ssize_t core_alua_show_access_type(
1756 struct t10_alua_tg_pt_gp *tg_pt_gp,
1757 char *page)
1759 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1760 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1761 return sprintf(page, "Implict and Explict\n");
1762 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1763 return sprintf(page, "Implict\n");
1764 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1765 return sprintf(page, "Explict\n");
1766 else
1767 return sprintf(page, "None\n");
1770 ssize_t core_alua_store_access_type(
1771 struct t10_alua_tg_pt_gp *tg_pt_gp,
1772 const char *page,
1773 size_t count)
1775 unsigned long tmp;
1776 int ret;
1778 ret = strict_strtoul(page, 0, &tmp);
1779 if (ret < 0) {
1780 pr_err("Unable to extract alua_access_type\n");
1781 return -EINVAL;
1783 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1784 pr_err("Illegal value for alua_access_type:"
1785 " %lu\n", tmp);
1786 return -EINVAL;
1788 if (tmp == 3)
1789 tg_pt_gp->tg_pt_gp_alua_access_type =
1790 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1791 else if (tmp == 2)
1792 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1793 else if (tmp == 1)
1794 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1795 else
1796 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1798 return count;
1801 ssize_t core_alua_show_nonop_delay_msecs(
1802 struct t10_alua_tg_pt_gp *tg_pt_gp,
1803 char *page)
1805 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1808 ssize_t core_alua_store_nonop_delay_msecs(
1809 struct t10_alua_tg_pt_gp *tg_pt_gp,
1810 const char *page,
1811 size_t count)
1813 unsigned long tmp;
1814 int ret;
1816 ret = strict_strtoul(page, 0, &tmp);
1817 if (ret < 0) {
1818 pr_err("Unable to extract nonop_delay_msecs\n");
1819 return -EINVAL;
1821 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1822 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1823 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1824 ALUA_MAX_NONOP_DELAY_MSECS);
1825 return -EINVAL;
1827 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1829 return count;
1832 ssize_t core_alua_show_trans_delay_msecs(
1833 struct t10_alua_tg_pt_gp *tg_pt_gp,
1834 char *page)
1836 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1839 ssize_t core_alua_store_trans_delay_msecs(
1840 struct t10_alua_tg_pt_gp *tg_pt_gp,
1841 const char *page,
1842 size_t count)
1844 unsigned long tmp;
1845 int ret;
1847 ret = strict_strtoul(page, 0, &tmp);
1848 if (ret < 0) {
1849 pr_err("Unable to extract trans_delay_msecs\n");
1850 return -EINVAL;
1852 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1853 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1854 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1855 ALUA_MAX_TRANS_DELAY_MSECS);
1856 return -EINVAL;
1858 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1860 return count;
1863 ssize_t core_alua_show_preferred_bit(
1864 struct t10_alua_tg_pt_gp *tg_pt_gp,
1865 char *page)
1867 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1870 ssize_t core_alua_store_preferred_bit(
1871 struct t10_alua_tg_pt_gp *tg_pt_gp,
1872 const char *page,
1873 size_t count)
1875 unsigned long tmp;
1876 int ret;
1878 ret = strict_strtoul(page, 0, &tmp);
1879 if (ret < 0) {
1880 pr_err("Unable to extract preferred ALUA value\n");
1881 return -EINVAL;
1883 if ((tmp != 0) && (tmp != 1)) {
1884 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1885 return -EINVAL;
1887 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1889 return count;
1892 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1894 if (!lun->lun_sep)
1895 return -ENODEV;
1897 return sprintf(page, "%d\n",
1898 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1901 ssize_t core_alua_store_offline_bit(
1902 struct se_lun *lun,
1903 const char *page,
1904 size_t count)
1906 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1907 unsigned long tmp;
1908 int ret;
1910 if (!lun->lun_sep)
1911 return -ENODEV;
1913 ret = strict_strtoul(page, 0, &tmp);
1914 if (ret < 0) {
1915 pr_err("Unable to extract alua_tg_pt_offline value\n");
1916 return -EINVAL;
1918 if ((tmp != 0) && (tmp != 1)) {
1919 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1920 tmp);
1921 return -EINVAL;
1923 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1924 if (!tg_pt_gp_mem) {
1925 pr_err("Unable to locate *tg_pt_gp_mem\n");
1926 return -EINVAL;
1929 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1930 lun->lun_sep, 0, (int)tmp);
1931 if (ret < 0)
1932 return -EINVAL;
1934 return count;
1937 ssize_t core_alua_show_secondary_status(
1938 struct se_lun *lun,
1939 char *page)
1941 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1944 ssize_t core_alua_store_secondary_status(
1945 struct se_lun *lun,
1946 const char *page,
1947 size_t count)
1949 unsigned long tmp;
1950 int ret;
1952 ret = strict_strtoul(page, 0, &tmp);
1953 if (ret < 0) {
1954 pr_err("Unable to extract alua_tg_pt_status\n");
1955 return -EINVAL;
1957 if ((tmp != ALUA_STATUS_NONE) &&
1958 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1959 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1960 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1961 tmp);
1962 return -EINVAL;
1964 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1966 return count;
1969 ssize_t core_alua_show_secondary_write_metadata(
1970 struct se_lun *lun,
1971 char *page)
1973 return sprintf(page, "%d\n",
1974 lun->lun_sep->sep_tg_pt_secondary_write_md);
1977 ssize_t core_alua_store_secondary_write_metadata(
1978 struct se_lun *lun,
1979 const char *page,
1980 size_t count)
1982 unsigned long tmp;
1983 int ret;
1985 ret = strict_strtoul(page, 0, &tmp);
1986 if (ret < 0) {
1987 pr_err("Unable to extract alua_tg_pt_write_md\n");
1988 return -EINVAL;
1990 if ((tmp != 0) && (tmp != 1)) {
1991 pr_err("Illegal value for alua_tg_pt_write_md:"
1992 " %lu\n", tmp);
1993 return -EINVAL;
1995 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
1997 return count;
2000 int core_setup_alua(struct se_device *dev, int force_pt)
2002 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
2003 struct t10_alua *alua = &su_dev->t10_alua;
2004 struct t10_alua_lu_gp_member *lu_gp_mem;
2006 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
2007 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
2008 * cause a problem because libata and some SATA RAID HBAs appear
2009 * under Linux/SCSI, but emulate SCSI logic themselves.
2011 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
2012 !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
2013 alua->alua_type = SPC_ALUA_PASSTHROUGH;
2014 alua->alua_state_check = &core_alua_state_check_nop;
2015 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2016 " emulation\n", dev->transport->name);
2017 return 0;
2020 * If SPC-3 or above is reported by real or emulated struct se_device,
2021 * use emulated ALUA.
2023 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
2024 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2025 " device\n", dev->transport->name);
2027 * Associate this struct se_device with the default ALUA
2028 * LUN Group.
2030 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2031 if (IS_ERR(lu_gp_mem))
2032 return PTR_ERR(lu_gp_mem);
2034 alua->alua_type = SPC3_ALUA_EMULATED;
2035 alua->alua_state_check = &core_alua_state_check;
2036 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2037 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2038 default_lu_gp);
2039 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2041 pr_debug("%s: Adding to default ALUA LU Group:"
2042 " core/alua/lu_gps/default_lu_gp\n",
2043 dev->transport->name);
2044 } else {
2045 alua->alua_type = SPC2_ALUA_DISABLED;
2046 alua->alua_state_check = &core_alua_state_check_nop;
2047 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2048 " device\n", dev->transport->name);
2051 return 0;