/*-
* Copyright (c) 2003-2009 Silicon Graphics International Corp.
* Copyright (c) 2012 The FreeBSD Foundation
* 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: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
*/
/*
* 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/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 <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_frontend_internal.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;
/*
* Size and alignment macros needed for Copan-specific HA hardware. These
* can go away when the HA code is re-written, and uses busdma for any
* hardware.
*/
#define CTL_ALIGN_8B(target, source, type) \
if (((uint32_t)source & 0x7) != 0) \
target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
else \
target = (type)source;
#define CTL_SIZE_8B(target, size) \
if ((size & 0x7) != 0) \
target = size + (0x8 - (size & 0x7)); \
else \
target = size;
#define CTL_ALIGN_8B_MARGIN 16
/*
* Template mode pages.
*/
/*
* Note that these are default values only. The actual values will be
* filled in when the user does a mode sense.
*/
static struct copan_power_subpage power_page_default = {
/*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
/*subpage*/ PWR_SUBPAGE_CODE,
/*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
(sizeof(struct copan_power_subpage) - 4) & 0x00ff},
/*page_version*/ PWR_VERSION,
/* total_luns */ 26,
/* max_active_luns*/ PWR_DFLT_MAX_LUNS,
/*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0}
};
static struct copan_power_subpage power_page_changeable = {
/*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
/*subpage*/ PWR_SUBPAGE_CODE,
/*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
(sizeof(struct copan_power_subpage) - 4) & 0x00ff},
/*page_version*/ 0,
/* total_luns */ 0,
/* max_active_luns*/ 0,
/*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0}
};
static struct copan_aps_subpage aps_page_default = {
APS_PAGE_CODE | SMPH_SPF, //page_code
APS_SUBPAGE_CODE, //subpage
{(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
(sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
APS_VERSION, //page_version
0, //lock_active
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0} //reserved
};
static struct copan_aps_subpage aps_page_changeable = {
APS_PAGE_CODE | SMPH_SPF, //page_code
APS_SUBPAGE_CODE, //subpage
{(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
(sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
0, //page_version
0, //lock_active
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0} //reserved
};
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 */
};
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 */
};
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}
};
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}
};
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}
};
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}
};
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}
};
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}
};
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}
};
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*/0,
/*flags4*/0,
/*aen_holdoff_period*/{0, 0},
/*busy_timeout_period*/{0, 0},
/*extended_selftest_completion_time*/{0, 0}
};
/*
* XXX KDM move these into the softc.
*/
static int rcv_sync_msg;
static int persis_offset;
static uint8_t ctl_pause_rtr;
static int ctl_is_single = 1;
static int index_to_aps_page;
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 verbose = 0;
SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
&verbose, 0, "Show SCSI errors returned to initiator");
/*
* 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 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 void ctl_ioctl_online(void *arg);
static void ctl_ioctl_offline(void *arg);
static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
static int ctl_ioctl_submit_wait(union ctl_io *io);
static void ctl_ioctl_datamove(union ctl_io *io);
static void ctl_ioctl_done(union ctl_io *io);
static void ctl_ioctl_hard_startstop_callback(void *arg,
struct cfi_metatask *metatask);
static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
static int 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 uint32_t ctl_map_lun(int port_num, uint32_t lun);
static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
#ifdef unused
static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
uint32_t targ_target, uint32_t targ_lun,
int can_wait);
static void ctl_kfree_io(union ctl_io *io);
#endif /* unused */
static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
struct ctl_be_lun *be_lun, struct ctl_id target_id);
static int ctl_free_lun(struct ctl_lun *lun);
static void ctl_create_lun(struct ctl_be_lun *be_lun);
/**
static void ctl_failover_change_pages(struct ctl_softc *softc,
struct ctl_scsiio *ctsio, int master);
**/
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);
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_softc *ctl_softc,
struct ctl_lun *lun,
const struct ctl_cmd_entry *entry,
struct ctl_scsiio *ctsio);
//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
static void ctl_failover(void);
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_lun_reset(struct ctl_lun *lun, union ctl_io *io,
ctl_ua_type ua_type);
static int ctl_abort_task(union ctl_io *io);
static int ctl_abort_task_set(union ctl_io *io);
static int ctl_i_t_nexus_reset(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 int ctl_process_done(union ctl_io *io);
static void ctl_lun_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);
/*
* 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");
MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
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 ioctl_frontend =
{
.name = "ioctl",
};
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,
sizeof(ctsio->sense_data));
memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
&msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
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;
#if 0
/*
* Attempt to catch the situation where an I/O has
* been freed, and we're using it again.
*/
if (ctsio->io_hdr.io_type == 0xff) {
union ctl_io *tmp_io;
tmp_io = (union ctl_io *)ctsio;
printf("%s: %p use after free!\n", __func__,
ctsio);
printf("%s: type %d msg %d cdb %x iptl: "
"%d:%d:%d:%d tag 0x%04x "
"flag %#x status %x\n",
__func__,
tmp_io->io_hdr.io_type,
tmp_io->io_hdr.msg_type,
tmp_io->scsiio.cdb[0],
tmp_io->io_hdr.nexus.initid.id,
tmp_io->io_hdr.nexus.targ_port,
tmp_io->io_hdr.nexus.targ_target.id,
tmp_io->io_hdr.nexus.targ_lun,
(tmp_io->io_hdr.io_type ==
CTL_IO_TASK) ?
tmp_io->taskio.tag_num :
tmp_io->scsiio.tag_num,
tmp_io->io_hdr.flags,
tmp_io->io_hdr.status);
}
#endif
ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
ctl_enqueue_isc((union ctl_io *)ctsio);
}
/*
* 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 *ctl_softc;
union ctl_io *io;
struct ctl_prio *presio;
ctl_ha_status isc_status;
ctl_softc = control_softc;
io = NULL;
#if 0
printf("CTL: Isc Msg event %d\n", event);
#endif
if (event == CTL_HA_EVT_MSG_RECV) {
union ctl_ha_msg msg_info;
isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), /*wait*/ 0);
#if 0
printf("CTL: msg_type %d\n", msg_info.msg_type);
#endif
if (isc_status != 0) {
printf("Error receiving message, status = %d\n",
isc_status);
return;
}
switch (msg_info.hdr.msg_type) {
case CTL_MSG_SERIALIZE:
#if 0
printf("Serialize\n");
#endif
io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
if (io == NULL) {
printf("ctl_isc_event_handler: can't allocate "
"ctl_io!\n");
/* Bad Juju */
/* Need to set busy and send msg back */
msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
msg_info.hdr.status = CTL_SCSI_ERROR;
msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
msg_info.scsi.sense_len = 0;
if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
}
goto bailout;
}
ctl_zero_io(io);
// populate ctsio from msg_info
io->io_hdr.io_type = CTL_IO_SCSI;
io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
io->io_hdr.original_sc = msg_info.hdr.original_sc;
#if 0
printf("pOrig %x\n", (int)msg_info.original_sc);
#endif
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 (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
io->io_hdr.flags |= CTL_FLAG_INT_COPY;
io->io_hdr.nexus = msg_info.hdr.nexus;
#if 0
printf("targ %d, port %d, iid %d, lun %d\n",
io->io_hdr.nexus.targ_target.id,
io->io_hdr.nexus.targ_port,
io->io_hdr.nexus.initid.id,
io->io_hdr.nexus.targ_lun);
#endif
io->scsiio.tag_num = msg_info.scsi.tag_num;
io->scsiio.tag_type = msg_info.scsi.tag_type;
memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
CTL_MAX_CDBLEN);
if (ctl_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_info.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_info.hdr.serializing_sc;
if (msg_info.dt.sg_sequence == 0) {
/*
* XXX KDM we use the preallocated S/G list
* here, but we'll need to change this to
* dynamic allocation if we need larger S/G
* lists.
*/
if (msg_info.dt.kern_sg_entries >
sizeof(io->io_hdr.remote_sglist) /
sizeof(io->io_hdr.remote_sglist[0])) {
printf("%s: number of S/G entries "
"needed %u > allocated num %zd\n",
__func__,
msg_info.dt.kern_sg_entries,
sizeof(io->io_hdr.remote_sglist)/
sizeof(io->io_hdr.remote_sglist[0]));
/*
* XXX KDM send a message back to
* the other side to shut down the
* DMA. The error will come back
* through via the normal channel.
*/
break;
}
sgl = io->io_hdr.remote_sglist;
memset(sgl, 0,
sizeof(io->io_hdr.remote_sglist));
io->scsiio.kern_data_ptr = (uint8_t *)sgl;
io->scsiio.kern_sg_entries =
msg_info.dt.kern_sg_entries;
io->scsiio.rem_sg_entries =
msg_info.dt.kern_sg_entries;
io->scsiio.kern_data_len =
msg_info.dt.kern_data_len;
io->scsiio.kern_total_len =
msg_info.dt.kern_total_len;
io->scsiio.kern_data_resid =
msg_info.dt.kern_data_resid;
io->scsiio.kern_rel_offset =
msg_info.dt.kern_rel_offset;
/*
* Clear out per-DMA flags.
*/
io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
/*
* Add per-DMA flags that are set for this
* particular DMA request.
*/
io->io_hdr.flags |= msg_info.dt.flags &
CTL_FLAG_RDMA_MASK;
} else
sgl = (struct ctl_sg_entry *)
io->scsiio.kern_data_ptr;
for (i = msg_info.dt.sent_sg_entries, j = 0;
i < (msg_info.dt.sent_sg_entries +
msg_info.dt.cur_sg_entries); i++, j++) {
sgl[i].addr = msg_info.dt.sg_list[j].addr;
sgl[i].len = msg_info.dt.sg_list[j].len;
#if 0
printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
__func__,
msg_info.dt.sg_list[j].addr,
msg_info.dt.sg_list[j].len,
sgl[i].addr, sgl[i].len, j, i);
#endif
}
#if 0
memcpy(&sgl[msg_info.dt.sent_sg_entries],
msg_info.dt.sg_list,
sizeof(*sgl) * msg_info.dt.cur_sg_entries);
#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_info.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_info.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_info.hdr.serializing_sc;
io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
io->io_hdr.status = msg_info.hdr.status;
io->scsiio.scsi_status = msg_info.scsi.scsi_status;
io->scsiio.sense_len = msg_info.scsi.sense_len;
io->scsiio.sense_residual =msg_info.scsi.sense_residual;
io->io_hdr.port_status = msg_info.scsi.fetd_status;
io->scsiio.residual = msg_info.scsi.residual;
memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
sizeof(io->scsiio.sense_data));
ctl_enqueue_isc(io);
break;
}
/* Preformed on Originating SC, SER_ONLY mode */
case CTL_MSG_R2R:
io = msg_info.hdr.original_sc;
if (io == NULL) {
printf("%s: Major Bummer\n", __func__);
return;
} else {
#if 0
printf("pOrig %x\n",(int) ctsio);
#endif
}
io->io_hdr.msg_type = CTL_MSG_R2R;
io->io_hdr.serializing_sc = msg_info.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 (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
ctl_isc_handler_finish_xfer(ctl_softc,
&msg_info);
else
ctl_isc_handler_finish_ser_only(ctl_softc,
&msg_info);
break;
/* Preformed on Originating SC */
case CTL_MSG_BAD_JUJU:
io = msg_info.hdr.original_sc;
if (io == NULL) {
printf("%s: Bad JUJU!, original_sc is NULL!\n",
__func__);
break;
}
ctl_copy_sense_data(&msg_info, 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_info.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(
(void *)ctl_softc->othersc_pool);
if (taskio == NULL) {
printf("ctl_isc_event_handler: can't allocate "
"ctl_io!\n");
/* Bad Juju */
/* should I just call the proper reset func
here??? */
goto bailout;
}
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_info.hdr.nexus;
taskio->task_action = msg_info.task.task_action;
taskio->tag_num = msg_info.task.tag_num;
taskio->tag_type = msg_info.task.tag_type;
#ifdef CTL_TIME_IO
taskio->io_hdr.start_time = time_uptime;
getbintime(&taskio->io_hdr.start_bt);
#if 0
cs_prof_gettime(&taskio->io_hdr.start_ticks);
#endif
#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(
(void *)ctl_softc->othersc_pool);
if (presio == NULL) {
printf("ctl_isc_event_handler: can't allocate "
"ctl_io!\n");
/* Bad Juju */
/* Need to set busy and send msg back */
goto bailout;
}
ctl_zero_io((union ctl_io *)presio);
presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
presio->pr_msg = msg_info.pr;
ctl_enqueue_isc((union ctl_io *)presio);
break;
case CTL_MSG_SYNC_FE:
rcv_sync_msg = 1;
break;
case CTL_MSG_APS_LOCK: {
// It's quicker to execute this then to
// queue it.
struct ctl_lun *lun;
struct ctl_page_index *page_index;
struct copan_aps_subpage *current_sp;
uint32_t targ_lun;
targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
lun = ctl_softc->ctl_luns[targ_lun];
mtx_lock(&lun->lun_lock);
page_index = &lun->mode_pages.index[index_to_aps_page];
current_sp = (struct copan_aps_subpage *)
(page_index->page_data +
(page_index->page_len * CTL_PAGE_CURRENT));
current_sp->lock_active = msg_info.aps.lock_flag;
mtx_unlock(&lun->lun_lock);
break;
}
default:
printf("How did I get here?\n");
}
} else if (event == CTL_HA_EVT_MSG_SENT) {
if (param != CTL_HA_STATUS_SUCCESS) {
printf("Bad status from ctl_ha_msg_send status %d\n",
param);
}
return;
} else if (event == CTL_HA_EVT_DISCONNECT) {
printf("CTL: Got a disconnect from Isc\n");
return;
} else {
printf("ctl_isc_event_handler: Unknown event %d\n", event);
return;
}
bailout:
return;
}
static void
ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
{
struct scsi_sense_data *sense;
sense = &dest->scsiio.sense_data;
bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
dest->scsiio.scsi_status = src->scsi.scsi_status;
dest->scsiio.sense_len = src->scsi.sense_len;
dest->io_hdr.status = src->hdr.status;
}
static int
ctl_init(void)
{
struct ctl_softc *softc;
struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
struct ctl_port *port;
uint8_t sc_id =0;
int i, error, retval;
//int isc_retval;
retval = 0;
ctl_pause_rtr = 0;
rcv_sync_msg = 0;
control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
M_WAITOK | M_ZERO);
softc = control_softc;
softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
"cam/ctl");
softc->dev->si_drv1 = softc;
/*
* By default, return a "bad LUN" peripheral qualifier for unknown
* LUNs. The user can override this default using the tunable or
* sysctl. See the comment in ctl_inquiry_std() for more details.
*/
softc->inquiry_pq_no_lun = 1;
TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
&softc->inquiry_pq_no_lun);
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);
}
SYSCTL_ADD_INT(&softc->sysctl_ctx,
SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
"inquiry_pq_no_lun", CTLFLAG_RW,
&softc->inquiry_pq_no_lun, 0,
"Report no lun possible for invalid LUNs");
mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
softc->open_count = 0;
/*
* Default to actually sending a SYNCHRONIZE CACHE command down to
* the drive.
*/
softc->flags = CTL_FLAG_REAL_SYNC;
/*
* 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.
*/
#ifdef NEEDTOPORT
scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
#endif
if (sc_id == 0) {
softc->flags |= CTL_FLAG_MASTER_SHELF;
persis_offset = 0;
} else
persis_offset = CTL_MAX_INITIATORS;
/*
* XXX KDM need to figure out where we want to get our target ID
* and WWID. Is it different on each port?
*/
softc->target.id = 0;
softc->target.wwid[0] = 0x12345678;
softc->target.wwid[1] = 0x87654321;
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);
STAILQ_INIT(&softc->io_pools);
ctl_tpc_init(softc);
if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
&internal_pool)!= 0){
printf("ctl: can't allocate %d entry internal pool, "
"exiting\n", CTL_POOL_ENTRIES_INTERNAL);
return (ENOMEM);
}
if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
printf("ctl: can't allocate %d entry emergency pool, "
"exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
ctl_pool_free(internal_pool);
return (ENOMEM);
}
if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, 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);
ctl_pool_free(internal_pool);
ctl_pool_free(emergency_pool);
return (ENOMEM);
}
softc->internal_pool = internal_pool;
softc->emergency_pool = emergency_pool;
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(internal_pool);
ctl_pool_free(emergency_pool);
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(internal_pool);
ctl_pool_free(emergency_pool);
ctl_pool_free(other_pool);
return (error);
}
if (bootverbose)
printf("ctl: CAM Target Layer loaded\n");
/*
* Initialize the ioctl front end.
*/
ctl_frontend_register(&ioctl_frontend);
port = &softc->ioctl_info.port;
port->frontend = &ioctl_frontend;
sprintf(softc->ioctl_info.port_name, "ioctl");
port->port_type = CTL_PORT_IOCTL;
port->num_requested_ctl_io = 100;
port->port_name = softc->ioctl_info.port_name;
port->port_online = ctl_ioctl_online;
port->port_offline = ctl_ioctl_offline;
port->onoff_arg = &softc->ioctl_info;
port->lun_enable = ctl_ioctl_lun_enable;
port->lun_disable = ctl_ioctl_lun_disable;
port->targ_lun_arg = &softc->ioctl_info;
port->fe_datamove = ctl_ioctl_datamove;
port->fe_done = ctl_ioctl_done;
port->max_targets = 15;
port->max_target_id = 15;
if (ctl_port_register(&softc->ioctl_info.port,
(softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
printf("ctl: ioctl front end registration failed, will "
"continue anyway\n");
}
#ifdef CTL_IO_DELAY
if (sizeof(struct callout) > CTL_TIMER_BYTES) {
printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
sizeof(struct callout), CTL_TIMER_BYTES);
return (EINVAL);
}
#endif /* CTL_IO_DELAY */
return (0);
}
void
ctl_shutdown(void)
{
struct ctl_softc *softc;
struct ctl_lun *lun, *next_lun;
struct ctl_io_pool *pool;
softc = (struct ctl_softc *)control_softc;
if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
printf("ctl: ioctl front end deregistration failed\n");
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);
ctl_frontend_deregister(&ioctl_frontend);
/*
* This will rip the rug out from under any FETDs or anyone else
* that has a pool allocated. Since we increment our module
* refcount any time someone outside the main CTL module allocates
* a pool, we shouldn't have any problems here. The user won't be
* able to unload the CTL module until client modules have
* successfully unloaded.
*/
while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
ctl_pool_free(pool);
#if 0
ctl_shutdown_thread(softc->work_thread);
mtx_destroy(&softc->queue_lock);
#endif
ctl_tpc_shutdown(softc);
mtx_destroy(&softc->pool_lock);
mtx_destroy(&softc->ctl_lock);
destroy_dev(softc->dev);
sysctl_ctx_free(&softc->sysctl_ctx);
free(control_softc, M_DEVBUF);
control_softc = NULL;
if (bootverbose)
printf("ctl: CAM Target Layer unloaded\n");
}
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);
}
int
ctl_port_enable(ctl_port_type port_type)
{
struct ctl_softc *softc;
struct ctl_port *port;
if (ctl_is_single == 0) {
union ctl_ha_msg msg_info;
int isc_retval;
#if 0
printf("%s: HA mode, synchronizing frontend enable\n",
__func__);
#endif
msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
printf("Sync msg send error retval %d\n", isc_retval);
}
if (!rcv_sync_msg) {
isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), 1);
}
#if 0
printf("CTL:Frontend Enable\n");
} else {
printf("%s: single mode, skipping frontend synchronization\n",
__func__);
#endif
}
softc = control_softc;
STAILQ_FOREACH(port, &softc->port_list, links) {
if (port_type & port->port_type)
{
#if 0
printf("port %d\n", port->targ_port);
#endif
ctl_port_online(port);
}
}
return (0);
}
int
ctl_port_disable(ctl_port_type port_type)
{
struct ctl_softc *softc;
struct ctl_port *port;
softc = control_softc;
STAILQ_FOREACH(port, &softc->port_list, links) {
if (port_type & port->port_type)
ctl_port_offline(port);
}
return (0);
}
/*
* Returns 0 for success, 1 for failure.
* Currently the only failure mode is if there aren't enough entries
* allocated. So, in case of a failure, look at num_entries_dropped,
* reallocate and try again.
*/
int
ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
int *num_entries_filled, int *num_entries_dropped,
ctl_port_type port_type, int no_virtual)
{
struct ctl_softc *softc;
struct ctl_port *port;
int entries_dropped, entries_filled;
int retval;
int i;
softc = control_softc;
retval = 0;
entries_filled = 0;
entries_dropped = 0;
i = 0;
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(port, &softc->port_list, links) {
struct ctl_port_entry *entry;
if ((port->port_type & port_type) == 0)
continue;
if ((no_virtual != 0)
&& (port->virtual_port != 0))
continue;
if (entries_filled >= num_entries_alloced) {
entries_dropped++;
continue;
}
entry = &entries[i];
entry->port_type = port->port_type;
strlcpy(entry->port_name, port->port_name,
sizeof(entry->port_name));
entry->physical_port = port->physical_port;
entry->virtual_port = port->virtual_port;
entry->wwnn = port->wwnn;
entry->wwpn = port->wwpn;
i++;
entries_filled++;
}
mtx_unlock(&softc->ctl_lock);
if (entries_dropped > 0)
retval = 1;
*num_entries_dropped = entries_dropped;
*num_entries_filled = entries_filled;
return (retval);
}
static void
ctl_ioctl_online(void *arg)
{
struct ctl_ioctl_info *ioctl_info;
ioctl_info = (struct ctl_ioctl_info *)arg;
ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
}
static void
ctl_ioctl_offline(void *arg)
{
struct ctl_ioctl_info *ioctl_info;
ioctl_info = (struct ctl_ioctl_info *)arg;
ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
}
/*
* 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 = control_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);
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 = control_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);
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));
}
}
}
static int
ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
{
return (0);
}
static int
ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
{
return (0);
}
/*
* Data movement routine for the CTL ioctl frontend port.
*/
static int
ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
{
struct ctl_sg_entry *ext_sglist, *kern_sglist;
struct ctl_sg_entry ext_entry, kern_entry;
int ext_sglen, ext_sg_entries, kern_sg_entries;
int ext_sg_start, ext_offset;
int len_to_copy, len_copied;
int kern_watermark, ext_watermark;
int ext_sglist_malloced;
int i, j;
ext_sglist_malloced = 0;
ext_sg_start = 0;
ext_offset = 0;
CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
/*
* If this flag is set, fake the data transfer.
*/
if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
ctsio->ext_data_filled = ctsio->ext_data_len;
goto bailout;
}
/*
* To simplify things here, if we have a single buffer, stick it in
* a S/G entry and just make it a single entry S/G list.
*/
if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
int len_seen;
ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
M_WAITOK);
ext_sglist_malloced = 1;
if (copyin(ctsio->ext_data_ptr, ext_sglist,
ext_sglen) != 0) {
ctl_set_internal_failure(ctsio,
/*sks_valid*/ 0,
/*retry_count*/ 0);
goto bailout;
}
ext_sg_entries = ctsio->ext_sg_entries;
len_seen = 0;
for (i = 0; i < ext_sg_entries; i++) {
if ((len_seen + ext_sglist[i].len) >=
ctsio->ext_data_filled) {
ext_sg_start = i;
ext_offset = ctsio->ext_data_filled - len_seen;
break;
}
len_seen += ext_sglist[i].len;
}
} else {
ext_sglist = &ext_entry;
ext_sglist->addr = ctsio->ext_data_ptr;
ext_sglist->len = ctsio->ext_data_len;
ext_sg_entries = 1;
ext_sg_start = 0;
ext_offset = ctsio->ext_data_filled;
}
if (ctsio->kern_sg_entries > 0) {
kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
kern_sg_entries = ctsio->kern_sg_entries;
} else {
kern_sglist = &kern_entry;
kern_sglist->addr = ctsio->kern_data_ptr;
kern_sglist->len = ctsio->kern_data_len;
kern_sg_entries = 1;
}
kern_watermark = 0;
ext_watermark = ext_offset;
len_copied = 0;
for (i = ext_sg_start, j = 0;
i < ext_sg_entries && j < kern_sg_entries;) {
uint8_t *ext_ptr, *kern_ptr;
len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
kern_sglist[j].len - kern_watermark);
ext_ptr = (uint8_t *)ext_sglist[i].addr;
ext_ptr = ext_ptr + ext_watermark;
if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
/*
* XXX KDM fix this!
*/
panic("need to implement bus address support");
#if 0
kern_ptr = bus_to_virt(kern_sglist[j].addr);
#endif
} else
kern_ptr = (uint8_t *)kern_sglist[j].addr;
kern_ptr = kern_ptr + kern_watermark;
kern_watermark += len_to_copy;
ext_watermark += len_to_copy;
if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
CTL_FLAG_DATA_IN) {
CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
"bytes to user\n", len_to_copy));
CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
"to %p\n", kern_ptr, ext_ptr));
if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
ctl_set_internal_failure(ctsio,
/*sks_valid*/ 0,
/*retry_count*/ 0);
goto bailout;
}
} else {
CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
"bytes from user\n", len_to_copy));
CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
"to %p\n", ext_ptr, kern_ptr));
if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
ctl_set_internal_failure(ctsio,
/*sks_valid*/ 0,
/*retry_count*/0);
goto bailout;
}
}
len_copied += len_to_copy;
if (ext_sglist[i].len == ext_watermark) {
i++;
ext_watermark = 0;
}
if (kern_sglist[j].len == kern_watermark) {
j++;
kern_watermark = 0;
}
}
ctsio->ext_data_filled += len_copied;
CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
"kern_sg_entries: %d\n", ext_sg_entries,
kern_sg_entries));
CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
"kern_data_len = %d\n", ctsio->ext_data_len,
ctsio->kern_data_len));
/* XXX KDM set residual?? */
bailout:
if (ext_sglist_malloced != 0)
free(ext_sglist, M_CTL);
return (CTL_RETVAL_COMPLETE);
}
/*
* 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 *ctl_softc;
union ctl_ha_msg msg_info;
struct ctl_lun *lun;
int retval = 0;
uint32_t targ_lun;
ctl_softc = control_softc;
targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
lun = ctl_softc->ctl_luns[targ_lun];
if (lun==NULL)
{
/*
* Why isn't LUN defined? The other side wouldn't
* send a cmd if the LUN is undefined.
*/
printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
/* "Logical unit not supported" */
ctl_set_sense_data(&msg_info.scsi.sense_data,
lun,
/*sense_format*/SSD_TYPE_NONE,
/*current_error*/ 1,
/*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
/*asc*/ 0x25,
/*ascq*/ 0x00,
SSD_ELEM_NONE);
msg_info.scsi.sense_len = SSD_FULL_SIZE;
msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
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;
if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
}
return(1);
}
mtx_lock(&lun->lun_lock);
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);
break;
case CTL_ACTION_PASS:
case CTL_ACTION_SKIP:
if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
ctl_enqueue_rtr((union ctl_io *)ctsio);
} else {
/* 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;
#if 0
printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
#endif
if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
}
}
break;
case CTL_ACTION_OVERLAP:
/* OVERLAPPED COMMANDS ATTEMPTED */
ctl_set_sense_data(&msg_info.scsi.sense_data,
lun,
/*sense_format*/SSD_TYPE_NONE,
/*current_error*/ 1,
/*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
/*asc*/ 0x4E,
/*ascq*/ 0x00,
SSD_ELEM_NONE);
msg_info.scsi.sense_len = SSD_FULL_SIZE;
msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
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;
#if 0
printf("BAD JUJU:Major Bummer Overlap\n");
#endif
TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
retval = 1;
if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
}
break;
case CTL_ACTION_OVERLAP_TAG:
/* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
ctl_set_sense_data(&msg_info.scsi.sense_data,
lun,
/*sense_format*/SSD_TYPE_NONE,
/*current_error*/ 1,
/*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
/*asc*/ 0x4D,
/*ascq*/ ctsio->tag_num & 0xff,
SSD_ELEM_NONE);
msg_info.scsi.sense_len = SSD_FULL_SIZE;
msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
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;
#if 0
printf("BAD JUJU:Major Bummer Overlap Tag\n");
#endif
TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
retval = 1;
if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
}
break;
case CTL_ACTION_ERROR:
default:
/* "Internal target failure" */
ctl_set_sense_data(&msg_info.scsi.sense_data,
lun,
/*sense_format*/SSD_TYPE_NONE,
/*current_error*/ 1,
/*sense_key*/ SSD_KEY_HARDWARE_ERROR,
/*asc*/ 0x44,
/*ascq*/ 0x00,
SSD_ELEM_NONE);
msg_info.scsi.sense_len = SSD_FULL_SIZE;
msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
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;
#if 0
printf("BAD JUJU:Major Bummer HW Error\n");
#endif
TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
retval = 1;
if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
}
break;
}
mtx_unlock(&lun->lun_lock);
return (retval);
}
static int
ctl_ioctl_submit_wait(union ctl_io *io)
{
struct ctl_fe_ioctl_params params;
ctl_fe_ioctl_state last_state;
int done, retval;
retval = 0;
bzero(¶ms, sizeof(params));
mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
cv_init(¶ms.sem, "ctlioccv");
params.state = CTL_IOCTL_INPROG;
last_state = params.state;
io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
/* This shouldn't happen */
if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
return (retval);
done = 0;
do {
mtx_lock(¶ms.ioctl_mtx);
/*
* Check the state here, and don't sleep if the state has
* already changed (i.e. wakeup has already occured, but we
* weren't waiting yet).
*/
if (params.state == last_state) {
/* XXX KDM cv_wait_sig instead? */
cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
}
last_state = params.state;
switch (params.state) {
case CTL_IOCTL_INPROG:
/* Why did we wake up? */
/* XXX KDM error here? */
mtx_unlock(¶ms.ioctl_mtx);
break;
case CTL_IOCTL_DATAMOVE:
CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
/*
* change last_state back to INPROG to avoid
* deadlock on subsequent data moves.
*/
params.state = last_state = CTL_IOCTL_INPROG;
mtx_unlock(¶ms.ioctl_mtx);
ctl_ioctl_do_datamove(&io->scsiio);
/*
* Note that in some cases, most notably writes,
* this will queue the I/O and call us back later.
* In other cases, generally reads, this routine
* will immediately call back and wake us up,
* probably using our own context.
*/
io->scsiio.be_move_done(io);
break;
case CTL_IOCTL_DONE:
mtx_unlock(¶ms.ioctl_mtx);
CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
done = 1;
break;
default:
mtx_unlock(¶ms.ioctl_mtx);
/* XXX KDM error here? */
break;
}
} while (done == 0);
mtx_destroy(¶ms.ioctl_mtx);
cv_destroy(¶ms.sem);
return (CTL_RETVAL_COMPLETE);
}
static void
ctl_ioctl_datamove(union ctl_io *io)
{
struct ctl_fe_ioctl_params *params;
params = (struct ctl_fe_ioctl_params *)
io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
mtx_lock(¶ms->ioctl_mtx);
params->state = CTL_IOCTL_DATAMOVE;
cv_broadcast(¶ms->sem);
mtx_unlock(¶ms->ioctl_mtx);
}
static void
ctl_ioctl_done(union ctl_io *io)
{
struct ctl_fe_ioctl_params *params;
params = (struct ctl_fe_ioctl_params *)
io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
mtx_lock(¶ms->ioctl_mtx);
params->state = CTL_IOCTL_DONE;
cv_broadcast(¶ms->sem);
mtx_unlock(¶ms->ioctl_mtx);
}
static void
ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
{
struct ctl_fe_ioctl_startstop_info *sd_info;
sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
sd_info->hs_info.status = metatask->status;
sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
sd_info->hs_info.luns_complete =
metatask->taskinfo.startstop.luns_complete;
sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
cv_broadcast(&sd_info->sem);
}
static void
ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
{
struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
mtx_lock(fe_bbr_info->lock);
fe_bbr_info->bbr_info->status = metatask->status;
fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
fe_bbr_info->wakeup_done = 1;
mtx_unlock(fe_bbr_info->lock);
cv_broadcast(&fe_bbr_info->sem);
}
/*
* Returns 0 for success, errno for failure.
*/
static int
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;
int retval;
retval = 0;
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);
return (retval);
}
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 retval;
retval = 0;
for (; *str; 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 int
ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
struct thread *td)
{
struct ctl_softc *softc;
int retval;
softc = control_softc;
retval = 0;
switch (cmd) {
case CTL_IO: {
union ctl_io *io;
void *pool_tmp;
/*
* If we haven't been "enabled", don't allow any SCSI I/O
* to this FETD.
*/
if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
retval = EPERM;
break;
}
io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
if (io == NULL) {
printf("ctl_ioctl: can't allocate ctl_io!\n");
retval = ENOSPC;
break;
}
/*
* Need to save the pool reference so it doesn't get
* spammed by the user's ctl_io.
*/
pool_tmp = io->io_hdr.pool;
memcpy(io, (void *)addr, sizeof(*io));
io->io_hdr.pool = pool_tmp;
/*
* No status yet, so make sure the status is set properly.
*/
io->io_hdr.status = CTL_STATUS_NONE;
/*
* The user sets the initiator ID, target and LUN IDs.
*/
io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
io->io_hdr.flags |= CTL_FLAG_USER_REQ;
if ((io->io_hdr.io_type == CTL_IO_SCSI)
&& (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
retval = ctl_ioctl_submit_wait(io);
if (retval != 0) {
ctl_free_io(io);
break;
}
memcpy((void *)addr, io, sizeof(*io));
/* return this to our pool */
ctl_free_io(io);
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;
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) {
/*
* 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,
*/
mtx_unlock(&softc->ctl_lock);
if (cmd == CTL_ENABLE_PORT) {
struct ctl_lun *lun;
STAILQ_FOREACH(lun, &softc->lun_list,
links) {
port->lun_enable(port->targ_lun_arg,
lun->target,
lun->lun);
}
ctl_port_online(port);
} else if (cmd == CTL_DISABLE_PORT) {
struct ctl_lun *lun;
ctl_port_offline(port);
STAILQ_FOREACH(lun, &softc->lun_list,
links) {
port->lun_disable(
port->targ_lun_arg,
lun->target,
lun->lun);
}
}
mtx_lock(&softc->ctl_lock);
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: {
struct ctl_lun *lun;
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_lun *lun;
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) {
retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
ooa_hdr, entries);
if (retval != 0)
break;
}
if (retval != 0) {
mtx_unlock(&softc->ctl_lock);
free(entries, M_CTL);
break;
}
} else {
lun = softc->ctl_luns[ooa_hdr->lun_num];
retval = 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);
}
getbintime(&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_lun *lun;
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_HARD_START:
case CTL_HARD_STOP: {
struct ctl_fe_ioctl_startstop_info ss_info;
struct cfi_metatask *metatask;
struct mtx hs_mtx;
mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
cv_init(&ss_info.sem, "hard start/stop cv" );
metatask = cfi_alloc_metatask(/*can_wait*/ 1);
if (metatask == NULL) {
retval = ENOMEM;
mtx_destroy(&hs_mtx);
break;
}
if (cmd == CTL_HARD_START)
metatask->tasktype = CFI_TASK_STARTUP;
else
metatask->tasktype = CFI_TASK_SHUTDOWN;
metatask->callback = ctl_ioctl_hard_startstop_callback;
metatask->callback_arg = &ss_info;
cfi_action(metatask);
/* Wait for the callback */
mtx_lock(&hs_mtx);
cv_wait_sig(&ss_info.sem, &hs_mtx);
mtx_unlock(&hs_mtx);
/*
* All information has been copied from the metatask by the
* time cv_broadcast() is called, so we free the metatask here.
*/
cfi_free_metatask(metatask);
memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
mtx_destroy(&hs_mtx);
break;
}
case CTL_BBRREAD: {
struct ctl_bbrread_info *bbr_info;
struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
struct mtx bbr_mtx;
struct cfi_metatask *metatask;
bbr_info = (struct ctl_bbrread_info *)addr;
bzero(&fe_bbr_info, sizeof(fe_bbr_info));
bzero(&bbr_mtx, sizeof(bbr_mtx));
mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
fe_bbr_info.bbr_info = bbr_info;
fe_bbr_info.lock = &bbr_mtx;
cv_init(&fe_bbr_info.sem, "BBR read cv");
metatask = cfi_alloc_metatask(/*can_wait*/ 1);
if (metatask == NULL) {
mtx_destroy(&bbr_mtx);
cv_destroy(&fe_bbr_info.sem);
retval = ENOMEM;
break;
}
metatask->tasktype = CFI_TASK_BBRREAD;
metatask->callback = ctl_ioctl_bbrread_callback;
metatask->callback_arg = &fe_bbr_info;
metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
metatask->taskinfo.bbrread.lba = bbr_info->lba;
metatask->taskinfo.bbrread.len = bbr_info->len;
cfi_action(metatask);
mtx_lock(&bbr_mtx);
while (fe_bbr_info.wakeup_done == 0)
cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
mtx_unlock(&bbr_mtx);
bbr_info->status = metatask->status;
bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
memcpy(&bbr_info->sense_data,
&metatask->taskinfo.bbrread.sense_data,
ctl_min(sizeof(bbr_info->sense_data),
sizeof(metatask->taskinfo.bbrread.sense_data)));
cfi_free_metatask(metatask);
mtx_destroy(&bbr_mtx);
cv_destroy(&fe_bbr_info.sem);
break;
}
case CTL_DELAY_IO: {
struct ctl_io_delay_info *delay_info;
#ifdef CTL_IO_DELAY
struct ctl_lun *lun;
#endif /* CTL_IO_DELAY */
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;
struct ctl_lun *lun;
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;
}
/*
* 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;
struct ctl_lun *lun;
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;
struct ctl_lun *lun;
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;
struct ctl_lun *lun;
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, idx;
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++) {
struct ctl_lun *lun;
lun = softc->ctl_luns[i];
if ((lun == NULL)
|| ((lun->flags & CTL_LUN_DISABLED) != 0))
continue;
for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
idx = j * CTL_MAX_INIT_PER_PORT + k;
if (lun->per_res[idx].registered == 0)
continue;
printf(" LUN %d port %d iid %d key "
"%#jx\n", i, j, k,
(uintmax_t)scsi_8btou64(
lun->per_res[idx].res_key.key));
}
}
}
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 *lun;
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);
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);
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, %d bytes is too small",
list->alloc_len);
break;
}
sbuf_finish(sb);
retval = copyout(sbuf_data(sb), list->lun_xml,
sbuf_len(sb) + 1);
list->fill_len = sbuf_len(sb) + 1;
list->status = CTL_LUN_LIST_OK;
sbuf_delete(sb);
break;
}
case CTL_ISCSI: {
struct ctl_iscsi *ci;
struct ctl_frontend *fe;
ci = (struct ctl_iscsi *)addr;
fe = ctl_frontend_find("iscsi");
if (fe == NULL) {
ci->status = CTL_ISCSI_ERROR;
snprintf(ci->error_str, sizeof(ci->error_str),
"Frontend \"iscsi\" not found.");
break;
}
retval = fe->ioctl(dev, cmd, addr, flag, td);
break;
}
case CTL_PORT_REQ: {
struct ctl_req *req;
struct ctl_frontend *fe;
req = (struct ctl_req *)addr;
fe = ctl_frontend_find(req->driver);
if (fe == NULL) {
req->status = CTL_LUN_ERROR;
snprintf(req->error_str, sizeof(req->error_str),
"Frontend \"%s\" not found.", req->driver);
break;
}
if (req->num_args > 0) {
req->kern_args = ctl_copyin_args(req->num_args,
req->args, req->error_str, sizeof(req->error_str));
if (req->kern_args == NULL) {
req->status = CTL_LUN_ERROR;
break;
}
}
retval = fe->ioctl(dev, cmd, addr, flag, td);
if (req->num_args > 0) {
ctl_copyout_args(req->num_args, req->kern_args);
ctl_free_args(req->num_args, req->kern_args);
}
break;
}
case CTL_PORT_LIST: {
struct sbuf *sb;
struct ctl_port *port;
struct ctl_lun_list *list;
struct ctl_option *opt;
list = (struct ctl_lun_list *)addr;
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, "<ctlportlist>\n");
mtx_lock(&softc->ctl_lock);
STAILQ_FOREACH(port, &softc->port_list, links) {
retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
(uintmax_t)port->targ_port);
/*
* Bail out as soon as we see that we've overfilled
* the buffer.
*/
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<frontend_type>%s"
"</frontend_type>\n", port->frontend->name);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
port->port_type);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<online>%s</online>\n",
(port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
port->port_name);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
port->physical_port);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
port->virtual_port);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n",
(uintmax_t)port->wwnn);
if (retval != 0)
break;
retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n",
(uintmax_t)port->wwpn);
if (retval != 0)
break;
if (port->port_info != NULL) {
retval = port->port_info(port->onoff_arg, sb);
if (retval != 0)
break;
}
STAILQ_FOREACH(opt, &port->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, "</targ_port>\n");
if (retval != 0)
break;
}
mtx_unlock(&softc->ctl_lock);
if ((retval != 0)
|| ((retval = sbuf_printf(sb, "</ctlportlist>\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, %d bytes is too small",
list->alloc_len);
break;
}
sbuf_finish(sb);
retval = copyout(sbuf_data(sb), list->lun_xml,
|