Blame view

drivers/target/target_core_alua.c 55.7 KB
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  /*******************************************************************************
   * Filename:  target_core_alua.c
   *
   * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA)
   *
   * Copyright (c) 2009-2010 Rising Tide Systems
   * Copyright (c) 2009-2010 Linux-iSCSI.org
   *
   * Nicholas A. Bellinger <nab@kernel.org>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software
   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   *
   ******************************************************************************/
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
26
27
28
  #include <linux/slab.h>
  #include <linux/spinlock.h>
  #include <linux/configfs.h>
c53181af8   Paul Gortmaker   drivers/target: A...
29
  #include <linux/export.h>
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  #include <scsi/scsi.h>
  #include <scsi/scsi_cmnd.h>
  
  #include <target/target_core_base.h>
  #include <target/target_core_device.h>
  #include <target/target_core_transport.h>
  #include <target/target_core_fabric_ops.h>
  #include <target/target_core_configfs.h>
  
  #include "target_core_alua.h"
  #include "target_core_hba.h"
  #include "target_core_ua.h"
  
  static int core_alua_check_transition(int state, int *primary);
  static int core_alua_set_tg_pt_secondary_state(
  		struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  		struct se_port *port, int explict, int offline);
e3d6f909e   Andy Grover   target: Core clea...
47
48
49
50
51
52
53
  static u16 alua_lu_gps_counter;
  static u32 alua_lu_gps_count;
  
  static DEFINE_SPINLOCK(lu_gps_lock);
  static LIST_HEAD(lu_gps_list);
  
  struct t10_alua_lu_gp *default_lu_gp;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
54
55
56
57
58
  /*
   * REPORT_TARGET_PORT_GROUPS
   *
   * See spc4r17 section 6.27
   */
e76a35d6c   Christoph Hellwig   target: pass the ...
59
  int target_emulate_report_target_port_groups(struct se_task *task)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
60
  {
e76a35d6c   Christoph Hellwig   target: pass the ...
61
  	struct se_cmd *cmd = task->task_se_cmd;
5951146de   Andy Grover   target: More core...
62
  	struct se_subsystem_dev *su_dev = cmd->se_dev->se_sub_dev;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
63
64
65
  	struct se_port *port;
  	struct t10_alua_tg_pt_gp *tg_pt_gp;
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
05d1c7c0d   Andy Grover   target: Make all ...
66
  	unsigned char *buf;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
67
68
  	u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first
  				    Target port group descriptor */
6b20fa9aa   Nicholas Bellinger   target: Fix REPOR...
69
70
71
72
73
74
75
76
77
78
  	/*
  	 * Need at least 4 bytes of response data or else we can't
  	 * even fit the return data length.
  	 */
  	if (cmd->data_length < 4) {
  		pr_warn("REPORT TARGET PORT GROUPS allocation length %u"
  			" too small
  ", cmd->data_length);
  		return -EINVAL;
  	}
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
79

05d1c7c0d   Andy Grover   target: Make all ...
80
  	buf = transport_kmap_first_data_page(cmd);
e3d6f909e   Andy Grover   target: Core clea...
81
82
  	spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  	list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
83
84
  			tg_pt_gp_list) {
  		/*
6b20fa9aa   Nicholas Bellinger   target: Fix REPOR...
85
86
87
88
89
90
91
92
93
94
95
  		 * Check if the Target port group and Target port descriptor list
  		 * based on tg_pt_gp_members count will fit into the response payload.
  		 * Otherwise, bump rd_len to let the initiator know we have exceeded
  		 * the allocation length and the response is truncated.
  		 */
  		if ((off + 8 + (tg_pt_gp->tg_pt_gp_members * 4)) >
  		     cmd->data_length) {
  			rd_len += 8 + (tg_pt_gp->tg_pt_gp_members * 4);
  			continue;
  		}
  		/*
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
  		 * PREF: Preferred target port bit, determine if this
  		 * bit should be set for port group.
  		 */
  		if (tg_pt_gp->tg_pt_gp_pref)
  			buf[off] = 0x80;
  		/*
  		 * Set the ASYMMETRIC ACCESS State
  		 */
  		buf[off++] |= (atomic_read(
  			&tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff);
  		/*
  		 * Set supported ASYMMETRIC ACCESS State bits
  		 */
  		buf[off] = 0x80; /* T_SUP */
  		buf[off] |= 0x40; /* O_SUP */
  		buf[off] |= 0x8; /* U_SUP */
  		buf[off] |= 0x4; /* S_SUP */
  		buf[off] |= 0x2; /* AN_SUP */
  		buf[off++] |= 0x1; /* AO_SUP */
  		/*
  		 * TARGET PORT GROUP
  		 */
  		buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff);
  		buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff);
  
  		off++; /* Skip over Reserved */
  		/*
  		 * STATUS CODE
  		 */
  		buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff);
  		/*
  		 * Vendor Specific field
  		 */
  		buf[off++] = 0x00;
  		/*
  		 * TARGET PORT COUNT
  		 */
  		buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff);
  		rd_len += 8;
  
  		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  		list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list,
  				tg_pt_gp_mem_list) {
  			port = tg_pt_gp_mem->tg_pt;
  			/*
  			 * Start Target Port descriptor format
  			 *
  			 * See spc4r17 section 6.2.7 Table 247
  			 */
  			off += 2; /* Skip over Obsolete */
  			/*
  			 * Set RELATIVE TARGET PORT IDENTIFIER
  			 */
  			buf[off++] = ((port->sep_rtpi >> 8) & 0xff);
  			buf[off++] = (port->sep_rtpi & 0xff);
  			rd_len += 4;
  		}
  		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  	}
e3d6f909e   Andy Grover   target: Core clea...
155
  	spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
156
157
158
159
160
161
162
  	/*
  	 * Set the RETURN DATA LENGTH set in the header of the DataIN Payload
  	 */
  	buf[0] = ((rd_len >> 24) & 0xff);
  	buf[1] = ((rd_len >> 16) & 0xff);
  	buf[2] = ((rd_len >> 8) & 0xff);
  	buf[3] = (rd_len & 0xff);
05d1c7c0d   Andy Grover   target: Make all ...
163
  	transport_kunmap_first_data_page(cmd);
d29a5b6ac   Christoph Hellwig   target: remove SC...
164
165
  	task->task_scsi_status = GOOD;
  	transport_complete_task(task, 1);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
166
167
168
169
170
171
172
173
  	return 0;
  }
  
  /*
   * SET_TARGET_PORT_GROUPS for explict ALUA operation.
   *
   * See spc4r17 section 6.35
   */
e76a35d6c   Christoph Hellwig   target: pass the ...
174
  int target_emulate_set_target_port_groups(struct se_task *task)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
175
  {
e76a35d6c   Christoph Hellwig   target: pass the ...
176
  	struct se_cmd *cmd = task->task_se_cmd;
5951146de   Andy Grover   target: More core...
177
  	struct se_device *dev = cmd->se_dev;
e3d6f909e   Andy Grover   target: Core clea...
178
179
180
  	struct se_subsystem_dev *su_dev = dev->se_sub_dev;
  	struct se_port *port, *l_port = cmd->se_lun->lun_sep;
  	struct se_node_acl *nacl = cmd->se_sess->se_node_acl;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
181
182
  	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp;
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem;
05d1c7c0d   Andy Grover   target: Make all ...
183
184
  	unsigned char *buf;
  	unsigned char *ptr;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
185
186
187
  	u32 len = 4; /* Skip over RESERVED area in header */
  	int alua_access_state, primary = 0, rc;
  	u16 tg_pt_id, rtpi;
03e98c9eb   Nicholas Bellinger   target: Address l...
188
189
190
191
  	if (!l_port) {
  		cmd->scsi_sense_reason = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  		return -EINVAL;
  	}
05d1c7c0d   Andy Grover   target: Make all ...
192
  	buf = transport_kmap_first_data_page(cmd);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
193
194
195
196
197
  	/*
  	 * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed
  	 * for the local tg_pt_gp.
  	 */
  	l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem;
6708bb27b   Andy Grover   target: Follow up...
198
199
200
  	if (!l_tg_pt_gp_mem) {
  		pr_err("Unable to access l_port->sep_alua_tg_pt_gp_mem
  ");
03e98c9eb   Nicholas Bellinger   target: Address l...
201
202
  		cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  		rc = -EINVAL;
05d1c7c0d   Andy Grover   target: Make all ...
203
  		goto out;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
204
205
206
  	}
  	spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp;
6708bb27b   Andy Grover   target: Follow up...
207
  	if (!l_tg_pt_gp) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
208
  		spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
6708bb27b   Andy Grover   target: Follow up...
209
210
  		pr_err("Unable to access *l_tg_pt_gp_mem->tg_pt_gp
  ");
03e98c9eb   Nicholas Bellinger   target: Address l...
211
212
  		cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  		rc = -EINVAL;
05d1c7c0d   Andy Grover   target: Make all ...
213
  		goto out;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
214
215
216
  	}
  	rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA);
  	spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock);
6708bb27b   Andy Grover   target: Follow up...
217
218
  	if (!rc) {
  		pr_debug("Unable to process SET_TARGET_PORT_GROUPS"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
219
220
  				" while TPGS_EXPLICT_ALUA is disabled
  ");
03e98c9eb   Nicholas Bellinger   target: Address l...
221
222
  		cmd->scsi_sense_reason = TCM_UNSUPPORTED_SCSI_OPCODE;
  		rc = -EINVAL;
05d1c7c0d   Andy Grover   target: Make all ...
223
  		goto out;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
224
  	}
05d1c7c0d   Andy Grover   target: Make all ...
225
  	ptr = &buf[4]; /* Skip over RESERVED area in header */
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  	while (len < cmd->data_length) {
  		alua_access_state = (ptr[0] & 0x0f);
  		/*
  		 * Check the received ALUA access state, and determine if
  		 * the state is a primary or secondary target port asymmetric
  		 * access state.
  		 */
  		rc = core_alua_check_transition(alua_access_state, &primary);
  		if (rc != 0) {
  			/*
  			 * If the SET TARGET PORT GROUPS attempts to establish
  			 * an invalid combination of target port asymmetric
  			 * access states or attempts to establish an
  			 * unsupported target port asymmetric access state,
  			 * then the command shall be terminated with CHECK
  			 * CONDITION status, with the sense key set to ILLEGAL
  			 * REQUEST, and the additional sense code set to INVALID
  			 * FIELD IN PARAMETER LIST.
  			 */
03e98c9eb   Nicholas Bellinger   target: Address l...
245
246
  			cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  			rc = -EINVAL;
05d1c7c0d   Andy Grover   target: Make all ...
247
  			goto out;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
  		}
  		rc = -1;
  		/*
  		 * If the ASYMMETRIC ACCESS STATE field (see table 267)
  		 * specifies a primary target port asymmetric access state,
  		 * then the TARGET PORT GROUP OR TARGET PORT field specifies
  		 * a primary target port group for which the primary target
  		 * port asymmetric access state shall be changed. If the
  		 * ASYMMETRIC ACCESS STATE field specifies a secondary target
  		 * port asymmetric access state, then the TARGET PORT GROUP OR
  		 * TARGET PORT field specifies the relative target port
  		 * identifier (see 3.1.120) of the target port for which the
  		 * secondary target port asymmetric access state shall be
  		 * changed.
  		 */
  		if (primary) {
  			tg_pt_id = ((ptr[2] << 8) & 0xff);
  			tg_pt_id |= (ptr[3] & 0xff);
  			/*
  			 * Locate the matching target port group ID from
  			 * the global tg_pt_gp list
  			 */
e3d6f909e   Andy Grover   target: Core clea...
270
  			spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
271
  			list_for_each_entry(tg_pt_gp,
e3d6f909e   Andy Grover   target: Core clea...
272
  					&su_dev->t10_alua.tg_pt_gps_list,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
273
  					tg_pt_gp_list) {
6708bb27b   Andy Grover   target: Follow up...
274
  				if (!tg_pt_gp->tg_pt_gp_valid_id)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
275
276
277
278
279
280
281
  					continue;
  
  				if (tg_pt_id != tg_pt_gp->tg_pt_gp_id)
  					continue;
  
  				atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  				smp_mb__after_atomic_inc();
e3d6f909e   Andy Grover   target: Core clea...
282
  				spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
283
284
285
286
  
  				rc = core_alua_do_port_transition(tg_pt_gp,
  						dev, l_port, nacl,
  						alua_access_state, 1);
e3d6f909e   Andy Grover   target: Core clea...
287
  				spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
288
289
290
291
  				atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  				smp_mb__after_atomic_dec();
  				break;
  			}
e3d6f909e   Andy Grover   target: Core clea...
292
  			spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
293
294
295
296
  			/*
  			 * If not matching target port group ID can be located
  			 * throw an exception with ASCQ: INVALID_PARAMETER_LIST
  			 */
05d1c7c0d   Andy Grover   target: Make all ...
297
  			if (rc != 0) {
03e98c9eb   Nicholas Bellinger   target: Address l...
298
299
  				cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  				rc = -EINVAL;
05d1c7c0d   Andy Grover   target: Make all ...
300
301
  				goto out;
  			}
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  		} else {
  			/*
  			 * Extact the RELATIVE TARGET PORT IDENTIFIER to identify
  			 * the Target Port in question for the the incoming
  			 * SET_TARGET_PORT_GROUPS op.
  			 */
  			rtpi = ((ptr[2] << 8) & 0xff);
  			rtpi |= (ptr[3] & 0xff);
  			/*
  			 * Locate the matching relative target port identifer
  			 * for the struct se_device storage object.
  			 */
  			spin_lock(&dev->se_port_lock);
  			list_for_each_entry(port, &dev->dev_sep_list,
  							sep_list) {
  				if (port->sep_rtpi != rtpi)
  					continue;
  
  				tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  				spin_unlock(&dev->se_port_lock);
  
  				rc = core_alua_set_tg_pt_secondary_state(
  						tg_pt_gp_mem, port, 1, 1);
  
  				spin_lock(&dev->se_port_lock);
  				break;
  			}
  			spin_unlock(&dev->se_port_lock);
  			/*
  			 * If not matching relative target port identifier can
  			 * be located, throw an exception with ASCQ:
  			 * INVALID_PARAMETER_LIST
  			 */
05d1c7c0d   Andy Grover   target: Make all ...
335
  			if (rc != 0) {
03e98c9eb   Nicholas Bellinger   target: Address l...
336
337
  				cmd->scsi_sense_reason = TCM_INVALID_PARAMETER_LIST;
  				rc = -EINVAL;
05d1c7c0d   Andy Grover   target: Make all ...
338
339
  				goto out;
  			}
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
340
341
342
343
344
  		}
  
  		ptr += 4;
  		len += 4;
  	}
05d1c7c0d   Andy Grover   target: Make all ...
345
346
  out:
  	transport_kunmap_first_data_page(cmd);
d29a5b6ac   Christoph Hellwig   target: remove SC...
347
348
  	task->task_scsi_status = GOOD;
  	transport_complete_task(task, 1);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
  	return 0;
  }
  
  static inline int core_alua_state_nonoptimized(
  	struct se_cmd *cmd,
  	unsigned char *cdb,
  	int nonop_delay_msecs,
  	u8 *alua_ascq)
  {
  	/*
  	 * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked
  	 * later to determine if processing of this cmd needs to be
  	 * temporarily delayed for the Active/NonOptimized primary access state.
  	 */
  	cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED;
  	cmd->alua_nonop_delay = nonop_delay_msecs;
  	return 0;
  }
  
  static inline int core_alua_state_standby(
  	struct se_cmd *cmd,
  	unsigned char *cdb,
  	u8 *alua_ascq)
  {
  	/*
  	 * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by
  	 * spc4r17 section 5.9.2.4.4
  	 */
  	switch (cdb[0]) {
  	case INQUIRY:
  	case LOG_SELECT:
  	case LOG_SENSE:
  	case MODE_SELECT:
  	case MODE_SENSE:
  	case REPORT_LUNS:
  	case RECEIVE_DIAGNOSTIC:
  	case SEND_DIAGNOSTIC:
  	case MAINTENANCE_IN:
  		switch (cdb[1]) {
  		case MI_REPORT_TARGET_PGS:
  			return 0;
  		default:
  			*alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  			return 1;
  		}
  	case MAINTENANCE_OUT:
  		switch (cdb[1]) {
  		case MO_SET_TARGET_PGS:
  			return 0;
  		default:
  			*alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  			return 1;
  		}
  	case REQUEST_SENSE:
  	case PERSISTENT_RESERVE_IN:
  	case PERSISTENT_RESERVE_OUT:
  	case READ_BUFFER:
  	case WRITE_BUFFER:
  		return 0;
  	default:
  		*alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY;
  		return 1;
  	}
  
  	return 0;
  }
  
  static inline int core_alua_state_unavailable(
  	struct se_cmd *cmd,
  	unsigned char *cdb,
  	u8 *alua_ascq)
  {
  	/*
  	 * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by
  	 * spc4r17 section 5.9.2.4.5
  	 */
  	switch (cdb[0]) {
  	case INQUIRY:
  	case REPORT_LUNS:
  	case MAINTENANCE_IN:
  		switch (cdb[1]) {
  		case MI_REPORT_TARGET_PGS:
  			return 0;
  		default:
  			*alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  			return 1;
  		}
  	case MAINTENANCE_OUT:
  		switch (cdb[1]) {
  		case MO_SET_TARGET_PGS:
  			return 0;
  		default:
  			*alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  			return 1;
  		}
  	case REQUEST_SENSE:
  	case READ_BUFFER:
  	case WRITE_BUFFER:
  		return 0;
  	default:
  		*alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE;
  		return 1;
  	}
  
  	return 0;
  }
  
  static inline int core_alua_state_transition(
  	struct se_cmd *cmd,
  	unsigned char *cdb,
  	u8 *alua_ascq)
  {
  	/*
  	 * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by
  	 * spc4r17 section 5.9.2.5
  	 */
  	switch (cdb[0]) {
  	case INQUIRY:
  	case REPORT_LUNS:
  	case MAINTENANCE_IN:
  		switch (cdb[1]) {
  		case MI_REPORT_TARGET_PGS:
  			return 0;
  		default:
  			*alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  			return 1;
  		}
  	case REQUEST_SENSE:
  	case READ_BUFFER:
  	case WRITE_BUFFER:
  		return 0;
  	default:
  		*alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION;
  		return 1;
  	}
  
  	return 0;
  }
  
  /*
   * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED
   * in transport_cmd_sequencer().  This function is assigned to
   * struct t10_alua *->state_check() in core_setup_alua()
   */
  static int core_alua_state_check_nop(
  	struct se_cmd *cmd,
  	unsigned char *cdb,
  	u8 *alua_ascq)
  {
  	return 0;
  }
  
  /*
   * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer().
   * This function is assigned to struct t10_alua *->state_check() in
   * core_setup_alua()
   *
   * Also, this function can return three different return codes to
   * signal transport_generic_cmd_sequencer()
   *
   * return 1: Is used to signal LUN not accecsable, and check condition/not ready
   * return 0: Used to signal success
   * reutrn -1: Used to signal failure, and invalid cdb field
   */
  static int core_alua_state_check(
  	struct se_cmd *cmd,
  	unsigned char *cdb,
  	u8 *alua_ascq)
  {
e3d6f909e   Andy Grover   target: Core clea...
518
  	struct se_lun *lun = cmd->se_lun;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
519
520
521
522
  	struct se_port *port = lun->lun_sep;
  	struct t10_alua_tg_pt_gp *tg_pt_gp;
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  	int out_alua_state, nonop_delay_msecs;
6708bb27b   Andy Grover   target: Follow up...
523
  	if (!port)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
524
525
526
527
528
529
530
  		return 0;
  	/*
  	 * First, check for a struct se_port specific secondary ALUA target port
  	 * access state: OFFLINE
  	 */
  	if (atomic_read(&port->sep_tg_pt_secondary_offline)) {
  		*alua_ascq = ASCQ_04H_ALUA_OFFLINE;
6708bb27b   Andy Grover   target: Follow up...
531
  		pr_debug("ALUA: Got secondary offline status for local"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
  				" target port
  ");
  		*alua_ascq = ASCQ_04H_ALUA_OFFLINE;
  		return 1;
  	}
  	 /*
  	 * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the
  	 * ALUA target port group, to obtain current ALUA access state.
  	 * Otherwise look for the underlying struct se_device association with
  	 * a ALUA logical unit group.
  	 */
  	tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
  	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
  	out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  	nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs;
  	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	/*
25985edce   Lucas De Marchi   Fix common misspe...
550
551
  	 * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a separate conditional
  	 * statement so the compiler knows explicitly to check this case first.
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
  	 * For the Optimized ALUA access state case, we want to process the
  	 * incoming fabric cmd ASAP..
  	 */
  	if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED)
  		return 0;
  
  	switch (out_alua_state) {
  	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  		return core_alua_state_nonoptimized(cmd, cdb,
  					nonop_delay_msecs, alua_ascq);
  	case ALUA_ACCESS_STATE_STANDBY:
  		return core_alua_state_standby(cmd, cdb, alua_ascq);
  	case ALUA_ACCESS_STATE_UNAVAILABLE:
  		return core_alua_state_unavailable(cmd, cdb, alua_ascq);
  	case ALUA_ACCESS_STATE_TRANSITION:
  		return core_alua_state_transition(cmd, cdb, alua_ascq);
  	/*
  	 * OFFLINE is a secondary ALUA target port group access state, that is
  	 * handled above with struct se_port->sep_tg_pt_secondary_offline=1
  	 */
  	case ALUA_ACCESS_STATE_OFFLINE:
  	default:
6708bb27b   Andy Grover   target: Follow up...
574
575
  		pr_err("Unknown ALUA access state: 0x%02x
  ",
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
576
  				out_alua_state);
e3d6f909e   Andy Grover   target: Core clea...
577
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
  	}
  
  	return 0;
  }
  
  /*
   * Check implict and explict ALUA state change request.
   */
  static int core_alua_check_transition(int state, int *primary)
  {
  	switch (state) {
  	case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  	case ALUA_ACCESS_STATE_STANDBY:
  	case ALUA_ACCESS_STATE_UNAVAILABLE:
  		/*
  		 * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are
  		 * defined as primary target port asymmetric access states.
  		 */
  		*primary = 1;
  		break;
  	case ALUA_ACCESS_STATE_OFFLINE:
  		/*
  		 * OFFLINE state is defined as a secondary target port
  		 * asymmetric access state.
  		 */
  		*primary = 0;
  		break;
  	default:
6708bb27b   Andy Grover   target: Follow up...
607
608
  		pr_err("Unknown ALUA access state: 0x%02x
  ", state);
e3d6f909e   Andy Grover   target: Core clea...
609
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
  	}
  
  	return 0;
  }
  
  static char *core_alua_dump_state(int state)
  {
  	switch (state) {
  	case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED:
  		return "Active/Optimized";
  	case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED:
  		return "Active/NonOptimized";
  	case ALUA_ACCESS_STATE_STANDBY:
  		return "Standby";
  	case ALUA_ACCESS_STATE_UNAVAILABLE:
  		return "Unavailable";
  	case ALUA_ACCESS_STATE_OFFLINE:
  		return "Offline";
  	default:
  		return "Unknown";
  	}
  
  	return NULL;
  }
  
  char *core_alua_dump_status(int status)
  {
  	switch (status) {
  	case ALUA_STATUS_NONE:
  		return "None";
  	case ALUA_STATUS_ALTERED_BY_EXPLICT_STPG:
  		return "Altered by Explict STPG";
  	case ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA:
  		return "Altered by Implict ALUA";
  	default:
  		return "Unknown";
  	}
  
  	return NULL;
  }
  
  /*
   * Used by fabric modules to determine when we need to delay processing
   * for the Active/NonOptimized paths..
   */
  int core_alua_check_nonop_delay(
  	struct se_cmd *cmd)
  {
  	if (!(cmd->se_cmd_flags & SCF_ALUA_NON_OPTIMIZED))
  		return 0;
  	if (in_interrupt())
  		return 0;
  	/*
  	 * The ALUA Active/NonOptimized access state delay can be disabled
  	 * in via configfs with a value of zero
  	 */
6708bb27b   Andy Grover   target: Follow up...
666
  	if (!cmd->alua_nonop_delay)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
  		return 0;
  	/*
  	 * struct se_cmd->alua_nonop_delay gets set by a target port group
  	 * defined interval in core_alua_state_nonoptimized()
  	 */
  	msleep_interruptible(cmd->alua_nonop_delay);
  	return 0;
  }
  EXPORT_SYMBOL(core_alua_check_nonop_delay);
  
  /*
   * Called with tg_pt_gp->tg_pt_gp_md_mutex or tg_pt_gp_mem->sep_tg_pt_md_mutex
   *
   */
  static int core_alua_write_tpg_metadata(
  	const char *path,
  	unsigned char *md_buf,
  	u32 md_buf_len)
  {
  	mm_segment_t old_fs;
  	struct file *file;
  	struct iovec iov[1];
  	int flags = O_RDWR | O_CREAT | O_TRUNC, ret;
  
  	memset(iov, 0, sizeof(struct iovec));
  
  	file = filp_open(path, flags, 0600);
  	if (IS_ERR(file) || !file || !file->f_dentry) {
6708bb27b   Andy Grover   target: Follow up...
695
696
  		pr_err("filp_open(%s) for ALUA metadata failed
  ",
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
697
698
699
700
701
702
703
704
705
706
707
708
709
  			path);
  		return -ENODEV;
  	}
  
  	iov[0].iov_base = &md_buf[0];
  	iov[0].iov_len = md_buf_len;
  
  	old_fs = get_fs();
  	set_fs(get_ds());
  	ret = vfs_writev(file, &iov[0], 1, &file->f_pos);
  	set_fs(old_fs);
  
  	if (ret < 0) {
6708bb27b   Andy Grover   target: Follow up...
710
711
  		pr_err("Error writing ALUA metadata file: %s
  ", path);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
  		filp_close(file, NULL);
  		return -EIO;
  	}
  	filp_close(file, NULL);
  
  	return 0;
  }
  
  /*
   * Called with tg_pt_gp->tg_pt_gp_md_mutex held
   */
  static int core_alua_update_tpg_primary_metadata(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	int primary_state,
  	unsigned char *md_buf)
  {
  	struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  	struct t10_wwn *wwn = &su_dev->t10_wwn;
  	char path[ALUA_METADATA_PATH_LEN];
  	int len;
  
  	memset(path, 0, ALUA_METADATA_PATH_LEN);
  
  	len = snprintf(md_buf, tg_pt_gp->tg_pt_gp_md_buf_len,
  			"tg_pt_gp_id=%hu
  "
  			"alua_access_state=0x%02x
  "
  			"alua_access_status=0x%02x
  ",
  			tg_pt_gp->tg_pt_gp_id, primary_state,
  			tg_pt_gp->tg_pt_gp_alua_access_status);
  
  	snprintf(path, ALUA_METADATA_PATH_LEN,
  		"/var/target/alua/tpgs_%s/%s", &wwn->unit_serial[0],
  		config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item));
  
  	return core_alua_write_tpg_metadata(path, md_buf, len);
  }
  
  static int core_alua_do_transition_tg_pt(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	struct se_port *l_port,
  	struct se_node_acl *nacl,
  	unsigned char *md_buf,
  	int new_state,
  	int explict)
  {
  	struct se_dev_entry *se_deve;
  	struct se_lun_acl *lacl;
  	struct se_port *port;
  	struct t10_alua_tg_pt_gp_member *mem;
  	int old_state = 0;
  	/*
  	 * Save the old primary ALUA access state, and set the current state
  	 * to ALUA_ACCESS_STATE_TRANSITION.
  	 */
  	old_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state);
  	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  			ALUA_ACCESS_STATE_TRANSITION);
  	tg_pt_gp->tg_pt_gp_alua_access_status = (explict) ?
  				ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  				ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
  	/*
  	 * Check for the optional ALUA primary state transition delay
  	 */
  	if (tg_pt_gp->tg_pt_gp_trans_delay_msecs != 0)
  		msleep_interruptible(tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  
  	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  	list_for_each_entry(mem, &tg_pt_gp->tg_pt_gp_mem_list,
  				tg_pt_gp_mem_list) {
  		port = mem->tg_pt;
  		/*
  		 * After an implicit target port asymmetric access state
  		 * change, a device server shall establish a unit attention
  		 * condition for the initiator port associated with every I_T
  		 * nexus with the additional sense code set to ASYMMETRIC
  		 * ACCESS STATE CHAGED.
  		 *
  		 * After an explicit target port asymmetric access state
  		 * change, a device server shall establish a unit attention
  		 * condition with the additional sense code set to ASYMMETRIC
  		 * ACCESS STATE CHANGED for the initiator port associated with
  		 * every I_T nexus other than the I_T nexus on which the SET
  		 * TARGET PORT GROUPS command
  		 */
  		atomic_inc(&mem->tg_pt_gp_mem_ref_cnt);
  		smp_mb__after_atomic_inc();
  		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  
  		spin_lock_bh(&port->sep_alua_lock);
  		list_for_each_entry(se_deve, &port->sep_alua_list,
  					alua_port_list) {
  			lacl = se_deve->se_lun_acl;
  			/*
  			 * se_deve->se_lun_acl pointer may be NULL for a
  			 * entry created without explict Node+MappedLUN ACLs
  			 */
6708bb27b   Andy Grover   target: Follow up...
811
  			if (!lacl)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
  				continue;
  
  			if (explict &&
  			   (nacl != NULL) && (nacl == lacl->se_lun_nacl) &&
  			   (l_port != NULL) && (l_port == port))
  				continue;
  
  			core_scsi3_ua_allocate(lacl->se_lun_nacl,
  				se_deve->mapped_lun, 0x2A,
  				ASCQ_2AH_ASYMMETRIC_ACCESS_STATE_CHANGED);
  		}
  		spin_unlock_bh(&port->sep_alua_lock);
  
  		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  		atomic_dec(&mem->tg_pt_gp_mem_ref_cnt);
  		smp_mb__after_atomic_dec();
  	}
  	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  	/*
  	 * Update the ALUA metadata buf that has been allocated in
  	 * core_alua_do_port_transition(), this metadata will be written
  	 * to struct file.
  	 *
  	 * Note that there is the case where we do not want to update the
  	 * metadata when the saved metadata is being parsed in userspace
  	 * when setting the existing port access state and access status.
  	 *
  	 * Also note that the failure to write out the ALUA metadata to
  	 * struct file does NOT affect the actual ALUA transition.
  	 */
  	if (tg_pt_gp->tg_pt_gp_write_metadata) {
  		mutex_lock(&tg_pt_gp->tg_pt_gp_md_mutex);
  		core_alua_update_tpg_primary_metadata(tg_pt_gp,
  					new_state, md_buf);
  		mutex_unlock(&tg_pt_gp->tg_pt_gp_md_mutex);
  	}
  	/*
  	 * Set the current primary ALUA access state to the requested new state
  	 */
  	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state, new_state);
6708bb27b   Andy Grover   target: Follow up...
852
  	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
  		" from primary access state %s to %s
  ", (explict) ? "explict" :
  		"implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  		tg_pt_gp->tg_pt_gp_id, core_alua_dump_state(old_state),
  		core_alua_dump_state(new_state));
  
  	return 0;
  }
  
  int core_alua_do_port_transition(
  	struct t10_alua_tg_pt_gp *l_tg_pt_gp,
  	struct se_device *l_dev,
  	struct se_port *l_port,
  	struct se_node_acl *l_nacl,
  	int new_state,
  	int explict)
  {
  	struct se_device *dev;
  	struct se_port *port;
  	struct se_subsystem_dev *su_dev;
  	struct se_node_acl *nacl;
  	struct t10_alua_lu_gp *lu_gp;
  	struct t10_alua_lu_gp_member *lu_gp_mem, *local_lu_gp_mem;
  	struct t10_alua_tg_pt_gp *tg_pt_gp;
  	unsigned char *md_buf;
  	int primary;
  
  	if (core_alua_check_transition(new_state, &primary) != 0)
  		return -EINVAL;
  
  	md_buf = kzalloc(l_tg_pt_gp->tg_pt_gp_md_buf_len, GFP_KERNEL);
6708bb27b   Andy Grover   target: Follow up...
884
885
886
  	if (!md_buf) {
  		pr_err("Unable to allocate buf for ALUA metadata
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
887
888
889
890
891
892
893
894
895
896
897
898
899
900
  		return -ENOMEM;
  	}
  
  	local_lu_gp_mem = l_dev->dev_alua_lu_gp_mem;
  	spin_lock(&local_lu_gp_mem->lu_gp_mem_lock);
  	lu_gp = local_lu_gp_mem->lu_gp;
  	atomic_inc(&lu_gp->lu_gp_ref_cnt);
  	smp_mb__after_atomic_inc();
  	spin_unlock(&local_lu_gp_mem->lu_gp_mem_lock);
  	/*
  	 * For storage objects that are members of the 'default_lu_gp',
  	 * we only do transition on the passed *l_tp_pt_gp, and not
  	 * on all of the matching target port groups IDs in default_lu_gp.
  	 */
6708bb27b   Andy Grover   target: Follow up...
901
  	if (!lu_gp->lu_gp_id) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
  		/*
  		 * core_alua_do_transition_tg_pt() will always return
  		 * success.
  		 */
  		core_alua_do_transition_tg_pt(l_tg_pt_gp, l_port, l_nacl,
  					md_buf, new_state, explict);
  		atomic_dec(&lu_gp->lu_gp_ref_cnt);
  		smp_mb__after_atomic_dec();
  		kfree(md_buf);
  		return 0;
  	}
  	/*
  	 * For all other LU groups aside from 'default_lu_gp', walk all of
  	 * the associated storage objects looking for a matching target port
  	 * group ID from the local target port group.
  	 */
  	spin_lock(&lu_gp->lu_gp_lock);
  	list_for_each_entry(lu_gp_mem, &lu_gp->lu_gp_mem_list,
  				lu_gp_mem_list) {
  
  		dev = lu_gp_mem->lu_gp_mem_dev;
  		su_dev = dev->se_sub_dev;
  		atomic_inc(&lu_gp_mem->lu_gp_mem_ref_cnt);
  		smp_mb__after_atomic_inc();
  		spin_unlock(&lu_gp->lu_gp_lock);
e3d6f909e   Andy Grover   target: Core clea...
927
  		spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
928
  		list_for_each_entry(tg_pt_gp,
e3d6f909e   Andy Grover   target: Core clea...
929
  				&su_dev->t10_alua.tg_pt_gps_list,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
930
  				tg_pt_gp_list) {
6708bb27b   Andy Grover   target: Follow up...
931
  			if (!tg_pt_gp->tg_pt_gp_valid_id)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
  				continue;
  			/*
  			 * If the target behavior port asymmetric access state
  			 * is changed for any target port group accessiable via
  			 * a logical unit within a LU group, the target port
  			 * behavior group asymmetric access states for the same
  			 * target port group accessible via other logical units
  			 * in that LU group will also change.
  			 */
  			if (l_tg_pt_gp->tg_pt_gp_id != tg_pt_gp->tg_pt_gp_id)
  				continue;
  
  			if (l_tg_pt_gp == tg_pt_gp) {
  				port = l_port;
  				nacl = l_nacl;
  			} else {
  				port = NULL;
  				nacl = NULL;
  			}
  			atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
  			smp_mb__after_atomic_inc();
e3d6f909e   Andy Grover   target: Core clea...
953
  			spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
954
955
956
957
958
959
  			/*
  			 * core_alua_do_transition_tg_pt() will always return
  			 * success.
  			 */
  			core_alua_do_transition_tg_pt(tg_pt_gp, port,
  					nacl, md_buf, new_state, explict);
e3d6f909e   Andy Grover   target: Core clea...
960
  			spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
961
962
963
  			atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
  			smp_mb__after_atomic_dec();
  		}
e3d6f909e   Andy Grover   target: Core clea...
964
  		spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
965
966
967
968
969
970
  
  		spin_lock(&lu_gp->lu_gp_lock);
  		atomic_dec(&lu_gp_mem->lu_gp_mem_ref_cnt);
  		smp_mb__after_atomic_dec();
  	}
  	spin_unlock(&lu_gp->lu_gp_lock);
6708bb27b   Andy Grover   target: Follow up...
971
  	pr_debug("Successfully processed LU Group: %s all ALUA TG PT"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
  		" Group IDs: %hu %s transition to primary state: %s
  ",
  		config_item_name(&lu_gp->lu_gp_group.cg_item),
  		l_tg_pt_gp->tg_pt_gp_id, (explict) ? "explict" : "implict",
  		core_alua_dump_state(new_state));
  
  	atomic_dec(&lu_gp->lu_gp_ref_cnt);
  	smp_mb__after_atomic_dec();
  	kfree(md_buf);
  	return 0;
  }
  
  /*
   * Called with tg_pt_gp_mem->sep_tg_pt_md_mutex held
   */
  static int core_alua_update_tpg_secondary_metadata(
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  	struct se_port *port,
  	unsigned char *md_buf,
  	u32 md_buf_len)
  {
  	struct se_portal_group *se_tpg = port->sep_tpg;
  	char path[ALUA_METADATA_PATH_LEN], wwn[ALUA_SECONDARY_METADATA_WWN_LEN];
  	int len;
  
  	memset(path, 0, ALUA_METADATA_PATH_LEN);
  	memset(wwn, 0, ALUA_SECONDARY_METADATA_WWN_LEN);
  
  	len = snprintf(wwn, ALUA_SECONDARY_METADATA_WWN_LEN, "%s",
e3d6f909e   Andy Grover   target: Core clea...
1001
  			se_tpg->se_tpg_tfo->tpg_get_wwn(se_tpg));
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1002

e3d6f909e   Andy Grover   target: Core clea...
1003
  	if (se_tpg->se_tpg_tfo->tpg_get_tag != NULL)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1004
  		snprintf(wwn+len, ALUA_SECONDARY_METADATA_WWN_LEN-len, "+%hu",
e3d6f909e   Andy Grover   target: Core clea...
1005
  				se_tpg->se_tpg_tfo->tpg_get_tag(se_tpg));
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1006
1007
1008
1009
1010
1011
1012
1013
1014
  
  	len = snprintf(md_buf, md_buf_len, "alua_tg_pt_offline=%d
  "
  			"alua_tg_pt_status=0x%02x
  ",
  			atomic_read(&port->sep_tg_pt_secondary_offline),
  			port->sep_tg_pt_secondary_stat);
  
  	snprintf(path, ALUA_METADATA_PATH_LEN, "/var/target/alua/%s/%s/lun_%u",
e3d6f909e   Andy Grover   target: Core clea...
1015
  			se_tpg->se_tpg_tfo->get_fabric_name(), wwn,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
  			port->sep_lun->unpacked_lun);
  
  	return core_alua_write_tpg_metadata(path, md_buf, len);
  }
  
  static int core_alua_set_tg_pt_secondary_state(
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  	struct se_port *port,
  	int explict,
  	int offline)
  {
  	struct t10_alua_tg_pt_gp *tg_pt_gp;
  	unsigned char *md_buf;
  	u32 md_buf_len;
  	int trans_delay_msecs;
  
  	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
6708bb27b   Andy Grover   target: Follow up...
1034
  	if (!tg_pt_gp) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1035
  		spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
6708bb27b   Andy Grover   target: Follow up...
1036
  		pr_err("Unable to complete secondary state"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1037
1038
  				" transition
  ");
e3d6f909e   Andy Grover   target: Core clea...
1039
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
  	}
  	trans_delay_msecs = tg_pt_gp->tg_pt_gp_trans_delay_msecs;
  	/*
  	 * Set the secondary ALUA target port access state to OFFLINE
  	 * or release the previously secondary state for struct se_port
  	 */
  	if (offline)
  		atomic_set(&port->sep_tg_pt_secondary_offline, 1);
  	else
  		atomic_set(&port->sep_tg_pt_secondary_offline, 0);
  
  	md_buf_len = tg_pt_gp->tg_pt_gp_md_buf_len;
  	port->sep_tg_pt_secondary_stat = (explict) ?
  			ALUA_STATUS_ALTERED_BY_EXPLICT_STPG :
  			ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA;
6708bb27b   Andy Grover   target: Follow up...
1055
  	pr_debug("Successful %s ALUA transition TG PT Group: %s ID: %hu"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
  		" to secondary access state: %s
  ", (explict) ? "explict" :
  		"implict", config_item_name(&tg_pt_gp->tg_pt_gp_group.cg_item),
  		tg_pt_gp->tg_pt_gp_id, (offline) ? "OFFLINE" : "ONLINE");
  
  	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	/*
  	 * Do the optional transition delay after we set the secondary
  	 * ALUA access state.
  	 */
  	if (trans_delay_msecs != 0)
  		msleep_interruptible(trans_delay_msecs);
  	/*
  	 * See if we need to update the ALUA fabric port metadata for
  	 * secondary state and status
  	 */
  	if (port->sep_tg_pt_secondary_write_md) {
  		md_buf = kzalloc(md_buf_len, GFP_KERNEL);
6708bb27b   Andy Grover   target: Follow up...
1074
1075
  		if (!md_buf) {
  			pr_err("Unable to allocate md_buf for"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1076
1077
  				" secondary ALUA access metadata
  ");
e3d6f909e   Andy Grover   target: Core clea...
1078
  			return -ENOMEM;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
  		}
  		mutex_lock(&port->sep_tg_pt_md_mutex);
  		core_alua_update_tpg_secondary_metadata(tg_pt_gp_mem, port,
  				md_buf, md_buf_len);
  		mutex_unlock(&port->sep_tg_pt_md_mutex);
  
  		kfree(md_buf);
  	}
  
  	return 0;
  }
  
  struct t10_alua_lu_gp *
  core_alua_allocate_lu_gp(const char *name, int def_group)
  {
  	struct t10_alua_lu_gp *lu_gp;
  
  	lu_gp = kmem_cache_zalloc(t10_alua_lu_gp_cache, GFP_KERNEL);
6708bb27b   Andy Grover   target: Follow up...
1097
1098
1099
  	if (!lu_gp) {
  		pr_err("Unable to allocate struct t10_alua_lu_gp
  ");
6eab04a87   Justin P. Mattock   treewide: remove ...
1100
  		return ERR_PTR(-ENOMEM);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1101
  	}
e3d6f909e   Andy Grover   target: Core clea...
1102
  	INIT_LIST_HEAD(&lu_gp->lu_gp_node);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1103
1104
1105
1106
1107
  	INIT_LIST_HEAD(&lu_gp->lu_gp_mem_list);
  	spin_lock_init(&lu_gp->lu_gp_lock);
  	atomic_set(&lu_gp->lu_gp_ref_cnt, 0);
  
  	if (def_group) {
e3d6f909e   Andy Grover   target: Core clea...
1108
  		lu_gp->lu_gp_id = alua_lu_gps_counter++;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1109
  		lu_gp->lu_gp_valid_id = 1;
e3d6f909e   Andy Grover   target: Core clea...
1110
  		alua_lu_gps_count++;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
  	}
  
  	return lu_gp;
  }
  
  int core_alua_set_lu_gp_id(struct t10_alua_lu_gp *lu_gp, u16 lu_gp_id)
  {
  	struct t10_alua_lu_gp *lu_gp_tmp;
  	u16 lu_gp_id_tmp;
  	/*
  	 * The lu_gp->lu_gp_id may only be set once..
  	 */
  	if (lu_gp->lu_gp_valid_id) {
6708bb27b   Andy Grover   target: Follow up...
1124
  		pr_warn("ALUA LU Group already has a valid ID,"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1125
1126
  			" ignoring request
  ");
e3d6f909e   Andy Grover   target: Core clea...
1127
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1128
  	}
e3d6f909e   Andy Grover   target: Core clea...
1129
1130
  	spin_lock(&lu_gps_lock);
  	if (alua_lu_gps_count == 0x0000ffff) {
6708bb27b   Andy Grover   target: Follow up...
1131
  		pr_err("Maximum ALUA alua_lu_gps_count:"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1132
1133
  				" 0x0000ffff reached
  ");
e3d6f909e   Andy Grover   target: Core clea...
1134
  		spin_unlock(&lu_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1135
  		kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
e3d6f909e   Andy Grover   target: Core clea...
1136
  		return -ENOSPC;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1137
1138
1139
  	}
  again:
  	lu_gp_id_tmp = (lu_gp_id != 0) ? lu_gp_id :
e3d6f909e   Andy Grover   target: Core clea...
1140
  				alua_lu_gps_counter++;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1141

e3d6f909e   Andy Grover   target: Core clea...
1142
  	list_for_each_entry(lu_gp_tmp, &lu_gps_list, lu_gp_node) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1143
  		if (lu_gp_tmp->lu_gp_id == lu_gp_id_tmp) {
6708bb27b   Andy Grover   target: Follow up...
1144
  			if (!lu_gp_id)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1145
  				goto again;
6708bb27b   Andy Grover   target: Follow up...
1146
  			pr_warn("ALUA Logical Unit Group ID: %hu"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1147
1148
1149
  				" already exists, ignoring request
  ",
  				lu_gp_id);
e3d6f909e   Andy Grover   target: Core clea...
1150
1151
  			spin_unlock(&lu_gps_lock);
  			return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1152
1153
1154
1155
1156
  		}
  	}
  
  	lu_gp->lu_gp_id = lu_gp_id_tmp;
  	lu_gp->lu_gp_valid_id = 1;
e3d6f909e   Andy Grover   target: Core clea...
1157
1158
1159
  	list_add_tail(&lu_gp->lu_gp_node, &lu_gps_list);
  	alua_lu_gps_count++;
  	spin_unlock(&lu_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
  
  	return 0;
  }
  
  static struct t10_alua_lu_gp_member *
  core_alua_allocate_lu_gp_mem(struct se_device *dev)
  {
  	struct t10_alua_lu_gp_member *lu_gp_mem;
  
  	lu_gp_mem = kmem_cache_zalloc(t10_alua_lu_gp_mem_cache, GFP_KERNEL);
6708bb27b   Andy Grover   target: Follow up...
1170
1171
1172
  	if (!lu_gp_mem) {
  		pr_err("Unable to allocate struct t10_alua_lu_gp_member
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
  		return ERR_PTR(-ENOMEM);
  	}
  	INIT_LIST_HEAD(&lu_gp_mem->lu_gp_mem_list);
  	spin_lock_init(&lu_gp_mem->lu_gp_mem_lock);
  	atomic_set(&lu_gp_mem->lu_gp_mem_ref_cnt, 0);
  
  	lu_gp_mem->lu_gp_mem_dev = dev;
  	dev->dev_alua_lu_gp_mem = lu_gp_mem;
  
  	return lu_gp_mem;
  }
  
  void core_alua_free_lu_gp(struct t10_alua_lu_gp *lu_gp)
  {
  	struct t10_alua_lu_gp_member *lu_gp_mem, *lu_gp_mem_tmp;
  	/*
  	 * Once we have reached this point, config_item_put() has
  	 * already been called from target_core_alua_drop_lu_gp().
  	 *
  	 * Here, we remove the *lu_gp from the global list so that
  	 * no associations can be made while we are releasing
  	 * struct t10_alua_lu_gp.
  	 */
e3d6f909e   Andy Grover   target: Core clea...
1196
  	spin_lock(&lu_gps_lock);
e3d6f909e   Andy Grover   target: Core clea...
1197
1198
1199
  	list_del(&lu_gp->lu_gp_node);
  	alua_lu_gps_count--;
  	spin_unlock(&lu_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
  	/*
  	 * Allow struct t10_alua_lu_gp * referenced by core_alua_get_lu_gp_by_name()
  	 * in target_core_configfs.c:target_core_store_alua_lu_gp() to be
  	 * released with core_alua_put_lu_gp_from_name()
  	 */
  	while (atomic_read(&lu_gp->lu_gp_ref_cnt))
  		cpu_relax();
  	/*
  	 * Release reference to struct t10_alua_lu_gp * from all associated
  	 * struct se_device.
  	 */
  	spin_lock(&lu_gp->lu_gp_lock);
  	list_for_each_entry_safe(lu_gp_mem, lu_gp_mem_tmp,
  				&lu_gp->lu_gp_mem_list, lu_gp_mem_list) {
  		if (lu_gp_mem->lu_gp_assoc) {
  			list_del(&lu_gp_mem->lu_gp_mem_list);
  			lu_gp->lu_gp_members--;
  			lu_gp_mem->lu_gp_assoc = 0;
  		}
  		spin_unlock(&lu_gp->lu_gp_lock);
  		/*
  		 *
25985edce   Lucas De Marchi   Fix common misspe...
1222
  		 * lu_gp_mem is associated with a single
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1223
1224
1225
1226
1227
1228
1229
  		 * struct se_device->dev_alua_lu_gp_mem, and is released when
  		 * struct se_device is released via core_alua_free_lu_gp_mem().
  		 *
  		 * If the passed lu_gp does NOT match the default_lu_gp, assume
  		 * we want to re-assocate a given lu_gp_mem with default_lu_gp.
  		 */
  		spin_lock(&lu_gp_mem->lu_gp_mem_lock);
e3d6f909e   Andy Grover   target: Core clea...
1230
  		if (lu_gp != default_lu_gp)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1231
  			__core_alua_attach_lu_gp_mem(lu_gp_mem,
e3d6f909e   Andy Grover   target: Core clea...
1232
  					default_lu_gp);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
  		else
  			lu_gp_mem->lu_gp = NULL;
  		spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  
  		spin_lock(&lu_gp->lu_gp_lock);
  	}
  	spin_unlock(&lu_gp->lu_gp_lock);
  
  	kmem_cache_free(t10_alua_lu_gp_cache, lu_gp);
  }
  
  void core_alua_free_lu_gp_mem(struct se_device *dev)
  {
  	struct se_subsystem_dev *su_dev = dev->se_sub_dev;
e3d6f909e   Andy Grover   target: Core clea...
1247
  	struct t10_alua *alua = &su_dev->t10_alua;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1248
1249
1250
1251
1252
1253
1254
  	struct t10_alua_lu_gp *lu_gp;
  	struct t10_alua_lu_gp_member *lu_gp_mem;
  
  	if (alua->alua_type != SPC3_ALUA_EMULATED)
  		return;
  
  	lu_gp_mem = dev->dev_alua_lu_gp_mem;
6708bb27b   Andy Grover   target: Follow up...
1255
  	if (!lu_gp_mem)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1256
1257
1258
1259
1260
1261
1262
  		return;
  
  	while (atomic_read(&lu_gp_mem->lu_gp_mem_ref_cnt))
  		cpu_relax();
  
  	spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  	lu_gp = lu_gp_mem->lu_gp;
6708bb27b   Andy Grover   target: Follow up...
1263
  	if (lu_gp) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
  		spin_lock(&lu_gp->lu_gp_lock);
  		if (lu_gp_mem->lu_gp_assoc) {
  			list_del(&lu_gp_mem->lu_gp_mem_list);
  			lu_gp->lu_gp_members--;
  			lu_gp_mem->lu_gp_assoc = 0;
  		}
  		spin_unlock(&lu_gp->lu_gp_lock);
  		lu_gp_mem->lu_gp = NULL;
  	}
  	spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
  
  	kmem_cache_free(t10_alua_lu_gp_mem_cache, lu_gp_mem);
  }
  
  struct t10_alua_lu_gp *core_alua_get_lu_gp_by_name(const char *name)
  {
  	struct t10_alua_lu_gp *lu_gp;
  	struct config_item *ci;
e3d6f909e   Andy Grover   target: Core clea...
1282
1283
  	spin_lock(&lu_gps_lock);
  	list_for_each_entry(lu_gp, &lu_gps_list, lu_gp_node) {
6708bb27b   Andy Grover   target: Follow up...
1284
  		if (!lu_gp->lu_gp_valid_id)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1285
1286
  			continue;
  		ci = &lu_gp->lu_gp_group.cg_item;
6708bb27b   Andy Grover   target: Follow up...
1287
  		if (!strcmp(config_item_name(ci), name)) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1288
  			atomic_inc(&lu_gp->lu_gp_ref_cnt);
e3d6f909e   Andy Grover   target: Core clea...
1289
  			spin_unlock(&lu_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1290
1291
1292
  			return lu_gp;
  		}
  	}
e3d6f909e   Andy Grover   target: Core clea...
1293
  	spin_unlock(&lu_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1294
1295
1296
1297
1298
1299
  
  	return NULL;
  }
  
  void core_alua_put_lu_gp_from_name(struct t10_alua_lu_gp *lu_gp)
  {
e3d6f909e   Andy Grover   target: Core clea...
1300
  	spin_lock(&lu_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1301
  	atomic_dec(&lu_gp->lu_gp_ref_cnt);
e3d6f909e   Andy Grover   target: Core clea...
1302
  	spin_unlock(&lu_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
  }
  
  /*
   * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
   */
  void __core_alua_attach_lu_gp_mem(
  	struct t10_alua_lu_gp_member *lu_gp_mem,
  	struct t10_alua_lu_gp *lu_gp)
  {
  	spin_lock(&lu_gp->lu_gp_lock);
  	lu_gp_mem->lu_gp = lu_gp;
  	lu_gp_mem->lu_gp_assoc = 1;
  	list_add_tail(&lu_gp_mem->lu_gp_mem_list, &lu_gp->lu_gp_mem_list);
  	lu_gp->lu_gp_members++;
  	spin_unlock(&lu_gp->lu_gp_lock);
  }
  
  /*
   * Called with struct t10_alua_lu_gp_member->lu_gp_mem_lock
   */
  void __core_alua_drop_lu_gp_mem(
  	struct t10_alua_lu_gp_member *lu_gp_mem,
  	struct t10_alua_lu_gp *lu_gp)
  {
  	spin_lock(&lu_gp->lu_gp_lock);
  	list_del(&lu_gp_mem->lu_gp_mem_list);
  	lu_gp_mem->lu_gp = NULL;
  	lu_gp_mem->lu_gp_assoc = 0;
  	lu_gp->lu_gp_members--;
  	spin_unlock(&lu_gp->lu_gp_lock);
  }
  
  struct t10_alua_tg_pt_gp *core_alua_allocate_tg_pt_gp(
  	struct se_subsystem_dev *su_dev,
  	const char *name,
  	int def_group)
  {
  	struct t10_alua_tg_pt_gp *tg_pt_gp;
  
  	tg_pt_gp = kmem_cache_zalloc(t10_alua_tg_pt_gp_cache, GFP_KERNEL);
6708bb27b   Andy Grover   target: Follow up...
1343
1344
1345
  	if (!tg_pt_gp) {
  		pr_err("Unable to allocate struct t10_alua_tg_pt_gp
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
  		return NULL;
  	}
  	INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_list);
  	INIT_LIST_HEAD(&tg_pt_gp->tg_pt_gp_mem_list);
  	mutex_init(&tg_pt_gp->tg_pt_gp_md_mutex);
  	spin_lock_init(&tg_pt_gp->tg_pt_gp_lock);
  	atomic_set(&tg_pt_gp->tg_pt_gp_ref_cnt, 0);
  	tg_pt_gp->tg_pt_gp_su_dev = su_dev;
  	tg_pt_gp->tg_pt_gp_md_buf_len = ALUA_MD_BUF_LEN;
  	atomic_set(&tg_pt_gp->tg_pt_gp_alua_access_state,
  		ALUA_ACCESS_STATE_ACTIVE_OPTMIZED);
  	/*
  	 * Enable both explict and implict ALUA support by default
  	 */
  	tg_pt_gp->tg_pt_gp_alua_access_type =
  			TPGS_EXPLICT_ALUA | TPGS_IMPLICT_ALUA;
  	/*
  	 * Set the default Active/NonOptimized Delay in milliseconds
  	 */
  	tg_pt_gp->tg_pt_gp_nonop_delay_msecs = ALUA_DEFAULT_NONOP_DELAY_MSECS;
  	tg_pt_gp->tg_pt_gp_trans_delay_msecs = ALUA_DEFAULT_TRANS_DELAY_MSECS;
  
  	if (def_group) {
e3d6f909e   Andy Grover   target: Core clea...
1369
  		spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1370
  		tg_pt_gp->tg_pt_gp_id =
e3d6f909e   Andy Grover   target: Core clea...
1371
  				su_dev->t10_alua.alua_tg_pt_gps_counter++;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1372
  		tg_pt_gp->tg_pt_gp_valid_id = 1;
e3d6f909e   Andy Grover   target: Core clea...
1373
  		su_dev->t10_alua.alua_tg_pt_gps_count++;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1374
  		list_add_tail(&tg_pt_gp->tg_pt_gp_list,
e3d6f909e   Andy Grover   target: Core clea...
1375
1376
  			      &su_dev->t10_alua.tg_pt_gps_list);
  		spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
  	}
  
  	return tg_pt_gp;
  }
  
  int core_alua_set_tg_pt_gp_id(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	u16 tg_pt_gp_id)
  {
  	struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  	struct t10_alua_tg_pt_gp *tg_pt_gp_tmp;
  	u16 tg_pt_gp_id_tmp;
  	/*
  	 * The tg_pt_gp->tg_pt_gp_id may only be set once..
  	 */
  	if (tg_pt_gp->tg_pt_gp_valid_id) {
6708bb27b   Andy Grover   target: Follow up...
1393
  		pr_warn("ALUA TG PT Group already has a valid ID,"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1394
1395
  			" ignoring request
  ");
e3d6f909e   Andy Grover   target: Core clea...
1396
  		return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1397
  	}
e3d6f909e   Andy Grover   target: Core clea...
1398
1399
  	spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  	if (su_dev->t10_alua.alua_tg_pt_gps_count == 0x0000ffff) {
6708bb27b   Andy Grover   target: Follow up...
1400
  		pr_err("Maximum ALUA alua_tg_pt_gps_count:"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1401
1402
  			" 0x0000ffff reached
  ");
e3d6f909e   Andy Grover   target: Core clea...
1403
  		spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1404
  		kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
e3d6f909e   Andy Grover   target: Core clea...
1405
  		return -ENOSPC;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1406
1407
1408
  	}
  again:
  	tg_pt_gp_id_tmp = (tg_pt_gp_id != 0) ? tg_pt_gp_id :
e3d6f909e   Andy Grover   target: Core clea...
1409
  			su_dev->t10_alua.alua_tg_pt_gps_counter++;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1410

e3d6f909e   Andy Grover   target: Core clea...
1411
  	list_for_each_entry(tg_pt_gp_tmp, &su_dev->t10_alua.tg_pt_gps_list,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1412
1413
  			tg_pt_gp_list) {
  		if (tg_pt_gp_tmp->tg_pt_gp_id == tg_pt_gp_id_tmp) {
6708bb27b   Andy Grover   target: Follow up...
1414
  			if (!tg_pt_gp_id)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1415
  				goto again;
6708bb27b   Andy Grover   target: Follow up...
1416
  			pr_err("ALUA Target Port Group ID: %hu already"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1417
1418
  				" exists, ignoring request
  ", tg_pt_gp_id);
e3d6f909e   Andy Grover   target: Core clea...
1419
1420
  			spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
  			return -EINVAL;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1421
1422
1423
1424
1425
1426
  		}
  	}
  
  	tg_pt_gp->tg_pt_gp_id = tg_pt_gp_id_tmp;
  	tg_pt_gp->tg_pt_gp_valid_id = 1;
  	list_add_tail(&tg_pt_gp->tg_pt_gp_list,
e3d6f909e   Andy Grover   target: Core clea...
1427
1428
1429
  			&su_dev->t10_alua.tg_pt_gps_list);
  	su_dev->t10_alua.alua_tg_pt_gps_count++;
  	spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
  
  	return 0;
  }
  
  struct t10_alua_tg_pt_gp_member *core_alua_allocate_tg_pt_gp_mem(
  	struct se_port *port)
  {
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  
  	tg_pt_gp_mem = kmem_cache_zalloc(t10_alua_tg_pt_gp_mem_cache,
  				GFP_KERNEL);
6708bb27b   Andy Grover   target: Follow up...
1441
1442
1443
  	if (!tg_pt_gp_mem) {
  		pr_err("Unable to allocate struct t10_alua_tg_pt_gp_member
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1444
1445
1446
1447
1448
1449
1450
1451
  		return ERR_PTR(-ENOMEM);
  	}
  	INIT_LIST_HEAD(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  	spin_lock_init(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	atomic_set(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt, 0);
  
  	tg_pt_gp_mem->tg_pt = port;
  	port->sep_alua_tg_pt_gp_mem = tg_pt_gp_mem;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
  
  	return tg_pt_gp_mem;
  }
  
  void core_alua_free_tg_pt_gp(
  	struct t10_alua_tg_pt_gp *tg_pt_gp)
  {
  	struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *tg_pt_gp_mem_tmp;
  	/*
  	 * Once we have reached this point, config_item_put() has already
  	 * been called from target_core_alua_drop_tg_pt_gp().
  	 *
  	 * Here we remove *tg_pt_gp from the global list so that
  	 * no assications *OR* explict ALUA via SET_TARGET_PORT_GROUPS
  	 * can be made while we are releasing struct t10_alua_tg_pt_gp.
  	 */
e3d6f909e   Andy Grover   target: Core clea...
1469
  	spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1470
  	list_del(&tg_pt_gp->tg_pt_gp_list);
e3d6f909e   Andy Grover   target: Core clea...
1471
1472
  	su_dev->t10_alua.alua_tg_pt_gps_counter--;
  	spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
  	/*
  	 * Allow a struct t10_alua_tg_pt_gp_member * referenced by
  	 * core_alua_get_tg_pt_gp_by_name() in
  	 * target_core_configfs.c:target_core_store_alua_tg_pt_gp()
  	 * to be released with core_alua_put_tg_pt_gp_from_name().
  	 */
  	while (atomic_read(&tg_pt_gp->tg_pt_gp_ref_cnt))
  		cpu_relax();
  	/*
  	 * Release reference to struct t10_alua_tg_pt_gp from all associated
  	 * struct se_port.
  	 */
  	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  	list_for_each_entry_safe(tg_pt_gp_mem, tg_pt_gp_mem_tmp,
  			&tg_pt_gp->tg_pt_gp_mem_list, tg_pt_gp_mem_list) {
  		if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  			list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  			tg_pt_gp->tg_pt_gp_members--;
  			tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  		}
  		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  		/*
25985edce   Lucas De Marchi   Fix common misspe...
1495
  		 * tg_pt_gp_mem is associated with a single
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1496
1497
1498
1499
1500
1501
1502
1503
  		 * se_port->sep_alua_tg_pt_gp_mem, and is released via
  		 * core_alua_free_tg_pt_gp_mem().
  		 *
  		 * If the passed tg_pt_gp does NOT match the default_tg_pt_gp,
  		 * assume we want to re-assocate a given tg_pt_gp_mem with
  		 * default_tg_pt_gp.
  		 */
  		spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
e3d6f909e   Andy Grover   target: Core clea...
1504
  		if (tg_pt_gp != su_dev->t10_alua.default_tg_pt_gp) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1505
  			__core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
e3d6f909e   Andy Grover   target: Core clea...
1506
  					su_dev->t10_alua.default_tg_pt_gp);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
  		} else
  			tg_pt_gp_mem->tg_pt_gp = NULL;
  		spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  
  		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  	}
  	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  
  	kmem_cache_free(t10_alua_tg_pt_gp_cache, tg_pt_gp);
  }
  
  void core_alua_free_tg_pt_gp_mem(struct se_port *port)
  {
  	struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
e3d6f909e   Andy Grover   target: Core clea...
1521
  	struct t10_alua *alua = &su_dev->t10_alua;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1522
1523
1524
1525
1526
1527
1528
  	struct t10_alua_tg_pt_gp *tg_pt_gp;
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  
  	if (alua->alua_type != SPC3_ALUA_EMULATED)
  		return;
  
  	tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
6708bb27b   Andy Grover   target: Follow up...
1529
  	if (!tg_pt_gp_mem)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1530
1531
1532
1533
1534
1535
1536
  		return;
  
  	while (atomic_read(&tg_pt_gp_mem->tg_pt_gp_mem_ref_cnt))
  		cpu_relax();
  
  	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
6708bb27b   Andy Grover   target: Follow up...
1537
  	if (tg_pt_gp) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
  		spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  		if (tg_pt_gp_mem->tg_pt_gp_assoc) {
  			list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  			tg_pt_gp->tg_pt_gp_members--;
  			tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  		}
  		spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  		tg_pt_gp_mem->tg_pt_gp = NULL;
  	}
  	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  
  	kmem_cache_free(t10_alua_tg_pt_gp_mem_cache, tg_pt_gp_mem);
  }
  
  static struct t10_alua_tg_pt_gp *core_alua_get_tg_pt_gp_by_name(
  	struct se_subsystem_dev *su_dev,
  	const char *name)
  {
  	struct t10_alua_tg_pt_gp *tg_pt_gp;
  	struct config_item *ci;
e3d6f909e   Andy Grover   target: Core clea...
1558
1559
  	spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
  	list_for_each_entry(tg_pt_gp, &su_dev->t10_alua.tg_pt_gps_list,
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1560
  			tg_pt_gp_list) {
6708bb27b   Andy Grover   target: Follow up...
1561
  		if (!tg_pt_gp->tg_pt_gp_valid_id)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1562
1563
  			continue;
  		ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
6708bb27b   Andy Grover   target: Follow up...
1564
  		if (!strcmp(config_item_name(ci), name)) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1565
  			atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt);
e3d6f909e   Andy Grover   target: Core clea...
1566
  			spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1567
1568
1569
  			return tg_pt_gp;
  		}
  	}
e3d6f909e   Andy Grover   target: Core clea...
1570
  	spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1571
1572
1573
1574
1575
1576
1577
1578
  
  	return NULL;
  }
  
  static void core_alua_put_tg_pt_gp_from_name(
  	struct t10_alua_tg_pt_gp *tg_pt_gp)
  {
  	struct se_subsystem_dev *su_dev = tg_pt_gp->tg_pt_gp_su_dev;
e3d6f909e   Andy Grover   target: Core clea...
1579
  	spin_lock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1580
  	atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt);
e3d6f909e   Andy Grover   target: Core clea...
1581
  	spin_unlock(&su_dev->t10_alua.tg_pt_gps_lock);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
  }
  
  /*
   * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
   */
  void __core_alua_attach_tg_pt_gp_mem(
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  	struct t10_alua_tg_pt_gp *tg_pt_gp)
  {
  	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  	tg_pt_gp_mem->tg_pt_gp = tg_pt_gp;
  	tg_pt_gp_mem->tg_pt_gp_assoc = 1;
  	list_add_tail(&tg_pt_gp_mem->tg_pt_gp_mem_list,
  			&tg_pt_gp->tg_pt_gp_mem_list);
  	tg_pt_gp->tg_pt_gp_members++;
  	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  }
  
  /*
   * Called with struct t10_alua_tg_pt_gp_member->tg_pt_gp_mem_lock held
   */
  static void __core_alua_drop_tg_pt_gp_mem(
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem,
  	struct t10_alua_tg_pt_gp *tg_pt_gp)
  {
  	spin_lock(&tg_pt_gp->tg_pt_gp_lock);
  	list_del(&tg_pt_gp_mem->tg_pt_gp_mem_list);
  	tg_pt_gp_mem->tg_pt_gp = NULL;
  	tg_pt_gp_mem->tg_pt_gp_assoc = 0;
  	tg_pt_gp->tg_pt_gp_members--;
  	spin_unlock(&tg_pt_gp->tg_pt_gp_lock);
  }
  
  ssize_t core_alua_show_tg_pt_gp_info(struct se_port *port, char *page)
  {
  	struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  	struct config_item *tg_pt_ci;
e3d6f909e   Andy Grover   target: Core clea...
1619
  	struct t10_alua *alua = &su_dev->t10_alua;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1620
1621
1622
1623
1624
1625
1626
1627
  	struct t10_alua_tg_pt_gp *tg_pt_gp;
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  	ssize_t len = 0;
  
  	if (alua->alua_type != SPC3_ALUA_EMULATED)
  		return len;
  
  	tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
6708bb27b   Andy Grover   target: Follow up...
1628
  	if (!tg_pt_gp_mem)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1629
1630
1631
1632
  		return len;
  
  	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
6708bb27b   Andy Grover   target: Follow up...
1633
  	if (tg_pt_gp) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
  		tg_pt_ci = &tg_pt_gp->tg_pt_gp_group.cg_item;
  		len += sprintf(page, "TG Port Alias: %s
  TG Port Group ID:"
  			" %hu
  TG Port Primary Access State: %s
  TG Port "
  			"Primary Access Status: %s
  TG Port Secondary Access"
  			" State: %s
  TG Port Secondary Access Status: %s
  ",
  			config_item_name(tg_pt_ci), tg_pt_gp->tg_pt_gp_id,
  			core_alua_dump_state(atomic_read(
  					&tg_pt_gp->tg_pt_gp_alua_access_state)),
  			core_alua_dump_status(
  				tg_pt_gp->tg_pt_gp_alua_access_status),
  			(atomic_read(&port->sep_tg_pt_secondary_offline)) ?
  			"Offline" : "None",
  			core_alua_dump_status(port->sep_tg_pt_secondary_stat));
  	}
  	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  
  	return len;
  }
  
  ssize_t core_alua_store_tg_pt_gp_info(
  	struct se_port *port,
  	const char *page,
  	size_t count)
  {
  	struct se_portal_group *tpg;
  	struct se_lun *lun;
  	struct se_subsystem_dev *su_dev = port->sep_lun->lun_se_dev->se_sub_dev;
  	struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *tg_pt_gp_new = NULL;
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  	unsigned char buf[TG_PT_GROUP_NAME_BUF];
  	int move = 0;
  
  	tpg = port->sep_tpg;
  	lun = port->sep_lun;
e3d6f909e   Andy Grover   target: Core clea...
1674
  	if (su_dev->t10_alua.alua_type != SPC3_ALUA_EMULATED) {
6708bb27b   Andy Grover   target: Follow up...
1675
  		pr_warn("SPC3_ALUA_EMULATED not enabled for"
e3d6f909e   Andy Grover   target: Core clea...
1676
1677
1678
  			" %s/tpgt_%hu/%s
  ", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  			tpg->se_tpg_tfo->tpg_get_tag(tpg),
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1679
1680
1681
1682
1683
  			config_item_name(&lun->lun_group.cg_item));
  		return -EINVAL;
  	}
  
  	if (count > TG_PT_GROUP_NAME_BUF) {
6708bb27b   Andy Grover   target: Follow up...
1684
1685
  		pr_err("ALUA Target Port Group alias too large!
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
  		return -EINVAL;
  	}
  	memset(buf, 0, TG_PT_GROUP_NAME_BUF);
  	memcpy(buf, page, count);
  	/*
  	 * Any ALUA target port group alias besides "NULL" means we will be
  	 * making a new group association.
  	 */
  	if (strcmp(strstrip(buf), "NULL")) {
  		/*
  		 * core_alua_get_tg_pt_gp_by_name() will increment reference to
  		 * struct t10_alua_tg_pt_gp.  This reference is released with
  		 * core_alua_put_tg_pt_gp_from_name() below.
  		 */
  		tg_pt_gp_new = core_alua_get_tg_pt_gp_by_name(su_dev,
  					strstrip(buf));
6708bb27b   Andy Grover   target: Follow up...
1702
  		if (!tg_pt_gp_new)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1703
1704
1705
  			return -ENODEV;
  	}
  	tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem;
6708bb27b   Andy Grover   target: Follow up...
1706
  	if (!tg_pt_gp_mem) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1707
1708
  		if (tg_pt_gp_new)
  			core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
6708bb27b   Andy Grover   target: Follow up...
1709
1710
  		pr_err("NULL struct se_port->sep_alua_tg_pt_gp_mem pointer
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1711
1712
1713
1714
1715
  		return -EINVAL;
  	}
  
  	spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  	tg_pt_gp = tg_pt_gp_mem->tg_pt_gp;
6708bb27b   Andy Grover   target: Follow up...
1716
  	if (tg_pt_gp) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1717
1718
1719
1720
  		/*
  		 * Clearing an existing tg_pt_gp association, and replacing
  		 * with the default_tg_pt_gp.
  		 */
6708bb27b   Andy Grover   target: Follow up...
1721
1722
  		if (!tg_pt_gp_new) {
  			pr_debug("Target_Core_ConfigFS: Moving"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1723
1724
1725
1726
  				" %s/tpgt_%hu/%s from ALUA Target Port Group:"
  				" alua/%s, ID: %hu back to"
  				" default_tg_pt_gp
  ",
e3d6f909e   Andy Grover   target: Core clea...
1727
1728
  				tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  				tpg->se_tpg_tfo->tpg_get_tag(tpg),
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1729
1730
1731
1732
1733
1734
1735
  				config_item_name(&lun->lun_group.cg_item),
  				config_item_name(
  					&tg_pt_gp->tg_pt_gp_group.cg_item),
  				tg_pt_gp->tg_pt_gp_id);
  
  			__core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  			__core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem,
e3d6f909e   Andy Grover   target: Core clea...
1736
  					su_dev->t10_alua.default_tg_pt_gp);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
  			spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
  
  			return count;
  		}
  		/*
  		 * Removing existing association of tg_pt_gp_mem with tg_pt_gp
  		 */
  		__core_alua_drop_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp);
  		move = 1;
  	}
  	/*
  	 * Associate tg_pt_gp_mem with tg_pt_gp_new.
  	 */
  	__core_alua_attach_tg_pt_gp_mem(tg_pt_gp_mem, tg_pt_gp_new);
  	spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock);
6708bb27b   Andy Grover   target: Follow up...
1752
  	pr_debug("Target_Core_ConfigFS: %s %s/tpgt_%hu/%s to ALUA"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1753
1754
  		" Target Port Group: alua/%s, ID: %hu
  ", (move) ?
e3d6f909e   Andy Grover   target: Core clea...
1755
1756
  		"Moving" : "Adding", tpg->se_tpg_tfo->tpg_get_wwn(tpg),
  		tpg->se_tpg_tfo->tpg_get_tag(tpg),
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
  		config_item_name(&lun->lun_group.cg_item),
  		config_item_name(&tg_pt_gp_new->tg_pt_gp_group.cg_item),
  		tg_pt_gp_new->tg_pt_gp_id);
  
  	core_alua_put_tg_pt_gp_from_name(tg_pt_gp_new);
  	return count;
  }
  
  ssize_t core_alua_show_access_type(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	char *page)
  {
  	if ((tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA) &&
  	    (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA))
  		return sprintf(page, "Implict and Explict
  ");
  	else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_IMPLICT_ALUA)
  		return sprintf(page, "Implict
  ");
  	else if (tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA)
  		return sprintf(page, "Explict
  ");
  	else
  		return sprintf(page, "None
  ");
  }
  
  ssize_t core_alua_store_access_type(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	const char *page,
  	size_t count)
  {
  	unsigned long tmp;
  	int ret;
  
  	ret = strict_strtoul(page, 0, &tmp);
  	if (ret < 0) {
6708bb27b   Andy Grover   target: Follow up...
1794
1795
  		pr_err("Unable to extract alua_access_type
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1796
1797
1798
  		return -EINVAL;
  	}
  	if ((tmp != 0) && (tmp != 1) && (tmp != 2) && (tmp != 3)) {
6708bb27b   Andy Grover   target: Follow up...
1799
  		pr_err("Illegal value for alua_access_type:"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
  				" %lu
  ", tmp);
  		return -EINVAL;
  	}
  	if (tmp == 3)
  		tg_pt_gp->tg_pt_gp_alua_access_type =
  			TPGS_IMPLICT_ALUA | TPGS_EXPLICT_ALUA;
  	else if (tmp == 2)
  		tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_EXPLICT_ALUA;
  	else if (tmp == 1)
  		tg_pt_gp->tg_pt_gp_alua_access_type = TPGS_IMPLICT_ALUA;
  	else
  		tg_pt_gp->tg_pt_gp_alua_access_type = 0;
  
  	return count;
  }
  
  ssize_t core_alua_show_nonop_delay_msecs(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	char *page)
  {
  	return sprintf(page, "%d
  ", tg_pt_gp->tg_pt_gp_nonop_delay_msecs);
  }
  
  ssize_t core_alua_store_nonop_delay_msecs(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	const char *page,
  	size_t count)
  {
  	unsigned long tmp;
  	int ret;
  
  	ret = strict_strtoul(page, 0, &tmp);
  	if (ret < 0) {
6708bb27b   Andy Grover   target: Follow up...
1835
1836
  		pr_err("Unable to extract nonop_delay_msecs
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1837
1838
1839
  		return -EINVAL;
  	}
  	if (tmp > ALUA_MAX_NONOP_DELAY_MSECS) {
6708bb27b   Andy Grover   target: Follow up...
1840
  		pr_err("Passed nonop_delay_msecs: %lu, exceeds"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
  			" ALUA_MAX_NONOP_DELAY_MSECS: %d
  ", tmp,
  			ALUA_MAX_NONOP_DELAY_MSECS);
  		return -EINVAL;
  	}
  	tg_pt_gp->tg_pt_gp_nonop_delay_msecs = (int)tmp;
  
  	return count;
  }
  
  ssize_t core_alua_show_trans_delay_msecs(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	char *page)
  {
  	return sprintf(page, "%d
  ", tg_pt_gp->tg_pt_gp_trans_delay_msecs);
  }
  
  ssize_t core_alua_store_trans_delay_msecs(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	const char *page,
  	size_t count)
  {
  	unsigned long tmp;
  	int ret;
  
  	ret = strict_strtoul(page, 0, &tmp);
  	if (ret < 0) {
6708bb27b   Andy Grover   target: Follow up...
1869
1870
  		pr_err("Unable to extract trans_delay_msecs
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1871
1872
1873
  		return -EINVAL;
  	}
  	if (tmp > ALUA_MAX_TRANS_DELAY_MSECS) {
6708bb27b   Andy Grover   target: Follow up...
1874
  		pr_err("Passed trans_delay_msecs: %lu, exceeds"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
  			" ALUA_MAX_TRANS_DELAY_MSECS: %d
  ", tmp,
  			ALUA_MAX_TRANS_DELAY_MSECS);
  		return -EINVAL;
  	}
  	tg_pt_gp->tg_pt_gp_trans_delay_msecs = (int)tmp;
  
  	return count;
  }
  
  ssize_t core_alua_show_preferred_bit(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	char *page)
  {
  	return sprintf(page, "%d
  ", tg_pt_gp->tg_pt_gp_pref);
  }
  
  ssize_t core_alua_store_preferred_bit(
  	struct t10_alua_tg_pt_gp *tg_pt_gp,
  	const char *page,
  	size_t count)
  {
  	unsigned long tmp;
  	int ret;
  
  	ret = strict_strtoul(page, 0, &tmp);
  	if (ret < 0) {
6708bb27b   Andy Grover   target: Follow up...
1903
1904
  		pr_err("Unable to extract preferred ALUA value
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1905
1906
1907
  		return -EINVAL;
  	}
  	if ((tmp != 0) && (tmp != 1)) {
6708bb27b   Andy Grover   target: Follow up...
1908
1909
  		pr_err("Illegal value for preferred ALUA: %lu
  ", tmp);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1910
1911
1912
1913
1914
1915
1916
1917
1918
  		return -EINVAL;
  	}
  	tg_pt_gp->tg_pt_gp_pref = (int)tmp;
  
  	return count;
  }
  
  ssize_t core_alua_show_offline_bit(struct se_lun *lun, char *page)
  {
6708bb27b   Andy Grover   target: Follow up...
1919
  	if (!lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
  		return -ENODEV;
  
  	return sprintf(page, "%d
  ",
  		atomic_read(&lun->lun_sep->sep_tg_pt_secondary_offline));
  }
  
  ssize_t core_alua_store_offline_bit(
  	struct se_lun *lun,
  	const char *page,
  	size_t count)
  {
  	struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem;
  	unsigned long tmp;
  	int ret;
6708bb27b   Andy Grover   target: Follow up...
1935
  	if (!lun->lun_sep)
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1936
1937
1938
1939
  		return -ENODEV;
  
  	ret = strict_strtoul(page, 0, &tmp);
  	if (ret < 0) {
6708bb27b   Andy Grover   target: Follow up...
1940
1941
  		pr_err("Unable to extract alua_tg_pt_offline value
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1942
1943
1944
  		return -EINVAL;
  	}
  	if ((tmp != 0) && (tmp != 1)) {
6708bb27b   Andy Grover   target: Follow up...
1945
1946
  		pr_err("Illegal value for alua_tg_pt_offline: %lu
  ",
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1947
1948
1949
1950
  				tmp);
  		return -EINVAL;
  	}
  	tg_pt_gp_mem = lun->lun_sep->sep_alua_tg_pt_gp_mem;
6708bb27b   Andy Grover   target: Follow up...
1951
1952
1953
  	if (!tg_pt_gp_mem) {
  		pr_err("Unable to locate *tg_pt_gp_mem
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
  		return -EINVAL;
  	}
  
  	ret = core_alua_set_tg_pt_secondary_state(tg_pt_gp_mem,
  			lun->lun_sep, 0, (int)tmp);
  	if (ret < 0)
  		return -EINVAL;
  
  	return count;
  }
  
  ssize_t core_alua_show_secondary_status(
  	struct se_lun *lun,
  	char *page)
  {
  	return sprintf(page, "%d
  ", lun->lun_sep->sep_tg_pt_secondary_stat);
  }
  
  ssize_t core_alua_store_secondary_status(
  	struct se_lun *lun,
  	const char *page,
  	size_t count)
  {
  	unsigned long tmp;
  	int ret;
  
  	ret = strict_strtoul(page, 0, &tmp);
  	if (ret < 0) {
6708bb27b   Andy Grover   target: Follow up...
1983
1984
  		pr_err("Unable to extract alua_tg_pt_status
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1985
1986
1987
1988
1989
  		return -EINVAL;
  	}
  	if ((tmp != ALUA_STATUS_NONE) &&
  	    (tmp != ALUA_STATUS_ALTERED_BY_EXPLICT_STPG) &&
  	    (tmp != ALUA_STATUS_ALTERED_BY_IMPLICT_ALUA)) {
6708bb27b   Andy Grover   target: Follow up...
1990
1991
  		pr_err("Illegal value for alua_tg_pt_status: %lu
  ",
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
  				tmp);
  		return -EINVAL;
  	}
  	lun->lun_sep->sep_tg_pt_secondary_stat = (int)tmp;
  
  	return count;
  }
  
  ssize_t core_alua_show_secondary_write_metadata(
  	struct se_lun *lun,
  	char *page)
  {
  	return sprintf(page, "%d
  ",
  			lun->lun_sep->sep_tg_pt_secondary_write_md);
  }
  
  ssize_t core_alua_store_secondary_write_metadata(
  	struct se_lun *lun,
  	const char *page,
  	size_t count)
  {
  	unsigned long tmp;
  	int ret;
  
  	ret = strict_strtoul(page, 0, &tmp);
  	if (ret < 0) {
6708bb27b   Andy Grover   target: Follow up...
2019
2020
  		pr_err("Unable to extract alua_tg_pt_write_md
  ");
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2021
2022
2023
  		return -EINVAL;
  	}
  	if ((tmp != 0) && (tmp != 1)) {
6708bb27b   Andy Grover   target: Follow up...
2024
  		pr_err("Illegal value for alua_tg_pt_write_md:"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
  				" %lu
  ", tmp);
  		return -EINVAL;
  	}
  	lun->lun_sep->sep_tg_pt_secondary_write_md = (int)tmp;
  
  	return count;
  }
  
  int core_setup_alua(struct se_device *dev, int force_pt)
  {
  	struct se_subsystem_dev *su_dev = dev->se_sub_dev;
e3d6f909e   Andy Grover   target: Core clea...
2037
  	struct t10_alua *alua = &su_dev->t10_alua;
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2038
2039
2040
2041
2042
2043
2044
  	struct t10_alua_lu_gp_member *lu_gp_mem;
  	/*
  	 * If this device is from Target_Core_Mod/pSCSI, use the ALUA logic
  	 * of the Underlying SCSI hardware.  In Linux/SCSI terms, this can
  	 * cause a problem because libata and some SATA RAID HBAs appear
  	 * under Linux/SCSI, but emulate SCSI logic themselves.
  	 */
e3d6f909e   Andy Grover   target: Core clea...
2045
2046
  	if (((dev->transport->transport_type == TRANSPORT_PLUGIN_PHBA_PDEV) &&
  	    !(dev->se_sub_dev->se_dev_attrib.emulate_alua)) || force_pt) {
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2047
2048
  		alua->alua_type = SPC_ALUA_PASSTHROUGH;
  		alua->alua_state_check = &core_alua_state_check_nop;
6708bb27b   Andy Grover   target: Follow up...
2049
  		pr_debug("%s: Using SPC_ALUA_PASSTHROUGH, no ALUA"
e3d6f909e   Andy Grover   target: Core clea...
2050
2051
  			" emulation
  ", dev->transport->name);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2052
2053
2054
2055
2056
2057
  		return 0;
  	}
  	/*
  	 * If SPC-3 or above is reported by real or emulated struct se_device,
  	 * use emulated ALUA.
  	 */
e3d6f909e   Andy Grover   target: Core clea...
2058
  	if (dev->transport->get_device_rev(dev) >= SCSI_3) {
6708bb27b   Andy Grover   target: Follow up...
2059
  		pr_debug("%s: Enabling ALUA Emulation for SPC-3"
e3d6f909e   Andy Grover   target: Core clea...
2060
2061
  			" device
  ", dev->transport->name);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2062
  		/*
25985edce   Lucas De Marchi   Fix common misspe...
2063
  		 * Associate this struct se_device with the default ALUA
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2064
2065
2066
  		 * LUN Group.
  		 */
  		lu_gp_mem = core_alua_allocate_lu_gp_mem(dev);
e3d6f909e   Andy Grover   target: Core clea...
2067
2068
  		if (IS_ERR(lu_gp_mem))
  			return PTR_ERR(lu_gp_mem);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2069
2070
2071
2072
2073
  
  		alua->alua_type = SPC3_ALUA_EMULATED;
  		alua->alua_state_check = &core_alua_state_check;
  		spin_lock(&lu_gp_mem->lu_gp_mem_lock);
  		__core_alua_attach_lu_gp_mem(lu_gp_mem,
e3d6f909e   Andy Grover   target: Core clea...
2074
  				default_lu_gp);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2075
  		spin_unlock(&lu_gp_mem->lu_gp_mem_lock);
6708bb27b   Andy Grover   target: Follow up...
2076
  		pr_debug("%s: Adding to default ALUA LU Group:"
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2077
2078
  			" core/alua/lu_gps/default_lu_gp
  ",
e3d6f909e   Andy Grover   target: Core clea...
2079
  			dev->transport->name);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2080
2081
2082
  	} else {
  		alua->alua_type = SPC2_ALUA_DISABLED;
  		alua->alua_state_check = &core_alua_state_check_nop;
6708bb27b   Andy Grover   target: Follow up...
2083
  		pr_debug("%s: Disabling ALUA Emulation for SPC-2"
e3d6f909e   Andy Grover   target: Core clea...
2084
2085
  			" device
  ", dev->transport->name);
c66ac9db8   Nicholas Bellinger   [SCSI] target: Ad...
2086
2087
2088
2089
  	}
  
  	return 0;
  }