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