mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / target / target_core_alua.c
blob792424ffa53b53859184d6ce713ab647dacf2e38
1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * (c) Copyright 2009-2013 Datera, Inc.
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 READ_CAPACITY:
413 return 0;
414 case SERVICE_ACTION_IN:
415 switch (cdb[1] & 0x1f) {
416 case SAI_READ_CAPACITY_16:
417 return 0;
418 default:
419 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
420 return 1;
422 case MAINTENANCE_IN:
423 switch (cdb[1] & 0x1f) {
424 case MI_REPORT_TARGET_PGS:
425 return 0;
426 default:
427 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
428 return 1;
430 case MAINTENANCE_OUT:
431 switch (cdb[1]) {
432 case MO_SET_TARGET_PGS:
433 return 0;
434 default:
435 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
436 return 1;
438 case REQUEST_SENSE:
439 case PERSISTENT_RESERVE_IN:
440 case PERSISTENT_RESERVE_OUT:
441 case READ_BUFFER:
442 case WRITE_BUFFER:
443 return 0;
444 default:
445 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
446 return 1;
449 return 0;
452 static inline int core_alua_state_unavailable(
453 struct se_cmd *cmd,
454 unsigned char *cdb,
455 u8 *alua_ascq)
458 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
459 * spc4r17 section 5.9.2.4.5
461 switch (cdb[0]) {
462 case INQUIRY:
463 case REPORT_LUNS:
464 return 0;
465 case MAINTENANCE_IN:
466 switch (cdb[1] & 0x1f) {
467 case MI_REPORT_TARGET_PGS:
468 return 0;
469 default:
470 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
471 return 1;
473 case MAINTENANCE_OUT:
474 switch (cdb[1]) {
475 case MO_SET_TARGET_PGS:
476 return 0;
477 default:
478 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
479 return 1;
481 case REQUEST_SENSE:
482 case READ_BUFFER:
483 case WRITE_BUFFER:
484 return 0;
485 default:
486 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
487 return 1;
490 return 0;
493 static inline int core_alua_state_transition(
494 struct se_cmd *cmd,
495 unsigned char *cdb,
496 u8 *alua_ascq)
499 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
500 * spc4r17 section 5.9.2.5
502 switch (cdb[0]) {
503 case INQUIRY:
504 case REPORT_LUNS:
505 return 0;
506 case MAINTENANCE_IN:
507 switch (cdb[1] & 0x1f) {
508 case MI_REPORT_TARGET_PGS:
509 return 0;
510 default:
511 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
512 return 1;
514 case REQUEST_SENSE:
515 case READ_BUFFER:
516 case WRITE_BUFFER:
517 return 0;
518 default:
519 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
520 return 1;
523 return 0;
527 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
528 * return 0: Used to signal success
529 * reutrn -1: Used to signal failure, and invalid cdb field
531 sense_reason_t
532 target_alua_state_check(struct se_cmd *cmd)
534 struct se_device *dev = cmd->se_dev;
535 unsigned char *cdb = cmd->t_task_cdb;
536 struct se_lun *lun = cmd->se_lun;
537 struct se_port *port = lun->lun_sep;
538 struct t10_alua_tg_pt_gp *tg_pt_gp;
539 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
540 int out_alua_state, nonop_delay_msecs;
541 u8 alua_ascq;
542 int ret;
544 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
545 return 0;
546 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
547 return 0;
549 if (!port)
550 return 0;
552 * First, check for a struct se_port specific secondary ALUA target port
553 * access state: OFFLINE
555 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
556 pr_debug("ALUA: Got secondary offline status for local"
557 " target port\n");
558 alua_ascq = ASCQ_04H_ALUA_OFFLINE;
559 ret = 1;
560 goto out;
563 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
564 * ALUA target port group, to obtain current ALUA access state.
565 * Otherwise look for the underlying struct se_device association with
566 * a ALUA logical unit group.
568 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
569 if (!tg_pt_gp_mem)
570 return 0;
572 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
573 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
574 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
575 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
576 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
578 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
579 * statement so the compiler knows explicitly to check this case first.
580 * For the Optimized ALUA access state case, we want to process the
581 * incoming fabric cmd ASAP..
583 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
584 return 0;
586 switch (out_alua_state) {
587 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
588 ret = core_alua_state_nonoptimized(cmd, cdb,
589 nonop_delay_msecs, &alua_ascq);
590 break;
591 case ALUA_ACCESS_STATE_STANDBY:
592 ret = core_alua_state_standby(cmd, cdb, &alua_ascq);
593 break;
594 case ALUA_ACCESS_STATE_UNAVAILABLE:
595 ret = core_alua_state_unavailable(cmd, cdb, &alua_ascq);
596 break;
597 case ALUA_ACCESS_STATE_TRANSITION:
598 ret = core_alua_state_transition(cmd, cdb, &alua_ascq);
599 break;
601 * OFFLINE is a secondary ALUA target port group access state, that is
602 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
604 case ALUA_ACCESS_STATE_OFFLINE:
605 default:
606 pr_err("Unknown ALUA access state: 0x%02x\n",
607 out_alua_state);
608 return TCM_INVALID_CDB_FIELD;
611 out:
612 if (ret > 0) {
614 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
615 * The ALUA additional sense code qualifier (ASCQ) is determined
616 * by the ALUA primary or secondary access state..
618 pr_debug("[%s]: ALUA TG Port not available, "
619 "SenseKey: NOT_READY, ASC/ASCQ: "
620 "0x04/0x%02x\n",
621 cmd->se_tfo->get_fabric_name(), alua_ascq);
623 cmd->scsi_asc = 0x04;
624 cmd->scsi_ascq = alua_ascq;
625 return TCM_CHECK_CONDITION_NOT_READY;
628 return 0;
632 * Check implict and explict ALUA state change request.
634 static sense_reason_t
635 core_alua_check_transition(int state, int *primary)
637 switch (state) {
638 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
639 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
640 case ALUA_ACCESS_STATE_STANDBY:
641 case ALUA_ACCESS_STATE_UNAVAILABLE:
643 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
644 * defined as primary target port asymmetric access states.
646 *primary = 1;
647 break;
648 case ALUA_ACCESS_STATE_OFFLINE:
650 * OFFLINE state is defined as a secondary target port
651 * asymmetric access state.
653 *primary = 0;
654 break;
655 default:
656 pr_err("Unknown ALUA access state: 0x%02x\n", state);
657 return TCM_INVALID_PARAMETER_LIST;
660 return 0;
663 static char *core_alua_dump_state(int state)
665 switch (state) {
666 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
667 return "Active/Optimized";
668 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
669 return "Active/NonOptimized";
670 case ALUA_ACCESS_STATE_STANDBY:
671 return "Standby";
672 case ALUA_ACCESS_STATE_UNAVAILABLE:
673 return "Unavailable";
674 case ALUA_ACCESS_STATE_OFFLINE:
675 return "Offline";
676 default:
677 return "Unknown";
680 return NULL;
683 char *core_alua_dump_status(int status)
685 switch (status) {
686 case ALUA_STATUS_NONE:
687 return "None";
688 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
689 return "Altered by Explict STPG";
690 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
691 return "Altered by Implict ALUA";
692 default:
693 return "Unknown";
696 return NULL;
700 * Used by fabric modules to determine when we need to delay processing
701 * for the Active/NonOptimized paths..
703 int core_alua_check_nonop_delay(
704 struct se_cmd *cmd)
706 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
707 return 0;
708 if (in_interrupt())
709 return 0;
711 * The ALUA Active/NonOptimized access state delay can be disabled
712 * in via configfs with a value of zero
714 if (!cmd->alua_nonop_delay)
715 return 0;
717 * struct se_cmd->alua_nonop_delay gets set by a target port group
718 * defined interval in core_alua_state_nonoptimized()
720 msleep_interruptible(cmd->alua_nonop_delay);
721 return 0;
723 EXPORT_SYMBOL(core_alua_check_nonop_delay);
726 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
729 static int core_alua_write_tpg_metadata(
730 const char *path,
731 unsigned char *md_buf,
732 u32 md_buf_len)
734 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
735 int ret;
737 if (IS_ERR(file)) {
738 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
739 return -ENODEV;
741 ret = kernel_write(file, md_buf, md_buf_len, 0);
742 if (ret < 0)
743 pr_err("Error writing ALUA metadata file: %s\n", path);
744 fput(file);
745 return (ret < 0) ? -EIO : 0;
749 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
751 static int core_alua_update_tpg_primary_metadata(
752 struct t10_alua_tg_pt_gp *tg_pt_gp,
753 int primary_state,
754 unsigned char *md_buf)
756 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
757 char path[ALUA_METADATA_PATH_LEN];
758 int len;
760 memset(path, 0, ALUA_METADATA_PATH_LEN);
762 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
763 "tg_pt_gp_id=%hu\n"
764 "alua_access_state=0x%02x\n"
765 "alua_access_status=0x%02x\n",
766 tg_pt_gp->tg_pt_gp_id, primary_state,
767 tg_pt_gp->tg_pt_gp_alua_access_status);
769 snprintf(path, ALUA_METADATA_PATH_LEN,
770 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
771 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
773 return core_alua_write_tpg_metadata(path, md_buf, len);
776 static int core_alua_do_transition_tg_pt(
777 struct t10_alua_tg_pt_gp *tg_pt_gp,
778 struct se_port *l_port,
779 struct se_node_acl *nacl,
780 unsigned char *md_buf,
781 int new_state,
782 int explict)
784 struct se_dev_entry *se_deve;
785 struct se_lun_acl *lacl;
786 struct se_port *port;
787 struct t10_alua_tg_pt_gp_member *mem;
788 int old_state = 0;
790 * Save the old primary ALUA access state, and set the current state
791 * to ALUA_ACCESS_STATE_TRANSITION.
793 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
794 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
795 ALUA_ACCESS_STATE_TRANSITION);
796 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
797 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
798 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
800 * Check for the optional ALUA primary state transition delay
802 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
803 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
805 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
806 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
807 tg_pt_gp_mem_list) {
808 port = mem->tg_pt;
810 * After an implicit target port asymmetric access state
811 * change, a device server shall establish a unit attention
812 * condition for the initiator port associated with every I_T
813 * nexus with the additional sense code set to ASYMMETRIC
814 * ACCESS STATE CHAGED.
816 * After an explicit target port asymmetric access state
817 * change, a device server shall establish a unit attention
818 * condition with the additional sense code set to ASYMMETRIC
819 * ACCESS STATE CHANGED for the initiator port associated with
820 * every I_T nexus other than the I_T nexus on which the SET
821 * TARGET PORT GROUPS command
823 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
824 smp_mb__after_atomic_inc();
825 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
827 spin_lock_bh(&port->sep_alua_lock);
828 list_for_each_entry(se_deve, &port->sep_alua_list,
829 alua_port_list) {
830 lacl = se_deve->se_lun_acl;
832 * se_deve->se_lun_acl pointer may be NULL for a
833 * entry created without explict Node+MappedLUN ACLs
835 if (!lacl)
836 continue;
838 if (explict &&
839 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
840 (l_port != NULL) && (l_port == port))
841 continue;
843 core_scsi3_ua_allocate(lacl->se_lun_nacl,
844 se_deve->mapped_lun, 0x2A,
845 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
847 spin_unlock_bh(&port->sep_alua_lock);
849 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
850 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
851 smp_mb__after_atomic_dec();
853 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
855 * Update the ALUA metadata buf that has been allocated in
856 * core_alua_do_port_transition(), this metadata will be written
857 * to struct file.
859 * Note that there is the case where we do not want to update the
860 * metadata when the saved metadata is being parsed in userspace
861 * when setting the existing port access state and access status.
863 * Also note that the failure to write out the ALUA metadata to
864 * struct file does NOT affect the actual ALUA transition.
866 if (tg_pt_gp->tg_pt_gp_write_metadata) {
867 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
868 core_alua_update_tpg_primary_metadata(tg_pt_gp,
869 new_state, md_buf);
870 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
873 * Set the current primary ALUA access state to the requested new state
875 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
877 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
878 " from primary access state %s to %s\n", (explict) ? "explict" :
879 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
880 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
881 core_alua_dump_state(new_state));
883 return 0;
886 int core_alua_do_port_transition(
887 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
888 struct se_device *l_dev,
889 struct se_port *l_port,
890 struct se_node_acl *l_nacl,
891 int new_state,
892 int explict)
894 struct se_device *dev;
895 struct se_port *port;
896 struct se_node_acl *nacl;
897 struct t10_alua_lu_gp *lu_gp;
898 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
899 struct t10_alua_tg_pt_gp *tg_pt_gp;
900 unsigned char *md_buf;
901 int primary;
903 if (core_alua_check_transition(new_state, &primary) != 0)
904 return -EINVAL;
906 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
907 if (!md_buf) {
908 pr_err("Unable to allocate buf for ALUA metadata\n");
909 return -ENOMEM;
912 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
913 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
914 lu_gp = local_lu_gp_mem->lu_gp;
915 atomic_inc(&lu_gp->lu_gp_ref_cnt);
916 smp_mb__after_atomic_inc();
917 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
919 * For storage objects that are members of the 'default_lu_gp',
920 * we only do transition on the passed *l_tp_pt_gp, and not
921 * on all of the matching target port groups IDs in default_lu_gp.
923 if (!lu_gp->lu_gp_id) {
925 * core_alua_do_transition_tg_pt() will always return
926 * success.
928 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
929 md_buf, new_state, explict);
930 atomic_dec(&lu_gp->lu_gp_ref_cnt);
931 smp_mb__after_atomic_dec();
932 kfree(md_buf);
933 return 0;
936 * For all other LU groups aside from 'default_lu_gp', walk all of
937 * the associated storage objects looking for a matching target port
938 * group ID from the local target port group.
940 spin_lock(&lu_gp->lu_gp_lock);
941 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
942 lu_gp_mem_list) {
944 dev = lu_gp_mem->lu_gp_mem_dev;
945 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
946 smp_mb__after_atomic_inc();
947 spin_unlock(&lu_gp->lu_gp_lock);
949 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
950 list_for_each_entry(tg_pt_gp,
951 &dev->t10_alua.tg_pt_gps_list,
952 tg_pt_gp_list) {
954 if (!tg_pt_gp->tg_pt_gp_valid_id)
955 continue;
957 * If the target behavior port asymmetric access state
958 * is changed for any target port group accessiable via
959 * a logical unit within a LU group, the target port
960 * behavior group asymmetric access states for the same
961 * target port group accessible via other logical units
962 * in that LU group will also change.
964 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
965 continue;
967 if (l_tg_pt_gp == tg_pt_gp) {
968 port = l_port;
969 nacl = l_nacl;
970 } else {
971 port = NULL;
972 nacl = NULL;
974 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
975 smp_mb__after_atomic_inc();
976 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
978 * core_alua_do_transition_tg_pt() will always return
979 * success.
981 core_alua_do_transition_tg_pt(tg_pt_gp, port,
982 nacl, md_buf, new_state, explict);
984 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
985 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
986 smp_mb__after_atomic_dec();
988 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
990 spin_lock(&lu_gp->lu_gp_lock);
991 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
992 smp_mb__after_atomic_dec();
994 spin_unlock(&lu_gp->lu_gp_lock);
996 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
997 " Group IDs: %hu %s transition to primary state: %s\n",
998 config_item_name(&lu_gp->lu_gp_group.cg_item),
999 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
1000 core_alua_dump_state(new_state));
1002 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1003 smp_mb__after_atomic_dec();
1004 kfree(md_buf);
1005 return 0;
1009 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1011 static int core_alua_update_tpg_secondary_metadata(
1012 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1013 struct se_port *port,
1014 unsigned char *md_buf,
1015 u32 md_buf_len)
1017 struct se_portal_group *se_tpg = port->sep_tpg;
1018 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1019 int len;
1021 memset(path, 0, ALUA_METADATA_PATH_LEN);
1022 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1024 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
1025 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1027 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1028 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1029 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1031 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1032 "alua_tg_pt_status=0x%02x\n",
1033 atomic_read(&port->sep_tg_pt_secondary_offline),
1034 port->sep_tg_pt_secondary_stat);
1036 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1037 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1038 port->sep_lun->unpacked_lun);
1040 return core_alua_write_tpg_metadata(path, md_buf, len);
1043 static int core_alua_set_tg_pt_secondary_state(
1044 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1045 struct se_port *port,
1046 int explict,
1047 int offline)
1049 struct t10_alua_tg_pt_gp *tg_pt_gp;
1050 unsigned char *md_buf;
1051 u32 md_buf_len;
1052 int trans_delay_msecs;
1054 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1055 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1056 if (!tg_pt_gp) {
1057 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1058 pr_err("Unable to complete secondary state"
1059 " transition\n");
1060 return -EINVAL;
1062 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1064 * Set the secondary ALUA target port access state to OFFLINE
1065 * or release the previously secondary state for struct se_port
1067 if (offline)
1068 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1069 else
1070 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1072 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1073 port->sep_tg_pt_secondary_stat = (explict) ?
1074 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1075 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1077 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1078 " to secondary access state: %s\n", (explict) ? "explict" :
1079 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1080 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1082 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1084 * Do the optional transition delay after we set the secondary
1085 * ALUA access state.
1087 if (trans_delay_msecs != 0)
1088 msleep_interruptible(trans_delay_msecs);
1090 * See if we need to update the ALUA fabric port metadata for
1091 * secondary state and status
1093 if (port->sep_tg_pt_secondary_write_md) {
1094 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1095 if (!md_buf) {
1096 pr_err("Unable to allocate md_buf for"
1097 " secondary ALUA access metadata\n");
1098 return -ENOMEM;
1100 mutex_lock(&port->sep_tg_pt_md_mutex);
1101 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1102 md_buf, md_buf_len);
1103 mutex_unlock(&port->sep_tg_pt_md_mutex);
1105 kfree(md_buf);
1108 return 0;
1111 struct t10_alua_lu_gp *
1112 core_alua_allocate_lu_gp(const char *name, int def_group)
1114 struct t10_alua_lu_gp *lu_gp;
1116 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1117 if (!lu_gp) {
1118 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1119 return ERR_PTR(-ENOMEM);
1121 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1122 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1123 spin_lock_init(&lu_gp->lu_gp_lock);
1124 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1126 if (def_group) {
1127 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1128 lu_gp->lu_gp_valid_id = 1;
1129 alua_lu_gps_count++;
1132 return lu_gp;
1135 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1137 struct t10_alua_lu_gp *lu_gp_tmp;
1138 u16 lu_gp_id_tmp;
1140 * The lu_gp->lu_gp_id may only be set once..
1142 if (lu_gp->lu_gp_valid_id) {
1143 pr_warn("ALUA LU Group already has a valid ID,"
1144 " ignoring request\n");
1145 return -EINVAL;
1148 spin_lock(&lu_gps_lock);
1149 if (alua_lu_gps_count == 0x0000ffff) {
1150 pr_err("Maximum ALUA alua_lu_gps_count:"
1151 " 0x0000ffff reached\n");
1152 spin_unlock(&lu_gps_lock);
1153 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1154 return -ENOSPC;
1156 again:
1157 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1158 alua_lu_gps_counter++;
1160 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1161 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1162 if (!lu_gp_id)
1163 goto again;
1165 pr_warn("ALUA Logical Unit Group ID: %hu"
1166 " already exists, ignoring request\n",
1167 lu_gp_id);
1168 spin_unlock(&lu_gps_lock);
1169 return -EINVAL;
1173 lu_gp->lu_gp_id = lu_gp_id_tmp;
1174 lu_gp->lu_gp_valid_id = 1;
1175 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1176 alua_lu_gps_count++;
1177 spin_unlock(&lu_gps_lock);
1179 return 0;
1182 static struct t10_alua_lu_gp_member *
1183 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1185 struct t10_alua_lu_gp_member *lu_gp_mem;
1187 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1188 if (!lu_gp_mem) {
1189 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1190 return ERR_PTR(-ENOMEM);
1192 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1193 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1194 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1196 lu_gp_mem->lu_gp_mem_dev = dev;
1197 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1199 return lu_gp_mem;
1202 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1204 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1206 * Once we have reached this point, config_item_put() has
1207 * already been called from target_core_alua_drop_lu_gp().
1209 * Here, we remove the *lu_gp from the global list so that
1210 * no associations can be made while we are releasing
1211 * struct t10_alua_lu_gp.
1213 spin_lock(&lu_gps_lock);
1214 list_del(&lu_gp->lu_gp_node);
1215 alua_lu_gps_count--;
1216 spin_unlock(&lu_gps_lock);
1218 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1219 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1220 * released with core_alua_put_lu_gp_from_name()
1222 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1223 cpu_relax();
1225 * Release reference to struct t10_alua_lu_gp * from all associated
1226 * struct se_device.
1228 spin_lock(&lu_gp->lu_gp_lock);
1229 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1230 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1231 if (lu_gp_mem->lu_gp_assoc) {
1232 list_del(&lu_gp_mem->lu_gp_mem_list);
1233 lu_gp->lu_gp_members--;
1234 lu_gp_mem->lu_gp_assoc = 0;
1236 spin_unlock(&lu_gp->lu_gp_lock);
1239 * lu_gp_mem is associated with a single
1240 * struct se_device->dev_alua_lu_gp_mem, and is released when
1241 * struct se_device is released via core_alua_free_lu_gp_mem().
1243 * If the passed lu_gp does NOT match the default_lu_gp, assume
1244 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1246 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1247 if (lu_gp != default_lu_gp)
1248 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1249 default_lu_gp);
1250 else
1251 lu_gp_mem->lu_gp = NULL;
1252 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1254 spin_lock(&lu_gp->lu_gp_lock);
1256 spin_unlock(&lu_gp->lu_gp_lock);
1258 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1261 void core_alua_free_lu_gp_mem(struct se_device *dev)
1263 struct t10_alua_lu_gp *lu_gp;
1264 struct t10_alua_lu_gp_member *lu_gp_mem;
1266 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1267 if (!lu_gp_mem)
1268 return;
1270 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1271 cpu_relax();
1273 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1274 lu_gp = lu_gp_mem->lu_gp;
1275 if (lu_gp) {
1276 spin_lock(&lu_gp->lu_gp_lock);
1277 if (lu_gp_mem->lu_gp_assoc) {
1278 list_del(&lu_gp_mem->lu_gp_mem_list);
1279 lu_gp->lu_gp_members--;
1280 lu_gp_mem->lu_gp_assoc = 0;
1282 spin_unlock(&lu_gp->lu_gp_lock);
1283 lu_gp_mem->lu_gp = NULL;
1285 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1287 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1290 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1292 struct t10_alua_lu_gp *lu_gp;
1293 struct config_item *ci;
1295 spin_lock(&lu_gps_lock);
1296 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1297 if (!lu_gp->lu_gp_valid_id)
1298 continue;
1299 ci = &lu_gp->lu_gp_group.cg_item;
1300 if (!strcmp(config_item_name(ci), name)) {
1301 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1302 spin_unlock(&lu_gps_lock);
1303 return lu_gp;
1306 spin_unlock(&lu_gps_lock);
1308 return NULL;
1311 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1313 spin_lock(&lu_gps_lock);
1314 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1315 spin_unlock(&lu_gps_lock);
1319 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1321 void __core_alua_attach_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 lu_gp_mem->lu_gp = lu_gp;
1327 lu_gp_mem->lu_gp_assoc = 1;
1328 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1329 lu_gp->lu_gp_members++;
1330 spin_unlock(&lu_gp->lu_gp_lock);
1334 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1336 void __core_alua_drop_lu_gp_mem(
1337 struct t10_alua_lu_gp_member *lu_gp_mem,
1338 struct t10_alua_lu_gp *lu_gp)
1340 spin_lock(&lu_gp->lu_gp_lock);
1341 list_del(&lu_gp_mem->lu_gp_mem_list);
1342 lu_gp_mem->lu_gp = NULL;
1343 lu_gp_mem->lu_gp_assoc = 0;
1344 lu_gp->lu_gp_members--;
1345 spin_unlock(&lu_gp->lu_gp_lock);
1348 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1349 const char *name, int def_group)
1351 struct t10_alua_tg_pt_gp *tg_pt_gp;
1353 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1354 if (!tg_pt_gp) {
1355 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1356 return NULL;
1358 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1359 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1360 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1361 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1362 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1363 tg_pt_gp->tg_pt_gp_dev = dev;
1364 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1365 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1366 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1368 * Enable both explict and implict ALUA support by default
1370 tg_pt_gp->tg_pt_gp_alua_access_type =
1371 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1373 * Set the default Active/NonOptimized Delay in milliseconds
1375 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1376 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1377 tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS;
1379 if (def_group) {
1380 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1381 tg_pt_gp->tg_pt_gp_id =
1382 dev->t10_alua.alua_tg_pt_gps_counter++;
1383 tg_pt_gp->tg_pt_gp_valid_id = 1;
1384 dev->t10_alua.alua_tg_pt_gps_count++;
1385 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1386 &dev->t10_alua.tg_pt_gps_list);
1387 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1390 return tg_pt_gp;
1393 int core_alua_set_tg_pt_gp_id(
1394 struct t10_alua_tg_pt_gp *tg_pt_gp,
1395 u16 tg_pt_gp_id)
1397 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1398 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1399 u16 tg_pt_gp_id_tmp;
1402 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1404 if (tg_pt_gp->tg_pt_gp_valid_id) {
1405 pr_warn("ALUA TG PT Group already has a valid ID,"
1406 " ignoring request\n");
1407 return -EINVAL;
1410 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1411 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1412 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1413 " 0x0000ffff reached\n");
1414 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1415 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1416 return -ENOSPC;
1418 again:
1419 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1420 dev->t10_alua.alua_tg_pt_gps_counter++;
1422 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
1423 tg_pt_gp_list) {
1424 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1425 if (!tg_pt_gp_id)
1426 goto again;
1428 pr_err("ALUA Target Port Group ID: %hu already"
1429 " exists, ignoring request\n", tg_pt_gp_id);
1430 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1431 return -EINVAL;
1435 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1436 tg_pt_gp->tg_pt_gp_valid_id = 1;
1437 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1438 &dev->t10_alua.tg_pt_gps_list);
1439 dev->t10_alua.alua_tg_pt_gps_count++;
1440 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1442 return 0;
1445 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1446 struct se_port *port)
1448 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1450 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1451 GFP_KERNEL);
1452 if (!tg_pt_gp_mem) {
1453 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1454 return ERR_PTR(-ENOMEM);
1456 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1457 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1458 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1460 tg_pt_gp_mem->tg_pt = port;
1461 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1463 return tg_pt_gp_mem;
1466 void core_alua_free_tg_pt_gp(
1467 struct t10_alua_tg_pt_gp *tg_pt_gp)
1469 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1470 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1473 * Once we have reached this point, config_item_put() has already
1474 * been called from target_core_alua_drop_tg_pt_gp().
1476 * Here we remove *tg_pt_gp from the global list so that
1477 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1478 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1480 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1481 list_del(&tg_pt_gp->tg_pt_gp_list);
1482 dev->t10_alua.alua_tg_pt_gps_counter--;
1483 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1486 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1487 * core_alua_get_tg_pt_gp_by_name() in
1488 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1489 * to be released with core_alua_put_tg_pt_gp_from_name().
1491 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1492 cpu_relax();
1495 * Release reference to struct t10_alua_tg_pt_gp from all associated
1496 * struct se_port.
1498 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1499 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1500 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1501 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1502 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1503 tg_pt_gp->tg_pt_gp_members--;
1504 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1506 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1508 * tg_pt_gp_mem is associated with a single
1509 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1510 * core_alua_free_tg_pt_gp_mem().
1512 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1513 * assume we want to re-assocate a given tg_pt_gp_mem with
1514 * default_tg_pt_gp.
1516 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1517 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
1518 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1519 dev->t10_alua.default_tg_pt_gp);
1520 } else
1521 tg_pt_gp_mem->tg_pt_gp = NULL;
1522 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1524 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1526 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1528 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1531 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1533 struct t10_alua_tg_pt_gp *tg_pt_gp;
1534 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1536 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1537 if (!tg_pt_gp_mem)
1538 return;
1540 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1541 cpu_relax();
1543 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1544 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1545 if (tg_pt_gp) {
1546 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1547 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1548 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1549 tg_pt_gp->tg_pt_gp_members--;
1550 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1552 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1553 tg_pt_gp_mem->tg_pt_gp = NULL;
1555 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1557 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1560 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1561 struct se_device *dev, const char *name)
1563 struct t10_alua_tg_pt_gp *tg_pt_gp;
1564 struct config_item *ci;
1566 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1567 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1568 tg_pt_gp_list) {
1569 if (!tg_pt_gp->tg_pt_gp_valid_id)
1570 continue;
1571 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1572 if (!strcmp(config_item_name(ci), name)) {
1573 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1574 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1575 return tg_pt_gp;
1578 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1580 return NULL;
1583 static void core_alua_put_tg_pt_gp_from_name(
1584 struct t10_alua_tg_pt_gp *tg_pt_gp)
1586 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1588 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1589 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1590 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1594 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1596 void __core_alua_attach_tg_pt_gp_mem(
1597 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1598 struct t10_alua_tg_pt_gp *tg_pt_gp)
1600 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1601 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1602 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1603 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1604 &tg_pt_gp->tg_pt_gp_mem_list);
1605 tg_pt_gp->tg_pt_gp_members++;
1606 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1610 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1612 static void __core_alua_drop_tg_pt_gp_mem(
1613 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1614 struct t10_alua_tg_pt_gp *tg_pt_gp)
1616 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1617 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1618 tg_pt_gp_mem->tg_pt_gp = NULL;
1619 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1620 tg_pt_gp->tg_pt_gp_members--;
1621 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1624 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1626 struct config_item *tg_pt_ci;
1627 struct t10_alua_tg_pt_gp *tg_pt_gp;
1628 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1629 ssize_t len = 0;
1631 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1632 if (!tg_pt_gp_mem)
1633 return len;
1635 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1636 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1637 if (tg_pt_gp) {
1638 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1639 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1640 " %hu\nTG Port Primary Access State: %s\nTG Port "
1641 "Primary Access Status: %s\nTG Port Secondary Access"
1642 " State: %s\nTG Port Secondary Access Status: %s\n",
1643 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1644 core_alua_dump_state(atomic_read(
1645 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1646 core_alua_dump_status(
1647 tg_pt_gp->tg_pt_gp_alua_access_status),
1648 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1649 "Offline" : "None",
1650 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1652 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1654 return len;
1657 ssize_t core_alua_store_tg_pt_gp_info(
1658 struct se_port *port,
1659 const char *page,
1660 size_t count)
1662 struct se_portal_group *tpg;
1663 struct se_lun *lun;
1664 struct se_device *dev = port->sep_lun->lun_se_dev;
1665 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1666 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1667 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1668 int move = 0;
1670 tpg = port->sep_tpg;
1671 lun = port->sep_lun;
1673 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1674 if (!tg_pt_gp_mem)
1675 return 0;
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(dev,
1694 strstrip(buf));
1695 if (!tg_pt_gp_new)
1696 return -ENODEV;
1699 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1700 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1701 if (tg_pt_gp) {
1703 * Clearing an existing tg_pt_gp association, and replacing
1704 * with the default_tg_pt_gp.
1706 if (!tg_pt_gp_new) {
1707 pr_debug("Target_Core_ConfigFS: Moving"
1708 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1709 " alua/%s, ID: %hu back to"
1710 " default_tg_pt_gp\n",
1711 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1712 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1713 config_item_name(&lun->lun_group.cg_item),
1714 config_item_name(
1715 &tg_pt_gp->tg_pt_gp_group.cg_item),
1716 tg_pt_gp->tg_pt_gp_id);
1718 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1719 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1720 dev->t10_alua.default_tg_pt_gp);
1721 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1723 return count;
1726 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1728 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1729 move = 1;
1732 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1734 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1735 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1736 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1737 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1738 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1739 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1740 config_item_name(&lun->lun_group.cg_item),
1741 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1742 tg_pt_gp_new->tg_pt_gp_id);
1744 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1745 return count;
1748 ssize_t core_alua_show_access_type(
1749 struct t10_alua_tg_pt_gp *tg_pt_gp,
1750 char *page)
1752 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1753 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1754 return sprintf(page, "Implict and Explict\n");
1755 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1756 return sprintf(page, "Implict\n");
1757 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1758 return sprintf(page, "Explict\n");
1759 else
1760 return sprintf(page, "None\n");
1763 ssize_t core_alua_store_access_type(
1764 struct t10_alua_tg_pt_gp *tg_pt_gp,
1765 const char *page,
1766 size_t count)
1768 unsigned long tmp;
1769 int ret;
1771 ret = kstrtoul(page, 0, &tmp);
1772 if (ret < 0) {
1773 pr_err("Unable to extract alua_access_type\n");
1774 return ret;
1776 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1777 pr_err("Illegal value for alua_access_type:"
1778 " %lu\n", tmp);
1779 return -EINVAL;
1781 if (tmp == 3)
1782 tg_pt_gp->tg_pt_gp_alua_access_type =
1783 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1784 else if (tmp == 2)
1785 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1786 else if (tmp == 1)
1787 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1788 else
1789 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1791 return count;
1794 ssize_t core_alua_show_nonop_delay_msecs(
1795 struct t10_alua_tg_pt_gp *tg_pt_gp,
1796 char *page)
1798 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1801 ssize_t core_alua_store_nonop_delay_msecs(
1802 struct t10_alua_tg_pt_gp *tg_pt_gp,
1803 const char *page,
1804 size_t count)
1806 unsigned long tmp;
1807 int ret;
1809 ret = kstrtoul(page, 0, &tmp);
1810 if (ret < 0) {
1811 pr_err("Unable to extract nonop_delay_msecs\n");
1812 return ret;
1814 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1815 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1816 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1817 ALUA_MAX_NONOP_DELAY_MSECS);
1818 return -EINVAL;
1820 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1822 return count;
1825 ssize_t core_alua_show_trans_delay_msecs(
1826 struct t10_alua_tg_pt_gp *tg_pt_gp,
1827 char *page)
1829 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1832 ssize_t core_alua_store_trans_delay_msecs(
1833 struct t10_alua_tg_pt_gp *tg_pt_gp,
1834 const char *page,
1835 size_t count)
1837 unsigned long tmp;
1838 int ret;
1840 ret = kstrtoul(page, 0, &tmp);
1841 if (ret < 0) {
1842 pr_err("Unable to extract trans_delay_msecs\n");
1843 return ret;
1845 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1846 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1847 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1848 ALUA_MAX_TRANS_DELAY_MSECS);
1849 return -EINVAL;
1851 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1853 return count;
1856 ssize_t core_alua_show_implict_trans_secs(
1857 struct t10_alua_tg_pt_gp *tg_pt_gp,
1858 char *page)
1860 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs);
1863 ssize_t core_alua_store_implict_trans_secs(
1864 struct t10_alua_tg_pt_gp *tg_pt_gp,
1865 const char *page,
1866 size_t count)
1868 unsigned long tmp;
1869 int ret;
1871 ret = kstrtoul(page, 0, &tmp);
1872 if (ret < 0) {
1873 pr_err("Unable to extract implict_trans_secs\n");
1874 return ret;
1876 if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) {
1877 pr_err("Passed implict_trans_secs: %lu, exceeds"
1878 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
1879 ALUA_MAX_IMPLICT_TRANS_SECS);
1880 return -EINVAL;
1882 tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp;
1884 return count;
1887 ssize_t core_alua_show_preferred_bit(
1888 struct t10_alua_tg_pt_gp *tg_pt_gp,
1889 char *page)
1891 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1894 ssize_t core_alua_store_preferred_bit(
1895 struct t10_alua_tg_pt_gp *tg_pt_gp,
1896 const char *page,
1897 size_t count)
1899 unsigned long tmp;
1900 int ret;
1902 ret = kstrtoul(page, 0, &tmp);
1903 if (ret < 0) {
1904 pr_err("Unable to extract preferred ALUA value\n");
1905 return ret;
1907 if ((tmp != 0) && (tmp != 1)) {
1908 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1909 return -EINVAL;
1911 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1913 return count;
1916 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1918 if (!lun->lun_sep)
1919 return -ENODEV;
1921 return sprintf(page, "%d\n",
1922 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1925 ssize_t core_alua_store_offline_bit(
1926 struct se_lun *lun,
1927 const char *page,
1928 size_t count)
1930 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1931 unsigned long tmp;
1932 int ret;
1934 if (!lun->lun_sep)
1935 return -ENODEV;
1937 ret = kstrtoul(page, 0, &tmp);
1938 if (ret < 0) {
1939 pr_err("Unable to extract alua_tg_pt_offline value\n");
1940 return ret;
1942 if ((tmp != 0) && (tmp != 1)) {
1943 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1944 tmp);
1945 return -EINVAL;
1947 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1948 if (!tg_pt_gp_mem) {
1949 pr_err("Unable to locate *tg_pt_gp_mem\n");
1950 return -EINVAL;
1953 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1954 lun->lun_sep, 0, (int)tmp);
1955 if (ret < 0)
1956 return -EINVAL;
1958 return count;
1961 ssize_t core_alua_show_secondary_status(
1962 struct se_lun *lun,
1963 char *page)
1965 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1968 ssize_t core_alua_store_secondary_status(
1969 struct se_lun *lun,
1970 const char *page,
1971 size_t count)
1973 unsigned long tmp;
1974 int ret;
1976 ret = kstrtoul(page, 0, &tmp);
1977 if (ret < 0) {
1978 pr_err("Unable to extract alua_tg_pt_status\n");
1979 return ret;
1981 if ((tmp != ALUA_STATUS_NONE) &&
1982 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1983 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1984 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1985 tmp);
1986 return -EINVAL;
1988 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1990 return count;
1993 ssize_t core_alua_show_secondary_write_metadata(
1994 struct se_lun *lun,
1995 char *page)
1997 return sprintf(page, "%d\n",
1998 lun->lun_sep->sep_tg_pt_secondary_write_md);
2001 ssize_t core_alua_store_secondary_write_metadata(
2002 struct se_lun *lun,
2003 const char *page,
2004 size_t count)
2006 unsigned long tmp;
2007 int ret;
2009 ret = kstrtoul(page, 0, &tmp);
2010 if (ret < 0) {
2011 pr_err("Unable to extract alua_tg_pt_write_md\n");
2012 return ret;
2014 if ((tmp != 0) && (tmp != 1)) {
2015 pr_err("Illegal value for alua_tg_pt_write_md:"
2016 " %lu\n", tmp);
2017 return -EINVAL;
2019 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
2021 return count;
2024 int core_setup_alua(struct se_device *dev)
2026 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
2027 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2028 struct t10_alua_lu_gp_member *lu_gp_mem;
2031 * Associate this struct se_device with the default ALUA
2032 * LUN Group.
2034 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2035 if (IS_ERR(lu_gp_mem))
2036 return PTR_ERR(lu_gp_mem);
2038 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2039 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2040 default_lu_gp);
2041 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2043 pr_debug("%s: Adding to default ALUA LU Group:"
2044 " core/alua/lu_gps/default_lu_gp\n",
2045 dev->transport->name);
2048 return 0;