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