blob: b3302a9200a27fc4c4049e0ecf8916e3de401d63 [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 *
6 * Copyright (c) 2009-2010 Rising Tide Systems
7 * Copyright (c) 2009-2010 Linux-iSCSI.org
8 *
9 * Nicholas A. Bellinger <nab@kernel.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 *
25 ******************************************************************************/
26
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080027#include <linux/slab.h>
28#include <linux/spinlock.h>
29#include <linux/configfs.h>
Paul Gortmakerc53181a2011-08-30 18:16:43 -040030#include <linux/export.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080031#include <scsi/scsi.h>
32#include <scsi/scsi_cmnd.h>
Roland Dreier33395fb2012-02-23 17:22:12 -080033#include <asm/unaligned.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080034
35#include <target/target_core_base.h>
Christoph Hellwigc4795fb2011-11-16 09:46:48 -050036#include <target/target_core_backend.h>
37#include <target/target_core_fabric.h>
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080038#include <target/target_core_configfs.h>
39
Christoph Hellwige26d99a2011-11-14 12:30:30 -050040#include "target_core_internal.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080041#include "target_core_alua.h"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080042#include "target_core_ua.h"
43
44static int core_alua_check_transition(int state, int *primary);
45static int core_alua_set_tg_pt_secondary_state(
46 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
47 struct se_port *port, int explict, int offline);
48
Andy Grovere3d6f902011-07-19 08:55:10 +000049static u16 alua_lu_gps_counter;
50static u32 alua_lu_gps_count;
51
52static DEFINE_SPINLOCK(lu_gps_lock);
53static LIST_HEAD(lu_gps_list);
54
55struct t10_alua_lu_gp *default_lu_gp;
56
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080057/*
58 * REPORT_TARGET_PORT_GROUPS
59 *
60 * See spc4r17 section 6.27
61 */
Christoph Hellwig6bb35e02012-04-23 11:35:33 -040062int target_emulate_report_target_port_groups(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080063{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040064 struct se_device *dev = cmd->se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080065 struct se_port *port;
66 struct t10_alua_tg_pt_gp *tg_pt_gp;
67 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
Andy Grover05d1c7c2011-07-20 19:13:28 +000068 unsigned char *buf;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -070069 u32 rd_len = 0, off;
70 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -070071 /*
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -070072 * Skip over RESERVED area to first Target port group descriptor
73 * depending on the PARAMETER DATA FORMAT type..
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -070074 */
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -070075 if (ext_hdr != 0)
76 off = 8;
77 else
78 off = 4;
79
80 if (cmd->data_length < off) {
81 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
82 " small for %s header\n", cmd->data_length,
83 (ext_hdr) ? "extended" : "normal");
84 cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -070085 return -EINVAL;
86 }
Andy Grover49493142012-01-16 16:57:08 -080087 buf = transport_kmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +000088
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -040089 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
90 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -080091 tg_pt_gp_list) {
92 /*
Nicholas Bellinger6b20fa92011-10-18 23:48:04 -070093 * Check if the Target port group and Target port descriptor list
94 * based on tg_pt_gp_members count will fit into the response payload.
95 * Otherwise, bump rd_len to let the initiator know we have exceeded
96 * the allocation length and the response is truncated.
97 */
98 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
99 cmd->data_length) {
100 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
101 continue;
102 }
103 /*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800104 * PREF: Preferred target port bit, determine if this
105 * bit should be set for port group.
106 */
107 if (tg_pt_gp->tg_pt_gp_pref)
108 buf[off] = 0x80;
109 /*
110 * Set the ASYMMETRIC ACCESS State
111 */
112 buf[off++] |= (atomic_read(
113 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
114 /*
115 * Set supported ASYMMETRIC ACCESS State bits
116 */
117 buf[off] = 0x80; /* T_SUP */
118 buf[off] |= 0x40; /* O_SUP */
119 buf[off] |= 0x8; /* U_SUP */
120 buf[off] |= 0x4; /* S_SUP */
121 buf[off] |= 0x2; /* AN_SUP */
122 buf[off++] |= 0x1; /* AO_SUP */
123 /*
124 * TARGET PORT GROUP
125 */
126 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
127 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
128
129 off++; /* Skip over Reserved */
130 /*
131 * STATUS CODE
132 */
133 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
134 /*
135 * Vendor Specific field
136 */
137 buf[off++] = 0x00;
138 /*
139 * TARGET PORT COUNT
140 */
141 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
142 rd_len += 8;
143
144 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
145 list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
146 tg_pt_gp_mem_list) {
147 port = tg_pt_gp_mem->tg_pt;
148 /*
149 * Start Target Port descriptor format
150 *
151 * See spc4r17 section 6.2.7 Table 247
152 */
153 off += 2; /* Skip over Obsolete */
154 /*
155 * Set RELATIVE TARGET PORT IDENTIFIER
156 */
157 buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
158 buf[off++] = (port->sep_rtpi & 0xff);
159 rd_len += 4;
160 }
161 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
162 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400163 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800164 /*
165 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
166 */
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700167 put_unaligned_be32(rd_len, &buf[0]);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800168
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -0700169 /*
170 * Fill in the Extended header parameter data format if requested
171 */
172 if (ext_hdr != 0) {
173 buf[4] = 0x10;
174 /*
175 * Set the implict transition time (in seconds) for the application
176 * client to use as a base for it's transition timeout value.
177 *
178 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
179 * this CDB was received upon to determine this value individually
180 * for ALUA target port group.
181 */
182 port = cmd->se_lun->lun_sep;
183 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
184 if (tg_pt_gp_mem) {
185 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
186 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
187 if (tg_pt_gp)
188 buf[5] = tg_pt_gp->tg_pt_gp_implict_trans_secs;
189 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
190 }
191 }
Andy Grover49493142012-01-16 16:57:08 -0800192 transport_kunmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000193
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400194 target_complete_cmd(cmd, GOOD);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800195 return 0;
196}
197
198/*
199 * SET_TARGET_PORT_GROUPS for explict ALUA operation.
200 *
201 * See spc4r17 section 6.35
202 */
Christoph Hellwig6bb35e02012-04-23 11:35:33 -0400203int target_emulate_set_target_port_groups(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800204{
Andy Grover5951146d2011-07-19 10:26:37 +0000205 struct se_device *dev = cmd->se_dev;
Andy Grovere3d6f902011-07-19 08:55:10 +0000206 struct se_port *port, *l_port = cmd->se_lun->lun_sep;
207 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800208 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
209 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000210 unsigned char *buf;
211 unsigned char *ptr;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800212 u32 len = 4; /* Skip over RESERVED area in header */
213 int alua_access_state, primary = 0, rc;
214 u16 tg_pt_id, rtpi;
215
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700216 if (!l_port) {
217 cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
218 return -EINVAL;
219 }
Paolo Bonzini0d7f1292012-09-07 17:30:33 +0200220 if (cmd->data_length < 4) {
221 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
222 " small\n", cmd->data_length);
223 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
224 return -EINVAL;
225 }
226
Andy Grover49493142012-01-16 16:57:08 -0800227 buf = transport_kmap_data_sg(cmd);
Andy Grover05d1c7c2011-07-20 19:13:28 +0000228
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800229 /*
230 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
231 * for the local tg_pt_gp.
232 */
233 l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -0700234 if (!l_tg_pt_gp_mem) {
235 pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700236 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
237 rc = -EINVAL;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000238 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800239 }
240 spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
241 l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -0700242 if (!l_tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800243 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
Andy Grover6708bb22011-06-08 10:36:43 -0700244 pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700245 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
246 rc = -EINVAL;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000247 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800248 }
249 rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
250 spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
251
Andy Grover6708bb22011-06-08 10:36:43 -0700252 if (!rc) {
253 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800254 " while TPGS_EXPLICT_ALUA is disabled\n");
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700255 cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
256 rc = -EINVAL;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000257 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800258 }
259
Andy Grover05d1c7c2011-07-20 19:13:28 +0000260 ptr = &buf[4]; /* Skip over RESERVED area in header */
261
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800262 while (len < cmd->data_length) {
263 alua_access_state = (ptr[0] & 0x0f);
264 /*
265 * Check the received ALUA access state, and determine if
266 * the state is a primary or secondary target port asymmetric
267 * access state.
268 */
269 rc = core_alua_check_transition(alua_access_state, &primary);
270 if (rc != 0) {
271 /*
272 * If the SET TARGET PORT GROUPS attempts to establish
273 * an invalid combination of target port asymmetric
274 * access states or attempts to establish an
275 * unsupported target port asymmetric access state,
276 * then the command shall be terminated with CHECK
277 * CONDITION status, with the sense key set to ILLEGAL
278 * REQUEST, and the additional sense code set to INVALID
279 * FIELD IN PARAMETER LIST.
280 */
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700281 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
282 rc = -EINVAL;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000283 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800284 }
285 rc = -1;
286 /*
287 * If the ASYMMETRIC ACCESS STATE field (see table 267)
288 * specifies a primary target port asymmetric access state,
289 * then the TARGET PORT GROUP OR TARGET PORT field specifies
290 * a primary target port group for which the primary target
291 * port asymmetric access state shall be changed. If the
292 * ASYMMETRIC ACCESS STATE field specifies a secondary target
293 * port asymmetric access state, then the TARGET PORT GROUP OR
294 * TARGET PORT field specifies the relative target port
295 * identifier (see 3.1.120) of the target port for which the
296 * secondary target port asymmetric access state shall be
297 * changed.
298 */
299 if (primary) {
Roland Dreier33395fb2012-02-23 17:22:12 -0800300 tg_pt_id = get_unaligned_be16(ptr + 2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800301 /*
302 * Locate the matching target port group ID from
303 * the global tg_pt_gp list
304 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400305 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800306 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400307 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800308 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -0700309 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800310 continue;
311
312 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
313 continue;
314
315 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
316 smp_mb__after_atomic_inc();
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400317 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800318
319 rc = core_alua_do_port_transition(tg_pt_gp,
320 dev, l_port, nacl,
321 alua_access_state, 1);
322
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400323 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800324 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
325 smp_mb__after_atomic_dec();
326 break;
327 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400328 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800329 /*
330 * If not matching target port group ID can be located
331 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
332 */
Andy Grover05d1c7c2011-07-20 19:13:28 +0000333 if (rc != 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700334 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
335 rc = -EINVAL;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000336 goto out;
337 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800338 } else {
339 /*
340 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
341 * the Target Port in question for the the incoming
342 * SET_TARGET_PORT_GROUPS op.
343 */
Roland Dreier33395fb2012-02-23 17:22:12 -0800344 rtpi = get_unaligned_be16(ptr + 2);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800345 /*
Masanari Iida35d1efe2012-08-16 22:43:13 +0900346 * Locate the matching relative target port identifier
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800347 * for the struct se_device storage object.
348 */
349 spin_lock(&dev->se_port_lock);
350 list_for_each_entry(port, &dev->dev_sep_list,
351 sep_list) {
352 if (port->sep_rtpi != rtpi)
353 continue;
354
355 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
356 spin_unlock(&dev->se_port_lock);
357
358 rc = core_alua_set_tg_pt_secondary_state(
359 tg_pt_gp_mem, port, 1, 1);
360
361 spin_lock(&dev->se_port_lock);
362 break;
363 }
364 spin_unlock(&dev->se_port_lock);
365 /*
366 * If not matching relative target port identifier can
367 * be located, throw an exception with ASCQ:
368 * INVALID_PARAMETER_LIST
369 */
Andy Grover05d1c7c2011-07-20 19:13:28 +0000370 if (rc != 0) {
Nicholas Bellinger03e98c92011-11-04 02:36:16 -0700371 cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
372 rc = -EINVAL;
Andy Grover05d1c7c2011-07-20 19:13:28 +0000373 goto out;
374 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800375 }
376
377 ptr += 4;
378 len += 4;
379 }
380
Andy Grover05d1c7c2011-07-20 19:13:28 +0000381out:
Andy Grover49493142012-01-16 16:57:08 -0800382 transport_kunmap_data_sg(cmd);
Roland Dreier59e4f542012-06-04 23:24:51 -0700383 if (!rc)
384 target_complete_cmd(cmd, GOOD);
385 return rc;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800386}
387
388static inline int core_alua_state_nonoptimized(
389 struct se_cmd *cmd,
390 unsigned char *cdb,
391 int nonop_delay_msecs,
392 u8 *alua_ascq)
393{
394 /*
395 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
396 * later to determine if processing of this cmd needs to be
397 * temporarily delayed for the Active/NonOptimized primary access state.
398 */
399 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
400 cmd->alua_nonop_delay = nonop_delay_msecs;
401 return 0;
402}
403
404static inline int core_alua_state_standby(
405 struct se_cmd *cmd,
406 unsigned char *cdb,
407 u8 *alua_ascq)
408{
409 /*
410 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
411 * spc4r17 section 5.9.2.4.4
412 */
413 switch (cdb[0]) {
414 case INQUIRY:
415 case LOG_SELECT:
416 case LOG_SENSE:
417 case MODE_SELECT:
418 case MODE_SENSE:
419 case REPORT_LUNS:
420 case RECEIVE_DIAGNOSTIC:
421 case SEND_DIAGNOSTIC:
422 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700423 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800424 case MI_REPORT_TARGET_PGS:
425 return 0;
426 default:
427 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
428 return 1;
429 }
430 case MAINTENANCE_OUT:
431 switch (cdb[1]) {
432 case MO_SET_TARGET_PGS:
433 return 0;
434 default:
435 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
436 return 1;
437 }
438 case REQUEST_SENSE:
439 case PERSISTENT_RESERVE_IN:
440 case PERSISTENT_RESERVE_OUT:
441 case READ_BUFFER:
442 case WRITE_BUFFER:
443 return 0;
444 default:
445 *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
446 return 1;
447 }
448
449 return 0;
450}
451
452static inline int core_alua_state_unavailable(
453 struct se_cmd *cmd,
454 unsigned char *cdb,
455 u8 *alua_ascq)
456{
457 /*
458 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
459 * spc4r17 section 5.9.2.4.5
460 */
461 switch (cdb[0]) {
462 case INQUIRY:
463 case REPORT_LUNS:
464 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700465 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800466 case MI_REPORT_TARGET_PGS:
467 return 0;
468 default:
469 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
470 return 1;
471 }
472 case MAINTENANCE_OUT:
473 switch (cdb[1]) {
474 case MO_SET_TARGET_PGS:
475 return 0;
476 default:
477 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
478 return 1;
479 }
480 case REQUEST_SENSE:
481 case READ_BUFFER:
482 case WRITE_BUFFER:
483 return 0;
484 default:
485 *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
486 return 1;
487 }
488
489 return 0;
490}
491
492static inline int core_alua_state_transition(
493 struct se_cmd *cmd,
494 unsigned char *cdb,
495 u8 *alua_ascq)
496{
497 /*
498 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
499 * spc4r17 section 5.9.2.5
500 */
501 switch (cdb[0]) {
502 case INQUIRY:
503 case REPORT_LUNS:
504 case MAINTENANCE_IN:
Nicholas Bellingerba539742012-05-16 21:52:10 -0700505 switch (cdb[1] & 0x1f) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800506 case MI_REPORT_TARGET_PGS:
507 return 0;
508 default:
509 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
510 return 1;
511 }
512 case REQUEST_SENSE:
513 case READ_BUFFER:
514 case WRITE_BUFFER:
515 return 0;
516 default:
517 *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
518 return 1;
519 }
520
521 return 0;
522}
523
524/*
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800525 * return 1: Is used to signal LUN not accecsable, and check condition/not ready
526 * return 0: Used to signal success
527 * reutrn -1: Used to signal failure, and invalid cdb field
528 */
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400529int target_alua_state_check(struct se_cmd *cmd)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800530{
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400531 struct se_device *dev = cmd->se_dev;
532 unsigned char *cdb = cmd->t_task_cdb;
Andy Grovere3d6f902011-07-19 08:55:10 +0000533 struct se_lun *lun = cmd->se_lun;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800534 struct se_port *port = lun->lun_sep;
535 struct t10_alua_tg_pt_gp *tg_pt_gp;
536 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
537 int out_alua_state, nonop_delay_msecs;
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400538 u8 alua_ascq;
539 int ret;
540
541 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
542 return 0;
543 if (dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV)
544 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800545
Andy Grover6708bb22011-06-08 10:36:43 -0700546 if (!port)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800547 return 0;
548 /*
549 * First, check for a struct se_port specific secondary ALUA target port
550 * access state: OFFLINE
551 */
552 if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
Andy Grover6708bb22011-06-08 10:36:43 -0700553 pr_debug("ALUA: Got secondary offline status for local"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800554 " target port\n");
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400555 alua_ascq = ASCQ_04H_ALUA_OFFLINE;
556 ret = 1;
557 goto out;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800558 }
559 /*
560 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
561 * ALUA target port group, to obtain current ALUA access state.
562 * Otherwise look for the underlying struct se_device association with
563 * a ALUA logical unit group.
564 */
565 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
566 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
567 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
568 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
569 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
570 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
571 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300572 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
573 * statement so the compiler knows explicitly to check this case first.
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800574 * For the Optimized ALUA access state case, we want to process the
575 * incoming fabric cmd ASAP..
576 */
577 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
578 return 0;
579
580 switch (out_alua_state) {
581 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400582 ret = core_alua_state_nonoptimized(cmd, cdb,
583 nonop_delay_msecs, &alua_ascq);
584 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800585 case ALUA_ACCESS_STATE_STANDBY:
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400586 ret = core_alua_state_standby(cmd, cdb, &alua_ascq);
587 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800588 case ALUA_ACCESS_STATE_UNAVAILABLE:
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400589 ret = core_alua_state_unavailable(cmd, cdb, &alua_ascq);
590 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800591 case ALUA_ACCESS_STATE_TRANSITION:
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400592 ret = core_alua_state_transition(cmd, cdb, &alua_ascq);
593 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800594 /*
595 * OFFLINE is a secondary ALUA target port group access state, that is
596 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
597 */
598 case ALUA_ACCESS_STATE_OFFLINE:
599 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700600 pr_err("Unknown ALUA access state: 0x%02x\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800601 out_alua_state);
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400602 ret = -EINVAL;
603 break;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800604 }
605
Christoph Hellwigc87fbd52012-10-10 17:37:16 -0400606out:
607 if (ret > 0) {
608 /*
609 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
610 * The ALUA additional sense code qualifier (ASCQ) is determined
611 * by the ALUA primary or secondary access state..
612 */
613 pr_debug("[%s]: ALUA TG Port not available, "
614 "SenseKey: NOT_READY, ASC/ASCQ: "
615 "0x04/0x%02x\n",
616 cmd->se_tfo->get_fabric_name(), alua_ascq);
617
618 cmd->scsi_asc = 0x04;
619 cmd->scsi_ascq = alua_ascq;
620 }
621
622 return ret;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800623}
624
625/*
626 * Check implict and explict ALUA state change request.
627 */
628static int core_alua_check_transition(int state, int *primary)
629{
630 switch (state) {
631 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
632 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
633 case ALUA_ACCESS_STATE_STANDBY:
634 case ALUA_ACCESS_STATE_UNAVAILABLE:
635 /*
636 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
637 * defined as primary target port asymmetric access states.
638 */
639 *primary = 1;
640 break;
641 case ALUA_ACCESS_STATE_OFFLINE:
642 /*
643 * OFFLINE state is defined as a secondary target port
644 * asymmetric access state.
645 */
646 *primary = 0;
647 break;
648 default:
Andy Grover6708bb22011-06-08 10:36:43 -0700649 pr_err("Unknown ALUA access state: 0x%02x\n", state);
Andy Grovere3d6f902011-07-19 08:55:10 +0000650 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800651 }
652
653 return 0;
654}
655
656static char *core_alua_dump_state(int state)
657{
658 switch (state) {
659 case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
660 return "Active/Optimized";
661 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
662 return "Active/NonOptimized";
663 case ALUA_ACCESS_STATE_STANDBY:
664 return "Standby";
665 case ALUA_ACCESS_STATE_UNAVAILABLE:
666 return "Unavailable";
667 case ALUA_ACCESS_STATE_OFFLINE:
668 return "Offline";
669 default:
670 return "Unknown";
671 }
672
673 return NULL;
674}
675
676char *core_alua_dump_status(int status)
677{
678 switch (status) {
679 case ALUA_STATUS_NONE:
680 return "None";
681 case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
682 return "Altered by Explict STPG";
683 case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
684 return "Altered by Implict ALUA";
685 default:
686 return "Unknown";
687 }
688
689 return NULL;
690}
691
692/*
693 * Used by fabric modules to determine when we need to delay processing
694 * for the Active/NonOptimized paths..
695 */
696int core_alua_check_nonop_delay(
697 struct se_cmd *cmd)
698{
699 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
700 return 0;
701 if (in_interrupt())
702 return 0;
703 /*
704 * The ALUA Active/NonOptimized access state delay can be disabled
705 * in via configfs with a value of zero
706 */
Andy Grover6708bb22011-06-08 10:36:43 -0700707 if (!cmd->alua_nonop_delay)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800708 return 0;
709 /*
710 * struct se_cmd->alua_nonop_delay gets set by a target port group
711 * defined interval in core_alua_state_nonoptimized()
712 */
713 msleep_interruptible(cmd->alua_nonop_delay);
714 return 0;
715}
716EXPORT_SYMBOL(core_alua_check_nonop_delay);
717
718/*
719 * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
720 *
721 */
722static int core_alua_write_tpg_metadata(
723 const char *path,
724 unsigned char *md_buf,
725 u32 md_buf_len)
726{
727 mm_segment_t old_fs;
728 struct file *file;
729 struct iovec iov[1];
730 int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
731
732 memset(iov, 0, sizeof(struct iovec));
733
734 file = filp_open(path, flags, 0600);
735 if (IS_ERR(file) || !file || !file->f_dentry) {
Andy Grover6708bb22011-06-08 10:36:43 -0700736 pr_err("filp_open(%s) for ALUA metadata failed\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800737 path);
738 return -ENODEV;
739 }
740
741 iov[0].iov_base = &md_buf[0];
742 iov[0].iov_len = md_buf_len;
743
744 old_fs = get_fs();
745 set_fs(get_ds());
746 ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
747 set_fs(old_fs);
748
749 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -0700750 pr_err("Error writing ALUA metadata file: %s\n", path);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800751 filp_close(file, NULL);
752 return -EIO;
753 }
754 filp_close(file, NULL);
755
756 return 0;
757}
758
759/*
760 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
761 */
762static int core_alua_update_tpg_primary_metadata(
763 struct t10_alua_tg_pt_gp *tg_pt_gp,
764 int primary_state,
765 unsigned char *md_buf)
766{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400767 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800768 char path[ALUA_METADATA_PATH_LEN];
769 int len;
770
771 memset(path, 0, ALUA_METADATA_PATH_LEN);
772
773 len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
774 "tg_pt_gp_id=%hu\n"
775 "alua_access_state=0x%02x\n"
776 "alua_access_status=0x%02x\n",
777 tg_pt_gp->tg_pt_gp_id, primary_state,
778 tg_pt_gp->tg_pt_gp_alua_access_status);
779
780 snprintf(path, ALUA_METADATA_PATH_LEN,
781 "/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
782 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
783
784 return core_alua_write_tpg_metadata(path, md_buf, len);
785}
786
787static int core_alua_do_transition_tg_pt(
788 struct t10_alua_tg_pt_gp *tg_pt_gp,
789 struct se_port *l_port,
790 struct se_node_acl *nacl,
791 unsigned char *md_buf,
792 int new_state,
793 int explict)
794{
795 struct se_dev_entry *se_deve;
796 struct se_lun_acl *lacl;
797 struct se_port *port;
798 struct t10_alua_tg_pt_gp_member *mem;
799 int old_state = 0;
800 /*
801 * Save the old primary ALUA access state, and set the current state
802 * to ALUA_ACCESS_STATE_TRANSITION.
803 */
804 old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
805 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
806 ALUA_ACCESS_STATE_TRANSITION);
807 tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
808 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
809 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
810 /*
811 * Check for the optional ALUA primary state transition delay
812 */
813 if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
814 msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
815
816 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
817 list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
818 tg_pt_gp_mem_list) {
819 port = mem->tg_pt;
820 /*
821 * After an implicit target port asymmetric access state
822 * change, a device server shall establish a unit attention
823 * condition for the initiator port associated with every I_T
824 * nexus with the additional sense code set to ASYMMETRIC
825 * ACCESS STATE CHAGED.
826 *
827 * After an explicit target port asymmetric access state
828 * change, a device server shall establish a unit attention
829 * condition with the additional sense code set to ASYMMETRIC
830 * ACCESS STATE CHANGED for the initiator port associated with
831 * every I_T nexus other than the I_T nexus on which the SET
832 * TARGET PORT GROUPS command
833 */
834 atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
835 smp_mb__after_atomic_inc();
836 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
837
838 spin_lock_bh(&port->sep_alua_lock);
839 list_for_each_entry(se_deve, &port->sep_alua_list,
840 alua_port_list) {
841 lacl = se_deve->se_lun_acl;
842 /*
843 * se_deve->se_lun_acl pointer may be NULL for a
844 * entry created without explict Node+MappedLUN ACLs
845 */
Andy Grover6708bb22011-06-08 10:36:43 -0700846 if (!lacl)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800847 continue;
848
849 if (explict &&
850 (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
851 (l_port != NULL) && (l_port == port))
852 continue;
853
854 core_scsi3_ua_allocate(lacl->se_lun_nacl,
855 se_deve->mapped_lun, 0x2A,
856 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
857 }
858 spin_unlock_bh(&port->sep_alua_lock);
859
860 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
861 atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
862 smp_mb__after_atomic_dec();
863 }
864 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
865 /*
866 * Update the ALUA metadata buf that has been allocated in
867 * core_alua_do_port_transition(), this metadata will be written
868 * to struct file.
869 *
870 * Note that there is the case where we do not want to update the
871 * metadata when the saved metadata is being parsed in userspace
872 * when setting the existing port access state and access status.
873 *
874 * Also note that the failure to write out the ALUA metadata to
875 * struct file does NOT affect the actual ALUA transition.
876 */
877 if (tg_pt_gp->tg_pt_gp_write_metadata) {
878 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
879 core_alua_update_tpg_primary_metadata(tg_pt_gp,
880 new_state, md_buf);
881 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
882 }
883 /*
884 * Set the current primary ALUA access state to the requested new state
885 */
886 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
887
Andy Grover6708bb22011-06-08 10:36:43 -0700888 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800889 " from primary access state %s to %s\n", (explict) ? "explict" :
890 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
891 tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
892 core_alua_dump_state(new_state));
893
894 return 0;
895}
896
897int core_alua_do_port_transition(
898 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
899 struct se_device *l_dev,
900 struct se_port *l_port,
901 struct se_node_acl *l_nacl,
902 int new_state,
903 int explict)
904{
905 struct se_device *dev;
906 struct se_port *port;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800907 struct se_node_acl *nacl;
908 struct t10_alua_lu_gp *lu_gp;
909 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
910 struct t10_alua_tg_pt_gp *tg_pt_gp;
911 unsigned char *md_buf;
912 int primary;
913
914 if (core_alua_check_transition(new_state, &primary) != 0)
915 return -EINVAL;
916
917 md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -0700918 if (!md_buf) {
919 pr_err("Unable to allocate buf for ALUA metadata\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800920 return -ENOMEM;
921 }
922
923 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
924 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
925 lu_gp = local_lu_gp_mem->lu_gp;
926 atomic_inc(&lu_gp->lu_gp_ref_cnt);
927 smp_mb__after_atomic_inc();
928 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
929 /*
930 * For storage objects that are members of the 'default_lu_gp',
931 * we only do transition on the passed *l_tp_pt_gp, and not
932 * on all of the matching target port groups IDs in default_lu_gp.
933 */
Andy Grover6708bb22011-06-08 10:36:43 -0700934 if (!lu_gp->lu_gp_id) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800935 /*
936 * core_alua_do_transition_tg_pt() will always return
937 * success.
938 */
939 core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
940 md_buf, new_state, explict);
941 atomic_dec(&lu_gp->lu_gp_ref_cnt);
942 smp_mb__after_atomic_dec();
943 kfree(md_buf);
944 return 0;
945 }
946 /*
947 * For all other LU groups aside from 'default_lu_gp', walk all of
948 * the associated storage objects looking for a matching target port
949 * group ID from the local target port group.
950 */
951 spin_lock(&lu_gp->lu_gp_lock);
952 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
953 lu_gp_mem_list) {
954
955 dev = lu_gp_mem->lu_gp_mem_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800956 atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
957 smp_mb__after_atomic_inc();
958 spin_unlock(&lu_gp->lu_gp_lock);
959
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400960 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800961 list_for_each_entry(tg_pt_gp,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400962 &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800963 tg_pt_gp_list) {
964
Andy Grover6708bb22011-06-08 10:36:43 -0700965 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800966 continue;
967 /*
968 * If the target behavior port asymmetric access state
969 * is changed for any target port group accessiable via
970 * a logical unit within a LU group, the target port
971 * behavior group asymmetric access states for the same
972 * target port group accessible via other logical units
973 * in that LU group will also change.
974 */
975 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
976 continue;
977
978 if (l_tg_pt_gp == tg_pt_gp) {
979 port = l_port;
980 nacl = l_nacl;
981 } else {
982 port = NULL;
983 nacl = NULL;
984 }
985 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
986 smp_mb__after_atomic_inc();
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400987 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800988 /*
989 * core_alua_do_transition_tg_pt() will always return
990 * success.
991 */
992 core_alua_do_transition_tg_pt(tg_pt_gp, port,
993 nacl, md_buf, new_state, explict);
994
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400995 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -0800996 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
997 smp_mb__after_atomic_dec();
998 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -0400999 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001000
1001 spin_lock(&lu_gp->lu_gp_lock);
1002 atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
1003 smp_mb__after_atomic_dec();
1004 }
1005 spin_unlock(&lu_gp->lu_gp_lock);
1006
Andy Grover6708bb22011-06-08 10:36:43 -07001007 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001008 " Group IDs: %hu %s transition to primary state: %s\n",
1009 config_item_name(&lu_gp->lu_gp_group.cg_item),
1010 l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
1011 core_alua_dump_state(new_state));
1012
1013 atomic_dec(&lu_gp->lu_gp_ref_cnt);
1014 smp_mb__after_atomic_dec();
1015 kfree(md_buf);
1016 return 0;
1017}
1018
1019/*
1020 * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
1021 */
1022static int core_alua_update_tpg_secondary_metadata(
1023 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1024 struct se_port *port,
1025 unsigned char *md_buf,
1026 u32 md_buf_len)
1027{
1028 struct se_portal_group *se_tpg = port->sep_tpg;
1029 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1030 int len;
1031
1032 memset(path, 0, ALUA_METADATA_PATH_LEN);
1033 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1034
1035 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
Andy Grovere3d6f902011-07-19 08:55:10 +00001036 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001037
Andy Grovere3d6f902011-07-19 08:55:10 +00001038 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001039 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
Andy Grovere3d6f902011-07-19 08:55:10 +00001040 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001041
1042 len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d\n"
1043 "alua_tg_pt_status=0x%02x\n",
1044 atomic_read(&port->sep_tg_pt_secondary_offline),
1045 port->sep_tg_pt_secondary_stat);
1046
1047 snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
Andy Grovere3d6f902011-07-19 08:55:10 +00001048 se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001049 port->sep_lun->unpacked_lun);
1050
1051 return core_alua_write_tpg_metadata(path, md_buf, len);
1052}
1053
1054static int core_alua_set_tg_pt_secondary_state(
1055 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1056 struct se_port *port,
1057 int explict,
1058 int offline)
1059{
1060 struct t10_alua_tg_pt_gp *tg_pt_gp;
1061 unsigned char *md_buf;
1062 u32 md_buf_len;
1063 int trans_delay_msecs;
1064
1065 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1066 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001067 if (!tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001068 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001069 pr_err("Unable to complete secondary state"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001070 " transition\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001071 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001072 }
1073 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1074 /*
1075 * Set the secondary ALUA target port access state to OFFLINE
1076 * or release the previously secondary state for struct se_port
1077 */
1078 if (offline)
1079 atomic_set(&port->sep_tg_pt_secondary_offline, 1);
1080 else
1081 atomic_set(&port->sep_tg_pt_secondary_offline, 0);
1082
1083 md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
1084 port->sep_tg_pt_secondary_stat = (explict) ?
1085 ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
1086 ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
1087
Andy Grover6708bb22011-06-08 10:36:43 -07001088 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001089 " to secondary access state: %s\n", (explict) ? "explict" :
1090 "implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
1091 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1092
1093 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1094 /*
1095 * Do the optional transition delay after we set the secondary
1096 * ALUA access state.
1097 */
1098 if (trans_delay_msecs != 0)
1099 msleep_interruptible(trans_delay_msecs);
1100 /*
1101 * See if we need to update the ALUA fabric port metadata for
1102 * secondary state and status
1103 */
1104 if (port->sep_tg_pt_secondary_write_md) {
1105 md_buf = kzalloc(md_buf_len, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001106 if (!md_buf) {
1107 pr_err("Unable to allocate md_buf for"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001108 " secondary ALUA access metadata\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001109 return -ENOMEM;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001110 }
1111 mutex_lock(&port->sep_tg_pt_md_mutex);
1112 core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
1113 md_buf, md_buf_len);
1114 mutex_unlock(&port->sep_tg_pt_md_mutex);
1115
1116 kfree(md_buf);
1117 }
1118
1119 return 0;
1120}
1121
1122struct t10_alua_lu_gp *
1123core_alua_allocate_lu_gp(const char *name, int def_group)
1124{
1125 struct t10_alua_lu_gp *lu_gp;
1126
1127 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001128 if (!lu_gp) {
1129 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001130 return ERR_PTR(-ENOMEM);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001131 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001132 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001133 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1134 spin_lock_init(&lu_gp->lu_gp_lock);
1135 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1136
1137 if (def_group) {
Andy Grovere3d6f902011-07-19 08:55:10 +00001138 lu_gp->lu_gp_id = alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001139 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001140 alua_lu_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001141 }
1142
1143 return lu_gp;
1144}
1145
1146int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1147{
1148 struct t10_alua_lu_gp *lu_gp_tmp;
1149 u16 lu_gp_id_tmp;
1150 /*
1151 * The lu_gp->lu_gp_id may only be set once..
1152 */
1153 if (lu_gp->lu_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001154 pr_warn("ALUA LU Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001155 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001156 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001157 }
1158
Andy Grovere3d6f902011-07-19 08:55:10 +00001159 spin_lock(&lu_gps_lock);
1160 if (alua_lu_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001161 pr_err("Maximum ALUA alua_lu_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001162 " 0x0000ffff reached\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001163 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001164 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001165 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001166 }
1167again:
1168 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
Andy Grovere3d6f902011-07-19 08:55:10 +00001169 alua_lu_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001170
Andy Grovere3d6f902011-07-19 08:55:10 +00001171 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001172 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001173 if (!lu_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001174 goto again;
1175
Andy Grover6708bb22011-06-08 10:36:43 -07001176 pr_warn("ALUA Logical Unit Group ID: %hu"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001177 " already exists, ignoring request\n",
1178 lu_gp_id);
Andy Grovere3d6f902011-07-19 08:55:10 +00001179 spin_unlock(&lu_gps_lock);
1180 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001181 }
1182 }
1183
1184 lu_gp->lu_gp_id = lu_gp_id_tmp;
1185 lu_gp->lu_gp_valid_id = 1;
Andy Grovere3d6f902011-07-19 08:55:10 +00001186 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1187 alua_lu_gps_count++;
1188 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001189
1190 return 0;
1191}
1192
1193static struct t10_alua_lu_gp_member *
1194core_alua_allocate_lu_gp_mem(struct se_device *dev)
1195{
1196 struct t10_alua_lu_gp_member *lu_gp_mem;
1197
1198 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001199 if (!lu_gp_mem) {
1200 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001201 return ERR_PTR(-ENOMEM);
1202 }
1203 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1204 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1205 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1206
1207 lu_gp_mem->lu_gp_mem_dev = dev;
1208 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1209
1210 return lu_gp_mem;
1211}
1212
1213void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1214{
1215 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1216 /*
1217 * Once we have reached this point, config_item_put() has
1218 * already been called from target_core_alua_drop_lu_gp().
1219 *
1220 * Here, we remove the *lu_gp from the global list so that
1221 * no associations can be made while we are releasing
1222 * struct t10_alua_lu_gp.
1223 */
Andy Grovere3d6f902011-07-19 08:55:10 +00001224 spin_lock(&lu_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001225 list_del(&lu_gp->lu_gp_node);
1226 alua_lu_gps_count--;
1227 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001228 /*
1229 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1230 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1231 * released with core_alua_put_lu_gp_from_name()
1232 */
1233 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1234 cpu_relax();
1235 /*
1236 * Release reference to struct t10_alua_lu_gp * from all associated
1237 * struct se_device.
1238 */
1239 spin_lock(&lu_gp->lu_gp_lock);
1240 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1241 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1242 if (lu_gp_mem->lu_gp_assoc) {
1243 list_del(&lu_gp_mem->lu_gp_mem_list);
1244 lu_gp->lu_gp_members--;
1245 lu_gp_mem->lu_gp_assoc = 0;
1246 }
1247 spin_unlock(&lu_gp->lu_gp_lock);
1248 /*
1249 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001250 * lu_gp_mem is associated with a single
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001251 * struct se_device->dev_alua_lu_gp_mem, and is released when
1252 * struct se_device is released via core_alua_free_lu_gp_mem().
1253 *
1254 * If the passed lu_gp does NOT match the default_lu_gp, assume
1255 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
1256 */
1257 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001258 if (lu_gp != default_lu_gp)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001259 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00001260 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001261 else
1262 lu_gp_mem->lu_gp = NULL;
1263 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1264
1265 spin_lock(&lu_gp->lu_gp_lock);
1266 }
1267 spin_unlock(&lu_gp->lu_gp_lock);
1268
1269 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1270}
1271
1272void core_alua_free_lu_gp_mem(struct se_device *dev)
1273{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001274 struct t10_alua_lu_gp *lu_gp;
1275 struct t10_alua_lu_gp_member *lu_gp_mem;
1276
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001277 lu_gp_mem = dev->dev_alua_lu_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -07001278 if (!lu_gp_mem)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001279 return;
1280
1281 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1282 cpu_relax();
1283
1284 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1285 lu_gp = lu_gp_mem->lu_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001286 if (lu_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001287 spin_lock(&lu_gp->lu_gp_lock);
1288 if (lu_gp_mem->lu_gp_assoc) {
1289 list_del(&lu_gp_mem->lu_gp_mem_list);
1290 lu_gp->lu_gp_members--;
1291 lu_gp_mem->lu_gp_assoc = 0;
1292 }
1293 spin_unlock(&lu_gp->lu_gp_lock);
1294 lu_gp_mem->lu_gp = NULL;
1295 }
1296 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1297
1298 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1299}
1300
1301struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1302{
1303 struct t10_alua_lu_gp *lu_gp;
1304 struct config_item *ci;
1305
Andy Grovere3d6f902011-07-19 08:55:10 +00001306 spin_lock(&lu_gps_lock);
1307 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
Andy Grover6708bb22011-06-08 10:36:43 -07001308 if (!lu_gp->lu_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001309 continue;
1310 ci = &lu_gp->lu_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001311 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001312 atomic_inc(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001313 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001314 return lu_gp;
1315 }
1316 }
Andy Grovere3d6f902011-07-19 08:55:10 +00001317 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001318
1319 return NULL;
1320}
1321
1322void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1323{
Andy Grovere3d6f902011-07-19 08:55:10 +00001324 spin_lock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001325 atomic_dec(&lu_gp->lu_gp_ref_cnt);
Andy Grovere3d6f902011-07-19 08:55:10 +00001326 spin_unlock(&lu_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001327}
1328
1329/*
1330 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1331 */
1332void __core_alua_attach_lu_gp_mem(
1333 struct t10_alua_lu_gp_member *lu_gp_mem,
1334 struct t10_alua_lu_gp *lu_gp)
1335{
1336 spin_lock(&lu_gp->lu_gp_lock);
1337 lu_gp_mem->lu_gp = lu_gp;
1338 lu_gp_mem->lu_gp_assoc = 1;
1339 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1340 lu_gp->lu_gp_members++;
1341 spin_unlock(&lu_gp->lu_gp_lock);
1342}
1343
1344/*
1345 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1346 */
1347void __core_alua_drop_lu_gp_mem(
1348 struct t10_alua_lu_gp_member *lu_gp_mem,
1349 struct t10_alua_lu_gp *lu_gp)
1350{
1351 spin_lock(&lu_gp->lu_gp_lock);
1352 list_del(&lu_gp_mem->lu_gp_mem_list);
1353 lu_gp_mem->lu_gp = NULL;
1354 lu_gp_mem->lu_gp_assoc = 0;
1355 lu_gp->lu_gp_members--;
1356 spin_unlock(&lu_gp->lu_gp_lock);
1357}
1358
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001359struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1360 const char *name, int def_group)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001361{
1362 struct t10_alua_tg_pt_gp *tg_pt_gp;
1363
1364 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001365 if (!tg_pt_gp) {
1366 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001367 return NULL;
1368 }
1369 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
1370 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
1371 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1372 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1373 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001374 tg_pt_gp->tg_pt_gp_dev = dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001375 tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
1376 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1377 ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
1378 /*
1379 * Enable both explict and implict ALUA support by default
1380 */
1381 tg_pt_gp->tg_pt_gp_alua_access_type =
1382 TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
1383 /*
1384 * Set the default Active/NonOptimized Delay in milliseconds
1385 */
1386 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1387 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07001388 tg_pt_gp->tg_pt_gp_implict_trans_secs = ALUA_DEFAULT_IMPLICT_TRANS_SECS;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001389
1390 if (def_group) {
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001391 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001392 tg_pt_gp->tg_pt_gp_id =
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001393 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001394 tg_pt_gp->tg_pt_gp_valid_id = 1;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001395 dev->t10_alua.alua_tg_pt_gps_count++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001396 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001397 &dev->t10_alua.tg_pt_gps_list);
1398 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001399 }
1400
1401 return tg_pt_gp;
1402}
1403
1404int core_alua_set_tg_pt_gp_id(
1405 struct t10_alua_tg_pt_gp *tg_pt_gp,
1406 u16 tg_pt_gp_id)
1407{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001408 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001409 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1410 u16 tg_pt_gp_id_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001411
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001412 /*
1413 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1414 */
1415 if (tg_pt_gp->tg_pt_gp_valid_id) {
Andy Grover6708bb22011-06-08 10:36:43 -07001416 pr_warn("ALUA TG PT Group already has a valid ID,"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001417 " ignoring request\n");
Andy Grovere3d6f902011-07-19 08:55:10 +00001418 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001419 }
1420
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001421 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1422 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
Andy Grover6708bb22011-06-08 10:36:43 -07001423 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001424 " 0x0000ffff reached\n");
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001425 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001426 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
Andy Grovere3d6f902011-07-19 08:55:10 +00001427 return -ENOSPC;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001428 }
1429again:
1430 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001431 dev->t10_alua.alua_tg_pt_gps_counter++;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001432
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001433 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001434 tg_pt_gp_list) {
1435 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
Andy Grover6708bb22011-06-08 10:36:43 -07001436 if (!tg_pt_gp_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001437 goto again;
1438
Andy Grover6708bb22011-06-08 10:36:43 -07001439 pr_err("ALUA Target Port Group ID: %hu already"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001440 " exists, ignoring request\n", tg_pt_gp_id);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001441 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Andy Grovere3d6f902011-07-19 08:55:10 +00001442 return -EINVAL;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001443 }
1444 }
1445
1446 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1447 tg_pt_gp->tg_pt_gp_valid_id = 1;
1448 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001449 &dev->t10_alua.tg_pt_gps_list);
1450 dev->t10_alua.alua_tg_pt_gps_count++;
1451 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001452
1453 return 0;
1454}
1455
1456struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
1457 struct se_port *port)
1458{
1459 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1460
1461 tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
1462 GFP_KERNEL);
Andy Grover6708bb22011-06-08 10:36:43 -07001463 if (!tg_pt_gp_mem) {
1464 pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001465 return ERR_PTR(-ENOMEM);
1466 }
1467 INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1468 spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1469 atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
1470
1471 tg_pt_gp_mem->tg_pt = port;
1472 port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001473
1474 return tg_pt_gp_mem;
1475}
1476
1477void core_alua_free_tg_pt_gp(
1478 struct t10_alua_tg_pt_gp *tg_pt_gp)
1479{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001480 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001481 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001482
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001483 /*
1484 * Once we have reached this point, config_item_put() has already
1485 * been called from target_core_alua_drop_tg_pt_gp().
1486 *
1487 * Here we remove *tg_pt_gp from the global list so that
1488 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
1489 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1490 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001491 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001492 list_del(&tg_pt_gp->tg_pt_gp_list);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001493 dev->t10_alua.alua_tg_pt_gps_counter--;
1494 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1495
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001496 /*
1497 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1498 * core_alua_get_tg_pt_gp_by_name() in
1499 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1500 * to be released with core_alua_put_tg_pt_gp_from_name().
1501 */
1502 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1503 cpu_relax();
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001504
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001505 /*
1506 * Release reference to struct t10_alua_tg_pt_gp from all associated
1507 * struct se_port.
1508 */
1509 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1510 list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
1511 &tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
1512 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1513 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1514 tg_pt_gp->tg_pt_gp_members--;
1515 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1516 }
1517 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1518 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001519 * tg_pt_gp_mem is associated with a single
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001520 * se_port->sep_alua_tg_pt_gp_mem, and is released via
1521 * core_alua_free_tg_pt_gp_mem().
1522 *
1523 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
1524 * assume we want to re-assocate a given tg_pt_gp_mem with
1525 * default_tg_pt_gp.
1526 */
1527 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001528 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001529 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001530 dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001531 } else
1532 tg_pt_gp_mem->tg_pt_gp = NULL;
1533 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1534
1535 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1536 }
1537 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1538
1539 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1540}
1541
1542void core_alua_free_tg_pt_gp_mem(struct se_port *port)
1543{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001544 struct t10_alua_tg_pt_gp *tg_pt_gp;
1545 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1546
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001547 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -07001548 if (!tg_pt_gp_mem)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001549 return;
1550
1551 while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
1552 cpu_relax();
1553
1554 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1555 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001556 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001557 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1558 if (tg_pt_gp_mem->tg_pt_gp_assoc) {
1559 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1560 tg_pt_gp->tg_pt_gp_members--;
1561 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1562 }
1563 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1564 tg_pt_gp_mem->tg_pt_gp = NULL;
1565 }
1566 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1567
1568 kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
1569}
1570
1571static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001572 struct se_device *dev, const char *name)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001573{
1574 struct t10_alua_tg_pt_gp *tg_pt_gp;
1575 struct config_item *ci;
1576
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001577 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1578 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001579 tg_pt_gp_list) {
Andy Grover6708bb22011-06-08 10:36:43 -07001580 if (!tg_pt_gp->tg_pt_gp_valid_id)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001581 continue;
1582 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
Andy Grover6708bb22011-06-08 10:36:43 -07001583 if (!strcmp(config_item_name(ci), name)) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001584 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001585 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001586 return tg_pt_gp;
1587 }
1588 }
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001589 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001590
1591 return NULL;
1592}
1593
1594static void core_alua_put_tg_pt_gp_from_name(
1595 struct t10_alua_tg_pt_gp *tg_pt_gp)
1596{
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001597 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001598
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001599 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001600 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001601 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001602}
1603
1604/*
1605 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1606 */
1607void __core_alua_attach_tg_pt_gp_mem(
1608 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1609 struct t10_alua_tg_pt_gp *tg_pt_gp)
1610{
1611 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1612 tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
1613 tg_pt_gp_mem->tg_pt_gp_assoc = 1;
1614 list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
1615 &tg_pt_gp->tg_pt_gp_mem_list);
1616 tg_pt_gp->tg_pt_gp_members++;
1617 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1618}
1619
1620/*
1621 * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
1622 */
1623static void __core_alua_drop_tg_pt_gp_mem(
1624 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
1625 struct t10_alua_tg_pt_gp *tg_pt_gp)
1626{
1627 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1628 list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
1629 tg_pt_gp_mem->tg_pt_gp = NULL;
1630 tg_pt_gp_mem->tg_pt_gp_assoc = 0;
1631 tg_pt_gp->tg_pt_gp_members--;
1632 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1633}
1634
1635ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
1636{
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001637 struct config_item *tg_pt_ci;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001638 struct t10_alua_tg_pt_gp *tg_pt_gp;
1639 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1640 ssize_t len = 0;
1641
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001642 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -07001643 if (!tg_pt_gp_mem)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001644 return len;
1645
1646 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1647 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001648 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001649 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1650 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1651 " %hu\nTG Port Primary Access State: %s\nTG Port "
1652 "Primary Access Status: %s\nTG Port Secondary Access"
1653 " State: %s\nTG Port Secondary Access Status: %s\n",
1654 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1655 core_alua_dump_state(atomic_read(
1656 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1657 core_alua_dump_status(
1658 tg_pt_gp->tg_pt_gp_alua_access_status),
1659 (atomic_read(&port->sep_tg_pt_secondary_offline)) ?
1660 "Offline" : "None",
1661 core_alua_dump_status(port->sep_tg_pt_secondary_stat));
1662 }
1663 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1664
1665 return len;
1666}
1667
1668ssize_t core_alua_store_tg_pt_gp_info(
1669 struct se_port *port,
1670 const char *page,
1671 size_t count)
1672{
1673 struct se_portal_group *tpg;
1674 struct se_lun *lun;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001675 struct se_device *dev = port->sep_lun->lun_se_dev;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001676 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
1677 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1678 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1679 int move = 0;
1680
1681 tpg = port->sep_tpg;
1682 lun = port->sep_lun;
1683
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04001684 tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
1685 if (!tg_pt_gp_mem)
1686 return 0;
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001687
1688 if (count > TG_PT_GROUP_NAME_BUF) {
Andy Grover6708bb22011-06-08 10:36:43 -07001689 pr_err("ALUA Target Port Group alias too large!\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001690 return -EINVAL;
1691 }
1692 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1693 memcpy(buf, page, count);
1694 /*
1695 * Any ALUA target port group alias besides "NULL" means we will be
1696 * making a new group association.
1697 */
1698 if (strcmp(strstrip(buf), "NULL")) {
1699 /*
1700 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1701 * struct t10_alua_tg_pt_gp. This reference is released with
1702 * core_alua_put_tg_pt_gp_from_name() below.
1703 */
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001704 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001705 strstrip(buf));
Andy Grover6708bb22011-06-08 10:36:43 -07001706 if (!tg_pt_gp_new)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001707 return -ENODEV;
1708 }
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001709
1710 spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1711 tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
Andy Grover6708bb22011-06-08 10:36:43 -07001712 if (tg_pt_gp) {
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001713 /*
1714 * Clearing an existing tg_pt_gp association, and replacing
1715 * with the default_tg_pt_gp.
1716 */
Andy Grover6708bb22011-06-08 10:36:43 -07001717 if (!tg_pt_gp_new) {
1718 pr_debug("Target_Core_ConfigFS: Moving"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001719 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
1720 " alua/%s, ID: %hu back to"
1721 " default_tg_pt_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00001722 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1723 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001724 config_item_name(&lun->lun_group.cg_item),
1725 config_item_name(
1726 &tg_pt_gp->tg_pt_gp_group.cg_item),
1727 tg_pt_gp->tg_pt_gp_id);
1728
1729 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1730 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04001731 dev->t10_alua.default_tg_pt_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001732 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
1733
1734 return count;
1735 }
1736 /*
1737 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
1738 */
1739 __core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
1740 move = 1;
1741 }
1742 /*
1743 * Associate tg_pt_gp_mem with tg_pt_gp_new.
1744 */
1745 __core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
1746 spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
Andy Grover6708bb22011-06-08 10:36:43 -07001747 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001748 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
Andy Grovere3d6f902011-07-19 08:55:10 +00001749 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
1750 tpg->se_tpg_tfo->tpg_get_tag(tpg),
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001751 config_item_name(&lun->lun_group.cg_item),
1752 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
1753 tg_pt_gp_new->tg_pt_gp_id);
1754
1755 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
1756 return count;
1757}
1758
1759ssize_t core_alua_show_access_type(
1760 struct t10_alua_tg_pt_gp *tg_pt_gp,
1761 char *page)
1762{
1763 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
1764 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
1765 return sprintf(page, "Implict and Explict\n");
1766 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
1767 return sprintf(page, "Implict\n");
1768 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
1769 return sprintf(page, "Explict\n");
1770 else
1771 return sprintf(page, "None\n");
1772}
1773
1774ssize_t core_alua_store_access_type(
1775 struct t10_alua_tg_pt_gp *tg_pt_gp,
1776 const char *page,
1777 size_t count)
1778{
1779 unsigned long tmp;
1780 int ret;
1781
1782 ret = strict_strtoul(page, 0, &tmp);
1783 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001784 pr_err("Unable to extract alua_access_type\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001785 return -EINVAL;
1786 }
1787 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001788 pr_err("Illegal value for alua_access_type:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001789 " %lu\n", tmp);
1790 return -EINVAL;
1791 }
1792 if (tmp == 3)
1793 tg_pt_gp->tg_pt_gp_alua_access_type =
1794 TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
1795 else if (tmp == 2)
1796 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
1797 else if (tmp == 1)
1798 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
1799 else
1800 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
1801
1802 return count;
1803}
1804
1805ssize_t core_alua_show_nonop_delay_msecs(
1806 struct t10_alua_tg_pt_gp *tg_pt_gp,
1807 char *page)
1808{
1809 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
1810}
1811
1812ssize_t core_alua_store_nonop_delay_msecs(
1813 struct t10_alua_tg_pt_gp *tg_pt_gp,
1814 const char *page,
1815 size_t count)
1816{
1817 unsigned long tmp;
1818 int ret;
1819
1820 ret = strict_strtoul(page, 0, &tmp);
1821 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001822 pr_err("Unable to extract nonop_delay_msecs\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001823 return -EINVAL;
1824 }
1825 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07001826 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001827 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
1828 ALUA_MAX_NONOP_DELAY_MSECS);
1829 return -EINVAL;
1830 }
1831 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
1832
1833 return count;
1834}
1835
1836ssize_t core_alua_show_trans_delay_msecs(
1837 struct t10_alua_tg_pt_gp *tg_pt_gp,
1838 char *page)
1839{
1840 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
1841}
1842
1843ssize_t core_alua_store_trans_delay_msecs(
1844 struct t10_alua_tg_pt_gp *tg_pt_gp,
1845 const char *page,
1846 size_t count)
1847{
1848 unsigned long tmp;
1849 int ret;
1850
1851 ret = strict_strtoul(page, 0, &tmp);
1852 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001853 pr_err("Unable to extract trans_delay_msecs\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001854 return -EINVAL;
1855 }
1856 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
Andy Grover6708bb22011-06-08 10:36:43 -07001857 pr_err("Passed trans_delay_msecs: %lu, exceeds"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001858 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
1859 ALUA_MAX_TRANS_DELAY_MSECS);
1860 return -EINVAL;
1861 }
1862 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
1863
1864 return count;
1865}
1866
Nicholas Bellinger5b9a4d72012-05-16 22:02:34 -07001867ssize_t core_alua_show_implict_trans_secs(
1868 struct t10_alua_tg_pt_gp *tg_pt_gp,
1869 char *page)
1870{
1871 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implict_trans_secs);
1872}
1873
1874ssize_t core_alua_store_implict_trans_secs(
1875 struct t10_alua_tg_pt_gp *tg_pt_gp,
1876 const char *page,
1877 size_t count)
1878{
1879 unsigned long tmp;
1880 int ret;
1881
1882 ret = strict_strtoul(page, 0, &tmp);
1883 if (ret < 0) {
1884 pr_err("Unable to extract implict_trans_secs\n");
1885 return -EINVAL;
1886 }
1887 if (tmp > ALUA_MAX_IMPLICT_TRANS_SECS) {
1888 pr_err("Passed implict_trans_secs: %lu, exceeds"
1889 " ALUA_MAX_IMPLICT_TRANS_SECS: %d\n", tmp,
1890 ALUA_MAX_IMPLICT_TRANS_SECS);
1891 return -EINVAL;
1892 }
1893 tg_pt_gp->tg_pt_gp_implict_trans_secs = (int)tmp;
1894
1895 return count;
1896}
1897
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001898ssize_t core_alua_show_preferred_bit(
1899 struct t10_alua_tg_pt_gp *tg_pt_gp,
1900 char *page)
1901{
1902 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
1903}
1904
1905ssize_t core_alua_store_preferred_bit(
1906 struct t10_alua_tg_pt_gp *tg_pt_gp,
1907 const char *page,
1908 size_t count)
1909{
1910 unsigned long tmp;
1911 int ret;
1912
1913 ret = strict_strtoul(page, 0, &tmp);
1914 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001915 pr_err("Unable to extract preferred ALUA value\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001916 return -EINVAL;
1917 }
1918 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001919 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001920 return -EINVAL;
1921 }
1922 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
1923
1924 return count;
1925}
1926
1927ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
1928{
Andy Grover6708bb22011-06-08 10:36:43 -07001929 if (!lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001930 return -ENODEV;
1931
1932 return sprintf(page, "%d\n",
1933 atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
1934}
1935
1936ssize_t core_alua_store_offline_bit(
1937 struct se_lun *lun,
1938 const char *page,
1939 size_t count)
1940{
1941 struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
1942 unsigned long tmp;
1943 int ret;
1944
Andy Grover6708bb22011-06-08 10:36:43 -07001945 if (!lun->lun_sep)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001946 return -ENODEV;
1947
1948 ret = strict_strtoul(page, 0, &tmp);
1949 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001950 pr_err("Unable to extract alua_tg_pt_offline value\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001951 return -EINVAL;
1952 }
1953 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001954 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001955 tmp);
1956 return -EINVAL;
1957 }
1958 tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
Andy Grover6708bb22011-06-08 10:36:43 -07001959 if (!tg_pt_gp_mem) {
1960 pr_err("Unable to locate *tg_pt_gp_mem\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001961 return -EINVAL;
1962 }
1963
1964 ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
1965 lun->lun_sep, 0, (int)tmp);
1966 if (ret < 0)
1967 return -EINVAL;
1968
1969 return count;
1970}
1971
1972ssize_t core_alua_show_secondary_status(
1973 struct se_lun *lun,
1974 char *page)
1975{
1976 return sprintf(page, "%d\n", lun->lun_sep->sep_tg_pt_secondary_stat);
1977}
1978
1979ssize_t core_alua_store_secondary_status(
1980 struct se_lun *lun,
1981 const char *page,
1982 size_t count)
1983{
1984 unsigned long tmp;
1985 int ret;
1986
1987 ret = strict_strtoul(page, 0, &tmp);
1988 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07001989 pr_err("Unable to extract alua_tg_pt_status\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001990 return -EINVAL;
1991 }
1992 if ((tmp != ALUA_STATUS_NONE) &&
1993 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
1994 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
Andy Grover6708bb22011-06-08 10:36:43 -07001995 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08001996 tmp);
1997 return -EINVAL;
1998 }
1999 lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
2000
2001 return count;
2002}
2003
2004ssize_t core_alua_show_secondary_write_metadata(
2005 struct se_lun *lun,
2006 char *page)
2007{
2008 return sprintf(page, "%d\n",
2009 lun->lun_sep->sep_tg_pt_secondary_write_md);
2010}
2011
2012ssize_t core_alua_store_secondary_write_metadata(
2013 struct se_lun *lun,
2014 const char *page,
2015 size_t count)
2016{
2017 unsigned long tmp;
2018 int ret;
2019
2020 ret = strict_strtoul(page, 0, &tmp);
2021 if (ret < 0) {
Andy Grover6708bb22011-06-08 10:36:43 -07002022 pr_err("Unable to extract alua_tg_pt_write_md\n");
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002023 return -EINVAL;
2024 }
2025 if ((tmp != 0) && (tmp != 1)) {
Andy Grover6708bb22011-06-08 10:36:43 -07002026 pr_err("Illegal value for alua_tg_pt_write_md:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002027 " %lu\n", tmp);
2028 return -EINVAL;
2029 }
2030 lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
2031
2032 return count;
2033}
2034
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002035int core_setup_alua(struct se_device *dev)
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002036{
Christoph Hellwigc87fbd52012-10-10 17:37:16 -04002037 if (dev->transport->transport_type != TRANSPORT_PLUGIN_PHBA_PDEV &&
2038 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2039 struct t10_alua_lu_gp_member *lu_gp_mem;
Christoph Hellwig0fd97cc2012-10-08 00:03:19 -04002040
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002041 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002042 * Associate this struct se_device with the default ALUA
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002043 * LUN Group.
2044 */
2045 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
Andy Grovere3d6f902011-07-19 08:55:10 +00002046 if (IS_ERR(lu_gp_mem))
2047 return PTR_ERR(lu_gp_mem);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002048
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002049 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2050 __core_alua_attach_lu_gp_mem(lu_gp_mem,
Andy Grovere3d6f902011-07-19 08:55:10 +00002051 default_lu_gp);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002052 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2053
Andy Grover6708bb22011-06-08 10:36:43 -07002054 pr_debug("%s: Adding to default ALUA LU Group:"
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002055 " core/alua/lu_gps/default_lu_gp\n",
Andy Grovere3d6f902011-07-19 08:55:10 +00002056 dev->transport->name);
Nicholas Bellingerc66ac9d2010-12-17 11:11:26 -08002057 }
2058
2059 return 0;
2060}