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