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