blob: 02d8e1a8a6a520006dc82cf4e68eb33607c70506 [file] [log] [blame]
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001/*******************************************************************************
2 * Filename: target_core_alua.c
3 *
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
5 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07006 * (c) Copyright 2009-2013 Datera, Inc.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08007 *
8 * Nicholas A. Bellinger <nab@kernel.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 ******************************************************************************/
25
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080026#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/configfs.h>
Paul Gortmakerc53181a2011-08-30 18:16:43 -040029#include <linux/export.h>
Al Viro0e9b10a2013-02-23 15:22:43 -050030#include <linux/file.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080031#include <scsi/scsi.h>
32#include <scsi/scsi_cmnd.h>
Roland Dreier33395fb2012-02-23 17:22:12 -080033#include <asm/unaligned.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080034
35#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050036#include <target/target_core_backend.h>
37#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080038
Christoph Hellwige26d99a2011-11-14 12:30:30 -050039#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include "target_core_ua.h"
42
Hannes Reineckebb91c1a2013-12-17 09:18:43 +010043static sense_reason_t core_alua_check_transition(int state, int valid,
44 int *primary);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080045static int core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -070046 struct se_lun *lun, int explicit, int offline);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080047
Hannes Reineckebb91c1a2013-12-17 09:18:43 +010048static char *core_alua_dump_state(int state);
49
Christoph Hellwigadf653f2015-05-25 21:33:08 -070050static void __target_attach_tg_pt_gp(struct se_lun *lun,
51 struct t10_alua_tg_pt_gp *tg_pt_gp);
52
Andy Grovere3d6f902011-07-19 08:55:10 +000053static u16 alua_lu_gps_counter;
54static u32 alua_lu_gps_count;
55
56static DEFINE_SPINLOCK(lu_gps_lock);
57static LIST_HEAD(lu_gps_list);
58
59struct t10_alua_lu_gp *default_lu_gp;
60
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080061/*
Hannes Reineckec66094b2013-12-17 09:18:49 +010062 * REPORT REFERRALS
63 *
64 * See sbc3r35 section 5.23
65 */
66sense_reason_t
67target_emulate_report_referrals(struct se_cmd *cmd)
68{
69 struct se_device *dev = cmd->se_dev;
70 struct t10_alua_lba_map *map;
71 struct t10_alua_lba_map_member *map_mem;
72 unsigned char *buf;
73 u32 rd_len = 0, off;
74
75 if (cmd->data_length < 4) {
76 pr_warn("REPORT REFERRALS allocation length %u too"
77 " small\n", cmd->data_length);
78 return TCM_INVALID_CDB_FIELD;
79 }
80
81 buf = transport_kmap_data_sg(cmd);
82 if (!buf)
83 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
84
85 off = 4;
86 spin_lock(&dev->t10_alua.lba_map_lock);
87 if (list_empty(&dev->t10_alua.lba_map_list)) {
88 spin_unlock(&dev->t10_alua.lba_map_lock);
89 transport_kunmap_data_sg(cmd);
90
91 return TCM_UNSUPPORTED_SCSI_OPCODE;
92 }
93
94 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
95 lba_map_list) {
96 int desc_num = off + 3;
97 int pg_num;
98
99 off += 4;
Hannes Reinecke38edd722013-12-19 14:36:11 +0100100 if (cmd->data_length > off)
101 put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100102 off += 8;
Hannes Reinecke38edd722013-12-19 14:36:11 +0100103 if (cmd->data_length > off)
104 put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100105 off += 8;
106 rd_len += 20;
107 pg_num = 0;
108 list_for_each_entry(map_mem, &map->lba_map_mem_list,
109 lba_map_mem_list) {
Hannes Reinecke38edd722013-12-19 14:36:11 +0100110 int alua_state = map_mem->lba_map_mem_alua_state;
111 int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;
112
113 if (cmd->data_length > off)
114 buf[off] = alua_state & 0x0f;
115 off += 2;
116 if (cmd->data_length > off)
117 buf[off] = (alua_pg_id >> 8) & 0xff;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100118 off++;
Hannes Reinecke38edd722013-12-19 14:36:11 +0100119 if (cmd->data_length > off)
120 buf[off] = (alua_pg_id & 0xff);
121 off++;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100122 rd_len += 4;
123 pg_num++;
124 }
Hannes Reinecke38edd722013-12-19 14:36:11 +0100125 if (cmd->data_length > desc_num)
126 buf[desc_num] = pg_num;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100127 }
128 spin_unlock(&dev->t10_alua.lba_map_lock);
129
130 /*
131 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
132 */
133 put_unaligned_be16(rd_len, &buf[2]);
134
135 transport_kunmap_data_sg(cmd);
136
137 target_complete_cmd(cmd, GOOD);
138 return 0;
139}
140
141/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800142 * REPORT_TARGET_PORT_GROUPS
143 *
144 * See spc4r17 section 6.27
145 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800146sense_reason_t
147target_emulate_report_target_port_groups(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800148{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400149 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800150 struct t10_alua_tg_pt_gp *tg_pt_gp;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700151 struct se_lun *lun;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000152 unsigned char *buf;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700153 u32 rd_len = 0, off;
154 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800155
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700156 /*
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700157 * Skip over RESERVED area to first Target port group descriptor
158 * depending on the PARAMETER DATA FORMAT type..
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700159 */
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700160 if (ext_hdr != 0)
161 off = 8;
162 else
163 off = 4;
164
165 if (cmd->data_length < off) {
166 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
167 " small for %s header\n", cmd->data_length,
168 (ext_hdr) ? "extended" : "normal");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800169 return TCM_INVALID_CDB_FIELD;
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700170 }
Andy Grover49493142012-01-16 16:57:08 -0800171 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800172 if (!buf)
173 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000174
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400175 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
176 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800177 tg_pt_gp_list) {
178 /*
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700179 * Check if the Target port group and Target port descriptor list
180 * based on tg_pt_gp_members count will fit into the response payload.
181 * Otherwise, bump rd_len to let the initiator know we have exceeded
182 * the allocation length and the response is truncated.
183 */
184 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
185 cmd->data_length) {
186 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
187 continue;
188 }
189 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800190 * PREF: Preferred target port bit, determine if this
191 * bit should be set for port group.
192 */
193 if (tg_pt_gp->tg_pt_gp_pref)
194 buf[off] = 0x80;
195 /*
196 * Set the ASYMMETRIC ACCESS State
197 */
198 buf[off++] |= (atomic_read(
199 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
200 /*
201 * Set supported ASYMMETRIC ACCESS State bits
202 */
Hannes Reineckec0dc9412013-11-19 09:07:49 +0100203 buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204 /*
205 * TARGET PORT GROUP
206 */
207 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
208 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
209
210 off++; /* Skip over Reserved */
211 /*
212 * STATUS CODE
213 */
214 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
215 /*
216 * Vendor Specific field
217 */
218 buf[off++] = 0x00;
219 /*
220 * TARGET PORT COUNT
221 */
222 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
223 rd_len += 8;
224
225 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700226 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
227 lun_tg_pt_gp_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800228 /*
229 * Start Target Port descriptor format
230 *
231 * See spc4r17 section 6.2.7 Table 247
232 */
233 off += 2; /* Skip over Obsolete */
234 /*
235 * Set RELATIVE TARGET PORT IDENTIFIER
236 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700237 buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
238 buf[off++] = (lun->lun_rtpi & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800239 rd_len += 4;
240 }
241 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
242 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400243 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800244 /*
245 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
246 */
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700247 put_unaligned_be32(rd_len, &buf[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800248
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700249 /*
250 * Fill in the Extended header parameter data format if requested
251 */
252 if (ext_hdr != 0) {
253 buf[4] = 0x10;
254 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100255 * Set the implicit transition time (in seconds) for the application
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700256 * client to use as a base for it's transition timeout value.
257 *
258 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
259 * this CDB was received upon to determine this value individually
260 * for ALUA target port group.
261 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700262 spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
263 tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
264 if (tg_pt_gp)
265 buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
266 spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700267 }
Andy Grover49493142012-01-16 16:57:08 -0800268 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000269
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400270 target_complete_cmd(cmd, GOOD);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800271 return 0;
272}
273
274/*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100275 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800276 *
277 * See spc4r17 section 6.35
278 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800279sense_reason_t
280target_emulate_set_target_port_groups(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800281{
Andy Grover5951146d2011-07-19 10:26:37 +0000282 struct se_device *dev = cmd->se_dev;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700283 struct se_lun *l_lun = cmd->se_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +0000284 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800285 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000286 unsigned char *buf;
287 unsigned char *ptr;
Hannes Reineckea0d50f62012-12-17 09:53:34 +0100288 sense_reason_t rc = TCM_NO_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800289 u32 len = 4; /* Skip over RESERVED area in header */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100290 int alua_access_state, primary = 0, valid_states;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800291 u16 tg_pt_id, rtpi;
292
Paolo Bonzini0d7f1292012-09-07 17:30:33 +0200293 if (cmd->data_length < 4) {
294 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
295 " small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800296 return TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +0200297 }
298
Andy Grover49493142012-01-16 16:57:08 -0800299 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800300 if (!buf)
301 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000302
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800303 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100304 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800305 * for the local tg_pt_gp.
306 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700307 spin_lock(&l_lun->lun_tg_pt_gp_lock);
308 l_tg_pt_gp = l_lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -0700309 if (!l_tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700310 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
311 pr_err("Unable to access l_lun->tg_pt_gp\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800312 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000313 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800315
Hannes Reinecke125d0112013-11-19 09:07:46 +0100316 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700317 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700318 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
Hannes Reinecke125d0112013-11-19 09:07:46 +0100319 " while TPGS_EXPLICIT_ALUA is disabled\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800320 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000321 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800322 }
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100323 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700324 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800325
Andy Grover05d1c7c2011-07-20 19:13:28 +0000326 ptr = &buf[4]; /* Skip over RESERVED area in header */
327
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800328 while (len < cmd->data_length) {
Christoph Hellwigde103c92012-11-06 12:24:09 -0800329 bool found = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800330 alua_access_state = (ptr[0] & 0x0f);
331 /*
332 * Check the received ALUA access state, and determine if
333 * the state is a primary or secondary target port asymmetric
334 * access state.
335 */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100336 rc = core_alua_check_transition(alua_access_state,
337 valid_states, &primary);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800338 if (rc) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800339 /*
340 * If the SET TARGET PORT GROUPS attempts to establish
341 * an invalid combination of target port asymmetric
342 * access states or attempts to establish an
343 * unsupported target port asymmetric access state,
344 * then the command shall be terminated with CHECK
345 * CONDITION status, with the sense key set to ILLEGAL
346 * REQUEST, and the additional sense code set to INVALID
347 * FIELD IN PARAMETER LIST.
348 */
Andy Grover05d1c7c2011-07-20 19:13:28 +0000349 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800350 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800351
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800352 /*
353 * If the ASYMMETRIC ACCESS STATE field (see table 267)
354 * specifies a primary target port asymmetric access state,
355 * then the TARGET PORT GROUP OR TARGET PORT field specifies
356 * a primary target port group for which the primary target
357 * port asymmetric access state shall be changed. If the
358 * ASYMMETRIC ACCESS STATE field specifies a secondary target
359 * port asymmetric access state, then the TARGET PORT GROUP OR
360 * TARGET PORT field specifies the relative target port
361 * identifier (see 3.1.120) of the target port for which the
362 * secondary target port asymmetric access state shall be
363 * changed.
364 */
365 if (primary) {
Roland Dreier33395fb2012-02-23 17:22:12 -0800366 tg_pt_id = get_unaligned_be16(ptr + 2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800367 /*
368 * Locate the matching target port group ID from
369 * the global tg_pt_gp list
370 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400371 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800372 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400373 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800374 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700375 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800376 continue;
377
378 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
379 continue;
380
Joern Engel33940d02014-09-16 16:23:12 -0400381 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800382
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400383 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800384
Christoph Hellwigde103c92012-11-06 12:24:09 -0800385 if (!core_alua_do_port_transition(tg_pt_gp,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700386 dev, l_lun, nacl,
Christoph Hellwigde103c92012-11-06 12:24:09 -0800387 alua_access_state, 1))
388 found = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800389
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400390 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Joern Engel33940d02014-09-16 16:23:12 -0400391 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800392 break;
393 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400394 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800395 } else {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700396 struct se_lun *lun;
397
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800398 /*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100399 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800400 * the Target Port in question for the the incoming
401 * SET_TARGET_PORT_GROUPS op.
402 */
Roland Dreier33395fb2012-02-23 17:22:12 -0800403 rtpi = get_unaligned_be16(ptr + 2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800404 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +0900405 * Locate the matching relative target port identifier
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800406 * for the struct se_device storage object.
407 */
408 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700409 list_for_each_entry(lun, &dev->dev_sep_list,
410 lun_dev_link) {
411 if (lun->lun_rtpi != rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800412 continue;
413
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700414 // XXX: racy unlock
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800415 spin_unlock(&dev->se_port_lock);
416
Christoph Hellwigde103c92012-11-06 12:24:09 -0800417 if (!core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700418 lun, 1, 1))
Christoph Hellwigde103c92012-11-06 12:24:09 -0800419 found = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800420
421 spin_lock(&dev->se_port_lock);
422 break;
423 }
424 spin_unlock(&dev->se_port_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800425 }
426
427 if (!found) {
428 rc = TCM_INVALID_PARAMETER_LIST;
429 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800430 }
431
432 ptr += 4;
433 len += 4;
434 }
435
Andy Grover05d1c7c2011-07-20 19:13:28 +0000436out:
Andy Grover49493142012-01-16 16:57:08 -0800437 transport_kunmap_data_sg(cmd);
Roland Dreier59e4f542012-06-04 23:24:51 -0700438 if (!rc)
439 target_complete_cmd(cmd, GOOD);
440 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800441}
442
Paul Bolle1e3ab992014-02-20 09:07:52 +0100443static inline void set_ascq(struct se_cmd *cmd, u8 alua_ascq)
444{
445 /*
446 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
447 * The ALUA additional sense code qualifier (ASCQ) is determined
448 * by the ALUA primary or secondary access state..
449 */
450 pr_debug("[%s]: ALUA TG Port not available, "
451 "SenseKey: NOT_READY, ASC/ASCQ: "
452 "0x04/0x%02x\n",
453 cmd->se_tfo->get_fabric_name(), alua_ascq);
454
455 cmd->scsi_asc = 0x04;
456 cmd->scsi_ascq = alua_ascq;
457}
458
459static inline void core_alua_state_nonoptimized(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800460 struct se_cmd *cmd,
461 unsigned char *cdb,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100462 int nonop_delay_msecs)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800463{
464 /*
465 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
466 * later to determine if processing of this cmd needs to be
467 * temporarily delayed for the Active/NonOptimized primary access state.
468 */
469 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
470 cmd->alua_nonop_delay = nonop_delay_msecs;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800471}
472
Hannes Reineckec66094b2013-12-17 09:18:49 +0100473static inline int core_alua_state_lba_dependent(
474 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100475 struct t10_alua_tg_pt_gp *tg_pt_gp)
Hannes Reineckec66094b2013-12-17 09:18:49 +0100476{
477 struct se_device *dev = cmd->se_dev;
478 u64 segment_size, segment_mult, sectors, lba;
479
480 /* Only need to check for cdb actually containing LBAs */
481 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
482 return 0;
483
484 spin_lock(&dev->t10_alua.lba_map_lock);
485 segment_size = dev->t10_alua.lba_map_segment_size;
486 segment_mult = dev->t10_alua.lba_map_segment_multiplier;
487 sectors = cmd->data_length / dev->dev_attrib.block_size;
488
489 lba = cmd->t_task_lba;
490 while (lba < cmd->t_task_lba + sectors) {
491 struct t10_alua_lba_map *cur_map = NULL, *map;
492 struct t10_alua_lba_map_member *map_mem;
493
494 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
495 lba_map_list) {
496 u64 start_lba, last_lba;
497 u64 first_lba = map->lba_map_first_lba;
498
499 if (segment_mult) {
500 u64 tmp = lba;
Nicholas Bellingercdf55942014-01-30 14:05:16 -0800501 start_lba = do_div(tmp, segment_size * segment_mult);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100502
503 last_lba = first_lba + segment_size - 1;
504 if (start_lba >= first_lba &&
505 start_lba <= last_lba) {
506 lba += segment_size;
507 cur_map = map;
508 break;
509 }
510 } else {
511 last_lba = map->lba_map_last_lba;
512 if (lba >= first_lba && lba <= last_lba) {
513 lba = last_lba + 1;
514 cur_map = map;
515 break;
516 }
517 }
518 }
519 if (!cur_map) {
520 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100521 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100522 return 1;
523 }
524 list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
525 lba_map_mem_list) {
526 if (map_mem->lba_map_mem_alua_pg_id !=
527 tg_pt_gp->tg_pt_gp_id)
528 continue;
529 switch(map_mem->lba_map_mem_alua_state) {
530 case ALUA_ACCESS_STATE_STANDBY:
531 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100532 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100533 return 1;
534 case ALUA_ACCESS_STATE_UNAVAILABLE:
535 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100536 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100537 return 1;
538 default:
539 break;
540 }
541 }
542 }
543 spin_unlock(&dev->t10_alua.lba_map_lock);
544 return 0;
545}
546
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547static inline int core_alua_state_standby(
548 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100549 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800550{
551 /*
552 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
553 * spc4r17 section 5.9.2.4.4
554 */
555 switch (cdb[0]) {
556 case INQUIRY:
557 case LOG_SELECT:
558 case LOG_SENSE:
559 case MODE_SELECT:
560 case MODE_SENSE:
561 case REPORT_LUNS:
562 case RECEIVE_DIAGNOSTIC:
563 case SEND_DIAGNOSTIC:
Nicholas Bellingere7810c22014-06-05 23:37:00 -0700564 case READ_CAPACITY:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700565 return 0;
Hannes Reineckeeb846d92014-11-17 14:25:19 +0100566 case SERVICE_ACTION_IN_16:
Nicholas Bellingere7810c22014-06-05 23:37:00 -0700567 switch (cdb[1] & 0x1f) {
568 case SAI_READ_CAPACITY_16:
569 return 0;
570 default:
571 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
572 return 1;
573 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700575 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800576 case MI_REPORT_TARGET_PGS:
577 return 0;
578 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100579 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800580 return 1;
581 }
582 case MAINTENANCE_OUT:
583 switch (cdb[1]) {
584 case MO_SET_TARGET_PGS:
585 return 0;
586 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100587 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 return 1;
589 }
590 case REQUEST_SENSE:
591 case PERSISTENT_RESERVE_IN:
592 case PERSISTENT_RESERVE_OUT:
593 case READ_BUFFER:
594 case WRITE_BUFFER:
595 return 0;
596 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100597 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800598 return 1;
599 }
600
601 return 0;
602}
603
604static inline int core_alua_state_unavailable(
605 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100606 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800607{
608 /*
609 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
610 * spc4r17 section 5.9.2.4.5
611 */
612 switch (cdb[0]) {
613 case INQUIRY:
614 case REPORT_LUNS:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700615 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800616 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700617 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800618 case MI_REPORT_TARGET_PGS:
619 return 0;
620 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100621 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800622 return 1;
623 }
624 case MAINTENANCE_OUT:
625 switch (cdb[1]) {
626 case MO_SET_TARGET_PGS:
627 return 0;
628 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100629 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800630 return 1;
631 }
632 case REQUEST_SENSE:
633 case READ_BUFFER:
634 case WRITE_BUFFER:
635 return 0;
636 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100637 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800638 return 1;
639 }
640
641 return 0;
642}
643
644static inline int core_alua_state_transition(
645 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100646 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800647{
648 /*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100649 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800650 * spc4r17 section 5.9.2.5
651 */
652 switch (cdb[0]) {
653 case INQUIRY:
654 case REPORT_LUNS:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700655 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800656 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700657 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800658 case MI_REPORT_TARGET_PGS:
659 return 0;
660 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100661 set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800662 return 1;
663 }
664 case REQUEST_SENSE:
665 case READ_BUFFER:
666 case WRITE_BUFFER:
667 return 0;
668 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100669 set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800670 return 1;
671 }
672
673 return 0;
674}
675
676/*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100677 * return 1: Is used to signal LUN not accessible, and check condition/not ready
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800678 * return 0: Used to signal success
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100679 * return -1: Used to signal failure, and invalid cdb field
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800680 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800681sense_reason_t
682target_alua_state_check(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800683{
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400684 struct se_device *dev = cmd->se_dev;
685 unsigned char *cdb = cmd->t_task_cdb;
Andy Grovere3d6f902011-07-19 08:55:10 +0000686 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800687 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800688 int out_alua_state, nonop_delay_msecs;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400689
690 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
691 return 0;
Andy Grovera3541702015-05-19 14:44:41 -0700692 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400693 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800694
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800695 /*
696 * First, check for a struct se_port specific secondary ALUA target port
697 * access state: OFFLINE
698 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700699 if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700700 pr_debug("ALUA: Got secondary offline status for local"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800701 " target port\n");
Paul Bolle1e3ab992014-02-20 09:07:52 +0100702 set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE);
703 return TCM_CHECK_CONDITION_NOT_READY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800704 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700705
706 if (!lun->lun_tg_pt_gp)
Nicholas Bellinger89c12cc2013-08-22 15:35:45 -0700707 return 0;
708
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700709 spin_lock(&lun->lun_tg_pt_gp_lock);
710 tg_pt_gp = lun->lun_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800711 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
712 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700713
714 // XXX: keeps using tg_pt_gp witout reference after unlock
715 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800716 /*
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100717 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300718 * statement so the compiler knows explicitly to check this case first.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800719 * For the Optimized ALUA access state case, we want to process the
720 * incoming fabric cmd ASAP..
721 */
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100722 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800723 return 0;
724
725 switch (out_alua_state) {
726 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100727 core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400728 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800729 case ALUA_ACCESS_STATE_STANDBY:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100730 if (core_alua_state_standby(cmd, cdb))
731 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400732 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800733 case ALUA_ACCESS_STATE_UNAVAILABLE:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100734 if (core_alua_state_unavailable(cmd, cdb))
735 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400736 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800737 case ALUA_ACCESS_STATE_TRANSITION:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100738 if (core_alua_state_transition(cmd, cdb))
739 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400740 break;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100741 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100742 if (core_alua_state_lba_dependent(cmd, tg_pt_gp))
743 return TCM_CHECK_CONDITION_NOT_READY;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100744 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800745 /*
746 * OFFLINE is a secondary ALUA target port group access state, that is
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700747 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800748 */
749 case ALUA_ACCESS_STATE_OFFLINE:
750 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700751 pr_err("Unknown ALUA access state: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800752 out_alua_state);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800753 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800754 }
755
Christoph Hellwigde103c92012-11-06 12:24:09 -0800756 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800757}
758
759/*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100760 * Check implicit and explicit ALUA state change request.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800761 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800762static sense_reason_t
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100763core_alua_check_transition(int state, int valid, int *primary)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800764{
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100765 /*
766 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
767 * defined as primary target port asymmetric access states.
768 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800769 switch (state) {
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100770 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100771 if (!(valid & ALUA_AO_SUP))
772 goto not_supported;
773 *primary = 1;
774 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800775 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100776 if (!(valid & ALUA_AN_SUP))
777 goto not_supported;
778 *primary = 1;
779 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800780 case ALUA_ACCESS_STATE_STANDBY:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100781 if (!(valid & ALUA_S_SUP))
782 goto not_supported;
783 *primary = 1;
784 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800785 case ALUA_ACCESS_STATE_UNAVAILABLE:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100786 if (!(valid & ALUA_U_SUP))
787 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800788 *primary = 1;
789 break;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100790 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
791 if (!(valid & ALUA_LBD_SUP))
792 goto not_supported;
793 *primary = 1;
794 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800795 case ALUA_ACCESS_STATE_OFFLINE:
796 /*
797 * OFFLINE state is defined as a secondary target port
798 * asymmetric access state.
799 */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100800 if (!(valid & ALUA_O_SUP))
801 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800802 *primary = 0;
803 break;
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100804 case ALUA_ACCESS_STATE_TRANSITION:
805 /*
806 * Transitioning is set internally, and
807 * cannot be selected manually.
808 */
809 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800810 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700811 pr_err("Unknown ALUA access state: 0x%02x\n", state);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800812 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800813 }
814
815 return 0;
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100816
817not_supported:
818 pr_err("ALUA access state %s not supported",
819 core_alua_dump_state(state));
820 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800821}
822
823static char *core_alua_dump_state(int state)
824{
825 switch (state) {
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100826 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800827 return "Active/Optimized";
828 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
829 return "Active/NonOptimized";
Hannes Reineckec66094b2013-12-17 09:18:49 +0100830 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
831 return "LBA Dependent";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800832 case ALUA_ACCESS_STATE_STANDBY:
833 return "Standby";
834 case ALUA_ACCESS_STATE_UNAVAILABLE:
835 return "Unavailable";
836 case ALUA_ACCESS_STATE_OFFLINE:
837 return "Offline";
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100838 case ALUA_ACCESS_STATE_TRANSITION:
839 return "Transitioning";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800840 default:
841 return "Unknown";
842 }
843
844 return NULL;
845}
846
847char *core_alua_dump_status(int status)
848{
849 switch (status) {
850 case ALUA_STATUS_NONE:
851 return "None";
Hannes Reinecke125d0112013-11-19 09:07:46 +0100852 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
853 return "Altered by Explicit STPG";
854 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
855 return "Altered by Implicit ALUA";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800856 default:
857 return "Unknown";
858 }
859
860 return NULL;
861}
862
863/*
864 * Used by fabric modules to determine when we need to delay processing
865 * for the Active/NonOptimized paths..
866 */
867int core_alua_check_nonop_delay(
868 struct se_cmd *cmd)
869{
870 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
871 return 0;
872 if (in_interrupt())
873 return 0;
874 /*
875 * The ALUA Active/NonOptimized access state delay can be disabled
876 * in via configfs with a value of zero
877 */
Andy Grover6708bb22011-06-08 10:36:43 -0700878 if (!cmd->alua_nonop_delay)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800879 return 0;
880 /*
881 * struct se_cmd->alua_nonop_delay gets set by a target port group
882 * defined interval in core_alua_state_nonoptimized()
883 */
884 msleep_interruptible(cmd->alua_nonop_delay);
885 return 0;
886}
887EXPORT_SYMBOL(core_alua_check_nonop_delay);
888
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800889static int core_alua_write_tpg_metadata(
890 const char *path,
891 unsigned char *md_buf,
892 u32 md_buf_len)
893{
Al Viro0e9b10a2013-02-23 15:22:43 -0500894 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
895 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800896
Al Viro0e9b10a2013-02-23 15:22:43 -0500897 if (IS_ERR(file)) {
898 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800899 return -ENODEV;
900 }
Al Viro0e9b10a2013-02-23 15:22:43 -0500901 ret = kernel_write(file, md_buf, md_buf_len, 0);
902 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700903 pr_err("Error writing ALUA metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -0500904 fput(file);
Gera Kazakovf730f912013-09-09 15:47:06 -0700905 return (ret < 0) ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800906}
907
908/*
909 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
910 */
911static int core_alua_update_tpg_primary_metadata(
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100912 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800913{
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100914 unsigned char *md_buf;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400915 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800916 char path[ALUA_METADATA_PATH_LEN];
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100917 int len, rc;
918
919 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
920 if (!md_buf) {
921 pr_err("Unable to allocate buf for ALUA metadata\n");
922 return -ENOMEM;
923 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800924
925 memset(path, 0, ALUA_METADATA_PATH_LEN);
926
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100927 len = snprintf(md_buf, ALUA_MD_BUF_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800928 "tg_pt_gp_id=%hu\n"
929 "alua_access_state=0x%02x\n"
930 "alua_access_status=0x%02x\n",
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100931 tg_pt_gp->tg_pt_gp_id,
932 tg_pt_gp->tg_pt_gp_alua_pending_state,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800933 tg_pt_gp->tg_pt_gp_alua_access_status);
934
935 snprintf(path, ALUA_METADATA_PATH_LEN,
936 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
937 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
938
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100939 rc = core_alua_write_tpg_metadata(path, md_buf, len);
940 kfree(md_buf);
941 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800942}
943
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100944static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800945{
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100946 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work,
947 struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work.work);
948 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800949 struct se_dev_entry *se_deve;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700950 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800951 struct se_lun_acl *lacl;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100952 bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
953 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800954
955 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700956 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
957 lun_tg_pt_gp_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958 /*
959 * After an implicit target port asymmetric access state
960 * change, a device server shall establish a unit attention
961 * condition for the initiator port associated with every I_T
962 * nexus with the additional sense code set to ASYMMETRIC
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100963 * ACCESS STATE CHANGED.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800964 *
965 * After an explicit target port asymmetric access state
966 * change, a device server shall establish a unit attention
967 * condition with the additional sense code set to ASYMMETRIC
968 * ACCESS STATE CHANGED for the initiator port associated with
969 * every I_T nexus other than the I_T nexus on which the SET
970 * TARGET PORT GROUPS command
971 */
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700972 if (!percpu_ref_tryget_live(&lun->lun_ref))
973 continue;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800974 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
975
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700976 spin_lock_bh(&lun->lun_deve_lock);
977 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700978 lacl = rcu_dereference_check(se_deve->se_lun_acl,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700979 lockdep_is_held(&lun->lun_deve_lock));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800980 /*
981 * se_deve->se_lun_acl pointer may be NULL for a
Hannes Reinecke125d0112013-11-19 09:07:46 +0100982 * entry created without explicit Node+MappedLUN ACLs
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800983 */
Andy Grover6708bb22011-06-08 10:36:43 -0700984 if (!lacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800985 continue;
986
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100987 if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
988 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
989 (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
990 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl) &&
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700991 (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
992 (tg_pt_gp->tg_pt_gp_alua_lun == lun))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800993 continue;
994
995 core_scsi3_ua_allocate(lacl->se_lun_nacl,
996 se_deve->mapped_lun, 0x2A,
997 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
998 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700999 spin_unlock_bh(&lun->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001000
1001 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Nicholas Bellinger9e37d042015-05-20 21:21:08 -07001002 percpu_ref_put(&lun->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001003 }
1004 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1005 /*
1006 * Update the ALUA metadata buf that has been allocated in
1007 * core_alua_do_port_transition(), this metadata will be written
1008 * to struct file.
1009 *
1010 * Note that there is the case where we do not want to update the
1011 * metadata when the saved metadata is being parsed in userspace
1012 * when setting the existing port access state and access status.
1013 *
1014 * Also note that the failure to write out the ALUA metadata to
1015 * struct file does NOT affect the actual ALUA transition.
1016 */
1017 if (tg_pt_gp->tg_pt_gp_write_metadata) {
1018 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001019 core_alua_update_tpg_primary_metadata(tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001020 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
1021 }
1022 /*
1023 * Set the current primary ALUA access state to the requested new state
1024 */
Hannes Reineckedfbce752013-12-17 09:18:45 +01001025 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1026 tg_pt_gp->tg_pt_gp_alua_pending_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001027
Andy Grover6708bb22011-06-08 10:36:43 -07001028 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Hannes Reinecke125d0112013-11-19 09:07:46 +01001029 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1030 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
Hannes Reineckedfbce752013-12-17 09:18:45 +01001031 tg_pt_gp->tg_pt_gp_id,
1032 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state),
1033 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001034 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1035 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001036 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1037
1038 if (tg_pt_gp->tg_pt_gp_transition_complete)
1039 complete(tg_pt_gp->tg_pt_gp_transition_complete);
1040}
1041
1042static int core_alua_do_transition_tg_pt(
1043 struct t10_alua_tg_pt_gp *tg_pt_gp,
1044 int new_state,
1045 int explicit)
1046{
1047 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1048 DECLARE_COMPLETION_ONSTACK(wait);
1049
1050 /* Nothing to be done here */
1051 if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state)
1052 return 0;
1053
1054 if (new_state == ALUA_ACCESS_STATE_TRANSITION)
1055 return -EAGAIN;
1056
1057 /*
1058 * Flush any pending transitions
1059 */
1060 if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs &&
1061 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) ==
1062 ALUA_ACCESS_STATE_TRANSITION) {
1063 /* Just in case */
1064 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1065 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1066 flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
1067 wait_for_completion(&wait);
1068 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1069 return 0;
1070 }
1071
1072 /*
1073 * Save the old primary ALUA access state, and set the current state
1074 * to ALUA_ACCESS_STATE_TRANSITION.
1075 */
1076 tg_pt_gp->tg_pt_gp_alua_previous_state =
1077 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
1078 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1079
1080 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1081 ALUA_ACCESS_STATE_TRANSITION);
1082 tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1083 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1084 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1085
1086 /*
1087 * Check for the optional ALUA primary state transition delay
1088 */
1089 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
1090 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1091
1092 /*
1093 * Take a reference for workqueue item
1094 */
1095 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1096 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001097 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1098
1099 if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs) {
1100 unsigned long transition_tmo;
1101
1102 transition_tmo = tg_pt_gp->tg_pt_gp_implicit_trans_secs * HZ;
1103 queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
1104 &tg_pt_gp->tg_pt_gp_transition_work,
1105 transition_tmo);
1106 } else {
1107 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1108 queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
1109 &tg_pt_gp->tg_pt_gp_transition_work, 0);
1110 wait_for_completion(&wait);
1111 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1112 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001113
1114 return 0;
1115}
1116
1117int core_alua_do_port_transition(
1118 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1119 struct se_device *l_dev,
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001120 struct se_lun *l_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001121 struct se_node_acl *l_nacl,
1122 int new_state,
Hannes Reinecke125d0112013-11-19 09:07:46 +01001123 int explicit)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001124{
1125 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001126 struct t10_alua_lu_gp *lu_gp;
1127 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1128 struct t10_alua_tg_pt_gp *tg_pt_gp;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001129 int primary, valid_states, rc = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001130
Hannes Reineckebb91c1a2013-12-17 09:18:43 +01001131 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1132 if (core_alua_check_transition(new_state, valid_states, &primary) != 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001133 return -EINVAL;
1134
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001135 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1136 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1137 lu_gp = local_lu_gp_mem->lu_gp;
1138 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1140 /*
1141 * For storage objects that are members of the 'default_lu_gp',
1142 * we only do transition on the passed *l_tp_pt_gp, and not
1143 * on all of the matching target port groups IDs in default_lu_gp.
1144 */
Andy Grover6708bb22011-06-08 10:36:43 -07001145 if (!lu_gp->lu_gp_id) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001146 /*
1147 * core_alua_do_transition_tg_pt() will always return
1148 * success.
1149 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001150 l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001151 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1152 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1153 new_state, explicit);
Joern Engel33940d02014-09-16 16:23:12 -04001154 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001155 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001156 }
1157 /*
1158 * For all other LU groups aside from 'default_lu_gp', walk all of
1159 * the associated storage objects looking for a matching target port
1160 * group ID from the local target port group.
1161 */
1162 spin_lock(&lu_gp->lu_gp_lock);
1163 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1164 lu_gp_mem_list) {
1165
1166 dev = lu_gp_mem->lu_gp_mem_dev;
Joern Engel33940d02014-09-16 16:23:12 -04001167 atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001168 spin_unlock(&lu_gp->lu_gp_lock);
1169
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001170 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001171 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001172 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001173 tg_pt_gp_list) {
1174
Andy Grover6708bb22011-06-08 10:36:43 -07001175 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001176 continue;
1177 /*
1178 * If the target behavior port asymmetric access state
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001179 * is changed for any target port group accessible via
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001180 * a logical unit within a LU group, the target port
1181 * behavior group asymmetric access states for the same
1182 * target port group accessible via other logical units
1183 * in that LU group will also change.
1184 */
1185 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1186 continue;
1187
1188 if (l_tg_pt_gp == tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001189 tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001190 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191 } else {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001192 tg_pt_gp->tg_pt_gp_alua_lun = NULL;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001193 tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001194 }
Joern Engel33940d02014-09-16 16:23:12 -04001195 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001196 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001197 /*
1198 * core_alua_do_transition_tg_pt() will always return
1199 * success.
1200 */
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001201 rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1202 new_state, explicit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001203
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001204 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Joern Engel33940d02014-09-16 16:23:12 -04001205 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001206 if (rc)
1207 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001208 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001209 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001210
1211 spin_lock(&lu_gp->lu_gp_lock);
Joern Engel33940d02014-09-16 16:23:12 -04001212 atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001213 }
1214 spin_unlock(&lu_gp->lu_gp_lock);
1215
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001216 if (!rc) {
1217 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1218 " Group IDs: %hu %s transition to primary state: %s\n",
1219 config_item_name(&lu_gp->lu_gp_group.cg_item),
1220 l_tg_pt_gp->tg_pt_gp_id,
1221 (explicit) ? "explicit" : "implicit",
1222 core_alua_dump_state(new_state));
1223 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001224
Joern Engel33940d02014-09-16 16:23:12 -04001225 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001226 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001227}
1228
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001229static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001230{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001231 struct se_portal_group *se_tpg = lun->lun_tpg;
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001232 unsigned char *md_buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001233 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001234 int len, rc;
1235
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001236 mutex_lock(&lun->lun_tg_pt_md_mutex);
1237
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001238 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1239 if (!md_buf) {
1240 pr_err("Unable to allocate buf for ALUA metadata\n");
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001241 rc = -ENOMEM;
1242 goto out_unlock;
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001243 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001244
1245 memset(path, 0, ALUA_METADATA_PATH_LEN);
1246 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1247
1248 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +00001249 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001250
Andy Grovere3d6f902011-07-19 08:55:10 +00001251 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001252 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
Andy Grovere3d6f902011-07-19 08:55:10 +00001253 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001254
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001255 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001256 "alua_tg_pt_status=0x%02x\n",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001257 atomic_read(&lun->lun_tg_pt_secondary_offline),
1258 lun->lun_tg_pt_secondary_stat);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259
1260 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
Andy Grovere3d6f902011-07-19 08:55:10 +00001261 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001262 lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001263
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001264 rc = core_alua_write_tpg_metadata(path, md_buf, len);
1265 kfree(md_buf);
1266
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001267out_unlock:
1268 mutex_unlock(&lun->lun_tg_pt_md_mutex);
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001269 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001270}
1271
1272static int core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001273 struct se_lun *lun,
Hannes Reinecke125d0112013-11-19 09:07:46 +01001274 int explicit,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001275 int offline)
1276{
1277 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001278 int trans_delay_msecs;
1279
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001280 spin_lock(&lun->lun_tg_pt_gp_lock);
1281 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001282 if (!tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001283 spin_unlock(&lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001284 pr_err("Unable to complete secondary state"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001285 " transition\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001286 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287 }
1288 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1289 /*
1290 * Set the secondary ALUA target port access state to OFFLINE
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001291 * or release the previously secondary state for struct se_lun
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001292 */
1293 if (offline)
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001294 atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001295 else
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001296 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001297
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001298 lun->lun_tg_pt_secondary_stat = (explicit) ?
Hannes Reinecke125d0112013-11-19 09:07:46 +01001299 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1300 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001301
Andy Grover6708bb22011-06-08 10:36:43 -07001302 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Hannes Reinecke125d0112013-11-19 09:07:46 +01001303 " to secondary access state: %s\n", (explicit) ? "explicit" :
1304 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1306
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001307 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001308 /*
1309 * Do the optional transition delay after we set the secondary
1310 * ALUA access state.
1311 */
1312 if (trans_delay_msecs != 0)
1313 msleep_interruptible(trans_delay_msecs);
1314 /*
1315 * See if we need to update the ALUA fabric port metadata for
1316 * secondary state and status
1317 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001318 if (lun->lun_tg_pt_secondary_write_md)
1319 core_alua_update_tpg_secondary_metadata(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001320
1321 return 0;
1322}
1323
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001324struct t10_alua_lba_map *
1325core_alua_allocate_lba_map(struct list_head *list,
1326 u64 first_lba, u64 last_lba)
1327{
1328 struct t10_alua_lba_map *lba_map;
1329
1330 lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1331 if (!lba_map) {
1332 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1333 return ERR_PTR(-ENOMEM);
1334 }
1335 INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1336 lba_map->lba_map_first_lba = first_lba;
1337 lba_map->lba_map_last_lba = last_lba;
1338
1339 list_add_tail(&lba_map->lba_map_list, list);
1340 return lba_map;
1341}
1342
1343int
1344core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1345 int pg_id, int state)
1346{
1347 struct t10_alua_lba_map_member *lba_map_mem;
1348
1349 list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1350 lba_map_mem_list) {
1351 if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1352 pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1353 return -EINVAL;
1354 }
1355 }
1356
1357 lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1358 if (!lba_map_mem) {
1359 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1360 return -ENOMEM;
1361 }
1362 lba_map_mem->lba_map_mem_alua_state = state;
1363 lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1364
1365 list_add_tail(&lba_map_mem->lba_map_mem_list,
1366 &lba_map->lba_map_mem_list);
1367 return 0;
1368}
1369
1370void
1371core_alua_free_lba_map(struct list_head *lba_list)
1372{
1373 struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1374 struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1375
1376 list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1377 lba_map_list) {
1378 list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1379 &lba_map->lba_map_mem_list,
1380 lba_map_mem_list) {
1381 list_del(&lba_map_mem->lba_map_mem_list);
1382 kmem_cache_free(t10_alua_lba_map_mem_cache,
1383 lba_map_mem);
1384 }
1385 list_del(&lba_map->lba_map_list);
1386 kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1387 }
1388}
1389
1390void
1391core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1392 int segment_size, int segment_mult)
1393{
1394 struct list_head old_lba_map_list;
1395 struct t10_alua_tg_pt_gp *tg_pt_gp;
1396 int activate = 0, supported;
1397
1398 INIT_LIST_HEAD(&old_lba_map_list);
1399 spin_lock(&dev->t10_alua.lba_map_lock);
1400 dev->t10_alua.lba_map_segment_size = segment_size;
1401 dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1402 list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1403 if (lba_map_list) {
1404 list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1405 activate = 1;
1406 }
1407 spin_unlock(&dev->t10_alua.lba_map_lock);
1408 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1409 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1410 tg_pt_gp_list) {
1411
1412 if (!tg_pt_gp->tg_pt_gp_valid_id)
1413 continue;
1414 supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1415 if (activate)
1416 supported |= ALUA_LBD_SUP;
1417 else
1418 supported &= ~ALUA_LBD_SUP;
1419 tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1420 }
1421 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1422 core_alua_free_lba_map(&old_lba_map_list);
1423}
1424
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001425struct t10_alua_lu_gp *
1426core_alua_allocate_lu_gp(const char *name, int def_group)
1427{
1428 struct t10_alua_lu_gp *lu_gp;
1429
1430 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001431 if (!lu_gp) {
1432 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001433 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001434 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001435 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001436 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1437 spin_lock_init(&lu_gp->lu_gp_lock);
1438 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1439
1440 if (def_group) {
Andy Grovere3d6f902011-07-19 08:55:10 +00001441 lu_gp->lu_gp_id = alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001442 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001443 alua_lu_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001444 }
1445
1446 return lu_gp;
1447}
1448
1449int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1450{
1451 struct t10_alua_lu_gp *lu_gp_tmp;
1452 u16 lu_gp_id_tmp;
1453 /*
1454 * The lu_gp->lu_gp_id may only be set once..
1455 */
1456 if (lu_gp->lu_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001457 pr_warn("ALUA LU Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001458 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001459 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 }
1461
Andy Grovere3d6f902011-07-19 08:55:10 +00001462 spin_lock(&lu_gps_lock);
1463 if (alua_lu_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001464 pr_err("Maximum ALUA alua_lu_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001465 " 0x0000ffff reached\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001466 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001467 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001468 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001469 }
1470again:
1471 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
Andy Grovere3d6f902011-07-19 08:55:10 +00001472 alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001473
Andy Grovere3d6f902011-07-19 08:55:10 +00001474 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001475 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001476 if (!lu_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001477 goto again;
1478
Andy Grover6708bb22011-06-08 10:36:43 -07001479 pr_warn("ALUA Logical Unit Group ID: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001480 " already exists, ignoring request\n",
1481 lu_gp_id);
Andy Grovere3d6f902011-07-19 08:55:10 +00001482 spin_unlock(&lu_gps_lock);
1483 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001484 }
1485 }
1486
1487 lu_gp->lu_gp_id = lu_gp_id_tmp;
1488 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001489 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1490 alua_lu_gps_count++;
1491 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492
1493 return 0;
1494}
1495
1496static struct t10_alua_lu_gp_member *
1497core_alua_allocate_lu_gp_mem(struct se_device *dev)
1498{
1499 struct t10_alua_lu_gp_member *lu_gp_mem;
1500
1501 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001502 if (!lu_gp_mem) {
1503 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001504 return ERR_PTR(-ENOMEM);
1505 }
1506 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1507 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1508 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1509
1510 lu_gp_mem->lu_gp_mem_dev = dev;
1511 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1512
1513 return lu_gp_mem;
1514}
1515
1516void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1517{
1518 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1519 /*
1520 * Once we have reached this point, config_item_put() has
1521 * already been called from target_core_alua_drop_lu_gp().
1522 *
1523 * Here, we remove the *lu_gp from the global list so that
1524 * no associations can be made while we are releasing
1525 * struct t10_alua_lu_gp.
1526 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001527 spin_lock(&lu_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001528 list_del(&lu_gp->lu_gp_node);
1529 alua_lu_gps_count--;
1530 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001531 /*
1532 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1533 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1534 * released with core_alua_put_lu_gp_from_name()
1535 */
1536 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1537 cpu_relax();
1538 /*
1539 * Release reference to struct t10_alua_lu_gp * from all associated
1540 * struct se_device.
1541 */
1542 spin_lock(&lu_gp->lu_gp_lock);
1543 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1544 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1545 if (lu_gp_mem->lu_gp_assoc) {
1546 list_del(&lu_gp_mem->lu_gp_mem_list);
1547 lu_gp->lu_gp_members--;
1548 lu_gp_mem->lu_gp_assoc = 0;
1549 }
1550 spin_unlock(&lu_gp->lu_gp_lock);
1551 /*
1552 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001553 * lu_gp_mem is associated with a single
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001554 * struct se_device->dev_alua_lu_gp_mem, and is released when
1555 * struct se_device is released via core_alua_free_lu_gp_mem().
1556 *
1557 * If the passed lu_gp does NOT match the default_lu_gp, assume
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001558 * we want to re-associate a given lu_gp_mem with default_lu_gp.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001559 */
1560 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001561 if (lu_gp != default_lu_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001562 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00001563 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001564 else
1565 lu_gp_mem->lu_gp = NULL;
1566 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1567
1568 spin_lock(&lu_gp->lu_gp_lock);
1569 }
1570 spin_unlock(&lu_gp->lu_gp_lock);
1571
1572 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1573}
1574
1575void core_alua_free_lu_gp_mem(struct se_device *dev)
1576{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001577 struct t10_alua_lu_gp *lu_gp;
1578 struct t10_alua_lu_gp_member *lu_gp_mem;
1579
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001580 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -07001581 if (!lu_gp_mem)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 return;
1583
1584 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1585 cpu_relax();
1586
1587 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1588 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001589 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590 spin_lock(&lu_gp->lu_gp_lock);
1591 if (lu_gp_mem->lu_gp_assoc) {
1592 list_del(&lu_gp_mem->lu_gp_mem_list);
1593 lu_gp->lu_gp_members--;
1594 lu_gp_mem->lu_gp_assoc = 0;
1595 }
1596 spin_unlock(&lu_gp->lu_gp_lock);
1597 lu_gp_mem->lu_gp = NULL;
1598 }
1599 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1600
1601 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1602}
1603
1604struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1605{
1606 struct t10_alua_lu_gp *lu_gp;
1607 struct config_item *ci;
1608
Andy Grovere3d6f902011-07-19 08:55:10 +00001609 spin_lock(&lu_gps_lock);
1610 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
Andy Grover6708bb22011-06-08 10:36:43 -07001611 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001612 continue;
1613 ci = &lu_gp->lu_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001614 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001615 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001616 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001617 return lu_gp;
1618 }
1619 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001620 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001621
1622 return NULL;
1623}
1624
1625void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1626{
Andy Grovere3d6f902011-07-19 08:55:10 +00001627 spin_lock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001628 atomic_dec(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001629 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001630}
1631
1632/*
1633 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1634 */
1635void __core_alua_attach_lu_gp_mem(
1636 struct t10_alua_lu_gp_member *lu_gp_mem,
1637 struct t10_alua_lu_gp *lu_gp)
1638{
1639 spin_lock(&lu_gp->lu_gp_lock);
1640 lu_gp_mem->lu_gp = lu_gp;
1641 lu_gp_mem->lu_gp_assoc = 1;
1642 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1643 lu_gp->lu_gp_members++;
1644 spin_unlock(&lu_gp->lu_gp_lock);
1645}
1646
1647/*
1648 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1649 */
1650void __core_alua_drop_lu_gp_mem(
1651 struct t10_alua_lu_gp_member *lu_gp_mem,
1652 struct t10_alua_lu_gp *lu_gp)
1653{
1654 spin_lock(&lu_gp->lu_gp_lock);
1655 list_del(&lu_gp_mem->lu_gp_mem_list);
1656 lu_gp_mem->lu_gp = NULL;
1657 lu_gp_mem->lu_gp_assoc = 0;
1658 lu_gp->lu_gp_members--;
1659 spin_unlock(&lu_gp->lu_gp_lock);
1660}
1661
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001662struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1663 const char *name, int def_group)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001664{
1665 struct t10_alua_tg_pt_gp *tg_pt_gp;
1666
1667 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001668 if (!tg_pt_gp) {
1669 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001670 return NULL;
1671 }
1672 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001673 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001674 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1675 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1676 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001677 INIT_DELAYED_WORK(&tg_pt_gp->tg_pt_gp_transition_work,
1678 core_alua_do_transition_tg_pt_work);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001679 tg_pt_gp->tg_pt_gp_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001680 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
Hannes Reinecke73f3bf52013-11-19 09:07:48 +01001681 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001682 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001683 * Enable both explicit and implicit ALUA support by default
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001684 */
1685 tg_pt_gp->tg_pt_gp_alua_access_type =
Hannes Reinecke125d0112013-11-19 09:07:46 +01001686 TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001687 /*
1688 * Set the default Active/NonOptimized Delay in milliseconds
1689 */
1690 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1691 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
Hannes Reinecke125d0112013-11-19 09:07:46 +01001692 tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001693
Hannes Reineckec0dc9412013-11-19 09:07:49 +01001694 /*
1695 * Enable all supported states
1696 */
1697 tg_pt_gp->tg_pt_gp_alua_supported_states =
1698 ALUA_T_SUP | ALUA_O_SUP |
1699 ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1700
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001701 if (def_group) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001702 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001703 tg_pt_gp->tg_pt_gp_id =
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001704 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705 tg_pt_gp->tg_pt_gp_valid_id = 1;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001706 dev->t10_alua.alua_tg_pt_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001707 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001708 &dev->t10_alua.tg_pt_gps_list);
1709 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001710 }
1711
1712 return tg_pt_gp;
1713}
1714
1715int core_alua_set_tg_pt_gp_id(
1716 struct t10_alua_tg_pt_gp *tg_pt_gp,
1717 u16 tg_pt_gp_id)
1718{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001719 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001720 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1721 u16 tg_pt_gp_id_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001722
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001723 /*
1724 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1725 */
1726 if (tg_pt_gp->tg_pt_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001727 pr_warn("ALUA TG PT Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001728 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001729 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001730 }
1731
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001732 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1733 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001734 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001735 " 0x0000ffff reached\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001736 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001737 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001738 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001739 }
1740again:
1741 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001742 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001743
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001744 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001745 tg_pt_gp_list) {
1746 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001747 if (!tg_pt_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001748 goto again;
1749
Andy Grover6708bb22011-06-08 10:36:43 -07001750 pr_err("ALUA Target Port Group ID: %hu already"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001751 " exists, ignoring request\n", tg_pt_gp_id);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001752 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001753 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001754 }
1755 }
1756
1757 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1758 tg_pt_gp->tg_pt_gp_valid_id = 1;
1759 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001760 &dev->t10_alua.tg_pt_gps_list);
1761 dev->t10_alua.alua_tg_pt_gps_count++;
1762 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001763
1764 return 0;
1765}
1766
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001767void core_alua_free_tg_pt_gp(
1768 struct t10_alua_tg_pt_gp *tg_pt_gp)
1769{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001770 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001771 struct se_lun *lun, *next;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001772
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001773 /*
1774 * Once we have reached this point, config_item_put() has already
1775 * been called from target_core_alua_drop_tg_pt_gp().
1776 *
1777 * Here we remove *tg_pt_gp from the global list so that
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001778 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001779 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1780 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001781 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001782 list_del(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001783 dev->t10_alua.alua_tg_pt_gps_counter--;
1784 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1785
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001786 flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
1787
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001788 /*
1789 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1790 * core_alua_get_tg_pt_gp_by_name() in
1791 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1792 * to be released with core_alua_put_tg_pt_gp_from_name().
1793 */
1794 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1795 cpu_relax();
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001796
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001797 /*
1798 * Release reference to struct t10_alua_tg_pt_gp from all associated
1799 * struct se_port.
1800 */
1801 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001802 list_for_each_entry_safe(lun, next,
1803 &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1804 list_del_init(&lun->lun_tg_pt_gp_link);
1805 tg_pt_gp->tg_pt_gp_members--;
1806
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001807 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1808 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001809 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001810 * assume we want to re-associate a given tg_pt_gp_mem with
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001811 * default_tg_pt_gp.
1812 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001813 spin_lock(&lun->lun_tg_pt_gp_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001814 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001815 __target_attach_tg_pt_gp(lun,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001816 dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001817 } else
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001818 lun->lun_tg_pt_gp = NULL;
1819 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001820
1821 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1822 }
1823 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1824
1825 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1826}
1827
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001828static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001829 struct se_device *dev, const char *name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001830{
1831 struct t10_alua_tg_pt_gp *tg_pt_gp;
1832 struct config_item *ci;
1833
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001834 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1835 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001836 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001837 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001838 continue;
1839 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001840 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001841 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001842 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001843 return tg_pt_gp;
1844 }
1845 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001846 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001847
1848 return NULL;
1849}
1850
1851static void core_alua_put_tg_pt_gp_from_name(
1852 struct t10_alua_tg_pt_gp *tg_pt_gp)
1853{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001854 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001855
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001856 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001857 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001858 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001859}
1860
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001861static void __target_attach_tg_pt_gp(struct se_lun *lun,
1862 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001863{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001864 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1865
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001866 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001867 lun->lun_tg_pt_gp = tg_pt_gp;
1868 list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001869 tg_pt_gp->tg_pt_gp_members++;
1870 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1871}
1872
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001873void target_attach_tg_pt_gp(struct se_lun *lun,
1874 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001875{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001876 spin_lock(&lun->lun_tg_pt_gp_lock);
1877 __target_attach_tg_pt_gp(lun, tg_pt_gp);
1878 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001879}
1880
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001881static void __target_detach_tg_pt_gp(struct se_lun *lun,
1882 struct t10_alua_tg_pt_gp *tg_pt_gp)
1883{
1884 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1885
1886 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1887 list_del_init(&lun->lun_tg_pt_gp_link);
1888 tg_pt_gp->tg_pt_gp_members--;
1889 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1890
1891 lun->lun_tg_pt_gp = NULL;
1892}
1893
1894void target_detach_tg_pt_gp(struct se_lun *lun)
1895{
1896 struct t10_alua_tg_pt_gp *tg_pt_gp;
1897
1898 spin_lock(&lun->lun_tg_pt_gp_lock);
1899 tg_pt_gp = lun->lun_tg_pt_gp;
1900 if (tg_pt_gp)
1901 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1902 spin_unlock(&lun->lun_tg_pt_gp_lock);
1903}
1904
1905ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001906{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001907 struct config_item *tg_pt_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001908 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001909 ssize_t len = 0;
1910
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001911 spin_lock(&lun->lun_tg_pt_gp_lock);
1912 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001913 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001914 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1915 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1916 " %hu\nTG Port Primary Access State: %s\nTG Port "
1917 "Primary Access Status: %s\nTG Port Secondary Access"
1918 " State: %s\nTG Port Secondary Access Status: %s\n",
1919 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1920 core_alua_dump_state(atomic_read(
1921 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1922 core_alua_dump_status(
1923 tg_pt_gp->tg_pt_gp_alua_access_status),
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001924 atomic_read(&lun->lun_tg_pt_secondary_offline) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001925 "Offline" : "None",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001926 core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001927 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001928 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001929
1930 return len;
1931}
1932
1933ssize_t core_alua_store_tg_pt_gp_info(
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001934 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001935 const char *page,
1936 size_t count)
1937{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001938 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -07001939 /*
1940 * rcu_dereference_raw protected by se_lun->lun_group symlink
1941 * reference to se_device->dev_group.
1942 */
1943 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001944 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001945 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1946 int move = 0;
1947
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001948 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
1949 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1950 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001951
1952 if (count > TG_PT_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001953 pr_err("ALUA Target Port Group alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954 return -EINVAL;
1955 }
1956 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1957 memcpy(buf, page, count);
1958 /*
1959 * Any ALUA target port group alias besides "NULL" means we will be
1960 * making a new group association.
1961 */
1962 if (strcmp(strstrip(buf), "NULL")) {
1963 /*
1964 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1965 * struct t10_alua_tg_pt_gp. This reference is released with
1966 * core_alua_put_tg_pt_gp_from_name() below.
1967 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001968 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969 strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001970 if (!tg_pt_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001971 return -ENODEV;
1972 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001973
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001974 spin_lock(&lun->lun_tg_pt_gp_lock);
1975 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001976 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001977 /*
1978 * Clearing an existing tg_pt_gp association, and replacing
1979 * with the default_tg_pt_gp.
1980 */
Andy Grover6708bb22011-06-08 10:36:43 -07001981 if (!tg_pt_gp_new) {
1982 pr_debug("Target_Core_ConfigFS: Moving"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001983 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1984 " alua/%s, ID: %hu back to"
1985 " default_tg_pt_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001986 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1987 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001988 config_item_name(&lun->lun_group.cg_item),
1989 config_item_name(
1990 &tg_pt_gp->tg_pt_gp_group.cg_item),
1991 tg_pt_gp->tg_pt_gp_id);
1992
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001993 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1994 __target_attach_tg_pt_gp(lun,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001995 dev->t10_alua.default_tg_pt_gp);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001996 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001997
1998 return count;
1999 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002000 __target_detach_tg_pt_gp(lun, tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002001 move = 1;
2002 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002003
2004 __target_attach_tg_pt_gp(lun, tg_pt_gp_new);
2005 spin_unlock(&lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002006 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002007 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002008 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2009 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002010 config_item_name(&lun->lun_group.cg_item),
2011 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
2012 tg_pt_gp_new->tg_pt_gp_id);
2013
2014 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
2015 return count;
2016}
2017
2018ssize_t core_alua_show_access_type(
2019 struct t10_alua_tg_pt_gp *tg_pt_gp,
2020 char *page)
2021{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002022 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
2023 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
2024 return sprintf(page, "Implicit and Explicit\n");
2025 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
2026 return sprintf(page, "Implicit\n");
2027 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
2028 return sprintf(page, "Explicit\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002029 else
2030 return sprintf(page, "None\n");
2031}
2032
2033ssize_t core_alua_store_access_type(
2034 struct t10_alua_tg_pt_gp *tg_pt_gp,
2035 const char *page,
2036 size_t count)
2037{
2038 unsigned long tmp;
2039 int ret;
2040
Jingoo Han57103d72013-07-19 16:22:19 +09002041 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002042 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002043 pr_err("Unable to extract alua_access_type\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002044 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002045 }
2046 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002047 pr_err("Illegal value for alua_access_type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002048 " %lu\n", tmp);
2049 return -EINVAL;
2050 }
2051 if (tmp == 3)
2052 tg_pt_gp->tg_pt_gp_alua_access_type =
Hannes Reinecke125d0112013-11-19 09:07:46 +01002053 TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002054 else if (tmp == 2)
Hannes Reinecke125d0112013-11-19 09:07:46 +01002055 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002056 else if (tmp == 1)
Hannes Reinecke125d0112013-11-19 09:07:46 +01002057 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002058 else
2059 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2060
2061 return count;
2062}
2063
2064ssize_t core_alua_show_nonop_delay_msecs(
2065 struct t10_alua_tg_pt_gp *tg_pt_gp,
2066 char *page)
2067{
2068 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2069}
2070
2071ssize_t core_alua_store_nonop_delay_msecs(
2072 struct t10_alua_tg_pt_gp *tg_pt_gp,
2073 const char *page,
2074 size_t count)
2075{
2076 unsigned long tmp;
2077 int ret;
2078
Jingoo Han57103d72013-07-19 16:22:19 +09002079 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002080 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002081 pr_err("Unable to extract nonop_delay_msecs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002082 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002083 }
2084 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07002085 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002086 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2087 ALUA_MAX_NONOP_DELAY_MSECS);
2088 return -EINVAL;
2089 }
2090 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2091
2092 return count;
2093}
2094
2095ssize_t core_alua_show_trans_delay_msecs(
2096 struct t10_alua_tg_pt_gp *tg_pt_gp,
2097 char *page)
2098{
2099 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2100}
2101
2102ssize_t core_alua_store_trans_delay_msecs(
2103 struct t10_alua_tg_pt_gp *tg_pt_gp,
2104 const char *page,
2105 size_t count)
2106{
2107 unsigned long tmp;
2108 int ret;
2109
Jingoo Han57103d72013-07-19 16:22:19 +09002110 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002111 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002112 pr_err("Unable to extract trans_delay_msecs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002113 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002114 }
2115 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07002116 pr_err("Passed trans_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002117 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2118 ALUA_MAX_TRANS_DELAY_MSECS);
2119 return -EINVAL;
2120 }
2121 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2122
2123 return count;
2124}
2125
Hannes Reinecke125d0112013-11-19 09:07:46 +01002126ssize_t core_alua_show_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002127 struct t10_alua_tg_pt_gp *tg_pt_gp,
2128 char *page)
2129{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002130 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002131}
2132
Hannes Reinecke125d0112013-11-19 09:07:46 +01002133ssize_t core_alua_store_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002134 struct t10_alua_tg_pt_gp *tg_pt_gp,
2135 const char *page,
2136 size_t count)
2137{
2138 unsigned long tmp;
2139 int ret;
2140
Jingoo Han57103d72013-07-19 16:22:19 +09002141 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002142 if (ret < 0) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002143 pr_err("Unable to extract implicit_trans_secs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002144 return ret;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002145 }
Hannes Reinecke125d0112013-11-19 09:07:46 +01002146 if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2147 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2148 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2149 ALUA_MAX_IMPLICIT_TRANS_SECS);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002150 return -EINVAL;
2151 }
Hannes Reinecke125d0112013-11-19 09:07:46 +01002152 tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002153
2154 return count;
2155}
2156
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002157ssize_t core_alua_show_preferred_bit(
2158 struct t10_alua_tg_pt_gp *tg_pt_gp,
2159 char *page)
2160{
2161 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2162}
2163
2164ssize_t core_alua_store_preferred_bit(
2165 struct t10_alua_tg_pt_gp *tg_pt_gp,
2166 const char *page,
2167 size_t count)
2168{
2169 unsigned long tmp;
2170 int ret;
2171
Jingoo Han57103d72013-07-19 16:22:19 +09002172 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002173 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002174 pr_err("Unable to extract preferred ALUA value\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002175 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002176 }
2177 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002178 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002179 return -EINVAL;
2180 }
2181 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2182
2183 return count;
2184}
2185
2186ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2187{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002188 return sprintf(page, "%d\n",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002189 atomic_read(&lun->lun_tg_pt_secondary_offline));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002190}
2191
2192ssize_t core_alua_store_offline_bit(
2193 struct se_lun *lun,
2194 const char *page,
2195 size_t count)
2196{
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -07002197 /*
2198 * rcu_dereference_raw protected by se_lun->lun_group symlink
2199 * reference to se_device->dev_group.
2200 */
2201 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002202 unsigned long tmp;
2203 int ret;
2204
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002205 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
2206 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002207 return -ENODEV;
2208
Jingoo Han57103d72013-07-19 16:22:19 +09002209 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002210 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002211 pr_err("Unable to extract alua_tg_pt_offline value\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002212 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002213 }
2214 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002215 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002216 tmp);
2217 return -EINVAL;
2218 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002219
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002220 ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002221 if (ret < 0)
2222 return -EINVAL;
2223
2224 return count;
2225}
2226
2227ssize_t core_alua_show_secondary_status(
2228 struct se_lun *lun,
2229 char *page)
2230{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002231 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002232}
2233
2234ssize_t core_alua_store_secondary_status(
2235 struct se_lun *lun,
2236 const char *page,
2237 size_t count)
2238{
2239 unsigned long tmp;
2240 int ret;
2241
Jingoo Han57103d72013-07-19 16:22:19 +09002242 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002243 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002244 pr_err("Unable to extract alua_tg_pt_status\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002245 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002246 }
2247 if ((tmp != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002248 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2249 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002250 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002251 tmp);
2252 return -EINVAL;
2253 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002254 lun->lun_tg_pt_secondary_stat = (int)tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002255
2256 return count;
2257}
2258
2259ssize_t core_alua_show_secondary_write_metadata(
2260 struct se_lun *lun,
2261 char *page)
2262{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002263 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002264}
2265
2266ssize_t core_alua_store_secondary_write_metadata(
2267 struct se_lun *lun,
2268 const char *page,
2269 size_t count)
2270{
2271 unsigned long tmp;
2272 int ret;
2273
Jingoo Han57103d72013-07-19 16:22:19 +09002274 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002275 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002276 pr_err("Unable to extract alua_tg_pt_write_md\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002277 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002278 }
2279 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002280 pr_err("Illegal value for alua_tg_pt_write_md:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002281 " %lu\n", tmp);
2282 return -EINVAL;
2283 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002284 lun->lun_tg_pt_secondary_write_md = (int)tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002285
2286 return count;
2287}
2288
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002289int core_setup_alua(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002290{
Andy Grovera3541702015-05-19 14:44:41 -07002291 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04002292 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2293 struct t10_alua_lu_gp_member *lu_gp_mem;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002294
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002295 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002296 * Associate this struct se_device with the default ALUA
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002297 * LUN Group.
2298 */
2299 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002300 if (IS_ERR(lu_gp_mem))
2301 return PTR_ERR(lu_gp_mem);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002302
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002303 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2304 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00002305 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002306 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2307
Andy Grover6708bb22011-06-08 10:36:43 -07002308 pr_debug("%s: Adding to default ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002309 " core/alua/lu_gps/default_lu_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002310 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002311 }
2312
2313 return 0;
2314}