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