arm: mvebu: PCIe Device Tree informations for OpenBlocks AX3-4
[linux-2.6/btrfs-unstable.git] / drivers / target / target_core_alua.c
blobff1c5ee352cb91107d61f09ae277a33acff9fa26
1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * (c) Copyright 2009-2012 RisingTide Systems LLC.
8 * Nicholas A. Bellinger <nab@kernel.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 ******************************************************************************/
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/configfs.h>
29 #include <linux/export.h>
30 #include <linux/file.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 sense_reason_t 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 sense_reason_t
63 target_emulate_report_target_port_groups(struct se_cmd *cmd)
65 struct se_device *dev = cmd->se_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;
71 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
74 * Skip over RESERVED area to first Target port group descriptor
75 * depending on the PARAMETER DATA FORMAT type..
77 if (ext_hdr != 0)
78 off = 8;
79 else
80 off = 4;
82 if (cmd->data_length < off) {
83 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
84 " small for %s header\n", cmd->data_length,
85 (ext_hdr) ? "extended" : "normal");
86 return TCM_INVALID_CDB_FIELD;
88 buf = transport_kmap_data_sg(cmd);
89 if (!buf)
90 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
92 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
93 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
94 tg_pt_gp_list) {
96 * Check if the Target port group and Target port descriptor list
97 * based on tg_pt_gp_members count will fit into the response payload.
98 * Otherwise, bump rd_len to let the initiator know we have exceeded
99 * the allocation length and the response is truncated.
101 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
102 cmd->data_length) {
103 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
104 continue;
107 * PREF: Preferred target port bit, determine if this
108 * bit should be set for port group.
110 if (tg_pt_gp->tg_pt_gp_pref)
111 buf[off] = 0x80;
113 * Set the ASYMMETRIC ACCESS State
115 buf[off++] |= (atomic_read(
116 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
118 * Set supported ASYMMETRIC ACCESS State bits
120 buf[off] = 0x80; /* T_SUP */
121 buf[off] |= 0x40; /* O_SUP */
122 buf[off] |= 0x8; /* U_SUP */
123 buf[off] |= 0x4; /* S_SUP */
124 buf[off] |= 0x2; /* AN_SUP */
125 buf[off++] |= 0x1; /* AO_SUP */
127 * TARGET PORT GROUP
129 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
130 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
132 off++; /* Skip over Reserved */
134 * STATUS CODE
136 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
138 * Vendor Specific field
140 buf[off++] = 0x00;
142 * TARGET PORT COUNT
144 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
145 rd_len += 8;
147 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
148 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
149 tg_pt_gp_mem_list) {
150 port = tg_pt_gp_mem->tg_pt;
152 * Start Target Port descriptor format
154 * See spc4r17 section 6.2.7 Table 247
156 off += 2; /* Skip over Obsolete */
158 * Set RELATIVE TARGET PORT IDENTIFIER
160 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
161 buf[off++] = (port->sep_rtpi & 0xff);
162 rd_len += 4;
164 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
166 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
168 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
170 put_unaligned_be32(rd_len, &buf[0]);
173 * Fill in the Extended header parameter data format if requested
175 if (ext_hdr != 0) {
176 buf[4] = 0x10;
178 * Set the implict transition time (in seconds) for the application
179 * client to use as a base for it's transition timeout value.
181 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
182 * this CDB was received upon to determine this value individually
183 * for ALUA target port group.
185 port = cmd->se_lun->lun_sep;
186 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
187 if (tg_pt_gp_mem) {
188 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
189 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
190 if (tg_pt_gp)
191 buf[5] = tg_pt_gp->tg_pt_gp_implict_trans_secs;
192 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
195 transport_kunmap_data_sg(cmd);
197 target_complete_cmd(cmd, GOOD);
198 return 0;
202 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
204 * See spc4r17 section 6.35
206 sense_reason_t
207 target_emulate_set_target_port_groups(struct se_cmd *cmd)
209 struct se_device *dev = cmd->se_dev;
210 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
211 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
212 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
213 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
214 unsigned char *buf;
215 unsigned char *ptr;
216 sense_reason_t rc = TCM_NO_SENSE;
217 u32 len = 4; /* Skip over RESERVED area in header */
218 int alua_access_state, primary = 0;
219 u16 tg_pt_id, rtpi;
221 if (!l_port)
222 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
224 if (cmd->data_length < 4) {
225 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
226 " small\n", cmd->data_length);
227 return TCM_INVALID_PARAMETER_LIST;
230 buf = transport_kmap_data_sg(cmd);
231 if (!buf)
232 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
235 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
236 * for the local tg_pt_gp.
238 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
239 if (!l_tg_pt_gp_mem) {
240 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
241 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
242 goto out;
244 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
245 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
246 if (!l_tg_pt_gp) {
247 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
248 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
249 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
250 goto out;
252 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
254 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)) {
255 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
256 " while TPGS_EXPLICT_ALUA is disabled\n");
257 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
258 goto out;
261 ptr = &buf[4]; /* Skip over RESERVED area in header */
263 while (len < cmd->data_length) {
264 bool found = false;
265 alua_access_state = (ptr[0] & 0x0f);
267 * Check the received ALUA access state, and determine if
268 * the state is a primary or secondary target port asymmetric
269 * access state.
271 rc = core_alua_check_transition(alua_access_state, &primary);
272 if (rc) {
274 * If the SET TARGET PORT GROUPS attempts to establish
275 * an invalid combination of target port asymmetric
276 * access states or attempts to establish an
277 * unsupported target port asymmetric access state,
278 * then the command shall be terminated with CHECK
279 * CONDITION status, with the sense key set to ILLEGAL
280 * REQUEST, and the additional sense code set to INVALID
281 * FIELD IN PARAMETER LIST.
283 goto out;
287 * If the ASYMMETRIC ACCESS STATE field (see table 267)
288 * specifies a primary target port asymmetric access state,
289 * then the TARGET PORT GROUP OR TARGET PORT field specifies
290 * a primary target port group for which the primary target
291 * port asymmetric access state shall be changed. If the
292 * ASYMMETRIC ACCESS STATE field specifies a secondary target
293 * port asymmetric access state, then the TARGET PORT GROUP OR
294 * TARGET PORT field specifies the relative target port
295 * identifier (see 3.1.120) of the target port for which the
296 * secondary target port asymmetric access state shall be
297 * changed.
299 if (primary) {
300 tg_pt_id = get_unaligned_be16(ptr + 2);
302 * Locate the matching target port group ID from
303 * the global tg_pt_gp list
305 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
306 list_for_each_entry(tg_pt_gp,
307 &dev->t10_alua.tg_pt_gps_list,
308 tg_pt_gp_list) {
309 if (!tg_pt_gp->tg_pt_gp_valid_id)
310 continue;
312 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
313 continue;
315 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
316 smp_mb__after_atomic_inc();
318 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
320 if (!core_alua_do_port_transition(tg_pt_gp,
321 dev, l_port, nacl,
322 alua_access_state, 1))
323 found = true;
325 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
326 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
327 smp_mb__after_atomic_dec();
328 break;
330 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
331 } else {
333 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
334 * the Target Port in question for the the incoming
335 * SET_TARGET_PORT_GROUPS op.
337 rtpi = get_unaligned_be16(ptr + 2);
339 * Locate the matching relative target port identifier
340 * for the struct se_device storage object.
342 spin_lock(&dev->se_port_lock);
343 list_for_each_entry(port, &dev->dev_sep_list,
344 sep_list) {
345 if (port->sep_rtpi != rtpi)
346 continue;
348 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
350 spin_unlock(&dev->se_port_lock);
352 if (!core_alua_set_tg_pt_secondary_state(
353 tg_pt_gp_mem, port, 1, 1))
354 found = true;
356 spin_lock(&dev->se_port_lock);
357 break;
359 spin_unlock(&dev->se_port_lock);
362 if (!found) {
363 rc = TCM_INVALID_PARAMETER_LIST;
364 goto out;
367 ptr += 4;
368 len += 4;
371 out:
372 transport_kunmap_data_sg(cmd);
373 if (!rc)
374 target_complete_cmd(cmd, GOOD);
375 return rc;
378 static inline int core_alua_state_nonoptimized(
379 struct se_cmd *cmd,
380 unsigned char *cdb,
381 int nonop_delay_msecs,
382 u8 *alua_ascq)
385 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
386 * later to determine if processing of this cmd needs to be
387 * temporarily delayed for the Active/NonOptimized primary access state.
389 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
390 cmd->alua_nonop_delay = nonop_delay_msecs;
391 return 0;
394 static inline int core_alua_state_standby(
395 struct se_cmd *cmd,
396 unsigned char *cdb,
397 u8 *alua_ascq)
400 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
401 * spc4r17 section 5.9.2.4.4
403 switch (cdb[0]) {
404 case INQUIRY:
405 case LOG_SELECT:
406 case LOG_SENSE:
407 case MODE_SELECT:
408 case MODE_SENSE:
409 case REPORT_LUNS:
410 case RECEIVE_DIAGNOSTIC:
411 case SEND_DIAGNOSTIC:
412 case MAINTENANCE_IN:
413 switch (cdb[1] & 0x1f) {
414 case MI_REPORT_TARGET_PGS:
415 return 0;
416 default:
417 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
418 return 1;
420 case MAINTENANCE_OUT:
421 switch (cdb[1]) {
422 case MO_SET_TARGET_PGS:
423 return 0;
424 default:
425 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
426 return 1;
428 case REQUEST_SENSE:
429 case PERSISTENT_RESERVE_IN:
430 case PERSISTENT_RESERVE_OUT:
431 case READ_BUFFER:
432 case WRITE_BUFFER:
433 return 0;
434 default:
435 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
436 return 1;
439 return 0;
442 static inline int core_alua_state_unavailable(
443 struct se_cmd *cmd,
444 unsigned char *cdb,
445 u8 *alua_ascq)
448 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
449 * spc4r17 section 5.9.2.4.5
451 switch (cdb[0]) {
452 case INQUIRY:
453 case REPORT_LUNS:
454 case MAINTENANCE_IN:
455 switch (cdb[1] & 0x1f) {
456 case MI_REPORT_TARGET_PGS:
457 return 0;
458 default:
459 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
460 return 1;
462 case MAINTENANCE_OUT:
463 switch (cdb[1]) {
464 case MO_SET_TARGET_PGS:
465 return 0;
466 default:
467 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
468 return 1;
470 case REQUEST_SENSE:
471 case READ_BUFFER:
472 case WRITE_BUFFER:
473 return 0;
474 default:
475 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
476 return 1;
479 return 0;
482 static inline int core_alua_state_transition(
483 struct se_cmd *cmd,
484 unsigned char *cdb,
485 u8 *alua_ascq)
488 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
489 * spc4r17 section 5.9.2.5
491 switch (cdb[0]) {
492 case INQUIRY:
493 case REPORT_LUNS:
494 case MAINTENANCE_IN:
495 switch (cdb[1] & 0x1f) {
496 case MI_REPORT_TARGET_PGS:
497 return 0;
498 default:
499 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
500 return 1;
502 case REQUEST_SENSE:
503 case READ_BUFFER:
504 case WRITE_BUFFER:
505 return 0;
506 default:
507 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
508 return 1;
511 return 0;
515 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
516 * return 0: Used to signal success
517 * reutrn -1: Used to signal failure, and invalid cdb field
519 sense_reason_t
520 target_alua_state_check(struct se_cmd *cmd)
522 struct se_device *dev = cmd->se_dev;
523 unsigned char *cdb = cmd->t_task_cdb;
524 struct se_lun *lun = cmd->se_lun;
525 struct se_port *port = lun->lun_sep;
526 struct t10_alua_tg_pt_gp *tg_pt_gp;
527 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
528 int out_alua_state, nonop_delay_msecs;
529 u8 alua_ascq;
530 int ret;
532 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
533 return 0;
534 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
535 return 0;
537 if (!port)
538 return 0;
540 * First, check for a struct se_port specific secondary ALUA target port
541 * access state: OFFLINE
543 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
544 pr_debug("ALUA: Got secondary offline status for local"
545 " target port\n");
546 alua_ascq = ASCQ_04H_ALUA_OFFLINE;
547 ret = 1;
548 goto out;
551 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
552 * ALUA target port group, to obtain current ALUA access state.
553 * Otherwise look for the underlying struct se_device association with
554 * a ALUA logical unit group.
556 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
557 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
558 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
559 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
560 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
561 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
563 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
564 * statement so the compiler knows explicitly to check this case first.
565 * For the Optimized ALUA access state case, we want to process the
566 * incoming fabric cmd ASAP..
568 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
569 return 0;
571 switch (out_alua_state) {
572 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
573 ret = core_alua_state_nonoptimized(cmd, cdb,
574 nonop_delay_msecs, &alua_ascq);
575 break;
576 case ALUA_ACCESS_STATE_STANDBY:
577 ret = core_alua_state_standby(cmd, cdb, &alua_ascq);
578 break;
579 case ALUA_ACCESS_STATE_UNAVAILABLE:
580 ret = core_alua_state_unavailable(cmd, cdb, &alua_ascq);
581 break;
582 case ALUA_ACCESS_STATE_TRANSITION:
583 ret = core_alua_state_transition(cmd, cdb, &alua_ascq);
584 break;
586 * OFFLINE is a secondary ALUA target port group access state, that is
587 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
589 case ALUA_ACCESS_STATE_OFFLINE:
590 default:
591 pr_err("Unknown ALUA access state: 0x%02x\n",
592 out_alua_state);
593 return TCM_INVALID_CDB_FIELD;
596 out:
597 if (ret > 0) {
599 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
600 * The ALUA additional sense code qualifier (ASCQ) is determined
601 * by the ALUA primary or secondary access state..
603 pr_debug("[%s]: ALUA TG Port not available, "
604 "SenseKey: NOT_READY, ASC/ASCQ: "
605 "0x04/0x%02x\n",
606 cmd->se_tfo->get_fabric_name(), alua_ascq);
608 cmd->scsi_asc = 0x04;
609 cmd->scsi_ascq = alua_ascq;
610 return TCM_CHECK_CONDITION_NOT_READY;
613 return 0;
617 * Check implict and explict ALUA state change request.
619 static sense_reason_t
620 core_alua_check_transition(int state, int *primary)
622 switch (state) {
623 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
624 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
625 case ALUA_ACCESS_STATE_STANDBY:
626 case ALUA_ACCESS_STATE_UNAVAILABLE:
628 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
629 * defined as primary target port asymmetric access states.
631 *primary = 1;
632 break;
633 case ALUA_ACCESS_STATE_OFFLINE:
635 * OFFLINE state is defined as a secondary target port
636 * asymmetric access state.
638 *primary = 0;
639 break;
640 default:
641 pr_err("Unknown ALUA access state: 0x%02x\n", state);
642 return TCM_INVALID_PARAMETER_LIST;
645 return 0;
648 static char *core_alua_dump_state(int state)
650 switch (state) {
651 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
652 return "Active/Optimized";
653 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
654 return "Active/NonOptimized";
655 case ALUA_ACCESS_STATE_STANDBY:
656 return "Standby";
657 case ALUA_ACCESS_STATE_UNAVAILABLE:
658 return "Unavailable";
659 case ALUA_ACCESS_STATE_OFFLINE:
660 return "Offline";
661 default:
662 return "Unknown";
665 return NULL;
668 char *core_alua_dump_status(int status)
670 switch (status) {
671 case ALUA_STATUS_NONE:
672 return "None";
673 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
674 return "Altered by Explict STPG";
675 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
676 return "Altered by Implict ALUA";
677 default:
678 return "Unknown";
681 return NULL;
685 * Used by fabric modules to determine when we need to delay processing
686 * for the Active/NonOptimized paths..
688 int core_alua_check_nonop_delay(
689 struct se_cmd *cmd)
691 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
692 return 0;
693 if (in_interrupt())
694 return 0;
696 * The ALUA Active/NonOptimized access state delay can be disabled
697 * in via configfs with a value of zero
699 if (!cmd->alua_nonop_delay)
700 return 0;
702 * struct se_cmd->alua_nonop_delay gets set by a target port group
703 * defined interval in core_alua_state_nonoptimized()
705 msleep_interruptible(cmd->alua_nonop_delay);
706 return 0;
708 EXPORT_SYMBOL(core_alua_check_nonop_delay);
711 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
714 static int core_alua_write_tpg_metadata(
715 const char *path,
716 unsigned char *md_buf,
717 u32 md_buf_len)
719 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
720 int ret;
722 if (IS_ERR(file)) {
723 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
724 return -ENODEV;
726 ret = kernel_write(file, md_buf, md_buf_len, 0);
727 if (ret < 0)
728 pr_err("Error writing ALUA metadata file: %s\n", path);
729 fput(file);
730 return ret ? -EIO : 0;
734 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
736 static int core_alua_update_tpg_primary_metadata(
737 struct t10_alua_tg_pt_gp *tg_pt_gp,
738 int primary_state,
739 unsigned char *md_buf)
741 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
742 char path[ALUA_METADATA_PATH_LEN];
743 int len;
745 memset(path, 0, ALUA_METADATA_PATH_LEN);
747 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
748 "tg_pt_gp_id=%hu\n"
749 "alua_access_state=0x%02x\n"
750 "alua_access_status=0x%02x\n",
751 tg_pt_gp->tg_pt_gp_id, primary_state,
752 tg_pt_gp->tg_pt_gp_alua_access_status);
754 snprintf(path, ALUA_METADATA_PATH_LEN,
755 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
756 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
758 return core_alua_write_tpg_metadata(path, md_buf, len);
761 static int core_alua_do_transition_tg_pt(
762 struct t10_alua_tg_pt_gp *tg_pt_gp,
763 struct se_port *l_port,
764 struct se_node_acl *nacl,
765 unsigned char *md_buf,
766 int new_state,
767 int explict)
769 struct se_dev_entry *se_deve;
770 struct se_lun_acl *lacl;
771 struct se_port *port;
772 struct t10_alua_tg_pt_gp_member *mem;
773 int old_state = 0;
775 * Save the old primary ALUA access state, and set the current state
776 * to ALUA_ACCESS_STATE_TRANSITION.
778 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
779 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
780 ALUA_ACCESS_STATE_TRANSITION);
781 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
782 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
783 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
785 * Check for the optional ALUA primary state transition delay
787 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
788 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
790 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
791 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
792 tg_pt_gp_mem_list) {
793 port = mem->tg_pt;
795 * After an implicit target port asymmetric access state
796 * change, a device server shall establish a unit attention
797 * condition for the initiator port associated with every I_T
798 * nexus with the additional sense code set to ASYMMETRIC
799 * ACCESS STATE CHAGED.
801 * After an explicit target port asymmetric access state
802 * change, a device server shall establish a unit attention
803 * condition with the additional sense code set to ASYMMETRIC
804 * ACCESS STATE CHANGED for the initiator port associated with
805 * every I_T nexus other than the I_T nexus on which the SET
806 * TARGET PORT GROUPS command
808 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
809 smp_mb__after_atomic_inc();
810 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
812 spin_lock_bh(&port->sep_alua_lock);
813 list_for_each_entry(se_deve, &port->sep_alua_list,
814 alua_port_list) {
815 lacl = se_deve->se_lun_acl;
817 * se_deve->se_lun_acl pointer may be NULL for a
818 * entry created without explict Node+MappedLUN ACLs
820 if (!lacl)
821 continue;
823 if (explict &&
824 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
825 (l_port != NULL) && (l_port == port))
826 continue;
828 core_scsi3_ua_allocate(lacl->se_lun_nacl,
829 se_deve->mapped_lun, 0x2A,
830 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
832 spin_unlock_bh(&port->sep_alua_lock);
834 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
835 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
836 smp_mb__after_atomic_dec();
838 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
840 * Update the ALUA metadata buf that has been allocated in
841 * core_alua_do_port_transition(), this metadata will be written
842 * to struct file.
844 * Note that there is the case where we do not want to update the
845 * metadata when the saved metadata is being parsed in userspace
846 * when setting the existing port access state and access status.
848 * Also note that the failure to write out the ALUA metadata to
849 * struct file does NOT affect the actual ALUA transition.
851 if (tg_pt_gp->tg_pt_gp_write_metadata) {
852 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
853 core_alua_update_tpg_primary_metadata(tg_pt_gp,
854 new_state, md_buf);
855 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
858 * Set the current primary ALUA access state to the requested new state
860 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
862 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
863 " from primary access state %s to %s\n", (explict) ? "explict" :
864 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
865 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
866 core_alua_dump_state(new_state));
868 return 0;
871 int core_alua_do_port_transition(
872 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
873 struct se_device *l_dev,
874 struct se_port *l_port,
875 struct se_node_acl *l_nacl,
876 int new_state,
877 int explict)
879 struct se_device *dev;
880 struct se_port *port;
881 struct se_node_acl *nacl;
882 struct t10_alua_lu_gp *lu_gp;
883 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
884 struct t10_alua_tg_pt_gp *tg_pt_gp;
885 unsigned char *md_buf;
886 int primary;
888 if (core_alua_check_transition(new_state, &primary) != 0)
889 return -EINVAL;
891 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
892 if (!md_buf) {
893 pr_err("Unable to allocate buf for ALUA metadata\n");
894 return -ENOMEM;
897 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
898 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
899 lu_gp = local_lu_gp_mem->lu_gp;
900 atomic_inc(&lu_gp->lu_gp_ref_cnt);
901 smp_mb__after_atomic_inc();
902 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
904 * For storage objects that are members of the 'default_lu_gp',
905 * we only do transition on the passed *l_tp_pt_gp, and not
906 * on all of the matching target port groups IDs in default_lu_gp.
908 if (!lu_gp->lu_gp_id) {
910 * core_alua_do_transition_tg_pt() will always return
911 * success.
913 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
914 md_buf, new_state, explict);
915 atomic_dec(&lu_gp->lu_gp_ref_cnt);
916 smp_mb__after_atomic_dec();
917 kfree(md_buf);
918 return 0;
921 * For all other LU groups aside from 'default_lu_gp', walk all of
922 * the associated storage objects looking for a matching target port
923 * group ID from the local target port group.
925 spin_lock(&lu_gp->lu_gp_lock);
926 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
927 lu_gp_mem_list) {
929 dev = lu_gp_mem->lu_gp_mem_dev;
930 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
931 smp_mb__after_atomic_inc();
932 spin_unlock(&lu_gp->lu_gp_lock);
934 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
935 list_for_each_entry(tg_pt_gp,
936 &dev->t10_alua.tg_pt_gps_list,
937 tg_pt_gp_list) {
939 if (!tg_pt_gp->tg_pt_gp_valid_id)
940 continue;
942 * If the target behavior port asymmetric access state
943 * is changed for any target port group accessiable via
944 * a logical unit within a LU group, the target port
945 * behavior group asymmetric access states for the same
946 * target port group accessible via other logical units
947 * in that LU group will also change.
949 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
950 continue;
952 if (l_tg_pt_gp == tg_pt_gp) {
953 port = l_port;
954 nacl = l_nacl;
955 } else {
956 port = NULL;
957 nacl = NULL;
959 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
960 smp_mb__after_atomic_inc();
961 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
963 * core_alua_do_transition_tg_pt() will always return
964 * success.
966 core_alua_do_transition_tg_pt(tg_pt_gp, port,
967 nacl, md_buf, new_state, explict);
969 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
970 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
971 smp_mb__after_atomic_dec();
973 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
975 spin_lock(&lu_gp->lu_gp_lock);
976 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
977 smp_mb__after_atomic_dec();
979 spin_unlock(&lu_gp->lu_gp_lock);
981 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
982 " Group IDs: %hu %s transition to primary state: %s\n",
983 config_item_name(&lu_gp->lu_gp_group.cg_item),
984 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
985 core_alua_dump_state(new_state));
987 atomic_dec(&lu_gp->lu_gp_ref_cnt);
988 smp_mb__after_atomic_dec();
989 kfree(md_buf);
990 return 0;
994 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
996 static int core_alua_update_tpg_secondary_metadata(
997 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
998 struct se_port *port,
999 unsigned char *md_buf,
1000 u32 md_buf_len)
1002 struct se_portal_group *se_tpg = port->sep_tpg;
1003 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1004 int len;
1006 memset(path, 0, ALUA_METADATA_PATH_LEN);
1007 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1009 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1010 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1012 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1013 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1014 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1016 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1017 "alua_tg_pt_status=0x%02x\n",
1018 atomic_read(&port->sep_tg_pt_secondary_offline),
1019 port->sep_tg_pt_secondary_stat);
1021 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1022 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1023 port->sep_lun->unpacked_lun);
1025 return core_alua_write_tpg_metadata(path, md_buf, len);
1028 static int core_alua_set_tg_pt_secondary_state(
1029 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1030 struct se_port *port,
1031 int explict,
1032 int offline)
1034 struct t10_alua_tg_pt_gp *tg_pt_gp;
1035 unsigned char *md_buf;
1036 u32 md_buf_len;
1037 int trans_delay_msecs;
1039 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1040 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1041 if (!tg_pt_gp) {
1042 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1043 pr_err("Unable to complete secondary state"
1044 " transition\n");
1045 return -EINVAL;
1047 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1049 * Set the secondary ALUA target port access state to OFFLINE
1050 * or release the previously secondary state for struct se_port
1052 if (offline)
1053 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1054 else
1055 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1057 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1058 port->sep_tg_pt_secondary_stat = (explict) ?
1059 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1060 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1062 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1063 " to secondary access state: %s\n", (explict) ? "explict" :
1064 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1065 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1067 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1069 * Do the optional transition delay after we set the secondary
1070 * ALUA access state.
1072 if (trans_delay_msecs != 0)
1073 msleep_interruptible(trans_delay_msecs);
1075 * See if we need to update the ALUA fabric port metadata for
1076 * secondary state and status
1078 if (port->sep_tg_pt_secondary_write_md) {
1079 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1080 if (!md_buf) {
1081 pr_err("Unable to allocate md_buf for"
1082 " secondary ALUA access metadata\n");
1083 return -ENOMEM;
1085 mutex_lock(&port->sep_tg_pt_md_mutex);
1086 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1087 md_buf, md_buf_len);
1088 mutex_unlock(&port->sep_tg_pt_md_mutex);
1090 kfree(md_buf);
1093 return 0;
1096 struct t10_alua_lu_gp *
1097 core_alua_allocate_lu_gp(const char *name, int def_group)
1099 struct t10_alua_lu_gp *lu_gp;
1101 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1102 if (!lu_gp) {
1103 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1104 return ERR_PTR(-ENOMEM);
1106 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1107 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1108 spin_lock_init(&lu_gp->lu_gp_lock);
1109 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1111 if (def_group) {
1112 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1113 lu_gp->lu_gp_valid_id = 1;
1114 alua_lu_gps_count++;
1117 return lu_gp;
1120 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1122 struct t10_alua_lu_gp *lu_gp_tmp;
1123 u16 lu_gp_id_tmp;
1125 * The lu_gp->lu_gp_id may only be set once..
1127 if (lu_gp->lu_gp_valid_id) {
1128 pr_warn("ALUA LU Group already has a valid ID,"
1129 " ignoring request\n");
1130 return -EINVAL;
1133 spin_lock(&lu_gps_lock);
1134 if (alua_lu_gps_count == 0x0000ffff) {
1135 pr_err("Maximum ALUA alua_lu_gps_count:"
1136 " 0x0000ffff reached\n");
1137 spin_unlock(&lu_gps_lock);
1138 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1139 return -ENOSPC;
1141 again:
1142 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1143 alua_lu_gps_counter++;
1145 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1146 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1147 if (!lu_gp_id)
1148 goto again;
1150 pr_warn("ALUA Logical Unit Group ID: %hu"
1151 " already exists, ignoring request\n",
1152 lu_gp_id);
1153 spin_unlock(&lu_gps_lock);
1154 return -EINVAL;
1158 lu_gp->lu_gp_id = lu_gp_id_tmp;
1159 lu_gp->lu_gp_valid_id = 1;
1160 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1161 alua_lu_gps_count++;
1162 spin_unlock(&lu_gps_lock);
1164 return 0;
1167 static struct t10_alua_lu_gp_member *
1168 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1170 struct t10_alua_lu_gp_member *lu_gp_mem;
1172 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1173 if (!lu_gp_mem) {
1174 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1175 return ERR_PTR(-ENOMEM);
1177 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1178 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1179 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1181 lu_gp_mem->lu_gp_mem_dev = dev;
1182 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1184 return lu_gp_mem;
1187 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1189 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1191 * Once we have reached this point, config_item_put() has
1192 * already been called from target_core_alua_drop_lu_gp().
1194 * Here, we remove the *lu_gp from the global list so that
1195 * no associations can be made while we are releasing
1196 * struct t10_alua_lu_gp.
1198 spin_lock(&lu_gps_lock);
1199 list_del(&lu_gp->lu_gp_node);
1200 alua_lu_gps_count--;
1201 spin_unlock(&lu_gps_lock);
1203 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1204 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1205 * released with core_alua_put_lu_gp_from_name()
1207 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1208 cpu_relax();
1210 * Release reference to struct t10_alua_lu_gp * from all associated
1211 * struct se_device.
1213 spin_lock(&lu_gp->lu_gp_lock);
1214 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1215 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1216 if (lu_gp_mem->lu_gp_assoc) {
1217 list_del(&lu_gp_mem->lu_gp_mem_list);
1218 lu_gp->lu_gp_members--;
1219 lu_gp_mem->lu_gp_assoc = 0;
1221 spin_unlock(&lu_gp->lu_gp_lock);
1224 * lu_gp_mem is associated with a single
1225 * struct se_device->dev_alua_lu_gp_mem, and is released when
1226 * struct se_device is released via core_alua_free_lu_gp_mem().
1228 * If the passed lu_gp does NOT match the default_lu_gp, assume
1229 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1231 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1232 if (lu_gp != default_lu_gp)
1233 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1234 default_lu_gp);
1235 else
1236 lu_gp_mem->lu_gp = NULL;
1237 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1239 spin_lock(&lu_gp->lu_gp_lock);
1241 spin_unlock(&lu_gp->lu_gp_lock);
1243 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1246 void core_alua_free_lu_gp_mem(struct se_device *dev)
1248 struct t10_alua_lu_gp *lu_gp;
1249 struct t10_alua_lu_gp_member *lu_gp_mem;
1251 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1252 if (!lu_gp_mem)
1253 return;
1255 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1256 cpu_relax();
1258 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1259 lu_gp = lu_gp_mem->lu_gp;
1260 if (lu_gp) {
1261 spin_lock(&lu_gp->lu_gp_lock);
1262 if (lu_gp_mem->lu_gp_assoc) {
1263 list_del(&lu_gp_mem->lu_gp_mem_list);
1264 lu_gp->lu_gp_members--;
1265 lu_gp_mem->lu_gp_assoc = 0;
1267 spin_unlock(&lu_gp->lu_gp_lock);
1268 lu_gp_mem->lu_gp = NULL;
1270 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1272 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1275 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1277 struct t10_alua_lu_gp *lu_gp;
1278 struct config_item *ci;
1280 spin_lock(&lu_gps_lock);
1281 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1282 if (!lu_gp->lu_gp_valid_id)
1283 continue;
1284 ci = &lu_gp->lu_gp_group.cg_item;
1285 if (!strcmp(config_item_name(ci), name)) {
1286 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1287 spin_unlock(&lu_gps_lock);
1288 return lu_gp;
1291 spin_unlock(&lu_gps_lock);
1293 return NULL;
1296 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1298 spin_lock(&lu_gps_lock);
1299 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1300 spin_unlock(&lu_gps_lock);
1304 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1306 void __core_alua_attach_lu_gp_mem(
1307 struct t10_alua_lu_gp_member *lu_gp_mem,
1308 struct t10_alua_lu_gp *lu_gp)
1310 spin_lock(&lu_gp->lu_gp_lock);
1311 lu_gp_mem->lu_gp = lu_gp;
1312 lu_gp_mem->lu_gp_assoc = 1;
1313 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1314 lu_gp->lu_gp_members++;
1315 spin_unlock(&lu_gp->lu_gp_lock);
1319 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1321 void __core_alua_drop_lu_gp_mem(
1322 struct t10_alua_lu_gp_member *lu_gp_mem,
1323 struct t10_alua_lu_gp *lu_gp)
1325 spin_lock(&lu_gp->lu_gp_lock);
1326 list_del(&lu_gp_mem->lu_gp_mem_list);
1327 lu_gp_mem->lu_gp = NULL;
1328 lu_gp_mem->lu_gp_assoc = 0;
1329 lu_gp->lu_gp_members--;
1330 spin_unlock(&lu_gp->lu_gp_lock);
1333 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1334 const char *name, int def_group)
1336 struct t10_alua_tg_pt_gp *tg_pt_gp;
1338 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1339 if (!tg_pt_gp) {
1340 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1341 return NULL;
1343 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1344 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1345 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1346 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1347 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1348 tg_pt_gp->tg_pt_gp_dev = dev;
1349 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1350 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1351 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1353 * Enable both explict and implict ALUA support by default
1355 tg_pt_gp->tg_pt_gp_alua_access_type =
1356 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1358 * Set the default Active/NonOptimized Delay in milliseconds
1360 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1361 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1362 tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS;
1364 if (def_group) {
1365 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1366 tg_pt_gp->tg_pt_gp_id =
1367 dev->t10_alua.alua_tg_pt_gps_counter++;
1368 tg_pt_gp->tg_pt_gp_valid_id = 1;
1369 dev->t10_alua.alua_tg_pt_gps_count++;
1370 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1371 &dev->t10_alua.tg_pt_gps_list);
1372 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1375 return tg_pt_gp;
1378 int core_alua_set_tg_pt_gp_id(
1379 struct t10_alua_tg_pt_gp *tg_pt_gp,
1380 u16 tg_pt_gp_id)
1382 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1383 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1384 u16 tg_pt_gp_id_tmp;
1387 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1389 if (tg_pt_gp->tg_pt_gp_valid_id) {
1390 pr_warn("ALUA TG PT Group already has a valid ID,"
1391 " ignoring request\n");
1392 return -EINVAL;
1395 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1396 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1397 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1398 " 0x0000ffff reached\n");
1399 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1400 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1401 return -ENOSPC;
1403 again:
1404 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1405 dev->t10_alua.alua_tg_pt_gps_counter++;
1407 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1408 tg_pt_gp_list) {
1409 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1410 if (!tg_pt_gp_id)
1411 goto again;
1413 pr_err("ALUA Target Port Group ID: %hu already"
1414 " exists, ignoring request\n", tg_pt_gp_id);
1415 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1416 return -EINVAL;
1420 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1421 tg_pt_gp->tg_pt_gp_valid_id = 1;
1422 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1423 &dev->t10_alua.tg_pt_gps_list);
1424 dev->t10_alua.alua_tg_pt_gps_count++;
1425 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1427 return 0;
1430 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1431 struct se_port *port)
1433 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1435 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1436 GFP_KERNEL);
1437 if (!tg_pt_gp_mem) {
1438 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1439 return ERR_PTR(-ENOMEM);
1441 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1442 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1443 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1445 tg_pt_gp_mem->tg_pt = port;
1446 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1448 return tg_pt_gp_mem;
1451 void core_alua_free_tg_pt_gp(
1452 struct t10_alua_tg_pt_gp *tg_pt_gp)
1454 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1455 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1458 * Once we have reached this point, config_item_put() has already
1459 * been called from target_core_alua_drop_tg_pt_gp().
1461 * Here we remove *tg_pt_gp from the global list so that
1462 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1463 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1465 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1466 list_del(&tg_pt_gp->tg_pt_gp_list);
1467 dev->t10_alua.alua_tg_pt_gps_counter--;
1468 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1471 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1472 * core_alua_get_tg_pt_gp_by_name() in
1473 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1474 * to be released with core_alua_put_tg_pt_gp_from_name().
1476 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1477 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 != dev->t10_alua.default_tg_pt_gp) {
1503 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1504 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 t10_alua_tg_pt_gp *tg_pt_gp;
1519 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1521 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1522 if (!tg_pt_gp_mem)
1523 return;
1525 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1526 cpu_relax();
1528 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1529 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1530 if (tg_pt_gp) {
1531 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1532 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1533 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1534 tg_pt_gp->tg_pt_gp_members--;
1535 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1537 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1538 tg_pt_gp_mem->tg_pt_gp = NULL;
1540 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1542 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1545 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1546 struct se_device *dev, const char *name)
1548 struct t10_alua_tg_pt_gp *tg_pt_gp;
1549 struct config_item *ci;
1551 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1552 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1553 tg_pt_gp_list) {
1554 if (!tg_pt_gp->tg_pt_gp_valid_id)
1555 continue;
1556 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1557 if (!strcmp(config_item_name(ci), name)) {
1558 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1559 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1560 return tg_pt_gp;
1563 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1565 return NULL;
1568 static void core_alua_put_tg_pt_gp_from_name(
1569 struct t10_alua_tg_pt_gp *tg_pt_gp)
1571 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1573 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1574 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1575 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1579 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1581 void __core_alua_attach_tg_pt_gp_mem(
1582 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1583 struct t10_alua_tg_pt_gp *tg_pt_gp)
1585 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1586 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1587 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1588 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1589 &tg_pt_gp->tg_pt_gp_mem_list);
1590 tg_pt_gp->tg_pt_gp_members++;
1591 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1595 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1597 static void __core_alua_drop_tg_pt_gp_mem(
1598 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1599 struct t10_alua_tg_pt_gp *tg_pt_gp)
1601 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1602 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1603 tg_pt_gp_mem->tg_pt_gp = NULL;
1604 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1605 tg_pt_gp->tg_pt_gp_members--;
1606 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1609 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1611 struct config_item *tg_pt_ci;
1612 struct t10_alua_tg_pt_gp *tg_pt_gp;
1613 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1614 ssize_t len = 0;
1616 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1617 if (!tg_pt_gp_mem)
1618 return len;
1620 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1621 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1622 if (tg_pt_gp) {
1623 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1624 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1625 " %hu\nTG Port Primary Access State: %s\nTG Port "
1626 "Primary Access Status: %s\nTG Port Secondary Access"
1627 " State: %s\nTG Port Secondary Access Status: %s\n",
1628 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1629 core_alua_dump_state(atomic_read(
1630 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1631 core_alua_dump_status(
1632 tg_pt_gp->tg_pt_gp_alua_access_status),
1633 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1634 "Offline" : "None",
1635 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1637 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1639 return len;
1642 ssize_t core_alua_store_tg_pt_gp_info(
1643 struct se_port *port,
1644 const char *page,
1645 size_t count)
1647 struct se_portal_group *tpg;
1648 struct se_lun *lun;
1649 struct se_device *dev = port->sep_lun->lun_se_dev;
1650 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1651 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1652 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1653 int move = 0;
1655 tpg = port->sep_tpg;
1656 lun = port->sep_lun;
1658 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1659 if (!tg_pt_gp_mem)
1660 return 0;
1662 if (count > TG_PT_GROUP_NAME_BUF) {
1663 pr_err("ALUA Target Port Group alias too large!\n");
1664 return -EINVAL;
1666 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1667 memcpy(buf, page, count);
1669 * Any ALUA target port group alias besides "NULL" means we will be
1670 * making a new group association.
1672 if (strcmp(strstrip(buf), "NULL")) {
1674 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1675 * struct t10_alua_tg_pt_gp. This reference is released with
1676 * core_alua_put_tg_pt_gp_from_name() below.
1678 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
1679 strstrip(buf));
1680 if (!tg_pt_gp_new)
1681 return -ENODEV;
1684 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1685 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1686 if (tg_pt_gp) {
1688 * Clearing an existing tg_pt_gp association, and replacing
1689 * with the default_tg_pt_gp.
1691 if (!tg_pt_gp_new) {
1692 pr_debug("Target_Core_ConfigFS: Moving"
1693 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1694 " alua/%s, ID: %hu back to"
1695 " default_tg_pt_gp\n",
1696 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1697 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1698 config_item_name(&lun->lun_group.cg_item),
1699 config_item_name(
1700 &tg_pt_gp->tg_pt_gp_group.cg_item),
1701 tg_pt_gp->tg_pt_gp_id);
1703 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1704 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1705 dev->t10_alua.default_tg_pt_gp);
1706 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1708 return count;
1711 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1713 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1714 move = 1;
1717 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1719 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1720 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1721 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1722 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1723 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1724 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1725 config_item_name(&lun->lun_group.cg_item),
1726 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1727 tg_pt_gp_new->tg_pt_gp_id);
1729 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1730 return count;
1733 ssize_t core_alua_show_access_type(
1734 struct t10_alua_tg_pt_gp *tg_pt_gp,
1735 char *page)
1737 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1738 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1739 return sprintf(page, "Implict and Explict\n");
1740 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1741 return sprintf(page, "Implict\n");
1742 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1743 return sprintf(page, "Explict\n");
1744 else
1745 return sprintf(page, "None\n");
1748 ssize_t core_alua_store_access_type(
1749 struct t10_alua_tg_pt_gp *tg_pt_gp,
1750 const char *page,
1751 size_t count)
1753 unsigned long tmp;
1754 int ret;
1756 ret = strict_strtoul(page, 0, &tmp);
1757 if (ret < 0) {
1758 pr_err("Unable to extract alua_access_type\n");
1759 return -EINVAL;
1761 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1762 pr_err("Illegal value for alua_access_type:"
1763 " %lu\n", tmp);
1764 return -EINVAL;
1766 if (tmp == 3)
1767 tg_pt_gp->tg_pt_gp_alua_access_type =
1768 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1769 else if (tmp == 2)
1770 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1771 else if (tmp == 1)
1772 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1773 else
1774 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1776 return count;
1779 ssize_t core_alua_show_nonop_delay_msecs(
1780 struct t10_alua_tg_pt_gp *tg_pt_gp,
1781 char *page)
1783 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1786 ssize_t core_alua_store_nonop_delay_msecs(
1787 struct t10_alua_tg_pt_gp *tg_pt_gp,
1788 const char *page,
1789 size_t count)
1791 unsigned long tmp;
1792 int ret;
1794 ret = strict_strtoul(page, 0, &tmp);
1795 if (ret < 0) {
1796 pr_err("Unable to extract nonop_delay_msecs\n");
1797 return -EINVAL;
1799 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1800 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1801 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1802 ALUA_MAX_NONOP_DELAY_MSECS);
1803 return -EINVAL;
1805 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1807 return count;
1810 ssize_t core_alua_show_trans_delay_msecs(
1811 struct t10_alua_tg_pt_gp *tg_pt_gp,
1812 char *page)
1814 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1817 ssize_t core_alua_store_trans_delay_msecs(
1818 struct t10_alua_tg_pt_gp *tg_pt_gp,
1819 const char *page,
1820 size_t count)
1822 unsigned long tmp;
1823 int ret;
1825 ret = strict_strtoul(page, 0, &tmp);
1826 if (ret < 0) {
1827 pr_err("Unable to extract trans_delay_msecs\n");
1828 return -EINVAL;
1830 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1831 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1832 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1833 ALUA_MAX_TRANS_DELAY_MSECS);
1834 return -EINVAL;
1836 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1838 return count;
1841 ssize_t core_alua_show_implict_trans_secs(
1842 struct t10_alua_tg_pt_gp *tg_pt_gp,
1843 char *page)
1845 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs);
1848 ssize_t core_alua_store_implict_trans_secs(
1849 struct t10_alua_tg_pt_gp *tg_pt_gp,
1850 const char *page,
1851 size_t count)
1853 unsigned long tmp;
1854 int ret;
1856 ret = strict_strtoul(page, 0, &tmp);
1857 if (ret < 0) {
1858 pr_err("Unable to extract implict_trans_secs\n");
1859 return -EINVAL;
1861 if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) {
1862 pr_err("Passed implict_trans_secs: %lu, exceeds"
1863 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
1864 ALUA_MAX_IMPLICT_TRANS_SECS);
1865 return -EINVAL;
1867 tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp;
1869 return count;
1872 ssize_t core_alua_show_preferred_bit(
1873 struct t10_alua_tg_pt_gp *tg_pt_gp,
1874 char *page)
1876 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1879 ssize_t core_alua_store_preferred_bit(
1880 struct t10_alua_tg_pt_gp *tg_pt_gp,
1881 const char *page,
1882 size_t count)
1884 unsigned long tmp;
1885 int ret;
1887 ret = strict_strtoul(page, 0, &tmp);
1888 if (ret < 0) {
1889 pr_err("Unable to extract preferred ALUA value\n");
1890 return -EINVAL;
1892 if ((tmp != 0) && (tmp != 1)) {
1893 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1894 return -EINVAL;
1896 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1898 return count;
1901 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1903 if (!lun->lun_sep)
1904 return -ENODEV;
1906 return sprintf(page, "%d\n",
1907 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1910 ssize_t core_alua_store_offline_bit(
1911 struct se_lun *lun,
1912 const char *page,
1913 size_t count)
1915 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1916 unsigned long tmp;
1917 int ret;
1919 if (!lun->lun_sep)
1920 return -ENODEV;
1922 ret = strict_strtoul(page, 0, &tmp);
1923 if (ret < 0) {
1924 pr_err("Unable to extract alua_tg_pt_offline value\n");
1925 return -EINVAL;
1927 if ((tmp != 0) && (tmp != 1)) {
1928 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1929 tmp);
1930 return -EINVAL;
1932 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1933 if (!tg_pt_gp_mem) {
1934 pr_err("Unable to locate *tg_pt_gp_mem\n");
1935 return -EINVAL;
1938 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1939 lun->lun_sep, 0, (int)tmp);
1940 if (ret < 0)
1941 return -EINVAL;
1943 return count;
1946 ssize_t core_alua_show_secondary_status(
1947 struct se_lun *lun,
1948 char *page)
1950 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1953 ssize_t core_alua_store_secondary_status(
1954 struct se_lun *lun,
1955 const char *page,
1956 size_t count)
1958 unsigned long tmp;
1959 int ret;
1961 ret = strict_strtoul(page, 0, &tmp);
1962 if (ret < 0) {
1963 pr_err("Unable to extract alua_tg_pt_status\n");
1964 return -EINVAL;
1966 if ((tmp != ALUA_STATUS_NONE) &&
1967 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1968 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1969 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1970 tmp);
1971 return -EINVAL;
1973 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1975 return count;
1978 ssize_t core_alua_show_secondary_write_metadata(
1979 struct se_lun *lun,
1980 char *page)
1982 return sprintf(page, "%d\n",
1983 lun->lun_sep->sep_tg_pt_secondary_write_md);
1986 ssize_t core_alua_store_secondary_write_metadata(
1987 struct se_lun *lun,
1988 const char *page,
1989 size_t count)
1991 unsigned long tmp;
1992 int ret;
1994 ret = strict_strtoul(page, 0, &tmp);
1995 if (ret < 0) {
1996 pr_err("Unable to extract alua_tg_pt_write_md\n");
1997 return -EINVAL;
1999 if ((tmp != 0) && (tmp != 1)) {
2000 pr_err("Illegal value for alua_tg_pt_write_md:"
2001 " %lu\n", tmp);
2002 return -EINVAL;
2004 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
2006 return count;
2009 int core_setup_alua(struct se_device *dev)
2011 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
2012 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2013 struct t10_alua_lu_gp_member *lu_gp_mem;
2016 * Associate this struct se_device with the default ALUA
2017 * LUN Group.
2019 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2020 if (IS_ERR(lu_gp_mem))
2021 return PTR_ERR(lu_gp_mem);
2023 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2024 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2025 default_lu_gp);
2026 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2028 pr_debug("%s: Adding to default ALUA LU Group:"
2029 " core/alua/lu_gps/default_lu_gp\n",
2030 dev->transport->name);
2033 return 0;