blob: c56ae024c42ece0096896f0fc6e584dc4af231b3 [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 Reineckeee8c07d2015-06-11 10:01:24 +0200944static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800945{
946 struct se_dev_entry *se_deve;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700947 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800948 struct se_lun_acl *lacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800949
950 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700951 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
952 lun_tg_pt_gp_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800953 /*
954 * After an implicit target port asymmetric access state
955 * change, a device server shall establish a unit attention
956 * condition for the initiator port associated with every I_T
957 * nexus with the additional sense code set to ASYMMETRIC
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100958 * ACCESS STATE CHANGED.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800959 *
960 * After an explicit target port asymmetric access state
961 * change, a device server shall establish a unit attention
962 * condition with the additional sense code set to ASYMMETRIC
963 * ACCESS STATE CHANGED for the initiator port associated with
964 * every I_T nexus other than the I_T nexus on which the SET
965 * TARGET PORT GROUPS command
966 */
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700967 if (!percpu_ref_tryget_live(&lun->lun_ref))
968 continue;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800969 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
970
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700971 spin_lock(&lun->lun_deve_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700972 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700973 lacl = rcu_dereference_check(se_deve->se_lun_acl,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700974 lockdep_is_held(&lun->lun_deve_lock));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800975
Hannes Reineckec51c8e72015-06-11 10:01:26 +0200976 /*
977 * spc4r37 p.242:
978 * After an explicit target port asymmetric access
979 * state change, a device server shall establish a
980 * unit attention condition with the additional sense
981 * code set to ASYMMETRIC ACCESS STATE CHANGED for
982 * the initiator port associated with every I_T nexus
983 * other than the I_T nexus on which the SET TARGET
984 * PORT GROUPS command was received.
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) &&
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700988 (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
989 (tg_pt_gp->tg_pt_gp_alua_lun == lun))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800990 continue;
991
Hannes Reineckec51c8e72015-06-11 10:01:26 +0200992 /*
993 * se_deve->se_lun_acl pointer may be NULL for a
994 * entry created without explicit Node+MappedLUN ACLs
995 */
996 if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
997 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl))
998 continue;
999
1000 core_scsi3_ua_allocate(se_deve, 0x2A,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001001 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
1002 }
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -07001003 spin_unlock(&lun->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001004
1005 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Nicholas Bellinger9e37d042015-05-20 21:21:08 -07001006 percpu_ref_put(&lun->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001007 }
1008 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
Hannes Reineckeee8c07d2015-06-11 10:01:24 +02001009}
1010
1011static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
1012{
1013 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work,
1014 struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work.work);
1015 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1016 bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
1017 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
1018
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001019 /*
1020 * Update the ALUA metadata buf that has been allocated in
1021 * core_alua_do_port_transition(), this metadata will be written
1022 * to struct file.
1023 *
1024 * Note that there is the case where we do not want to update the
1025 * metadata when the saved metadata is being parsed in userspace
1026 * when setting the existing port access state and access status.
1027 *
1028 * Also note that the failure to write out the ALUA metadata to
1029 * struct file does NOT affect the actual ALUA transition.
1030 */
1031 if (tg_pt_gp->tg_pt_gp_write_metadata) {
1032 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001033 core_alua_update_tpg_primary_metadata(tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001034 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
1035 }
1036 /*
1037 * Set the current primary ALUA access state to the requested new state
1038 */
Hannes Reineckedfbce752013-12-17 09:18:45 +01001039 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1040 tg_pt_gp->tg_pt_gp_alua_pending_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041
Andy Grover6708bb22011-06-08 10:36:43 -07001042 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Hannes Reinecke125d0112013-11-19 09:07:46 +01001043 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1044 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
Hannes Reineckedfbce752013-12-17 09:18:45 +01001045 tg_pt_gp->tg_pt_gp_id,
1046 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state),
1047 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
Hannes Reineckeee8c07d2015-06-11 10:01:24 +02001048
1049 core_alua_queue_state_change_ua(tg_pt_gp);
1050
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001051 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1052 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001053 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1054
1055 if (tg_pt_gp->tg_pt_gp_transition_complete)
1056 complete(tg_pt_gp->tg_pt_gp_transition_complete);
1057}
1058
1059static int core_alua_do_transition_tg_pt(
1060 struct t10_alua_tg_pt_gp *tg_pt_gp,
1061 int new_state,
1062 int explicit)
1063{
1064 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1065 DECLARE_COMPLETION_ONSTACK(wait);
1066
1067 /* Nothing to be done here */
1068 if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state)
1069 return 0;
1070
1071 if (new_state == ALUA_ACCESS_STATE_TRANSITION)
1072 return -EAGAIN;
1073
1074 /*
1075 * Flush any pending transitions
1076 */
1077 if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs &&
1078 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) ==
1079 ALUA_ACCESS_STATE_TRANSITION) {
1080 /* Just in case */
1081 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1082 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1083 flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
1084 wait_for_completion(&wait);
1085 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1086 return 0;
1087 }
1088
1089 /*
1090 * Save the old primary ALUA access state, and set the current state
1091 * to ALUA_ACCESS_STATE_TRANSITION.
1092 */
1093 tg_pt_gp->tg_pt_gp_alua_previous_state =
1094 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
1095 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1096
1097 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1098 ALUA_ACCESS_STATE_TRANSITION);
1099 tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1100 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1101 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1102
Hannes Reineckeee8c07d2015-06-11 10:01:24 +02001103 core_alua_queue_state_change_ua(tg_pt_gp);
1104
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001105 /*
1106 * Check for the optional ALUA primary state transition delay
1107 */
1108 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
1109 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1110
1111 /*
1112 * Take a reference for workqueue item
1113 */
1114 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1115 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001116 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1117
1118 if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs) {
1119 unsigned long transition_tmo;
1120
1121 transition_tmo = tg_pt_gp->tg_pt_gp_implicit_trans_secs * HZ;
1122 queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
1123 &tg_pt_gp->tg_pt_gp_transition_work,
1124 transition_tmo);
1125 } else {
1126 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1127 queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
1128 &tg_pt_gp->tg_pt_gp_transition_work, 0);
1129 wait_for_completion(&wait);
1130 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1131 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001132
1133 return 0;
1134}
1135
1136int core_alua_do_port_transition(
1137 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1138 struct se_device *l_dev,
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001139 struct se_lun *l_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001140 struct se_node_acl *l_nacl,
1141 int new_state,
Hannes Reinecke125d0112013-11-19 09:07:46 +01001142 int explicit)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001143{
1144 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001145 struct t10_alua_lu_gp *lu_gp;
1146 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1147 struct t10_alua_tg_pt_gp *tg_pt_gp;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001148 int primary, valid_states, rc = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001149
Hannes Reineckebb91c1a2013-12-17 09:18:43 +01001150 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1151 if (core_alua_check_transition(new_state, valid_states, &primary) != 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001152 return -EINVAL;
1153
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001154 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1155 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1156 lu_gp = local_lu_gp_mem->lu_gp;
1157 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001158 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1159 /*
1160 * For storage objects that are members of the 'default_lu_gp',
1161 * we only do transition on the passed *l_tp_pt_gp, and not
1162 * on all of the matching target port groups IDs in default_lu_gp.
1163 */
Andy Grover6708bb22011-06-08 10:36:43 -07001164 if (!lu_gp->lu_gp_id) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001165 /*
1166 * core_alua_do_transition_tg_pt() will always return
1167 * success.
1168 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001169 l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001170 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1171 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1172 new_state, explicit);
Joern Engel33940d02014-09-16 16:23:12 -04001173 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001174 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001175 }
1176 /*
1177 * For all other LU groups aside from 'default_lu_gp', walk all of
1178 * the associated storage objects looking for a matching target port
1179 * group ID from the local target port group.
1180 */
1181 spin_lock(&lu_gp->lu_gp_lock);
1182 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1183 lu_gp_mem_list) {
1184
1185 dev = lu_gp_mem->lu_gp_mem_dev;
Joern Engel33940d02014-09-16 16:23:12 -04001186 atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001187 spin_unlock(&lu_gp->lu_gp_lock);
1188
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001189 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001190 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001191 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001192 tg_pt_gp_list) {
1193
Andy Grover6708bb22011-06-08 10:36:43 -07001194 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001195 continue;
1196 /*
1197 * If the target behavior port asymmetric access state
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001198 * is changed for any target port group accessible via
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001199 * a logical unit within a LU group, the target port
1200 * behavior group asymmetric access states for the same
1201 * target port group accessible via other logical units
1202 * in that LU group will also change.
1203 */
1204 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1205 continue;
1206
1207 if (l_tg_pt_gp == tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001208 tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001209 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210 } else {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001211 tg_pt_gp->tg_pt_gp_alua_lun = NULL;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001212 tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213 }
Joern Engel33940d02014-09-16 16:23:12 -04001214 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001215 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001216 /*
1217 * core_alua_do_transition_tg_pt() will always return
1218 * success.
1219 */
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001220 rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1221 new_state, explicit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001222
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001223 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Joern Engel33940d02014-09-16 16:23:12 -04001224 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001225 if (rc)
1226 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001228 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001229
1230 spin_lock(&lu_gp->lu_gp_lock);
Joern Engel33940d02014-09-16 16:23:12 -04001231 atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001232 }
1233 spin_unlock(&lu_gp->lu_gp_lock);
1234
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001235 if (!rc) {
1236 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1237 " Group IDs: %hu %s transition to primary state: %s\n",
1238 config_item_name(&lu_gp->lu_gp_group.cg_item),
1239 l_tg_pt_gp->tg_pt_gp_id,
1240 (explicit) ? "explicit" : "implicit",
1241 core_alua_dump_state(new_state));
1242 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001243
Joern Engel33940d02014-09-16 16:23:12 -04001244 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001245 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001246}
1247
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001248static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001249{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001250 struct se_portal_group *se_tpg = lun->lun_tpg;
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001251 unsigned char *md_buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001252 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001253 int len, rc;
1254
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001255 mutex_lock(&lun->lun_tg_pt_md_mutex);
1256
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001257 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1258 if (!md_buf) {
1259 pr_err("Unable to allocate buf for ALUA metadata\n");
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001260 rc = -ENOMEM;
1261 goto out_unlock;
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001262 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001263
1264 memset(path, 0, ALUA_METADATA_PATH_LEN);
1265 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1266
1267 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +00001268 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001269
Andy Grovere3d6f902011-07-19 08:55:10 +00001270 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001271 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
Andy Grovere3d6f902011-07-19 08:55:10 +00001272 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001273
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001274 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001275 "alua_tg_pt_status=0x%02x\n",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001276 atomic_read(&lun->lun_tg_pt_secondary_offline),
1277 lun->lun_tg_pt_secondary_stat);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278
Hannes Reineckef2d30682015-06-10 08:41:22 +02001279 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%llu",
Andy Grovere3d6f902011-07-19 08:55:10 +00001280 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001281 lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001282
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001283 rc = core_alua_write_tpg_metadata(path, md_buf, len);
1284 kfree(md_buf);
1285
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001286out_unlock:
1287 mutex_unlock(&lun->lun_tg_pt_md_mutex);
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001288 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001289}
1290
1291static int core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001292 struct se_lun *lun,
Hannes Reinecke125d0112013-11-19 09:07:46 +01001293 int explicit,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001294 int offline)
1295{
1296 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001297 int trans_delay_msecs;
1298
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001299 spin_lock(&lun->lun_tg_pt_gp_lock);
1300 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001301 if (!tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001302 spin_unlock(&lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001303 pr_err("Unable to complete secondary state"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001304 " transition\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001305 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001306 }
1307 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1308 /*
1309 * Set the secondary ALUA target port access state to OFFLINE
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001310 * or release the previously secondary state for struct se_lun
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001311 */
1312 if (offline)
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001313 atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314 else
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001315 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001316
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001317 lun->lun_tg_pt_secondary_stat = (explicit) ?
Hannes Reinecke125d0112013-11-19 09:07:46 +01001318 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1319 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001320
Andy Grover6708bb22011-06-08 10:36:43 -07001321 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Hannes Reinecke125d0112013-11-19 09:07:46 +01001322 " to secondary access state: %s\n", (explicit) ? "explicit" :
1323 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001324 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1325
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001326 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327 /*
1328 * Do the optional transition delay after we set the secondary
1329 * ALUA access state.
1330 */
1331 if (trans_delay_msecs != 0)
1332 msleep_interruptible(trans_delay_msecs);
1333 /*
1334 * See if we need to update the ALUA fabric port metadata for
1335 * secondary state and status
1336 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001337 if (lun->lun_tg_pt_secondary_write_md)
1338 core_alua_update_tpg_secondary_metadata(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001339
1340 return 0;
1341}
1342
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001343struct t10_alua_lba_map *
1344core_alua_allocate_lba_map(struct list_head *list,
1345 u64 first_lba, u64 last_lba)
1346{
1347 struct t10_alua_lba_map *lba_map;
1348
1349 lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1350 if (!lba_map) {
1351 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1352 return ERR_PTR(-ENOMEM);
1353 }
1354 INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1355 lba_map->lba_map_first_lba = first_lba;
1356 lba_map->lba_map_last_lba = last_lba;
1357
1358 list_add_tail(&lba_map->lba_map_list, list);
1359 return lba_map;
1360}
1361
1362int
1363core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1364 int pg_id, int state)
1365{
1366 struct t10_alua_lba_map_member *lba_map_mem;
1367
1368 list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1369 lba_map_mem_list) {
1370 if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1371 pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1372 return -EINVAL;
1373 }
1374 }
1375
1376 lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1377 if (!lba_map_mem) {
1378 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1379 return -ENOMEM;
1380 }
1381 lba_map_mem->lba_map_mem_alua_state = state;
1382 lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1383
1384 list_add_tail(&lba_map_mem->lba_map_mem_list,
1385 &lba_map->lba_map_mem_list);
1386 return 0;
1387}
1388
1389void
1390core_alua_free_lba_map(struct list_head *lba_list)
1391{
1392 struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1393 struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1394
1395 list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1396 lba_map_list) {
1397 list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1398 &lba_map->lba_map_mem_list,
1399 lba_map_mem_list) {
1400 list_del(&lba_map_mem->lba_map_mem_list);
1401 kmem_cache_free(t10_alua_lba_map_mem_cache,
1402 lba_map_mem);
1403 }
1404 list_del(&lba_map->lba_map_list);
1405 kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1406 }
1407}
1408
1409void
1410core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1411 int segment_size, int segment_mult)
1412{
1413 struct list_head old_lba_map_list;
1414 struct t10_alua_tg_pt_gp *tg_pt_gp;
1415 int activate = 0, supported;
1416
1417 INIT_LIST_HEAD(&old_lba_map_list);
1418 spin_lock(&dev->t10_alua.lba_map_lock);
1419 dev->t10_alua.lba_map_segment_size = segment_size;
1420 dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1421 list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1422 if (lba_map_list) {
1423 list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1424 activate = 1;
1425 }
1426 spin_unlock(&dev->t10_alua.lba_map_lock);
1427 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1428 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1429 tg_pt_gp_list) {
1430
1431 if (!tg_pt_gp->tg_pt_gp_valid_id)
1432 continue;
1433 supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1434 if (activate)
1435 supported |= ALUA_LBD_SUP;
1436 else
1437 supported &= ~ALUA_LBD_SUP;
1438 tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1439 }
1440 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1441 core_alua_free_lba_map(&old_lba_map_list);
1442}
1443
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001444struct t10_alua_lu_gp *
1445core_alua_allocate_lu_gp(const char *name, int def_group)
1446{
1447 struct t10_alua_lu_gp *lu_gp;
1448
1449 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001450 if (!lu_gp) {
1451 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001452 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001453 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001454 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001455 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1456 spin_lock_init(&lu_gp->lu_gp_lock);
1457 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1458
1459 if (def_group) {
Andy Grovere3d6f902011-07-19 08:55:10 +00001460 lu_gp->lu_gp_id = alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001461 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001462 alua_lu_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001463 }
1464
1465 return lu_gp;
1466}
1467
1468int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1469{
1470 struct t10_alua_lu_gp *lu_gp_tmp;
1471 u16 lu_gp_id_tmp;
1472 /*
1473 * The lu_gp->lu_gp_id may only be set once..
1474 */
1475 if (lu_gp->lu_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001476 pr_warn("ALUA LU Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001478 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001479 }
1480
Andy Grovere3d6f902011-07-19 08:55:10 +00001481 spin_lock(&lu_gps_lock);
1482 if (alua_lu_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001483 pr_err("Maximum ALUA alua_lu_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001484 " 0x0000ffff reached\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001485 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001486 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001487 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001488 }
1489again:
1490 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
Andy Grovere3d6f902011-07-19 08:55:10 +00001491 alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492
Andy Grovere3d6f902011-07-19 08:55:10 +00001493 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001494 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001495 if (!lu_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496 goto again;
1497
Andy Grover6708bb22011-06-08 10:36:43 -07001498 pr_warn("ALUA Logical Unit Group ID: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001499 " already exists, ignoring request\n",
1500 lu_gp_id);
Andy Grovere3d6f902011-07-19 08:55:10 +00001501 spin_unlock(&lu_gps_lock);
1502 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001503 }
1504 }
1505
1506 lu_gp->lu_gp_id = lu_gp_id_tmp;
1507 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001508 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1509 alua_lu_gps_count++;
1510 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001511
1512 return 0;
1513}
1514
1515static struct t10_alua_lu_gp_member *
1516core_alua_allocate_lu_gp_mem(struct se_device *dev)
1517{
1518 struct t10_alua_lu_gp_member *lu_gp_mem;
1519
1520 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001521 if (!lu_gp_mem) {
1522 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001523 return ERR_PTR(-ENOMEM);
1524 }
1525 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1526 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1527 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1528
1529 lu_gp_mem->lu_gp_mem_dev = dev;
1530 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1531
1532 return lu_gp_mem;
1533}
1534
1535void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1536{
1537 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1538 /*
1539 * Once we have reached this point, config_item_put() has
1540 * already been called from target_core_alua_drop_lu_gp().
1541 *
1542 * Here, we remove the *lu_gp from the global list so that
1543 * no associations can be made while we are releasing
1544 * struct t10_alua_lu_gp.
1545 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001546 spin_lock(&lu_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001547 list_del(&lu_gp->lu_gp_node);
1548 alua_lu_gps_count--;
1549 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001550 /*
1551 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1552 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1553 * released with core_alua_put_lu_gp_from_name()
1554 */
1555 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1556 cpu_relax();
1557 /*
1558 * Release reference to struct t10_alua_lu_gp * from all associated
1559 * struct se_device.
1560 */
1561 spin_lock(&lu_gp->lu_gp_lock);
1562 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1563 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1564 if (lu_gp_mem->lu_gp_assoc) {
1565 list_del(&lu_gp_mem->lu_gp_mem_list);
1566 lu_gp->lu_gp_members--;
1567 lu_gp_mem->lu_gp_assoc = 0;
1568 }
1569 spin_unlock(&lu_gp->lu_gp_lock);
1570 /*
1571 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001572 * lu_gp_mem is associated with a single
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001573 * struct se_device->dev_alua_lu_gp_mem, and is released when
1574 * struct se_device is released via core_alua_free_lu_gp_mem().
1575 *
1576 * If the passed lu_gp does NOT match the default_lu_gp, assume
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001577 * we want to re-associate a given lu_gp_mem with default_lu_gp.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001578 */
1579 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001580 if (lu_gp != default_lu_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001581 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00001582 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001583 else
1584 lu_gp_mem->lu_gp = NULL;
1585 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1586
1587 spin_lock(&lu_gp->lu_gp_lock);
1588 }
1589 spin_unlock(&lu_gp->lu_gp_lock);
1590
1591 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1592}
1593
1594void core_alua_free_lu_gp_mem(struct se_device *dev)
1595{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001596 struct t10_alua_lu_gp *lu_gp;
1597 struct t10_alua_lu_gp_member *lu_gp_mem;
1598
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001599 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -07001600 if (!lu_gp_mem)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001601 return;
1602
1603 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1604 cpu_relax();
1605
1606 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1607 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001608 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001609 spin_lock(&lu_gp->lu_gp_lock);
1610 if (lu_gp_mem->lu_gp_assoc) {
1611 list_del(&lu_gp_mem->lu_gp_mem_list);
1612 lu_gp->lu_gp_members--;
1613 lu_gp_mem->lu_gp_assoc = 0;
1614 }
1615 spin_unlock(&lu_gp->lu_gp_lock);
1616 lu_gp_mem->lu_gp = NULL;
1617 }
1618 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1619
1620 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1621}
1622
1623struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1624{
1625 struct t10_alua_lu_gp *lu_gp;
1626 struct config_item *ci;
1627
Andy Grovere3d6f902011-07-19 08:55:10 +00001628 spin_lock(&lu_gps_lock);
1629 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
Andy Grover6708bb22011-06-08 10:36:43 -07001630 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001631 continue;
1632 ci = &lu_gp->lu_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001633 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001634 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001635 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001636 return lu_gp;
1637 }
1638 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001639 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001640
1641 return NULL;
1642}
1643
1644void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1645{
Andy Grovere3d6f902011-07-19 08:55:10 +00001646 spin_lock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001647 atomic_dec(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001648 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001649}
1650
1651/*
1652 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1653 */
1654void __core_alua_attach_lu_gp_mem(
1655 struct t10_alua_lu_gp_member *lu_gp_mem,
1656 struct t10_alua_lu_gp *lu_gp)
1657{
1658 spin_lock(&lu_gp->lu_gp_lock);
1659 lu_gp_mem->lu_gp = lu_gp;
1660 lu_gp_mem->lu_gp_assoc = 1;
1661 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1662 lu_gp->lu_gp_members++;
1663 spin_unlock(&lu_gp->lu_gp_lock);
1664}
1665
1666/*
1667 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1668 */
1669void __core_alua_drop_lu_gp_mem(
1670 struct t10_alua_lu_gp_member *lu_gp_mem,
1671 struct t10_alua_lu_gp *lu_gp)
1672{
1673 spin_lock(&lu_gp->lu_gp_lock);
1674 list_del(&lu_gp_mem->lu_gp_mem_list);
1675 lu_gp_mem->lu_gp = NULL;
1676 lu_gp_mem->lu_gp_assoc = 0;
1677 lu_gp->lu_gp_members--;
1678 spin_unlock(&lu_gp->lu_gp_lock);
1679}
1680
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001681struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1682 const char *name, int def_group)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001683{
1684 struct t10_alua_tg_pt_gp *tg_pt_gp;
1685
1686 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001687 if (!tg_pt_gp) {
1688 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001689 return NULL;
1690 }
1691 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001692 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001693 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1694 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1695 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001696 INIT_DELAYED_WORK(&tg_pt_gp->tg_pt_gp_transition_work,
1697 core_alua_do_transition_tg_pt_work);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001698 tg_pt_gp->tg_pt_gp_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001699 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
Hannes Reinecke73f3bf52013-11-19 09:07:48 +01001700 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001702 * Enable both explicit and implicit ALUA support by default
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001703 */
1704 tg_pt_gp->tg_pt_gp_alua_access_type =
Hannes Reinecke125d0112013-11-19 09:07:46 +01001705 TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001706 /*
1707 * Set the default Active/NonOptimized Delay in milliseconds
1708 */
1709 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1710 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
Hannes Reinecke125d0112013-11-19 09:07:46 +01001711 tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001712
Hannes Reineckec0dc9412013-11-19 09:07:49 +01001713 /*
1714 * Enable all supported states
1715 */
1716 tg_pt_gp->tg_pt_gp_alua_supported_states =
1717 ALUA_T_SUP | ALUA_O_SUP |
1718 ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1719
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001720 if (def_group) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001721 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001722 tg_pt_gp->tg_pt_gp_id =
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001723 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001724 tg_pt_gp->tg_pt_gp_valid_id = 1;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001725 dev->t10_alua.alua_tg_pt_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001726 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001727 &dev->t10_alua.tg_pt_gps_list);
1728 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001729 }
1730
1731 return tg_pt_gp;
1732}
1733
1734int core_alua_set_tg_pt_gp_id(
1735 struct t10_alua_tg_pt_gp *tg_pt_gp,
1736 u16 tg_pt_gp_id)
1737{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001738 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1740 u16 tg_pt_gp_id_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001741
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001742 /*
1743 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1744 */
1745 if (tg_pt_gp->tg_pt_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001746 pr_warn("ALUA TG PT Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001747 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001748 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001749 }
1750
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001751 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1752 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001753 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001754 " 0x0000ffff reached\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001755 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001756 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001757 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001758 }
1759again:
1760 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001761 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001762
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001763 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001764 tg_pt_gp_list) {
1765 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001766 if (!tg_pt_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001767 goto again;
1768
Andy Grover6708bb22011-06-08 10:36:43 -07001769 pr_err("ALUA Target Port Group ID: %hu already"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001770 " exists, ignoring request\n", tg_pt_gp_id);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001771 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001772 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001773 }
1774 }
1775
1776 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1777 tg_pt_gp->tg_pt_gp_valid_id = 1;
1778 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001779 &dev->t10_alua.tg_pt_gps_list);
1780 dev->t10_alua.alua_tg_pt_gps_count++;
1781 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001782
1783 return 0;
1784}
1785
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001786void core_alua_free_tg_pt_gp(
1787 struct t10_alua_tg_pt_gp *tg_pt_gp)
1788{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001789 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001790 struct se_lun *lun, *next;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001791
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001792 /*
1793 * Once we have reached this point, config_item_put() has already
1794 * been called from target_core_alua_drop_tg_pt_gp().
1795 *
1796 * Here we remove *tg_pt_gp from the global list so that
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001797 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001798 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1799 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001800 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001801 list_del(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001802 dev->t10_alua.alua_tg_pt_gps_counter--;
1803 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1804
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001805 flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
1806
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001807 /*
1808 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1809 * core_alua_get_tg_pt_gp_by_name() in
1810 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1811 * to be released with core_alua_put_tg_pt_gp_from_name().
1812 */
1813 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1814 cpu_relax();
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001815
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001816 /*
1817 * Release reference to struct t10_alua_tg_pt_gp from all associated
1818 * struct se_port.
1819 */
1820 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001821 list_for_each_entry_safe(lun, next,
1822 &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1823 list_del_init(&lun->lun_tg_pt_gp_link);
1824 tg_pt_gp->tg_pt_gp_members--;
1825
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001826 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1827 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001828 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001829 * assume we want to re-associate a given tg_pt_gp_mem with
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001830 * default_tg_pt_gp.
1831 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001832 spin_lock(&lun->lun_tg_pt_gp_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001833 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001834 __target_attach_tg_pt_gp(lun,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001835 dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001836 } else
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001837 lun->lun_tg_pt_gp = NULL;
1838 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001839
1840 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1841 }
1842 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1843
1844 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1845}
1846
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001847static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001848 struct se_device *dev, const char *name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001849{
1850 struct t10_alua_tg_pt_gp *tg_pt_gp;
1851 struct config_item *ci;
1852
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001853 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1854 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001855 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001856 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857 continue;
1858 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001859 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001860 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001861 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001862 return tg_pt_gp;
1863 }
1864 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001865 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866
1867 return NULL;
1868}
1869
1870static void core_alua_put_tg_pt_gp_from_name(
1871 struct t10_alua_tg_pt_gp *tg_pt_gp)
1872{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001873 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001874
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001875 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001876 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001877 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001878}
1879
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001880static void __target_attach_tg_pt_gp(struct se_lun *lun,
1881 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001882{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001883 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1884
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001885 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001886 lun->lun_tg_pt_gp = tg_pt_gp;
1887 list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001888 tg_pt_gp->tg_pt_gp_members++;
1889 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1890}
1891
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001892void target_attach_tg_pt_gp(struct se_lun *lun,
1893 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001894{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001895 spin_lock(&lun->lun_tg_pt_gp_lock);
1896 __target_attach_tg_pt_gp(lun, tg_pt_gp);
1897 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001898}
1899
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001900static void __target_detach_tg_pt_gp(struct se_lun *lun,
1901 struct t10_alua_tg_pt_gp *tg_pt_gp)
1902{
1903 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1904
1905 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1906 list_del_init(&lun->lun_tg_pt_gp_link);
1907 tg_pt_gp->tg_pt_gp_members--;
1908 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1909
1910 lun->lun_tg_pt_gp = NULL;
1911}
1912
1913void target_detach_tg_pt_gp(struct se_lun *lun)
1914{
1915 struct t10_alua_tg_pt_gp *tg_pt_gp;
1916
1917 spin_lock(&lun->lun_tg_pt_gp_lock);
1918 tg_pt_gp = lun->lun_tg_pt_gp;
1919 if (tg_pt_gp)
1920 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1921 spin_unlock(&lun->lun_tg_pt_gp_lock);
1922}
1923
1924ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001925{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001926 struct config_item *tg_pt_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001928 ssize_t len = 0;
1929
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001930 spin_lock(&lun->lun_tg_pt_gp_lock);
1931 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001932 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1934 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1935 " %hu\nTG Port Primary Access State: %s\nTG Port "
1936 "Primary Access Status: %s\nTG Port Secondary Access"
1937 " State: %s\nTG Port Secondary Access Status: %s\n",
1938 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1939 core_alua_dump_state(atomic_read(
1940 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1941 core_alua_dump_status(
1942 tg_pt_gp->tg_pt_gp_alua_access_status),
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001943 atomic_read(&lun->lun_tg_pt_secondary_offline) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001944 "Offline" : "None",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001945 core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001947 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001948
1949 return len;
1950}
1951
1952ssize_t core_alua_store_tg_pt_gp_info(
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001953 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954 const char *page,
1955 size_t count)
1956{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001957 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -07001958 /*
1959 * rcu_dereference_raw protected by se_lun->lun_group symlink
1960 * reference to se_device->dev_group.
1961 */
1962 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001963 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001964 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1965 int move = 0;
1966
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001967 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
1968 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1969 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001970
1971 if (count > TG_PT_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001972 pr_err("ALUA Target Port Group alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001973 return -EINVAL;
1974 }
1975 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1976 memcpy(buf, page, count);
1977 /*
1978 * Any ALUA target port group alias besides "NULL" means we will be
1979 * making a new group association.
1980 */
1981 if (strcmp(strstrip(buf), "NULL")) {
1982 /*
1983 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1984 * struct t10_alua_tg_pt_gp. This reference is released with
1985 * core_alua_put_tg_pt_gp_from_name() below.
1986 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001987 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001988 strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001989 if (!tg_pt_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001990 return -ENODEV;
1991 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001992
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001993 spin_lock(&lun->lun_tg_pt_gp_lock);
1994 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001995 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001996 /*
1997 * Clearing an existing tg_pt_gp association, and replacing
1998 * with the default_tg_pt_gp.
1999 */
Andy Grover6708bb22011-06-08 10:36:43 -07002000 if (!tg_pt_gp_new) {
2001 pr_debug("Target_Core_ConfigFS: Moving"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
2003 " alua/%s, ID: %hu back to"
2004 " default_tg_pt_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002005 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2006 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007 config_item_name(&lun->lun_group.cg_item),
2008 config_item_name(
2009 &tg_pt_gp->tg_pt_gp_group.cg_item),
2010 tg_pt_gp->tg_pt_gp_id);
2011
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002012 __target_detach_tg_pt_gp(lun, tg_pt_gp);
2013 __target_attach_tg_pt_gp(lun,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002014 dev->t10_alua.default_tg_pt_gp);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002015 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002016
2017 return count;
2018 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002019 __target_detach_tg_pt_gp(lun, tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002020 move = 1;
2021 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002022
2023 __target_attach_tg_pt_gp(lun, tg_pt_gp_new);
2024 spin_unlock(&lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002025 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002026 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002027 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2028 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002029 config_item_name(&lun->lun_group.cg_item),
2030 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
2031 tg_pt_gp_new->tg_pt_gp_id);
2032
2033 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
2034 return count;
2035}
2036
2037ssize_t core_alua_show_access_type(
2038 struct t10_alua_tg_pt_gp *tg_pt_gp,
2039 char *page)
2040{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002041 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
2042 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
2043 return sprintf(page, "Implicit and Explicit\n");
2044 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
2045 return sprintf(page, "Implicit\n");
2046 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
2047 return sprintf(page, "Explicit\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002048 else
2049 return sprintf(page, "None\n");
2050}
2051
2052ssize_t core_alua_store_access_type(
2053 struct t10_alua_tg_pt_gp *tg_pt_gp,
2054 const char *page,
2055 size_t count)
2056{
2057 unsigned long tmp;
2058 int ret;
2059
Jingoo Han57103d72013-07-19 16:22:19 +09002060 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002061 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002062 pr_err("Unable to extract alua_access_type\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002063 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002064 }
2065 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002066 pr_err("Illegal value for alua_access_type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002067 " %lu\n", tmp);
2068 return -EINVAL;
2069 }
2070 if (tmp == 3)
2071 tg_pt_gp->tg_pt_gp_alua_access_type =
Hannes Reinecke125d0112013-11-19 09:07:46 +01002072 TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002073 else if (tmp == 2)
Hannes Reinecke125d0112013-11-19 09:07:46 +01002074 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002075 else if (tmp == 1)
Hannes Reinecke125d0112013-11-19 09:07:46 +01002076 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002077 else
2078 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2079
2080 return count;
2081}
2082
2083ssize_t core_alua_show_nonop_delay_msecs(
2084 struct t10_alua_tg_pt_gp *tg_pt_gp,
2085 char *page)
2086{
2087 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2088}
2089
2090ssize_t core_alua_store_nonop_delay_msecs(
2091 struct t10_alua_tg_pt_gp *tg_pt_gp,
2092 const char *page,
2093 size_t count)
2094{
2095 unsigned long tmp;
2096 int ret;
2097
Jingoo Han57103d72013-07-19 16:22:19 +09002098 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002099 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002100 pr_err("Unable to extract nonop_delay_msecs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002101 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002102 }
2103 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07002104 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002105 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2106 ALUA_MAX_NONOP_DELAY_MSECS);
2107 return -EINVAL;
2108 }
2109 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2110
2111 return count;
2112}
2113
2114ssize_t core_alua_show_trans_delay_msecs(
2115 struct t10_alua_tg_pt_gp *tg_pt_gp,
2116 char *page)
2117{
2118 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2119}
2120
2121ssize_t core_alua_store_trans_delay_msecs(
2122 struct t10_alua_tg_pt_gp *tg_pt_gp,
2123 const char *page,
2124 size_t count)
2125{
2126 unsigned long tmp;
2127 int ret;
2128
Jingoo Han57103d72013-07-19 16:22:19 +09002129 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002130 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002131 pr_err("Unable to extract trans_delay_msecs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002132 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002133 }
2134 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07002135 pr_err("Passed trans_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002136 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2137 ALUA_MAX_TRANS_DELAY_MSECS);
2138 return -EINVAL;
2139 }
2140 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2141
2142 return count;
2143}
2144
Hannes Reinecke125d0112013-11-19 09:07:46 +01002145ssize_t core_alua_show_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002146 struct t10_alua_tg_pt_gp *tg_pt_gp,
2147 char *page)
2148{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002149 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002150}
2151
Hannes Reinecke125d0112013-11-19 09:07:46 +01002152ssize_t core_alua_store_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002153 struct t10_alua_tg_pt_gp *tg_pt_gp,
2154 const char *page,
2155 size_t count)
2156{
2157 unsigned long tmp;
2158 int ret;
2159
Jingoo Han57103d72013-07-19 16:22:19 +09002160 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002161 if (ret < 0) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002162 pr_err("Unable to extract implicit_trans_secs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002163 return ret;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002164 }
Hannes Reinecke125d0112013-11-19 09:07:46 +01002165 if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2166 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2167 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2168 ALUA_MAX_IMPLICIT_TRANS_SECS);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002169 return -EINVAL;
2170 }
Hannes Reinecke125d0112013-11-19 09:07:46 +01002171 tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002172
2173 return count;
2174}
2175
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002176ssize_t core_alua_show_preferred_bit(
2177 struct t10_alua_tg_pt_gp *tg_pt_gp,
2178 char *page)
2179{
2180 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2181}
2182
2183ssize_t core_alua_store_preferred_bit(
2184 struct t10_alua_tg_pt_gp *tg_pt_gp,
2185 const char *page,
2186 size_t count)
2187{
2188 unsigned long tmp;
2189 int ret;
2190
Jingoo Han57103d72013-07-19 16:22:19 +09002191 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002192 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002193 pr_err("Unable to extract preferred ALUA value\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002194 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002195 }
2196 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002197 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002198 return -EINVAL;
2199 }
2200 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2201
2202 return count;
2203}
2204
2205ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2206{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207 return sprintf(page, "%d\n",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002208 atomic_read(&lun->lun_tg_pt_secondary_offline));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002209}
2210
2211ssize_t core_alua_store_offline_bit(
2212 struct se_lun *lun,
2213 const char *page,
2214 size_t count)
2215{
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -07002216 /*
2217 * rcu_dereference_raw protected by se_lun->lun_group symlink
2218 * reference to se_device->dev_group.
2219 */
2220 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002221 unsigned long tmp;
2222 int ret;
2223
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002224 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
2225 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002226 return -ENODEV;
2227
Jingoo Han57103d72013-07-19 16:22:19 +09002228 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002229 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002230 pr_err("Unable to extract alua_tg_pt_offline value\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002231 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002232 }
2233 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002234 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235 tmp);
2236 return -EINVAL;
2237 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002238
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002239 ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002240 if (ret < 0)
2241 return -EINVAL;
2242
2243 return count;
2244}
2245
2246ssize_t core_alua_show_secondary_status(
2247 struct se_lun *lun,
2248 char *page)
2249{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002250 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002251}
2252
2253ssize_t core_alua_store_secondary_status(
2254 struct se_lun *lun,
2255 const char *page,
2256 size_t count)
2257{
2258 unsigned long tmp;
2259 int ret;
2260
Jingoo Han57103d72013-07-19 16:22:19 +09002261 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002262 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002263 pr_err("Unable to extract alua_tg_pt_status\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002264 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002265 }
2266 if ((tmp != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002267 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2268 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002269 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002270 tmp);
2271 return -EINVAL;
2272 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002273 lun->lun_tg_pt_secondary_stat = (int)tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002274
2275 return count;
2276}
2277
2278ssize_t core_alua_show_secondary_write_metadata(
2279 struct se_lun *lun,
2280 char *page)
2281{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002282 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002283}
2284
2285ssize_t core_alua_store_secondary_write_metadata(
2286 struct se_lun *lun,
2287 const char *page,
2288 size_t count)
2289{
2290 unsigned long tmp;
2291 int ret;
2292
Jingoo Han57103d72013-07-19 16:22:19 +09002293 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002294 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002295 pr_err("Unable to extract alua_tg_pt_write_md\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002296 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002297 }
2298 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002299 pr_err("Illegal value for alua_tg_pt_write_md:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300 " %lu\n", tmp);
2301 return -EINVAL;
2302 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002303 lun->lun_tg_pt_secondary_write_md = (int)tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002304
2305 return count;
2306}
2307
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002308int core_setup_alua(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002309{
Andy Grovera3541702015-05-19 14:44:41 -07002310 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04002311 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2312 struct t10_alua_lu_gp_member *lu_gp_mem;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002313
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002314 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002315 * Associate this struct se_device with the default ALUA
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002316 * LUN Group.
2317 */
2318 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002319 if (IS_ERR(lu_gp_mem))
2320 return PTR_ERR(lu_gp_mem);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002321
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002322 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2323 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00002324 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002325 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2326
Andy Grover6708bb22011-06-08 10:36:43 -07002327 pr_debug("%s: Adding to default ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002328 " core/alua/lu_gps/default_lu_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002329 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002330 }
2331
2332 return 0;
2333}