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