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