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