blob: 4c82bbe19003d083979fac3139ab91e3a15a01a0 [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>
Bart Van Asscheba929992015-05-08 10:11:12 +020031#include <scsi/scsi_proto.h>
Roland Dreier33395fb2012-02-23 17:22:12 -080032#include <asm/unaligned.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080033
34#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050035#include <target/target_core_backend.h>
36#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080037
Christoph Hellwige26d99a2011-11-14 12:30:30 -050038#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080039#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080040#include "target_core_ua.h"
41
Hannes Reineckebb91c1a2013-12-17 09:18:43 +010042static sense_reason_t core_alua_check_transition(int state, int valid,
43 int *primary);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080044static int core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -070045 struct se_lun *lun, int explicit, int offline);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080046
Hannes Reineckebb91c1a2013-12-17 09:18:43 +010047static char *core_alua_dump_state(int state);
48
Christoph Hellwigadf653f2015-05-25 21:33:08 -070049static void __target_attach_tg_pt_gp(struct se_lun *lun,
50 struct t10_alua_tg_pt_gp *tg_pt_gp);
51
Andy Grovere3d6f902011-07-19 08:55:10 +000052static u16 alua_lu_gps_counter;
53static u32 alua_lu_gps_count;
54
55static DEFINE_SPINLOCK(lu_gps_lock);
56static LIST_HEAD(lu_gps_list);
57
58struct t10_alua_lu_gp *default_lu_gp;
59
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080060/*
Hannes Reineckec66094b2013-12-17 09:18:49 +010061 * REPORT REFERRALS
62 *
63 * See sbc3r35 section 5.23
64 */
65sense_reason_t
66target_emulate_report_referrals(struct se_cmd *cmd)
67{
68 struct se_device *dev = cmd->se_dev;
69 struct t10_alua_lba_map *map;
70 struct t10_alua_lba_map_member *map_mem;
71 unsigned char *buf;
72 u32 rd_len = 0, off;
73
74 if (cmd->data_length < 4) {
75 pr_warn("REPORT REFERRALS allocation length %u too"
76 " small\n", cmd->data_length);
77 return TCM_INVALID_CDB_FIELD;
78 }
79
80 buf = transport_kmap_data_sg(cmd);
81 if (!buf)
82 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
83
84 off = 4;
85 spin_lock(&dev->t10_alua.lba_map_lock);
86 if (list_empty(&dev->t10_alua.lba_map_list)) {
87 spin_unlock(&dev->t10_alua.lba_map_lock);
88 transport_kunmap_data_sg(cmd);
89
90 return TCM_UNSUPPORTED_SCSI_OPCODE;
91 }
92
93 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
94 lba_map_list) {
95 int desc_num = off + 3;
96 int pg_num;
97
98 off += 4;
Hannes Reinecke38edd722013-12-19 14:36:11 +010099 if (cmd->data_length > off)
100 put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100101 off += 8;
Hannes Reinecke38edd722013-12-19 14:36:11 +0100102 if (cmd->data_length > off)
103 put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100104 off += 8;
105 rd_len += 20;
106 pg_num = 0;
107 list_for_each_entry(map_mem, &map->lba_map_mem_list,
108 lba_map_mem_list) {
Hannes Reinecke38edd722013-12-19 14:36:11 +0100109 int alua_state = map_mem->lba_map_mem_alua_state;
110 int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;
111
112 if (cmd->data_length > off)
113 buf[off] = alua_state & 0x0f;
114 off += 2;
115 if (cmd->data_length > off)
116 buf[off] = (alua_pg_id >> 8) & 0xff;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100117 off++;
Hannes Reinecke38edd722013-12-19 14:36:11 +0100118 if (cmd->data_length > off)
119 buf[off] = (alua_pg_id & 0xff);
120 off++;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100121 rd_len += 4;
122 pg_num++;
123 }
Hannes Reinecke38edd722013-12-19 14:36:11 +0100124 if (cmd->data_length > desc_num)
125 buf[desc_num] = pg_num;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100126 }
127 spin_unlock(&dev->t10_alua.lba_map_lock);
128
129 /*
130 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
131 */
132 put_unaligned_be16(rd_len, &buf[2]);
133
134 transport_kunmap_data_sg(cmd);
135
136 target_complete_cmd(cmd, GOOD);
137 return 0;
138}
139
140/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800141 * REPORT_TARGET_PORT_GROUPS
142 *
143 * See spc4r17 section 6.27
144 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800145sense_reason_t
146target_emulate_report_target_port_groups(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800147{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400148 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800149 struct t10_alua_tg_pt_gp *tg_pt_gp;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700150 struct se_lun *lun;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000151 unsigned char *buf;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700152 u32 rd_len = 0, off;
153 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800154
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700155 /*
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700156 * Skip over RESERVED area to first Target port group descriptor
157 * depending on the PARAMETER DATA FORMAT type..
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700158 */
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700159 if (ext_hdr != 0)
160 off = 8;
161 else
162 off = 4;
163
164 if (cmd->data_length < off) {
165 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
166 " small for %s header\n", cmd->data_length,
167 (ext_hdr) ? "extended" : "normal");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800168 return TCM_INVALID_CDB_FIELD;
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700169 }
Andy Grover49493142012-01-16 16:57:08 -0800170 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800171 if (!buf)
172 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000173
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400174 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
175 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800176 tg_pt_gp_list) {
177 /*
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -0700178 * Check if the Target port group and Target port descriptor list
179 * based on tg_pt_gp_members count will fit into the response payload.
180 * Otherwise, bump rd_len to let the initiator know we have exceeded
181 * the allocation length and the response is truncated.
182 */
183 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
184 cmd->data_length) {
185 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
186 continue;
187 }
188 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800189 * PREF: Preferred target port bit, determine if this
190 * bit should be set for port group.
191 */
192 if (tg_pt_gp->tg_pt_gp_pref)
193 buf[off] = 0x80;
194 /*
195 * Set the ASYMMETRIC ACCESS State
196 */
197 buf[off++] |= (atomic_read(
198 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
199 /*
200 * Set supported ASYMMETRIC ACCESS State bits
201 */
Hannes Reineckec0dc9412013-11-19 09:07:49 +0100202 buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800203 /*
204 * TARGET PORT GROUP
205 */
206 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
207 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
208
209 off++; /* Skip over Reserved */
210 /*
211 * STATUS CODE
212 */
213 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
214 /*
215 * Vendor Specific field
216 */
217 buf[off++] = 0x00;
218 /*
219 * TARGET PORT COUNT
220 */
221 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
222 rd_len += 8;
223
224 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700225 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
226 lun_tg_pt_gp_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800227 /*
228 * Start Target Port descriptor format
229 *
230 * See spc4r17 section 6.2.7 Table 247
231 */
232 off += 2; /* Skip over Obsolete */
233 /*
234 * Set RELATIVE TARGET PORT IDENTIFIER
235 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700236 buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
237 buf[off++] = (lun->lun_rtpi & 0xff);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800238 rd_len += 4;
239 }
240 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
241 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400242 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243 /*
244 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
245 */
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700246 put_unaligned_be32(rd_len, &buf[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800247
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700248 /*
249 * Fill in the Extended header parameter data format if requested
250 */
251 if (ext_hdr != 0) {
252 buf[4] = 0x10;
253 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100254 * Set the implicit transition time (in seconds) for the application
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700255 * client to use as a base for it's transition timeout value.
256 *
257 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
258 * this CDB was received upon to determine this value individually
259 * for ALUA target port group.
260 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700261 spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
262 tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
263 if (tg_pt_gp)
264 buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
265 spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700266 }
Andy Grover49493142012-01-16 16:57:08 -0800267 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000268
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400269 target_complete_cmd(cmd, GOOD);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800270 return 0;
271}
272
273/*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100274 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800275 *
276 * See spc4r17 section 6.35
277 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800278sense_reason_t
279target_emulate_set_target_port_groups(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800280{
Andy Grover5951146d2011-07-19 10:26:37 +0000281 struct se_device *dev = cmd->se_dev;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700282 struct se_lun *l_lun = cmd->se_lun;
Andy Grovere3d6f902011-07-19 08:55:10 +0000283 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000285 unsigned char *buf;
286 unsigned char *ptr;
Hannes Reineckea0d50f62012-12-17 09:53:34 +0100287 sense_reason_t rc = TCM_NO_SENSE;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800288 u32 len = 4; /* Skip over RESERVED area in header */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100289 int alua_access_state, primary = 0, valid_states;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800290 u16 tg_pt_id, rtpi;
291
Paolo Bonzini0d7f1292012-09-07 17:30:33 +0200292 if (cmd->data_length < 4) {
293 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
294 " small\n", cmd->data_length);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800295 return TCM_INVALID_PARAMETER_LIST;
Paolo Bonzini0d7f1292012-09-07 17:30:33 +0200296 }
297
Andy Grover49493142012-01-16 16:57:08 -0800298 buf = transport_kmap_data_sg(cmd);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800299 if (!buf)
300 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000301
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800302 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100303 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800304 * for the local tg_pt_gp.
305 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700306 spin_lock(&l_lun->lun_tg_pt_gp_lock);
307 l_tg_pt_gp = l_lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -0700308 if (!l_tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700309 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
310 pr_err("Unable to access l_lun->tg_pt_gp\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800311 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000312 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800313 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800314
Hannes Reinecke125d0112013-11-19 09:07:46 +0100315 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700316 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700317 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
Hannes Reinecke125d0112013-11-19 09:07:46 +0100318 " while TPGS_EXPLICIT_ALUA is disabled\n");
Christoph Hellwigde103c92012-11-06 12:24:09 -0800319 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000320 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800321 }
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100322 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700323 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800324
Andy Grover05d1c7c2011-07-20 19:13:28 +0000325 ptr = &buf[4]; /* Skip over RESERVED area in header */
326
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800327 while (len < cmd->data_length) {
Christoph Hellwigde103c92012-11-06 12:24:09 -0800328 bool found = false;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800329 alua_access_state = (ptr[0] & 0x0f);
330 /*
331 * Check the received ALUA access state, and determine if
332 * the state is a primary or secondary target port asymmetric
333 * access state.
334 */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100335 rc = core_alua_check_transition(alua_access_state,
336 valid_states, &primary);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800337 if (rc) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 /*
339 * If the SET TARGET PORT GROUPS attempts to establish
340 * an invalid combination of target port asymmetric
341 * access states or attempts to establish an
342 * unsupported target port asymmetric access state,
343 * then the command shall be terminated with CHECK
344 * CONDITION status, with the sense key set to ILLEGAL
345 * REQUEST, and the additional sense code set to INVALID
346 * FIELD IN PARAMETER LIST.
347 */
Andy Grover05d1c7c2011-07-20 19:13:28 +0000348 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800349 }
Christoph Hellwigde103c92012-11-06 12:24:09 -0800350
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800351 /*
352 * If the ASYMMETRIC ACCESS STATE field (see table 267)
353 * specifies a primary target port asymmetric access state,
354 * then the TARGET PORT GROUP OR TARGET PORT field specifies
355 * a primary target port group for which the primary target
356 * port asymmetric access state shall be changed. If the
357 * ASYMMETRIC ACCESS STATE field specifies a secondary target
358 * port asymmetric access state, then the TARGET PORT GROUP OR
359 * TARGET PORT field specifies the relative target port
360 * identifier (see 3.1.120) of the target port for which the
361 * secondary target port asymmetric access state shall be
362 * changed.
363 */
364 if (primary) {
Roland Dreier33395fb2012-02-23 17:22:12 -0800365 tg_pt_id = get_unaligned_be16(ptr + 2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800366 /*
367 * Locate the matching target port group ID from
368 * the global tg_pt_gp list
369 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400370 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800371 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400372 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800373 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700374 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375 continue;
376
377 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
378 continue;
379
Joern Engel33940d02014-09-16 16:23:12 -0400380 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800381
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400382 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800383
Christoph Hellwigde103c92012-11-06 12:24:09 -0800384 if (!core_alua_do_port_transition(tg_pt_gp,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700385 dev, l_lun, nacl,
Christoph Hellwigde103c92012-11-06 12:24:09 -0800386 alua_access_state, 1))
387 found = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800388
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400389 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Joern Engel33940d02014-09-16 16:23:12 -0400390 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800391 break;
392 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400393 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800394 } else {
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700395 struct se_lun *lun;
396
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800397 /*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100398 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800399 * the Target Port in question for the the incoming
400 * SET_TARGET_PORT_GROUPS op.
401 */
Roland Dreier33395fb2012-02-23 17:22:12 -0800402 rtpi = get_unaligned_be16(ptr + 2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800403 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +0900404 * Locate the matching relative target port identifier
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800405 * for the struct se_device storage object.
406 */
407 spin_lock(&dev->se_port_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700408 list_for_each_entry(lun, &dev->dev_sep_list,
409 lun_dev_link) {
410 if (lun->lun_rtpi != rtpi)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800411 continue;
412
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700413 // XXX: racy unlock
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800414 spin_unlock(&dev->se_port_lock);
415
Christoph Hellwigde103c92012-11-06 12:24:09 -0800416 if (!core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700417 lun, 1, 1))
Christoph Hellwigde103c92012-11-06 12:24:09 -0800418 found = true;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800419
420 spin_lock(&dev->se_port_lock);
421 break;
422 }
423 spin_unlock(&dev->se_port_lock);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800424 }
425
426 if (!found) {
427 rc = TCM_INVALID_PARAMETER_LIST;
428 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800429 }
430
431 ptr += 4;
432 len += 4;
433 }
434
Andy Grover05d1c7c2011-07-20 19:13:28 +0000435out:
Andy Grover49493142012-01-16 16:57:08 -0800436 transport_kunmap_data_sg(cmd);
Roland Dreier59e4f542012-06-04 23:24:51 -0700437 if (!rc)
438 target_complete_cmd(cmd, GOOD);
439 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800440}
441
Paul Bolle1e3ab992014-02-20 09:07:52 +0100442static inline void set_ascq(struct se_cmd *cmd, u8 alua_ascq)
443{
444 /*
445 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
446 * The ALUA additional sense code qualifier (ASCQ) is determined
447 * by the ALUA primary or secondary access state..
448 */
449 pr_debug("[%s]: ALUA TG Port not available, "
450 "SenseKey: NOT_READY, ASC/ASCQ: "
451 "0x04/0x%02x\n",
452 cmd->se_tfo->get_fabric_name(), alua_ascq);
453
454 cmd->scsi_asc = 0x04;
455 cmd->scsi_ascq = alua_ascq;
456}
457
458static inline void core_alua_state_nonoptimized(
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800459 struct se_cmd *cmd,
460 unsigned char *cdb,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100461 int nonop_delay_msecs)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800462{
463 /*
464 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
465 * later to determine if processing of this cmd needs to be
466 * temporarily delayed for the Active/NonOptimized primary access state.
467 */
468 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
469 cmd->alua_nonop_delay = nonop_delay_msecs;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800470}
471
Hannes Reineckec66094b2013-12-17 09:18:49 +0100472static inline int core_alua_state_lba_dependent(
473 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100474 struct t10_alua_tg_pt_gp *tg_pt_gp)
Hannes Reineckec66094b2013-12-17 09:18:49 +0100475{
476 struct se_device *dev = cmd->se_dev;
477 u64 segment_size, segment_mult, sectors, lba;
478
479 /* Only need to check for cdb actually containing LBAs */
480 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
481 return 0;
482
483 spin_lock(&dev->t10_alua.lba_map_lock);
484 segment_size = dev->t10_alua.lba_map_segment_size;
485 segment_mult = dev->t10_alua.lba_map_segment_multiplier;
486 sectors = cmd->data_length / dev->dev_attrib.block_size;
487
488 lba = cmd->t_task_lba;
489 while (lba < cmd->t_task_lba + sectors) {
490 struct t10_alua_lba_map *cur_map = NULL, *map;
491 struct t10_alua_lba_map_member *map_mem;
492
493 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
494 lba_map_list) {
495 u64 start_lba, last_lba;
496 u64 first_lba = map->lba_map_first_lba;
497
498 if (segment_mult) {
499 u64 tmp = lba;
Nicholas Bellingercdf55942014-01-30 14:05:16 -0800500 start_lba = do_div(tmp, segment_size * segment_mult);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100501
502 last_lba = first_lba + segment_size - 1;
503 if (start_lba >= first_lba &&
504 start_lba <= last_lba) {
505 lba += segment_size;
506 cur_map = map;
507 break;
508 }
509 } else {
510 last_lba = map->lba_map_last_lba;
511 if (lba >= first_lba && lba <= last_lba) {
512 lba = last_lba + 1;
513 cur_map = map;
514 break;
515 }
516 }
517 }
518 if (!cur_map) {
519 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100520 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100521 return 1;
522 }
523 list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
524 lba_map_mem_list) {
525 if (map_mem->lba_map_mem_alua_pg_id !=
526 tg_pt_gp->tg_pt_gp_id)
527 continue;
528 switch(map_mem->lba_map_mem_alua_state) {
529 case ALUA_ACCESS_STATE_STANDBY:
530 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100531 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100532 return 1;
533 case ALUA_ACCESS_STATE_UNAVAILABLE:
534 spin_unlock(&dev->t10_alua.lba_map_lock);
Paul Bolle1e3ab992014-02-20 09:07:52 +0100535 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Hannes Reineckec66094b2013-12-17 09:18:49 +0100536 return 1;
537 default:
538 break;
539 }
540 }
541 }
542 spin_unlock(&dev->t10_alua.lba_map_lock);
543 return 0;
544}
545
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800546static inline int core_alua_state_standby(
547 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100548 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800549{
550 /*
551 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
552 * spc4r17 section 5.9.2.4.4
553 */
554 switch (cdb[0]) {
555 case INQUIRY:
556 case LOG_SELECT:
557 case LOG_SENSE:
558 case MODE_SELECT:
559 case MODE_SENSE:
560 case REPORT_LUNS:
561 case RECEIVE_DIAGNOSTIC:
562 case SEND_DIAGNOSTIC:
Nicholas Bellingere7810c22014-06-05 23:37:00 -0700563 case READ_CAPACITY:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700564 return 0;
Hannes Reineckeeb846d92014-11-17 14:25:19 +0100565 case SERVICE_ACTION_IN_16:
Nicholas Bellingere7810c22014-06-05 23:37:00 -0700566 switch (cdb[1] & 0x1f) {
567 case SAI_READ_CAPACITY_16:
568 return 0;
569 default:
570 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
571 return 1;
572 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800573 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700574 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800575 case MI_REPORT_TARGET_PGS:
576 return 0;
577 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100578 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800579 return 1;
580 }
581 case MAINTENANCE_OUT:
582 switch (cdb[1]) {
583 case MO_SET_TARGET_PGS:
584 return 0;
585 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100586 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800587 return 1;
588 }
589 case REQUEST_SENSE:
590 case PERSISTENT_RESERVE_IN:
591 case PERSISTENT_RESERVE_OUT:
592 case READ_BUFFER:
593 case WRITE_BUFFER:
594 return 0;
595 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100596 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800597 return 1;
598 }
599
600 return 0;
601}
602
603static inline int core_alua_state_unavailable(
604 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100605 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800606{
607 /*
608 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
609 * spc4r17 section 5.9.2.4.5
610 */
611 switch (cdb[0]) {
612 case INQUIRY:
613 case REPORT_LUNS:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700614 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800615 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700616 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800617 case MI_REPORT_TARGET_PGS:
618 return 0;
619 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100620 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800621 return 1;
622 }
623 case MAINTENANCE_OUT:
624 switch (cdb[1]) {
625 case MO_SET_TARGET_PGS:
626 return 0;
627 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100628 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800629 return 1;
630 }
631 case REQUEST_SENSE:
632 case READ_BUFFER:
633 case WRITE_BUFFER:
634 return 0;
635 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100636 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800637 return 1;
638 }
639
640 return 0;
641}
642
643static inline int core_alua_state_transition(
644 struct se_cmd *cmd,
Paul Bolle1e3ab992014-02-20 09:07:52 +0100645 unsigned char *cdb)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800646{
647 /*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100648 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800649 * spc4r17 section 5.9.2.5
650 */
651 switch (cdb[0]) {
652 case INQUIRY:
653 case REPORT_LUNS:
Nicholas Bellinger30f359a2013-04-10 15:00:27 -0700654 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800655 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700656 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800657 case MI_REPORT_TARGET_PGS:
658 return 0;
659 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100660 set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800661 return 1;
662 }
663 case REQUEST_SENSE:
664 case READ_BUFFER:
665 case WRITE_BUFFER:
666 return 0;
667 default:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100668 set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800669 return 1;
670 }
671
672 return 0;
673}
674
675/*
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100676 * return 1: Is used to signal LUN not accessible, and check condition/not ready
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800677 * return 0: Used to signal success
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100678 * return -1: Used to signal failure, and invalid cdb field
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800679 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800680sense_reason_t
681target_alua_state_check(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800682{
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400683 struct se_device *dev = cmd->se_dev;
684 unsigned char *cdb = cmd->t_task_cdb;
Andy Grovere3d6f902011-07-19 08:55:10 +0000685 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800686 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800687 int out_alua_state, nonop_delay_msecs;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400688
689 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
690 return 0;
Andy Grovera3541702015-05-19 14:44:41 -0700691 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH)
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400692 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800693
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800694 /*
695 * First, check for a struct se_port specific secondary ALUA target port
696 * access state: OFFLINE
697 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700698 if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700699 pr_debug("ALUA: Got secondary offline status for local"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800700 " target port\n");
Paul Bolle1e3ab992014-02-20 09:07:52 +0100701 set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE);
702 return TCM_CHECK_CONDITION_NOT_READY;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800703 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700704
705 if (!lun->lun_tg_pt_gp)
Nicholas Bellinger89c12cc2013-08-22 15:35:45 -0700706 return 0;
707
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700708 spin_lock(&lun->lun_tg_pt_gp_lock);
709 tg_pt_gp = lun->lun_tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800710 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
711 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700712
713 // XXX: keeps using tg_pt_gp witout reference after unlock
714 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800715 /*
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100716 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300717 * statement so the compiler knows explicitly to check this case first.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800718 * For the Optimized ALUA access state case, we want to process the
719 * incoming fabric cmd ASAP..
720 */
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100721 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800722 return 0;
723
724 switch (out_alua_state) {
725 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100726 core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400727 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800728 case ALUA_ACCESS_STATE_STANDBY:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100729 if (core_alua_state_standby(cmd, cdb))
730 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400731 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800732 case ALUA_ACCESS_STATE_UNAVAILABLE:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100733 if (core_alua_state_unavailable(cmd, cdb))
734 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400735 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800736 case ALUA_ACCESS_STATE_TRANSITION:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100737 if (core_alua_state_transition(cmd, cdb))
738 return TCM_CHECK_CONDITION_NOT_READY;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400739 break;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100740 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
Paul Bolle1e3ab992014-02-20 09:07:52 +0100741 if (core_alua_state_lba_dependent(cmd, tg_pt_gp))
742 return TCM_CHECK_CONDITION_NOT_READY;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100743 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800744 /*
745 * OFFLINE is a secondary ALUA target port group access state, that is
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700746 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800747 */
748 case ALUA_ACCESS_STATE_OFFLINE:
749 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700750 pr_err("Unknown ALUA access state: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751 out_alua_state);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800752 return TCM_INVALID_CDB_FIELD;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800753 }
754
Christoph Hellwigde103c92012-11-06 12:24:09 -0800755 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800756}
757
758/*
Hannes Reinecke125d0112013-11-19 09:07:46 +0100759 * Check implicit and explicit ALUA state change request.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800760 */
Christoph Hellwigde103c92012-11-06 12:24:09 -0800761static sense_reason_t
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100762core_alua_check_transition(int state, int valid, int *primary)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800763{
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100764 /*
765 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
766 * defined as primary target port asymmetric access states.
767 */
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800768 switch (state) {
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100769 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100770 if (!(valid & ALUA_AO_SUP))
771 goto not_supported;
772 *primary = 1;
773 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800774 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100775 if (!(valid & ALUA_AN_SUP))
776 goto not_supported;
777 *primary = 1;
778 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800779 case ALUA_ACCESS_STATE_STANDBY:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100780 if (!(valid & ALUA_S_SUP))
781 goto not_supported;
782 *primary = 1;
783 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800784 case ALUA_ACCESS_STATE_UNAVAILABLE:
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100785 if (!(valid & ALUA_U_SUP))
786 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800787 *primary = 1;
788 break;
Hannes Reineckec66094b2013-12-17 09:18:49 +0100789 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
790 if (!(valid & ALUA_LBD_SUP))
791 goto not_supported;
792 *primary = 1;
793 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800794 case ALUA_ACCESS_STATE_OFFLINE:
795 /*
796 * OFFLINE state is defined as a secondary target port
797 * asymmetric access state.
798 */
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100799 if (!(valid & ALUA_O_SUP))
800 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800801 *primary = 0;
802 break;
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100803 case ALUA_ACCESS_STATE_TRANSITION:
804 /*
805 * Transitioning is set internally, and
806 * cannot be selected manually.
807 */
808 goto not_supported;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800809 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700810 pr_err("Unknown ALUA access state: 0x%02x\n", state);
Christoph Hellwigde103c92012-11-06 12:24:09 -0800811 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800812 }
813
814 return 0;
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100815
816not_supported:
817 pr_err("ALUA access state %s not supported",
818 core_alua_dump_state(state));
819 return TCM_INVALID_PARAMETER_LIST;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800820}
821
822static char *core_alua_dump_state(int state)
823{
824 switch (state) {
Hannes Reinecke73f3bf52013-11-19 09:07:48 +0100825 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800826 return "Active/Optimized";
827 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
828 return "Active/NonOptimized";
Hannes Reineckec66094b2013-12-17 09:18:49 +0100829 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
830 return "LBA Dependent";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800831 case ALUA_ACCESS_STATE_STANDBY:
832 return "Standby";
833 case ALUA_ACCESS_STATE_UNAVAILABLE:
834 return "Unavailable";
835 case ALUA_ACCESS_STATE_OFFLINE:
836 return "Offline";
Hannes Reineckebb91c1a2013-12-17 09:18:43 +0100837 case ALUA_ACCESS_STATE_TRANSITION:
838 return "Transitioning";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800839 default:
840 return "Unknown";
841 }
842
843 return NULL;
844}
845
846char *core_alua_dump_status(int status)
847{
848 switch (status) {
849 case ALUA_STATUS_NONE:
850 return "None";
Hannes Reinecke125d0112013-11-19 09:07:46 +0100851 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
852 return "Altered by Explicit STPG";
853 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
854 return "Altered by Implicit ALUA";
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800855 default:
856 return "Unknown";
857 }
858
859 return NULL;
860}
861
862/*
863 * Used by fabric modules to determine when we need to delay processing
864 * for the Active/NonOptimized paths..
865 */
866int core_alua_check_nonop_delay(
867 struct se_cmd *cmd)
868{
869 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
870 return 0;
871 if (in_interrupt())
872 return 0;
873 /*
874 * The ALUA Active/NonOptimized access state delay can be disabled
875 * in via configfs with a value of zero
876 */
Andy Grover6708bb22011-06-08 10:36:43 -0700877 if (!cmd->alua_nonop_delay)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800878 return 0;
879 /*
880 * struct se_cmd->alua_nonop_delay gets set by a target port group
881 * defined interval in core_alua_state_nonoptimized()
882 */
883 msleep_interruptible(cmd->alua_nonop_delay);
884 return 0;
885}
886EXPORT_SYMBOL(core_alua_check_nonop_delay);
887
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800888static int core_alua_write_tpg_metadata(
889 const char *path,
890 unsigned char *md_buf,
891 u32 md_buf_len)
892{
Al Viro0e9b10a2013-02-23 15:22:43 -0500893 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
894 int ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800895
Al Viro0e9b10a2013-02-23 15:22:43 -0500896 if (IS_ERR(file)) {
897 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800898 return -ENODEV;
899 }
Al Viro0e9b10a2013-02-23 15:22:43 -0500900 ret = kernel_write(file, md_buf, md_buf_len, 0);
901 if (ret < 0)
Andy Grover6708bb22011-06-08 10:36:43 -0700902 pr_err("Error writing ALUA metadata file: %s\n", path);
Al Viro0e9b10a2013-02-23 15:22:43 -0500903 fput(file);
Gera Kazakovf730f912013-09-09 15:47:06 -0700904 return (ret < 0) ? -EIO : 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800905}
906
907/*
908 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
909 */
910static int core_alua_update_tpg_primary_metadata(
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100911 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800912{
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100913 unsigned char *md_buf;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400914 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800915 char path[ALUA_METADATA_PATH_LEN];
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100916 int len, rc;
917
918 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
919 if (!md_buf) {
920 pr_err("Unable to allocate buf for ALUA metadata\n");
921 return -ENOMEM;
922 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800923
924 memset(path, 0, ALUA_METADATA_PATH_LEN);
925
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100926 len = snprintf(md_buf, ALUA_MD_BUF_LEN,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800927 "tg_pt_gp_id=%hu\n"
928 "alua_access_state=0x%02x\n"
929 "alua_access_status=0x%02x\n",
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100930 tg_pt_gp->tg_pt_gp_id,
931 tg_pt_gp->tg_pt_gp_alua_pending_state,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800932 tg_pt_gp->tg_pt_gp_alua_access_status);
933
934 snprintf(path, ALUA_METADATA_PATH_LEN,
Lee Duncanfdddf932016-04-14 18:18:51 -0700935 "%s/alua/tpgs_%s/%s", db_root, &wwn->unit_serial[0],
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800936 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
937
Hannes Reinecke1e0b9402013-12-17 09:18:44 +0100938 rc = core_alua_write_tpg_metadata(path, md_buf, len);
939 kfree(md_buf);
940 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800941}
942
Hannes Reineckeee8c07d2015-06-11 10:01:24 +0200943static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800944{
945 struct se_dev_entry *se_deve;
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700946 struct se_lun *lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800947 struct se_lun_acl *lacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800948
949 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700950 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
951 lun_tg_pt_gp_link) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800952 /*
953 * After an implicit target port asymmetric access state
954 * change, a device server shall establish a unit attention
955 * condition for the initiator port associated with every I_T
956 * nexus with the additional sense code set to ASYMMETRIC
Hannes Reineckef1ae05d2013-11-19 09:07:47 +0100957 * ACCESS STATE CHANGED.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800958 *
959 * After an explicit target port asymmetric access state
960 * change, a device server shall establish a unit attention
961 * condition with the additional sense code set to ASYMMETRIC
962 * ACCESS STATE CHANGED for the initiator port associated with
963 * every I_T nexus other than the I_T nexus on which the SET
964 * TARGET PORT GROUPS command
965 */
Nicholas Bellinger9e37d042015-05-20 21:21:08 -0700966 if (!percpu_ref_tryget_live(&lun->lun_ref))
967 continue;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800968 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
969
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -0700970 spin_lock(&lun->lun_deve_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700971 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
Nicholas Bellinger29a05de2015-03-22 20:42:19 -0700972 lacl = rcu_dereference_check(se_deve->se_lun_acl,
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700973 lockdep_is_held(&lun->lun_deve_lock));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800974
Hannes Reineckec51c8e72015-06-11 10:01:26 +0200975 /*
976 * spc4r37 p.242:
977 * After an explicit target port asymmetric access
978 * state change, a device server shall establish a
979 * unit attention condition with the additional sense
980 * code set to ASYMMETRIC ACCESS STATE CHANGED for
981 * the initiator port associated with every I_T nexus
982 * other than the I_T nexus on which the SET TARGET
983 * PORT GROUPS command was received.
984 */
Hannes Reinecke9c6e1642013-12-17 09:18:46 +0100985 if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
986 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
Christoph Hellwigadf653f2015-05-25 21:33:08 -0700987 (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
988 (tg_pt_gp->tg_pt_gp_alua_lun == lun))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800989 continue;
990
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800991 /*
992 * se_deve->se_lun_acl pointer may be NULL for a
993 * entry created without explicit Node+MappedLUN ACLs
994 */
Hannes Reineckec51c8e72015-06-11 10:01:26 +0200995 if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
996 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800997 continue;
998
Hannes Reineckec51c8e72015-06-11 10:01:26 +0200999 core_scsi3_ua_allocate(se_deve, 0x2A,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001000 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
1001 }
Nicholas Bellinger1adff1b2015-06-22 23:44:05 -07001002 spin_unlock(&lun->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001003
1004 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Nicholas Bellinger9e37d042015-05-20 21:21:08 -07001005 percpu_ref_put(&lun->lun_ref);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001006 }
1007 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
Hannes Reineckeee8c07d2015-06-11 10:01:24 +02001008}
1009
1010static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
1011{
1012 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work,
1013 struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work.work);
1014 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1015 bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
1016 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
1017
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001018 /*
1019 * Update the ALUA metadata buf that has been allocated in
1020 * core_alua_do_port_transition(), this metadata will be written
1021 * to struct file.
1022 *
1023 * Note that there is the case where we do not want to update the
1024 * metadata when the saved metadata is being parsed in userspace
1025 * when setting the existing port access state and access status.
1026 *
1027 * Also note that the failure to write out the ALUA metadata to
1028 * struct file does NOT affect the actual ALUA transition.
1029 */
1030 if (tg_pt_gp->tg_pt_gp_write_metadata) {
1031 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001032 core_alua_update_tpg_primary_metadata(tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001033 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
1034 }
1035 /*
1036 * Set the current primary ALUA access state to the requested new state
1037 */
Hannes Reineckedfbce752013-12-17 09:18:45 +01001038 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1039 tg_pt_gp->tg_pt_gp_alua_pending_state);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001040
Andy Grover6708bb22011-06-08 10:36:43 -07001041 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Hannes Reinecke125d0112013-11-19 09:07:46 +01001042 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1043 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
Hannes Reineckedfbce752013-12-17 09:18:45 +01001044 tg_pt_gp->tg_pt_gp_id,
1045 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state),
1046 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
Hannes Reineckeee8c07d2015-06-11 10:01:24 +02001047
1048 core_alua_queue_state_change_ua(tg_pt_gp);
1049
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001050 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1051 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001052 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1053
1054 if (tg_pt_gp->tg_pt_gp_transition_complete)
1055 complete(tg_pt_gp->tg_pt_gp_transition_complete);
1056}
1057
1058static int core_alua_do_transition_tg_pt(
1059 struct t10_alua_tg_pt_gp *tg_pt_gp,
1060 int new_state,
1061 int explicit)
1062{
1063 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1064 DECLARE_COMPLETION_ONSTACK(wait);
1065
1066 /* Nothing to be done here */
1067 if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state)
1068 return 0;
1069
1070 if (new_state == ALUA_ACCESS_STATE_TRANSITION)
1071 return -EAGAIN;
1072
1073 /*
1074 * Flush any pending transitions
1075 */
1076 if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs &&
1077 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) ==
1078 ALUA_ACCESS_STATE_TRANSITION) {
1079 /* Just in case */
1080 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1081 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1082 flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
1083 wait_for_completion(&wait);
1084 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1085 return 0;
1086 }
1087
1088 /*
1089 * Save the old primary ALUA access state, and set the current state
1090 * to ALUA_ACCESS_STATE_TRANSITION.
1091 */
1092 tg_pt_gp->tg_pt_gp_alua_previous_state =
1093 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
1094 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1095
1096 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1097 ALUA_ACCESS_STATE_TRANSITION);
1098 tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1099 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1100 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1101
Hannes Reineckeee8c07d2015-06-11 10:01:24 +02001102 core_alua_queue_state_change_ua(tg_pt_gp);
1103
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001104 /*
1105 * Check for the optional ALUA primary state transition delay
1106 */
1107 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
1108 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1109
1110 /*
1111 * Take a reference for workqueue item
1112 */
1113 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1114 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001115 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1116
1117 if (!explicit && tg_pt_gp->tg_pt_gp_implicit_trans_secs) {
1118 unsigned long transition_tmo;
1119
1120 transition_tmo = tg_pt_gp->tg_pt_gp_implicit_trans_secs * HZ;
1121 queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
1122 &tg_pt_gp->tg_pt_gp_transition_work,
1123 transition_tmo);
1124 } else {
1125 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
1126 queue_delayed_work(tg_pt_gp->tg_pt_gp_dev->tmr_wq,
1127 &tg_pt_gp->tg_pt_gp_transition_work, 0);
1128 wait_for_completion(&wait);
1129 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1130 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001131
1132 return 0;
1133}
1134
1135int core_alua_do_port_transition(
1136 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1137 struct se_device *l_dev,
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001138 struct se_lun *l_lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139 struct se_node_acl *l_nacl,
1140 int new_state,
Hannes Reinecke125d0112013-11-19 09:07:46 +01001141 int explicit)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001142{
1143 struct se_device *dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001144 struct t10_alua_lu_gp *lu_gp;
1145 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1146 struct t10_alua_tg_pt_gp *tg_pt_gp;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001147 int primary, valid_states, rc = 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001148
Hannes Reineckebb91c1a2013-12-17 09:18:43 +01001149 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1150 if (core_alua_check_transition(new_state, valid_states, &primary) != 0)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001151 return -EINVAL;
1152
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001153 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1154 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1155 lu_gp = local_lu_gp_mem->lu_gp;
1156 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1158 /*
1159 * For storage objects that are members of the 'default_lu_gp',
1160 * we only do transition on the passed *l_tp_pt_gp, and not
1161 * on all of the matching target port groups IDs in default_lu_gp.
1162 */
Andy Grover6708bb22011-06-08 10:36:43 -07001163 if (!lu_gp->lu_gp_id) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164 /*
1165 * core_alua_do_transition_tg_pt() will always return
1166 * success.
1167 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001168 l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001169 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1170 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1171 new_state, explicit);
Joern Engel33940d02014-09-16 16:23:12 -04001172 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001173 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001174 }
1175 /*
1176 * For all other LU groups aside from 'default_lu_gp', walk all of
1177 * the associated storage objects looking for a matching target port
1178 * group ID from the local target port group.
1179 */
1180 spin_lock(&lu_gp->lu_gp_lock);
1181 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1182 lu_gp_mem_list) {
1183
1184 dev = lu_gp_mem->lu_gp_mem_dev;
Joern Engel33940d02014-09-16 16:23:12 -04001185 atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001186 spin_unlock(&lu_gp->lu_gp_lock);
1187
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001188 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001190 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001191 tg_pt_gp_list) {
1192
Andy Grover6708bb22011-06-08 10:36:43 -07001193 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001194 continue;
1195 /*
1196 * If the target behavior port asymmetric access state
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001197 * is changed for any target port group accessible via
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001198 * a logical unit within a LU group, the target port
1199 * behavior group asymmetric access states for the same
1200 * target port group accessible via other logical units
1201 * in that LU group will also change.
1202 */
1203 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1204 continue;
1205
1206 if (l_tg_pt_gp == tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001207 tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001208 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001209 } else {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001210 tg_pt_gp->tg_pt_gp_alua_lun = NULL;
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001211 tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001212 }
Joern Engel33940d02014-09-16 16:23:12 -04001213 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001214 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001215 /*
1216 * core_alua_do_transition_tg_pt() will always return
1217 * success.
1218 */
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001219 rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1220 new_state, explicit);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001221
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001222 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Joern Engel33940d02014-09-16 16:23:12 -04001223 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001224 if (rc)
1225 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001226 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001227 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001228
1229 spin_lock(&lu_gp->lu_gp_lock);
Joern Engel33940d02014-09-16 16:23:12 -04001230 atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001231 }
1232 spin_unlock(&lu_gp->lu_gp_lock);
1233
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001234 if (!rc) {
1235 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1236 " Group IDs: %hu %s transition to primary state: %s\n",
1237 config_item_name(&lu_gp->lu_gp_group.cg_item),
1238 l_tg_pt_gp->tg_pt_gp_id,
1239 (explicit) ? "explicit" : "implicit",
1240 core_alua_dump_state(new_state));
1241 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001242
Joern Engel33940d02014-09-16 16:23:12 -04001243 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001244 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001245}
1246
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001247static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001248{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001249 struct se_portal_group *se_tpg = lun->lun_tpg;
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001250 unsigned char *md_buf;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001251 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001252 int len, rc;
1253
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001254 mutex_lock(&lun->lun_tg_pt_md_mutex);
1255
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001256 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1257 if (!md_buf) {
1258 pr_err("Unable to allocate buf for ALUA metadata\n");
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001259 rc = -ENOMEM;
1260 goto out_unlock;
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001261 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001262
1263 memset(path, 0, ALUA_METADATA_PATH_LEN);
1264 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1265
1266 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +00001267 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001268
Andy Grovere3d6f902011-07-19 08:55:10 +00001269 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001270 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
Andy Grovere3d6f902011-07-19 08:55:10 +00001271 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001272
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001273 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 "alua_tg_pt_status=0x%02x\n",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001275 atomic_read(&lun->lun_tg_pt_secondary_offline),
1276 lun->lun_tg_pt_secondary_stat);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001277
Lee Duncanfdddf932016-04-14 18:18:51 -07001278 snprintf(path, ALUA_METADATA_PATH_LEN, "%s/alua/%s/%s/lun_%llu",
1279 db_root, se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001280 lun->unpacked_lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001281
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001282 rc = core_alua_write_tpg_metadata(path, md_buf, len);
1283 kfree(md_buf);
1284
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001285out_unlock:
1286 mutex_unlock(&lun->lun_tg_pt_md_mutex);
Hannes Reinecke1e0b9402013-12-17 09:18:44 +01001287 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001288}
1289
1290static int core_alua_set_tg_pt_secondary_state(
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001291 struct se_lun *lun,
Hannes Reinecke125d0112013-11-19 09:07:46 +01001292 int explicit,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001293 int offline)
1294{
1295 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001296 int trans_delay_msecs;
1297
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001298 spin_lock(&lun->lun_tg_pt_gp_lock);
1299 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001300 if (!tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001301 spin_unlock(&lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001302 pr_err("Unable to complete secondary state"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001303 " transition\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001304 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001305 }
1306 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1307 /*
1308 * Set the secondary ALUA target port access state to OFFLINE
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001309 * or release the previously secondary state for struct se_lun
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001310 */
1311 if (offline)
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001312 atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001313 else
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001314 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001315
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001316 lun->lun_tg_pt_secondary_stat = (explicit) ?
Hannes Reinecke125d0112013-11-19 09:07:46 +01001317 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1318 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001319
Andy Grover6708bb22011-06-08 10:36:43 -07001320 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Hannes Reinecke125d0112013-11-19 09:07:46 +01001321 " to secondary access state: %s\n", (explicit) ? "explicit" :
1322 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001323 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1324
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001325 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001326 /*
1327 * Do the optional transition delay after we set the secondary
1328 * ALUA access state.
1329 */
1330 if (trans_delay_msecs != 0)
1331 msleep_interruptible(trans_delay_msecs);
1332 /*
1333 * See if we need to update the ALUA fabric port metadata for
1334 * secondary state and status
1335 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001336 if (lun->lun_tg_pt_secondary_write_md)
1337 core_alua_update_tpg_secondary_metadata(lun);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001338
1339 return 0;
1340}
1341
Hannes Reinecke229d4f12013-12-17 09:18:50 +01001342struct t10_alua_lba_map *
1343core_alua_allocate_lba_map(struct list_head *list,
1344 u64 first_lba, u64 last_lba)
1345{
1346 struct t10_alua_lba_map *lba_map;
1347
1348 lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1349 if (!lba_map) {
1350 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1351 return ERR_PTR(-ENOMEM);
1352 }
1353 INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1354 lba_map->lba_map_first_lba = first_lba;
1355 lba_map->lba_map_last_lba = last_lba;
1356
1357 list_add_tail(&lba_map->lba_map_list, list);
1358 return lba_map;
1359}
1360
1361int
1362core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1363 int pg_id, int state)
1364{
1365 struct t10_alua_lba_map_member *lba_map_mem;
1366
1367 list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1368 lba_map_mem_list) {
1369 if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1370 pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1371 return -EINVAL;
1372 }
1373 }
1374
1375 lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1376 if (!lba_map_mem) {
1377 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1378 return -ENOMEM;
1379 }
1380 lba_map_mem->lba_map_mem_alua_state = state;
1381 lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1382
1383 list_add_tail(&lba_map_mem->lba_map_mem_list,
1384 &lba_map->lba_map_mem_list);
1385 return 0;
1386}
1387
1388void
1389core_alua_free_lba_map(struct list_head *lba_list)
1390{
1391 struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1392 struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1393
1394 list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1395 lba_map_list) {
1396 list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1397 &lba_map->lba_map_mem_list,
1398 lba_map_mem_list) {
1399 list_del(&lba_map_mem->lba_map_mem_list);
1400 kmem_cache_free(t10_alua_lba_map_mem_cache,
1401 lba_map_mem);
1402 }
1403 list_del(&lba_map->lba_map_list);
1404 kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1405 }
1406}
1407
1408void
1409core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1410 int segment_size, int segment_mult)
1411{
1412 struct list_head old_lba_map_list;
1413 struct t10_alua_tg_pt_gp *tg_pt_gp;
1414 int activate = 0, supported;
1415
1416 INIT_LIST_HEAD(&old_lba_map_list);
1417 spin_lock(&dev->t10_alua.lba_map_lock);
1418 dev->t10_alua.lba_map_segment_size = segment_size;
1419 dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1420 list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1421 if (lba_map_list) {
1422 list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1423 activate = 1;
1424 }
1425 spin_unlock(&dev->t10_alua.lba_map_lock);
1426 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1427 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1428 tg_pt_gp_list) {
1429
1430 if (!tg_pt_gp->tg_pt_gp_valid_id)
1431 continue;
1432 supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1433 if (activate)
1434 supported |= ALUA_LBD_SUP;
1435 else
1436 supported &= ~ALUA_LBD_SUP;
1437 tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1438 }
1439 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1440 core_alua_free_lba_map(&old_lba_map_list);
1441}
1442
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443struct t10_alua_lu_gp *
1444core_alua_allocate_lu_gp(const char *name, int def_group)
1445{
1446 struct t10_alua_lu_gp *lu_gp;
1447
1448 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001449 if (!lu_gp) {
1450 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001451 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001453 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001454 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1455 spin_lock_init(&lu_gp->lu_gp_lock);
1456 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1457
1458 if (def_group) {
Andy Grovere3d6f902011-07-19 08:55:10 +00001459 lu_gp->lu_gp_id = alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001460 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001461 alua_lu_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001462 }
1463
1464 return lu_gp;
1465}
1466
1467int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1468{
1469 struct t10_alua_lu_gp *lu_gp_tmp;
1470 u16 lu_gp_id_tmp;
1471 /*
1472 * The lu_gp->lu_gp_id may only be set once..
1473 */
1474 if (lu_gp->lu_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001475 pr_warn("ALUA LU Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001476 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001477 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001478 }
1479
Andy Grovere3d6f902011-07-19 08:55:10 +00001480 spin_lock(&lu_gps_lock);
1481 if (alua_lu_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001482 pr_err("Maximum ALUA alua_lu_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483 " 0x0000ffff reached\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001484 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001485 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001486 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001487 }
1488again:
1489 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
Andy Grovere3d6f902011-07-19 08:55:10 +00001490 alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001491
Andy Grovere3d6f902011-07-19 08:55:10 +00001492 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001493 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001494 if (!lu_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001495 goto again;
1496
Andy Grover6708bb22011-06-08 10:36:43 -07001497 pr_warn("ALUA Logical Unit Group ID: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001498 " already exists, ignoring request\n",
1499 lu_gp_id);
Andy Grovere3d6f902011-07-19 08:55:10 +00001500 spin_unlock(&lu_gps_lock);
1501 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001502 }
1503 }
1504
1505 lu_gp->lu_gp_id = lu_gp_id_tmp;
1506 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001507 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1508 alua_lu_gps_count++;
1509 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001510
1511 return 0;
1512}
1513
1514static struct t10_alua_lu_gp_member *
1515core_alua_allocate_lu_gp_mem(struct se_device *dev)
1516{
1517 struct t10_alua_lu_gp_member *lu_gp_mem;
1518
1519 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001520 if (!lu_gp_mem) {
1521 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001522 return ERR_PTR(-ENOMEM);
1523 }
1524 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1525 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1526 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1527
1528 lu_gp_mem->lu_gp_mem_dev = dev;
1529 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1530
1531 return lu_gp_mem;
1532}
1533
1534void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1535{
1536 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1537 /*
1538 * Once we have reached this point, config_item_put() has
1539 * already been called from target_core_alua_drop_lu_gp().
1540 *
1541 * Here, we remove the *lu_gp from the global list so that
1542 * no associations can be made while we are releasing
1543 * struct t10_alua_lu_gp.
1544 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001545 spin_lock(&lu_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001546 list_del(&lu_gp->lu_gp_node);
1547 alua_lu_gps_count--;
1548 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001549 /*
1550 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1551 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1552 * released with core_alua_put_lu_gp_from_name()
1553 */
1554 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1555 cpu_relax();
1556 /*
1557 * Release reference to struct t10_alua_lu_gp * from all associated
1558 * struct se_device.
1559 */
1560 spin_lock(&lu_gp->lu_gp_lock);
1561 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1562 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1563 if (lu_gp_mem->lu_gp_assoc) {
1564 list_del(&lu_gp_mem->lu_gp_mem_list);
1565 lu_gp->lu_gp_members--;
1566 lu_gp_mem->lu_gp_assoc = 0;
1567 }
1568 spin_unlock(&lu_gp->lu_gp_lock);
1569 /*
1570 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001571 * lu_gp_mem is associated with a single
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001572 * struct se_device->dev_alua_lu_gp_mem, and is released when
1573 * struct se_device is released via core_alua_free_lu_gp_mem().
1574 *
1575 * If the passed lu_gp does NOT match the default_lu_gp, assume
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001576 * we want to re-associate a given lu_gp_mem with default_lu_gp.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001577 */
1578 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001579 if (lu_gp != default_lu_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001580 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00001581 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001582 else
1583 lu_gp_mem->lu_gp = NULL;
1584 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1585
1586 spin_lock(&lu_gp->lu_gp_lock);
1587 }
1588 spin_unlock(&lu_gp->lu_gp_lock);
1589
1590 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1591}
1592
1593void core_alua_free_lu_gp_mem(struct se_device *dev)
1594{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001595 struct t10_alua_lu_gp *lu_gp;
1596 struct t10_alua_lu_gp_member *lu_gp_mem;
1597
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001598 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -07001599 if (!lu_gp_mem)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 return;
1601
1602 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1603 cpu_relax();
1604
1605 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1606 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001607 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001608 spin_lock(&lu_gp->lu_gp_lock);
1609 if (lu_gp_mem->lu_gp_assoc) {
1610 list_del(&lu_gp_mem->lu_gp_mem_list);
1611 lu_gp->lu_gp_members--;
1612 lu_gp_mem->lu_gp_assoc = 0;
1613 }
1614 spin_unlock(&lu_gp->lu_gp_lock);
1615 lu_gp_mem->lu_gp = NULL;
1616 }
1617 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1618
1619 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1620}
1621
1622struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1623{
1624 struct t10_alua_lu_gp *lu_gp;
1625 struct config_item *ci;
1626
Andy Grovere3d6f902011-07-19 08:55:10 +00001627 spin_lock(&lu_gps_lock);
1628 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
Andy Grover6708bb22011-06-08 10:36:43 -07001629 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001630 continue;
1631 ci = &lu_gp->lu_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001632 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001633 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001634 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001635 return lu_gp;
1636 }
1637 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001638 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001639
1640 return NULL;
1641}
1642
1643void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1644{
Andy Grovere3d6f902011-07-19 08:55:10 +00001645 spin_lock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001646 atomic_dec(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001647 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001648}
1649
1650/*
1651 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1652 */
1653void __core_alua_attach_lu_gp_mem(
1654 struct t10_alua_lu_gp_member *lu_gp_mem,
1655 struct t10_alua_lu_gp *lu_gp)
1656{
1657 spin_lock(&lu_gp->lu_gp_lock);
1658 lu_gp_mem->lu_gp = lu_gp;
1659 lu_gp_mem->lu_gp_assoc = 1;
1660 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1661 lu_gp->lu_gp_members++;
1662 spin_unlock(&lu_gp->lu_gp_lock);
1663}
1664
1665/*
1666 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1667 */
1668void __core_alua_drop_lu_gp_mem(
1669 struct t10_alua_lu_gp_member *lu_gp_mem,
1670 struct t10_alua_lu_gp *lu_gp)
1671{
1672 spin_lock(&lu_gp->lu_gp_lock);
1673 list_del(&lu_gp_mem->lu_gp_mem_list);
1674 lu_gp_mem->lu_gp = NULL;
1675 lu_gp_mem->lu_gp_assoc = 0;
1676 lu_gp->lu_gp_members--;
1677 spin_unlock(&lu_gp->lu_gp_lock);
1678}
1679
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001680struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1681 const char *name, int def_group)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001682{
1683 struct t10_alua_tg_pt_gp *tg_pt_gp;
1684
1685 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001686 if (!tg_pt_gp) {
1687 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001688 return NULL;
1689 }
1690 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001691 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001692 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1693 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1694 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001695 INIT_DELAYED_WORK(&tg_pt_gp->tg_pt_gp_transition_work,
1696 core_alua_do_transition_tg_pt_work);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001697 tg_pt_gp->tg_pt_gp_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001698 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
Hannes Reinecke73f3bf52013-11-19 09:07:48 +01001699 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001700 /*
Hannes Reinecke125d0112013-11-19 09:07:46 +01001701 * Enable both explicit and implicit ALUA support by default
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001702 */
1703 tg_pt_gp->tg_pt_gp_alua_access_type =
Hannes Reinecke125d0112013-11-19 09:07:46 +01001704 TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705 /*
1706 * Set the default Active/NonOptimized Delay in milliseconds
1707 */
1708 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1709 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
Hannes Reinecke125d0112013-11-19 09:07:46 +01001710 tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001711
Hannes Reineckec0dc9412013-11-19 09:07:49 +01001712 /*
1713 * Enable all supported states
1714 */
1715 tg_pt_gp->tg_pt_gp_alua_supported_states =
1716 ALUA_T_SUP | ALUA_O_SUP |
1717 ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1718
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001719 if (def_group) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001720 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001721 tg_pt_gp->tg_pt_gp_id =
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001722 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001723 tg_pt_gp->tg_pt_gp_valid_id = 1;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001724 dev->t10_alua.alua_tg_pt_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001725 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001726 &dev->t10_alua.tg_pt_gps_list);
1727 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001728 }
1729
1730 return tg_pt_gp;
1731}
1732
1733int core_alua_set_tg_pt_gp_id(
1734 struct t10_alua_tg_pt_gp *tg_pt_gp,
1735 u16 tg_pt_gp_id)
1736{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001737 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001738 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1739 u16 tg_pt_gp_id_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001740
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001741 /*
1742 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1743 */
1744 if (tg_pt_gp->tg_pt_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001745 pr_warn("ALUA TG PT Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001746 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001747 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001748 }
1749
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001750 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1751 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001752 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001753 " 0x0000ffff reached\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001754 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001755 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001756 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001757 }
1758again:
1759 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001760 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001761
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001762 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001763 tg_pt_gp_list) {
1764 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001765 if (!tg_pt_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001766 goto again;
1767
Andy Grover6708bb22011-06-08 10:36:43 -07001768 pr_err("ALUA Target Port Group ID: %hu already"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001769 " exists, ignoring request\n", tg_pt_gp_id);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001770 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001771 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001772 }
1773 }
1774
1775 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1776 tg_pt_gp->tg_pt_gp_valid_id = 1;
1777 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001778 &dev->t10_alua.tg_pt_gps_list);
1779 dev->t10_alua.alua_tg_pt_gps_count++;
1780 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001781
1782 return 0;
1783}
1784
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001785void core_alua_free_tg_pt_gp(
1786 struct t10_alua_tg_pt_gp *tg_pt_gp)
1787{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001788 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001789 struct se_lun *lun, *next;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001790
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001791 /*
1792 * Once we have reached this point, config_item_put() has already
1793 * been called from target_core_alua_drop_tg_pt_gp().
1794 *
1795 * Here we remove *tg_pt_gp from the global list so that
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001796 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001797 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1798 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001799 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001800 list_del(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001801 dev->t10_alua.alua_tg_pt_gps_counter--;
1802 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1803
Hannes Reinecke9c6e1642013-12-17 09:18:46 +01001804 flush_delayed_work(&tg_pt_gp->tg_pt_gp_transition_work);
1805
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001806 /*
1807 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1808 * core_alua_get_tg_pt_gp_by_name() in
1809 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1810 * to be released with core_alua_put_tg_pt_gp_from_name().
1811 */
1812 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1813 cpu_relax();
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001814
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001815 /*
1816 * Release reference to struct t10_alua_tg_pt_gp from all associated
1817 * struct se_port.
1818 */
1819 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001820 list_for_each_entry_safe(lun, next,
1821 &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1822 list_del_init(&lun->lun_tg_pt_gp_link);
1823 tg_pt_gp->tg_pt_gp_members--;
1824
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001825 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1826 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001827 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
Hannes Reineckef1ae05d2013-11-19 09:07:47 +01001828 * assume we want to re-associate a given tg_pt_gp_mem with
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001829 * default_tg_pt_gp.
1830 */
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001831 spin_lock(&lun->lun_tg_pt_gp_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001832 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001833 __target_attach_tg_pt_gp(lun,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001834 dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001835 } else
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001836 lun->lun_tg_pt_gp = NULL;
1837 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001838
1839 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1840 }
1841 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1842
1843 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1844}
1845
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001846static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001847 struct se_device *dev, const char *name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001848{
1849 struct t10_alua_tg_pt_gp *tg_pt_gp;
1850 struct config_item *ci;
1851
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001852 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1853 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001855 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001856 continue;
1857 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001858 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001859 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001860 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001861 return tg_pt_gp;
1862 }
1863 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001864 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001865
1866 return NULL;
1867}
1868
1869static void core_alua_put_tg_pt_gp_from_name(
1870 struct t10_alua_tg_pt_gp *tg_pt_gp)
1871{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001872 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001873
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001874 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001875 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001876 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001877}
1878
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001879static void __target_attach_tg_pt_gp(struct se_lun *lun,
1880 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001881{
Hannes Reinecke3dd348f2015-06-11 10:01:27 +02001882 struct se_dev_entry *se_deve;
1883
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001884 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1885
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001886 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001887 lun->lun_tg_pt_gp = tg_pt_gp;
1888 list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001889 tg_pt_gp->tg_pt_gp_members++;
Hannes Reinecke3dd348f2015-06-11 10:01:27 +02001890 spin_lock(&lun->lun_deve_lock);
1891 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
1892 core_scsi3_ua_allocate(se_deve, 0x3f,
1893 ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED);
1894 spin_unlock(&lun->lun_deve_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001895 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1896}
1897
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001898void target_attach_tg_pt_gp(struct se_lun *lun,
1899 struct t10_alua_tg_pt_gp *tg_pt_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001900{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001901 spin_lock(&lun->lun_tg_pt_gp_lock);
1902 __target_attach_tg_pt_gp(lun, tg_pt_gp);
1903 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001904}
1905
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001906static void __target_detach_tg_pt_gp(struct se_lun *lun,
1907 struct t10_alua_tg_pt_gp *tg_pt_gp)
1908{
1909 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1910
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001911 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001912 list_del_init(&lun->lun_tg_pt_gp_link);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001913 tg_pt_gp->tg_pt_gp_members--;
1914 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001915
1916 lun->lun_tg_pt_gp = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001917}
1918
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001919void target_detach_tg_pt_gp(struct se_lun *lun)
1920{
1921 struct t10_alua_tg_pt_gp *tg_pt_gp;
1922
1923 spin_lock(&lun->lun_tg_pt_gp_lock);
1924 tg_pt_gp = lun->lun_tg_pt_gp;
1925 if (tg_pt_gp)
1926 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1927 spin_unlock(&lun->lun_tg_pt_gp_lock);
1928}
1929
1930ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001931{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001932 struct config_item *tg_pt_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001933 struct t10_alua_tg_pt_gp *tg_pt_gp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001934 ssize_t len = 0;
1935
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001936 spin_lock(&lun->lun_tg_pt_gp_lock);
1937 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001938 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001939 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1940 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1941 " %hu\nTG Port Primary Access State: %s\nTG Port "
1942 "Primary Access Status: %s\nTG Port Secondary Access"
1943 " State: %s\nTG Port Secondary Access Status: %s\n",
1944 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1945 core_alua_dump_state(atomic_read(
1946 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1947 core_alua_dump_status(
1948 tg_pt_gp->tg_pt_gp_alua_access_status),
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001949 atomic_read(&lun->lun_tg_pt_secondary_offline) ?
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001950 "Offline" : "None",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001951 core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001952 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001953 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001954
1955 return len;
1956}
1957
1958ssize_t core_alua_store_tg_pt_gp_info(
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001959 struct se_lun *lun,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001960 const char *page,
1961 size_t count)
1962{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001963 struct se_portal_group *tpg = lun->lun_tpg;
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -07001964 /*
1965 * rcu_dereference_raw protected by se_lun->lun_group symlink
1966 * reference to se_device->dev_group.
1967 */
1968 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001969 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001970 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1971 int move = 0;
1972
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001973 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
1974 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1975 return -ENODEV;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001976
1977 if (count > TG_PT_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001978 pr_err("ALUA Target Port Group alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001979 return -EINVAL;
1980 }
1981 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1982 memcpy(buf, page, count);
1983 /*
1984 * Any ALUA target port group alias besides "NULL" means we will be
1985 * making a new group association.
1986 */
1987 if (strcmp(strstrip(buf), "NULL")) {
1988 /*
1989 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1990 * struct t10_alua_tg_pt_gp. This reference is released with
1991 * core_alua_put_tg_pt_gp_from_name() below.
1992 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001993 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001994 strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001995 if (!tg_pt_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001996 return -ENODEV;
1997 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001998
Christoph Hellwigadf653f2015-05-25 21:33:08 -07001999 spin_lock(&lun->lun_tg_pt_gp_lock);
2000 tg_pt_gp = lun->lun_tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07002001 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002002 /*
2003 * Clearing an existing tg_pt_gp association, and replacing
2004 * with the default_tg_pt_gp.
2005 */
Andy Grover6708bb22011-06-08 10:36:43 -07002006 if (!tg_pt_gp_new) {
2007 pr_debug("Target_Core_ConfigFS: Moving"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002008 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
2009 " alua/%s, ID: %hu back to"
2010 " default_tg_pt_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002011 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2012 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002013 config_item_name(&lun->lun_group.cg_item),
2014 config_item_name(
2015 &tg_pt_gp->tg_pt_gp_group.cg_item),
2016 tg_pt_gp->tg_pt_gp_id);
2017
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002018 __target_detach_tg_pt_gp(lun, tg_pt_gp);
2019 __target_attach_tg_pt_gp(lun,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002020 dev->t10_alua.default_tg_pt_gp);
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002021 spin_unlock(&lun->lun_tg_pt_gp_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002022
2023 return count;
2024 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002025 __target_detach_tg_pt_gp(lun, tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002026 move = 1;
2027 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002028
2029 __target_attach_tg_pt_gp(lun, tg_pt_gp_new);
2030 spin_unlock(&lun->lun_tg_pt_gp_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07002031 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002032 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00002033 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2034 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002035 config_item_name(&lun->lun_group.cg_item),
2036 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
2037 tg_pt_gp_new->tg_pt_gp_id);
2038
2039 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
2040 return count;
2041}
2042
2043ssize_t core_alua_show_access_type(
2044 struct t10_alua_tg_pt_gp *tg_pt_gp,
2045 char *page)
2046{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002047 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
2048 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
2049 return sprintf(page, "Implicit and Explicit\n");
2050 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
2051 return sprintf(page, "Implicit\n");
2052 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
2053 return sprintf(page, "Explicit\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002054 else
2055 return sprintf(page, "None\n");
2056}
2057
2058ssize_t core_alua_store_access_type(
2059 struct t10_alua_tg_pt_gp *tg_pt_gp,
2060 const char *page,
2061 size_t count)
2062{
2063 unsigned long tmp;
2064 int ret;
2065
Jingoo Han57103d72013-07-19 16:22:19 +09002066 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002067 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002068 pr_err("Unable to extract alua_access_type\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002069 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002070 }
2071 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002072 pr_err("Illegal value for alua_access_type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002073 " %lu\n", tmp);
2074 return -EINVAL;
2075 }
2076 if (tmp == 3)
2077 tg_pt_gp->tg_pt_gp_alua_access_type =
Hannes Reinecke125d0112013-11-19 09:07:46 +01002078 TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002079 else if (tmp == 2)
Hannes Reinecke125d0112013-11-19 09:07:46 +01002080 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002081 else if (tmp == 1)
Hannes Reinecke125d0112013-11-19 09:07:46 +01002082 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002083 else
2084 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2085
2086 return count;
2087}
2088
2089ssize_t core_alua_show_nonop_delay_msecs(
2090 struct t10_alua_tg_pt_gp *tg_pt_gp,
2091 char *page)
2092{
2093 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2094}
2095
2096ssize_t core_alua_store_nonop_delay_msecs(
2097 struct t10_alua_tg_pt_gp *tg_pt_gp,
2098 const char *page,
2099 size_t count)
2100{
2101 unsigned long tmp;
2102 int ret;
2103
Jingoo Han57103d72013-07-19 16:22:19 +09002104 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002105 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002106 pr_err("Unable to extract nonop_delay_msecs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002107 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002108 }
2109 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07002110 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002111 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2112 ALUA_MAX_NONOP_DELAY_MSECS);
2113 return -EINVAL;
2114 }
2115 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2116
2117 return count;
2118}
2119
2120ssize_t core_alua_show_trans_delay_msecs(
2121 struct t10_alua_tg_pt_gp *tg_pt_gp,
2122 char *page)
2123{
2124 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2125}
2126
2127ssize_t core_alua_store_trans_delay_msecs(
2128 struct t10_alua_tg_pt_gp *tg_pt_gp,
2129 const char *page,
2130 size_t count)
2131{
2132 unsigned long tmp;
2133 int ret;
2134
Jingoo Han57103d72013-07-19 16:22:19 +09002135 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002136 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002137 pr_err("Unable to extract trans_delay_msecs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002138 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002139 }
2140 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07002141 pr_err("Passed trans_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002142 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2143 ALUA_MAX_TRANS_DELAY_MSECS);
2144 return -EINVAL;
2145 }
2146 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2147
2148 return count;
2149}
2150
Hannes Reinecke125d0112013-11-19 09:07:46 +01002151ssize_t core_alua_show_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002152 struct t10_alua_tg_pt_gp *tg_pt_gp,
2153 char *page)
2154{
Hannes Reinecke125d0112013-11-19 09:07:46 +01002155 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002156}
2157
Hannes Reinecke125d0112013-11-19 09:07:46 +01002158ssize_t core_alua_store_implicit_trans_secs(
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002159 struct t10_alua_tg_pt_gp *tg_pt_gp,
2160 const char *page,
2161 size_t count)
2162{
2163 unsigned long tmp;
2164 int ret;
2165
Jingoo Han57103d72013-07-19 16:22:19 +09002166 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002167 if (ret < 0) {
Hannes Reinecke125d0112013-11-19 09:07:46 +01002168 pr_err("Unable to extract implicit_trans_secs\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002169 return ret;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002170 }
Hannes Reinecke125d0112013-11-19 09:07:46 +01002171 if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2172 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2173 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2174 ALUA_MAX_IMPLICIT_TRANS_SECS);
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002175 return -EINVAL;
2176 }
Hannes Reinecke125d0112013-11-19 09:07:46 +01002177 tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07002178
2179 return count;
2180}
2181
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002182ssize_t core_alua_show_preferred_bit(
2183 struct t10_alua_tg_pt_gp *tg_pt_gp,
2184 char *page)
2185{
2186 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2187}
2188
2189ssize_t core_alua_store_preferred_bit(
2190 struct t10_alua_tg_pt_gp *tg_pt_gp,
2191 const char *page,
2192 size_t count)
2193{
2194 unsigned long tmp;
2195 int ret;
2196
Jingoo Han57103d72013-07-19 16:22:19 +09002197 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002198 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002199 pr_err("Unable to extract preferred ALUA value\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002200 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002201 }
2202 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002203 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002204 return -EINVAL;
2205 }
2206 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2207
2208 return count;
2209}
2210
2211ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2212{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002213 return sprintf(page, "%d\n",
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002214 atomic_read(&lun->lun_tg_pt_secondary_offline));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002215}
2216
2217ssize_t core_alua_store_offline_bit(
2218 struct se_lun *lun,
2219 const char *page,
2220 size_t count)
2221{
Nicholas Bellinger4cc987e2015-05-19 00:03:07 -07002222 /*
2223 * rcu_dereference_raw protected by se_lun->lun_group symlink
2224 * reference to se_device->dev_group.
2225 */
2226 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002227 unsigned long tmp;
2228 int ret;
2229
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002230 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH ||
2231 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002232 return -ENODEV;
2233
Jingoo Han57103d72013-07-19 16:22:19 +09002234 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002235 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002236 pr_err("Unable to extract alua_tg_pt_offline value\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002237 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002238 }
2239 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002240 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002241 tmp);
2242 return -EINVAL;
2243 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002244
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002245 ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002246 if (ret < 0)
2247 return -EINVAL;
2248
2249 return count;
2250}
2251
2252ssize_t core_alua_show_secondary_status(
2253 struct se_lun *lun,
2254 char *page)
2255{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002256 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002257}
2258
2259ssize_t core_alua_store_secondary_status(
2260 struct se_lun *lun,
2261 const char *page,
2262 size_t count)
2263{
2264 unsigned long tmp;
2265 int ret;
2266
Jingoo Han57103d72013-07-19 16:22:19 +09002267 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002268 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002269 pr_err("Unable to extract alua_tg_pt_status\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002270 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002271 }
2272 if ((tmp != ALUA_STATUS_NONE) &&
Hannes Reinecke125d0112013-11-19 09:07:46 +01002273 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2274 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002275 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002276 tmp);
2277 return -EINVAL;
2278 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002279 lun->lun_tg_pt_secondary_stat = (int)tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002280
2281 return count;
2282}
2283
2284ssize_t core_alua_show_secondary_write_metadata(
2285 struct se_lun *lun,
2286 char *page)
2287{
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002288 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002289}
2290
2291ssize_t core_alua_store_secondary_write_metadata(
2292 struct se_lun *lun,
2293 const char *page,
2294 size_t count)
2295{
2296 unsigned long tmp;
2297 int ret;
2298
Jingoo Han57103d72013-07-19 16:22:19 +09002299 ret = kstrtoul(page, 0, &tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002300 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002301 pr_err("Unable to extract alua_tg_pt_write_md\n");
Jingoo Han57103d72013-07-19 16:22:19 +09002302 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002303 }
2304 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002305 pr_err("Illegal value for alua_tg_pt_write_md:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002306 " %lu\n", tmp);
2307 return -EINVAL;
2308 }
Christoph Hellwigadf653f2015-05-25 21:33:08 -07002309 lun->lun_tg_pt_secondary_write_md = (int)tmp;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002310
2311 return count;
2312}
2313
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002314int core_setup_alua(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002315{
Andy Grovera3541702015-05-19 14:44:41 -07002316 if (!(dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH) &&
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04002317 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2318 struct t10_alua_lu_gp_member *lu_gp_mem;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002319
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002320 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002321 * Associate this struct se_device with the default ALUA
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002322 * LUN Group.
2323 */
2324 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002325 if (IS_ERR(lu_gp_mem))
2326 return PTR_ERR(lu_gp_mem);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002327
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002328 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2329 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00002330 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002331 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2332
Andy Grover6708bb22011-06-08 10:36:43 -07002333 pr_debug("%s: Adding to default ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002334 " core/alua/lu_gps/default_lu_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002335 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002336 }
2337
2338 return 0;
2339}