diff options
Diffstat (limited to 'drivers')
35 files changed, 27472 insertions, 58 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig index dd0a5b5e9bf..9bfb71ff3a6 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig @@ -26,6 +26,8 @@ source "drivers/ata/Kconfig" source "drivers/md/Kconfig" +source "drivers/target/Kconfig" + source "drivers/message/fusion/Kconfig" source "drivers/firewire/Kconfig" diff --git a/drivers/Makefile b/drivers/Makefile index ef5132469f5..7eb35f47946 100644 --- a/drivers/Makefile +++ b/drivers/Makefile @@ -46,6 +46,7 @@ obj-y += macintosh/ obj-$(CONFIG_IDE) += ide/ obj-$(CONFIG_SCSI) += scsi/ obj-$(CONFIG_ATA) += ata/ +obj-$(CONFIG_TARGET_CORE) += target/ obj-$(CONFIG_MTD) += mtd/ obj-$(CONFIG_SPI) += spi/ obj-y += net/ diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c index b65e65aa07e..e56730214c0 100644 --- a/drivers/scsi/sd.c +++ b/drivers/scsi/sd.c @@ -990,30 +990,51 @@ out: static void set_media_not_present(struct scsi_disk *sdkp) { - sdkp->media_present = 0; - sdkp->capacity = 0; - sdkp->device->changed = 1; + if (sdkp->media_present) + sdkp->device->changed = 1; + + if (sdkp->device->removable) { + sdkp->media_present = 0; + sdkp->capacity = 0; + } +} + +static int media_not_present(struct scsi_disk *sdkp, + struct scsi_sense_hdr *sshdr) +{ + if (!scsi_sense_valid(sshdr)) + return 0; + + /* not invoked for commands that could return deferred errors */ + switch (sshdr->sense_key) { + case UNIT_ATTENTION: + case NOT_READY: + /* medium not present */ + if (sshdr->asc == 0x3A) { + set_media_not_present(sdkp); + return 1; + } + } + return 0; } /** - * sd_media_changed - check if our medium changed - * @disk: kernel device descriptor + * sd_check_events - check media events + * @disk: kernel device descriptor + * @clearing: disk events currently being cleared * - * Returns 0 if not applicable or no change; 1 if change + * Returns mask of DISK_EVENT_*. * * Note: this function is invoked from the block subsystem. **/ -static int sd_media_changed(struct gendisk *disk) +static unsigned int sd_check_events(struct gendisk *disk, unsigned int clearing) { struct scsi_disk *sdkp = scsi_disk(disk); struct scsi_device *sdp = sdkp->device; struct scsi_sense_hdr *sshdr = NULL; int retval; - SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_media_changed\n")); - - if (!sdp->removable) - return 0; + SCSI_LOG_HLQUEUE(3, sd_printk(KERN_INFO, sdkp, "sd_check_events\n")); /* * If the device is offline, don't send any commands - just pretend as @@ -1043,40 +1064,32 @@ static int sd_media_changed(struct gendisk *disk) sshdr); } - if (retval) { + /* failed to execute TUR, assume media not present */ + if (host_byte(retval)) { set_media_not_present(sdkp); goto out; } + if (media_not_present(sdkp, sshdr)) + goto out; + /* * For removable scsi disk we have to recognise the presence - * of a disk in the drive. This is kept in the struct scsi_disk - * struct and tested at open ! Daniel Roche (dan@lectra.fr) + * of a disk in the drive. */ + if (!sdkp->media_present) + sdp->changed = 1; sdkp->media_present = 1; - out: /* - * Report a media change under the following conditions: - * - * Medium is present now and wasn't present before. - * Medium wasn't present before and is present now. - * Medium was present at all times, but it changed while - * we weren't looking (sdp->changed is set). + * sdp->changed is set under the following conditions: * - * If there was no medium before and there is no medium now then - * don't report a change, even if a medium was inserted and removed - * while we weren't looking. + * Medium present state has changed in either direction. + * Device has indicated UNIT_ATTENTION. */ - retval = (sdkp->media_present != sdkp->previous_state || - (sdkp->media_present && sdp->changed)); - if (retval) - sdev_evt_send_simple(sdp, SDEV_EVT_MEDIA_CHANGE, GFP_KERNEL); - sdkp->previous_state = sdkp->media_present; - - /* sdp->changed indicates medium was changed or is not present */ - sdp->changed = !sdkp->media_present; kfree(sshdr); + retval = sdp->changed ? DISK_EVENT_MEDIA_CHANGE : 0; + sdp->changed = 0; return retval; } @@ -1169,7 +1182,7 @@ static const struct block_device_operations sd_fops = { #ifdef CONFIG_COMPAT .compat_ioctl = sd_compat_ioctl, #endif - .media_changed = sd_media_changed, + .check_events = sd_check_events, .revalidate_disk = sd_revalidate_disk, .unlock_native_capacity = sd_unlock_native_capacity, }; @@ -1312,23 +1325,6 @@ static int sd_done(struct scsi_cmnd *SCpnt) return good_bytes; } -static int media_not_present(struct scsi_disk *sdkp, - struct scsi_sense_hdr *sshdr) -{ - - if (!scsi_sense_valid(sshdr)) - return 0; - /* not invoked for commands that could return deferred errors */ - if (sshdr->sense_key != NOT_READY && - sshdr->sense_key != UNIT_ATTENTION) - return 0; - if (sshdr->asc != 0x3A) /* medium not present */ - return 0; - - set_media_not_present(sdkp); - return 1; -} - /* * spinup disk - called only in sd_revalidate_disk() */ @@ -1503,7 +1499,7 @@ static void read_capacity_error(struct scsi_disk *sdkp, struct scsi_device *sdp, */ if (sdp->removable && sense_valid && sshdr->sense_key == NOT_READY) - sdp->changed = 1; + set_media_not_present(sdkp); /* * We used to set media_present to 0 here to indicate no media @@ -2389,8 +2385,10 @@ static void sd_probe_async(void *data, async_cookie_t cookie) gd->driverfs_dev = &sdp->sdev_gendev; gd->flags = GENHD_FL_EXT_DEVT; - if (sdp->removable) + if (sdp->removable) { gd->flags |= GENHD_FL_REMOVABLE; + gd->events |= DISK_EVENT_MEDIA_CHANGE; + } add_disk(gd); sd_dif_config_host(sdkp); @@ -2472,7 +2470,6 @@ static int sd_probe(struct device *dev) sdkp->disk = gd; sdkp->index = index; atomic_set(&sdkp->openers, 0); - sdkp->previous_state = 1; if (!sdp->request_queue->rq_timeout) { if (sdp->type != TYPE_MOD) diff --git a/drivers/scsi/sd.h b/drivers/scsi/sd.h index 55488faf081..c9d8f6ca49e 100644 --- a/drivers/scsi/sd.h +++ b/drivers/scsi/sd.h @@ -55,7 +55,6 @@ struct scsi_disk { u8 media_present; u8 write_prot; u8 protection_type;/* Data Integrity Field */ - unsigned previous_state : 1; unsigned ATO : 1; /* state of disk ATO bit */ unsigned WCE : 1; /* state of disk WCE bit */ unsigned RCD : 1; /* state of disk RCD bit, unused */ diff --git a/drivers/scsi/sr.c b/drivers/scsi/sr.c index be6baf8ad70..aefadc6a160 100644 --- a/drivers/scsi/sr.c +++ b/drivers/scsi/sr.c @@ -249,10 +249,6 @@ skip_tur: cd->device->changed = 0; } - /* for backward compatibility */ - if (events & DISK_EVENT_MEDIA_CHANGE) - sdev_evt_send_simple(cd->device, SDEV_EVT_MEDIA_CHANGE, - GFP_KERNEL); return events; } diff --git a/drivers/target/Kconfig b/drivers/target/Kconfig new file mode 100644 index 00000000000..2fac3be209a --- /dev/null +++ b/drivers/target/Kconfig @@ -0,0 +1,32 @@ + +menuconfig TARGET_CORE + tristate "Generic Target Core Mod (TCM) and ConfigFS Infrastructure" + depends on SCSI && BLOCK + select CONFIGFS_FS + default n + help + Say Y or M here to enable the TCM Storage Engine and ConfigFS enabled + control path for target_core_mod. This includes built-in TCM RAMDISK + subsystem logic for virtual LUN 0 access + +if TARGET_CORE + +config TCM_IBLOCK + tristate "TCM/IBLOCK Subsystem Plugin for Linux/BLOCK" + help + Say Y here to enable the TCM/IBLOCK subsystem plugin for non-buffered + access to Linux/Block devices using BIO + +config TCM_FILEIO + tristate "TCM/FILEIO Subsystem Plugin for Linux/VFS" + help + Say Y here to enable the TCM/FILEIO subsystem plugin for buffered + access to Linux/VFS struct file or struct block_device + +config TCM_PSCSI + tristate "TCM/pSCSI Subsystem Plugin for Linux/SCSI" + help + Say Y here to enable the TCM/pSCSI subsystem plugin for non-buffered + passthrough access to Linux/SCSI device + +endif diff --git a/drivers/target/Makefile b/drivers/target/Makefile new file mode 100644 index 00000000000..5cfd70819f0 --- /dev/null +++ b/drivers/target/Makefile @@ -0,0 +1,24 @@ +EXTRA_CFLAGS += -I$(srctree)/drivers/target/ -I$(srctree)/drivers/scsi/ + +target_core_mod-y := target_core_configfs.o \ + target_core_device.o \ + target_core_fabric_configfs.o \ + target_core_fabric_lib.o \ + target_core_hba.o \ + target_core_pr.o \ + target_core_alua.o \ + target_core_scdb.o \ + target_core_tmr.o \ + target_core_tpg.o \ + target_core_transport.o \ + target_core_cdb.o \ + target_core_ua.o \ + target_core_rd.o \ + target_core_mib.o + +obj-$(CONFIG_TARGET_CORE) += target_core_mod.o + +# Subsystem modules +obj-$(CONFIG_TCM_IBLOCK) += target_core_iblock.o +obj-$(CONFIG_TCM_FILEIO) += target_core_file.o +obj-$(CONFIG_TCM_PSCSI) += target_core_pscsi.o diff --git a/drivers/target/target_core_alua.c b/drivers/target/target_core_alua.c new file mode 100644 index 00000000000..2c5fcfed593 --- /dev/null +++ b/drivers/target/target_core_alua.c @@ -0,0 +1,1991 @@ +/******************************************************************************* + * Filename: target_core_alua.c + * + * This file contains SPC-3 compliant asymmetric logical unit assigntment (ALUA) + * + * Copyright (c) 2009-2010 Rising Tide Systems + * Copyright (c) 2009-2010 Linux-iSCSI.org + * + * Nicholas A. Bellinger <nab@kernel.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + ******************************************************************************/ + +#include <linux/version.h> +#include <linux/slab.h> +#include <linux/spinlock.h> +#include <linux/configfs.h> +#include <scsi/scsi.h> +#include <scsi/scsi_cmnd.h> + +#include <target/target_core_base.h> +#include <target/target_core_device.h> +#include <target/target_core_transport.h> +#include <target/target_core_fabric_ops.h> +#include <target/target_core_configfs.h> + +#include "target_core_alua.h" +#include "target_core_hba.h" +#include "target_core_ua.h" + +static int core_alua_check_transition(int state, int *primary); +static int core_alua_set_tg_pt_secondary_state( + struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, + struct se_port *port, int explict, int offline); + +/* + * REPORT_TARGET_PORT_GROUPS + * + * See spc4r17 section 6.27 + */ +int core_emulate_report_target_port_groups(struct se_cmd *cmd) +{ + struct se_subsystem_dev *su_dev = SE_DEV(cmd)->se_sub_dev; + struct se_port *port; + struct t10_alua_tg_pt_gp *tg_pt_gp; + struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; + unsigned char *buf = (unsigned char *)T_TASK(cmd)->t_task_buf; + u32 rd_len = 0, off = 4; /* Skip over RESERVED area to first + Target port group descriptor */ + + spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock); + list_for_each_entry(tg_pt_gp, &T10_ALUA(su_dev)->tg_pt_gps_list, + tg_pt_gp_list) { + /* + * PREF: Preferred target port bit, determine if this + * bit should be set for port group. + */ + if (tg_pt_gp->tg_pt_gp_pref) + buf[off] = 0x80; + /* + * Set the ASYMMETRIC ACCESS State + */ + buf[off++] |= (atomic_read( + &tg_pt_gp->tg_pt_gp_alua_access_state) & 0xff); + /* + * Set supported ASYMMETRIC ACCESS State bits + */ + buf[off] = 0x80; /* T_SUP */ + buf[off] |= 0x40; /* O_SUP */ + buf[off] |= 0x8; /* U_SUP */ + buf[off] |= 0x4; /* S_SUP */ + buf[off] |= 0x2; /* AN_SUP */ + buf[off++] |= 0x1; /* AO_SUP */ + /* + * TARGET PORT GROUP + */ + buf[off++] = ((tg_pt_gp->tg_pt_gp_id >> 8) & 0xff); + buf[off++] = (tg_pt_gp->tg_pt_gp_id & 0xff); + + off++; /* Skip over Reserved */ + /* + * STATUS CODE + */ + buf[off++] = (tg_pt_gp->tg_pt_gp_alua_access_status & 0xff); + /* + * Vendor Specific field + */ + buf[off++] = 0x00; + /* + * TARGET PORT COUNT + */ + buf[off++] = (tg_pt_gp->tg_pt_gp_members & 0xff); + rd_len += 8; + + spin_lock(&tg_pt_gp->tg_pt_gp_lock); + list_for_each_entry(tg_pt_gp_mem, &tg_pt_gp->tg_pt_gp_mem_list, + tg_pt_gp_mem_list) { + port = tg_pt_gp_mem->tg_pt; + /* + * Start Target Port descriptor format + * + * See spc4r17 section 6.2.7 Table 247 + */ + off += 2; /* Skip over Obsolete */ + /* + * Set RELATIVE TARGET PORT IDENTIFIER + */ + buf[off++] = ((port->sep_rtpi >> 8) & 0xff); + buf[off++] = (port->sep_rtpi & 0xff); + rd_len += 4; + } + spin_unlock(&tg_pt_gp->tg_pt_gp_lock); + } + spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock); + /* + * Set the RETURN DATA LENGTH set in the header of the DataIN Payload + */ + buf[0] = ((rd_len >> 24) & 0xff); + buf[1] = ((rd_len >> 16) & 0xff); + buf[2] = ((rd_len >> 8) & 0xff); + buf[3] = (rd_len & 0xff); + + return 0; +} + +/* + * SET_TARGET_PORT_GROUPS for explict ALUA operation. + * + * See spc4r17 section 6.35 + */ +int core_emulate_set_target_port_groups(struct se_cmd *cmd) +{ + struct se_device *dev = SE_DEV(cmd); + struct se_subsystem_dev *su_dev = SE_DEV(cmd)->se_sub_dev; + struct se_port *port, *l_port = SE_LUN(cmd)->lun_sep; + struct se_node_acl *nacl = SE_SESS(cmd)->se_node_acl; + struct t10_alua_tg_pt_gp *tg_pt_gp = NULL, *l_tg_pt_gp; + struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem, *l_tg_pt_gp_mem; + unsigned char *buf = (unsigned char *)T_TASK(cmd)->t_task_buf; + unsigned char *ptr = &buf[4]; /* Skip over RESERVED area in header */ + u32 len = 4; /* Skip over RESERVED area in header */ + int alua_access_state, primary = 0, rc; + u16 tg_pt_id, rtpi; + + if (!(l_port)) + return PYX_TRANSPORT_LU_COMM_FAILURE; + /* + * Determine if explict ALUA via SET_TARGET_PORT_GROUPS is allowed + * for the local tg_pt_gp. + */ + l_tg_pt_gp_mem = l_port->sep_alua_tg_pt_gp_mem; + if (!(l_tg_pt_gp_mem)) { + printk(KERN_ERR "Unable to access l_port->sep_alua_tg_pt_gp_mem\n"); + return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE; + } + spin_lock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock); + l_tg_pt_gp = l_tg_pt_gp_mem->tg_pt_gp; + if (!(l_tg_pt_gp)) { + spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock); + printk(KERN_ERR "Unable to access *l_tg_pt_gp_mem->tg_pt_gp\n"); + return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE; + } + rc = (l_tg_pt_gp->tg_pt_gp_alua_access_type & TPGS_EXPLICT_ALUA); + spin_unlock(&l_tg_pt_gp_mem->tg_pt_gp_mem_lock); + + if (!(rc)) { + printk(KERN_INFO "Unable to process SET_TARGET_PORT_GROUPS" + " while TPGS_EXPLICT_ALUA is disabled\n"); + return PYX_TRANSPORT_UNKNOWN_SAM_OPCODE; + } + + while (len < cmd->data_length) { + alua_access_state = (ptr[0] & 0x0f); + /* + * Check the received ALUA access state, and determine if + * the state is a primary or secondary target port asymmetric + * access state. + */ + rc = core_alua_check_transition(alua_access_state, &primary); + if (rc != 0) { + /* + * If the SET TARGET PORT GROUPS attempts to establish + * an invalid combination of target port asymmetric + * access states or attempts to establish an + * unsupported target port asymmetric access state, + * then the command shall be terminated with CHECK + * CONDITION status, with the sense key set to ILLEGAL + * REQUEST, and the additional sense code set to INVALID + * FIELD IN PARAMETER LIST. + */ + return PYX_TRANSPORT_INVALID_PARAMETER_LIST; + } + rc = -1; + /* + * If the ASYMMETRIC ACCESS STATE field (see table 267) + * specifies a primary target port asymmetric access state, + * then the TARGET PORT GROUP OR TARGET PORT field specifies + * a primary target port group for which the primary target + * port asymmetric access state shall be changed. If the + * ASYMMETRIC ACCESS STATE field specifies a secondary target + * port asymmetric access state, then the TARGET PORT GROUP OR + * TARGET PORT field specifies the relative target port + * identifier (see 3.1.120) of the target port for which the + * secondary target port asymmetric access state shall be + * changed. + */ + if (primary) { + tg_pt_id = ((ptr[2] << 8) & 0xff); + tg_pt_id |= (ptr[3] & 0xff); + /* + * Locate the matching target port group ID from + * the global tg_pt_gp list + */ + spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock); + list_for_each_entry(tg_pt_gp, + &T10_ALUA(su_dev)->tg_pt_gps_list, + tg_pt_gp_list) { + if (!(tg_pt_gp->tg_pt_gp_valid_id)) + continue; + + if (tg_pt_id != tg_pt_gp->tg_pt_gp_id) + continue; + + atomic_inc(&tg_pt_gp->tg_pt_gp_ref_cnt); + smp_mb__after_atomic_inc(); + spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock); + + rc = core_alua_do_port_transition(tg_pt_gp, + dev, l_port, nacl, + alua_access_state, 1); + + spin_lock(&T10_ALUA(su_dev)->tg_pt_gps_lock); + atomic_dec(&tg_pt_gp->tg_pt_gp_ref_cnt); + smp_mb__after_atomic_dec(); + break; + } + spin_unlock(&T10_ALUA(su_dev)->tg_pt_gps_lock); + /* + * If not matching target port group ID can be located + * throw an exception with ASCQ: INVALID_PARAMETER_LIST + */ + if (rc != 0) + return PYX_TRANSPORT_INVALID_PARAMETER_LIST; + } else { + /* + * Extact the RELATIVE TARGET PORT IDENTIFIER to identify + * the Target Port in question for the the incoming + * SET_TARGET_PORT_GROUPS op. + */ + rtpi = ((ptr[2] << 8) & 0xff); + rtpi |= (ptr[3] & 0xff); + /* + * Locate the matching relative target port identifer + * for the struct se_device storage object. + */ + spin_lock(&dev->se_port_lock); + list_for_each_entry(port, &dev->dev_sep_list, + sep_list) { + if (port->sep_rtpi != rtpi) + continue; + + tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; + spin_unlock(&dev->se_port_lock); + + rc = core_alua_set_tg_pt_secondary_state( + tg_pt_gp_mem, port, 1, 1); + + spin_lock(&dev->se_port_lock); + break; + } + spin_unlock(&dev->se_port_lock); + /* + * If not matching relative target port identifier can + * be located, throw an exception with ASCQ: + * INVALID_PARAMETER_LIST + */ + if (rc != 0) + return PYX_TRANSPORT_INVALID_PARAMETER_LIST; + } + + ptr += 4; + len += 4; + } + + return 0; +} + +static inline int core_alua_state_nonoptimized( + struct se_cmd *cmd, + unsigned char *cdb, + int nonop_delay_msecs, + u8 *alua_ascq) +{ + /* + * Set SCF_ALUA_NON_OPTIMIZED here, this value will be checked + * later to determine if processing of this cmd needs to be + * temporarily delayed for the Active/NonOptimized primary access state. + */ + cmd->se_cmd_flags |= SCF_ALUA_NON_OPTIMIZED; + cmd->alua_nonop_delay = nonop_delay_msecs; + return 0; +} + +static inline int core_alua_state_standby( + struct se_cmd *cmd, + unsigned char *cdb, + u8 *alua_ascq) +{ + /* + * Allowed CDBs for ALUA_ACCESS_STATE_STANDBY as defined by + * spc4r17 section 5.9.2.4.4 + */ + switch (cdb[0]) { + case INQUIRY: + case LOG_SELECT: + case LOG_SENSE: + case MODE_SELECT: + case MODE_SENSE: + case REPORT_LUNS: + case RECEIVE_DIAGNOSTIC: + case SEND_DIAGNOSTIC: + case MAINTENANCE_IN: + switch (cdb[1]) { + case MI_REPORT_TARGET_PGS: + return 0; + default: + *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY; + return 1; + } + case MAINTENANCE_OUT: + switch (cdb[1]) { + case MO_SET_TARGET_PGS: + return 0; + default: + *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY; + return 1; + } + case REQUEST_SENSE: + case PERSISTENT_RESERVE_IN: + case PERSISTENT_RESERVE_OUT: + case READ_BUFFER: + case WRITE_BUFFER: + return 0; + default: + *alua_ascq = ASCQ_04H_ALUA_TG_PT_STANDBY; + return 1; + } + + return 0; +} + +static inline int core_alua_state_unavailable( + struct se_cmd *cmd, + unsigned char *cdb, + u8 *alua_ascq) +{ + /* + * Allowed CDBs for ALUA_ACCESS_STATE_UNAVAILABLE as defined by + * spc4r17 section 5.9.2.4.5 + */ + switch (cdb[0]) { + case INQUIRY: + case REPORT_LUNS: + case MAINTENANCE_IN: + switch (cdb[1]) { + case MI_REPORT_TARGET_PGS: + return 0; + default: + *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE; + return 1; + } + case MAINTENANCE_OUT: + switch (cdb[1]) { + case MO_SET_TARGET_PGS: + return 0; + default: + *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE; + return 1; + } + case REQUEST_SENSE: + case READ_BUFFER: + case WRITE_BUFFER: + return 0; + default: + *alua_ascq = ASCQ_04H_ALUA_TG_PT_UNAVAILABLE; + return 1; + } + + return 0; +} + +static inline int core_alua_state_transition( + struct se_cmd *cmd, + unsigned char *cdb, + u8 *alua_ascq) +{ + /* + * Allowed CDBs for ALUA_ACCESS_STATE_TRANSITIO as defined by + * spc4r17 section 5.9.2.5 + */ + switch (cdb[0]) { + case INQUIRY: + case REPORT_LUNS: + case MAINTENANCE_IN: + switch (cdb[1]) { + case MI_REPORT_TARGET_PGS: + return 0; + default: + *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION; + return 1; + } + case REQUEST_SENSE: + case READ_BUFFER: + case WRITE_BUFFER: + return 0; + default: + *alua_ascq = ASCQ_04H_ALUA_STATE_TRANSITION; + return 1; + } + + return 0; +} + +/* + * Used for alua_type SPC_ALUA_PASSTHROUGH and SPC2_ALUA_DISABLED + * in transport_cmd_sequencer(). This function is assigned to + * struct t10_alua *->state_check() in core_setup_alua() + */ +static int core_alua_state_check_nop( + struct se_cmd *cmd, + unsigned char *cdb, + u8 *alua_ascq) +{ + return 0; +} + +/* + * Used for alua_type SPC3_ALUA_EMULATED in transport_cmd_sequencer(). + * This function is assigned to struct t10_alua *->state_check() in + * core_setup_alua() + * + * Also, this function can return three different return codes to + * signal transport_generic_cmd_sequencer() + * + * return 1: Is used to signal LUN not accecsable, and check condition/not ready + * return 0: Used to signal success + * reutrn -1: Used to signal failure, and invalid cdb field + */ +static int core_alua_state_check( + struct se_cmd *cmd, + unsigned char *cdb, + u8 *alua_ascq) +{ + struct se_lun *lun = SE_LUN(cmd); + struct se_port *port = lun->lun_sep; + struct t10_alua_tg_pt_gp *tg_pt_gp; + struct t10_alua_tg_pt_gp_member *tg_pt_gp_mem; + int out_alua_state, nonop_delay_msecs; + + if (!(port)) + return 0; + /* + * First, check for a struct se_port specific secondary ALUA target port + * access state: OFFLINE + */ + if (atomic_read(&port->sep_tg_pt_secondary_offline)) { + *alua_ascq = ASCQ_04H_ALUA_OFFLINE; + printk(KERN_INFO "ALUA: Got secondary offline status for local" + " target port\n"); + *alua_ascq = ASCQ_04H_ALUA_OFFLINE; + return 1; + } + /* + * Second, obtain the struct t10_alua_tg_pt_gp_member pointer to the + * ALUA target port group, to obtain current ALUA access state. + * Otherwise look for the underlying struct se_device association with + * a ALUA logical unit group. + */ + tg_pt_gp_mem = port->sep_alua_tg_pt_gp_mem; + spin_lock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); + tg_pt_gp = tg_pt_gp_mem->tg_pt_gp; + out_alua_state = atomic_read(&tg_pt_gp->tg_pt_gp_alua_access_state); + nonop_delay_msecs = tg_pt_gp->tg_pt_gp_nonop_delay_msecs; + spin_unlock(&tg_pt_gp_mem->tg_pt_gp_mem_lock); + /* + * Process ALUA_ACCESS_STATE_ACTIVE_OPTMIZED in a seperate conditional + * statement so the complier knows explictly to check this case first. + * For the Optimized ALUA access state case, we want to process the + * incoming fabric cmd ASAP.. + */ + if (out_alua_state == ALUA_ACCESS_STATE_ACTIVE_OPTMIZED) + return 0; + + switch (out_alua_state) { + case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED: + return core_alua_state_nonoptimized(cmd, cdb, + nonop_delay_msecs, alua_ascq); + case ALUA_ACCESS_STATE_STANDBY: + return core_alua_state_standby(cmd, cdb, alua_ascq); + case ALUA_ACCESS_STATE_UNAVAILABLE: + return core_alua_state_unavailable(cmd, cdb, alua_ascq); + case ALUA_ACCESS_STATE_TRANSITION: + return core_alua_state_transition(cmd, cdb, alua_ascq); + /* + * OFFLINE is a secondary ALUA target port group access state, that is + * handled above with struct se_port->sep_tg_pt_secondary_offline=1 + */ + case ALUA_ACCESS_STATE_OFFLINE: + default: + printk(KERN_ERR "Unknown ALUA access state: 0x%02x\n", + out_alua_state); + return -1; + } + + return 0; +} + +/* + * Check implict and explict ALUA state change request. + */ +static int core_alua_check_transition(int state, int *primary) +{ + switch (state) { + case ALUA_ACCESS_STATE_ACTIVE_OPTMIZED: + case ALUA_ACCESS_STATE_ACTIVE_NON_OPTIMIZED: + case ALUA_ACCESS_STATE_STANDBY: + case ALUA_ACCESS_STATE_UNAVAILABLE: + /* + * OPTIMIZED, NON-OPTIMIZED, STANDBY and UNAVAILABLE are + * defined as primary target port asymmetric access states. + */ + *primary = 1; + break; + case ALUA_ACCESS_STATE_OFFLINE: + /* + * OFFLINE state is defined as a secondary target port + * asymmetric access state. + */ + *primary = 0; + break; + default: + printk(KERN_ERR "Unknown ALUA access state: 0x%02x\n", state); + retur |