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