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