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