]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/target/target_core_alua.c
Bluetooth: hci_bcm: Support platform enumeration
[mirror_ubuntu-bionic-kernel.git] / drivers / target / target_core_alua.c
CommitLineData
c66ac9db
NB
1/*******************************************************************************
2 * Filename: target_core_alua.c
3 *
4 * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
5 *
4c76251e 6 * (c) Copyright 2009-2013 Datera, Inc.
c66ac9db
NB
7 *
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
c66ac9db
NB
26#include <linux/slab.h>
27#include <linux/spinlock.h>
28#include <linux/configfs.h>
8dcf07be 29#include <linux/delay.h>
c53181af 30#include <linux/export.h>
8dcf07be 31#include <linux/fcntl.h>
0e9b10a9 32#include <linux/file.h>
8dcf07be 33#include <linux/fs.h>
ba929992 34#include <scsi/scsi_proto.h>
33395fb8 35#include <asm/unaligned.h>
c66ac9db
NB
36
37#include <target/target_core_base.h>
c4795fb2
CH
38#include <target/target_core_backend.h>
39#include <target/target_core_fabric.h>
c66ac9db 40
e26d99ae 41#include "target_core_internal.h"
c66ac9db 42#include "target_core_alua.h"
c66ac9db
NB
43#include "target_core_ua.h"
44
bb91c1a0 45static sense_reason_t core_alua_check_transition(int state, int valid,
1ca4d4fa 46 int *primary, int explicit);
c66ac9db 47static int core_alua_set_tg_pt_secondary_state(
adf653f9 48 struct se_lun *lun, int explicit, int offline);
c66ac9db 49
bb91c1a0
HR
50static char *core_alua_dump_state(int state);
51
adf653f9
CH
52static void __target_attach_tg_pt_gp(struct se_lun *lun,
53 struct t10_alua_tg_pt_gp *tg_pt_gp);
54
e3d6f909
AG
55static u16 alua_lu_gps_counter;
56static u32 alua_lu_gps_count;
57
58static DEFINE_SPINLOCK(lu_gps_lock);
59static LIST_HEAD(lu_gps_list);
60
61struct t10_alua_lu_gp *default_lu_gp;
62
c66094bf
HR
63/*
64 * REPORT REFERRALS
65 *
66 * See sbc3r35 section 5.23
67 */
68sense_reason_t
69target_emulate_report_referrals(struct se_cmd *cmd)
70{
71 struct se_device *dev = cmd->se_dev;
72 struct t10_alua_lba_map *map;
73 struct t10_alua_lba_map_member *map_mem;
74 unsigned char *buf;
75 u32 rd_len = 0, off;
76
77 if (cmd->data_length < 4) {
78 pr_warn("REPORT REFERRALS allocation length %u too"
79 " small\n", cmd->data_length);
80 return TCM_INVALID_CDB_FIELD;
81 }
82
83 buf = transport_kmap_data_sg(cmd);
84 if (!buf)
85 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
86
87 off = 4;
88 spin_lock(&dev->t10_alua.lba_map_lock);
89 if (list_empty(&dev->t10_alua.lba_map_list)) {
90 spin_unlock(&dev->t10_alua.lba_map_lock);
91 transport_kunmap_data_sg(cmd);
92
93 return TCM_UNSUPPORTED_SCSI_OPCODE;
94 }
95
96 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
97 lba_map_list) {
98 int desc_num = off + 3;
99 int pg_num;
100
101 off += 4;
38edd724
HR
102 if (cmd->data_length > off)
103 put_unaligned_be64(map->lba_map_first_lba, &buf[off]);
c66094bf 104 off += 8;
38edd724
HR
105 if (cmd->data_length > off)
106 put_unaligned_be64(map->lba_map_last_lba, &buf[off]);
c66094bf
HR
107 off += 8;
108 rd_len += 20;
109 pg_num = 0;
110 list_for_each_entry(map_mem, &map->lba_map_mem_list,
111 lba_map_mem_list) {
38edd724
HR
112 int alua_state = map_mem->lba_map_mem_alua_state;
113 int alua_pg_id = map_mem->lba_map_mem_alua_pg_id;
114
115 if (cmd->data_length > off)
116 buf[off] = alua_state & 0x0f;
117 off += 2;
118 if (cmd->data_length > off)
119 buf[off] = (alua_pg_id >> 8) & 0xff;
120 off++;
121 if (cmd->data_length > off)
122 buf[off] = (alua_pg_id & 0xff);
c66094bf 123 off++;
c66094bf
HR
124 rd_len += 4;
125 pg_num++;
126 }
38edd724
HR
127 if (cmd->data_length > desc_num)
128 buf[desc_num] = pg_num;
c66094bf
HR
129 }
130 spin_unlock(&dev->t10_alua.lba_map_lock);
131
132 /*
133 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
134 */
135 put_unaligned_be16(rd_len, &buf[2]);
136
137 transport_kunmap_data_sg(cmd);
138
139 target_complete_cmd(cmd, GOOD);
140 return 0;
141}
142
c66ac9db
NB
143/*
144 * REPORT_TARGET_PORT_GROUPS
145 *
146 * See spc4r17 section 6.27
147 */
de103c93
CH
148sense_reason_t
149target_emulate_report_target_port_groups(struct se_cmd *cmd)
c66ac9db 150{
0fd97ccf 151 struct se_device *dev = cmd->se_dev;
c66ac9db 152 struct t10_alua_tg_pt_gp *tg_pt_gp;
adf653f9 153 struct se_lun *lun;
05d1c7c0 154 unsigned char *buf;
5b9a4d72
NB
155 u32 rd_len = 0, off;
156 int ext_hdr = (cmd->t_task_cdb[1] & 0x20);
de103c93 157
6b20fa9a 158 /*
5b9a4d72
NB
159 * Skip over RESERVED area to first Target port group descriptor
160 * depending on the PARAMETER DATA FORMAT type..
6b20fa9a 161 */
5b9a4d72
NB
162 if (ext_hdr != 0)
163 off = 8;
164 else
165 off = 4;
166
167 if (cmd->data_length < off) {
168 pr_warn("REPORT TARGET PORT GROUPS allocation length %u too"
169 " small for %s header\n", cmd->data_length,
170 (ext_hdr) ? "extended" : "normal");
de103c93 171 return TCM_INVALID_CDB_FIELD;
6b20fa9a 172 }
4949314c 173 buf = transport_kmap_data_sg(cmd);
de103c93
CH
174 if (!buf)
175 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
05d1c7c0 176
0fd97ccf
CH
177 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
178 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
c66ac9db 179 tg_pt_gp_list) {
6b20fa9a
NB
180 /*
181 * Check if the Target port group and Target port descriptor list
182 * based on tg_pt_gp_members count will fit into the response payload.
183 * Otherwise, bump rd_len to let the initiator know we have exceeded
184 * the allocation length and the response is truncated.
185 */
186 if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
187 cmd->data_length) {
188 rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
189 continue;
190 }
c66ac9db
NB
191 /*
192 * PREF: Preferred target port bit, determine if this
193 * bit should be set for port group.
194 */
195 if (tg_pt_gp->tg_pt_gp_pref)
196 buf[off] = 0x80;
197 /*
198 * Set the ASYMMETRIC ACCESS State
199 */
200 buf[off++] |= (atomic_read(
201 &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
202 /*
203 * Set supported ASYMMETRIC ACCESS State bits
204 */
c0dc941e 205 buf[off++] |= tg_pt_gp->tg_pt_gp_alua_supported_states;
c66ac9db
NB
206 /*
207 * TARGET PORT GROUP
208 */
209 buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
210 buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
211
212 off++; /* Skip over Reserved */
213 /*
214 * STATUS CODE
215 */
216 buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
217 /*
218 * Vendor Specific field
219 */
220 buf[off++] = 0x00;
221 /*
222 * TARGET PORT COUNT
223 */
224 buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
225 rd_len += 8;
226
227 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
adf653f9
CH
228 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
229 lun_tg_pt_gp_link) {
c66ac9db
NB
230 /*
231 * Start Target Port descriptor format
232 *
233 * See spc4r17 section 6.2.7 Table 247
234 */
235 off += 2; /* Skip over Obsolete */
236 /*
237 * Set RELATIVE TARGET PORT IDENTIFIER
238 */
adf653f9
CH
239 buf[off++] = ((lun->lun_rtpi >> 8) & 0xff);
240 buf[off++] = (lun->lun_rtpi & 0xff);
c66ac9db
NB
241 rd_len += 4;
242 }
243 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
244 }
0fd97ccf 245 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db
NB
246 /*
247 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
248 */
5b9a4d72 249 put_unaligned_be32(rd_len, &buf[0]);
c66ac9db 250
5b9a4d72
NB
251 /*
252 * Fill in the Extended header parameter data format if requested
253 */
254 if (ext_hdr != 0) {
255 buf[4] = 0x10;
256 /*
125d0119 257 * Set the implicit transition time (in seconds) for the application
5b9a4d72
NB
258 * client to use as a base for it's transition timeout value.
259 *
260 * Use the current tg_pt_gp_mem -> tg_pt_gp membership from the LUN
261 * this CDB was received upon to determine this value individually
262 * for ALUA target port group.
263 */
adf653f9
CH
264 spin_lock(&cmd->se_lun->lun_tg_pt_gp_lock);
265 tg_pt_gp = cmd->se_lun->lun_tg_pt_gp;
266 if (tg_pt_gp)
267 buf[5] = tg_pt_gp->tg_pt_gp_implicit_trans_secs;
268 spin_unlock(&cmd->se_lun->lun_tg_pt_gp_lock);
5b9a4d72 269 }
4949314c 270 transport_kunmap_data_sg(cmd);
05d1c7c0 271
6bb35e00 272 target_complete_cmd(cmd, GOOD);
c66ac9db
NB
273 return 0;
274}
275
276/*
125d0119 277 * SET_TARGET_PORT_GROUPS for explicit ALUA operation.
c66ac9db
NB
278 *
279 * See spc4r17 section 6.35
280 */
de103c93
CH
281sense_reason_t
282target_emulate_set_target_port_groups(struct se_cmd *cmd)
c66ac9db 283{
5951146d 284 struct se_device *dev = cmd->se_dev;
adf653f9 285 struct se_lun *l_lun = cmd->se_lun;
e3d6f909 286 struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
c66ac9db 287 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
05d1c7c0
AG
288 unsigned char *buf;
289 unsigned char *ptr;
a0d50f62 290 sense_reason_t rc = TCM_NO_SENSE;
c66ac9db 291 u32 len = 4; /* Skip over RESERVED area in header */
bb91c1a0 292 int alua_access_state, primary = 0, valid_states;
c66ac9db
NB
293 u16 tg_pt_id, rtpi;
294
0d7f1299
PB
295 if (cmd->data_length < 4) {
296 pr_warn("SET TARGET PORT GROUPS parameter list length %u too"
297 " small\n", cmd->data_length);
de103c93 298 return TCM_INVALID_PARAMETER_LIST;
0d7f1299
PB
299 }
300
4949314c 301 buf = transport_kmap_data_sg(cmd);
de103c93
CH
302 if (!buf)
303 return TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
05d1c7c0 304
c66ac9db 305 /*
125d0119 306 * Determine if explicit ALUA via SET_TARGET_PORT_GROUPS is allowed
c66ac9db
NB
307 * for the local tg_pt_gp.
308 */
adf653f9
CH
309 spin_lock(&l_lun->lun_tg_pt_gp_lock);
310 l_tg_pt_gp = l_lun->lun_tg_pt_gp;
6708bb27 311 if (!l_tg_pt_gp) {
adf653f9
CH
312 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
313 pr_err("Unable to access l_lun->tg_pt_gp\n");
de103c93 314 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
05d1c7c0 315 goto out;
c66ac9db 316 }
c66ac9db 317
125d0119 318 if (!(l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)) {
adf653f9 319 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
6708bb27 320 pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
125d0119 321 " while TPGS_EXPLICIT_ALUA is disabled\n");
de103c93 322 rc = TCM_UNSUPPORTED_SCSI_OPCODE;
05d1c7c0 323 goto out;
c66ac9db 324 }
bb91c1a0 325 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
adf653f9 326 spin_unlock(&l_lun->lun_tg_pt_gp_lock);
c66ac9db 327
05d1c7c0
AG
328 ptr = &buf[4]; /* Skip over RESERVED area in header */
329
c66ac9db 330 while (len < cmd->data_length) {
de103c93 331 bool found = false;
c66ac9db
NB
332 alua_access_state = (ptr[0] & 0x0f);
333 /*
334 * Check the received ALUA access state, and determine if
335 * the state is a primary or secondary target port asymmetric
336 * access state.
337 */
1ca4d4fa
MC
338 rc = core_alua_check_transition(alua_access_state, valid_states,
339 &primary, 1);
de103c93 340 if (rc) {
c66ac9db
NB
341 /*
342 * If the SET TARGET PORT GROUPS attempts to establish
343 * an invalid combination of target port asymmetric
344 * access states or attempts to establish an
345 * unsupported target port asymmetric access state,
346 * then the command shall be terminated with CHECK
347 * CONDITION status, with the sense key set to ILLEGAL
348 * REQUEST, and the additional sense code set to INVALID
349 * FIELD IN PARAMETER LIST.
350 */
05d1c7c0 351 goto out;
c66ac9db 352 }
de103c93 353
c66ac9db
NB
354 /*
355 * If the ASYMMETRIC ACCESS STATE field (see table 267)
356 * specifies a primary target port asymmetric access state,
357 * then the TARGET PORT GROUP OR TARGET PORT field specifies
358 * a primary target port group for which the primary target
359 * port asymmetric access state shall be changed. If the
360 * ASYMMETRIC ACCESS STATE field specifies a secondary target
361 * port asymmetric access state, then the TARGET PORT GROUP OR
362 * TARGET PORT field specifies the relative target port
363 * identifier (see 3.1.120) of the target port for which the
364 * secondary target port asymmetric access state shall be
365 * changed.
366 */
367 if (primary) {
33395fb8 368 tg_pt_id = get_unaligned_be16(ptr + 2);
c66ac9db
NB
369 /*
370 * Locate the matching target port group ID from
371 * the global tg_pt_gp list
372 */
0fd97ccf 373 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db 374 list_for_each_entry(tg_pt_gp,
0fd97ccf 375 &dev->t10_alua.tg_pt_gps_list,
c66ac9db 376 tg_pt_gp_list) {
6708bb27 377 if (!tg_pt_gp->tg_pt_gp_valid_id)
c66ac9db
NB
378 continue;
379
380 if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
381 continue;
382
33940d09 383 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
de103c93 384
0fd97ccf 385 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db 386
de103c93 387 if (!core_alua_do_port_transition(tg_pt_gp,
adf653f9 388 dev, l_lun, nacl,
de103c93
CH
389 alua_access_state, 1))
390 found = true;
c66ac9db 391
0fd97ccf 392 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
33940d09 393 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
c66ac9db
NB
394 break;
395 }
0fd97ccf 396 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db 397 } else {
adf653f9
CH
398 struct se_lun *lun;
399
c66ac9db 400 /*
f1ae05d5 401 * Extract the RELATIVE TARGET PORT IDENTIFIER to identify
c66ac9db
NB
402 * the Target Port in question for the the incoming
403 * SET_TARGET_PORT_GROUPS op.
404 */
33395fb8 405 rtpi = get_unaligned_be16(ptr + 2);
c66ac9db 406 /*
35d1efe8 407 * Locate the matching relative target port identifier
c66ac9db
NB
408 * for the struct se_device storage object.
409 */
410 spin_lock(&dev->se_port_lock);
adf653f9
CH
411 list_for_each_entry(lun, &dev->dev_sep_list,
412 lun_dev_link) {
413 if (lun->lun_rtpi != rtpi)
c66ac9db
NB
414 continue;
415
adf653f9 416 // XXX: racy unlock
c66ac9db
NB
417 spin_unlock(&dev->se_port_lock);
418
de103c93 419 if (!core_alua_set_tg_pt_secondary_state(
adf653f9 420 lun, 1, 1))
de103c93 421 found = true;
c66ac9db
NB
422
423 spin_lock(&dev->se_port_lock);
424 break;
425 }
426 spin_unlock(&dev->se_port_lock);
de103c93
CH
427 }
428
429 if (!found) {
430 rc = TCM_INVALID_PARAMETER_LIST;
431 goto out;
c66ac9db
NB
432 }
433
434 ptr += 4;
435 len += 4;
436 }
437
05d1c7c0 438out:
4949314c 439 transport_kunmap_data_sg(cmd);
59e4f541
RD
440 if (!rc)
441 target_complete_cmd(cmd, GOOD);
442 return rc;
c66ac9db
NB
443}
444
1e3ab99d
PB
445static inline void set_ascq(struct se_cmd *cmd, u8 alua_ascq)
446{
447 /*
448 * Set SCSI additional sense code (ASC) to 'LUN Not Accessible';
449 * The ALUA additional sense code qualifier (ASCQ) is determined
450 * by the ALUA primary or secondary access state..
451 */
452 pr_debug("[%s]: ALUA TG Port not available, "
453 "SenseKey: NOT_READY, ASC/ASCQ: "
454 "0x04/0x%02x\n",
455 cmd->se_tfo->get_fabric_name(), alua_ascq);
456
457 cmd->scsi_asc = 0x04;
458 cmd->scsi_ascq = alua_ascq;
459}
460
461static inline void core_alua_state_nonoptimized(
c66ac9db
NB
462 struct se_cmd *cmd,
463 unsigned char *cdb,
1e3ab99d 464 int nonop_delay_msecs)
c66ac9db
NB
465{
466 /*
467 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
468 * later to determine if processing of this cmd needs to be
469 * temporarily delayed for the Active/NonOptimized primary access state.
470 */
471 cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
472 cmd->alua_nonop_delay = nonop_delay_msecs;
c66ac9db
NB
473}
474
c66094bf
HR
475static inline int core_alua_state_lba_dependent(
476 struct se_cmd *cmd,
1e3ab99d 477 struct t10_alua_tg_pt_gp *tg_pt_gp)
c66094bf
HR
478{
479 struct se_device *dev = cmd->se_dev;
480 u64 segment_size, segment_mult, sectors, lba;
481
482 /* Only need to check for cdb actually containing LBAs */
483 if (!(cmd->se_cmd_flags & SCF_SCSI_DATA_CDB))
484 return 0;
485
486 spin_lock(&dev->t10_alua.lba_map_lock);
487 segment_size = dev->t10_alua.lba_map_segment_size;
488 segment_mult = dev->t10_alua.lba_map_segment_multiplier;
489 sectors = cmd->data_length / dev->dev_attrib.block_size;
490
491 lba = cmd->t_task_lba;
492 while (lba < cmd->t_task_lba + sectors) {
493 struct t10_alua_lba_map *cur_map = NULL, *map;
494 struct t10_alua_lba_map_member *map_mem;
495
496 list_for_each_entry(map, &dev->t10_alua.lba_map_list,
497 lba_map_list) {
498 u64 start_lba, last_lba;
499 u64 first_lba = map->lba_map_first_lba;
500
501 if (segment_mult) {
502 u64 tmp = lba;
cdf55949 503 start_lba = do_div(tmp, segment_size * segment_mult);
c66094bf
HR
504
505 last_lba = first_lba + segment_size - 1;
506 if (start_lba >= first_lba &&
507 start_lba <= last_lba) {
508 lba += segment_size;
509 cur_map = map;
510 break;
511 }
512 } else {
513 last_lba = map->lba_map_last_lba;
514 if (lba >= first_lba && lba <= last_lba) {
515 lba = last_lba + 1;
516 cur_map = map;
517 break;
518 }
519 }
520 }
521 if (!cur_map) {
522 spin_unlock(&dev->t10_alua.lba_map_lock);
1e3ab99d 523 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
c66094bf
HR
524 return 1;
525 }
526 list_for_each_entry(map_mem, &cur_map->lba_map_mem_list,
527 lba_map_mem_list) {
528 if (map_mem->lba_map_mem_alua_pg_id !=
529 tg_pt_gp->tg_pt_gp_id)
530 continue;
531 switch(map_mem->lba_map_mem_alua_state) {
532 case ALUA_ACCESS_STATE_STANDBY:
533 spin_unlock(&dev->t10_alua.lba_map_lock);
1e3ab99d 534 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
c66094bf
HR
535 return 1;
536 case ALUA_ACCESS_STATE_UNAVAILABLE:
537 spin_unlock(&dev->t10_alua.lba_map_lock);
1e3ab99d 538 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
c66094bf
HR
539 return 1;
540 default:
541 break;
542 }
543 }
544 }
545 spin_unlock(&dev->t10_alua.lba_map_lock);
546 return 0;
547}
548
c66ac9db
NB
549static inline int core_alua_state_standby(
550 struct se_cmd *cmd,
1e3ab99d 551 unsigned char *cdb)
c66ac9db
NB
552{
553 /*
554 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
555 * spc4r17 section 5.9.2.4.4
556 */
557 switch (cdb[0]) {
558 case INQUIRY:
559 case LOG_SELECT:
560 case LOG_SENSE:
561 case MODE_SELECT:
562 case MODE_SENSE:
563 case REPORT_LUNS:
564 case RECEIVE_DIAGNOSTIC:
565 case SEND_DIAGNOSTIC:
e7810c2d 566 case READ_CAPACITY:
30f359a6 567 return 0;
eb846d9f 568 case SERVICE_ACTION_IN_16:
e7810c2d
NB
569 switch (cdb[1] & 0x1f) {
570 case SAI_READ_CAPACITY_16:
571 return 0;
572 default:
573 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
574 return 1;
575 }
c66ac9db 576 case MAINTENANCE_IN:
ba539743 577 switch (cdb[1] & 0x1f) {
c66ac9db
NB
578 case MI_REPORT_TARGET_PGS:
579 return 0;
580 default:
1e3ab99d 581 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
c66ac9db
NB
582 return 1;
583 }
584 case MAINTENANCE_OUT:
585 switch (cdb[1]) {
586 case MO_SET_TARGET_PGS:
587 return 0;
588 default:
1e3ab99d 589 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
c66ac9db
NB
590 return 1;
591 }
592 case REQUEST_SENSE:
593 case PERSISTENT_RESERVE_IN:
594 case PERSISTENT_RESERVE_OUT:
595 case READ_BUFFER:
596 case WRITE_BUFFER:
597 return 0;
598 default:
1e3ab99d 599 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_STANDBY);
c66ac9db
NB
600 return 1;
601 }
602
603 return 0;
604}
605
606static inline int core_alua_state_unavailable(
607 struct se_cmd *cmd,
1e3ab99d 608 unsigned char *cdb)
c66ac9db
NB
609{
610 /*
611 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
612 * spc4r17 section 5.9.2.4.5
613 */
614 switch (cdb[0]) {
615 case INQUIRY:
616 case REPORT_LUNS:
30f359a6 617 return 0;
c66ac9db 618 case MAINTENANCE_IN:
ba539743 619 switch (cdb[1] & 0x1f) {
c66ac9db
NB
620 case MI_REPORT_TARGET_PGS:
621 return 0;
622 default:
1e3ab99d 623 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
c66ac9db
NB
624 return 1;
625 }
626 case MAINTENANCE_OUT:
627 switch (cdb[1]) {
628 case MO_SET_TARGET_PGS:
629 return 0;
630 default:
1e3ab99d 631 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
c66ac9db
NB
632 return 1;
633 }
634 case REQUEST_SENSE:
635 case READ_BUFFER:
636 case WRITE_BUFFER:
637 return 0;
638 default:
1e3ab99d 639 set_ascq(cmd, ASCQ_04H_ALUA_TG_PT_UNAVAILABLE);
c66ac9db
NB
640 return 1;
641 }
642
643 return 0;
644}
645
646static inline int core_alua_state_transition(
647 struct se_cmd *cmd,
1e3ab99d 648 unsigned char *cdb)
c66ac9db
NB
649{
650 /*
f1ae05d5 651 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITION as defined by
c66ac9db
NB
652 * spc4r17 section 5.9.2.5
653 */
654 switch (cdb[0]) {
655 case INQUIRY:
656 case REPORT_LUNS:
30f359a6 657 return 0;
c66ac9db 658 case MAINTENANCE_IN:
ba539743 659 switch (cdb[1] & 0x1f) {
c66ac9db
NB
660 case MI_REPORT_TARGET_PGS:
661 return 0;
662 default:
1e3ab99d 663 set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
c66ac9db
NB
664 return 1;
665 }
666 case REQUEST_SENSE:
667 case READ_BUFFER:
668 case WRITE_BUFFER:
669 return 0;
670 default:
1e3ab99d 671 set_ascq(cmd, ASCQ_04H_ALUA_STATE_TRANSITION);
c66ac9db
NB
672 return 1;
673 }
674
675 return 0;
676}
677
678/*
f1ae05d5 679 * return 1: Is used to signal LUN not accessible, and check condition/not ready
c66ac9db 680 * return 0: Used to signal success
f1ae05d5 681 * return -1: Used to signal failure, and invalid cdb field
c66ac9db 682 */
de103c93
CH
683sense_reason_t
684target_alua_state_check(struct se_cmd *cmd)
c66ac9db 685{
c87fbd56
CH
686 struct se_device *dev = cmd->se_dev;
687 unsigned char *cdb = cmd->t_task_cdb;
e3d6f909 688 struct se_lun *lun = cmd->se_lun;
c66ac9db 689 struct t10_alua_tg_pt_gp *tg_pt_gp;
c66ac9db 690 int out_alua_state, nonop_delay_msecs;
c87fbd56
CH
691
692 if (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)
693 return 0;
530c6891 694 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
c87fbd56 695 return 0;
c66ac9db 696
c66ac9db
NB
697 /*
698 * First, check for a struct se_port specific secondary ALUA target port
699 * access state: OFFLINE
700 */
adf653f9 701 if (atomic_read(&lun->lun_tg_pt_secondary_offline)) {
6708bb27 702 pr_debug("ALUA: Got secondary offline status for local"
c66ac9db 703 " target port\n");
1e3ab99d
PB
704 set_ascq(cmd, ASCQ_04H_ALUA_OFFLINE);
705 return TCM_CHECK_CONDITION_NOT_READY;
c66ac9db 706 }
adf653f9
CH
707
708 if (!lun->lun_tg_pt_gp)
89c12cc9
NB
709 return 0;
710
adf653f9
CH
711 spin_lock(&lun->lun_tg_pt_gp_lock);
712 tg_pt_gp = lun->lun_tg_pt_gp;
c66ac9db
NB
713 out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
714 nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
adf653f9
CH
715
716 // XXX: keeps using tg_pt_gp witout reference after unlock
717 spin_unlock(&lun->lun_tg_pt_gp_lock);
c66ac9db 718 /*
73f3bf51 719 * Process ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED in a separate conditional
25985edc 720 * statement so the compiler knows explicitly to check this case first.
c66ac9db
NB
721 * For the Optimized ALUA access state case, we want to process the
722 * incoming fabric cmd ASAP..
723 */
73f3bf51 724 if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED)
c66ac9db
NB
725 return 0;
726
727 switch (out_alua_state) {
728 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
1e3ab99d 729 core_alua_state_nonoptimized(cmd, cdb, nonop_delay_msecs);
c87fbd56 730 break;
c66ac9db 731 case ALUA_ACCESS_STATE_STANDBY:
1e3ab99d
PB
732 if (core_alua_state_standby(cmd, cdb))
733 return TCM_CHECK_CONDITION_NOT_READY;
c87fbd56 734 break;
c66ac9db 735 case ALUA_ACCESS_STATE_UNAVAILABLE:
1e3ab99d
PB
736 if (core_alua_state_unavailable(cmd, cdb))
737 return TCM_CHECK_CONDITION_NOT_READY;
c87fbd56 738 break;
c66ac9db 739 case ALUA_ACCESS_STATE_TRANSITION:
1e3ab99d
PB
740 if (core_alua_state_transition(cmd, cdb))
741 return TCM_CHECK_CONDITION_NOT_READY;
c87fbd56 742 break;
c66094bf 743 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
1e3ab99d
PB
744 if (core_alua_state_lba_dependent(cmd, tg_pt_gp))
745 return TCM_CHECK_CONDITION_NOT_READY;
c66094bf 746 break;
c66ac9db
NB
747 /*
748 * OFFLINE is a secondary ALUA target port group access state, that is
adf653f9 749 * handled above with struct se_lun->lun_tg_pt_secondary_offline=1
c66ac9db
NB
750 */
751 case ALUA_ACCESS_STATE_OFFLINE:
752 default:
6708bb27 753 pr_err("Unknown ALUA access state: 0x%02x\n",
c66ac9db 754 out_alua_state);
de103c93 755 return TCM_INVALID_CDB_FIELD;
c66ac9db
NB
756 }
757
de103c93 758 return 0;
c66ac9db
NB
759}
760
761/*
125d0119 762 * Check implicit and explicit ALUA state change request.
c66ac9db 763 */
de103c93 764static sense_reason_t
1ca4d4fa 765core_alua_check_transition(int state, int valid, int *primary, int explicit)
c66ac9db 766{
bb91c1a0
HR
767 /*
768 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
769 * defined as primary target port asymmetric access states.
770 */
c66ac9db 771 switch (state) {
73f3bf51 772 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
bb91c1a0
HR
773 if (!(valid & ALUA_AO_SUP))
774 goto not_supported;
775 *primary = 1;
776 break;
c66ac9db 777 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
bb91c1a0
HR
778 if (!(valid & ALUA_AN_SUP))
779 goto not_supported;
780 *primary = 1;
781 break;
c66ac9db 782 case ALUA_ACCESS_STATE_STANDBY:
bb91c1a0
HR
783 if (!(valid & ALUA_S_SUP))
784 goto not_supported;
785 *primary = 1;
786 break;
c66ac9db 787 case ALUA_ACCESS_STATE_UNAVAILABLE:
bb91c1a0
HR
788 if (!(valid & ALUA_U_SUP))
789 goto not_supported;
c66ac9db
NB
790 *primary = 1;
791 break;
c66094bf
HR
792 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
793 if (!(valid & ALUA_LBD_SUP))
794 goto not_supported;
795 *primary = 1;
796 break;
c66ac9db
NB
797 case ALUA_ACCESS_STATE_OFFLINE:
798 /*
799 * OFFLINE state is defined as a secondary target port
800 * asymmetric access state.
801 */
bb91c1a0
HR
802 if (!(valid & ALUA_O_SUP))
803 goto not_supported;
c66ac9db
NB
804 *primary = 0;
805 break;
bb91c1a0 806 case ALUA_ACCESS_STATE_TRANSITION:
1ca4d4fa
MC
807 if (!(valid & ALUA_T_SUP) || explicit)
808 /*
809 * Transitioning is set internally and by tcmu daemon,
810 * and cannot be selected through a STPG.
811 */
812 goto not_supported;
813 *primary = 0;
814 break;
c66ac9db 815 default:
6708bb27 816 pr_err("Unknown ALUA access state: 0x%02x\n", state);
de103c93 817 return TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
818 }
819
820 return 0;
bb91c1a0
HR
821
822not_supported:
823 pr_err("ALUA access state %s not supported",
824 core_alua_dump_state(state));
825 return TCM_INVALID_PARAMETER_LIST;
c66ac9db
NB
826}
827
828static char *core_alua_dump_state(int state)
829{
830 switch (state) {
73f3bf51 831 case ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED:
c66ac9db
NB
832 return "Active/Optimized";
833 case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
834 return "Active/NonOptimized";
c66094bf
HR
835 case ALUA_ACCESS_STATE_LBA_DEPENDENT:
836 return "LBA Dependent";
c66ac9db
NB
837 case ALUA_ACCESS_STATE_STANDBY:
838 return "Standby";
839 case ALUA_ACCESS_STATE_UNAVAILABLE:
840 return "Unavailable";
841 case ALUA_ACCESS_STATE_OFFLINE:
842 return "Offline";
bb91c1a0
HR
843 case ALUA_ACCESS_STATE_TRANSITION:
844 return "Transitioning";
c66ac9db
NB
845 default:
846 return "Unknown";
847 }
848
849 return NULL;
850}
851
852char *core_alua_dump_status(int status)
853{
854 switch (status) {
855 case ALUA_STATUS_NONE:
856 return "None";
125d0119
HR
857 case ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG:
858 return "Altered by Explicit STPG";
859 case ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA:
860 return "Altered by Implicit ALUA";
c66ac9db
NB
861 default:
862 return "Unknown";
863 }
864
865 return NULL;
866}
867
868/*
869 * Used by fabric modules to determine when we need to delay processing
870 * for the Active/NonOptimized paths..
871 */
872int core_alua_check_nonop_delay(
873 struct se_cmd *cmd)
874{
875 if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
876 return 0;
877 if (in_interrupt())
878 return 0;
879 /*
880 * The ALUA Active/NonOptimized access state delay can be disabled
881 * in via configfs with a value of zero
882 */
6708bb27 883 if (!cmd->alua_nonop_delay)
c66ac9db
NB
884 return 0;
885 /*
886 * struct se_cmd->alua_nonop_delay gets set by a target port group
887 * defined interval in core_alua_state_nonoptimized()
888 */
889 msleep_interruptible(cmd->alua_nonop_delay);
890 return 0;
891}
892EXPORT_SYMBOL(core_alua_check_nonop_delay);
893
c66ac9db
NB
894static int core_alua_write_tpg_metadata(
895 const char *path,
896 unsigned char *md_buf,
897 u32 md_buf_len)
898{
0e9b10a9
AV
899 struct file *file = filp_open(path, O_RDWR | O_CREAT | O_TRUNC, 0600);
900 int ret;
c66ac9db 901
0e9b10a9
AV
902 if (IS_ERR(file)) {
903 pr_err("filp_open(%s) for ALUA metadata failed\n", path);
c66ac9db
NB
904 return -ENODEV;
905 }
0e9b10a9
AV
906 ret = kernel_write(file, md_buf, md_buf_len, 0);
907 if (ret < 0)
6708bb27 908 pr_err("Error writing ALUA metadata file: %s\n", path);
0e9b10a9 909 fput(file);
f730f915 910 return (ret < 0) ? -EIO : 0;
c66ac9db
NB
911}
912
913/*
914 * Called with tg_pt_gp->tg_pt_gp_md_mutex held
915 */
916static int core_alua_update_tpg_primary_metadata(
9c6e164c 917 struct t10_alua_tg_pt_gp *tg_pt_gp)
c66ac9db 918{
1e0b9403 919 unsigned char *md_buf;
0fd97ccf 920 struct t10_wwn *wwn = &tg_pt_gp->tg_pt_gp_dev->t10_wwn;
c66ac9db 921 char path[ALUA_METADATA_PATH_LEN];
1e0b9403
HR
922 int len, rc;
923
924 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
925 if (!md_buf) {
926 pr_err("Unable to allocate buf for ALUA metadata\n");
927 return -ENOMEM;
928 }
c66ac9db
NB
929
930 memset(path, 0, ALUA_METADATA_PATH_LEN);
931
1e0b9403 932 len = snprintf(md_buf, ALUA_MD_BUF_LEN,
c66ac9db
NB
933 "tg_pt_gp_id=%hu\n"
934 "alua_access_state=0x%02x\n"
935 "alua_access_status=0x%02x\n",
9c6e164c
HR
936 tg_pt_gp->tg_pt_gp_id,
937 tg_pt_gp->tg_pt_gp_alua_pending_state,
c66ac9db
NB
938 tg_pt_gp->tg_pt_gp_alua_access_status);
939
940 snprintf(path, ALUA_METADATA_PATH_LEN,
fdddf932 941 "%s/alua/tpgs_%s/%s", db_root, &wwn->unit_serial[0],
c66ac9db
NB
942 config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
943
1e0b9403
HR
944 rc = core_alua_write_tpg_metadata(path, md_buf, len);
945 kfree(md_buf);
946 return rc;
c66ac9db
NB
947}
948
ee8c07d0 949static void core_alua_queue_state_change_ua(struct t10_alua_tg_pt_gp *tg_pt_gp)
c66ac9db
NB
950{
951 struct se_dev_entry *se_deve;
adf653f9 952 struct se_lun *lun;
c66ac9db 953 struct se_lun_acl *lacl;
c66ac9db
NB
954
955 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
adf653f9
CH
956 list_for_each_entry(lun, &tg_pt_gp->tg_pt_gp_lun_list,
957 lun_tg_pt_gp_link) {
c66ac9db
NB
958 /*
959 * After an implicit target port asymmetric access state
960 * change, a device server shall establish a unit attention
961 * condition for the initiator port associated with every I_T
962 * nexus with the additional sense code set to ASYMMETRIC
f1ae05d5 963 * ACCESS STATE CHANGED.
c66ac9db
NB
964 *
965 * After an explicit target port asymmetric access state
966 * change, a device server shall establish a unit attention
967 * condition with the additional sense code set to ASYMMETRIC
968 * ACCESS STATE CHANGED for the initiator port associated with
969 * every I_T nexus other than the I_T nexus on which the SET
970 * TARGET PORT GROUPS command
971 */
9e37d042
NB
972 if (!percpu_ref_tryget_live(&lun->lun_ref))
973 continue;
c66ac9db
NB
974 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
975
1adff1b3 976 spin_lock(&lun->lun_deve_lock);
adf653f9 977 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link) {
29a05dee 978 lacl = rcu_dereference_check(se_deve->se_lun_acl,
adf653f9 979 lockdep_is_held(&lun->lun_deve_lock));
c51c8e7b 980
c66ac9db 981 /*
c51c8e7b
HR
982 * spc4r37 p.242:
983 * After an explicit target port asymmetric access
984 * state change, a device server shall establish a
985 * unit attention condition with the additional sense
986 * code set to ASYMMETRIC ACCESS STATE CHANGED for
987 * the initiator port associated with every I_T nexus
988 * other than the I_T nexus on which the SET TARGET
989 * PORT GROUPS command was received.
c66ac9db 990 */
9c6e164c
HR
991 if ((tg_pt_gp->tg_pt_gp_alua_access_status ==
992 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
adf653f9
CH
993 (tg_pt_gp->tg_pt_gp_alua_lun != NULL) &&
994 (tg_pt_gp->tg_pt_gp_alua_lun == lun))
c66ac9db
NB
995 continue;
996
c51c8e7b
HR
997 /*
998 * se_deve->se_lun_acl pointer may be NULL for a
999 * entry created without explicit Node+MappedLUN ACLs
1000 */
1001 if (lacl && (tg_pt_gp->tg_pt_gp_alua_nacl != NULL) &&
1002 (tg_pt_gp->tg_pt_gp_alua_nacl == lacl->se_lun_nacl))
1003 continue;
1004
1005 core_scsi3_ua_allocate(se_deve, 0x2A,
c66ac9db
NB
1006 ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
1007 }
1adff1b3 1008 spin_unlock(&lun->lun_deve_lock);
c66ac9db
NB
1009
1010 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
9e37d042 1011 percpu_ref_put(&lun->lun_ref);
c66ac9db
NB
1012 }
1013 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
ee8c07d0
HR
1014}
1015
1016static void core_alua_do_transition_tg_pt_work(struct work_struct *work)
1017{
1018 struct t10_alua_tg_pt_gp *tg_pt_gp = container_of(work,
d7175373 1019 struct t10_alua_tg_pt_gp, tg_pt_gp_transition_work);
ee8c07d0
HR
1020 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1021 bool explicit = (tg_pt_gp->tg_pt_gp_alua_access_status ==
1022 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG);
1023
c66ac9db
NB
1024 /*
1025 * Update the ALUA metadata buf that has been allocated in
1026 * core_alua_do_port_transition(), this metadata will be written
1027 * to struct file.
1028 *
1029 * Note that there is the case where we do not want to update the
1030 * metadata when the saved metadata is being parsed in userspace
1031 * when setting the existing port access state and access status.
1032 *
1033 * Also note that the failure to write out the ALUA metadata to
1034 * struct file does NOT affect the actual ALUA transition.
1035 */
1036 if (tg_pt_gp->tg_pt_gp_write_metadata) {
1037 mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
9c6e164c 1038 core_alua_update_tpg_primary_metadata(tg_pt_gp);
c66ac9db
NB
1039 mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
1040 }
1041 /*
1042 * Set the current primary ALUA access state to the requested new state
1043 */
dfbce75a
HR
1044 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1045 tg_pt_gp->tg_pt_gp_alua_pending_state);
c66ac9db 1046
6708bb27 1047 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
125d0119
HR
1048 " from primary access state %s to %s\n", (explicit) ? "explicit" :
1049 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
dfbce75a
HR
1050 tg_pt_gp->tg_pt_gp_id,
1051 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_previous_state),
1052 core_alua_dump_state(tg_pt_gp->tg_pt_gp_alua_pending_state));
ee8c07d0
HR
1053
1054 core_alua_queue_state_change_ua(tg_pt_gp);
1055
9c6e164c
HR
1056 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1057 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
9c6e164c
HR
1058 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1059
1060 if (tg_pt_gp->tg_pt_gp_transition_complete)
1061 complete(tg_pt_gp->tg_pt_gp_transition_complete);
1062}
1063
1064static int core_alua_do_transition_tg_pt(
1065 struct t10_alua_tg_pt_gp *tg_pt_gp,
1066 int new_state,
1067 int explicit)
1068{
1069 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
1070 DECLARE_COMPLETION_ONSTACK(wait);
1071
1072 /* Nothing to be done here */
1073 if (atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state) == new_state)
1074 return 0;
1075
1ca4d4fa 1076 if (explicit && new_state == ALUA_ACCESS_STATE_TRANSITION)
9c6e164c
HR
1077 return -EAGAIN;
1078
1079 /*
1080 * Flush any pending transitions
1081 */
760bf578 1082 if (!explicit)
d7175373 1083 flush_work(&tg_pt_gp->tg_pt_gp_transition_work);
9c6e164c
HR
1084
1085 /*
1086 * Save the old primary ALUA access state, and set the current state
1087 * to ALUA_ACCESS_STATE_TRANSITION.
1088 */
9c6e164c
HR
1089 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
1090 ALUA_ACCESS_STATE_TRANSITION);
1091 tg_pt_gp->tg_pt_gp_alua_access_status = (explicit) ?
1092 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1093 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
1094
ee8c07d0
HR
1095 core_alua_queue_state_change_ua(tg_pt_gp);
1096
1ca4d4fa
MC
1097 if (new_state == ALUA_ACCESS_STATE_TRANSITION)
1098 return 0;
1099
1100 tg_pt_gp->tg_pt_gp_alua_previous_state =
1101 atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
1102 tg_pt_gp->tg_pt_gp_alua_pending_state = new_state;
1103
9c6e164c
HR
1104 /*
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);
9c6e164c
HR
1115 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1116
d7175373
MC
1117 schedule_work(&tg_pt_gp->tg_pt_gp_transition_work);
1118 if (explicit) {
9c6e164c 1119 tg_pt_gp->tg_pt_gp_transition_complete = &wait;
9c6e164c
HR
1120 wait_for_completion(&wait);
1121 tg_pt_gp->tg_pt_gp_transition_complete = NULL;
1122 }
c66ac9db
NB
1123
1124 return 0;
1125}
1126
1127int core_alua_do_port_transition(
1128 struct t10_alua_tg_pt_gp *l_tg_pt_gp,
1129 struct se_device *l_dev,
adf653f9 1130 struct se_lun *l_lun,
c66ac9db
NB
1131 struct se_node_acl *l_nacl,
1132 int new_state,
125d0119 1133 int explicit)
c66ac9db
NB
1134{
1135 struct se_device *dev;
c66ac9db
NB
1136 struct t10_alua_lu_gp *lu_gp;
1137 struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
1138 struct t10_alua_tg_pt_gp *tg_pt_gp;
9c6e164c 1139 int primary, valid_states, rc = 0;
c66ac9db 1140
0a414572
MC
1141 if (l_dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA)
1142 return -ENODEV;
1143
bb91c1a0 1144 valid_states = l_tg_pt_gp->tg_pt_gp_alua_supported_states;
1ca4d4fa
MC
1145 if (core_alua_check_transition(new_state, valid_states, &primary,
1146 explicit) != 0)
c66ac9db
NB
1147 return -EINVAL;
1148
c66ac9db
NB
1149 local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
1150 spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
1151 lu_gp = local_lu_gp_mem->lu_gp;
1152 atomic_inc(&lu_gp->lu_gp_ref_cnt);
c66ac9db
NB
1153 spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
1154 /*
1155 * For storage objects that are members of the 'default_lu_gp',
1156 * we only do transition on the passed *l_tp_pt_gp, and not
1157 * on all of the matching target port groups IDs in default_lu_gp.
1158 */
6708bb27 1159 if (!lu_gp->lu_gp_id) {
c66ac9db
NB
1160 /*
1161 * core_alua_do_transition_tg_pt() will always return
1162 * success.
1163 */
adf653f9 1164 l_tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
9c6e164c
HR
1165 l_tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
1166 rc = core_alua_do_transition_tg_pt(l_tg_pt_gp,
1167 new_state, explicit);
33940d09 1168 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
9c6e164c 1169 return rc;
c66ac9db
NB
1170 }
1171 /*
1172 * For all other LU groups aside from 'default_lu_gp', walk all of
1173 * the associated storage objects looking for a matching target port
1174 * group ID from the local target port group.
1175 */
1176 spin_lock(&lu_gp->lu_gp_lock);
1177 list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
1178 lu_gp_mem_list) {
1179
1180 dev = lu_gp_mem->lu_gp_mem_dev;
33940d09 1181 atomic_inc_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
c66ac9db
NB
1182 spin_unlock(&lu_gp->lu_gp_lock);
1183
0fd97ccf 1184 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db 1185 list_for_each_entry(tg_pt_gp,
0fd97ccf 1186 &dev->t10_alua.tg_pt_gps_list,
c66ac9db
NB
1187 tg_pt_gp_list) {
1188
6708bb27 1189 if (!tg_pt_gp->tg_pt_gp_valid_id)
c66ac9db
NB
1190 continue;
1191 /*
1192 * If the target behavior port asymmetric access state
f1ae05d5 1193 * is changed for any target port group accessible via
c66ac9db
NB
1194 * a logical unit within a LU group, the target port
1195 * behavior group asymmetric access states for the same
1196 * target port group accessible via other logical units
1197 * in that LU group will also change.
1198 */
1199 if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
1200 continue;
1201
1202 if (l_tg_pt_gp == tg_pt_gp) {
adf653f9 1203 tg_pt_gp->tg_pt_gp_alua_lun = l_lun;
9c6e164c 1204 tg_pt_gp->tg_pt_gp_alua_nacl = l_nacl;
c66ac9db 1205 } else {
adf653f9 1206 tg_pt_gp->tg_pt_gp_alua_lun = NULL;
9c6e164c 1207 tg_pt_gp->tg_pt_gp_alua_nacl = NULL;
c66ac9db 1208 }
33940d09 1209 atomic_inc_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
0fd97ccf 1210 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db
NB
1211 /*
1212 * core_alua_do_transition_tg_pt() will always return
1213 * success.
1214 */
9c6e164c
HR
1215 rc = core_alua_do_transition_tg_pt(tg_pt_gp,
1216 new_state, explicit);
c66ac9db 1217
0fd97ccf 1218 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
33940d09 1219 atomic_dec_mb(&tg_pt_gp->tg_pt_gp_ref_cnt);
9c6e164c
HR
1220 if (rc)
1221 break;
c66ac9db 1222 }
0fd97ccf 1223 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db
NB
1224
1225 spin_lock(&lu_gp->lu_gp_lock);
33940d09 1226 atomic_dec_mb(&lu_gp_mem->lu_gp_mem_ref_cnt);
c66ac9db
NB
1227 }
1228 spin_unlock(&lu_gp->lu_gp_lock);
1229
9c6e164c
HR
1230 if (!rc) {
1231 pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
1232 " Group IDs: %hu %s transition to primary state: %s\n",
1233 config_item_name(&lu_gp->lu_gp_group.cg_item),
1234 l_tg_pt_gp->tg_pt_gp_id,
1235 (explicit) ? "explicit" : "implicit",
1236 core_alua_dump_state(new_state));
1237 }
c66ac9db 1238
33940d09 1239 atomic_dec_mb(&lu_gp->lu_gp_ref_cnt);
9c6e164c 1240 return rc;
c66ac9db
NB
1241}
1242
adf653f9 1243static int core_alua_update_tpg_secondary_metadata(struct se_lun *lun)
c66ac9db 1244{
adf653f9 1245 struct se_portal_group *se_tpg = lun->lun_tpg;
1e0b9403 1246 unsigned char *md_buf;
c66ac9db 1247 char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
1e0b9403
HR
1248 int len, rc;
1249
adf653f9
CH
1250 mutex_lock(&lun->lun_tg_pt_md_mutex);
1251
1e0b9403
HR
1252 md_buf = kzalloc(ALUA_MD_BUF_LEN, GFP_KERNEL);
1253 if (!md_buf) {
1254 pr_err("Unable to allocate buf for ALUA metadata\n");
adf653f9
CH
1255 rc = -ENOMEM;
1256 goto out_unlock;
1e0b9403 1257 }
c66ac9db
NB
1258
1259 memset(path, 0, ALUA_METADATA_PATH_LEN);
1260 memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
1261
1262 len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
e3d6f909 1263 se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
c66ac9db 1264
e3d6f909 1265 if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
c66ac9db 1266 snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
e3d6f909 1267 se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
c66ac9db 1268
1e0b9403 1269 len = snprintf(md_buf, ALUA_MD_BUF_LEN, "alua_tg_pt_offline=%d\n"
c66ac9db 1270 "alua_tg_pt_status=0x%02x\n",
adf653f9
CH
1271 atomic_read(&lun->lun_tg_pt_secondary_offline),
1272 lun->lun_tg_pt_secondary_stat);
c66ac9db 1273
fdddf932
LD
1274 snprintf(path, ALUA_METADATA_PATH_LEN, "%s/alua/%s/%s/lun_%llu",
1275 db_root, se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
adf653f9 1276 lun->unpacked_lun);
c66ac9db 1277
1e0b9403
HR
1278 rc = core_alua_write_tpg_metadata(path, md_buf, len);
1279 kfree(md_buf);
1280
adf653f9
CH
1281out_unlock:
1282 mutex_unlock(&lun->lun_tg_pt_md_mutex);
1e0b9403 1283 return rc;
c66ac9db
NB
1284}
1285
1286static int core_alua_set_tg_pt_secondary_state(
adf653f9 1287 struct se_lun *lun,
125d0119 1288 int explicit,
c66ac9db
NB
1289 int offline)
1290{
1291 struct t10_alua_tg_pt_gp *tg_pt_gp;
c66ac9db
NB
1292 int trans_delay_msecs;
1293
adf653f9
CH
1294 spin_lock(&lun->lun_tg_pt_gp_lock);
1295 tg_pt_gp = lun->lun_tg_pt_gp;
6708bb27 1296 if (!tg_pt_gp) {
adf653f9 1297 spin_unlock(&lun->lun_tg_pt_gp_lock);
6708bb27 1298 pr_err("Unable to complete secondary state"
c66ac9db 1299 " transition\n");
e3d6f909 1300 return -EINVAL;
c66ac9db
NB
1301 }
1302 trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
1303 /*
1304 * Set the secondary ALUA target port access state to OFFLINE
adf653f9 1305 * or release the previously secondary state for struct se_lun
c66ac9db
NB
1306 */
1307 if (offline)
adf653f9 1308 atomic_set(&lun->lun_tg_pt_secondary_offline, 1);
c66ac9db 1309 else
adf653f9 1310 atomic_set(&lun->lun_tg_pt_secondary_offline, 0);
c66ac9db 1311
adf653f9 1312 lun->lun_tg_pt_secondary_stat = (explicit) ?
125d0119
HR
1313 ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG :
1314 ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA;
c66ac9db 1315
6708bb27 1316 pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
125d0119
HR
1317 " to secondary access state: %s\n", (explicit) ? "explicit" :
1318 "implicit", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
c66ac9db
NB
1319 tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
1320
adf653f9 1321 spin_unlock(&lun->lun_tg_pt_gp_lock);
c66ac9db
NB
1322 /*
1323 * Do the optional transition delay after we set the secondary
1324 * ALUA access state.
1325 */
1326 if (trans_delay_msecs != 0)
1327 msleep_interruptible(trans_delay_msecs);
1328 /*
1329 * See if we need to update the ALUA fabric port metadata for
1330 * secondary state and status
1331 */
adf653f9
CH
1332 if (lun->lun_tg_pt_secondary_write_md)
1333 core_alua_update_tpg_secondary_metadata(lun);
c66ac9db
NB
1334
1335 return 0;
1336}
1337
229d4f11
HR
1338struct t10_alua_lba_map *
1339core_alua_allocate_lba_map(struct list_head *list,
1340 u64 first_lba, u64 last_lba)
1341{
1342 struct t10_alua_lba_map *lba_map;
1343
1344 lba_map = kmem_cache_zalloc(t10_alua_lba_map_cache, GFP_KERNEL);
1345 if (!lba_map) {
1346 pr_err("Unable to allocate struct t10_alua_lba_map\n");
1347 return ERR_PTR(-ENOMEM);
1348 }
1349 INIT_LIST_HEAD(&lba_map->lba_map_mem_list);
1350 lba_map->lba_map_first_lba = first_lba;
1351 lba_map->lba_map_last_lba = last_lba;
1352
1353 list_add_tail(&lba_map->lba_map_list, list);
1354 return lba_map;
1355}
1356
1357int
1358core_alua_allocate_lba_map_mem(struct t10_alua_lba_map *lba_map,
1359 int pg_id, int state)
1360{
1361 struct t10_alua_lba_map_member *lba_map_mem;
1362
1363 list_for_each_entry(lba_map_mem, &lba_map->lba_map_mem_list,
1364 lba_map_mem_list) {
1365 if (lba_map_mem->lba_map_mem_alua_pg_id == pg_id) {
1366 pr_err("Duplicate pg_id %d in lba_map\n", pg_id);
1367 return -EINVAL;
1368 }
1369 }
1370
1371 lba_map_mem = kmem_cache_zalloc(t10_alua_lba_map_mem_cache, GFP_KERNEL);
1372 if (!lba_map_mem) {
1373 pr_err("Unable to allocate struct t10_alua_lba_map_mem\n");
1374 return -ENOMEM;
1375 }
1376 lba_map_mem->lba_map_mem_alua_state = state;
1377 lba_map_mem->lba_map_mem_alua_pg_id = pg_id;
1378
1379 list_add_tail(&lba_map_mem->lba_map_mem_list,
1380 &lba_map->lba_map_mem_list);
1381 return 0;
1382}
1383
1384void
1385core_alua_free_lba_map(struct list_head *lba_list)
1386{
1387 struct t10_alua_lba_map *lba_map, *lba_map_tmp;
1388 struct t10_alua_lba_map_member *lba_map_mem, *lba_map_mem_tmp;
1389
1390 list_for_each_entry_safe(lba_map, lba_map_tmp, lba_list,
1391 lba_map_list) {
1392 list_for_each_entry_safe(lba_map_mem, lba_map_mem_tmp,
1393 &lba_map->lba_map_mem_list,
1394 lba_map_mem_list) {
1395 list_del(&lba_map_mem->lba_map_mem_list);
1396 kmem_cache_free(t10_alua_lba_map_mem_cache,
1397 lba_map_mem);
1398 }
1399 list_del(&lba_map->lba_map_list);
1400 kmem_cache_free(t10_alua_lba_map_cache, lba_map);
1401 }
1402}
1403
1404void
1405core_alua_set_lba_map(struct se_device *dev, struct list_head *lba_map_list,
1406 int segment_size, int segment_mult)
1407{
1408 struct list_head old_lba_map_list;
1409 struct t10_alua_tg_pt_gp *tg_pt_gp;
1410 int activate = 0, supported;
1411
1412 INIT_LIST_HEAD(&old_lba_map_list);
1413 spin_lock(&dev->t10_alua.lba_map_lock);
1414 dev->t10_alua.lba_map_segment_size = segment_size;
1415 dev->t10_alua.lba_map_segment_multiplier = segment_mult;
1416 list_splice_init(&dev->t10_alua.lba_map_list, &old_lba_map_list);
1417 if (lba_map_list) {
1418 list_splice_init(lba_map_list, &dev->t10_alua.lba_map_list);
1419 activate = 1;
1420 }
1421 spin_unlock(&dev->t10_alua.lba_map_lock);
1422 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1423 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
1424 tg_pt_gp_list) {
1425
1426 if (!tg_pt_gp->tg_pt_gp_valid_id)
1427 continue;
1428 supported = tg_pt_gp->tg_pt_gp_alua_supported_states;
1429 if (activate)
1430 supported |= ALUA_LBD_SUP;
1431 else
1432 supported &= ~ALUA_LBD_SUP;
1433 tg_pt_gp->tg_pt_gp_alua_supported_states = supported;
1434 }
1435 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1436 core_alua_free_lba_map(&old_lba_map_list);
1437}
1438
c66ac9db
NB
1439struct t10_alua_lu_gp *
1440core_alua_allocate_lu_gp(const char *name, int def_group)
1441{
1442 struct t10_alua_lu_gp *lu_gp;
1443
1444 lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
6708bb27
AG
1445 if (!lu_gp) {
1446 pr_err("Unable to allocate struct t10_alua_lu_gp\n");
6eab04a8 1447 return ERR_PTR(-ENOMEM);
c66ac9db 1448 }
e3d6f909 1449 INIT_LIST_HEAD(&lu_gp->lu_gp_node);
c66ac9db
NB
1450 INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
1451 spin_lock_init(&lu_gp->lu_gp_lock);
1452 atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
1453
1454 if (def_group) {
e3d6f909 1455 lu_gp->lu_gp_id = alua_lu_gps_counter++;
c66ac9db 1456 lu_gp->lu_gp_valid_id = 1;
e3d6f909 1457 alua_lu_gps_count++;
c66ac9db
NB
1458 }
1459
1460 return lu_gp;
1461}
1462
1463int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
1464{
1465 struct t10_alua_lu_gp *lu_gp_tmp;
1466 u16 lu_gp_id_tmp;
1467 /*
1468 * The lu_gp->lu_gp_id may only be set once..
1469 */
1470 if (lu_gp->lu_gp_valid_id) {
6708bb27 1471 pr_warn("ALUA LU Group already has a valid ID,"
c66ac9db 1472 " ignoring request\n");
e3d6f909 1473 return -EINVAL;
c66ac9db
NB
1474 }
1475
e3d6f909
AG
1476 spin_lock(&lu_gps_lock);
1477 if (alua_lu_gps_count == 0x0000ffff) {
6708bb27 1478 pr_err("Maximum ALUA alua_lu_gps_count:"
c66ac9db 1479 " 0x0000ffff reached\n");
e3d6f909 1480 spin_unlock(&lu_gps_lock);
c66ac9db 1481 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
e3d6f909 1482 return -ENOSPC;
c66ac9db
NB
1483 }
1484again:
1485 lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
e3d6f909 1486 alua_lu_gps_counter++;
c66ac9db 1487
e3d6f909 1488 list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
c66ac9db 1489 if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
6708bb27 1490 if (!lu_gp_id)
c66ac9db
NB
1491 goto again;
1492
6708bb27 1493 pr_warn("ALUA Logical Unit Group ID: %hu"
c66ac9db
NB
1494 " already exists, ignoring request\n",
1495 lu_gp_id);
e3d6f909
AG
1496 spin_unlock(&lu_gps_lock);
1497 return -EINVAL;
c66ac9db
NB
1498 }
1499 }
1500
1501 lu_gp->lu_gp_id = lu_gp_id_tmp;
1502 lu_gp->lu_gp_valid_id = 1;
e3d6f909
AG
1503 list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
1504 alua_lu_gps_count++;
1505 spin_unlock(&lu_gps_lock);
c66ac9db
NB
1506
1507 return 0;
1508}
1509
1510static struct t10_alua_lu_gp_member *
1511core_alua_allocate_lu_gp_mem(struct se_device *dev)
1512{
1513 struct t10_alua_lu_gp_member *lu_gp_mem;
1514
1515 lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
6708bb27
AG
1516 if (!lu_gp_mem) {
1517 pr_err("Unable to allocate struct t10_alua_lu_gp_member\n");
c66ac9db
NB
1518 return ERR_PTR(-ENOMEM);
1519 }
1520 INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
1521 spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
1522 atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
1523
1524 lu_gp_mem->lu_gp_mem_dev = dev;
1525 dev->dev_alua_lu_gp_mem = lu_gp_mem;
1526
1527 return lu_gp_mem;
1528}
1529
1530void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
1531{
1532 struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
1533 /*
1534 * Once we have reached this point, config_item_put() has
1535 * already been called from target_core_alua_drop_lu_gp().
1536 *
1537 * Here, we remove the *lu_gp from the global list so that
1538 * no associations can be made while we are releasing
1539 * struct t10_alua_lu_gp.
1540 */
e3d6f909 1541 spin_lock(&lu_gps_lock);
e3d6f909
AG
1542 list_del(&lu_gp->lu_gp_node);
1543 alua_lu_gps_count--;
1544 spin_unlock(&lu_gps_lock);
c66ac9db
NB
1545 /*
1546 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
1547 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
1548 * released with core_alua_put_lu_gp_from_name()
1549 */
1550 while (atomic_read(&lu_gp->lu_gp_ref_cnt))
1551 cpu_relax();
1552 /*
1553 * Release reference to struct t10_alua_lu_gp * from all associated
1554 * struct se_device.
1555 */
1556 spin_lock(&lu_gp->lu_gp_lock);
1557 list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
1558 &lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
1559 if (lu_gp_mem->lu_gp_assoc) {
1560 list_del(&lu_gp_mem->lu_gp_mem_list);
1561 lu_gp->lu_gp_members--;
1562 lu_gp_mem->lu_gp_assoc = 0;
1563 }
1564 spin_unlock(&lu_gp->lu_gp_lock);
1565 /*
1566 *
25985edc 1567 * lu_gp_mem is associated with a single
c66ac9db
NB
1568 * struct se_device->dev_alua_lu_gp_mem, and is released when
1569 * struct se_device is released via core_alua_free_lu_gp_mem().
1570 *
1571 * If the passed lu_gp does NOT match the default_lu_gp, assume
f1ae05d5 1572 * we want to re-associate a given lu_gp_mem with default_lu_gp.
c66ac9db
NB
1573 */
1574 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
e3d6f909 1575 if (lu_gp != default_lu_gp)
c66ac9db 1576 __core_alua_attach_lu_gp_mem(lu_gp_mem,
e3d6f909 1577 default_lu_gp);
c66ac9db
NB
1578 else
1579 lu_gp_mem->lu_gp = NULL;
1580 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1581
1582 spin_lock(&lu_gp->lu_gp_lock);
1583 }
1584 spin_unlock(&lu_gp->lu_gp_lock);
1585
1586 kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
1587}
1588
1589void core_alua_free_lu_gp_mem(struct se_device *dev)
1590{
c66ac9db
NB
1591 struct t10_alua_lu_gp *lu_gp;
1592 struct t10_alua_lu_gp_member *lu_gp_mem;
1593
c66ac9db 1594 lu_gp_mem = dev->dev_alua_lu_gp_mem;
6708bb27 1595 if (!lu_gp_mem)
c66ac9db
NB
1596 return;
1597
1598 while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
1599 cpu_relax();
1600
1601 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
1602 lu_gp = lu_gp_mem->lu_gp;
6708bb27 1603 if (lu_gp) {
c66ac9db
NB
1604 spin_lock(&lu_gp->lu_gp_lock);
1605 if (lu_gp_mem->lu_gp_assoc) {
1606 list_del(&lu_gp_mem->lu_gp_mem_list);
1607 lu_gp->lu_gp_members--;
1608 lu_gp_mem->lu_gp_assoc = 0;
1609 }
1610 spin_unlock(&lu_gp->lu_gp_lock);
1611 lu_gp_mem->lu_gp = NULL;
1612 }
1613 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
1614
1615 kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
1616}
1617
1618struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
1619{
1620 struct t10_alua_lu_gp *lu_gp;
1621 struct config_item *ci;
1622
e3d6f909
AG
1623 spin_lock(&lu_gps_lock);
1624 list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
6708bb27 1625 if (!lu_gp->lu_gp_valid_id)
c66ac9db
NB
1626 continue;
1627 ci = &lu_gp->lu_gp_group.cg_item;
6708bb27 1628 if (!strcmp(config_item_name(ci), name)) {
c66ac9db 1629 atomic_inc(&lu_gp->lu_gp_ref_cnt);
e3d6f909 1630 spin_unlock(&lu_gps_lock);
c66ac9db
NB
1631 return lu_gp;
1632 }
1633 }
e3d6f909 1634 spin_unlock(&lu_gps_lock);
c66ac9db
NB
1635
1636 return NULL;
1637}
1638
1639void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
1640{
e3d6f909 1641 spin_lock(&lu_gps_lock);
c66ac9db 1642 atomic_dec(&lu_gp->lu_gp_ref_cnt);
e3d6f909 1643 spin_unlock(&lu_gps_lock);
c66ac9db
NB
1644}
1645
1646/*
1647 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1648 */
1649void __core_alua_attach_lu_gp_mem(
1650 struct t10_alua_lu_gp_member *lu_gp_mem,
1651 struct t10_alua_lu_gp *lu_gp)
1652{
1653 spin_lock(&lu_gp->lu_gp_lock);
1654 lu_gp_mem->lu_gp = lu_gp;
1655 lu_gp_mem->lu_gp_assoc = 1;
1656 list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
1657 lu_gp->lu_gp_members++;
1658 spin_unlock(&lu_gp->lu_gp_lock);
1659}
1660
1661/*
1662 * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
1663 */
1664void __core_alua_drop_lu_gp_mem(
1665 struct t10_alua_lu_gp_member *lu_gp_mem,
1666 struct t10_alua_lu_gp *lu_gp)
1667{
1668 spin_lock(&lu_gp->lu_gp_lock);
1669 list_del(&lu_gp_mem->lu_gp_mem_list);
1670 lu_gp_mem->lu_gp = NULL;
1671 lu_gp_mem->lu_gp_assoc = 0;
1672 lu_gp->lu_gp_members--;
1673 spin_unlock(&lu_gp->lu_gp_lock);
1674}
1675
0fd97ccf
CH
1676struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(struct se_device *dev,
1677 const char *name, int def_group)
c66ac9db
NB
1678{
1679 struct t10_alua_tg_pt_gp *tg_pt_gp;
1680
1681 tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
6708bb27
AG
1682 if (!tg_pt_gp) {
1683 pr_err("Unable to allocate struct t10_alua_tg_pt_gp\n");
c66ac9db
NB
1684 return NULL;
1685 }
1686 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
adf653f9 1687 INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_lun_list);
c66ac9db
NB
1688 mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
1689 spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
1690 atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
d7175373
MC
1691 INIT_WORK(&tg_pt_gp->tg_pt_gp_transition_work,
1692 core_alua_do_transition_tg_pt_work);
0fd97ccf 1693 tg_pt_gp->tg_pt_gp_dev = dev;
c66ac9db 1694 atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
73f3bf51 1695 ALUA_ACCESS_STATE_ACTIVE_OPTIMIZED);
c66ac9db 1696 /*
125d0119 1697 * Enable both explicit and implicit ALUA support by default
c66ac9db
NB
1698 */
1699 tg_pt_gp->tg_pt_gp_alua_access_type =
125d0119 1700 TPGS_EXPLICIT_ALUA | TPGS_IMPLICIT_ALUA;
c66ac9db
NB
1701 /*
1702 * Set the default Active/NonOptimized Delay in milliseconds
1703 */
1704 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
1705 tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
125d0119 1706 tg_pt_gp->tg_pt_gp_implicit_trans_secs = ALUA_DEFAULT_IMPLICIT_TRANS_SECS;
c66ac9db 1707
c0dc941e
HR
1708 /*
1709 * Enable all supported states
1710 */
1711 tg_pt_gp->tg_pt_gp_alua_supported_states =
1712 ALUA_T_SUP | ALUA_O_SUP |
1713 ALUA_U_SUP | ALUA_S_SUP | ALUA_AN_SUP | ALUA_AO_SUP;
1714
c66ac9db 1715 if (def_group) {
0fd97ccf 1716 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db 1717 tg_pt_gp->tg_pt_gp_id =
0fd97ccf 1718 dev->t10_alua.alua_tg_pt_gps_counter++;
c66ac9db 1719 tg_pt_gp->tg_pt_gp_valid_id = 1;
0fd97ccf 1720 dev->t10_alua.alua_tg_pt_gps_count++;
c66ac9db 1721 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
0fd97ccf
CH
1722 &dev->t10_alua.tg_pt_gps_list);
1723 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db
NB
1724 }
1725
1726 return tg_pt_gp;
1727}
1728
1729int core_alua_set_tg_pt_gp_id(
1730 struct t10_alua_tg_pt_gp *tg_pt_gp,
1731 u16 tg_pt_gp_id)
1732{
0fd97ccf 1733 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
c66ac9db
NB
1734 struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
1735 u16 tg_pt_gp_id_tmp;
0fd97ccf 1736
c66ac9db
NB
1737 /*
1738 * The tg_pt_gp->tg_pt_gp_id may only be set once..
1739 */
1740 if (tg_pt_gp->tg_pt_gp_valid_id) {
6708bb27 1741 pr_warn("ALUA TG PT Group already has a valid ID,"
c66ac9db 1742 " ignoring request\n");
e3d6f909 1743 return -EINVAL;
c66ac9db
NB
1744 }
1745
0fd97ccf
CH
1746 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1747 if (dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
6708bb27 1748 pr_err("Maximum ALUA alua_tg_pt_gps_count:"
c66ac9db 1749 " 0x0000ffff reached\n");
0fd97ccf 1750 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db 1751 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
e3d6f909 1752 return -ENOSPC;
c66ac9db
NB
1753 }
1754again:
1755 tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
0fd97ccf 1756 dev->t10_alua.alua_tg_pt_gps_counter++;
c66ac9db 1757
0fd97ccf 1758 list_for_each_entry(tg_pt_gp_tmp, &dev->t10_alua.tg_pt_gps_list,
c66ac9db
NB
1759 tg_pt_gp_list) {
1760 if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
6708bb27 1761 if (!tg_pt_gp_id)
c66ac9db
NB
1762 goto again;
1763
6708bb27 1764 pr_err("ALUA Target Port Group ID: %hu already"
c66ac9db 1765 " exists, ignoring request\n", tg_pt_gp_id);
0fd97ccf 1766 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
e3d6f909 1767 return -EINVAL;
c66ac9db
NB
1768 }
1769 }
1770
1771 tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
1772 tg_pt_gp->tg_pt_gp_valid_id = 1;
1773 list_add_tail(&tg_pt_gp->tg_pt_gp_list,
0fd97ccf
CH
1774 &dev->t10_alua.tg_pt_gps_list);
1775 dev->t10_alua.alua_tg_pt_gps_count++;
1776 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db
NB
1777
1778 return 0;
1779}
1780
c66ac9db
NB
1781void core_alua_free_tg_pt_gp(
1782 struct t10_alua_tg_pt_gp *tg_pt_gp)
1783{
0fd97ccf 1784 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
adf653f9 1785 struct se_lun *lun, *next;
0fd97ccf 1786
c66ac9db
NB
1787 /*
1788 * Once we have reached this point, config_item_put() has already
1789 * been called from target_core_alua_drop_tg_pt_gp().
1790 *
1791 * Here we remove *tg_pt_gp from the global list so that
f1ae05d5 1792 * no associations *OR* explicit ALUA via SET_TARGET_PORT_GROUPS
c66ac9db
NB
1793 * can be made while we are releasing struct t10_alua_tg_pt_gp.
1794 */
0fd97ccf 1795 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db 1796 list_del(&tg_pt_gp->tg_pt_gp_list);
0fd97ccf
CH
1797 dev->t10_alua.alua_tg_pt_gps_counter--;
1798 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
1799
d7175373 1800 flush_work(&tg_pt_gp->tg_pt_gp_transition_work);
9c6e164c 1801
c66ac9db
NB
1802 /*
1803 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
1804 * core_alua_get_tg_pt_gp_by_name() in
1805 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
1806 * to be released with core_alua_put_tg_pt_gp_from_name().
1807 */
1808 while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
1809 cpu_relax();
0fd97ccf 1810
c66ac9db
NB
1811 /*
1812 * Release reference to struct t10_alua_tg_pt_gp from all associated
1813 * struct se_port.
1814 */
1815 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
adf653f9
CH
1816 list_for_each_entry_safe(lun, next,
1817 &tg_pt_gp->tg_pt_gp_lun_list, lun_tg_pt_gp_link) {
1818 list_del_init(&lun->lun_tg_pt_gp_link);
1819 tg_pt_gp->tg_pt_gp_members--;
1820
c66ac9db
NB
1821 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1822 /*
c66ac9db 1823 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
f1ae05d5 1824 * assume we want to re-associate a given tg_pt_gp_mem with
c66ac9db
NB
1825 * default_tg_pt_gp.
1826 */
adf653f9 1827 spin_lock(&lun->lun_tg_pt_gp_lock);
0fd97ccf 1828 if (tg_pt_gp != dev->t10_alua.default_tg_pt_gp) {
adf653f9 1829 __target_attach_tg_pt_gp(lun,
0fd97ccf 1830 dev->t10_alua.default_tg_pt_gp);
c66ac9db 1831 } else
adf653f9
CH
1832 lun->lun_tg_pt_gp = NULL;
1833 spin_unlock(&lun->lun_tg_pt_gp_lock);
c66ac9db
NB
1834
1835 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
1836 }
1837 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1838
1839 kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
1840}
1841
c66ac9db 1842static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
0fd97ccf 1843 struct se_device *dev, const char *name)
c66ac9db
NB
1844{
1845 struct t10_alua_tg_pt_gp *tg_pt_gp;
1846 struct config_item *ci;
1847
0fd97ccf
CH
1848 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
1849 list_for_each_entry(tg_pt_gp, &dev->t10_alua.tg_pt_gps_list,
c66ac9db 1850 tg_pt_gp_list) {
6708bb27 1851 if (!tg_pt_gp->tg_pt_gp_valid_id)
c66ac9db
NB
1852 continue;
1853 ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
6708bb27 1854 if (!strcmp(config_item_name(ci), name)) {
c66ac9db 1855 atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
0fd97ccf 1856 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db
NB
1857 return tg_pt_gp;
1858 }
1859 }
0fd97ccf 1860 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db
NB
1861
1862 return NULL;
1863}
1864
1865static void core_alua_put_tg_pt_gp_from_name(
1866 struct t10_alua_tg_pt_gp *tg_pt_gp)
1867{
0fd97ccf 1868 struct se_device *dev = tg_pt_gp->tg_pt_gp_dev;
c66ac9db 1869
0fd97ccf 1870 spin_lock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db 1871 atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
0fd97ccf 1872 spin_unlock(&dev->t10_alua.tg_pt_gps_lock);
c66ac9db
NB
1873}
1874
adf653f9
CH
1875static void __target_attach_tg_pt_gp(struct se_lun *lun,
1876 struct t10_alua_tg_pt_gp *tg_pt_gp)
c66ac9db 1877{
3dd348fc
HR
1878 struct se_dev_entry *se_deve;
1879
adf653f9
CH
1880 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1881
c66ac9db 1882 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
adf653f9
CH
1883 lun->lun_tg_pt_gp = tg_pt_gp;
1884 list_add_tail(&lun->lun_tg_pt_gp_link, &tg_pt_gp->tg_pt_gp_lun_list);
c66ac9db 1885 tg_pt_gp->tg_pt_gp_members++;
3dd348fc
HR
1886 spin_lock(&lun->lun_deve_lock);
1887 list_for_each_entry(se_deve, &lun->lun_deve_list, lun_link)
1888 core_scsi3_ua_allocate(se_deve, 0x3f,
1889 ASCQ_3FH_INQUIRY_DATA_HAS_CHANGED);
1890 spin_unlock(&lun->lun_deve_lock);
c66ac9db
NB
1891 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
1892}
1893
adf653f9
CH
1894void target_attach_tg_pt_gp(struct se_lun *lun,
1895 struct t10_alua_tg_pt_gp *tg_pt_gp)
c66ac9db 1896{
adf653f9
CH
1897 spin_lock(&lun->lun_tg_pt_gp_lock);
1898 __target_attach_tg_pt_gp(lun, tg_pt_gp);
1899 spin_unlock(&lun->lun_tg_pt_gp_lock);
1900}
1901
1902static void __target_detach_tg_pt_gp(struct se_lun *lun,
1903 struct t10_alua_tg_pt_gp *tg_pt_gp)
c66ac9db 1904{
adf653f9
CH
1905 assert_spin_locked(&lun->lun_tg_pt_gp_lock);
1906
c66ac9db 1907 spin_lock(&tg_pt_gp->tg_pt_gp_lock);
adf653f9 1908 list_del_init(&lun->lun_tg_pt_gp_link);
c66ac9db
NB
1909 tg_pt_gp->tg_pt_gp_members--;
1910 spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
adf653f9
CH
1911
1912 lun->lun_tg_pt_gp = NULL;
c66ac9db
NB
1913}
1914
adf653f9
CH
1915void target_detach_tg_pt_gp(struct se_lun *lun)
1916{
1917 struct t10_alua_tg_pt_gp *tg_pt_gp;
1918
1919 spin_lock(&lun->lun_tg_pt_gp_lock);
1920 tg_pt_gp = lun->lun_tg_pt_gp;
1921 if (tg_pt_gp)
1922 __target_detach_tg_pt_gp(lun, tg_pt_gp);
1923 spin_unlock(&lun->lun_tg_pt_gp_lock);
1924}
1925
1926ssize_t core_alua_show_tg_pt_gp_info(struct se_lun *lun, char *page)
c66ac9db 1927{
c66ac9db 1928 struct config_item *tg_pt_ci;
c66ac9db 1929 struct t10_alua_tg_pt_gp *tg_pt_gp;
c66ac9db
NB
1930 ssize_t len = 0;
1931
adf653f9
CH
1932 spin_lock(&lun->lun_tg_pt_gp_lock);
1933 tg_pt_gp = lun->lun_tg_pt_gp;
6708bb27 1934 if (tg_pt_gp) {
c66ac9db
NB
1935 tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
1936 len += sprintf(page, "TG Port Alias: %s\nTG Port Group ID:"
1937 " %hu\nTG Port Primary Access State: %s\nTG Port "
1938 "Primary Access Status: %s\nTG Port Secondary Access"
1939 " State: %s\nTG Port Secondary Access Status: %s\n",
1940 config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
1941 core_alua_dump_state(atomic_read(
1942 &tg_pt_gp->tg_pt_gp_alua_access_state)),
1943 core_alua_dump_status(
1944 tg_pt_gp->tg_pt_gp_alua_access_status),
adf653f9 1945 atomic_read(&lun->lun_tg_pt_secondary_offline) ?
c66ac9db 1946 "Offline" : "None",
adf653f9 1947 core_alua_dump_status(lun->lun_tg_pt_secondary_stat));
c66ac9db 1948 }
adf653f9 1949 spin_unlock(&lun->lun_tg_pt_gp_lock);
c66ac9db
NB
1950
1951 return len;
1952}
1953
1954ssize_t core_alua_store_tg_pt_gp_info(
adf653f9 1955 struct se_lun *lun,
c66ac9db
NB
1956 const char *page,
1957 size_t count)
1958{
adf653f9 1959 struct se_portal_group *tpg = lun->lun_tpg;
4cc987ea
NB
1960 /*
1961 * rcu_dereference_raw protected by se_lun->lun_group symlink
1962 * reference to se_device->dev_group.
1963 */
1964 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
c66ac9db 1965 struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
c66ac9db
NB
1966 unsigned char buf[TG_PT_GROUP_NAME_BUF];
1967 int move = 0;
1968
530c6891 1969 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
adf653f9
CH
1970 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
1971 return -ENODEV;
c66ac9db
NB
1972
1973 if (count > TG_PT_GROUP_NAME_BUF) {
6708bb27 1974 pr_err("ALUA Target Port Group alias too large!\n");
c66ac9db
NB
1975 return -EINVAL;
1976 }
1977 memset(buf, 0, TG_PT_GROUP_NAME_BUF);
1978 memcpy(buf, page, count);
1979 /*
1980 * Any ALUA target port group alias besides "NULL" means we will be
1981 * making a new group association.
1982 */
1983 if (strcmp(strstrip(buf), "NULL")) {
1984 /*
1985 * core_alua_get_tg_pt_gp_by_name() will increment reference to
1986 * struct t10_alua_tg_pt_gp. This reference is released with
1987 * core_alua_put_tg_pt_gp_from_name() below.
1988 */
0fd97ccf 1989 tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(dev,
c66ac9db 1990 strstrip(buf));
6708bb27 1991 if (!tg_pt_gp_new)
c66ac9db
NB
1992 return -ENODEV;
1993 }
c66ac9db 1994
adf653f9
CH
1995 spin_lock(&lun->lun_tg_pt_gp_lock);
1996 tg_pt_gp = lun->lun_tg_pt_gp;
6708bb27 1997 if (tg_pt_gp) {
c66ac9db
NB
1998 /*
1999 * Clearing an existing tg_pt_gp association, and replacing
2000 * with the default_tg_pt_gp.
2001 */
6708bb27
AG
2002 if (!tg_pt_gp_new) {
2003 pr_debug("Target_Core_ConfigFS: Moving"
c66ac9db
NB
2004 " %s/tpgt_%hu/%s from ALUA Target Port Group:"
2005 " alua/%s, ID: %hu back to"
2006 " default_tg_pt_gp\n",
e3d6f909
AG
2007 tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2008 tpg->se_tpg_tfo->tpg_get_tag(tpg),
c66ac9db
NB
2009 config_item_name(&lun->lun_group.cg_item),
2010 config_item_name(
2011 &tg_pt_gp->tg_pt_gp_group.cg_item),
2012 tg_pt_gp->tg_pt_gp_id);
2013
adf653f9
CH
2014 __target_detach_tg_pt_gp(lun, tg_pt_gp);
2015 __target_attach_tg_pt_gp(lun,
0fd97ccf 2016 dev->t10_alua.default_tg_pt_gp);
adf653f9 2017 spin_unlock(&lun->lun_tg_pt_gp_lock);
c66ac9db
NB
2018
2019 return count;
2020 }
adf653f9 2021 __target_detach_tg_pt_gp(lun, tg_pt_gp);
c66ac9db
NB
2022 move = 1;
2023 }
adf653f9
CH
2024
2025 __target_attach_tg_pt_gp(lun, tg_pt_gp_new);
2026 spin_unlock(&lun->lun_tg_pt_gp_lock);
6708bb27 2027 pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
c66ac9db 2028 " Target Port Group: alua/%s, ID: %hu\n", (move) ?
e3d6f909
AG
2029 "Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
2030 tpg->se_tpg_tfo->tpg_get_tag(tpg),
c66ac9db
NB
2031 config_item_name(&lun->lun_group.cg_item),
2032 config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
2033 tg_pt_gp_new->tg_pt_gp_id);
2034
2035 core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
2036 return count;
2037}
2038
2039ssize_t core_alua_show_access_type(
2040 struct t10_alua_tg_pt_gp *tg_pt_gp,
2041 char *page)
2042{
125d0119
HR
2043 if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA) &&
2044 (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA))
2045 return sprintf(page, "Implicit and Explicit\n");
2046 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICIT_ALUA)
2047 return sprintf(page, "Implicit\n");
2048 else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICIT_ALUA)
2049 return sprintf(page, "Explicit\n");
c66ac9db
NB
2050 else
2051 return sprintf(page, "None\n");
2052}
2053
2054ssize_t core_alua_store_access_type(
2055 struct t10_alua_tg_pt_gp *tg_pt_gp,
2056 const char *page,
2057 size_t count)
2058{
2059 unsigned long tmp;
2060 int ret;
2061
57103d7f 2062 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2063 if (ret < 0) {
6708bb27 2064 pr_err("Unable to extract alua_access_type\n");
57103d7f 2065 return ret;
c66ac9db
NB
2066 }
2067 if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
6708bb27 2068 pr_err("Illegal value for alua_access_type:"
c66ac9db
NB
2069 " %lu\n", tmp);
2070 return -EINVAL;
2071 }
2072 if (tmp == 3)
2073 tg_pt_gp->tg_pt_gp_alua_access_type =
125d0119 2074 TPGS_IMPLICIT_ALUA | TPGS_EXPLICIT_ALUA;
c66ac9db 2075 else if (tmp == 2)
125d0119 2076 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICIT_ALUA;
c66ac9db 2077 else if (tmp == 1)
125d0119 2078 tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICIT_ALUA;
c66ac9db
NB
2079 else
2080 tg_pt_gp->tg_pt_gp_alua_access_type = 0;
2081
2082 return count;
2083}
2084
2085ssize_t core_alua_show_nonop_delay_msecs(
2086 struct t10_alua_tg_pt_gp *tg_pt_gp,
2087 char *page)
2088{
2089 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
2090}
2091
2092ssize_t core_alua_store_nonop_delay_msecs(
2093 struct t10_alua_tg_pt_gp *tg_pt_gp,
2094 const char *page,
2095 size_t count)
2096{
2097 unsigned long tmp;
2098 int ret;
2099
57103d7f 2100 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2101 if (ret < 0) {
6708bb27 2102 pr_err("Unable to extract nonop_delay_msecs\n");
57103d7f 2103 return ret;
c66ac9db
NB
2104 }
2105 if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
6708bb27 2106 pr_err("Passed nonop_delay_msecs: %lu, exceeds"
c66ac9db
NB
2107 " ALUA_MAX_NONOP_DELAY_MSECS: %d\n", tmp,
2108 ALUA_MAX_NONOP_DELAY_MSECS);
2109 return -EINVAL;
2110 }
2111 tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
2112
2113 return count;
2114}
2115
2116ssize_t core_alua_show_trans_delay_msecs(
2117 struct t10_alua_tg_pt_gp *tg_pt_gp,
2118 char *page)
2119{
2120 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
2121}
2122
2123ssize_t core_alua_store_trans_delay_msecs(
2124 struct t10_alua_tg_pt_gp *tg_pt_gp,
2125 const char *page,
2126 size_t count)
2127{
2128 unsigned long tmp;
2129 int ret;
2130
57103d7f 2131 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2132 if (ret < 0) {
6708bb27 2133 pr_err("Unable to extract trans_delay_msecs\n");
57103d7f 2134 return ret;
c66ac9db
NB
2135 }
2136 if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
6708bb27 2137 pr_err("Passed trans_delay_msecs: %lu, exceeds"
c66ac9db
NB
2138 " ALUA_MAX_TRANS_DELAY_MSECS: %d\n", tmp,
2139 ALUA_MAX_TRANS_DELAY_MSECS);
2140 return -EINVAL;
2141 }
2142 tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
2143
2144 return count;
2145}
2146
125d0119 2147ssize_t core_alua_show_implicit_trans_secs(
5b9a4d72
NB
2148 struct t10_alua_tg_pt_gp *tg_pt_gp,
2149 char *page)
2150{
125d0119 2151 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_implicit_trans_secs);
5b9a4d72
NB
2152}
2153
125d0119 2154ssize_t core_alua_store_implicit_trans_secs(
5b9a4d72
NB
2155 struct t10_alua_tg_pt_gp *tg_pt_gp,
2156 const char *page,
2157 size_t count)
2158{
2159 unsigned long tmp;
2160 int ret;
2161
57103d7f 2162 ret = kstrtoul(page, 0, &tmp);
5b9a4d72 2163 if (ret < 0) {
125d0119 2164 pr_err("Unable to extract implicit_trans_secs\n");
57103d7f 2165 return ret;
5b9a4d72 2166 }
125d0119
HR
2167 if (tmp > ALUA_MAX_IMPLICIT_TRANS_SECS) {
2168 pr_err("Passed implicit_trans_secs: %lu, exceeds"
2169 " ALUA_MAX_IMPLICIT_TRANS_SECS: %d\n", tmp,
2170 ALUA_MAX_IMPLICIT_TRANS_SECS);
5b9a4d72
NB
2171 return -EINVAL;
2172 }
125d0119 2173 tg_pt_gp->tg_pt_gp_implicit_trans_secs = (int)tmp;
5b9a4d72
NB
2174
2175 return count;
2176}
2177
c66ac9db
NB
2178ssize_t core_alua_show_preferred_bit(
2179 struct t10_alua_tg_pt_gp *tg_pt_gp,
2180 char *page)
2181{
2182 return sprintf(page, "%d\n", tg_pt_gp->tg_pt_gp_pref);
2183}
2184
2185ssize_t core_alua_store_preferred_bit(
2186 struct t10_alua_tg_pt_gp *tg_pt_gp,
2187 const char *page,
2188 size_t count)
2189{
2190 unsigned long tmp;
2191 int ret;
2192
57103d7f 2193 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2194 if (ret < 0) {
6708bb27 2195 pr_err("Unable to extract preferred ALUA value\n");
57103d7f 2196 return ret;
c66ac9db
NB
2197 }
2198 if ((tmp != 0) && (tmp != 1)) {
6708bb27 2199 pr_err("Illegal value for preferred ALUA: %lu\n", tmp);
c66ac9db
NB
2200 return -EINVAL;
2201 }
2202 tg_pt_gp->tg_pt_gp_pref = (int)tmp;
2203
2204 return count;
2205}
2206
2207ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
2208{
c66ac9db 2209 return sprintf(page, "%d\n",
adf653f9 2210 atomic_read(&lun->lun_tg_pt_secondary_offline));
c66ac9db
NB
2211}
2212
2213ssize_t core_alua_store_offline_bit(
2214 struct se_lun *lun,
2215 const char *page,
2216 size_t count)
2217{
4cc987ea
NB
2218 /*
2219 * rcu_dereference_raw protected by se_lun->lun_group symlink
2220 * reference to se_device->dev_group.
2221 */
2222 struct se_device *dev = rcu_dereference_raw(lun->lun_se_dev);
c66ac9db
NB
2223 unsigned long tmp;
2224 int ret;
2225
530c6891 2226 if (dev->transport->transport_flags & TRANSPORT_FLAG_PASSTHROUGH_ALUA ||
adf653f9 2227 (dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE))
c66ac9db
NB
2228 return -ENODEV;
2229
57103d7f 2230 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2231 if (ret < 0) {
6708bb27 2232 pr_err("Unable to extract alua_tg_pt_offline value\n");
57103d7f 2233 return ret;
c66ac9db
NB
2234 }
2235 if ((tmp != 0) && (tmp != 1)) {
6708bb27 2236 pr_err("Illegal value for alua_tg_pt_offline: %lu\n",
c66ac9db
NB
2237 tmp);
2238 return -EINVAL;
2239 }
c66ac9db 2240
adf653f9 2241 ret = core_alua_set_tg_pt_secondary_state(lun, 0, (int)tmp);
c66ac9db
NB
2242 if (ret < 0)
2243 return -EINVAL;
2244
2245 return count;
2246}
2247
2248ssize_t core_alua_show_secondary_status(
2249 struct se_lun *lun,
2250 char *page)
2251{
adf653f9 2252 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_stat);
c66ac9db
NB
2253}
2254
2255ssize_t core_alua_store_secondary_status(
2256 struct se_lun *lun,
2257 const char *page,
2258 size_t count)
2259{
2260 unsigned long tmp;
2261 int ret;
2262
57103d7f 2263 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2264 if (ret < 0) {
6708bb27 2265 pr_err("Unable to extract alua_tg_pt_status\n");
57103d7f 2266 return ret;
c66ac9db
NB
2267 }
2268 if ((tmp != ALUA_STATUS_NONE) &&
125d0119
HR
2269 (tmp != ALUA_STATUS_ALTERED_BY_EXPLICIT_STPG) &&
2270 (tmp != ALUA_STATUS_ALTERED_BY_IMPLICIT_ALUA)) {
6708bb27 2271 pr_err("Illegal value for alua_tg_pt_status: %lu\n",
c66ac9db
NB
2272 tmp);
2273 return -EINVAL;
2274 }
adf653f9 2275 lun->lun_tg_pt_secondary_stat = (int)tmp;
c66ac9db
NB
2276
2277 return count;
2278}
2279
2280ssize_t core_alua_show_secondary_write_metadata(
2281 struct se_lun *lun,
2282 char *page)
2283{
adf653f9 2284 return sprintf(page, "%d\n", lun->lun_tg_pt_secondary_write_md);
c66ac9db
NB
2285}
2286
2287ssize_t core_alua_store_secondary_write_metadata(
2288 struct se_lun *lun,
2289 const char *page,
2290 size_t count)
2291{
2292 unsigned long tmp;
2293 int ret;
2294
57103d7f 2295 ret = kstrtoul(page, 0, &tmp);
c66ac9db 2296 if (ret < 0) {
6708bb27 2297 pr_err("Unable to extract alua_tg_pt_write_md\n");
57103d7f 2298 return ret;
c66ac9db
NB
2299 }
2300 if ((tmp != 0) && (tmp != 1)) {
6708bb27 2301 pr_err("Illegal value for alua_tg_pt_write_md:"
c66ac9db
NB
2302 " %lu\n", tmp);
2303 return -EINVAL;
2304 }
adf653f9 2305 lun->lun_tg_pt_secondary_write_md = (int)tmp;
c66ac9db
NB
2306
2307 return count;
2308}
2309
0fd97ccf 2310int core_setup_alua(struct se_device *dev)
c66ac9db 2311{
530c6891
MC
2312 if (!(dev->transport->transport_flags &
2313 TRANSPORT_FLAG_PASSTHROUGH_ALUA) &&
c87fbd56
CH
2314 !(dev->se_hba->hba_flags & HBA_FLAGS_INTERNAL_USE)) {
2315 struct t10_alua_lu_gp_member *lu_gp_mem;
0fd97ccf 2316
c66ac9db 2317 /*
25985edc 2318 * Associate this struct se_device with the default ALUA
c66ac9db
NB
2319 * LUN Group.
2320 */
2321 lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
e3d6f909
AG
2322 if (IS_ERR(lu_gp_mem))
2323 return PTR_ERR(lu_gp_mem);
c66ac9db 2324
c66ac9db
NB
2325 spin_lock(&lu_gp_mem->lu_gp_mem_lock);
2326 __core_alua_attach_lu_gp_mem(lu_gp_mem,
e3d6f909 2327 default_lu_gp);
c66ac9db
NB
2328 spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
2329
6708bb27 2330 pr_debug("%s: Adding to default ALUA LU Group:"
c66ac9db 2331 " core/alua/lu_gps/default_lu_gp\n",
e3d6f909 2332 dev->transport->name);
c66ac9db
NB
2333 }
2334
2335 return 0;
2336}