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