USB: serial: fix whitespace issues
[zen-stable.git] / drivers / target / target_core_alua.c
blob88f2ad43ec8b589922e34de340dac32325080f13
1 /*******************************************************************************
2 * Filename: target_core_alua.c
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
6 * Copyright (c) 2009-2010 Rising Tide Systems
7 * Copyright (c) 2009-2010 Linux-iSCSI.org
9 * Nicholas A. Bellinger <nab@kernel.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 ******************************************************************************/
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/configfs.h>
30 #include <linux/export.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_cmnd.h>
34 #include <target/target_core_base.h>
35 #include <target/target_core_device.h>
36 #include <target/target_core_transport.h>
37 #include <target/target_core_fabric_ops.h>
38 #include <target/target_core_configfs.h>
40 #include "target_core_alua.h"
41 #include "target_core_hba.h"
42 #include "target_core_ua.h"
44 static int core_alua_check_transition(int state, int *primary);
45 static int core_alua_set_tg_pt_secondary_state(
46 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
47 struct se_port *port, int explict, int offline);
49 static u16 alua_lu_gps_counter;
50 static u32 alua_lu_gps_count;
52 static DEFINE_SPINLOCK(lu_gps_lock);
53 static LIST_HEAD(lu_gps_list);
55 struct t10_alua_lu_gp *default_lu_gp;
58 * REPORT_TARGET_PORT_GROUPS
60 * See spc4r17 section 6.27
62 int target_emulate_report_target_port_groups(struct se_task *task)
64 struct se_cmd *cmd = task->task_se_cmd;
65 struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
66 struct se_port *port;
67 struct t10_alua_tg_pt_gp *tg_pt_gp;
68 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
69 unsigned char *buf;
70 u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
71 Target port group descriptor */
73 * Need at least 4 bytes of response data or else we can't
74 * even fit the return data length.
76 if (cmd->data_length < 4) {
77 pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
78 " too small\n", cmd->data_length);
79 return -EINVAL;
82 buf = transport_kmap_first_data_page(cmd);
84 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
85 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
86 tg_pt_gp_list) {
88 * Check if the Target port group and Target port descriptor list
89 * based on tg_pt_gp_members count will fit into the response payload.
90 * Otherwise, bump rd_len to let the initiator know we have exceeded
91 * the allocation length and the response is truncated.
93 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
94 cmd->data_length) {
95 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
96 continue;
99 * PREF: Preferred target port bit, determine if this
100 * bit should be set for port group.
102 if (tg_pt_gp->tg_pt_gp_pref)
103 buf[off] = 0x80;
105 * Set the ASYMMETRIC ACCESS State
107 buf[off++] |= (atomic_read(
108 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
110 * Set supported ASYMMETRIC ACCESS State bits
112 buf[off] = 0x80; /* T_SUP */
113 buf[off] |= 0x40; /* O_SUP */
114 buf[off] |= 0x8; /* U_SUP */
115 buf[off] |= 0x4; /* S_SUP */
116 buf[off] |= 0x2; /* AN_SUP */
117 buf[off++] |= 0x1; /* AO_SUP */
119 * TARGET PORT GROUP
121 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
122 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
124 off++; /* Skip over Reserved */
126 * STATUS CODE
128 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
130 * Vendor Specific field
132 buf[off++] = 0x00;
134 * TARGET PORT COUNT
136 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
137 rd_len += 8;
139 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
140 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
141 tg_pt_gp_mem_list) {
142 port = tg_pt_gp_mem->tg_pt;
144 * Start Target Port descriptor format
146 * See spc4r17 section 6.2.7 Table 247
148 off += 2; /* Skip over Obsolete */
150 * Set RELATIVE TARGET PORT IDENTIFIER
152 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
153 buf[off++] = (port->sep_rtpi & 0xff);
154 rd_len += 4;
156 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
158 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
160 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
162 buf[0] = ((rd_len >> 24) & 0xff);
163 buf[1] = ((rd_len >> 16) & 0xff);
164 buf[2] = ((rd_len >> 8) & 0xff);
165 buf[3] = (rd_len & 0xff);
167 transport_kunmap_first_data_page(cmd);
169 task->task_scsi_status = GOOD;
170 transport_complete_task(task, 1);
171 return 0;
175 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
177 * See spc4r17 section 6.35
179 int target_emulate_set_target_port_groups(struct se_task *task)
181 struct se_cmd *cmd = task->task_se_cmd;
182 struct se_device *dev = cmd->se_dev;
183 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
184 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
185 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
186 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
187 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
188 unsigned char *buf;
189 unsigned char *ptr;
190 u32 len = 4; /* Skip over RESERVED area in header */
191 int alua_access_state, primary = 0, rc;
192 u16 tg_pt_id, rtpi;
194 if (!l_port)
195 return PYX_TRANSPORT_LU_COMM_FAILURE;
197 buf = transport_kmap_first_data_page(cmd);
200 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
201 * for the local tg_pt_gp.
203 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
204 if (!l_tg_pt_gp_mem) {
205 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
206 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
207 goto out;
209 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
210 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
211 if (!l_tg_pt_gp) {
212 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
213 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
214 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
215 goto out;
217 rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
218 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
220 if (!rc) {
221 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
222 " while TPGS_EXPLICT_ALUA is disabled\n");
223 rc = PYX_TRANSPORT_UNKNOWN_SAM_OPCODE;
224 goto out;
227 ptr = &buf[4]; /* Skip over RESERVED area in header */
229 while (len < cmd->data_length) {
230 alua_access_state = (ptr[0] & 0x0f);
232 * Check the received ALUA access state, and determine if
233 * the state is a primary or secondary target port asymmetric
234 * access state.
236 rc = core_alua_check_transition(alua_access_state, &primary);
237 if (rc != 0) {
239 * If the SET TARGET PORT GROUPS attempts to establish
240 * an invalid combination of target port asymmetric
241 * access states or attempts to establish an
242 * unsupported target port asymmetric access state,
243 * then the command shall be terminated with CHECK
244 * CONDITION status, with the sense key set to ILLEGAL
245 * REQUEST, and the additional sense code set to INVALID
246 * FIELD IN PARAMETER LIST.
248 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
249 goto out;
251 rc = -1;
253 * If the ASYMMETRIC ACCESS STATE field (see table 267)
254 * specifies a primary target port asymmetric access state,
255 * then the TARGET PORT GROUP OR TARGET PORT field specifies
256 * a primary target port group for which the primary target
257 * port asymmetric access state shall be changed. If the
258 * ASYMMETRIC ACCESS STATE field specifies a secondary target
259 * port asymmetric access state, then the TARGET PORT GROUP OR
260 * TARGET PORT field specifies the relative target port
261 * identifier (see 3.1.120) of the target port for which the
262 * secondary target port asymmetric access state shall be
263 * changed.
265 if (primary) {
266 tg_pt_id = ((ptr[2] << 8) & 0xff);
267 tg_pt_id |= (ptr[3] & 0xff);
269 * Locate the matching target port group ID from
270 * the global tg_pt_gp list
272 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
273 list_for_each_entry(tg_pt_gp,
274 &su_dev->t10_alua.tg_pt_gps_list,
275 tg_pt_gp_list) {
276 if (!tg_pt_gp->tg_pt_gp_valid_id)
277 continue;
279 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
280 continue;
282 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
283 smp_mb__after_atomic_inc();
284 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
286 rc = core_alua_do_port_transition(tg_pt_gp,
287 dev, l_port, nacl,
288 alua_access_state, 1);
290 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
291 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
292 smp_mb__after_atomic_dec();
293 break;
295 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
297 * If not matching target port group ID can be located
298 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
300 if (rc != 0) {
301 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
302 goto out;
304 } else {
306 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
307 * the Target Port in question for the the incoming
308 * SET_TARGET_PORT_GROUPS op.
310 rtpi = ((ptr[2] << 8) & 0xff);
311 rtpi |= (ptr[3] & 0xff);
313 * Locate the matching relative target port identifer
314 * for the struct se_device storage object.
316 spin_lock(&dev->se_port_lock);
317 list_for_each_entry(port, &dev->dev_sep_list,
318 sep_list) {
319 if (port->sep_rtpi != rtpi)
320 continue;
322 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
323 spin_unlock(&dev->se_port_lock);
325 rc = core_alua_set_tg_pt_secondary_state(
326 tg_pt_gp_mem, port, 1, 1);
328 spin_lock(&dev->se_port_lock);
329 break;
331 spin_unlock(&dev->se_port_lock);
333 * If not matching relative target port identifier can
334 * be located, throw an exception with ASCQ:
335 * INVALID_PARAMETER_LIST
337 if (rc != 0) {
338 rc = PYX_TRANSPORT_INVALID_PARAMETER_LIST;
339 goto out;
343 ptr += 4;
344 len += 4;
347 out:
348 transport_kunmap_first_data_page(cmd);
349 task->task_scsi_status = GOOD;
350 transport_complete_task(task, 1);
351 return 0;
354 static inline int core_alua_state_nonoptimized(
355 struct se_cmd *cmd,
356 unsigned char *cdb,
357 int nonop_delay_msecs,
358 u8 *alua_ascq)
361 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
362 * later to determine if processing of this cmd needs to be
363 * temporarily delayed for the Active/NonOptimized primary access state.
365 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
366 cmd->alua_nonop_delay = nonop_delay_msecs;
367 return 0;
370 static inline int core_alua_state_standby(
371 struct se_cmd *cmd,
372 unsigned char *cdb,
373 u8 *alua_ascq)
376 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
377 * spc4r17 section 5.9.2.4.4
379 switch (cdb[0]) {
380 case INQUIRY:
381 case LOG_SELECT:
382 case LOG_SENSE:
383 case MODE_SELECT:
384 case MODE_SENSE:
385 case REPORT_LUNS:
386 case RECEIVE_DIAGNOSTIC:
387 case SEND_DIAGNOSTIC:
388 case MAINTENANCE_IN:
389 switch (cdb[1]) {
390 case MI_REPORT_TARGET_PGS:
391 return 0;
392 default:
393 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
394 return 1;
396 case MAINTENANCE_OUT:
397 switch (cdb[1]) {
398 case MO_SET_TARGET_PGS:
399 return 0;
400 default:
401 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
402 return 1;
404 case REQUEST_SENSE:
405 case PERSISTENT_RESERVE_IN:
406 case PERSISTENT_RESERVE_OUT:
407 case READ_BUFFER:
408 case WRITE_BUFFER:
409 return 0;
410 default:
411 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
412 return 1;
415 return 0;
418 static inline int core_alua_state_unavailable(
419 struct se_cmd *cmd,
420 unsigned char *cdb,
421 u8 *alua_ascq)
424 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
425 * spc4r17 section 5.9.2.4.5
427 switch (cdb[0]) {
428 case INQUIRY:
429 case REPORT_LUNS:
430 case MAINTENANCE_IN:
431 switch (cdb[1]) {
432 case MI_REPORT_TARGET_PGS:
433 return 0;
434 default:
435 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
436 return 1;
438 case MAINTENANCE_OUT:
439 switch (cdb[1]) {
440 case MO_SET_TARGET_PGS:
441 return 0;
442 default:
443 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
444 return 1;
446 case REQUEST_SENSE:
447 case READ_BUFFER:
448 case WRITE_BUFFER:
449 return 0;
450 default:
451 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
452 return 1;
455 return 0;
458 static inline int core_alua_state_transition(
459 struct se_cmd *cmd,
460 unsigned char *cdb,
461 u8 *alua_ascq)
464 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
465 * spc4r17 section 5.9.2.5
467 switch (cdb[0]) {
468 case INQUIRY:
469 case REPORT_LUNS:
470 case MAINTENANCE_IN:
471 switch (cdb[1]) {
472 case MI_REPORT_TARGET_PGS:
473 return 0;
474 default:
475 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
476 return 1;
478 case REQUEST_SENSE:
479 case READ_BUFFER:
480 case WRITE_BUFFER:
481 return 0;
482 default:
483 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
484 return 1;
487 return 0;
491 * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
492 * in transport_cmd_sequencer(). This function is assigned to
493 * struct t10_alua *->state_check() in core_setup_alua()
495 static int core_alua_state_check_nop(
496 struct se_cmd *cmd,
497 unsigned char *cdb,
498 u8 *alua_ascq)
500 return 0;
504 * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
505 * This function is assigned to struct t10_alua *->state_check() in
506 * core_setup_alua()
508 * Also, this function can return three different return codes to
509 * signal transport_generic_cmd_sequencer()
511 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
512 * return 0: Used to signal success
513 * reutrn -1: Used to signal failure, and invalid cdb field
515 static int core_alua_state_check(
516 struct se_cmd *cmd,
517 unsigned char *cdb,
518 u8 *alua_ascq)
520 struct se_lun *lun = cmd->se_lun;
521 struct se_port *port = lun->lun_sep;
522 struct t10_alua_tg_pt_gp *tg_pt_gp;
523 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
524 int out_alua_state, nonop_delay_msecs;
526 if (!port)
527 return 0;
529 * First, check for a struct se_port specific secondary ALUA target port
530 * access state: OFFLINE
532 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
533 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
534 pr_debug("ALUA: Got secondary offline status for local"
535 " target port\n");
536 *alua_ascq = ASCQ_04H_ALUA_OFFLINE;
537 return 1;
540 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
541 * ALUA target port group, to obtain current ALUA access state.
542 * Otherwise look for the underlying struct se_device association with
543 * a ALUA logical unit group.
545 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
546 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
547 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
548 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
549 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
550 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
552 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
553 * statement so the compiler knows explicitly to check this case first.
554 * For the Optimized ALUA access state case, we want to process the
555 * incoming fabric cmd ASAP..
557 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
558 return 0;
560 switch (out_alua_state) {
561 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
562 return core_alua_state_nonoptimized(cmd, cdb,
563 nonop_delay_msecs, alua_ascq);
564 case ALUA_ACCESS_STATE_STANDBY:
565 return core_alua_state_standby(cmd, cdb, alua_ascq);
566 case ALUA_ACCESS_STATE_UNAVAILABLE:
567 return core_alua_state_unavailable(cmd, cdb, alua_ascq);
568 case ALUA_ACCESS_STATE_TRANSITION:
569 return core_alua_state_transition(cmd, cdb, alua_ascq);
571 * OFFLINE is a secondary ALUA target port group access state, that is
572 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
574 case ALUA_ACCESS_STATE_OFFLINE:
575 default:
576 pr_err("Unknown ALUA access state: 0x%02x\n",
577 out_alua_state);
578 return -EINVAL;
581 return 0;
585 * Check implict and explict ALUA state change request.
587 static int core_alua_check_transition(int state, int *primary)
589 switch (state) {
590 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
591 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
592 case ALUA_ACCESS_STATE_STANDBY:
593 case ALUA_ACCESS_STATE_UNAVAILABLE:
595 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
596 * defined as primary target port asymmetric access states.
598 *primary = 1;
599 break;
600 case ALUA_ACCESS_STATE_OFFLINE:
602 * OFFLINE state is defined as a secondary target port
603 * asymmetric access state.
605 *primary = 0;
606 break;
607 default:
608 pr_err("Unknown ALUA access state: 0x%02x\n", state);
609 return -EINVAL;
612 return 0;
615 static char *core_alua_dump_state(int state)
617 switch (state) {
618 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
619 return "Active/Optimized";
620 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
621 return "Active/NonOptimized";
622 case ALUA_ACCESS_STATE_STANDBY:
623 return "Standby";
624 case ALUA_ACCESS_STATE_UNAVAILABLE:
625 return "Unavailable";
626 case ALUA_ACCESS_STATE_OFFLINE:
627 return "Offline";
628 default:
629 return "Unknown";
632 return NULL;
635 char *core_alua_dump_status(int status)
637 switch (status) {
638 case ALUA_STATUS_NONE:
639 return "None";
640 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
641 return "Altered by Explict STPG";
642 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
643 return "Altered by Implict ALUA";
644 default:
645 return "Unknown";
648 return NULL;
652 * Used by fabric modules to determine when we need to delay processing
653 * for the Active/NonOptimized paths..
655 int core_alua_check_nonop_delay(
656 struct se_cmd *cmd)
658 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
659 return 0;
660 if (in_interrupt())
661 return 0;
663 * The ALUA Active/NonOptimized access state delay can be disabled
664 * in via configfs with a value of zero
666 if (!cmd->alua_nonop_delay)
667 return 0;
669 * struct se_cmd->alua_nonop_delay gets set by a target port group
670 * defined interval in core_alua_state_nonoptimized()
672 msleep_interruptible(cmd->alua_nonop_delay);
673 return 0;
675 EXPORT_SYMBOL(core_alua_check_nonop_delay);
678 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
681 static int core_alua_write_tpg_metadata(
682 const char *path,
683 unsigned char *md_buf,
684 u32 md_buf_len)
686 mm_segment_t old_fs;
687 struct file *file;
688 struct iovec iov[1];
689 int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
691 memset(iov, 0, sizeof(struct iovec));
693 file = filp_open(path, flags, 0600);
694 if (IS_ERR(file) || !file || !file->f_dentry) {
695 pr_err("filp_open(%s) for ALUA metadata failed\n",
696 path);
697 return -ENODEV;
700 iov[0].iov_base = &md_buf[0];
701 iov[0].iov_len = md_buf_len;
703 old_fs = get_fs();
704 set_fs(get_ds());
705 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
706 set_fs(old_fs);
708 if (ret < 0) {
709 pr_err("Error writing ALUA metadata file: %s\n", path);
710 filp_close(file, NULL);
711 return -EIO;
713 filp_close(file, NULL);
715 return 0;
719 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
721 static int core_alua_update_tpg_primary_metadata(
722 struct t10_alua_tg_pt_gp *tg_pt_gp,
723 int primary_state,
724 unsigned char *md_buf)
726 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
727 struct t10_wwn *wwn = &su_dev->t10_wwn;
728 char path[ALUA_METADATA_PATH_LEN];
729 int len;
731 memset(path, 0, ALUA_METADATA_PATH_LEN);
733 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
734 "tg_pt_gp_id=%hu\n"
735 "alua_access_state=0x%02x\n"
736 "alua_access_status=0x%02x\n",
737 tg_pt_gp->tg_pt_gp_id, primary_state,
738 tg_pt_gp->tg_pt_gp_alua_access_status);
740 snprintf(path, ALUA_METADATA_PATH_LEN,
741 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
742 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
744 return core_alua_write_tpg_metadata(path, md_buf, len);
747 static int core_alua_do_transition_tg_pt(
748 struct t10_alua_tg_pt_gp *tg_pt_gp,
749 struct se_port *l_port,
750 struct se_node_acl *nacl,
751 unsigned char *md_buf,
752 int new_state,
753 int explict)
755 struct se_dev_entry *se_deve;
756 struct se_lun_acl *lacl;
757 struct se_port *port;
758 struct t10_alua_tg_pt_gp_member *mem;
759 int old_state = 0;
761 * Save the old primary ALUA access state, and set the current state
762 * to ALUA_ACCESS_STATE_TRANSITION.
764 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
765 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
766 ALUA_ACCESS_STATE_TRANSITION);
767 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
768 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
769 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
771 * Check for the optional ALUA primary state transition delay
773 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
774 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
776 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
777 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
778 tg_pt_gp_mem_list) {
779 port = mem->tg_pt;
781 * After an implicit target port asymmetric access state
782 * change, a device server shall establish a unit attention
783 * condition for the initiator port associated with every I_T
784 * nexus with the additional sense code set to ASYMMETRIC
785 * ACCESS STATE CHAGED.
787 * After an explicit target port asymmetric access state
788 * change, a device server shall establish a unit attention
789 * condition with the additional sense code set to ASYMMETRIC
790 * ACCESS STATE CHANGED for the initiator port associated with
791 * every I_T nexus other than the I_T nexus on which the SET
792 * TARGET PORT GROUPS command
794 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
795 smp_mb__after_atomic_inc();
796 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
798 spin_lock_bh(&port->sep_alua_lock);
799 list_for_each_entry(se_deve, &port->sep_alua_list,
800 alua_port_list) {
801 lacl = se_deve->se_lun_acl;
803 * se_deve->se_lun_acl pointer may be NULL for a
804 * entry created without explict Node+MappedLUN ACLs
806 if (!lacl)
807 continue;
809 if (explict &&
810 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
811 (l_port != NULL) && (l_port == port))
812 continue;
814 core_scsi3_ua_allocate(lacl->se_lun_nacl,
815 se_deve->mapped_lun, 0x2A,
816 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
818 spin_unlock_bh(&port->sep_alua_lock);
820 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
821 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
822 smp_mb__after_atomic_dec();
824 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
826 * Update the ALUA metadata buf that has been allocated in
827 * core_alua_do_port_transition(), this metadata will be written
828 * to struct file.
830 * Note that there is the case where we do not want to update the
831 * metadata when the saved metadata is being parsed in userspace
832 * when setting the existing port access state and access status.
834 * Also note that the failure to write out the ALUA metadata to
835 * struct file does NOT affect the actual ALUA transition.
837 if (tg_pt_gp->tg_pt_gp_write_metadata) {
838 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
839 core_alua_update_tpg_primary_metadata(tg_pt_gp,
840 new_state, md_buf);
841 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
844 * Set the current primary ALUA access state to the requested new state
846 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
848 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
849 " from primary access state %s to %s\n", (explict) ? "explict" :
850 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
851 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
852 core_alua_dump_state(new_state));
854 return 0;
857 int core_alua_do_port_transition(
858 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
859 struct se_device *l_dev,
860 struct se_port *l_port,
861 struct se_node_acl *l_nacl,
862 int new_state,
863 int explict)
865 struct se_device *dev;
866 struct se_port *port;
867 struct se_subsystem_dev *su_dev;
868 struct se_node_acl *nacl;
869 struct t10_alua_lu_gp *lu_gp;
870 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
871 struct t10_alua_tg_pt_gp *tg_pt_gp;
872 unsigned char *md_buf;
873 int primary;
875 if (core_alua_check_transition(new_state, &primary) != 0)
876 return -EINVAL;
878 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
879 if (!md_buf) {
880 pr_err("Unable to allocate buf for ALUA metadata\n");
881 return -ENOMEM;
884 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
885 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
886 lu_gp = local_lu_gp_mem->lu_gp;
887 atomic_inc(&lu_gp->lu_gp_ref_cnt);
888 smp_mb__after_atomic_inc();
889 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
891 * For storage objects that are members of the 'default_lu_gp',
892 * we only do transition on the passed *l_tp_pt_gp, and not
893 * on all of the matching target port groups IDs in default_lu_gp.
895 if (!lu_gp->lu_gp_id) {
897 * core_alua_do_transition_tg_pt() will always return
898 * success.
900 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
901 md_buf, new_state, explict);
902 atomic_dec(&lu_gp->lu_gp_ref_cnt);
903 smp_mb__after_atomic_dec();
904 kfree(md_buf);
905 return 0;
908 * For all other LU groups aside from 'default_lu_gp', walk all of
909 * the associated storage objects looking for a matching target port
910 * group ID from the local target port group.
912 spin_lock(&lu_gp->lu_gp_lock);
913 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
914 lu_gp_mem_list) {
916 dev = lu_gp_mem->lu_gp_mem_dev;
917 su_dev = dev->se_sub_dev;
918 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
919 smp_mb__after_atomic_inc();
920 spin_unlock(&lu_gp->lu_gp_lock);
922 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
923 list_for_each_entry(tg_pt_gp,
924 &su_dev->t10_alua.tg_pt_gps_list,
925 tg_pt_gp_list) {
927 if (!tg_pt_gp->tg_pt_gp_valid_id)
928 continue;
930 * If the target behavior port asymmetric access state
931 * is changed for any target port group accessiable via
932 * a logical unit within a LU group, the target port
933 * behavior group asymmetric access states for the same
934 * target port group accessible via other logical units
935 * in that LU group will also change.
937 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
938 continue;
940 if (l_tg_pt_gp == tg_pt_gp) {
941 port = l_port;
942 nacl = l_nacl;
943 } else {
944 port = NULL;
945 nacl = NULL;
947 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
948 smp_mb__after_atomic_inc();
949 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
951 * core_alua_do_transition_tg_pt() will always return
952 * success.
954 core_alua_do_transition_tg_pt(tg_pt_gp, port,
955 nacl, md_buf, new_state, explict);
957 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
958 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
959 smp_mb__after_atomic_dec();
961 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
963 spin_lock(&lu_gp->lu_gp_lock);
964 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
965 smp_mb__after_atomic_dec();
967 spin_unlock(&lu_gp->lu_gp_lock);
969 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
970 " Group IDs: %hu %s transition to primary state: %s\n",
971 config_item_name(&lu_gp->lu_gp_group.cg_item),
972 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
973 core_alua_dump_state(new_state));
975 atomic_dec(&lu_gp->lu_gp_ref_cnt);
976 smp_mb__after_atomic_dec();
977 kfree(md_buf);
978 return 0;
982 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
984 static int core_alua_update_tpg_secondary_metadata(
985 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
986 struct se_port *port,
987 unsigned char *md_buf,
988 u32 md_buf_len)
990 struct se_portal_group *se_tpg = port->sep_tpg;
991 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
992 int len;
994 memset(path, 0, ALUA_METADATA_PATH_LEN);
995 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
997 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
998 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
1000 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
1001 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
1002 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
1004 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1005 "alua_tg_pt_status=0x%02x\n",
1006 atomic_read(&port->sep_tg_pt_secondary_offline),
1007 port->sep_tg_pt_secondary_stat);
1009 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
1010 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
1011 port->sep_lun->unpacked_lun);
1013 return core_alua_write_tpg_metadata(path, md_buf, len);
1016 static int core_alua_set_tg_pt_secondary_state(
1017 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1018 struct se_port *port,
1019 int explict,
1020 int offline)
1022 struct t10_alua_tg_pt_gp *tg_pt_gp;
1023 unsigned char *md_buf;
1024 u32 md_buf_len;
1025 int trans_delay_msecs;
1027 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1028 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1029 if (!tg_pt_gp) {
1030 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1031 pr_err("Unable to complete secondary state"
1032 " transition\n");
1033 return -EINVAL;
1035 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1037 * Set the secondary ALUA target port access state to OFFLINE
1038 * or release the previously secondary state for struct se_port
1040 if (offline)
1041 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1042 else
1043 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1045 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1046 port->sep_tg_pt_secondary_stat = (explict) ?
1047 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1048 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1050 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
1051 " to secondary access state: %s\n", (explict) ? "explict" :
1052 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1053 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1055 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1057 * Do the optional transition delay after we set the secondary
1058 * ALUA access state.
1060 if (trans_delay_msecs != 0)
1061 msleep_interruptible(trans_delay_msecs);
1063 * See if we need to update the ALUA fabric port metadata for
1064 * secondary state and status
1066 if (port->sep_tg_pt_secondary_write_md) {
1067 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
1068 if (!md_buf) {
1069 pr_err("Unable to allocate md_buf for"
1070 " secondary ALUA access metadata\n");
1071 return -ENOMEM;
1073 mutex_lock(&port->sep_tg_pt_md_mutex);
1074 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1075 md_buf, md_buf_len);
1076 mutex_unlock(&port->sep_tg_pt_md_mutex);
1078 kfree(md_buf);
1081 return 0;
1084 struct t10_alua_lu_gp *
1085 core_alua_allocate_lu_gp(const char *name, int def_group)
1087 struct t10_alua_lu_gp *lu_gp;
1089 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
1090 if (!lu_gp) {
1091 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
1092 return ERR_PTR(-ENOMEM);
1094 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
1095 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1096 spin_lock_init(&lu_gp->lu_gp_lock);
1097 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1099 if (def_group) {
1100 lu_gp->lu_gp_id = alua_lu_gps_counter++;
1101 lu_gp->lu_gp_valid_id = 1;
1102 alua_lu_gps_count++;
1105 return lu_gp;
1108 int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1110 struct t10_alua_lu_gp *lu_gp_tmp;
1111 u16 lu_gp_id_tmp;
1113 * The lu_gp->lu_gp_id may only be set once..
1115 if (lu_gp->lu_gp_valid_id) {
1116 pr_warn("ALUA LU Group already has a valid ID,"
1117 " ignoring request\n");
1118 return -EINVAL;
1121 spin_lock(&lu_gps_lock);
1122 if (alua_lu_gps_count == 0x0000ffff) {
1123 pr_err("Maximum ALUA alua_lu_gps_count:"
1124 " 0x0000ffff reached\n");
1125 spin_unlock(&lu_gps_lock);
1126 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1127 return -ENOSPC;
1129 again:
1130 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
1131 alua_lu_gps_counter++;
1133 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
1134 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
1135 if (!lu_gp_id)
1136 goto again;
1138 pr_warn("ALUA Logical Unit Group ID: %hu"
1139 " already exists, ignoring request\n",
1140 lu_gp_id);
1141 spin_unlock(&lu_gps_lock);
1142 return -EINVAL;
1146 lu_gp->lu_gp_id = lu_gp_id_tmp;
1147 lu_gp->lu_gp_valid_id = 1;
1148 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1149 alua_lu_gps_count++;
1150 spin_unlock(&lu_gps_lock);
1152 return 0;
1155 static struct t10_alua_lu_gp_member *
1156 core_alua_allocate_lu_gp_mem(struct se_device *dev)
1158 struct t10_alua_lu_gp_member *lu_gp_mem;
1160 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
1161 if (!lu_gp_mem) {
1162 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
1163 return ERR_PTR(-ENOMEM);
1165 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1166 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1167 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1169 lu_gp_mem->lu_gp_mem_dev = dev;
1170 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1172 return lu_gp_mem;
1175 void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1177 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1179 * Once we have reached this point, config_item_put() has
1180 * already been called from target_core_alua_drop_lu_gp().
1182 * Here, we remove the *lu_gp from the global list so that
1183 * no associations can be made while we are releasing
1184 * struct t10_alua_lu_gp.
1186 spin_lock(&lu_gps_lock);
1187 atomic_set(&lu_gp->lu_gp_shutdown, 1);
1188 list_del(&lu_gp->lu_gp_node);
1189 alua_lu_gps_count--;
1190 spin_unlock(&lu_gps_lock);
1192 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1193 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1194 * released with core_alua_put_lu_gp_from_name()
1196 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1197 cpu_relax();
1199 * Release reference to struct t10_alua_lu_gp * from all associated
1200 * struct se_device.
1202 spin_lock(&lu_gp->lu_gp_lock);
1203 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1204 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1205 if (lu_gp_mem->lu_gp_assoc) {
1206 list_del(&lu_gp_mem->lu_gp_mem_list);
1207 lu_gp->lu_gp_members--;
1208 lu_gp_mem->lu_gp_assoc = 0;
1210 spin_unlock(&lu_gp->lu_gp_lock);
1213 * lu_gp_mem is associated with a single
1214 * struct se_device->dev_alua_lu_gp_mem, and is released when
1215 * struct se_device is released via core_alua_free_lu_gp_mem().
1217 * If the passed lu_gp does NOT match the default_lu_gp, assume
1218 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1220 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1221 if (lu_gp != default_lu_gp)
1222 __core_alua_attach_lu_gp_mem(lu_gp_mem,
1223 default_lu_gp);
1224 else
1225 lu_gp_mem->lu_gp = NULL;
1226 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1228 spin_lock(&lu_gp->lu_gp_lock);
1230 spin_unlock(&lu_gp->lu_gp_lock);
1232 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1235 void core_alua_free_lu_gp_mem(struct se_device *dev)
1237 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1238 struct t10_alua *alua = &su_dev->t10_alua;
1239 struct t10_alua_lu_gp *lu_gp;
1240 struct t10_alua_lu_gp_member *lu_gp_mem;
1242 if (alua->alua_type != SPC3_ALUA_EMULATED)
1243 return;
1245 lu_gp_mem = dev->dev_alua_lu_gp_mem;
1246 if (!lu_gp_mem)
1247 return;
1249 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1250 cpu_relax();
1252 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1253 lu_gp = lu_gp_mem->lu_gp;
1254 if (lu_gp) {
1255 spin_lock(&lu_gp->lu_gp_lock);
1256 if (lu_gp_mem->lu_gp_assoc) {
1257 list_del(&lu_gp_mem->lu_gp_mem_list);
1258 lu_gp->lu_gp_members--;
1259 lu_gp_mem->lu_gp_assoc = 0;
1261 spin_unlock(&lu_gp->lu_gp_lock);
1262 lu_gp_mem->lu_gp = NULL;
1264 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1266 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1269 struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1271 struct t10_alua_lu_gp *lu_gp;
1272 struct config_item *ci;
1274 spin_lock(&lu_gps_lock);
1275 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
1276 if (!lu_gp->lu_gp_valid_id)
1277 continue;
1278 ci = &lu_gp->lu_gp_group.cg_item;
1279 if (!strcmp(config_item_name(ci), name)) {
1280 atomic_inc(&lu_gp->lu_gp_ref_cnt);
1281 spin_unlock(&lu_gps_lock);
1282 return lu_gp;
1285 spin_unlock(&lu_gps_lock);
1287 return NULL;
1290 void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1292 spin_lock(&lu_gps_lock);
1293 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1294 spin_unlock(&lu_gps_lock);
1298 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1300 void __core_alua_attach_lu_gp_mem(
1301 struct t10_alua_lu_gp_member *lu_gp_mem,
1302 struct t10_alua_lu_gp *lu_gp)
1304 spin_lock(&lu_gp->lu_gp_lock);
1305 lu_gp_mem->lu_gp = lu_gp;
1306 lu_gp_mem->lu_gp_assoc = 1;
1307 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1308 lu_gp->lu_gp_members++;
1309 spin_unlock(&lu_gp->lu_gp_lock);
1313 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1315 void __core_alua_drop_lu_gp_mem(
1316 struct t10_alua_lu_gp_member *lu_gp_mem,
1317 struct t10_alua_lu_gp *lu_gp)
1319 spin_lock(&lu_gp->lu_gp_lock);
1320 list_del(&lu_gp_mem->lu_gp_mem_list);
1321 lu_gp_mem->lu_gp = NULL;
1322 lu_gp_mem->lu_gp_assoc = 0;
1323 lu_gp->lu_gp_members--;
1324 spin_unlock(&lu_gp->lu_gp_lock);
1327 struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
1328 struct se_subsystem_dev *su_dev,
1329 const char *name,
1330 int def_group)
1332 struct t10_alua_tg_pt_gp *tg_pt_gp;
1334 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
1335 if (!tg_pt_gp) {
1336 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
1337 return NULL;
1339 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1340 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1341 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1342 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1343 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
1344 tg_pt_gp->tg_pt_gp_su_dev = su_dev;
1345 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1346 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1347 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1349 * Enable both explict and implict ALUA support by default
1351 tg_pt_gp->tg_pt_gp_alua_access_type =
1352 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1354 * Set the default Active/NonOptimized Delay in milliseconds
1356 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1357 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
1359 if (def_group) {
1360 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1361 tg_pt_gp->tg_pt_gp_id =
1362 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1363 tg_pt_gp->tg_pt_gp_valid_id = 1;
1364 su_dev->t10_alua.alua_tg_pt_gps_count++;
1365 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1366 &su_dev->t10_alua.tg_pt_gps_list);
1367 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1370 return tg_pt_gp;
1373 int core_alua_set_tg_pt_gp_id(
1374 struct t10_alua_tg_pt_gp *tg_pt_gp,
1375 u16 tg_pt_gp_id)
1377 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1378 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1379 u16 tg_pt_gp_id_tmp;
1381 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1383 if (tg_pt_gp->tg_pt_gp_valid_id) {
1384 pr_warn("ALUA TG PT Group already has a valid ID,"
1385 " ignoring request\n");
1386 return -EINVAL;
1389 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1390 if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
1391 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
1392 " 0x0000ffff reached\n");
1393 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1394 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1395 return -ENOSPC;
1397 again:
1398 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
1399 su_dev->t10_alua.alua_tg_pt_gps_counter++;
1401 list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
1402 tg_pt_gp_list) {
1403 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
1404 if (!tg_pt_gp_id)
1405 goto again;
1407 pr_err("ALUA Target Port Group ID: %hu already"
1408 " exists, ignoring request\n", tg_pt_gp_id);
1409 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1410 return -EINVAL;
1414 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1415 tg_pt_gp->tg_pt_gp_valid_id = 1;
1416 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
1417 &su_dev->t10_alua.tg_pt_gps_list);
1418 su_dev->t10_alua.alua_tg_pt_gps_count++;
1419 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1421 return 0;
1424 struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1425 struct se_port *port)
1427 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1429 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1430 GFP_KERNEL);
1431 if (!tg_pt_gp_mem) {
1432 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
1433 return ERR_PTR(-ENOMEM);
1435 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1436 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1437 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1439 tg_pt_gp_mem->tg_pt = port;
1440 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
1441 atomic_set(&port->sep_tg_pt_gp_active, 1);
1443 return tg_pt_gp_mem;
1446 void core_alua_free_tg_pt_gp(
1447 struct t10_alua_tg_pt_gp *tg_pt_gp)
1449 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1450 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
1452 * Once we have reached this point, config_item_put() has already
1453 * been called from target_core_alua_drop_tg_pt_gp().
1455 * Here we remove *tg_pt_gp from the global list so that
1456 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1457 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1459 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1460 list_del(&tg_pt_gp->tg_pt_gp_list);
1461 su_dev->t10_alua.alua_tg_pt_gps_counter--;
1462 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1464 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1465 * core_alua_get_tg_pt_gp_by_name() in
1466 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1467 * to be released with core_alua_put_tg_pt_gp_from_name().
1469 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1470 cpu_relax();
1472 * Release reference to struct t10_alua_tg_pt_gp from all associated
1473 * struct se_port.
1475 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1476 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1477 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1478 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1479 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1480 tg_pt_gp->tg_pt_gp_members--;
1481 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1483 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1485 * tg_pt_gp_mem is associated with a single
1486 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1487 * core_alua_free_tg_pt_gp_mem().
1489 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1490 * assume we want to re-assocate a given tg_pt_gp_mem with
1491 * default_tg_pt_gp.
1493 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1494 if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
1495 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1496 su_dev->t10_alua.default_tg_pt_gp);
1497 } else
1498 tg_pt_gp_mem->tg_pt_gp = NULL;
1499 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1501 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1503 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1505 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1508 void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1510 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1511 struct t10_alua *alua = &su_dev->t10_alua;
1512 struct t10_alua_tg_pt_gp *tg_pt_gp;
1513 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1515 if (alua->alua_type != SPC3_ALUA_EMULATED)
1516 return;
1518 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1519 if (!tg_pt_gp_mem)
1520 return;
1522 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1523 cpu_relax();
1525 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1526 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1527 if (tg_pt_gp) {
1528 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1529 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1530 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1531 tg_pt_gp->tg_pt_gp_members--;
1532 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1534 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1535 tg_pt_gp_mem->tg_pt_gp = NULL;
1537 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1539 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1542 static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
1543 struct se_subsystem_dev *su_dev,
1544 const char *name)
1546 struct t10_alua_tg_pt_gp *tg_pt_gp;
1547 struct config_item *ci;
1549 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1550 list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
1551 tg_pt_gp_list) {
1552 if (!tg_pt_gp->tg_pt_gp_valid_id)
1553 continue;
1554 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1555 if (!strcmp(config_item_name(ci), name)) {
1556 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
1557 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1558 return tg_pt_gp;
1561 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1563 return NULL;
1566 static void core_alua_put_tg_pt_gp_from_name(
1567 struct t10_alua_tg_pt_gp *tg_pt_gp)
1569 struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
1571 spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
1572 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
1573 spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
1577 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1579 void __core_alua_attach_tg_pt_gp_mem(
1580 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1581 struct t10_alua_tg_pt_gp *tg_pt_gp)
1583 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1584 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1585 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1586 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1587 &tg_pt_gp->tg_pt_gp_mem_list);
1588 tg_pt_gp->tg_pt_gp_members++;
1589 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1593 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1595 static void __core_alua_drop_tg_pt_gp_mem(
1596 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1597 struct t10_alua_tg_pt_gp *tg_pt_gp)
1599 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1600 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1601 tg_pt_gp_mem->tg_pt_gp = NULL;
1602 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1603 tg_pt_gp->tg_pt_gp_members--;
1604 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1607 ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1609 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1610 struct config_item *tg_pt_ci;
1611 struct t10_alua *alua = &su_dev->t10_alua;
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 if (alua->alua_type != SPC3_ALUA_EMULATED)
1617 return len;
1619 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1620 if (!tg_pt_gp_mem)
1621 return len;
1623 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1624 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1625 if (tg_pt_gp) {
1626 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1627 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1628 " %hu\nTG Port Primary Access State: %s\nTG Port "
1629 "Primary Access Status: %s\nTG Port Secondary Access"
1630 " State: %s\nTG Port Secondary Access Status: %s\n",
1631 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1632 core_alua_dump_state(atomic_read(
1633 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1634 core_alua_dump_status(
1635 tg_pt_gp->tg_pt_gp_alua_access_status),
1636 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1637 "Offline" : "None",
1638 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1640 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1642 return len;
1645 ssize_t core_alua_store_tg_pt_gp_info(
1646 struct se_port *port,
1647 const char *page,
1648 size_t count)
1650 struct se_portal_group *tpg;
1651 struct se_lun *lun;
1652 struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
1653 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1654 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1655 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1656 int move = 0;
1658 tpg = port->sep_tpg;
1659 lun = port->sep_lun;
1661 if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
1662 pr_warn("SPC3_ALUA_EMULATED not enabled for"
1663 " %s/tpgt_%hu/%s\n", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1664 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1665 config_item_name(&lun->lun_group.cg_item));
1666 return -EINVAL;
1669 if (count > TG_PT_GROUP_NAME_BUF) {
1670 pr_err("ALUA Target Port Group alias too large!\n");
1671 return -EINVAL;
1673 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1674 memcpy(buf, page, count);
1676 * Any ALUA target port group alias besides "NULL" means we will be
1677 * making a new group association.
1679 if (strcmp(strstrip(buf), "NULL")) {
1681 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1682 * struct t10_alua_tg_pt_gp. This reference is released with
1683 * core_alua_put_tg_pt_gp_from_name() below.
1685 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
1686 strstrip(buf));
1687 if (!tg_pt_gp_new)
1688 return -ENODEV;
1690 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1691 if (!tg_pt_gp_mem) {
1692 if (tg_pt_gp_new)
1693 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1694 pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer\n");
1695 return -EINVAL;
1698 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1699 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
1700 if (tg_pt_gp) {
1702 * Clearing an existing tg_pt_gp association, and replacing
1703 * with the default_tg_pt_gp.
1705 if (!tg_pt_gp_new) {
1706 pr_debug("Target_Core_ConfigFS: Moving"
1707 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1708 " alua/%s, ID: %hu back to"
1709 " default_tg_pt_gp\n",
1710 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1711 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1712 config_item_name(&lun->lun_group.cg_item),
1713 config_item_name(
1714 &tg_pt_gp->tg_pt_gp_group.cg_item),
1715 tg_pt_gp->tg_pt_gp_id);
1717 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1718 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
1719 su_dev->t10_alua.default_tg_pt_gp);
1720 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1722 return count;
1725 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1727 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1728 move = 1;
1731 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1733 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1734 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1735 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
1736 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
1737 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1738 tpg->se_tpg_tfo->tpg_get_tag(tpg),
1739 config_item_name(&lun->lun_group.cg_item),
1740 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1741 tg_pt_gp_new->tg_pt_gp_id);
1743 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1744 return count;
1747 ssize_t core_alua_show_access_type(
1748 struct t10_alua_tg_pt_gp *tg_pt_gp,
1749 char *page)
1751 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1752 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1753 return sprintf(page, "Implict and Explict\n");
1754 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1755 return sprintf(page, "Implict\n");
1756 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1757 return sprintf(page, "Explict\n");
1758 else
1759 return sprintf(page, "None\n");
1762 ssize_t core_alua_store_access_type(
1763 struct t10_alua_tg_pt_gp *tg_pt_gp,
1764 const char *page,
1765 size_t count)
1767 unsigned long tmp;
1768 int ret;
1770 ret = strict_strtoul(page, 0, &tmp);
1771 if (ret < 0) {
1772 pr_err("Unable to extract alua_access_type\n");
1773 return -EINVAL;
1775 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
1776 pr_err("Illegal value for alua_access_type:"
1777 " %lu\n", tmp);
1778 return -EINVAL;
1780 if (tmp == 3)
1781 tg_pt_gp->tg_pt_gp_alua_access_type =
1782 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1783 else if (tmp == 2)
1784 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1785 else if (tmp == 1)
1786 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1787 else
1788 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1790 return count;
1793 ssize_t core_alua_show_nonop_delay_msecs(
1794 struct t10_alua_tg_pt_gp *tg_pt_gp,
1795 char *page)
1797 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1800 ssize_t core_alua_store_nonop_delay_msecs(
1801 struct t10_alua_tg_pt_gp *tg_pt_gp,
1802 const char *page,
1803 size_t count)
1805 unsigned long tmp;
1806 int ret;
1808 ret = strict_strtoul(page, 0, &tmp);
1809 if (ret < 0) {
1810 pr_err("Unable to extract nonop_delay_msecs\n");
1811 return -EINVAL;
1813 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
1814 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
1815 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1816 ALUA_MAX_NONOP_DELAY_MSECS);
1817 return -EINVAL;
1819 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1821 return count;
1824 ssize_t core_alua_show_trans_delay_msecs(
1825 struct t10_alua_tg_pt_gp *tg_pt_gp,
1826 char *page)
1828 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1831 ssize_t core_alua_store_trans_delay_msecs(
1832 struct t10_alua_tg_pt_gp *tg_pt_gp,
1833 const char *page,
1834 size_t count)
1836 unsigned long tmp;
1837 int ret;
1839 ret = strict_strtoul(page, 0, &tmp);
1840 if (ret < 0) {
1841 pr_err("Unable to extract trans_delay_msecs\n");
1842 return -EINVAL;
1844 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
1845 pr_err("Passed trans_delay_msecs: %lu, exceeds"
1846 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1847 ALUA_MAX_TRANS_DELAY_MSECS);
1848 return -EINVAL;
1850 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1852 return count;
1855 ssize_t core_alua_show_preferred_bit(
1856 struct t10_alua_tg_pt_gp *tg_pt_gp,
1857 char *page)
1859 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1862 ssize_t core_alua_store_preferred_bit(
1863 struct t10_alua_tg_pt_gp *tg_pt_gp,
1864 const char *page,
1865 size_t count)
1867 unsigned long tmp;
1868 int ret;
1870 ret = strict_strtoul(page, 0, &tmp);
1871 if (ret < 0) {
1872 pr_err("Unable to extract preferred ALUA value\n");
1873 return -EINVAL;
1875 if ((tmp != 0) && (tmp != 1)) {
1876 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
1877 return -EINVAL;
1879 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1881 return count;
1884 ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1886 if (!lun->lun_sep)
1887 return -ENODEV;
1889 return sprintf(page, "%d\n",
1890 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1893 ssize_t core_alua_store_offline_bit(
1894 struct se_lun *lun,
1895 const char *page,
1896 size_t count)
1898 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1899 unsigned long tmp;
1900 int ret;
1902 if (!lun->lun_sep)
1903 return -ENODEV;
1905 ret = strict_strtoul(page, 0, &tmp);
1906 if (ret < 0) {
1907 pr_err("Unable to extract alua_tg_pt_offline value\n");
1908 return -EINVAL;
1910 if ((tmp != 0) && (tmp != 1)) {
1911 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
1912 tmp);
1913 return -EINVAL;
1915 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
1916 if (!tg_pt_gp_mem) {
1917 pr_err("Unable to locate *tg_pt_gp_mem\n");
1918 return -EINVAL;
1921 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1922 lun->lun_sep, 0, (int)tmp);
1923 if (ret < 0)
1924 return -EINVAL;
1926 return count;
1929 ssize_t core_alua_show_secondary_status(
1930 struct se_lun *lun,
1931 char *page)
1933 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1936 ssize_t core_alua_store_secondary_status(
1937 struct se_lun *lun,
1938 const char *page,
1939 size_t count)
1941 unsigned long tmp;
1942 int ret;
1944 ret = strict_strtoul(page, 0, &tmp);
1945 if (ret < 0) {
1946 pr_err("Unable to extract alua_tg_pt_status\n");
1947 return -EINVAL;
1949 if ((tmp != ALUA_STATUS_NONE) &&
1950 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1951 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
1952 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
1953 tmp);
1954 return -EINVAL;
1956 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
1958 return count;
1961 ssize_t core_alua_show_secondary_write_metadata(
1962 struct se_lun *lun,
1963 char *page)
1965 return sprintf(page, "%d\n",
1966 lun->lun_sep->sep_tg_pt_secondary_write_md);
1969 ssize_t core_alua_store_secondary_write_metadata(
1970 struct se_lun *lun,
1971 const char *page,
1972 size_t count)
1974 unsigned long tmp;
1975 int ret;
1977 ret = strict_strtoul(page, 0, &tmp);
1978 if (ret < 0) {
1979 pr_err("Unable to extract alua_tg_pt_write_md\n");
1980 return -EINVAL;
1982 if ((tmp != 0) && (tmp != 1)) {
1983 pr_err("Illegal value for alua_tg_pt_write_md:"
1984 " %lu\n", tmp);
1985 return -EINVAL;
1987 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
1989 return count;
1992 int core_setup_alua(struct se_device *dev, int force_pt)
1994 struct se_subsystem_dev *su_dev = dev->se_sub_dev;
1995 struct t10_alua *alua = &su_dev->t10_alua;
1996 struct t10_alua_lu_gp_member *lu_gp_mem;
1998 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
1999 * of the Underlying SCSI hardware. In Linux/SCSI terms, this can
2000 * cause a problem because libata and some SATA RAID HBAs appear
2001 * under Linux/SCSI, but emulate SCSI logic themselves.
2003 if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
2004 !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
2005 alua->alua_type = SPC_ALUA_PASSTHROUGH;
2006 alua->alua_state_check = &core_alua_state_check_nop;
2007 pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
2008 " emulation\n", dev->transport->name);
2009 return 0;
2012 * If SPC-3 or above is reported by real or emulated struct se_device,
2013 * use emulated ALUA.
2015 if (dev->transport->get_device_rev(dev) >= SCSI_3) {
2016 pr_debug("%s: Enabling ALUA Emulation for SPC-3"
2017 " device\n", dev->transport->name);
2019 * Associate this struct se_device with the default ALUA
2020 * LUN Group.
2022 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
2023 if (IS_ERR(lu_gp_mem))
2024 return PTR_ERR(lu_gp_mem);
2026 alua->alua_type = SPC3_ALUA_EMULATED;
2027 alua->alua_state_check = &core_alua_state_check;
2028 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2029 __core_alua_attach_lu_gp_mem(lu_gp_mem,
2030 default_lu_gp);
2031 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2033 pr_debug("%s: Adding to default ALUA LU Group:"
2034 " core/alua/lu_gps/default_lu_gp\n",
2035 dev->transport->name);
2036 } else {
2037 alua->alua_type = SPC2_ALUA_DISABLED;
2038 alua->alua_state_check = &core_alua_state_check_nop;
2039 pr_debug("%s: Disabling ALUA Emulation for SPC-2"
2040 " device\n", dev->transport->name);
2043 return 0;