blob: 1109c2833fe69ca85a5130dafb397bca870cedfc [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_alua.c
3 *
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2009-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
8 * Nicholas A. Bellinger <nab@kernel.org>
9 *
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.
14 *
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.
19 *
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.
23 *
24 ******************************************************************************/
25
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080026#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/configfs.h>
Paul Gortmakerc53181a2011-08-30 18:16:43 -040029#include <linux/export.h>
Al Viro0e9b10a2013-02-23 15:22:43 -050030#include <linux/file.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080031#include <scsi/scsi.h>
32#include <scsi/scsi_cmnd.h>
Roland Dreier33395fb2012-02-23 17:22:12 -080033#include <asm/unaligned.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080034
35#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050036#include <target/target_core_backend.h>
37#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080038
Christoph Hellwige26d99a2011-11-14 12:30:30 -050039#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include "target_core_ua.h"
42
Hannes Reineckebb91c1a2013-12-17 09:18:43 +010043static sense_reason_t core_alua_check_transition(int state, int valid,
44 int *primary);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080045static int core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -070046 struct se_lun *lun, int explicit, int offline);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
Hannes Reineckebb91c1a2013-12-17 09:18:43 +010048static char *core_alua_dump_state(int state);
49
Christoph Hellwigadf653f2015-05-25 21:33:08 -070050static void __target_attach_tg_pt_gp(struct se_lun *lun,
51 struct t10_alua_tg_pt_gp *tg_pt_gp);
52
Andy Grovere3d6f902011-07-19 08:55:10 +000053static u16 alua_lu_gps_counter;
54static u32 alua_lu_gps_count;
55
56static DEFINE_SPINLOCK(lu_gps_lock);
57static LIST_HEAD(lu_gps_list);
58
59struct t10_alua_lu_gp *default_lu_gp;
60
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061/*
Hannes Reineckec66094b2013-12-17 09:18:49 +010062 * REPORT REFERRALS
63 *
64 * See sbc3r35 section 5.23
65 */
66sense_reason_t
67target_emulate_report_referrals(struct se_cmd *cmd)
68{
69 struct se_device *dev = cmd->se_dev;
70 struct t10_alua_lba_map *map;
71 struct t10_alua_lba_map_member *map_mem;
72 unsigned char *buf;
73 u32 rd_len = 0, off;
74
75 if (cmd->data_length < 4) {
76 pr_warn("REPORT REFERRALS allocation length %u too"
77 " small\n", cmd->data_length);
78 return TCM_INVALID_CDB_FIELD;
79 }
80
81 buf = transport_kmap_data_sg(cmd);
82 if (!buf)
83 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
84
85 off = 4;
86 spin_lock(&dev->t10_alua.lba_map_lock);
87 if (list_empty(&dev->t10_alua.lba_map_list)) {
88 spin_unlock(&dev->t10_alua.lba_map_lock);
89 transport_kunmap_data_sg(cmd);
90
91 return TCM_UNSUPPORTED_SCSI_OPCODE;
92 }
93
94 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
95 lba_map_list) {
96 int desc_num = off + 3;
97 int pg_num;
98
99 off += 4;
Hannes Reinecke38edd722013-12-19 14:36:11 +0100100 if (cmd->data_length > off)
101 put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100102 off += 8;
Hannes Reinecke38edd722013-12-19 14:36:11 +0100103 if (cmd->data_length > off)
104 put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100105 off += 8;
106 rd_len += 20;
107 pg_num = 0;
108 list_for_each_entry(map_mem, &map->lba_map_mem_list,
109 lba_map_mem_list) {
Hannes Reinecke38edd722013-12-19 14:36:11 +0100110 int alua_state = map_mem->lba_map_mem_alua_state;
111 int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;
112
113 if (cmd->data_length > off)
114 buf[off] = alua_state & 0x0f;
115 off += 2;
116 if (cmd->data_length > off)
117 buf[off] = (alua_pg_id >> 8) & 0xff;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100118 off++;
Hannes Reinecke38edd722013-12-19 14:36:11 +0100119 if (cmd->data_length > off)
120 buf[off] = (alua_pg_id & 0xff);
121 off++;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100122 rd_len += 4;
123 pg_num++;
124 }
Hannes Reinecke38edd722013-12-19 14:36:11 +0100125 if (cmd->data_length > desc_num)
126 buf[desc_num] = pg_num;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100127 }
128 spin_unlock(&dev->t10_alua.lba_map_lock);
129
130 /*
131 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
132 */
133 put_unaligned_be16(rd_len, &buf[2]);
134
135 transport_kunmap_data_sg(cmd);
136
137 target_complete_cmd(cmd, GOOD);
138 return 0;
139}
140
141/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800142 * REPORT_TARGET_PORT_GROUPS
143 *
144 * See spc4r17 section 6.27
145 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800146sense_reason_t
147target_emulate_report_target_port_groups(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800148{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400149 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800150 struct t10_alua_tg_pt_gp *tg_pt_gp;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700151 struct se_lun *lun;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000152 unsigned char *buf;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700153 u32 rd_len = 0, off;
154 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800155
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700156 /*
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700157 * Skip over RESERVED area to first Target port group descriptor
158 * depending on the PARAMETER DATA FORMAT type..
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700159 */
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700160 if (ext_hdr != 0)
161 off = 8;
162 else
163 off = 4;
164
165 if (cmd->data_length < off) {
166 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
167 " small for %s header\n", cmd->data_length,
168 (ext_hdr) ? "extended" : "normal");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800169 return TCM_INVALID_CDB_FIELD;
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700170 }
Andy Grover49493142012-01-16 16:57:08 -0800171 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800172 if (!buf)
173 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000174
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400175 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
176 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800177 tg_pt_gp_list) {
178 /*
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700179 * Check if the Target port group and Target port descriptor list
180 * based on tg_pt_gp_members count will fit into the response payload.
181 * Otherwise, bump rd_len to let the initiator know we have exceeded
182 * the allocation length and the response is truncated.
183 */
184 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
185 cmd->data_length) {
186 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
187 continue;
188 }
189 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190 * PREF: Preferred target port bit, determine if this
191 * bit should be set for port group.
192 */
193 if (tg_pt_gp->tg_pt_gp_pref)
194 buf[off] = 0x80;
195 /*
196 * Set the ASYMMETRIC ACCESS State
197 */
198 buf[off++] |= (atomic_read(
199 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
200 /*
201 * Set supported ASYMMETRIC ACCESS State bits
202 */
Hannes Reineckec0dc9412013-11-19 09:07:49 +0100203 buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204 /*
205 * TARGET PORT GROUP
206 */
207 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
208 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
209
210 off++; /* Skip over Reserved */
211 /*
212 * STATUS CODE
213 */
214 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
215 /*
216 * Vendor Specific field
217 */
218 buf[off++] = 0x00;
219 /*
220 * TARGET PORT COUNT
221 */
222 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
223 rd_len += 8;
224
225 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700226 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
227 lun_tg_pt_gp_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228 /*
229 * Start Target Port descriptor format
230 *
231 * See spc4r17 section 6.2.7 Table 247
232 */
233 off += 2; /* Skip over Obsolete */
234 /*
235 * Set RELATIVE TARGET PORT IDENTIFIER
236 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700237 buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
238 buf[off++] = (lun->lun_rtpi & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800239 rd_len += 4;
240 }
241 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
242 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400243 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800244 /*
245 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
246 */
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700247 put_unaligned_be32(rd_len, &buf[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800248
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700249 /*
250 * Fill in the Extended header parameter data format if requested
251 */
252 if (ext_hdr != 0) {
253 buf[4] = 0x10;
254 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100255 * Set the implicit transition time (in seconds) for the application
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700256 * client to use as a base for it's transition timeout value.
257 *
258 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
259 * this CDB was received upon to determine this value individually
260 * for ALUA target port group.
261 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700262 spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
263 tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
264 if (tg_pt_gp)
265 buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
266 spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700267 }
Andy Grover49493142012-01-16 16:57:08 -0800268 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000269
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400270 target_complete_cmd(cmd, GOOD);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800271 return 0;
272}
273
274/*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100275 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276 *
277 * See spc4r17 section 6.35
278 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800279sense_reason_t
280target_emulate_set_target_port_groups(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281{
Andy Grover5951146d2011-07-19 10:26:37 +0000282 struct se_device *dev = cmd->se_dev;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700283 struct se_lun *l_lun = cmd->se_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +0000284 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800285 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000286 unsigned char *buf;
287 unsigned char *ptr;
Hannes Reineckea0d50f62012-12-17 09:53:34 +0100288 sense_reason_t rc = TCM_NO_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800289 u32 len = 4; /* Skip over RESERVED area in header */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100290 int alua_access_state, primary = 0, valid_states;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800291 u16 tg_pt_id, rtpi;
292
Paolo Bonzini0d7f1292012-09-07 17:30:33 +0200293 if (cmd->data_length < 4) {
294 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
295 " small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800296 return TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +0200297 }
298
Andy Grover49493142012-01-16 16:57:08 -0800299 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800300 if (!buf)
301 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000302
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100304 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800305 * for the local tg_pt_gp.
306 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700307 spin_lock(&l_lun->lun_tg_pt_gp_lock);
308 l_tg_pt_gp = l_lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -0700309 if (!l_tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700310 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
311 pr_err("Unable to access l_lun->tg_pt_gp\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800312 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000313 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315
Hannes Reinecke125d0112013-11-19 09:07:46 +0100316 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700317 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700318 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
Hannes Reinecke125d0112013-11-19 09:07:46 +0100319 " while TPGS_EXPLICIT_ALUA is disabled\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800320 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000321 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800322 }
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100323 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700324 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800325
Andy Grover05d1c7c2011-07-20 19:13:28 +0000326 ptr = &buf[4]; /* Skip over RESERVED area in header */
327
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328 while (len < cmd->data_length) {
Christoph Hellwigde103c92012-11-06 12:24:09 -0800329 bool found = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800330 alua_access_state = (ptr[0] & 0x0f);
331 /*
332 * Check the received ALUA access state, and determine if
333 * the state is a primary or secondary target port asymmetric
334 * access state.
335 */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100336 rc = core_alua_check_transition(alua_access_state,
337 valid_states, &primary);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800338 if (rc) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800339 /*
340 * If the SET TARGET PORT GROUPS attempts to establish
341 * an invalid combination of target port asymmetric
342 * access states or attempts to establish an
343 * unsupported target port asymmetric access state,
344 * then the command shall be terminated with CHECK
345 * CONDITION status, with the sense key set to ILLEGAL
346 * REQUEST, and the additional sense code set to INVALID
347 * FIELD IN PARAMETER LIST.
348 */
Andy Grover05d1c7c2011-07-20 19:13:28 +0000349 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800350 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800351
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800352 /*
353 * If the ASYMMETRIC ACCESS STATE field (see table 267)
354 * specifies a primary target port asymmetric access state,
355 * then the TARGET PORT GROUP OR TARGET PORT field specifies
356 * a primary target port group for which the primary target
357 * port asymmetric access state shall be changed. If the
358 * ASYMMETRIC ACCESS STATE field specifies a secondary target
359 * port asymmetric access state, then the TARGET PORT GROUP OR
360 * TARGET PORT field specifies the relative target port
361 * identifier (see 3.1.120) of the target port for which the
362 * secondary target port asymmetric access state shall be
363 * changed.
364 */
365 if (primary) {
Roland Dreier33395fb2012-02-23 17:22:12 -0800366 tg_pt_id = get_unaligned_be16(ptr + 2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800367 /*
368 * Locate the matching target port group ID from
369 * the global tg_pt_gp list
370 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400371 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400373 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700375 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800376 continue;
377
378 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
379 continue;
380
Joern Engel33940d02014-09-16 16:23:12 -0400381 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800382
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400383 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384
Christoph Hellwigde103c92012-11-06 12:24:09 -0800385 if (!core_alua_do_port_transition(tg_pt_gp,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700386 dev, l_lun, nacl,
Christoph Hellwigde103c92012-11-06 12:24:09 -0800387 alua_access_state, 1))
388 found = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400390 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Joern Engel33940d02014-09-16 16:23:12 -0400391 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 break;
393 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400394 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 } else {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700396 struct se_lun *lun;
397
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 /*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100399 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400 * the Target Port in question for the the incoming
401 * SET_TARGET_PORT_GROUPS op.
402 */
Roland Dreier33395fb2012-02-23 17:22:12 -0800403 rtpi = get_unaligned_be16(ptr + 2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800404 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +0900405 * Locate the matching relative target port identifier
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 * for the struct se_device storage object.
407 */
408 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700409 list_for_each_entry(lun, &dev->dev_sep_list,
410 lun_dev_link) {
411 if (lun->lun_rtpi != rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 continue;
413
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700414 // XXX: racy unlock
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 spin_unlock(&dev->se_port_lock);
416
Christoph Hellwigde103c92012-11-06 12:24:09 -0800417 if (!core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700418 lun, 1, 1))
Christoph Hellwigde103c92012-11-06 12:24:09 -0800419 found = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800420
421 spin_lock(&dev->se_port_lock);
422 break;
423 }
424 spin_unlock(&dev->se_port_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800425 }
426
427 if (!found) {
428 rc = TCM_INVALID_PARAMETER_LIST;
429 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 }
431
432 ptr += 4;
433 len += 4;
434 }
435
Andy Grover05d1c7c2011-07-20 19:13:28 +0000436out:
Andy Grover49493142012-01-16 16:57:08 -0800437 transport_kunmap_data_sg(cmd);
Roland Dreier59e4f542012-06-04 23:24:51 -0700438 if (!rc)
439 target_complete_cmd(cmd, GOOD);
440 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441}
442
Paul Bolle1e3ab992014-02-20 09:07:52 +0100443static inline void set_ascq(struct se_cmd *cmd, u8 alua_ascq)
444{
445 /*
446 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
447 * The ALUA additional sense code qualifier (ASCQ) is determined
448 * by the ALUA primary or secondary access state..
449 */
450 pr_debug("[%s]: ALUA TG Port not available, "
451 "SenseKey: NOT_READY, ASC/ASCQ: "
452 "0x04/0x%02x\n",
453 cmd->se_tfo->get_fabric_name(), alua_ascq);
454
455 cmd->scsi_asc = 0x04;
456 cmd->scsi_ascq = alua_ascq;
457}
458
459static inline void core_alua_state_nonoptimized(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460 struct se_cmd *cmd,
461 unsigned char *cdb,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100462 int nonop_delay_msecs)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800463{
464 /*
465 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
466 * later to determine if processing of this cmd needs to be
467 * temporarily delayed for the Active/NonOptimized primary access state.
468 */
469 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
470 cmd->alua_nonop_delay = nonop_delay_msecs;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471}
472
Hannes Reineckec66094b2013-12-17 09:18:49 +0100473static inline int core_alua_state_lba_dependent(
474 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100475 struct t10_alua_tg_pt_gp *tg_pt_gp)
Hannes Reineckec66094b2013-12-17 09:18:49 +0100476{
477 struct se_device *dev = cmd->se_dev;
478 u64 segment_size, segment_mult, sectors, lba;
479
480 /* Only need to check for cdb actually containing LBAs */
481 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
482 return 0;
483
484 spin_lock(&dev->t10_alua.lba_map_lock);
485 segment_size = dev->t10_alua.lba_map_segment_size;
486 segment_mult = dev->t10_alua.lba_map_segment_multiplier;
487 sectors = cmd->data_length / dev->dev_attrib.block_size;
488
489 lba = cmd->t_task_lba;
490 while (lba < cmd->t_task_lba + sectors) {
491 struct t10_alua_lba_map *cur_map = NULL, *map;
492 struct t10_alua_lba_map_member *map_mem;
493
494 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
495 lba_map_list) {
496 u64 start_lba, last_lba;
497 u64 first_lba = map->lba_map_first_lba;
498
499 if (segment_mult) {
500 u64 tmp = lba;
Nicholas Bellingercdf55942014-01-30 14:05:16 -0800501 start_lba = do_div(tmp, segment_size * segment_mult);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100502
503 last_lba = first_lba + segment_size - 1;
504 if (start_lba >= first_lba &&
505 start_lba <= last_lba) {
506 lba += segment_size;
507 cur_map = map;
508 break;
509 }
510 } else {
511 last_lba = map->lba_map_last_lba;
512 if (lba >= first_lba && lba <= last_lba) {
513 lba = last_lba + 1;
514 cur_map = map;
515 break;
516 }
517 }
518 }
519 if (!cur_map) {
520 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100521 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100522 return 1;
523 }
524 list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
525 lba_map_mem_list) {
526 if (map_mem->lba_map_mem_alua_pg_id !=
527 tg_pt_gp->tg_pt_gp_id)
528 continue;
529 switch(map_mem->lba_map_mem_alua_state) {
530 case ALUA_ACCESS_STATE_STANDBY:
531 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100532 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100533 return 1;
534 case ALUA_ACCESS_STATE_UNAVAILABLE:
535 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100536 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100537 return 1;
538 default:
539 break;
540 }
541 }
542 }
543 spin_unlock(&dev->t10_alua.lba_map_lock);
544 return 0;
545}
546
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547static inline int core_alua_state_standby(
548 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100549 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800550{
551 /*
552 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
553 * spc4r17 section 5.9.2.4.4
554 */
555 switch (cdb[0]) {
556 case INQUIRY:
557 case LOG_SELECT:
558 case LOG_SENSE:
559 case MODE_SELECT:
560 case MODE_SENSE:
561 case REPORT_LUNS:
562 case RECEIVE_DIAGNOSTIC:
563 case SEND_DIAGNOSTIC:
Nicholas Bellingere7810c22014-06-05 23:37:00 -0700564 case READ_CAPACITY:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700565 return 0;
Hannes Reineckeeb846d92014-11-17 14:25:19 +0100566 case SERVICE_ACTION_IN_16:
Nicholas Bellingere7810c22014-06-05 23:37:00 -0700567 switch (cdb[1] & 0x1f) {
568 case SAI_READ_CAPACITY_16:
569 return 0;
570 default:
571 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
572 return 1;
573 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700575 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800576 case MI_REPORT_TARGET_PGS:
577 return 0;
578 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100579 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580 return 1;
581 }
582 case MAINTENANCE_OUT:
583 switch (cdb[1]) {
584 case MO_SET_TARGET_PGS:
585 return 0;
586 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100587 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 return 1;
589 }
590 case REQUEST_SENSE:
591 case PERSISTENT_RESERVE_IN:
592 case PERSISTENT_RESERVE_OUT:
593 case READ_BUFFER:
594 case WRITE_BUFFER:
595 return 0;
596 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100597 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800598 return 1;
599 }
600
601 return 0;
602}
603
604static inline int core_alua_state_unavailable(
605 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100606 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800607{
608 /*
609 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
610 * spc4r17 section 5.9.2.4.5
611 */
612 switch (cdb[0]) {
613 case INQUIRY:
614 case REPORT_LUNS:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700615 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800616 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700617 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800618 case MI_REPORT_TARGET_PGS:
619 return 0;
620 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100621 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800622 return 1;
623 }
624 case MAINTENANCE_OUT:
625 switch (cdb[1]) {
626 case MO_SET_TARGET_PGS:
627 return 0;
628 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100629 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630 return 1;
631 }
632 case REQUEST_SENSE:
633 case READ_BUFFER:
634 case WRITE_BUFFER:
635 return 0;
636 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100637 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800638 return 1;
639 }
640
641 return 0;
642}
643
644static inline int core_alua_state_transition(
645 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100646 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800647{
648 /*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100649 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800650 * spc4r17 section 5.9.2.5
651 */
652 switch (cdb[0]) {
653 case INQUIRY:
654 case REPORT_LUNS:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700655 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700657 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800658 case MI_REPORT_TARGET_PGS:
659 return 0;
660 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100661 set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800662 return 1;
663 }
664 case REQUEST_SENSE:
665 case READ_BUFFER:
666 case WRITE_BUFFER:
667 return 0;
668 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100669 set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800670 return 1;
671 }
672
673 return 0;
674}
675
676/*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100677 * return 1: Is used to signal LUN not accessible, and check condition/not ready
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800678 * return 0: Used to signal success
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100679 * return -1: Used to signal failure, and invalid cdb field
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800681sense_reason_t
682target_alua_state_check(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800683{
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400684 struct se_device *dev = cmd->se_dev;
685 unsigned char *cdb = cmd->t_task_cdb;
Andy Grovere3d6f902011-07-19 08:55:10 +0000686 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800687 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800688 int out_alua_state, nonop_delay_msecs;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400689
690 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
691 return 0;
Andy Grovera3541702015-05-19 14:44:41 -0700692 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400693 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800694
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 /*
696 * First, check for a struct se_port specific secondary ALUA target port
697 * access state: OFFLINE
698 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700699 if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700700 pr_debug("ALUA: Got secondary offline status for local"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800701 " target port\n");
Paul Bolle1e3ab992014-02-20 09:07:52 +0100702 set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE);
703 return TCM_CHECK_CONDITION_NOT_READY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800704 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700705
706 if (!lun->lun_tg_pt_gp)
Nicholas Bellinger89c12cc2013-08-22 15:35:45 -0700707 return 0;
708
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700709 spin_lock(&lun->lun_tg_pt_gp_lock);
710 tg_pt_gp = lun->lun_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800711 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
712 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700713
714 // XXX: keeps using tg_pt_gp witout reference after unlock
715 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800716 /*
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100717 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300718 * statement so the compiler knows explicitly to check this case first.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800719 * For the Optimized ALUA access state case, we want to process the
720 * incoming fabric cmd ASAP..
721 */
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100722 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800723 return 0;
724
725 switch (out_alua_state) {
726 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100727 core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400728 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800729 case ALUA_ACCESS_STATE_STANDBY:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100730 if (core_alua_state_standby(cmd, cdb))
731 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400732 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800733 case ALUA_ACCESS_STATE_UNAVAILABLE:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100734 if (core_alua_state_unavailable(cmd, cdb))
735 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400736 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800737 case ALUA_ACCESS_STATE_TRANSITION:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100738 if (core_alua_state_transition(cmd, cdb))
739 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400740 break;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100741 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100742 if (core_alua_state_lba_dependent(cmd, tg_pt_gp))
743 return TCM_CHECK_CONDITION_NOT_READY;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100744 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800745 /*
746 * OFFLINE is a secondary ALUA target port group access state, that is
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700747 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800748 */
749 case ALUA_ACCESS_STATE_OFFLINE:
750 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700751 pr_err("Unknown ALUA access state: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800752 out_alua_state);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800753 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800754 }
755
Christoph Hellwigde103c92012-11-06 12:24:09 -0800756 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757}
758
759/*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100760 * Check implicit and explicit ALUA state change request.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800761 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800762static sense_reason_t
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100763core_alua_check_transition(int state, int valid, int *primary)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800764{
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100765 /*
766 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
767 * defined as primary target port asymmetric access states.
768 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800769 switch (state) {
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100770 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100771 if (!(valid & ALUA_AO_SUP))
772 goto not_supported;
773 *primary = 1;
774 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800775 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100776 if (!(valid & ALUA_AN_SUP))
777 goto not_supported;
778 *primary = 1;
779 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800780 case ALUA_ACCESS_STATE_STANDBY:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100781 if (!(valid & ALUA_S_SUP))
782 goto not_supported;
783 *primary = 1;
784 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800785 case ALUA_ACCESS_STATE_UNAVAILABLE:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100786 if (!(valid & ALUA_U_SUP))
787 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800788 *primary = 1;
789 break;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100790 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
791 if (!(valid & ALUA_LBD_SUP))
792 goto not_supported;
793 *primary = 1;
794 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800795 case ALUA_ACCESS_STATE_OFFLINE:
796 /*
797 * OFFLINE state is defined as a secondary target port
798 * asymmetric access state.
799 */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100800 if (!(valid & ALUA_O_SUP))
801 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800802 *primary = 0;
803 break;
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100804 case ALUA_ACCESS_STATE_TRANSITION:
805 /*
806 * Transitioning is set internally, and
807 * cannot be selected manually.
808 */
809 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800810 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700811 pr_err("Unknown ALUA access state: 0x%02x\n", state);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800812 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800813 }
814
815 return 0;
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100816
817not_supported:
818 pr_err("ALUA access state %s not supported",
819 core_alua_dump_state(state));
820 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800821}
822
823static char *core_alua_dump_state(int state)
824{
825 switch (state) {
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100826 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800827 return "Active/Optimized";
828 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
829 return "Active/NonOptimized";
Hannes Reineckec66094b2013-12-17 09:18:49 +0100830 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
831 return "LBA Dependent";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800832 case ALUA_ACCESS_STATE_STANDBY:
833 return "Standby";
834 case ALUA_ACCESS_STATE_UNAVAILABLE:
835 return "Unavailable";
836 case ALUA_ACCESS_STATE_OFFLINE:
837 return "Offline";
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100838 case ALUA_ACCESS_STATE_TRANSITION:
839 return "Transitioning";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800840 default:
841 return "Unknown";
842 }
843
844 return NULL;
845}
846
847char *core_alua_dump_status(int status)
848{
849 switch (status) {
850 case ALUA_STATUS_NONE:
851 return "None";
Hannes Reinecke125d0112013-11-19 09:07:46 +0100852 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
853 return "Altered by Explicit STPG";
854 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
855 return "Altered by Implicit ALUA";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856 default:
857 return "Unknown";
858 }
859
860 return NULL;
861}
862
863/*
864 * Used by fabric modules to determine when we need to delay processing
865 * for the Active/NonOptimized paths..
866 */
867int core_alua_check_nonop_delay(
868 struct se_cmd *cmd)
869{
870 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
871 return 0;
872 if (in_interrupt())
873 return 0;
874 /*
875 * The ALUA Active/NonOptimized access state delay can be disabled
876 * in via configfs with a value of zero
877 */
Andy Grover6708bb22011-06-08 10:36:43 -0700878 if (!cmd->alua_nonop_delay)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800879 return 0;
880 /*
881 * struct se_cmd->alua_nonop_delay gets set by a target port group
882 * defined interval in core_alua_state_nonoptimized()
883 */
884 msleep_interruptible(cmd->alua_nonop_delay);
885 return 0;
886}
887EXPORT_SYMBOL(core_alua_check_nonop_delay);
888
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800889static int core_alua_write_tpg_metadata(
890 const char *path,
891 unsigned char *md_buf,
892 u32 md_buf_len)
893{
Al Viro0e9b10a2013-02-23 15:22:43 -0500894 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
895 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800896
Al Viro0e9b10a2013-02-23 15:22:43 -0500897 if (IS_ERR(file)) {
898 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800899 return -ENODEV;
900 }
Al Viro0e9b10a2013-02-23 15:22:43 -0500901 ret = kernel_write(file, md_buf, md_buf_len, 0);
902 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700903 pr_err("Error writing ALUA metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -0500904 fput(file);
Gera Kazakovf730f912013-09-09 15:47:06 -0700905 return (ret < 0) ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800906}
907
908/*
909 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
910 */
911static int core_alua_update_tpg_primary_metadata(
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100912 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800913{
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100914 unsigned char *md_buf;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400915 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800916 char path[ALUA_METADATA_PATH_LEN];
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100917 int len, rc;
918
919 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
920 if (!md_buf) {
921 pr_err("Unable to allocate buf for ALUA metadata\n");
922 return -ENOMEM;
923 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800924
925 memset(path, 0, ALUA_METADATA_PATH_LEN);
926
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100927 len = snprintf(md_buf, ALUA_MD_BUF_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800928 "tg_pt_gp_id=%hu\n"
929 "alua_access_state=0x%02x\n"
930 "alua_access_status=0x%02x\n",
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100931 tg_pt_gp->tg_pt_gp_id,
932 tg_pt_gp->tg_pt_gp_alua_pending_state,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800933 tg_pt_gp->tg_pt_gp_alua_access_status);
934
935 snprintf(path, ALUA_METADATA_PATH_LEN,
936 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
937 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
938
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100939 rc = core_alua_write_tpg_metadata(path, md_buf, len);
940 kfree(md_buf);
941 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800942}
943
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100944static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800945{
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100946 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work,
947 struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work.work);
948 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800949 struct se_dev_entry *se_deve;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700950 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800951 struct se_lun_acl *lacl;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100952 bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
953 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800954
955 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700956 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
957 lun_tg_pt_gp_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958 /*
959 * After an implicit target port asymmetric access state
960 * change, a device server shall establish a unit attention
961 * condition for the initiator port associated with every I_T
962 * nexus with the additional sense code set to ASYMMETRIC
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100963 * ACCESS STATE CHANGED.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800964 *
965 * After an explicit target port asymmetric access state
966 * change, a device server shall establish a unit attention
967 * condition with the additional sense code set to ASYMMETRIC
968 * ACCESS STATE CHANGED for the initiator port associated with
969 * every I_T nexus other than the I_T nexus on which the SET
970 * TARGET PORT GROUPS command
971 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700972 atomic_inc_mb(&lun->lun_active);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800973 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
974
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700975 spin_lock_bh(&lun->lun_deve_lock);
976 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700977 lacl = rcu_dereference_check(se_deve->se_lun_acl,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700978 lockdep_is_held(&lun->lun_deve_lock));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800979 /*
980 * se_deve->se_lun_acl pointer may be NULL for a
Hannes Reinecke125d0112013-11-19 09:07:46 +0100981 * entry created without explicit Node+MappedLUN ACLs
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800982 */
Andy Grover6708bb22011-06-08 10:36:43 -0700983 if (!lacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800984 continue;
985
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100986 if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
987 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
988 (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
989 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl) &&
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700990 (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
991 (tg_pt_gp->tg_pt_gp_alua_lun == lun))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800992 continue;
993
994 core_scsi3_ua_allocate(lacl->se_lun_nacl,
995 se_deve->mapped_lun, 0x2A,
996 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
997 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700998 spin_unlock_bh(&lun->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800999
1000 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001001 atomic_dec_mb(&lun->lun_active);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001002 }
1003 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1004 /*
1005 * Update the ALUA metadata buf that has been allocated in
1006 * core_alua_do_port_transition(), this metadata will be written
1007 * to struct file.
1008 *
1009 * Note that there is the case where we do not want to update the
1010 * metadata when the saved metadata is being parsed in userspace
1011 * when setting the existing port access state and access status.
1012 *
1013 * Also note that the failure to write out the ALUA metadata to
1014 * struct file does NOT affect the actual ALUA transition.
1015 */
1016 if (tg_pt_gp->tg_pt_gp_write_metadata) {
1017 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001018 core_alua_update_tpg_primary_metadata(tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001019 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
1020 }
1021 /*
1022 * Set the current primary ALUA access state to the requested new state
1023 */
Hannes Reineckedfbce752013-12-17 09:18:45 +01001024 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1025 tg_pt_gp->tg_pt_gp_alua_pending_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001026
Andy Grover6708bb22011-06-08 10:36:43 -07001027 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Hannes Reinecke125d0112013-11-19 09:07:46 +01001028 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1029 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
Hannes Reineckedfbce752013-12-17 09:18:45 +01001030 tg_pt_gp->tg_pt_gp_id,
1031 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state),
1032 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001033 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1034 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001035 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1036
1037 if (tg_pt_gp->tg_pt_gp_transition_complete)
1038 complete(tg_pt_gp->tg_pt_gp_transition_complete);
1039}
1040
1041static int core_alua_do_transition_tg_pt(
1042 struct t10_alua_tg_pt_gp *tg_pt_gp,
1043 int new_state,
1044 int explicit)
1045{
1046 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1047 DECLARE_COMPLETION_ONSTACK(wait);
1048
1049 /* Nothing to be done here */
1050 if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state)
1051 return 0;
1052
1053 if (new_state == ALUA_ACCESS_STATE_TRANSITION)
1054 return -EAGAIN;
1055
1056 /*
1057 * Flush any pending transitions
1058 */
1059 if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs &&
1060 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) ==
1061 ALUA_ACCESS_STATE_TRANSITION) {
1062 /* Just in case */
1063 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1064 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1065 flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
1066 wait_for_completion(&wait);
1067 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1068 return 0;
1069 }
1070
1071 /*
1072 * Save the old primary ALUA access state, and set the current state
1073 * to ALUA_ACCESS_STATE_TRANSITION.
1074 */
1075 tg_pt_gp->tg_pt_gp_alua_previous_state =
1076 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
1077 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1078
1079 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1080 ALUA_ACCESS_STATE_TRANSITION);
1081 tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1082 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1083 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1084
1085 /*
1086 * Check for the optional ALUA primary state transition delay
1087 */
1088 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
1089 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1090
1091 /*
1092 * Take a reference for workqueue item
1093 */
1094 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1095 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001096 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1097
1098 if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs) {
1099 unsigned long transition_tmo;
1100
1101 transition_tmo = tg_pt_gp->tg_pt_gp_implicit_trans_secs * HZ;
1102 queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
1103 &tg_pt_gp->tg_pt_gp_transition_work,
1104 transition_tmo);
1105 } else {
1106 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1107 queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
1108 &tg_pt_gp->tg_pt_gp_transition_work, 0);
1109 wait_for_completion(&wait);
1110 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1111 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001112
1113 return 0;
1114}
1115
1116int core_alua_do_port_transition(
1117 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1118 struct se_device *l_dev,
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001119 struct se_lun *l_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001120 struct se_node_acl *l_nacl,
1121 int new_state,
Hannes Reinecke125d0112013-11-19 09:07:46 +01001122 int explicit)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001123{
1124 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001125 struct t10_alua_lu_gp *lu_gp;
1126 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1127 struct t10_alua_tg_pt_gp *tg_pt_gp;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001128 int primary, valid_states, rc = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001129
Hannes Reineckebb91c1a2013-12-17 09:18:43 +01001130 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1131 if (core_alua_check_transition(new_state, valid_states, &primary) != 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132 return -EINVAL;
1133
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001134 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1135 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1136 lu_gp = local_lu_gp_mem->lu_gp;
1137 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001138 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1139 /*
1140 * For storage objects that are members of the 'default_lu_gp',
1141 * we only do transition on the passed *l_tp_pt_gp, and not
1142 * on all of the matching target port groups IDs in default_lu_gp.
1143 */
Andy Grover6708bb22011-06-08 10:36:43 -07001144 if (!lu_gp->lu_gp_id) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 /*
1146 * core_alua_do_transition_tg_pt() will always return
1147 * success.
1148 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001149 l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001150 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1151 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1152 new_state, explicit);
Joern Engel33940d02014-09-16 16:23:12 -04001153 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001154 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155 }
1156 /*
1157 * For all other LU groups aside from 'default_lu_gp', walk all of
1158 * the associated storage objects looking for a matching target port
1159 * group ID from the local target port group.
1160 */
1161 spin_lock(&lu_gp->lu_gp_lock);
1162 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1163 lu_gp_mem_list) {
1164
1165 dev = lu_gp_mem->lu_gp_mem_dev;
Joern Engel33940d02014-09-16 16:23:12 -04001166 atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001167 spin_unlock(&lu_gp->lu_gp_lock);
1168
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001169 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001170 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001171 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001172 tg_pt_gp_list) {
1173
Andy Grover6708bb22011-06-08 10:36:43 -07001174 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001175 continue;
1176 /*
1177 * If the target behavior port asymmetric access state
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001178 * is changed for any target port group accessible via
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001179 * a logical unit within a LU group, the target port
1180 * behavior group asymmetric access states for the same
1181 * target port group accessible via other logical units
1182 * in that LU group will also change.
1183 */
1184 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1185 continue;
1186
1187 if (l_tg_pt_gp == tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001188 tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001189 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190 } else {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001191 tg_pt_gp->tg_pt_gp_alua_lun = NULL;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001192 tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001193 }
Joern Engel33940d02014-09-16 16:23:12 -04001194 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001195 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001196 /*
1197 * core_alua_do_transition_tg_pt() will always return
1198 * success.
1199 */
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001200 rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1201 new_state, explicit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001202
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001203 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Joern Engel33940d02014-09-16 16:23:12 -04001204 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001205 if (rc)
1206 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001207 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001208 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001209
1210 spin_lock(&lu_gp->lu_gp_lock);
Joern Engel33940d02014-09-16 16:23:12 -04001211 atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212 }
1213 spin_unlock(&lu_gp->lu_gp_lock);
1214
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001215 if (!rc) {
1216 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1217 " Group IDs: %hu %s transition to primary state: %s\n",
1218 config_item_name(&lu_gp->lu_gp_group.cg_item),
1219 l_tg_pt_gp->tg_pt_gp_id,
1220 (explicit) ? "explicit" : "implicit",
1221 core_alua_dump_state(new_state));
1222 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001223
Joern Engel33940d02014-09-16 16:23:12 -04001224 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001225 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001226}
1227
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001228static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001230 struct se_portal_group *se_tpg = lun->lun_tpg;
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001231 unsigned char *md_buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001232 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001233 int len, rc;
1234
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001235 mutex_lock(&lun->lun_tg_pt_md_mutex);
1236
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001237 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1238 if (!md_buf) {
1239 pr_err("Unable to allocate buf for ALUA metadata\n");
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001240 rc = -ENOMEM;
1241 goto out_unlock;
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001242 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001243
1244 memset(path, 0, ALUA_METADATA_PATH_LEN);
1245 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1246
1247 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +00001248 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001249
Andy Grovere3d6f902011-07-19 08:55:10 +00001250 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001251 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
Andy Grovere3d6f902011-07-19 08:55:10 +00001252 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001253
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001254 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001255 "alua_tg_pt_status=0x%02x\n",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001256 atomic_read(&lun->lun_tg_pt_secondary_offline),
1257 lun->lun_tg_pt_secondary_stat);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001258
1259 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
Andy Grovere3d6f902011-07-19 08:55:10 +00001260 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001261 lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001262
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001263 rc = core_alua_write_tpg_metadata(path, md_buf, len);
1264 kfree(md_buf);
1265
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001266out_unlock:
1267 mutex_unlock(&lun->lun_tg_pt_md_mutex);
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001268 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001269}
1270
1271static int core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001272 struct se_lun *lun,
Hannes Reinecke125d0112013-11-19 09:07:46 +01001273 int explicit,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 int offline)
1275{
1276 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001277 int trans_delay_msecs;
1278
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001279 spin_lock(&lun->lun_tg_pt_gp_lock);
1280 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001281 if (!tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001282 spin_unlock(&lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001283 pr_err("Unable to complete secondary state"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001284 " transition\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001285 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001286 }
1287 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1288 /*
1289 * Set the secondary ALUA target port access state to OFFLINE
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001290 * or release the previously secondary state for struct se_lun
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001291 */
1292 if (offline)
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001293 atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001294 else
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001295 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001297 lun->lun_tg_pt_secondary_stat = (explicit) ?
Hannes Reinecke125d0112013-11-19 09:07:46 +01001298 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1299 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001300
Andy Grover6708bb22011-06-08 10:36:43 -07001301 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Hannes Reinecke125d0112013-11-19 09:07:46 +01001302 " to secondary access state: %s\n", (explicit) ? "explicit" :
1303 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1305
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001306 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001307 /*
1308 * Do the optional transition delay after we set the secondary
1309 * ALUA access state.
1310 */
1311 if (trans_delay_msecs != 0)
1312 msleep_interruptible(trans_delay_msecs);
1313 /*
1314 * See if we need to update the ALUA fabric port metadata for
1315 * secondary state and status
1316 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001317 if (lun->lun_tg_pt_secondary_write_md)
1318 core_alua_update_tpg_secondary_metadata(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001319
1320 return 0;
1321}
1322
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001323struct t10_alua_lba_map *
1324core_alua_allocate_lba_map(struct list_head *list,
1325 u64 first_lba, u64 last_lba)
1326{
1327 struct t10_alua_lba_map *lba_map;
1328
1329 lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1330 if (!lba_map) {
1331 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1332 return ERR_PTR(-ENOMEM);
1333 }
1334 INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1335 lba_map->lba_map_first_lba = first_lba;
1336 lba_map->lba_map_last_lba = last_lba;
1337
1338 list_add_tail(&lba_map->lba_map_list, list);
1339 return lba_map;
1340}
1341
1342int
1343core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1344 int pg_id, int state)
1345{
1346 struct t10_alua_lba_map_member *lba_map_mem;
1347
1348 list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1349 lba_map_mem_list) {
1350 if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1351 pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1352 return -EINVAL;
1353 }
1354 }
1355
1356 lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1357 if (!lba_map_mem) {
1358 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1359 return -ENOMEM;
1360 }
1361 lba_map_mem->lba_map_mem_alua_state = state;
1362 lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1363
1364 list_add_tail(&lba_map_mem->lba_map_mem_list,
1365 &lba_map->lba_map_mem_list);
1366 return 0;
1367}
1368
1369void
1370core_alua_free_lba_map(struct list_head *lba_list)
1371{
1372 struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1373 struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1374
1375 list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1376 lba_map_list) {
1377 list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1378 &lba_map->lba_map_mem_list,
1379 lba_map_mem_list) {
1380 list_del(&lba_map_mem->lba_map_mem_list);
1381 kmem_cache_free(t10_alua_lba_map_mem_cache,
1382 lba_map_mem);
1383 }
1384 list_del(&lba_map->lba_map_list);
1385 kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1386 }
1387}
1388
1389void
1390core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1391 int segment_size, int segment_mult)
1392{
1393 struct list_head old_lba_map_list;
1394 struct t10_alua_tg_pt_gp *tg_pt_gp;
1395 int activate = 0, supported;
1396
1397 INIT_LIST_HEAD(&old_lba_map_list);
1398 spin_lock(&dev->t10_alua.lba_map_lock);
1399 dev->t10_alua.lba_map_segment_size = segment_size;
1400 dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1401 list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1402 if (lba_map_list) {
1403 list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1404 activate = 1;
1405 }
1406 spin_unlock(&dev->t10_alua.lba_map_lock);
1407 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1408 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1409 tg_pt_gp_list) {
1410
1411 if (!tg_pt_gp->tg_pt_gp_valid_id)
1412 continue;
1413 supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1414 if (activate)
1415 supported |= ALUA_LBD_SUP;
1416 else
1417 supported &= ~ALUA_LBD_SUP;
1418 tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1419 }
1420 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1421 core_alua_free_lba_map(&old_lba_map_list);
1422}
1423
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001424struct t10_alua_lu_gp *
1425core_alua_allocate_lu_gp(const char *name, int def_group)
1426{
1427 struct t10_alua_lu_gp *lu_gp;
1428
1429 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001430 if (!lu_gp) {
1431 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001432 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001433 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001434 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001435 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1436 spin_lock_init(&lu_gp->lu_gp_lock);
1437 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1438
1439 if (def_group) {
Andy Grovere3d6f902011-07-19 08:55:10 +00001440 lu_gp->lu_gp_id = alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001441 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001442 alua_lu_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443 }
1444
1445 return lu_gp;
1446}
1447
1448int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1449{
1450 struct t10_alua_lu_gp *lu_gp_tmp;
1451 u16 lu_gp_id_tmp;
1452 /*
1453 * The lu_gp->lu_gp_id may only be set once..
1454 */
1455 if (lu_gp->lu_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001456 pr_warn("ALUA LU Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001457 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001458 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001459 }
1460
Andy Grovere3d6f902011-07-19 08:55:10 +00001461 spin_lock(&lu_gps_lock);
1462 if (alua_lu_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001463 pr_err("Maximum ALUA alua_lu_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001464 " 0x0000ffff reached\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001465 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001466 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001467 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001468 }
1469again:
1470 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
Andy Grovere3d6f902011-07-19 08:55:10 +00001471 alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001472
Andy Grovere3d6f902011-07-19 08:55:10 +00001473 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001474 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001475 if (!lu_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 goto again;
1477
Andy Grover6708bb22011-06-08 10:36:43 -07001478 pr_warn("ALUA Logical Unit Group ID: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 " already exists, ignoring request\n",
1480 lu_gp_id);
Andy Grovere3d6f902011-07-19 08:55:10 +00001481 spin_unlock(&lu_gps_lock);
1482 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483 }
1484 }
1485
1486 lu_gp->lu_gp_id = lu_gp_id_tmp;
1487 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001488 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1489 alua_lu_gps_count++;
1490 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001491
1492 return 0;
1493}
1494
1495static struct t10_alua_lu_gp_member *
1496core_alua_allocate_lu_gp_mem(struct se_device *dev)
1497{
1498 struct t10_alua_lu_gp_member *lu_gp_mem;
1499
1500 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001501 if (!lu_gp_mem) {
1502 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001503 return ERR_PTR(-ENOMEM);
1504 }
1505 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1506 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1507 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1508
1509 lu_gp_mem->lu_gp_mem_dev = dev;
1510 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1511
1512 return lu_gp_mem;
1513}
1514
1515void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1516{
1517 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1518 /*
1519 * Once we have reached this point, config_item_put() has
1520 * already been called from target_core_alua_drop_lu_gp().
1521 *
1522 * Here, we remove the *lu_gp from the global list so that
1523 * no associations can be made while we are releasing
1524 * struct t10_alua_lu_gp.
1525 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001526 spin_lock(&lu_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001527 list_del(&lu_gp->lu_gp_node);
1528 alua_lu_gps_count--;
1529 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001530 /*
1531 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1532 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1533 * released with core_alua_put_lu_gp_from_name()
1534 */
1535 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1536 cpu_relax();
1537 /*
1538 * Release reference to struct t10_alua_lu_gp * from all associated
1539 * struct se_device.
1540 */
1541 spin_lock(&lu_gp->lu_gp_lock);
1542 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1543 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1544 if (lu_gp_mem->lu_gp_assoc) {
1545 list_del(&lu_gp_mem->lu_gp_mem_list);
1546 lu_gp->lu_gp_members--;
1547 lu_gp_mem->lu_gp_assoc = 0;
1548 }
1549 spin_unlock(&lu_gp->lu_gp_lock);
1550 /*
1551 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001552 * lu_gp_mem is associated with a single
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001553 * struct se_device->dev_alua_lu_gp_mem, and is released when
1554 * struct se_device is released via core_alua_free_lu_gp_mem().
1555 *
1556 * If the passed lu_gp does NOT match the default_lu_gp, assume
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001557 * we want to re-associate a given lu_gp_mem with default_lu_gp.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001558 */
1559 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001560 if (lu_gp != default_lu_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001561 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00001562 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001563 else
1564 lu_gp_mem->lu_gp = NULL;
1565 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1566
1567 spin_lock(&lu_gp->lu_gp_lock);
1568 }
1569 spin_unlock(&lu_gp->lu_gp_lock);
1570
1571 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1572}
1573
1574void core_alua_free_lu_gp_mem(struct se_device *dev)
1575{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001576 struct t10_alua_lu_gp *lu_gp;
1577 struct t10_alua_lu_gp_member *lu_gp_mem;
1578
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -07001580 if (!lu_gp_mem)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001581 return;
1582
1583 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1584 cpu_relax();
1585
1586 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1587 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001588 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001589 spin_lock(&lu_gp->lu_gp_lock);
1590 if (lu_gp_mem->lu_gp_assoc) {
1591 list_del(&lu_gp_mem->lu_gp_mem_list);
1592 lu_gp->lu_gp_members--;
1593 lu_gp_mem->lu_gp_assoc = 0;
1594 }
1595 spin_unlock(&lu_gp->lu_gp_lock);
1596 lu_gp_mem->lu_gp = NULL;
1597 }
1598 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1599
1600 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1601}
1602
1603struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1604{
1605 struct t10_alua_lu_gp *lu_gp;
1606 struct config_item *ci;
1607
Andy Grovere3d6f902011-07-19 08:55:10 +00001608 spin_lock(&lu_gps_lock);
1609 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
Andy Grover6708bb22011-06-08 10:36:43 -07001610 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001611 continue;
1612 ci = &lu_gp->lu_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001613 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001614 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001615 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001616 return lu_gp;
1617 }
1618 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001619 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001620
1621 return NULL;
1622}
1623
1624void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1625{
Andy Grovere3d6f902011-07-19 08:55:10 +00001626 spin_lock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001627 atomic_dec(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001628 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001629}
1630
1631/*
1632 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1633 */
1634void __core_alua_attach_lu_gp_mem(
1635 struct t10_alua_lu_gp_member *lu_gp_mem,
1636 struct t10_alua_lu_gp *lu_gp)
1637{
1638 spin_lock(&lu_gp->lu_gp_lock);
1639 lu_gp_mem->lu_gp = lu_gp;
1640 lu_gp_mem->lu_gp_assoc = 1;
1641 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1642 lu_gp->lu_gp_members++;
1643 spin_unlock(&lu_gp->lu_gp_lock);
1644}
1645
1646/*
1647 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1648 */
1649void __core_alua_drop_lu_gp_mem(
1650 struct t10_alua_lu_gp_member *lu_gp_mem,
1651 struct t10_alua_lu_gp *lu_gp)
1652{
1653 spin_lock(&lu_gp->lu_gp_lock);
1654 list_del(&lu_gp_mem->lu_gp_mem_list);
1655 lu_gp_mem->lu_gp = NULL;
1656 lu_gp_mem->lu_gp_assoc = 0;
1657 lu_gp->lu_gp_members--;
1658 spin_unlock(&lu_gp->lu_gp_lock);
1659}
1660
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001661struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1662 const char *name, int def_group)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001663{
1664 struct t10_alua_tg_pt_gp *tg_pt_gp;
1665
1666 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001667 if (!tg_pt_gp) {
1668 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001669 return NULL;
1670 }
1671 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001672 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001673 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1674 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1675 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001676 INIT_DELAYED_WORK(&tg_pt_gp->tg_pt_gp_transition_work,
1677 core_alua_do_transition_tg_pt_work);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001678 tg_pt_gp->tg_pt_gp_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001679 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
Hannes Reinecke73f3bf52013-11-19 09:07:48 +01001680 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001681 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001682 * Enable both explicit and implicit ALUA support by default
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683 */
1684 tg_pt_gp->tg_pt_gp_alua_access_type =
Hannes Reinecke125d0112013-11-19 09:07:46 +01001685 TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001686 /*
1687 * Set the default Active/NonOptimized Delay in milliseconds
1688 */
1689 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1690 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
Hannes Reinecke125d0112013-11-19 09:07:46 +01001691 tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001692
Hannes Reineckec0dc9412013-11-19 09:07:49 +01001693 /*
1694 * Enable all supported states
1695 */
1696 tg_pt_gp->tg_pt_gp_alua_supported_states =
1697 ALUA_T_SUP | ALUA_O_SUP |
1698 ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1699
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001700 if (def_group) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001701 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001702 tg_pt_gp->tg_pt_gp_id =
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001703 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001704 tg_pt_gp->tg_pt_gp_valid_id = 1;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001705 dev->t10_alua.alua_tg_pt_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001707 &dev->t10_alua.tg_pt_gps_list);
1708 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001709 }
1710
1711 return tg_pt_gp;
1712}
1713
1714int core_alua_set_tg_pt_gp_id(
1715 struct t10_alua_tg_pt_gp *tg_pt_gp,
1716 u16 tg_pt_gp_id)
1717{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001718 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001719 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1720 u16 tg_pt_gp_id_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001721
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001722 /*
1723 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1724 */
1725 if (tg_pt_gp->tg_pt_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001726 pr_warn("ALUA TG PT Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001727 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001728 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729 }
1730
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001731 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1732 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001733 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001734 " 0x0000ffff reached\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001735 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001736 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001737 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001738 }
1739again:
1740 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001741 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001742
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001743 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001744 tg_pt_gp_list) {
1745 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001746 if (!tg_pt_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001747 goto again;
1748
Andy Grover6708bb22011-06-08 10:36:43 -07001749 pr_err("ALUA Target Port Group ID: %hu already"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001750 " exists, ignoring request\n", tg_pt_gp_id);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001751 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001752 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001753 }
1754 }
1755
1756 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1757 tg_pt_gp->tg_pt_gp_valid_id = 1;
1758 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001759 &dev->t10_alua.tg_pt_gps_list);
1760 dev->t10_alua.alua_tg_pt_gps_count++;
1761 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001762
1763 return 0;
1764}
1765
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001766void core_alua_free_tg_pt_gp(
1767 struct t10_alua_tg_pt_gp *tg_pt_gp)
1768{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001769 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001770 struct se_lun *lun, *next;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001771
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772 /*
1773 * Once we have reached this point, config_item_put() has already
1774 * been called from target_core_alua_drop_tg_pt_gp().
1775 *
1776 * Here we remove *tg_pt_gp from the global list so that
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001777 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001778 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1779 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001780 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001781 list_del(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001782 dev->t10_alua.alua_tg_pt_gps_counter--;
1783 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1784
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001785 flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
1786
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001787 /*
1788 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1789 * core_alua_get_tg_pt_gp_by_name() in
1790 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1791 * to be released with core_alua_put_tg_pt_gp_from_name().
1792 */
1793 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1794 cpu_relax();
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001795
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001796 /*
1797 * Release reference to struct t10_alua_tg_pt_gp from all associated
1798 * struct se_port.
1799 */
1800 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001801 list_for_each_entry_safe(lun, next,
1802 &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1803 list_del_init(&lun->lun_tg_pt_gp_link);
1804 tg_pt_gp->tg_pt_gp_members--;
1805
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1807 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001808 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001809 * assume we want to re-associate a given tg_pt_gp_mem with
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001810 * default_tg_pt_gp.
1811 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001812 spin_lock(&lun->lun_tg_pt_gp_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001813 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001814 __target_attach_tg_pt_gp(lun,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001815 dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001816 } else
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001817 lun->lun_tg_pt_gp = NULL;
1818 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001819
1820 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1821 }
1822 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1823
1824 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1825}
1826
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001827static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001828 struct se_device *dev, const char *name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001829{
1830 struct t10_alua_tg_pt_gp *tg_pt_gp;
1831 struct config_item *ci;
1832
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001833 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1834 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001835 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001836 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001837 continue;
1838 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001839 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001840 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001841 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001842 return tg_pt_gp;
1843 }
1844 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001845 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001846
1847 return NULL;
1848}
1849
1850static void core_alua_put_tg_pt_gp_from_name(
1851 struct t10_alua_tg_pt_gp *tg_pt_gp)
1852{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001853 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001855 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001856 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001857 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001858}
1859
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001860static void __target_attach_tg_pt_gp(struct se_lun *lun,
1861 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001862{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001863 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1864
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001865 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001866 lun->lun_tg_pt_gp = tg_pt_gp;
1867 list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001868 tg_pt_gp->tg_pt_gp_members++;
1869 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1870}
1871
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001872void target_attach_tg_pt_gp(struct se_lun *lun,
1873 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001874{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001875 spin_lock(&lun->lun_tg_pt_gp_lock);
1876 __target_attach_tg_pt_gp(lun, tg_pt_gp);
1877 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001878}
1879
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001880static void __target_detach_tg_pt_gp(struct se_lun *lun,
1881 struct t10_alua_tg_pt_gp *tg_pt_gp)
1882{
1883 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1884
1885 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1886 list_del_init(&lun->lun_tg_pt_gp_link);
1887 tg_pt_gp->tg_pt_gp_members--;
1888 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1889
1890 lun->lun_tg_pt_gp = NULL;
1891}
1892
1893void target_detach_tg_pt_gp(struct se_lun *lun)
1894{
1895 struct t10_alua_tg_pt_gp *tg_pt_gp;
1896
1897 spin_lock(&lun->lun_tg_pt_gp_lock);
1898 tg_pt_gp = lun->lun_tg_pt_gp;
1899 if (tg_pt_gp)
1900 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1901 spin_unlock(&lun->lun_tg_pt_gp_lock);
1902}
1903
1904ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001905{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001906 struct config_item *tg_pt_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001907 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001908 ssize_t len = 0;
1909
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001910 spin_lock(&lun->lun_tg_pt_gp_lock);
1911 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001912 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001913 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1914 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1915 " %hu\nTG Port Primary Access State: %s\nTG Port "
1916 "Primary Access Status: %s\nTG Port Secondary Access"
1917 " State: %s\nTG Port Secondary Access Status: %s\n",
1918 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1919 core_alua_dump_state(atomic_read(
1920 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1921 core_alua_dump_status(
1922 tg_pt_gp->tg_pt_gp_alua_access_status),
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001923 atomic_read(&lun->lun_tg_pt_secondary_offline) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001924 "Offline" : "None",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001925 core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001926 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001927 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001928
1929 return len;
1930}
1931
1932ssize_t core_alua_store_tg_pt_gp_info(
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001933 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001934 const char *page,
1935 size_t count)
1936{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001937 struct se_portal_group *tpg = lun->lun_tpg;
1938 struct se_device *dev = lun->lun_se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001939 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001940 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1941 int move = 0;
1942
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001943 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
1944 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1945 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946
1947 if (count > TG_PT_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001948 pr_err("ALUA Target Port Group alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001949 return -EINVAL;
1950 }
1951 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1952 memcpy(buf, page, count);
1953 /*
1954 * Any ALUA target port group alias besides "NULL" means we will be
1955 * making a new group association.
1956 */
1957 if (strcmp(strstrip(buf), "NULL")) {
1958 /*
1959 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1960 * struct t10_alua_tg_pt_gp. This reference is released with
1961 * core_alua_put_tg_pt_gp_from_name() below.
1962 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001963 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001964 strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001965 if (!tg_pt_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001966 return -ENODEV;
1967 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001968
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001969 spin_lock(&lun->lun_tg_pt_gp_lock);
1970 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001971 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001972 /*
1973 * Clearing an existing tg_pt_gp association, and replacing
1974 * with the default_tg_pt_gp.
1975 */
Andy Grover6708bb22011-06-08 10:36:43 -07001976 if (!tg_pt_gp_new) {
1977 pr_debug("Target_Core_ConfigFS: Moving"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001978 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1979 " alua/%s, ID: %hu back to"
1980 " default_tg_pt_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001981 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1982 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001983 config_item_name(&lun->lun_group.cg_item),
1984 config_item_name(
1985 &tg_pt_gp->tg_pt_gp_group.cg_item),
1986 tg_pt_gp->tg_pt_gp_id);
1987
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001988 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1989 __target_attach_tg_pt_gp(lun,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001990 dev->t10_alua.default_tg_pt_gp);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001991 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001992
1993 return count;
1994 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001995 __target_detach_tg_pt_gp(lun, tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001996 move = 1;
1997 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001998
1999 __target_attach_tg_pt_gp(lun, tg_pt_gp_new);
2000 spin_unlock(&lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002001 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002003 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2004 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002005 config_item_name(&lun->lun_group.cg_item),
2006 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
2007 tg_pt_gp_new->tg_pt_gp_id);
2008
2009 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
2010 return count;
2011}
2012
2013ssize_t core_alua_show_access_type(
2014 struct t10_alua_tg_pt_gp *tg_pt_gp,
2015 char *page)
2016{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002017 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
2018 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
2019 return sprintf(page, "Implicit and Explicit\n");
2020 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
2021 return sprintf(page, "Implicit\n");
2022 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
2023 return sprintf(page, "Explicit\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002024 else
2025 return sprintf(page, "None\n");
2026}
2027
2028ssize_t core_alua_store_access_type(
2029 struct t10_alua_tg_pt_gp *tg_pt_gp,
2030 const char *page,
2031 size_t count)
2032{
2033 unsigned long tmp;
2034 int ret;
2035
Jingoo Han57103d72013-07-19 16:22:19 +09002036 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002037 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002038 pr_err("Unable to extract alua_access_type\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002039 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002040 }
2041 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002042 pr_err("Illegal value for alua_access_type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043 " %lu\n", tmp);
2044 return -EINVAL;
2045 }
2046 if (tmp == 3)
2047 tg_pt_gp->tg_pt_gp_alua_access_type =
Hannes Reinecke125d0112013-11-19 09:07:46 +01002048 TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002049 else if (tmp == 2)
Hannes Reinecke125d0112013-11-19 09:07:46 +01002050 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002051 else if (tmp == 1)
Hannes Reinecke125d0112013-11-19 09:07:46 +01002052 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002053 else
2054 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2055
2056 return count;
2057}
2058
2059ssize_t core_alua_show_nonop_delay_msecs(
2060 struct t10_alua_tg_pt_gp *tg_pt_gp,
2061 char *page)
2062{
2063 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2064}
2065
2066ssize_t core_alua_store_nonop_delay_msecs(
2067 struct t10_alua_tg_pt_gp *tg_pt_gp,
2068 const char *page,
2069 size_t count)
2070{
2071 unsigned long tmp;
2072 int ret;
2073
Jingoo Han57103d72013-07-19 16:22:19 +09002074 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002075 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002076 pr_err("Unable to extract nonop_delay_msecs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002077 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002078 }
2079 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07002080 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002081 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2082 ALUA_MAX_NONOP_DELAY_MSECS);
2083 return -EINVAL;
2084 }
2085 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2086
2087 return count;
2088}
2089
2090ssize_t core_alua_show_trans_delay_msecs(
2091 struct t10_alua_tg_pt_gp *tg_pt_gp,
2092 char *page)
2093{
2094 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2095}
2096
2097ssize_t core_alua_store_trans_delay_msecs(
2098 struct t10_alua_tg_pt_gp *tg_pt_gp,
2099 const char *page,
2100 size_t count)
2101{
2102 unsigned long tmp;
2103 int ret;
2104
Jingoo Han57103d72013-07-19 16:22:19 +09002105 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002106 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002107 pr_err("Unable to extract trans_delay_msecs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002108 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002109 }
2110 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07002111 pr_err("Passed trans_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002112 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2113 ALUA_MAX_TRANS_DELAY_MSECS);
2114 return -EINVAL;
2115 }
2116 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2117
2118 return count;
2119}
2120
Hannes Reinecke125d0112013-11-19 09:07:46 +01002121ssize_t core_alua_show_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002122 struct t10_alua_tg_pt_gp *tg_pt_gp,
2123 char *page)
2124{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002125 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002126}
2127
Hannes Reinecke125d0112013-11-19 09:07:46 +01002128ssize_t core_alua_store_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002129 struct t10_alua_tg_pt_gp *tg_pt_gp,
2130 const char *page,
2131 size_t count)
2132{
2133 unsigned long tmp;
2134 int ret;
2135
Jingoo Han57103d72013-07-19 16:22:19 +09002136 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002137 if (ret < 0) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002138 pr_err("Unable to extract implicit_trans_secs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002139 return ret;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002140 }
Hannes Reinecke125d0112013-11-19 09:07:46 +01002141 if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2142 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2143 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2144 ALUA_MAX_IMPLICIT_TRANS_SECS);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002145 return -EINVAL;
2146 }
Hannes Reinecke125d0112013-11-19 09:07:46 +01002147 tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002148
2149 return count;
2150}
2151
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002152ssize_t core_alua_show_preferred_bit(
2153 struct t10_alua_tg_pt_gp *tg_pt_gp,
2154 char *page)
2155{
2156 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2157}
2158
2159ssize_t core_alua_store_preferred_bit(
2160 struct t10_alua_tg_pt_gp *tg_pt_gp,
2161 const char *page,
2162 size_t count)
2163{
2164 unsigned long tmp;
2165 int ret;
2166
Jingoo Han57103d72013-07-19 16:22:19 +09002167 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002168 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002169 pr_err("Unable to extract preferred ALUA value\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002170 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002171 }
2172 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002173 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002174 return -EINVAL;
2175 }
2176 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2177
2178 return count;
2179}
2180
2181ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2182{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002183 return sprintf(page, "%d\n",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002184 atomic_read(&lun->lun_tg_pt_secondary_offline));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002185}
2186
2187ssize_t core_alua_store_offline_bit(
2188 struct se_lun *lun,
2189 const char *page,
2190 size_t count)
2191{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002192 struct se_device *dev = lun->lun_se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002193 unsigned long tmp;
2194 int ret;
2195
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002196 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
2197 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002198 return -ENODEV;
2199
Jingoo Han57103d72013-07-19 16:22:19 +09002200 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002201 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002202 pr_err("Unable to extract alua_tg_pt_offline value\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002203 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002204 }
2205 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002206 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207 tmp);
2208 return -EINVAL;
2209 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002210
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002211 ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002212 if (ret < 0)
2213 return -EINVAL;
2214
2215 return count;
2216}
2217
2218ssize_t core_alua_show_secondary_status(
2219 struct se_lun *lun,
2220 char *page)
2221{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002222 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002223}
2224
2225ssize_t core_alua_store_secondary_status(
2226 struct se_lun *lun,
2227 const char *page,
2228 size_t count)
2229{
2230 unsigned long tmp;
2231 int ret;
2232
Jingoo Han57103d72013-07-19 16:22:19 +09002233 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002234 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002235 pr_err("Unable to extract alua_tg_pt_status\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002236 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002237 }
2238 if ((tmp != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002239 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2240 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002241 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002242 tmp);
2243 return -EINVAL;
2244 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002245 lun->lun_tg_pt_secondary_stat = (int)tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002246
2247 return count;
2248}
2249
2250ssize_t core_alua_show_secondary_write_metadata(
2251 struct se_lun *lun,
2252 char *page)
2253{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002254 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255}
2256
2257ssize_t core_alua_store_secondary_write_metadata(
2258 struct se_lun *lun,
2259 const char *page,
2260 size_t count)
2261{
2262 unsigned long tmp;
2263 int ret;
2264
Jingoo Han57103d72013-07-19 16:22:19 +09002265 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002266 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002267 pr_err("Unable to extract alua_tg_pt_write_md\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002268 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002269 }
2270 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002271 pr_err("Illegal value for alua_tg_pt_write_md:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002272 " %lu\n", tmp);
2273 return -EINVAL;
2274 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002275 lun->lun_tg_pt_secondary_write_md = (int)tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002276
2277 return count;
2278}
2279
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002280int core_setup_alua(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002281{
Andy Grovera3541702015-05-19 14:44:41 -07002282 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04002283 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2284 struct t10_alua_lu_gp_member *lu_gp_mem;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002285
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002286 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002287 * Associate this struct se_device with the default ALUA
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002288 * LUN Group.
2289 */
2290 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002291 if (IS_ERR(lu_gp_mem))
2292 return PTR_ERR(lu_gp_mem);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002293
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002294 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2295 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00002296 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002297 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2298
Andy Grover6708bb22011-06-08 10:36:43 -07002299 pr_debug("%s: Adding to default ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300 " core/alua/lu_gps/default_lu_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002301 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302 }
2303
2304 return 0;
2305}