ctl.c revision 268682
1204076Spjd/*- 2204076Spjd * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3219351Spjd * Copyright (c) 2012 The FreeBSD Foundation 4204076Spjd * All rights reserved. 5204076Spjd * 6204076Spjd * Portions of this software were developed by Edward Tomasz Napierala 7204076Spjd * under sponsorship from the FreeBSD Foundation. 8204076Spjd * 9204076Spjd * Redistribution and use in source and binary forms, with or without 10204076Spjd * modification, are permitted provided that the following conditions 11204076Spjd * are met: 12204076Spjd * 1. Redistributions of source code must retain the above copyright 13204076Spjd * notice, this list of conditions, and the following disclaimer, 14204076Spjd * without modification. 15204076Spjd * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16204076Spjd * substantially similar to the "NO WARRANTY" disclaimer below 17204076Spjd * ("Disclaimer") and any redistribution must be conditioned upon 18204076Spjd * including a substantially similar Disclaimer requirement for further 19204076Spjd * binary redistribution. 20204076Spjd * 21204076Spjd * NO WARRANTY 22204076Spjd * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23204076Spjd * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24204076Spjd * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25204076Spjd * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26204076Spjd * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27204076Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28204076Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29204076Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30204076Spjd * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31204076Spjd * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32204076Spjd * POSSIBILITY OF SUCH DAMAGES. 33204076Spjd * 34204076Spjd * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35204076Spjd */ 36204076Spjd/* 37218044Spjd * CAM Target Layer, a SCSI device emulation subsystem. 38204076Spjd * 39204076Spjd * Author: Ken Merry <ken@FreeBSD.org> 40204076Spjd */ 41204076Spjd 42204076Spjd#define _CTL_C 43204076Spjd 44204076Spjd#include <sys/cdefs.h> 45204076Spjd__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 268682 2014-07-15 17:05:11Z mav $"); 46204076Spjd 47204076Spjd#include <sys/param.h> 48204076Spjd#include <sys/systm.h> 49219813Spjd#include <sys/kernel.h> 50204076Spjd#include <sys/types.h> 51204076Spjd#include <sys/kthread.h> 52204076Spjd#include <sys/bio.h> 53204076Spjd#include <sys/fcntl.h> 54204076Spjd#include <sys/lock.h> 55204076Spjd#include <sys/module.h> 56212038Spjd#include <sys/mutex.h> 57204076Spjd#include <sys/condvar.h> 58204076Spjd#include <sys/malloc.h> 59204076Spjd#include <sys/conf.h> 60211977Spjd#include <sys/ioccom.h> 61204076Spjd#include <sys/queue.h> 62204076Spjd#include <sys/sbuf.h> 63204076Spjd#include <sys/smp.h> 64210886Spjd#include <sys/endian.h> 65204076Spjd#include <sys/sysctl.h> 66204076Spjd 67204076Spjd#include <cam/cam.h> 68204076Spjd#include <cam/scsi/scsi_all.h> 69233679Strociny#include <cam/scsi/scsi_da.h> 70233679Strociny#include <cam/ctl/ctl_io.h> 71246922Spjd#include <cam/ctl/ctl.h> 72204076Spjd#include <cam/ctl/ctl_frontend.h> 73229944Spjd#include <cam/ctl/ctl_frontend_internal.h> 74229944Spjd#include <cam/ctl/ctl_util.h> 75204076Spjd#include <cam/ctl/ctl_backend.h> 76211977Spjd#include <cam/ctl/ctl_ioctl.h> 77213430Spjd#include <cam/ctl/ctl_ha.h> 78211977Spjd#include <cam/ctl/ctl_private.h> 79204076Spjd#include <cam/ctl/ctl_debug.h> 80204076Spjd#include <cam/ctl/ctl_scsi_all.h> 81204076Spjd#include <cam/ctl/ctl_error.h> 82204076Spjd 83204076Spjdstruct ctl_softc *control_softc = NULL; 84204076Spjd 85204076Spjd/* 86204076Spjd * Size and alignment macros needed for Copan-specific HA hardware. These 87204076Spjd * can go away when the HA code is re-written, and uses busdma for any 88204076Spjd * hardware. 89204076Spjd */ 90204076Spjd#define CTL_ALIGN_8B(target, source, type) \ 91204076Spjd if (((uint32_t)source & 0x7) != 0) \ 92204076Spjd target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93204076Spjd else \ 94204076Spjd target = (type)source; 95204076Spjd 96204076Spjd#define CTL_SIZE_8B(target, size) \ 97204076Spjd if ((size & 0x7) != 0) \ 98204076Spjd target = size + (0x8 - (size & 0x7)); \ 99204076Spjd else \ 100204076Spjd target = size; 101218041Spjd 102218041Spjd#define CTL_ALIGN_8B_MARGIN 16 103218041Spjd 104230457Spjd/* 105222108Spjd * Template mode pages. 106218041Spjd */ 107230457Spjd 108218041Spjd/* 109218041Spjd * Note that these are default values only. The actual values will be 110218041Spjd * filled in when the user does a mode sense. 111218041Spjd */ 112218041Spjdstatic struct copan_power_subpage power_page_default = { 113218041Spjd /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 114218041Spjd /*subpage*/ PWR_SUBPAGE_CODE, 115218041Spjd /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 116218041Spjd (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 117218041Spjd /*page_version*/ PWR_VERSION, 118218370Spjd /* total_luns */ 26, 119218370Spjd /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 120218370Spjd /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 121218370Spjd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122218370Spjd 0, 0, 0, 0, 0, 0} 123218370Spjd}; 124230457Spjd 125230457Spjdstatic struct copan_power_subpage power_page_changeable = { 126218041Spjd /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 127218041Spjd /*subpage*/ PWR_SUBPAGE_CODE, 128218041Spjd /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 129218041Spjd (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 130230457Spjd /*page_version*/ 0, 131230457Spjd /* total_luns */ 0, 132222108Spjd /* max_active_luns*/ 0, 133222108Spjd /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 134230457Spjd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135222108Spjd 0, 0, 0, 0, 0, 0} 136218041Spjd}; 137218041Spjd 138218041Spjdstatic struct copan_aps_subpage aps_page_default = { 139218041Spjd APS_PAGE_CODE | SMPH_SPF, //page_code 140218041Spjd APS_SUBPAGE_CODE, //subpage 141218044Spjd {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 142218044Spjd (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 143218044Spjd APS_VERSION, //page_version 144218044Spjd 0, //lock_active 145218044Spjd {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146218044Spjd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147219864Spjd 0, 0, 0, 0, 0} //reserved 148218044Spjd}; 149219864Spjd 150218044Spjdstatic struct copan_aps_subpage aps_page_changeable = { 151218044Spjd APS_PAGE_CODE | SMPH_SPF, //page_code 152218044Spjd APS_SUBPAGE_CODE, //subpage 153219864Spjd {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 154218044Spjd (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 155219864Spjd 0, //page_version 156218044Spjd 0, //lock_active 157218044Spjd {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158218044Spjd 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159219864Spjd 0, 0, 0, 0, 0} //reserved 160218044Spjd}; 161218044Spjd 162218044Spjdstatic struct copan_debugconf_subpage debugconf_page_default = { 163218044Spjd DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 164218044Spjd DBGCNF_SUBPAGE_CODE, /* subpage */ 165218044Spjd {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 166218044Spjd (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 167218044Spjd DBGCNF_VERSION, /* page_version */ 168218044Spjd {CTL_TIME_IO_DEFAULT_SECS>>8, 169218044Spjd CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 170218044Spjd}; 171218044Spjd 172218044Spjdstatic struct copan_debugconf_subpage debugconf_page_changeable = { 173218044Spjd DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 174218044Spjd DBGCNF_SUBPAGE_CODE, /* subpage */ 175218044Spjd {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 176218044Spjd (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 177218044Spjd 0, /* page_version */ 178218044Spjd {0xff,0xff}, /* ctl_time_io_secs */ 179218044Spjd}; 180218044Spjd 181218044Spjdstatic struct scsi_format_page format_page_default = { 182218044Spjd /*page_code*/SMS_FORMAT_DEVICE_PAGE, 183229945Spjd /*page_length*/sizeof(struct scsi_format_page) - 2, 184218373Spjd /*tracks_per_zone*/ {0, 0}, 185218373Spjd /*alt_sectors_per_zone*/ {0, 0}, 186218373Spjd /*alt_tracks_per_zone*/ {0, 0}, 187218044Spjd /*alt_tracks_per_lun*/ {0, 0}, 188218373Spjd /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 189218044Spjd CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 190218044Spjd /*bytes_per_sector*/ {0, 0}, 191218044Spjd /*interleave*/ {0, 0}, 192218044Spjd /*track_skew*/ {0, 0}, 193218044Spjd /*cylinder_skew*/ {0, 0}, 194218044Spjd /*flags*/ SFP_HSEC, 195218044Spjd /*reserved*/ {0, 0, 0} 196218044Spjd}; 197218044Spjd 198218044Spjdstatic struct scsi_format_page format_page_changeable = { 199218375Spjd /*page_code*/SMS_FORMAT_DEVICE_PAGE, 200218044Spjd /*page_length*/sizeof(struct scsi_format_page) - 2, 201218044Spjd /*tracks_per_zone*/ {0, 0}, 202218374Spjd /*alt_sectors_per_zone*/ {0, 0}, 203218044Spjd /*alt_tracks_per_zone*/ {0, 0}, 204218044Spjd /*alt_tracks_per_lun*/ {0, 0}, 205218044Spjd /*sectors_per_track*/ {0, 0}, 206218044Spjd /*bytes_per_sector*/ {0, 0}, 207218375Spjd /*interleave*/ {0, 0}, 208218044Spjd /*track_skew*/ {0, 0}, 209218044Spjd /*cylinder_skew*/ {0, 0}, 210218044Spjd /*flags*/ 0, 211218044Spjd /*reserved*/ {0, 0, 0} 212218044Spjd}; 213218044Spjd 214218044Spjdstatic struct scsi_rigid_disk_page rigid_disk_page_default = { 215218375Spjd /*page_code*/SMS_RIGID_DISK_PAGE, 216218044Spjd /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 217218044Spjd /*cylinders*/ {0, 0, 0}, 218218044Spjd /*heads*/ CTL_DEFAULT_HEADS, 219218044Spjd /*start_write_precomp*/ {0, 0, 0}, 220218044Spjd /*start_reduced_current*/ {0, 0, 0}, 221218375Spjd /*step_rate*/ {0, 0}, 222218044Spjd /*landing_zone_cylinder*/ {0, 0, 0}, 223218044Spjd /*rpl*/ SRDP_RPL_DISABLED, 224218044Spjd /*rotational_offset*/ 0, 225218044Spjd /*reserved1*/ 0, 226218044Spjd /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 227218044Spjd CTL_DEFAULT_ROTATION_RATE & 0xff}, 228218375Spjd /*reserved2*/ {0, 0} 229218044Spjd}; 230218044Spjd 231218044Spjdstatic struct scsi_rigid_disk_page rigid_disk_page_changeable = { 232218044Spjd /*page_code*/SMS_RIGID_DISK_PAGE, 233218044Spjd /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 234218375Spjd /*cylinders*/ {0, 0, 0}, 235218044Spjd /*heads*/ 0, 236218044Spjd /*start_write_precomp*/ {0, 0, 0}, 237218044Spjd /*start_reduced_current*/ {0, 0, 0}, 238218044Spjd /*step_rate*/ {0, 0}, 239219900Spjd /*landing_zone_cylinder*/ {0, 0, 0}, 240219900Spjd /*rpl*/ 0, 241218218Spjd /*rotational_offset*/ 0, 242218375Spjd /*reserved1*/ 0, 243218218Spjd /*rotation_rate*/ {0, 0}, 244218218Spjd /*reserved2*/ {0, 0} 245218218Spjd}; 246218218Spjd 247218218Spjdstatic struct scsi_caching_page caching_page_default = { 248218375Spjd /*page_code*/SMS_CACHING_PAGE, 249218218Spjd /*page_length*/sizeof(struct scsi_caching_page) - 2, 250218218Spjd /*flags1*/ SCP_DISC | SCP_WCE, 251218218Spjd /*ret_priority*/ 0, 252218218Spjd /*disable_pf_transfer_len*/ {0xff, 0xff}, 253218044Spjd /*min_prefetch*/ {0, 0}, 254219900Spjd /*max_prefetch*/ {0xff, 0xff}, 255219900Spjd /*max_pf_ceiling*/ {0xff, 0xff}, 256219900Spjd /*flags2*/ 0, 257219900Spjd /*cache_segments*/ 0, 258219900Spjd /*cache_seg_size*/ {0, 0}, 259219900Spjd /*reserved*/ 0, 260219900Spjd /*non_cache_seg_size*/ {0, 0, 0} 261219900Spjd}; 262219900Spjd 263218044Spjdstatic struct scsi_caching_page caching_page_changeable = { 264218044Spjd /*page_code*/SMS_CACHING_PAGE, 265218375Spjd /*page_length*/sizeof(struct scsi_caching_page) - 2, 266218044Spjd /*flags1*/ 0, 267218044Spjd /*ret_priority*/ 0, 268218044Spjd /*disable_pf_transfer_len*/ {0, 0}, 269218044Spjd /*min_prefetch*/ {0, 0}, 270218044Spjd /*max_prefetch*/ {0, 0}, 271218375Spjd /*max_pf_ceiling*/ {0, 0}, 272218044Spjd /*flags2*/ 0, 273218044Spjd /*cache_segments*/ 0, 274218044Spjd /*cache_seg_size*/ {0, 0}, 275218044Spjd /*reserved*/ 0, 276218044Spjd /*non_cache_seg_size*/ {0, 0, 0} 277218044Spjd}; 278218044Spjd 279218375Spjdstatic struct scsi_control_page control_page_default = { 280218044Spjd /*page_code*/SMS_CONTROL_MODE_PAGE, 281218044Spjd /*page_length*/sizeof(struct scsi_control_page) - 2, 282218044Spjd /*rlec*/0, 283218044Spjd /*queue_flags*/0, 284218044Spjd /*eca_and_aen*/0, 285218375Spjd /*reserved*/0, 286218044Spjd /*aen_holdoff_period*/{0, 0} 287218044Spjd}; 288218044Spjd 289218044Spjdstatic struct scsi_control_page control_page_changeable = { 290218044Spjd /*page_code*/SMS_CONTROL_MODE_PAGE, 291218044Spjd /*page_length*/sizeof(struct scsi_control_page) - 2, 292218375Spjd /*rlec*/SCP_DSENSE, 293218044Spjd /*queue_flags*/0, 294218044Spjd /*eca_and_aen*/0, 295218044Spjd /*reserved*/0, 296218044Spjd /*aen_holdoff_period*/{0, 0} 297218044Spjd}; 298218044Spjd 299218044Spjd 300218044Spjd/* 301218044Spjd * XXX KDM move these into the softc. 302218044Spjd */ 303218044Spjdstatic int rcv_sync_msg; 304218044Spjdstatic int persis_offset; 305218044Spjdstatic uint8_t ctl_pause_rtr; 306218044Spjdstatic int ctl_is_single = 1; 307204076Spjdstatic int index_to_aps_page; 308207372Spjd 309207372SpjdSYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 310207372Spjdstatic int worker_threads = -1; 311207372SpjdTUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 312207372SpjdSYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 313207372Spjd &worker_threads, 1, "Number of worker threads"); 314207372Spjdstatic int verbose = 0; 315207372SpjdTUNABLE_INT("kern.cam.ctl.verbose", &verbose); 316207372SpjdSYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 317207372Spjd &verbose, 0, "Show SCSI errors returned to initiator"); 318207372Spjd 319207372Spjd/* 320207372Spjd * Serial number (0x80), device id (0x83), supported pages (0x00), 321207372Spjd * Block limits (0xB0) and Logical Block Provisioning (0xB2) 322207372Spjd */ 323207372Spjd#define SCSI_EVPD_NUM_SUPPORTED_PAGES 5 324204076Spjd 325204076Spjdstatic void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 326204076Spjd int param); 327204076Spjdstatic void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 328204076Spjdstatic int ctl_init(void); 329204076Spjdvoid ctl_shutdown(void); 330204076Spjdstatic int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 331204076Spjdstatic int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 332204076Spjdstatic void ctl_ioctl_online(void *arg); 333204076Spjdstatic void ctl_ioctl_offline(void *arg); 334204076Spjdstatic int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 335204076Spjdstatic int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 336204076Spjdstatic int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 337204076Spjdstatic int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 338204076Spjdstatic int ctl_ioctl_submit_wait(union ctl_io *io); 339211977Spjdstatic void ctl_ioctl_datamove(union ctl_io *io); 340211977Spjdstatic void ctl_ioctl_done(union ctl_io *io); 341204076Spjdstatic void ctl_ioctl_hard_startstop_callback(void *arg, 342211977Spjd struct cfi_metatask *metatask); 343204076Spjdstatic void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 344204076Spjdstatic int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 345204076Spjd struct ctl_ooa *ooa_hdr, 346204076Spjd struct ctl_ooa_entry *kern_entries); 347207372Spjdstatic int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 348213006Spjd struct thread *td); 349204076Spjduint32_t ctl_get_resindex(struct ctl_nexus *nexus); 350207372Spjduint32_t ctl_port_idx(int port_num); 351207372Spjd#ifdef unused 352207372Spjdstatic union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 353207372Spjd uint32_t targ_target, uint32_t targ_lun, 354207372Spjd int can_wait); 355207372Spjdstatic void ctl_kfree_io(union ctl_io *io); 356207372Spjd#endif /* unused */ 357207348Spjdstatic int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 358207348Spjd struct ctl_be_lun *be_lun, struct ctl_id target_id); 359207348Spjdstatic int ctl_free_lun(struct ctl_lun *lun); 360207348Spjdstatic void ctl_create_lun(struct ctl_be_lun *be_lun); 361207348Spjd/** 362207348Spjdstatic void ctl_failover_change_pages(struct ctl_softc *softc, 363207348Spjd struct ctl_scsiio *ctsio, int master); 364207348Spjd**/ 365204076Spjd 366204076Spjdstatic int ctl_do_mode_select(union ctl_io *io); 367204076Spjdstatic int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 368204076Spjd uint64_t res_key, uint64_t sa_res_key, 369204076Spjd uint8_t type, uint32_t residx, 370210886Spjd struct ctl_scsiio *ctsio, 371210886Spjd struct scsi_per_res_out *cdb, 372210886Spjd struct scsi_per_res_out_parms* param); 373210886Spjdstatic void ctl_pro_preempt_other(struct ctl_lun *lun, 374210886Spjd union ctl_ha_msg *msg); 375218138Spjdstatic void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 376210886Spjdstatic int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 377210886Spjdstatic int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 378210886Spjdstatic int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 379210886Spjdstatic int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 380210886Spjd int alloc_len); 381210886Spjdstatic int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 382210886Spjdstatic int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 383210886Spjdstatic int ctl_inquiry_std(struct ctl_scsiio *ctsio); 384210886Spjdstatic int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 385219818Spjdstatic ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 386219818Spjdstatic ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 387210886Spjd union ctl_io *ooa_io); 388210886Spjdstatic ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 389219351Spjd union ctl_io *starting_io); 390219351Spjdstatic int ctl_check_blocked(struct ctl_lun *lun); 391219354Spjdstatic int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 392219354Spjd struct ctl_lun *lun, 393210886Spjd const struct ctl_cmd_entry *entry, 394210886Spjd struct ctl_scsiio *ctsio); 395211886Spjd//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 396211886Spjdstatic void ctl_failover(void); 397225830Spjdstatic int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 398225830Spjd struct ctl_scsiio *ctsio); 399225830Spjdstatic int ctl_scsiio(struct ctl_scsiio *ctsio); 400225830Spjd 401225830Spjdstatic int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 402225830Spjdstatic int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 403210886Spjd ctl_ua_type ua_type); 404210886Spjdstatic int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 405210886Spjd ctl_ua_type ua_type); 406210886Spjdstatic int ctl_abort_task(union ctl_io *io); 407210886Spjdstatic void ctl_run_task(union ctl_io *io); 408210886Spjd#ifdef CTL_IO_DELAY 409210886Spjdstatic void ctl_datamove_timer_wakeup(void *arg); 410210886Spjdstatic void ctl_done_timer_wakeup(void *arg); 411210886Spjd#endif /* CTL_IO_DELAY */ 412218138Spjd 413218138Spjdstatic void ctl_send_datamove_done(union ctl_io *io, int have_lock); 414218138Spjdstatic void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 415210886Spjdstatic int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 416210886Spjdstatic void ctl_datamove_remote_write(union ctl_io *io); 417210886Spjdstatic int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 418210886Spjdstatic void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 419210886Spjdstatic int ctl_datamove_remote_sgl_setup(union ctl_io *io); 420210886Spjdstatic int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 421219818Spjd ctl_ha_dt_cb callback); 422219818Spjdstatic void ctl_datamove_remote_read(union ctl_io *io); 423210886Spjdstatic void ctl_datamove_remote(union ctl_io *io); 424210886Spjdstatic int ctl_process_done(union ctl_io *io); 425219351Spjdstatic void ctl_lun_thread(void *arg); 426219351Spjdstatic void ctl_work_thread(void *arg); 427219354Spjdstatic void ctl_enqueue_incoming(union ctl_io *io); 428219354Spjdstatic void ctl_enqueue_rtr(union ctl_io *io); 429210886Spjdstatic void ctl_enqueue_done(union ctl_io *io); 430210886Spjdstatic void ctl_enqueue_isc(union ctl_io *io); 431211886Spjdstatic const struct ctl_cmd_entry * 432211886Spjd ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 433225830Spjdstatic const struct ctl_cmd_entry * 434225830Spjd ctl_validate_command(struct ctl_scsiio *ctsio); 435210886Spjdstatic int ctl_cmd_applicable(uint8_t lun_type, 436210886Spjd const struct ctl_cmd_entry *entry); 437210886Spjd 438204076Spjd/* 439217784Spjd * Load the serialization table. This isn't very pretty, but is probably 440217784Spjd * the easiest way to do it. 441217784Spjd */ 442217784Spjd#include "ctl_ser_table.c" 443217784Spjd 444218138Spjd/* 445217784Spjd * We only need to define open, close and ioctl routines for this driver. 446217784Spjd */ 447221076Strocinystatic struct cdevsw ctl_cdevsw = { 448217784Spjd .d_version = D_VERSION, 449219818Spjd .d_flags = 0, 450217784Spjd .d_open = ctl_open, 451219351Spjd .d_close = ctl_close, 452219354Spjd .d_ioctl = ctl_ioctl, 453217784Spjd .d_name = "ctl", 454217784Spjd}; 455225830Spjd 456217784Spjd 457217784SpjdMALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 458217784SpjdMALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 459217784Spjd 460217784Spjdstatic int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 461229945Spjd 462217784Spjdstatic moduledata_t ctl_moduledata = { 463217784Spjd "ctl", 464217784Spjd ctl_module_event_handler, 465217784Spjd NULL 466217784Spjd}; 467217784Spjd 468217784SpjdDECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 469229945SpjdMODULE_VERSION(ctl, 1); 470217784Spjd 471217784Spjdstatic struct ctl_frontend ioctl_frontend = 472217784Spjd{ 473217784Spjd .name = "ioctl", 474217784Spjd}; 475217784Spjd 476217784Spjdstatic void 477217784Spjdctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 478217784Spjd union ctl_ha_msg *msg_info) 479217784Spjd{ 480217784Spjd struct ctl_scsiio *ctsio; 481217784Spjd 482204076Spjd if (msg_info->hdr.original_sc == NULL) { 483204076Spjd printf("%s: original_sc == NULL!\n", __func__); 484210886Spjd /* XXX KDM now what? */ 485210886Spjd return; 486222108Spjd } 487226463Spjd 488222108Spjd ctsio = &msg_info->hdr.original_sc->scsiio; 489210886Spjd ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 490226463Spjd ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 491204076Spjd ctsio->io_hdr.status = msg_info->hdr.status; 492210886Spjd ctsio->scsi_status = msg_info->scsi.scsi_status; 493210886Spjd ctsio->sense_len = msg_info->scsi.sense_len; 494226463Spjd ctsio->sense_residual = msg_info->scsi.sense_residual; 495226463Spjd ctsio->residual = msg_info->scsi.residual; 496210886Spjd memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 497210886Spjd sizeof(ctsio->sense_data)); 498210886Spjd memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 499210886Spjd &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 500210886Spjd ctl_enqueue_isc((union ctl_io *)ctsio); 501210886Spjd} 502210886Spjd 503210886Spjdstatic void 504210886Spjdctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 505229945Spjd union ctl_ha_msg *msg_info) 506210886Spjd{ 507210886Spjd struct ctl_scsiio *ctsio; 508210886Spjd 509210886Spjd if (msg_info->hdr.serializing_sc == NULL) { 510210886Spjd printf("%s: serializing_sc == NULL!\n", __func__); 511210886Spjd /* XXX KDM now what? */ 512210886Spjd return; 513222108Spjd } 514210886Spjd 515222108Spjd ctsio = &msg_info->hdr.serializing_sc->scsiio; 516222108Spjd#if 0 517222108Spjd /* 518222108Spjd * Attempt to catch the situation where an I/O has 519222108Spjd * been freed, and we're using it again. 520210886Spjd */ 521222108Spjd if (ctsio->io_hdr.io_type == 0xff) { 522222108Spjd union ctl_io *tmp_io; 523222108Spjd tmp_io = (union ctl_io *)ctsio; 524222108Spjd printf("%s: %p use after free!\n", __func__, 525222108Spjd ctsio); 526222108Spjd printf("%s: type %d msg %d cdb %x iptl: " 527222108Spjd "%d:%d:%d:%d tag 0x%04x " 528222108Spjd "flag %#x status %x\n", 529222108Spjd __func__, 530222108Spjd tmp_io->io_hdr.io_type, 531222108Spjd tmp_io->io_hdr.msg_type, 532222108Spjd tmp_io->scsiio.cdb[0], 533222108Spjd tmp_io->io_hdr.nexus.initid.id, 534210886Spjd tmp_io->io_hdr.nexus.targ_port, 535222108Spjd tmp_io->io_hdr.nexus.targ_target.id, 536222108Spjd tmp_io->io_hdr.nexus.targ_lun, 537222108Spjd (tmp_io->io_hdr.io_type == 538222108Spjd CTL_IO_TASK) ? 539226463Spjd tmp_io->taskio.tag_num : 540226463Spjd tmp_io->scsiio.tag_num, 541226463Spjd tmp_io->io_hdr.flags, 542233679Strociny tmp_io->io_hdr.status); 543233679Strociny } 544226463Spjd#endif 545226463Spjd ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 546226463Spjd ctl_enqueue_isc((union ctl_io *)ctsio); 547226463Spjd} 548226463Spjd 549226463Spjd/* 550226463Spjd * ISC (Inter Shelf Communication) event handler. Events from the HA 551226463Spjd * subsystem come in here. 552226463Spjd */ 553226463Spjdstatic void 554226463Spjdctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 555229945Spjd{ 556226463Spjd struct ctl_softc *ctl_softc; 557226463Spjd union ctl_io *io; 558226463Spjd struct ctl_prio *presio; 559226463Spjd ctl_ha_status isc_status; 560226463Spjd 561226463Spjd ctl_softc = control_softc; 562226463Spjd io = NULL; 563226463Spjd 564226463Spjd 565222108Spjd#if 0 566222108Spjd printf("CTL: Isc Msg event %d\n", event); 567222108Spjd#endif 568210886Spjd if (event == CTL_HA_EVT_MSG_RECV) { 569222108Spjd union ctl_ha_msg msg_info; 570210886Spjd 571210886Spjd isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 572210886Spjd sizeof(msg_info), /*wait*/ 0); 573210886Spjd#if 0 574210886Spjd printf("CTL: msg_type %d\n", msg_info.msg_type); 575210886Spjd#endif 576210886Spjd if (isc_status != 0) { 577210886Spjd printf("Error receiving message, status = %d\n", 578210886Spjd isc_status); 579210886Spjd return; 580222108Spjd } 581226463Spjd 582226463Spjd switch (msg_info.hdr.msg_type) { 583229946Spjd case CTL_MSG_SERIALIZE: 584229946Spjd#if 0 585229946Spjd printf("Serialize\n"); 586229946Spjd#endif 587229946Spjd io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 588229946Spjd if (io == NULL) { 589229946Spjd printf("ctl_isc_event_handler: can't allocate " 590229946Spjd "ctl_io!\n"); 591226463Spjd /* Bad Juju */ 592222108Spjd /* Need to set busy and send msg back */ 593222108Spjd msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 594222108Spjd msg_info.hdr.status = CTL_SCSI_ERROR; 595222108Spjd msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 596222108Spjd msg_info.scsi.sense_len = 0; 597222108Spjd if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 598222108Spjd sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 599222108Spjd } 600222108Spjd goto bailout; 601222108Spjd } 602222108Spjd ctl_zero_io(io); 603222108Spjd // populate ctsio from msg_info 604222108Spjd io->io_hdr.io_type = CTL_IO_SCSI; 605222108Spjd io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 606210886Spjd io->io_hdr.original_sc = msg_info.hdr.original_sc; 607222108Spjd#if 0 608210886Spjd printf("pOrig %x\n", (int)msg_info.original_sc); 609210886Spjd#endif 610210886Spjd io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 611210886Spjd CTL_FLAG_IO_ACTIVE; 612210886Spjd /* 613210886Spjd * If we're in serialization-only mode, we don't 614210886Spjd * want to go through full done processing. Thus 615210886Spjd * the COPY flag. 616210886Spjd * 617210886Spjd * XXX KDM add another flag that is more specific. 618210886Spjd */ 619210886Spjd if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 620210886Spjd io->io_hdr.flags |= CTL_FLAG_INT_COPY; 621210886Spjd io->io_hdr.nexus = msg_info.hdr.nexus; 622210886Spjd#if 0 623210886Spjd printf("targ %d, port %d, iid %d, lun %d\n", 624210886Spjd io->io_hdr.nexus.targ_target.id, 625210886Spjd io->io_hdr.nexus.targ_port, 626210886Spjd io->io_hdr.nexus.initid.id, 627210886Spjd io->io_hdr.nexus.targ_lun); 628210886Spjd#endif 629210886Spjd io->scsiio.tag_num = msg_info.scsi.tag_num; 630210886Spjd io->scsiio.tag_type = msg_info.scsi.tag_type; 631210886Spjd memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 632210886Spjd CTL_MAX_CDBLEN); 633210886Spjd if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 634210886Spjd const struct ctl_cmd_entry *entry; 635210886Spjd 636210886Spjd entry = ctl_get_cmd_entry(&io->scsiio); 637210886Spjd io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 638210886Spjd io->io_hdr.flags |= 639210886Spjd entry->flags & CTL_FLAG_DATA_MASK; 640210886Spjd } 641210886Spjd ctl_enqueue_isc(io); 642210886Spjd break; 643210886Spjd 644210886Spjd /* Performed on the Originating SC, XFER mode only */ 645210886Spjd case CTL_MSG_DATAMOVE: { 646210886Spjd struct ctl_sg_entry *sgl; 647210886Spjd int i, j; 648210886Spjd 649210886Spjd io = msg_info.hdr.original_sc; 650210886Spjd if (io == NULL) { 651210886Spjd printf("%s: original_sc == NULL!\n", __func__); 652225830Spjd /* XXX KDM do something here */ 653225830Spjd break; 654225830Spjd } 655210886Spjd io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 656225830Spjd io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 657225830Spjd /* 658225830Spjd * Keep track of this, we need to send it back over 659210886Spjd * when the datamove is complete. 660210886Spjd */ 661210886Spjd io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 662210886Spjd 663210886Spjd if (msg_info.dt.sg_sequence == 0) { 664210886Spjd /* 665218138Spjd * XXX KDM we use the preallocated S/G list 666210886Spjd * here, but we'll need to change this to 667210886Spjd * dynamic allocation if we need larger S/G 668210886Spjd * lists. 669210886Spjd */ 670210886Spjd if (msg_info.dt.kern_sg_entries > 671210886Spjd sizeof(io->io_hdr.remote_sglist) / 672210886Spjd sizeof(io->io_hdr.remote_sglist[0])) { 673210886Spjd printf("%s: number of S/G entries " 674210886Spjd "needed %u > allocated num %zd\n", 675210886Spjd __func__, 676210886Spjd msg_info.dt.kern_sg_entries, 677210886Spjd sizeof(io->io_hdr.remote_sglist)/ 678210886Spjd sizeof(io->io_hdr.remote_sglist[0])); 679210886Spjd 680210886Spjd /* 681219818Spjd * XXX KDM send a message back to 682219818Spjd * the other side to shut down the 683210886Spjd * DMA. The error will come back 684219351Spjd * through via the normal channel. 685219354Spjd */ 686210886Spjd break; 687217729Spjd } 688217729Spjd sgl = io->io_hdr.remote_sglist; 689225830Spjd memset(sgl, 0, 690217784Spjd sizeof(io->io_hdr.remote_sglist)); 691217784Spjd 692210886Spjd io->scsiio.kern_data_ptr = (uint8_t *)sgl; 693210886Spjd 694210886Spjd io->scsiio.kern_sg_entries = 695210886Spjd msg_info.dt.kern_sg_entries; 696210886Spjd io->scsiio.rem_sg_entries = 697210886Spjd msg_info.dt.kern_sg_entries; 698210886Spjd io->scsiio.kern_data_len = 699210886Spjd msg_info.dt.kern_data_len; 700210886Spjd io->scsiio.kern_total_len = 701210886Spjd msg_info.dt.kern_total_len; 702222108Spjd io->scsiio.kern_data_resid = 703222108Spjd msg_info.dt.kern_data_resid; 704222108Spjd io->scsiio.kern_rel_offset = 705222108Spjd msg_info.dt.kern_rel_offset; 706222108Spjd /* 707222108Spjd * Clear out per-DMA flags. 708222108Spjd */ 709222108Spjd io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 710222108Spjd /* 711222108Spjd * Add per-DMA flags that are set for this 712222108Spjd * particular DMA request. 713222108Spjd */ 714222108Spjd io->io_hdr.flags |= msg_info.dt.flags & 715222108Spjd CTL_FLAG_RDMA_MASK; 716210886Spjd } else 717210886Spjd sgl = (struct ctl_sg_entry *) 718226463Spjd io->scsiio.kern_data_ptr; 719226463Spjd 720210886Spjd for (i = msg_info.dt.sent_sg_entries, j = 0; 721204076Spjd i < (msg_info.dt.sent_sg_entries + 722204076Spjd msg_info.dt.cur_sg_entries); i++, j++) { 723204076Spjd sgl[i].addr = msg_info.dt.sg_list[j].addr; 724211899Spjd sgl[i].len = msg_info.dt.sg_list[j].len; 725211899Spjd 726211899Spjd#if 0 727211899Spjd printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 728211899Spjd __func__, 729211899Spjd msg_info.dt.sg_list[j].addr, 730211899Spjd msg_info.dt.sg_list[j].len, 731211899Spjd sgl[i].addr, sgl[i].len, j, i); 732211899Spjd#endif 733211899Spjd } 734211899Spjd#if 0 735211899Spjd memcpy(&sgl[msg_info.dt.sent_sg_entries], 736211899Spjd msg_info.dt.sg_list, 737211899Spjd sizeof(*sgl) * msg_info.dt.cur_sg_entries); 738211899Spjd#endif 739211899Spjd 740211899Spjd /* 741211899Spjd * If this is the last piece of the I/O, we've got 742211899Spjd * the full S/G list. Queue processing in the thread. 743222108Spjd * Otherwise wait for the next piece. 744204076Spjd */ 745204076Spjd if (msg_info.dt.sg_last != 0) 746204076Spjd ctl_enqueue_isc(io); 747204076Spjd break; 748204076Spjd } 749204076Spjd /* Performed on the Serializing (primary) SC, XFER mode only */ 750204076Spjd case CTL_MSG_DATAMOVE_DONE: { 751246922Spjd if (msg_info.hdr.serializing_sc == NULL) { 752204076Spjd printf("%s: serializing_sc == NULL!\n", 753204076Spjd __func__); 754204076Spjd /* XXX KDM now what? */ 755204076Spjd break; 756222108Spjd } 757204076Spjd /* 758204076Spjd * We grab the sense information here in case 759229945Spjd * there was a failure, so we can return status 760204076Spjd * back to the initiator. 761204076Spjd */ 762204076Spjd io = msg_info.hdr.serializing_sc; 763204076Spjd io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 764204076Spjd io->io_hdr.status = msg_info.hdr.status; 765204076Spjd io->scsiio.scsi_status = msg_info.scsi.scsi_status; 766209185Spjd io->scsiio.sense_len = msg_info.scsi.sense_len; 767204076Spjd io->scsiio.sense_residual =msg_info.scsi.sense_residual; 768207371Spjd io->io_hdr.port_status = msg_info.scsi.fetd_status; 769229945Spjd io->scsiio.residual = msg_info.scsi.residual; 770207371Spjd memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 771207371Spjd sizeof(io->scsiio.sense_data)); 772204076Spjd ctl_enqueue_isc(io); 773204076Spjd break; 774204076Spjd } 775204076Spjd 776204076Spjd /* Preformed on Originating SC, SER_ONLY mode */ 777204076Spjd case CTL_MSG_R2R: 778204076Spjd io = msg_info.hdr.original_sc; 779204076Spjd if (io == NULL) { 780204076Spjd printf("%s: Major Bummer\n", __func__); 781204076Spjd return; 782204076Spjd } else { 783204076Spjd#if 0 784204076Spjd printf("pOrig %x\n",(int) ctsio); 785204076Spjd#endif 786204076Spjd } 787204076Spjd io->io_hdr.msg_type = CTL_MSG_R2R; 788229945Spjd io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 789204076Spjd ctl_enqueue_isc(io); 790204076Spjd break; 791204076Spjd 792204076Spjd /* 793204076Spjd * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 794204076Spjd * mode. 795204076Spjd * Performed on the Originating (i.e. secondary) SC in XFER 796204076Spjd * mode 797204076Spjd */ 798204076Spjd case CTL_MSG_FINISH_IO: 799204076Spjd if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 800204076Spjd ctl_isc_handler_finish_xfer(ctl_softc, 801246922Spjd &msg_info); 802246922Spjd else 803246922Spjd ctl_isc_handler_finish_ser_only(ctl_softc, 804246922Spjd &msg_info); 805246922Spjd break; 806246922Spjd 807246922Spjd /* Preformed on Originating SC */ 808246922Spjd case CTL_MSG_BAD_JUJU: 809204076Spjd io = msg_info.hdr.original_sc; 810204076Spjd if (io == NULL) { 811229778Suqs printf("%s: Bad JUJU!, original_sc is NULL!\n", 812204076Spjd __func__); 813204076Spjd break; 814204076Spjd } 815204076Spjd ctl_copy_sense_data(&msg_info, io); 816204076Spjd /* 817204076Spjd * IO should have already been cleaned up on other 818204076Spjd * SC so clear this flag so we won't send a message 819204076Spjd * back to finish the IO there. 820204076Spjd */ 821204076Spjd io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 822204076Spjd io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 823204076Spjd 824204076Spjd /* io = msg_info.hdr.serializing_sc; */ 825204076Spjd io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 826204076Spjd ctl_enqueue_isc(io); 827204076Spjd break; 828204076Spjd 829204076Spjd /* Handle resets sent from the other side */ 830204076Spjd case CTL_MSG_MANAGE_TASKS: { 831204076Spjd struct ctl_taskio *taskio; 832204076Spjd taskio = (struct ctl_taskio *)ctl_alloc_io( 833204076Spjd (void *)ctl_softc->othersc_pool); 834204076Spjd if (taskio == NULL) { 835204076Spjd printf("ctl_isc_event_handler: can't allocate " 836204076Spjd "ctl_io!\n"); 837204076Spjd /* Bad Juju */ 838204076Spjd /* should I just call the proper reset func 839204076Spjd here??? */ 840204076Spjd goto bailout; 841204076Spjd } 842204076Spjd ctl_zero_io((union ctl_io *)taskio); 843204076Spjd taskio->io_hdr.io_type = CTL_IO_TASK; 844204076Spjd taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 845204076Spjd taskio->io_hdr.nexus = msg_info.hdr.nexus; 846204076Spjd taskio->task_action = msg_info.task.task_action; 847204076Spjd taskio->tag_num = msg_info.task.tag_num; 848220890Spjd taskio->tag_type = msg_info.task.tag_type; 849204076Spjd#ifdef CTL_TIME_IO 850204076Spjd taskio->io_hdr.start_time = time_uptime; 851204076Spjd getbintime(&taskio->io_hdr.start_bt); 852204076Spjd#if 0 853204076Spjd cs_prof_gettime(&taskio->io_hdr.start_ticks); 854220898Spjd#endif 855220898Spjd#endif /* CTL_TIME_IO */ 856220898Spjd ctl_run_task((union ctl_io *)taskio); 857220899Spjd break; 858220898Spjd } 859220898Spjd /* Persistent Reserve action which needs attention */ 860220898Spjd case CTL_MSG_PERS_ACTION: 861204076Spjd presio = (struct ctl_prio *)ctl_alloc_io( 862204076Spjd (void *)ctl_softc->othersc_pool); 863204076Spjd if (presio == NULL) { 864204076Spjd printf("ctl_isc_event_handler: can't allocate " 865204076Spjd "ctl_io!\n"); 866204076Spjd /* Bad Juju */ 867209185Spjd /* Need to set busy and send msg back */ 868204076Spjd goto bailout; 869204076Spjd } 870204076Spjd ctl_zero_io((union ctl_io *)presio); 871204076Spjd presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 872204076Spjd presio->pr_msg = msg_info.pr; 873204076Spjd ctl_enqueue_isc((union ctl_io *)presio); 874204076Spjd break; 875204076Spjd case CTL_MSG_SYNC_FE: 876218138Spjd rcv_sync_msg = 1; 877204076Spjd break; 878204076Spjd case CTL_MSG_APS_LOCK: { 879218138Spjd // It's quicker to execute this then to 880204076Spjd // queue it. 881204076Spjd struct ctl_lun *lun; 882204076Spjd struct ctl_page_index *page_index; 883204076Spjd struct copan_aps_subpage *current_sp; 884229945Spjd uint32_t targ_lun; 885204076Spjd 886204076Spjd targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 887204076Spjd lun = ctl_softc->ctl_luns[targ_lun]; 888204076Spjd mtx_lock(&lun->lun_lock); 889204076Spjd page_index = &lun->mode_pages.index[index_to_aps_page]; 890204076Spjd current_sp = (struct copan_aps_subpage *) 891204076Spjd (page_index->page_data + 892204076Spjd (page_index->page_len * CTL_PAGE_CURRENT)); 893204076Spjd 894204076Spjd current_sp->lock_active = msg_info.aps.lock_flag; 895204076Spjd mtx_unlock(&lun->lun_lock); 896207372Spjd break; 897204076Spjd } 898204076Spjd default: 899204076Spjd printf("How did I get here?\n"); 900204076Spjd } 901207372Spjd } else if (event == CTL_HA_EVT_MSG_SENT) { 902204076Spjd if (param != CTL_HA_STATUS_SUCCESS) { 903213006Spjd printf("Bad status from ctl_ha_msg_send status %d\n", 904204076Spjd param); 905204076Spjd } 906204076Spjd return; 907213981Spjd } else if (event == CTL_HA_EVT_DISCONNECT) { 908213981Spjd printf("CTL: Got a disconnect from Isc\n"); 909204076Spjd return; 910204076Spjd } else { 911204076Spjd printf("ctl_isc_event_handler: Unknown event %d\n", event); 912204076Spjd return; 913204076Spjd } 914204076Spjd 915204076Spjdbailout: 916204076Spjd return; 917204076Spjd} 918204076Spjd 919204076Spjdstatic void 920204076Spjdctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 921204076Spjd{ 922260006Strociny struct scsi_sense_data *sense; 923260006Strociny 924260006Strociny sense = &dest->scsiio.sense_data; 925260006Strociny bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 926260006Strociny dest->scsiio.scsi_status = src->scsi.scsi_status; 927260006Strociny dest->scsiio.sense_len = src->scsi.sense_len; 928246922Spjd dest->io_hdr.status = src->hdr.status; 929204076Spjd} 930204076Spjd 931246922Spjdstatic int 932204076Spjdctl_init(void) 933204076Spjd{ 934204076Spjd struct ctl_softc *softc; 935204076Spjd struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 936204076Spjd struct ctl_port *port; 937204076Spjd uint8_t sc_id =0; 938204076Spjd int i, error, retval; 939204076Spjd //int isc_retval; 940204076Spjd 941204076Spjd retval = 0; 942246922Spjd ctl_pause_rtr = 0; 943204076Spjd rcv_sync_msg = 0; 944204076Spjd 945204076Spjd control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 946204076Spjd M_WAITOK | M_ZERO); 947204076Spjd softc = control_softc; 948204076Spjd 949204076Spjd softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 950204076Spjd "cam/ctl"); 951204076Spjd 952204076Spjd softc->dev->si_drv1 = softc; 953204076Spjd 954204076Spjd /* 955204076Spjd * By default, return a "bad LUN" peripheral qualifier for unknown 956204076Spjd * LUNs. The user can override this default using the tunable or 957204076Spjd * sysctl. See the comment in ctl_inquiry_std() for more details. 958204076Spjd */ 959204076Spjd softc->inquiry_pq_no_lun = 1; 960204076Spjd TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 961204076Spjd &softc->inquiry_pq_no_lun); 962204076Spjd sysctl_ctx_init(&softc->sysctl_ctx); 963204076Spjd softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 964204076Spjd SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 965204076Spjd CTLFLAG_RD, 0, "CAM Target Layer"); 966204076Spjd 967204076Spjd if (softc->sysctl_tree == NULL) { 968204076Spjd printf("%s: unable to allocate sysctl tree\n", __func__); 969204076Spjd destroy_dev(softc->dev); 970204076Spjd free(control_softc, M_DEVBUF); 971229945Spjd control_softc = NULL; 972204076Spjd return (ENOMEM); 973204076Spjd } 974204076Spjd 975204076Spjd SYSCTL_ADD_INT(&softc->sysctl_ctx, 976204076Spjd SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 977204076Spjd "inquiry_pq_no_lun", CTLFLAG_RW, 978204076Spjd &softc->inquiry_pq_no_lun, 0, 979204076Spjd "Report no lun possible for invalid LUNs"); 980204076Spjd 981204076Spjd mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 982204076Spjd mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 983204076Spjd softc->open_count = 0; 984204076Spjd 985204076Spjd /* 986204076Spjd * Default to actually sending a SYNCHRONIZE CACHE command down to 987218218Spjd * the drive. 988218218Spjd */ 989218218Spjd softc->flags = CTL_FLAG_REAL_SYNC; 990218218Spjd 991218218Spjd /* 992219814Spjd * In Copan's HA scheme, the "master" and "slave" roles are 993219814Spjd * figured out through the slot the controller is in. Although it 994219900Spjd * is an active/active system, someone has to be in charge. 995219900Spjd */ 996229945Spjd#ifdef NEEDTOPORT 997218218Spjd scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 998218218Spjd#endif 999218218Spjd 1000218218Spjd if (sc_id == 0) { 1001219818Spjd softc->flags |= CTL_FLAG_MASTER_SHELF; 1002229945Spjd persis_offset = 0; 1003218218Spjd } else 1004218218Spjd persis_offset = CTL_MAX_INITIATORS; 1005218218Spjd 1006218218Spjd /* 1007218218Spjd * XXX KDM need to figure out where we want to get our target ID 1008218218Spjd * and WWID. Is it different on each port? 1009229945Spjd */ 1010218218Spjd softc->target.id = 0; 1011218218Spjd softc->target.wwid[0] = 0x12345678; 1012218218Spjd softc->target.wwid[1] = 0x87654321; 1013218218Spjd STAILQ_INIT(&softc->lun_list); 1014218218Spjd STAILQ_INIT(&softc->pending_lun_queue); 1015218218Spjd STAILQ_INIT(&softc->fe_list); 1016218218Spjd STAILQ_INIT(&softc->port_list); 1017218218Spjd STAILQ_INIT(&softc->be_list); 1018229945Spjd STAILQ_INIT(&softc->io_pools); 1019218218Spjd 1020218218Spjd if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1021218218Spjd &internal_pool)!= 0){ 1022229945Spjd printf("ctl: can't allocate %d entry internal pool, " 1023218218Spjd "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1024219814Spjd return (ENOMEM); 1025219814Spjd } 1026218218Spjd 1027218218Spjd if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1028218218Spjd CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1029219837Spjd printf("ctl: can't allocate %d entry emergency pool, " 1030204076Spjd "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1031213009Spjd ctl_pool_free(internal_pool); 1032213009Spjd return (ENOMEM); 1033219837Spjd } 1034204076Spjd 1035213009Spjd if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1036213009Spjd &other_pool) != 0) 1037211977Spjd { 1038213009Spjd printf("ctl: can't allocate %d entry other SC pool, " 1039213009Spjd "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1040213009Spjd ctl_pool_free(internal_pool); 1041213009Spjd ctl_pool_free(emergency_pool); 1042213009Spjd return (ENOMEM); 1043213009Spjd } 1044219837Spjd 1045219837Spjd softc->internal_pool = internal_pool; 1046219837Spjd softc->emergency_pool = emergency_pool; 1047219837Spjd softc->othersc_pool = other_pool; 1048219837Spjd 1049219837Spjd if (worker_threads <= 0) 1050219837Spjd worker_threads = max(1, mp_ncpus / 4); 1051219837Spjd if (worker_threads > CTL_MAX_THREADS) 1052219837Spjd worker_threads = CTL_MAX_THREADS; 1053219837Spjd 1054219837Spjd for (i = 0; i < worker_threads; i++) { 1055219837Spjd struct ctl_thread *thr = &softc->threads[i]; 1056219837Spjd 1057219837Spjd mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1058219837Spjd thr->ctl_softc = softc; 1059219837Spjd STAILQ_INIT(&thr->incoming_queue); 1060219837Spjd STAILQ_INIT(&thr->rtr_queue); 1061219837Spjd STAILQ_INIT(&thr->done_queue); 1062219837Spjd STAILQ_INIT(&thr->isc_queue); 1063219837Spjd 1064219837Spjd error = kproc_kthread_add(ctl_work_thread, thr, 1065219837Spjd &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1066219837Spjd if (error != 0) { 1067219837Spjd printf("error creating CTL work thread!\n"); 1068219837Spjd ctl_pool_free(internal_pool); 1069222108Spjd ctl_pool_free(emergency_pool); 1070219837Spjd ctl_pool_free(other_pool); 1071219837Spjd return (error); 1072219837Spjd } 1073219837Spjd } 1074219837Spjd error = kproc_kthread_add(ctl_lun_thread, softc, 1075219864Spjd &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1076219837Spjd if (error != 0) { 1077219837Spjd printf("error creating CTL lun thread!\n"); 1078219837Spjd ctl_pool_free(internal_pool); 1079204076Spjd ctl_pool_free(emergency_pool); 1080219837Spjd ctl_pool_free(other_pool); 1081204076Spjd return (error); 1082209177Spjd } 1083204076Spjd if (bootverbose) 1084212038Spjd printf("ctl: CAM Target Layer loaded\n"); 1085218138Spjd 1086212038Spjd /* 1087222108Spjd * Initialize the initiator and portname mappings 1088222108Spjd */ 1089222108Spjd memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid)); 1090222108Spjd 1091222108Spjd /* 1092222108Spjd * Initialize the ioctl front end. 1093222108Spjd */ 1094222108Spjd ctl_frontend_register(&ioctl_frontend); 1095212038Spjd port = &softc->ioctl_info.port; 1096212038Spjd port->frontend = &ioctl_frontend; 1097212038Spjd sprintf(softc->ioctl_info.port_name, "ioctl"); 1098212038Spjd port->port_type = CTL_PORT_IOCTL; 1099218138Spjd port->num_requested_ctl_io = 100; 1100212038Spjd port->port_name = softc->ioctl_info.port_name; 1101212038Spjd port->port_online = ctl_ioctl_online; 1102218218Spjd port->port_offline = ctl_ioctl_offline; 1103218218Spjd port->onoff_arg = &softc->ioctl_info; 1104219900Spjd port->lun_enable = ctl_ioctl_lun_enable; 1105218218Spjd port->lun_disable = ctl_ioctl_lun_disable; 1106218218Spjd port->targ_lun_arg = &softc->ioctl_info; 1107218218Spjd port->fe_datamove = ctl_ioctl_datamove; 1108218218Spjd port->fe_done = ctl_ioctl_done; 1109219900Spjd port->max_targets = 15; 1110219900Spjd port->max_target_id = 15; 1111218218Spjd 1112212038Spjd if (ctl_port_register(&softc->ioctl_info.port, 1113204076Spjd (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1114218138Spjd printf("ctl: ioctl front end registration failed, will " 1115213009Spjd "continue anyway\n"); 1116219813Spjd } 1117219813Spjd 1118213429Spjd#ifdef CTL_IO_DELAY 1119219813Spjd if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1120219813Spjd printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1121219813Spjd sizeof(struct callout), CTL_TIMER_BYTES); 1122219813Spjd return (EINVAL); 1123219813Spjd } 1124219813Spjd#endif /* CTL_IO_DELAY */ 1125219813Spjd 1126219813Spjd return (0); 1127219813Spjd} 1128204076Spjd 1129204076Spjdvoid 1130204076Spjdctl_shutdown(void) 1131204076Spjd{ 1132204076Spjd struct ctl_softc *softc; 1133204076Spjd struct ctl_lun *lun, *next_lun; 1134219837Spjd struct ctl_io_pool *pool; 1135219837Spjd 1136219837Spjd softc = (struct ctl_softc *)control_softc; 1137219837Spjd 1138219837Spjd if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1139219837Spjd printf("ctl: ioctl front end deregistration failed\n"); 1140212038Spjd 1141204076Spjd mtx_lock(&softc->ctl_lock); 1142222108Spjd 1143222108Spjd /* 1144222108Spjd * Free up each LUN. 1145222108Spjd */ 1146222108Spjd for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1147222108Spjd next_lun = STAILQ_NEXT(lun, links); 1148212038Spjd ctl_free_lun(lun); 1149212038Spjd } 1150212038Spjd 1151212038Spjd mtx_unlock(&softc->ctl_lock); 1152212038Spjd 1153212038Spjd ctl_frontend_deregister(&ioctl_frontend); 1154212038Spjd 1155212038Spjd /* 1156212038Spjd * This will rip the rug out from under any FETDs or anyone else 1157219900Spjd * that has a pool allocated. Since we increment our module 1158219900Spjd * refcount any time someone outside the main CTL module allocates 1159219900Spjd * a pool, we shouldn't have any problems here. The user won't be 1160219900Spjd * able to unload the CTL module until client modules have 1161218218Spjd * successfully unloaded. 1162212038Spjd */ 1163219900Spjd while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1164219900Spjd ctl_pool_free(pool); 1165219900Spjd 1166219900Spjd#if 0 1167219900Spjd ctl_shutdown_thread(softc->work_thread); 1168219900Spjd mtx_destroy(&softc->queue_lock); 1169219900Spjd#endif 1170219900Spjd 1171218218Spjd mtx_destroy(&softc->pool_lock); 1172212038Spjd mtx_destroy(&softc->ctl_lock); 1173204076Spjd 1174204076Spjd destroy_dev(softc->dev); 1175204076Spjd 1176213428Spjd sysctl_ctx_free(&softc->sysctl_ctx); 1177213428Spjd 1178213428Spjd free(control_softc, M_DEVBUF); 1179213428Spjd control_softc = NULL; 1180213428Spjd 1181213428Spjd if (bootverbose) 1182204076Spjd printf("ctl: CAM Target Layer unloaded\n"); 1183204076Spjd} 1184204076Spjd 1185222108Spjdstatic int 1186204076Spjdctl_module_event_handler(module_t mod, int what, void *arg) 1187204076Spjd{ 1188213009Spjd 1189204076Spjd switch (what) { 1190204076Spjd case MOD_LOAD: 1191204076Spjd return (ctl_init()); 1192204076Spjd case MOD_UNLOAD: 1193204076Spjd return (EBUSY); 1194204076Spjd default: 1195204076Spjd return (EOPNOTSUPP); 1196204076Spjd } 1197204076Spjd} 1198204076Spjd 1199204076Spjd/* 1200204076Spjd * XXX KDM should we do some access checks here? Bump a reference count to 1201204076Spjd * prevent a CTL module from being unloaded while someone has it open? 1202204076Spjd */ 1203204076Spjdstatic int 1204204076Spjdctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1205204076Spjd{ 1206204076Spjd return (0); 1207204076Spjd} 1208204076Spjd 1209204076Spjdstatic int 1210204076Spjdctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1211204076Spjd{ 1212204076Spjd return (0); 1213204076Spjd} 1214204076Spjd 1215204076Spjdint 1216204076Spjdctl_port_enable(ctl_port_type port_type) 1217204076Spjd{ 1218204076Spjd struct ctl_softc *softc; 1219204076Spjd struct ctl_port *port; 1220217965Spjd 1221204076Spjd if (ctl_is_single == 0) { 1222204076Spjd union ctl_ha_msg msg_info; 1223214273Spjd int isc_retval; 1224214273Spjd 1225226461Spjd#if 0 1226226461Spjd printf("%s: HA mode, synchronizing frontend enable\n", 1227226461Spjd __func__); 1228226461Spjd#endif 1229226461Spjd msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1230226461Spjd if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1231226461Spjd sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1232226461Spjd printf("Sync msg send error retval %d\n", isc_retval); 1233226461Spjd } 1234226461Spjd if (!rcv_sync_msg) { 1235226461Spjd isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1236226461Spjd sizeof(msg_info), 1); 1237226461Spjd } 1238226461Spjd#if 0 1239226461Spjd printf("CTL:Frontend Enable\n"); 1240226461Spjd } else { 1241210883Spjd printf("%s: single mode, skipping frontend synchronization\n", 1242218138Spjd __func__); 1243204076Spjd#endif 1244226463Spjd } 1245226463Spjd 1246226463Spjd softc = control_softc; 1247226463Spjd 1248226463Spjd STAILQ_FOREACH(port, &softc->port_list, links) { 1249226463Spjd if (port_type & port->port_type) 1250229944Spjd { 1251233679Strociny#if 0 1252229944Spjd printf("port %d\n", port->targ_port); 1253229944Spjd#endif 1254229944Spjd ctl_port_online(port); 1255229944Spjd } 1256229944Spjd } 1257229944Spjd 1258229944Spjd return (0); 1259229944Spjd} 1260229944Spjd 1261229944Spjdint 1262229944Spjdctl_port_disable(ctl_port_type port_type) 1263229944Spjd{ 1264229944Spjd struct ctl_softc *softc; 1265229944Spjd struct ctl_port *port; 1266226463Spjd 1267226463Spjd softc = control_softc; 1268226463Spjd 1269213428Spjd STAILQ_FOREACH(port, &softc->port_list, links) { 1270217307Spjd if (port_type & port->port_type) 1271217307Spjd ctl_port_offline(port); 1272217307Spjd } 1273217307Spjd 1274217307Spjd return (0); 1275217307Spjd} 1276217307Spjd 1277213428Spjd/* 1278213428Spjd * Returns 0 for success, 1 for failure. 1279213428Spjd * Currently the only failure mode is if there aren't enough entries 1280213428Spjd * allocated. So, in case of a failure, look at num_entries_dropped, 1281217307Spjd * reallocate and try again. 1282213009Spjd */ 1283213009Spjdint 1284213009Spjdctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1285213009Spjd int *num_entries_filled, int *num_entries_dropped, 1286213009Spjd ctl_port_type port_type, int no_virtual) 1287213009Spjd{ 1288204076Spjd struct ctl_softc *softc; 1289204076Spjd struct ctl_port *port; 1290229945Spjd int entries_dropped, entries_filled; 1291204076Spjd int retval; 1292204076Spjd int i; 1293204076Spjd 1294204076Spjd softc = control_softc; 1295204076Spjd 1296222108Spjd retval = 0; 1297229945Spjd entries_filled = 0; 1298222108Spjd entries_dropped = 0; 1299222108Spjd 1300222108Spjd i = 0; 1301222108Spjd mtx_lock(&softc->ctl_lock); 1302204076Spjd STAILQ_FOREACH(port, &softc->port_list, links) { 1303204076Spjd struct ctl_port_entry *entry; 1304204076Spjd 1305229945Spjd if ((port->port_type & port_type) == 0) 1306204076Spjd continue; 1307204076Spjd 1308204076Spjd if ((no_virtual != 0) 1309204076Spjd && (port->virtual_port != 0)) 1310204076Spjd continue; 1311204076Spjd 1312233679Strociny if (entries_filled >= num_entries_alloced) { 1313233679Strociny entries_dropped++; 1314204076Spjd continue; 1315229945Spjd } 1316204076Spjd entry = &entries[i]; 1317226463Spjd 1318226463Spjd entry->port_type = port->port_type; 1319226463Spjd strlcpy(entry->port_name, port->port_name, 1320226463Spjd sizeof(entry->port_name)); 1321204076Spjd entry->physical_port = port->physical_port; 1322204076Spjd entry->virtual_port = port->virtual_port; 1323204076Spjd entry->wwnn = port->wwnn; 1324222108Spjd entry->wwpn = port->wwpn; 1325222108Spjd 1326222108Spjd i++; 1327222108Spjd entries_filled++; 1328222108Spjd } 1329222108Spjd 1330222108Spjd mtx_unlock(&softc->ctl_lock); 1331222108Spjd 1332211977Spjd if (entries_dropped > 0) 1333211977Spjd retval = 1; 1334204076Spjd 1335204076Spjd *num_entries_dropped = entries_dropped; 1336204076Spjd *num_entries_filled = entries_filled; 1337204076Spjd 1338 return (retval); 1339} 1340 1341static void 1342ctl_ioctl_online(void *arg) 1343{ 1344 struct ctl_ioctl_info *ioctl_info; 1345 1346 ioctl_info = (struct ctl_ioctl_info *)arg; 1347 1348 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1349} 1350 1351static void 1352ctl_ioctl_offline(void *arg) 1353{ 1354 struct ctl_ioctl_info *ioctl_info; 1355 1356 ioctl_info = (struct ctl_ioctl_info *)arg; 1357 1358 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1359} 1360 1361/* 1362 * Remove an initiator by port number and initiator ID. 1363 * Returns 0 for success, 1 for failure. 1364 */ 1365int 1366ctl_remove_initiator(int32_t targ_port, uint32_t iid) 1367{ 1368 struct ctl_softc *softc; 1369 1370 softc = control_softc; 1371 1372 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1373 1374 if ((targ_port < 0) 1375 || (targ_port > CTL_MAX_PORTS)) { 1376 printf("%s: invalid port number %d\n", __func__, targ_port); 1377 return (1); 1378 } 1379 if (iid > CTL_MAX_INIT_PER_PORT) { 1380 printf("%s: initiator ID %u > maximun %u!\n", 1381 __func__, iid, CTL_MAX_INIT_PER_PORT); 1382 return (1); 1383 } 1384 1385 mtx_lock(&softc->ctl_lock); 1386 1387 softc->wwpn_iid[targ_port][iid].in_use = 0; 1388 1389 mtx_unlock(&softc->ctl_lock); 1390 1391 return (0); 1392} 1393 1394/* 1395 * Add an initiator to the initiator map. 1396 * Returns 0 for success, 1 for failure. 1397 */ 1398int 1399ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid) 1400{ 1401 struct ctl_softc *softc; 1402 int retval; 1403 1404 softc = control_softc; 1405 1406 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1407 1408 retval = 0; 1409 1410 if ((targ_port < 0) 1411 || (targ_port > CTL_MAX_PORTS)) { 1412 printf("%s: invalid port number %d\n", __func__, targ_port); 1413 return (1); 1414 } 1415 if (iid > CTL_MAX_INIT_PER_PORT) { 1416 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n", 1417 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1418 return (1); 1419 } 1420 1421 mtx_lock(&softc->ctl_lock); 1422 1423 if (softc->wwpn_iid[targ_port][iid].in_use != 0) { 1424 /* 1425 * We don't treat this as an error. 1426 */ 1427 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) { 1428 printf("%s: port %d iid %u WWPN %#jx arrived again?\n", 1429 __func__, targ_port, iid, (uintmax_t)wwpn); 1430 goto bailout; 1431 } 1432 1433 /* 1434 * This is an error, but what do we do about it? The 1435 * driver is telling us we have a new WWPN for this 1436 * initiator ID, so we pretty much need to use it. 1437 */ 1438 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is " 1439 "still at that address\n", __func__, targ_port, iid, 1440 (uintmax_t)wwpn, 1441 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn); 1442 1443 /* 1444 * XXX KDM clear have_ca and ua_pending on each LUN for 1445 * this initiator. 1446 */ 1447 } 1448 softc->wwpn_iid[targ_port][iid].in_use = 1; 1449 softc->wwpn_iid[targ_port][iid].iid = iid; 1450 softc->wwpn_iid[targ_port][iid].wwpn = wwpn; 1451 softc->wwpn_iid[targ_port][iid].port = targ_port; 1452 1453bailout: 1454 1455 mtx_unlock(&softc->ctl_lock); 1456 1457 return (retval); 1458} 1459 1460static int 1461ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1462{ 1463 return (0); 1464} 1465 1466static int 1467ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1468{ 1469 return (0); 1470} 1471 1472/* 1473 * Data movement routine for the CTL ioctl frontend port. 1474 */ 1475static int 1476ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1477{ 1478 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1479 struct ctl_sg_entry ext_entry, kern_entry; 1480 int ext_sglen, ext_sg_entries, kern_sg_entries; 1481 int ext_sg_start, ext_offset; 1482 int len_to_copy, len_copied; 1483 int kern_watermark, ext_watermark; 1484 int ext_sglist_malloced; 1485 int i, j; 1486 1487 ext_sglist_malloced = 0; 1488 ext_sg_start = 0; 1489 ext_offset = 0; 1490 1491 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1492 1493 /* 1494 * If this flag is set, fake the data transfer. 1495 */ 1496 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1497 ctsio->ext_data_filled = ctsio->ext_data_len; 1498 goto bailout; 1499 } 1500 1501 /* 1502 * To simplify things here, if we have a single buffer, stick it in 1503 * a S/G entry and just make it a single entry S/G list. 1504 */ 1505 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1506 int len_seen; 1507 1508 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1509 1510 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1511 M_WAITOK); 1512 ext_sglist_malloced = 1; 1513 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1514 ext_sglen) != 0) { 1515 ctl_set_internal_failure(ctsio, 1516 /*sks_valid*/ 0, 1517 /*retry_count*/ 0); 1518 goto bailout; 1519 } 1520 ext_sg_entries = ctsio->ext_sg_entries; 1521 len_seen = 0; 1522 for (i = 0; i < ext_sg_entries; i++) { 1523 if ((len_seen + ext_sglist[i].len) >= 1524 ctsio->ext_data_filled) { 1525 ext_sg_start = i; 1526 ext_offset = ctsio->ext_data_filled - len_seen; 1527 break; 1528 } 1529 len_seen += ext_sglist[i].len; 1530 } 1531 } else { 1532 ext_sglist = &ext_entry; 1533 ext_sglist->addr = ctsio->ext_data_ptr; 1534 ext_sglist->len = ctsio->ext_data_len; 1535 ext_sg_entries = 1; 1536 ext_sg_start = 0; 1537 ext_offset = ctsio->ext_data_filled; 1538 } 1539 1540 if (ctsio->kern_sg_entries > 0) { 1541 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1542 kern_sg_entries = ctsio->kern_sg_entries; 1543 } else { 1544 kern_sglist = &kern_entry; 1545 kern_sglist->addr = ctsio->kern_data_ptr; 1546 kern_sglist->len = ctsio->kern_data_len; 1547 kern_sg_entries = 1; 1548 } 1549 1550 1551 kern_watermark = 0; 1552 ext_watermark = ext_offset; 1553 len_copied = 0; 1554 for (i = ext_sg_start, j = 0; 1555 i < ext_sg_entries && j < kern_sg_entries;) { 1556 uint8_t *ext_ptr, *kern_ptr; 1557 1558 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1559 kern_sglist[j].len - kern_watermark); 1560 1561 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1562 ext_ptr = ext_ptr + ext_watermark; 1563 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1564 /* 1565 * XXX KDM fix this! 1566 */ 1567 panic("need to implement bus address support"); 1568#if 0 1569 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1570#endif 1571 } else 1572 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1573 kern_ptr = kern_ptr + kern_watermark; 1574 1575 kern_watermark += len_to_copy; 1576 ext_watermark += len_to_copy; 1577 1578 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1579 CTL_FLAG_DATA_IN) { 1580 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1581 "bytes to user\n", len_to_copy)); 1582 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1583 "to %p\n", kern_ptr, ext_ptr)); 1584 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1585 ctl_set_internal_failure(ctsio, 1586 /*sks_valid*/ 0, 1587 /*retry_count*/ 0); 1588 goto bailout; 1589 } 1590 } else { 1591 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1592 "bytes from user\n", len_to_copy)); 1593 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1594 "to %p\n", ext_ptr, kern_ptr)); 1595 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1596 ctl_set_internal_failure(ctsio, 1597 /*sks_valid*/ 0, 1598 /*retry_count*/0); 1599 goto bailout; 1600 } 1601 } 1602 1603 len_copied += len_to_copy; 1604 1605 if (ext_sglist[i].len == ext_watermark) { 1606 i++; 1607 ext_watermark = 0; 1608 } 1609 1610 if (kern_sglist[j].len == kern_watermark) { 1611 j++; 1612 kern_watermark = 0; 1613 } 1614 } 1615 1616 ctsio->ext_data_filled += len_copied; 1617 1618 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1619 "kern_sg_entries: %d\n", ext_sg_entries, 1620 kern_sg_entries)); 1621 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1622 "kern_data_len = %d\n", ctsio->ext_data_len, 1623 ctsio->kern_data_len)); 1624 1625 1626 /* XXX KDM set residual?? */ 1627bailout: 1628 1629 if (ext_sglist_malloced != 0) 1630 free(ext_sglist, M_CTL); 1631 1632 return (CTL_RETVAL_COMPLETE); 1633} 1634 1635/* 1636 * Serialize a command that went down the "wrong" side, and so was sent to 1637 * this controller for execution. The logic is a little different than the 1638 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1639 * sent back to the other side, but in the success case, we execute the 1640 * command on this side (XFER mode) or tell the other side to execute it 1641 * (SER_ONLY mode). 1642 */ 1643static int 1644ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1645{ 1646 struct ctl_softc *ctl_softc; 1647 union ctl_ha_msg msg_info; 1648 struct ctl_lun *lun; 1649 int retval = 0; 1650 uint32_t targ_lun; 1651 1652 ctl_softc = control_softc; 1653 1654 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1655 lun = ctl_softc->ctl_luns[targ_lun]; 1656 if (lun==NULL) 1657 { 1658 /* 1659 * Why isn't LUN defined? The other side wouldn't 1660 * send a cmd if the LUN is undefined. 1661 */ 1662 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1663 1664 /* "Logical unit not supported" */ 1665 ctl_set_sense_data(&msg_info.scsi.sense_data, 1666 lun, 1667 /*sense_format*/SSD_TYPE_NONE, 1668 /*current_error*/ 1, 1669 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1670 /*asc*/ 0x25, 1671 /*ascq*/ 0x00, 1672 SSD_ELEM_NONE); 1673 1674 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1675 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1676 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1677 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1678 msg_info.hdr.serializing_sc = NULL; 1679 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1680 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1681 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1682 } 1683 return(1); 1684 1685 } 1686 1687 mtx_lock(&lun->lun_lock); 1688 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1689 1690 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1691 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1692 ooa_links))) { 1693 case CTL_ACTION_BLOCK: 1694 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1695 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1696 blocked_links); 1697 break; 1698 case CTL_ACTION_PASS: 1699 case CTL_ACTION_SKIP: 1700 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1701 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1702 ctl_enqueue_rtr((union ctl_io *)ctsio); 1703 } else { 1704 1705 /* send msg back to other side */ 1706 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1707 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1708 msg_info.hdr.msg_type = CTL_MSG_R2R; 1709#if 0 1710 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1711#endif 1712 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1713 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1714 } 1715 } 1716 break; 1717 case CTL_ACTION_OVERLAP: 1718 /* OVERLAPPED COMMANDS ATTEMPTED */ 1719 ctl_set_sense_data(&msg_info.scsi.sense_data, 1720 lun, 1721 /*sense_format*/SSD_TYPE_NONE, 1722 /*current_error*/ 1, 1723 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1724 /*asc*/ 0x4E, 1725 /*ascq*/ 0x00, 1726 SSD_ELEM_NONE); 1727 1728 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1729 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1730 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1731 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1732 msg_info.hdr.serializing_sc = NULL; 1733 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1734#if 0 1735 printf("BAD JUJU:Major Bummer Overlap\n"); 1736#endif 1737 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1738 retval = 1; 1739 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1740 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1741 } 1742 break; 1743 case CTL_ACTION_OVERLAP_TAG: 1744 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1745 ctl_set_sense_data(&msg_info.scsi.sense_data, 1746 lun, 1747 /*sense_format*/SSD_TYPE_NONE, 1748 /*current_error*/ 1, 1749 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1750 /*asc*/ 0x4D, 1751 /*ascq*/ ctsio->tag_num & 0xff, 1752 SSD_ELEM_NONE); 1753 1754 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1755 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1756 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1757 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1758 msg_info.hdr.serializing_sc = NULL; 1759 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1760#if 0 1761 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1762#endif 1763 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1764 retval = 1; 1765 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1766 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1767 } 1768 break; 1769 case CTL_ACTION_ERROR: 1770 default: 1771 /* "Internal target failure" */ 1772 ctl_set_sense_data(&msg_info.scsi.sense_data, 1773 lun, 1774 /*sense_format*/SSD_TYPE_NONE, 1775 /*current_error*/ 1, 1776 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1777 /*asc*/ 0x44, 1778 /*ascq*/ 0x00, 1779 SSD_ELEM_NONE); 1780 1781 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1782 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1783 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1784 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1785 msg_info.hdr.serializing_sc = NULL; 1786 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1787#if 0 1788 printf("BAD JUJU:Major Bummer HW Error\n"); 1789#endif 1790 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1791 retval = 1; 1792 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1793 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1794 } 1795 break; 1796 } 1797 mtx_unlock(&lun->lun_lock); 1798 return (retval); 1799} 1800 1801static int 1802ctl_ioctl_submit_wait(union ctl_io *io) 1803{ 1804 struct ctl_fe_ioctl_params params; 1805 ctl_fe_ioctl_state last_state; 1806 int done, retval; 1807 1808 retval = 0; 1809 1810 bzero(¶ms, sizeof(params)); 1811 1812 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1813 cv_init(¶ms.sem, "ctlioccv"); 1814 params.state = CTL_IOCTL_INPROG; 1815 last_state = params.state; 1816 1817 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1818 1819 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1820 1821 /* This shouldn't happen */ 1822 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1823 return (retval); 1824 1825 done = 0; 1826 1827 do { 1828 mtx_lock(¶ms.ioctl_mtx); 1829 /* 1830 * Check the state here, and don't sleep if the state has 1831 * already changed (i.e. wakeup has already occured, but we 1832 * weren't waiting yet). 1833 */ 1834 if (params.state == last_state) { 1835 /* XXX KDM cv_wait_sig instead? */ 1836 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1837 } 1838 last_state = params.state; 1839 1840 switch (params.state) { 1841 case CTL_IOCTL_INPROG: 1842 /* Why did we wake up? */ 1843 /* XXX KDM error here? */ 1844 mtx_unlock(¶ms.ioctl_mtx); 1845 break; 1846 case CTL_IOCTL_DATAMOVE: 1847 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1848 1849 /* 1850 * change last_state back to INPROG to avoid 1851 * deadlock on subsequent data moves. 1852 */ 1853 params.state = last_state = CTL_IOCTL_INPROG; 1854 1855 mtx_unlock(¶ms.ioctl_mtx); 1856 ctl_ioctl_do_datamove(&io->scsiio); 1857 /* 1858 * Note that in some cases, most notably writes, 1859 * this will queue the I/O and call us back later. 1860 * In other cases, generally reads, this routine 1861 * will immediately call back and wake us up, 1862 * probably using our own context. 1863 */ 1864 io->scsiio.be_move_done(io); 1865 break; 1866 case CTL_IOCTL_DONE: 1867 mtx_unlock(¶ms.ioctl_mtx); 1868 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1869 done = 1; 1870 break; 1871 default: 1872 mtx_unlock(¶ms.ioctl_mtx); 1873 /* XXX KDM error here? */ 1874 break; 1875 } 1876 } while (done == 0); 1877 1878 mtx_destroy(¶ms.ioctl_mtx); 1879 cv_destroy(¶ms.sem); 1880 1881 return (CTL_RETVAL_COMPLETE); 1882} 1883 1884static void 1885ctl_ioctl_datamove(union ctl_io *io) 1886{ 1887 struct ctl_fe_ioctl_params *params; 1888 1889 params = (struct ctl_fe_ioctl_params *) 1890 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1891 1892 mtx_lock(¶ms->ioctl_mtx); 1893 params->state = CTL_IOCTL_DATAMOVE; 1894 cv_broadcast(¶ms->sem); 1895 mtx_unlock(¶ms->ioctl_mtx); 1896} 1897 1898static void 1899ctl_ioctl_done(union ctl_io *io) 1900{ 1901 struct ctl_fe_ioctl_params *params; 1902 1903 params = (struct ctl_fe_ioctl_params *) 1904 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1905 1906 mtx_lock(¶ms->ioctl_mtx); 1907 params->state = CTL_IOCTL_DONE; 1908 cv_broadcast(¶ms->sem); 1909 mtx_unlock(¶ms->ioctl_mtx); 1910} 1911 1912static void 1913ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 1914{ 1915 struct ctl_fe_ioctl_startstop_info *sd_info; 1916 1917 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 1918 1919 sd_info->hs_info.status = metatask->status; 1920 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 1921 sd_info->hs_info.luns_complete = 1922 metatask->taskinfo.startstop.luns_complete; 1923 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 1924 1925 cv_broadcast(&sd_info->sem); 1926} 1927 1928static void 1929ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 1930{ 1931 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 1932 1933 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 1934 1935 mtx_lock(fe_bbr_info->lock); 1936 fe_bbr_info->bbr_info->status = metatask->status; 1937 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 1938 fe_bbr_info->wakeup_done = 1; 1939 mtx_unlock(fe_bbr_info->lock); 1940 1941 cv_broadcast(&fe_bbr_info->sem); 1942} 1943 1944/* 1945 * Returns 0 for success, errno for failure. 1946 */ 1947static int 1948ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1949 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1950{ 1951 union ctl_io *io; 1952 int retval; 1953 1954 retval = 0; 1955 1956 mtx_lock(&lun->lun_lock); 1957 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 1958 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 1959 ooa_links)) { 1960 struct ctl_ooa_entry *entry; 1961 1962 /* 1963 * If we've got more than we can fit, just count the 1964 * remaining entries. 1965 */ 1966 if (*cur_fill_num >= ooa_hdr->alloc_num) 1967 continue; 1968 1969 entry = &kern_entries[*cur_fill_num]; 1970 1971 entry->tag_num = io->scsiio.tag_num; 1972 entry->lun_num = lun->lun; 1973#ifdef CTL_TIME_IO 1974 entry->start_bt = io->io_hdr.start_bt; 1975#endif 1976 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 1977 entry->cdb_len = io->scsiio.cdb_len; 1978 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 1979 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 1980 1981 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 1982 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 1983 1984 if (io->io_hdr.flags & CTL_FLAG_ABORT) 1985 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 1986 1987 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 1988 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 1989 1990 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 1991 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 1992 } 1993 mtx_unlock(&lun->lun_lock); 1994 1995 return (retval); 1996} 1997 1998static void * 1999ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2000 size_t error_str_len) 2001{ 2002 void *kptr; 2003 2004 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2005 2006 if (copyin(user_addr, kptr, len) != 0) { 2007 snprintf(error_str, error_str_len, "Error copying %d bytes " 2008 "from user address %p to kernel address %p", len, 2009 user_addr, kptr); 2010 free(kptr, M_CTL); 2011 return (NULL); 2012 } 2013 2014 return (kptr); 2015} 2016 2017static void 2018ctl_free_args(int num_args, struct ctl_be_arg *args) 2019{ 2020 int i; 2021 2022 if (args == NULL) 2023 return; 2024 2025 for (i = 0; i < num_args; i++) { 2026 free(args[i].kname, M_CTL); 2027 free(args[i].kvalue, M_CTL); 2028 } 2029 2030 free(args, M_CTL); 2031} 2032 2033static struct ctl_be_arg * 2034ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2035 char *error_str, size_t error_str_len) 2036{ 2037 struct ctl_be_arg *args; 2038 int i; 2039 2040 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2041 error_str, error_str_len); 2042 2043 if (args == NULL) 2044 goto bailout; 2045 2046 for (i = 0; i < num_args; i++) { 2047 args[i].kname = NULL; 2048 args[i].kvalue = NULL; 2049 } 2050 2051 for (i = 0; i < num_args; i++) { 2052 uint8_t *tmpptr; 2053 2054 args[i].kname = ctl_copyin_alloc(args[i].name, 2055 args[i].namelen, error_str, error_str_len); 2056 if (args[i].kname == NULL) 2057 goto bailout; 2058 2059 if (args[i].kname[args[i].namelen - 1] != '\0') { 2060 snprintf(error_str, error_str_len, "Argument %d " 2061 "name is not NUL-terminated", i); 2062 goto bailout; 2063 } 2064 2065 if (args[i].flags & CTL_BEARG_RD) { 2066 tmpptr = ctl_copyin_alloc(args[i].value, 2067 args[i].vallen, error_str, error_str_len); 2068 if (tmpptr == NULL) 2069 goto bailout; 2070 if ((args[i].flags & CTL_BEARG_ASCII) 2071 && (tmpptr[args[i].vallen - 1] != '\0')) { 2072 snprintf(error_str, error_str_len, "Argument " 2073 "%d value is not NUL-terminated", i); 2074 goto bailout; 2075 } 2076 args[i].kvalue = tmpptr; 2077 } else { 2078 args[i].kvalue = malloc(args[i].vallen, 2079 M_CTL, M_WAITOK | M_ZERO); 2080 } 2081 } 2082 2083 return (args); 2084bailout: 2085 2086 ctl_free_args(num_args, args); 2087 2088 return (NULL); 2089} 2090 2091static void 2092ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2093{ 2094 int i; 2095 2096 for (i = 0; i < num_args; i++) { 2097 if (args[i].flags & CTL_BEARG_WR) 2098 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2099 } 2100} 2101 2102/* 2103 * Escape characters that are illegal or not recommended in XML. 2104 */ 2105int 2106ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2107{ 2108 int retval; 2109 2110 retval = 0; 2111 2112 for (; *str; str++) { 2113 switch (*str) { 2114 case '&': 2115 retval = sbuf_printf(sb, "&"); 2116 break; 2117 case '>': 2118 retval = sbuf_printf(sb, ">"); 2119 break; 2120 case '<': 2121 retval = sbuf_printf(sb, "<"); 2122 break; 2123 default: 2124 retval = sbuf_putc(sb, *str); 2125 break; 2126 } 2127 2128 if (retval != 0) 2129 break; 2130 2131 } 2132 2133 return (retval); 2134} 2135 2136static int 2137ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2138 struct thread *td) 2139{ 2140 struct ctl_softc *softc; 2141 int retval; 2142 2143 softc = control_softc; 2144 2145 retval = 0; 2146 2147 switch (cmd) { 2148 case CTL_IO: { 2149 union ctl_io *io; 2150 void *pool_tmp; 2151 2152 /* 2153 * If we haven't been "enabled", don't allow any SCSI I/O 2154 * to this FETD. 2155 */ 2156 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2157 retval = EPERM; 2158 break; 2159 } 2160 2161 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2162 if (io == NULL) { 2163 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2164 retval = ENOSPC; 2165 break; 2166 } 2167 2168 /* 2169 * Need to save the pool reference so it doesn't get 2170 * spammed by the user's ctl_io. 2171 */ 2172 pool_tmp = io->io_hdr.pool; 2173 2174 memcpy(io, (void *)addr, sizeof(*io)); 2175 2176 io->io_hdr.pool = pool_tmp; 2177 /* 2178 * No status yet, so make sure the status is set properly. 2179 */ 2180 io->io_hdr.status = CTL_STATUS_NONE; 2181 2182 /* 2183 * The user sets the initiator ID, target and LUN IDs. 2184 */ 2185 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2186 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2187 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2188 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2189 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2190 2191 retval = ctl_ioctl_submit_wait(io); 2192 2193 if (retval != 0) { 2194 ctl_free_io(io); 2195 break; 2196 } 2197 2198 memcpy((void *)addr, io, sizeof(*io)); 2199 2200 /* return this to our pool */ 2201 ctl_free_io(io); 2202 2203 break; 2204 } 2205 case CTL_ENABLE_PORT: 2206 case CTL_DISABLE_PORT: 2207 case CTL_SET_PORT_WWNS: { 2208 struct ctl_port *port; 2209 struct ctl_port_entry *entry; 2210 2211 entry = (struct ctl_port_entry *)addr; 2212 2213 mtx_lock(&softc->ctl_lock); 2214 STAILQ_FOREACH(port, &softc->port_list, links) { 2215 int action, done; 2216 2217 action = 0; 2218 done = 0; 2219 2220 if ((entry->port_type == CTL_PORT_NONE) 2221 && (entry->targ_port == port->targ_port)) { 2222 /* 2223 * If the user only wants to enable or 2224 * disable or set WWNs on a specific port, 2225 * do the operation and we're done. 2226 */ 2227 action = 1; 2228 done = 1; 2229 } else if (entry->port_type & port->port_type) { 2230 /* 2231 * Compare the user's type mask with the 2232 * particular frontend type to see if we 2233 * have a match. 2234 */ 2235 action = 1; 2236 done = 0; 2237 2238 /* 2239 * Make sure the user isn't trying to set 2240 * WWNs on multiple ports at the same time. 2241 */ 2242 if (cmd == CTL_SET_PORT_WWNS) { 2243 printf("%s: Can't set WWNs on " 2244 "multiple ports\n", __func__); 2245 retval = EINVAL; 2246 break; 2247 } 2248 } 2249 if (action != 0) { 2250 /* 2251 * XXX KDM we have to drop the lock here, 2252 * because the online/offline operations 2253 * can potentially block. We need to 2254 * reference count the frontends so they 2255 * can't go away, 2256 */ 2257 mtx_unlock(&softc->ctl_lock); 2258 2259 if (cmd == CTL_ENABLE_PORT) { 2260 struct ctl_lun *lun; 2261 2262 STAILQ_FOREACH(lun, &softc->lun_list, 2263 links) { 2264 port->lun_enable(port->targ_lun_arg, 2265 lun->target, 2266 lun->lun); 2267 } 2268 2269 ctl_port_online(port); 2270 } else if (cmd == CTL_DISABLE_PORT) { 2271 struct ctl_lun *lun; 2272 2273 ctl_port_offline(port); 2274 2275 STAILQ_FOREACH(lun, &softc->lun_list, 2276 links) { 2277 port->lun_disable( 2278 port->targ_lun_arg, 2279 lun->target, 2280 lun->lun); 2281 } 2282 } 2283 2284 mtx_lock(&softc->ctl_lock); 2285 2286 if (cmd == CTL_SET_PORT_WWNS) 2287 ctl_port_set_wwns(port, 2288 (entry->flags & CTL_PORT_WWNN_VALID) ? 2289 1 : 0, entry->wwnn, 2290 (entry->flags & CTL_PORT_WWPN_VALID) ? 2291 1 : 0, entry->wwpn); 2292 } 2293 if (done != 0) 2294 break; 2295 } 2296 mtx_unlock(&softc->ctl_lock); 2297 break; 2298 } 2299 case CTL_GET_PORT_LIST: { 2300 struct ctl_port *port; 2301 struct ctl_port_list *list; 2302 int i; 2303 2304 list = (struct ctl_port_list *)addr; 2305 2306 if (list->alloc_len != (list->alloc_num * 2307 sizeof(struct ctl_port_entry))) { 2308 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2309 "alloc_num %u * sizeof(struct ctl_port_entry) " 2310 "%zu\n", __func__, list->alloc_len, 2311 list->alloc_num, sizeof(struct ctl_port_entry)); 2312 retval = EINVAL; 2313 break; 2314 } 2315 list->fill_len = 0; 2316 list->fill_num = 0; 2317 list->dropped_num = 0; 2318 i = 0; 2319 mtx_lock(&softc->ctl_lock); 2320 STAILQ_FOREACH(port, &softc->port_list, links) { 2321 struct ctl_port_entry entry, *list_entry; 2322 2323 if (list->fill_num >= list->alloc_num) { 2324 list->dropped_num++; 2325 continue; 2326 } 2327 2328 entry.port_type = port->port_type; 2329 strlcpy(entry.port_name, port->port_name, 2330 sizeof(entry.port_name)); 2331 entry.targ_port = port->targ_port; 2332 entry.physical_port = port->physical_port; 2333 entry.virtual_port = port->virtual_port; 2334 entry.wwnn = port->wwnn; 2335 entry.wwpn = port->wwpn; 2336 if (port->status & CTL_PORT_STATUS_ONLINE) 2337 entry.online = 1; 2338 else 2339 entry.online = 0; 2340 2341 list_entry = &list->entries[i]; 2342 2343 retval = copyout(&entry, list_entry, sizeof(entry)); 2344 if (retval != 0) { 2345 printf("%s: CTL_GET_PORT_LIST: copyout " 2346 "returned %d\n", __func__, retval); 2347 break; 2348 } 2349 i++; 2350 list->fill_num++; 2351 list->fill_len += sizeof(entry); 2352 } 2353 mtx_unlock(&softc->ctl_lock); 2354 2355 /* 2356 * If this is non-zero, we had a copyout fault, so there's 2357 * probably no point in attempting to set the status inside 2358 * the structure. 2359 */ 2360 if (retval != 0) 2361 break; 2362 2363 if (list->dropped_num > 0) 2364 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2365 else 2366 list->status = CTL_PORT_LIST_OK; 2367 break; 2368 } 2369 case CTL_DUMP_OOA: { 2370 struct ctl_lun *lun; 2371 union ctl_io *io; 2372 char printbuf[128]; 2373 struct sbuf sb; 2374 2375 mtx_lock(&softc->ctl_lock); 2376 printf("Dumping OOA queues:\n"); 2377 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2378 mtx_lock(&lun->lun_lock); 2379 for (io = (union ctl_io *)TAILQ_FIRST( 2380 &lun->ooa_queue); io != NULL; 2381 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2382 ooa_links)) { 2383 sbuf_new(&sb, printbuf, sizeof(printbuf), 2384 SBUF_FIXEDLEN); 2385 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2386 (intmax_t)lun->lun, 2387 io->scsiio.tag_num, 2388 (io->io_hdr.flags & 2389 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2390 (io->io_hdr.flags & 2391 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2392 (io->io_hdr.flags & 2393 CTL_FLAG_ABORT) ? " ABORT" : "", 2394 (io->io_hdr.flags & 2395 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2396 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2397 sbuf_finish(&sb); 2398 printf("%s\n", sbuf_data(&sb)); 2399 } 2400 mtx_unlock(&lun->lun_lock); 2401 } 2402 printf("OOA queues dump done\n"); 2403 mtx_unlock(&softc->ctl_lock); 2404 break; 2405 } 2406 case CTL_GET_OOA: { 2407 struct ctl_lun *lun; 2408 struct ctl_ooa *ooa_hdr; 2409 struct ctl_ooa_entry *entries; 2410 uint32_t cur_fill_num; 2411 2412 ooa_hdr = (struct ctl_ooa *)addr; 2413 2414 if ((ooa_hdr->alloc_len == 0) 2415 || (ooa_hdr->alloc_num == 0)) { 2416 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2417 "must be non-zero\n", __func__, 2418 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2419 retval = EINVAL; 2420 break; 2421 } 2422 2423 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2424 sizeof(struct ctl_ooa_entry))) { 2425 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2426 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2427 __func__, ooa_hdr->alloc_len, 2428 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2429 retval = EINVAL; 2430 break; 2431 } 2432 2433 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2434 if (entries == NULL) { 2435 printf("%s: could not allocate %d bytes for OOA " 2436 "dump\n", __func__, ooa_hdr->alloc_len); 2437 retval = ENOMEM; 2438 break; 2439 } 2440 2441 mtx_lock(&softc->ctl_lock); 2442 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2443 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2444 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2445 mtx_unlock(&softc->ctl_lock); 2446 free(entries, M_CTL); 2447 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2448 __func__, (uintmax_t)ooa_hdr->lun_num); 2449 retval = EINVAL; 2450 break; 2451 } 2452 2453 cur_fill_num = 0; 2454 2455 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2456 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2457 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2458 ooa_hdr, entries); 2459 if (retval != 0) 2460 break; 2461 } 2462 if (retval != 0) { 2463 mtx_unlock(&softc->ctl_lock); 2464 free(entries, M_CTL); 2465 break; 2466 } 2467 } else { 2468 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2469 2470 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2471 entries); 2472 } 2473 mtx_unlock(&softc->ctl_lock); 2474 2475 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2476 ooa_hdr->fill_len = ooa_hdr->fill_num * 2477 sizeof(struct ctl_ooa_entry); 2478 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2479 if (retval != 0) { 2480 printf("%s: error copying out %d bytes for OOA dump\n", 2481 __func__, ooa_hdr->fill_len); 2482 } 2483 2484 getbintime(&ooa_hdr->cur_bt); 2485 2486 if (cur_fill_num > ooa_hdr->alloc_num) { 2487 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2488 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2489 } else { 2490 ooa_hdr->dropped_num = 0; 2491 ooa_hdr->status = CTL_OOA_OK; 2492 } 2493 2494 free(entries, M_CTL); 2495 break; 2496 } 2497 case CTL_CHECK_OOA: { 2498 union ctl_io *io; 2499 struct ctl_lun *lun; 2500 struct ctl_ooa_info *ooa_info; 2501 2502 2503 ooa_info = (struct ctl_ooa_info *)addr; 2504 2505 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2506 ooa_info->status = CTL_OOA_INVALID_LUN; 2507 break; 2508 } 2509 mtx_lock(&softc->ctl_lock); 2510 lun = softc->ctl_luns[ooa_info->lun_id]; 2511 if (lun == NULL) { 2512 mtx_unlock(&softc->ctl_lock); 2513 ooa_info->status = CTL_OOA_INVALID_LUN; 2514 break; 2515 } 2516 mtx_lock(&lun->lun_lock); 2517 mtx_unlock(&softc->ctl_lock); 2518 ooa_info->num_entries = 0; 2519 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2520 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2521 &io->io_hdr, ooa_links)) { 2522 ooa_info->num_entries++; 2523 } 2524 mtx_unlock(&lun->lun_lock); 2525 2526 ooa_info->status = CTL_OOA_SUCCESS; 2527 2528 break; 2529 } 2530 case CTL_HARD_START: 2531 case CTL_HARD_STOP: { 2532 struct ctl_fe_ioctl_startstop_info ss_info; 2533 struct cfi_metatask *metatask; 2534 struct mtx hs_mtx; 2535 2536 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2537 2538 cv_init(&ss_info.sem, "hard start/stop cv" ); 2539 2540 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2541 if (metatask == NULL) { 2542 retval = ENOMEM; 2543 mtx_destroy(&hs_mtx); 2544 break; 2545 } 2546 2547 if (cmd == CTL_HARD_START) 2548 metatask->tasktype = CFI_TASK_STARTUP; 2549 else 2550 metatask->tasktype = CFI_TASK_SHUTDOWN; 2551 2552 metatask->callback = ctl_ioctl_hard_startstop_callback; 2553 metatask->callback_arg = &ss_info; 2554 2555 cfi_action(metatask); 2556 2557 /* Wait for the callback */ 2558 mtx_lock(&hs_mtx); 2559 cv_wait_sig(&ss_info.sem, &hs_mtx); 2560 mtx_unlock(&hs_mtx); 2561 2562 /* 2563 * All information has been copied from the metatask by the 2564 * time cv_broadcast() is called, so we free the metatask here. 2565 */ 2566 cfi_free_metatask(metatask); 2567 2568 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2569 2570 mtx_destroy(&hs_mtx); 2571 break; 2572 } 2573 case CTL_BBRREAD: { 2574 struct ctl_bbrread_info *bbr_info; 2575 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2576 struct mtx bbr_mtx; 2577 struct cfi_metatask *metatask; 2578 2579 bbr_info = (struct ctl_bbrread_info *)addr; 2580 2581 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2582 2583 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2584 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2585 2586 fe_bbr_info.bbr_info = bbr_info; 2587 fe_bbr_info.lock = &bbr_mtx; 2588 2589 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2590 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2591 2592 if (metatask == NULL) { 2593 mtx_destroy(&bbr_mtx); 2594 cv_destroy(&fe_bbr_info.sem); 2595 retval = ENOMEM; 2596 break; 2597 } 2598 metatask->tasktype = CFI_TASK_BBRREAD; 2599 metatask->callback = ctl_ioctl_bbrread_callback; 2600 metatask->callback_arg = &fe_bbr_info; 2601 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2602 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2603 metatask->taskinfo.bbrread.len = bbr_info->len; 2604 2605 cfi_action(metatask); 2606 2607 mtx_lock(&bbr_mtx); 2608 while (fe_bbr_info.wakeup_done == 0) 2609 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2610 mtx_unlock(&bbr_mtx); 2611 2612 bbr_info->status = metatask->status; 2613 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2614 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2615 memcpy(&bbr_info->sense_data, 2616 &metatask->taskinfo.bbrread.sense_data, 2617 ctl_min(sizeof(bbr_info->sense_data), 2618 sizeof(metatask->taskinfo.bbrread.sense_data))); 2619 2620 cfi_free_metatask(metatask); 2621 2622 mtx_destroy(&bbr_mtx); 2623 cv_destroy(&fe_bbr_info.sem); 2624 2625 break; 2626 } 2627 case CTL_DELAY_IO: { 2628 struct ctl_io_delay_info *delay_info; 2629#ifdef CTL_IO_DELAY 2630 struct ctl_lun *lun; 2631#endif /* CTL_IO_DELAY */ 2632 2633 delay_info = (struct ctl_io_delay_info *)addr; 2634 2635#ifdef CTL_IO_DELAY 2636 mtx_lock(&softc->ctl_lock); 2637 2638 if ((delay_info->lun_id > CTL_MAX_LUNS) 2639 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2640 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2641 } else { 2642 lun = softc->ctl_luns[delay_info->lun_id]; 2643 mtx_lock(&lun->lun_lock); 2644 2645 delay_info->status = CTL_DELAY_STATUS_OK; 2646 2647 switch (delay_info->delay_type) { 2648 case CTL_DELAY_TYPE_CONT: 2649 break; 2650 case CTL_DELAY_TYPE_ONESHOT: 2651 break; 2652 default: 2653 delay_info->status = 2654 CTL_DELAY_STATUS_INVALID_TYPE; 2655 break; 2656 } 2657 2658 switch (delay_info->delay_loc) { 2659 case CTL_DELAY_LOC_DATAMOVE: 2660 lun->delay_info.datamove_type = 2661 delay_info->delay_type; 2662 lun->delay_info.datamove_delay = 2663 delay_info->delay_secs; 2664 break; 2665 case CTL_DELAY_LOC_DONE: 2666 lun->delay_info.done_type = 2667 delay_info->delay_type; 2668 lun->delay_info.done_delay = 2669 delay_info->delay_secs; 2670 break; 2671 default: 2672 delay_info->status = 2673 CTL_DELAY_STATUS_INVALID_LOC; 2674 break; 2675 } 2676 mtx_unlock(&lun->lun_lock); 2677 } 2678 2679 mtx_unlock(&softc->ctl_lock); 2680#else 2681 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2682#endif /* CTL_IO_DELAY */ 2683 break; 2684 } 2685 case CTL_REALSYNC_SET: { 2686 int *syncstate; 2687 2688 syncstate = (int *)addr; 2689 2690 mtx_lock(&softc->ctl_lock); 2691 switch (*syncstate) { 2692 case 0: 2693 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2694 break; 2695 case 1: 2696 softc->flags |= CTL_FLAG_REAL_SYNC; 2697 break; 2698 default: 2699 retval = EINVAL; 2700 break; 2701 } 2702 mtx_unlock(&softc->ctl_lock); 2703 break; 2704 } 2705 case CTL_REALSYNC_GET: { 2706 int *syncstate; 2707 2708 syncstate = (int*)addr; 2709 2710 mtx_lock(&softc->ctl_lock); 2711 if (softc->flags & CTL_FLAG_REAL_SYNC) 2712 *syncstate = 1; 2713 else 2714 *syncstate = 0; 2715 mtx_unlock(&softc->ctl_lock); 2716 2717 break; 2718 } 2719 case CTL_SETSYNC: 2720 case CTL_GETSYNC: { 2721 struct ctl_sync_info *sync_info; 2722 struct ctl_lun *lun; 2723 2724 sync_info = (struct ctl_sync_info *)addr; 2725 2726 mtx_lock(&softc->ctl_lock); 2727 lun = softc->ctl_luns[sync_info->lun_id]; 2728 if (lun == NULL) { 2729 mtx_unlock(&softc->ctl_lock); 2730 sync_info->status = CTL_GS_SYNC_NO_LUN; 2731 } 2732 /* 2733 * Get or set the sync interval. We're not bounds checking 2734 * in the set case, hopefully the user won't do something 2735 * silly. 2736 */ 2737 mtx_lock(&lun->lun_lock); 2738 mtx_unlock(&softc->ctl_lock); 2739 if (cmd == CTL_GETSYNC) 2740 sync_info->sync_interval = lun->sync_interval; 2741 else 2742 lun->sync_interval = sync_info->sync_interval; 2743 mtx_unlock(&lun->lun_lock); 2744 2745 sync_info->status = CTL_GS_SYNC_OK; 2746 2747 break; 2748 } 2749 case CTL_GETSTATS: { 2750 struct ctl_stats *stats; 2751 struct ctl_lun *lun; 2752 int i; 2753 2754 stats = (struct ctl_stats *)addr; 2755 2756 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2757 stats->alloc_len) { 2758 stats->status = CTL_SS_NEED_MORE_SPACE; 2759 stats->num_luns = softc->num_luns; 2760 break; 2761 } 2762 /* 2763 * XXX KDM no locking here. If the LUN list changes, 2764 * things can blow up. 2765 */ 2766 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2767 i++, lun = STAILQ_NEXT(lun, links)) { 2768 retval = copyout(&lun->stats, &stats->lun_stats[i], 2769 sizeof(lun->stats)); 2770 if (retval != 0) 2771 break; 2772 } 2773 stats->num_luns = softc->num_luns; 2774 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2775 softc->num_luns; 2776 stats->status = CTL_SS_OK; 2777#ifdef CTL_TIME_IO 2778 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2779#else 2780 stats->flags = CTL_STATS_FLAG_NONE; 2781#endif 2782 getnanouptime(&stats->timestamp); 2783 break; 2784 } 2785 case CTL_ERROR_INJECT: { 2786 struct ctl_error_desc *err_desc, *new_err_desc; 2787 struct ctl_lun *lun; 2788 2789 err_desc = (struct ctl_error_desc *)addr; 2790 2791 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2792 M_WAITOK | M_ZERO); 2793 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2794 2795 mtx_lock(&softc->ctl_lock); 2796 lun = softc->ctl_luns[err_desc->lun_id]; 2797 if (lun == NULL) { 2798 mtx_unlock(&softc->ctl_lock); 2799 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2800 __func__, (uintmax_t)err_desc->lun_id); 2801 retval = EINVAL; 2802 break; 2803 } 2804 mtx_lock(&lun->lun_lock); 2805 mtx_unlock(&softc->ctl_lock); 2806 2807 /* 2808 * We could do some checking here to verify the validity 2809 * of the request, but given the complexity of error 2810 * injection requests, the checking logic would be fairly 2811 * complex. 2812 * 2813 * For now, if the request is invalid, it just won't get 2814 * executed and might get deleted. 2815 */ 2816 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2817 2818 /* 2819 * XXX KDM check to make sure the serial number is unique, 2820 * in case we somehow manage to wrap. That shouldn't 2821 * happen for a very long time, but it's the right thing to 2822 * do. 2823 */ 2824 new_err_desc->serial = lun->error_serial; 2825 err_desc->serial = lun->error_serial; 2826 lun->error_serial++; 2827 2828 mtx_unlock(&lun->lun_lock); 2829 break; 2830 } 2831 case CTL_ERROR_INJECT_DELETE: { 2832 struct ctl_error_desc *delete_desc, *desc, *desc2; 2833 struct ctl_lun *lun; 2834 int delete_done; 2835 2836 delete_desc = (struct ctl_error_desc *)addr; 2837 delete_done = 0; 2838 2839 mtx_lock(&softc->ctl_lock); 2840 lun = softc->ctl_luns[delete_desc->lun_id]; 2841 if (lun == NULL) { 2842 mtx_unlock(&softc->ctl_lock); 2843 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2844 __func__, (uintmax_t)delete_desc->lun_id); 2845 retval = EINVAL; 2846 break; 2847 } 2848 mtx_lock(&lun->lun_lock); 2849 mtx_unlock(&softc->ctl_lock); 2850 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2851 if (desc->serial != delete_desc->serial) 2852 continue; 2853 2854 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2855 links); 2856 free(desc, M_CTL); 2857 delete_done = 1; 2858 } 2859 mtx_unlock(&lun->lun_lock); 2860 if (delete_done == 0) { 2861 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2862 "error serial %ju on LUN %u\n", __func__, 2863 delete_desc->serial, delete_desc->lun_id); 2864 retval = EINVAL; 2865 break; 2866 } 2867 break; 2868 } 2869 case CTL_DUMP_STRUCTS: { 2870 int i, j, k; 2871 struct ctl_port *port; 2872 struct ctl_frontend *fe; 2873 2874 printf("CTL IID to WWPN map start:\n"); 2875 for (i = 0; i < CTL_MAX_PORTS; i++) { 2876 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2877 if (softc->wwpn_iid[i][j].in_use == 0) 2878 continue; 2879 2880 printf("port %d iid %u WWPN %#jx\n", 2881 softc->wwpn_iid[i][j].port, 2882 softc->wwpn_iid[i][j].iid, 2883 (uintmax_t)softc->wwpn_iid[i][j].wwpn); 2884 } 2885 } 2886 printf("CTL IID to WWPN map end\n"); 2887 printf("CTL Persistent Reservation information start:\n"); 2888 for (i = 0; i < CTL_MAX_LUNS; i++) { 2889 struct ctl_lun *lun; 2890 2891 lun = softc->ctl_luns[i]; 2892 2893 if ((lun == NULL) 2894 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2895 continue; 2896 2897 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2898 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2899 if (lun->per_res[j+k].registered == 0) 2900 continue; 2901 printf("LUN %d port %d iid %d key " 2902 "%#jx\n", i, j, k, 2903 (uintmax_t)scsi_8btou64( 2904 lun->per_res[j+k].res_key.key)); 2905 } 2906 } 2907 } 2908 printf("CTL Persistent Reservation information end\n"); 2909 printf("CTL Ports:\n"); 2910 /* 2911 * XXX KDM calling this without a lock. We'd likely want 2912 * to drop the lock before calling the frontend's dump 2913 * routine anyway. 2914 */ 2915 STAILQ_FOREACH(port, &softc->port_list, links) { 2916 printf("Port %s Frontend %s Type %u pport %d vport %d WWNN " 2917 "%#jx WWPN %#jx\n", port->port_name, 2918 port->frontend->name, port->port_type, 2919 port->physical_port, port->virtual_port, 2920 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2921 } 2922 printf("CTL Port information end\n"); 2923 printf("CTL Frontends:\n"); 2924 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2925 printf("Frontend %s\n", fe->name); 2926 if (fe->fe_dump != NULL) 2927 fe->fe_dump(); 2928 } 2929 printf("CTL Frontend information end\n"); 2930 break; 2931 } 2932 case CTL_LUN_REQ: { 2933 struct ctl_lun_req *lun_req; 2934 struct ctl_backend_driver *backend; 2935 2936 lun_req = (struct ctl_lun_req *)addr; 2937 2938 backend = ctl_backend_find(lun_req->backend); 2939 if (backend == NULL) { 2940 lun_req->status = CTL_LUN_ERROR; 2941 snprintf(lun_req->error_str, 2942 sizeof(lun_req->error_str), 2943 "Backend \"%s\" not found.", 2944 lun_req->backend); 2945 break; 2946 } 2947 if (lun_req->num_be_args > 0) { 2948 lun_req->kern_be_args = ctl_copyin_args( 2949 lun_req->num_be_args, 2950 lun_req->be_args, 2951 lun_req->error_str, 2952 sizeof(lun_req->error_str)); 2953 if (lun_req->kern_be_args == NULL) { 2954 lun_req->status = CTL_LUN_ERROR; 2955 break; 2956 } 2957 } 2958 2959 retval = backend->ioctl(dev, cmd, addr, flag, td); 2960 2961 if (lun_req->num_be_args > 0) { 2962 ctl_copyout_args(lun_req->num_be_args, 2963 lun_req->kern_be_args); 2964 ctl_free_args(lun_req->num_be_args, 2965 lun_req->kern_be_args); 2966 } 2967 break; 2968 } 2969 case CTL_LUN_LIST: { 2970 struct sbuf *sb; 2971 struct ctl_lun *lun; 2972 struct ctl_lun_list *list; 2973 struct ctl_option *opt; 2974 2975 list = (struct ctl_lun_list *)addr; 2976 2977 /* 2978 * Allocate a fixed length sbuf here, based on the length 2979 * of the user's buffer. We could allocate an auto-extending 2980 * buffer, and then tell the user how much larger our 2981 * amount of data is than his buffer, but that presents 2982 * some problems: 2983 * 2984 * 1. The sbuf(9) routines use a blocking malloc, and so 2985 * we can't hold a lock while calling them with an 2986 * auto-extending buffer. 2987 * 2988 * 2. There is not currently a LUN reference counting 2989 * mechanism, outside of outstanding transactions on 2990 * the LUN's OOA queue. So a LUN could go away on us 2991 * while we're getting the LUN number, backend-specific 2992 * information, etc. Thus, given the way things 2993 * currently work, we need to hold the CTL lock while 2994 * grabbing LUN information. 2995 * 2996 * So, from the user's standpoint, the best thing to do is 2997 * allocate what he thinks is a reasonable buffer length, 2998 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 2999 * double the buffer length and try again. (And repeat 3000 * that until he succeeds.) 3001 */ 3002 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3003 if (sb == NULL) { 3004 list->status = CTL_LUN_LIST_ERROR; 3005 snprintf(list->error_str, sizeof(list->error_str), 3006 "Unable to allocate %d bytes for LUN list", 3007 list->alloc_len); 3008 break; 3009 } 3010 3011 sbuf_printf(sb, "<ctllunlist>\n"); 3012 3013 mtx_lock(&softc->ctl_lock); 3014 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3015 mtx_lock(&lun->lun_lock); 3016 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3017 (uintmax_t)lun->lun); 3018 3019 /* 3020 * Bail out as soon as we see that we've overfilled 3021 * the buffer. 3022 */ 3023 if (retval != 0) 3024 break; 3025 3026 retval = sbuf_printf(sb, "\t<backend_type>%s" 3027 "</backend_type>\n", 3028 (lun->backend == NULL) ? "none" : 3029 lun->backend->name); 3030 3031 if (retval != 0) 3032 break; 3033 3034 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3035 lun->be_lun->lun_type); 3036 3037 if (retval != 0) 3038 break; 3039 3040 if (lun->backend == NULL) { 3041 retval = sbuf_printf(sb, "</lun>\n"); 3042 if (retval != 0) 3043 break; 3044 continue; 3045 } 3046 3047 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3048 (lun->be_lun->maxlba > 0) ? 3049 lun->be_lun->maxlba + 1 : 0); 3050 3051 if (retval != 0) 3052 break; 3053 3054 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3055 lun->be_lun->blocksize); 3056 3057 if (retval != 0) 3058 break; 3059 3060 retval = sbuf_printf(sb, "\t<serial_number>"); 3061 3062 if (retval != 0) 3063 break; 3064 3065 retval = ctl_sbuf_printf_esc(sb, 3066 lun->be_lun->serial_num); 3067 3068 if (retval != 0) 3069 break; 3070 3071 retval = sbuf_printf(sb, "</serial_number>\n"); 3072 3073 if (retval != 0) 3074 break; 3075 3076 retval = sbuf_printf(sb, "\t<device_id>"); 3077 3078 if (retval != 0) 3079 break; 3080 3081 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3082 3083 if (retval != 0) 3084 break; 3085 3086 retval = sbuf_printf(sb, "</device_id>\n"); 3087 3088 if (retval != 0) 3089 break; 3090 3091 if (lun->backend->lun_info != NULL) { 3092 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3093 if (retval != 0) 3094 break; 3095 } 3096 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3097 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3098 opt->name, opt->value, opt->name); 3099 if (retval != 0) 3100 break; 3101 } 3102 3103 retval = sbuf_printf(sb, "</lun>\n"); 3104 3105 if (retval != 0) 3106 break; 3107 mtx_unlock(&lun->lun_lock); 3108 } 3109 if (lun != NULL) 3110 mtx_unlock(&lun->lun_lock); 3111 mtx_unlock(&softc->ctl_lock); 3112 3113 if ((retval != 0) 3114 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3115 retval = 0; 3116 sbuf_delete(sb); 3117 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3118 snprintf(list->error_str, sizeof(list->error_str), 3119 "Out of space, %d bytes is too small", 3120 list->alloc_len); 3121 break; 3122 } 3123 3124 sbuf_finish(sb); 3125 3126 retval = copyout(sbuf_data(sb), list->lun_xml, 3127 sbuf_len(sb) + 1); 3128 3129 list->fill_len = sbuf_len(sb) + 1; 3130 list->status = CTL_LUN_LIST_OK; 3131 sbuf_delete(sb); 3132 break; 3133 } 3134 case CTL_ISCSI: { 3135 struct ctl_iscsi *ci; 3136 struct ctl_frontend *fe; 3137 3138 ci = (struct ctl_iscsi *)addr; 3139 3140 fe = ctl_frontend_find("iscsi"); 3141 if (fe == NULL) { 3142 ci->status = CTL_ISCSI_ERROR; 3143 snprintf(ci->error_str, sizeof(ci->error_str), 3144 "Frontend \"iscsi\" not found."); 3145 break; 3146 } 3147 3148 retval = fe->ioctl(dev, cmd, addr, flag, td); 3149 break; 3150 } 3151 case CTL_PORT_REQ: { 3152 struct ctl_req *req; 3153 struct ctl_frontend *fe; 3154 3155 req = (struct ctl_req *)addr; 3156 3157 fe = ctl_frontend_find(req->driver); 3158 if (fe == NULL) { 3159 req->status = CTL_LUN_ERROR; 3160 snprintf(req->error_str, sizeof(req->error_str), 3161 "Frontend \"%s\" not found.", req->driver); 3162 break; 3163 } 3164 if (req->num_args > 0) { 3165 req->kern_args = ctl_copyin_args(req->num_args, 3166 req->args, req->error_str, sizeof(req->error_str)); 3167 if (req->kern_args == NULL) { 3168 req->status = CTL_LUN_ERROR; 3169 break; 3170 } 3171 } 3172 3173 retval = fe->ioctl(dev, cmd, addr, flag, td); 3174 3175 if (req->num_args > 0) { 3176 ctl_copyout_args(req->num_args, req->kern_args); 3177 ctl_free_args(req->num_args, req->kern_args); 3178 } 3179 break; 3180 } 3181 case CTL_PORT_LIST: { 3182 struct sbuf *sb; 3183 struct ctl_port *port; 3184 struct ctl_lun_list *list; 3185 struct ctl_option *opt; 3186 3187 list = (struct ctl_lun_list *)addr; 3188 3189 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3190 if (sb == NULL) { 3191 list->status = CTL_LUN_LIST_ERROR; 3192 snprintf(list->error_str, sizeof(list->error_str), 3193 "Unable to allocate %d bytes for LUN list", 3194 list->alloc_len); 3195 break; 3196 } 3197 3198 sbuf_printf(sb, "<ctlportlist>\n"); 3199 3200 mtx_lock(&softc->ctl_lock); 3201 STAILQ_FOREACH(port, &softc->port_list, links) { 3202 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3203 (uintmax_t)port->targ_port); 3204 3205 /* 3206 * Bail out as soon as we see that we've overfilled 3207 * the buffer. 3208 */ 3209 if (retval != 0) 3210 break; 3211 3212 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3213 "</frontend_type>\n", port->frontend->name); 3214 if (retval != 0) 3215 break; 3216 3217 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3218 port->port_type); 3219 if (retval != 0) 3220 break; 3221 3222 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3223 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3224 if (retval != 0) 3225 break; 3226 3227 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3228 port->port_name); 3229 if (retval != 0) 3230 break; 3231 3232 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3233 port->physical_port); 3234 if (retval != 0) 3235 break; 3236 3237 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3238 port->virtual_port); 3239 if (retval != 0) 3240 break; 3241 3242 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n", 3243 (uintmax_t)port->wwnn); 3244 if (retval != 0) 3245 break; 3246 3247 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n", 3248 (uintmax_t)port->wwpn); 3249 if (retval != 0) 3250 break; 3251 3252 STAILQ_FOREACH(opt, &port->options, links) { 3253 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3254 opt->name, opt->value, opt->name); 3255 if (retval != 0) 3256 break; 3257 } 3258 3259 retval = sbuf_printf(sb, "</targ_port>\n"); 3260 if (retval != 0) 3261 break; 3262 } 3263 mtx_unlock(&softc->ctl_lock); 3264 3265 if ((retval != 0) 3266 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3267 retval = 0; 3268 sbuf_delete(sb); 3269 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3270 snprintf(list->error_str, sizeof(list->error_str), 3271 "Out of space, %d bytes is too small", 3272 list->alloc_len); 3273 break; 3274 } 3275 3276 sbuf_finish(sb); 3277 3278 retval = copyout(sbuf_data(sb), list->lun_xml, 3279 sbuf_len(sb) + 1); 3280 3281 list->fill_len = sbuf_len(sb) + 1; 3282 list->status = CTL_LUN_LIST_OK; 3283 sbuf_delete(sb); 3284 break; 3285 } 3286 default: { 3287 /* XXX KDM should we fix this? */ 3288#if 0 3289 struct ctl_backend_driver *backend; 3290 unsigned int type; 3291 int found; 3292 3293 found = 0; 3294 3295 /* 3296 * We encode the backend type as the ioctl type for backend 3297 * ioctls. So parse it out here, and then search for a 3298 * backend of this type. 3299 */ 3300 type = _IOC_TYPE(cmd); 3301 3302 STAILQ_FOREACH(backend, &softc->be_list, links) { 3303 if (backend->type == type) { 3304 found = 1; 3305 break; 3306 } 3307 } 3308 if (found == 0) { 3309 printf("ctl: unknown ioctl command %#lx or backend " 3310 "%d\n", cmd, type); 3311 retval = EINVAL; 3312 break; 3313 } 3314 retval = backend->ioctl(dev, cmd, addr, flag, td); 3315#endif 3316 retval = ENOTTY; 3317 break; 3318 } 3319 } 3320 return (retval); 3321} 3322 3323uint32_t 3324ctl_get_initindex(struct ctl_nexus *nexus) 3325{ 3326 if (nexus->targ_port < CTL_MAX_PORTS) 3327 return (nexus->initid.id + 3328 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3329 else 3330 return (nexus->initid.id + 3331 ((nexus->targ_port - CTL_MAX_PORTS) * 3332 CTL_MAX_INIT_PER_PORT)); 3333} 3334 3335uint32_t 3336ctl_get_resindex(struct ctl_nexus *nexus) 3337{ 3338 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3339} 3340 3341uint32_t 3342ctl_port_idx(int port_num) 3343{ 3344 if (port_num < CTL_MAX_PORTS) 3345 return(port_num); 3346 else 3347 return(port_num - CTL_MAX_PORTS); 3348} 3349 3350/* 3351 * Note: This only works for bitmask sizes that are at least 32 bits, and 3352 * that are a power of 2. 3353 */ 3354int 3355ctl_ffz(uint32_t *mask, uint32_t size) 3356{ 3357 uint32_t num_chunks, num_pieces; 3358 int i, j; 3359 3360 num_chunks = (size >> 5); 3361 if (num_chunks == 0) 3362 num_chunks++; 3363 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3364 3365 for (i = 0; i < num_chunks; i++) { 3366 for (j = 0; j < num_pieces; j++) { 3367 if ((mask[i] & (1 << j)) == 0) 3368 return ((i << 5) + j); 3369 } 3370 } 3371 3372 return (-1); 3373} 3374 3375int 3376ctl_set_mask(uint32_t *mask, uint32_t bit) 3377{ 3378 uint32_t chunk, piece; 3379 3380 chunk = bit >> 5; 3381 piece = bit % (sizeof(uint32_t) * 8); 3382 3383 if ((mask[chunk] & (1 << piece)) != 0) 3384 return (-1); 3385 else 3386 mask[chunk] |= (1 << piece); 3387 3388 return (0); 3389} 3390 3391int 3392ctl_clear_mask(uint32_t *mask, uint32_t bit) 3393{ 3394 uint32_t chunk, piece; 3395 3396 chunk = bit >> 5; 3397 piece = bit % (sizeof(uint32_t) * 8); 3398 3399 if ((mask[chunk] & (1 << piece)) == 0) 3400 return (-1); 3401 else 3402 mask[chunk] &= ~(1 << piece); 3403 3404 return (0); 3405} 3406 3407int 3408ctl_is_set(uint32_t *mask, uint32_t bit) 3409{ 3410 uint32_t chunk, piece; 3411 3412 chunk = bit >> 5; 3413 piece = bit % (sizeof(uint32_t) * 8); 3414 3415 if ((mask[chunk] & (1 << piece)) == 0) 3416 return (0); 3417 else 3418 return (1); 3419} 3420 3421#ifdef unused 3422/* 3423 * The bus, target and lun are optional, they can be filled in later. 3424 * can_wait is used to determine whether we can wait on the malloc or not. 3425 */ 3426union ctl_io* 3427ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3428 uint32_t targ_lun, int can_wait) 3429{ 3430 union ctl_io *io; 3431 3432 if (can_wait) 3433 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3434 else 3435 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3436 3437 if (io != NULL) { 3438 io->io_hdr.io_type = io_type; 3439 io->io_hdr.targ_port = targ_port; 3440 /* 3441 * XXX KDM this needs to change/go away. We need to move 3442 * to a preallocated pool of ctl_scsiio structures. 3443 */ 3444 io->io_hdr.nexus.targ_target.id = targ_target; 3445 io->io_hdr.nexus.targ_lun = targ_lun; 3446 } 3447 3448 return (io); 3449} 3450 3451void 3452ctl_kfree_io(union ctl_io *io) 3453{ 3454 free(io, M_CTL); 3455} 3456#endif /* unused */ 3457 3458/* 3459 * ctl_softc, pool_type, total_ctl_io are passed in. 3460 * npool is passed out. 3461 */ 3462int 3463ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3464 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3465{ 3466 uint32_t i; 3467 union ctl_io *cur_io, *next_io; 3468 struct ctl_io_pool *pool; 3469 int retval; 3470 3471 retval = 0; 3472 3473 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3474 M_NOWAIT | M_ZERO); 3475 if (pool == NULL) { 3476 retval = ENOMEM; 3477 goto bailout; 3478 } 3479 3480 pool->type = pool_type; 3481 pool->ctl_softc = ctl_softc; 3482 3483 mtx_lock(&ctl_softc->pool_lock); 3484 pool->id = ctl_softc->cur_pool_id++; 3485 mtx_unlock(&ctl_softc->pool_lock); 3486 3487 pool->flags = CTL_POOL_FLAG_NONE; 3488 pool->refcount = 1; /* Reference for validity. */ 3489 STAILQ_INIT(&pool->free_queue); 3490 3491 /* 3492 * XXX KDM other options here: 3493 * - allocate a page at a time 3494 * - allocate one big chunk of memory. 3495 * Page allocation might work well, but would take a little more 3496 * tracking. 3497 */ 3498 for (i = 0; i < total_ctl_io; i++) { 3499 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3500 M_NOWAIT); 3501 if (cur_io == NULL) { 3502 retval = ENOMEM; 3503 break; 3504 } 3505 cur_io->io_hdr.pool = pool; 3506 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3507 pool->total_ctl_io++; 3508 pool->free_ctl_io++; 3509 } 3510 3511 if (retval != 0) { 3512 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3513 cur_io != NULL; cur_io = next_io) { 3514 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3515 links); 3516 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3517 ctl_io_hdr, links); 3518 free(cur_io, M_CTLIO); 3519 } 3520 3521 free(pool, M_CTL); 3522 goto bailout; 3523 } 3524 mtx_lock(&ctl_softc->pool_lock); 3525 ctl_softc->num_pools++; 3526 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3527 /* 3528 * Increment our usage count if this is an external consumer, so we 3529 * can't get unloaded until the external consumer (most likely a 3530 * FETD) unloads and frees his pool. 3531 * 3532 * XXX KDM will this increment the caller's module use count, or 3533 * mine? 3534 */ 3535#if 0 3536 if ((pool_type != CTL_POOL_EMERGENCY) 3537 && (pool_type != CTL_POOL_INTERNAL) 3538 && (pool_type != CTL_POOL_4OTHERSC)) 3539 MOD_INC_USE_COUNT; 3540#endif 3541 3542 mtx_unlock(&ctl_softc->pool_lock); 3543 3544 *npool = pool; 3545 3546bailout: 3547 3548 return (retval); 3549} 3550 3551static int 3552ctl_pool_acquire(struct ctl_io_pool *pool) 3553{ 3554 3555 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3556 3557 if (pool->flags & CTL_POOL_FLAG_INVALID) 3558 return (EINVAL); 3559 3560 pool->refcount++; 3561 3562 return (0); 3563} 3564 3565static void 3566ctl_pool_release(struct ctl_io_pool *pool) 3567{ 3568 struct ctl_softc *ctl_softc = pool->ctl_softc; 3569 union ctl_io *io; 3570 3571 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3572 3573 if (--pool->refcount != 0) 3574 return; 3575 3576 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3577 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3578 links); 3579 free(io, M_CTLIO); 3580 } 3581 3582 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3583 ctl_softc->num_pools--; 3584 3585 /* 3586 * XXX KDM will this decrement the caller's usage count or mine? 3587 */ 3588#if 0 3589 if ((pool->type != CTL_POOL_EMERGENCY) 3590 && (pool->type != CTL_POOL_INTERNAL) 3591 && (pool->type != CTL_POOL_4OTHERSC)) 3592 MOD_DEC_USE_COUNT; 3593#endif 3594 3595 free(pool, M_CTL); 3596} 3597 3598void 3599ctl_pool_free(struct ctl_io_pool *pool) 3600{ 3601 struct ctl_softc *ctl_softc; 3602 3603 if (pool == NULL) 3604 return; 3605 3606 ctl_softc = pool->ctl_softc; 3607 mtx_lock(&ctl_softc->pool_lock); 3608 pool->flags |= CTL_POOL_FLAG_INVALID; 3609 ctl_pool_release(pool); 3610 mtx_unlock(&ctl_softc->pool_lock); 3611} 3612 3613/* 3614 * This routine does not block (except for spinlocks of course). 3615 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3616 * possible. 3617 */ 3618union ctl_io * 3619ctl_alloc_io(void *pool_ref) 3620{ 3621 union ctl_io *io; 3622 struct ctl_softc *ctl_softc; 3623 struct ctl_io_pool *pool, *npool; 3624 struct ctl_io_pool *emergency_pool; 3625 3626 pool = (struct ctl_io_pool *)pool_ref; 3627 3628 if (pool == NULL) { 3629 printf("%s: pool is NULL\n", __func__); 3630 return (NULL); 3631 } 3632 3633 emergency_pool = NULL; 3634 3635 ctl_softc = pool->ctl_softc; 3636 3637 mtx_lock(&ctl_softc->pool_lock); 3638 /* 3639 * First, try to get the io structure from the user's pool. 3640 */ 3641 if (ctl_pool_acquire(pool) == 0) { 3642 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3643 if (io != NULL) { 3644 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3645 pool->total_allocated++; 3646 pool->free_ctl_io--; 3647 mtx_unlock(&ctl_softc->pool_lock); 3648 return (io); 3649 } else 3650 ctl_pool_release(pool); 3651 } 3652 /* 3653 * If he doesn't have any io structures left, search for an 3654 * emergency pool and grab one from there. 3655 */ 3656 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3657 if (npool->type != CTL_POOL_EMERGENCY) 3658 continue; 3659 3660 if (ctl_pool_acquire(npool) != 0) 3661 continue; 3662 3663 emergency_pool = npool; 3664 3665 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3666 if (io != NULL) { 3667 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3668 npool->total_allocated++; 3669 npool->free_ctl_io--; 3670 mtx_unlock(&ctl_softc->pool_lock); 3671 return (io); 3672 } else 3673 ctl_pool_release(npool); 3674 } 3675 3676 /* Drop the spinlock before we malloc */ 3677 mtx_unlock(&ctl_softc->pool_lock); 3678 3679 /* 3680 * The emergency pool (if it exists) didn't have one, so try an 3681 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3682 */ 3683 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3684 if (io != NULL) { 3685 /* 3686 * If the emergency pool exists but is empty, add this 3687 * ctl_io to its list when it gets freed. 3688 */ 3689 if (emergency_pool != NULL) { 3690 mtx_lock(&ctl_softc->pool_lock); 3691 if (ctl_pool_acquire(emergency_pool) == 0) { 3692 io->io_hdr.pool = emergency_pool; 3693 emergency_pool->total_ctl_io++; 3694 /* 3695 * Need to bump this, otherwise 3696 * total_allocated and total_freed won't 3697 * match when we no longer have anything 3698 * outstanding. 3699 */ 3700 emergency_pool->total_allocated++; 3701 } 3702 mtx_unlock(&ctl_softc->pool_lock); 3703 } else 3704 io->io_hdr.pool = NULL; 3705 } 3706 3707 return (io); 3708} 3709 3710void 3711ctl_free_io(union ctl_io *io) 3712{ 3713 if (io == NULL) 3714 return; 3715 3716 /* 3717 * If this ctl_io has a pool, return it to that pool. 3718 */ 3719 if (io->io_hdr.pool != NULL) { 3720 struct ctl_io_pool *pool; 3721 3722 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3723 mtx_lock(&pool->ctl_softc->pool_lock); 3724 io->io_hdr.io_type = 0xff; 3725 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3726 pool->total_freed++; 3727 pool->free_ctl_io++; 3728 ctl_pool_release(pool); 3729 mtx_unlock(&pool->ctl_softc->pool_lock); 3730 } else { 3731 /* 3732 * Otherwise, just free it. We probably malloced it and 3733 * the emergency pool wasn't available. 3734 */ 3735 free(io, M_CTLIO); 3736 } 3737 3738} 3739 3740void 3741ctl_zero_io(union ctl_io *io) 3742{ 3743 void *pool_ref; 3744 3745 if (io == NULL) 3746 return; 3747 3748 /* 3749 * May need to preserve linked list pointers at some point too. 3750 */ 3751 pool_ref = io->io_hdr.pool; 3752 3753 memset(io, 0, sizeof(*io)); 3754 3755 io->io_hdr.pool = pool_ref; 3756} 3757 3758/* 3759 * This routine is currently used for internal copies of ctl_ios that need 3760 * to persist for some reason after we've already returned status to the 3761 * FETD. (Thus the flag set.) 3762 * 3763 * XXX XXX 3764 * Note that this makes a blind copy of all fields in the ctl_io, except 3765 * for the pool reference. This includes any memory that has been 3766 * allocated! That memory will no longer be valid after done has been 3767 * called, so this would be VERY DANGEROUS for command that actually does 3768 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3769 * start and stop commands, which don't transfer any data, so this is not a 3770 * problem. If it is used for anything else, the caller would also need to 3771 * allocate data buffer space and this routine would need to be modified to 3772 * copy the data buffer(s) as well. 3773 */ 3774void 3775ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3776{ 3777 void *pool_ref; 3778 3779 if ((src == NULL) 3780 || (dest == NULL)) 3781 return; 3782 3783 /* 3784 * May need to preserve linked list pointers at some point too. 3785 */ 3786 pool_ref = dest->io_hdr.pool; 3787 3788 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3789 3790 dest->io_hdr.pool = pool_ref; 3791 /* 3792 * We need to know that this is an internal copy, and doesn't need 3793 * to get passed back to the FETD that allocated it. 3794 */ 3795 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3796} 3797 3798#ifdef NEEDTOPORT 3799static void 3800ctl_update_power_subpage(struct copan_power_subpage *page) 3801{ 3802 int num_luns, num_partitions, config_type; 3803 struct ctl_softc *softc; 3804 cs_BOOL_t aor_present, shelf_50pct_power; 3805 cs_raidset_personality_t rs_type; 3806 int max_active_luns; 3807 3808 softc = control_softc; 3809 3810 /* subtract out the processor LUN */ 3811 num_luns = softc->num_luns - 1; 3812 /* 3813 * Default to 7 LUNs active, which was the only number we allowed 3814 * in the past. 3815 */ 3816 max_active_luns = 7; 3817 3818 num_partitions = config_GetRsPartitionInfo(); 3819 config_type = config_GetConfigType(); 3820 shelf_50pct_power = config_GetShelfPowerMode(); 3821 aor_present = config_IsAorRsPresent(); 3822 3823 rs_type = ddb_GetRsRaidType(1); 3824 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3825 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3826 EPRINT(0, "Unsupported RS type %d!", rs_type); 3827 } 3828 3829 3830 page->total_luns = num_luns; 3831 3832 switch (config_type) { 3833 case 40: 3834 /* 3835 * In a 40 drive configuration, it doesn't matter what DC 3836 * cards we have, whether we have AOR enabled or not, 3837 * partitioning or not, or what type of RAIDset we have. 3838 * In that scenario, we can power up every LUN we present 3839 * to the user. 3840 */ 3841 max_active_luns = num_luns; 3842 3843 break; 3844 case 64: 3845 if (shelf_50pct_power == CS_FALSE) { 3846 /* 25% power */ 3847 if (aor_present == CS_TRUE) { 3848 if (rs_type == 3849 CS_RAIDSET_PERSONALITY_RAID5) { 3850 max_active_luns = 7; 3851 } else if (rs_type == 3852 CS_RAIDSET_PERSONALITY_RAID1){ 3853 max_active_luns = 14; 3854 } else { 3855 /* XXX KDM now what?? */ 3856 } 3857 } else { 3858 if (rs_type == 3859 CS_RAIDSET_PERSONALITY_RAID5) { 3860 max_active_luns = 8; 3861 } else if (rs_type == 3862 CS_RAIDSET_PERSONALITY_RAID1){ 3863 max_active_luns = 16; 3864 } else { 3865 /* XXX KDM now what?? */ 3866 } 3867 } 3868 } else { 3869 /* 50% power */ 3870 /* 3871 * With 50% power in a 64 drive configuration, we 3872 * can power all LUNs we present. 3873 */ 3874 max_active_luns = num_luns; 3875 } 3876 break; 3877 case 112: 3878 if (shelf_50pct_power == CS_FALSE) { 3879 /* 25% power */ 3880 if (aor_present == CS_TRUE) { 3881 if (rs_type == 3882 CS_RAIDSET_PERSONALITY_RAID5) { 3883 max_active_luns = 7; 3884 } else if (rs_type == 3885 CS_RAIDSET_PERSONALITY_RAID1){ 3886 max_active_luns = 14; 3887 } else { 3888 /* XXX KDM now what?? */ 3889 } 3890 } else { 3891 if (rs_type == 3892 CS_RAIDSET_PERSONALITY_RAID5) { 3893 max_active_luns = 8; 3894 } else if (rs_type == 3895 CS_RAIDSET_PERSONALITY_RAID1){ 3896 max_active_luns = 16; 3897 } else { 3898 /* XXX KDM now what?? */ 3899 } 3900 } 3901 } else { 3902 /* 50% power */ 3903 if (aor_present == CS_TRUE) { 3904 if (rs_type == 3905 CS_RAIDSET_PERSONALITY_RAID5) { 3906 max_active_luns = 14; 3907 } else if (rs_type == 3908 CS_RAIDSET_PERSONALITY_RAID1){ 3909 /* 3910 * We're assuming here that disk 3911 * caching is enabled, and so we're 3912 * able to power up half of each 3913 * LUN, and cache all writes. 3914 */ 3915 max_active_luns = num_luns; 3916 } else { 3917 /* XXX KDM now what?? */ 3918 } 3919 } else { 3920 if (rs_type == 3921 CS_RAIDSET_PERSONALITY_RAID5) { 3922 max_active_luns = 15; 3923 } else if (rs_type == 3924 CS_RAIDSET_PERSONALITY_RAID1){ 3925 max_active_luns = 30; 3926 } else { 3927 /* XXX KDM now what?? */ 3928 } 3929 } 3930 } 3931 break; 3932 default: 3933 /* 3934 * In this case, we have an unknown configuration, so we 3935 * just use the default from above. 3936 */ 3937 break; 3938 } 3939 3940 page->max_active_luns = max_active_luns; 3941#if 0 3942 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 3943 page->total_luns, page->max_active_luns); 3944#endif 3945} 3946#endif /* NEEDTOPORT */ 3947 3948/* 3949 * This routine could be used in the future to load default and/or saved 3950 * mode page parameters for a particuar lun. 3951 */ 3952static int 3953ctl_init_page_index(struct ctl_lun *lun) 3954{ 3955 int i; 3956 struct ctl_page_index *page_index; 3957 struct ctl_softc *softc; 3958 3959 memcpy(&lun->mode_pages.index, page_index_template, 3960 sizeof(page_index_template)); 3961 3962 softc = lun->ctl_softc; 3963 3964 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3965 3966 page_index = &lun->mode_pages.index[i]; 3967 /* 3968 * If this is a disk-only mode page, there's no point in 3969 * setting it up. For some pages, we have to have some 3970 * basic information about the disk in order to calculate the 3971 * mode page data. 3972 */ 3973 if ((lun->be_lun->lun_type != T_DIRECT) 3974 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3975 continue; 3976 3977 switch (page_index->page_code & SMPH_PC_MASK) { 3978 case SMS_FORMAT_DEVICE_PAGE: { 3979 struct scsi_format_page *format_page; 3980 3981 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3982 panic("subpage is incorrect!"); 3983 3984 /* 3985 * Sectors per track are set above. Bytes per 3986 * sector need to be set here on a per-LUN basis. 3987 */ 3988 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3989 &format_page_default, 3990 sizeof(format_page_default)); 3991 memcpy(&lun->mode_pages.format_page[ 3992 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3993 sizeof(format_page_changeable)); 3994 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3995 &format_page_default, 3996 sizeof(format_page_default)); 3997 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3998 &format_page_default, 3999 sizeof(format_page_default)); 4000 4001 format_page = &lun->mode_pages.format_page[ 4002 CTL_PAGE_CURRENT]; 4003 scsi_ulto2b(lun->be_lun->blocksize, 4004 format_page->bytes_per_sector); 4005 4006 format_page = &lun->mode_pages.format_page[ 4007 CTL_PAGE_DEFAULT]; 4008 scsi_ulto2b(lun->be_lun->blocksize, 4009 format_page->bytes_per_sector); 4010 4011 format_page = &lun->mode_pages.format_page[ 4012 CTL_PAGE_SAVED]; 4013 scsi_ulto2b(lun->be_lun->blocksize, 4014 format_page->bytes_per_sector); 4015 4016 page_index->page_data = 4017 (uint8_t *)lun->mode_pages.format_page; 4018 break; 4019 } 4020 case SMS_RIGID_DISK_PAGE: { 4021 struct scsi_rigid_disk_page *rigid_disk_page; 4022 uint32_t sectors_per_cylinder; 4023 uint64_t cylinders; 4024#ifndef __XSCALE__ 4025 int shift; 4026#endif /* !__XSCALE__ */ 4027 4028 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4029 panic("invalid subpage value %d", 4030 page_index->subpage); 4031 4032 /* 4033 * Rotation rate and sectors per track are set 4034 * above. We calculate the cylinders here based on 4035 * capacity. Due to the number of heads and 4036 * sectors per track we're using, smaller arrays 4037 * may turn out to have 0 cylinders. Linux and 4038 * FreeBSD don't pay attention to these mode pages 4039 * to figure out capacity, but Solaris does. It 4040 * seems to deal with 0 cylinders just fine, and 4041 * works out a fake geometry based on the capacity. 4042 */ 4043 memcpy(&lun->mode_pages.rigid_disk_page[ 4044 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4045 sizeof(rigid_disk_page_default)); 4046 memcpy(&lun->mode_pages.rigid_disk_page[ 4047 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4048 sizeof(rigid_disk_page_changeable)); 4049 memcpy(&lun->mode_pages.rigid_disk_page[ 4050 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4051 sizeof(rigid_disk_page_default)); 4052 memcpy(&lun->mode_pages.rigid_disk_page[ 4053 CTL_PAGE_SAVED], &rigid_disk_page_default, 4054 sizeof(rigid_disk_page_default)); 4055 4056 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4057 CTL_DEFAULT_HEADS; 4058 4059 /* 4060 * The divide method here will be more accurate, 4061 * probably, but results in floating point being 4062 * used in the kernel on i386 (__udivdi3()). On the 4063 * XScale, though, __udivdi3() is implemented in 4064 * software. 4065 * 4066 * The shift method for cylinder calculation is 4067 * accurate if sectors_per_cylinder is a power of 4068 * 2. Otherwise it might be slightly off -- you 4069 * might have a bit of a truncation problem. 4070 */ 4071#ifdef __XSCALE__ 4072 cylinders = (lun->be_lun->maxlba + 1) / 4073 sectors_per_cylinder; 4074#else 4075 for (shift = 31; shift > 0; shift--) { 4076 if (sectors_per_cylinder & (1 << shift)) 4077 break; 4078 } 4079 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4080#endif 4081 4082 /* 4083 * We've basically got 3 bytes, or 24 bits for the 4084 * cylinder size in the mode page. If we're over, 4085 * just round down to 2^24. 4086 */ 4087 if (cylinders > 0xffffff) 4088 cylinders = 0xffffff; 4089 4090 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4091 CTL_PAGE_CURRENT]; 4092 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4093 4094 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4095 CTL_PAGE_DEFAULT]; 4096 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4097 4098 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4099 CTL_PAGE_SAVED]; 4100 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4101 4102 page_index->page_data = 4103 (uint8_t *)lun->mode_pages.rigid_disk_page; 4104 break; 4105 } 4106 case SMS_CACHING_PAGE: { 4107 4108 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4109 panic("invalid subpage value %d", 4110 page_index->subpage); 4111 /* 4112 * Defaults should be okay here, no calculations 4113 * needed. 4114 */ 4115 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4116 &caching_page_default, 4117 sizeof(caching_page_default)); 4118 memcpy(&lun->mode_pages.caching_page[ 4119 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4120 sizeof(caching_page_changeable)); 4121 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4122 &caching_page_default, 4123 sizeof(caching_page_default)); 4124 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4125 &caching_page_default, 4126 sizeof(caching_page_default)); 4127 page_index->page_data = 4128 (uint8_t *)lun->mode_pages.caching_page; 4129 break; 4130 } 4131 case SMS_CONTROL_MODE_PAGE: { 4132 4133 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4134 panic("invalid subpage value %d", 4135 page_index->subpage); 4136 4137 /* 4138 * Defaults should be okay here, no calculations 4139 * needed. 4140 */ 4141 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4142 &control_page_default, 4143 sizeof(control_page_default)); 4144 memcpy(&lun->mode_pages.control_page[ 4145 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4146 sizeof(control_page_changeable)); 4147 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4148 &control_page_default, 4149 sizeof(control_page_default)); 4150 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4151 &control_page_default, 4152 sizeof(control_page_default)); 4153 page_index->page_data = 4154 (uint8_t *)lun->mode_pages.control_page; 4155 break; 4156 4157 } 4158 case SMS_VENDOR_SPECIFIC_PAGE:{ 4159 switch (page_index->subpage) { 4160 case PWR_SUBPAGE_CODE: { 4161 struct copan_power_subpage *current_page, 4162 *saved_page; 4163 4164 memcpy(&lun->mode_pages.power_subpage[ 4165 CTL_PAGE_CURRENT], 4166 &power_page_default, 4167 sizeof(power_page_default)); 4168 memcpy(&lun->mode_pages.power_subpage[ 4169 CTL_PAGE_CHANGEABLE], 4170 &power_page_changeable, 4171 sizeof(power_page_changeable)); 4172 memcpy(&lun->mode_pages.power_subpage[ 4173 CTL_PAGE_DEFAULT], 4174 &power_page_default, 4175 sizeof(power_page_default)); 4176 memcpy(&lun->mode_pages.power_subpage[ 4177 CTL_PAGE_SAVED], 4178 &power_page_default, 4179 sizeof(power_page_default)); 4180 page_index->page_data = 4181 (uint8_t *)lun->mode_pages.power_subpage; 4182 4183 current_page = (struct copan_power_subpage *) 4184 (page_index->page_data + 4185 (page_index->page_len * 4186 CTL_PAGE_CURRENT)); 4187 saved_page = (struct copan_power_subpage *) 4188 (page_index->page_data + 4189 (page_index->page_len * 4190 CTL_PAGE_SAVED)); 4191 break; 4192 } 4193 case APS_SUBPAGE_CODE: { 4194 struct copan_aps_subpage *current_page, 4195 *saved_page; 4196 4197 // This gets set multiple times but 4198 // it should always be the same. It's 4199 // only done during init so who cares. 4200 index_to_aps_page = i; 4201 4202 memcpy(&lun->mode_pages.aps_subpage[ 4203 CTL_PAGE_CURRENT], 4204 &aps_page_default, 4205 sizeof(aps_page_default)); 4206 memcpy(&lun->mode_pages.aps_subpage[ 4207 CTL_PAGE_CHANGEABLE], 4208 &aps_page_changeable, 4209 sizeof(aps_page_changeable)); 4210 memcpy(&lun->mode_pages.aps_subpage[ 4211 CTL_PAGE_DEFAULT], 4212 &aps_page_default, 4213 sizeof(aps_page_default)); 4214 memcpy(&lun->mode_pages.aps_subpage[ 4215 CTL_PAGE_SAVED], 4216 &aps_page_default, 4217 sizeof(aps_page_default)); 4218 page_index->page_data = 4219 (uint8_t *)lun->mode_pages.aps_subpage; 4220 4221 current_page = (struct copan_aps_subpage *) 4222 (page_index->page_data + 4223 (page_index->page_len * 4224 CTL_PAGE_CURRENT)); 4225 saved_page = (struct copan_aps_subpage *) 4226 (page_index->page_data + 4227 (page_index->page_len * 4228 CTL_PAGE_SAVED)); 4229 break; 4230 } 4231 case DBGCNF_SUBPAGE_CODE: { 4232 struct copan_debugconf_subpage *current_page, 4233 *saved_page; 4234 4235 memcpy(&lun->mode_pages.debugconf_subpage[ 4236 CTL_PAGE_CURRENT], 4237 &debugconf_page_default, 4238 sizeof(debugconf_page_default)); 4239 memcpy(&lun->mode_pages.debugconf_subpage[ 4240 CTL_PAGE_CHANGEABLE], 4241 &debugconf_page_changeable, 4242 sizeof(debugconf_page_changeable)); 4243 memcpy(&lun->mode_pages.debugconf_subpage[ 4244 CTL_PAGE_DEFAULT], 4245 &debugconf_page_default, 4246 sizeof(debugconf_page_default)); 4247 memcpy(&lun->mode_pages.debugconf_subpage[ 4248 CTL_PAGE_SAVED], 4249 &debugconf_page_default, 4250 sizeof(debugconf_page_default)); 4251 page_index->page_data = 4252 (uint8_t *)lun->mode_pages.debugconf_subpage; 4253 4254 current_page = (struct copan_debugconf_subpage *) 4255 (page_index->page_data + 4256 (page_index->page_len * 4257 CTL_PAGE_CURRENT)); 4258 saved_page = (struct copan_debugconf_subpage *) 4259 (page_index->page_data + 4260 (page_index->page_len * 4261 CTL_PAGE_SAVED)); 4262 break; 4263 } 4264 default: 4265 panic("invalid subpage value %d", 4266 page_index->subpage); 4267 break; 4268 } 4269 break; 4270 } 4271 default: 4272 panic("invalid page value %d", 4273 page_index->page_code & SMPH_PC_MASK); 4274 break; 4275 } 4276 } 4277 4278 return (CTL_RETVAL_COMPLETE); 4279} 4280 4281/* 4282 * LUN allocation. 4283 * 4284 * Requirements: 4285 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4286 * wants us to allocate the LUN and he can block. 4287 * - ctl_softc is always set 4288 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4289 * 4290 * Returns 0 for success, non-zero (errno) for failure. 4291 */ 4292static int 4293ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4294 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4295{ 4296 struct ctl_lun *nlun, *lun; 4297 struct ctl_port *port; 4298 int lun_number, i, lun_malloced; 4299 4300 if (be_lun == NULL) 4301 return (EINVAL); 4302 4303 /* 4304 * We currently only support Direct Access or Processor LUN types. 4305 */ 4306 switch (be_lun->lun_type) { 4307 case T_DIRECT: 4308 break; 4309 case T_PROCESSOR: 4310 break; 4311 case T_SEQUENTIAL: 4312 case T_CHANGER: 4313 default: 4314 be_lun->lun_config_status(be_lun->be_lun, 4315 CTL_LUN_CONFIG_FAILURE); 4316 break; 4317 } 4318 if (ctl_lun == NULL) { 4319 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4320 lun_malloced = 1; 4321 } else { 4322 lun_malloced = 0; 4323 lun = ctl_lun; 4324 } 4325 4326 memset(lun, 0, sizeof(*lun)); 4327 if (lun_malloced) 4328 lun->flags = CTL_LUN_MALLOCED; 4329 4330 mtx_lock(&ctl_softc->ctl_lock); 4331 /* 4332 * See if the caller requested a particular LUN number. If so, see 4333 * if it is available. Otherwise, allocate the first available LUN. 4334 */ 4335 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4336 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4337 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4338 mtx_unlock(&ctl_softc->ctl_lock); 4339 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4340 printf("ctl: requested LUN ID %d is higher " 4341 "than CTL_MAX_LUNS - 1 (%d)\n", 4342 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4343 } else { 4344 /* 4345 * XXX KDM return an error, or just assign 4346 * another LUN ID in this case?? 4347 */ 4348 printf("ctl: requested LUN ID %d is already " 4349 "in use\n", be_lun->req_lun_id); 4350 } 4351 if (lun->flags & CTL_LUN_MALLOCED) 4352 free(lun, M_CTL); 4353 be_lun->lun_config_status(be_lun->be_lun, 4354 CTL_LUN_CONFIG_FAILURE); 4355 return (ENOSPC); 4356 } 4357 lun_number = be_lun->req_lun_id; 4358 } else { 4359 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4360 if (lun_number == -1) { 4361 mtx_unlock(&ctl_softc->ctl_lock); 4362 printf("ctl: can't allocate LUN on target %ju, out of " 4363 "LUNs\n", (uintmax_t)target_id.id); 4364 if (lun->flags & CTL_LUN_MALLOCED) 4365 free(lun, M_CTL); 4366 be_lun->lun_config_status(be_lun->be_lun, 4367 CTL_LUN_CONFIG_FAILURE); 4368 return (ENOSPC); 4369 } 4370 } 4371 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4372 4373 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4374 lun->target = target_id; 4375 lun->lun = lun_number; 4376 lun->be_lun = be_lun; 4377 /* 4378 * The processor LUN is always enabled. Disk LUNs come on line 4379 * disabled, and must be enabled by the backend. 4380 */ 4381 lun->flags |= CTL_LUN_DISABLED; 4382 lun->backend = be_lun->be; 4383 be_lun->ctl_lun = lun; 4384 be_lun->lun_id = lun_number; 4385 atomic_add_int(&be_lun->be->num_luns, 1); 4386 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4387 lun->flags |= CTL_LUN_STOPPED; 4388 4389 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4390 lun->flags |= CTL_LUN_INOPERABLE; 4391 4392 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4393 lun->flags |= CTL_LUN_PRIMARY_SC; 4394 4395 lun->ctl_softc = ctl_softc; 4396 TAILQ_INIT(&lun->ooa_queue); 4397 TAILQ_INIT(&lun->blocked_queue); 4398 STAILQ_INIT(&lun->error_list); 4399 4400 /* 4401 * Initialize the mode page index. 4402 */ 4403 ctl_init_page_index(lun); 4404 4405 /* 4406 * Set the poweron UA for all initiators on this LUN only. 4407 */ 4408 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4409 lun->pending_sense[i].ua_pending = CTL_UA_POWERON; 4410 4411 /* 4412 * Now, before we insert this lun on the lun list, set the lun 4413 * inventory changed UA for all other luns. 4414 */ 4415 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4416 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4417 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4418 } 4419 } 4420 4421 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4422 4423 ctl_softc->ctl_luns[lun_number] = lun; 4424 4425 ctl_softc->num_luns++; 4426 4427 /* Setup statistics gathering */ 4428 lun->stats.device_type = be_lun->lun_type; 4429 lun->stats.lun_number = lun_number; 4430 if (lun->stats.device_type == T_DIRECT) 4431 lun->stats.blocksize = be_lun->blocksize; 4432 else 4433 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4434 for (i = 0;i < CTL_MAX_PORTS;i++) 4435 lun->stats.ports[i].targ_port = i; 4436 4437 mtx_unlock(&ctl_softc->ctl_lock); 4438 4439 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4440 4441 /* 4442 * Run through each registered FETD and bring it online if it isn't 4443 * already. Enable the target ID if it hasn't been enabled, and 4444 * enable this particular LUN. 4445 */ 4446 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4447 int retval; 4448 4449 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4450 if (retval != 0) { 4451 printf("ctl_alloc_lun: FETD %s port %d returned error " 4452 "%d for lun_enable on target %ju lun %d\n", 4453 port->port_name, port->targ_port, retval, 4454 (uintmax_t)target_id.id, lun_number); 4455 } else 4456 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4457 } 4458 return (0); 4459} 4460 4461/* 4462 * Delete a LUN. 4463 * Assumptions: 4464 * - LUN has already been marked invalid and any pending I/O has been taken 4465 * care of. 4466 */ 4467static int 4468ctl_free_lun(struct ctl_lun *lun) 4469{ 4470 struct ctl_softc *softc; 4471#if 0 4472 struct ctl_port *port; 4473#endif 4474 struct ctl_lun *nlun; 4475 int i; 4476 4477 softc = lun->ctl_softc; 4478 4479 mtx_assert(&softc->ctl_lock, MA_OWNED); 4480 4481 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4482 4483 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4484 4485 softc->ctl_luns[lun->lun] = NULL; 4486 4487 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4488 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4489 4490 softc->num_luns--; 4491 4492 /* 4493 * XXX KDM this scheme only works for a single target/multiple LUN 4494 * setup. It needs to be revamped for a multiple target scheme. 4495 * 4496 * XXX KDM this results in port->lun_disable() getting called twice, 4497 * once when ctl_disable_lun() is called, and a second time here. 4498 * We really need to re-think the LUN disable semantics. There 4499 * should probably be several steps/levels to LUN removal: 4500 * - disable 4501 * - invalidate 4502 * - free 4503 * 4504 * Right now we only have a disable method when communicating to 4505 * the front end ports, at least for individual LUNs. 4506 */ 4507#if 0 4508 STAILQ_FOREACH(port, &softc->port_list, links) { 4509 int retval; 4510 4511 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4512 lun->lun); 4513 if (retval != 0) { 4514 printf("ctl_free_lun: FETD %s port %d returned error " 4515 "%d for lun_disable on target %ju lun %jd\n", 4516 port->port_name, port->targ_port, retval, 4517 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4518 } 4519 4520 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4521 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4522 4523 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4524 if (retval != 0) { 4525 printf("ctl_free_lun: FETD %s port %d " 4526 "returned error %d for targ_disable on " 4527 "target %ju\n", port->port_name, 4528 port->targ_port, retval, 4529 (uintmax_t)lun->target.id); 4530 } else 4531 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4532 4533 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4534 continue; 4535 4536#if 0 4537 port->port_offline(port->onoff_arg); 4538 port->status &= ~CTL_PORT_STATUS_ONLINE; 4539#endif 4540 } 4541 } 4542#endif 4543 4544 /* 4545 * Tell the backend to free resources, if this LUN has a backend. 4546 */ 4547 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4548 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4549 4550 mtx_destroy(&lun->lun_lock); 4551 if (lun->flags & CTL_LUN_MALLOCED) 4552 free(lun, M_CTL); 4553 4554 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4555 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4556 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4557 } 4558 } 4559 4560 return (0); 4561} 4562 4563static void 4564ctl_create_lun(struct ctl_be_lun *be_lun) 4565{ 4566 struct ctl_softc *ctl_softc; 4567 4568 ctl_softc = control_softc; 4569 4570 /* 4571 * ctl_alloc_lun() should handle all potential failure cases. 4572 */ 4573 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4574} 4575 4576int 4577ctl_add_lun(struct ctl_be_lun *be_lun) 4578{ 4579 struct ctl_softc *ctl_softc = control_softc; 4580 4581 mtx_lock(&ctl_softc->ctl_lock); 4582 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4583 mtx_unlock(&ctl_softc->ctl_lock); 4584 wakeup(&ctl_softc->pending_lun_queue); 4585 4586 return (0); 4587} 4588 4589int 4590ctl_enable_lun(struct ctl_be_lun *be_lun) 4591{ 4592 struct ctl_softc *ctl_softc; 4593 struct ctl_port *port, *nport; 4594 struct ctl_lun *lun; 4595 int retval; 4596 4597 ctl_softc = control_softc; 4598 4599 lun = (struct ctl_lun *)be_lun->ctl_lun; 4600 4601 mtx_lock(&ctl_softc->ctl_lock); 4602 mtx_lock(&lun->lun_lock); 4603 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4604 /* 4605 * eh? Why did we get called if the LUN is already 4606 * enabled? 4607 */ 4608 mtx_unlock(&lun->lun_lock); 4609 mtx_unlock(&ctl_softc->ctl_lock); 4610 return (0); 4611 } 4612 lun->flags &= ~CTL_LUN_DISABLED; 4613 mtx_unlock(&lun->lun_lock); 4614 4615 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4616 nport = STAILQ_NEXT(port, links); 4617 4618 /* 4619 * Drop the lock while we call the FETD's enable routine. 4620 * This can lead to a callback into CTL (at least in the 4621 * case of the internal initiator frontend. 4622 */ 4623 mtx_unlock(&ctl_softc->ctl_lock); 4624 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4625 mtx_lock(&ctl_softc->ctl_lock); 4626 if (retval != 0) { 4627 printf("%s: FETD %s port %d returned error " 4628 "%d for lun_enable on target %ju lun %jd\n", 4629 __func__, port->port_name, port->targ_port, retval, 4630 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4631 } 4632#if 0 4633 else { 4634 /* NOTE: TODO: why does lun enable affect port status? */ 4635 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4636 } 4637#endif 4638 } 4639 4640 mtx_unlock(&ctl_softc->ctl_lock); 4641 4642 return (0); 4643} 4644 4645int 4646ctl_disable_lun(struct ctl_be_lun *be_lun) 4647{ 4648 struct ctl_softc *ctl_softc; 4649 struct ctl_port *port; 4650 struct ctl_lun *lun; 4651 int retval; 4652 4653 ctl_softc = control_softc; 4654 4655 lun = (struct ctl_lun *)be_lun->ctl_lun; 4656 4657 mtx_lock(&ctl_softc->ctl_lock); 4658 mtx_lock(&lun->lun_lock); 4659 if (lun->flags & CTL_LUN_DISABLED) { 4660 mtx_unlock(&lun->lun_lock); 4661 mtx_unlock(&ctl_softc->ctl_lock); 4662 return (0); 4663 } 4664 lun->flags |= CTL_LUN_DISABLED; 4665 mtx_unlock(&lun->lun_lock); 4666 4667 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4668 mtx_unlock(&ctl_softc->ctl_lock); 4669 /* 4670 * Drop the lock before we call the frontend's disable 4671 * routine, to avoid lock order reversals. 4672 * 4673 * XXX KDM what happens if the frontend list changes while 4674 * we're traversing it? It's unlikely, but should be handled. 4675 */ 4676 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4677 lun->lun); 4678 mtx_lock(&ctl_softc->ctl_lock); 4679 if (retval != 0) { 4680 printf("ctl_alloc_lun: FETD %s port %d returned error " 4681 "%d for lun_disable on target %ju lun %jd\n", 4682 port->port_name, port->targ_port, retval, 4683 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4684 } 4685 } 4686 4687 mtx_unlock(&ctl_softc->ctl_lock); 4688 4689 return (0); 4690} 4691 4692int 4693ctl_start_lun(struct ctl_be_lun *be_lun) 4694{ 4695 struct ctl_softc *ctl_softc; 4696 struct ctl_lun *lun; 4697 4698 ctl_softc = control_softc; 4699 4700 lun = (struct ctl_lun *)be_lun->ctl_lun; 4701 4702 mtx_lock(&lun->lun_lock); 4703 lun->flags &= ~CTL_LUN_STOPPED; 4704 mtx_unlock(&lun->lun_lock); 4705 4706 return (0); 4707} 4708 4709int 4710ctl_stop_lun(struct ctl_be_lun *be_lun) 4711{ 4712 struct ctl_softc *ctl_softc; 4713 struct ctl_lun *lun; 4714 4715 ctl_softc = control_softc; 4716 4717 lun = (struct ctl_lun *)be_lun->ctl_lun; 4718 4719 mtx_lock(&lun->lun_lock); 4720 lun->flags |= CTL_LUN_STOPPED; 4721 mtx_unlock(&lun->lun_lock); 4722 4723 return (0); 4724} 4725 4726int 4727ctl_lun_offline(struct ctl_be_lun *be_lun) 4728{ 4729 struct ctl_softc *ctl_softc; 4730 struct ctl_lun *lun; 4731 4732 ctl_softc = control_softc; 4733 4734 lun = (struct ctl_lun *)be_lun->ctl_lun; 4735 4736 mtx_lock(&lun->lun_lock); 4737 lun->flags |= CTL_LUN_OFFLINE; 4738 mtx_unlock(&lun->lun_lock); 4739 4740 return (0); 4741} 4742 4743int 4744ctl_lun_online(struct ctl_be_lun *be_lun) 4745{ 4746 struct ctl_softc *ctl_softc; 4747 struct ctl_lun *lun; 4748 4749 ctl_softc = control_softc; 4750 4751 lun = (struct ctl_lun *)be_lun->ctl_lun; 4752 4753 mtx_lock(&lun->lun_lock); 4754 lun->flags &= ~CTL_LUN_OFFLINE; 4755 mtx_unlock(&lun->lun_lock); 4756 4757 return (0); 4758} 4759 4760int 4761ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4762{ 4763 struct ctl_softc *ctl_softc; 4764 struct ctl_lun *lun; 4765 4766 ctl_softc = control_softc; 4767 4768 lun = (struct ctl_lun *)be_lun->ctl_lun; 4769 4770 mtx_lock(&lun->lun_lock); 4771 4772 /* 4773 * The LUN needs to be disabled before it can be marked invalid. 4774 */ 4775 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4776 mtx_unlock(&lun->lun_lock); 4777 return (-1); 4778 } 4779 /* 4780 * Mark the LUN invalid. 4781 */ 4782 lun->flags |= CTL_LUN_INVALID; 4783 4784 /* 4785 * If there is nothing in the OOA queue, go ahead and free the LUN. 4786 * If we have something in the OOA queue, we'll free it when the 4787 * last I/O completes. 4788 */ 4789 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4790 mtx_unlock(&lun->lun_lock); 4791 mtx_lock(&ctl_softc->ctl_lock); 4792 ctl_free_lun(lun); 4793 mtx_unlock(&ctl_softc->ctl_lock); 4794 } else 4795 mtx_unlock(&lun->lun_lock); 4796 4797 return (0); 4798} 4799 4800int 4801ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4802{ 4803 struct ctl_softc *ctl_softc; 4804 struct ctl_lun *lun; 4805 4806 ctl_softc = control_softc; 4807 lun = (struct ctl_lun *)be_lun->ctl_lun; 4808 4809 mtx_lock(&lun->lun_lock); 4810 lun->flags |= CTL_LUN_INOPERABLE; 4811 mtx_unlock(&lun->lun_lock); 4812 4813 return (0); 4814} 4815 4816int 4817ctl_lun_operable(struct ctl_be_lun *be_lun) 4818{ 4819 struct ctl_softc *ctl_softc; 4820 struct ctl_lun *lun; 4821 4822 ctl_softc = control_softc; 4823 lun = (struct ctl_lun *)be_lun->ctl_lun; 4824 4825 mtx_lock(&lun->lun_lock); 4826 lun->flags &= ~CTL_LUN_INOPERABLE; 4827 mtx_unlock(&lun->lun_lock); 4828 4829 return (0); 4830} 4831 4832int 4833ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 4834 int lock) 4835{ 4836 struct ctl_softc *softc; 4837 struct ctl_lun *lun; 4838 struct copan_aps_subpage *current_sp; 4839 struct ctl_page_index *page_index; 4840 int i; 4841 4842 softc = control_softc; 4843 4844 mtx_lock(&softc->ctl_lock); 4845 4846 lun = (struct ctl_lun *)be_lun->ctl_lun; 4847 mtx_lock(&lun->lun_lock); 4848 4849 page_index = NULL; 4850 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4851 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 4852 APS_PAGE_CODE) 4853 continue; 4854 4855 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 4856 continue; 4857 page_index = &lun->mode_pages.index[i]; 4858 } 4859 4860 if (page_index == NULL) { 4861 mtx_unlock(&lun->lun_lock); 4862 mtx_unlock(&softc->ctl_lock); 4863 printf("%s: APS subpage not found for lun %ju!\n", __func__, 4864 (uintmax_t)lun->lun); 4865 return (1); 4866 } 4867#if 0 4868 if ((softc->aps_locked_lun != 0) 4869 && (softc->aps_locked_lun != lun->lun)) { 4870 printf("%s: attempt to lock LUN %llu when %llu is already " 4871 "locked\n"); 4872 mtx_unlock(&lun->lun_lock); 4873 mtx_unlock(&softc->ctl_lock); 4874 return (1); 4875 } 4876#endif 4877 4878 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 4879 (page_index->page_len * CTL_PAGE_CURRENT)); 4880 4881 if (lock != 0) { 4882 current_sp->lock_active = APS_LOCK_ACTIVE; 4883 softc->aps_locked_lun = lun->lun; 4884 } else { 4885 current_sp->lock_active = 0; 4886 softc->aps_locked_lun = 0; 4887 } 4888 4889 4890 /* 4891 * If we're in HA mode, try to send the lock message to the other 4892 * side. 4893 */ 4894 if (ctl_is_single == 0) { 4895 int isc_retval; 4896 union ctl_ha_msg lock_msg; 4897 4898 lock_msg.hdr.nexus = *nexus; 4899 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 4900 if (lock != 0) 4901 lock_msg.aps.lock_flag = 1; 4902 else 4903 lock_msg.aps.lock_flag = 0; 4904 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 4905 sizeof(lock_msg), 0); 4906 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 4907 printf("%s: APS (lock=%d) error returned from " 4908 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 4909 mtx_unlock(&lun->lun_lock); 4910 mtx_unlock(&softc->ctl_lock); 4911 return (1); 4912 } 4913 } 4914 4915 mtx_unlock(&lun->lun_lock); 4916 mtx_unlock(&softc->ctl_lock); 4917 4918 return (0); 4919} 4920 4921void 4922ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4923{ 4924 struct ctl_lun *lun; 4925 struct ctl_softc *softc; 4926 int i; 4927 4928 softc = control_softc; 4929 4930 lun = (struct ctl_lun *)be_lun->ctl_lun; 4931 4932 mtx_lock(&lun->lun_lock); 4933 4934 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4935 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED; 4936 4937 mtx_unlock(&lun->lun_lock); 4938} 4939 4940/* 4941 * Backend "memory move is complete" callback for requests that never 4942 * make it down to say RAIDCore's configuration code. 4943 */ 4944int 4945ctl_config_move_done(union ctl_io *io) 4946{ 4947 int retval; 4948 4949 retval = CTL_RETVAL_COMPLETE; 4950 4951 4952 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4953 /* 4954 * XXX KDM this shouldn't happen, but what if it does? 4955 */ 4956 if (io->io_hdr.io_type != CTL_IO_SCSI) 4957 panic("I/O type isn't CTL_IO_SCSI!"); 4958 4959 if ((io->io_hdr.port_status == 0) 4960 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4961 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 4962 io->io_hdr.status = CTL_SUCCESS; 4963 else if ((io->io_hdr.port_status != 0) 4964 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4965 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 4966 /* 4967 * For hardware error sense keys, the sense key 4968 * specific value is defined to be a retry count, 4969 * but we use it to pass back an internal FETD 4970 * error code. XXX KDM Hopefully the FETD is only 4971 * using 16 bits for an error code, since that's 4972 * all the space we have in the sks field. 4973 */ 4974 ctl_set_internal_failure(&io->scsiio, 4975 /*sks_valid*/ 1, 4976 /*retry_count*/ 4977 io->io_hdr.port_status); 4978 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4979 free(io->scsiio.kern_data_ptr, M_CTL); 4980 ctl_done(io); 4981 goto bailout; 4982 } 4983 4984 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 4985 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 4986 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4987 /* 4988 * XXX KDM just assuming a single pointer here, and not a 4989 * S/G list. If we start using S/G lists for config data, 4990 * we'll need to know how to clean them up here as well. 4991 */ 4992 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4993 free(io->scsiio.kern_data_ptr, M_CTL); 4994 /* Hopefully the user has already set the status... */ 4995 ctl_done(io); 4996 } else { 4997 /* 4998 * XXX KDM now we need to continue data movement. Some 4999 * options: 5000 * - call ctl_scsiio() again? We don't do this for data 5001 * writes, because for those at least we know ahead of 5002 * time where the write will go and how long it is. For 5003 * config writes, though, that information is largely 5004 * contained within the write itself, thus we need to 5005 * parse out the data again. 5006 * 5007 * - Call some other function once the data is in? 5008 */ 5009 5010 /* 5011 * XXX KDM call ctl_scsiio() again for now, and check flag 5012 * bits to see whether we're allocated or not. 5013 */ 5014 retval = ctl_scsiio(&io->scsiio); 5015 } 5016bailout: 5017 return (retval); 5018} 5019 5020/* 5021 * This gets called by a backend driver when it is done with a 5022 * data_submit method. 5023 */ 5024void 5025ctl_data_submit_done(union ctl_io *io) 5026{ 5027 /* 5028 * If the IO_CONT flag is set, we need to call the supplied 5029 * function to continue processing the I/O, instead of completing 5030 * the I/O just yet. 5031 * 5032 * If there is an error, though, we don't want to keep processing. 5033 * Instead, just send status back to the initiator. 5034 */ 5035 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5036 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5037 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5038 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5039 io->scsiio.io_cont(io); 5040 return; 5041 } 5042 ctl_done(io); 5043} 5044 5045/* 5046 * This gets called by a backend driver when it is done with a 5047 * configuration write. 5048 */ 5049void 5050ctl_config_write_done(union ctl_io *io) 5051{ 5052 /* 5053 * If the IO_CONT flag is set, we need to call the supplied 5054 * function to continue processing the I/O, instead of completing 5055 * the I/O just yet. 5056 * 5057 * If there is an error, though, we don't want to keep processing. 5058 * Instead, just send status back to the initiator. 5059 */ 5060 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 5061 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 5062 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 5063 io->scsiio.io_cont(io); 5064 return; 5065 } 5066 /* 5067 * Since a configuration write can be done for commands that actually 5068 * have data allocated, like write buffer, and commands that have 5069 * no data, like start/stop unit, we need to check here. 5070 */ 5071 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 5072 free(io->scsiio.kern_data_ptr, M_CTL); 5073 ctl_done(io); 5074} 5075 5076/* 5077 * SCSI release command. 5078 */ 5079int 5080ctl_scsi_release(struct ctl_scsiio *ctsio) 5081{ 5082 int length, longid, thirdparty_id, resv_id; 5083 struct ctl_softc *ctl_softc; 5084 struct ctl_lun *lun; 5085 5086 length = 0; 5087 resv_id = 0; 5088 5089 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5090 5091 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5092 ctl_softc = control_softc; 5093 5094 switch (ctsio->cdb[0]) { 5095 case RELEASE_10: { 5096 struct scsi_release_10 *cdb; 5097 5098 cdb = (struct scsi_release_10 *)ctsio->cdb; 5099 5100 if (cdb->byte2 & SR10_LONGID) 5101 longid = 1; 5102 else 5103 thirdparty_id = cdb->thirdparty_id; 5104 5105 resv_id = cdb->resv_id; 5106 length = scsi_2btoul(cdb->length); 5107 break; 5108 } 5109 } 5110 5111 5112 /* 5113 * XXX KDM right now, we only support LUN reservation. We don't 5114 * support 3rd party reservations, or extent reservations, which 5115 * might actually need the parameter list. If we've gotten this 5116 * far, we've got a LUN reservation. Anything else got kicked out 5117 * above. So, according to SPC, ignore the length. 5118 */ 5119 length = 0; 5120 5121 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5122 && (length > 0)) { 5123 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5124 ctsio->kern_data_len = length; 5125 ctsio->kern_total_len = length; 5126 ctsio->kern_data_resid = 0; 5127 ctsio->kern_rel_offset = 0; 5128 ctsio->kern_sg_entries = 0; 5129 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5130 ctsio->be_move_done = ctl_config_move_done; 5131 ctl_datamove((union ctl_io *)ctsio); 5132 5133 return (CTL_RETVAL_COMPLETE); 5134 } 5135 5136 if (length > 0) 5137 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5138 5139 mtx_lock(&lun->lun_lock); 5140 5141 /* 5142 * According to SPC, it is not an error for an intiator to attempt 5143 * to release a reservation on a LUN that isn't reserved, or that 5144 * is reserved by another initiator. The reservation can only be 5145 * released, though, by the initiator who made it or by one of 5146 * several reset type events. 5147 */ 5148 if (lun->flags & CTL_LUN_RESERVED) { 5149 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5150 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5151 && (ctsio->io_hdr.nexus.targ_target.id == 5152 lun->rsv_nexus.targ_target.id)) { 5153 lun->flags &= ~CTL_LUN_RESERVED; 5154 } 5155 } 5156 5157 mtx_unlock(&lun->lun_lock); 5158 5159 ctsio->scsi_status = SCSI_STATUS_OK; 5160 ctsio->io_hdr.status = CTL_SUCCESS; 5161 5162 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5163 free(ctsio->kern_data_ptr, M_CTL); 5164 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5165 } 5166 5167 ctl_done((union ctl_io *)ctsio); 5168 return (CTL_RETVAL_COMPLETE); 5169} 5170 5171int 5172ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5173{ 5174 int extent, thirdparty, longid; 5175 int resv_id, length; 5176 uint64_t thirdparty_id; 5177 struct ctl_softc *ctl_softc; 5178 struct ctl_lun *lun; 5179 5180 extent = 0; 5181 thirdparty = 0; 5182 longid = 0; 5183 resv_id = 0; 5184 length = 0; 5185 thirdparty_id = 0; 5186 5187 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5188 5189 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5190 ctl_softc = control_softc; 5191 5192 switch (ctsio->cdb[0]) { 5193 case RESERVE_10: { 5194 struct scsi_reserve_10 *cdb; 5195 5196 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5197 5198 if (cdb->byte2 & SR10_LONGID) 5199 longid = 1; 5200 else 5201 thirdparty_id = cdb->thirdparty_id; 5202 5203 resv_id = cdb->resv_id; 5204 length = scsi_2btoul(cdb->length); 5205 break; 5206 } 5207 } 5208 5209 /* 5210 * XXX KDM right now, we only support LUN reservation. We don't 5211 * support 3rd party reservations, or extent reservations, which 5212 * might actually need the parameter list. If we've gotten this 5213 * far, we've got a LUN reservation. Anything else got kicked out 5214 * above. So, according to SPC, ignore the length. 5215 */ 5216 length = 0; 5217 5218 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5219 && (length > 0)) { 5220 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5221 ctsio->kern_data_len = length; 5222 ctsio->kern_total_len = length; 5223 ctsio->kern_data_resid = 0; 5224 ctsio->kern_rel_offset = 0; 5225 ctsio->kern_sg_entries = 0; 5226 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5227 ctsio->be_move_done = ctl_config_move_done; 5228 ctl_datamove((union ctl_io *)ctsio); 5229 5230 return (CTL_RETVAL_COMPLETE); 5231 } 5232 5233 if (length > 0) 5234 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5235 5236 mtx_lock(&lun->lun_lock); 5237 if (lun->flags & CTL_LUN_RESERVED) { 5238 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5239 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5240 || (ctsio->io_hdr.nexus.targ_target.id != 5241 lun->rsv_nexus.targ_target.id)) { 5242 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5243 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5244 goto bailout; 5245 } 5246 } 5247 5248 lun->flags |= CTL_LUN_RESERVED; 5249 lun->rsv_nexus = ctsio->io_hdr.nexus; 5250 5251 ctsio->scsi_status = SCSI_STATUS_OK; 5252 ctsio->io_hdr.status = CTL_SUCCESS; 5253 5254bailout: 5255 mtx_unlock(&lun->lun_lock); 5256 5257 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5258 free(ctsio->kern_data_ptr, M_CTL); 5259 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5260 } 5261 5262 ctl_done((union ctl_io *)ctsio); 5263 return (CTL_RETVAL_COMPLETE); 5264} 5265 5266int 5267ctl_start_stop(struct ctl_scsiio *ctsio) 5268{ 5269 struct scsi_start_stop_unit *cdb; 5270 struct ctl_lun *lun; 5271 struct ctl_softc *ctl_softc; 5272 int retval; 5273 5274 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5275 5276 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5277 ctl_softc = control_softc; 5278 retval = 0; 5279 5280 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5281 5282 /* 5283 * XXX KDM 5284 * We don't support the immediate bit on a stop unit. In order to 5285 * do that, we would need to code up a way to know that a stop is 5286 * pending, and hold off any new commands until it completes, one 5287 * way or another. Then we could accept or reject those commands 5288 * depending on its status. We would almost need to do the reverse 5289 * of what we do below for an immediate start -- return the copy of 5290 * the ctl_io to the FETD with status to send to the host (and to 5291 * free the copy!) and then free the original I/O once the stop 5292 * actually completes. That way, the OOA queue mechanism can work 5293 * to block commands that shouldn't proceed. Another alternative 5294 * would be to put the copy in the queue in place of the original, 5295 * and return the original back to the caller. That could be 5296 * slightly safer.. 5297 */ 5298 if ((cdb->byte2 & SSS_IMMED) 5299 && ((cdb->how & SSS_START) == 0)) { 5300 ctl_set_invalid_field(ctsio, 5301 /*sks_valid*/ 1, 5302 /*command*/ 1, 5303 /*field*/ 1, 5304 /*bit_valid*/ 1, 5305 /*bit*/ 0); 5306 ctl_done((union ctl_io *)ctsio); 5307 return (CTL_RETVAL_COMPLETE); 5308 } 5309 5310 if ((lun->flags & CTL_LUN_PR_RESERVED) 5311 && ((cdb->how & SSS_START)==0)) { 5312 uint32_t residx; 5313 5314 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5315 if (!lun->per_res[residx].registered 5316 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5317 5318 ctl_set_reservation_conflict(ctsio); 5319 ctl_done((union ctl_io *)ctsio); 5320 return (CTL_RETVAL_COMPLETE); 5321 } 5322 } 5323 5324 /* 5325 * If there is no backend on this device, we can't start or stop 5326 * it. In theory we shouldn't get any start/stop commands in the 5327 * first place at this level if the LUN doesn't have a backend. 5328 * That should get stopped by the command decode code. 5329 */ 5330 if (lun->backend == NULL) { 5331 ctl_set_invalid_opcode(ctsio); 5332 ctl_done((union ctl_io *)ctsio); 5333 return (CTL_RETVAL_COMPLETE); 5334 } 5335 5336 /* 5337 * XXX KDM Copan-specific offline behavior. 5338 * Figure out a reasonable way to port this? 5339 */ 5340#ifdef NEEDTOPORT 5341 mtx_lock(&lun->lun_lock); 5342 5343 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5344 && (lun->flags & CTL_LUN_OFFLINE)) { 5345 /* 5346 * If the LUN is offline, and the on/offline bit isn't set, 5347 * reject the start or stop. Otherwise, let it through. 5348 */ 5349 mtx_unlock(&lun->lun_lock); 5350 ctl_set_lun_not_ready(ctsio); 5351 ctl_done((union ctl_io *)ctsio); 5352 } else { 5353 mtx_unlock(&lun->lun_lock); 5354#endif /* NEEDTOPORT */ 5355 /* 5356 * This could be a start or a stop when we're online, 5357 * or a stop/offline or start/online. A start or stop when 5358 * we're offline is covered in the case above. 5359 */ 5360 /* 5361 * In the non-immediate case, we send the request to 5362 * the backend and return status to the user when 5363 * it is done. 5364 * 5365 * In the immediate case, we allocate a new ctl_io 5366 * to hold a copy of the request, and send that to 5367 * the backend. We then set good status on the 5368 * user's request and return it immediately. 5369 */ 5370 if (cdb->byte2 & SSS_IMMED) { 5371 union ctl_io *new_io; 5372 5373 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5374 if (new_io == NULL) { 5375 ctl_set_busy(ctsio); 5376 ctl_done((union ctl_io *)ctsio); 5377 } else { 5378 ctl_copy_io((union ctl_io *)ctsio, 5379 new_io); 5380 retval = lun->backend->config_write(new_io); 5381 ctl_set_success(ctsio); 5382 ctl_done((union ctl_io *)ctsio); 5383 } 5384 } else { 5385 retval = lun->backend->config_write( 5386 (union ctl_io *)ctsio); 5387 } 5388#ifdef NEEDTOPORT 5389 } 5390#endif 5391 return (retval); 5392} 5393 5394/* 5395 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5396 * we don't really do anything with the LBA and length fields if the user 5397 * passes them in. Instead we'll just flush out the cache for the entire 5398 * LUN. 5399 */ 5400int 5401ctl_sync_cache(struct ctl_scsiio *ctsio) 5402{ 5403 struct ctl_lun *lun; 5404 struct ctl_softc *ctl_softc; 5405 uint64_t starting_lba; 5406 uint32_t block_count; 5407 int retval; 5408 5409 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5410 5411 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5412 ctl_softc = control_softc; 5413 retval = 0; 5414 5415 switch (ctsio->cdb[0]) { 5416 case SYNCHRONIZE_CACHE: { 5417 struct scsi_sync_cache *cdb; 5418 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5419 5420 starting_lba = scsi_4btoul(cdb->begin_lba); 5421 block_count = scsi_2btoul(cdb->lb_count); 5422 break; 5423 } 5424 case SYNCHRONIZE_CACHE_16: { 5425 struct scsi_sync_cache_16 *cdb; 5426 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5427 5428 starting_lba = scsi_8btou64(cdb->begin_lba); 5429 block_count = scsi_4btoul(cdb->lb_count); 5430 break; 5431 } 5432 default: 5433 ctl_set_invalid_opcode(ctsio); 5434 ctl_done((union ctl_io *)ctsio); 5435 goto bailout; 5436 break; /* NOTREACHED */ 5437 } 5438 5439 /* 5440 * We check the LBA and length, but don't do anything with them. 5441 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5442 * get flushed. This check will just help satisfy anyone who wants 5443 * to see an error for an out of range LBA. 5444 */ 5445 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5446 ctl_set_lba_out_of_range(ctsio); 5447 ctl_done((union ctl_io *)ctsio); 5448 goto bailout; 5449 } 5450 5451 /* 5452 * If this LUN has no backend, we can't flush the cache anyway. 5453 */ 5454 if (lun->backend == NULL) { 5455 ctl_set_invalid_opcode(ctsio); 5456 ctl_done((union ctl_io *)ctsio); 5457 goto bailout; 5458 } 5459 5460 /* 5461 * Check to see whether we're configured to send the SYNCHRONIZE 5462 * CACHE command directly to the back end. 5463 */ 5464 mtx_lock(&lun->lun_lock); 5465 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5466 && (++(lun->sync_count) >= lun->sync_interval)) { 5467 lun->sync_count = 0; 5468 mtx_unlock(&lun->lun_lock); 5469 retval = lun->backend->config_write((union ctl_io *)ctsio); 5470 } else { 5471 mtx_unlock(&lun->lun_lock); 5472 ctl_set_success(ctsio); 5473 ctl_done((union ctl_io *)ctsio); 5474 } 5475 5476bailout: 5477 5478 return (retval); 5479} 5480 5481int 5482ctl_format(struct ctl_scsiio *ctsio) 5483{ 5484 struct scsi_format *cdb; 5485 struct ctl_lun *lun; 5486 struct ctl_softc *ctl_softc; 5487 int length, defect_list_len; 5488 5489 CTL_DEBUG_PRINT(("ctl_format\n")); 5490 5491 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5492 ctl_softc = control_softc; 5493 5494 cdb = (struct scsi_format *)ctsio->cdb; 5495 5496 length = 0; 5497 if (cdb->byte2 & SF_FMTDATA) { 5498 if (cdb->byte2 & SF_LONGLIST) 5499 length = sizeof(struct scsi_format_header_long); 5500 else 5501 length = sizeof(struct scsi_format_header_short); 5502 } 5503 5504 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5505 && (length > 0)) { 5506 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5507 ctsio->kern_data_len = length; 5508 ctsio->kern_total_len = length; 5509 ctsio->kern_data_resid = 0; 5510 ctsio->kern_rel_offset = 0; 5511 ctsio->kern_sg_entries = 0; 5512 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5513 ctsio->be_move_done = ctl_config_move_done; 5514 ctl_datamove((union ctl_io *)ctsio); 5515 5516 return (CTL_RETVAL_COMPLETE); 5517 } 5518 5519 defect_list_len = 0; 5520 5521 if (cdb->byte2 & SF_FMTDATA) { 5522 if (cdb->byte2 & SF_LONGLIST) { 5523 struct scsi_format_header_long *header; 5524 5525 header = (struct scsi_format_header_long *) 5526 ctsio->kern_data_ptr; 5527 5528 defect_list_len = scsi_4btoul(header->defect_list_len); 5529 if (defect_list_len != 0) { 5530 ctl_set_invalid_field(ctsio, 5531 /*sks_valid*/ 1, 5532 /*command*/ 0, 5533 /*field*/ 2, 5534 /*bit_valid*/ 0, 5535 /*bit*/ 0); 5536 goto bailout; 5537 } 5538 } else { 5539 struct scsi_format_header_short *header; 5540 5541 header = (struct scsi_format_header_short *) 5542 ctsio->kern_data_ptr; 5543 5544 defect_list_len = scsi_2btoul(header->defect_list_len); 5545 if (defect_list_len != 0) { 5546 ctl_set_invalid_field(ctsio, 5547 /*sks_valid*/ 1, 5548 /*command*/ 0, 5549 /*field*/ 2, 5550 /*bit_valid*/ 0, 5551 /*bit*/ 0); 5552 goto bailout; 5553 } 5554 } 5555 } 5556 5557 /* 5558 * The format command will clear out the "Medium format corrupted" 5559 * status if set by the configuration code. That status is really 5560 * just a way to notify the host that we have lost the media, and 5561 * get them to issue a command that will basically make them think 5562 * they're blowing away the media. 5563 */ 5564 mtx_lock(&lun->lun_lock); 5565 lun->flags &= ~CTL_LUN_INOPERABLE; 5566 mtx_unlock(&lun->lun_lock); 5567 5568 ctsio->scsi_status = SCSI_STATUS_OK; 5569 ctsio->io_hdr.status = CTL_SUCCESS; 5570bailout: 5571 5572 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5573 free(ctsio->kern_data_ptr, M_CTL); 5574 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5575 } 5576 5577 ctl_done((union ctl_io *)ctsio); 5578 return (CTL_RETVAL_COMPLETE); 5579} 5580 5581int 5582ctl_read_buffer(struct ctl_scsiio *ctsio) 5583{ 5584 struct scsi_read_buffer *cdb; 5585 struct ctl_lun *lun; 5586 int buffer_offset, len; 5587 static uint8_t descr[4]; 5588 static uint8_t echo_descr[4] = { 0 }; 5589 5590 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5591 5592 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5593 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5594 5595 if (lun->flags & CTL_LUN_PR_RESERVED) { 5596 uint32_t residx; 5597 5598 /* 5599 * XXX KDM need a lock here. 5600 */ 5601 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5602 if ((lun->res_type == SPR_TYPE_EX_AC 5603 && residx != lun->pr_res_idx) 5604 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5605 || lun->res_type == SPR_TYPE_EX_AC_AR) 5606 && !lun->per_res[residx].registered)) { 5607 ctl_set_reservation_conflict(ctsio); 5608 ctl_done((union ctl_io *)ctsio); 5609 return (CTL_RETVAL_COMPLETE); 5610 } 5611 } 5612 5613 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5614 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5615 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5616 ctl_set_invalid_field(ctsio, 5617 /*sks_valid*/ 1, 5618 /*command*/ 1, 5619 /*field*/ 1, 5620 /*bit_valid*/ 1, 5621 /*bit*/ 4); 5622 ctl_done((union ctl_io *)ctsio); 5623 return (CTL_RETVAL_COMPLETE); 5624 } 5625 5626 len = scsi_3btoul(cdb->length); 5627 buffer_offset = scsi_3btoul(cdb->offset); 5628 5629 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5630 ctl_set_invalid_field(ctsio, 5631 /*sks_valid*/ 1, 5632 /*command*/ 1, 5633 /*field*/ 6, 5634 /*bit_valid*/ 0, 5635 /*bit*/ 0); 5636 ctl_done((union ctl_io *)ctsio); 5637 return (CTL_RETVAL_COMPLETE); 5638 } 5639 5640 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5641 descr[0] = 0; 5642 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5643 ctsio->kern_data_ptr = descr; 5644 len = min(len, sizeof(descr)); 5645 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5646 ctsio->kern_data_ptr = echo_descr; 5647 len = min(len, sizeof(echo_descr)); 5648 } else 5649 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5650 ctsio->kern_data_len = len; 5651 ctsio->kern_total_len = len; 5652 ctsio->kern_data_resid = 0; 5653 ctsio->kern_rel_offset = 0; 5654 ctsio->kern_sg_entries = 0; 5655 ctsio->be_move_done = ctl_config_move_done; 5656 ctl_datamove((union ctl_io *)ctsio); 5657 5658 return (CTL_RETVAL_COMPLETE); 5659} 5660 5661int 5662ctl_write_buffer(struct ctl_scsiio *ctsio) 5663{ 5664 struct scsi_write_buffer *cdb; 5665 struct ctl_lun *lun; 5666 int buffer_offset, len; 5667 5668 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5669 5670 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5671 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5672 5673 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5674 ctl_set_invalid_field(ctsio, 5675 /*sks_valid*/ 1, 5676 /*command*/ 1, 5677 /*field*/ 1, 5678 /*bit_valid*/ 1, 5679 /*bit*/ 4); 5680 ctl_done((union ctl_io *)ctsio); 5681 return (CTL_RETVAL_COMPLETE); 5682 } 5683 5684 len = scsi_3btoul(cdb->length); 5685 buffer_offset = scsi_3btoul(cdb->offset); 5686 5687 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5688 ctl_set_invalid_field(ctsio, 5689 /*sks_valid*/ 1, 5690 /*command*/ 1, 5691 /*field*/ 6, 5692 /*bit_valid*/ 0, 5693 /*bit*/ 0); 5694 ctl_done((union ctl_io *)ctsio); 5695 return (CTL_RETVAL_COMPLETE); 5696 } 5697 5698 /* 5699 * If we've got a kernel request that hasn't been malloced yet, 5700 * malloc it and tell the caller the data buffer is here. 5701 */ 5702 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5703 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5704 ctsio->kern_data_len = len; 5705 ctsio->kern_total_len = len; 5706 ctsio->kern_data_resid = 0; 5707 ctsio->kern_rel_offset = 0; 5708 ctsio->kern_sg_entries = 0; 5709 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5710 ctsio->be_move_done = ctl_config_move_done; 5711 ctl_datamove((union ctl_io *)ctsio); 5712 5713 return (CTL_RETVAL_COMPLETE); 5714 } 5715 5716 ctl_done((union ctl_io *)ctsio); 5717 5718 return (CTL_RETVAL_COMPLETE); 5719} 5720 5721int 5722ctl_write_same(struct ctl_scsiio *ctsio) 5723{ 5724 struct ctl_lun *lun; 5725 struct ctl_lba_len_flags *lbalen; 5726 uint64_t lba; 5727 uint32_t num_blocks; 5728 int len, retval; 5729 uint8_t byte2; 5730 5731 retval = CTL_RETVAL_COMPLETE; 5732 5733 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5734 5735 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5736 5737 switch (ctsio->cdb[0]) { 5738 case WRITE_SAME_10: { 5739 struct scsi_write_same_10 *cdb; 5740 5741 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5742 5743 lba = scsi_4btoul(cdb->addr); 5744 num_blocks = scsi_2btoul(cdb->length); 5745 byte2 = cdb->byte2; 5746 break; 5747 } 5748 case WRITE_SAME_16: { 5749 struct scsi_write_same_16 *cdb; 5750 5751 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5752 5753 lba = scsi_8btou64(cdb->addr); 5754 num_blocks = scsi_4btoul(cdb->length); 5755 byte2 = cdb->byte2; 5756 break; 5757 } 5758 default: 5759 /* 5760 * We got a command we don't support. This shouldn't 5761 * happen, commands should be filtered out above us. 5762 */ 5763 ctl_set_invalid_opcode(ctsio); 5764 ctl_done((union ctl_io *)ctsio); 5765 5766 return (CTL_RETVAL_COMPLETE); 5767 break; /* NOTREACHED */ 5768 } 5769 5770 /* 5771 * The first check is to make sure we're in bounds, the second 5772 * check is to catch wrap-around problems. If the lba + num blocks 5773 * is less than the lba, then we've wrapped around and the block 5774 * range is invalid anyway. 5775 */ 5776 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5777 || ((lba + num_blocks) < lba)) { 5778 ctl_set_lba_out_of_range(ctsio); 5779 ctl_done((union ctl_io *)ctsio); 5780 return (CTL_RETVAL_COMPLETE); 5781 } 5782 5783 /* Zero number of blocks means "to the last logical block" */ 5784 if (num_blocks == 0) { 5785 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5786 ctl_set_invalid_field(ctsio, 5787 /*sks_valid*/ 0, 5788 /*command*/ 1, 5789 /*field*/ 0, 5790 /*bit_valid*/ 0, 5791 /*bit*/ 0); 5792 ctl_done((union ctl_io *)ctsio); 5793 return (CTL_RETVAL_COMPLETE); 5794 } 5795 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5796 } 5797 5798 len = lun->be_lun->blocksize; 5799 5800 /* 5801 * If we've got a kernel request that hasn't been malloced yet, 5802 * malloc it and tell the caller the data buffer is here. 5803 */ 5804 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5805 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5806 ctsio->kern_data_len = len; 5807 ctsio->kern_total_len = len; 5808 ctsio->kern_data_resid = 0; 5809 ctsio->kern_rel_offset = 0; 5810 ctsio->kern_sg_entries = 0; 5811 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5812 ctsio->be_move_done = ctl_config_move_done; 5813 ctl_datamove((union ctl_io *)ctsio); 5814 5815 return (CTL_RETVAL_COMPLETE); 5816 } 5817 5818 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5819 lbalen->lba = lba; 5820 lbalen->len = num_blocks; 5821 lbalen->flags = byte2; 5822 retval = lun->backend->config_write((union ctl_io *)ctsio); 5823 5824 return (retval); 5825} 5826 5827int 5828ctl_unmap(struct ctl_scsiio *ctsio) 5829{ 5830 struct ctl_lun *lun; 5831 struct scsi_unmap *cdb; 5832 struct ctl_ptr_len_flags *ptrlen; 5833 struct scsi_unmap_header *hdr; 5834 struct scsi_unmap_desc *buf, *end; 5835 uint64_t lba; 5836 uint32_t num_blocks; 5837 int len, retval; 5838 uint8_t byte2; 5839 5840 retval = CTL_RETVAL_COMPLETE; 5841 5842 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5843 5844 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5845 cdb = (struct scsi_unmap *)ctsio->cdb; 5846 5847 len = scsi_2btoul(cdb->length); 5848 byte2 = cdb->byte2; 5849 5850 /* 5851 * If we've got a kernel request that hasn't been malloced yet, 5852 * malloc it and tell the caller the data buffer is here. 5853 */ 5854 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5855 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5856 ctsio->kern_data_len = len; 5857 ctsio->kern_total_len = len; 5858 ctsio->kern_data_resid = 0; 5859 ctsio->kern_rel_offset = 0; 5860 ctsio->kern_sg_entries = 0; 5861 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5862 ctsio->be_move_done = ctl_config_move_done; 5863 ctl_datamove((union ctl_io *)ctsio); 5864 5865 return (CTL_RETVAL_COMPLETE); 5866 } 5867 5868 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5869 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5870 if (len < sizeof (*hdr) || 5871 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5872 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5873 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5874 ctl_set_invalid_field(ctsio, 5875 /*sks_valid*/ 0, 5876 /*command*/ 0, 5877 /*field*/ 0, 5878 /*bit_valid*/ 0, 5879 /*bit*/ 0); 5880 ctl_done((union ctl_io *)ctsio); 5881 return (CTL_RETVAL_COMPLETE); 5882 } 5883 len = scsi_2btoul(hdr->desc_length); 5884 buf = (struct scsi_unmap_desc *)(hdr + 1); 5885 end = buf + len / sizeof(*buf); 5886 5887 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5888 ptrlen->ptr = (void *)buf; 5889 ptrlen->len = len; 5890 ptrlen->flags = byte2; 5891 5892 for (; buf < end; buf++) { 5893 lba = scsi_8btou64(buf->lba); 5894 num_blocks = scsi_4btoul(buf->length); 5895 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5896 || ((lba + num_blocks) < lba)) { 5897 ctl_set_lba_out_of_range(ctsio); 5898 ctl_done((union ctl_io *)ctsio); 5899 return (CTL_RETVAL_COMPLETE); 5900 } 5901 } 5902 5903 retval = lun->backend->config_write((union ctl_io *)ctsio); 5904 5905 return (retval); 5906} 5907 5908/* 5909 * Note that this function currently doesn't actually do anything inside 5910 * CTL to enforce things if the DQue bit is turned on. 5911 * 5912 * Also note that this function can't be used in the default case, because 5913 * the DQue bit isn't set in the changeable mask for the control mode page 5914 * anyway. This is just here as an example for how to implement a page 5915 * handler, and a placeholder in case we want to allow the user to turn 5916 * tagged queueing on and off. 5917 * 5918 * The D_SENSE bit handling is functional, however, and will turn 5919 * descriptor sense on and off for a given LUN. 5920 */ 5921int 5922ctl_control_page_handler(struct ctl_scsiio *ctsio, 5923 struct ctl_page_index *page_index, uint8_t *page_ptr) 5924{ 5925 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5926 struct ctl_lun *lun; 5927 struct ctl_softc *softc; 5928 int set_ua; 5929 uint32_t initidx; 5930 5931 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5932 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5933 set_ua = 0; 5934 5935 user_cp = (struct scsi_control_page *)page_ptr; 5936 current_cp = (struct scsi_control_page *) 5937 (page_index->page_data + (page_index->page_len * 5938 CTL_PAGE_CURRENT)); 5939 saved_cp = (struct scsi_control_page *) 5940 (page_index->page_data + (page_index->page_len * 5941 CTL_PAGE_SAVED)); 5942 5943 softc = control_softc; 5944 5945 mtx_lock(&lun->lun_lock); 5946 if (((current_cp->rlec & SCP_DSENSE) == 0) 5947 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5948 /* 5949 * Descriptor sense is currently turned off and the user 5950 * wants to turn it on. 5951 */ 5952 current_cp->rlec |= SCP_DSENSE; 5953 saved_cp->rlec |= SCP_DSENSE; 5954 lun->flags |= CTL_LUN_SENSE_DESC; 5955 set_ua = 1; 5956 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5957 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5958 /* 5959 * Descriptor sense is currently turned on, and the user 5960 * wants to turn it off. 5961 */ 5962 current_cp->rlec &= ~SCP_DSENSE; 5963 saved_cp->rlec &= ~SCP_DSENSE; 5964 lun->flags &= ~CTL_LUN_SENSE_DESC; 5965 set_ua = 1; 5966 } 5967 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 5968 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 5969#ifdef NEEDTOPORT 5970 csevent_log(CSC_CTL | CSC_SHELF_SW | 5971 CTL_UNTAG_TO_UNTAG, 5972 csevent_LogType_Trace, 5973 csevent_Severity_Information, 5974 csevent_AlertLevel_Green, 5975 csevent_FRU_Firmware, 5976 csevent_FRU_Unknown, 5977 "Received untagged to untagged transition"); 5978#endif /* NEEDTOPORT */ 5979 } else { 5980#ifdef NEEDTOPORT 5981 csevent_log(CSC_CTL | CSC_SHELF_SW | 5982 CTL_UNTAG_TO_TAG, 5983 csevent_LogType_ConfigChange, 5984 csevent_Severity_Information, 5985 csevent_AlertLevel_Green, 5986 csevent_FRU_Firmware, 5987 csevent_FRU_Unknown, 5988 "Received untagged to tagged " 5989 "queueing transition"); 5990#endif /* NEEDTOPORT */ 5991 5992 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 5993 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 5994 set_ua = 1; 5995 } 5996 } else { 5997 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 5998#ifdef NEEDTOPORT 5999 csevent_log(CSC_CTL | CSC_SHELF_SW | 6000 CTL_TAG_TO_UNTAG, 6001 csevent_LogType_ConfigChange, 6002 csevent_Severity_Warning, 6003 csevent_AlertLevel_Yellow, 6004 csevent_FRU_Firmware, 6005 csevent_FRU_Unknown, 6006 "Received tagged queueing to untagged " 6007 "transition"); 6008#endif /* NEEDTOPORT */ 6009 6010 current_cp->queue_flags |= SCP_QUEUE_DQUE; 6011 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 6012 set_ua = 1; 6013 } else { 6014#ifdef NEEDTOPORT 6015 csevent_log(CSC_CTL | CSC_SHELF_SW | 6016 CTL_TAG_TO_TAG, 6017 csevent_LogType_Trace, 6018 csevent_Severity_Information, 6019 csevent_AlertLevel_Green, 6020 csevent_FRU_Firmware, 6021 csevent_FRU_Unknown, 6022 "Received tagged queueing to tagged " 6023 "queueing transition"); 6024#endif /* NEEDTOPORT */ 6025 } 6026 } 6027 if (set_ua != 0) { 6028 int i; 6029 /* 6030 * Let other initiators know that the mode 6031 * parameters for this LUN have changed. 6032 */ 6033 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6034 if (i == initidx) 6035 continue; 6036 6037 lun->pending_sense[i].ua_pending |= 6038 CTL_UA_MODE_CHANGE; 6039 } 6040 } 6041 mtx_unlock(&lun->lun_lock); 6042 6043 return (0); 6044} 6045 6046int 6047ctl_power_sp_handler(struct ctl_scsiio *ctsio, 6048 struct ctl_page_index *page_index, uint8_t *page_ptr) 6049{ 6050 return (0); 6051} 6052 6053int 6054ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 6055 struct ctl_page_index *page_index, int pc) 6056{ 6057 struct copan_power_subpage *page; 6058 6059 page = (struct copan_power_subpage *)page_index->page_data + 6060 (page_index->page_len * pc); 6061 6062 switch (pc) { 6063 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6064 /* 6065 * We don't update the changable bits for this page. 6066 */ 6067 break; 6068 case SMS_PAGE_CTRL_CURRENT >> 6: 6069 case SMS_PAGE_CTRL_DEFAULT >> 6: 6070 case SMS_PAGE_CTRL_SAVED >> 6: 6071#ifdef NEEDTOPORT 6072 ctl_update_power_subpage(page); 6073#endif 6074 break; 6075 default: 6076#ifdef NEEDTOPORT 6077 EPRINT(0, "Invalid PC %d!!", pc); 6078#endif 6079 break; 6080 } 6081 return (0); 6082} 6083 6084 6085int 6086ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 6087 struct ctl_page_index *page_index, uint8_t *page_ptr) 6088{ 6089 struct copan_aps_subpage *user_sp; 6090 struct copan_aps_subpage *current_sp; 6091 union ctl_modepage_info *modepage_info; 6092 struct ctl_softc *softc; 6093 struct ctl_lun *lun; 6094 int retval; 6095 6096 retval = CTL_RETVAL_COMPLETE; 6097 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 6098 (page_index->page_len * CTL_PAGE_CURRENT)); 6099 softc = control_softc; 6100 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6101 6102 user_sp = (struct copan_aps_subpage *)page_ptr; 6103 6104 modepage_info = (union ctl_modepage_info *) 6105 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6106 6107 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 6108 modepage_info->header.subpage = page_index->subpage; 6109 modepage_info->aps.lock_active = user_sp->lock_active; 6110 6111 mtx_lock(&softc->ctl_lock); 6112 6113 /* 6114 * If there is a request to lock the LUN and another LUN is locked 6115 * this is an error. If the requested LUN is already locked ignore 6116 * the request. If no LUN is locked attempt to lock it. 6117 * if there is a request to unlock the LUN and the LUN is currently 6118 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6119 * if another LUN is locked or no LUN is locked. 6120 */ 6121 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6122 if (softc->aps_locked_lun == lun->lun) { 6123 /* 6124 * This LUN is already locked, so we're done. 6125 */ 6126 retval = CTL_RETVAL_COMPLETE; 6127 } else if (softc->aps_locked_lun == 0) { 6128 /* 6129 * No one has the lock, pass the request to the 6130 * backend. 6131 */ 6132 retval = lun->backend->config_write( 6133 (union ctl_io *)ctsio); 6134 } else { 6135 /* 6136 * Someone else has the lock, throw out the request. 6137 */ 6138 ctl_set_already_locked(ctsio); 6139 free(ctsio->kern_data_ptr, M_CTL); 6140 ctl_done((union ctl_io *)ctsio); 6141 6142 /* 6143 * Set the return value so that ctl_do_mode_select() 6144 * won't try to complete the command. We already 6145 * completed it here. 6146 */ 6147 retval = CTL_RETVAL_ERROR; 6148 } 6149 } else if (softc->aps_locked_lun == lun->lun) { 6150 /* 6151 * This LUN is locked, so pass the unlock request to the 6152 * backend. 6153 */ 6154 retval = lun->backend->config_write((union ctl_io *)ctsio); 6155 } 6156 mtx_unlock(&softc->ctl_lock); 6157 6158 return (retval); 6159} 6160 6161int 6162ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6163 struct ctl_page_index *page_index, 6164 uint8_t *page_ptr) 6165{ 6166 uint8_t *c; 6167 int i; 6168 6169 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6170 ctl_time_io_secs = 6171 (c[0] << 8) | 6172 (c[1] << 0) | 6173 0; 6174 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6175 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6176 printf("page data:"); 6177 for (i=0; i<8; i++) 6178 printf(" %.2x",page_ptr[i]); 6179 printf("\n"); 6180 return (0); 6181} 6182 6183int 6184ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6185 struct ctl_page_index *page_index, 6186 int pc) 6187{ 6188 struct copan_debugconf_subpage *page; 6189 6190 page = (struct copan_debugconf_subpage *)page_index->page_data + 6191 (page_index->page_len * pc); 6192 6193 switch (pc) { 6194 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6195 case SMS_PAGE_CTRL_DEFAULT >> 6: 6196 case SMS_PAGE_CTRL_SAVED >> 6: 6197 /* 6198 * We don't update the changable or default bits for this page. 6199 */ 6200 break; 6201 case SMS_PAGE_CTRL_CURRENT >> 6: 6202 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6203 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6204 break; 6205 default: 6206#ifdef NEEDTOPORT 6207 EPRINT(0, "Invalid PC %d!!", pc); 6208#endif /* NEEDTOPORT */ 6209 break; 6210 } 6211 return (0); 6212} 6213 6214 6215static int 6216ctl_do_mode_select(union ctl_io *io) 6217{ 6218 struct scsi_mode_page_header *page_header; 6219 struct ctl_page_index *page_index; 6220 struct ctl_scsiio *ctsio; 6221 int control_dev, page_len; 6222 int page_len_offset, page_len_size; 6223 union ctl_modepage_info *modepage_info; 6224 struct ctl_lun *lun; 6225 int *len_left, *len_used; 6226 int retval, i; 6227 6228 ctsio = &io->scsiio; 6229 page_index = NULL; 6230 page_len = 0; 6231 retval = CTL_RETVAL_COMPLETE; 6232 6233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6234 6235 if (lun->be_lun->lun_type != T_DIRECT) 6236 control_dev = 1; 6237 else 6238 control_dev = 0; 6239 6240 modepage_info = (union ctl_modepage_info *) 6241 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6242 len_left = &modepage_info->header.len_left; 6243 len_used = &modepage_info->header.len_used; 6244 6245do_next_page: 6246 6247 page_header = (struct scsi_mode_page_header *) 6248 (ctsio->kern_data_ptr + *len_used); 6249 6250 if (*len_left == 0) { 6251 free(ctsio->kern_data_ptr, M_CTL); 6252 ctl_set_success(ctsio); 6253 ctl_done((union ctl_io *)ctsio); 6254 return (CTL_RETVAL_COMPLETE); 6255 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6256 6257 free(ctsio->kern_data_ptr, M_CTL); 6258 ctl_set_param_len_error(ctsio); 6259 ctl_done((union ctl_io *)ctsio); 6260 return (CTL_RETVAL_COMPLETE); 6261 6262 } else if ((page_header->page_code & SMPH_SPF) 6263 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6264 6265 free(ctsio->kern_data_ptr, M_CTL); 6266 ctl_set_param_len_error(ctsio); 6267 ctl_done((union ctl_io *)ctsio); 6268 return (CTL_RETVAL_COMPLETE); 6269 } 6270 6271 6272 /* 6273 * XXX KDM should we do something with the block descriptor? 6274 */ 6275 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6276 6277 if ((control_dev != 0) 6278 && (lun->mode_pages.index[i].page_flags & 6279 CTL_PAGE_FLAG_DISK_ONLY)) 6280 continue; 6281 6282 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6283 (page_header->page_code & SMPH_PC_MASK)) 6284 continue; 6285 6286 /* 6287 * If neither page has a subpage code, then we've got a 6288 * match. 6289 */ 6290 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6291 && ((page_header->page_code & SMPH_SPF) == 0)) { 6292 page_index = &lun->mode_pages.index[i]; 6293 page_len = page_header->page_length; 6294 break; 6295 } 6296 6297 /* 6298 * If both pages have subpages, then the subpage numbers 6299 * have to match. 6300 */ 6301 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6302 && (page_header->page_code & SMPH_SPF)) { 6303 struct scsi_mode_page_header_sp *sph; 6304 6305 sph = (struct scsi_mode_page_header_sp *)page_header; 6306 6307 if (lun->mode_pages.index[i].subpage == 6308 sph->subpage) { 6309 page_index = &lun->mode_pages.index[i]; 6310 page_len = scsi_2btoul(sph->page_length); 6311 break; 6312 } 6313 } 6314 } 6315 6316 /* 6317 * If we couldn't find the page, or if we don't have a mode select 6318 * handler for it, send back an error to the user. 6319 */ 6320 if ((page_index == NULL) 6321 || (page_index->select_handler == NULL)) { 6322 ctl_set_invalid_field(ctsio, 6323 /*sks_valid*/ 1, 6324 /*command*/ 0, 6325 /*field*/ *len_used, 6326 /*bit_valid*/ 0, 6327 /*bit*/ 0); 6328 free(ctsio->kern_data_ptr, M_CTL); 6329 ctl_done((union ctl_io *)ctsio); 6330 return (CTL_RETVAL_COMPLETE); 6331 } 6332 6333 if (page_index->page_code & SMPH_SPF) { 6334 page_len_offset = 2; 6335 page_len_size = 2; 6336 } else { 6337 page_len_size = 1; 6338 page_len_offset = 1; 6339 } 6340 6341 /* 6342 * If the length the initiator gives us isn't the one we specify in 6343 * the mode page header, or if they didn't specify enough data in 6344 * the CDB to avoid truncating this page, kick out the request. 6345 */ 6346 if ((page_len != (page_index->page_len - page_len_offset - 6347 page_len_size)) 6348 || (*len_left < page_index->page_len)) { 6349 6350 6351 ctl_set_invalid_field(ctsio, 6352 /*sks_valid*/ 1, 6353 /*command*/ 0, 6354 /*field*/ *len_used + page_len_offset, 6355 /*bit_valid*/ 0, 6356 /*bit*/ 0); 6357 free(ctsio->kern_data_ptr, M_CTL); 6358 ctl_done((union ctl_io *)ctsio); 6359 return (CTL_RETVAL_COMPLETE); 6360 } 6361 6362 /* 6363 * Run through the mode page, checking to make sure that the bits 6364 * the user changed are actually legal for him to change. 6365 */ 6366 for (i = 0; i < page_index->page_len; i++) { 6367 uint8_t *user_byte, *change_mask, *current_byte; 6368 int bad_bit; 6369 int j; 6370 6371 user_byte = (uint8_t *)page_header + i; 6372 change_mask = page_index->page_data + 6373 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6374 current_byte = page_index->page_data + 6375 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6376 6377 /* 6378 * Check to see whether the user set any bits in this byte 6379 * that he is not allowed to set. 6380 */ 6381 if ((*user_byte & ~(*change_mask)) == 6382 (*current_byte & ~(*change_mask))) 6383 continue; 6384 6385 /* 6386 * Go through bit by bit to determine which one is illegal. 6387 */ 6388 bad_bit = 0; 6389 for (j = 7; j >= 0; j--) { 6390 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6391 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6392 bad_bit = i; 6393 break; 6394 } 6395 } 6396 ctl_set_invalid_field(ctsio, 6397 /*sks_valid*/ 1, 6398 /*command*/ 0, 6399 /*field*/ *len_used + i, 6400 /*bit_valid*/ 1, 6401 /*bit*/ bad_bit); 6402 free(ctsio->kern_data_ptr, M_CTL); 6403 ctl_done((union ctl_io *)ctsio); 6404 return (CTL_RETVAL_COMPLETE); 6405 } 6406 6407 /* 6408 * Decrement these before we call the page handler, since we may 6409 * end up getting called back one way or another before the handler 6410 * returns to this context. 6411 */ 6412 *len_left -= page_index->page_len; 6413 *len_used += page_index->page_len; 6414 6415 retval = page_index->select_handler(ctsio, page_index, 6416 (uint8_t *)page_header); 6417 6418 /* 6419 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6420 * wait until this queued command completes to finish processing 6421 * the mode page. If it returns anything other than 6422 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6423 * already set the sense information, freed the data pointer, and 6424 * completed the io for us. 6425 */ 6426 if (retval != CTL_RETVAL_COMPLETE) 6427 goto bailout_no_done; 6428 6429 /* 6430 * If the initiator sent us more than one page, parse the next one. 6431 */ 6432 if (*len_left > 0) 6433 goto do_next_page; 6434 6435 ctl_set_success(ctsio); 6436 free(ctsio->kern_data_ptr, M_CTL); 6437 ctl_done((union ctl_io *)ctsio); 6438 6439bailout_no_done: 6440 6441 return (CTL_RETVAL_COMPLETE); 6442 6443} 6444 6445int 6446ctl_mode_select(struct ctl_scsiio *ctsio) 6447{ 6448 int param_len, pf, sp; 6449 int header_size, bd_len; 6450 int len_left, len_used; 6451 struct ctl_page_index *page_index; 6452 struct ctl_lun *lun; 6453 int control_dev, page_len; 6454 union ctl_modepage_info *modepage_info; 6455 int retval; 6456 6457 pf = 0; 6458 sp = 0; 6459 page_len = 0; 6460 len_used = 0; 6461 len_left = 0; 6462 retval = 0; 6463 bd_len = 0; 6464 page_index = NULL; 6465 6466 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6467 6468 if (lun->be_lun->lun_type != T_DIRECT) 6469 control_dev = 1; 6470 else 6471 control_dev = 0; 6472 6473 switch (ctsio->cdb[0]) { 6474 case MODE_SELECT_6: { 6475 struct scsi_mode_select_6 *cdb; 6476 6477 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6478 6479 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6480 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6481 6482 param_len = cdb->length; 6483 header_size = sizeof(struct scsi_mode_header_6); 6484 break; 6485 } 6486 case MODE_SELECT_10: { 6487 struct scsi_mode_select_10 *cdb; 6488 6489 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6490 6491 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6492 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6493 6494 param_len = scsi_2btoul(cdb->length); 6495 header_size = sizeof(struct scsi_mode_header_10); 6496 break; 6497 } 6498 default: 6499 ctl_set_invalid_opcode(ctsio); 6500 ctl_done((union ctl_io *)ctsio); 6501 return (CTL_RETVAL_COMPLETE); 6502 break; /* NOTREACHED */ 6503 } 6504 6505 /* 6506 * From SPC-3: 6507 * "A parameter list length of zero indicates that the Data-Out Buffer 6508 * shall be empty. This condition shall not be considered as an error." 6509 */ 6510 if (param_len == 0) { 6511 ctl_set_success(ctsio); 6512 ctl_done((union ctl_io *)ctsio); 6513 return (CTL_RETVAL_COMPLETE); 6514 } 6515 6516 /* 6517 * Since we'll hit this the first time through, prior to 6518 * allocation, we don't need to free a data buffer here. 6519 */ 6520 if (param_len < header_size) { 6521 ctl_set_param_len_error(ctsio); 6522 ctl_done((union ctl_io *)ctsio); 6523 return (CTL_RETVAL_COMPLETE); 6524 } 6525 6526 /* 6527 * Allocate the data buffer and grab the user's data. In theory, 6528 * we shouldn't have to sanity check the parameter list length here 6529 * because the maximum size is 64K. We should be able to malloc 6530 * that much without too many problems. 6531 */ 6532 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6533 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6534 ctsio->kern_data_len = param_len; 6535 ctsio->kern_total_len = param_len; 6536 ctsio->kern_data_resid = 0; 6537 ctsio->kern_rel_offset = 0; 6538 ctsio->kern_sg_entries = 0; 6539 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6540 ctsio->be_move_done = ctl_config_move_done; 6541 ctl_datamove((union ctl_io *)ctsio); 6542 6543 return (CTL_RETVAL_COMPLETE); 6544 } 6545 6546 switch (ctsio->cdb[0]) { 6547 case MODE_SELECT_6: { 6548 struct scsi_mode_header_6 *mh6; 6549 6550 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6551 bd_len = mh6->blk_desc_len; 6552 break; 6553 } 6554 case MODE_SELECT_10: { 6555 struct scsi_mode_header_10 *mh10; 6556 6557 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6558 bd_len = scsi_2btoul(mh10->blk_desc_len); 6559 break; 6560 } 6561 default: 6562 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6563 break; 6564 } 6565 6566 if (param_len < (header_size + bd_len)) { 6567 free(ctsio->kern_data_ptr, M_CTL); 6568 ctl_set_param_len_error(ctsio); 6569 ctl_done((union ctl_io *)ctsio); 6570 return (CTL_RETVAL_COMPLETE); 6571 } 6572 6573 /* 6574 * Set the IO_CONT flag, so that if this I/O gets passed to 6575 * ctl_config_write_done(), it'll get passed back to 6576 * ctl_do_mode_select() for further processing, or completion if 6577 * we're all done. 6578 */ 6579 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6580 ctsio->io_cont = ctl_do_mode_select; 6581 6582 modepage_info = (union ctl_modepage_info *) 6583 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6584 6585 memset(modepage_info, 0, sizeof(*modepage_info)); 6586 6587 len_left = param_len - header_size - bd_len; 6588 len_used = header_size + bd_len; 6589 6590 modepage_info->header.len_left = len_left; 6591 modepage_info->header.len_used = len_used; 6592 6593 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6594} 6595 6596int 6597ctl_mode_sense(struct ctl_scsiio *ctsio) 6598{ 6599 struct ctl_lun *lun; 6600 int pc, page_code, dbd, llba, subpage; 6601 int alloc_len, page_len, header_len, total_len; 6602 struct scsi_mode_block_descr *block_desc; 6603 struct ctl_page_index *page_index; 6604 int control_dev; 6605 6606 dbd = 0; 6607 llba = 0; 6608 block_desc = NULL; 6609 page_index = NULL; 6610 6611 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6612 6613 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6614 6615 if (lun->be_lun->lun_type != T_DIRECT) 6616 control_dev = 1; 6617 else 6618 control_dev = 0; 6619 6620 if (lun->flags & CTL_LUN_PR_RESERVED) { 6621 uint32_t residx; 6622 6623 /* 6624 * XXX KDM need a lock here. 6625 */ 6626 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6627 if ((lun->res_type == SPR_TYPE_EX_AC 6628 && residx != lun->pr_res_idx) 6629 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6630 || lun->res_type == SPR_TYPE_EX_AC_AR) 6631 && !lun->per_res[residx].registered)) { 6632 ctl_set_reservation_conflict(ctsio); 6633 ctl_done((union ctl_io *)ctsio); 6634 return (CTL_RETVAL_COMPLETE); 6635 } 6636 } 6637 6638 switch (ctsio->cdb[0]) { 6639 case MODE_SENSE_6: { 6640 struct scsi_mode_sense_6 *cdb; 6641 6642 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6643 6644 header_len = sizeof(struct scsi_mode_hdr_6); 6645 if (cdb->byte2 & SMS_DBD) 6646 dbd = 1; 6647 else 6648 header_len += sizeof(struct scsi_mode_block_descr); 6649 6650 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6651 page_code = cdb->page & SMS_PAGE_CODE; 6652 subpage = cdb->subpage; 6653 alloc_len = cdb->length; 6654 break; 6655 } 6656 case MODE_SENSE_10: { 6657 struct scsi_mode_sense_10 *cdb; 6658 6659 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6660 6661 header_len = sizeof(struct scsi_mode_hdr_10); 6662 6663 if (cdb->byte2 & SMS_DBD) 6664 dbd = 1; 6665 else 6666 header_len += sizeof(struct scsi_mode_block_descr); 6667 if (cdb->byte2 & SMS10_LLBAA) 6668 llba = 1; 6669 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6670 page_code = cdb->page & SMS_PAGE_CODE; 6671 subpage = cdb->subpage; 6672 alloc_len = scsi_2btoul(cdb->length); 6673 break; 6674 } 6675 default: 6676 ctl_set_invalid_opcode(ctsio); 6677 ctl_done((union ctl_io *)ctsio); 6678 return (CTL_RETVAL_COMPLETE); 6679 break; /* NOTREACHED */ 6680 } 6681 6682 /* 6683 * We have to make a first pass through to calculate the size of 6684 * the pages that match the user's query. Then we allocate enough 6685 * memory to hold it, and actually copy the data into the buffer. 6686 */ 6687 switch (page_code) { 6688 case SMS_ALL_PAGES_PAGE: { 6689 int i; 6690 6691 page_len = 0; 6692 6693 /* 6694 * At the moment, values other than 0 and 0xff here are 6695 * reserved according to SPC-3. 6696 */ 6697 if ((subpage != SMS_SUBPAGE_PAGE_0) 6698 && (subpage != SMS_SUBPAGE_ALL)) { 6699 ctl_set_invalid_field(ctsio, 6700 /*sks_valid*/ 1, 6701 /*command*/ 1, 6702 /*field*/ 3, 6703 /*bit_valid*/ 0, 6704 /*bit*/ 0); 6705 ctl_done((union ctl_io *)ctsio); 6706 return (CTL_RETVAL_COMPLETE); 6707 } 6708 6709 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6710 if ((control_dev != 0) 6711 && (lun->mode_pages.index[i].page_flags & 6712 CTL_PAGE_FLAG_DISK_ONLY)) 6713 continue; 6714 6715 /* 6716 * We don't use this subpage if the user didn't 6717 * request all subpages. 6718 */ 6719 if ((lun->mode_pages.index[i].subpage != 0) 6720 && (subpage == SMS_SUBPAGE_PAGE_0)) 6721 continue; 6722 6723#if 0 6724 printf("found page %#x len %d\n", 6725 lun->mode_pages.index[i].page_code & 6726 SMPH_PC_MASK, 6727 lun->mode_pages.index[i].page_len); 6728#endif 6729 page_len += lun->mode_pages.index[i].page_len; 6730 } 6731 break; 6732 } 6733 default: { 6734 int i; 6735 6736 page_len = 0; 6737 6738 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6739 /* Look for the right page code */ 6740 if ((lun->mode_pages.index[i].page_code & 6741 SMPH_PC_MASK) != page_code) 6742 continue; 6743 6744 /* Look for the right subpage or the subpage wildcard*/ 6745 if ((lun->mode_pages.index[i].subpage != subpage) 6746 && (subpage != SMS_SUBPAGE_ALL)) 6747 continue; 6748 6749 /* Make sure the page is supported for this dev type */ 6750 if ((control_dev != 0) 6751 && (lun->mode_pages.index[i].page_flags & 6752 CTL_PAGE_FLAG_DISK_ONLY)) 6753 continue; 6754 6755#if 0 6756 printf("found page %#x len %d\n", 6757 lun->mode_pages.index[i].page_code & 6758 SMPH_PC_MASK, 6759 lun->mode_pages.index[i].page_len); 6760#endif 6761 6762 page_len += lun->mode_pages.index[i].page_len; 6763 } 6764 6765 if (page_len == 0) { 6766 ctl_set_invalid_field(ctsio, 6767 /*sks_valid*/ 1, 6768 /*command*/ 1, 6769 /*field*/ 2, 6770 /*bit_valid*/ 1, 6771 /*bit*/ 5); 6772 ctl_done((union ctl_io *)ctsio); 6773 return (CTL_RETVAL_COMPLETE); 6774 } 6775 break; 6776 } 6777 } 6778 6779 total_len = header_len + page_len; 6780#if 0 6781 printf("header_len = %d, page_len = %d, total_len = %d\n", 6782 header_len, page_len, total_len); 6783#endif 6784 6785 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6786 ctsio->kern_sg_entries = 0; 6787 ctsio->kern_data_resid = 0; 6788 ctsio->kern_rel_offset = 0; 6789 if (total_len < alloc_len) { 6790 ctsio->residual = alloc_len - total_len; 6791 ctsio->kern_data_len = total_len; 6792 ctsio->kern_total_len = total_len; 6793 } else { 6794 ctsio->residual = 0; 6795 ctsio->kern_data_len = alloc_len; 6796 ctsio->kern_total_len = alloc_len; 6797 } 6798 6799 switch (ctsio->cdb[0]) { 6800 case MODE_SENSE_6: { 6801 struct scsi_mode_hdr_6 *header; 6802 6803 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6804 6805 header->datalen = ctl_min(total_len - 1, 254); 6806 6807 if (dbd) 6808 header->block_descr_len = 0; 6809 else 6810 header->block_descr_len = 6811 sizeof(struct scsi_mode_block_descr); 6812 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6813 break; 6814 } 6815 case MODE_SENSE_10: { 6816 struct scsi_mode_hdr_10 *header; 6817 int datalen; 6818 6819 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6820 6821 datalen = ctl_min(total_len - 2, 65533); 6822 scsi_ulto2b(datalen, header->datalen); 6823 if (dbd) 6824 scsi_ulto2b(0, header->block_descr_len); 6825 else 6826 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6827 header->block_descr_len); 6828 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6829 break; 6830 } 6831 default: 6832 panic("invalid CDB type %#x", ctsio->cdb[0]); 6833 break; /* NOTREACHED */ 6834 } 6835 6836 /* 6837 * If we've got a disk, use its blocksize in the block 6838 * descriptor. Otherwise, just set it to 0. 6839 */ 6840 if (dbd == 0) { 6841 if (control_dev != 0) 6842 scsi_ulto3b(lun->be_lun->blocksize, 6843 block_desc->block_len); 6844 else 6845 scsi_ulto3b(0, block_desc->block_len); 6846 } 6847 6848 switch (page_code) { 6849 case SMS_ALL_PAGES_PAGE: { 6850 int i, data_used; 6851 6852 data_used = header_len; 6853 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6854 struct ctl_page_index *page_index; 6855 6856 page_index = &lun->mode_pages.index[i]; 6857 6858 if ((control_dev != 0) 6859 && (page_index->page_flags & 6860 CTL_PAGE_FLAG_DISK_ONLY)) 6861 continue; 6862 6863 /* 6864 * We don't use this subpage if the user didn't 6865 * request all subpages. We already checked (above) 6866 * to make sure the user only specified a subpage 6867 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6868 */ 6869 if ((page_index->subpage != 0) 6870 && (subpage == SMS_SUBPAGE_PAGE_0)) 6871 continue; 6872 6873 /* 6874 * Call the handler, if it exists, to update the 6875 * page to the latest values. 6876 */ 6877 if (page_index->sense_handler != NULL) 6878 page_index->sense_handler(ctsio, page_index,pc); 6879 6880 memcpy(ctsio->kern_data_ptr + data_used, 6881 page_index->page_data + 6882 (page_index->page_len * pc), 6883 page_index->page_len); 6884 data_used += page_index->page_len; 6885 } 6886 break; 6887 } 6888 default: { 6889 int i, data_used; 6890 6891 data_used = header_len; 6892 6893 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6894 struct ctl_page_index *page_index; 6895 6896 page_index = &lun->mode_pages.index[i]; 6897 6898 /* Look for the right page code */ 6899 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6900 continue; 6901 6902 /* Look for the right subpage or the subpage wildcard*/ 6903 if ((page_index->subpage != subpage) 6904 && (subpage != SMS_SUBPAGE_ALL)) 6905 continue; 6906 6907 /* Make sure the page is supported for this dev type */ 6908 if ((control_dev != 0) 6909 && (page_index->page_flags & 6910 CTL_PAGE_FLAG_DISK_ONLY)) 6911 continue; 6912 6913 /* 6914 * Call the handler, if it exists, to update the 6915 * page to the latest values. 6916 */ 6917 if (page_index->sense_handler != NULL) 6918 page_index->sense_handler(ctsio, page_index,pc); 6919 6920 memcpy(ctsio->kern_data_ptr + data_used, 6921 page_index->page_data + 6922 (page_index->page_len * pc), 6923 page_index->page_len); 6924 data_used += page_index->page_len; 6925 } 6926 break; 6927 } 6928 } 6929 6930 ctsio->scsi_status = SCSI_STATUS_OK; 6931 6932 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6933 ctsio->be_move_done = ctl_config_move_done; 6934 ctl_datamove((union ctl_io *)ctsio); 6935 6936 return (CTL_RETVAL_COMPLETE); 6937} 6938 6939int 6940ctl_read_capacity(struct ctl_scsiio *ctsio) 6941{ 6942 struct scsi_read_capacity *cdb; 6943 struct scsi_read_capacity_data *data; 6944 struct ctl_lun *lun; 6945 uint32_t lba; 6946 6947 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6948 6949 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6950 6951 lba = scsi_4btoul(cdb->addr); 6952 if (((cdb->pmi & SRC_PMI) == 0) 6953 && (lba != 0)) { 6954 ctl_set_invalid_field(/*ctsio*/ ctsio, 6955 /*sks_valid*/ 1, 6956 /*command*/ 1, 6957 /*field*/ 2, 6958 /*bit_valid*/ 0, 6959 /*bit*/ 0); 6960 ctl_done((union ctl_io *)ctsio); 6961 return (CTL_RETVAL_COMPLETE); 6962 } 6963 6964 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6965 6966 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6967 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6968 ctsio->residual = 0; 6969 ctsio->kern_data_len = sizeof(*data); 6970 ctsio->kern_total_len = sizeof(*data); 6971 ctsio->kern_data_resid = 0; 6972 ctsio->kern_rel_offset = 0; 6973 ctsio->kern_sg_entries = 0; 6974 6975 /* 6976 * If the maximum LBA is greater than 0xfffffffe, the user must 6977 * issue a SERVICE ACTION IN (16) command, with the read capacity 6978 * serivce action set. 6979 */ 6980 if (lun->be_lun->maxlba > 0xfffffffe) 6981 scsi_ulto4b(0xffffffff, data->addr); 6982 else 6983 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6984 6985 /* 6986 * XXX KDM this may not be 512 bytes... 6987 */ 6988 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6989 6990 ctsio->scsi_status = SCSI_STATUS_OK; 6991 6992 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6993 ctsio->be_move_done = ctl_config_move_done; 6994 ctl_datamove((union ctl_io *)ctsio); 6995 6996 return (CTL_RETVAL_COMPLETE); 6997} 6998 6999int 7000ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7001{ 7002 struct scsi_read_capacity_16 *cdb; 7003 struct scsi_read_capacity_data_long *data; 7004 struct ctl_lun *lun; 7005 uint64_t lba; 7006 uint32_t alloc_len; 7007 7008 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7009 7010 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7011 7012 alloc_len = scsi_4btoul(cdb->alloc_len); 7013 lba = scsi_8btou64(cdb->addr); 7014 7015 if ((cdb->reladr & SRC16_PMI) 7016 && (lba != 0)) { 7017 ctl_set_invalid_field(/*ctsio*/ ctsio, 7018 /*sks_valid*/ 1, 7019 /*command*/ 1, 7020 /*field*/ 2, 7021 /*bit_valid*/ 0, 7022 /*bit*/ 0); 7023 ctl_done((union ctl_io *)ctsio); 7024 return (CTL_RETVAL_COMPLETE); 7025 } 7026 7027 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7028 7029 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7030 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7031 7032 if (sizeof(*data) < alloc_len) { 7033 ctsio->residual = alloc_len - sizeof(*data); 7034 ctsio->kern_data_len = sizeof(*data); 7035 ctsio->kern_total_len = sizeof(*data); 7036 } else { 7037 ctsio->residual = 0; 7038 ctsio->kern_data_len = alloc_len; 7039 ctsio->kern_total_len = alloc_len; 7040 } 7041 ctsio->kern_data_resid = 0; 7042 ctsio->kern_rel_offset = 0; 7043 ctsio->kern_sg_entries = 0; 7044 7045 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7046 /* XXX KDM this may not be 512 bytes... */ 7047 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7048 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7049 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7050 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7051 data->lalba_lbp[0] |= SRC16_LBPME; 7052 7053 ctsio->scsi_status = SCSI_STATUS_OK; 7054 7055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7056 ctsio->be_move_done = ctl_config_move_done; 7057 ctl_datamove((union ctl_io *)ctsio); 7058 7059 return (CTL_RETVAL_COMPLETE); 7060} 7061 7062int 7063ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7064{ 7065 struct scsi_maintenance_in *cdb; 7066 int retval; 7067 int alloc_len, total_len = 0; 7068 int num_target_port_groups, single; 7069 struct ctl_lun *lun; 7070 struct ctl_softc *softc; 7071 struct scsi_target_group_data *rtg_ptr; 7072 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2; 7073 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2, 7074 *tp_desc_ptr2_1, *tp_desc_ptr2_2; 7075 7076 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7077 7078 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7079 softc = control_softc; 7080 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7081 7082 retval = CTL_RETVAL_COMPLETE; 7083 7084 single = ctl_is_single; 7085 if (single) 7086 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1; 7087 else 7088 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7089 7090 total_len = sizeof(struct scsi_target_group_data) + 7091 sizeof(struct scsi_target_port_group_descriptor) * 7092 num_target_port_groups + 7093 sizeof(struct scsi_target_port_descriptor) * 7094 NUM_PORTS_PER_GRP * num_target_port_groups; 7095 7096 alloc_len = scsi_4btoul(cdb->length); 7097 7098 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7099 7100 ctsio->kern_sg_entries = 0; 7101 7102 if (total_len < alloc_len) { 7103 ctsio->residual = alloc_len - total_len; 7104 ctsio->kern_data_len = total_len; 7105 ctsio->kern_total_len = total_len; 7106 } else { 7107 ctsio->residual = 0; 7108 ctsio->kern_data_len = alloc_len; 7109 ctsio->kern_total_len = alloc_len; 7110 } 7111 ctsio->kern_data_resid = 0; 7112 ctsio->kern_rel_offset = 0; 7113 7114 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr; 7115 7116 tpg_desc_ptr1 = &rtg_ptr->groups[0]; 7117 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0]; 7118 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *) 7119 &tp_desc_ptr1_1->desc_list[0]; 7120 7121 if (single == 0) { 7122 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *) 7123 &tp_desc_ptr1_2->desc_list[0]; 7124 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0]; 7125 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *) 7126 &tp_desc_ptr2_1->desc_list[0]; 7127 } else { 7128 tpg_desc_ptr2 = NULL; 7129 tp_desc_ptr2_1 = NULL; 7130 tp_desc_ptr2_2 = NULL; 7131 } 7132 7133 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7134 if (single == 0) { 7135 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7136 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7137 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7138 tpg_desc_ptr2->pref_state = 7139 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7140 } else { 7141 tpg_desc_ptr1->pref_state = 7142 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7143 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7144 } 7145 } else { 7146 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7147 tpg_desc_ptr1->pref_state = 7148 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7149 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7150 } else { 7151 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7152 tpg_desc_ptr2->pref_state = 7153 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7154 } 7155 } 7156 } else { 7157 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7158 } 7159 tpg_desc_ptr1->support = 0; 7160 tpg_desc_ptr1->target_port_group[1] = 1; 7161 tpg_desc_ptr1->status = TPG_IMPLICIT; 7162 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP; 7163 7164 if (single == 0) { 7165 tpg_desc_ptr2->support = 0; 7166 tpg_desc_ptr2->target_port_group[1] = 2; 7167 tpg_desc_ptr2->status = TPG_IMPLICIT; 7168 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP; 7169 7170 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7171 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7172 7173 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9; 7174 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10; 7175 } else { 7176 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7177 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7178 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7179 } else { 7180 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9; 7181 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10; 7182 } 7183 } 7184 7185 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7186 ctsio->be_move_done = ctl_config_move_done; 7187 7188 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7189 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7190 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7191 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7192 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7193 7194 ctl_datamove((union ctl_io *)ctsio); 7195 return(retval); 7196} 7197 7198int 7199ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7200{ 7201 struct ctl_lun *lun; 7202 struct scsi_report_supported_opcodes *cdb; 7203 const struct ctl_cmd_entry *entry, *sentry; 7204 struct scsi_report_supported_opcodes_all *all; 7205 struct scsi_report_supported_opcodes_descr *descr; 7206 struct scsi_report_supported_opcodes_one *one; 7207 int retval; 7208 int alloc_len, total_len; 7209 int opcode, service_action, i, j, num; 7210 7211 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7212 7213 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7214 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7215 7216 retval = CTL_RETVAL_COMPLETE; 7217 7218 opcode = cdb->requested_opcode; 7219 service_action = scsi_2btoul(cdb->requested_service_action); 7220 switch (cdb->options & RSO_OPTIONS_MASK) { 7221 case RSO_OPTIONS_ALL: 7222 num = 0; 7223 for (i = 0; i < 256; i++) { 7224 entry = &ctl_cmd_table[i]; 7225 if (entry->flags & CTL_CMD_FLAG_SA5) { 7226 for (j = 0; j < 32; j++) { 7227 sentry = &((const struct ctl_cmd_entry *) 7228 entry->execute)[j]; 7229 if (ctl_cmd_applicable( 7230 lun->be_lun->lun_type, sentry)) 7231 num++; 7232 } 7233 } else { 7234 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7235 entry)) 7236 num++; 7237 } 7238 } 7239 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7240 num * sizeof(struct scsi_report_supported_opcodes_descr); 7241 break; 7242 case RSO_OPTIONS_OC: 7243 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7244 ctl_set_invalid_field(/*ctsio*/ ctsio, 7245 /*sks_valid*/ 1, 7246 /*command*/ 1, 7247 /*field*/ 2, 7248 /*bit_valid*/ 1, 7249 /*bit*/ 2); 7250 ctl_done((union ctl_io *)ctsio); 7251 return (CTL_RETVAL_COMPLETE); 7252 } 7253 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7254 break; 7255 case RSO_OPTIONS_OC_SA: 7256 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7257 service_action >= 32) { 7258 ctl_set_invalid_field(/*ctsio*/ ctsio, 7259 /*sks_valid*/ 1, 7260 /*command*/ 1, 7261 /*field*/ 2, 7262 /*bit_valid*/ 1, 7263 /*bit*/ 2); 7264 ctl_done((union ctl_io *)ctsio); 7265 return (CTL_RETVAL_COMPLETE); 7266 } 7267 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7268 break; 7269 default: 7270 ctl_set_invalid_field(/*ctsio*/ ctsio, 7271 /*sks_valid*/ 1, 7272 /*command*/ 1, 7273 /*field*/ 2, 7274 /*bit_valid*/ 1, 7275 /*bit*/ 2); 7276 ctl_done((union ctl_io *)ctsio); 7277 return (CTL_RETVAL_COMPLETE); 7278 } 7279 7280 alloc_len = scsi_4btoul(cdb->length); 7281 7282 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7283 7284 ctsio->kern_sg_entries = 0; 7285 7286 if (total_len < alloc_len) { 7287 ctsio->residual = alloc_len - total_len; 7288 ctsio->kern_data_len = total_len; 7289 ctsio->kern_total_len = total_len; 7290 } else { 7291 ctsio->residual = 0; 7292 ctsio->kern_data_len = alloc_len; 7293 ctsio->kern_total_len = alloc_len; 7294 } 7295 ctsio->kern_data_resid = 0; 7296 ctsio->kern_rel_offset = 0; 7297 7298 switch (cdb->options & RSO_OPTIONS_MASK) { 7299 case RSO_OPTIONS_ALL: 7300 all = (struct scsi_report_supported_opcodes_all *) 7301 ctsio->kern_data_ptr; 7302 num = 0; 7303 for (i = 0; i < 256; i++) { 7304 entry = &ctl_cmd_table[i]; 7305 if (entry->flags & CTL_CMD_FLAG_SA5) { 7306 for (j = 0; j < 32; j++) { 7307 sentry = &((const struct ctl_cmd_entry *) 7308 entry->execute)[j]; 7309 if (!ctl_cmd_applicable( 7310 lun->be_lun->lun_type, sentry)) 7311 continue; 7312 descr = &all->descr[num++]; 7313 descr->opcode = i; 7314 scsi_ulto2b(j, descr->service_action); 7315 descr->flags = RSO_SERVACTV; 7316 scsi_ulto2b(sentry->length, 7317 descr->cdb_length); 7318 } 7319 } else { 7320 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7321 entry)) 7322 continue; 7323 descr = &all->descr[num++]; 7324 descr->opcode = i; 7325 scsi_ulto2b(0, descr->service_action); 7326 descr->flags = 0; 7327 scsi_ulto2b(entry->length, descr->cdb_length); 7328 } 7329 } 7330 scsi_ulto4b( 7331 num * sizeof(struct scsi_report_supported_opcodes_descr), 7332 all->length); 7333 break; 7334 case RSO_OPTIONS_OC: 7335 one = (struct scsi_report_supported_opcodes_one *) 7336 ctsio->kern_data_ptr; 7337 entry = &ctl_cmd_table[opcode]; 7338 goto fill_one; 7339 case RSO_OPTIONS_OC_SA: 7340 one = (struct scsi_report_supported_opcodes_one *) 7341 ctsio->kern_data_ptr; 7342 entry = &ctl_cmd_table[opcode]; 7343 entry = &((const struct ctl_cmd_entry *) 7344 entry->execute)[service_action]; 7345fill_one: 7346 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7347 one->support = 3; 7348 scsi_ulto2b(entry->length, one->cdb_length); 7349 one->cdb_usage[0] = opcode; 7350 memcpy(&one->cdb_usage[1], entry->usage, 7351 entry->length - 1); 7352 } else 7353 one->support = 1; 7354 break; 7355 } 7356 7357 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7358 ctsio->be_move_done = ctl_config_move_done; 7359 7360 ctl_datamove((union ctl_io *)ctsio); 7361 return(retval); 7362} 7363 7364int 7365ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7366{ 7367 struct ctl_lun *lun; 7368 struct scsi_report_supported_tmf *cdb; 7369 struct scsi_report_supported_tmf_data *data; 7370 int retval; 7371 int alloc_len, total_len; 7372 7373 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7374 7375 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7376 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7377 7378 retval = CTL_RETVAL_COMPLETE; 7379 7380 total_len = sizeof(struct scsi_report_supported_tmf_data); 7381 alloc_len = scsi_4btoul(cdb->length); 7382 7383 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7384 7385 ctsio->kern_sg_entries = 0; 7386 7387 if (total_len < alloc_len) { 7388 ctsio->residual = alloc_len - total_len; 7389 ctsio->kern_data_len = total_len; 7390 ctsio->kern_total_len = total_len; 7391 } else { 7392 ctsio->residual = 0; 7393 ctsio->kern_data_len = alloc_len; 7394 ctsio->kern_total_len = alloc_len; 7395 } 7396 ctsio->kern_data_resid = 0; 7397 ctsio->kern_rel_offset = 0; 7398 7399 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7400 data->byte1 |= RST_ATS | RST_LURS | RST_TRS; 7401 7402 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7403 ctsio->be_move_done = ctl_config_move_done; 7404 7405 ctl_datamove((union ctl_io *)ctsio); 7406 return (retval); 7407} 7408 7409int 7410ctl_report_timestamp(struct ctl_scsiio *ctsio) 7411{ 7412 struct ctl_lun *lun; 7413 struct scsi_report_timestamp *cdb; 7414 struct scsi_report_timestamp_data *data; 7415 struct timeval tv; 7416 int64_t timestamp; 7417 int retval; 7418 int alloc_len, total_len; 7419 7420 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7421 7422 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7423 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7424 7425 retval = CTL_RETVAL_COMPLETE; 7426 7427 total_len = sizeof(struct scsi_report_timestamp_data); 7428 alloc_len = scsi_4btoul(cdb->length); 7429 7430 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7431 7432 ctsio->kern_sg_entries = 0; 7433 7434 if (total_len < alloc_len) { 7435 ctsio->residual = alloc_len - total_len; 7436 ctsio->kern_data_len = total_len; 7437 ctsio->kern_total_len = total_len; 7438 } else { 7439 ctsio->residual = 0; 7440 ctsio->kern_data_len = alloc_len; 7441 ctsio->kern_total_len = alloc_len; 7442 } 7443 ctsio->kern_data_resid = 0; 7444 ctsio->kern_rel_offset = 0; 7445 7446 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7447 scsi_ulto2b(sizeof(*data) - 2, data->length); 7448 data->origin = RTS_ORIG_OUTSIDE; 7449 getmicrotime(&tv); 7450 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7451 scsi_ulto4b(timestamp >> 16, data->timestamp); 7452 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7453 7454 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7455 ctsio->be_move_done = ctl_config_move_done; 7456 7457 ctl_datamove((union ctl_io *)ctsio); 7458 return (retval); 7459} 7460 7461int 7462ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7463{ 7464 struct scsi_per_res_in *cdb; 7465 int alloc_len, total_len = 0; 7466 /* struct scsi_per_res_in_rsrv in_data; */ 7467 struct ctl_lun *lun; 7468 struct ctl_softc *softc; 7469 7470 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7471 7472 softc = control_softc; 7473 7474 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7475 7476 alloc_len = scsi_2btoul(cdb->length); 7477 7478 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7479 7480retry: 7481 mtx_lock(&lun->lun_lock); 7482 switch (cdb->action) { 7483 case SPRI_RK: /* read keys */ 7484 total_len = sizeof(struct scsi_per_res_in_keys) + 7485 lun->pr_key_count * 7486 sizeof(struct scsi_per_res_key); 7487 break; 7488 case SPRI_RR: /* read reservation */ 7489 if (lun->flags & CTL_LUN_PR_RESERVED) 7490 total_len = sizeof(struct scsi_per_res_in_rsrv); 7491 else 7492 total_len = sizeof(struct scsi_per_res_in_header); 7493 break; 7494 case SPRI_RC: /* report capabilities */ 7495 total_len = sizeof(struct scsi_per_res_cap); 7496 break; 7497 default: 7498 panic("Invalid PR type %x", cdb->action); 7499 } 7500 mtx_unlock(&lun->lun_lock); 7501 7502 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7503 7504 if (total_len < alloc_len) { 7505 ctsio->residual = alloc_len - total_len; 7506 ctsio->kern_data_len = total_len; 7507 ctsio->kern_total_len = total_len; 7508 } else { 7509 ctsio->residual = 0; 7510 ctsio->kern_data_len = alloc_len; 7511 ctsio->kern_total_len = alloc_len; 7512 } 7513 7514 ctsio->kern_data_resid = 0; 7515 ctsio->kern_rel_offset = 0; 7516 ctsio->kern_sg_entries = 0; 7517 7518 mtx_lock(&lun->lun_lock); 7519 switch (cdb->action) { 7520 case SPRI_RK: { // read keys 7521 struct scsi_per_res_in_keys *res_keys; 7522 int i, key_count; 7523 7524 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7525 7526 /* 7527 * We had to drop the lock to allocate our buffer, which 7528 * leaves time for someone to come in with another 7529 * persistent reservation. (That is unlikely, though, 7530 * since this should be the only persistent reservation 7531 * command active right now.) 7532 */ 7533 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7534 (lun->pr_key_count * 7535 sizeof(struct scsi_per_res_key)))){ 7536 mtx_unlock(&lun->lun_lock); 7537 free(ctsio->kern_data_ptr, M_CTL); 7538 printf("%s: reservation length changed, retrying\n", 7539 __func__); 7540 goto retry; 7541 } 7542 7543 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7544 7545 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7546 lun->pr_key_count, res_keys->header.length); 7547 7548 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7549 if (!lun->per_res[i].registered) 7550 continue; 7551 7552 /* 7553 * We used lun->pr_key_count to calculate the 7554 * size to allocate. If it turns out the number of 7555 * initiators with the registered flag set is 7556 * larger than that (i.e. they haven't been kept in 7557 * sync), we've got a problem. 7558 */ 7559 if (key_count >= lun->pr_key_count) { 7560#ifdef NEEDTOPORT 7561 csevent_log(CSC_CTL | CSC_SHELF_SW | 7562 CTL_PR_ERROR, 7563 csevent_LogType_Fault, 7564 csevent_AlertLevel_Yellow, 7565 csevent_FRU_ShelfController, 7566 csevent_FRU_Firmware, 7567 csevent_FRU_Unknown, 7568 "registered keys %d >= key " 7569 "count %d", key_count, 7570 lun->pr_key_count); 7571#endif 7572 key_count++; 7573 continue; 7574 } 7575 memcpy(res_keys->keys[key_count].key, 7576 lun->per_res[i].res_key.key, 7577 ctl_min(sizeof(res_keys->keys[key_count].key), 7578 sizeof(lun->per_res[i].res_key))); 7579 key_count++; 7580 } 7581 break; 7582 } 7583 case SPRI_RR: { // read reservation 7584 struct scsi_per_res_in_rsrv *res; 7585 int tmp_len, header_only; 7586 7587 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7588 7589 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7590 7591 if (lun->flags & CTL_LUN_PR_RESERVED) 7592 { 7593 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7594 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7595 res->header.length); 7596 header_only = 0; 7597 } else { 7598 tmp_len = sizeof(struct scsi_per_res_in_header); 7599 scsi_ulto4b(0, res->header.length); 7600 header_only = 1; 7601 } 7602 7603 /* 7604 * We had to drop the lock to allocate our buffer, which 7605 * leaves time for someone to come in with another 7606 * persistent reservation. (That is unlikely, though, 7607 * since this should be the only persistent reservation 7608 * command active right now.) 7609 */ 7610 if (tmp_len != total_len) { 7611 mtx_unlock(&lun->lun_lock); 7612 free(ctsio->kern_data_ptr, M_CTL); 7613 printf("%s: reservation status changed, retrying\n", 7614 __func__); 7615 goto retry; 7616 } 7617 7618 /* 7619 * No reservation held, so we're done. 7620 */ 7621 if (header_only != 0) 7622 break; 7623 7624 /* 7625 * If the registration is an All Registrants type, the key 7626 * is 0, since it doesn't really matter. 7627 */ 7628 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7629 memcpy(res->data.reservation, 7630 &lun->per_res[lun->pr_res_idx].res_key, 7631 sizeof(struct scsi_per_res_key)); 7632 } 7633 res->data.scopetype = lun->res_type; 7634 break; 7635 } 7636 case SPRI_RC: //report capabilities 7637 { 7638 struct scsi_per_res_cap *res_cap; 7639 uint16_t type_mask; 7640 7641 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7642 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7643 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7644 type_mask = SPRI_TM_WR_EX_AR | 7645 SPRI_TM_EX_AC_RO | 7646 SPRI_TM_WR_EX_RO | 7647 SPRI_TM_EX_AC | 7648 SPRI_TM_WR_EX | 7649 SPRI_TM_EX_AC_AR; 7650 scsi_ulto2b(type_mask, res_cap->type_mask); 7651 break; 7652 } 7653 case SPRI_RS: //read full status 7654 default: 7655 /* 7656 * This is a bug, because we just checked for this above, 7657 * and should have returned an error. 7658 */ 7659 panic("Invalid PR type %x", cdb->action); 7660 break; /* NOTREACHED */ 7661 } 7662 mtx_unlock(&lun->lun_lock); 7663 7664 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7665 ctsio->be_move_done = ctl_config_move_done; 7666 7667 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7668 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7669 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7670 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7671 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7672 7673 ctl_datamove((union ctl_io *)ctsio); 7674 7675 return (CTL_RETVAL_COMPLETE); 7676} 7677 7678/* 7679 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7680 * it should return. 7681 */ 7682static int 7683ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7684 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7685 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7686 struct scsi_per_res_out_parms* param) 7687{ 7688 union ctl_ha_msg persis_io; 7689 int retval, i; 7690 int isc_retval; 7691 7692 retval = 0; 7693 7694 mtx_lock(&lun->lun_lock); 7695 if (sa_res_key == 0) { 7696 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7697 /* validate scope and type */ 7698 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7699 SPR_LU_SCOPE) { 7700 mtx_unlock(&lun->lun_lock); 7701 ctl_set_invalid_field(/*ctsio*/ ctsio, 7702 /*sks_valid*/ 1, 7703 /*command*/ 1, 7704 /*field*/ 2, 7705 /*bit_valid*/ 1, 7706 /*bit*/ 4); 7707 ctl_done((union ctl_io *)ctsio); 7708 return (1); 7709 } 7710 7711 if (type>8 || type==2 || type==4 || type==0) { 7712 mtx_unlock(&lun->lun_lock); 7713 ctl_set_invalid_field(/*ctsio*/ ctsio, 7714 /*sks_valid*/ 1, 7715 /*command*/ 1, 7716 /*field*/ 2, 7717 /*bit_valid*/ 1, 7718 /*bit*/ 0); 7719 ctl_done((union ctl_io *)ctsio); 7720 return (1); 7721 } 7722 7723 /* temporarily unregister this nexus */ 7724 lun->per_res[residx].registered = 0; 7725 7726 /* 7727 * Unregister everybody else and build UA for 7728 * them 7729 */ 7730 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7731 if (lun->per_res[i].registered == 0) 7732 continue; 7733 7734 if (!persis_offset 7735 && i <CTL_MAX_INITIATORS) 7736 lun->pending_sense[i].ua_pending |= 7737 CTL_UA_REG_PREEMPT; 7738 else if (persis_offset 7739 && i >= persis_offset) 7740 lun->pending_sense[i-persis_offset 7741 ].ua_pending |= 7742 CTL_UA_REG_PREEMPT; 7743 lun->per_res[i].registered = 0; 7744 memset(&lun->per_res[i].res_key, 0, 7745 sizeof(struct scsi_per_res_key)); 7746 } 7747 lun->per_res[residx].registered = 1; 7748 lun->pr_key_count = 1; 7749 lun->res_type = type; 7750 if (lun->res_type != SPR_TYPE_WR_EX_AR 7751 && lun->res_type != SPR_TYPE_EX_AC_AR) 7752 lun->pr_res_idx = residx; 7753 7754 /* send msg to other side */ 7755 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7756 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7757 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7758 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7759 persis_io.pr.pr_info.res_type = type; 7760 memcpy(persis_io.pr.pr_info.sa_res_key, 7761 param->serv_act_res_key, 7762 sizeof(param->serv_act_res_key)); 7763 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7764 &persis_io, sizeof(persis_io), 0)) > 7765 CTL_HA_STATUS_SUCCESS) { 7766 printf("CTL:Persis Out error returned " 7767 "from ctl_ha_msg_send %d\n", 7768 isc_retval); 7769 } 7770 } else { 7771 /* not all registrants */ 7772 mtx_unlock(&lun->lun_lock); 7773 free(ctsio->kern_data_ptr, M_CTL); 7774 ctl_set_invalid_field(ctsio, 7775 /*sks_valid*/ 1, 7776 /*command*/ 0, 7777 /*field*/ 8, 7778 /*bit_valid*/ 0, 7779 /*bit*/ 0); 7780 ctl_done((union ctl_io *)ctsio); 7781 return (1); 7782 } 7783 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7784 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7785 int found = 0; 7786 7787 if (res_key == sa_res_key) { 7788 /* special case */ 7789 /* 7790 * The spec implies this is not good but doesn't 7791 * say what to do. There are two choices either 7792 * generate a res conflict or check condition 7793 * with illegal field in parameter data. Since 7794 * that is what is done when the sa_res_key is 7795 * zero I'll take that approach since this has 7796 * to do with the sa_res_key. 7797 */ 7798 mtx_unlock(&lun->lun_lock); 7799 free(ctsio->kern_data_ptr, M_CTL); 7800 ctl_set_invalid_field(ctsio, 7801 /*sks_valid*/ 1, 7802 /*command*/ 0, 7803 /*field*/ 8, 7804 /*bit_valid*/ 0, 7805 /*bit*/ 0); 7806 ctl_done((union ctl_io *)ctsio); 7807 return (1); 7808 } 7809 7810 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7811 if (lun->per_res[i].registered 7812 && memcmp(param->serv_act_res_key, 7813 lun->per_res[i].res_key.key, 7814 sizeof(struct scsi_per_res_key)) != 0) 7815 continue; 7816 7817 found = 1; 7818 lun->per_res[i].registered = 0; 7819 memset(&lun->per_res[i].res_key, 0, 7820 sizeof(struct scsi_per_res_key)); 7821 lun->pr_key_count--; 7822 7823 if (!persis_offset 7824 && i < CTL_MAX_INITIATORS) 7825 lun->pending_sense[i].ua_pending |= 7826 CTL_UA_REG_PREEMPT; 7827 else if (persis_offset 7828 && i >= persis_offset) 7829 lun->pending_sense[i-persis_offset].ua_pending|= 7830 CTL_UA_REG_PREEMPT; 7831 } 7832 if (!found) { 7833 mtx_unlock(&lun->lun_lock); 7834 free(ctsio->kern_data_ptr, M_CTL); 7835 ctl_set_reservation_conflict(ctsio); 7836 ctl_done((union ctl_io *)ctsio); 7837 return (CTL_RETVAL_COMPLETE); 7838 } 7839 /* send msg to other side */ 7840 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7841 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7842 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7843 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7844 persis_io.pr.pr_info.res_type = type; 7845 memcpy(persis_io.pr.pr_info.sa_res_key, 7846 param->serv_act_res_key, 7847 sizeof(param->serv_act_res_key)); 7848 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7849 &persis_io, sizeof(persis_io), 0)) > 7850 CTL_HA_STATUS_SUCCESS) { 7851 printf("CTL:Persis Out error returned from " 7852 "ctl_ha_msg_send %d\n", isc_retval); 7853 } 7854 } else { 7855 /* Reserved but not all registrants */ 7856 /* sa_res_key is res holder */ 7857 if (memcmp(param->serv_act_res_key, 7858 lun->per_res[lun->pr_res_idx].res_key.key, 7859 sizeof(struct scsi_per_res_key)) == 0) { 7860 /* validate scope and type */ 7861 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7862 SPR_LU_SCOPE) { 7863 mtx_unlock(&lun->lun_lock); 7864 ctl_set_invalid_field(/*ctsio*/ ctsio, 7865 /*sks_valid*/ 1, 7866 /*command*/ 1, 7867 /*field*/ 2, 7868 /*bit_valid*/ 1, 7869 /*bit*/ 4); 7870 ctl_done((union ctl_io *)ctsio); 7871 return (1); 7872 } 7873 7874 if (type>8 || type==2 || type==4 || type==0) { 7875 mtx_unlock(&lun->lun_lock); 7876 ctl_set_invalid_field(/*ctsio*/ ctsio, 7877 /*sks_valid*/ 1, 7878 /*command*/ 1, 7879 /*field*/ 2, 7880 /*bit_valid*/ 1, 7881 /*bit*/ 0); 7882 ctl_done((union ctl_io *)ctsio); 7883 return (1); 7884 } 7885 7886 /* 7887 * Do the following: 7888 * if sa_res_key != res_key remove all 7889 * registrants w/sa_res_key and generate UA 7890 * for these registrants(Registrations 7891 * Preempted) if it wasn't an exclusive 7892 * reservation generate UA(Reservations 7893 * Preempted) for all other registered nexuses 7894 * if the type has changed. Establish the new 7895 * reservation and holder. If res_key and 7896 * sa_res_key are the same do the above 7897 * except don't unregister the res holder. 7898 */ 7899 7900 /* 7901 * Temporarily unregister so it won't get 7902 * removed or UA generated 7903 */ 7904 lun->per_res[residx].registered = 0; 7905 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7906 if (lun->per_res[i].registered == 0) 7907 continue; 7908 7909 if (memcmp(param->serv_act_res_key, 7910 lun->per_res[i].res_key.key, 7911 sizeof(struct scsi_per_res_key)) == 0) { 7912 lun->per_res[i].registered = 0; 7913 memset(&lun->per_res[i].res_key, 7914 0, 7915 sizeof(struct scsi_per_res_key)); 7916 lun->pr_key_count--; 7917 7918 if (!persis_offset 7919 && i < CTL_MAX_INITIATORS) 7920 lun->pending_sense[i 7921 ].ua_pending |= 7922 CTL_UA_REG_PREEMPT; 7923 else if (persis_offset 7924 && i >= persis_offset) 7925 lun->pending_sense[ 7926 i-persis_offset].ua_pending |= 7927 CTL_UA_REG_PREEMPT; 7928 } else if (type != lun->res_type 7929 && (lun->res_type == SPR_TYPE_WR_EX_RO 7930 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7931 if (!persis_offset 7932 && i < CTL_MAX_INITIATORS) 7933 lun->pending_sense[i 7934 ].ua_pending |= 7935 CTL_UA_RES_RELEASE; 7936 else if (persis_offset 7937 && i >= persis_offset) 7938 lun->pending_sense[ 7939 i-persis_offset 7940 ].ua_pending |= 7941 CTL_UA_RES_RELEASE; 7942 } 7943 } 7944 lun->per_res[residx].registered = 1; 7945 lun->res_type = type; 7946 if (lun->res_type != SPR_TYPE_WR_EX_AR 7947 && lun->res_type != SPR_TYPE_EX_AC_AR) 7948 lun->pr_res_idx = residx; 7949 else 7950 lun->pr_res_idx = 7951 CTL_PR_ALL_REGISTRANTS; 7952 7953 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7954 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7955 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7956 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7957 persis_io.pr.pr_info.res_type = type; 7958 memcpy(persis_io.pr.pr_info.sa_res_key, 7959 param->serv_act_res_key, 7960 sizeof(param->serv_act_res_key)); 7961 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7962 &persis_io, sizeof(persis_io), 0)) > 7963 CTL_HA_STATUS_SUCCESS) { 7964 printf("CTL:Persis Out error returned " 7965 "from ctl_ha_msg_send %d\n", 7966 isc_retval); 7967 } 7968 } else { 7969 /* 7970 * sa_res_key is not the res holder just 7971 * remove registrants 7972 */ 7973 int found=0; 7974 7975 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7976 if (memcmp(param->serv_act_res_key, 7977 lun->per_res[i].res_key.key, 7978 sizeof(struct scsi_per_res_key)) != 0) 7979 continue; 7980 7981 found = 1; 7982 lun->per_res[i].registered = 0; 7983 memset(&lun->per_res[i].res_key, 0, 7984 sizeof(struct scsi_per_res_key)); 7985 lun->pr_key_count--; 7986 7987 if (!persis_offset 7988 && i < CTL_MAX_INITIATORS) 7989 lun->pending_sense[i].ua_pending |= 7990 CTL_UA_REG_PREEMPT; 7991 else if (persis_offset 7992 && i >= persis_offset) 7993 lun->pending_sense[ 7994 i-persis_offset].ua_pending |= 7995 CTL_UA_REG_PREEMPT; 7996 } 7997 7998 if (!found) { 7999 mtx_unlock(&lun->lun_lock); 8000 free(ctsio->kern_data_ptr, M_CTL); 8001 ctl_set_reservation_conflict(ctsio); 8002 ctl_done((union ctl_io *)ctsio); 8003 return (1); 8004 } 8005 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8006 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8007 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8008 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8009 persis_io.pr.pr_info.res_type = type; 8010 memcpy(persis_io.pr.pr_info.sa_res_key, 8011 param->serv_act_res_key, 8012 sizeof(param->serv_act_res_key)); 8013 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8014 &persis_io, sizeof(persis_io), 0)) > 8015 CTL_HA_STATUS_SUCCESS) { 8016 printf("CTL:Persis Out error returned " 8017 "from ctl_ha_msg_send %d\n", 8018 isc_retval); 8019 } 8020 } 8021 } 8022 8023 lun->PRGeneration++; 8024 mtx_unlock(&lun->lun_lock); 8025 8026 return (retval); 8027} 8028 8029static void 8030ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8031{ 8032 int i; 8033 8034 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8035 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8036 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 8037 msg->pr.pr_info.sa_res_key, 8038 sizeof(struct scsi_per_res_key)) != 0) { 8039 uint64_t sa_res_key; 8040 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8041 8042 if (sa_res_key == 0) { 8043 /* temporarily unregister this nexus */ 8044 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8045 8046 /* 8047 * Unregister everybody else and build UA for 8048 * them 8049 */ 8050 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8051 if (lun->per_res[i].registered == 0) 8052 continue; 8053 8054 if (!persis_offset 8055 && i < CTL_MAX_INITIATORS) 8056 lun->pending_sense[i].ua_pending |= 8057 CTL_UA_REG_PREEMPT; 8058 else if (persis_offset && i >= persis_offset) 8059 lun->pending_sense[i - 8060 persis_offset].ua_pending |= 8061 CTL_UA_REG_PREEMPT; 8062 lun->per_res[i].registered = 0; 8063 memset(&lun->per_res[i].res_key, 0, 8064 sizeof(struct scsi_per_res_key)); 8065 } 8066 8067 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8068 lun->pr_key_count = 1; 8069 lun->res_type = msg->pr.pr_info.res_type; 8070 if (lun->res_type != SPR_TYPE_WR_EX_AR 8071 && lun->res_type != SPR_TYPE_EX_AC_AR) 8072 lun->pr_res_idx = msg->pr.pr_info.residx; 8073 } else { 8074 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8075 if (memcmp(msg->pr.pr_info.sa_res_key, 8076 lun->per_res[i].res_key.key, 8077 sizeof(struct scsi_per_res_key)) != 0) 8078 continue; 8079 8080 lun->per_res[i].registered = 0; 8081 memset(&lun->per_res[i].res_key, 0, 8082 sizeof(struct scsi_per_res_key)); 8083 lun->pr_key_count--; 8084 8085 if (!persis_offset 8086 && i < persis_offset) 8087 lun->pending_sense[i].ua_pending |= 8088 CTL_UA_REG_PREEMPT; 8089 else if (persis_offset 8090 && i >= persis_offset) 8091 lun->pending_sense[i - 8092 persis_offset].ua_pending |= 8093 CTL_UA_REG_PREEMPT; 8094 } 8095 } 8096 } else { 8097 /* 8098 * Temporarily unregister so it won't get removed 8099 * or UA generated 8100 */ 8101 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8102 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8103 if (lun->per_res[i].registered == 0) 8104 continue; 8105 8106 if (memcmp(msg->pr.pr_info.sa_res_key, 8107 lun->per_res[i].res_key.key, 8108 sizeof(struct scsi_per_res_key)) == 0) { 8109 lun->per_res[i].registered = 0; 8110 memset(&lun->per_res[i].res_key, 0, 8111 sizeof(struct scsi_per_res_key)); 8112 lun->pr_key_count--; 8113 if (!persis_offset 8114 && i < CTL_MAX_INITIATORS) 8115 lun->pending_sense[i].ua_pending |= 8116 CTL_UA_REG_PREEMPT; 8117 else if (persis_offset 8118 && i >= persis_offset) 8119 lun->pending_sense[i - 8120 persis_offset].ua_pending |= 8121 CTL_UA_REG_PREEMPT; 8122 } else if (msg->pr.pr_info.res_type != lun->res_type 8123 && (lun->res_type == SPR_TYPE_WR_EX_RO 8124 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8125 if (!persis_offset 8126 && i < persis_offset) 8127 lun->pending_sense[i 8128 ].ua_pending |= 8129 CTL_UA_RES_RELEASE; 8130 else if (persis_offset 8131 && i >= persis_offset) 8132 lun->pending_sense[i - 8133 persis_offset].ua_pending |= 8134 CTL_UA_RES_RELEASE; 8135 } 8136 } 8137 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8138 lun->res_type = msg->pr.pr_info.res_type; 8139 if (lun->res_type != SPR_TYPE_WR_EX_AR 8140 && lun->res_type != SPR_TYPE_EX_AC_AR) 8141 lun->pr_res_idx = msg->pr.pr_info.residx; 8142 else 8143 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8144 } 8145 lun->PRGeneration++; 8146 8147} 8148 8149 8150int 8151ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8152{ 8153 int retval; 8154 int isc_retval; 8155 u_int32_t param_len; 8156 struct scsi_per_res_out *cdb; 8157 struct ctl_lun *lun; 8158 struct scsi_per_res_out_parms* param; 8159 struct ctl_softc *softc; 8160 uint32_t residx; 8161 uint64_t res_key, sa_res_key; 8162 uint8_t type; 8163 union ctl_ha_msg persis_io; 8164 int i; 8165 8166 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8167 8168 retval = CTL_RETVAL_COMPLETE; 8169 8170 softc = control_softc; 8171 8172 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8173 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8174 8175 /* 8176 * We only support whole-LUN scope. The scope & type are ignored for 8177 * register, register and ignore existing key and clear. 8178 * We sometimes ignore scope and type on preempts too!! 8179 * Verify reservation type here as well. 8180 */ 8181 type = cdb->scope_type & SPR_TYPE_MASK; 8182 if ((cdb->action == SPRO_RESERVE) 8183 || (cdb->action == SPRO_RELEASE)) { 8184 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8185 ctl_set_invalid_field(/*ctsio*/ ctsio, 8186 /*sks_valid*/ 1, 8187 /*command*/ 1, 8188 /*field*/ 2, 8189 /*bit_valid*/ 1, 8190 /*bit*/ 4); 8191 ctl_done((union ctl_io *)ctsio); 8192 return (CTL_RETVAL_COMPLETE); 8193 } 8194 8195 if (type>8 || type==2 || type==4 || type==0) { 8196 ctl_set_invalid_field(/*ctsio*/ ctsio, 8197 /*sks_valid*/ 1, 8198 /*command*/ 1, 8199 /*field*/ 2, 8200 /*bit_valid*/ 1, 8201 /*bit*/ 0); 8202 ctl_done((union ctl_io *)ctsio); 8203 return (CTL_RETVAL_COMPLETE); 8204 } 8205 } 8206 8207 param_len = scsi_4btoul(cdb->length); 8208 8209 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8210 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8211 ctsio->kern_data_len = param_len; 8212 ctsio->kern_total_len = param_len; 8213 ctsio->kern_data_resid = 0; 8214 ctsio->kern_rel_offset = 0; 8215 ctsio->kern_sg_entries = 0; 8216 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8217 ctsio->be_move_done = ctl_config_move_done; 8218 ctl_datamove((union ctl_io *)ctsio); 8219 8220 return (CTL_RETVAL_COMPLETE); 8221 } 8222 8223 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8224 8225 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8226 res_key = scsi_8btou64(param->res_key.key); 8227 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8228 8229 /* 8230 * Validate the reservation key here except for SPRO_REG_IGNO 8231 * This must be done for all other service actions 8232 */ 8233 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8234 mtx_lock(&lun->lun_lock); 8235 if (lun->per_res[residx].registered) { 8236 if (memcmp(param->res_key.key, 8237 lun->per_res[residx].res_key.key, 8238 ctl_min(sizeof(param->res_key), 8239 sizeof(lun->per_res[residx].res_key))) != 0) { 8240 /* 8241 * The current key passed in doesn't match 8242 * the one the initiator previously 8243 * registered. 8244 */ 8245 mtx_unlock(&lun->lun_lock); 8246 free(ctsio->kern_data_ptr, M_CTL); 8247 ctl_set_reservation_conflict(ctsio); 8248 ctl_done((union ctl_io *)ctsio); 8249 return (CTL_RETVAL_COMPLETE); 8250 } 8251 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8252 /* 8253 * We are not registered 8254 */ 8255 mtx_unlock(&lun->lun_lock); 8256 free(ctsio->kern_data_ptr, M_CTL); 8257 ctl_set_reservation_conflict(ctsio); 8258 ctl_done((union ctl_io *)ctsio); 8259 return (CTL_RETVAL_COMPLETE); 8260 } else if (res_key != 0) { 8261 /* 8262 * We are not registered and trying to register but 8263 * the register key isn't zero. 8264 */ 8265 mtx_unlock(&lun->lun_lock); 8266 free(ctsio->kern_data_ptr, M_CTL); 8267 ctl_set_reservation_conflict(ctsio); 8268 ctl_done((union ctl_io *)ctsio); 8269 return (CTL_RETVAL_COMPLETE); 8270 } 8271 mtx_unlock(&lun->lun_lock); 8272 } 8273 8274 switch (cdb->action & SPRO_ACTION_MASK) { 8275 case SPRO_REGISTER: 8276 case SPRO_REG_IGNO: { 8277 8278#if 0 8279 printf("Registration received\n"); 8280#endif 8281 8282 /* 8283 * We don't support any of these options, as we report in 8284 * the read capabilities request (see 8285 * ctl_persistent_reserve_in(), above). 8286 */ 8287 if ((param->flags & SPR_SPEC_I_PT) 8288 || (param->flags & SPR_ALL_TG_PT) 8289 || (param->flags & SPR_APTPL)) { 8290 int bit_ptr; 8291 8292 if (param->flags & SPR_APTPL) 8293 bit_ptr = 0; 8294 else if (param->flags & SPR_ALL_TG_PT) 8295 bit_ptr = 2; 8296 else /* SPR_SPEC_I_PT */ 8297 bit_ptr = 3; 8298 8299 free(ctsio->kern_data_ptr, M_CTL); 8300 ctl_set_invalid_field(ctsio, 8301 /*sks_valid*/ 1, 8302 /*command*/ 0, 8303 /*field*/ 20, 8304 /*bit_valid*/ 1, 8305 /*bit*/ bit_ptr); 8306 ctl_done((union ctl_io *)ctsio); 8307 return (CTL_RETVAL_COMPLETE); 8308 } 8309 8310 mtx_lock(&lun->lun_lock); 8311 8312 /* 8313 * The initiator wants to clear the 8314 * key/unregister. 8315 */ 8316 if (sa_res_key == 0) { 8317 if ((res_key == 0 8318 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8319 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8320 && !lun->per_res[residx].registered)) { 8321 mtx_unlock(&lun->lun_lock); 8322 goto done; 8323 } 8324 8325 lun->per_res[residx].registered = 0; 8326 memset(&lun->per_res[residx].res_key, 8327 0, sizeof(lun->per_res[residx].res_key)); 8328 lun->pr_key_count--; 8329 8330 if (residx == lun->pr_res_idx) { 8331 lun->flags &= ~CTL_LUN_PR_RESERVED; 8332 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8333 8334 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8335 || lun->res_type == SPR_TYPE_EX_AC_RO) 8336 && lun->pr_key_count) { 8337 /* 8338 * If the reservation is a registrants 8339 * only type we need to generate a UA 8340 * for other registered inits. The 8341 * sense code should be RESERVATIONS 8342 * RELEASED 8343 */ 8344 8345 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8346 if (lun->per_res[ 8347 i+persis_offset].registered 8348 == 0) 8349 continue; 8350 lun->pending_sense[i 8351 ].ua_pending |= 8352 CTL_UA_RES_RELEASE; 8353 } 8354 } 8355 lun->res_type = 0; 8356 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8357 if (lun->pr_key_count==0) { 8358 lun->flags &= ~CTL_LUN_PR_RESERVED; 8359 lun->res_type = 0; 8360 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8361 } 8362 } 8363 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8364 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8365 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8366 persis_io.pr.pr_info.residx = residx; 8367 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8368 &persis_io, sizeof(persis_io), 0 )) > 8369 CTL_HA_STATUS_SUCCESS) { 8370 printf("CTL:Persis Out error returned from " 8371 "ctl_ha_msg_send %d\n", isc_retval); 8372 } 8373 } else /* sa_res_key != 0 */ { 8374 8375 /* 8376 * If we aren't registered currently then increment 8377 * the key count and set the registered flag. 8378 */ 8379 if (!lun->per_res[residx].registered) { 8380 lun->pr_key_count++; 8381 lun->per_res[residx].registered = 1; 8382 } 8383 8384 memcpy(&lun->per_res[residx].res_key, 8385 param->serv_act_res_key, 8386 ctl_min(sizeof(param->serv_act_res_key), 8387 sizeof(lun->per_res[residx].res_key))); 8388 8389 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8390 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8391 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8392 persis_io.pr.pr_info.residx = residx; 8393 memcpy(persis_io.pr.pr_info.sa_res_key, 8394 param->serv_act_res_key, 8395 sizeof(param->serv_act_res_key)); 8396 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8397 &persis_io, sizeof(persis_io), 0)) > 8398 CTL_HA_STATUS_SUCCESS) { 8399 printf("CTL:Persis Out error returned from " 8400 "ctl_ha_msg_send %d\n", isc_retval); 8401 } 8402 } 8403 lun->PRGeneration++; 8404 mtx_unlock(&lun->lun_lock); 8405 8406 break; 8407 } 8408 case SPRO_RESERVE: 8409#if 0 8410 printf("Reserve executed type %d\n", type); 8411#endif 8412 mtx_lock(&lun->lun_lock); 8413 if (lun->flags & CTL_LUN_PR_RESERVED) { 8414 /* 8415 * if this isn't the reservation holder and it's 8416 * not a "all registrants" type or if the type is 8417 * different then we have a conflict 8418 */ 8419 if ((lun->pr_res_idx != residx 8420 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8421 || lun->res_type != type) { 8422 mtx_unlock(&lun->lun_lock); 8423 free(ctsio->kern_data_ptr, M_CTL); 8424 ctl_set_reservation_conflict(ctsio); 8425 ctl_done((union ctl_io *)ctsio); 8426 return (CTL_RETVAL_COMPLETE); 8427 } 8428 mtx_unlock(&lun->lun_lock); 8429 } else /* create a reservation */ { 8430 /* 8431 * If it's not an "all registrants" type record 8432 * reservation holder 8433 */ 8434 if (type != SPR_TYPE_WR_EX_AR 8435 && type != SPR_TYPE_EX_AC_AR) 8436 lun->pr_res_idx = residx; /* Res holder */ 8437 else 8438 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8439 8440 lun->flags |= CTL_LUN_PR_RESERVED; 8441 lun->res_type = type; 8442 8443 mtx_unlock(&lun->lun_lock); 8444 8445 /* send msg to other side */ 8446 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8447 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8448 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8449 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8450 persis_io.pr.pr_info.res_type = type; 8451 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8452 &persis_io, sizeof(persis_io), 0)) > 8453 CTL_HA_STATUS_SUCCESS) { 8454 printf("CTL:Persis Out error returned from " 8455 "ctl_ha_msg_send %d\n", isc_retval); 8456 } 8457 } 8458 break; 8459 8460 case SPRO_RELEASE: 8461 mtx_lock(&lun->lun_lock); 8462 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8463 /* No reservation exists return good status */ 8464 mtx_unlock(&lun->lun_lock); 8465 goto done; 8466 } 8467 /* 8468 * Is this nexus a reservation holder? 8469 */ 8470 if (lun->pr_res_idx != residx 8471 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8472 /* 8473 * not a res holder return good status but 8474 * do nothing 8475 */ 8476 mtx_unlock(&lun->lun_lock); 8477 goto done; 8478 } 8479 8480 if (lun->res_type != type) { 8481 mtx_unlock(&lun->lun_lock); 8482 free(ctsio->kern_data_ptr, M_CTL); 8483 ctl_set_illegal_pr_release(ctsio); 8484 ctl_done((union ctl_io *)ctsio); 8485 return (CTL_RETVAL_COMPLETE); 8486 } 8487 8488 /* okay to release */ 8489 lun->flags &= ~CTL_LUN_PR_RESERVED; 8490 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8491 lun->res_type = 0; 8492 8493 /* 8494 * if this isn't an exclusive access 8495 * res generate UA for all other 8496 * registrants. 8497 */ 8498 if (type != SPR_TYPE_EX_AC 8499 && type != SPR_TYPE_WR_EX) { 8500 /* 8501 * temporarily unregister so we don't generate UA 8502 */ 8503 lun->per_res[residx].registered = 0; 8504 8505 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8506 if (lun->per_res[i+persis_offset].registered 8507 == 0) 8508 continue; 8509 lun->pending_sense[i].ua_pending |= 8510 CTL_UA_RES_RELEASE; 8511 } 8512 8513 lun->per_res[residx].registered = 1; 8514 } 8515 mtx_unlock(&lun->lun_lock); 8516 /* Send msg to other side */ 8517 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8518 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8519 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8520 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8521 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8522 printf("CTL:Persis Out error returned from " 8523 "ctl_ha_msg_send %d\n", isc_retval); 8524 } 8525 break; 8526 8527 case SPRO_CLEAR: 8528 /* send msg to other side */ 8529 8530 mtx_lock(&lun->lun_lock); 8531 lun->flags &= ~CTL_LUN_PR_RESERVED; 8532 lun->res_type = 0; 8533 lun->pr_key_count = 0; 8534 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8535 8536 8537 memset(&lun->per_res[residx].res_key, 8538 0, sizeof(lun->per_res[residx].res_key)); 8539 lun->per_res[residx].registered = 0; 8540 8541 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8542 if (lun->per_res[i].registered) { 8543 if (!persis_offset && i < CTL_MAX_INITIATORS) 8544 lun->pending_sense[i].ua_pending |= 8545 CTL_UA_RES_PREEMPT; 8546 else if (persis_offset && i >= persis_offset) 8547 lun->pending_sense[i-persis_offset 8548 ].ua_pending |= CTL_UA_RES_PREEMPT; 8549 8550 memset(&lun->per_res[i].res_key, 8551 0, sizeof(struct scsi_per_res_key)); 8552 lun->per_res[i].registered = 0; 8553 } 8554 lun->PRGeneration++; 8555 mtx_unlock(&lun->lun_lock); 8556 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8557 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8558 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8559 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8560 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8561 printf("CTL:Persis Out error returned from " 8562 "ctl_ha_msg_send %d\n", isc_retval); 8563 } 8564 break; 8565 8566 case SPRO_PREEMPT: { 8567 int nretval; 8568 8569 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8570 residx, ctsio, cdb, param); 8571 if (nretval != 0) 8572 return (CTL_RETVAL_COMPLETE); 8573 break; 8574 } 8575 default: 8576 panic("Invalid PR type %x", cdb->action); 8577 } 8578 8579done: 8580 free(ctsio->kern_data_ptr, M_CTL); 8581 ctl_set_success(ctsio); 8582 ctl_done((union ctl_io *)ctsio); 8583 8584 return (retval); 8585} 8586 8587/* 8588 * This routine is for handling a message from the other SC pertaining to 8589 * persistent reserve out. All the error checking will have been done 8590 * so only perorming the action need be done here to keep the two 8591 * in sync. 8592 */ 8593static void 8594ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8595{ 8596 struct ctl_lun *lun; 8597 struct ctl_softc *softc; 8598 int i; 8599 uint32_t targ_lun; 8600 8601 softc = control_softc; 8602 8603 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8604 lun = softc->ctl_luns[targ_lun]; 8605 mtx_lock(&lun->lun_lock); 8606 switch(msg->pr.pr_info.action) { 8607 case CTL_PR_REG_KEY: 8608 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8609 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8610 lun->pr_key_count++; 8611 } 8612 lun->PRGeneration++; 8613 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8614 msg->pr.pr_info.sa_res_key, 8615 sizeof(struct scsi_per_res_key)); 8616 break; 8617 8618 case CTL_PR_UNREG_KEY: 8619 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8620 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8621 0, sizeof(struct scsi_per_res_key)); 8622 lun->pr_key_count--; 8623 8624 /* XXX Need to see if the reservation has been released */ 8625 /* if so do we need to generate UA? */ 8626 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8627 lun->flags &= ~CTL_LUN_PR_RESERVED; 8628 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8629 8630 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8631 || lun->res_type == SPR_TYPE_EX_AC_RO) 8632 && lun->pr_key_count) { 8633 /* 8634 * If the reservation is a registrants 8635 * only type we need to generate a UA 8636 * for other registered inits. The 8637 * sense code should be RESERVATIONS 8638 * RELEASED 8639 */ 8640 8641 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8642 if (lun->per_res[i+ 8643 persis_offset].registered == 0) 8644 continue; 8645 8646 lun->pending_sense[i 8647 ].ua_pending |= 8648 CTL_UA_RES_RELEASE; 8649 } 8650 } 8651 lun->res_type = 0; 8652 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8653 if (lun->pr_key_count==0) { 8654 lun->flags &= ~CTL_LUN_PR_RESERVED; 8655 lun->res_type = 0; 8656 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8657 } 8658 } 8659 lun->PRGeneration++; 8660 break; 8661 8662 case CTL_PR_RESERVE: 8663 lun->flags |= CTL_LUN_PR_RESERVED; 8664 lun->res_type = msg->pr.pr_info.res_type; 8665 lun->pr_res_idx = msg->pr.pr_info.residx; 8666 8667 break; 8668 8669 case CTL_PR_RELEASE: 8670 /* 8671 * if this isn't an exclusive access res generate UA for all 8672 * other registrants. 8673 */ 8674 if (lun->res_type != SPR_TYPE_EX_AC 8675 && lun->res_type != SPR_TYPE_WR_EX) { 8676 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8677 if (lun->per_res[i+persis_offset].registered) 8678 lun->pending_sense[i].ua_pending |= 8679 CTL_UA_RES_RELEASE; 8680 } 8681 8682 lun->flags &= ~CTL_LUN_PR_RESERVED; 8683 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8684 lun->res_type = 0; 8685 break; 8686 8687 case CTL_PR_PREEMPT: 8688 ctl_pro_preempt_other(lun, msg); 8689 break; 8690 case CTL_PR_CLEAR: 8691 lun->flags &= ~CTL_LUN_PR_RESERVED; 8692 lun->res_type = 0; 8693 lun->pr_key_count = 0; 8694 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8695 8696 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8697 if (lun->per_res[i].registered == 0) 8698 continue; 8699 if (!persis_offset 8700 && i < CTL_MAX_INITIATORS) 8701 lun->pending_sense[i].ua_pending |= 8702 CTL_UA_RES_PREEMPT; 8703 else if (persis_offset 8704 && i >= persis_offset) 8705 lun->pending_sense[i-persis_offset].ua_pending|= 8706 CTL_UA_RES_PREEMPT; 8707 memset(&lun->per_res[i].res_key, 0, 8708 sizeof(struct scsi_per_res_key)); 8709 lun->per_res[i].registered = 0; 8710 } 8711 lun->PRGeneration++; 8712 break; 8713 } 8714 8715 mtx_unlock(&lun->lun_lock); 8716} 8717 8718int 8719ctl_read_write(struct ctl_scsiio *ctsio) 8720{ 8721 struct ctl_lun *lun; 8722 struct ctl_lba_len_flags *lbalen; 8723 uint64_t lba; 8724 uint32_t num_blocks; 8725 int fua, dpo; 8726 int retval; 8727 int isread; 8728 8729 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8730 8731 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8732 8733 fua = 0; 8734 dpo = 0; 8735 8736 retval = CTL_RETVAL_COMPLETE; 8737 8738 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8739 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8740 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8741 uint32_t residx; 8742 8743 /* 8744 * XXX KDM need a lock here. 8745 */ 8746 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8747 if ((lun->res_type == SPR_TYPE_EX_AC 8748 && residx != lun->pr_res_idx) 8749 || ((lun->res_type == SPR_TYPE_EX_AC_RO 8750 || lun->res_type == SPR_TYPE_EX_AC_AR) 8751 && !lun->per_res[residx].registered)) { 8752 ctl_set_reservation_conflict(ctsio); 8753 ctl_done((union ctl_io *)ctsio); 8754 return (CTL_RETVAL_COMPLETE); 8755 } 8756 } 8757 8758 switch (ctsio->cdb[0]) { 8759 case READ_6: 8760 case WRITE_6: { 8761 struct scsi_rw_6 *cdb; 8762 8763 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8764 8765 lba = scsi_3btoul(cdb->addr); 8766 /* only 5 bits are valid in the most significant address byte */ 8767 lba &= 0x1fffff; 8768 num_blocks = cdb->length; 8769 /* 8770 * This is correct according to SBC-2. 8771 */ 8772 if (num_blocks == 0) 8773 num_blocks = 256; 8774 break; 8775 } 8776 case READ_10: 8777 case WRITE_10: { 8778 struct scsi_rw_10 *cdb; 8779 8780 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8781 8782 if (cdb->byte2 & SRW10_FUA) 8783 fua = 1; 8784 if (cdb->byte2 & SRW10_DPO) 8785 dpo = 1; 8786 8787 lba = scsi_4btoul(cdb->addr); 8788 num_blocks = scsi_2btoul(cdb->length); 8789 break; 8790 } 8791 case WRITE_VERIFY_10: { 8792 struct scsi_write_verify_10 *cdb; 8793 8794 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8795 8796 /* 8797 * XXX KDM we should do actual write verify support at some 8798 * point. This is obviously fake, we're just translating 8799 * things to a write. So we don't even bother checking the 8800 * BYTCHK field, since we don't do any verification. If 8801 * the user asks for it, we'll just pretend we did it. 8802 */ 8803 if (cdb->byte2 & SWV_DPO) 8804 dpo = 1; 8805 8806 lba = scsi_4btoul(cdb->addr); 8807 num_blocks = scsi_2btoul(cdb->length); 8808 break; 8809 } 8810 case READ_12: 8811 case WRITE_12: { 8812 struct scsi_rw_12 *cdb; 8813 8814 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8815 8816 if (cdb->byte2 & SRW12_FUA) 8817 fua = 1; 8818 if (cdb->byte2 & SRW12_DPO) 8819 dpo = 1; 8820 lba = scsi_4btoul(cdb->addr); 8821 num_blocks = scsi_4btoul(cdb->length); 8822 break; 8823 } 8824 case WRITE_VERIFY_12: { 8825 struct scsi_write_verify_12 *cdb; 8826 8827 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8828 8829 if (cdb->byte2 & SWV_DPO) 8830 dpo = 1; 8831 8832 lba = scsi_4btoul(cdb->addr); 8833 num_blocks = scsi_4btoul(cdb->length); 8834 8835 break; 8836 } 8837 case READ_16: 8838 case WRITE_16: { 8839 struct scsi_rw_16 *cdb; 8840 8841 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8842 8843 if (cdb->byte2 & SRW12_FUA) 8844 fua = 1; 8845 if (cdb->byte2 & SRW12_DPO) 8846 dpo = 1; 8847 8848 lba = scsi_8btou64(cdb->addr); 8849 num_blocks = scsi_4btoul(cdb->length); 8850 break; 8851 } 8852 case WRITE_VERIFY_16: { 8853 struct scsi_write_verify_16 *cdb; 8854 8855 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8856 8857 if (cdb->byte2 & SWV_DPO) 8858 dpo = 1; 8859 8860 lba = scsi_8btou64(cdb->addr); 8861 num_blocks = scsi_4btoul(cdb->length); 8862 break; 8863 } 8864 default: 8865 /* 8866 * We got a command we don't support. This shouldn't 8867 * happen, commands should be filtered out above us. 8868 */ 8869 ctl_set_invalid_opcode(ctsio); 8870 ctl_done((union ctl_io *)ctsio); 8871 8872 return (CTL_RETVAL_COMPLETE); 8873 break; /* NOTREACHED */ 8874 } 8875 8876 /* 8877 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8878 * interesting for us, but if RAIDCore is in write-back mode, 8879 * getting it to do write-through for a particular transaction may 8880 * not be possible. 8881 */ 8882 8883 /* 8884 * The first check is to make sure we're in bounds, the second 8885 * check is to catch wrap-around problems. If the lba + num blocks 8886 * is less than the lba, then we've wrapped around and the block 8887 * range is invalid anyway. 8888 */ 8889 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8890 || ((lba + num_blocks) < lba)) { 8891 ctl_set_lba_out_of_range(ctsio); 8892 ctl_done((union ctl_io *)ctsio); 8893 return (CTL_RETVAL_COMPLETE); 8894 } 8895 8896 /* 8897 * According to SBC-3, a transfer length of 0 is not an error. 8898 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8899 * translates to 256 blocks for those commands. 8900 */ 8901 if (num_blocks == 0) { 8902 ctl_set_success(ctsio); 8903 ctl_done((union ctl_io *)ctsio); 8904 return (CTL_RETVAL_COMPLETE); 8905 } 8906 8907 lbalen = (struct ctl_lba_len_flags *) 8908 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8909 lbalen->lba = lba; 8910 lbalen->len = num_blocks; 8911 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 8912 8913 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8914 ctsio->kern_rel_offset = 0; 8915 8916 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8917 8918 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8919 8920 return (retval); 8921} 8922 8923static int 8924ctl_cnw_cont(union ctl_io *io) 8925{ 8926 struct ctl_scsiio *ctsio; 8927 struct ctl_lun *lun; 8928 struct ctl_lba_len_flags *lbalen; 8929 int retval; 8930 8931 ctsio = &io->scsiio; 8932 ctsio->io_hdr.status = CTL_STATUS_NONE; 8933 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8934 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8935 lbalen = (struct ctl_lba_len_flags *) 8936 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8937 lbalen->flags = CTL_LLF_WRITE; 8938 8939 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8940 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8941 return (retval); 8942} 8943 8944int 8945ctl_cnw(struct ctl_scsiio *ctsio) 8946{ 8947 struct ctl_lun *lun; 8948 struct ctl_lba_len_flags *lbalen; 8949 uint64_t lba; 8950 uint32_t num_blocks; 8951 int fua, dpo; 8952 int retval; 8953 8954 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8955 8956 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8957 8958 fua = 0; 8959 dpo = 0; 8960 8961 retval = CTL_RETVAL_COMPLETE; 8962 8963 switch (ctsio->cdb[0]) { 8964 case COMPARE_AND_WRITE: { 8965 struct scsi_compare_and_write *cdb; 8966 8967 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8968 8969 if (cdb->byte2 & SRW10_FUA) 8970 fua = 1; 8971 if (cdb->byte2 & SRW10_DPO) 8972 dpo = 1; 8973 lba = scsi_8btou64(cdb->addr); 8974 num_blocks = cdb->length; 8975 break; 8976 } 8977 default: 8978 /* 8979 * We got a command we don't support. This shouldn't 8980 * happen, commands should be filtered out above us. 8981 */ 8982 ctl_set_invalid_opcode(ctsio); 8983 ctl_done((union ctl_io *)ctsio); 8984 8985 return (CTL_RETVAL_COMPLETE); 8986 break; /* NOTREACHED */ 8987 } 8988 8989 /* 8990 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8991 * interesting for us, but if RAIDCore is in write-back mode, 8992 * getting it to do write-through for a particular transaction may 8993 * not be possible. 8994 */ 8995 8996 /* 8997 * The first check is to make sure we're in bounds, the second 8998 * check is to catch wrap-around problems. If the lba + num blocks 8999 * is less than the lba, then we've wrapped around and the block 9000 * range is invalid anyway. 9001 */ 9002 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9003 || ((lba + num_blocks) < lba)) { 9004 ctl_set_lba_out_of_range(ctsio); 9005 ctl_done((union ctl_io *)ctsio); 9006 return (CTL_RETVAL_COMPLETE); 9007 } 9008 9009 /* 9010 * According to SBC-3, a transfer length of 0 is not an error. 9011 */ 9012 if (num_blocks == 0) { 9013 ctl_set_success(ctsio); 9014 ctl_done((union ctl_io *)ctsio); 9015 return (CTL_RETVAL_COMPLETE); 9016 } 9017 9018 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9019 ctsio->kern_rel_offset = 0; 9020 9021 /* 9022 * Set the IO_CONT flag, so that if this I/O gets passed to 9023 * ctl_data_submit_done(), it'll get passed back to 9024 * ctl_ctl_cnw_cont() for further processing. 9025 */ 9026 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9027 ctsio->io_cont = ctl_cnw_cont; 9028 9029 lbalen = (struct ctl_lba_len_flags *) 9030 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9031 lbalen->lba = lba; 9032 lbalen->len = num_blocks; 9033 lbalen->flags = CTL_LLF_COMPARE; 9034 9035 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9036 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9037 return (retval); 9038} 9039 9040int 9041ctl_verify(struct ctl_scsiio *ctsio) 9042{ 9043 struct ctl_lun *lun; 9044 struct ctl_lba_len_flags *lbalen; 9045 uint64_t lba; 9046 uint32_t num_blocks; 9047 int bytchk, dpo; 9048 int retval; 9049 9050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9051 9052 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9053 9054 bytchk = 0; 9055 dpo = 0; 9056 retval = CTL_RETVAL_COMPLETE; 9057 9058 switch (ctsio->cdb[0]) { 9059 case VERIFY_10: { 9060 struct scsi_verify_10 *cdb; 9061 9062 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9063 if (cdb->byte2 & SVFY_BYTCHK) 9064 bytchk = 1; 9065 if (cdb->byte2 & SVFY_DPO) 9066 dpo = 1; 9067 lba = scsi_4btoul(cdb->addr); 9068 num_blocks = scsi_2btoul(cdb->length); 9069 break; 9070 } 9071 case VERIFY_12: { 9072 struct scsi_verify_12 *cdb; 9073 9074 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9075 if (cdb->byte2 & SVFY_BYTCHK) 9076 bytchk = 1; 9077 if (cdb->byte2 & SVFY_DPO) 9078 dpo = 1; 9079 lba = scsi_4btoul(cdb->addr); 9080 num_blocks = scsi_4btoul(cdb->length); 9081 break; 9082 } 9083 case VERIFY_16: { 9084 struct scsi_rw_16 *cdb; 9085 9086 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9087 if (cdb->byte2 & SVFY_BYTCHK) 9088 bytchk = 1; 9089 if (cdb->byte2 & SVFY_DPO) 9090 dpo = 1; 9091 lba = scsi_8btou64(cdb->addr); 9092 num_blocks = scsi_4btoul(cdb->length); 9093 break; 9094 } 9095 default: 9096 /* 9097 * We got a command we don't support. This shouldn't 9098 * happen, commands should be filtered out above us. 9099 */ 9100 ctl_set_invalid_opcode(ctsio); 9101 ctl_done((union ctl_io *)ctsio); 9102 return (CTL_RETVAL_COMPLETE); 9103 } 9104 9105 /* 9106 * The first check is to make sure we're in bounds, the second 9107 * check is to catch wrap-around problems. If the lba + num blocks 9108 * is less than the lba, then we've wrapped around and the block 9109 * range is invalid anyway. 9110 */ 9111 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9112 || ((lba + num_blocks) < lba)) { 9113 ctl_set_lba_out_of_range(ctsio); 9114 ctl_done((union ctl_io *)ctsio); 9115 return (CTL_RETVAL_COMPLETE); 9116 } 9117 9118 /* 9119 * According to SBC-3, a transfer length of 0 is not an error. 9120 */ 9121 if (num_blocks == 0) { 9122 ctl_set_success(ctsio); 9123 ctl_done((union ctl_io *)ctsio); 9124 return (CTL_RETVAL_COMPLETE); 9125 } 9126 9127 lbalen = (struct ctl_lba_len_flags *) 9128 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9129 lbalen->lba = lba; 9130 lbalen->len = num_blocks; 9131 if (bytchk) { 9132 lbalen->flags = CTL_LLF_COMPARE; 9133 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9134 } else { 9135 lbalen->flags = CTL_LLF_VERIFY; 9136 ctsio->kern_total_len = 0; 9137 } 9138 ctsio->kern_rel_offset = 0; 9139 9140 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9141 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9142 return (retval); 9143} 9144 9145int 9146ctl_report_luns(struct ctl_scsiio *ctsio) 9147{ 9148 struct scsi_report_luns *cdb; 9149 struct scsi_report_luns_data *lun_data; 9150 struct ctl_lun *lun, *request_lun; 9151 int num_luns, retval; 9152 uint32_t alloc_len, lun_datalen; 9153 int num_filled, well_known; 9154 uint32_t initidx, targ_lun_id, lun_id; 9155 9156 retval = CTL_RETVAL_COMPLETE; 9157 well_known = 0; 9158 9159 cdb = (struct scsi_report_luns *)ctsio->cdb; 9160 9161 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9162 9163 mtx_lock(&control_softc->ctl_lock); 9164 num_luns = control_softc->num_luns; 9165 mtx_unlock(&control_softc->ctl_lock); 9166 9167 switch (cdb->select_report) { 9168 case RPL_REPORT_DEFAULT: 9169 case RPL_REPORT_ALL: 9170 break; 9171 case RPL_REPORT_WELLKNOWN: 9172 well_known = 1; 9173 num_luns = 0; 9174 break; 9175 default: 9176 ctl_set_invalid_field(ctsio, 9177 /*sks_valid*/ 1, 9178 /*command*/ 1, 9179 /*field*/ 2, 9180 /*bit_valid*/ 0, 9181 /*bit*/ 0); 9182 ctl_done((union ctl_io *)ctsio); 9183 return (retval); 9184 break; /* NOTREACHED */ 9185 } 9186 9187 alloc_len = scsi_4btoul(cdb->length); 9188 /* 9189 * The initiator has to allocate at least 16 bytes for this request, 9190 * so he can at least get the header and the first LUN. Otherwise 9191 * we reject the request (per SPC-3 rev 14, section 6.21). 9192 */ 9193 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9194 sizeof(struct scsi_report_luns_lundata))) { 9195 ctl_set_invalid_field(ctsio, 9196 /*sks_valid*/ 1, 9197 /*command*/ 1, 9198 /*field*/ 6, 9199 /*bit_valid*/ 0, 9200 /*bit*/ 0); 9201 ctl_done((union ctl_io *)ctsio); 9202 return (retval); 9203 } 9204 9205 request_lun = (struct ctl_lun *) 9206 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9207 9208 lun_datalen = sizeof(*lun_data) + 9209 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9210 9211 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9212 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9213 ctsio->kern_sg_entries = 0; 9214 9215 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9216 9217 mtx_lock(&control_softc->ctl_lock); 9218 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9219 lun_id = targ_lun_id; 9220 if (ctsio->io_hdr.nexus.lun_map_fn != NULL) 9221 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id); 9222 if (lun_id >= CTL_MAX_LUNS) 9223 continue; 9224 lun = control_softc->ctl_luns[lun_id]; 9225 if (lun == NULL) 9226 continue; 9227 9228 if (targ_lun_id <= 0xff) { 9229 /* 9230 * Peripheral addressing method, bus number 0. 9231 */ 9232 lun_data->luns[num_filled].lundata[0] = 9233 RPL_LUNDATA_ATYP_PERIPH; 9234 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9235 num_filled++; 9236 } else if (targ_lun_id <= 0x3fff) { 9237 /* 9238 * Flat addressing method. 9239 */ 9240 lun_data->luns[num_filled].lundata[0] = 9241 RPL_LUNDATA_ATYP_FLAT | 9242 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9243#ifdef OLDCTLHEADERS 9244 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9245 (targ_lun_id & SRLD_BUS_LUN_MASK); 9246#endif 9247 lun_data->luns[num_filled].lundata[1] = 9248#ifdef OLDCTLHEADERS 9249 targ_lun_id >> SRLD_BUS_LUN_BITS; 9250#endif 9251 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9252 num_filled++; 9253 } else { 9254 printf("ctl_report_luns: bogus LUN number %jd, " 9255 "skipping\n", (intmax_t)targ_lun_id); 9256 } 9257 /* 9258 * According to SPC-3, rev 14 section 6.21: 9259 * 9260 * "The execution of a REPORT LUNS command to any valid and 9261 * installed logical unit shall clear the REPORTED LUNS DATA 9262 * HAS CHANGED unit attention condition for all logical 9263 * units of that target with respect to the requesting 9264 * initiator. A valid and installed logical unit is one 9265 * having a PERIPHERAL QUALIFIER of 000b in the standard 9266 * INQUIRY data (see 6.4.2)." 9267 * 9268 * If request_lun is NULL, the LUN this report luns command 9269 * was issued to is either disabled or doesn't exist. In that 9270 * case, we shouldn't clear any pending lun change unit 9271 * attention. 9272 */ 9273 if (request_lun != NULL) { 9274 mtx_lock(&lun->lun_lock); 9275 lun->pending_sense[initidx].ua_pending &= 9276 ~CTL_UA_LUN_CHANGE; 9277 mtx_unlock(&lun->lun_lock); 9278 } 9279 } 9280 mtx_unlock(&control_softc->ctl_lock); 9281 9282 /* 9283 * It's quite possible that we've returned fewer LUNs than we allocated 9284 * space for. Trim it. 9285 */ 9286 lun_datalen = sizeof(*lun_data) + 9287 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9288 9289 if (lun_datalen < alloc_len) { 9290 ctsio->residual = alloc_len - lun_datalen; 9291 ctsio->kern_data_len = lun_datalen; 9292 ctsio->kern_total_len = lun_datalen; 9293 } else { 9294 ctsio->residual = 0; 9295 ctsio->kern_data_len = alloc_len; 9296 ctsio->kern_total_len = alloc_len; 9297 } 9298 ctsio->kern_data_resid = 0; 9299 ctsio->kern_rel_offset = 0; 9300 ctsio->kern_sg_entries = 0; 9301 9302 /* 9303 * We set this to the actual data length, regardless of how much 9304 * space we actually have to return results. If the user looks at 9305 * this value, he'll know whether or not he allocated enough space 9306 * and reissue the command if necessary. We don't support well 9307 * known logical units, so if the user asks for that, return none. 9308 */ 9309 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9310 9311 /* 9312 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9313 * this request. 9314 */ 9315 ctsio->scsi_status = SCSI_STATUS_OK; 9316 9317 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9318 ctsio->be_move_done = ctl_config_move_done; 9319 ctl_datamove((union ctl_io *)ctsio); 9320 9321 return (retval); 9322} 9323 9324int 9325ctl_request_sense(struct ctl_scsiio *ctsio) 9326{ 9327 struct scsi_request_sense *cdb; 9328 struct scsi_sense_data *sense_ptr; 9329 struct ctl_lun *lun; 9330 uint32_t initidx; 9331 int have_error; 9332 scsi_sense_data_type sense_format; 9333 9334 cdb = (struct scsi_request_sense *)ctsio->cdb; 9335 9336 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9337 9338 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9339 9340 /* 9341 * Determine which sense format the user wants. 9342 */ 9343 if (cdb->byte2 & SRS_DESC) 9344 sense_format = SSD_TYPE_DESC; 9345 else 9346 sense_format = SSD_TYPE_FIXED; 9347 9348 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9349 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9350 ctsio->kern_sg_entries = 0; 9351 9352 /* 9353 * struct scsi_sense_data, which is currently set to 256 bytes, is 9354 * larger than the largest allowed value for the length field in the 9355 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9356 */ 9357 ctsio->residual = 0; 9358 ctsio->kern_data_len = cdb->length; 9359 ctsio->kern_total_len = cdb->length; 9360 9361 ctsio->kern_data_resid = 0; 9362 ctsio->kern_rel_offset = 0; 9363 ctsio->kern_sg_entries = 0; 9364 9365 /* 9366 * If we don't have a LUN, we don't have any pending sense. 9367 */ 9368 if (lun == NULL) 9369 goto no_sense; 9370 9371 have_error = 0; 9372 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9373 /* 9374 * Check for pending sense, and then for pending unit attentions. 9375 * Pending sense gets returned first, then pending unit attentions. 9376 */ 9377 mtx_lock(&lun->lun_lock); 9378 if (ctl_is_set(lun->have_ca, initidx)) { 9379 scsi_sense_data_type stored_format; 9380 9381 /* 9382 * Check to see which sense format was used for the stored 9383 * sense data. 9384 */ 9385 stored_format = scsi_sense_type( 9386 &lun->pending_sense[initidx].sense); 9387 9388 /* 9389 * If the user requested a different sense format than the 9390 * one we stored, then we need to convert it to the other 9391 * format. If we're going from descriptor to fixed format 9392 * sense data, we may lose things in translation, depending 9393 * on what options were used. 9394 * 9395 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9396 * for some reason we'll just copy it out as-is. 9397 */ 9398 if ((stored_format == SSD_TYPE_FIXED) 9399 && (sense_format == SSD_TYPE_DESC)) 9400 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9401 &lun->pending_sense[initidx].sense, 9402 (struct scsi_sense_data_desc *)sense_ptr); 9403 else if ((stored_format == SSD_TYPE_DESC) 9404 && (sense_format == SSD_TYPE_FIXED)) 9405 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9406 &lun->pending_sense[initidx].sense, 9407 (struct scsi_sense_data_fixed *)sense_ptr); 9408 else 9409 memcpy(sense_ptr, &lun->pending_sense[initidx].sense, 9410 ctl_min(sizeof(*sense_ptr), 9411 sizeof(lun->pending_sense[initidx].sense))); 9412 9413 ctl_clear_mask(lun->have_ca, initidx); 9414 have_error = 1; 9415 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) { 9416 ctl_ua_type ua_type; 9417 9418 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending, 9419 sense_ptr, sense_format); 9420 if (ua_type != CTL_UA_NONE) { 9421 have_error = 1; 9422 /* We're reporting this UA, so clear it */ 9423 lun->pending_sense[initidx].ua_pending &= ~ua_type; 9424 } 9425 } 9426 mtx_unlock(&lun->lun_lock); 9427 9428 /* 9429 * We already have a pending error, return it. 9430 */ 9431 if (have_error != 0) { 9432 /* 9433 * We report the SCSI status as OK, since the status of the 9434 * request sense command itself is OK. 9435 */ 9436 ctsio->scsi_status = SCSI_STATUS_OK; 9437 9438 /* 9439 * We report 0 for the sense length, because we aren't doing 9440 * autosense in this case. We're reporting sense as 9441 * parameter data. 9442 */ 9443 ctsio->sense_len = 0; 9444 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9445 ctsio->be_move_done = ctl_config_move_done; 9446 ctl_datamove((union ctl_io *)ctsio); 9447 9448 return (CTL_RETVAL_COMPLETE); 9449 } 9450 9451no_sense: 9452 9453 /* 9454 * No sense information to report, so we report that everything is 9455 * okay. 9456 */ 9457 ctl_set_sense_data(sense_ptr, 9458 lun, 9459 sense_format, 9460 /*current_error*/ 1, 9461 /*sense_key*/ SSD_KEY_NO_SENSE, 9462 /*asc*/ 0x00, 9463 /*ascq*/ 0x00, 9464 SSD_ELEM_NONE); 9465 9466 ctsio->scsi_status = SCSI_STATUS_OK; 9467 9468 /* 9469 * We report 0 for the sense length, because we aren't doing 9470 * autosense in this case. We're reporting sense as parameter data. 9471 */ 9472 ctsio->sense_len = 0; 9473 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9474 ctsio->be_move_done = ctl_config_move_done; 9475 ctl_datamove((union ctl_io *)ctsio); 9476 9477 return (CTL_RETVAL_COMPLETE); 9478} 9479 9480int 9481ctl_tur(struct ctl_scsiio *ctsio) 9482{ 9483 struct ctl_lun *lun; 9484 9485 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9486 9487 CTL_DEBUG_PRINT(("ctl_tur\n")); 9488 9489 if (lun == NULL) 9490 return (EINVAL); 9491 9492 ctsio->scsi_status = SCSI_STATUS_OK; 9493 ctsio->io_hdr.status = CTL_SUCCESS; 9494 9495 ctl_done((union ctl_io *)ctsio); 9496 9497 return (CTL_RETVAL_COMPLETE); 9498} 9499 9500#ifdef notyet 9501static int 9502ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9503{ 9504 9505} 9506#endif 9507 9508static int 9509ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9510{ 9511 struct scsi_vpd_supported_pages *pages; 9512 int sup_page_size; 9513 struct ctl_lun *lun; 9514 9515 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9516 9517 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9518 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9519 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9520 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9521 ctsio->kern_sg_entries = 0; 9522 9523 if (sup_page_size < alloc_len) { 9524 ctsio->residual = alloc_len - sup_page_size; 9525 ctsio->kern_data_len = sup_page_size; 9526 ctsio->kern_total_len = sup_page_size; 9527 } else { 9528 ctsio->residual = 0; 9529 ctsio->kern_data_len = alloc_len; 9530 ctsio->kern_total_len = alloc_len; 9531 } 9532 ctsio->kern_data_resid = 0; 9533 ctsio->kern_rel_offset = 0; 9534 ctsio->kern_sg_entries = 0; 9535 9536 /* 9537 * The control device is always connected. The disk device, on the 9538 * other hand, may not be online all the time. Need to change this 9539 * to figure out whether the disk device is actually online or not. 9540 */ 9541 if (lun != NULL) 9542 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9543 lun->be_lun->lun_type; 9544 else 9545 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9546 9547 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9548 /* Supported VPD pages */ 9549 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9550 /* Serial Number */ 9551 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9552 /* Device Identification */ 9553 pages->page_list[2] = SVPD_DEVICE_ID; 9554 /* Block limits */ 9555 pages->page_list[3] = SVPD_BLOCK_LIMITS; 9556 /* Logical Block Provisioning */ 9557 pages->page_list[4] = SVPD_LBP; 9558 9559 ctsio->scsi_status = SCSI_STATUS_OK; 9560 9561 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9562 ctsio->be_move_done = ctl_config_move_done; 9563 ctl_datamove((union ctl_io *)ctsio); 9564 9565 return (CTL_RETVAL_COMPLETE); 9566} 9567 9568static int 9569ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9570{ 9571 struct scsi_vpd_unit_serial_number *sn_ptr; 9572 struct ctl_lun *lun; 9573 9574 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9575 9576 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9577 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9578 ctsio->kern_sg_entries = 0; 9579 9580 if (sizeof(*sn_ptr) < alloc_len) { 9581 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9582 ctsio->kern_data_len = sizeof(*sn_ptr); 9583 ctsio->kern_total_len = sizeof(*sn_ptr); 9584 } else { 9585 ctsio->residual = 0; 9586 ctsio->kern_data_len = alloc_len; 9587 ctsio->kern_total_len = alloc_len; 9588 } 9589 ctsio->kern_data_resid = 0; 9590 ctsio->kern_rel_offset = 0; 9591 ctsio->kern_sg_entries = 0; 9592 9593 /* 9594 * The control device is always connected. The disk device, on the 9595 * other hand, may not be online all the time. Need to change this 9596 * to figure out whether the disk device is actually online or not. 9597 */ 9598 if (lun != NULL) 9599 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9600 lun->be_lun->lun_type; 9601 else 9602 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9603 9604 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9605 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9606 /* 9607 * If we don't have a LUN, we just leave the serial number as 9608 * all spaces. 9609 */ 9610 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9611 if (lun != NULL) { 9612 strncpy((char *)sn_ptr->serial_num, 9613 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9614 } 9615 ctsio->scsi_status = SCSI_STATUS_OK; 9616 9617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9618 ctsio->be_move_done = ctl_config_move_done; 9619 ctl_datamove((union ctl_io *)ctsio); 9620 9621 return (CTL_RETVAL_COMPLETE); 9622} 9623 9624 9625static int 9626ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9627{ 9628 struct scsi_vpd_device_id *devid_ptr; 9629 struct scsi_vpd_id_descriptor *desc, *desc1; 9630 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */ 9631 struct scsi_vpd_id_t10 *t10id; 9632 struct ctl_softc *ctl_softc; 9633 struct ctl_lun *lun; 9634 struct ctl_port *port; 9635 char *val; 9636 int data_len, devid_len; 9637 9638 ctl_softc = control_softc; 9639 9640 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9641 9642 if (port->devid != NULL) 9643 return ((port->devid)(ctsio, alloc_len)); 9644 9645 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9646 9647 if (lun == NULL) { 9648 devid_len = CTL_DEVID_MIN_LEN; 9649 } else { 9650 devid_len = max(CTL_DEVID_MIN_LEN, 9651 strnlen(lun->be_lun->device_id, CTL_DEVID_LEN)); 9652 } 9653 9654 data_len = sizeof(struct scsi_vpd_device_id) + 9655 sizeof(struct scsi_vpd_id_descriptor) + 9656 sizeof(struct scsi_vpd_id_t10) + devid_len + 9657 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN + 9658 sizeof(struct scsi_vpd_id_descriptor) + 9659 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9660 sizeof(struct scsi_vpd_id_descriptor) + 9661 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9662 9663 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9664 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9665 ctsio->kern_sg_entries = 0; 9666 9667 if (data_len < alloc_len) { 9668 ctsio->residual = alloc_len - data_len; 9669 ctsio->kern_data_len = data_len; 9670 ctsio->kern_total_len = data_len; 9671 } else { 9672 ctsio->residual = 0; 9673 ctsio->kern_data_len = alloc_len; 9674 ctsio->kern_total_len = alloc_len; 9675 } 9676 ctsio->kern_data_resid = 0; 9677 ctsio->kern_rel_offset = 0; 9678 ctsio->kern_sg_entries = 0; 9679 9680 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9681 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 9682 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9683 sizeof(struct scsi_vpd_id_t10) + devid_len); 9684 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] + 9685 CTL_WWPN_LEN); 9686 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] + 9687 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9688 9689 /* 9690 * The control device is always connected. The disk device, on the 9691 * other hand, may not be online all the time. 9692 */ 9693 if (lun != NULL) 9694 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9695 lun->be_lun->lun_type; 9696 else 9697 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9698 9699 devid_ptr->page_code = SVPD_DEVICE_ID; 9700 9701 scsi_ulto2b(data_len - 4, devid_ptr->length); 9702 9703 /* 9704 * For Fibre channel, 9705 */ 9706 if (port->port_type == CTL_PORT_FC) 9707 { 9708 desc->proto_codeset = (SCSI_PROTO_FC << 4) | 9709 SVPD_ID_CODESET_ASCII; 9710 desc1->proto_codeset = (SCSI_PROTO_FC << 4) | 9711 SVPD_ID_CODESET_BINARY; 9712 } 9713 else 9714 { 9715 desc->proto_codeset = (SCSI_PROTO_SPI << 4) | 9716 SVPD_ID_CODESET_ASCII; 9717 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) | 9718 SVPD_ID_CODESET_BINARY; 9719 } 9720 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset; 9721 9722 /* 9723 * We're using a LUN association here. i.e., this device ID is a 9724 * per-LUN identifier. 9725 */ 9726 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 9727 desc->length = sizeof(*t10id) + devid_len; 9728 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 9729 "vendor")) == NULL) { 9730 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 9731 } else { 9732 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 9733 strncpy(t10id->vendor, val, 9734 min(sizeof(t10id->vendor), strlen(val))); 9735 } 9736 9737 /* 9738 * desc1 is for the WWPN which is a port asscociation. 9739 */ 9740 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA; 9741 desc1->length = CTL_WWPN_LEN; 9742 /* XXX Call Reggie's get_WWNN func here then add port # to the end */ 9743 /* For testing just create the WWPN */ 9744#if 0 9745 ddb_GetWWNN((char *)desc1->identifier); 9746 9747 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9748 /* This is so Copancontrol will return something sane */ 9749 if (ctsio->io_hdr.nexus.targ_port!=0 && 9750 ctsio->io_hdr.nexus.targ_port!=8) 9751 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1; 9752 else 9753 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port; 9754#endif 9755 9756 be64enc(desc1->identifier, port->wwpn); 9757 9758 /* 9759 * desc2 is for the Relative Target Port(type 4h) identifier 9760 */ 9761 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9762 | SVPD_ID_TYPE_RELTARG; 9763 desc2->length = 4; 9764//#if 0 9765 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9766 /* This is so Copancontrol will return something sane */ 9767 if (ctsio->io_hdr.nexus.targ_port!=0 && 9768 ctsio->io_hdr.nexus.targ_port!=8) 9769 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1; 9770 else 9771 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port; 9772//#endif 9773 9774 /* 9775 * desc3 is for the Target Port Group(type 5h) identifier 9776 */ 9777 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9778 | SVPD_ID_TYPE_TPORTGRP; 9779 desc3->length = 4; 9780 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single) 9781 desc3->identifier[3] = 1; 9782 else 9783 desc3->identifier[3] = 2; 9784 9785 /* 9786 * If we've actually got a backend, copy the device id from the 9787 * per-LUN data. Otherwise, set it to all spaces. 9788 */ 9789 if (lun != NULL) { 9790 /* 9791 * Copy the backend's LUN ID. 9792 */ 9793 strncpy((char *)t10id->vendor_spec_id, 9794 (char *)lun->be_lun->device_id, devid_len); 9795 } else { 9796 /* 9797 * No backend, set this to spaces. 9798 */ 9799 memset(t10id->vendor_spec_id, 0x20, devid_len); 9800 } 9801 9802 ctsio->scsi_status = SCSI_STATUS_OK; 9803 9804 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9805 ctsio->be_move_done = ctl_config_move_done; 9806 ctl_datamove((union ctl_io *)ctsio); 9807 9808 return (CTL_RETVAL_COMPLETE); 9809} 9810 9811static int 9812ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9813{ 9814 struct scsi_vpd_block_limits *bl_ptr; 9815 struct ctl_lun *lun; 9816 int bs; 9817 9818 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9819 bs = lun->be_lun->blocksize; 9820 9821 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9822 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9823 ctsio->kern_sg_entries = 0; 9824 9825 if (sizeof(*bl_ptr) < alloc_len) { 9826 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9827 ctsio->kern_data_len = sizeof(*bl_ptr); 9828 ctsio->kern_total_len = sizeof(*bl_ptr); 9829 } else { 9830 ctsio->residual = 0; 9831 ctsio->kern_data_len = alloc_len; 9832 ctsio->kern_total_len = alloc_len; 9833 } 9834 ctsio->kern_data_resid = 0; 9835 ctsio->kern_rel_offset = 0; 9836 ctsio->kern_sg_entries = 0; 9837 9838 /* 9839 * The control device is always connected. The disk device, on the 9840 * other hand, may not be online all the time. Need to change this 9841 * to figure out whether the disk device is actually online or not. 9842 */ 9843 if (lun != NULL) 9844 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9845 lun->be_lun->lun_type; 9846 else 9847 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9848 9849 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9850 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 9851 bl_ptr->max_cmp_write_len = 0xff; 9852 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9853 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 9854 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9855 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9856 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9857 } 9858 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9859 9860 ctsio->scsi_status = SCSI_STATUS_OK; 9861 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9862 ctsio->be_move_done = ctl_config_move_done; 9863 ctl_datamove((union ctl_io *)ctsio); 9864 9865 return (CTL_RETVAL_COMPLETE); 9866} 9867 9868static int 9869ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9870{ 9871 struct scsi_vpd_logical_block_prov *lbp_ptr; 9872 struct ctl_lun *lun; 9873 int bs; 9874 9875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9876 bs = lun->be_lun->blocksize; 9877 9878 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9879 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9880 ctsio->kern_sg_entries = 0; 9881 9882 if (sizeof(*lbp_ptr) < alloc_len) { 9883 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9884 ctsio->kern_data_len = sizeof(*lbp_ptr); 9885 ctsio->kern_total_len = sizeof(*lbp_ptr); 9886 } else { 9887 ctsio->residual = 0; 9888 ctsio->kern_data_len = alloc_len; 9889 ctsio->kern_total_len = alloc_len; 9890 } 9891 ctsio->kern_data_resid = 0; 9892 ctsio->kern_rel_offset = 0; 9893 ctsio->kern_sg_entries = 0; 9894 9895 /* 9896 * The control device is always connected. The disk device, on the 9897 * other hand, may not be online all the time. Need to change this 9898 * to figure out whether the disk device is actually online or not. 9899 */ 9900 if (lun != NULL) 9901 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9902 lun->be_lun->lun_type; 9903 else 9904 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9905 9906 lbp_ptr->page_code = SVPD_LBP; 9907 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 9908 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10; 9909 9910 ctsio->scsi_status = SCSI_STATUS_OK; 9911 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9912 ctsio->be_move_done = ctl_config_move_done; 9913 ctl_datamove((union ctl_io *)ctsio); 9914 9915 return (CTL_RETVAL_COMPLETE); 9916} 9917 9918static int 9919ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9920{ 9921 struct scsi_inquiry *cdb; 9922 struct ctl_lun *lun; 9923 int alloc_len, retval; 9924 9925 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9926 cdb = (struct scsi_inquiry *)ctsio->cdb; 9927 9928 retval = CTL_RETVAL_COMPLETE; 9929 9930 alloc_len = scsi_2btoul(cdb->length); 9931 9932 switch (cdb->page_code) { 9933 case SVPD_SUPPORTED_PAGES: 9934 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9935 break; 9936 case SVPD_UNIT_SERIAL_NUMBER: 9937 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9938 break; 9939 case SVPD_DEVICE_ID: 9940 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9941 break; 9942 case SVPD_BLOCK_LIMITS: 9943 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9944 break; 9945 case SVPD_LBP: 9946 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9947 break; 9948 default: 9949 ctl_set_invalid_field(ctsio, 9950 /*sks_valid*/ 1, 9951 /*command*/ 1, 9952 /*field*/ 2, 9953 /*bit_valid*/ 0, 9954 /*bit*/ 0); 9955 ctl_done((union ctl_io *)ctsio); 9956 retval = CTL_RETVAL_COMPLETE; 9957 break; 9958 } 9959 9960 return (retval); 9961} 9962 9963static int 9964ctl_inquiry_std(struct ctl_scsiio *ctsio) 9965{ 9966 struct scsi_inquiry_data *inq_ptr; 9967 struct scsi_inquiry *cdb; 9968 struct ctl_softc *ctl_softc; 9969 struct ctl_lun *lun; 9970 char *val; 9971 uint32_t alloc_len; 9972 int is_fc; 9973 9974 ctl_softc = control_softc; 9975 9976 /* 9977 * Figure out whether we're talking to a Fibre Channel port or not. 9978 * We treat the ioctl front end, and any SCSI adapters, as packetized 9979 * SCSI front ends. 9980 */ 9981 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type != 9982 CTL_PORT_FC) 9983 is_fc = 0; 9984 else 9985 is_fc = 1; 9986 9987 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9988 cdb = (struct scsi_inquiry *)ctsio->cdb; 9989 alloc_len = scsi_2btoul(cdb->length); 9990 9991 /* 9992 * We malloc the full inquiry data size here and fill it 9993 * in. If the user only asks for less, we'll give him 9994 * that much. 9995 */ 9996 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 9997 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 9998 ctsio->kern_sg_entries = 0; 9999 ctsio->kern_data_resid = 0; 10000 ctsio->kern_rel_offset = 0; 10001 10002 if (sizeof(*inq_ptr) < alloc_len) { 10003 ctsio->residual = alloc_len - sizeof(*inq_ptr); 10004 ctsio->kern_data_len = sizeof(*inq_ptr); 10005 ctsio->kern_total_len = sizeof(*inq_ptr); 10006 } else { 10007 ctsio->residual = 0; 10008 ctsio->kern_data_len = alloc_len; 10009 ctsio->kern_total_len = alloc_len; 10010 } 10011 10012 /* 10013 * If we have a LUN configured, report it as connected. Otherwise, 10014 * report that it is offline or no device is supported, depending 10015 * on the value of inquiry_pq_no_lun. 10016 * 10017 * According to the spec (SPC-4 r34), the peripheral qualifier 10018 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10019 * 10020 * "A peripheral device having the specified peripheral device type 10021 * is not connected to this logical unit. However, the device 10022 * server is capable of supporting the specified peripheral device 10023 * type on this logical unit." 10024 * 10025 * According to the same spec, the peripheral qualifier 10026 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10027 * 10028 * "The device server is not capable of supporting a peripheral 10029 * device on this logical unit. For this peripheral qualifier the 10030 * peripheral device type shall be set to 1Fh. All other peripheral 10031 * device type values are reserved for this peripheral qualifier." 10032 * 10033 * Given the text, it would seem that we probably want to report that 10034 * the LUN is offline here. There is no LUN connected, but we can 10035 * support a LUN at the given LUN number. 10036 * 10037 * In the real world, though, it sounds like things are a little 10038 * different: 10039 * 10040 * - Linux, when presented with a LUN with the offline peripheral 10041 * qualifier, will create an sg driver instance for it. So when 10042 * you attach it to CTL, you wind up with a ton of sg driver 10043 * instances. (One for every LUN that Linux bothered to probe.) 10044 * Linux does this despite the fact that it issues a REPORT LUNs 10045 * to LUN 0 to get the inventory of supported LUNs. 10046 * 10047 * - There is other anecdotal evidence (from Emulex folks) about 10048 * arrays that use the offline peripheral qualifier for LUNs that 10049 * are on the "passive" path in an active/passive array. 10050 * 10051 * So the solution is provide a hopefully reasonable default 10052 * (return bad/no LUN) and allow the user to change the behavior 10053 * with a tunable/sysctl variable. 10054 */ 10055 if (lun != NULL) 10056 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10057 lun->be_lun->lun_type; 10058 else if (ctl_softc->inquiry_pq_no_lun == 0) 10059 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10060 else 10061 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10062 10063 /* RMB in byte 2 is 0 */ 10064 inq_ptr->version = SCSI_REV_SPC3; 10065 10066 /* 10067 * According to SAM-3, even if a device only supports a single 10068 * level of LUN addressing, it should still set the HISUP bit: 10069 * 10070 * 4.9.1 Logical unit numbers overview 10071 * 10072 * All logical unit number formats described in this standard are 10073 * hierarchical in structure even when only a single level in that 10074 * hierarchy is used. The HISUP bit shall be set to one in the 10075 * standard INQUIRY data (see SPC-2) when any logical unit number 10076 * format described in this standard is used. Non-hierarchical 10077 * formats are outside the scope of this standard. 10078 * 10079 * Therefore we set the HiSup bit here. 10080 * 10081 * The reponse format is 2, per SPC-3. 10082 */ 10083 inq_ptr->response_format = SID_HiSup | 2; 10084 10085 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 10086 CTL_DEBUG_PRINT(("additional_length = %d\n", 10087 inq_ptr->additional_length)); 10088 10089 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT; 10090 /* 16 bit addressing */ 10091 if (is_fc == 0) 10092 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10093 /* XXX set the SID_MultiP bit here if we're actually going to 10094 respond on multiple ports */ 10095 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10096 10097 /* 16 bit data bus, synchronous transfers */ 10098 /* XXX these flags don't apply for FC */ 10099 if (is_fc == 0) 10100 inq_ptr->flags = SID_WBus16 | SID_Sync; 10101 /* 10102 * XXX KDM do we want to support tagged queueing on the control 10103 * device at all? 10104 */ 10105 if ((lun == NULL) 10106 || (lun->be_lun->lun_type != T_PROCESSOR)) 10107 inq_ptr->flags |= SID_CmdQue; 10108 /* 10109 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10110 * We have 8 bytes for the vendor name, and 16 bytes for the device 10111 * name and 4 bytes for the revision. 10112 */ 10113 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10114 "vendor")) == NULL) { 10115 strcpy(inq_ptr->vendor, CTL_VENDOR); 10116 } else { 10117 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10118 strncpy(inq_ptr->vendor, val, 10119 min(sizeof(inq_ptr->vendor), strlen(val))); 10120 } 10121 if (lun == NULL) { 10122 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10123 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10124 switch (lun->be_lun->lun_type) { 10125 case T_DIRECT: 10126 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 10127 break; 10128 case T_PROCESSOR: 10129 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 10130 break; 10131 default: 10132 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 10133 break; 10134 } 10135 } else { 10136 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10137 strncpy(inq_ptr->product, val, 10138 min(sizeof(inq_ptr->product), strlen(val))); 10139 } 10140 10141 /* 10142 * XXX make this a macro somewhere so it automatically gets 10143 * incremented when we make changes. 10144 */ 10145 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10146 "revision")) == NULL) { 10147 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10148 } else { 10149 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10150 strncpy(inq_ptr->revision, val, 10151 min(sizeof(inq_ptr->revision), strlen(val))); 10152 } 10153 10154 /* 10155 * For parallel SCSI, we support double transition and single 10156 * transition clocking. We also support QAS (Quick Arbitration 10157 * and Selection) and Information Unit transfers on both the 10158 * control and array devices. 10159 */ 10160 if (is_fc == 0) 10161 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10162 SID_SPI_IUS; 10163 10164 /* SAM-3 */ 10165 scsi_ulto2b(0x0060, inq_ptr->version1); 10166 /* SPC-3 (no version claimed) XXX should we claim a version? */ 10167 scsi_ulto2b(0x0300, inq_ptr->version2); 10168 if (is_fc) { 10169 /* FCP-2 ANSI INCITS.350:2003 */ 10170 scsi_ulto2b(0x0917, inq_ptr->version3); 10171 } else { 10172 /* SPI-4 ANSI INCITS.362:200x */ 10173 scsi_ulto2b(0x0B56, inq_ptr->version3); 10174 } 10175 10176 if (lun == NULL) { 10177 /* SBC-2 (no version claimed) XXX should we claim a version? */ 10178 scsi_ulto2b(0x0320, inq_ptr->version4); 10179 } else { 10180 switch (lun->be_lun->lun_type) { 10181 case T_DIRECT: 10182 /* 10183 * SBC-2 (no version claimed) XXX should we claim a 10184 * version? 10185 */ 10186 scsi_ulto2b(0x0320, inq_ptr->version4); 10187 break; 10188 case T_PROCESSOR: 10189 default: 10190 break; 10191 } 10192 } 10193 10194 ctsio->scsi_status = SCSI_STATUS_OK; 10195 if (ctsio->kern_data_len > 0) { 10196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10197 ctsio->be_move_done = ctl_config_move_done; 10198 ctl_datamove((union ctl_io *)ctsio); 10199 } else { 10200 ctsio->io_hdr.status = CTL_SUCCESS; 10201 ctl_done((union ctl_io *)ctsio); 10202 } 10203 10204 return (CTL_RETVAL_COMPLETE); 10205} 10206 10207int 10208ctl_inquiry(struct ctl_scsiio *ctsio) 10209{ 10210 struct scsi_inquiry *cdb; 10211 int retval; 10212 10213 cdb = (struct scsi_inquiry *)ctsio->cdb; 10214 10215 retval = 0; 10216 10217 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10218 10219 /* 10220 * Right now, we don't support the CmdDt inquiry information. 10221 * This would be nice to support in the future. When we do 10222 * support it, we should change this test so that it checks to make 10223 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10224 */ 10225#ifdef notyet 10226 if (((cdb->byte2 & SI_EVPD) 10227 && (cdb->byte2 & SI_CMDDT))) 10228#endif 10229 if (cdb->byte2 & SI_CMDDT) { 10230 /* 10231 * Point to the SI_CMDDT bit. We might change this 10232 * when we support SI_CMDDT, but since both bits would be 10233 * "wrong", this should probably just stay as-is then. 10234 */ 10235 ctl_set_invalid_field(ctsio, 10236 /*sks_valid*/ 1, 10237 /*command*/ 1, 10238 /*field*/ 1, 10239 /*bit_valid*/ 1, 10240 /*bit*/ 1); 10241 ctl_done((union ctl_io *)ctsio); 10242 return (CTL_RETVAL_COMPLETE); 10243 } 10244 if (cdb->byte2 & SI_EVPD) 10245 retval = ctl_inquiry_evpd(ctsio); 10246#ifdef notyet 10247 else if (cdb->byte2 & SI_CMDDT) 10248 retval = ctl_inquiry_cmddt(ctsio); 10249#endif 10250 else 10251 retval = ctl_inquiry_std(ctsio); 10252 10253 return (retval); 10254} 10255 10256/* 10257 * For known CDB types, parse the LBA and length. 10258 */ 10259static int 10260ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10261{ 10262 if (io->io_hdr.io_type != CTL_IO_SCSI) 10263 return (1); 10264 10265 switch (io->scsiio.cdb[0]) { 10266 case COMPARE_AND_WRITE: { 10267 struct scsi_compare_and_write *cdb; 10268 10269 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10270 10271 *lba = scsi_8btou64(cdb->addr); 10272 *len = cdb->length; 10273 break; 10274 } 10275 case READ_6: 10276 case WRITE_6: { 10277 struct scsi_rw_6 *cdb; 10278 10279 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10280 10281 *lba = scsi_3btoul(cdb->addr); 10282 /* only 5 bits are valid in the most significant address byte */ 10283 *lba &= 0x1fffff; 10284 *len = cdb->length; 10285 break; 10286 } 10287 case READ_10: 10288 case WRITE_10: { 10289 struct scsi_rw_10 *cdb; 10290 10291 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10292 10293 *lba = scsi_4btoul(cdb->addr); 10294 *len = scsi_2btoul(cdb->length); 10295 break; 10296 } 10297 case WRITE_VERIFY_10: { 10298 struct scsi_write_verify_10 *cdb; 10299 10300 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10301 10302 *lba = scsi_4btoul(cdb->addr); 10303 *len = scsi_2btoul(cdb->length); 10304 break; 10305 } 10306 case READ_12: 10307 case WRITE_12: { 10308 struct scsi_rw_12 *cdb; 10309 10310 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10311 10312 *lba = scsi_4btoul(cdb->addr); 10313 *len = scsi_4btoul(cdb->length); 10314 break; 10315 } 10316 case WRITE_VERIFY_12: { 10317 struct scsi_write_verify_12 *cdb; 10318 10319 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10320 10321 *lba = scsi_4btoul(cdb->addr); 10322 *len = scsi_4btoul(cdb->length); 10323 break; 10324 } 10325 case READ_16: 10326 case WRITE_16: { 10327 struct scsi_rw_16 *cdb; 10328 10329 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10330 10331 *lba = scsi_8btou64(cdb->addr); 10332 *len = scsi_4btoul(cdb->length); 10333 break; 10334 } 10335 case WRITE_VERIFY_16: { 10336 struct scsi_write_verify_16 *cdb; 10337 10338 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10339 10340 10341 *lba = scsi_8btou64(cdb->addr); 10342 *len = scsi_4btoul(cdb->length); 10343 break; 10344 } 10345 case WRITE_SAME_10: { 10346 struct scsi_write_same_10 *cdb; 10347 10348 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10349 10350 *lba = scsi_4btoul(cdb->addr); 10351 *len = scsi_2btoul(cdb->length); 10352 break; 10353 } 10354 case WRITE_SAME_16: { 10355 struct scsi_write_same_16 *cdb; 10356 10357 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10358 10359 *lba = scsi_8btou64(cdb->addr); 10360 *len = scsi_4btoul(cdb->length); 10361 break; 10362 } 10363 case VERIFY_10: { 10364 struct scsi_verify_10 *cdb; 10365 10366 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10367 10368 *lba = scsi_4btoul(cdb->addr); 10369 *len = scsi_2btoul(cdb->length); 10370 break; 10371 } 10372 case VERIFY_12: { 10373 struct scsi_verify_12 *cdb; 10374 10375 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10376 10377 *lba = scsi_4btoul(cdb->addr); 10378 *len = scsi_4btoul(cdb->length); 10379 break; 10380 } 10381 case VERIFY_16: { 10382 struct scsi_verify_16 *cdb; 10383 10384 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10385 10386 *lba = scsi_8btou64(cdb->addr); 10387 *len = scsi_4btoul(cdb->length); 10388 break; 10389 } 10390 default: 10391 return (1); 10392 break; /* NOTREACHED */ 10393 } 10394 10395 return (0); 10396} 10397 10398static ctl_action 10399ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10400{ 10401 uint64_t endlba1, endlba2; 10402 10403 endlba1 = lba1 + len1 - 1; 10404 endlba2 = lba2 + len2 - 1; 10405 10406 if ((endlba1 < lba2) 10407 || (endlba2 < lba1)) 10408 return (CTL_ACTION_PASS); 10409 else 10410 return (CTL_ACTION_BLOCK); 10411} 10412 10413static ctl_action 10414ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10415{ 10416 uint64_t lba1, lba2; 10417 uint32_t len1, len2; 10418 int retval; 10419 10420 retval = ctl_get_lba_len(io1, &lba1, &len1); 10421 if (retval != 0) 10422 return (CTL_ACTION_ERROR); 10423 10424 retval = ctl_get_lba_len(io2, &lba2, &len2); 10425 if (retval != 0) 10426 return (CTL_ACTION_ERROR); 10427 10428 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10429} 10430 10431static ctl_action 10432ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10433{ 10434 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10435 ctl_serialize_action *serialize_row; 10436 10437 /* 10438 * The initiator attempted multiple untagged commands at the same 10439 * time. Can't do that. 10440 */ 10441 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10442 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10443 && ((pending_io->io_hdr.nexus.targ_port == 10444 ooa_io->io_hdr.nexus.targ_port) 10445 && (pending_io->io_hdr.nexus.initid.id == 10446 ooa_io->io_hdr.nexus.initid.id)) 10447 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10448 return (CTL_ACTION_OVERLAP); 10449 10450 /* 10451 * The initiator attempted to send multiple tagged commands with 10452 * the same ID. (It's fine if different initiators have the same 10453 * tag ID.) 10454 * 10455 * Even if all of those conditions are true, we don't kill the I/O 10456 * if the command ahead of us has been aborted. We won't end up 10457 * sending it to the FETD, and it's perfectly legal to resend a 10458 * command with the same tag number as long as the previous 10459 * instance of this tag number has been aborted somehow. 10460 */ 10461 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10462 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10463 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10464 && ((pending_io->io_hdr.nexus.targ_port == 10465 ooa_io->io_hdr.nexus.targ_port) 10466 && (pending_io->io_hdr.nexus.initid.id == 10467 ooa_io->io_hdr.nexus.initid.id)) 10468 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10469 return (CTL_ACTION_OVERLAP_TAG); 10470 10471 /* 10472 * If we get a head of queue tag, SAM-3 says that we should 10473 * immediately execute it. 10474 * 10475 * What happens if this command would normally block for some other 10476 * reason? e.g. a request sense with a head of queue tag 10477 * immediately after a write. Normally that would block, but this 10478 * will result in its getting executed immediately... 10479 * 10480 * We currently return "pass" instead of "skip", so we'll end up 10481 * going through the rest of the queue to check for overlapped tags. 10482 * 10483 * XXX KDM check for other types of blockage first?? 10484 */ 10485 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10486 return (CTL_ACTION_PASS); 10487 10488 /* 10489 * Ordered tags have to block until all items ahead of them 10490 * have completed. If we get called with an ordered tag, we always 10491 * block, if something else is ahead of us in the queue. 10492 */ 10493 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10494 return (CTL_ACTION_BLOCK); 10495 10496 /* 10497 * Simple tags get blocked until all head of queue and ordered tags 10498 * ahead of them have completed. I'm lumping untagged commands in 10499 * with simple tags here. XXX KDM is that the right thing to do? 10500 */ 10501 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10502 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10503 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10504 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10505 return (CTL_ACTION_BLOCK); 10506 10507 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10508 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10509 10510 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10511 10512 switch (serialize_row[pending_entry->seridx]) { 10513 case CTL_SER_BLOCK: 10514 return (CTL_ACTION_BLOCK); 10515 break; /* NOTREACHED */ 10516 case CTL_SER_EXTENT: 10517 return (ctl_extent_check(pending_io, ooa_io)); 10518 break; /* NOTREACHED */ 10519 case CTL_SER_PASS: 10520 return (CTL_ACTION_PASS); 10521 break; /* NOTREACHED */ 10522 case CTL_SER_SKIP: 10523 return (CTL_ACTION_SKIP); 10524 break; 10525 default: 10526 panic("invalid serialization value %d", 10527 serialize_row[pending_entry->seridx]); 10528 break; /* NOTREACHED */ 10529 } 10530 10531 return (CTL_ACTION_ERROR); 10532} 10533 10534/* 10535 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10536 * Assumptions: 10537 * - pending_io is generally either incoming, or on the blocked queue 10538 * - starting I/O is the I/O we want to start the check with. 10539 */ 10540static ctl_action 10541ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10542 union ctl_io *starting_io) 10543{ 10544 union ctl_io *ooa_io; 10545 ctl_action action; 10546 10547 mtx_assert(&lun->lun_lock, MA_OWNED); 10548 10549 /* 10550 * Run back along the OOA queue, starting with the current 10551 * blocked I/O and going through every I/O before it on the 10552 * queue. If starting_io is NULL, we'll just end up returning 10553 * CTL_ACTION_PASS. 10554 */ 10555 for (ooa_io = starting_io; ooa_io != NULL; 10556 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10557 ooa_links)){ 10558 10559 /* 10560 * This routine just checks to see whether 10561 * cur_blocked is blocked by ooa_io, which is ahead 10562 * of it in the queue. It doesn't queue/dequeue 10563 * cur_blocked. 10564 */ 10565 action = ctl_check_for_blockage(pending_io, ooa_io); 10566 switch (action) { 10567 case CTL_ACTION_BLOCK: 10568 case CTL_ACTION_OVERLAP: 10569 case CTL_ACTION_OVERLAP_TAG: 10570 case CTL_ACTION_SKIP: 10571 case CTL_ACTION_ERROR: 10572 return (action); 10573 break; /* NOTREACHED */ 10574 case CTL_ACTION_PASS: 10575 break; 10576 default: 10577 panic("invalid action %d", action); 10578 break; /* NOTREACHED */ 10579 } 10580 } 10581 10582 return (CTL_ACTION_PASS); 10583} 10584 10585/* 10586 * Assumptions: 10587 * - An I/O has just completed, and has been removed from the per-LUN OOA 10588 * queue, so some items on the blocked queue may now be unblocked. 10589 */ 10590static int 10591ctl_check_blocked(struct ctl_lun *lun) 10592{ 10593 union ctl_io *cur_blocked, *next_blocked; 10594 10595 mtx_assert(&lun->lun_lock, MA_OWNED); 10596 10597 /* 10598 * Run forward from the head of the blocked queue, checking each 10599 * entry against the I/Os prior to it on the OOA queue to see if 10600 * there is still any blockage. 10601 * 10602 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10603 * with our removing a variable on it while it is traversing the 10604 * list. 10605 */ 10606 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10607 cur_blocked != NULL; cur_blocked = next_blocked) { 10608 union ctl_io *prev_ooa; 10609 ctl_action action; 10610 10611 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10612 blocked_links); 10613 10614 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10615 ctl_ooaq, ooa_links); 10616 10617 /* 10618 * If cur_blocked happens to be the first item in the OOA 10619 * queue now, prev_ooa will be NULL, and the action 10620 * returned will just be CTL_ACTION_PASS. 10621 */ 10622 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10623 10624 switch (action) { 10625 case CTL_ACTION_BLOCK: 10626 /* Nothing to do here, still blocked */ 10627 break; 10628 case CTL_ACTION_OVERLAP: 10629 case CTL_ACTION_OVERLAP_TAG: 10630 /* 10631 * This shouldn't happen! In theory we've already 10632 * checked this command for overlap... 10633 */ 10634 break; 10635 case CTL_ACTION_PASS: 10636 case CTL_ACTION_SKIP: { 10637 struct ctl_softc *softc; 10638 const struct ctl_cmd_entry *entry; 10639 uint32_t initidx; 10640 int isc_retval; 10641 10642 /* 10643 * The skip case shouldn't happen, this transaction 10644 * should have never made it onto the blocked queue. 10645 */ 10646 /* 10647 * This I/O is no longer blocked, we can remove it 10648 * from the blocked queue. Since this is a TAILQ 10649 * (doubly linked list), we can do O(1) removals 10650 * from any place on the list. 10651 */ 10652 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10653 blocked_links); 10654 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10655 10656 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10657 /* 10658 * Need to send IO back to original side to 10659 * run 10660 */ 10661 union ctl_ha_msg msg_info; 10662 10663 msg_info.hdr.original_sc = 10664 cur_blocked->io_hdr.original_sc; 10665 msg_info.hdr.serializing_sc = cur_blocked; 10666 msg_info.hdr.msg_type = CTL_MSG_R2R; 10667 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10668 &msg_info, sizeof(msg_info), 0)) > 10669 CTL_HA_STATUS_SUCCESS) { 10670 printf("CTL:Check Blocked error from " 10671 "ctl_ha_msg_send %d\n", 10672 isc_retval); 10673 } 10674 break; 10675 } 10676 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 10677 softc = control_softc; 10678 10679 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 10680 10681 /* 10682 * Check this I/O for LUN state changes that may 10683 * have happened while this command was blocked. 10684 * The LUN state may have been changed by a command 10685 * ahead of us in the queue, so we need to re-check 10686 * for any states that can be caused by SCSI 10687 * commands. 10688 */ 10689 if (ctl_scsiio_lun_check(softc, lun, entry, 10690 &cur_blocked->scsiio) == 0) { 10691 cur_blocked->io_hdr.flags |= 10692 CTL_FLAG_IS_WAS_ON_RTR; 10693 ctl_enqueue_rtr(cur_blocked); 10694 } else 10695 ctl_done(cur_blocked); 10696 break; 10697 } 10698 default: 10699 /* 10700 * This probably shouldn't happen -- we shouldn't 10701 * get CTL_ACTION_ERROR, or anything else. 10702 */ 10703 break; 10704 } 10705 } 10706 10707 return (CTL_RETVAL_COMPLETE); 10708} 10709 10710/* 10711 * This routine (with one exception) checks LUN flags that can be set by 10712 * commands ahead of us in the OOA queue. These flags have to be checked 10713 * when a command initially comes in, and when we pull a command off the 10714 * blocked queue and are preparing to execute it. The reason we have to 10715 * check these flags for commands on the blocked queue is that the LUN 10716 * state may have been changed by a command ahead of us while we're on the 10717 * blocked queue. 10718 * 10719 * Ordering is somewhat important with these checks, so please pay 10720 * careful attention to the placement of any new checks. 10721 */ 10722static int 10723ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 10724 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10725{ 10726 int retval; 10727 10728 retval = 0; 10729 10730 mtx_assert(&lun->lun_lock, MA_OWNED); 10731 10732 /* 10733 * If this shelf is a secondary shelf controller, we have to reject 10734 * any media access commands. 10735 */ 10736#if 0 10737 /* No longer needed for HA */ 10738 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 10739 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 10740 ctl_set_lun_standby(ctsio); 10741 retval = 1; 10742 goto bailout; 10743 } 10744#endif 10745 10746 /* 10747 * Check for a reservation conflict. If this command isn't allowed 10748 * even on reserved LUNs, and if this initiator isn't the one who 10749 * reserved us, reject the command with a reservation conflict. 10750 */ 10751 if ((lun->flags & CTL_LUN_RESERVED) 10752 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10753 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 10754 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 10755 || (ctsio->io_hdr.nexus.targ_target.id != 10756 lun->rsv_nexus.targ_target.id)) { 10757 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10758 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10759 retval = 1; 10760 goto bailout; 10761 } 10762 } 10763 10764 if ( (lun->flags & CTL_LUN_PR_RESERVED) 10765 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 10766 uint32_t residx; 10767 10768 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 10769 /* 10770 * if we aren't registered or it's a res holder type 10771 * reservation and this isn't the res holder then set a 10772 * conflict. 10773 * NOTE: Commands which might be allowed on write exclusive 10774 * type reservations are checked in the particular command 10775 * for a conflict. Read and SSU are the only ones. 10776 */ 10777 if (!lun->per_res[residx].registered 10778 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10779 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10780 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10781 retval = 1; 10782 goto bailout; 10783 } 10784 10785 } 10786 10787 if ((lun->flags & CTL_LUN_OFFLINE) 10788 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 10789 ctl_set_lun_not_ready(ctsio); 10790 retval = 1; 10791 goto bailout; 10792 } 10793 10794 /* 10795 * If the LUN is stopped, see if this particular command is allowed 10796 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 10797 */ 10798 if ((lun->flags & CTL_LUN_STOPPED) 10799 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10800 /* "Logical unit not ready, initializing cmd. required" */ 10801 ctl_set_lun_stopped(ctsio); 10802 retval = 1; 10803 goto bailout; 10804 } 10805 10806 if ((lun->flags & CTL_LUN_INOPERABLE) 10807 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10808 /* "Medium format corrupted" */ 10809 ctl_set_medium_format_corrupted(ctsio); 10810 retval = 1; 10811 goto bailout; 10812 } 10813 10814bailout: 10815 return (retval); 10816 10817} 10818 10819static void 10820ctl_failover_io(union ctl_io *io, int have_lock) 10821{ 10822 ctl_set_busy(&io->scsiio); 10823 ctl_done(io); 10824} 10825 10826static void 10827ctl_failover(void) 10828{ 10829 struct ctl_lun *lun; 10830 struct ctl_softc *ctl_softc; 10831 union ctl_io *next_io, *pending_io; 10832 union ctl_io *io; 10833 int lun_idx; 10834 int i; 10835 10836 ctl_softc = control_softc; 10837 10838 mtx_lock(&ctl_softc->ctl_lock); 10839 /* 10840 * Remove any cmds from the other SC from the rtr queue. These 10841 * will obviously only be for LUNs for which we're the primary. 10842 * We can't send status or get/send data for these commands. 10843 * Since they haven't been executed yet, we can just remove them. 10844 * We'll either abort them or delete them below, depending on 10845 * which HA mode we're in. 10846 */ 10847#ifdef notyet 10848 mtx_lock(&ctl_softc->queue_lock); 10849 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 10850 io != NULL; io = next_io) { 10851 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 10852 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10853 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 10854 ctl_io_hdr, links); 10855 } 10856 mtx_unlock(&ctl_softc->queue_lock); 10857#endif 10858 10859 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 10860 lun = ctl_softc->ctl_luns[lun_idx]; 10861 if (lun==NULL) 10862 continue; 10863 10864 /* 10865 * Processor LUNs are primary on both sides. 10866 * XXX will this always be true? 10867 */ 10868 if (lun->be_lun->lun_type == T_PROCESSOR) 10869 continue; 10870 10871 if ((lun->flags & CTL_LUN_PRIMARY_SC) 10872 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10873 printf("FAILOVER: primary lun %d\n", lun_idx); 10874 /* 10875 * Remove all commands from the other SC. First from the 10876 * blocked queue then from the ooa queue. Once we have 10877 * removed them. Call ctl_check_blocked to see if there 10878 * is anything that can run. 10879 */ 10880 for (io = (union ctl_io *)TAILQ_FIRST( 10881 &lun->blocked_queue); io != NULL; io = next_io) { 10882 10883 next_io = (union ctl_io *)TAILQ_NEXT( 10884 &io->io_hdr, blocked_links); 10885 10886 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10887 TAILQ_REMOVE(&lun->blocked_queue, 10888 &io->io_hdr,blocked_links); 10889 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10890 TAILQ_REMOVE(&lun->ooa_queue, 10891 &io->io_hdr, ooa_links); 10892 10893 ctl_free_io(io); 10894 } 10895 } 10896 10897 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10898 io != NULL; io = next_io) { 10899 10900 next_io = (union ctl_io *)TAILQ_NEXT( 10901 &io->io_hdr, ooa_links); 10902 10903 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10904 10905 TAILQ_REMOVE(&lun->ooa_queue, 10906 &io->io_hdr, 10907 ooa_links); 10908 10909 ctl_free_io(io); 10910 } 10911 } 10912 ctl_check_blocked(lun); 10913 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 10914 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10915 10916 printf("FAILOVER: primary lun %d\n", lun_idx); 10917 /* 10918 * Abort all commands from the other SC. We can't 10919 * send status back for them now. These should get 10920 * cleaned up when they are completed or come out 10921 * for a datamove operation. 10922 */ 10923 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10924 io != NULL; io = next_io) { 10925 next_io = (union ctl_io *)TAILQ_NEXT( 10926 &io->io_hdr, ooa_links); 10927 10928 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10929 io->io_hdr.flags |= CTL_FLAG_ABORT; 10930 } 10931 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10932 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10933 10934 printf("FAILOVER: secondary lun %d\n", lun_idx); 10935 10936 lun->flags |= CTL_LUN_PRIMARY_SC; 10937 10938 /* 10939 * We send all I/O that was sent to this controller 10940 * and redirected to the other side back with 10941 * busy status, and have the initiator retry it. 10942 * Figuring out how much data has been transferred, 10943 * etc. and picking up where we left off would be 10944 * very tricky. 10945 * 10946 * XXX KDM need to remove I/O from the blocked 10947 * queue as well! 10948 */ 10949 for (pending_io = (union ctl_io *)TAILQ_FIRST( 10950 &lun->ooa_queue); pending_io != NULL; 10951 pending_io = next_io) { 10952 10953 next_io = (union ctl_io *)TAILQ_NEXT( 10954 &pending_io->io_hdr, ooa_links); 10955 10956 pending_io->io_hdr.flags &= 10957 ~CTL_FLAG_SENT_2OTHER_SC; 10958 10959 if (pending_io->io_hdr.flags & 10960 CTL_FLAG_IO_ACTIVE) { 10961 pending_io->io_hdr.flags |= 10962 CTL_FLAG_FAILOVER; 10963 } else { 10964 ctl_set_busy(&pending_io->scsiio); 10965 ctl_done(pending_io); 10966 } 10967 } 10968 10969 /* 10970 * Build Unit Attention 10971 */ 10972 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10973 lun->pending_sense[i].ua_pending |= 10974 CTL_UA_ASYM_ACC_CHANGE; 10975 } 10976 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10977 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10978 printf("FAILOVER: secondary lun %d\n", lun_idx); 10979 /* 10980 * if the first io on the OOA is not on the RtR queue 10981 * add it. 10982 */ 10983 lun->flags |= CTL_LUN_PRIMARY_SC; 10984 10985 pending_io = (union ctl_io *)TAILQ_FIRST( 10986 &lun->ooa_queue); 10987 if (pending_io==NULL) { 10988 printf("Nothing on OOA queue\n"); 10989 continue; 10990 } 10991 10992 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10993 if ((pending_io->io_hdr.flags & 10994 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 10995 pending_io->io_hdr.flags |= 10996 CTL_FLAG_IS_WAS_ON_RTR; 10997 ctl_enqueue_rtr(pending_io); 10998 } 10999#if 0 11000 else 11001 { 11002 printf("Tag 0x%04x is running\n", 11003 pending_io->scsiio.tag_num); 11004 } 11005#endif 11006 11007 next_io = (union ctl_io *)TAILQ_NEXT( 11008 &pending_io->io_hdr, ooa_links); 11009 for (pending_io=next_io; pending_io != NULL; 11010 pending_io = next_io) { 11011 pending_io->io_hdr.flags &= 11012 ~CTL_FLAG_SENT_2OTHER_SC; 11013 next_io = (union ctl_io *)TAILQ_NEXT( 11014 &pending_io->io_hdr, ooa_links); 11015 if (pending_io->io_hdr.flags & 11016 CTL_FLAG_IS_WAS_ON_RTR) { 11017#if 0 11018 printf("Tag 0x%04x is running\n", 11019 pending_io->scsiio.tag_num); 11020#endif 11021 continue; 11022 } 11023 11024 switch (ctl_check_ooa(lun, pending_io, 11025 (union ctl_io *)TAILQ_PREV( 11026 &pending_io->io_hdr, ctl_ooaq, 11027 ooa_links))) { 11028 11029 case CTL_ACTION_BLOCK: 11030 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11031 &pending_io->io_hdr, 11032 blocked_links); 11033 pending_io->io_hdr.flags |= 11034 CTL_FLAG_BLOCKED; 11035 break; 11036 case CTL_ACTION_PASS: 11037 case CTL_ACTION_SKIP: 11038 pending_io->io_hdr.flags |= 11039 CTL_FLAG_IS_WAS_ON_RTR; 11040 ctl_enqueue_rtr(pending_io); 11041 break; 11042 case CTL_ACTION_OVERLAP: 11043 ctl_set_overlapped_cmd( 11044 (struct ctl_scsiio *)pending_io); 11045 ctl_done(pending_io); 11046 break; 11047 case CTL_ACTION_OVERLAP_TAG: 11048 ctl_set_overlapped_tag( 11049 (struct ctl_scsiio *)pending_io, 11050 pending_io->scsiio.tag_num & 0xff); 11051 ctl_done(pending_io); 11052 break; 11053 case CTL_ACTION_ERROR: 11054 default: 11055 ctl_set_internal_failure( 11056 (struct ctl_scsiio *)pending_io, 11057 0, // sks_valid 11058 0); //retry count 11059 ctl_done(pending_io); 11060 break; 11061 } 11062 } 11063 11064 /* 11065 * Build Unit Attention 11066 */ 11067 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11068 lun->pending_sense[i].ua_pending |= 11069 CTL_UA_ASYM_ACC_CHANGE; 11070 } 11071 } else { 11072 panic("Unhandled HA mode failover, LUN flags = %#x, " 11073 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11074 } 11075 } 11076 ctl_pause_rtr = 0; 11077 mtx_unlock(&ctl_softc->ctl_lock); 11078} 11079 11080static int 11081ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11082{ 11083 struct ctl_lun *lun; 11084 const struct ctl_cmd_entry *entry; 11085 uint32_t initidx, targ_lun; 11086 int retval; 11087 11088 retval = 0; 11089 11090 lun = NULL; 11091 11092 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11093 if ((targ_lun < CTL_MAX_LUNS) 11094 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11095 lun = ctl_softc->ctl_luns[targ_lun]; 11096 /* 11097 * If the LUN is invalid, pretend that it doesn't exist. 11098 * It will go away as soon as all pending I/O has been 11099 * completed. 11100 */ 11101 if (lun->flags & CTL_LUN_DISABLED) { 11102 lun = NULL; 11103 } else { 11104 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11105 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11106 lun->be_lun; 11107 if (lun->be_lun->lun_type == T_PROCESSOR) { 11108 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11109 } 11110 11111 /* 11112 * Every I/O goes into the OOA queue for a 11113 * particular LUN, and stays there until completion. 11114 */ 11115 mtx_lock(&lun->lun_lock); 11116 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11117 ooa_links); 11118 } 11119 } else { 11120 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11121 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11122 } 11123 11124 /* Get command entry and return error if it is unsuppotyed. */ 11125 entry = ctl_validate_command(ctsio); 11126 if (entry == NULL) { 11127 if (lun) 11128 mtx_unlock(&lun->lun_lock); 11129 return (retval); 11130 } 11131 11132 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11133 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11134 11135 /* 11136 * Check to see whether we can send this command to LUNs that don't 11137 * exist. This should pretty much only be the case for inquiry 11138 * and request sense. Further checks, below, really require having 11139 * a LUN, so we can't really check the command anymore. Just put 11140 * it on the rtr queue. 11141 */ 11142 if (lun == NULL) { 11143 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11144 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11145 ctl_enqueue_rtr((union ctl_io *)ctsio); 11146 return (retval); 11147 } 11148 11149 ctl_set_unsupported_lun(ctsio); 11150 ctl_done((union ctl_io *)ctsio); 11151 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11152 return (retval); 11153 } else { 11154 /* 11155 * Make sure we support this particular command on this LUN. 11156 * e.g., we don't support writes to the control LUN. 11157 */ 11158 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11159 mtx_unlock(&lun->lun_lock); 11160 ctl_set_invalid_opcode(ctsio); 11161 ctl_done((union ctl_io *)ctsio); 11162 return (retval); 11163 } 11164 } 11165 11166 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11167 11168 /* 11169 * If we've got a request sense, it'll clear the contingent 11170 * allegiance condition. Otherwise, if we have a CA condition for 11171 * this initiator, clear it, because it sent down a command other 11172 * than request sense. 11173 */ 11174 if ((ctsio->cdb[0] != REQUEST_SENSE) 11175 && (ctl_is_set(lun->have_ca, initidx))) 11176 ctl_clear_mask(lun->have_ca, initidx); 11177 11178 /* 11179 * If the command has this flag set, it handles its own unit 11180 * attention reporting, we shouldn't do anything. Otherwise we 11181 * check for any pending unit attentions, and send them back to the 11182 * initiator. We only do this when a command initially comes in, 11183 * not when we pull it off the blocked queue. 11184 * 11185 * According to SAM-3, section 5.3.2, the order that things get 11186 * presented back to the host is basically unit attentions caused 11187 * by some sort of reset event, busy status, reservation conflicts 11188 * or task set full, and finally any other status. 11189 * 11190 * One issue here is that some of the unit attentions we report 11191 * don't fall into the "reset" category (e.g. "reported luns data 11192 * has changed"). So reporting it here, before the reservation 11193 * check, may be technically wrong. I guess the only thing to do 11194 * would be to check for and report the reset events here, and then 11195 * check for the other unit attention types after we check for a 11196 * reservation conflict. 11197 * 11198 * XXX KDM need to fix this 11199 */ 11200 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11201 ctl_ua_type ua_type; 11202 11203 ua_type = lun->pending_sense[initidx].ua_pending; 11204 if (ua_type != CTL_UA_NONE) { 11205 scsi_sense_data_type sense_format; 11206 11207 if (lun != NULL) 11208 sense_format = (lun->flags & 11209 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11210 SSD_TYPE_FIXED; 11211 else 11212 sense_format = SSD_TYPE_FIXED; 11213 11214 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11215 sense_format); 11216 if (ua_type != CTL_UA_NONE) { 11217 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11218 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11219 CTL_AUTOSENSE; 11220 ctsio->sense_len = SSD_FULL_SIZE; 11221 lun->pending_sense[initidx].ua_pending &= 11222 ~ua_type; 11223 mtx_unlock(&lun->lun_lock); 11224 ctl_done((union ctl_io *)ctsio); 11225 return (retval); 11226 } 11227 } 11228 } 11229 11230 11231 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11232 mtx_unlock(&lun->lun_lock); 11233 ctl_done((union ctl_io *)ctsio); 11234 return (retval); 11235 } 11236 11237 /* 11238 * XXX CHD this is where we want to send IO to other side if 11239 * this LUN is secondary on this SC. We will need to make a copy 11240 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11241 * the copy we send as FROM_OTHER. 11242 * We also need to stuff the address of the original IO so we can 11243 * find it easily. Something similar will need be done on the other 11244 * side so when we are done we can find the copy. 11245 */ 11246 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11247 union ctl_ha_msg msg_info; 11248 int isc_retval; 11249 11250 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11251 11252 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11253 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11254#if 0 11255 printf("1. ctsio %p\n", ctsio); 11256#endif 11257 msg_info.hdr.serializing_sc = NULL; 11258 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11259 msg_info.scsi.tag_num = ctsio->tag_num; 11260 msg_info.scsi.tag_type = ctsio->tag_type; 11261 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11262 11263 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11264 11265 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11266 (void *)&msg_info, sizeof(msg_info), 0)) > 11267 CTL_HA_STATUS_SUCCESS) { 11268 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11269 isc_retval); 11270 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11271 } else { 11272#if 0 11273 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11274#endif 11275 } 11276 11277 /* 11278 * XXX KDM this I/O is off the incoming queue, but hasn't 11279 * been inserted on any other queue. We may need to come 11280 * up with a holding queue while we wait for serialization 11281 * so that we have an idea of what we're waiting for from 11282 * the other side. 11283 */ 11284 mtx_unlock(&lun->lun_lock); 11285 return (retval); 11286 } 11287 11288 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11289 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11290 ctl_ooaq, ooa_links))) { 11291 case CTL_ACTION_BLOCK: 11292 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11293 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11294 blocked_links); 11295 mtx_unlock(&lun->lun_lock); 11296 return (retval); 11297 case CTL_ACTION_PASS: 11298 case CTL_ACTION_SKIP: 11299 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11300 mtx_unlock(&lun->lun_lock); 11301 ctl_enqueue_rtr((union ctl_io *)ctsio); 11302 break; 11303 case CTL_ACTION_OVERLAP: 11304 mtx_unlock(&lun->lun_lock); 11305 ctl_set_overlapped_cmd(ctsio); 11306 ctl_done((union ctl_io *)ctsio); 11307 break; 11308 case CTL_ACTION_OVERLAP_TAG: 11309 mtx_unlock(&lun->lun_lock); 11310 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11311 ctl_done((union ctl_io *)ctsio); 11312 break; 11313 case CTL_ACTION_ERROR: 11314 default: 11315 mtx_unlock(&lun->lun_lock); 11316 ctl_set_internal_failure(ctsio, 11317 /*sks_valid*/ 0, 11318 /*retry_count*/ 0); 11319 ctl_done((union ctl_io *)ctsio); 11320 break; 11321 } 11322 return (retval); 11323} 11324 11325const struct ctl_cmd_entry * 11326ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11327{ 11328 const struct ctl_cmd_entry *entry; 11329 int service_action; 11330 11331 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11332 if (entry->flags & CTL_CMD_FLAG_SA5) { 11333 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11334 entry = &((const struct ctl_cmd_entry *) 11335 entry->execute)[service_action]; 11336 } 11337 return (entry); 11338} 11339 11340const struct ctl_cmd_entry * 11341ctl_validate_command(struct ctl_scsiio *ctsio) 11342{ 11343 const struct ctl_cmd_entry *entry; 11344 int i; 11345 uint8_t diff; 11346 11347 entry = ctl_get_cmd_entry(ctsio); 11348 if (entry->execute == NULL) { 11349 ctl_set_invalid_opcode(ctsio); 11350 ctl_done((union ctl_io *)ctsio); 11351 return (NULL); 11352 } 11353 KASSERT(entry->length > 0, 11354 ("Not defined length for command 0x%02x/0x%02x", 11355 ctsio->cdb[0], ctsio->cdb[1])); 11356 for (i = 1; i < entry->length; i++) { 11357 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11358 if (diff == 0) 11359 continue; 11360 ctl_set_invalid_field(ctsio, 11361 /*sks_valid*/ 1, 11362 /*command*/ 1, 11363 /*field*/ i, 11364 /*bit_valid*/ 1, 11365 /*bit*/ fls(diff) - 1); 11366 ctl_done((union ctl_io *)ctsio); 11367 return (NULL); 11368 } 11369 return (entry); 11370} 11371 11372static int 11373ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11374{ 11375 11376 switch (lun_type) { 11377 case T_PROCESSOR: 11378 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11379 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11380 return (0); 11381 break; 11382 case T_DIRECT: 11383 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11384 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11385 return (0); 11386 break; 11387 default: 11388 return (0); 11389 } 11390 return (1); 11391} 11392 11393static int 11394ctl_scsiio(struct ctl_scsiio *ctsio) 11395{ 11396 int retval; 11397 const struct ctl_cmd_entry *entry; 11398 11399 retval = CTL_RETVAL_COMPLETE; 11400 11401 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11402 11403 entry = ctl_get_cmd_entry(ctsio); 11404 11405 /* 11406 * If this I/O has been aborted, just send it straight to 11407 * ctl_done() without executing it. 11408 */ 11409 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11410 ctl_done((union ctl_io *)ctsio); 11411 goto bailout; 11412 } 11413 11414 /* 11415 * All the checks should have been handled by ctl_scsiio_precheck(). 11416 * We should be clear now to just execute the I/O. 11417 */ 11418 retval = entry->execute(ctsio); 11419 11420bailout: 11421 return (retval); 11422} 11423 11424/* 11425 * Since we only implement one target right now, a bus reset simply resets 11426 * our single target. 11427 */ 11428static int 11429ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11430{ 11431 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11432} 11433 11434static int 11435ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11436 ctl_ua_type ua_type) 11437{ 11438 struct ctl_lun *lun; 11439 int retval; 11440 11441 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11442 union ctl_ha_msg msg_info; 11443 11444 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11445 msg_info.hdr.nexus = io->io_hdr.nexus; 11446 if (ua_type==CTL_UA_TARG_RESET) 11447 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11448 else 11449 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11450 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11451 msg_info.hdr.original_sc = NULL; 11452 msg_info.hdr.serializing_sc = NULL; 11453 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11454 (void *)&msg_info, sizeof(msg_info), 0)) { 11455 } 11456 } 11457 retval = 0; 11458 11459 mtx_lock(&ctl_softc->ctl_lock); 11460 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11461 retval += ctl_lun_reset(lun, io, ua_type); 11462 mtx_unlock(&ctl_softc->ctl_lock); 11463 11464 return (retval); 11465} 11466 11467/* 11468 * The LUN should always be set. The I/O is optional, and is used to 11469 * distinguish between I/Os sent by this initiator, and by other 11470 * initiators. We set unit attention for initiators other than this one. 11471 * SAM-3 is vague on this point. It does say that a unit attention should 11472 * be established for other initiators when a LUN is reset (see section 11473 * 5.7.3), but it doesn't specifically say that the unit attention should 11474 * be established for this particular initiator when a LUN is reset. Here 11475 * is the relevant text, from SAM-3 rev 8: 11476 * 11477 * 5.7.2 When a SCSI initiator port aborts its own tasks 11478 * 11479 * When a SCSI initiator port causes its own task(s) to be aborted, no 11480 * notification that the task(s) have been aborted shall be returned to 11481 * the SCSI initiator port other than the completion response for the 11482 * command or task management function action that caused the task(s) to 11483 * be aborted and notification(s) associated with related effects of the 11484 * action (e.g., a reset unit attention condition). 11485 * 11486 * XXX KDM for now, we're setting unit attention for all initiators. 11487 */ 11488static int 11489ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11490{ 11491 union ctl_io *xio; 11492#if 0 11493 uint32_t initindex; 11494#endif 11495 int i; 11496 11497 mtx_lock(&lun->lun_lock); 11498 /* 11499 * Run through the OOA queue and abort each I/O. 11500 */ 11501#if 0 11502 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11503#endif 11504 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11505 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11506 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11507 } 11508 11509 /* 11510 * This version sets unit attention for every 11511 */ 11512#if 0 11513 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11514 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11515 if (initindex == i) 11516 continue; 11517 lun->pending_sense[i].ua_pending |= ua_type; 11518 } 11519#endif 11520 11521 /* 11522 * A reset (any kind, really) clears reservations established with 11523 * RESERVE/RELEASE. It does not clear reservations established 11524 * with PERSISTENT RESERVE OUT, but we don't support that at the 11525 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11526 * reservations made with the RESERVE/RELEASE commands, because 11527 * those commands are obsolete in SPC-3. 11528 */ 11529 lun->flags &= ~CTL_LUN_RESERVED; 11530 11531 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11532 ctl_clear_mask(lun->have_ca, i); 11533 lun->pending_sense[i].ua_pending |= ua_type; 11534 } 11535 mtx_unlock(&lun->lun_lock); 11536 11537 return (0); 11538} 11539 11540static int 11541ctl_abort_task(union ctl_io *io) 11542{ 11543 union ctl_io *xio; 11544 struct ctl_lun *lun; 11545 struct ctl_softc *ctl_softc; 11546#if 0 11547 struct sbuf sb; 11548 char printbuf[128]; 11549#endif 11550 int found; 11551 uint32_t targ_lun; 11552 11553 ctl_softc = control_softc; 11554 found = 0; 11555 11556 /* 11557 * Look up the LUN. 11558 */ 11559 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11560 mtx_lock(&ctl_softc->ctl_lock); 11561 if ((targ_lun < CTL_MAX_LUNS) 11562 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11563 lun = ctl_softc->ctl_luns[targ_lun]; 11564 else { 11565 mtx_unlock(&ctl_softc->ctl_lock); 11566 goto bailout; 11567 } 11568 11569#if 0 11570 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11571 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11572#endif 11573 11574 mtx_lock(&lun->lun_lock); 11575 mtx_unlock(&ctl_softc->ctl_lock); 11576 /* 11577 * Run through the OOA queue and attempt to find the given I/O. 11578 * The target port, initiator ID, tag type and tag number have to 11579 * match the values that we got from the initiator. If we have an 11580 * untagged command to abort, simply abort the first untagged command 11581 * we come to. We only allow one untagged command at a time of course. 11582 */ 11583#if 0 11584 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11585#endif 11586 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11587 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11588#if 0 11589 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11590 11591 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11592 lun->lun, xio->scsiio.tag_num, 11593 xio->scsiio.tag_type, 11594 (xio->io_hdr.blocked_links.tqe_prev 11595 == NULL) ? "" : " BLOCKED", 11596 (xio->io_hdr.flags & 11597 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11598 (xio->io_hdr.flags & 11599 CTL_FLAG_ABORT) ? " ABORT" : "", 11600 (xio->io_hdr.flags & 11601 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11602 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11603 sbuf_finish(&sb); 11604 printf("%s\n", sbuf_data(&sb)); 11605#endif 11606 11607 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 11608 && (xio->io_hdr.nexus.initid.id == 11609 io->io_hdr.nexus.initid.id)) { 11610 /* 11611 * If the abort says that the task is untagged, the 11612 * task in the queue must be untagged. Otherwise, 11613 * we just check to see whether the tag numbers 11614 * match. This is because the QLogic firmware 11615 * doesn't pass back the tag type in an abort 11616 * request. 11617 */ 11618#if 0 11619 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11620 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11621 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 11622#endif 11623 /* 11624 * XXX KDM we've got problems with FC, because it 11625 * doesn't send down a tag type with aborts. So we 11626 * can only really go by the tag number... 11627 * This may cause problems with parallel SCSI. 11628 * Need to figure that out!! 11629 */ 11630 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11631 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11632 found = 1; 11633 if ((io->io_hdr.flags & 11634 CTL_FLAG_FROM_OTHER_SC) == 0 && 11635 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11636 union ctl_ha_msg msg_info; 11637 11638 io->io_hdr.flags |= 11639 CTL_FLAG_SENT_2OTHER_SC; 11640 msg_info.hdr.nexus = io->io_hdr.nexus; 11641 msg_info.task.task_action = 11642 CTL_TASK_ABORT_TASK; 11643 msg_info.task.tag_num = 11644 io->taskio.tag_num; 11645 msg_info.task.tag_type = 11646 io->taskio.tag_type; 11647 msg_info.hdr.msg_type = 11648 CTL_MSG_MANAGE_TASKS; 11649 msg_info.hdr.original_sc = NULL; 11650 msg_info.hdr.serializing_sc = NULL; 11651#if 0 11652 printf("Sent Abort to other side\n"); 11653#endif 11654 if (CTL_HA_STATUS_SUCCESS != 11655 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11656 (void *)&msg_info, 11657 sizeof(msg_info), 0)) { 11658 } 11659 } 11660#if 0 11661 printf("ctl_abort_task: found I/O to abort\n"); 11662#endif 11663 break; 11664 } 11665 } 11666 } 11667 mtx_unlock(&lun->lun_lock); 11668 11669bailout: 11670 11671 if (found == 0) { 11672 /* 11673 * This isn't really an error. It's entirely possible for 11674 * the abort and command completion to cross on the wire. 11675 * This is more of an informative/diagnostic error. 11676 */ 11677#if 0 11678 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11679 "%d:%d:%d:%d tag %d type %d\n", 11680 io->io_hdr.nexus.initid.id, 11681 io->io_hdr.nexus.targ_port, 11682 io->io_hdr.nexus.targ_target.id, 11683 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11684 io->taskio.tag_type); 11685#endif 11686 return (1); 11687 } else 11688 return (0); 11689} 11690 11691static void 11692ctl_run_task(union ctl_io *io) 11693{ 11694 struct ctl_softc *ctl_softc; 11695 int retval; 11696 const char *task_desc; 11697 11698 CTL_DEBUG_PRINT(("ctl_run_task\n")); 11699 11700 ctl_softc = control_softc; 11701 retval = 0; 11702 11703 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 11704 ("ctl_run_task: Unextected io_type %d\n", 11705 io->io_hdr.io_type)); 11706 11707 task_desc = ctl_scsi_task_string(&io->taskio); 11708 if (task_desc != NULL) { 11709#ifdef NEEDTOPORT 11710 csevent_log(CSC_CTL | CSC_SHELF_SW | 11711 CTL_TASK_REPORT, 11712 csevent_LogType_Trace, 11713 csevent_Severity_Information, 11714 csevent_AlertLevel_Green, 11715 csevent_FRU_Firmware, 11716 csevent_FRU_Unknown, 11717 "CTL: received task: %s",task_desc); 11718#endif 11719 } else { 11720#ifdef NEEDTOPORT 11721 csevent_log(CSC_CTL | CSC_SHELF_SW | 11722 CTL_TASK_REPORT, 11723 csevent_LogType_Trace, 11724 csevent_Severity_Information, 11725 csevent_AlertLevel_Green, 11726 csevent_FRU_Firmware, 11727 csevent_FRU_Unknown, 11728 "CTL: received unknown task " 11729 "type: %d (%#x)", 11730 io->taskio.task_action, 11731 io->taskio.task_action); 11732#endif 11733 } 11734 switch (io->taskio.task_action) { 11735 case CTL_TASK_ABORT_TASK: 11736 retval = ctl_abort_task(io); 11737 break; 11738 case CTL_TASK_ABORT_TASK_SET: 11739 break; 11740 case CTL_TASK_CLEAR_ACA: 11741 break; 11742 case CTL_TASK_CLEAR_TASK_SET: 11743 break; 11744 case CTL_TASK_LUN_RESET: { 11745 struct ctl_lun *lun; 11746 uint32_t targ_lun; 11747 int retval; 11748 11749 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11750 mtx_lock(&ctl_softc->ctl_lock); 11751 if ((targ_lun < CTL_MAX_LUNS) 11752 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11753 lun = ctl_softc->ctl_luns[targ_lun]; 11754 else { 11755 mtx_unlock(&ctl_softc->ctl_lock); 11756 retval = 1; 11757 break; 11758 } 11759 11760 if (!(io->io_hdr.flags & 11761 CTL_FLAG_FROM_OTHER_SC)) { 11762 union ctl_ha_msg msg_info; 11763 11764 io->io_hdr.flags |= 11765 CTL_FLAG_SENT_2OTHER_SC; 11766 msg_info.hdr.msg_type = 11767 CTL_MSG_MANAGE_TASKS; 11768 msg_info.hdr.nexus = io->io_hdr.nexus; 11769 msg_info.task.task_action = 11770 CTL_TASK_LUN_RESET; 11771 msg_info.hdr.original_sc = NULL; 11772 msg_info.hdr.serializing_sc = NULL; 11773 if (CTL_HA_STATUS_SUCCESS != 11774 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11775 (void *)&msg_info, 11776 sizeof(msg_info), 0)) { 11777 } 11778 } 11779 11780 retval = ctl_lun_reset(lun, io, 11781 CTL_UA_LUN_RESET); 11782 mtx_unlock(&ctl_softc->ctl_lock); 11783 break; 11784 } 11785 case CTL_TASK_TARGET_RESET: 11786 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 11787 break; 11788 case CTL_TASK_BUS_RESET: 11789 retval = ctl_bus_reset(ctl_softc, io); 11790 break; 11791 case CTL_TASK_PORT_LOGIN: 11792 break; 11793 case CTL_TASK_PORT_LOGOUT: 11794 break; 11795 default: 11796 printf("ctl_run_task: got unknown task management event %d\n", 11797 io->taskio.task_action); 11798 break; 11799 } 11800 if (retval == 0) 11801 io->io_hdr.status = CTL_SUCCESS; 11802 else 11803 io->io_hdr.status = CTL_ERROR; 11804 11805 /* 11806 * This will queue this I/O to the done queue, but the 11807 * work thread won't be able to process it until we 11808 * return and the lock is released. 11809 */ 11810 ctl_done(io); 11811} 11812 11813/* 11814 * For HA operation. Handle commands that come in from the other 11815 * controller. 11816 */ 11817static void 11818ctl_handle_isc(union ctl_io *io) 11819{ 11820 int free_io; 11821 struct ctl_lun *lun; 11822 struct ctl_softc *ctl_softc; 11823 uint32_t targ_lun; 11824 11825 ctl_softc = control_softc; 11826 11827 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11828 lun = ctl_softc->ctl_luns[targ_lun]; 11829 11830 switch (io->io_hdr.msg_type) { 11831 case CTL_MSG_SERIALIZE: 11832 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 11833 break; 11834 case CTL_MSG_R2R: { 11835 const struct ctl_cmd_entry *entry; 11836 11837 /* 11838 * This is only used in SER_ONLY mode. 11839 */ 11840 free_io = 0; 11841 entry = ctl_get_cmd_entry(&io->scsiio); 11842 mtx_lock(&lun->lun_lock); 11843 if (ctl_scsiio_lun_check(ctl_softc, lun, 11844 entry, (struct ctl_scsiio *)io) != 0) { 11845 mtx_unlock(&lun->lun_lock); 11846 ctl_done(io); 11847 break; 11848 } 11849 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11850 mtx_unlock(&lun->lun_lock); 11851 ctl_enqueue_rtr(io); 11852 break; 11853 } 11854 case CTL_MSG_FINISH_IO: 11855 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 11856 free_io = 0; 11857 ctl_done(io); 11858 } else { 11859 free_io = 1; 11860 mtx_lock(&lun->lun_lock); 11861 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11862 ooa_links); 11863 ctl_check_blocked(lun); 11864 mtx_unlock(&lun->lun_lock); 11865 } 11866 break; 11867 case CTL_MSG_PERS_ACTION: 11868 ctl_hndl_per_res_out_on_other_sc( 11869 (union ctl_ha_msg *)&io->presio.pr_msg); 11870 free_io = 1; 11871 break; 11872 case CTL_MSG_BAD_JUJU: 11873 free_io = 0; 11874 ctl_done(io); 11875 break; 11876 case CTL_MSG_DATAMOVE: 11877 /* Only used in XFER mode */ 11878 free_io = 0; 11879 ctl_datamove_remote(io); 11880 break; 11881 case CTL_MSG_DATAMOVE_DONE: 11882 /* Only used in XFER mode */ 11883 free_io = 0; 11884 io->scsiio.be_move_done(io); 11885 break; 11886 default: 11887 free_io = 1; 11888 printf("%s: Invalid message type %d\n", 11889 __func__, io->io_hdr.msg_type); 11890 break; 11891 } 11892 if (free_io) 11893 ctl_free_io(io); 11894 11895} 11896 11897 11898/* 11899 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11900 * there is no match. 11901 */ 11902static ctl_lun_error_pattern 11903ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11904{ 11905 const struct ctl_cmd_entry *entry; 11906 ctl_lun_error_pattern filtered_pattern, pattern; 11907 11908 pattern = desc->error_pattern; 11909 11910 /* 11911 * XXX KDM we need more data passed into this function to match a 11912 * custom pattern, and we actually need to implement custom pattern 11913 * matching. 11914 */ 11915 if (pattern & CTL_LUN_PAT_CMD) 11916 return (CTL_LUN_PAT_CMD); 11917 11918 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 11919 return (CTL_LUN_PAT_ANY); 11920 11921 entry = ctl_get_cmd_entry(ctsio); 11922 11923 filtered_pattern = entry->pattern & pattern; 11924 11925 /* 11926 * If the user requested specific flags in the pattern (e.g. 11927 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 11928 * flags. 11929 * 11930 * If the user did not specify any flags, it doesn't matter whether 11931 * or not the command supports the flags. 11932 */ 11933 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 11934 (pattern & ~CTL_LUN_PAT_MASK)) 11935 return (CTL_LUN_PAT_NONE); 11936 11937 /* 11938 * If the user asked for a range check, see if the requested LBA 11939 * range overlaps with this command's LBA range. 11940 */ 11941 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 11942 uint64_t lba1; 11943 uint32_t len1; 11944 ctl_action action; 11945 int retval; 11946 11947 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 11948 if (retval != 0) 11949 return (CTL_LUN_PAT_NONE); 11950 11951 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 11952 desc->lba_range.len); 11953 /* 11954 * A "pass" means that the LBA ranges don't overlap, so 11955 * this doesn't match the user's range criteria. 11956 */ 11957 if (action == CTL_ACTION_PASS) 11958 return (CTL_LUN_PAT_NONE); 11959 } 11960 11961 return (filtered_pattern); 11962} 11963 11964static void 11965ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 11966{ 11967 struct ctl_error_desc *desc, *desc2; 11968 11969 mtx_assert(&lun->lun_lock, MA_OWNED); 11970 11971 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 11972 ctl_lun_error_pattern pattern; 11973 /* 11974 * Check to see whether this particular command matches 11975 * the pattern in the descriptor. 11976 */ 11977 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 11978 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 11979 continue; 11980 11981 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 11982 case CTL_LUN_INJ_ABORTED: 11983 ctl_set_aborted(&io->scsiio); 11984 break; 11985 case CTL_LUN_INJ_MEDIUM_ERR: 11986 ctl_set_medium_error(&io->scsiio); 11987 break; 11988 case CTL_LUN_INJ_UA: 11989 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 11990 * OCCURRED */ 11991 ctl_set_ua(&io->scsiio, 0x29, 0x00); 11992 break; 11993 case CTL_LUN_INJ_CUSTOM: 11994 /* 11995 * We're assuming the user knows what he is doing. 11996 * Just copy the sense information without doing 11997 * checks. 11998 */ 11999 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12000 ctl_min(sizeof(desc->custom_sense), 12001 sizeof(io->scsiio.sense_data))); 12002 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12003 io->scsiio.sense_len = SSD_FULL_SIZE; 12004 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12005 break; 12006 case CTL_LUN_INJ_NONE: 12007 default: 12008 /* 12009 * If this is an error injection type we don't know 12010 * about, clear the continuous flag (if it is set) 12011 * so it will get deleted below. 12012 */ 12013 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12014 break; 12015 } 12016 /* 12017 * By default, each error injection action is a one-shot 12018 */ 12019 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12020 continue; 12021 12022 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12023 12024 free(desc, M_CTL); 12025 } 12026} 12027 12028#ifdef CTL_IO_DELAY 12029static void 12030ctl_datamove_timer_wakeup(void *arg) 12031{ 12032 union ctl_io *io; 12033 12034 io = (union ctl_io *)arg; 12035 12036 ctl_datamove(io); 12037} 12038#endif /* CTL_IO_DELAY */ 12039 12040void 12041ctl_datamove(union ctl_io *io) 12042{ 12043 void (*fe_datamove)(union ctl_io *io); 12044 12045 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12046 12047 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12048 12049#ifdef CTL_TIME_IO 12050 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12051 char str[256]; 12052 char path_str[64]; 12053 struct sbuf sb; 12054 12055 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12056 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12057 12058 sbuf_cat(&sb, path_str); 12059 switch (io->io_hdr.io_type) { 12060 case CTL_IO_SCSI: 12061 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12062 sbuf_printf(&sb, "\n"); 12063 sbuf_cat(&sb, path_str); 12064 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12065 io->scsiio.tag_num, io->scsiio.tag_type); 12066 break; 12067 case CTL_IO_TASK: 12068 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12069 "Tag Type: %d\n", io->taskio.task_action, 12070 io->taskio.tag_num, io->taskio.tag_type); 12071 break; 12072 default: 12073 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12074 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12075 break; 12076 } 12077 sbuf_cat(&sb, path_str); 12078 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12079 (intmax_t)time_uptime - io->io_hdr.start_time); 12080 sbuf_finish(&sb); 12081 printf("%s", sbuf_data(&sb)); 12082 } 12083#endif /* CTL_TIME_IO */ 12084 12085#ifdef CTL_IO_DELAY 12086 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12087 struct ctl_lun *lun; 12088 12089 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12090 12091 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12092 } else { 12093 struct ctl_lun *lun; 12094 12095 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12096 if ((lun != NULL) 12097 && (lun->delay_info.datamove_delay > 0)) { 12098 struct callout *callout; 12099 12100 callout = (struct callout *)&io->io_hdr.timer_bytes; 12101 callout_init(callout, /*mpsafe*/ 1); 12102 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12103 callout_reset(callout, 12104 lun->delay_info.datamove_delay * hz, 12105 ctl_datamove_timer_wakeup, io); 12106 if (lun->delay_info.datamove_type == 12107 CTL_DELAY_TYPE_ONESHOT) 12108 lun->delay_info.datamove_delay = 0; 12109 return; 12110 } 12111 } 12112#endif 12113 12114 /* 12115 * This command has been aborted. Set the port status, so we fail 12116 * the data move. 12117 */ 12118 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12119 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12120 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12121 io->io_hdr.nexus.targ_port, 12122 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12123 io->io_hdr.nexus.targ_lun); 12124 io->io_hdr.status = CTL_CMD_ABORTED; 12125 io->io_hdr.port_status = 31337; 12126 /* 12127 * Note that the backend, in this case, will get the 12128 * callback in its context. In other cases it may get 12129 * called in the frontend's interrupt thread context. 12130 */ 12131 io->scsiio.be_move_done(io); 12132 return; 12133 } 12134 12135 /* 12136 * If we're in XFER mode and this I/O is from the other shelf 12137 * controller, we need to send the DMA to the other side to 12138 * actually transfer the data to/from the host. In serialize only 12139 * mode the transfer happens below CTL and ctl_datamove() is only 12140 * called on the machine that originally received the I/O. 12141 */ 12142 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12143 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12144 union ctl_ha_msg msg; 12145 uint32_t sg_entries_sent; 12146 int do_sg_copy; 12147 int i; 12148 12149 memset(&msg, 0, sizeof(msg)); 12150 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12151 msg.hdr.original_sc = io->io_hdr.original_sc; 12152 msg.hdr.serializing_sc = io; 12153 msg.hdr.nexus = io->io_hdr.nexus; 12154 msg.dt.flags = io->io_hdr.flags; 12155 /* 12156 * We convert everything into a S/G list here. We can't 12157 * pass by reference, only by value between controllers. 12158 * So we can't pass a pointer to the S/G list, only as many 12159 * S/G entries as we can fit in here. If it's possible for 12160 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12161 * then we need to break this up into multiple transfers. 12162 */ 12163 if (io->scsiio.kern_sg_entries == 0) { 12164 msg.dt.kern_sg_entries = 1; 12165 /* 12166 * If this is in cached memory, flush the cache 12167 * before we send the DMA request to the other 12168 * controller. We want to do this in either the 12169 * read or the write case. The read case is 12170 * straightforward. In the write case, we want to 12171 * make sure nothing is in the local cache that 12172 * could overwrite the DMAed data. 12173 */ 12174 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12175 /* 12176 * XXX KDM use bus_dmamap_sync() here. 12177 */ 12178 } 12179 12180 /* 12181 * Convert to a physical address if this is a 12182 * virtual address. 12183 */ 12184 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12185 msg.dt.sg_list[0].addr = 12186 io->scsiio.kern_data_ptr; 12187 } else { 12188 /* 12189 * XXX KDM use busdma here! 12190 */ 12191#if 0 12192 msg.dt.sg_list[0].addr = (void *) 12193 vtophys(io->scsiio.kern_data_ptr); 12194#endif 12195 } 12196 12197 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12198 do_sg_copy = 0; 12199 } else { 12200 struct ctl_sg_entry *sgl; 12201 12202 do_sg_copy = 1; 12203 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12204 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12205 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12206 /* 12207 * XXX KDM use bus_dmamap_sync() here. 12208 */ 12209 } 12210 } 12211 12212 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12213 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12214 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12215 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12216 msg.dt.sg_sequence = 0; 12217 12218 /* 12219 * Loop until we've sent all of the S/G entries. On the 12220 * other end, we'll recompose these S/G entries into one 12221 * contiguous list before passing it to the 12222 */ 12223 for (sg_entries_sent = 0; sg_entries_sent < 12224 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12225 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12226 sizeof(msg.dt.sg_list[0])), 12227 msg.dt.kern_sg_entries - sg_entries_sent); 12228 12229 if (do_sg_copy != 0) { 12230 struct ctl_sg_entry *sgl; 12231 int j; 12232 12233 sgl = (struct ctl_sg_entry *) 12234 io->scsiio.kern_data_ptr; 12235 /* 12236 * If this is in cached memory, flush the cache 12237 * before we send the DMA request to the other 12238 * controller. We want to do this in either 12239 * the * read or the write case. The read 12240 * case is straightforward. In the write 12241 * case, we want to make sure nothing is 12242 * in the local cache that could overwrite 12243 * the DMAed data. 12244 */ 12245 12246 for (i = sg_entries_sent, j = 0; 12247 i < msg.dt.cur_sg_entries; i++, j++) { 12248 if ((io->io_hdr.flags & 12249 CTL_FLAG_NO_DATASYNC) == 0) { 12250 /* 12251 * XXX KDM use bus_dmamap_sync() 12252 */ 12253 } 12254 if ((io->io_hdr.flags & 12255 CTL_FLAG_BUS_ADDR) == 0) { 12256 /* 12257 * XXX KDM use busdma. 12258 */ 12259#if 0 12260 msg.dt.sg_list[j].addr =(void *) 12261 vtophys(sgl[i].addr); 12262#endif 12263 } else { 12264 msg.dt.sg_list[j].addr = 12265 sgl[i].addr; 12266 } 12267 msg.dt.sg_list[j].len = sgl[i].len; 12268 } 12269 } 12270 12271 sg_entries_sent += msg.dt.cur_sg_entries; 12272 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12273 msg.dt.sg_last = 1; 12274 else 12275 msg.dt.sg_last = 0; 12276 12277 /* 12278 * XXX KDM drop and reacquire the lock here? 12279 */ 12280 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12281 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12282 /* 12283 * XXX do something here. 12284 */ 12285 } 12286 12287 msg.dt.sent_sg_entries = sg_entries_sent; 12288 } 12289 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12290 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12291 ctl_failover_io(io, /*have_lock*/ 0); 12292 12293 } else { 12294 12295 /* 12296 * Lookup the fe_datamove() function for this particular 12297 * front end. 12298 */ 12299 fe_datamove = 12300 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12301 12302 fe_datamove(io); 12303 } 12304} 12305 12306static void 12307ctl_send_datamove_done(union ctl_io *io, int have_lock) 12308{ 12309 union ctl_ha_msg msg; 12310 int isc_status; 12311 12312 memset(&msg, 0, sizeof(msg)); 12313 12314 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12315 msg.hdr.original_sc = io; 12316 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12317 msg.hdr.nexus = io->io_hdr.nexus; 12318 msg.hdr.status = io->io_hdr.status; 12319 msg.scsi.tag_num = io->scsiio.tag_num; 12320 msg.scsi.tag_type = io->scsiio.tag_type; 12321 msg.scsi.scsi_status = io->scsiio.scsi_status; 12322 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12323 sizeof(io->scsiio.sense_data)); 12324 msg.scsi.sense_len = io->scsiio.sense_len; 12325 msg.scsi.sense_residual = io->scsiio.sense_residual; 12326 msg.scsi.fetd_status = io->io_hdr.port_status; 12327 msg.scsi.residual = io->scsiio.residual; 12328 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12329 12330 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12331 ctl_failover_io(io, /*have_lock*/ have_lock); 12332 return; 12333 } 12334 12335 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12336 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12337 /* XXX do something if this fails */ 12338 } 12339 12340} 12341 12342/* 12343 * The DMA to the remote side is done, now we need to tell the other side 12344 * we're done so it can continue with its data movement. 12345 */ 12346static void 12347ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12348{ 12349 union ctl_io *io; 12350 12351 io = rq->context; 12352 12353 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12354 printf("%s: ISC DMA write failed with error %d", __func__, 12355 rq->ret); 12356 ctl_set_internal_failure(&io->scsiio, 12357 /*sks_valid*/ 1, 12358 /*retry_count*/ rq->ret); 12359 } 12360 12361 ctl_dt_req_free(rq); 12362 12363 /* 12364 * In this case, we had to malloc the memory locally. Free it. 12365 */ 12366 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12367 int i; 12368 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12369 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12370 } 12371 /* 12372 * The data is in local and remote memory, so now we need to send 12373 * status (good or back) back to the other side. 12374 */ 12375 ctl_send_datamove_done(io, /*have_lock*/ 0); 12376} 12377 12378/* 12379 * We've moved the data from the host/controller into local memory. Now we 12380 * need to push it over to the remote controller's memory. 12381 */ 12382static int 12383ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12384{ 12385 int retval; 12386 12387 retval = 0; 12388 12389 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12390 ctl_datamove_remote_write_cb); 12391 12392 return (retval); 12393} 12394 12395static void 12396ctl_datamove_remote_write(union ctl_io *io) 12397{ 12398 int retval; 12399 void (*fe_datamove)(union ctl_io *io); 12400 12401 /* 12402 * - Get the data from the host/HBA into local memory. 12403 * - DMA memory from the local controller to the remote controller. 12404 * - Send status back to the remote controller. 12405 */ 12406 12407 retval = ctl_datamove_remote_sgl_setup(io); 12408 if (retval != 0) 12409 return; 12410 12411 /* Switch the pointer over so the FETD knows what to do */ 12412 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12413 12414 /* 12415 * Use a custom move done callback, since we need to send completion 12416 * back to the other controller, not to the backend on this side. 12417 */ 12418 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12419 12420 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12421 12422 fe_datamove(io); 12423 12424 return; 12425 12426} 12427 12428static int 12429ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12430{ 12431#if 0 12432 char str[256]; 12433 char path_str[64]; 12434 struct sbuf sb; 12435#endif 12436 12437 /* 12438 * In this case, we had to malloc the memory locally. Free it. 12439 */ 12440 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12441 int i; 12442 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12443 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12444 } 12445 12446#if 0 12447 scsi_path_string(io, path_str, sizeof(path_str)); 12448 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12449 sbuf_cat(&sb, path_str); 12450 scsi_command_string(&io->scsiio, NULL, &sb); 12451 sbuf_printf(&sb, "\n"); 12452 sbuf_cat(&sb, path_str); 12453 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12454 io->scsiio.tag_num, io->scsiio.tag_type); 12455 sbuf_cat(&sb, path_str); 12456 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12457 io->io_hdr.flags, io->io_hdr.status); 12458 sbuf_finish(&sb); 12459 printk("%s", sbuf_data(&sb)); 12460#endif 12461 12462 12463 /* 12464 * The read is done, now we need to send status (good or bad) back 12465 * to the other side. 12466 */ 12467 ctl_send_datamove_done(io, /*have_lock*/ 0); 12468 12469 return (0); 12470} 12471 12472static void 12473ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12474{ 12475 union ctl_io *io; 12476 void (*fe_datamove)(union ctl_io *io); 12477 12478 io = rq->context; 12479 12480 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12481 printf("%s: ISC DMA read failed with error %d", __func__, 12482 rq->ret); 12483 ctl_set_internal_failure(&io->scsiio, 12484 /*sks_valid*/ 1, 12485 /*retry_count*/ rq->ret); 12486 } 12487 12488 ctl_dt_req_free(rq); 12489 12490 /* Switch the pointer over so the FETD knows what to do */ 12491 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12492 12493 /* 12494 * Use a custom move done callback, since we need to send completion 12495 * back to the other controller, not to the backend on this side. 12496 */ 12497 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12498 12499 /* XXX KDM add checks like the ones in ctl_datamove? */ 12500 12501 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12502 12503 fe_datamove(io); 12504} 12505 12506static int 12507ctl_datamove_remote_sgl_setup(union ctl_io *io) 12508{ 12509 struct ctl_sg_entry *local_sglist, *remote_sglist; 12510 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12511 struct ctl_softc *softc; 12512 int retval; 12513 int i; 12514 12515 retval = 0; 12516 softc = control_softc; 12517 12518 local_sglist = io->io_hdr.local_sglist; 12519 local_dma_sglist = io->io_hdr.local_dma_sglist; 12520 remote_sglist = io->io_hdr.remote_sglist; 12521 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12522 12523 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12524 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12525 local_sglist[i].len = remote_sglist[i].len; 12526 12527 /* 12528 * XXX Detect the situation where the RS-level I/O 12529 * redirector on the other side has already read the 12530 * data off of the AOR RS on this side, and 12531 * transferred it to remote (mirror) memory on the 12532 * other side. Since we already have the data in 12533 * memory here, we just need to use it. 12534 * 12535 * XXX KDM this can probably be removed once we 12536 * get the cache device code in and take the 12537 * current AOR implementation out. 12538 */ 12539#ifdef NEEDTOPORT 12540 if ((remote_sglist[i].addr >= 12541 (void *)vtophys(softc->mirr->addr)) 12542 && (remote_sglist[i].addr < 12543 ((void *)vtophys(softc->mirr->addr) + 12544 CacheMirrorOffset))) { 12545 local_sglist[i].addr = remote_sglist[i].addr - 12546 CacheMirrorOffset; 12547 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12548 CTL_FLAG_DATA_IN) 12549 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12550 } else { 12551 local_sglist[i].addr = remote_sglist[i].addr + 12552 CacheMirrorOffset; 12553 } 12554#endif 12555#if 0 12556 printf("%s: local %p, remote %p, len %d\n", 12557 __func__, local_sglist[i].addr, 12558 remote_sglist[i].addr, local_sglist[i].len); 12559#endif 12560 } 12561 } else { 12562 uint32_t len_to_go; 12563 12564 /* 12565 * In this case, we don't have automatically allocated 12566 * memory for this I/O on this controller. This typically 12567 * happens with internal CTL I/O -- e.g. inquiry, mode 12568 * sense, etc. Anything coming from RAIDCore will have 12569 * a mirror area available. 12570 */ 12571 len_to_go = io->scsiio.kern_data_len; 12572 12573 /* 12574 * Clear the no datasync flag, we have to use malloced 12575 * buffers. 12576 */ 12577 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12578 12579 /* 12580 * The difficult thing here is that the size of the various 12581 * S/G segments may be different than the size from the 12582 * remote controller. That'll make it harder when DMAing 12583 * the data back to the other side. 12584 */ 12585 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12586 sizeof(io->io_hdr.remote_sglist[0])) && 12587 (len_to_go > 0); i++) { 12588 local_sglist[i].len = ctl_min(len_to_go, 131072); 12589 CTL_SIZE_8B(local_dma_sglist[i].len, 12590 local_sglist[i].len); 12591 local_sglist[i].addr = 12592 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12593 12594 local_dma_sglist[i].addr = local_sglist[i].addr; 12595 12596 if (local_sglist[i].addr == NULL) { 12597 int j; 12598 12599 printf("malloc failed for %zd bytes!", 12600 local_dma_sglist[i].len); 12601 for (j = 0; j < i; j++) { 12602 free(local_sglist[j].addr, M_CTL); 12603 } 12604 ctl_set_internal_failure(&io->scsiio, 12605 /*sks_valid*/ 1, 12606 /*retry_count*/ 4857); 12607 retval = 1; 12608 goto bailout_error; 12609 12610 } 12611 /* XXX KDM do we need a sync here? */ 12612 12613 len_to_go -= local_sglist[i].len; 12614 } 12615 /* 12616 * Reset the number of S/G entries accordingly. The 12617 * original number of S/G entries is available in 12618 * rem_sg_entries. 12619 */ 12620 io->scsiio.kern_sg_entries = i; 12621 12622#if 0 12623 printf("%s: kern_sg_entries = %d\n", __func__, 12624 io->scsiio.kern_sg_entries); 12625 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12626 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 12627 local_sglist[i].addr, local_sglist[i].len, 12628 local_dma_sglist[i].len); 12629#endif 12630 } 12631 12632 12633 return (retval); 12634 12635bailout_error: 12636 12637 ctl_send_datamove_done(io, /*have_lock*/ 0); 12638 12639 return (retval); 12640} 12641 12642static int 12643ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12644 ctl_ha_dt_cb callback) 12645{ 12646 struct ctl_ha_dt_req *rq; 12647 struct ctl_sg_entry *remote_sglist, *local_sglist; 12648 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 12649 uint32_t local_used, remote_used, total_used; 12650 int retval; 12651 int i, j; 12652 12653 retval = 0; 12654 12655 rq = ctl_dt_req_alloc(); 12656 12657 /* 12658 * If we failed to allocate the request, and if the DMA didn't fail 12659 * anyway, set busy status. This is just a resource allocation 12660 * failure. 12661 */ 12662 if ((rq == NULL) 12663 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 12664 ctl_set_busy(&io->scsiio); 12665 12666 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 12667 12668 if (rq != NULL) 12669 ctl_dt_req_free(rq); 12670 12671 /* 12672 * The data move failed. We need to return status back 12673 * to the other controller. No point in trying to DMA 12674 * data to the remote controller. 12675 */ 12676 12677 ctl_send_datamove_done(io, /*have_lock*/ 0); 12678 12679 retval = 1; 12680 12681 goto bailout; 12682 } 12683 12684 local_sglist = io->io_hdr.local_sglist; 12685 local_dma_sglist = io->io_hdr.local_dma_sglist; 12686 remote_sglist = io->io_hdr.remote_sglist; 12687 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12688 local_used = 0; 12689 remote_used = 0; 12690 total_used = 0; 12691 12692 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 12693 rq->ret = CTL_HA_STATUS_SUCCESS; 12694 rq->context = io; 12695 callback(rq); 12696 goto bailout; 12697 } 12698 12699 /* 12700 * Pull/push the data over the wire from/to the other controller. 12701 * This takes into account the possibility that the local and 12702 * remote sglists may not be identical in terms of the size of 12703 * the elements and the number of elements. 12704 * 12705 * One fundamental assumption here is that the length allocated for 12706 * both the local and remote sglists is identical. Otherwise, we've 12707 * essentially got a coding error of some sort. 12708 */ 12709 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12710 int isc_ret; 12711 uint32_t cur_len, dma_length; 12712 uint8_t *tmp_ptr; 12713 12714 rq->id = CTL_HA_DATA_CTL; 12715 rq->command = command; 12716 rq->context = io; 12717 12718 /* 12719 * Both pointers should be aligned. But it is possible 12720 * that the allocation length is not. They should both 12721 * also have enough slack left over at the end, though, 12722 * to round up to the next 8 byte boundary. 12723 */ 12724 cur_len = ctl_min(local_sglist[i].len - local_used, 12725 remote_sglist[j].len - remote_used); 12726 12727 /* 12728 * In this case, we have a size issue and need to decrease 12729 * the size, except in the case where we actually have less 12730 * than 8 bytes left. In that case, we need to increase 12731 * the DMA length to get the last bit. 12732 */ 12733 if ((cur_len & 0x7) != 0) { 12734 if (cur_len > 0x7) { 12735 cur_len = cur_len - (cur_len & 0x7); 12736 dma_length = cur_len; 12737 } else { 12738 CTL_SIZE_8B(dma_length, cur_len); 12739 } 12740 12741 } else 12742 dma_length = cur_len; 12743 12744 /* 12745 * If we had to allocate memory for this I/O, instead of using 12746 * the non-cached mirror memory, we'll need to flush the cache 12747 * before trying to DMA to the other controller. 12748 * 12749 * We could end up doing this multiple times for the same 12750 * segment if we have a larger local segment than remote 12751 * segment. That shouldn't be an issue. 12752 */ 12753 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12754 /* 12755 * XXX KDM use bus_dmamap_sync() here. 12756 */ 12757 } 12758 12759 rq->size = dma_length; 12760 12761 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12762 tmp_ptr += local_used; 12763 12764 /* Use physical addresses when talking to ISC hardware */ 12765 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12766 /* XXX KDM use busdma */ 12767#if 0 12768 rq->local = vtophys(tmp_ptr); 12769#endif 12770 } else 12771 rq->local = tmp_ptr; 12772 12773 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12774 tmp_ptr += remote_used; 12775 rq->remote = tmp_ptr; 12776 12777 rq->callback = NULL; 12778 12779 local_used += cur_len; 12780 if (local_used >= local_sglist[i].len) { 12781 i++; 12782 local_used = 0; 12783 } 12784 12785 remote_used += cur_len; 12786 if (remote_used >= remote_sglist[j].len) { 12787 j++; 12788 remote_used = 0; 12789 } 12790 total_used += cur_len; 12791 12792 if (total_used >= io->scsiio.kern_data_len) 12793 rq->callback = callback; 12794 12795 if ((rq->size & 0x7) != 0) { 12796 printf("%s: warning: size %d is not on 8b boundary\n", 12797 __func__, rq->size); 12798 } 12799 if (((uintptr_t)rq->local & 0x7) != 0) { 12800 printf("%s: warning: local %p not on 8b boundary\n", 12801 __func__, rq->local); 12802 } 12803 if (((uintptr_t)rq->remote & 0x7) != 0) { 12804 printf("%s: warning: remote %p not on 8b boundary\n", 12805 __func__, rq->local); 12806 } 12807#if 0 12808 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12809 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12810 rq->local, rq->remote, rq->size); 12811#endif 12812 12813 isc_ret = ctl_dt_single(rq); 12814 if (isc_ret == CTL_HA_STATUS_WAIT) 12815 continue; 12816 12817 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 12818 rq->ret = CTL_HA_STATUS_SUCCESS; 12819 } else { 12820 rq->ret = isc_ret; 12821 } 12822 callback(rq); 12823 goto bailout; 12824 } 12825 12826bailout: 12827 return (retval); 12828 12829} 12830 12831static void 12832ctl_datamove_remote_read(union ctl_io *io) 12833{ 12834 int retval; 12835 int i; 12836 12837 /* 12838 * This will send an error to the other controller in the case of a 12839 * failure. 12840 */ 12841 retval = ctl_datamove_remote_sgl_setup(io); 12842 if (retval != 0) 12843 return; 12844 12845 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12846 ctl_datamove_remote_read_cb); 12847 if ((retval != 0) 12848 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 12849 /* 12850 * Make sure we free memory if there was an error.. The 12851 * ctl_datamove_remote_xfer() function will send the 12852 * datamove done message, or call the callback with an 12853 * error if there is a problem. 12854 */ 12855 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12856 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12857 } 12858 12859 return; 12860} 12861 12862/* 12863 * Process a datamove request from the other controller. This is used for 12864 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12865 * first. Once that is complete, the data gets DMAed into the remote 12866 * controller's memory. For reads, we DMA from the remote controller's 12867 * memory into our memory first, and then move it out to the FETD. 12868 */ 12869static void 12870ctl_datamove_remote(union ctl_io *io) 12871{ 12872 struct ctl_softc *softc; 12873 12874 softc = control_softc; 12875 12876 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 12877 12878 /* 12879 * Note that we look for an aborted I/O here, but don't do some of 12880 * the other checks that ctl_datamove() normally does. We don't 12881 * need to run the task queue, because this I/O is on the ISC 12882 * queue, which is executed by the work thread after the task queue. 12883 * We don't need to run the datamove delay code, since that should 12884 * have been done if need be on the other controller. 12885 */ 12886 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12887 12888 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 12889 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 12890 io->io_hdr.nexus.targ_port, 12891 io->io_hdr.nexus.targ_target.id, 12892 io->io_hdr.nexus.targ_lun); 12893 io->io_hdr.status = CTL_CMD_ABORTED; 12894 io->io_hdr.port_status = 31338; 12895 12896 ctl_send_datamove_done(io, /*have_lock*/ 0); 12897 12898 return; 12899 } 12900 12901 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 12902 ctl_datamove_remote_write(io); 12903 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 12904 ctl_datamove_remote_read(io); 12905 } else { 12906 union ctl_ha_msg msg; 12907 struct scsi_sense_data *sense; 12908 uint8_t sks[3]; 12909 int retry_count; 12910 12911 memset(&msg, 0, sizeof(msg)); 12912 12913 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 12914 msg.hdr.status = CTL_SCSI_ERROR; 12915 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 12916 12917 retry_count = 4243; 12918 12919 sense = &msg.scsi.sense_data; 12920 sks[0] = SSD_SCS_VALID; 12921 sks[1] = (retry_count >> 8) & 0xff; 12922 sks[2] = retry_count & 0xff; 12923 12924 /* "Internal target failure" */ 12925 scsi_set_sense_data(sense, 12926 /*sense_format*/ SSD_TYPE_NONE, 12927 /*current_error*/ 1, 12928 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 12929 /*asc*/ 0x44, 12930 /*ascq*/ 0x00, 12931 /*type*/ SSD_ELEM_SKS, 12932 /*size*/ sizeof(sks), 12933 /*data*/ sks, 12934 SSD_ELEM_NONE); 12935 12936 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12937 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12938 ctl_failover_io(io, /*have_lock*/ 1); 12939 return; 12940 } 12941 12942 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 12943 CTL_HA_STATUS_SUCCESS) { 12944 /* XXX KDM what to do if this fails? */ 12945 } 12946 return; 12947 } 12948 12949} 12950 12951static int 12952ctl_process_done(union ctl_io *io) 12953{ 12954 struct ctl_lun *lun; 12955 struct ctl_softc *ctl_softc; 12956 void (*fe_done)(union ctl_io *io); 12957 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 12958 12959 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12960 12961 fe_done = 12962 control_softc->ctl_ports[targ_port]->fe_done; 12963 12964#ifdef CTL_TIME_IO 12965 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12966 char str[256]; 12967 char path_str[64]; 12968 struct sbuf sb; 12969 12970 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12971 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12972 12973 sbuf_cat(&sb, path_str); 12974 switch (io->io_hdr.io_type) { 12975 case CTL_IO_SCSI: 12976 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12977 sbuf_printf(&sb, "\n"); 12978 sbuf_cat(&sb, path_str); 12979 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12980 io->scsiio.tag_num, io->scsiio.tag_type); 12981 break; 12982 case CTL_IO_TASK: 12983 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12984 "Tag Type: %d\n", io->taskio.task_action, 12985 io->taskio.tag_num, io->taskio.tag_type); 12986 break; 12987 default: 12988 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12989 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12990 break; 12991 } 12992 sbuf_cat(&sb, path_str); 12993 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12994 (intmax_t)time_uptime - io->io_hdr.start_time); 12995 sbuf_finish(&sb); 12996 printf("%s", sbuf_data(&sb)); 12997 } 12998#endif /* CTL_TIME_IO */ 12999 13000 switch (io->io_hdr.io_type) { 13001 case CTL_IO_SCSI: 13002 break; 13003 case CTL_IO_TASK: 13004 if (bootverbose || verbose > 0) 13005 ctl_io_error_print(io, NULL); 13006 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13007 ctl_free_io(io); 13008 else 13009 fe_done(io); 13010 return (CTL_RETVAL_COMPLETE); 13011 break; 13012 default: 13013 printf("ctl_process_done: invalid io type %d\n", 13014 io->io_hdr.io_type); 13015 panic("ctl_process_done: invalid io type %d\n", 13016 io->io_hdr.io_type); 13017 break; /* NOTREACHED */ 13018 } 13019 13020 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13021 if (lun == NULL) { 13022 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13023 io->io_hdr.nexus.targ_mapped_lun)); 13024 fe_done(io); 13025 goto bailout; 13026 } 13027 ctl_softc = lun->ctl_softc; 13028 13029 mtx_lock(&lun->lun_lock); 13030 13031 /* 13032 * Check to see if we have any errors to inject here. We only 13033 * inject errors for commands that don't already have errors set. 13034 */ 13035 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13036 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13037 ctl_inject_error(lun, io); 13038 13039 /* 13040 * XXX KDM how do we treat commands that aren't completed 13041 * successfully? 13042 * 13043 * XXX KDM should we also track I/O latency? 13044 */ 13045 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13046 io->io_hdr.io_type == CTL_IO_SCSI) { 13047#ifdef CTL_TIME_IO 13048 struct bintime cur_bt; 13049#endif 13050 int type; 13051 13052 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13053 CTL_FLAG_DATA_IN) 13054 type = CTL_STATS_READ; 13055 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13056 CTL_FLAG_DATA_OUT) 13057 type = CTL_STATS_WRITE; 13058 else 13059 type = CTL_STATS_NO_IO; 13060 13061 lun->stats.ports[targ_port].bytes[type] += 13062 io->scsiio.kern_total_len; 13063 lun->stats.ports[targ_port].operations[type]++; 13064#ifdef CTL_TIME_IO 13065 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13066 &io->io_hdr.dma_bt); 13067 lun->stats.ports[targ_port].num_dmas[type] += 13068 io->io_hdr.num_dmas; 13069 getbintime(&cur_bt); 13070 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13071 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13072#endif 13073 } 13074 13075 /* 13076 * Remove this from the OOA queue. 13077 */ 13078 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13079 13080 /* 13081 * Run through the blocked queue on this LUN and see if anything 13082 * has become unblocked, now that this transaction is done. 13083 */ 13084 ctl_check_blocked(lun); 13085 13086 /* 13087 * If the LUN has been invalidated, free it if there is nothing 13088 * left on its OOA queue. 13089 */ 13090 if ((lun->flags & CTL_LUN_INVALID) 13091 && TAILQ_EMPTY(&lun->ooa_queue)) { 13092 mtx_unlock(&lun->lun_lock); 13093 mtx_lock(&ctl_softc->ctl_lock); 13094 ctl_free_lun(lun); 13095 mtx_unlock(&ctl_softc->ctl_lock); 13096 } else 13097 mtx_unlock(&lun->lun_lock); 13098 13099 /* 13100 * If this command has been aborted, make sure we set the status 13101 * properly. The FETD is responsible for freeing the I/O and doing 13102 * whatever it needs to do to clean up its state. 13103 */ 13104 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13105 io->io_hdr.status = CTL_CMD_ABORTED; 13106 13107 /* 13108 * We print out status for every task management command. For SCSI 13109 * commands, we filter out any unit attention errors; they happen 13110 * on every boot, and would clutter up the log. Note: task 13111 * management commands aren't printed here, they are printed above, 13112 * since they should never even make it down here. 13113 */ 13114 switch (io->io_hdr.io_type) { 13115 case CTL_IO_SCSI: { 13116 int error_code, sense_key, asc, ascq; 13117 13118 sense_key = 0; 13119 13120 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 13121 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13122 /* 13123 * Since this is just for printing, no need to 13124 * show errors here. 13125 */ 13126 scsi_extract_sense_len(&io->scsiio.sense_data, 13127 io->scsiio.sense_len, 13128 &error_code, 13129 &sense_key, 13130 &asc, 13131 &ascq, 13132 /*show_errors*/ 0); 13133 } 13134 13135 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 13136 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 13137 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 13138 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 13139 13140 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 13141 ctl_softc->skipped_prints++; 13142 } else { 13143 uint32_t skipped_prints; 13144 13145 skipped_prints = ctl_softc->skipped_prints; 13146 13147 ctl_softc->skipped_prints = 0; 13148 ctl_softc->last_print_jiffies = time_uptime; 13149 13150 if (skipped_prints > 0) { 13151#ifdef NEEDTOPORT 13152 csevent_log(CSC_CTL | CSC_SHELF_SW | 13153 CTL_ERROR_REPORT, 13154 csevent_LogType_Trace, 13155 csevent_Severity_Information, 13156 csevent_AlertLevel_Green, 13157 csevent_FRU_Firmware, 13158 csevent_FRU_Unknown, 13159 "High CTL error volume, %d prints " 13160 "skipped", skipped_prints); 13161#endif 13162 } 13163 if (bootverbose || verbose > 0) 13164 ctl_io_error_print(io, NULL); 13165 } 13166 } 13167 break; 13168 } 13169 case CTL_IO_TASK: 13170 if (bootverbose || verbose > 0) 13171 ctl_io_error_print(io, NULL); 13172 break; 13173 default: 13174 break; 13175 } 13176 13177 /* 13178 * Tell the FETD or the other shelf controller we're done with this 13179 * command. Note that only SCSI commands get to this point. Task 13180 * management commands are completed above. 13181 * 13182 * We only send status to the other controller if we're in XFER 13183 * mode. In SER_ONLY mode, the I/O is done on the controller that 13184 * received the I/O (from CTL's perspective), and so the status is 13185 * generated there. 13186 * 13187 * XXX KDM if we hold the lock here, we could cause a deadlock 13188 * if the frontend comes back in in this context to queue 13189 * something. 13190 */ 13191 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13192 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13193 union ctl_ha_msg msg; 13194 13195 memset(&msg, 0, sizeof(msg)); 13196 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13197 msg.hdr.original_sc = io->io_hdr.original_sc; 13198 msg.hdr.nexus = io->io_hdr.nexus; 13199 msg.hdr.status = io->io_hdr.status; 13200 msg.scsi.scsi_status = io->scsiio.scsi_status; 13201 msg.scsi.tag_num = io->scsiio.tag_num; 13202 msg.scsi.tag_type = io->scsiio.tag_type; 13203 msg.scsi.sense_len = io->scsiio.sense_len; 13204 msg.scsi.sense_residual = io->scsiio.sense_residual; 13205 msg.scsi.residual = io->scsiio.residual; 13206 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13207 sizeof(io->scsiio.sense_data)); 13208 /* 13209 * We copy this whether or not this is an I/O-related 13210 * command. Otherwise, we'd have to go and check to see 13211 * whether it's a read/write command, and it really isn't 13212 * worth it. 13213 */ 13214 memcpy(&msg.scsi.lbalen, 13215 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13216 sizeof(msg.scsi.lbalen)); 13217 13218 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13219 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13220 /* XXX do something here */ 13221 } 13222 13223 ctl_free_io(io); 13224 } else 13225 fe_done(io); 13226 13227bailout: 13228 13229 return (CTL_RETVAL_COMPLETE); 13230} 13231 13232/* 13233 * Front end should call this if it doesn't do autosense. When the request 13234 * sense comes back in from the initiator, we'll dequeue this and send it. 13235 */ 13236int 13237ctl_queue_sense(union ctl_io *io) 13238{ 13239 struct ctl_lun *lun; 13240 struct ctl_softc *ctl_softc; 13241 uint32_t initidx, targ_lun; 13242 13243 ctl_softc = control_softc; 13244 13245 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13246 13247 /* 13248 * LUN lookup will likely move to the ctl_work_thread() once we 13249 * have our new queueing infrastructure (that doesn't put things on 13250 * a per-LUN queue initially). That is so that we can handle 13251 * things like an INQUIRY to a LUN that we don't have enabled. We 13252 * can't deal with that right now. 13253 */ 13254 mtx_lock(&ctl_softc->ctl_lock); 13255 13256 /* 13257 * If we don't have a LUN for this, just toss the sense 13258 * information. 13259 */ 13260 targ_lun = io->io_hdr.nexus.targ_lun; 13261 if (io->io_hdr.nexus.lun_map_fn != NULL) 13262 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 13263 if ((targ_lun < CTL_MAX_LUNS) 13264 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13265 lun = ctl_softc->ctl_luns[targ_lun]; 13266 else 13267 goto bailout; 13268 13269 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13270 13271 mtx_lock(&lun->lun_lock); 13272 /* 13273 * Already have CA set for this LUN...toss the sense information. 13274 */ 13275 if (ctl_is_set(lun->have_ca, initidx)) { 13276 mtx_unlock(&lun->lun_lock); 13277 goto bailout; 13278 } 13279 13280 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data, 13281 ctl_min(sizeof(lun->pending_sense[initidx].sense), 13282 sizeof(io->scsiio.sense_data))); 13283 ctl_set_mask(lun->have_ca, initidx); 13284 mtx_unlock(&lun->lun_lock); 13285 13286bailout: 13287 mtx_unlock(&ctl_softc->ctl_lock); 13288 13289 ctl_free_io(io); 13290 13291 return (CTL_RETVAL_COMPLETE); 13292} 13293 13294/* 13295 * Primary command inlet from frontend ports. All SCSI and task I/O 13296 * requests must go through this function. 13297 */ 13298int 13299ctl_queue(union ctl_io *io) 13300{ 13301 struct ctl_softc *ctl_softc; 13302 13303 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13304 13305 ctl_softc = control_softc; 13306 13307#ifdef CTL_TIME_IO 13308 io->io_hdr.start_time = time_uptime; 13309 getbintime(&io->io_hdr.start_bt); 13310#endif /* CTL_TIME_IO */ 13311 13312 /* Map FE-specific LUN ID into global one. */ 13313 if (io->io_hdr.nexus.lun_map_fn != NULL) 13314 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.lun_map_fn( 13315 io->io_hdr.nexus.lun_map_arg, io->io_hdr.nexus.targ_lun); 13316 else 13317 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.targ_lun; 13318 13319 switch (io->io_hdr.io_type) { 13320 case CTL_IO_SCSI: 13321 case CTL_IO_TASK: 13322 ctl_enqueue_incoming(io); 13323 break; 13324 default: 13325 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13326 return (EINVAL); 13327 } 13328 13329 return (CTL_RETVAL_COMPLETE); 13330} 13331 13332#ifdef CTL_IO_DELAY 13333static void 13334ctl_done_timer_wakeup(void *arg) 13335{ 13336 union ctl_io *io; 13337 13338 io = (union ctl_io *)arg; 13339 ctl_done(io); 13340} 13341#endif /* CTL_IO_DELAY */ 13342 13343void 13344ctl_done(union ctl_io *io) 13345{ 13346 struct ctl_softc *ctl_softc; 13347 13348 ctl_softc = control_softc; 13349 13350 /* 13351 * Enable this to catch duplicate completion issues. 13352 */ 13353#if 0 13354 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13355 printf("%s: type %d msg %d cdb %x iptl: " 13356 "%d:%d:%d:%d tag 0x%04x " 13357 "flag %#x status %x\n", 13358 __func__, 13359 io->io_hdr.io_type, 13360 io->io_hdr.msg_type, 13361 io->scsiio.cdb[0], 13362 io->io_hdr.nexus.initid.id, 13363 io->io_hdr.nexus.targ_port, 13364 io->io_hdr.nexus.targ_target.id, 13365 io->io_hdr.nexus.targ_lun, 13366 (io->io_hdr.io_type == 13367 CTL_IO_TASK) ? 13368 io->taskio.tag_num : 13369 io->scsiio.tag_num, 13370 io->io_hdr.flags, 13371 io->io_hdr.status); 13372 } else 13373 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13374#endif 13375 13376 /* 13377 * This is an internal copy of an I/O, and should not go through 13378 * the normal done processing logic. 13379 */ 13380 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13381 return; 13382 13383 /* 13384 * We need to send a msg to the serializing shelf to finish the IO 13385 * as well. We don't send a finish message to the other shelf if 13386 * this is a task management command. Task management commands 13387 * aren't serialized in the OOA queue, but rather just executed on 13388 * both shelf controllers for commands that originated on that 13389 * controller. 13390 */ 13391 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13392 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13393 union ctl_ha_msg msg_io; 13394 13395 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13396 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13397 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13398 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13399 } 13400 /* continue on to finish IO */ 13401 } 13402#ifdef CTL_IO_DELAY 13403 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13404 struct ctl_lun *lun; 13405 13406 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13407 13408 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13409 } else { 13410 struct ctl_lun *lun; 13411 13412 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13413 13414 if ((lun != NULL) 13415 && (lun->delay_info.done_delay > 0)) { 13416 struct callout *callout; 13417 13418 callout = (struct callout *)&io->io_hdr.timer_bytes; 13419 callout_init(callout, /*mpsafe*/ 1); 13420 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13421 callout_reset(callout, 13422 lun->delay_info.done_delay * hz, 13423 ctl_done_timer_wakeup, io); 13424 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13425 lun->delay_info.done_delay = 0; 13426 return; 13427 } 13428 } 13429#endif /* CTL_IO_DELAY */ 13430 13431 ctl_enqueue_done(io); 13432} 13433 13434int 13435ctl_isc(struct ctl_scsiio *ctsio) 13436{ 13437 struct ctl_lun *lun; 13438 int retval; 13439 13440 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13441 13442 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13443 13444 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13445 13446 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13447 13448 return (retval); 13449} 13450 13451 13452static void 13453ctl_work_thread(void *arg) 13454{ 13455 struct ctl_thread *thr = (struct ctl_thread *)arg; 13456 struct ctl_softc *softc = thr->ctl_softc; 13457 union ctl_io *io; 13458 int retval; 13459 13460 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13461 13462 for (;;) { 13463 retval = 0; 13464 13465 /* 13466 * We handle the queues in this order: 13467 * - ISC 13468 * - done queue (to free up resources, unblock other commands) 13469 * - RtR queue 13470 * - incoming queue 13471 * 13472 * If those queues are empty, we break out of the loop and 13473 * go to sleep. 13474 */ 13475 mtx_lock(&thr->queue_lock); 13476 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13477 if (io != NULL) { 13478 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13479 mtx_unlock(&thr->queue_lock); 13480 ctl_handle_isc(io); 13481 continue; 13482 } 13483 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13484 if (io != NULL) { 13485 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13486 /* clear any blocked commands, call fe_done */ 13487 mtx_unlock(&thr->queue_lock); 13488 retval = ctl_process_done(io); 13489 continue; 13490 } 13491 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13492 if (io != NULL) { 13493 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13494 mtx_unlock(&thr->queue_lock); 13495 if (io->io_hdr.io_type == CTL_IO_TASK) 13496 ctl_run_task(io); 13497 else 13498 ctl_scsiio_precheck(softc, &io->scsiio); 13499 continue; 13500 } 13501 if (!ctl_pause_rtr) { 13502 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13503 if (io != NULL) { 13504 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13505 mtx_unlock(&thr->queue_lock); 13506 retval = ctl_scsiio(&io->scsiio); 13507 if (retval != CTL_RETVAL_COMPLETE) 13508 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13509 continue; 13510 } 13511 } 13512 13513 /* Sleep until we have something to do. */ 13514 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13515 } 13516} 13517 13518static void 13519ctl_lun_thread(void *arg) 13520{ 13521 struct ctl_softc *softc = (struct ctl_softc *)arg; 13522 struct ctl_be_lun *be_lun; 13523 int retval; 13524 13525 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13526 13527 for (;;) { 13528 retval = 0; 13529 mtx_lock(&softc->ctl_lock); 13530 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13531 if (be_lun != NULL) { 13532 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13533 mtx_unlock(&softc->ctl_lock); 13534 ctl_create_lun(be_lun); 13535 continue; 13536 } 13537 13538 /* Sleep until we have something to do. */ 13539 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13540 PDROP | PRIBIO, "-", 0); 13541 } 13542} 13543 13544static void 13545ctl_enqueue_incoming(union ctl_io *io) 13546{ 13547 struct ctl_softc *softc = control_softc; 13548 struct ctl_thread *thr; 13549 u_int idx; 13550 13551 idx = (io->io_hdr.nexus.targ_port * 127 + 13552 io->io_hdr.nexus.initid.id) % worker_threads; 13553 thr = &softc->threads[idx]; 13554 mtx_lock(&thr->queue_lock); 13555 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13556 mtx_unlock(&thr->queue_lock); 13557 wakeup(thr); 13558} 13559 13560static void 13561ctl_enqueue_rtr(union ctl_io *io) 13562{ 13563 struct ctl_softc *softc = control_softc; 13564 struct ctl_thread *thr; 13565 13566 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13567 mtx_lock(&thr->queue_lock); 13568 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13569 mtx_unlock(&thr->queue_lock); 13570 wakeup(thr); 13571} 13572 13573static void 13574ctl_enqueue_done(union ctl_io *io) 13575{ 13576 struct ctl_softc *softc = control_softc; 13577 struct ctl_thread *thr; 13578 13579 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13580 mtx_lock(&thr->queue_lock); 13581 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13582 mtx_unlock(&thr->queue_lock); 13583 wakeup(thr); 13584} 13585 13586static void 13587ctl_enqueue_isc(union ctl_io *io) 13588{ 13589 struct ctl_softc *softc = control_softc; 13590 struct ctl_thread *thr; 13591 13592 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13593 mtx_lock(&thr->queue_lock); 13594 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13595 mtx_unlock(&thr->queue_lock); 13596 wakeup(thr); 13597} 13598 13599/* Initialization and failover */ 13600 13601void 13602ctl_init_isc_msg(void) 13603{ 13604 printf("CTL: Still calling this thing\n"); 13605} 13606 13607/* 13608 * Init component 13609 * Initializes component into configuration defined by bootMode 13610 * (see hasc-sv.c) 13611 * returns hasc_Status: 13612 * OK 13613 * ERROR - fatal error 13614 */ 13615static ctl_ha_comp_status 13616ctl_isc_init(struct ctl_ha_component *c) 13617{ 13618 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13619 13620 c->status = ret; 13621 return ret; 13622} 13623 13624/* Start component 13625 * Starts component in state requested. If component starts successfully, 13626 * it must set its own state to the requestrd state 13627 * When requested state is HASC_STATE_HA, the component may refine it 13628 * by adding _SLAVE or _MASTER flags. 13629 * Currently allowed state transitions are: 13630 * UNKNOWN->HA - initial startup 13631 * UNKNOWN->SINGLE - initial startup when no parter detected 13632 * HA->SINGLE - failover 13633 * returns ctl_ha_comp_status: 13634 * OK - component successfully started in requested state 13635 * FAILED - could not start the requested state, failover may 13636 * be possible 13637 * ERROR - fatal error detected, no future startup possible 13638 */ 13639static ctl_ha_comp_status 13640ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 13641{ 13642 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13643 13644 printf("%s: go\n", __func__); 13645 13646 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 13647 if (c->state == CTL_HA_STATE_UNKNOWN ) { 13648 ctl_is_single = 0; 13649 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 13650 != CTL_HA_STATUS_SUCCESS) { 13651 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 13652 ret = CTL_HA_COMP_STATUS_ERROR; 13653 } 13654 } else if (CTL_HA_STATE_IS_HA(c->state) 13655 && CTL_HA_STATE_IS_SINGLE(state)){ 13656 // HA->SINGLE transition 13657 ctl_failover(); 13658 ctl_is_single = 1; 13659 } else { 13660 printf("ctl_isc_start:Invalid state transition %X->%X\n", 13661 c->state, state); 13662 ret = CTL_HA_COMP_STATUS_ERROR; 13663 } 13664 if (CTL_HA_STATE_IS_SINGLE(state)) 13665 ctl_is_single = 1; 13666 13667 c->state = state; 13668 c->status = ret; 13669 return ret; 13670} 13671 13672/* 13673 * Quiesce component 13674 * The component must clear any error conditions (set status to OK) and 13675 * prepare itself to another Start call 13676 * returns ctl_ha_comp_status: 13677 * OK 13678 * ERROR 13679 */ 13680static ctl_ha_comp_status 13681ctl_isc_quiesce(struct ctl_ha_component *c) 13682{ 13683 int ret = CTL_HA_COMP_STATUS_OK; 13684 13685 ctl_pause_rtr = 1; 13686 c->status = ret; 13687 return ret; 13688} 13689 13690struct ctl_ha_component ctl_ha_component_ctlisc = 13691{ 13692 .name = "CTL ISC", 13693 .state = CTL_HA_STATE_UNKNOWN, 13694 .init = ctl_isc_init, 13695 .start = ctl_isc_start, 13696 .quiesce = ctl_isc_quiesce 13697}; 13698 13699/* 13700 * vim: ts=8 13701 */ 13702