/*-
* Copyright (c) 2003-2009 Silicon Graphics International Corp.
* Copyright (c) 2012 The FreeBSD Foundation
* Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
* All rights reserved.
*
* Portions of this software were developed by Edward Tomasz Napierala
* under sponsorship from the FreeBSD Foundation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer,
* without modification.
* 2. Redistributions in binary form must reproduce at minimum a disclaimer
* substantially similar to the "NO WARRANTY" disclaimer below
* ("Disclaimer") and any redistribution must be conditioned upon
* including a substantially similar Disclaimer requirement for further
* binary redistribution.
*
* NO WARRANTY
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* $Id$
*/
/*
* CAM Target Layer, a SCSI device emulation subsystem.
*
* Author: Ken Merry <ken@FreeBSD.org>
*/
#define _CTL_C
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/ctype.h>
#include <sys/kernel.h>
#include <sys/types.h>
#include <sys/kthread.h>
#include <sys/bio.h>
#include <sys/fcntl.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/malloc.h>
#include <sys/conf.h>
#include <sys/ioccom.h>
#include <sys/queue.h>
#include <sys/sbuf.h>
#include <sys/smp.h>
#include <sys/endian.h>
#include <sys/sysctl.h>
#include <vm/uma.h>
#include <cam/cam.h>
#include <cam/scsi/scsi_all.h>
#include <cam/scsi/scsi_da.h>
#include <cam/ctl/ctl_io.h>
#include <cam/ctl/ctl.h>
#include <cam/ctl/ctl_frontend.h>
#include <cam/ctl/ctl_util.h>
#include <cam/ctl/ctl_backend.h>
#include <cam/ctl/ctl_ioctl.h>
#include <cam/ctl/ctl_ha.h>
#include <cam/ctl/ctl_private.h>
#include <cam/ctl/ctl_debug.h>
#include <cam/ctl/ctl_scsi_all.h>
#include <cam/ctl/ctl_error.h>
struct ctl_softc *control_softc = NULL;
/*
* Template mode pages.
*/
/*
* Note that these are default values only. The actual values will be
* filled in when the user does a mode sense.
*/
const static struct copan_debugconf_subpage debugconf_page_default = {
DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
DBGCNF_SUBPAGE_CODE, /* subpage */
{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
(sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
DBGCNF_VERSION, /* page_version */
{CTL_TIME_IO_DEFAULT_SECS>>8,
CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
};
const static struct copan_debugconf_subpage debugconf_page_changeable = {
DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
DBGCNF_SUBPAGE_CODE, /* subpage */
{(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
(sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
0, /* page_version */
{0xff,0xff}, /* ctl_time_io_secs */
};
const static struct scsi_da_rw_recovery_page rw_er_page_default = {
/*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
/*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
/*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
/*read_retry_count*/0,
/*correction_span*/0,
/*head_offset_count*/0,
/*data_strobe_offset_cnt*/0,
/*byte8*/SMS_RWER_LBPERE,
/*write_retry_count*/0,
/*reserved2*/0,
/*recovery_time_limit*/{0, 0},
};
const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
/*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
/*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
/*byte3*/0,
/*read_retry_count*/0,
/*correction_span*/0,
/*head_offset_count*/0,
/*data_strobe_offset_cnt*/0,
/*byte8*/0,
/*write_retry_count*/0,
/*reserved2*/0,
/*recovery_time_limit*/{0, 0},
};
const static struct scsi_format_page format_page_default = {
/*page_code*/SMS_FORMAT_DEVICE_PAGE,
/*page_length*/sizeof(struct scsi_format_page) - 2,
/*tracks_per_zone*/ {0, 0},
/*alt_sectors_per_zone*/ {0, 0},
/*alt_tracks_per_zone*/ {0, 0},
/*alt_tracks_per_lun*/ {0, 0},
/*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
/*bytes_per_sector*/ {0, 0},
/*interleave*/ {0, 0},
/*track_skew*/ {0, 0},
/*cylinder_skew*/ {0, 0},
/*flags*/ SFP_HSEC,
/*reserved*/ {0, 0, 0}
};
const static struct scsi_format_page format_page_changeable = {
/*page_code*/SMS_FORMAT_DEVICE_PAGE,
/*page_length*/sizeof(struct scsi_format_page) - 2,
/*tracks_per_zone*/ {0, 0},
/*alt_sectors_per_zone*/ {0, 0},
/*alt_tracks_per_zone*/ {0, 0},
/*alt_tracks_per_lun*/ {0, 0},
/*sectors_per_track*/ {0, 0},
/*bytes_per_sector*/ {0, 0},
/*interleave*/ {0, 0},
/*track_skew*/ {0, 0},
/*cylinder_skew*/ {0, 0},
/*flags*/ 0,
/*reserved*/ {0, 0, 0}
};
const static struct scsi_rigid_disk_page rigid_disk_page_default = {
/*page_code*/SMS_RIGID_DISK_PAGE,
/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
/*cylinders*/ {0, 0, 0},
/*heads*/ CTL_DEFAULT_HEADS,
/*start_write_precomp*/ {0, 0, 0},
/*start_reduced_current*/ {0, 0, 0},
/*step_rate*/ {0, 0},
/*landing_zone_cylinder*/ {0, 0, 0},
/*rpl*/ SRDP_RPL_DISABLED,
/*rotational_offset*/ 0,
/*reserved1*/ 0,
/*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
CTL_DEFAULT_ROTATION_RATE & 0xff},
/*reserved2*/ {0, 0}
};
const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
/*page_code*/SMS_RIGID_DISK_PAGE,
/*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
/*cylinders*/ {0, 0, 0},
/*heads*/ 0,
/*start_write_precomp*/ {0, 0, 0},
/*start_reduced_current*/ {0, 0, 0},
/*step_rate*/ {0, 0},
/*landing_zone_cylinder*/ {0, 0, 0},
/*rpl*/ 0,
/*rotational_offset*/ 0,
/*reserved1*/ 0,
/*rotation_rate*/ {0, 0},
/*reserved2*/ {0, 0}
};
const static struct scsi_caching_page caching_page_default = {
/*page_code*/SMS_CACHING_PAGE,
/*page_length*/sizeof(struct scsi_caching_page) - 2,
/*flags1*/ SCP_DISC | SCP_WCE,
/*ret_priority*/ 0,
/*disable_pf_transfer_len*/ {0xff, 0xff},
/*min_prefetch*/ {0, 0},
/*max_prefetch*/ {0xff, 0xff},
/*max_pf_ceiling*/ {0xff, 0xff},
/*flags2*/ 0,
/*cache_segments*/ 0,
/*cache_seg_size*/ {0, 0},
/*reserved*/ 0,
/*non_cache_seg_size*/ {0, 0, 0}
};
const static struct scsi_caching_page caching_page_changeable = {
/*page_code*/SMS_CACHING_PAGE,
/*page_length*/sizeof(struct scsi_caching_page) - 2,
/*flags1*/ SCP_WCE | SCP_RCD,
/*ret_priority*/ 0,
/*disable_pf_transfer_len*/ {0, 0},
/*min_prefetch*/ {0, 0},
/*max_prefetch*/ {0, 0},
/*max_pf_ceiling*/ {0, 0},
/*flags2*/ 0,
/*cache_segments*/ 0,
/*cache_seg_size*/ {0, 0},
/*reserved*/ 0,
/*non_cache_seg_size*/ {0, 0, 0}
};
const static struct scsi_control_page control_page_default = {
/*page_code*/SMS_CONTROL_MODE_PAGE,
/*page_length*/sizeof(struct scsi_control_page) - 2,
/*rlec*/0,
/*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
/*eca_and_aen*/0,
/*flags4*/SCP_TAS,
/*aen_holdoff_period*/{0, 0},
/*busy_timeout_period*/{0, 0},
/*extended_selftest_completion_time*/{0, 0}
};
const static struct scsi_control_page control_page_changeable = {
/*page_code*/SMS_CONTROL_MODE_PAGE,
/*page_length*/sizeof(struct scsi_control_page) - 2,
/*rlec*/SCP_DSENSE,
/*queue_flags*/SCP_QUEUE_ALG_MASK,
/*eca_and_aen*/SCP_SWP,
/*flags4*/0,
/*aen_holdoff_period*/{0, 0},
/*busy_timeout_period*/{0, 0},
/*extended_selftest_completion_time*/{0, 0}
};
#define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
const static struct scsi_control_ext_page control_ext_page_default = {
/*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
/*subpage_code*/0x01,
/*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
/*flags*/0,
/*prio*/0,
/*max_sense*/0
};
const static struct scsi_control_ext_page control_ext_page_changeable = {
/*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
/*subpage_code*/0x01,
/*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
/*flags*/0,
/*prio*/0,
/*max_sense*/0
};
const static struct scsi_info_exceptions_page ie_page_default = {
/*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
/*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
/*info_flags*/SIEP_FLAGS_DEXCPT,
/*mrie*/0,
/*interval_timer*/{0, 0, 0, 0},
/*report_count*/{0, 0, 0, 0}
};
const static struct scsi_info_exceptions_page ie_page_changeable = {
/*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
/*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
/*info_flags*/0,
/*mrie*/0,
/*interval_timer*/{0, 0, 0, 0},
/*report_count*/{0, 0, 0, 0}
};
#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
/*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
/*subpage_code*/0x02,
/*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
/*flags*/0,
/*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/*descr*/{}},
{{/*flags*/0,
/*resource*/0x01,
/*reserved*/{0, 0},
/*count*/{0, 0, 0, 0}},
{/*flags*/0,
/*resource*/0x02,
/*reserved*/{0, 0},
/*count*/{0, 0, 0, 0}},
{/*flags*/0,
/*resource*/0xf1,
/*reserved*/{0, 0},
/*count*/{0, 0, 0, 0}},
{/*flags*/0,
/*resource*/0xf2,
/*reserved*/{0, 0},
/*count*/{0, 0, 0, 0}}
}
};
const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
/*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
/*subpage_code*/0x02,
/*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
/*flags*/0,
/*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
/*descr*/{}},
{{/*flags*/0,
/*resource*/0,
/*reserved*/{0, 0},
/*count*/{0, 0, 0, 0}},
{/*flags*/0,
/*resource*/0,
/*reserved*/{0, 0},
/*count*/{0, 0, 0, 0}},
{/*flags*/0,
/*resource*/0,
/*reserved*/{0, 0},
/*count*/{0, 0, 0, 0}},
{/*flags*/0,
/*resource*/0,
/*reserved*/{0, 0},
/*count*/{0, 0, 0, 0}}
}
};
SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
static int worker_threads = -1;
SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
&worker_threads, 1, "Number of worker threads");
static int ctl_debug = CTL_DEBUG_NONE;
SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
&ctl_debug, 0, "Enabled debug flags");
/*
* Supported pages (0x00), Serial number (0x80), Device ID (0x83),
* Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
* SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
* Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
*/
#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
int param);
static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
static int ctl_init(void);
void ctl_shutdown(void);
static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
struct ctl_ooa *ooa_hdr,
struct ctl_ooa_entry *kern_entries);
static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
struct thread *td);
static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
struct ctl_be_lun *be_lun);
static int ctl_free_lun(struct ctl_lun *lun);
static void ctl_create_lun(struct ctl_be_lun *be_lun);
static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
static int ctl_do_mode_select(union ctl_io *io);
static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
uint64_t res_key, uint64_t sa_res_key,
uint8_t type, uint32_t residx,
struct ctl_scsiio *ctsio,
struct scsi_per_res_out *cdb,
struct scsi_per_res_out_parms* param);
static void ctl_pro_preempt_other(struct ctl_lun *lun,
union ctl_ha_msg *msg);
static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
int alloc_len);
static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
int alloc_len);
static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
bool seq);
static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
union ctl_io *pending_io, union ctl_io *ooa_io);
static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
union ctl_io *starting_io);
static int ctl_check_blocked(struct ctl_lun *lun);
static int ctl_scsiio_lun_check(struct ctl_lun *lun,
const struct ctl_cmd_entry *entry,
struct ctl_scsiio *ctsio);
static void ctl_failover_lun(struct ctl_lun *lun);
static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
struct ctl_scsiio *ctsio);
static int ctl_scsiio(struct ctl_scsiio *ctsio);
static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
ctl_ua_type ua_type);
static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
ctl_ua_type ua_type);
static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
static int ctl_abort_task(union ctl_io *io);
static int ctl_abort_task_set(union ctl_io *io);
static int ctl_query_task(union ctl_io *io, int task_set);
static int ctl_i_t_nexus_reset(union ctl_io *io);
static int ctl_query_async_event(union ctl_io *io);
static void ctl_run_task(union ctl_io *io);
#ifdef CTL_IO_DELAY
static void ctl_datamove_timer_wakeup(void *arg);
static void ctl_done_timer_wakeup(void *arg);
#endif /* CTL_IO_DELAY */
static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
static void ctl_datamove_remote_write(union ctl_io *io);
static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
ctl_ha_dt_cb callback);
static void ctl_datamove_remote_read(union ctl_io *io);
static void ctl_datamove_remote(union ctl_io *io);
static void ctl_process_done(union ctl_io *io);
static void ctl_lun_thread(void *arg);
static void ctl_thresh_thread(void *arg);
static void ctl_work_thread(void *arg);
static void ctl_enqueue_incoming(union ctl_io *io);
static void ctl_enqueue_rtr(union ctl_io *io);
static void ctl_enqueue_done(union ctl_io *io);
static void ctl_enqueue_isc(union ctl_io *io);
static const struct ctl_cmd_entry *
ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
static const struct ctl_cmd_entry *
ctl_validate_command(struct ctl_scsiio *ctsio);
static int ctl_cmd_applicable(uint8_t lun_type,
const struct ctl_cmd_entry *entry);
static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
/*
* Load the serialization table. This isn't very pretty, but is probably
* the easiest way to do it.
*/
#include "ctl_ser_table.c"
/*
* We only need to define open, close and ioctl routines for this driver.
*/
static struct cdevsw ctl_cdevsw = {
.d_version = D_VERSION,
.d_flags = 0,
.d_open = ctl_open,
.d_close = ctl_close,
.d_ioctl = ctl_ioctl,
.d_name = "ctl",
};
MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
static moduledata_t ctl_moduledata = {
"ctl",
ctl_module_event_handler,
NULL
};
DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
MODULE_VERSION(ctl, 1);
static struct ctl_frontend ha_frontend =
{
.name = "ha",
};
static void
ctl_ha_datamove(union ctl_io *io)
{
struct ctl_lun *lun;
struct ctl_sg_entry *sgl;
union ctl_ha_msg msg;
uint32_t sg_entries_sent;
int do_sg_copy, i, j;
lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
memset(&msg.dt, 0, sizeof(msg.dt));
msg.hdr.msg_type = CTL_MSG_DATAMOVE;
msg.hdr.original_sc = io->io_hdr.original_sc;
msg.hdr.serializing_sc = io;
msg.hdr.nexus = io->io_hdr.nexus;
msg.hdr.status = io->io_hdr.status;
msg.dt.flags = io->io_hdr.flags;
/*
* We convert everything into a S/G list here. We can't
* pass by reference, only by value between controllers.
* So we can't pass a pointer to the S/G list, only as many
* S/G entries as we can fit in here. If it's possible for
* us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
* then we need to break this up into multiple transfers.
*/
if (io->scsiio.kern_sg_entries == 0) {
msg.dt.kern_sg_entries = 1;
#if 0
if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
} else {
/* XXX KDM use busdma here! */
msg.dt.sg_list[0].addr =
(void *)vtophys(io->scsiio.kern_data_ptr);
}
#else
KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
("HA does not support BUS_ADDR"));
msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
#endif
msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
do_sg_copy = 0;
} else {
msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
do_sg_copy = 1;
}
msg.dt.kern_data_len = io->scsiio.kern_data_len;
msg.dt.kern_total_len = io->scsiio.kern_total_len;
msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
msg.dt.sg_sequence = 0;
/*
* Loop until we've sent all of the S/G entries. On the
* other end, we'll recompose these S/G entries into one
* contiguous list before processing.
*/
for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
msg.dt.sg_sequence++) {
msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
sizeof(msg.dt.sg_list[0])),
msg.dt.kern_sg_entries - sg_entries_sent);
if (do_sg_copy != 0) {
sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
for (i = sg_entries_sent, j = 0;
i < msg.dt.cur_sg_entries; i++, j++) {
#if 0
if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
msg.dt.sg_list[j].addr = sgl[i].addr;
} else {
/* XXX KDM use busdma here! */
msg.dt.sg_list[j].addr =
(void *)vtophys(sgl[i].addr);
}
#else
KASSERT((io->io_hdr.flags &
CTL_FLAG_BUS_ADDR) == 0,
("HA does not support BUS_ADDR"));
msg.dt.sg_list[j].addr = sgl[i].addr;
#endif
msg.dt.sg_list[j].len = sgl[i].len;
}
}
sg_entries_sent += msg.dt.cur_sg_entries;
msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
io->io_hdr.port_status = 31341;
io->scsiio.be_move_done(io);
return;
}
msg.dt.sent_sg_entries = sg_entries_sent;
}
/*
* Officially handover the request from us to peer.
* If failover has just happened, then we must return error.
* If failover happen just after, then it is not our problem.
*/
if (lun)
mtx_lock(&lun->lun_lock);
if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
if (lun)
mtx_unlock(&lun->lun_lock);
io->io_hdr.port_status = 31342;
io->scsiio.be_move_done(io);
return;
}
io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
if (lun)
mtx_unlock(&lun->lun_lock);
}
static void
ctl_ha_done(union ctl_io *io)
{
union ctl_ha_msg msg;
if (io->io_hdr.io_type == CTL_IO_SCSI) {
memset(&msg, 0, sizeof(msg));
msg.hdr.msg_type = CTL_MSG_FINISH_IO;
msg.hdr.original_sc = io->io_hdr.original_sc;
msg.hdr.nexus = io->io_hdr.nexus;
msg.hdr.status = io->io_hdr.status;
msg.scsi.scsi_status = io->scsiio.scsi_status;
msg.scsi.tag_num = io->scsiio.tag_num;
msg.scsi.tag_type = io->scsiio.tag_type;
msg.scsi.sense_len = io->scsiio.sense_len;
msg.scsi.sense_residual = io->scsiio.sense_residual;
msg.scsi.residual = io->scsiio.residual;
memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
io->scsiio.sense_len);
ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
msg.scsi.sense_len, M_WAITOK);
}
ctl_free_io(io);
}
static void
ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
union ctl_ha_msg *msg_info)
{
struct ctl_scsiio *ctsio;
if (msg_info->hdr.original_sc == NULL) {
printf("%s: original_sc == NULL!\n", __func__);
/* XXX KDM now what? */
return;
}
ctsio = &msg_info->hdr.original_sc->scsiio;
ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
ctsio->io_hdr.status = msg_info->hdr.status;
ctsio->scsi_status = msg_info->scsi.scsi_status;
ctsio->sense_len = msg_info->scsi.sense_len;
ctsio->sense_residual = msg_info->scsi.sense_residual;
ctsio->residual = msg_info->scsi.residual;
memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
msg_info->scsi.sense_len);
ctl_enqueue_isc((union ctl_io *)ctsio);
}
static void
ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
union ctl_ha_msg *msg_info)
{
struct ctl_scsiio *ctsio;
if (msg_info->hdr.serializing_sc == NULL) {
printf("%s: serializing_sc == NULL!\n", __func__);
/* XXX KDM now what? */
return;
}
ctsio = &msg_info->hdr.serializing_sc->scsiio;
ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
ctl_enqueue_isc((union ctl_io *)ctsio);
}
void
ctl_isc_announce_lun(struct ctl_lun *lun)
{
struct ctl_softc *softc = lun->ctl_softc;
union ctl_ha_msg *msg;
struct ctl_ha_msg_lun_pr_key pr_key;
int i, k;
if (softc->ha_link != CTL_HA_LINK_ONLINE)
return;
mtx_lock(&lun->lun_lock);
i = sizeof(msg->lun);
if (lun->lun_devid)
i += lun->lun_devid->len;
i += sizeof(pr_key) * lun->pr_key_count;
alloc:
mtx_unlock(&lun->lun_lock);
msg = malloc(i, M_CTL, M_WAITOK);
mtx_lock(&lun->lun_lock);
k = sizeof(msg->lun);
if (lun->lun_devid)
k += lun->lun_devid->len;
k += sizeof(pr_key) * lun->pr_key_count;
if (i < k) {
free(msg, M_CTL);
i = k;
goto alloc;
}
bzero(&msg->lun, sizeof(msg->lun));
msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
msg->hdr.nexus.targ_lun = lun->lun;
msg->hdr.nexus.targ_mapped_lun = lun->lun;
msg->lun.flags = lun->flags;
msg->lun.pr_generation = lun->PRGeneration;
msg->lun.pr_res_idx = lun->pr_res_idx;
msg->lun.pr_res_type = lun->res_type;
msg->lun.pr_key_count = lun->pr_key_count;
i = 0;
if (lun->lun_devid) {
msg->lun.lun_devid_len = lun->lun_devid->len;
memcpy(&msg->lun.data[i], lun->lun_devid->data,
msg->lun.lun_devid_len);
i += msg->lun.lun_devid_len;
}
for (k = 0; k < CTL_MAX_INITIATORS; k++) {
if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
continue;
pr_key.pr_iid = k;
memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
i += sizeof(pr_key);
}
mtx_unlock(&lun->lun_lock);
ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
M_WAITOK);
free(msg, M_CTL);
if (lun->flags & CTL_LUN_PRIMARY_SC) {
for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
ctl_isc_announce_mode(lun, -1,
lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
lun->mode_pages.index[i].subpage);
}
}
}
void
ctl_isc_announce_port(struct ctl_port *port)
{
struct ctl_softc *softc = port->ctl_softc;
union ctl_ha_msg *msg;
int i;
if (port->targ_port < softc->port_min ||
port->targ_port >= softc->port_max ||
softc->ha_link != CTL_HA_LINK_ONLINE)
return;
i = sizeof(msg->port) + strlen(port->port_name) + 1;
if (port->lun_map)
i += sizeof(uint32_t) * CTL_MAX_LUNS;
if (port->port_devid)
i += port->port_devid->len;
if (port->target_devid)
i += port->target_devid->len;
if (port->init_devid)
i += port->init_devid->len;
msg = malloc(i, M_CTL, M_WAITOK);
bzero(&msg->port, sizeof(msg->port));
msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
msg->hdr.nexus.targ_port = port->targ_port;
msg->port.port_type = port->port_type;
msg->port.physical_port = port->physical_port;
msg->port.virtual_port = port->virtual_port;
msg->port.status = port->status;
i = 0;
msg->port.name_len = sprintf(&msg->port.data[i],
"%d:%s", softc->ha_id, port->port_name) + 1;
i += msg->port.name_len;
if (port->lun_map) {
msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
memcpy(&msg->port.data[i], port->lun_map,
msg->port.lun_map_len);
i += msg->port.lun_map_len;
}
if (port->port_devid) {
msg->port.port_devid_len = port->port_devid->len;
memcpy(&msg->port.data[i], port->port_devid->data,
msg->port.port_devid_len);
i += msg->port.port_devid_len;
}
if (port->target_devid) {
msg->port.target_devid_len = port->target_devid->len;
memcpy(&msg->port.data[i], port->target_devid->data,
msg->port.target_devid_len);
i += msg->port.target_devid_len;
}
if (port->init_devid) {
msg->port.init_devid_len = port->init_devid->len;
memcpy(&msg->port.data[i], port->init_devid->data,
msg->port.init_devid_len);
i += msg->port.init_devid_len;
}
ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
M_WAITOK);
free(msg, M_CTL);
}
void
ctl_isc_announce_iid(struct ctl_port *port, int iid)
{
struct ctl_softc *softc = port->ctl_softc;
union ctl_ha_msg *msg;
int i, l;
if (port->targ_port < softc->port_min ||
port->targ_port >= softc->port_max ||
softc->ha_link != CTL_HA_LINK_ONLINE)
return;
mtx_lock(&softc->ctl_lock);
i = sizeof(msg->iid);
l = 0;
if (port->wwpn_iid[iid].name)
l = strlen(port->wwpn_iid[iid].name) + 1;
i += l;
msg = malloc(i, M_CTL, M_NOWAIT);
if (msg == NULL) {
mtx_unlock(&softc->ctl_lock);
return;
}
bzero(&msg->iid, sizeof(msg->iid));
msg->hdr.msg_type = CTL_MSG_IID_SYNC;
msg->hdr.nexus.targ_port = port->targ_port;
msg->hdr.nexus.initid = iid;
msg->iid.in_use = port->wwpn_iid[iid].in_use;
msg->iid.name_len = l;
msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
if (port->wwpn_iid[iid].name)
strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
mtx_unlock(&softc->ctl_lock);
ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
free(msg, M_CTL);
}
void
ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
uint8_t page, uint8_t subpage)
{
struct ctl_softc *softc = lun->ctl_softc;
union ctl_ha_msg msg;
int i;
if (softc->ha_link != CTL_HA_LINK_ONLINE)
return;
for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
page && lun->mode_pages.index[i].subpage == subpage)
break;
}
if (i == CTL_NUM_MODE_PAGES)
return;
bzero(&msg.mode, sizeof(msg.mode));
msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
msg.hdr.nexus.targ_lun = lun->lun;
msg.hdr.nexus.targ_mapped_lun = lun->lun;
msg.mode.page_code = page;
msg.mode.subpage = subpage;
msg.mode.page_len = lun->mode_pages.index[i].page_len;
memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
msg.mode.page_len);
ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
M_WAITOK);
}
static void
ctl_isc_ha_link_up(struct ctl_softc *softc)
{
struct ctl_port *port;
struct ctl_lun *lun;
union ctl_ha_msg msg;
int i;
/* Announce this node parameters to peer for validation. */
msg.login.msg_type = CTL_MSG_LOGIN;
msg.login.version = CTL_HA_VERSION;
msg.login.ha_mode = softc->ha_mode;
msg.login.ha_id = softc->ha_id;
msg.login.max_luns = CTL_MAX_LUNS;
msg.login.max_ports = CTL_MAX_PORTS;
msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
M_WAITOK);
STAILQ_FOREACH(port, &softc->port_list, links) {
ctl_isc_announce_port(port);
for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
if (port->wwpn_iid[i].in_use)
ctl_isc_announce_iid(port, i);
}
}
STAILQ_FOREACH(lun, &softc->lun_list, links)
ctl_isc_announce_lun(lun);
}
static void
ctl_isc_ha_link_down(struct ctl_softc *softc)
{
struct ctl_port *port;
struct ctl_lun *lun;
union ctl_io *io;
int i;
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(lun, &softc->lun_list, links) {
mtx_lock(&lun->lun_lock);
if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
}
mtx_unlock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
io = ctl_alloc_io(softc->othersc_pool);
mtx_lock(&softc->ctl_lock);
ctl_zero_io(io);
io->io_hdr.msg_type = CTL_MSG_FAILOVER;
io->io_hdr.nexus.targ_mapped_lun = lun->lun;
ctl_enqueue_isc(io);
}
STAILQ_FOREACH(port, &softc->port_list, links) {
if (port->targ_port >= softc->port_min &&
port->targ_port < softc->port_max)
continue;
port->status &= ~CTL_PORT_STATUS_ONLINE;
for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
port->wwpn_iid[i].in_use = 0;
free(port->wwpn_iid[i].name, M_CTL);
port->wwpn_iid[i].name = NULL;
}
}
mtx_unlock(&softc->ctl_lock);
}
static void
ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
{
struct ctl_lun *lun;
uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
mtx_lock(&softc->ctl_lock);
if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
(lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
mtx_lock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
msg->ua.ua_set)
memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
if (msg->ua.ua_all) {
if (msg->ua.ua_set)
ctl_est_ua_all(lun, iid, msg->ua.ua_type);
else
ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
} else {
if (msg->ua.ua_set)
ctl_est_ua(lun, iid, msg->ua.ua_type);
else
ctl_clr_ua(lun, iid, msg->ua.ua_type);
}
mtx_unlock(&lun->lun_lock);
} else
mtx_unlock(&softc->ctl_lock);
}
static void
ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
{
struct ctl_lun *lun;
struct ctl_ha_msg_lun_pr_key pr_key;
int i, k;
ctl_lun_flags oflags;
uint32_t targ_lun;
targ_lun = msg->hdr.nexus.targ_mapped_lun;
mtx_lock(&softc->ctl_lock);
if ((targ_lun >= CTL_MAX_LUNS) ||
((lun = softc->ctl_luns[targ_lun]) == NULL)) {
mtx_unlock(&softc->ctl_lock);
return;
}
mtx_lock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
if (lun->flags & CTL_LUN_DISABLED) {
mtx_unlock(&lun->lun_lock);
return;
}
i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
if (msg->lun.lun_devid_len != i || (i > 0 &&
memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
mtx_unlock(&lun->lun_lock);
printf("%s: Received conflicting HA LUN %d\n",
__func__, msg->hdr.nexus.targ_lun);
return;
} else {
/* Record whether peer is primary. */
oflags = lun->flags;
if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
(msg->lun.flags & CTL_LUN_DISABLED) == 0)
lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
else
lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
if (oflags != lun->flags)
ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
/* If peer is primary and we are not -- use data */
if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
(lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
lun->PRGeneration = msg->lun.pr_generation;
lun->pr_res_idx = msg->lun.pr_res_idx;
lun->res_type = msg->lun.pr_res_type;
lun->pr_key_count = msg->lun.pr_key_count;
for (k = 0; k < CTL_MAX_INITIATORS; k++)
ctl_clr_prkey(lun, k);
for (k = 0; k < msg->lun.pr_key_count; k++) {
memcpy(&pr_key, &msg->lun.data[i],
sizeof(pr_key));
ctl_alloc_prkey(lun, pr_key.pr_iid);
ctl_set_prkey(lun, pr_key.pr_iid,
pr_key.pr_key);
i += sizeof(pr_key);
}
}
mtx_unlock(&lun->lun_lock);
CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
__func__, msg->hdr.nexus.targ_lun,
(msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
"primary" : "secondary"));
/* If we are primary but peer doesn't know -- notify */
if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
(msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
ctl_isc_announce_lun(lun);
}
}
static void
ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
{
struct ctl_port *port;
struct ctl_lun *lun;
int i, new;
port = softc->ctl_ports[msg->hdr.nexus.targ_port];
if (port == NULL) {
CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
msg->hdr.nexus.targ_port));
new = 1;
port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
port->frontend = &ha_frontend;
port->targ_port = msg->hdr.nexus.targ_port;
port->fe_datamove = ctl_ha_datamove;
port->fe_done = ctl_ha_done;
} else if (port->frontend == &ha_frontend) {
CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
msg->hdr.nexus.targ_port));
new = 0;
} else {
printf("%s: Received conflicting HA port %d\n",
__func__, msg->hdr.nexus.targ_port);
return;
}
port->port_type = msg->port.port_type;
port->physical_port = msg->port.physical_port;
port->virtual_port = msg->port.virtual_port;
port->status = msg->port.status;
i = 0;
free(port->port_name, M_CTL);
port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
M_CTL);
i += msg->port.name_len;
if (msg->port.lun_map_len != 0) {
if (port->lun_map == NULL)
port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
M_CTL, M_WAITOK);
memcpy(port->lun_map, &msg->port.data[i],
sizeof(uint32_t) * CTL_MAX_LUNS);
i += msg->port.lun_map_len;
} else {
free(port->lun_map, M_CTL);
port->lun_map = NULL;
}
if (msg->port.port_devid_len != 0) {
if (port->port_devid == NULL ||
port->port_devid->len != msg->port.port_devid_len) {
free(port->port_devid, M_CTL);
port->port_devid = malloc(sizeof(struct ctl_devid) +
msg->port.port_devid_len, M_CTL, M_WAITOK);
}
memcpy(port->port_devid->data, &msg->port.data[i],
msg->port.port_devid_len);
port->port_devid->len = msg->port.port_devid_len;
i += msg->port.port_devid_len;
} else {
free(port->port_devid, M_CTL);
port->port_devid = NULL;
}
if (msg->port.target_devid_len != 0) {
if (port->target_devid == NULL ||
port->target_devid->len != msg->port.target_devid_len) {
free(port->target_devid, M_CTL);
port->target_devid = malloc(sizeof(struct ctl_devid) +
msg->port.target_devid_len, M_CTL, M_WAITOK);
}
memcpy(port->target_devid->data, &msg->port.data[i],
msg->port.target_devid_len);
port->target_devid->len = msg->port.target_devid_len;
i += msg->port.target_devid_len;
} else {
free(port->target_devid, M_CTL);
port->target_devid = NULL;
}
if (msg->port.init_devid_len != 0) {
if (port->init_devid == NULL ||
port->init_devid->len != msg->port.init_devid_len) {
free(port->init_devid, M_CTL);
port->init_devid = malloc(sizeof(struct ctl_devid) +
msg->port.init_devid_len, M_CTL, M_WAITOK);
}
memcpy(port->init_devid->data, &msg->port.data[i],
msg->port.init_devid_len);
port->init_devid->len = msg->port.init_devid_len;
i += msg->port.init_devid_len;
} else {
free(port->init_devid, M_CTL);
port->init_devid = NULL;
}
if (new) {
if (ctl_port_register(port) != 0) {
printf("%s: ctl_port_register() failed with error\n",
__func__);
}
}
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(lun, &softc->lun_list, links) {
if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
continue;
mtx_lock(&lun->lun_lock);
ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
mtx_unlock(&lun->lun_lock);
}
mtx_unlock(&softc->ctl_lock);
}
static void
ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
{
struct ctl_port *port;
int iid;
port = softc->ctl_ports[msg->hdr.nexus.targ_port];
if (port == NULL) {
printf("%s: Received IID for unknown port %d\n",
__func__, msg->hdr.nexus.targ_port);
return;
}
iid = msg->hdr.nexus.initid;
port->wwpn_iid[iid].in_use = msg->iid.in_use;
port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
free(port->wwpn_iid[iid].name, M_CTL);
if (msg->iid.name_len) {
port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
msg->iid.name_len, M_CTL);
} else
port->wwpn_iid[iid].name = NULL;
}
static void
ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
{
if (msg->login.version != CTL_HA_VERSION) {
printf("CTL HA peers have different versions %d != %d\n",
msg->login.version, CTL_HA_VERSION);
ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
return;
}
if (msg->login.ha_mode != softc->ha_mode) {
printf("CTL HA peers have different ha_mode %d != %d\n",
msg->login.ha_mode, softc->ha_mode);
ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
return;
}
if (msg->login.ha_id == softc->ha_id) {
printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
return;
}
if (msg->login.max_luns != CTL_MAX_LUNS ||
msg->login.max_ports != CTL_MAX_PORTS ||
msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
printf("CTL HA peers have different limits\n");
ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
return;
}
}
static void
ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
{
struct ctl_lun *lun;
int i;
uint32_t initidx, targ_lun;
targ_lun = msg->hdr.nexus.targ_mapped_lun;
mtx_lock(&softc->ctl_lock);
if ((targ_lun >= CTL_MAX_LUNS) ||
((lun = softc->ctl_luns[targ_lun]) == NULL)) {
mtx_unlock(&softc->ctl_lock);
return;
}
mtx_lock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
if (lun->flags & CTL_LUN_DISABLED) {
mtx_unlock(&lun->lun_lock);
return;
}
for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
msg->mode.page_code &&
lun->mode_pages.index[i].subpage == msg->mode.subpage)
break;
}
if (i == CTL_NUM_MODE_PAGES) {
mtx_unlock(&lun->lun_lock);
return;
}
memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
lun->mode_pages.index[i].page_len);
initidx = ctl_get_initindex(&msg->hdr.nexus);
if (initidx != -1)
ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
mtx_unlock(&lun->lun_lock);
}
/*
* ISC (Inter Shelf Communication) event handler. Events from the HA
* subsystem come in here.
*/
static void
ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
{
struct ctl_softc *softc = control_softc;
union ctl_io *io;
struct ctl_prio *presio;
ctl_ha_status isc_status;
CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
if (event == CTL_HA_EVT_MSG_RECV) {
union ctl_ha_msg *msg, msgbuf;
if (param > sizeof(msgbuf))
msg = malloc(param, M_CTL, M_WAITOK);
else
msg = &msgbuf;
isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
M_WAITOK);
if (isc_status != CTL_HA_STATUS_SUCCESS) {
printf("%s: Error receiving message: %d\n",
__func__, isc_status);
if (msg != &msgbuf)
free(msg, M_CTL);
return;
}
CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
switch (msg->hdr.msg_type) {
case CTL_MSG_SERIALIZE:
io = ctl_alloc_io(softc->othersc_pool);
ctl_zero_io(io);
// populate ctsio from msg
io->io_hdr.io_type = CTL_IO_SCSI;
io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
io->io_hdr.original_sc = msg->hdr.original_sc;
io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
CTL_FLAG_IO_ACTIVE;
/*
* If we're in serialization-only mode, we don't
* want to go through full done processing. Thus
* the COPY flag.
*
* XXX KDM add another flag that is more specific.
*/
if (softc->ha_mode != CTL_HA_MODE_XFER)
io->io_hdr.flags |= CTL_FLAG_INT_COPY;
io->io_hdr.nexus = msg->hdr.nexus;
#if 0
printf("port %u, iid %u, lun %u\n",
io->io_hdr.nexus.targ_port,
io->io_hdr.nexus.initid,
io->io_hdr.nexus.targ_lun);
#endif
io->scsiio.tag_num = msg->scsi.tag_num;
io->scsiio.tag_type = msg->scsi.tag_type;
#ifdef CTL_TIME_IO
io->io_hdr.start_time = time_uptime;
getbinuptime(&io->io_hdr.start_bt);
#endif /* CTL_TIME_IO */
io->scsiio.cdb_len = msg->scsi.cdb_len;
memcpy(io->scsiio.cdb, msg->scsi.cdb,
CTL_MAX_CDBLEN);
if (softc->ha_mode == CTL_HA_MODE_XFER) {
const struct ctl_cmd_entry *entry;
entry = ctl_get_cmd_entry(&io->scsiio, NULL);
io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
io->io_hdr.flags |=
entry->flags & CTL_FLAG_DATA_MASK;
}
ctl_enqueue_isc(io);
break;
/* Performed on the Originating SC, XFER mode only */
case CTL_MSG_DATAMOVE: {
struct ctl_sg_entry *sgl;
int i, j;
io = msg->hdr.original_sc;
if (io == NULL) {
printf("%s: original_sc == NULL!\n", __func__);
/* XXX KDM do something here */
break;
}
io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
/*
* Keep track of this, we need to send it back over
* when the datamove is complete.
*/
io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
if (msg->hdr.status == CTL_SUCCESS)
io->io_hdr.status = msg->hdr.status;
if (msg->dt.sg_sequence == 0) {
#ifdef CTL_TIME_IO
getbinuptime(&io->io_hdr.dma_start_bt);
#endif
i = msg->dt.kern_sg_entries +
msg->dt.kern_data_len /
CTL_HA_DATAMOVE_SEGMENT + 1;
sgl = malloc(sizeof(*sgl) * i, M_CTL,
M_WAITOK | M_ZERO);
io->io_hdr.remote_sglist = sgl;
io->io_hdr.local_sglist =
&sgl[msg->dt.kern_sg_entries];
io->scsiio.kern_data_ptr = (uint8_t *)sgl;
io->scsiio.kern_sg_entries =
msg->dt.kern_sg_entries;
io->scsiio.rem_sg_entries =
msg->dt.kern_sg_entries;
io->scsiio.kern_data_len =
msg->dt.kern_data_len;
io->scsiio.kern_total_len =
msg->dt.kern_total_len;
io->scsiio.kern_data_resid =
msg->dt.kern_data_resid;
io->scsiio.kern_rel_offset =
msg->dt.kern_rel_offset;
io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
io->io_hdr.flags |= msg->dt.flags &
CTL_FLAG_BUS_ADDR;
} else
sgl = (struct ctl_sg_entry *)
io->scsiio.kern_data_ptr;
for (i = msg->dt.sent_sg_entries, j = 0;
i < (msg->dt.sent_sg_entries +
msg->dt.cur_sg_entries); i++, j++) {
sgl[i].addr = msg->dt.sg_list[j].addr;
sgl[i].len = msg->dt.sg_list[j].len;
#if 0
printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
__func__, sgl[i].addr, sgl[i].len, j, i);
#endif
}
/*
* If this is the last piece of the I/O, we've got
* the full S/G list. Queue processing in the thread.
* Otherwise wait for the next piece.
*/
if (msg->dt.sg_last != 0)
ctl_enqueue_isc(io);
break;
}
/* Performed on the Serializing (primary) SC, XFER mode only */
case CTL_MSG_DATAMOVE_DONE: {
if (msg->hdr.serializing_sc == NULL) {
printf("%s: serializing_sc == NULL!\n",
__func__);
/* XXX KDM now what? */
break;
}
/*
* We grab the sense information here in case
* there was a failure, so we can return status
* back to the initiator.
*/
io = msg->hdr.serializing_sc;
io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
io->io_hdr.port_status = msg->scsi.fetd_status;
io->scsiio.residual = msg->scsi.residual;
if (msg->hdr.status != CTL_STATUS_NONE) {
io->io_hdr.status = msg->hdr.status;
io->scsiio.scsi_status = msg->scsi.scsi_status;
io->scsiio.sense_len = msg->scsi.sense_len;
io->scsiio.sense_residual =msg->scsi.sense_residual;
memcpy(&io->scsiio.sense_data,
&msg->scsi.sense_data,
msg->scsi.sense_len);
if (msg->hdr.status == CTL_SUCCESS)
io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
}
ctl_enqueue_isc(io);
break;
}
/* Preformed on Originating SC, SER_ONLY mode */
case CTL_MSG_R2R:
io = msg->hdr.original_sc;
if (io == NULL) {
printf("%s: original_sc == NULL!\n",
__func__);
break;
}
io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
io->io_hdr.msg_type = CTL_MSG_R2R;
io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
ctl_enqueue_isc(io);
break;
/*
* Performed on Serializing(i.e. primary SC) SC in SER_ONLY
* mode.
* Performed on the Originating (i.e. secondary) SC in XFER
* mode
*/
case CTL_MSG_FINISH_IO:
if (softc->ha_mode == CTL_HA_MODE_XFER)
ctl_isc_handler_finish_xfer(softc, msg);
else
ctl_isc_handler_finish_ser_only(softc, msg);
break;
/* Preformed on Originating SC */
case CTL_MSG_BAD_JUJU:
io = msg->hdr.original_sc;
if (io == NULL) {
printf("%s: Bad JUJU!, original_sc is NULL!\n",
__func__);
break;
}
ctl_copy_sense_data(msg, io);
/*
* IO should have already been cleaned up on other
* SC so clear this flag so we won't send a message
* back to finish the IO there.
*/
io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
/* io = msg->hdr.serializing_sc; */
io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
ctl_enqueue_isc(io);
break;
/* Handle resets sent from the other side */
case CTL_MSG_MANAGE_TASKS: {
struct ctl_taskio *taskio;
taskio = (struct ctl_taskio *)ctl_alloc_io(
softc->othersc_pool);
ctl_zero_io((union ctl_io *)taskio);
taskio->io_hdr.io_type = CTL_IO_TASK;
taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
taskio->io_hdr.nexus = msg->hdr.nexus;
taskio->task_action = msg->task.task_action;
taskio->tag_num = msg->task.tag_num;
taskio->tag_type = msg->task.tag_type;
#ifdef CTL_TIME_IO
taskio->io_hdr.start_time = time_uptime;
getbinuptime(&taskio->io_hdr.start_bt);
#endif /* CTL_TIME_IO */
ctl_run_task((union ctl_io *)taskio);
break;
}
/* Persistent Reserve action which needs attention */
case CTL_MSG_PERS_ACTION:
presio = (struct ctl_prio *)ctl_alloc_io(
softc->othersc_pool);
ctl_zero_io((union ctl_io *)presio);
presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
presio->io_hdr.nexus = msg->hdr.nexus;
presio->pr_msg = msg->pr;
ctl_enqueue_isc((union ctl_io *)presio);
break;
case CTL_MSG_UA:
ctl_isc_ua(softc, msg, param);
break;
case CTL_MSG_PORT_SYNC:
ctl_isc_port_sync(softc, msg, param);
break;
case CTL_MSG_LUN_SYNC:
ctl_isc_lun_sync(softc, msg, param);
break;
case CTL_MSG_IID_SYNC:
ctl_isc_iid_sync(softc, msg, param);
break;
case CTL_MSG_LOGIN:
ctl_isc_login(softc, msg, param);
break;
case CTL_MSG_MODE_SYNC:
ctl_isc_mode_sync(softc, msg, param);
break;
default:
printf("Received HA message of unknown type %d\n",
msg->hdr.msg_type);
ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
break;
}
if (msg != &msgbuf)
free(msg, M_CTL);
} else if (event == CTL_HA_EVT_LINK_CHANGE) {
printf("CTL: HA link status changed from %d to %d\n",
softc->ha_link, param);
if (param == softc->ha_link)
return;
if (softc->ha_link == CTL_HA_LINK_ONLINE) {
softc->ha_link = param;
ctl_isc_ha_link_down(softc);
} else {
softc->ha_link = param;
if (softc->ha_link == CTL_HA_LINK_ONLINE)
ctl_isc_ha_link_up(softc);
}
return;
} else {
printf("ctl_isc_event_handler: Unknown event %d\n", event);
return;
}
}
static void
ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
{
memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
src->scsi.sense_len);
dest->scsiio.scsi_status = src->scsi.scsi_status;
dest->scsiio.sense_len = src->scsi.sense_len;
dest->io_hdr.status = src->hdr.status;
}
static void
ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
{
memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
src->scsiio.sense_len);
dest->scsi.scsi_status = src->scsiio.scsi_status;
dest->scsi.sense_len = src->scsiio.sense_len;
dest->hdr.status = src->io_hdr.status;
}
void
ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
{
struct ctl_softc *softc = lun->ctl_softc;
ctl_ua_type *pu;
if (initidx < softc->init_min || initidx >= softc->init_max)
return;
mtx_assert(&lun->lun_lock, MA_OWNED);
pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
if (pu == NULL)
return;
pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
}
void
ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
{
int i;
mtx_assert(&lun->lun_lock, MA_OWNED);
if (lun->pending_ua[port] == NULL)
return;
for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
if (port * CTL_MAX_INIT_PER_PORT + i == except)
continue;
lun->pending_ua[port][i] |= ua;
}
}
void
ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
{
struct ctl_softc *softc = lun->ctl_softc;
int i;
mtx_assert(&lun->lun_lock, MA_OWNED);
for (i = softc->port_min; i < softc->port_max; i++)
ctl_est_ua_port(lun, i, except, ua);
}
void
ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
{
struct ctl_softc *softc = lun->ctl_softc;
ctl_ua_type *pu;
if (initidx < softc->init_min || initidx >= softc->init_max)
return;
mtx_assert(&lun->lun_lock, MA_OWNED);
pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
if (pu == NULL)
return;
pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
}
void
ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
{
struct ctl_softc *softc = lun->ctl_softc;
int i, j;
mtx_assert(&lun->lun_lock, MA_OWNED);
for (i = softc->port_min; i < softc->port_max; i++) {
if (lun->pending_ua[i] == NULL)
continue;
for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
if (i * CTL_MAX_INIT_PER_PORT + j == except)
continue;
lun->pending_ua[i][j] &= ~ua;
}
}
}
void
ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
ctl_ua_type ua_type)
{
struct ctl_lun *lun;
mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
mtx_lock(&lun->lun_lock);
ctl_clr_ua(lun, initidx, ua_type);
mtx_unlock(&lun->lun_lock);
}
}
static int
ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
{
struct ctl_softc *softc = (struct ctl_softc *)arg1;
struct ctl_lun *lun;
struct ctl_lun_req ireq;
int error, value;
value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
error = sysctl_handle_int(oidp, &value, 0, req);
if ((error != 0) || (req->newptr == NULL))
return (error);
mtx_lock(&softc->ctl_lock);
if (value == 0)
softc->flags |= CTL_FLAG_ACTIVE_SHELF;
else
softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
STAILQ_FOREACH(lun, &softc->lun_list, links) {
mtx_unlock(&softc->ctl_lock);
bzero(&ireq, sizeof(ireq));
ireq.reqtype = CTL_LUNREQ_MODIFY;
ireq.reqdata.modify.lun_id = lun->lun;
lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
curthread);
if (ireq.status != CTL_LUN_OK) {
printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
__func__, ireq.status, ireq.error_str);
}
mtx_lock(&softc->ctl_lock);
}
mtx_unlock(&softc->ctl_lock);
return (0);
}
static int
ctl_init(void)
{
struct ctl_softc *softc;
void *other_pool;
int i, error;
softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
M_WAITOK | M_ZERO);
softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
"cam/ctl");
softc->dev->si_drv1 = softc;
sysctl_ctx_init(&softc->sysctl_ctx);
softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
CTLFLAG_RD, 0, "CAM Target Layer");
if (softc->sysctl_tree == NULL) {
printf("%s: unable to allocate sysctl tree\n", __func__);
destroy_dev(softc->dev);
free(control_softc, M_DEVBUF);
control_softc = NULL;
return (ENOMEM);
}
mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
softc->open_count = 0;
/*
* Default to actually sending a SYNCHRONIZE CACHE command down to
* the drive.
*/
softc->flags = CTL_FLAG_REAL_SYNC;
SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
"HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
/*
* In Copan's HA scheme, the "master" and "slave" roles are
* figured out through the slot the controller is in. Although it
* is an active/active system, someone has to be in charge.
*/
SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
"HA head ID (0 - no HA)");
if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
softc->flags |= CTL_FLAG_ACTIVE_SHELF;
softc->is_single = 1;
softc->port_cnt = CTL_MAX_PORTS;
softc->port_min = 0;
} else {
softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
}
softc->port_max = softc->port_min + softc->port_cnt;
softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
"HA link state (0 - offline, 1 - unknown, 2 - online)");
STAILQ_INIT(&softc->lun_list);
STAILQ_INIT(&softc->pending_lun_queue);
STAILQ_INIT(&softc->fe_list);
STAILQ_INIT(&softc->port_list);
STAILQ_INIT(&softc->be_list);
ctl_tpc_init(softc);
if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
&other_pool) != 0)
{
printf("ctl: can't allocate %d entry other SC pool, "
"exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
return (ENOMEM);
}
softc->othersc_pool = other_pool;
if (worker_threads <= 0)
worker_threads = max(1, mp_ncpus / 4);
if (worker_threads > CTL_MAX_THREADS)
worker_threads = CTL_MAX_THREADS;
for (i = 0; i < worker_threads; i++) {
struct ctl_thread *thr = &softc->threads[i];
mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
thr->ctl_softc = softc;
STAILQ_INIT(&thr->incoming_queue);
STAILQ_INIT(&thr->rtr_queue);
STAILQ_INIT(&thr->done_queue);
STAILQ_INIT(&thr->isc_queue);
error = kproc_kthread_add(ctl_work_thread, thr,
&softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
if (error != 0) {
printf("error creating CTL work thread!\n");
ctl_pool_free(other_pool);
return (error);
}
}
error = kproc_kthread_add(ctl_lun_thread, softc,
&softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
if (error != 0) {
printf("error creating CTL lun thread!\n");
ctl_pool_free(other_pool);
return (error);
}
error = kproc_kthread_add(ctl_thresh_thread, softc,
&softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
if (error != 0) {
printf("error creating CTL threshold thread!\n");
ctl_pool_free(other_pool);
return (error);
}
SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
if (softc->is_single == 0) {
ctl_frontend_register(&ha_frontend);
if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
printf("ctl_init: ctl_ha_msg_init failed.\n");
softc->is_single = 1;
} else
if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
!= CTL_HA_STATUS_SUCCESS) {
printf("ctl_init: ctl_ha_msg_register failed.\n");
softc->is_single = 1;
}
}
return (0);
}
void
ctl_shutdown(void)
{
struct ctl_softc *softc = control_softc;
struct ctl_lun *lun, *next_lun;
if (softc->is_single == 0) {
ctl_ha_msg_shutdown(softc);
if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
!= CTL_HA_STATUS_SUCCESS)
printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
ctl_frontend_deregister(&ha_frontend);
}
mtx_lock(&softc->ctl_lock);
/*
* Free up each LUN.
*/
for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
next_lun = STAILQ_NEXT(lun, links);
ctl_free_lun(lun);
}
mtx_unlock(&softc->ctl_lock);
#if 0
ctl_shutdown_thread(softc->work_thread);
mtx_destroy(&softc->queue_lock);
#endif
ctl_tpc_shutdown(softc);
uma_zdestroy(softc->io_zone);
mtx_destroy(&softc->ctl_lock);
destroy_dev(softc->dev);
sysctl_ctx_free(&softc->sysctl_ctx);
free(control_softc, M_DEVBUF);
control_softc = NULL;
}
static int
ctl_module_event_handler(module_t mod, int what, void *arg)
{
switch (what) {
case MOD_LOAD:
return (ctl_init());
case MOD_UNLOAD:
return (EBUSY);
default:
return (EOPNOTSUPP);
}
}
/*
* XXX KDM should we do some access checks here? Bump a reference count to
* prevent a CTL module from being unloaded while someone has it open?
*/
static int
ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
{
return (0);
}
static int
ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
{
return (0);
}
/*
* Remove an initiator by port number and initiator ID.
* Returns 0 for success, -1 for failure.
*/
int
ctl_remove_initiator(struct ctl_port *port, int iid)
{
struct ctl_softc *softc = port->ctl_softc;
mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
if (iid > CTL_MAX_INIT_PER_PORT) {
printf("%s: initiator ID %u > maximun %u!\n",
__func__, iid, CTL_MAX_INIT_PER_PORT);
return (-1);
}
mtx_lock(&softc->ctl_lock);
port->wwpn_iid[iid].in_use--;
port->wwpn_iid[iid].last_use = time_uptime;
mtx_unlock(&softc->ctl_lock);
ctl_isc_announce_iid(port, iid);
return (0);
}
/*
* Add an initiator to the initiator map.
* Returns iid for success, < 0 for failure.
*/
int
ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
{
struct ctl_softc *softc = port->ctl_softc;
time_t best_time;
int i, best;
mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
if (iid >= CTL_MAX_INIT_PER_PORT) {
printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
__func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
free(name, M_CTL);
return (-1);
}
mtx_lock(&softc->ctl_lock);
if (iid < 0 && (wwpn != 0 || name != NULL)) {
for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
iid = i;
break;
}
if (name != NULL && port->wwpn_iid[i].name != NULL &&
strcmp(name, port->wwpn_iid[i].name) == 0) {
iid = i;
break;
}
}
}
if (iid < 0) {
for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
if (port->wwpn_iid[i].in_use == 0 &&
port->wwpn_iid[i].wwpn == 0 &&
port->wwpn_iid[i].name == NULL) {
iid = i;
break;
}
}
}
if (iid < 0) {
best = -1;
best_time = INT32_MAX;
for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
if (port->wwpn_iid[i].in_use == 0) {
if (port->wwpn_iid[i].last_use < best_time) {
best = i;
best_time = port->wwpn_iid[i].last_use;
}
}
}
iid = best;
}
if (iid < 0) {
mtx_unlock(&softc->ctl_lock);
free(name, M_CTL);
return (-2);
}
if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
/*
* This is not an error yet.
*/
if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
#if 0
printf("%s: port %d iid %u WWPN %#jx arrived"
" again\n", __func__, port->targ_port,
iid, (uintmax_t)wwpn);
#endif
goto take;
}
if (name != NULL && port->wwpn_iid[iid].name != NULL &&
strcmp(name, port->wwpn_iid[iid].name) == 0) {
#if 0
printf("%s: port %d iid %u name '%s' arrived"
" again\n", __func__, port->targ_port,
iid, name);
#endif
goto take;
}
/*
* This is an error, but what do we do about it? The
* driver is telling us we have a new WWPN for this
* initiator ID, so we pretty much need to use it.
*/
printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
" but WWPN %#jx '%s' is still at that address\n",
__func__, port->targ_port, iid, wwpn, name,
(uintmax_t)port->wwpn_iid[iid].wwpn,
port->wwpn_iid[iid].name);
/*
* XXX KDM clear have_ca and ua_pending on each LUN for
* this initiator.
*/
}
take:
free(port->wwpn_iid[iid].name, M_CTL);
port->wwpn_iid[iid].name = name;
port->wwpn_iid[iid].wwpn = wwpn;
port->wwpn_iid[iid].in_use++;
mtx_unlock(&softc->ctl_lock);
ctl_isc_announce_iid(port, iid);
return (iid);
}
static int
ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
{
int len;
switch (port->port_type) {
case CTL_PORT_FC:
{
struct scsi_transportid_fcp *id =
(struct scsi_transportid_fcp *)buf;
if (port->wwpn_iid[iid].wwpn == 0)
return (0);
memset(id, 0, sizeof(*id));
id->format_protocol = SCSI_PROTO_FC;
scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
return (sizeof(*id));
}
case CTL_PORT_ISCSI:
{
struct scsi_transportid_iscsi_port *id =
(struct scsi_transportid_iscsi_port *)buf;
if (port->wwpn_iid[iid].name == NULL)
return (0);
memset(id, 0, 256);
id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
SCSI_PROTO_ISCSI;
len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
len = roundup2(min(len, 252), 4);
scsi_ulto2b(len, id->additional_length);
return (sizeof(*id) + len);
}
case CTL_PORT_SAS:
{
struct scsi_transportid_sas *id =
(struct scsi_transportid_sas *)buf;
if (port->wwpn_iid[iid].wwpn == 0)
return (0);
memset(id, 0, sizeof(*id));
id->format_protocol = SCSI_PROTO_SAS;
scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
return (sizeof(*id));
}
default:
{
struct scsi_transportid_spi *id =
(struct scsi_transportid_spi *)buf;
memset(id, 0, sizeof(*id));
id->format_protocol = SCSI_PROTO_SPI;
scsi_ulto2b(iid, id->scsi_addr);
scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
return (sizeof(*id));
}
}
}
/*
* Serialize a command that went down the "wrong" side, and so was sent to
* this controller for execution. The logic is a little different than the
* standard case in ctl_scsiio_precheck(). Errors in this case need to get
* sent back to the other side, but in the success case, we execute the
* command on this side (XFER mode) or tell the other side to execute it
* (SER_ONLY mode).
*/
static int
ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
{
struct ctl_softc *softc = control_softc;
union ctl_ha_msg msg_info;
struct ctl_port *port;
struct ctl_lun *lun;
const struct ctl_cmd_entry *entry;
int retval = 0;
uint32_t targ_lun;
targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
mtx_lock(&softc->ctl_lock);
/* Make sure that we know about this port. */
port = ctl_io_port(&ctsio->io_hdr);
if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
/*retry_count*/ 1);
goto badjuju;
}
/* Make sure that we know about this LUN. */
if ((targ_lun < CTL_MAX_LUNS) &&
((lun = softc->ctl_luns[targ_lun]) != NULL)) {
mtx_lock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
/*
* If the LUN is invalid, pretend that it doesn't exist.
* It will go away as soon as all pending I/O has been
* completed.
*/
if (lun->flags & CTL_LUN_DISABLED) {
mtx_unlock(&lun->lun_lock);
lun = NULL;
}
} else {
mtx_unlock(&softc->ctl_lock);
lun = NULL;
}
if (lun == NULL) {
/*
* The other node would not send this request to us unless
* received announce that we are primary node for this LUN.
* If this LUN does not exist now, it is probably result of
* a race, so respond to initiator in the most opaque way.
*/
ctl_set_busy(ctsio);
goto badjuju;
}
entry = ctl_get_cmd_entry(ctsio, NULL);
if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
mtx_unlock(&lun->lun_lock);
goto badjuju;
}
ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
/*
* Every I/O goes into the OOA queue for a
* particular LUN, and stays there until completion.
*/
#ifdef CTL_TIME_IO
if (TAILQ_EMPTY(&lun->ooa_queue))
lun->idle_time += getsbinuptime() - lun->last_busy;
#endif
TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
(union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
ooa_links))) {
case CTL_ACTION_BLOCK:
ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
blocked_links);
mtx_unlock(&lun->lun_lock);
break;
case CTL_ACTION_PASS:
case CTL_ACTION_SKIP:
if (softc->ha_mode == CTL_HA_MODE_XFER) {
ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
ctl_enqueue_rtr((union ctl_io *)ctsio);
mtx_unlock(&lun->lun_lock);
} else {
ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
mtx_unlock(&lun->lun_lock);
/* send msg back to other side */
msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
msg_info.hdr.msg_type = CTL_MSG_R2R;
ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info.hdr), M_WAITOK);
}
break;
case CTL_ACTION_OVERLAP:
TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
mtx_unlock(&lun->lun_lock);
ctl_set_overlapped_cmd(ctsio);
goto badjuju;
case CTL_ACTION_OVERLAP_TAG:
TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
mtx_unlock(&lun->lun_lock);
ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
goto badjuju;
case CTL_ACTION_ERROR:
default:
TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
mtx_unlock(&lun->lun_lock);
ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
/*retry_count*/ 0);
badjuju:
ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
msg_info.hdr.serializing_sc = NULL;
msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info.scsi), M_WAITOK);
retval = 1;
break;
}
return (retval);
}
/*
* Returns 0 for success, errno for failure.
*/
static void
ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
{
union ctl_io *io;
mtx_lock(&lun->lun_lock);
for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
(*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
ooa_links)) {
struct ctl_ooa_entry *entry;
/*
* If we've got more than we can fit, just count the
* remaining entries.
*/
if (*cur_fill_num >= ooa_hdr->alloc_num)
continue;
entry = &kern_entries[*cur_fill_num];
entry->tag_num = io->scsiio.tag_num;
entry->lun_num = lun->lun;
#ifdef CTL_TIME_IO
entry->start_bt = io->io_hdr.start_bt;
#endif
bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
entry->cdb_len = io->scsiio.cdb_len;
if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
if (io->io_hdr.flags & CTL_FLAG_ABORT)
entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
}
mtx_unlock(&lun->lun_lock);
}
static void *
ctl_copyin_alloc(void *user_addr, int len, char *error_str,
size_t error_str_len)
{
void *kptr;
kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
if (copyin(user_addr, kptr, len) != 0) {
snprintf(error_str, error_str_len, "Error copying %d bytes "
"from user address %p to kernel address %p", len,
user_addr, kptr);
free(kptr, M_CTL);
return (NULL);
}
return (kptr);
}
static void
ctl_free_args(int num_args, struct ctl_be_arg *args)
{
int i;
if (args == NULL)
return;
for (i = 0; i < num_args; i++) {
free(args[i].kname, M_CTL);
free(args[i].kvalue, M_CTL);
}
free(args, M_CTL);
}
static struct ctl_be_arg *
ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
char *error_str, size_t error_str_len)
{
struct ctl_be_arg *args;
int i;
args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
error_str, error_str_len);
if (args == NULL)
goto bailout;
for (i = 0; i < num_args; i++) {
args[i].kname = NULL;
args[i].kvalue = NULL;
}
for (i = 0; i < num_args; i++) {
uint8_t *tmpptr;
args[i].kname = ctl_copyin_alloc(args[i].name,
args[i].namelen, error_str, error_str_len);
if (args[i].kname == NULL)
goto bailout;
if (args[i].kname[args[i].namelen - 1] != '\0') {
snprintf(error_str, error_str_len, "Argument %d "
"name is not NUL-terminated", i);
goto bailout;
}
if (args[i].flags & CTL_BEARG_RD) {
tmpptr = ctl_copyin_alloc(args[i].value,
args[i].vallen, error_str, error_str_len);
if (tmpptr == NULL)
goto bailout;
if ((args[i].flags & CTL_BEARG_ASCII)
&& (tmpptr[args[i].vallen - 1] != '\0')) {
snprintf(error_str, error_str_len, "Argument "
"%d value is not NUL-terminated", i);
goto bailout;
}
args[i].kvalue = tmpptr;
} else {
args[i].kvalue = malloc(args[i].vallen,
M_CTL, M_WAITOK | M_ZERO);
}
}
return (args);
bailout:
ctl_free_args(num_args, args);
return (NULL);
}
static void
ctl_copyout_args(int num_args, struct ctl_be_arg *args)
{
int i;
for (i = 0; i < num_args; i++) {
if (args[i].flags & CTL_BEARG_WR)
copyout(args[i].kvalue, args[i].value, args[i].vallen);
}
}
/*
* Escape characters that are illegal or not recommended in XML.
*/
int
ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
{
char *end = str + size;
int retval;
retval = 0;
for (; *str && str < end; str++) {
switch (*str) {
case '&':
retval = sbuf_printf(sb, "&");
break;
case '>':
retval = sbuf_printf(sb, ">");
break;
case '<':
retval = sbuf_printf(sb, "<");
break;
default:
retval = sbuf_putc(sb, *str);
break;
}
if (retval != 0)
break;
}
return (retval);
}
static void
ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
{
struct scsi_vpd_id_descriptor *desc;
int i;
if (id == NULL || id->len < 4)
return;
desc = (struct scsi_vpd_id_descriptor *)id->data;
switch (desc->id_type & SVPD_ID_TYPE_MASK) {
case SVPD_ID_TYPE_T10:
sbuf_printf(sb, "t10.");
break;
case SVPD_ID_TYPE_EUI64:
sbuf_printf(sb, "eui.");
break;
case SVPD_ID_TYPE_NAA:
sbuf_printf(sb, "naa.");
break;
case SVPD_ID_TYPE_SCSI_NAME:
break;
}
switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
case SVPD_ID_CODESET_BINARY:
for (i = 0; i < desc->length; i++)
sbuf_printf(sb, "%02x", desc->identifier[i]);
break;
case SVPD_ID_CODESET_ASCII:
sbuf_printf(sb, "%.*s", (int)desc->length,
(char *)desc->identifier);
break;
case SVPD_ID_CODESET_UTF8:
sbuf_printf(sb, "%s", (char *)desc->identifier);
break;
}
}
static int
ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
struct thread *td)
{
struct ctl_softc *softc = dev->si_drv1;
struct ctl_lun *lun;
int retval;
retval = 0;
switch (cmd) {
case CTL_IO:
retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
break;
case CTL_ENABLE_PORT:
case CTL_DISABLE_PORT:
case CTL_SET_PORT_WWNS: {
struct ctl_port *port;
struct ctl_port_entry *entry;
entry = (struct ctl_port_entry *)addr;
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(port, &softc->port_list, links) {
int action, done;
if (port->targ_port < softc->port_min ||
port->targ_port >= softc->port_max)
continue;
action = 0;
done = 0;
if ((entry->port_type == CTL_PORT_NONE)
&& (entry->targ_port == port->targ_port)) {
/*
* If the user only wants to enable or
* disable or set WWNs on a specific port,
* do the operation and we're done.
*/
action = 1;
done = 1;
} else if (entry->port_type & port->port_type) {
/*
* Compare the user's type mask with the
* particular frontend type to see if we
* have a match.
*/
action = 1;
done = 0;
/*
* Make sure the user isn't trying to set
* WWNs on multiple ports at the same time.
*/
if (cmd == CTL_SET_PORT_WWNS) {
printf("%s: Can't set WWNs on "
"multiple ports\n", __func__);
retval = EINVAL;
break;
}
}
if (action == 0)
continue;
/*
* XXX KDM we have to drop the lock here, because
* the online/offline operations can potentially
* block. We need to reference count the frontends
* so they can't go away,
*/
if (cmd == CTL_ENABLE_PORT) {
mtx_unlock(&softc->ctl_lock);
ctl_port_online(port);
mtx_lock(&softc->ctl_lock);
} else if (cmd == CTL_DISABLE_PORT) {
mtx_unlock(&softc->ctl_lock);
ctl_port_offline(port);
mtx_lock(&softc->ctl_lock);
} else if (cmd == CTL_SET_PORT_WWNS) {
ctl_port_set_wwns(port,
(entry->flags & CTL_PORT_WWNN_VALID) ?
1 : 0, entry->wwnn,
(entry->flags & CTL_PORT_WWPN_VALID) ?
1 : 0, entry->wwpn);
}
if (done != 0)
break;
}
mtx_unlock(&softc->ctl_lock);
break;
}
case CTL_GET_PORT_LIST: {
struct ctl_port *port;
struct ctl_port_list *list;
int i;
list = (struct ctl_port_list *)addr;
if (list->alloc_len != (list->alloc_num *
sizeof(struct ctl_port_entry))) {
printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
"alloc_num %u * sizeof(struct ctl_port_entry) "
"%zu\n", __func__, list->alloc_len,
list->alloc_num, sizeof(struct ctl_port_entry));
retval = EINVAL;
break;
}
list->fill_len = 0;
list->fill_num = 0;
list->dropped_num = 0;
i = 0;
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(port, &softc->port_list, links) {
struct ctl_port_entry entry, *list_entry;
if (list->fill_num >= list->alloc_num) {
list->dropped_num++;
continue;
}
entry.port_type = port->port_type;
strlcpy(entry.port_name, port->port_name,
sizeof(entry.port_name));
entry.targ_port = port->targ_port;
entry.physical_port = port->physical_port;
entry.virtual_port = port->virtual_port;
entry.wwnn = port->wwnn;
entry.wwpn = port->wwpn;
if (port->status & CTL_PORT_STATUS_ONLINE)
entry.online = 1;
else
entry.online = 0;
list_entry = &list->entries[i];
retval = copyout(&entry, list_entry, sizeof(entry));
if (retval != 0) {
printf("%s: CTL_GET_PORT_LIST: copyout "
"returned %d\n", __func__, retval);
break;
}
i++;
list->fill_num++;
list->fill_len += sizeof(entry);
}
mtx_unlock(&softc->ctl_lock);
/*
* If this is non-zero, we had a copyout fault, so there's
* probably no point in attempting to set the status inside
* the structure.
*/
if (retval != 0)
break;
if (list->dropped_num > 0)
list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
else
list->status = CTL_PORT_LIST_OK;
break;
}
case CTL_DUMP_OOA: {
union ctl_io *io;
char printbuf[128];
struct sbuf sb;
mtx_lock(&softc->ctl_lock);
printf("Dumping OOA queues:\n");
STAILQ_FOREACH(lun, &softc->lun_list, links) {
mtx_lock(&lun->lun_lock);
for (io = (union ctl_io *)TAILQ_FIRST(
&lun->ooa_queue); io != NULL;
io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
ooa_links)) {
sbuf_new(&sb, printbuf, sizeof(printbuf),
SBUF_FIXEDLEN);
sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
(intmax_t)lun->lun,
io->scsiio.tag_num,
(io->io_hdr.flags &
CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
(io->io_hdr.flags &
CTL_FLAG_DMA_INPROG) ? " DMA" : "",
(io->io_hdr.flags &
CTL_FLAG_ABORT) ? " ABORT" : "",
(io->io_hdr.flags &
CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
ctl_scsi_command_string(&io->scsiio, NULL, &sb);
sbuf_finish(&sb);
printf("%s\n", sbuf_data(&sb));
}
mtx_unlock(&lun->lun_lock);
}
printf("OOA queues dump done\n");
mtx_unlock(&softc->ctl_lock);
break;
}
case CTL_GET_OOA: {
struct ctl_ooa *ooa_hdr;
struct ctl_ooa_entry *entries;
uint32_t cur_fill_num;
ooa_hdr = (struct ctl_ooa *)addr;
if ((ooa_hdr->alloc_len == 0)
|| (ooa_hdr->alloc_num == 0)) {
printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
"must be non-zero\n", __func__,
ooa_hdr->alloc_len, ooa_hdr->alloc_num);
retval = EINVAL;
break;
}
if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
sizeof(struct ctl_ooa_entry))) {
printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
"num %d * sizeof(struct ctl_ooa_entry) %zd\n",
__func__, ooa_hdr->alloc_len,
ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
retval = EINVAL;
break;
}
entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
if (entries == NULL) {
printf("%s: could not allocate %d bytes for OOA "
"dump\n", __func__, ooa_hdr->alloc_len);
retval = ENOMEM;
break;
}
mtx_lock(&softc->ctl_lock);
if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
&& ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
|| (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
mtx_unlock(&softc->ctl_lock);
free(entries, M_CTL);
printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
__func__, (uintmax_t)ooa_hdr->lun_num);
retval = EINVAL;
break;
}
cur_fill_num = 0;
if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
STAILQ_FOREACH(lun, &softc->lun_list, links) {
ctl_ioctl_fill_ooa(lun, &cur_fill_num,
ooa_hdr, entries);
}
} else {
lun = softc->ctl_luns[ooa_hdr->lun_num];
ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
entries);
}
mtx_unlock(&softc->ctl_lock);
ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
ooa_hdr->fill_len = ooa_hdr->fill_num *
sizeof(struct ctl_ooa_entry);
retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
if (retval != 0) {
printf("%s: error copying out %d bytes for OOA dump\n",
__func__, ooa_hdr->fill_len);
}
getbinuptime(&ooa_hdr->cur_bt);
if (cur_fill_num > ooa_hdr->alloc_num) {
ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
} else {
ooa_hdr->dropped_num = 0;
ooa_hdr->status = CTL_OOA_OK;
}
free(entries, M_CTL);
break;
}
case CTL_CHECK_OOA: {
union ctl_io *io;
struct ctl_ooa_info *ooa_info;
ooa_info = (struct ctl_ooa_info *)addr;
if (ooa_info->lun_id >= CTL_MAX_LUNS) {
ooa_info->status = CTL_OOA_INVALID_LUN;
break;
}
mtx_lock(&softc->ctl_lock);
lun = softc->ctl_luns[ooa_info->lun_id];
if (lun == NULL) {
mtx_unlock(&softc->ctl_lock);
ooa_info->status = CTL_OOA_INVALID_LUN;
break;
}
mtx_lock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
ooa_info->num_entries = 0;
for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
io != NULL; io = (union ctl_io *)TAILQ_NEXT(
&io->io_hdr, ooa_links)) {
ooa_info->num_entries++;
}
mtx_unlock(&lun->lun_lock);
ooa_info->status = CTL_OOA_SUCCESS;
break;
}
case CTL_DELAY_IO: {
struct ctl_io_delay_info *delay_info;
delay_info = (struct ctl_io_delay_info *)addr;
#ifdef CTL_IO_DELAY
mtx_lock(&softc->ctl_lock);
if ((delay_info->lun_id >= CTL_MAX_LUNS)
|| (softc->ctl_luns[delay_info->lun_id] == NULL)) {
delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
} else {
lun = softc->ctl_luns[delay_info->lun_id];
mtx_lock(&lun->lun_lock);
delay_info->status = CTL_DELAY_STATUS_OK;
switch (delay_info->delay_type) {
case CTL_DELAY_TYPE_CONT:
break;
case CTL_DELAY_TYPE_ONESHOT:
break;
default:
delay_info->status =
CTL_DELAY_STATUS_INVALID_TYPE;
break;
}
switch (delay_info->delay_loc) {
case CTL_DELAY_LOC_DATAMOVE:
lun->delay_info.datamove_type =
delay_info->delay_type;
lun->delay_info.datamove_delay =
delay_info->delay_secs;
break;
case CTL_DELAY_LOC_DONE:
lun->delay_info.done_type =
delay_info->delay_type;
lun->delay_info.done_delay =
delay_info->delay_secs;
break;
default:
delay_info->status =
CTL_DELAY_STATUS_INVALID_LOC;
break;
}
mtx_unlock(&lun->lun_lock);
}
mtx_unlock(&softc->ctl_lock);
#else
delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
#endif /* CTL_IO_DELAY */
break;
}
case CTL_REALSYNC_SET: {
int *syncstate;
syncstate = (int *)addr;
mtx_lock(&softc->ctl_lock);
switch (*syncstate) {
case 0:
softc->flags &= ~CTL_FLAG_REAL_SYNC;
break;
case 1:
softc->flags |= CTL_FLAG_REAL_SYNC;
break;
default:
retval = EINVAL;
break;
}
mtx_unlock(&softc->ctl_lock);
break;
}
case CTL_REALSYNC_GET: {
int *syncstate;
syncstate = (int*)addr;
mtx_lock(&softc->ctl_lock);
if (softc->flags & CTL_FLAG_REAL_SYNC)
*syncstate = 1;
else
*syncstate = 0;
mtx_unlock(&softc->ctl_lock);
break;
}
case CTL_SETSYNC:
case CTL_GETSYNC: {
struct ctl_sync_info *sync_info;
sync_info = (struct ctl_sync_info *)addr;
mtx_lock(&softc->ctl_lock);
lun = softc->ctl_luns[sync_info->lun_id];
if (lun == NULL) {
mtx_unlock(&softc->ctl_lock);
sync_info->status = CTL_GS_SYNC_NO_LUN;
break;
}
/*
* Get or set the sync interval. We're not bounds checking
* in the set case, hopefully the user won't do something
* silly.
*/
mtx_lock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
if (cmd == CTL_GETSYNC)
sync_info->sync_interval = lun->sync_interval;
else
lun->sync_interval = sync_info->sync_interval;
mtx_unlock(&lun->lun_lock);
sync_info->status = CTL_GS_SYNC_OK;
break;
}
case CTL_GETSTATS: {
struct ctl_stats *stats;
int i;
stats = (struct ctl_stats *)addr;
if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
stats->alloc_len) {
stats->status = CTL_SS_NEED_MORE_SPACE;
stats->num_luns = softc->num_luns;
break;
}
/*
* XXX KDM no locking here. If the LUN list changes,
* things can blow up.
*/
for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
i++, lun = STAILQ_NEXT(lun, links)) {
retval = copyout(&lun->stats, &stats->lun_stats[i],
sizeof(lun->stats));
if (retval != 0)
break;
}
stats->num_luns = softc->num_luns;
stats->fill_len = sizeof(struct ctl_lun_io_stats) *
softc->num_luns;
stats->status = CTL_SS_OK;
#ifdef CTL_TIME_IO
stats->flags = CTL_STATS_FLAG_TIME_VALID;
#else
stats->flags = CTL_STATS_FLAG_NONE;
#endif
getnanouptime(&stats->timestamp);
break;
}
case CTL_ERROR_INJECT: {
struct ctl_error_desc *err_desc, *new_err_desc;
err_desc = (struct ctl_error_desc *)addr;
new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
M_WAITOK | M_ZERO);
bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
mtx_lock(&softc->ctl_lock);
lun = softc->ctl_luns[err_desc->lun_id];
if (lun == NULL) {
mtx_unlock(&softc->ctl_lock);
free(new_err_desc, M_CTL);
printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
__func__, (uintmax_t)err_desc->lun_id);
retval = EINVAL;
break;
}
mtx_lock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
/*
* We could do some checking here to verify the validity
* of the request, but given the complexity of error
* injection requests, the checking logic would be fairly
* complex.
*
* For now, if the request is invalid, it just won't get
* executed and might get deleted.
*/
STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
/*
* XXX KDM check to make sure the serial number is unique,
* in case we somehow manage to wrap. That shouldn't
* happen for a very long time, but it's the right thing to
* do.
*/
new_err_desc->serial = lun->error_serial;
err_desc->serial = lun->error_serial;
lun->error_serial++;
mtx_unlock(&lun->lun_lock);
break;
}
case CTL_ERROR_INJECT_DELETE: {
struct ctl_error_desc *delete_desc, *desc, *desc2;
int delete_done;
delete_desc = (struct ctl_error_desc *)addr;
delete_done = 0;
mtx_lock(&softc->ctl_lock);
lun = softc->ctl_luns[delete_desc->lun_id];
if (lun == NULL) {
mtx_unlock(&softc->ctl_lock);
printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
__func__, (uintmax_t)delete_desc->lun_id);
retval = EINVAL;
break;
}
mtx_lock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
if (desc->serial != delete_desc->serial)
continue;
STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
links);
free(desc, M_CTL);
delete_done = 1;
}
mtx_unlock(&lun->lun_lock);
if (delete_done == 0) {
printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
"error serial %ju on LUN %u\n", __func__,
delete_desc->serial, delete_desc->lun_id);
retval = EINVAL;
break;
}
break;
}
case CTL_DUMP_STRUCTS: {
int i, j, k;
struct ctl_port *port;
struct ctl_frontend *fe;
mtx_lock(&softc->ctl_lock);
printf("CTL Persistent Reservation information start:\n");
for (i = 0; i < CTL_MAX_LUNS; i++) {
lun = softc->ctl_luns[i];
if ((lun == NULL)
|| ((lun->flags & CTL_LUN_DISABLED) != 0))
continue;
for (j = 0; j < CTL_MAX_PORTS; j++) {
if (lun->pr_keys[j] == NULL)
continue;
for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
if (lun->pr_keys[j][k] == 0)
continue;
printf(" LUN %d port %d iid %d key "
"%#jx\n", i, j, k,
(uintmax_t)lun->pr_keys[j][k]);
}
}
}
printf("CTL Persistent Reservation information end\n");
printf("CTL Ports:\n");
STAILQ_FOREACH(port, &softc->port_list, links) {
printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
"%#jx WWPN %#jx\n", port->targ_port, port->port_name,
port->frontend->name, port->port_type,
port->physical_port, port->virtual_port,
(uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
if (port->wwpn_iid[j].in_use == 0 &&
port->wwpn_iid[j].wwpn == 0 &&
port->wwpn_iid[j].name == NULL)
continue;
printf(" iid %u use %d WWPN %#jx '%s'\n",
j, port->wwpn_iid[j].in_use,
(uintmax_t)port->wwpn_iid[j].wwpn,
port->wwpn_iid[j].name);
}
}
printf("CTL Port information end\n");
mtx_unlock(&softc->ctl_lock);
/*
* XXX KDM calling this without a lock. We'd likely want
* to drop the lock before calling the frontend's dump
* routine anyway.
*/
printf("CTL Frontends:\n");
STAILQ_FOREACH(fe, &softc->fe_list, links) {
printf(" Frontend '%s'\n", fe->name);
if (fe->fe_dump != NULL)
fe->fe_dump();
}
printf("CTL Frontend information end\n");
break;
}
case CTL_LUN_REQ: {
struct ctl_lun_req *lun_req;
struct ctl_backend_driver *backend;
lun_req = (struct ctl_lun_req *)addr;
backend = ctl_backend_find(lun_req->backend);
if (backend == NULL) {
lun_req->status = CTL_LUN_ERROR;
snprintf(lun_req->error_str,
sizeof(lun_req->error_str),
"Backend \"%s\" not found.",
lun_req->backend);
break;
}
if (lun_req->num_be_args > 0) {
lun_req->kern_be_args = ctl_copyin_args(
lun_req->num_be_args,
lun_req->be_args,
lun_req->error_str,
sizeof(lun_req->error_str));
if (lun_req->kern_be_args == NULL) {
lun_req->status = CTL_LUN_ERROR;
break;
}
}
retval = backend->ioctl(dev, cmd, addr, flag, td);
if (lun_req->num_be_args > 0) {
ctl_copyout_args(lun_req->num_be_args,
lun_req->kern_be_args);
ctl_free_args(lun_req->num_be_args,
lun_req->kern_be_args);
}
break;
}
case CTL_LUN_LIST: {
struct sbuf *sb;
struct ctl_lun_list *list;
struct ctl_option *opt;
list = (struct ctl_lun_list *)addr;
/*
* Allocate a fixed length sbuf here, based on the length
* of the user's buffer. We could allocate an auto-extending
* buffer, and then tell the user how much larger our
* amount of data is than his buffer, but that presents
* some problems:
*
* 1. The sbuf(9) routines use a blocking malloc, and so
* we can't hold a lock while calling them with an
* auto-extending buffer.
*
* 2. There is not currently a LUN reference counting
* mechanism, outside of outstanding transactions on
* the LUN's OOA queue. So a LUN could go away on us
* while we're getting the LUN number, backend-specific
* information, etc. Thus, given the way things
* currently work, we need to hold the CTL lock while
* grabbing LUN information.
*
* So, from the user's standpoint, the best thing to do is
* allocate what he thinks is a reasonable buffer length,
* and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
* double the buffer length and try again. (And repeat
* that until he succeeds.)
*/
sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
if (sb == NULL) {
list->status = CTL_LUN_LIST_ERROR;
snprintf(list->error_str, sizeof(list->error_str),
"Unable to allocate %d bytes for LUN list",
list->alloc_len);
break;
}
sbuf_printf(sb, "<ctllunlist>\n");
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(lun, &softc->lun_list, links) {
mtx_lock(&lun->lun_lock);
retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
(uintmax_t)lun->lun);
/*
* Bail out as soon as we see that we've overfilled
* the buffer.
*/
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<backend_type>%s"
"</backend_type>\n",
(lun->backend == NULL) ? "none" :
lun->backend->name);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
lun->be_lun->lun_type);
if (retval != 0)
break;
if (lun->backend == NULL) {
retval = sbuf_printf(sb, "</lun>\n");
if (retval != 0)
break;
continue;
}
retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
(lun->be_lun->maxlba > 0) ?
lun->be_lun->maxlba + 1 : 0);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
lun->be_lun->blocksize);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<serial_number>");
if (retval != 0)
break;
retval = ctl_sbuf_printf_esc(sb,
lun->be_lun->serial_num,
sizeof(lun->be_lun->serial_num));
if (retval != 0)
break;
retval = sbuf_printf(sb, "</serial_number>\n");
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<device_id>");
if (retval != 0)
break;
retval = ctl_sbuf_printf_esc(sb,
lun->be_lun->device_id,
sizeof(lun->be_lun->device_id));
if (retval != 0)
break;
retval = sbuf_printf(sb, "</device_id>\n");
if (retval != 0)
break;
if (lun->backend->lun_info != NULL) {
retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
if (retval != 0)
break;
}
STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
opt->name, opt->value, opt->name);
if (retval != 0)
break;
}
retval = sbuf_printf(sb, "</lun>\n");
if (retval != 0)
break;
mtx_unlock(&lun->lun_lock);
}
if (lun != NULL)
mtx_unlock(&lun->lun_lock);
mtx_unlock(&softc->ctl_lock);
if ((retval != 0)
|| ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
retval = 0;
sbuf_delete(sb);
list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
snprintf(list->error_str, sizeof(list->error_str),
"Out of space,
|