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