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