ctl.c revision 273979
1218585Sjkim/*- 2218585Sjkim * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3218585Sjkim * Copyright (c) 2012 The FreeBSD Foundation 4218585Sjkim * All rights reserved. 5218585Sjkim * 6218585Sjkim * Portions of this software were developed by Edward Tomasz Napierala 7218585Sjkim * under sponsorship from the FreeBSD Foundation. 8218585Sjkim * 9306536Sjkim * Redistribution and use in source and binary forms, with or without 10218585Sjkim * modification, are permitted provided that the following conditions 11218585Sjkim * are met: 12218585Sjkim * 1. Redistributions of source code must retain the above copyright 13218585Sjkim * notice, this list of conditions, and the following disclaimer, 14218585Sjkim * without modification. 15218585Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16218585Sjkim * substantially similar to the "NO WARRANTY" disclaimer below 17218585Sjkim * ("Disclaimer") and any redistribution must be conditioned upon 18218585Sjkim * including a substantially similar Disclaimer requirement for further 19218585Sjkim * binary redistribution. 20218585Sjkim * 21218585Sjkim * NO WARRANTY 22218585Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23218585Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24218585Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25218585Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26218585Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27218585Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28218585Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29218585Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30218585Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31218585Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32218585Sjkim * POSSIBILITY OF SUCH DAMAGES. 33218585Sjkim * 34218585Sjkim * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35218585Sjkim */ 36218585Sjkim/* 37218585Sjkim * CAM Target Layer, a SCSI device emulation subsystem. 38218585Sjkim * 39218585Sjkim * Author: Ken Merry <ken@FreeBSD.org> 40218585Sjkim */ 41218585Sjkim 42218585Sjkim#define _CTL_C 43218585Sjkim 44218585Sjkim#include <sys/cdefs.h> 45218590Sjkim__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 273979 2014-11-02 17:29:48Z mav $"); 46218590Sjkim 47218590Sjkim#include <sys/param.h> 48218590Sjkim#include <sys/systm.h> 49218590Sjkim#include <sys/ctype.h> 50306536Sjkim#include <sys/kernel.h> 51218585Sjkim#include <sys/types.h> 52218585Sjkim#include <sys/kthread.h> 53218585Sjkim#include <sys/bio.h> 54218585Sjkim#include <sys/fcntl.h> 55218585Sjkim#include <sys/lock.h> 56218585Sjkim#include <sys/module.h> 57218585Sjkim#include <sys/mutex.h> 58218585Sjkim#include <sys/condvar.h> 59218585Sjkim#include <sys/malloc.h> 60218585Sjkim#include <sys/conf.h> 61218585Sjkim#include <sys/ioccom.h> 62218585Sjkim#include <sys/queue.h> 63218585Sjkim#include <sys/sbuf.h> 64218585Sjkim#include <sys/smp.h> 65218585Sjkim#include <sys/endian.h> 66218585Sjkim#include <sys/sysctl.h> 67218585Sjkim 68218585Sjkim#include <cam/cam.h> 69218585Sjkim#include <cam/scsi/scsi_all.h> 70218585Sjkim#include <cam/scsi/scsi_da.h> 71218585Sjkim#include <cam/ctl/ctl_io.h> 72218585Sjkim#include <cam/ctl/ctl.h> 73218585Sjkim#include <cam/ctl/ctl_frontend.h> 74218585Sjkim#include <cam/ctl/ctl_frontend_internal.h> 75218585Sjkim#include <cam/ctl/ctl_util.h> 76218585Sjkim#include <cam/ctl/ctl_backend.h> 77218585Sjkim#include <cam/ctl/ctl_ioctl.h> 78218585Sjkim#include <cam/ctl/ctl_ha.h> 79218585Sjkim#include <cam/ctl/ctl_private.h> 80218585Sjkim#include <cam/ctl/ctl_debug.h> 81218585Sjkim#include <cam/ctl/ctl_scsi_all.h> 82218585Sjkim#include <cam/ctl/ctl_error.h> 83218585Sjkim 84218585Sjkimstruct ctl_softc *control_softc = NULL; 85218585Sjkim 86218585Sjkim/* 87218585Sjkim * Size and alignment macros needed for Copan-specific HA hardware. These 88218585Sjkim * can go away when the HA code is re-written, and uses busdma for any 89218585Sjkim * hardware. 90218585Sjkim */ 91218585Sjkim#define CTL_ALIGN_8B(target, source, type) \ 92218585Sjkim if (((uint32_t)source & 0x7) != 0) \ 93218585Sjkim target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 94218585Sjkim else \ 95218585Sjkim target = (type)source; 96218585Sjkim 97218585Sjkim#define CTL_SIZE_8B(target, size) \ 98218585Sjkim if ((size & 0x7) != 0) \ 99218585Sjkim target = size + (0x8 - (size & 0x7)); \ 100218585Sjkim else \ 101218585Sjkim target = size; 102218585Sjkim 103218585Sjkim#define CTL_ALIGN_8B_MARGIN 16 104218585Sjkim 105218585Sjkim/* 106218585Sjkim * Template mode pages. 107218585Sjkim */ 108218585Sjkim 109218585Sjkim/* 110218585Sjkim * Note that these are default values only. The actual values will be 111218585Sjkim * filled in when the user does a mode sense. 112218585Sjkim */ 113218585Sjkimstatic struct copan_debugconf_subpage debugconf_page_default = { 114218585Sjkim DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 115218585Sjkim DBGCNF_SUBPAGE_CODE, /* subpage */ 116218585Sjkim {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 117218585Sjkim (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 118218585Sjkim DBGCNF_VERSION, /* page_version */ 119218585Sjkim {CTL_TIME_IO_DEFAULT_SECS>>8, 120218585Sjkim CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 121218585Sjkim}; 122218585Sjkim 123306536Sjkimstatic struct copan_debugconf_subpage debugconf_page_changeable = { 124306536Sjkim DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 125306536Sjkim DBGCNF_SUBPAGE_CODE, /* subpage */ 126306536Sjkim {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 127306536Sjkim (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 128306536Sjkim 0, /* page_version */ 129218585Sjkim {0xff,0xff}, /* ctl_time_io_secs */ 130218585Sjkim}; 131218585Sjkim 132218585Sjkimstatic struct scsi_da_rw_recovery_page rw_er_page_default = { 133218585Sjkim /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 134218585Sjkim /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 135218585Sjkim /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 136218585Sjkim /*read_retry_count*/0, 137218585Sjkim /*correction_span*/0, 138218585Sjkim /*head_offset_count*/0, 139218585Sjkim /*data_strobe_offset_cnt*/0, 140218585Sjkim /*byte8*/0, 141218585Sjkim /*write_retry_count*/0, 142218585Sjkim /*reserved2*/0, 143218585Sjkim /*recovery_time_limit*/{0, 0}, 144218585Sjkim}; 145218585Sjkim 146218585Sjkimstatic struct scsi_da_rw_recovery_page rw_er_page_changeable = { 147218585Sjkim /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 148218585Sjkim /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 149218585Sjkim /*byte3*/0, 150218585Sjkim /*read_retry_count*/0, 151218585Sjkim /*correction_span*/0, 152218585Sjkim /*head_offset_count*/0, 153250838Sjkim /*data_strobe_offset_cnt*/0, 154218585Sjkim /*byte8*/0, 155218585Sjkim /*write_retry_count*/0, 156218585Sjkim /*reserved2*/0, 157218585Sjkim /*recovery_time_limit*/{0, 0}, 158218585Sjkim}; 159218585Sjkim 160218585Sjkimstatic struct scsi_format_page format_page_default = { 161218585Sjkim /*page_code*/SMS_FORMAT_DEVICE_PAGE, 162218585Sjkim /*page_length*/sizeof(struct scsi_format_page) - 2, 163218585Sjkim /*tracks_per_zone*/ {0, 0}, 164218585Sjkim /*alt_sectors_per_zone*/ {0, 0}, 165218585Sjkim /*alt_tracks_per_zone*/ {0, 0}, 166218585Sjkim /*alt_tracks_per_lun*/ {0, 0}, 167218585Sjkim /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 168218585Sjkim CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 169218585Sjkim /*bytes_per_sector*/ {0, 0}, 170218585Sjkim /*interleave*/ {0, 0}, 171218585Sjkim /*track_skew*/ {0, 0}, 172218585Sjkim /*cylinder_skew*/ {0, 0}, 173218585Sjkim /*flags*/ SFP_HSEC, 174218585Sjkim /*reserved*/ {0, 0, 0} 175218585Sjkim}; 176218585Sjkim 177218585Sjkimstatic struct scsi_format_page format_page_changeable = { 178218585Sjkim /*page_code*/SMS_FORMAT_DEVICE_PAGE, 179218585Sjkim /*page_length*/sizeof(struct scsi_format_page) - 2, 180218585Sjkim /*tracks_per_zone*/ {0, 0}, 181218585Sjkim /*alt_sectors_per_zone*/ {0, 0}, 182218585Sjkim /*alt_tracks_per_zone*/ {0, 0}, 183218585Sjkim /*alt_tracks_per_lun*/ {0, 0}, 184218585Sjkim /*sectors_per_track*/ {0, 0}, 185218585Sjkim /*bytes_per_sector*/ {0, 0}, 186218585Sjkim /*interleave*/ {0, 0}, 187218585Sjkim /*track_skew*/ {0, 0}, 188218585Sjkim /*cylinder_skew*/ {0, 0}, 189218585Sjkim /*flags*/ 0, 190218585Sjkim /*reserved*/ {0, 0, 0} 191218585Sjkim}; 192218585Sjkim 193218585Sjkimstatic struct scsi_rigid_disk_page rigid_disk_page_default = { 194218585Sjkim /*page_code*/SMS_RIGID_DISK_PAGE, 195218585Sjkim /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 196218585Sjkim /*cylinders*/ {0, 0, 0}, 197218585Sjkim /*heads*/ CTL_DEFAULT_HEADS, 198218585Sjkim /*start_write_precomp*/ {0, 0, 0}, 199218585Sjkim /*start_reduced_current*/ {0, 0, 0}, 200218585Sjkim /*step_rate*/ {0, 0}, 201218585Sjkim /*landing_zone_cylinder*/ {0, 0, 0}, 202218585Sjkim /*rpl*/ SRDP_RPL_DISABLED, 203218585Sjkim /*rotational_offset*/ 0, 204218585Sjkim /*reserved1*/ 0, 205218585Sjkim /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 206218585Sjkim CTL_DEFAULT_ROTATION_RATE & 0xff}, 207218585Sjkim /*reserved2*/ {0, 0} 208218585Sjkim}; 209218585Sjkim 210218585Sjkimstatic struct scsi_rigid_disk_page rigid_disk_page_changeable = { 211218585Sjkim /*page_code*/SMS_RIGID_DISK_PAGE, 212218585Sjkim /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 213218585Sjkim /*cylinders*/ {0, 0, 0}, 214218585Sjkim /*heads*/ 0, 215218585Sjkim /*start_write_precomp*/ {0, 0, 0}, 216218585Sjkim /*start_reduced_current*/ {0, 0, 0}, 217218585Sjkim /*step_rate*/ {0, 0}, 218218585Sjkim /*landing_zone_cylinder*/ {0, 0, 0}, 219218585Sjkim /*rpl*/ 0, 220218585Sjkim /*rotational_offset*/ 0, 221218585Sjkim /*reserved1*/ 0, 222218585Sjkim /*rotation_rate*/ {0, 0}, 223218585Sjkim /*reserved2*/ {0, 0} 224218585Sjkim}; 225218585Sjkim 226218585Sjkimstatic struct scsi_caching_page caching_page_default = { 227218585Sjkim /*page_code*/SMS_CACHING_PAGE, 228306536Sjkim /*page_length*/sizeof(struct scsi_caching_page) - 2, 229218585Sjkim /*flags1*/ SCP_DISC | SCP_WCE, 230218585Sjkim /*ret_priority*/ 0, 231218585Sjkim /*disable_pf_transfer_len*/ {0xff, 0xff}, 232218585Sjkim /*min_prefetch*/ {0, 0}, 233218585Sjkim /*max_prefetch*/ {0xff, 0xff}, 234218585Sjkim /*max_pf_ceiling*/ {0xff, 0xff}, 235218585Sjkim /*flags2*/ 0, 236218585Sjkim /*cache_segments*/ 0, 237218585Sjkim /*cache_seg_size*/ {0, 0}, 238218585Sjkim /*reserved*/ 0, 239306536Sjkim /*non_cache_seg_size*/ {0, 0, 0} 240306536Sjkim}; 241218585Sjkim 242218585Sjkimstatic struct scsi_caching_page caching_page_changeable = { 243218585Sjkim /*page_code*/SMS_CACHING_PAGE, 244218585Sjkim /*page_length*/sizeof(struct scsi_caching_page) - 2, 245218585Sjkim /*flags1*/ SCP_WCE | SCP_RCD, 246218585Sjkim /*ret_priority*/ 0, 247218585Sjkim /*disable_pf_transfer_len*/ {0, 0}, 248218585Sjkim /*min_prefetch*/ {0, 0}, 249218585Sjkim /*max_prefetch*/ {0, 0}, 250218585Sjkim /*max_pf_ceiling*/ {0, 0}, 251218585Sjkim /*flags2*/ 0, 252218585Sjkim /*cache_segments*/ 0, 253218585Sjkim /*cache_seg_size*/ {0, 0}, 254218585Sjkim /*reserved*/ 0, 255218585Sjkim /*non_cache_seg_size*/ {0, 0, 0} 256218585Sjkim}; 257218585Sjkim 258218585Sjkimstatic struct scsi_control_page control_page_default = { 259218585Sjkim /*page_code*/SMS_CONTROL_MODE_PAGE, 260218585Sjkim /*page_length*/sizeof(struct scsi_control_page) - 2, 261218585Sjkim /*rlec*/0, 262218585Sjkim /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 263218585Sjkim /*eca_and_aen*/0, 264218585Sjkim /*flags4*/SCP_TAS, 265218585Sjkim /*aen_holdoff_period*/{0, 0}, 266218585Sjkim /*busy_timeout_period*/{0, 0}, 267218585Sjkim /*extended_selftest_completion_time*/{0, 0} 268218585Sjkim}; 269218585Sjkim 270218585Sjkimstatic struct scsi_control_page control_page_changeable = { 271218585Sjkim /*page_code*/SMS_CONTROL_MODE_PAGE, 272218585Sjkim /*page_length*/sizeof(struct scsi_control_page) - 2, 273218585Sjkim /*rlec*/SCP_DSENSE, 274218585Sjkim /*queue_flags*/SCP_QUEUE_ALG_MASK, 275218585Sjkim /*eca_and_aen*/SCP_SWP, 276218585Sjkim /*flags4*/0, 277218585Sjkim /*aen_holdoff_period*/{0, 0}, 278218585Sjkim /*busy_timeout_period*/{0, 0}, 279218585Sjkim /*extended_selftest_completion_time*/{0, 0} 280218585Sjkim}; 281218585Sjkim 282218585Sjkimstatic struct scsi_info_exceptions_page ie_page_default = { 283218585Sjkim /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 284306536Sjkim /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 285306536Sjkim /*info_flags*/SIEP_FLAGS_DEXCPT, 286218585Sjkim /*mrie*/0, 287218585Sjkim /*interval_timer*/{0, 0, 0, 0}, 288218585Sjkim /*report_count*/{0, 0, 0, 0} 289218585Sjkim}; 290218585Sjkim 291218585Sjkimstatic struct scsi_info_exceptions_page ie_page_changeable = { 292218585Sjkim /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 293241973Sjkim /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 294218585Sjkim /*info_flags*/0, 295218585Sjkim /*mrie*/0, 296218585Sjkim /*interval_timer*/{0, 0, 0, 0}, 297218585Sjkim /*report_count*/{0, 0, 0, 0} 298218585Sjkim}; 299218585Sjkim 300218585Sjkimstatic struct scsi_logical_block_provisioning_page lbp_page_default = { 301218585Sjkim /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 302218585Sjkim /*subpage_code*/0x02, 303218585Sjkim /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4}, 304218585Sjkim /*flags*/0, 305218585Sjkim /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 306218585Sjkim /*descr*/{} 307218585Sjkim}; 308218585Sjkim 309218585Sjkimstatic struct scsi_logical_block_provisioning_page lbp_page_changeable = { 310218585Sjkim /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 311218585Sjkim /*subpage_code*/0x02, 312218585Sjkim /*page_length*/{0, sizeof(struct scsi_logical_block_provisioning_page) - 4}, 313218585Sjkim /*flags*/0, 314306536Sjkim /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 315306536Sjkim /*descr*/{} 316306536Sjkim}; 317306536Sjkim 318306536Sjkim/* 319306536Sjkim * XXX KDM move these into the softc. 320218585Sjkim */ 321306536Sjkimstatic int rcv_sync_msg; 322306536Sjkimstatic int persis_offset; 323218585Sjkimstatic uint8_t ctl_pause_rtr; 324218585Sjkimstatic int ctl_is_single = 1; 325218585Sjkim 326218585SjkimSYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 327218585Sjkimstatic int worker_threads = -1; 328218585SjkimTUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 329218585SjkimSYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 330218585Sjkim &worker_threads, 1, "Number of worker threads"); 331218585Sjkimstatic int ctl_debug = CTL_DEBUG_NONE; 332218585SjkimTUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 333218585SjkimSYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 334218585Sjkim &ctl_debug, 0, "Enabled debug flags"); 335218585Sjkim 336218585Sjkim/* 337218585Sjkim * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 338218585Sjkim * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 339218585Sjkim * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 340306536Sjkim * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 341306536Sjkim */ 342306536Sjkim#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 343218585Sjkim 344218585Sjkimstatic void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 345218585Sjkim int param); 346218585Sjkimstatic void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 347218585Sjkimstatic int ctl_init(void); 348218585Sjkimvoid ctl_shutdown(void); 349218585Sjkimstatic int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 350218585Sjkimstatic int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 351218585Sjkimstatic void ctl_ioctl_online(void *arg); 352218585Sjkimstatic void ctl_ioctl_offline(void *arg); 353218585Sjkimstatic int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 354218585Sjkimstatic int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 355218585Sjkimstatic int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 356218585Sjkimstatic int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 357218585Sjkimstatic int ctl_ioctl_submit_wait(union ctl_io *io); 358250838Sjkimstatic void ctl_ioctl_datamove(union ctl_io *io); 359218585Sjkimstatic void ctl_ioctl_done(union ctl_io *io); 360218585Sjkimstatic void ctl_ioctl_hard_startstop_callback(void *arg, 361218585Sjkim struct cfi_metatask *metatask); 362218585Sjkimstatic void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 363306536Sjkimstatic int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 364218585Sjkim struct ctl_ooa *ooa_hdr, 365218585Sjkim struct ctl_ooa_entry *kern_entries); 366218585Sjkimstatic int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 367218585Sjkim struct thread *td); 368306536Sjkimstatic uint32_t ctl_map_lun(int port_num, uint32_t lun); 369218585Sjkimstatic uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 370218585Sjkim#ifdef unused 371218585Sjkimstatic union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 372218585Sjkim uint32_t targ_target, uint32_t targ_lun, 373218585Sjkim int can_wait); 374218585Sjkimstatic void ctl_kfree_io(union ctl_io *io); 375218585Sjkim#endif /* unused */ 376218585Sjkimstatic int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 377218585Sjkim struct ctl_be_lun *be_lun, struct ctl_id target_id); 378218585Sjkimstatic int ctl_free_lun(struct ctl_lun *lun); 379218585Sjkimstatic void ctl_create_lun(struct ctl_be_lun *be_lun); 380218585Sjkim/** 381218585Sjkimstatic void ctl_failover_change_pages(struct ctl_softc *softc, 382218585Sjkim struct ctl_scsiio *ctsio, int master); 383218585Sjkim**/ 384218585Sjkim 385218585Sjkimstatic int ctl_do_mode_select(union ctl_io *io); 386218585Sjkimstatic int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 387218585Sjkim uint64_t res_key, uint64_t sa_res_key, 388218585Sjkim uint8_t type, uint32_t residx, 389218585Sjkim struct ctl_scsiio *ctsio, 390218585Sjkim struct scsi_per_res_out *cdb, 391218585Sjkim struct scsi_per_res_out_parms* param); 392306536Sjkimstatic void ctl_pro_preempt_other(struct ctl_lun *lun, 393306536Sjkim union ctl_ha_msg *msg); 394218585Sjkimstatic void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 395218585Sjkimstatic int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 396218585Sjkimstatic int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 397218585Sjkimstatic int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 398218585Sjkimstatic int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 399218585Sjkimstatic int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 400218585Sjkimstatic int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 401218585Sjkim int alloc_len); 402218585Sjkimstatic int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 403218585Sjkim int alloc_len); 404218585Sjkimstatic int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 405218585Sjkimstatic int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 406218585Sjkimstatic int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 407218585Sjkimstatic int ctl_inquiry_std(struct ctl_scsiio *ctsio); 408218585Sjkimstatic int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 409218585Sjkimstatic ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 410218585Sjkimstatic ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 411218585Sjkim union ctl_io *pending_io, union ctl_io *ooa_io); 412218585Sjkimstatic ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 413218585Sjkim union ctl_io *starting_io); 414218585Sjkimstatic int ctl_check_blocked(struct ctl_lun *lun); 415218585Sjkimstatic int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 416218585Sjkim struct ctl_lun *lun, 417218585Sjkim const struct ctl_cmd_entry *entry, 418 struct ctl_scsiio *ctsio); 419//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 420static void ctl_failover(void); 421static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 422 struct ctl_scsiio *ctsio); 423static int ctl_scsiio(struct ctl_scsiio *ctsio); 424 425static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 426static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 427 ctl_ua_type ua_type); 428static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 429 ctl_ua_type ua_type); 430static int ctl_abort_task(union ctl_io *io); 431static int ctl_abort_task_set(union ctl_io *io); 432static int ctl_i_t_nexus_reset(union ctl_io *io); 433static void ctl_run_task(union ctl_io *io); 434#ifdef CTL_IO_DELAY 435static void ctl_datamove_timer_wakeup(void *arg); 436static void ctl_done_timer_wakeup(void *arg); 437#endif /* CTL_IO_DELAY */ 438 439static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 440static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 441static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 442static void ctl_datamove_remote_write(union ctl_io *io); 443static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 444static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 445static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 446static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 447 ctl_ha_dt_cb callback); 448static void ctl_datamove_remote_read(union ctl_io *io); 449static void ctl_datamove_remote(union ctl_io *io); 450static int ctl_process_done(union ctl_io *io); 451static void ctl_lun_thread(void *arg); 452static void ctl_work_thread(void *arg); 453static void ctl_enqueue_incoming(union ctl_io *io); 454static void ctl_enqueue_rtr(union ctl_io *io); 455static void ctl_enqueue_done(union ctl_io *io); 456static void ctl_enqueue_isc(union ctl_io *io); 457static const struct ctl_cmd_entry * 458 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 459static const struct ctl_cmd_entry * 460 ctl_validate_command(struct ctl_scsiio *ctsio); 461static int ctl_cmd_applicable(uint8_t lun_type, 462 const struct ctl_cmd_entry *entry); 463 464/* 465 * Load the serialization table. This isn't very pretty, but is probably 466 * the easiest way to do it. 467 */ 468#include "ctl_ser_table.c" 469 470/* 471 * We only need to define open, close and ioctl routines for this driver. 472 */ 473static struct cdevsw ctl_cdevsw = { 474 .d_version = D_VERSION, 475 .d_flags = 0, 476 .d_open = ctl_open, 477 .d_close = ctl_close, 478 .d_ioctl = ctl_ioctl, 479 .d_name = "ctl", 480}; 481 482 483MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 484MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 485 486static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 487 488static moduledata_t ctl_moduledata = { 489 "ctl", 490 ctl_module_event_handler, 491 NULL 492}; 493 494DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 495MODULE_VERSION(ctl, 1); 496 497static struct ctl_frontend ioctl_frontend = 498{ 499 .name = "ioctl", 500}; 501 502static void 503ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 504 union ctl_ha_msg *msg_info) 505{ 506 struct ctl_scsiio *ctsio; 507 508 if (msg_info->hdr.original_sc == NULL) { 509 printf("%s: original_sc == NULL!\n", __func__); 510 /* XXX KDM now what? */ 511 return; 512 } 513 514 ctsio = &msg_info->hdr.original_sc->scsiio; 515 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 516 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 517 ctsio->io_hdr.status = msg_info->hdr.status; 518 ctsio->scsi_status = msg_info->scsi.scsi_status; 519 ctsio->sense_len = msg_info->scsi.sense_len; 520 ctsio->sense_residual = msg_info->scsi.sense_residual; 521 ctsio->residual = msg_info->scsi.residual; 522 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 523 sizeof(ctsio->sense_data)); 524 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 525 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 526 ctl_enqueue_isc((union ctl_io *)ctsio); 527} 528 529static void 530ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 531 union ctl_ha_msg *msg_info) 532{ 533 struct ctl_scsiio *ctsio; 534 535 if (msg_info->hdr.serializing_sc == NULL) { 536 printf("%s: serializing_sc == NULL!\n", __func__); 537 /* XXX KDM now what? */ 538 return; 539 } 540 541 ctsio = &msg_info->hdr.serializing_sc->scsiio; 542#if 0 543 /* 544 * Attempt to catch the situation where an I/O has 545 * been freed, and we're using it again. 546 */ 547 if (ctsio->io_hdr.io_type == 0xff) { 548 union ctl_io *tmp_io; 549 tmp_io = (union ctl_io *)ctsio; 550 printf("%s: %p use after free!\n", __func__, 551 ctsio); 552 printf("%s: type %d msg %d cdb %x iptl: " 553 "%d:%d:%d:%d tag 0x%04x " 554 "flag %#x status %x\n", 555 __func__, 556 tmp_io->io_hdr.io_type, 557 tmp_io->io_hdr.msg_type, 558 tmp_io->scsiio.cdb[0], 559 tmp_io->io_hdr.nexus.initid.id, 560 tmp_io->io_hdr.nexus.targ_port, 561 tmp_io->io_hdr.nexus.targ_target.id, 562 tmp_io->io_hdr.nexus.targ_lun, 563 (tmp_io->io_hdr.io_type == 564 CTL_IO_TASK) ? 565 tmp_io->taskio.tag_num : 566 tmp_io->scsiio.tag_num, 567 tmp_io->io_hdr.flags, 568 tmp_io->io_hdr.status); 569 } 570#endif 571 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 572 ctl_enqueue_isc((union ctl_io *)ctsio); 573} 574 575/* 576 * ISC (Inter Shelf Communication) event handler. Events from the HA 577 * subsystem come in here. 578 */ 579static void 580ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 581{ 582 struct ctl_softc *ctl_softc; 583 union ctl_io *io; 584 struct ctl_prio *presio; 585 ctl_ha_status isc_status; 586 587 ctl_softc = control_softc; 588 io = NULL; 589 590 591#if 0 592 printf("CTL: Isc Msg event %d\n", event); 593#endif 594 if (event == CTL_HA_EVT_MSG_RECV) { 595 union ctl_ha_msg msg_info; 596 597 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 598 sizeof(msg_info), /*wait*/ 0); 599#if 0 600 printf("CTL: msg_type %d\n", msg_info.msg_type); 601#endif 602 if (isc_status != 0) { 603 printf("Error receiving message, status = %d\n", 604 isc_status); 605 return; 606 } 607 608 switch (msg_info.hdr.msg_type) { 609 case CTL_MSG_SERIALIZE: 610#if 0 611 printf("Serialize\n"); 612#endif 613 io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 614 if (io == NULL) { 615 printf("ctl_isc_event_handler: can't allocate " 616 "ctl_io!\n"); 617 /* Bad Juju */ 618 /* Need to set busy and send msg back */ 619 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 620 msg_info.hdr.status = CTL_SCSI_ERROR; 621 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 622 msg_info.scsi.sense_len = 0; 623 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 624 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 625 } 626 goto bailout; 627 } 628 ctl_zero_io(io); 629 // populate ctsio from msg_info 630 io->io_hdr.io_type = CTL_IO_SCSI; 631 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 632 io->io_hdr.original_sc = msg_info.hdr.original_sc; 633#if 0 634 printf("pOrig %x\n", (int)msg_info.original_sc); 635#endif 636 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 637 CTL_FLAG_IO_ACTIVE; 638 /* 639 * If we're in serialization-only mode, we don't 640 * want to go through full done processing. Thus 641 * the COPY flag. 642 * 643 * XXX KDM add another flag that is more specific. 644 */ 645 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 646 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 647 io->io_hdr.nexus = msg_info.hdr.nexus; 648#if 0 649 printf("targ %d, port %d, iid %d, lun %d\n", 650 io->io_hdr.nexus.targ_target.id, 651 io->io_hdr.nexus.targ_port, 652 io->io_hdr.nexus.initid.id, 653 io->io_hdr.nexus.targ_lun); 654#endif 655 io->scsiio.tag_num = msg_info.scsi.tag_num; 656 io->scsiio.tag_type = msg_info.scsi.tag_type; 657 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 658 CTL_MAX_CDBLEN); 659 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 660 const struct ctl_cmd_entry *entry; 661 662 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 663 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 664 io->io_hdr.flags |= 665 entry->flags & CTL_FLAG_DATA_MASK; 666 } 667 ctl_enqueue_isc(io); 668 break; 669 670 /* Performed on the Originating SC, XFER mode only */ 671 case CTL_MSG_DATAMOVE: { 672 struct ctl_sg_entry *sgl; 673 int i, j; 674 675 io = msg_info.hdr.original_sc; 676 if (io == NULL) { 677 printf("%s: original_sc == NULL!\n", __func__); 678 /* XXX KDM do something here */ 679 break; 680 } 681 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 682 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 683 /* 684 * Keep track of this, we need to send it back over 685 * when the datamove is complete. 686 */ 687 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 688 689 if (msg_info.dt.sg_sequence == 0) { 690 /* 691 * XXX KDM we use the preallocated S/G list 692 * here, but we'll need to change this to 693 * dynamic allocation if we need larger S/G 694 * lists. 695 */ 696 if (msg_info.dt.kern_sg_entries > 697 sizeof(io->io_hdr.remote_sglist) / 698 sizeof(io->io_hdr.remote_sglist[0])) { 699 printf("%s: number of S/G entries " 700 "needed %u > allocated num %zd\n", 701 __func__, 702 msg_info.dt.kern_sg_entries, 703 sizeof(io->io_hdr.remote_sglist)/ 704 sizeof(io->io_hdr.remote_sglist[0])); 705 706 /* 707 * XXX KDM send a message back to 708 * the other side to shut down the 709 * DMA. The error will come back 710 * through via the normal channel. 711 */ 712 break; 713 } 714 sgl = io->io_hdr.remote_sglist; 715 memset(sgl, 0, 716 sizeof(io->io_hdr.remote_sglist)); 717 718 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 719 720 io->scsiio.kern_sg_entries = 721 msg_info.dt.kern_sg_entries; 722 io->scsiio.rem_sg_entries = 723 msg_info.dt.kern_sg_entries; 724 io->scsiio.kern_data_len = 725 msg_info.dt.kern_data_len; 726 io->scsiio.kern_total_len = 727 msg_info.dt.kern_total_len; 728 io->scsiio.kern_data_resid = 729 msg_info.dt.kern_data_resid; 730 io->scsiio.kern_rel_offset = 731 msg_info.dt.kern_rel_offset; 732 /* 733 * Clear out per-DMA flags. 734 */ 735 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 736 /* 737 * Add per-DMA flags that are set for this 738 * particular DMA request. 739 */ 740 io->io_hdr.flags |= msg_info.dt.flags & 741 CTL_FLAG_RDMA_MASK; 742 } else 743 sgl = (struct ctl_sg_entry *) 744 io->scsiio.kern_data_ptr; 745 746 for (i = msg_info.dt.sent_sg_entries, j = 0; 747 i < (msg_info.dt.sent_sg_entries + 748 msg_info.dt.cur_sg_entries); i++, j++) { 749 sgl[i].addr = msg_info.dt.sg_list[j].addr; 750 sgl[i].len = msg_info.dt.sg_list[j].len; 751 752#if 0 753 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 754 __func__, 755 msg_info.dt.sg_list[j].addr, 756 msg_info.dt.sg_list[j].len, 757 sgl[i].addr, sgl[i].len, j, i); 758#endif 759 } 760#if 0 761 memcpy(&sgl[msg_info.dt.sent_sg_entries], 762 msg_info.dt.sg_list, 763 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 764#endif 765 766 /* 767 * If this is the last piece of the I/O, we've got 768 * the full S/G list. Queue processing in the thread. 769 * Otherwise wait for the next piece. 770 */ 771 if (msg_info.dt.sg_last != 0) 772 ctl_enqueue_isc(io); 773 break; 774 } 775 /* Performed on the Serializing (primary) SC, XFER mode only */ 776 case CTL_MSG_DATAMOVE_DONE: { 777 if (msg_info.hdr.serializing_sc == NULL) { 778 printf("%s: serializing_sc == NULL!\n", 779 __func__); 780 /* XXX KDM now what? */ 781 break; 782 } 783 /* 784 * We grab the sense information here in case 785 * there was a failure, so we can return status 786 * back to the initiator. 787 */ 788 io = msg_info.hdr.serializing_sc; 789 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 790 io->io_hdr.status = msg_info.hdr.status; 791 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 792 io->scsiio.sense_len = msg_info.scsi.sense_len; 793 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 794 io->io_hdr.port_status = msg_info.scsi.fetd_status; 795 io->scsiio.residual = msg_info.scsi.residual; 796 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 797 sizeof(io->scsiio.sense_data)); 798 ctl_enqueue_isc(io); 799 break; 800 } 801 802 /* Preformed on Originating SC, SER_ONLY mode */ 803 case CTL_MSG_R2R: 804 io = msg_info.hdr.original_sc; 805 if (io == NULL) { 806 printf("%s: Major Bummer\n", __func__); 807 return; 808 } else { 809#if 0 810 printf("pOrig %x\n",(int) ctsio); 811#endif 812 } 813 io->io_hdr.msg_type = CTL_MSG_R2R; 814 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 815 ctl_enqueue_isc(io); 816 break; 817 818 /* 819 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 820 * mode. 821 * Performed on the Originating (i.e. secondary) SC in XFER 822 * mode 823 */ 824 case CTL_MSG_FINISH_IO: 825 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 826 ctl_isc_handler_finish_xfer(ctl_softc, 827 &msg_info); 828 else 829 ctl_isc_handler_finish_ser_only(ctl_softc, 830 &msg_info); 831 break; 832 833 /* Preformed on Originating SC */ 834 case CTL_MSG_BAD_JUJU: 835 io = msg_info.hdr.original_sc; 836 if (io == NULL) { 837 printf("%s: Bad JUJU!, original_sc is NULL!\n", 838 __func__); 839 break; 840 } 841 ctl_copy_sense_data(&msg_info, io); 842 /* 843 * IO should have already been cleaned up on other 844 * SC so clear this flag so we won't send a message 845 * back to finish the IO there. 846 */ 847 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 848 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 849 850 /* io = msg_info.hdr.serializing_sc; */ 851 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 852 ctl_enqueue_isc(io); 853 break; 854 855 /* Handle resets sent from the other side */ 856 case CTL_MSG_MANAGE_TASKS: { 857 struct ctl_taskio *taskio; 858 taskio = (struct ctl_taskio *)ctl_alloc_io( 859 (void *)ctl_softc->othersc_pool); 860 if (taskio == NULL) { 861 printf("ctl_isc_event_handler: can't allocate " 862 "ctl_io!\n"); 863 /* Bad Juju */ 864 /* should I just call the proper reset func 865 here??? */ 866 goto bailout; 867 } 868 ctl_zero_io((union ctl_io *)taskio); 869 taskio->io_hdr.io_type = CTL_IO_TASK; 870 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 871 taskio->io_hdr.nexus = msg_info.hdr.nexus; 872 taskio->task_action = msg_info.task.task_action; 873 taskio->tag_num = msg_info.task.tag_num; 874 taskio->tag_type = msg_info.task.tag_type; 875#ifdef CTL_TIME_IO 876 taskio->io_hdr.start_time = time_uptime; 877 getbintime(&taskio->io_hdr.start_bt); 878#if 0 879 cs_prof_gettime(&taskio->io_hdr.start_ticks); 880#endif 881#endif /* CTL_TIME_IO */ 882 ctl_run_task((union ctl_io *)taskio); 883 break; 884 } 885 /* Persistent Reserve action which needs attention */ 886 case CTL_MSG_PERS_ACTION: 887 presio = (struct ctl_prio *)ctl_alloc_io( 888 (void *)ctl_softc->othersc_pool); 889 if (presio == NULL) { 890 printf("ctl_isc_event_handler: can't allocate " 891 "ctl_io!\n"); 892 /* Bad Juju */ 893 /* Need to set busy and send msg back */ 894 goto bailout; 895 } 896 ctl_zero_io((union ctl_io *)presio); 897 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 898 presio->pr_msg = msg_info.pr; 899 ctl_enqueue_isc((union ctl_io *)presio); 900 break; 901 case CTL_MSG_SYNC_FE: 902 rcv_sync_msg = 1; 903 break; 904 default: 905 printf("How did I get here?\n"); 906 } 907 } else if (event == CTL_HA_EVT_MSG_SENT) { 908 if (param != CTL_HA_STATUS_SUCCESS) { 909 printf("Bad status from ctl_ha_msg_send status %d\n", 910 param); 911 } 912 return; 913 } else if (event == CTL_HA_EVT_DISCONNECT) { 914 printf("CTL: Got a disconnect from Isc\n"); 915 return; 916 } else { 917 printf("ctl_isc_event_handler: Unknown event %d\n", event); 918 return; 919 } 920 921bailout: 922 return; 923} 924 925static void 926ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 927{ 928 struct scsi_sense_data *sense; 929 930 sense = &dest->scsiio.sense_data; 931 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 932 dest->scsiio.scsi_status = src->scsi.scsi_status; 933 dest->scsiio.sense_len = src->scsi.sense_len; 934 dest->io_hdr.status = src->hdr.status; 935} 936 937static int 938ctl_init(void) 939{ 940 struct ctl_softc *softc; 941 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 942 struct ctl_port *port; 943 uint8_t sc_id =0; 944 int i, error, retval; 945 //int isc_retval; 946 947 retval = 0; 948 ctl_pause_rtr = 0; 949 rcv_sync_msg = 0; 950 951 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 952 M_WAITOK | M_ZERO); 953 softc = control_softc; 954 955 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 956 "cam/ctl"); 957 958 softc->dev->si_drv1 = softc; 959 960 /* 961 * By default, return a "bad LUN" peripheral qualifier for unknown 962 * LUNs. The user can override this default using the tunable or 963 * sysctl. See the comment in ctl_inquiry_std() for more details. 964 */ 965 softc->inquiry_pq_no_lun = 1; 966 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 967 &softc->inquiry_pq_no_lun); 968 sysctl_ctx_init(&softc->sysctl_ctx); 969 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 970 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 971 CTLFLAG_RD, 0, "CAM Target Layer"); 972 973 if (softc->sysctl_tree == NULL) { 974 printf("%s: unable to allocate sysctl tree\n", __func__); 975 destroy_dev(softc->dev); 976 free(control_softc, M_DEVBUF); 977 control_softc = NULL; 978 return (ENOMEM); 979 } 980 981 SYSCTL_ADD_INT(&softc->sysctl_ctx, 982 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 983 "inquiry_pq_no_lun", CTLFLAG_RW, 984 &softc->inquiry_pq_no_lun, 0, 985 "Report no lun possible for invalid LUNs"); 986 987 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 988 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 989 softc->open_count = 0; 990 991 /* 992 * Default to actually sending a SYNCHRONIZE CACHE command down to 993 * the drive. 994 */ 995 softc->flags = CTL_FLAG_REAL_SYNC; 996 997 /* 998 * In Copan's HA scheme, the "master" and "slave" roles are 999 * figured out through the slot the controller is in. Although it 1000 * is an active/active system, someone has to be in charge. 1001 */ 1002#ifdef NEEDTOPORT 1003 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 1004#endif 1005 1006 if (sc_id == 0) { 1007 softc->flags |= CTL_FLAG_MASTER_SHELF; 1008 persis_offset = 0; 1009 } else 1010 persis_offset = CTL_MAX_INITIATORS; 1011 1012 /* 1013 * XXX KDM need to figure out where we want to get our target ID 1014 * and WWID. Is it different on each port? 1015 */ 1016 softc->target.id = 0; 1017 softc->target.wwid[0] = 0x12345678; 1018 softc->target.wwid[1] = 0x87654321; 1019 STAILQ_INIT(&softc->lun_list); 1020 STAILQ_INIT(&softc->pending_lun_queue); 1021 STAILQ_INIT(&softc->fe_list); 1022 STAILQ_INIT(&softc->port_list); 1023 STAILQ_INIT(&softc->be_list); 1024 STAILQ_INIT(&softc->io_pools); 1025 ctl_tpc_init(softc); 1026 1027 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1028 &internal_pool)!= 0){ 1029 printf("ctl: can't allocate %d entry internal pool, " 1030 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1031 return (ENOMEM); 1032 } 1033 1034 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1035 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1036 printf("ctl: can't allocate %d entry emergency pool, " 1037 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1038 ctl_pool_free(internal_pool); 1039 return (ENOMEM); 1040 } 1041 1042 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1043 &other_pool) != 0) 1044 { 1045 printf("ctl: can't allocate %d entry other SC pool, " 1046 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1047 ctl_pool_free(internal_pool); 1048 ctl_pool_free(emergency_pool); 1049 return (ENOMEM); 1050 } 1051 1052 softc->internal_pool = internal_pool; 1053 softc->emergency_pool = emergency_pool; 1054 softc->othersc_pool = other_pool; 1055 1056 if (worker_threads <= 0) 1057 worker_threads = max(1, mp_ncpus / 4); 1058 if (worker_threads > CTL_MAX_THREADS) 1059 worker_threads = CTL_MAX_THREADS; 1060 1061 for (i = 0; i < worker_threads; i++) { 1062 struct ctl_thread *thr = &softc->threads[i]; 1063 1064 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1065 thr->ctl_softc = softc; 1066 STAILQ_INIT(&thr->incoming_queue); 1067 STAILQ_INIT(&thr->rtr_queue); 1068 STAILQ_INIT(&thr->done_queue); 1069 STAILQ_INIT(&thr->isc_queue); 1070 1071 error = kproc_kthread_add(ctl_work_thread, thr, 1072 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1073 if (error != 0) { 1074 printf("error creating CTL work thread!\n"); 1075 ctl_pool_free(internal_pool); 1076 ctl_pool_free(emergency_pool); 1077 ctl_pool_free(other_pool); 1078 return (error); 1079 } 1080 } 1081 error = kproc_kthread_add(ctl_lun_thread, softc, 1082 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1083 if (error != 0) { 1084 printf("error creating CTL lun thread!\n"); 1085 ctl_pool_free(internal_pool); 1086 ctl_pool_free(emergency_pool); 1087 ctl_pool_free(other_pool); 1088 return (error); 1089 } 1090 if (bootverbose) 1091 printf("ctl: CAM Target Layer loaded\n"); 1092 1093 /* 1094 * Initialize the ioctl front end. 1095 */ 1096 ctl_frontend_register(&ioctl_frontend); 1097 port = &softc->ioctl_info.port; 1098 port->frontend = &ioctl_frontend; 1099 sprintf(softc->ioctl_info.port_name, "ioctl"); 1100 port->port_type = CTL_PORT_IOCTL; 1101 port->num_requested_ctl_io = 100; 1102 port->port_name = softc->ioctl_info.port_name; 1103 port->port_online = ctl_ioctl_online; 1104 port->port_offline = ctl_ioctl_offline; 1105 port->onoff_arg = &softc->ioctl_info; 1106 port->lun_enable = ctl_ioctl_lun_enable; 1107 port->lun_disable = ctl_ioctl_lun_disable; 1108 port->targ_lun_arg = &softc->ioctl_info; 1109 port->fe_datamove = ctl_ioctl_datamove; 1110 port->fe_done = ctl_ioctl_done; 1111 port->max_targets = 15; 1112 port->max_target_id = 15; 1113 1114 if (ctl_port_register(&softc->ioctl_info.port, 1115 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1116 printf("ctl: ioctl front end registration failed, will " 1117 "continue anyway\n"); 1118 } 1119 1120#ifdef CTL_IO_DELAY 1121 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1122 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1123 sizeof(struct callout), CTL_TIMER_BYTES); 1124 return (EINVAL); 1125 } 1126#endif /* CTL_IO_DELAY */ 1127 1128 return (0); 1129} 1130 1131void 1132ctl_shutdown(void) 1133{ 1134 struct ctl_softc *softc; 1135 struct ctl_lun *lun, *next_lun; 1136 struct ctl_io_pool *pool; 1137 1138 softc = (struct ctl_softc *)control_softc; 1139 1140 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1141 printf("ctl: ioctl front end deregistration failed\n"); 1142 1143 mtx_lock(&softc->ctl_lock); 1144 1145 /* 1146 * Free up each LUN. 1147 */ 1148 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1149 next_lun = STAILQ_NEXT(lun, links); 1150 ctl_free_lun(lun); 1151 } 1152 1153 mtx_unlock(&softc->ctl_lock); 1154 1155 ctl_frontend_deregister(&ioctl_frontend); 1156 1157 /* 1158 * This will rip the rug out from under any FETDs or anyone else 1159 * that has a pool allocated. Since we increment our module 1160 * refcount any time someone outside the main CTL module allocates 1161 * a pool, we shouldn't have any problems here. The user won't be 1162 * able to unload the CTL module until client modules have 1163 * successfully unloaded. 1164 */ 1165 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1166 ctl_pool_free(pool); 1167 1168#if 0 1169 ctl_shutdown_thread(softc->work_thread); 1170 mtx_destroy(&softc->queue_lock); 1171#endif 1172 1173 ctl_tpc_shutdown(softc); 1174 mtx_destroy(&softc->pool_lock); 1175 mtx_destroy(&softc->ctl_lock); 1176 1177 destroy_dev(softc->dev); 1178 1179 sysctl_ctx_free(&softc->sysctl_ctx); 1180 1181 free(control_softc, M_DEVBUF); 1182 control_softc = NULL; 1183 1184 if (bootverbose) 1185 printf("ctl: CAM Target Layer unloaded\n"); 1186} 1187 1188static int 1189ctl_module_event_handler(module_t mod, int what, void *arg) 1190{ 1191 1192 switch (what) { 1193 case MOD_LOAD: 1194 return (ctl_init()); 1195 case MOD_UNLOAD: 1196 return (EBUSY); 1197 default: 1198 return (EOPNOTSUPP); 1199 } 1200} 1201 1202/* 1203 * XXX KDM should we do some access checks here? Bump a reference count to 1204 * prevent a CTL module from being unloaded while someone has it open? 1205 */ 1206static int 1207ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1208{ 1209 return (0); 1210} 1211 1212static int 1213ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1214{ 1215 return (0); 1216} 1217 1218int 1219ctl_port_enable(ctl_port_type port_type) 1220{ 1221 struct ctl_softc *softc; 1222 struct ctl_port *port; 1223 1224 if (ctl_is_single == 0) { 1225 union ctl_ha_msg msg_info; 1226 int isc_retval; 1227 1228#if 0 1229 printf("%s: HA mode, synchronizing frontend enable\n", 1230 __func__); 1231#endif 1232 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1233 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1234 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1235 printf("Sync msg send error retval %d\n", isc_retval); 1236 } 1237 if (!rcv_sync_msg) { 1238 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1239 sizeof(msg_info), 1); 1240 } 1241#if 0 1242 printf("CTL:Frontend Enable\n"); 1243 } else { 1244 printf("%s: single mode, skipping frontend synchronization\n", 1245 __func__); 1246#endif 1247 } 1248 1249 softc = control_softc; 1250 1251 STAILQ_FOREACH(port, &softc->port_list, links) { 1252 if (port_type & port->port_type) 1253 { 1254#if 0 1255 printf("port %d\n", port->targ_port); 1256#endif 1257 ctl_port_online(port); 1258 } 1259 } 1260 1261 return (0); 1262} 1263 1264int 1265ctl_port_disable(ctl_port_type port_type) 1266{ 1267 struct ctl_softc *softc; 1268 struct ctl_port *port; 1269 1270 softc = control_softc; 1271 1272 STAILQ_FOREACH(port, &softc->port_list, links) { 1273 if (port_type & port->port_type) 1274 ctl_port_offline(port); 1275 } 1276 1277 return (0); 1278} 1279 1280/* 1281 * Returns 0 for success, 1 for failure. 1282 * Currently the only failure mode is if there aren't enough entries 1283 * allocated. So, in case of a failure, look at num_entries_dropped, 1284 * reallocate and try again. 1285 */ 1286int 1287ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1288 int *num_entries_filled, int *num_entries_dropped, 1289 ctl_port_type port_type, int no_virtual) 1290{ 1291 struct ctl_softc *softc; 1292 struct ctl_port *port; 1293 int entries_dropped, entries_filled; 1294 int retval; 1295 int i; 1296 1297 softc = control_softc; 1298 1299 retval = 0; 1300 entries_filled = 0; 1301 entries_dropped = 0; 1302 1303 i = 0; 1304 mtx_lock(&softc->ctl_lock); 1305 STAILQ_FOREACH(port, &softc->port_list, links) { 1306 struct ctl_port_entry *entry; 1307 1308 if ((port->port_type & port_type) == 0) 1309 continue; 1310 1311 if ((no_virtual != 0) 1312 && (port->virtual_port != 0)) 1313 continue; 1314 1315 if (entries_filled >= num_entries_alloced) { 1316 entries_dropped++; 1317 continue; 1318 } 1319 entry = &entries[i]; 1320 1321 entry->port_type = port->port_type; 1322 strlcpy(entry->port_name, port->port_name, 1323 sizeof(entry->port_name)); 1324 entry->physical_port = port->physical_port; 1325 entry->virtual_port = port->virtual_port; 1326 entry->wwnn = port->wwnn; 1327 entry->wwpn = port->wwpn; 1328 1329 i++; 1330 entries_filled++; 1331 } 1332 1333 mtx_unlock(&softc->ctl_lock); 1334 1335 if (entries_dropped > 0) 1336 retval = 1; 1337 1338 *num_entries_dropped = entries_dropped; 1339 *num_entries_filled = entries_filled; 1340 1341 return (retval); 1342} 1343 1344static void 1345ctl_ioctl_online(void *arg) 1346{ 1347 struct ctl_ioctl_info *ioctl_info; 1348 1349 ioctl_info = (struct ctl_ioctl_info *)arg; 1350 1351 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1352} 1353 1354static void 1355ctl_ioctl_offline(void *arg) 1356{ 1357 struct ctl_ioctl_info *ioctl_info; 1358 1359 ioctl_info = (struct ctl_ioctl_info *)arg; 1360 1361 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1362} 1363 1364/* 1365 * Remove an initiator by port number and initiator ID. 1366 * Returns 0 for success, -1 for failure. 1367 */ 1368int 1369ctl_remove_initiator(struct ctl_port *port, int iid) 1370{ 1371 struct ctl_softc *softc = control_softc; 1372 1373 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1374 1375 if (iid > CTL_MAX_INIT_PER_PORT) { 1376 printf("%s: initiator ID %u > maximun %u!\n", 1377 __func__, iid, CTL_MAX_INIT_PER_PORT); 1378 return (-1); 1379 } 1380 1381 mtx_lock(&softc->ctl_lock); 1382 port->wwpn_iid[iid].in_use--; 1383 port->wwpn_iid[iid].last_use = time_uptime; 1384 mtx_unlock(&softc->ctl_lock); 1385 1386 return (0); 1387} 1388 1389/* 1390 * Add an initiator to the initiator map. 1391 * Returns iid for success, < 0 for failure. 1392 */ 1393int 1394ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1395{ 1396 struct ctl_softc *softc = control_softc; 1397 time_t best_time; 1398 int i, best; 1399 1400 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1401 1402 if (iid >= CTL_MAX_INIT_PER_PORT) { 1403 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1404 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1405 free(name, M_CTL); 1406 return (-1); 1407 } 1408 1409 mtx_lock(&softc->ctl_lock); 1410 1411 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1412 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1413 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1414 iid = i; 1415 break; 1416 } 1417 if (name != NULL && port->wwpn_iid[i].name != NULL && 1418 strcmp(name, port->wwpn_iid[i].name) == 0) { 1419 iid = i; 1420 break; 1421 } 1422 } 1423 } 1424 1425 if (iid < 0) { 1426 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1427 if (port->wwpn_iid[i].in_use == 0 && 1428 port->wwpn_iid[i].wwpn == 0 && 1429 port->wwpn_iid[i].name == NULL) { 1430 iid = i; 1431 break; 1432 } 1433 } 1434 } 1435 1436 if (iid < 0) { 1437 best = -1; 1438 best_time = INT32_MAX; 1439 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1440 if (port->wwpn_iid[i].in_use == 0) { 1441 if (port->wwpn_iid[i].last_use < best_time) { 1442 best = i; 1443 best_time = port->wwpn_iid[i].last_use; 1444 } 1445 } 1446 } 1447 iid = best; 1448 } 1449 1450 if (iid < 0) { 1451 mtx_unlock(&softc->ctl_lock); 1452 free(name, M_CTL); 1453 return (-2); 1454 } 1455 1456 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1457 /* 1458 * This is not an error yet. 1459 */ 1460 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1461#if 0 1462 printf("%s: port %d iid %u WWPN %#jx arrived" 1463 " again\n", __func__, port->targ_port, 1464 iid, (uintmax_t)wwpn); 1465#endif 1466 goto take; 1467 } 1468 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1469 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1470#if 0 1471 printf("%s: port %d iid %u name '%s' arrived" 1472 " again\n", __func__, port->targ_port, 1473 iid, name); 1474#endif 1475 goto take; 1476 } 1477 1478 /* 1479 * This is an error, but what do we do about it? The 1480 * driver is telling us we have a new WWPN for this 1481 * initiator ID, so we pretty much need to use it. 1482 */ 1483 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1484 " but WWPN %#jx '%s' is still at that address\n", 1485 __func__, port->targ_port, iid, wwpn, name, 1486 (uintmax_t)port->wwpn_iid[iid].wwpn, 1487 port->wwpn_iid[iid].name); 1488 1489 /* 1490 * XXX KDM clear have_ca and ua_pending on each LUN for 1491 * this initiator. 1492 */ 1493 } 1494take: 1495 free(port->wwpn_iid[iid].name, M_CTL); 1496 port->wwpn_iid[iid].name = name; 1497 port->wwpn_iid[iid].wwpn = wwpn; 1498 port->wwpn_iid[iid].in_use++; 1499 mtx_unlock(&softc->ctl_lock); 1500 1501 return (iid); 1502} 1503 1504static int 1505ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1506{ 1507 int len; 1508 1509 switch (port->port_type) { 1510 case CTL_PORT_FC: 1511 { 1512 struct scsi_transportid_fcp *id = 1513 (struct scsi_transportid_fcp *)buf; 1514 if (port->wwpn_iid[iid].wwpn == 0) 1515 return (0); 1516 memset(id, 0, sizeof(*id)); 1517 id->format_protocol = SCSI_PROTO_FC; 1518 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1519 return (sizeof(*id)); 1520 } 1521 case CTL_PORT_ISCSI: 1522 { 1523 struct scsi_transportid_iscsi_port *id = 1524 (struct scsi_transportid_iscsi_port *)buf; 1525 if (port->wwpn_iid[iid].name == NULL) 1526 return (0); 1527 memset(id, 0, 256); 1528 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1529 SCSI_PROTO_ISCSI; 1530 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1531 len = roundup2(min(len, 252), 4); 1532 scsi_ulto2b(len, id->additional_length); 1533 return (sizeof(*id) + len); 1534 } 1535 case CTL_PORT_SAS: 1536 { 1537 struct scsi_transportid_sas *id = 1538 (struct scsi_transportid_sas *)buf; 1539 if (port->wwpn_iid[iid].wwpn == 0) 1540 return (0); 1541 memset(id, 0, sizeof(*id)); 1542 id->format_protocol = SCSI_PROTO_SAS; 1543 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1544 return (sizeof(*id)); 1545 } 1546 default: 1547 { 1548 struct scsi_transportid_spi *id = 1549 (struct scsi_transportid_spi *)buf; 1550 memset(id, 0, sizeof(*id)); 1551 id->format_protocol = SCSI_PROTO_SPI; 1552 scsi_ulto2b(iid, id->scsi_addr); 1553 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1554 return (sizeof(*id)); 1555 } 1556 } 1557} 1558 1559static int 1560ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1561{ 1562 return (0); 1563} 1564 1565static int 1566ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1567{ 1568 return (0); 1569} 1570 1571/* 1572 * Data movement routine for the CTL ioctl frontend port. 1573 */ 1574static int 1575ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1576{ 1577 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1578 struct ctl_sg_entry ext_entry, kern_entry; 1579 int ext_sglen, ext_sg_entries, kern_sg_entries; 1580 int ext_sg_start, ext_offset; 1581 int len_to_copy, len_copied; 1582 int kern_watermark, ext_watermark; 1583 int ext_sglist_malloced; 1584 int i, j; 1585 1586 ext_sglist_malloced = 0; 1587 ext_sg_start = 0; 1588 ext_offset = 0; 1589 1590 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1591 1592 /* 1593 * If this flag is set, fake the data transfer. 1594 */ 1595 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1596 ctsio->ext_data_filled = ctsio->ext_data_len; 1597 goto bailout; 1598 } 1599 1600 /* 1601 * To simplify things here, if we have a single buffer, stick it in 1602 * a S/G entry and just make it a single entry S/G list. 1603 */ 1604 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1605 int len_seen; 1606 1607 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1608 1609 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1610 M_WAITOK); 1611 ext_sglist_malloced = 1; 1612 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1613 ext_sglen) != 0) { 1614 ctl_set_internal_failure(ctsio, 1615 /*sks_valid*/ 0, 1616 /*retry_count*/ 0); 1617 goto bailout; 1618 } 1619 ext_sg_entries = ctsio->ext_sg_entries; 1620 len_seen = 0; 1621 for (i = 0; i < ext_sg_entries; i++) { 1622 if ((len_seen + ext_sglist[i].len) >= 1623 ctsio->ext_data_filled) { 1624 ext_sg_start = i; 1625 ext_offset = ctsio->ext_data_filled - len_seen; 1626 break; 1627 } 1628 len_seen += ext_sglist[i].len; 1629 } 1630 } else { 1631 ext_sglist = &ext_entry; 1632 ext_sglist->addr = ctsio->ext_data_ptr; 1633 ext_sglist->len = ctsio->ext_data_len; 1634 ext_sg_entries = 1; 1635 ext_sg_start = 0; 1636 ext_offset = ctsio->ext_data_filled; 1637 } 1638 1639 if (ctsio->kern_sg_entries > 0) { 1640 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1641 kern_sg_entries = ctsio->kern_sg_entries; 1642 } else { 1643 kern_sglist = &kern_entry; 1644 kern_sglist->addr = ctsio->kern_data_ptr; 1645 kern_sglist->len = ctsio->kern_data_len; 1646 kern_sg_entries = 1; 1647 } 1648 1649 1650 kern_watermark = 0; 1651 ext_watermark = ext_offset; 1652 len_copied = 0; 1653 for (i = ext_sg_start, j = 0; 1654 i < ext_sg_entries && j < kern_sg_entries;) { 1655 uint8_t *ext_ptr, *kern_ptr; 1656 1657 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1658 kern_sglist[j].len - kern_watermark); 1659 1660 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1661 ext_ptr = ext_ptr + ext_watermark; 1662 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1663 /* 1664 * XXX KDM fix this! 1665 */ 1666 panic("need to implement bus address support"); 1667#if 0 1668 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1669#endif 1670 } else 1671 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1672 kern_ptr = kern_ptr + kern_watermark; 1673 1674 kern_watermark += len_to_copy; 1675 ext_watermark += len_to_copy; 1676 1677 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1678 CTL_FLAG_DATA_IN) { 1679 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1680 "bytes to user\n", len_to_copy)); 1681 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1682 "to %p\n", kern_ptr, ext_ptr)); 1683 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1684 ctl_set_internal_failure(ctsio, 1685 /*sks_valid*/ 0, 1686 /*retry_count*/ 0); 1687 goto bailout; 1688 } 1689 } else { 1690 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1691 "bytes from user\n", len_to_copy)); 1692 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1693 "to %p\n", ext_ptr, kern_ptr)); 1694 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1695 ctl_set_internal_failure(ctsio, 1696 /*sks_valid*/ 0, 1697 /*retry_count*/0); 1698 goto bailout; 1699 } 1700 } 1701 1702 len_copied += len_to_copy; 1703 1704 if (ext_sglist[i].len == ext_watermark) { 1705 i++; 1706 ext_watermark = 0; 1707 } 1708 1709 if (kern_sglist[j].len == kern_watermark) { 1710 j++; 1711 kern_watermark = 0; 1712 } 1713 } 1714 1715 ctsio->ext_data_filled += len_copied; 1716 1717 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1718 "kern_sg_entries: %d\n", ext_sg_entries, 1719 kern_sg_entries)); 1720 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1721 "kern_data_len = %d\n", ctsio->ext_data_len, 1722 ctsio->kern_data_len)); 1723 1724 1725 /* XXX KDM set residual?? */ 1726bailout: 1727 1728 if (ext_sglist_malloced != 0) 1729 free(ext_sglist, M_CTL); 1730 1731 return (CTL_RETVAL_COMPLETE); 1732} 1733 1734/* 1735 * Serialize a command that went down the "wrong" side, and so was sent to 1736 * this controller for execution. The logic is a little different than the 1737 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1738 * sent back to the other side, but in the success case, we execute the 1739 * command on this side (XFER mode) or tell the other side to execute it 1740 * (SER_ONLY mode). 1741 */ 1742static int 1743ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1744{ 1745 struct ctl_softc *ctl_softc; 1746 union ctl_ha_msg msg_info; 1747 struct ctl_lun *lun; 1748 int retval = 0; 1749 uint32_t targ_lun; 1750 1751 ctl_softc = control_softc; 1752 1753 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1754 lun = ctl_softc->ctl_luns[targ_lun]; 1755 if (lun==NULL) 1756 { 1757 /* 1758 * Why isn't LUN defined? The other side wouldn't 1759 * send a cmd if the LUN is undefined. 1760 */ 1761 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1762 1763 /* "Logical unit not supported" */ 1764 ctl_set_sense_data(&msg_info.scsi.sense_data, 1765 lun, 1766 /*sense_format*/SSD_TYPE_NONE, 1767 /*current_error*/ 1, 1768 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1769 /*asc*/ 0x25, 1770 /*ascq*/ 0x00, 1771 SSD_ELEM_NONE); 1772 1773 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1774 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1775 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1776 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1777 msg_info.hdr.serializing_sc = NULL; 1778 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1779 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1780 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1781 } 1782 return(1); 1783 1784 } 1785 1786 mtx_lock(&lun->lun_lock); 1787 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1788 1789 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1790 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1791 ooa_links))) { 1792 case CTL_ACTION_BLOCK: 1793 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1794 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1795 blocked_links); 1796 break; 1797 case CTL_ACTION_PASS: 1798 case CTL_ACTION_SKIP: 1799 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1800 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1801 ctl_enqueue_rtr((union ctl_io *)ctsio); 1802 } else { 1803 1804 /* send msg back to other side */ 1805 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1806 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1807 msg_info.hdr.msg_type = CTL_MSG_R2R; 1808#if 0 1809 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1810#endif 1811 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1812 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1813 } 1814 } 1815 break; 1816 case CTL_ACTION_OVERLAP: 1817 /* OVERLAPPED COMMANDS ATTEMPTED */ 1818 ctl_set_sense_data(&msg_info.scsi.sense_data, 1819 lun, 1820 /*sense_format*/SSD_TYPE_NONE, 1821 /*current_error*/ 1, 1822 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1823 /*asc*/ 0x4E, 1824 /*ascq*/ 0x00, 1825 SSD_ELEM_NONE); 1826 1827 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1828 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1829 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1830 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1831 msg_info.hdr.serializing_sc = NULL; 1832 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1833#if 0 1834 printf("BAD JUJU:Major Bummer Overlap\n"); 1835#endif 1836 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1837 retval = 1; 1838 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1839 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1840 } 1841 break; 1842 case CTL_ACTION_OVERLAP_TAG: 1843 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1844 ctl_set_sense_data(&msg_info.scsi.sense_data, 1845 lun, 1846 /*sense_format*/SSD_TYPE_NONE, 1847 /*current_error*/ 1, 1848 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1849 /*asc*/ 0x4D, 1850 /*ascq*/ ctsio->tag_num & 0xff, 1851 SSD_ELEM_NONE); 1852 1853 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1854 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1855 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1856 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1857 msg_info.hdr.serializing_sc = NULL; 1858 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1859#if 0 1860 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1861#endif 1862 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1863 retval = 1; 1864 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1865 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1866 } 1867 break; 1868 case CTL_ACTION_ERROR: 1869 default: 1870 /* "Internal target failure" */ 1871 ctl_set_sense_data(&msg_info.scsi.sense_data, 1872 lun, 1873 /*sense_format*/SSD_TYPE_NONE, 1874 /*current_error*/ 1, 1875 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1876 /*asc*/ 0x44, 1877 /*ascq*/ 0x00, 1878 SSD_ELEM_NONE); 1879 1880 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1881 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1882 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1883 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1884 msg_info.hdr.serializing_sc = NULL; 1885 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1886#if 0 1887 printf("BAD JUJU:Major Bummer HW Error\n"); 1888#endif 1889 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1890 retval = 1; 1891 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1892 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1893 } 1894 break; 1895 } 1896 mtx_unlock(&lun->lun_lock); 1897 return (retval); 1898} 1899 1900static int 1901ctl_ioctl_submit_wait(union ctl_io *io) 1902{ 1903 struct ctl_fe_ioctl_params params; 1904 ctl_fe_ioctl_state last_state; 1905 int done, retval; 1906 1907 retval = 0; 1908 1909 bzero(¶ms, sizeof(params)); 1910 1911 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1912 cv_init(¶ms.sem, "ctlioccv"); 1913 params.state = CTL_IOCTL_INPROG; 1914 last_state = params.state; 1915 1916 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1917 1918 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1919 1920 /* This shouldn't happen */ 1921 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1922 return (retval); 1923 1924 done = 0; 1925 1926 do { 1927 mtx_lock(¶ms.ioctl_mtx); 1928 /* 1929 * Check the state here, and don't sleep if the state has 1930 * already changed (i.e. wakeup has already occured, but we 1931 * weren't waiting yet). 1932 */ 1933 if (params.state == last_state) { 1934 /* XXX KDM cv_wait_sig instead? */ 1935 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1936 } 1937 last_state = params.state; 1938 1939 switch (params.state) { 1940 case CTL_IOCTL_INPROG: 1941 /* Why did we wake up? */ 1942 /* XXX KDM error here? */ 1943 mtx_unlock(¶ms.ioctl_mtx); 1944 break; 1945 case CTL_IOCTL_DATAMOVE: 1946 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1947 1948 /* 1949 * change last_state back to INPROG to avoid 1950 * deadlock on subsequent data moves. 1951 */ 1952 params.state = last_state = CTL_IOCTL_INPROG; 1953 1954 mtx_unlock(¶ms.ioctl_mtx); 1955 ctl_ioctl_do_datamove(&io->scsiio); 1956 /* 1957 * Note that in some cases, most notably writes, 1958 * this will queue the I/O and call us back later. 1959 * In other cases, generally reads, this routine 1960 * will immediately call back and wake us up, 1961 * probably using our own context. 1962 */ 1963 io->scsiio.be_move_done(io); 1964 break; 1965 case CTL_IOCTL_DONE: 1966 mtx_unlock(¶ms.ioctl_mtx); 1967 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1968 done = 1; 1969 break; 1970 default: 1971 mtx_unlock(¶ms.ioctl_mtx); 1972 /* XXX KDM error here? */ 1973 break; 1974 } 1975 } while (done == 0); 1976 1977 mtx_destroy(¶ms.ioctl_mtx); 1978 cv_destroy(¶ms.sem); 1979 1980 return (CTL_RETVAL_COMPLETE); 1981} 1982 1983static void 1984ctl_ioctl_datamove(union ctl_io *io) 1985{ 1986 struct ctl_fe_ioctl_params *params; 1987 1988 params = (struct ctl_fe_ioctl_params *) 1989 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1990 1991 mtx_lock(¶ms->ioctl_mtx); 1992 params->state = CTL_IOCTL_DATAMOVE; 1993 cv_broadcast(¶ms->sem); 1994 mtx_unlock(¶ms->ioctl_mtx); 1995} 1996 1997static void 1998ctl_ioctl_done(union ctl_io *io) 1999{ 2000 struct ctl_fe_ioctl_params *params; 2001 2002 params = (struct ctl_fe_ioctl_params *) 2003 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2004 2005 mtx_lock(¶ms->ioctl_mtx); 2006 params->state = CTL_IOCTL_DONE; 2007 cv_broadcast(¶ms->sem); 2008 mtx_unlock(¶ms->ioctl_mtx); 2009} 2010 2011static void 2012ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2013{ 2014 struct ctl_fe_ioctl_startstop_info *sd_info; 2015 2016 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2017 2018 sd_info->hs_info.status = metatask->status; 2019 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2020 sd_info->hs_info.luns_complete = 2021 metatask->taskinfo.startstop.luns_complete; 2022 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2023 2024 cv_broadcast(&sd_info->sem); 2025} 2026 2027static void 2028ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2029{ 2030 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2031 2032 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2033 2034 mtx_lock(fe_bbr_info->lock); 2035 fe_bbr_info->bbr_info->status = metatask->status; 2036 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2037 fe_bbr_info->wakeup_done = 1; 2038 mtx_unlock(fe_bbr_info->lock); 2039 2040 cv_broadcast(&fe_bbr_info->sem); 2041} 2042 2043/* 2044 * Returns 0 for success, errno for failure. 2045 */ 2046static int 2047ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2048 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2049{ 2050 union ctl_io *io; 2051 int retval; 2052 2053 retval = 0; 2054 2055 mtx_lock(&lun->lun_lock); 2056 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2057 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2058 ooa_links)) { 2059 struct ctl_ooa_entry *entry; 2060 2061 /* 2062 * If we've got more than we can fit, just count the 2063 * remaining entries. 2064 */ 2065 if (*cur_fill_num >= ooa_hdr->alloc_num) 2066 continue; 2067 2068 entry = &kern_entries[*cur_fill_num]; 2069 2070 entry->tag_num = io->scsiio.tag_num; 2071 entry->lun_num = lun->lun; 2072#ifdef CTL_TIME_IO 2073 entry->start_bt = io->io_hdr.start_bt; 2074#endif 2075 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2076 entry->cdb_len = io->scsiio.cdb_len; 2077 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2078 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2079 2080 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2081 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2082 2083 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2084 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2085 2086 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2087 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2088 2089 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2090 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2091 } 2092 mtx_unlock(&lun->lun_lock); 2093 2094 return (retval); 2095} 2096 2097static void * 2098ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2099 size_t error_str_len) 2100{ 2101 void *kptr; 2102 2103 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2104 2105 if (copyin(user_addr, kptr, len) != 0) { 2106 snprintf(error_str, error_str_len, "Error copying %d bytes " 2107 "from user address %p to kernel address %p", len, 2108 user_addr, kptr); 2109 free(kptr, M_CTL); 2110 return (NULL); 2111 } 2112 2113 return (kptr); 2114} 2115 2116static void 2117ctl_free_args(int num_args, struct ctl_be_arg *args) 2118{ 2119 int i; 2120 2121 if (args == NULL) 2122 return; 2123 2124 for (i = 0; i < num_args; i++) { 2125 free(args[i].kname, M_CTL); 2126 free(args[i].kvalue, M_CTL); 2127 } 2128 2129 free(args, M_CTL); 2130} 2131 2132static struct ctl_be_arg * 2133ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2134 char *error_str, size_t error_str_len) 2135{ 2136 struct ctl_be_arg *args; 2137 int i; 2138 2139 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2140 error_str, error_str_len); 2141 2142 if (args == NULL) 2143 goto bailout; 2144 2145 for (i = 0; i < num_args; i++) { 2146 args[i].kname = NULL; 2147 args[i].kvalue = NULL; 2148 } 2149 2150 for (i = 0; i < num_args; i++) { 2151 uint8_t *tmpptr; 2152 2153 args[i].kname = ctl_copyin_alloc(args[i].name, 2154 args[i].namelen, error_str, error_str_len); 2155 if (args[i].kname == NULL) 2156 goto bailout; 2157 2158 if (args[i].kname[args[i].namelen - 1] != '\0') { 2159 snprintf(error_str, error_str_len, "Argument %d " 2160 "name is not NUL-terminated", i); 2161 goto bailout; 2162 } 2163 2164 if (args[i].flags & CTL_BEARG_RD) { 2165 tmpptr = ctl_copyin_alloc(args[i].value, 2166 args[i].vallen, error_str, error_str_len); 2167 if (tmpptr == NULL) 2168 goto bailout; 2169 if ((args[i].flags & CTL_BEARG_ASCII) 2170 && (tmpptr[args[i].vallen - 1] != '\0')) { 2171 snprintf(error_str, error_str_len, "Argument " 2172 "%d value is not NUL-terminated", i); 2173 goto bailout; 2174 } 2175 args[i].kvalue = tmpptr; 2176 } else { 2177 args[i].kvalue = malloc(args[i].vallen, 2178 M_CTL, M_WAITOK | M_ZERO); 2179 } 2180 } 2181 2182 return (args); 2183bailout: 2184 2185 ctl_free_args(num_args, args); 2186 2187 return (NULL); 2188} 2189 2190static void 2191ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2192{ 2193 int i; 2194 2195 for (i = 0; i < num_args; i++) { 2196 if (args[i].flags & CTL_BEARG_WR) 2197 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2198 } 2199} 2200 2201/* 2202 * Escape characters that are illegal or not recommended in XML. 2203 */ 2204int 2205ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2206{ 2207 int retval; 2208 2209 retval = 0; 2210 2211 for (; *str; str++) { 2212 switch (*str) { 2213 case '&': 2214 retval = sbuf_printf(sb, "&"); 2215 break; 2216 case '>': 2217 retval = sbuf_printf(sb, ">"); 2218 break; 2219 case '<': 2220 retval = sbuf_printf(sb, "<"); 2221 break; 2222 default: 2223 retval = sbuf_putc(sb, *str); 2224 break; 2225 } 2226 2227 if (retval != 0) 2228 break; 2229 2230 } 2231 2232 return (retval); 2233} 2234 2235static void 2236ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2237{ 2238 struct scsi_vpd_id_descriptor *desc; 2239 int i; 2240 2241 if (id == NULL || id->len < 4) 2242 return; 2243 desc = (struct scsi_vpd_id_descriptor *)id->data; 2244 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2245 case SVPD_ID_TYPE_T10: 2246 sbuf_printf(sb, "t10."); 2247 break; 2248 case SVPD_ID_TYPE_EUI64: 2249 sbuf_printf(sb, "eui."); 2250 break; 2251 case SVPD_ID_TYPE_NAA: 2252 sbuf_printf(sb, "naa."); 2253 break; 2254 case SVPD_ID_TYPE_SCSI_NAME: 2255 break; 2256 } 2257 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2258 case SVPD_ID_CODESET_BINARY: 2259 for (i = 0; i < desc->length; i++) 2260 sbuf_printf(sb, "%02x", desc->identifier[i]); 2261 break; 2262 case SVPD_ID_CODESET_ASCII: 2263 sbuf_printf(sb, "%.*s", (int)desc->length, 2264 (char *)desc->identifier); 2265 break; 2266 case SVPD_ID_CODESET_UTF8: 2267 sbuf_printf(sb, "%s", (char *)desc->identifier); 2268 break; 2269 } 2270} 2271 2272static int 2273ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2274 struct thread *td) 2275{ 2276 struct ctl_softc *softc; 2277 int retval; 2278 2279 softc = control_softc; 2280 2281 retval = 0; 2282 2283 switch (cmd) { 2284 case CTL_IO: { 2285 union ctl_io *io; 2286 void *pool_tmp; 2287 2288 /* 2289 * If we haven't been "enabled", don't allow any SCSI I/O 2290 * to this FETD. 2291 */ 2292 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2293 retval = EPERM; 2294 break; 2295 } 2296 2297 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2298 if (io == NULL) { 2299 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2300 retval = ENOSPC; 2301 break; 2302 } 2303 2304 /* 2305 * Need to save the pool reference so it doesn't get 2306 * spammed by the user's ctl_io. 2307 */ 2308 pool_tmp = io->io_hdr.pool; 2309 2310 memcpy(io, (void *)addr, sizeof(*io)); 2311 2312 io->io_hdr.pool = pool_tmp; 2313 /* 2314 * No status yet, so make sure the status is set properly. 2315 */ 2316 io->io_hdr.status = CTL_STATUS_NONE; 2317 2318 /* 2319 * The user sets the initiator ID, target and LUN IDs. 2320 */ 2321 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2322 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2323 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2324 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2325 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2326 2327 retval = ctl_ioctl_submit_wait(io); 2328 2329 if (retval != 0) { 2330 ctl_free_io(io); 2331 break; 2332 } 2333 2334 memcpy((void *)addr, io, sizeof(*io)); 2335 2336 /* return this to our pool */ 2337 ctl_free_io(io); 2338 2339 break; 2340 } 2341 case CTL_ENABLE_PORT: 2342 case CTL_DISABLE_PORT: 2343 case CTL_SET_PORT_WWNS: { 2344 struct ctl_port *port; 2345 struct ctl_port_entry *entry; 2346 2347 entry = (struct ctl_port_entry *)addr; 2348 2349 mtx_lock(&softc->ctl_lock); 2350 STAILQ_FOREACH(port, &softc->port_list, links) { 2351 int action, done; 2352 2353 action = 0; 2354 done = 0; 2355 2356 if ((entry->port_type == CTL_PORT_NONE) 2357 && (entry->targ_port == port->targ_port)) { 2358 /* 2359 * If the user only wants to enable or 2360 * disable or set WWNs on a specific port, 2361 * do the operation and we're done. 2362 */ 2363 action = 1; 2364 done = 1; 2365 } else if (entry->port_type & port->port_type) { 2366 /* 2367 * Compare the user's type mask with the 2368 * particular frontend type to see if we 2369 * have a match. 2370 */ 2371 action = 1; 2372 done = 0; 2373 2374 /* 2375 * Make sure the user isn't trying to set 2376 * WWNs on multiple ports at the same time. 2377 */ 2378 if (cmd == CTL_SET_PORT_WWNS) { 2379 printf("%s: Can't set WWNs on " 2380 "multiple ports\n", __func__); 2381 retval = EINVAL; 2382 break; 2383 } 2384 } 2385 if (action != 0) { 2386 /* 2387 * XXX KDM we have to drop the lock here, 2388 * because the online/offline operations 2389 * can potentially block. We need to 2390 * reference count the frontends so they 2391 * can't go away, 2392 */ 2393 mtx_unlock(&softc->ctl_lock); 2394 2395 if (cmd == CTL_ENABLE_PORT) { 2396 struct ctl_lun *lun; 2397 2398 STAILQ_FOREACH(lun, &softc->lun_list, 2399 links) { 2400 port->lun_enable(port->targ_lun_arg, 2401 lun->target, 2402 lun->lun); 2403 } 2404 2405 ctl_port_online(port); 2406 } else if (cmd == CTL_DISABLE_PORT) { 2407 struct ctl_lun *lun; 2408 2409 ctl_port_offline(port); 2410 2411 STAILQ_FOREACH(lun, &softc->lun_list, 2412 links) { 2413 port->lun_disable( 2414 port->targ_lun_arg, 2415 lun->target, 2416 lun->lun); 2417 } 2418 } 2419 2420 mtx_lock(&softc->ctl_lock); 2421 2422 if (cmd == CTL_SET_PORT_WWNS) 2423 ctl_port_set_wwns(port, 2424 (entry->flags & CTL_PORT_WWNN_VALID) ? 2425 1 : 0, entry->wwnn, 2426 (entry->flags & CTL_PORT_WWPN_VALID) ? 2427 1 : 0, entry->wwpn); 2428 } 2429 if (done != 0) 2430 break; 2431 } 2432 mtx_unlock(&softc->ctl_lock); 2433 break; 2434 } 2435 case CTL_GET_PORT_LIST: { 2436 struct ctl_port *port; 2437 struct ctl_port_list *list; 2438 int i; 2439 2440 list = (struct ctl_port_list *)addr; 2441 2442 if (list->alloc_len != (list->alloc_num * 2443 sizeof(struct ctl_port_entry))) { 2444 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2445 "alloc_num %u * sizeof(struct ctl_port_entry) " 2446 "%zu\n", __func__, list->alloc_len, 2447 list->alloc_num, sizeof(struct ctl_port_entry)); 2448 retval = EINVAL; 2449 break; 2450 } 2451 list->fill_len = 0; 2452 list->fill_num = 0; 2453 list->dropped_num = 0; 2454 i = 0; 2455 mtx_lock(&softc->ctl_lock); 2456 STAILQ_FOREACH(port, &softc->port_list, links) { 2457 struct ctl_port_entry entry, *list_entry; 2458 2459 if (list->fill_num >= list->alloc_num) { 2460 list->dropped_num++; 2461 continue; 2462 } 2463 2464 entry.port_type = port->port_type; 2465 strlcpy(entry.port_name, port->port_name, 2466 sizeof(entry.port_name)); 2467 entry.targ_port = port->targ_port; 2468 entry.physical_port = port->physical_port; 2469 entry.virtual_port = port->virtual_port; 2470 entry.wwnn = port->wwnn; 2471 entry.wwpn = port->wwpn; 2472 if (port->status & CTL_PORT_STATUS_ONLINE) 2473 entry.online = 1; 2474 else 2475 entry.online = 0; 2476 2477 list_entry = &list->entries[i]; 2478 2479 retval = copyout(&entry, list_entry, sizeof(entry)); 2480 if (retval != 0) { 2481 printf("%s: CTL_GET_PORT_LIST: copyout " 2482 "returned %d\n", __func__, retval); 2483 break; 2484 } 2485 i++; 2486 list->fill_num++; 2487 list->fill_len += sizeof(entry); 2488 } 2489 mtx_unlock(&softc->ctl_lock); 2490 2491 /* 2492 * If this is non-zero, we had a copyout fault, so there's 2493 * probably no point in attempting to set the status inside 2494 * the structure. 2495 */ 2496 if (retval != 0) 2497 break; 2498 2499 if (list->dropped_num > 0) 2500 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2501 else 2502 list->status = CTL_PORT_LIST_OK; 2503 break; 2504 } 2505 case CTL_DUMP_OOA: { 2506 struct ctl_lun *lun; 2507 union ctl_io *io; 2508 char printbuf[128]; 2509 struct sbuf sb; 2510 2511 mtx_lock(&softc->ctl_lock); 2512 printf("Dumping OOA queues:\n"); 2513 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2514 mtx_lock(&lun->lun_lock); 2515 for (io = (union ctl_io *)TAILQ_FIRST( 2516 &lun->ooa_queue); io != NULL; 2517 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2518 ooa_links)) { 2519 sbuf_new(&sb, printbuf, sizeof(printbuf), 2520 SBUF_FIXEDLEN); 2521 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2522 (intmax_t)lun->lun, 2523 io->scsiio.tag_num, 2524 (io->io_hdr.flags & 2525 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2526 (io->io_hdr.flags & 2527 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2528 (io->io_hdr.flags & 2529 CTL_FLAG_ABORT) ? " ABORT" : "", 2530 (io->io_hdr.flags & 2531 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2532 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2533 sbuf_finish(&sb); 2534 printf("%s\n", sbuf_data(&sb)); 2535 } 2536 mtx_unlock(&lun->lun_lock); 2537 } 2538 printf("OOA queues dump done\n"); 2539 mtx_unlock(&softc->ctl_lock); 2540 break; 2541 } 2542 case CTL_GET_OOA: { 2543 struct ctl_lun *lun; 2544 struct ctl_ooa *ooa_hdr; 2545 struct ctl_ooa_entry *entries; 2546 uint32_t cur_fill_num; 2547 2548 ooa_hdr = (struct ctl_ooa *)addr; 2549 2550 if ((ooa_hdr->alloc_len == 0) 2551 || (ooa_hdr->alloc_num == 0)) { 2552 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2553 "must be non-zero\n", __func__, 2554 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2555 retval = EINVAL; 2556 break; 2557 } 2558 2559 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2560 sizeof(struct ctl_ooa_entry))) { 2561 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2562 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2563 __func__, ooa_hdr->alloc_len, 2564 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2565 retval = EINVAL; 2566 break; 2567 } 2568 2569 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2570 if (entries == NULL) { 2571 printf("%s: could not allocate %d bytes for OOA " 2572 "dump\n", __func__, ooa_hdr->alloc_len); 2573 retval = ENOMEM; 2574 break; 2575 } 2576 2577 mtx_lock(&softc->ctl_lock); 2578 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2579 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2580 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2581 mtx_unlock(&softc->ctl_lock); 2582 free(entries, M_CTL); 2583 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2584 __func__, (uintmax_t)ooa_hdr->lun_num); 2585 retval = EINVAL; 2586 break; 2587 } 2588 2589 cur_fill_num = 0; 2590 2591 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2592 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2593 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2594 ooa_hdr, entries); 2595 if (retval != 0) 2596 break; 2597 } 2598 if (retval != 0) { 2599 mtx_unlock(&softc->ctl_lock); 2600 free(entries, M_CTL); 2601 break; 2602 } 2603 } else { 2604 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2605 2606 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2607 entries); 2608 } 2609 mtx_unlock(&softc->ctl_lock); 2610 2611 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2612 ooa_hdr->fill_len = ooa_hdr->fill_num * 2613 sizeof(struct ctl_ooa_entry); 2614 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2615 if (retval != 0) { 2616 printf("%s: error copying out %d bytes for OOA dump\n", 2617 __func__, ooa_hdr->fill_len); 2618 } 2619 2620 getbintime(&ooa_hdr->cur_bt); 2621 2622 if (cur_fill_num > ooa_hdr->alloc_num) { 2623 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2624 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2625 } else { 2626 ooa_hdr->dropped_num = 0; 2627 ooa_hdr->status = CTL_OOA_OK; 2628 } 2629 2630 free(entries, M_CTL); 2631 break; 2632 } 2633 case CTL_CHECK_OOA: { 2634 union ctl_io *io; 2635 struct ctl_lun *lun; 2636 struct ctl_ooa_info *ooa_info; 2637 2638 2639 ooa_info = (struct ctl_ooa_info *)addr; 2640 2641 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2642 ooa_info->status = CTL_OOA_INVALID_LUN; 2643 break; 2644 } 2645 mtx_lock(&softc->ctl_lock); 2646 lun = softc->ctl_luns[ooa_info->lun_id]; 2647 if (lun == NULL) { 2648 mtx_unlock(&softc->ctl_lock); 2649 ooa_info->status = CTL_OOA_INVALID_LUN; 2650 break; 2651 } 2652 mtx_lock(&lun->lun_lock); 2653 mtx_unlock(&softc->ctl_lock); 2654 ooa_info->num_entries = 0; 2655 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2656 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2657 &io->io_hdr, ooa_links)) { 2658 ooa_info->num_entries++; 2659 } 2660 mtx_unlock(&lun->lun_lock); 2661 2662 ooa_info->status = CTL_OOA_SUCCESS; 2663 2664 break; 2665 } 2666 case CTL_HARD_START: 2667 case CTL_HARD_STOP: { 2668 struct ctl_fe_ioctl_startstop_info ss_info; 2669 struct cfi_metatask *metatask; 2670 struct mtx hs_mtx; 2671 2672 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2673 2674 cv_init(&ss_info.sem, "hard start/stop cv" ); 2675 2676 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2677 if (metatask == NULL) { 2678 retval = ENOMEM; 2679 mtx_destroy(&hs_mtx); 2680 break; 2681 } 2682 2683 if (cmd == CTL_HARD_START) 2684 metatask->tasktype = CFI_TASK_STARTUP; 2685 else 2686 metatask->tasktype = CFI_TASK_SHUTDOWN; 2687 2688 metatask->callback = ctl_ioctl_hard_startstop_callback; 2689 metatask->callback_arg = &ss_info; 2690 2691 cfi_action(metatask); 2692 2693 /* Wait for the callback */ 2694 mtx_lock(&hs_mtx); 2695 cv_wait_sig(&ss_info.sem, &hs_mtx); 2696 mtx_unlock(&hs_mtx); 2697 2698 /* 2699 * All information has been copied from the metatask by the 2700 * time cv_broadcast() is called, so we free the metatask here. 2701 */ 2702 cfi_free_metatask(metatask); 2703 2704 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2705 2706 mtx_destroy(&hs_mtx); 2707 break; 2708 } 2709 case CTL_BBRREAD: { 2710 struct ctl_bbrread_info *bbr_info; 2711 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2712 struct mtx bbr_mtx; 2713 struct cfi_metatask *metatask; 2714 2715 bbr_info = (struct ctl_bbrread_info *)addr; 2716 2717 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2718 2719 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2720 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2721 2722 fe_bbr_info.bbr_info = bbr_info; 2723 fe_bbr_info.lock = &bbr_mtx; 2724 2725 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2726 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2727 2728 if (metatask == NULL) { 2729 mtx_destroy(&bbr_mtx); 2730 cv_destroy(&fe_bbr_info.sem); 2731 retval = ENOMEM; 2732 break; 2733 } 2734 metatask->tasktype = CFI_TASK_BBRREAD; 2735 metatask->callback = ctl_ioctl_bbrread_callback; 2736 metatask->callback_arg = &fe_bbr_info; 2737 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2738 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2739 metatask->taskinfo.bbrread.len = bbr_info->len; 2740 2741 cfi_action(metatask); 2742 2743 mtx_lock(&bbr_mtx); 2744 while (fe_bbr_info.wakeup_done == 0) 2745 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2746 mtx_unlock(&bbr_mtx); 2747 2748 bbr_info->status = metatask->status; 2749 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2750 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2751 memcpy(&bbr_info->sense_data, 2752 &metatask->taskinfo.bbrread.sense_data, 2753 ctl_min(sizeof(bbr_info->sense_data), 2754 sizeof(metatask->taskinfo.bbrread.sense_data))); 2755 2756 cfi_free_metatask(metatask); 2757 2758 mtx_destroy(&bbr_mtx); 2759 cv_destroy(&fe_bbr_info.sem); 2760 2761 break; 2762 } 2763 case CTL_DELAY_IO: { 2764 struct ctl_io_delay_info *delay_info; 2765#ifdef CTL_IO_DELAY 2766 struct ctl_lun *lun; 2767#endif /* CTL_IO_DELAY */ 2768 2769 delay_info = (struct ctl_io_delay_info *)addr; 2770 2771#ifdef CTL_IO_DELAY 2772 mtx_lock(&softc->ctl_lock); 2773 2774 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2775 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2776 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2777 } else { 2778 lun = softc->ctl_luns[delay_info->lun_id]; 2779 mtx_lock(&lun->lun_lock); 2780 2781 delay_info->status = CTL_DELAY_STATUS_OK; 2782 2783 switch (delay_info->delay_type) { 2784 case CTL_DELAY_TYPE_CONT: 2785 break; 2786 case CTL_DELAY_TYPE_ONESHOT: 2787 break; 2788 default: 2789 delay_info->status = 2790 CTL_DELAY_STATUS_INVALID_TYPE; 2791 break; 2792 } 2793 2794 switch (delay_info->delay_loc) { 2795 case CTL_DELAY_LOC_DATAMOVE: 2796 lun->delay_info.datamove_type = 2797 delay_info->delay_type; 2798 lun->delay_info.datamove_delay = 2799 delay_info->delay_secs; 2800 break; 2801 case CTL_DELAY_LOC_DONE: 2802 lun->delay_info.done_type = 2803 delay_info->delay_type; 2804 lun->delay_info.done_delay = 2805 delay_info->delay_secs; 2806 break; 2807 default: 2808 delay_info->status = 2809 CTL_DELAY_STATUS_INVALID_LOC; 2810 break; 2811 } 2812 mtx_unlock(&lun->lun_lock); 2813 } 2814 2815 mtx_unlock(&softc->ctl_lock); 2816#else 2817 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2818#endif /* CTL_IO_DELAY */ 2819 break; 2820 } 2821 case CTL_REALSYNC_SET: { 2822 int *syncstate; 2823 2824 syncstate = (int *)addr; 2825 2826 mtx_lock(&softc->ctl_lock); 2827 switch (*syncstate) { 2828 case 0: 2829 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2830 break; 2831 case 1: 2832 softc->flags |= CTL_FLAG_REAL_SYNC; 2833 break; 2834 default: 2835 retval = EINVAL; 2836 break; 2837 } 2838 mtx_unlock(&softc->ctl_lock); 2839 break; 2840 } 2841 case CTL_REALSYNC_GET: { 2842 int *syncstate; 2843 2844 syncstate = (int*)addr; 2845 2846 mtx_lock(&softc->ctl_lock); 2847 if (softc->flags & CTL_FLAG_REAL_SYNC) 2848 *syncstate = 1; 2849 else 2850 *syncstate = 0; 2851 mtx_unlock(&softc->ctl_lock); 2852 2853 break; 2854 } 2855 case CTL_SETSYNC: 2856 case CTL_GETSYNC: { 2857 struct ctl_sync_info *sync_info; 2858 struct ctl_lun *lun; 2859 2860 sync_info = (struct ctl_sync_info *)addr; 2861 2862 mtx_lock(&softc->ctl_lock); 2863 lun = softc->ctl_luns[sync_info->lun_id]; 2864 if (lun == NULL) { 2865 mtx_unlock(&softc->ctl_lock); 2866 sync_info->status = CTL_GS_SYNC_NO_LUN; 2867 } 2868 /* 2869 * Get or set the sync interval. We're not bounds checking 2870 * in the set case, hopefully the user won't do something 2871 * silly. 2872 */ 2873 mtx_lock(&lun->lun_lock); 2874 mtx_unlock(&softc->ctl_lock); 2875 if (cmd == CTL_GETSYNC) 2876 sync_info->sync_interval = lun->sync_interval; 2877 else 2878 lun->sync_interval = sync_info->sync_interval; 2879 mtx_unlock(&lun->lun_lock); 2880 2881 sync_info->status = CTL_GS_SYNC_OK; 2882 2883 break; 2884 } 2885 case CTL_GETSTATS: { 2886 struct ctl_stats *stats; 2887 struct ctl_lun *lun; 2888 int i; 2889 2890 stats = (struct ctl_stats *)addr; 2891 2892 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2893 stats->alloc_len) { 2894 stats->status = CTL_SS_NEED_MORE_SPACE; 2895 stats->num_luns = softc->num_luns; 2896 break; 2897 } 2898 /* 2899 * XXX KDM no locking here. If the LUN list changes, 2900 * things can blow up. 2901 */ 2902 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2903 i++, lun = STAILQ_NEXT(lun, links)) { 2904 retval = copyout(&lun->stats, &stats->lun_stats[i], 2905 sizeof(lun->stats)); 2906 if (retval != 0) 2907 break; 2908 } 2909 stats->num_luns = softc->num_luns; 2910 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2911 softc->num_luns; 2912 stats->status = CTL_SS_OK; 2913#ifdef CTL_TIME_IO 2914 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2915#else 2916 stats->flags = CTL_STATS_FLAG_NONE; 2917#endif 2918 getnanouptime(&stats->timestamp); 2919 break; 2920 } 2921 case CTL_ERROR_INJECT: { 2922 struct ctl_error_desc *err_desc, *new_err_desc; 2923 struct ctl_lun *lun; 2924 2925 err_desc = (struct ctl_error_desc *)addr; 2926 2927 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2928 M_WAITOK | M_ZERO); 2929 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2930 2931 mtx_lock(&softc->ctl_lock); 2932 lun = softc->ctl_luns[err_desc->lun_id]; 2933 if (lun == NULL) { 2934 mtx_unlock(&softc->ctl_lock); 2935 free(new_err_desc, M_CTL); 2936 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2937 __func__, (uintmax_t)err_desc->lun_id); 2938 retval = EINVAL; 2939 break; 2940 } 2941 mtx_lock(&lun->lun_lock); 2942 mtx_unlock(&softc->ctl_lock); 2943 2944 /* 2945 * We could do some checking here to verify the validity 2946 * of the request, but given the complexity of error 2947 * injection requests, the checking logic would be fairly 2948 * complex. 2949 * 2950 * For now, if the request is invalid, it just won't get 2951 * executed and might get deleted. 2952 */ 2953 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2954 2955 /* 2956 * XXX KDM check to make sure the serial number is unique, 2957 * in case we somehow manage to wrap. That shouldn't 2958 * happen for a very long time, but it's the right thing to 2959 * do. 2960 */ 2961 new_err_desc->serial = lun->error_serial; 2962 err_desc->serial = lun->error_serial; 2963 lun->error_serial++; 2964 2965 mtx_unlock(&lun->lun_lock); 2966 break; 2967 } 2968 case CTL_ERROR_INJECT_DELETE: { 2969 struct ctl_error_desc *delete_desc, *desc, *desc2; 2970 struct ctl_lun *lun; 2971 int delete_done; 2972 2973 delete_desc = (struct ctl_error_desc *)addr; 2974 delete_done = 0; 2975 2976 mtx_lock(&softc->ctl_lock); 2977 lun = softc->ctl_luns[delete_desc->lun_id]; 2978 if (lun == NULL) { 2979 mtx_unlock(&softc->ctl_lock); 2980 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2981 __func__, (uintmax_t)delete_desc->lun_id); 2982 retval = EINVAL; 2983 break; 2984 } 2985 mtx_lock(&lun->lun_lock); 2986 mtx_unlock(&softc->ctl_lock); 2987 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2988 if (desc->serial != delete_desc->serial) 2989 continue; 2990 2991 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2992 links); 2993 free(desc, M_CTL); 2994 delete_done = 1; 2995 } 2996 mtx_unlock(&lun->lun_lock); 2997 if (delete_done == 0) { 2998 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2999 "error serial %ju on LUN %u\n", __func__, 3000 delete_desc->serial, delete_desc->lun_id); 3001 retval = EINVAL; 3002 break; 3003 } 3004 break; 3005 } 3006 case CTL_DUMP_STRUCTS: { 3007 int i, j, k, idx; 3008 struct ctl_port *port; 3009 struct ctl_frontend *fe; 3010 3011 mtx_lock(&softc->ctl_lock); 3012 printf("CTL Persistent Reservation information start:\n"); 3013 for (i = 0; i < CTL_MAX_LUNS; i++) { 3014 struct ctl_lun *lun; 3015 3016 lun = softc->ctl_luns[i]; 3017 3018 if ((lun == NULL) 3019 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3020 continue; 3021 3022 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3023 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3024 idx = j * CTL_MAX_INIT_PER_PORT + k; 3025 if (lun->pr_keys[idx] == 0) 3026 continue; 3027 printf(" LUN %d port %d iid %d key " 3028 "%#jx\n", i, j, k, 3029 (uintmax_t)lun->pr_keys[idx]); 3030 } 3031 } 3032 } 3033 printf("CTL Persistent Reservation information end\n"); 3034 printf("CTL Ports:\n"); 3035 STAILQ_FOREACH(port, &softc->port_list, links) { 3036 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3037 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3038 port->frontend->name, port->port_type, 3039 port->physical_port, port->virtual_port, 3040 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3041 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3042 if (port->wwpn_iid[j].in_use == 0 && 3043 port->wwpn_iid[j].wwpn == 0 && 3044 port->wwpn_iid[j].name == NULL) 3045 continue; 3046 3047 printf(" iid %u use %d WWPN %#jx '%s'\n", 3048 j, port->wwpn_iid[j].in_use, 3049 (uintmax_t)port->wwpn_iid[j].wwpn, 3050 port->wwpn_iid[j].name); 3051 } 3052 } 3053 printf("CTL Port information end\n"); 3054 mtx_unlock(&softc->ctl_lock); 3055 /* 3056 * XXX KDM calling this without a lock. We'd likely want 3057 * to drop the lock before calling the frontend's dump 3058 * routine anyway. 3059 */ 3060 printf("CTL Frontends:\n"); 3061 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3062 printf(" Frontend '%s'\n", fe->name); 3063 if (fe->fe_dump != NULL) 3064 fe->fe_dump(); 3065 } 3066 printf("CTL Frontend information end\n"); 3067 break; 3068 } 3069 case CTL_LUN_REQ: { 3070 struct ctl_lun_req *lun_req; 3071 struct ctl_backend_driver *backend; 3072 3073 lun_req = (struct ctl_lun_req *)addr; 3074 3075 backend = ctl_backend_find(lun_req->backend); 3076 if (backend == NULL) { 3077 lun_req->status = CTL_LUN_ERROR; 3078 snprintf(lun_req->error_str, 3079 sizeof(lun_req->error_str), 3080 "Backend \"%s\" not found.", 3081 lun_req->backend); 3082 break; 3083 } 3084 if (lun_req->num_be_args > 0) { 3085 lun_req->kern_be_args = ctl_copyin_args( 3086 lun_req->num_be_args, 3087 lun_req->be_args, 3088 lun_req->error_str, 3089 sizeof(lun_req->error_str)); 3090 if (lun_req->kern_be_args == NULL) { 3091 lun_req->status = CTL_LUN_ERROR; 3092 break; 3093 } 3094 } 3095 3096 retval = backend->ioctl(dev, cmd, addr, flag, td); 3097 3098 if (lun_req->num_be_args > 0) { 3099 ctl_copyout_args(lun_req->num_be_args, 3100 lun_req->kern_be_args); 3101 ctl_free_args(lun_req->num_be_args, 3102 lun_req->kern_be_args); 3103 } 3104 break; 3105 } 3106 case CTL_LUN_LIST: { 3107 struct sbuf *sb; 3108 struct ctl_lun *lun; 3109 struct ctl_lun_list *list; 3110 struct ctl_option *opt; 3111 3112 list = (struct ctl_lun_list *)addr; 3113 3114 /* 3115 * Allocate a fixed length sbuf here, based on the length 3116 * of the user's buffer. We could allocate an auto-extending 3117 * buffer, and then tell the user how much larger our 3118 * amount of data is than his buffer, but that presents 3119 * some problems: 3120 * 3121 * 1. The sbuf(9) routines use a blocking malloc, and so 3122 * we can't hold a lock while calling them with an 3123 * auto-extending buffer. 3124 * 3125 * 2. There is not currently a LUN reference counting 3126 * mechanism, outside of outstanding transactions on 3127 * the LUN's OOA queue. So a LUN could go away on us 3128 * while we're getting the LUN number, backend-specific 3129 * information, etc. Thus, given the way things 3130 * currently work, we need to hold the CTL lock while 3131 * grabbing LUN information. 3132 * 3133 * So, from the user's standpoint, the best thing to do is 3134 * allocate what he thinks is a reasonable buffer length, 3135 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3136 * double the buffer length and try again. (And repeat 3137 * that until he succeeds.) 3138 */ 3139 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3140 if (sb == NULL) { 3141 list->status = CTL_LUN_LIST_ERROR; 3142 snprintf(list->error_str, sizeof(list->error_str), 3143 "Unable to allocate %d bytes for LUN list", 3144 list->alloc_len); 3145 break; 3146 } 3147 3148 sbuf_printf(sb, "<ctllunlist>\n"); 3149 3150 mtx_lock(&softc->ctl_lock); 3151 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3152 mtx_lock(&lun->lun_lock); 3153 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3154 (uintmax_t)lun->lun); 3155 3156 /* 3157 * Bail out as soon as we see that we've overfilled 3158 * the buffer. 3159 */ 3160 if (retval != 0) 3161 break; 3162 3163 retval = sbuf_printf(sb, "\t<backend_type>%s" 3164 "</backend_type>\n", 3165 (lun->backend == NULL) ? "none" : 3166 lun->backend->name); 3167 3168 if (retval != 0) 3169 break; 3170 3171 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3172 lun->be_lun->lun_type); 3173 3174 if (retval != 0) 3175 break; 3176 3177 if (lun->backend == NULL) { 3178 retval = sbuf_printf(sb, "</lun>\n"); 3179 if (retval != 0) 3180 break; 3181 continue; 3182 } 3183 3184 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3185 (lun->be_lun->maxlba > 0) ? 3186 lun->be_lun->maxlba + 1 : 0); 3187 3188 if (retval != 0) 3189 break; 3190 3191 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3192 lun->be_lun->blocksize); 3193 3194 if (retval != 0) 3195 break; 3196 3197 retval = sbuf_printf(sb, "\t<serial_number>"); 3198 3199 if (retval != 0) 3200 break; 3201 3202 retval = ctl_sbuf_printf_esc(sb, 3203 lun->be_lun->serial_num); 3204 3205 if (retval != 0) 3206 break; 3207 3208 retval = sbuf_printf(sb, "</serial_number>\n"); 3209 3210 if (retval != 0) 3211 break; 3212 3213 retval = sbuf_printf(sb, "\t<device_id>"); 3214 3215 if (retval != 0) 3216 break; 3217 3218 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3219 3220 if (retval != 0) 3221 break; 3222 3223 retval = sbuf_printf(sb, "</device_id>\n"); 3224 3225 if (retval != 0) 3226 break; 3227 3228 if (lun->backend->lun_info != NULL) { 3229 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3230 if (retval != 0) 3231 break; 3232 } 3233 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3234 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3235 opt->name, opt->value, opt->name); 3236 if (retval != 0) 3237 break; 3238 } 3239 3240 retval = sbuf_printf(sb, "</lun>\n"); 3241 3242 if (retval != 0) 3243 break; 3244 mtx_unlock(&lun->lun_lock); 3245 } 3246 if (lun != NULL) 3247 mtx_unlock(&lun->lun_lock); 3248 mtx_unlock(&softc->ctl_lock); 3249 3250 if ((retval != 0) 3251 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3252 retval = 0; 3253 sbuf_delete(sb); 3254 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3255 snprintf(list->error_str, sizeof(list->error_str), 3256 "Out of space, %d bytes is too small", 3257 list->alloc_len); 3258 break; 3259 } 3260 3261 sbuf_finish(sb); 3262 3263 retval = copyout(sbuf_data(sb), list->lun_xml, 3264 sbuf_len(sb) + 1); 3265 3266 list->fill_len = sbuf_len(sb) + 1; 3267 list->status = CTL_LUN_LIST_OK; 3268 sbuf_delete(sb); 3269 break; 3270 } 3271 case CTL_ISCSI: { 3272 struct ctl_iscsi *ci; 3273 struct ctl_frontend *fe; 3274 3275 ci = (struct ctl_iscsi *)addr; 3276 3277 fe = ctl_frontend_find("iscsi"); 3278 if (fe == NULL) { 3279 ci->status = CTL_ISCSI_ERROR; 3280 snprintf(ci->error_str, sizeof(ci->error_str), 3281 "Frontend \"iscsi\" not found."); 3282 break; 3283 } 3284 3285 retval = fe->ioctl(dev, cmd, addr, flag, td); 3286 break; 3287 } 3288 case CTL_PORT_REQ: { 3289 struct ctl_req *req; 3290 struct ctl_frontend *fe; 3291 3292 req = (struct ctl_req *)addr; 3293 3294 fe = ctl_frontend_find(req->driver); 3295 if (fe == NULL) { 3296 req->status = CTL_LUN_ERROR; 3297 snprintf(req->error_str, sizeof(req->error_str), 3298 "Frontend \"%s\" not found.", req->driver); 3299 break; 3300 } 3301 if (req->num_args > 0) { 3302 req->kern_args = ctl_copyin_args(req->num_args, 3303 req->args, req->error_str, sizeof(req->error_str)); 3304 if (req->kern_args == NULL) { 3305 req->status = CTL_LUN_ERROR; 3306 break; 3307 } 3308 } 3309 3310 retval = fe->ioctl(dev, cmd, addr, flag, td); 3311 3312 if (req->num_args > 0) { 3313 ctl_copyout_args(req->num_args, req->kern_args); 3314 ctl_free_args(req->num_args, req->kern_args); 3315 } 3316 break; 3317 } 3318 case CTL_PORT_LIST: { 3319 struct sbuf *sb; 3320 struct ctl_port *port; 3321 struct ctl_lun_list *list; 3322 struct ctl_option *opt; 3323 int j; 3324 3325 list = (struct ctl_lun_list *)addr; 3326 3327 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3328 if (sb == NULL) { 3329 list->status = CTL_LUN_LIST_ERROR; 3330 snprintf(list->error_str, sizeof(list->error_str), 3331 "Unable to allocate %d bytes for LUN list", 3332 list->alloc_len); 3333 break; 3334 } 3335 3336 sbuf_printf(sb, "<ctlportlist>\n"); 3337 3338 mtx_lock(&softc->ctl_lock); 3339 STAILQ_FOREACH(port, &softc->port_list, links) { 3340 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3341 (uintmax_t)port->targ_port); 3342 3343 /* 3344 * Bail out as soon as we see that we've overfilled 3345 * the buffer. 3346 */ 3347 if (retval != 0) 3348 break; 3349 3350 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3351 "</frontend_type>\n", port->frontend->name); 3352 if (retval != 0) 3353 break; 3354 3355 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3356 port->port_type); 3357 if (retval != 0) 3358 break; 3359 3360 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3361 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3362 if (retval != 0) 3363 break; 3364 3365 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3366 port->port_name); 3367 if (retval != 0) 3368 break; 3369 3370 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3371 port->physical_port); 3372 if (retval != 0) 3373 break; 3374 3375 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3376 port->virtual_port); 3377 if (retval != 0) 3378 break; 3379 3380 if (port->target_devid != NULL) { 3381 sbuf_printf(sb, "\t<target>"); 3382 ctl_id_sbuf(port->target_devid, sb); 3383 sbuf_printf(sb, "</target>\n"); 3384 } 3385 3386 if (port->port_devid != NULL) { 3387 sbuf_printf(sb, "\t<port>"); 3388 ctl_id_sbuf(port->port_devid, sb); 3389 sbuf_printf(sb, "</port>\n"); 3390 } 3391 3392 if (port->port_info != NULL) { 3393 retval = port->port_info(port->onoff_arg, sb); 3394 if (retval != 0) 3395 break; 3396 } 3397 STAILQ_FOREACH(opt, &port->options, links) { 3398 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3399 opt->name, opt->value, opt->name); 3400 if (retval != 0) 3401 break; 3402 } 3403 3404 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3405 if (port->wwpn_iid[j].in_use == 0 || 3406 (port->wwpn_iid[j].wwpn == 0 && 3407 port->wwpn_iid[j].name == NULL)) 3408 continue; 3409 3410 if (port->wwpn_iid[j].name != NULL) 3411 retval = sbuf_printf(sb, 3412 "\t<initiator>%u %s</initiator>\n", 3413 j, port->wwpn_iid[j].name); 3414 else 3415 retval = sbuf_printf(sb, 3416 "\t<initiator>%u naa.%08jx</initiator>\n", 3417 j, port->wwpn_iid[j].wwpn); 3418 if (retval != 0) 3419 break; 3420 } 3421 if (retval != 0) 3422 break; 3423 3424 retval = sbuf_printf(sb, "</targ_port>\n"); 3425 if (retval != 0) 3426 break; 3427 } 3428 mtx_unlock(&softc->ctl_lock); 3429 3430 if ((retval != 0) 3431 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3432 retval = 0; 3433 sbuf_delete(sb); 3434 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3435 snprintf(list->error_str, sizeof(list->error_str), 3436 "Out of space, %d bytes is too small", 3437 list->alloc_len); 3438 break; 3439 } 3440 3441 sbuf_finish(sb); 3442 3443 retval = copyout(sbuf_data(sb), list->lun_xml, 3444 sbuf_len(sb) + 1); 3445 3446 list->fill_len = sbuf_len(sb) + 1; 3447 list->status = CTL_LUN_LIST_OK; 3448 sbuf_delete(sb); 3449 break; 3450 } 3451 default: { 3452 /* XXX KDM should we fix this? */ 3453#if 0 3454 struct ctl_backend_driver *backend; 3455 unsigned int type; 3456 int found; 3457 3458 found = 0; 3459 3460 /* 3461 * We encode the backend type as the ioctl type for backend 3462 * ioctls. So parse it out here, and then search for a 3463 * backend of this type. 3464 */ 3465 type = _IOC_TYPE(cmd); 3466 3467 STAILQ_FOREACH(backend, &softc->be_list, links) { 3468 if (backend->type == type) { 3469 found = 1; 3470 break; 3471 } 3472 } 3473 if (found == 0) { 3474 printf("ctl: unknown ioctl command %#lx or backend " 3475 "%d\n", cmd, type); 3476 retval = EINVAL; 3477 break; 3478 } 3479 retval = backend->ioctl(dev, cmd, addr, flag, td); 3480#endif 3481 retval = ENOTTY; 3482 break; 3483 } 3484 } 3485 return (retval); 3486} 3487 3488uint32_t 3489ctl_get_initindex(struct ctl_nexus *nexus) 3490{ 3491 if (nexus->targ_port < CTL_MAX_PORTS) 3492 return (nexus->initid.id + 3493 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3494 else 3495 return (nexus->initid.id + 3496 ((nexus->targ_port - CTL_MAX_PORTS) * 3497 CTL_MAX_INIT_PER_PORT)); 3498} 3499 3500uint32_t 3501ctl_get_resindex(struct ctl_nexus *nexus) 3502{ 3503 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3504} 3505 3506uint32_t 3507ctl_port_idx(int port_num) 3508{ 3509 if (port_num < CTL_MAX_PORTS) 3510 return(port_num); 3511 else 3512 return(port_num - CTL_MAX_PORTS); 3513} 3514 3515static uint32_t 3516ctl_map_lun(int port_num, uint32_t lun_id) 3517{ 3518 struct ctl_port *port; 3519 3520 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3521 if (port == NULL) 3522 return (UINT32_MAX); 3523 if (port->lun_map == NULL) 3524 return (lun_id); 3525 return (port->lun_map(port->targ_lun_arg, lun_id)); 3526} 3527 3528static uint32_t 3529ctl_map_lun_back(int port_num, uint32_t lun_id) 3530{ 3531 struct ctl_port *port; 3532 uint32_t i; 3533 3534 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3535 if (port->lun_map == NULL) 3536 return (lun_id); 3537 for (i = 0; i < CTL_MAX_LUNS; i++) { 3538 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3539 return (i); 3540 } 3541 return (UINT32_MAX); 3542} 3543 3544/* 3545 * Note: This only works for bitmask sizes that are at least 32 bits, and 3546 * that are a power of 2. 3547 */ 3548int 3549ctl_ffz(uint32_t *mask, uint32_t size) 3550{ 3551 uint32_t num_chunks, num_pieces; 3552 int i, j; 3553 3554 num_chunks = (size >> 5); 3555 if (num_chunks == 0) 3556 num_chunks++; 3557 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3558 3559 for (i = 0; i < num_chunks; i++) { 3560 for (j = 0; j < num_pieces; j++) { 3561 if ((mask[i] & (1 << j)) == 0) 3562 return ((i << 5) + j); 3563 } 3564 } 3565 3566 return (-1); 3567} 3568 3569int 3570ctl_set_mask(uint32_t *mask, uint32_t bit) 3571{ 3572 uint32_t chunk, piece; 3573 3574 chunk = bit >> 5; 3575 piece = bit % (sizeof(uint32_t) * 8); 3576 3577 if ((mask[chunk] & (1 << piece)) != 0) 3578 return (-1); 3579 else 3580 mask[chunk] |= (1 << piece); 3581 3582 return (0); 3583} 3584 3585int 3586ctl_clear_mask(uint32_t *mask, uint32_t bit) 3587{ 3588 uint32_t chunk, piece; 3589 3590 chunk = bit >> 5; 3591 piece = bit % (sizeof(uint32_t) * 8); 3592 3593 if ((mask[chunk] & (1 << piece)) == 0) 3594 return (-1); 3595 else 3596 mask[chunk] &= ~(1 << piece); 3597 3598 return (0); 3599} 3600 3601int 3602ctl_is_set(uint32_t *mask, uint32_t bit) 3603{ 3604 uint32_t chunk, piece; 3605 3606 chunk = bit >> 5; 3607 piece = bit % (sizeof(uint32_t) * 8); 3608 3609 if ((mask[chunk] & (1 << piece)) == 0) 3610 return (0); 3611 else 3612 return (1); 3613} 3614 3615#ifdef unused 3616/* 3617 * The bus, target and lun are optional, they can be filled in later. 3618 * can_wait is used to determine whether we can wait on the malloc or not. 3619 */ 3620union ctl_io* 3621ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3622 uint32_t targ_lun, int can_wait) 3623{ 3624 union ctl_io *io; 3625 3626 if (can_wait) 3627 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3628 else 3629 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3630 3631 if (io != NULL) { 3632 io->io_hdr.io_type = io_type; 3633 io->io_hdr.targ_port = targ_port; 3634 /* 3635 * XXX KDM this needs to change/go away. We need to move 3636 * to a preallocated pool of ctl_scsiio structures. 3637 */ 3638 io->io_hdr.nexus.targ_target.id = targ_target; 3639 io->io_hdr.nexus.targ_lun = targ_lun; 3640 } 3641 3642 return (io); 3643} 3644 3645void 3646ctl_kfree_io(union ctl_io *io) 3647{ 3648 free(io, M_CTL); 3649} 3650#endif /* unused */ 3651 3652/* 3653 * ctl_softc, pool_type, total_ctl_io are passed in. 3654 * npool is passed out. 3655 */ 3656int 3657ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3658 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3659{ 3660 uint32_t i; 3661 union ctl_io *cur_io, *next_io; 3662 struct ctl_io_pool *pool; 3663 int retval; 3664 3665 retval = 0; 3666 3667 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3668 M_NOWAIT | M_ZERO); 3669 if (pool == NULL) { 3670 retval = ENOMEM; 3671 goto bailout; 3672 } 3673 3674 pool->type = pool_type; 3675 pool->ctl_softc = ctl_softc; 3676 3677 mtx_lock(&ctl_softc->pool_lock); 3678 pool->id = ctl_softc->cur_pool_id++; 3679 mtx_unlock(&ctl_softc->pool_lock); 3680 3681 pool->flags = CTL_POOL_FLAG_NONE; 3682 pool->refcount = 1; /* Reference for validity. */ 3683 STAILQ_INIT(&pool->free_queue); 3684 3685 /* 3686 * XXX KDM other options here: 3687 * - allocate a page at a time 3688 * - allocate one big chunk of memory. 3689 * Page allocation might work well, but would take a little more 3690 * tracking. 3691 */ 3692 for (i = 0; i < total_ctl_io; i++) { 3693 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3694 M_NOWAIT); 3695 if (cur_io == NULL) { 3696 retval = ENOMEM; 3697 break; 3698 } 3699 cur_io->io_hdr.pool = pool; 3700 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3701 pool->total_ctl_io++; 3702 pool->free_ctl_io++; 3703 } 3704 3705 if (retval != 0) { 3706 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3707 cur_io != NULL; cur_io = next_io) { 3708 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3709 links); 3710 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3711 ctl_io_hdr, links); 3712 free(cur_io, M_CTLIO); 3713 } 3714 3715 free(pool, M_CTL); 3716 goto bailout; 3717 } 3718 mtx_lock(&ctl_softc->pool_lock); 3719 ctl_softc->num_pools++; 3720 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3721 /* 3722 * Increment our usage count if this is an external consumer, so we 3723 * can't get unloaded until the external consumer (most likely a 3724 * FETD) unloads and frees his pool. 3725 * 3726 * XXX KDM will this increment the caller's module use count, or 3727 * mine? 3728 */ 3729#if 0 3730 if ((pool_type != CTL_POOL_EMERGENCY) 3731 && (pool_type != CTL_POOL_INTERNAL) 3732 && (pool_type != CTL_POOL_4OTHERSC)) 3733 MOD_INC_USE_COUNT; 3734#endif 3735 3736 mtx_unlock(&ctl_softc->pool_lock); 3737 3738 *npool = pool; 3739 3740bailout: 3741 3742 return (retval); 3743} 3744 3745static int 3746ctl_pool_acquire(struct ctl_io_pool *pool) 3747{ 3748 3749 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3750 3751 if (pool->flags & CTL_POOL_FLAG_INVALID) 3752 return (EINVAL); 3753 3754 pool->refcount++; 3755 3756 return (0); 3757} 3758 3759static void 3760ctl_pool_release(struct ctl_io_pool *pool) 3761{ 3762 struct ctl_softc *ctl_softc = pool->ctl_softc; 3763 union ctl_io *io; 3764 3765 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3766 3767 if (--pool->refcount != 0) 3768 return; 3769 3770 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3771 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3772 links); 3773 free(io, M_CTLIO); 3774 } 3775 3776 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3777 ctl_softc->num_pools--; 3778 3779 /* 3780 * XXX KDM will this decrement the caller's usage count or mine? 3781 */ 3782#if 0 3783 if ((pool->type != CTL_POOL_EMERGENCY) 3784 && (pool->type != CTL_POOL_INTERNAL) 3785 && (pool->type != CTL_POOL_4OTHERSC)) 3786 MOD_DEC_USE_COUNT; 3787#endif 3788 3789 free(pool, M_CTL); 3790} 3791 3792void 3793ctl_pool_free(struct ctl_io_pool *pool) 3794{ 3795 struct ctl_softc *ctl_softc; 3796 3797 if (pool == NULL) 3798 return; 3799 3800 ctl_softc = pool->ctl_softc; 3801 mtx_lock(&ctl_softc->pool_lock); 3802 pool->flags |= CTL_POOL_FLAG_INVALID; 3803 ctl_pool_release(pool); 3804 mtx_unlock(&ctl_softc->pool_lock); 3805} 3806 3807/* 3808 * This routine does not block (except for spinlocks of course). 3809 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3810 * possible. 3811 */ 3812union ctl_io * 3813ctl_alloc_io(void *pool_ref) 3814{ 3815 union ctl_io *io; 3816 struct ctl_softc *ctl_softc; 3817 struct ctl_io_pool *pool, *npool; 3818 struct ctl_io_pool *emergency_pool; 3819 3820 pool = (struct ctl_io_pool *)pool_ref; 3821 3822 if (pool == NULL) { 3823 printf("%s: pool is NULL\n", __func__); 3824 return (NULL); 3825 } 3826 3827 emergency_pool = NULL; 3828 3829 ctl_softc = pool->ctl_softc; 3830 3831 mtx_lock(&ctl_softc->pool_lock); 3832 /* 3833 * First, try to get the io structure from the user's pool. 3834 */ 3835 if (ctl_pool_acquire(pool) == 0) { 3836 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3837 if (io != NULL) { 3838 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3839 pool->total_allocated++; 3840 pool->free_ctl_io--; 3841 mtx_unlock(&ctl_softc->pool_lock); 3842 return (io); 3843 } else 3844 ctl_pool_release(pool); 3845 } 3846 /* 3847 * If he doesn't have any io structures left, search for an 3848 * emergency pool and grab one from there. 3849 */ 3850 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3851 if (npool->type != CTL_POOL_EMERGENCY) 3852 continue; 3853 3854 if (ctl_pool_acquire(npool) != 0) 3855 continue; 3856 3857 emergency_pool = npool; 3858 3859 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3860 if (io != NULL) { 3861 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3862 npool->total_allocated++; 3863 npool->free_ctl_io--; 3864 mtx_unlock(&ctl_softc->pool_lock); 3865 return (io); 3866 } else 3867 ctl_pool_release(npool); 3868 } 3869 3870 /* Drop the spinlock before we malloc */ 3871 mtx_unlock(&ctl_softc->pool_lock); 3872 3873 /* 3874 * The emergency pool (if it exists) didn't have one, so try an 3875 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3876 */ 3877 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3878 if (io != NULL) { 3879 /* 3880 * If the emergency pool exists but is empty, add this 3881 * ctl_io to its list when it gets freed. 3882 */ 3883 if (emergency_pool != NULL) { 3884 mtx_lock(&ctl_softc->pool_lock); 3885 if (ctl_pool_acquire(emergency_pool) == 0) { 3886 io->io_hdr.pool = emergency_pool; 3887 emergency_pool->total_ctl_io++; 3888 /* 3889 * Need to bump this, otherwise 3890 * total_allocated and total_freed won't 3891 * match when we no longer have anything 3892 * outstanding. 3893 */ 3894 emergency_pool->total_allocated++; 3895 } 3896 mtx_unlock(&ctl_softc->pool_lock); 3897 } else 3898 io->io_hdr.pool = NULL; 3899 } 3900 3901 return (io); 3902} 3903 3904void 3905ctl_free_io(union ctl_io *io) 3906{ 3907 if (io == NULL) 3908 return; 3909 3910 /* 3911 * If this ctl_io has a pool, return it to that pool. 3912 */ 3913 if (io->io_hdr.pool != NULL) { 3914 struct ctl_io_pool *pool; 3915 3916 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3917 mtx_lock(&pool->ctl_softc->pool_lock); 3918 io->io_hdr.io_type = 0xff; 3919 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3920 pool->total_freed++; 3921 pool->free_ctl_io++; 3922 ctl_pool_release(pool); 3923 mtx_unlock(&pool->ctl_softc->pool_lock); 3924 } else { 3925 /* 3926 * Otherwise, just free it. We probably malloced it and 3927 * the emergency pool wasn't available. 3928 */ 3929 free(io, M_CTLIO); 3930 } 3931 3932} 3933 3934void 3935ctl_zero_io(union ctl_io *io) 3936{ 3937 void *pool_ref; 3938 3939 if (io == NULL) 3940 return; 3941 3942 /* 3943 * May need to preserve linked list pointers at some point too. 3944 */ 3945 pool_ref = io->io_hdr.pool; 3946 3947 memset(io, 0, sizeof(*io)); 3948 3949 io->io_hdr.pool = pool_ref; 3950} 3951 3952/* 3953 * This routine is currently used for internal copies of ctl_ios that need 3954 * to persist for some reason after we've already returned status to the 3955 * FETD. (Thus the flag set.) 3956 * 3957 * XXX XXX 3958 * Note that this makes a blind copy of all fields in the ctl_io, except 3959 * for the pool reference. This includes any memory that has been 3960 * allocated! That memory will no longer be valid after done has been 3961 * called, so this would be VERY DANGEROUS for command that actually does 3962 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3963 * start and stop commands, which don't transfer any data, so this is not a 3964 * problem. If it is used for anything else, the caller would also need to 3965 * allocate data buffer space and this routine would need to be modified to 3966 * copy the data buffer(s) as well. 3967 */ 3968void 3969ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3970{ 3971 void *pool_ref; 3972 3973 if ((src == NULL) 3974 || (dest == NULL)) 3975 return; 3976 3977 /* 3978 * May need to preserve linked list pointers at some point too. 3979 */ 3980 pool_ref = dest->io_hdr.pool; 3981 3982 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3983 3984 dest->io_hdr.pool = pool_ref; 3985 /* 3986 * We need to know that this is an internal copy, and doesn't need 3987 * to get passed back to the FETD that allocated it. 3988 */ 3989 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3990} 3991 3992/* 3993 * This routine could be used in the future to load default and/or saved 3994 * mode page parameters for a particuar lun. 3995 */ 3996static int 3997ctl_init_page_index(struct ctl_lun *lun) 3998{ 3999 int i; 4000 struct ctl_page_index *page_index; 4001 const char *value; 4002 4003 memcpy(&lun->mode_pages.index, page_index_template, 4004 sizeof(page_index_template)); 4005 4006 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4007 4008 page_index = &lun->mode_pages.index[i]; 4009 /* 4010 * If this is a disk-only mode page, there's no point in 4011 * setting it up. For some pages, we have to have some 4012 * basic information about the disk in order to calculate the 4013 * mode page data. 4014 */ 4015 if ((lun->be_lun->lun_type != T_DIRECT) 4016 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4017 continue; 4018 4019 switch (page_index->page_code & SMPH_PC_MASK) { 4020 case SMS_RW_ERROR_RECOVERY_PAGE: { 4021 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4022 panic("subpage is incorrect!"); 4023 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4024 &rw_er_page_default, 4025 sizeof(rw_er_page_default)); 4026 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4027 &rw_er_page_changeable, 4028 sizeof(rw_er_page_changeable)); 4029 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4030 &rw_er_page_default, 4031 sizeof(rw_er_page_default)); 4032 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4033 &rw_er_page_default, 4034 sizeof(rw_er_page_default)); 4035 page_index->page_data = 4036 (uint8_t *)lun->mode_pages.rw_er_page; 4037 break; 4038 } 4039 case SMS_FORMAT_DEVICE_PAGE: { 4040 struct scsi_format_page *format_page; 4041 4042 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4043 panic("subpage is incorrect!"); 4044 4045 /* 4046 * Sectors per track are set above. Bytes per 4047 * sector need to be set here on a per-LUN basis. 4048 */ 4049 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4050 &format_page_default, 4051 sizeof(format_page_default)); 4052 memcpy(&lun->mode_pages.format_page[ 4053 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4054 sizeof(format_page_changeable)); 4055 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4056 &format_page_default, 4057 sizeof(format_page_default)); 4058 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4059 &format_page_default, 4060 sizeof(format_page_default)); 4061 4062 format_page = &lun->mode_pages.format_page[ 4063 CTL_PAGE_CURRENT]; 4064 scsi_ulto2b(lun->be_lun->blocksize, 4065 format_page->bytes_per_sector); 4066 4067 format_page = &lun->mode_pages.format_page[ 4068 CTL_PAGE_DEFAULT]; 4069 scsi_ulto2b(lun->be_lun->blocksize, 4070 format_page->bytes_per_sector); 4071 4072 format_page = &lun->mode_pages.format_page[ 4073 CTL_PAGE_SAVED]; 4074 scsi_ulto2b(lun->be_lun->blocksize, 4075 format_page->bytes_per_sector); 4076 4077 page_index->page_data = 4078 (uint8_t *)lun->mode_pages.format_page; 4079 break; 4080 } 4081 case SMS_RIGID_DISK_PAGE: { 4082 struct scsi_rigid_disk_page *rigid_disk_page; 4083 uint32_t sectors_per_cylinder; 4084 uint64_t cylinders; 4085#ifndef __XSCALE__ 4086 int shift; 4087#endif /* !__XSCALE__ */ 4088 4089 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4090 panic("invalid subpage value %d", 4091 page_index->subpage); 4092 4093 /* 4094 * Rotation rate and sectors per track are set 4095 * above. We calculate the cylinders here based on 4096 * capacity. Due to the number of heads and 4097 * sectors per track we're using, smaller arrays 4098 * may turn out to have 0 cylinders. Linux and 4099 * FreeBSD don't pay attention to these mode pages 4100 * to figure out capacity, but Solaris does. It 4101 * seems to deal with 0 cylinders just fine, and 4102 * works out a fake geometry based on the capacity. 4103 */ 4104 memcpy(&lun->mode_pages.rigid_disk_page[ 4105 CTL_PAGE_CURRENT], &rigid_disk_page_default, 4106 sizeof(rigid_disk_page_default)); 4107 memcpy(&lun->mode_pages.rigid_disk_page[ 4108 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4109 sizeof(rigid_disk_page_changeable)); 4110 memcpy(&lun->mode_pages.rigid_disk_page[ 4111 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4112 sizeof(rigid_disk_page_default)); 4113 memcpy(&lun->mode_pages.rigid_disk_page[ 4114 CTL_PAGE_SAVED], &rigid_disk_page_default, 4115 sizeof(rigid_disk_page_default)); 4116 4117 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4118 CTL_DEFAULT_HEADS; 4119 4120 /* 4121 * The divide method here will be more accurate, 4122 * probably, but results in floating point being 4123 * used in the kernel on i386 (__udivdi3()). On the 4124 * XScale, though, __udivdi3() is implemented in 4125 * software. 4126 * 4127 * The shift method for cylinder calculation is 4128 * accurate if sectors_per_cylinder is a power of 4129 * 2. Otherwise it might be slightly off -- you 4130 * might have a bit of a truncation problem. 4131 */ 4132#ifdef __XSCALE__ 4133 cylinders = (lun->be_lun->maxlba + 1) / 4134 sectors_per_cylinder; 4135#else 4136 for (shift = 31; shift > 0; shift--) { 4137 if (sectors_per_cylinder & (1 << shift)) 4138 break; 4139 } 4140 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4141#endif 4142 4143 /* 4144 * We've basically got 3 bytes, or 24 bits for the 4145 * cylinder size in the mode page. If we're over, 4146 * just round down to 2^24. 4147 */ 4148 if (cylinders > 0xffffff) 4149 cylinders = 0xffffff; 4150 4151 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4152 CTL_PAGE_CURRENT]; 4153 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4154 4155 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4156 CTL_PAGE_DEFAULT]; 4157 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4158 4159 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4160 CTL_PAGE_SAVED]; 4161 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4162 4163 page_index->page_data = 4164 (uint8_t *)lun->mode_pages.rigid_disk_page; 4165 break; 4166 } 4167 case SMS_CACHING_PAGE: { 4168 struct scsi_caching_page *caching_page; 4169 4170 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4171 panic("invalid subpage value %d", 4172 page_index->subpage); 4173 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4174 &caching_page_default, 4175 sizeof(caching_page_default)); 4176 memcpy(&lun->mode_pages.caching_page[ 4177 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4178 sizeof(caching_page_changeable)); 4179 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4180 &caching_page_default, 4181 sizeof(caching_page_default)); 4182 caching_page = &lun->mode_pages.caching_page[ 4183 CTL_PAGE_SAVED]; 4184 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4185 if (value != NULL && strcmp(value, "off") == 0) 4186 caching_page->flags1 &= ~SCP_WCE; 4187 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4188 if (value != NULL && strcmp(value, "off") == 0) 4189 caching_page->flags1 |= SCP_RCD; 4190 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4191 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4192 sizeof(caching_page_default)); 4193 page_index->page_data = 4194 (uint8_t *)lun->mode_pages.caching_page; 4195 break; 4196 } 4197 case SMS_CONTROL_MODE_PAGE: { 4198 struct scsi_control_page *control_page; 4199 4200 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4201 panic("invalid subpage value %d", 4202 page_index->subpage); 4203 4204 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4205 &control_page_default, 4206 sizeof(control_page_default)); 4207 memcpy(&lun->mode_pages.control_page[ 4208 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4209 sizeof(control_page_changeable)); 4210 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4211 &control_page_default, 4212 sizeof(control_page_default)); 4213 control_page = &lun->mode_pages.control_page[ 4214 CTL_PAGE_SAVED]; 4215 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4216 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4217 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4218 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4219 } 4220 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4221 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4222 sizeof(control_page_default)); 4223 page_index->page_data = 4224 (uint8_t *)lun->mode_pages.control_page; 4225 break; 4226 4227 } 4228 case SMS_INFO_EXCEPTIONS_PAGE: { 4229 switch (page_index->subpage) { 4230 case SMS_SUBPAGE_PAGE_0: 4231 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4232 &ie_page_default, 4233 sizeof(ie_page_default)); 4234 memcpy(&lun->mode_pages.ie_page[ 4235 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4236 sizeof(ie_page_changeable)); 4237 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4238 &ie_page_default, 4239 sizeof(ie_page_default)); 4240 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4241 &ie_page_default, 4242 sizeof(ie_page_default)); 4243 page_index->page_data = 4244 (uint8_t *)lun->mode_pages.ie_page; 4245 break; 4246 case 0x02: 4247 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4248 &lbp_page_default, 4249 sizeof(lbp_page_default)); 4250 memcpy(&lun->mode_pages.lbp_page[ 4251 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4252 sizeof(lbp_page_changeable)); 4253 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4254 &lbp_page_default, 4255 sizeof(lbp_page_default)); 4256 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4257 &lbp_page_default, 4258 sizeof(lbp_page_default)); 4259 page_index->page_data = 4260 (uint8_t *)lun->mode_pages.lbp_page; 4261 } 4262 break; 4263 } 4264 case SMS_VENDOR_SPECIFIC_PAGE:{ 4265 switch (page_index->subpage) { 4266 case DBGCNF_SUBPAGE_CODE: { 4267 struct copan_debugconf_subpage *current_page, 4268 *saved_page; 4269 4270 memcpy(&lun->mode_pages.debugconf_subpage[ 4271 CTL_PAGE_CURRENT], 4272 &debugconf_page_default, 4273 sizeof(debugconf_page_default)); 4274 memcpy(&lun->mode_pages.debugconf_subpage[ 4275 CTL_PAGE_CHANGEABLE], 4276 &debugconf_page_changeable, 4277 sizeof(debugconf_page_changeable)); 4278 memcpy(&lun->mode_pages.debugconf_subpage[ 4279 CTL_PAGE_DEFAULT], 4280 &debugconf_page_default, 4281 sizeof(debugconf_page_default)); 4282 memcpy(&lun->mode_pages.debugconf_subpage[ 4283 CTL_PAGE_SAVED], 4284 &debugconf_page_default, 4285 sizeof(debugconf_page_default)); 4286 page_index->page_data = 4287 (uint8_t *)lun->mode_pages.debugconf_subpage; 4288 4289 current_page = (struct copan_debugconf_subpage *) 4290 (page_index->page_data + 4291 (page_index->page_len * 4292 CTL_PAGE_CURRENT)); 4293 saved_page = (struct copan_debugconf_subpage *) 4294 (page_index->page_data + 4295 (page_index->page_len * 4296 CTL_PAGE_SAVED)); 4297 break; 4298 } 4299 default: 4300 panic("invalid subpage value %d", 4301 page_index->subpage); 4302 break; 4303 } 4304 break; 4305 } 4306 default: 4307 panic("invalid page value %d", 4308 page_index->page_code & SMPH_PC_MASK); 4309 break; 4310 } 4311 } 4312 4313 return (CTL_RETVAL_COMPLETE); 4314} 4315 4316static int 4317ctl_init_log_page_index(struct ctl_lun *lun) 4318{ 4319 struct ctl_page_index *page_index; 4320 int i, j, prev; 4321 4322 memcpy(&lun->log_pages.index, log_page_index_template, 4323 sizeof(log_page_index_template)); 4324 4325 prev = -1; 4326 for (i = 0, j = 0; i < CTL_NUM_LOG_PAGES; i++) { 4327 4328 page_index = &lun->log_pages.index[i]; 4329 /* 4330 * If this is a disk-only mode page, there's no point in 4331 * setting it up. For some pages, we have to have some 4332 * basic information about the disk in order to calculate the 4333 * mode page data. 4334 */ 4335 if ((lun->be_lun->lun_type != T_DIRECT) 4336 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4337 continue; 4338 4339 if (page_index->page_code != prev) { 4340 lun->log_pages.pages_page[j] = page_index->page_code; 4341 prev = page_index->page_code; 4342 j++; 4343 } 4344 lun->log_pages.subpages_page[i*2] = page_index->page_code; 4345 lun->log_pages.subpages_page[i*2+1] = page_index->subpage; 4346 } 4347 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4348 lun->log_pages.index[0].page_len = j; 4349 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4350 lun->log_pages.index[1].page_len = i * 2; 4351 4352 return (CTL_RETVAL_COMPLETE); 4353} 4354 4355static int 4356hex2bin(const char *str, uint8_t *buf, int buf_size) 4357{ 4358 int i; 4359 u_char c; 4360 4361 memset(buf, 0, buf_size); 4362 while (isspace(str[0])) 4363 str++; 4364 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4365 str += 2; 4366 buf_size *= 2; 4367 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4368 c = str[i]; 4369 if (isdigit(c)) 4370 c -= '0'; 4371 else if (isalpha(c)) 4372 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4373 else 4374 break; 4375 if (c >= 16) 4376 break; 4377 if ((i & 1) == 0) 4378 buf[i / 2] |= (c << 4); 4379 else 4380 buf[i / 2] |= c; 4381 } 4382 return ((i + 1) / 2); 4383} 4384 4385/* 4386 * LUN allocation. 4387 * 4388 * Requirements: 4389 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4390 * wants us to allocate the LUN and he can block. 4391 * - ctl_softc is always set 4392 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4393 * 4394 * Returns 0 for success, non-zero (errno) for failure. 4395 */ 4396static int 4397ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4398 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4399{ 4400 struct ctl_lun *nlun, *lun; 4401 struct ctl_port *port; 4402 struct scsi_vpd_id_descriptor *desc; 4403 struct scsi_vpd_id_t10 *t10id; 4404 const char *eui, *naa, *scsiname, *vendor, *value; 4405 int lun_number, i, lun_malloced; 4406 int devidlen, idlen1, idlen2 = 0, len; 4407 4408 if (be_lun == NULL) 4409 return (EINVAL); 4410 4411 /* 4412 * We currently only support Direct Access or Processor LUN types. 4413 */ 4414 switch (be_lun->lun_type) { 4415 case T_DIRECT: 4416 break; 4417 case T_PROCESSOR: 4418 break; 4419 case T_SEQUENTIAL: 4420 case T_CHANGER: 4421 default: 4422 be_lun->lun_config_status(be_lun->be_lun, 4423 CTL_LUN_CONFIG_FAILURE); 4424 break; 4425 } 4426 if (ctl_lun == NULL) { 4427 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4428 lun_malloced = 1; 4429 } else { 4430 lun_malloced = 0; 4431 lun = ctl_lun; 4432 } 4433 4434 memset(lun, 0, sizeof(*lun)); 4435 if (lun_malloced) 4436 lun->flags = CTL_LUN_MALLOCED; 4437 4438 /* Generate LUN ID. */ 4439 devidlen = max(CTL_DEVID_MIN_LEN, 4440 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4441 idlen1 = sizeof(*t10id) + devidlen; 4442 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4443 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4444 if (scsiname != NULL) { 4445 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4446 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4447 } 4448 eui = ctl_get_opt(&be_lun->options, "eui"); 4449 if (eui != NULL) { 4450 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4451 } 4452 naa = ctl_get_opt(&be_lun->options, "naa"); 4453 if (naa != NULL) { 4454 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4455 } 4456 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4457 M_CTL, M_WAITOK | M_ZERO); 4458 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4459 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4460 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4461 desc->length = idlen1; 4462 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4463 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4464 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4465 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4466 } else { 4467 strncpy(t10id->vendor, vendor, 4468 min(sizeof(t10id->vendor), strlen(vendor))); 4469 } 4470 strncpy((char *)t10id->vendor_spec_id, 4471 (char *)be_lun->device_id, devidlen); 4472 if (scsiname != NULL) { 4473 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4474 desc->length); 4475 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4476 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4477 SVPD_ID_TYPE_SCSI_NAME; 4478 desc->length = idlen2; 4479 strlcpy(desc->identifier, scsiname, idlen2); 4480 } 4481 if (eui != NULL) { 4482 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4483 desc->length); 4484 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4485 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4486 SVPD_ID_TYPE_EUI64; 4487 desc->length = hex2bin(eui, desc->identifier, 16); 4488 desc->length = desc->length > 12 ? 16 : 4489 (desc->length > 8 ? 12 : 8); 4490 len -= 16 - desc->length; 4491 } 4492 if (naa != NULL) { 4493 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4494 desc->length); 4495 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4496 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4497 SVPD_ID_TYPE_NAA; 4498 desc->length = hex2bin(naa, desc->identifier, 16); 4499 desc->length = desc->length > 8 ? 16 : 8; 4500 len -= 16 - desc->length; 4501 } 4502 lun->lun_devid->len = len; 4503 4504 mtx_lock(&ctl_softc->ctl_lock); 4505 /* 4506 * See if the caller requested a particular LUN number. If so, see 4507 * if it is available. Otherwise, allocate the first available LUN. 4508 */ 4509 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4510 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4511 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4512 mtx_unlock(&ctl_softc->ctl_lock); 4513 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4514 printf("ctl: requested LUN ID %d is higher " 4515 "than CTL_MAX_LUNS - 1 (%d)\n", 4516 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4517 } else { 4518 /* 4519 * XXX KDM return an error, or just assign 4520 * another LUN ID in this case?? 4521 */ 4522 printf("ctl: requested LUN ID %d is already " 4523 "in use\n", be_lun->req_lun_id); 4524 } 4525 if (lun->flags & CTL_LUN_MALLOCED) 4526 free(lun, M_CTL); 4527 be_lun->lun_config_status(be_lun->be_lun, 4528 CTL_LUN_CONFIG_FAILURE); 4529 return (ENOSPC); 4530 } 4531 lun_number = be_lun->req_lun_id; 4532 } else { 4533 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4534 if (lun_number == -1) { 4535 mtx_unlock(&ctl_softc->ctl_lock); 4536 printf("ctl: can't allocate LUN on target %ju, out of " 4537 "LUNs\n", (uintmax_t)target_id.id); 4538 if (lun->flags & CTL_LUN_MALLOCED) 4539 free(lun, M_CTL); 4540 be_lun->lun_config_status(be_lun->be_lun, 4541 CTL_LUN_CONFIG_FAILURE); 4542 return (ENOSPC); 4543 } 4544 } 4545 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4546 4547 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4548 lun->target = target_id; 4549 lun->lun = lun_number; 4550 lun->be_lun = be_lun; 4551 /* 4552 * The processor LUN is always enabled. Disk LUNs come on line 4553 * disabled, and must be enabled by the backend. 4554 */ 4555 lun->flags |= CTL_LUN_DISABLED; 4556 lun->backend = be_lun->be; 4557 be_lun->ctl_lun = lun; 4558 be_lun->lun_id = lun_number; 4559 atomic_add_int(&be_lun->be->num_luns, 1); 4560 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4561 lun->flags |= CTL_LUN_OFFLINE; 4562 4563 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4564 lun->flags |= CTL_LUN_STOPPED; 4565 4566 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4567 lun->flags |= CTL_LUN_INOPERABLE; 4568 4569 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4570 lun->flags |= CTL_LUN_PRIMARY_SC; 4571 4572 value = ctl_get_opt(&be_lun->options, "readonly"); 4573 if (value != NULL && strcmp(value, "on") == 0) 4574 lun->flags |= CTL_LUN_READONLY; 4575 4576 lun->ctl_softc = ctl_softc; 4577 TAILQ_INIT(&lun->ooa_queue); 4578 TAILQ_INIT(&lun->blocked_queue); 4579 STAILQ_INIT(&lun->error_list); 4580 ctl_tpc_lun_init(lun); 4581 4582 /* 4583 * Initialize the mode and log page index. 4584 */ 4585 ctl_init_page_index(lun); 4586 ctl_init_log_page_index(lun); 4587 4588 /* 4589 * Set the poweron UA for all initiators on this LUN only. 4590 */ 4591 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4592 lun->pending_ua[i] = CTL_UA_POWERON; 4593 4594 /* 4595 * Now, before we insert this lun on the lun list, set the lun 4596 * inventory changed UA for all other luns. 4597 */ 4598 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4599 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4600 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4601 } 4602 } 4603 4604 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4605 4606 ctl_softc->ctl_luns[lun_number] = lun; 4607 4608 ctl_softc->num_luns++; 4609 4610 /* Setup statistics gathering */ 4611 lun->stats.device_type = be_lun->lun_type; 4612 lun->stats.lun_number = lun_number; 4613 if (lun->stats.device_type == T_DIRECT) 4614 lun->stats.blocksize = be_lun->blocksize; 4615 else 4616 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4617 for (i = 0;i < CTL_MAX_PORTS;i++) 4618 lun->stats.ports[i].targ_port = i; 4619 4620 mtx_unlock(&ctl_softc->ctl_lock); 4621 4622 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4623 4624 /* 4625 * Run through each registered FETD and bring it online if it isn't 4626 * already. Enable the target ID if it hasn't been enabled, and 4627 * enable this particular LUN. 4628 */ 4629 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4630 int retval; 4631 4632 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4633 if (retval != 0) { 4634 printf("ctl_alloc_lun: FETD %s port %d returned error " 4635 "%d for lun_enable on target %ju lun %d\n", 4636 port->port_name, port->targ_port, retval, 4637 (uintmax_t)target_id.id, lun_number); 4638 } else 4639 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4640 } 4641 return (0); 4642} 4643 4644/* 4645 * Delete a LUN. 4646 * Assumptions: 4647 * - LUN has already been marked invalid and any pending I/O has been taken 4648 * care of. 4649 */ 4650static int 4651ctl_free_lun(struct ctl_lun *lun) 4652{ 4653 struct ctl_softc *softc; 4654#if 0 4655 struct ctl_port *port; 4656#endif 4657 struct ctl_lun *nlun; 4658 int i; 4659 4660 softc = lun->ctl_softc; 4661 4662 mtx_assert(&softc->ctl_lock, MA_OWNED); 4663 4664 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4665 4666 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4667 4668 softc->ctl_luns[lun->lun] = NULL; 4669 4670 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4671 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4672 4673 softc->num_luns--; 4674 4675 /* 4676 * XXX KDM this scheme only works for a single target/multiple LUN 4677 * setup. It needs to be revamped for a multiple target scheme. 4678 * 4679 * XXX KDM this results in port->lun_disable() getting called twice, 4680 * once when ctl_disable_lun() is called, and a second time here. 4681 * We really need to re-think the LUN disable semantics. There 4682 * should probably be several steps/levels to LUN removal: 4683 * - disable 4684 * - invalidate 4685 * - free 4686 * 4687 * Right now we only have a disable method when communicating to 4688 * the front end ports, at least for individual LUNs. 4689 */ 4690#if 0 4691 STAILQ_FOREACH(port, &softc->port_list, links) { 4692 int retval; 4693 4694 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4695 lun->lun); 4696 if (retval != 0) { 4697 printf("ctl_free_lun: FETD %s port %d returned error " 4698 "%d for lun_disable on target %ju lun %jd\n", 4699 port->port_name, port->targ_port, retval, 4700 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4701 } 4702 4703 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4704 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4705 4706 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4707 if (retval != 0) { 4708 printf("ctl_free_lun: FETD %s port %d " 4709 "returned error %d for targ_disable on " 4710 "target %ju\n", port->port_name, 4711 port->targ_port, retval, 4712 (uintmax_t)lun->target.id); 4713 } else 4714 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4715 4716 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4717 continue; 4718 4719#if 0 4720 port->port_offline(port->onoff_arg); 4721 port->status &= ~CTL_PORT_STATUS_ONLINE; 4722#endif 4723 } 4724 } 4725#endif 4726 4727 /* 4728 * Tell the backend to free resources, if this LUN has a backend. 4729 */ 4730 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4731 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4732 4733 ctl_tpc_lun_shutdown(lun); 4734 mtx_destroy(&lun->lun_lock); 4735 free(lun->lun_devid, M_CTL); 4736 if (lun->flags & CTL_LUN_MALLOCED) 4737 free(lun, M_CTL); 4738 4739 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4740 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4741 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4742 } 4743 } 4744 4745 return (0); 4746} 4747 4748static void 4749ctl_create_lun(struct ctl_be_lun *be_lun) 4750{ 4751 struct ctl_softc *ctl_softc; 4752 4753 ctl_softc = control_softc; 4754 4755 /* 4756 * ctl_alloc_lun() should handle all potential failure cases. 4757 */ 4758 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4759} 4760 4761int 4762ctl_add_lun(struct ctl_be_lun *be_lun) 4763{ 4764 struct ctl_softc *ctl_softc = control_softc; 4765 4766 mtx_lock(&ctl_softc->ctl_lock); 4767 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4768 mtx_unlock(&ctl_softc->ctl_lock); 4769 wakeup(&ctl_softc->pending_lun_queue); 4770 4771 return (0); 4772} 4773 4774int 4775ctl_enable_lun(struct ctl_be_lun *be_lun) 4776{ 4777 struct ctl_softc *ctl_softc; 4778 struct ctl_port *port, *nport; 4779 struct ctl_lun *lun; 4780 int retval; 4781 4782 ctl_softc = control_softc; 4783 4784 lun = (struct ctl_lun *)be_lun->ctl_lun; 4785 4786 mtx_lock(&ctl_softc->ctl_lock); 4787 mtx_lock(&lun->lun_lock); 4788 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4789 /* 4790 * eh? Why did we get called if the LUN is already 4791 * enabled? 4792 */ 4793 mtx_unlock(&lun->lun_lock); 4794 mtx_unlock(&ctl_softc->ctl_lock); 4795 return (0); 4796 } 4797 lun->flags &= ~CTL_LUN_DISABLED; 4798 mtx_unlock(&lun->lun_lock); 4799 4800 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4801 nport = STAILQ_NEXT(port, links); 4802 4803 /* 4804 * Drop the lock while we call the FETD's enable routine. 4805 * This can lead to a callback into CTL (at least in the 4806 * case of the internal initiator frontend. 4807 */ 4808 mtx_unlock(&ctl_softc->ctl_lock); 4809 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4810 mtx_lock(&ctl_softc->ctl_lock); 4811 if (retval != 0) { 4812 printf("%s: FETD %s port %d returned error " 4813 "%d for lun_enable on target %ju lun %jd\n", 4814 __func__, port->port_name, port->targ_port, retval, 4815 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4816 } 4817#if 0 4818 else { 4819 /* NOTE: TODO: why does lun enable affect port status? */ 4820 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4821 } 4822#endif 4823 } 4824 4825 mtx_unlock(&ctl_softc->ctl_lock); 4826 4827 return (0); 4828} 4829 4830int 4831ctl_disable_lun(struct ctl_be_lun *be_lun) 4832{ 4833 struct ctl_softc *ctl_softc; 4834 struct ctl_port *port; 4835 struct ctl_lun *lun; 4836 int retval; 4837 4838 ctl_softc = control_softc; 4839 4840 lun = (struct ctl_lun *)be_lun->ctl_lun; 4841 4842 mtx_lock(&ctl_softc->ctl_lock); 4843 mtx_lock(&lun->lun_lock); 4844 if (lun->flags & CTL_LUN_DISABLED) { 4845 mtx_unlock(&lun->lun_lock); 4846 mtx_unlock(&ctl_softc->ctl_lock); 4847 return (0); 4848 } 4849 lun->flags |= CTL_LUN_DISABLED; 4850 mtx_unlock(&lun->lun_lock); 4851 4852 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4853 mtx_unlock(&ctl_softc->ctl_lock); 4854 /* 4855 * Drop the lock before we call the frontend's disable 4856 * routine, to avoid lock order reversals. 4857 * 4858 * XXX KDM what happens if the frontend list changes while 4859 * we're traversing it? It's unlikely, but should be handled. 4860 */ 4861 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4862 lun->lun); 4863 mtx_lock(&ctl_softc->ctl_lock); 4864 if (retval != 0) { 4865 printf("ctl_alloc_lun: FETD %s port %d returned error " 4866 "%d for lun_disable on target %ju lun %jd\n", 4867 port->port_name, port->targ_port, retval, 4868 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4869 } 4870 } 4871 4872 mtx_unlock(&ctl_softc->ctl_lock); 4873 4874 return (0); 4875} 4876 4877int 4878ctl_start_lun(struct ctl_be_lun *be_lun) 4879{ 4880 struct ctl_softc *ctl_softc; 4881 struct ctl_lun *lun; 4882 4883 ctl_softc = control_softc; 4884 4885 lun = (struct ctl_lun *)be_lun->ctl_lun; 4886 4887 mtx_lock(&lun->lun_lock); 4888 lun->flags &= ~CTL_LUN_STOPPED; 4889 mtx_unlock(&lun->lun_lock); 4890 4891 return (0); 4892} 4893 4894int 4895ctl_stop_lun(struct ctl_be_lun *be_lun) 4896{ 4897 struct ctl_softc *ctl_softc; 4898 struct ctl_lun *lun; 4899 4900 ctl_softc = control_softc; 4901 4902 lun = (struct ctl_lun *)be_lun->ctl_lun; 4903 4904 mtx_lock(&lun->lun_lock); 4905 lun->flags |= CTL_LUN_STOPPED; 4906 mtx_unlock(&lun->lun_lock); 4907 4908 return (0); 4909} 4910 4911int 4912ctl_lun_offline(struct ctl_be_lun *be_lun) 4913{ 4914 struct ctl_softc *ctl_softc; 4915 struct ctl_lun *lun; 4916 4917 ctl_softc = control_softc; 4918 4919 lun = (struct ctl_lun *)be_lun->ctl_lun; 4920 4921 mtx_lock(&lun->lun_lock); 4922 lun->flags |= CTL_LUN_OFFLINE; 4923 mtx_unlock(&lun->lun_lock); 4924 4925 return (0); 4926} 4927 4928int 4929ctl_lun_online(struct ctl_be_lun *be_lun) 4930{ 4931 struct ctl_softc *ctl_softc; 4932 struct ctl_lun *lun; 4933 4934 ctl_softc = control_softc; 4935 4936 lun = (struct ctl_lun *)be_lun->ctl_lun; 4937 4938 mtx_lock(&lun->lun_lock); 4939 lun->flags &= ~CTL_LUN_OFFLINE; 4940 mtx_unlock(&lun->lun_lock); 4941 4942 return (0); 4943} 4944 4945int 4946ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4947{ 4948 struct ctl_softc *ctl_softc; 4949 struct ctl_lun *lun; 4950 4951 ctl_softc = control_softc; 4952 4953 lun = (struct ctl_lun *)be_lun->ctl_lun; 4954 4955 mtx_lock(&lun->lun_lock); 4956 4957 /* 4958 * The LUN needs to be disabled before it can be marked invalid. 4959 */ 4960 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4961 mtx_unlock(&lun->lun_lock); 4962 return (-1); 4963 } 4964 /* 4965 * Mark the LUN invalid. 4966 */ 4967 lun->flags |= CTL_LUN_INVALID; 4968 4969 /* 4970 * If there is nothing in the OOA queue, go ahead and free the LUN. 4971 * If we have something in the OOA queue, we'll free it when the 4972 * last I/O completes. 4973 */ 4974 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4975 mtx_unlock(&lun->lun_lock); 4976 mtx_lock(&ctl_softc->ctl_lock); 4977 ctl_free_lun(lun); 4978 mtx_unlock(&ctl_softc->ctl_lock); 4979 } else 4980 mtx_unlock(&lun->lun_lock); 4981 4982 return (0); 4983} 4984 4985int 4986ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4987{ 4988 struct ctl_softc *ctl_softc; 4989 struct ctl_lun *lun; 4990 4991 ctl_softc = control_softc; 4992 lun = (struct ctl_lun *)be_lun->ctl_lun; 4993 4994 mtx_lock(&lun->lun_lock); 4995 lun->flags |= CTL_LUN_INOPERABLE; 4996 mtx_unlock(&lun->lun_lock); 4997 4998 return (0); 4999} 5000 5001int 5002ctl_lun_operable(struct ctl_be_lun *be_lun) 5003{ 5004 struct ctl_softc *ctl_softc; 5005 struct ctl_lun *lun; 5006 5007 ctl_softc = control_softc; 5008 lun = (struct ctl_lun *)be_lun->ctl_lun; 5009 5010 mtx_lock(&lun->lun_lock); 5011 lun->flags &= ~CTL_LUN_INOPERABLE; 5012 mtx_unlock(&lun->lun_lock); 5013 5014 return (0); 5015} 5016 5017void 5018ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5019{ 5020 struct ctl_lun *lun; 5021 struct ctl_softc *softc; 5022 int i; 5023 5024 softc = control_softc; 5025 5026 lun = (struct ctl_lun *)be_lun->ctl_lun; 5027 5028 mtx_lock(&lun->lun_lock); 5029 5030 for (i = 0; i < CTL_MAX_INITIATORS; i++) 5031 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 5032 5033 mtx_unlock(&lun->lun_lock); 5034} 5035 5036/* 5037 * Backend "memory move is complete" callback for requests that never 5038 * make it down to say RAIDCore's configuration code. 5039 */ 5040int 5041ctl_config_move_done(union ctl_io *io) 5042{ 5043 int retval; 5044 5045 retval = CTL_RETVAL_COMPLETE; 5046 5047 5048 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5049 /* 5050 * XXX KDM this shouldn't happen, but what if it does? 5051 */ 5052 if (io->io_hdr.io_type != CTL_IO_SCSI) 5053 panic("I/O type isn't CTL_IO_SCSI!"); 5054 5055 if ((io->io_hdr.port_status == 0) 5056 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5057 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 5058 io->io_hdr.status = CTL_SUCCESS; 5059 else if ((io->io_hdr.port_status != 0) 5060 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 5061 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 5062 /* 5063 * For hardware error sense keys, the sense key 5064 * specific value is defined to be a retry count, 5065 * but we use it to pass back an internal FETD 5066 * error code. XXX KDM Hopefully the FETD is only 5067 * using 16 bits for an error code, since that's 5068 * all the space we have in the sks field. 5069 */ 5070 ctl_set_internal_failure(&io->scsiio, 5071 /*sks_valid*/ 1, 5072 /*retry_count*/ 5073 io->io_hdr.port_status); 5074 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5075 free(io->scsiio.kern_data_ptr, M_CTL); 5076 ctl_done(io); 5077 goto bailout; 5078 } 5079 5080 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 5081 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 5082 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5083 /* 5084 * XXX KDM just assuming a single pointer here, and not a 5085 * S/G list. If we start using S/G lists for config data, 5086 * we'll need to know how to clean them up here as well. 5087 */ 5088 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5089 free(io->scsiio.kern_data_ptr, M_CTL); 5090 /* Hopefully the user has already set the status... */ 5091 ctl_done(io); 5092 } else { 5093 /* 5094 * XXX KDM now we need to continue data movement. Some 5095 * options: 5096 * - call ctl_scsiio() again? We don't do this for data 5097 * writes, because for those at least we know ahead of 5098 * time where the write will go and how long it is. For 5099 * config writes, though, that information is largely 5100 * contained within the write itself, thus we need to 5101 * parse out the data again. 5102 * 5103 * - Call some other function once the data is in? 5104 */ 5105 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5106 ctl_data_print(io); 5107 5108 /* 5109 * XXX KDM call ctl_scsiio() again for now, and check flag 5110 * bits to see whether we're allocated or not. 5111 */ 5112 retval = ctl_scsiio(&io->scsiio); 5113 } 5114bailout: 5115 return (retval); 5116} 5117 5118/* 5119 * This gets called by a backend driver when it is done with a 5120 * data_submit method. 5121 */ 5122void 5123ctl_data_submit_done(union ctl_io *io) 5124{ 5125 /* 5126 * If the IO_CONT flag is set, we need to call the supplied 5127 * function to continue processing the I/O, instead of completing 5128 * the I/O just yet. 5129 * 5130 * If there is an error, though, we don't want to keep processing. 5131 * Instead, just send status back to the initiator. 5132 */ 5133 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5134 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5135 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5136 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5137 io->scsiio.io_cont(io); 5138 return; 5139 } 5140 ctl_done(io); 5141} 5142 5143/* 5144 * This gets called by a backend driver when it is done with a 5145 * configuration write. 5146 */ 5147void 5148ctl_config_write_done(union ctl_io *io) 5149{ 5150 uint8_t *buf; 5151 5152 /* 5153 * If the IO_CONT flag is set, we need to call the supplied 5154 * function to continue processing the I/O, instead of completing 5155 * the I/O just yet. 5156 * 5157 * If there is an error, though, we don't want to keep processing. 5158 * Instead, just send status back to the initiator. 5159 */ 5160 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5161 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5162 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5163 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5164 io->scsiio.io_cont(io); 5165 return; 5166 } 5167 /* 5168 * Since a configuration write can be done for commands that actually 5169 * have data allocated, like write buffer, and commands that have 5170 * no data, like start/stop unit, we need to check here. 5171 */ 5172 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5173 buf = io->scsiio.kern_data_ptr; 5174 else 5175 buf = NULL; 5176 ctl_done(io); 5177 if (buf) 5178 free(buf, M_CTL); 5179} 5180 5181/* 5182 * SCSI release command. 5183 */ 5184int 5185ctl_scsi_release(struct ctl_scsiio *ctsio) 5186{ 5187 int length, longid, thirdparty_id, resv_id; 5188 struct ctl_softc *ctl_softc; 5189 struct ctl_lun *lun; 5190 uint32_t residx; 5191 5192 length = 0; 5193 resv_id = 0; 5194 5195 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5196 5197 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5198 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5199 ctl_softc = control_softc; 5200 5201 switch (ctsio->cdb[0]) { 5202 case RELEASE_10: { 5203 struct scsi_release_10 *cdb; 5204 5205 cdb = (struct scsi_release_10 *)ctsio->cdb; 5206 5207 if (cdb->byte2 & SR10_LONGID) 5208 longid = 1; 5209 else 5210 thirdparty_id = cdb->thirdparty_id; 5211 5212 resv_id = cdb->resv_id; 5213 length = scsi_2btoul(cdb->length); 5214 break; 5215 } 5216 } 5217 5218 5219 /* 5220 * XXX KDM right now, we only support LUN reservation. We don't 5221 * support 3rd party reservations, or extent reservations, which 5222 * might actually need the parameter list. If we've gotten this 5223 * far, we've got a LUN reservation. Anything else got kicked out 5224 * above. So, according to SPC, ignore the length. 5225 */ 5226 length = 0; 5227 5228 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5229 && (length > 0)) { 5230 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5231 ctsio->kern_data_len = length; 5232 ctsio->kern_total_len = length; 5233 ctsio->kern_data_resid = 0; 5234 ctsio->kern_rel_offset = 0; 5235 ctsio->kern_sg_entries = 0; 5236 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5237 ctsio->be_move_done = ctl_config_move_done; 5238 ctl_datamove((union ctl_io *)ctsio); 5239 5240 return (CTL_RETVAL_COMPLETE); 5241 } 5242 5243 if (length > 0) 5244 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5245 5246 mtx_lock(&lun->lun_lock); 5247 5248 /* 5249 * According to SPC, it is not an error for an intiator to attempt 5250 * to release a reservation on a LUN that isn't reserved, or that 5251 * is reserved by another initiator. The reservation can only be 5252 * released, though, by the initiator who made it or by one of 5253 * several reset type events. 5254 */ 5255 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5256 lun->flags &= ~CTL_LUN_RESERVED; 5257 5258 mtx_unlock(&lun->lun_lock); 5259 5260 ctsio->scsi_status = SCSI_STATUS_OK; 5261 ctsio->io_hdr.status = CTL_SUCCESS; 5262 5263 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5264 free(ctsio->kern_data_ptr, M_CTL); 5265 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5266 } 5267 5268 ctl_done((union ctl_io *)ctsio); 5269 return (CTL_RETVAL_COMPLETE); 5270} 5271 5272int 5273ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5274{ 5275 int extent, thirdparty, longid; 5276 int resv_id, length; 5277 uint64_t thirdparty_id; 5278 struct ctl_softc *ctl_softc; 5279 struct ctl_lun *lun; 5280 uint32_t residx; 5281 5282 extent = 0; 5283 thirdparty = 0; 5284 longid = 0; 5285 resv_id = 0; 5286 length = 0; 5287 thirdparty_id = 0; 5288 5289 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5290 5291 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5292 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5293 ctl_softc = control_softc; 5294 5295 switch (ctsio->cdb[0]) { 5296 case RESERVE_10: { 5297 struct scsi_reserve_10 *cdb; 5298 5299 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5300 5301 if (cdb->byte2 & SR10_LONGID) 5302 longid = 1; 5303 else 5304 thirdparty_id = cdb->thirdparty_id; 5305 5306 resv_id = cdb->resv_id; 5307 length = scsi_2btoul(cdb->length); 5308 break; 5309 } 5310 } 5311 5312 /* 5313 * XXX KDM right now, we only support LUN reservation. We don't 5314 * support 3rd party reservations, or extent reservations, which 5315 * might actually need the parameter list. If we've gotten this 5316 * far, we've got a LUN reservation. Anything else got kicked out 5317 * above. So, according to SPC, ignore the length. 5318 */ 5319 length = 0; 5320 5321 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5322 && (length > 0)) { 5323 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5324 ctsio->kern_data_len = length; 5325 ctsio->kern_total_len = length; 5326 ctsio->kern_data_resid = 0; 5327 ctsio->kern_rel_offset = 0; 5328 ctsio->kern_sg_entries = 0; 5329 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5330 ctsio->be_move_done = ctl_config_move_done; 5331 ctl_datamove((union ctl_io *)ctsio); 5332 5333 return (CTL_RETVAL_COMPLETE); 5334 } 5335 5336 if (length > 0) 5337 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5338 5339 mtx_lock(&lun->lun_lock); 5340 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5341 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5342 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5343 goto bailout; 5344 } 5345 5346 lun->flags |= CTL_LUN_RESERVED; 5347 lun->res_idx = residx; 5348 5349 ctsio->scsi_status = SCSI_STATUS_OK; 5350 ctsio->io_hdr.status = CTL_SUCCESS; 5351 5352bailout: 5353 mtx_unlock(&lun->lun_lock); 5354 5355 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5356 free(ctsio->kern_data_ptr, M_CTL); 5357 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5358 } 5359 5360 ctl_done((union ctl_io *)ctsio); 5361 return (CTL_RETVAL_COMPLETE); 5362} 5363 5364int 5365ctl_start_stop(struct ctl_scsiio *ctsio) 5366{ 5367 struct scsi_start_stop_unit *cdb; 5368 struct ctl_lun *lun; 5369 struct ctl_softc *ctl_softc; 5370 int retval; 5371 5372 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5373 5374 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5375 ctl_softc = control_softc; 5376 retval = 0; 5377 5378 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5379 5380 /* 5381 * XXX KDM 5382 * We don't support the immediate bit on a stop unit. In order to 5383 * do that, we would need to code up a way to know that a stop is 5384 * pending, and hold off any new commands until it completes, one 5385 * way or another. Then we could accept or reject those commands 5386 * depending on its status. We would almost need to do the reverse 5387 * of what we do below for an immediate start -- return the copy of 5388 * the ctl_io to the FETD with status to send to the host (and to 5389 * free the copy!) and then free the original I/O once the stop 5390 * actually completes. That way, the OOA queue mechanism can work 5391 * to block commands that shouldn't proceed. Another alternative 5392 * would be to put the copy in the queue in place of the original, 5393 * and return the original back to the caller. That could be 5394 * slightly safer.. 5395 */ 5396 if ((cdb->byte2 & SSS_IMMED) 5397 && ((cdb->how & SSS_START) == 0)) { 5398 ctl_set_invalid_field(ctsio, 5399 /*sks_valid*/ 1, 5400 /*command*/ 1, 5401 /*field*/ 1, 5402 /*bit_valid*/ 1, 5403 /*bit*/ 0); 5404 ctl_done((union ctl_io *)ctsio); 5405 return (CTL_RETVAL_COMPLETE); 5406 } 5407 5408 if ((lun->flags & CTL_LUN_PR_RESERVED) 5409 && ((cdb->how & SSS_START)==0)) { 5410 uint32_t residx; 5411 5412 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5413 if (lun->pr_keys[residx] == 0 5414 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5415 5416 ctl_set_reservation_conflict(ctsio); 5417 ctl_done((union ctl_io *)ctsio); 5418 return (CTL_RETVAL_COMPLETE); 5419 } 5420 } 5421 5422 /* 5423 * If there is no backend on this device, we can't start or stop 5424 * it. In theory we shouldn't get any start/stop commands in the 5425 * first place at this level if the LUN doesn't have a backend. 5426 * That should get stopped by the command decode code. 5427 */ 5428 if (lun->backend == NULL) { 5429 ctl_set_invalid_opcode(ctsio); 5430 ctl_done((union ctl_io *)ctsio); 5431 return (CTL_RETVAL_COMPLETE); 5432 } 5433 5434 /* 5435 * XXX KDM Copan-specific offline behavior. 5436 * Figure out a reasonable way to port this? 5437 */ 5438#ifdef NEEDTOPORT 5439 mtx_lock(&lun->lun_lock); 5440 5441 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5442 && (lun->flags & CTL_LUN_OFFLINE)) { 5443 /* 5444 * If the LUN is offline, and the on/offline bit isn't set, 5445 * reject the start or stop. Otherwise, let it through. 5446 */ 5447 mtx_unlock(&lun->lun_lock); 5448 ctl_set_lun_not_ready(ctsio); 5449 ctl_done((union ctl_io *)ctsio); 5450 } else { 5451 mtx_unlock(&lun->lun_lock); 5452#endif /* NEEDTOPORT */ 5453 /* 5454 * This could be a start or a stop when we're online, 5455 * or a stop/offline or start/online. A start or stop when 5456 * we're offline is covered in the case above. 5457 */ 5458 /* 5459 * In the non-immediate case, we send the request to 5460 * the backend and return status to the user when 5461 * it is done. 5462 * 5463 * In the immediate case, we allocate a new ctl_io 5464 * to hold a copy of the request, and send that to 5465 * the backend. We then set good status on the 5466 * user's request and return it immediately. 5467 */ 5468 if (cdb->byte2 & SSS_IMMED) { 5469 union ctl_io *new_io; 5470 5471 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5472 if (new_io == NULL) { 5473 ctl_set_busy(ctsio); 5474 ctl_done((union ctl_io *)ctsio); 5475 } else { 5476 ctl_copy_io((union ctl_io *)ctsio, 5477 new_io); 5478 retval = lun->backend->config_write(new_io); 5479 ctl_set_success(ctsio); 5480 ctl_done((union ctl_io *)ctsio); 5481 } 5482 } else { 5483 retval = lun->backend->config_write( 5484 (union ctl_io *)ctsio); 5485 } 5486#ifdef NEEDTOPORT 5487 } 5488#endif 5489 return (retval); 5490} 5491 5492/* 5493 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5494 * we don't really do anything with the LBA and length fields if the user 5495 * passes them in. Instead we'll just flush out the cache for the entire 5496 * LUN. 5497 */ 5498int 5499ctl_sync_cache(struct ctl_scsiio *ctsio) 5500{ 5501 struct ctl_lun *lun; 5502 struct ctl_softc *ctl_softc; 5503 uint64_t starting_lba; 5504 uint32_t block_count; 5505 int retval; 5506 5507 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5508 5509 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5510 ctl_softc = control_softc; 5511 retval = 0; 5512 5513 switch (ctsio->cdb[0]) { 5514 case SYNCHRONIZE_CACHE: { 5515 struct scsi_sync_cache *cdb; 5516 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5517 5518 starting_lba = scsi_4btoul(cdb->begin_lba); 5519 block_count = scsi_2btoul(cdb->lb_count); 5520 break; 5521 } 5522 case SYNCHRONIZE_CACHE_16: { 5523 struct scsi_sync_cache_16 *cdb; 5524 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5525 5526 starting_lba = scsi_8btou64(cdb->begin_lba); 5527 block_count = scsi_4btoul(cdb->lb_count); 5528 break; 5529 } 5530 default: 5531 ctl_set_invalid_opcode(ctsio); 5532 ctl_done((union ctl_io *)ctsio); 5533 goto bailout; 5534 break; /* NOTREACHED */ 5535 } 5536 5537 /* 5538 * We check the LBA and length, but don't do anything with them. 5539 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5540 * get flushed. This check will just help satisfy anyone who wants 5541 * to see an error for an out of range LBA. 5542 */ 5543 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5544 ctl_set_lba_out_of_range(ctsio); 5545 ctl_done((union ctl_io *)ctsio); 5546 goto bailout; 5547 } 5548 5549 /* 5550 * If this LUN has no backend, we can't flush the cache anyway. 5551 */ 5552 if (lun->backend == NULL) { 5553 ctl_set_invalid_opcode(ctsio); 5554 ctl_done((union ctl_io *)ctsio); 5555 goto bailout; 5556 } 5557 5558 /* 5559 * Check to see whether we're configured to send the SYNCHRONIZE 5560 * CACHE command directly to the back end. 5561 */ 5562 mtx_lock(&lun->lun_lock); 5563 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5564 && (++(lun->sync_count) >= lun->sync_interval)) { 5565 lun->sync_count = 0; 5566 mtx_unlock(&lun->lun_lock); 5567 retval = lun->backend->config_write((union ctl_io *)ctsio); 5568 } else { 5569 mtx_unlock(&lun->lun_lock); 5570 ctl_set_success(ctsio); 5571 ctl_done((union ctl_io *)ctsio); 5572 } 5573 5574bailout: 5575 5576 return (retval); 5577} 5578 5579int 5580ctl_format(struct ctl_scsiio *ctsio) 5581{ 5582 struct scsi_format *cdb; 5583 struct ctl_lun *lun; 5584 struct ctl_softc *ctl_softc; 5585 int length, defect_list_len; 5586 5587 CTL_DEBUG_PRINT(("ctl_format\n")); 5588 5589 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5590 ctl_softc = control_softc; 5591 5592 cdb = (struct scsi_format *)ctsio->cdb; 5593 5594 length = 0; 5595 if (cdb->byte2 & SF_FMTDATA) { 5596 if (cdb->byte2 & SF_LONGLIST) 5597 length = sizeof(struct scsi_format_header_long); 5598 else 5599 length = sizeof(struct scsi_format_header_short); 5600 } 5601 5602 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5603 && (length > 0)) { 5604 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5605 ctsio->kern_data_len = length; 5606 ctsio->kern_total_len = length; 5607 ctsio->kern_data_resid = 0; 5608 ctsio->kern_rel_offset = 0; 5609 ctsio->kern_sg_entries = 0; 5610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5611 ctsio->be_move_done = ctl_config_move_done; 5612 ctl_datamove((union ctl_io *)ctsio); 5613 5614 return (CTL_RETVAL_COMPLETE); 5615 } 5616 5617 defect_list_len = 0; 5618 5619 if (cdb->byte2 & SF_FMTDATA) { 5620 if (cdb->byte2 & SF_LONGLIST) { 5621 struct scsi_format_header_long *header; 5622 5623 header = (struct scsi_format_header_long *) 5624 ctsio->kern_data_ptr; 5625 5626 defect_list_len = scsi_4btoul(header->defect_list_len); 5627 if (defect_list_len != 0) { 5628 ctl_set_invalid_field(ctsio, 5629 /*sks_valid*/ 1, 5630 /*command*/ 0, 5631 /*field*/ 2, 5632 /*bit_valid*/ 0, 5633 /*bit*/ 0); 5634 goto bailout; 5635 } 5636 } else { 5637 struct scsi_format_header_short *header; 5638 5639 header = (struct scsi_format_header_short *) 5640 ctsio->kern_data_ptr; 5641 5642 defect_list_len = scsi_2btoul(header->defect_list_len); 5643 if (defect_list_len != 0) { 5644 ctl_set_invalid_field(ctsio, 5645 /*sks_valid*/ 1, 5646 /*command*/ 0, 5647 /*field*/ 2, 5648 /*bit_valid*/ 0, 5649 /*bit*/ 0); 5650 goto bailout; 5651 } 5652 } 5653 } 5654 5655 /* 5656 * The format command will clear out the "Medium format corrupted" 5657 * status if set by the configuration code. That status is really 5658 * just a way to notify the host that we have lost the media, and 5659 * get them to issue a command that will basically make them think 5660 * they're blowing away the media. 5661 */ 5662 mtx_lock(&lun->lun_lock); 5663 lun->flags &= ~CTL_LUN_INOPERABLE; 5664 mtx_unlock(&lun->lun_lock); 5665 5666 ctsio->scsi_status = SCSI_STATUS_OK; 5667 ctsio->io_hdr.status = CTL_SUCCESS; 5668bailout: 5669 5670 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5671 free(ctsio->kern_data_ptr, M_CTL); 5672 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5673 } 5674 5675 ctl_done((union ctl_io *)ctsio); 5676 return (CTL_RETVAL_COMPLETE); 5677} 5678 5679int 5680ctl_read_buffer(struct ctl_scsiio *ctsio) 5681{ 5682 struct scsi_read_buffer *cdb; 5683 struct ctl_lun *lun; 5684 int buffer_offset, len; 5685 static uint8_t descr[4]; 5686 static uint8_t echo_descr[4] = { 0 }; 5687 5688 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5689 5690 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5691 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5692 5693 if (lun->flags & CTL_LUN_PR_RESERVED) { 5694 uint32_t residx; 5695 5696 /* 5697 * XXX KDM need a lock here. 5698 */ 5699 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5700 if ((lun->res_type == SPR_TYPE_EX_AC 5701 && residx != lun->pr_res_idx) 5702 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5703 || lun->res_type == SPR_TYPE_EX_AC_AR) 5704 && lun->pr_keys[residx] == 0)) { 5705 ctl_set_reservation_conflict(ctsio); 5706 ctl_done((union ctl_io *)ctsio); 5707 return (CTL_RETVAL_COMPLETE); 5708 } 5709 } 5710 5711 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5712 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5713 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5714 ctl_set_invalid_field(ctsio, 5715 /*sks_valid*/ 1, 5716 /*command*/ 1, 5717 /*field*/ 1, 5718 /*bit_valid*/ 1, 5719 /*bit*/ 4); 5720 ctl_done((union ctl_io *)ctsio); 5721 return (CTL_RETVAL_COMPLETE); 5722 } 5723 5724 len = scsi_3btoul(cdb->length); 5725 buffer_offset = scsi_3btoul(cdb->offset); 5726 5727 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5728 ctl_set_invalid_field(ctsio, 5729 /*sks_valid*/ 1, 5730 /*command*/ 1, 5731 /*field*/ 6, 5732 /*bit_valid*/ 0, 5733 /*bit*/ 0); 5734 ctl_done((union ctl_io *)ctsio); 5735 return (CTL_RETVAL_COMPLETE); 5736 } 5737 5738 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5739 descr[0] = 0; 5740 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5741 ctsio->kern_data_ptr = descr; 5742 len = min(len, sizeof(descr)); 5743 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5744 ctsio->kern_data_ptr = echo_descr; 5745 len = min(len, sizeof(echo_descr)); 5746 } else 5747 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5748 ctsio->kern_data_len = len; 5749 ctsio->kern_total_len = len; 5750 ctsio->kern_data_resid = 0; 5751 ctsio->kern_rel_offset = 0; 5752 ctsio->kern_sg_entries = 0; 5753 ctsio->be_move_done = ctl_config_move_done; 5754 ctl_datamove((union ctl_io *)ctsio); 5755 5756 return (CTL_RETVAL_COMPLETE); 5757} 5758 5759int 5760ctl_write_buffer(struct ctl_scsiio *ctsio) 5761{ 5762 struct scsi_write_buffer *cdb; 5763 struct ctl_lun *lun; 5764 int buffer_offset, len; 5765 5766 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5767 5768 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5769 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5770 5771 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5772 ctl_set_invalid_field(ctsio, 5773 /*sks_valid*/ 1, 5774 /*command*/ 1, 5775 /*field*/ 1, 5776 /*bit_valid*/ 1, 5777 /*bit*/ 4); 5778 ctl_done((union ctl_io *)ctsio); 5779 return (CTL_RETVAL_COMPLETE); 5780 } 5781 5782 len = scsi_3btoul(cdb->length); 5783 buffer_offset = scsi_3btoul(cdb->offset); 5784 5785 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5786 ctl_set_invalid_field(ctsio, 5787 /*sks_valid*/ 1, 5788 /*command*/ 1, 5789 /*field*/ 6, 5790 /*bit_valid*/ 0, 5791 /*bit*/ 0); 5792 ctl_done((union ctl_io *)ctsio); 5793 return (CTL_RETVAL_COMPLETE); 5794 } 5795 5796 /* 5797 * If we've got a kernel request that hasn't been malloced yet, 5798 * malloc it and tell the caller the data buffer is here. 5799 */ 5800 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5801 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5802 ctsio->kern_data_len = len; 5803 ctsio->kern_total_len = len; 5804 ctsio->kern_data_resid = 0; 5805 ctsio->kern_rel_offset = 0; 5806 ctsio->kern_sg_entries = 0; 5807 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5808 ctsio->be_move_done = ctl_config_move_done; 5809 ctl_datamove((union ctl_io *)ctsio); 5810 5811 return (CTL_RETVAL_COMPLETE); 5812 } 5813 5814 ctl_done((union ctl_io *)ctsio); 5815 5816 return (CTL_RETVAL_COMPLETE); 5817} 5818 5819int 5820ctl_write_same(struct ctl_scsiio *ctsio) 5821{ 5822 struct ctl_lun *lun; 5823 struct ctl_lba_len_flags *lbalen; 5824 uint64_t lba; 5825 uint32_t num_blocks; 5826 int len, retval; 5827 uint8_t byte2; 5828 5829 retval = CTL_RETVAL_COMPLETE; 5830 5831 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5832 5833 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5834 5835 switch (ctsio->cdb[0]) { 5836 case WRITE_SAME_10: { 5837 struct scsi_write_same_10 *cdb; 5838 5839 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5840 5841 lba = scsi_4btoul(cdb->addr); 5842 num_blocks = scsi_2btoul(cdb->length); 5843 byte2 = cdb->byte2; 5844 break; 5845 } 5846 case WRITE_SAME_16: { 5847 struct scsi_write_same_16 *cdb; 5848 5849 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5850 5851 lba = scsi_8btou64(cdb->addr); 5852 num_blocks = scsi_4btoul(cdb->length); 5853 byte2 = cdb->byte2; 5854 break; 5855 } 5856 default: 5857 /* 5858 * We got a command we don't support. This shouldn't 5859 * happen, commands should be filtered out above us. 5860 */ 5861 ctl_set_invalid_opcode(ctsio); 5862 ctl_done((union ctl_io *)ctsio); 5863 5864 return (CTL_RETVAL_COMPLETE); 5865 break; /* NOTREACHED */ 5866 } 5867 5868 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5869 if ((byte2 & SWS_UNMAP) == 0 && 5870 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5871 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5872 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5873 ctl_done((union ctl_io *)ctsio); 5874 return (CTL_RETVAL_COMPLETE); 5875 } 5876 5877 /* 5878 * The first check is to make sure we're in bounds, the second 5879 * check is to catch wrap-around problems. If the lba + num blocks 5880 * is less than the lba, then we've wrapped around and the block 5881 * range is invalid anyway. 5882 */ 5883 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5884 || ((lba + num_blocks) < lba)) { 5885 ctl_set_lba_out_of_range(ctsio); 5886 ctl_done((union ctl_io *)ctsio); 5887 return (CTL_RETVAL_COMPLETE); 5888 } 5889 5890 /* Zero number of blocks means "to the last logical block" */ 5891 if (num_blocks == 0) { 5892 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5893 ctl_set_invalid_field(ctsio, 5894 /*sks_valid*/ 0, 5895 /*command*/ 1, 5896 /*field*/ 0, 5897 /*bit_valid*/ 0, 5898 /*bit*/ 0); 5899 ctl_done((union ctl_io *)ctsio); 5900 return (CTL_RETVAL_COMPLETE); 5901 } 5902 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5903 } 5904 5905 len = lun->be_lun->blocksize; 5906 5907 /* 5908 * If we've got a kernel request that hasn't been malloced yet, 5909 * malloc it and tell the caller the data buffer is here. 5910 */ 5911 if ((byte2 & SWS_NDOB) == 0 && 5912 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5913 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5914 ctsio->kern_data_len = len; 5915 ctsio->kern_total_len = len; 5916 ctsio->kern_data_resid = 0; 5917 ctsio->kern_rel_offset = 0; 5918 ctsio->kern_sg_entries = 0; 5919 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5920 ctsio->be_move_done = ctl_config_move_done; 5921 ctl_datamove((union ctl_io *)ctsio); 5922 5923 return (CTL_RETVAL_COMPLETE); 5924 } 5925 5926 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5927 lbalen->lba = lba; 5928 lbalen->len = num_blocks; 5929 lbalen->flags = byte2; 5930 retval = lun->backend->config_write((union ctl_io *)ctsio); 5931 5932 return (retval); 5933} 5934 5935int 5936ctl_unmap(struct ctl_scsiio *ctsio) 5937{ 5938 struct ctl_lun *lun; 5939 struct scsi_unmap *cdb; 5940 struct ctl_ptr_len_flags *ptrlen; 5941 struct scsi_unmap_header *hdr; 5942 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5943 uint64_t lba; 5944 uint32_t num_blocks; 5945 int len, retval; 5946 uint8_t byte2; 5947 5948 retval = CTL_RETVAL_COMPLETE; 5949 5950 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5951 5952 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5953 cdb = (struct scsi_unmap *)ctsio->cdb; 5954 5955 len = scsi_2btoul(cdb->length); 5956 byte2 = cdb->byte2; 5957 5958 /* 5959 * If we've got a kernel request that hasn't been malloced yet, 5960 * malloc it and tell the caller the data buffer is here. 5961 */ 5962 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5963 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5964 ctsio->kern_data_len = len; 5965 ctsio->kern_total_len = len; 5966 ctsio->kern_data_resid = 0; 5967 ctsio->kern_rel_offset = 0; 5968 ctsio->kern_sg_entries = 0; 5969 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5970 ctsio->be_move_done = ctl_config_move_done; 5971 ctl_datamove((union ctl_io *)ctsio); 5972 5973 return (CTL_RETVAL_COMPLETE); 5974 } 5975 5976 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5977 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5978 if (len < sizeof (*hdr) || 5979 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5980 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5981 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5982 ctl_set_invalid_field(ctsio, 5983 /*sks_valid*/ 0, 5984 /*command*/ 0, 5985 /*field*/ 0, 5986 /*bit_valid*/ 0, 5987 /*bit*/ 0); 5988 ctl_done((union ctl_io *)ctsio); 5989 return (CTL_RETVAL_COMPLETE); 5990 } 5991 len = scsi_2btoul(hdr->desc_length); 5992 buf = (struct scsi_unmap_desc *)(hdr + 1); 5993 end = buf + len / sizeof(*buf); 5994 5995 endnz = buf; 5996 for (range = buf; range < end; range++) { 5997 lba = scsi_8btou64(range->lba); 5998 num_blocks = scsi_4btoul(range->length); 5999 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6000 || ((lba + num_blocks) < lba)) { 6001 ctl_set_lba_out_of_range(ctsio); 6002 ctl_done((union ctl_io *)ctsio); 6003 return (CTL_RETVAL_COMPLETE); 6004 } 6005 if (num_blocks != 0) 6006 endnz = range + 1; 6007 } 6008 6009 /* 6010 * Block backend can not handle zero last range. 6011 * Filter it out and return if there is nothing left. 6012 */ 6013 len = (uint8_t *)endnz - (uint8_t *)buf; 6014 if (len == 0) { 6015 ctl_set_success(ctsio); 6016 ctl_done((union ctl_io *)ctsio); 6017 return (CTL_RETVAL_COMPLETE); 6018 } 6019 6020 mtx_lock(&lun->lun_lock); 6021 ptrlen = (struct ctl_ptr_len_flags *) 6022 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6023 ptrlen->ptr = (void *)buf; 6024 ptrlen->len = len; 6025 ptrlen->flags = byte2; 6026 ctl_check_blocked(lun); 6027 mtx_unlock(&lun->lun_lock); 6028 6029 retval = lun->backend->config_write((union ctl_io *)ctsio); 6030 return (retval); 6031} 6032 6033/* 6034 * Note that this function currently doesn't actually do anything inside 6035 * CTL to enforce things if the DQue bit is turned on. 6036 * 6037 * Also note that this function can't be used in the default case, because 6038 * the DQue bit isn't set in the changeable mask for the control mode page 6039 * anyway. This is just here as an example for how to implement a page 6040 * handler, and a placeholder in case we want to allow the user to turn 6041 * tagged queueing on and off. 6042 * 6043 * The D_SENSE bit handling is functional, however, and will turn 6044 * descriptor sense on and off for a given LUN. 6045 */ 6046int 6047ctl_control_page_handler(struct ctl_scsiio *ctsio, 6048 struct ctl_page_index *page_index, uint8_t *page_ptr) 6049{ 6050 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6051 struct ctl_lun *lun; 6052 struct ctl_softc *softc; 6053 int set_ua; 6054 uint32_t initidx; 6055 6056 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6057 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6058 set_ua = 0; 6059 6060 user_cp = (struct scsi_control_page *)page_ptr; 6061 current_cp = (struct scsi_control_page *) 6062 (page_index->page_data + (page_index->page_len * 6063 CTL_PAGE_CURRENT)); 6064 saved_cp = (struct scsi_control_page *) 6065 (page_index->page_data + (page_index->page_len * 6066 CTL_PAGE_SAVED)); 6067 6068 softc = control_softc; 6069 6070 mtx_lock(&lun->lun_lock); 6071 if (((current_cp->rlec & SCP_DSENSE) == 0) 6072 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6073 /* 6074 * Descriptor sense is currently turned off and the user 6075 * wants to turn it on. 6076 */ 6077 current_cp->rlec |= SCP_DSENSE; 6078 saved_cp->rlec |= SCP_DSENSE; 6079 lun->flags |= CTL_LUN_SENSE_DESC; 6080 set_ua = 1; 6081 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6082 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6083 /* 6084 * Descriptor sense is currently turned on, and the user 6085 * wants to turn it off. 6086 */ 6087 current_cp->rlec &= ~SCP_DSENSE; 6088 saved_cp->rlec &= ~SCP_DSENSE; 6089 lun->flags &= ~CTL_LUN_SENSE_DESC; 6090 set_ua = 1; 6091 } 6092 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6093 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6094 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6095 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6096 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6097 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6098 set_ua = 1; 6099 } 6100 if ((current_cp->eca_and_aen & SCP_SWP) != 6101 (user_cp->eca_and_aen & SCP_SWP)) { 6102 current_cp->eca_and_aen &= ~SCP_SWP; 6103 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6104 saved_cp->eca_and_aen &= ~SCP_SWP; 6105 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6106 set_ua = 1; 6107 } 6108 if (set_ua != 0) { 6109 int i; 6110 /* 6111 * Let other initiators know that the mode 6112 * parameters for this LUN have changed. 6113 */ 6114 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6115 if (i == initidx) 6116 continue; 6117 6118 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6119 } 6120 } 6121 mtx_unlock(&lun->lun_lock); 6122 6123 return (0); 6124} 6125 6126int 6127ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6128 struct ctl_page_index *page_index, uint8_t *page_ptr) 6129{ 6130 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6131 struct ctl_lun *lun; 6132 int set_ua; 6133 uint32_t initidx; 6134 6135 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6136 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6137 set_ua = 0; 6138 6139 user_cp = (struct scsi_caching_page *)page_ptr; 6140 current_cp = (struct scsi_caching_page *) 6141 (page_index->page_data + (page_index->page_len * 6142 CTL_PAGE_CURRENT)); 6143 saved_cp = (struct scsi_caching_page *) 6144 (page_index->page_data + (page_index->page_len * 6145 CTL_PAGE_SAVED)); 6146 6147 mtx_lock(&lun->lun_lock); 6148 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6149 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6150 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6151 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6152 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6153 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6154 set_ua = 1; 6155 } 6156 if (set_ua != 0) { 6157 int i; 6158 /* 6159 * Let other initiators know that the mode 6160 * parameters for this LUN have changed. 6161 */ 6162 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6163 if (i == initidx) 6164 continue; 6165 6166 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6167 } 6168 } 6169 mtx_unlock(&lun->lun_lock); 6170 6171 return (0); 6172} 6173 6174int 6175ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6176 struct ctl_page_index *page_index, 6177 uint8_t *page_ptr) 6178{ 6179 uint8_t *c; 6180 int i; 6181 6182 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6183 ctl_time_io_secs = 6184 (c[0] << 8) | 6185 (c[1] << 0) | 6186 0; 6187 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6188 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6189 printf("page data:"); 6190 for (i=0; i<8; i++) 6191 printf(" %.2x",page_ptr[i]); 6192 printf("\n"); 6193 return (0); 6194} 6195 6196int 6197ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6198 struct ctl_page_index *page_index, 6199 int pc) 6200{ 6201 struct copan_debugconf_subpage *page; 6202 6203 page = (struct copan_debugconf_subpage *)page_index->page_data + 6204 (page_index->page_len * pc); 6205 6206 switch (pc) { 6207 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6208 case SMS_PAGE_CTRL_DEFAULT >> 6: 6209 case SMS_PAGE_CTRL_SAVED >> 6: 6210 /* 6211 * We don't update the changable or default bits for this page. 6212 */ 6213 break; 6214 case SMS_PAGE_CTRL_CURRENT >> 6: 6215 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6216 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6217 break; 6218 default: 6219#ifdef NEEDTOPORT 6220 EPRINT(0, "Invalid PC %d!!", pc); 6221#endif /* NEEDTOPORT */ 6222 break; 6223 } 6224 return (0); 6225} 6226 6227 6228static int 6229ctl_do_mode_select(union ctl_io *io) 6230{ 6231 struct scsi_mode_page_header *page_header; 6232 struct ctl_page_index *page_index; 6233 struct ctl_scsiio *ctsio; 6234 int control_dev, page_len; 6235 int page_len_offset, page_len_size; 6236 union ctl_modepage_info *modepage_info; 6237 struct ctl_lun *lun; 6238 int *len_left, *len_used; 6239 int retval, i; 6240 6241 ctsio = &io->scsiio; 6242 page_index = NULL; 6243 page_len = 0; 6244 retval = CTL_RETVAL_COMPLETE; 6245 6246 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6247 6248 if (lun->be_lun->lun_type != T_DIRECT) 6249 control_dev = 1; 6250 else 6251 control_dev = 0; 6252 6253 modepage_info = (union ctl_modepage_info *) 6254 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6255 len_left = &modepage_info->header.len_left; 6256 len_used = &modepage_info->header.len_used; 6257 6258do_next_page: 6259 6260 page_header = (struct scsi_mode_page_header *) 6261 (ctsio->kern_data_ptr + *len_used); 6262 6263 if (*len_left == 0) { 6264 free(ctsio->kern_data_ptr, M_CTL); 6265 ctl_set_success(ctsio); 6266 ctl_done((union ctl_io *)ctsio); 6267 return (CTL_RETVAL_COMPLETE); 6268 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6269 6270 free(ctsio->kern_data_ptr, M_CTL); 6271 ctl_set_param_len_error(ctsio); 6272 ctl_done((union ctl_io *)ctsio); 6273 return (CTL_RETVAL_COMPLETE); 6274 6275 } else if ((page_header->page_code & SMPH_SPF) 6276 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6277 6278 free(ctsio->kern_data_ptr, M_CTL); 6279 ctl_set_param_len_error(ctsio); 6280 ctl_done((union ctl_io *)ctsio); 6281 return (CTL_RETVAL_COMPLETE); 6282 } 6283 6284 6285 /* 6286 * XXX KDM should we do something with the block descriptor? 6287 */ 6288 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6289 6290 if ((control_dev != 0) 6291 && (lun->mode_pages.index[i].page_flags & 6292 CTL_PAGE_FLAG_DISK_ONLY)) 6293 continue; 6294 6295 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6296 (page_header->page_code & SMPH_PC_MASK)) 6297 continue; 6298 6299 /* 6300 * If neither page has a subpage code, then we've got a 6301 * match. 6302 */ 6303 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6304 && ((page_header->page_code & SMPH_SPF) == 0)) { 6305 page_index = &lun->mode_pages.index[i]; 6306 page_len = page_header->page_length; 6307 break; 6308 } 6309 6310 /* 6311 * If both pages have subpages, then the subpage numbers 6312 * have to match. 6313 */ 6314 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6315 && (page_header->page_code & SMPH_SPF)) { 6316 struct scsi_mode_page_header_sp *sph; 6317 6318 sph = (struct scsi_mode_page_header_sp *)page_header; 6319 6320 if (lun->mode_pages.index[i].subpage == 6321 sph->subpage) { 6322 page_index = &lun->mode_pages.index[i]; 6323 page_len = scsi_2btoul(sph->page_length); 6324 break; 6325 } 6326 } 6327 } 6328 6329 /* 6330 * If we couldn't find the page, or if we don't have a mode select 6331 * handler for it, send back an error to the user. 6332 */ 6333 if ((page_index == NULL) 6334 || (page_index->select_handler == NULL)) { 6335 ctl_set_invalid_field(ctsio, 6336 /*sks_valid*/ 1, 6337 /*command*/ 0, 6338 /*field*/ *len_used, 6339 /*bit_valid*/ 0, 6340 /*bit*/ 0); 6341 free(ctsio->kern_data_ptr, M_CTL); 6342 ctl_done((union ctl_io *)ctsio); 6343 return (CTL_RETVAL_COMPLETE); 6344 } 6345 6346 if (page_index->page_code & SMPH_SPF) { 6347 page_len_offset = 2; 6348 page_len_size = 2; 6349 } else { 6350 page_len_size = 1; 6351 page_len_offset = 1; 6352 } 6353 6354 /* 6355 * If the length the initiator gives us isn't the one we specify in 6356 * the mode page header, or if they didn't specify enough data in 6357 * the CDB to avoid truncating this page, kick out the request. 6358 */ 6359 if ((page_len != (page_index->page_len - page_len_offset - 6360 page_len_size)) 6361 || (*len_left < page_index->page_len)) { 6362 6363 6364 ctl_set_invalid_field(ctsio, 6365 /*sks_valid*/ 1, 6366 /*command*/ 0, 6367 /*field*/ *len_used + page_len_offset, 6368 /*bit_valid*/ 0, 6369 /*bit*/ 0); 6370 free(ctsio->kern_data_ptr, M_CTL); 6371 ctl_done((union ctl_io *)ctsio); 6372 return (CTL_RETVAL_COMPLETE); 6373 } 6374 6375 /* 6376 * Run through the mode page, checking to make sure that the bits 6377 * the user changed are actually legal for him to change. 6378 */ 6379 for (i = 0; i < page_index->page_len; i++) { 6380 uint8_t *user_byte, *change_mask, *current_byte; 6381 int bad_bit; 6382 int j; 6383 6384 user_byte = (uint8_t *)page_header + i; 6385 change_mask = page_index->page_data + 6386 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6387 current_byte = page_index->page_data + 6388 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6389 6390 /* 6391 * Check to see whether the user set any bits in this byte 6392 * that he is not allowed to set. 6393 */ 6394 if ((*user_byte & ~(*change_mask)) == 6395 (*current_byte & ~(*change_mask))) 6396 continue; 6397 6398 /* 6399 * Go through bit by bit to determine which one is illegal. 6400 */ 6401 bad_bit = 0; 6402 for (j = 7; j >= 0; j--) { 6403 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6404 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6405 bad_bit = i; 6406 break; 6407 } 6408 } 6409 ctl_set_invalid_field(ctsio, 6410 /*sks_valid*/ 1, 6411 /*command*/ 0, 6412 /*field*/ *len_used + i, 6413 /*bit_valid*/ 1, 6414 /*bit*/ bad_bit); 6415 free(ctsio->kern_data_ptr, M_CTL); 6416 ctl_done((union ctl_io *)ctsio); 6417 return (CTL_RETVAL_COMPLETE); 6418 } 6419 6420 /* 6421 * Decrement these before we call the page handler, since we may 6422 * end up getting called back one way or another before the handler 6423 * returns to this context. 6424 */ 6425 *len_left -= page_index->page_len; 6426 *len_used += page_index->page_len; 6427 6428 retval = page_index->select_handler(ctsio, page_index, 6429 (uint8_t *)page_header); 6430 6431 /* 6432 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6433 * wait until this queued command completes to finish processing 6434 * the mode page. If it returns anything other than 6435 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6436 * already set the sense information, freed the data pointer, and 6437 * completed the io for us. 6438 */ 6439 if (retval != CTL_RETVAL_COMPLETE) 6440 goto bailout_no_done; 6441 6442 /* 6443 * If the initiator sent us more than one page, parse the next one. 6444 */ 6445 if (*len_left > 0) 6446 goto do_next_page; 6447 6448 ctl_set_success(ctsio); 6449 free(ctsio->kern_data_ptr, M_CTL); 6450 ctl_done((union ctl_io *)ctsio); 6451 6452bailout_no_done: 6453 6454 return (CTL_RETVAL_COMPLETE); 6455 6456} 6457 6458int 6459ctl_mode_select(struct ctl_scsiio *ctsio) 6460{ 6461 int param_len, pf, sp; 6462 int header_size, bd_len; 6463 int len_left, len_used; 6464 struct ctl_page_index *page_index; 6465 struct ctl_lun *lun; 6466 int control_dev, page_len; 6467 union ctl_modepage_info *modepage_info; 6468 int retval; 6469 6470 pf = 0; 6471 sp = 0; 6472 page_len = 0; 6473 len_used = 0; 6474 len_left = 0; 6475 retval = 0; 6476 bd_len = 0; 6477 page_index = NULL; 6478 6479 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6480 6481 if (lun->be_lun->lun_type != T_DIRECT) 6482 control_dev = 1; 6483 else 6484 control_dev = 0; 6485 6486 switch (ctsio->cdb[0]) { 6487 case MODE_SELECT_6: { 6488 struct scsi_mode_select_6 *cdb; 6489 6490 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6491 6492 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6493 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6494 6495 param_len = cdb->length; 6496 header_size = sizeof(struct scsi_mode_header_6); 6497 break; 6498 } 6499 case MODE_SELECT_10: { 6500 struct scsi_mode_select_10 *cdb; 6501 6502 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6503 6504 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6505 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6506 6507 param_len = scsi_2btoul(cdb->length); 6508 header_size = sizeof(struct scsi_mode_header_10); 6509 break; 6510 } 6511 default: 6512 ctl_set_invalid_opcode(ctsio); 6513 ctl_done((union ctl_io *)ctsio); 6514 return (CTL_RETVAL_COMPLETE); 6515 break; /* NOTREACHED */ 6516 } 6517 6518 /* 6519 * From SPC-3: 6520 * "A parameter list length of zero indicates that the Data-Out Buffer 6521 * shall be empty. This condition shall not be considered as an error." 6522 */ 6523 if (param_len == 0) { 6524 ctl_set_success(ctsio); 6525 ctl_done((union ctl_io *)ctsio); 6526 return (CTL_RETVAL_COMPLETE); 6527 } 6528 6529 /* 6530 * Since we'll hit this the first time through, prior to 6531 * allocation, we don't need to free a data buffer here. 6532 */ 6533 if (param_len < header_size) { 6534 ctl_set_param_len_error(ctsio); 6535 ctl_done((union ctl_io *)ctsio); 6536 return (CTL_RETVAL_COMPLETE); 6537 } 6538 6539 /* 6540 * Allocate the data buffer and grab the user's data. In theory, 6541 * we shouldn't have to sanity check the parameter list length here 6542 * because the maximum size is 64K. We should be able to malloc 6543 * that much without too many problems. 6544 */ 6545 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6546 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6547 ctsio->kern_data_len = param_len; 6548 ctsio->kern_total_len = param_len; 6549 ctsio->kern_data_resid = 0; 6550 ctsio->kern_rel_offset = 0; 6551 ctsio->kern_sg_entries = 0; 6552 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6553 ctsio->be_move_done = ctl_config_move_done; 6554 ctl_datamove((union ctl_io *)ctsio); 6555 6556 return (CTL_RETVAL_COMPLETE); 6557 } 6558 6559 switch (ctsio->cdb[0]) { 6560 case MODE_SELECT_6: { 6561 struct scsi_mode_header_6 *mh6; 6562 6563 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6564 bd_len = mh6->blk_desc_len; 6565 break; 6566 } 6567 case MODE_SELECT_10: { 6568 struct scsi_mode_header_10 *mh10; 6569 6570 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6571 bd_len = scsi_2btoul(mh10->blk_desc_len); 6572 break; 6573 } 6574 default: 6575 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6576 break; 6577 } 6578 6579 if (param_len < (header_size + bd_len)) { 6580 free(ctsio->kern_data_ptr, M_CTL); 6581 ctl_set_param_len_error(ctsio); 6582 ctl_done((union ctl_io *)ctsio); 6583 return (CTL_RETVAL_COMPLETE); 6584 } 6585 6586 /* 6587 * Set the IO_CONT flag, so that if this I/O gets passed to 6588 * ctl_config_write_done(), it'll get passed back to 6589 * ctl_do_mode_select() for further processing, or completion if 6590 * we're all done. 6591 */ 6592 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6593 ctsio->io_cont = ctl_do_mode_select; 6594 6595 modepage_info = (union ctl_modepage_info *) 6596 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6597 6598 memset(modepage_info, 0, sizeof(*modepage_info)); 6599 6600 len_left = param_len - header_size - bd_len; 6601 len_used = header_size + bd_len; 6602 6603 modepage_info->header.len_left = len_left; 6604 modepage_info->header.len_used = len_used; 6605 6606 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6607} 6608 6609int 6610ctl_mode_sense(struct ctl_scsiio *ctsio) 6611{ 6612 struct ctl_lun *lun; 6613 int pc, page_code, dbd, llba, subpage; 6614 int alloc_len, page_len, header_len, total_len; 6615 struct scsi_mode_block_descr *block_desc; 6616 struct ctl_page_index *page_index; 6617 int control_dev; 6618 6619 dbd = 0; 6620 llba = 0; 6621 block_desc = NULL; 6622 page_index = NULL; 6623 6624 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6625 6626 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6627 6628 if (lun->be_lun->lun_type != T_DIRECT) 6629 control_dev = 1; 6630 else 6631 control_dev = 0; 6632 6633 if (lun->flags & CTL_LUN_PR_RESERVED) { 6634 uint32_t residx; 6635 6636 /* 6637 * XXX KDM need a lock here. 6638 */ 6639 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6640 if ((lun->res_type == SPR_TYPE_EX_AC 6641 && residx != lun->pr_res_idx) 6642 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6643 || lun->res_type == SPR_TYPE_EX_AC_AR) 6644 && lun->pr_keys[residx] == 0)) { 6645 ctl_set_reservation_conflict(ctsio); 6646 ctl_done((union ctl_io *)ctsio); 6647 return (CTL_RETVAL_COMPLETE); 6648 } 6649 } 6650 6651 switch (ctsio->cdb[0]) { 6652 case MODE_SENSE_6: { 6653 struct scsi_mode_sense_6 *cdb; 6654 6655 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6656 6657 header_len = sizeof(struct scsi_mode_hdr_6); 6658 if (cdb->byte2 & SMS_DBD) 6659 dbd = 1; 6660 else 6661 header_len += sizeof(struct scsi_mode_block_descr); 6662 6663 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6664 page_code = cdb->page & SMS_PAGE_CODE; 6665 subpage = cdb->subpage; 6666 alloc_len = cdb->length; 6667 break; 6668 } 6669 case MODE_SENSE_10: { 6670 struct scsi_mode_sense_10 *cdb; 6671 6672 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6673 6674 header_len = sizeof(struct scsi_mode_hdr_10); 6675 6676 if (cdb->byte2 & SMS_DBD) 6677 dbd = 1; 6678 else 6679 header_len += sizeof(struct scsi_mode_block_descr); 6680 if (cdb->byte2 & SMS10_LLBAA) 6681 llba = 1; 6682 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6683 page_code = cdb->page & SMS_PAGE_CODE; 6684 subpage = cdb->subpage; 6685 alloc_len = scsi_2btoul(cdb->length); 6686 break; 6687 } 6688 default: 6689 ctl_set_invalid_opcode(ctsio); 6690 ctl_done((union ctl_io *)ctsio); 6691 return (CTL_RETVAL_COMPLETE); 6692 break; /* NOTREACHED */ 6693 } 6694 6695 /* 6696 * We have to make a first pass through to calculate the size of 6697 * the pages that match the user's query. Then we allocate enough 6698 * memory to hold it, and actually copy the data into the buffer. 6699 */ 6700 switch (page_code) { 6701 case SMS_ALL_PAGES_PAGE: { 6702 int i; 6703 6704 page_len = 0; 6705 6706 /* 6707 * At the moment, values other than 0 and 0xff here are 6708 * reserved according to SPC-3. 6709 */ 6710 if ((subpage != SMS_SUBPAGE_PAGE_0) 6711 && (subpage != SMS_SUBPAGE_ALL)) { 6712 ctl_set_invalid_field(ctsio, 6713 /*sks_valid*/ 1, 6714 /*command*/ 1, 6715 /*field*/ 3, 6716 /*bit_valid*/ 0, 6717 /*bit*/ 0); 6718 ctl_done((union ctl_io *)ctsio); 6719 return (CTL_RETVAL_COMPLETE); 6720 } 6721 6722 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6723 if ((control_dev != 0) 6724 && (lun->mode_pages.index[i].page_flags & 6725 CTL_PAGE_FLAG_DISK_ONLY)) 6726 continue; 6727 6728 /* 6729 * We don't use this subpage if the user didn't 6730 * request all subpages. 6731 */ 6732 if ((lun->mode_pages.index[i].subpage != 0) 6733 && (subpage == SMS_SUBPAGE_PAGE_0)) 6734 continue; 6735 6736#if 0 6737 printf("found page %#x len %d\n", 6738 lun->mode_pages.index[i].page_code & 6739 SMPH_PC_MASK, 6740 lun->mode_pages.index[i].page_len); 6741#endif 6742 page_len += lun->mode_pages.index[i].page_len; 6743 } 6744 break; 6745 } 6746 default: { 6747 int i; 6748 6749 page_len = 0; 6750 6751 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6752 /* Look for the right page code */ 6753 if ((lun->mode_pages.index[i].page_code & 6754 SMPH_PC_MASK) != page_code) 6755 continue; 6756 6757 /* Look for the right subpage or the subpage wildcard*/ 6758 if ((lun->mode_pages.index[i].subpage != subpage) 6759 && (subpage != SMS_SUBPAGE_ALL)) 6760 continue; 6761 6762 /* Make sure the page is supported for this dev type */ 6763 if ((control_dev != 0) 6764 && (lun->mode_pages.index[i].page_flags & 6765 CTL_PAGE_FLAG_DISK_ONLY)) 6766 continue; 6767 6768#if 0 6769 printf("found page %#x len %d\n", 6770 lun->mode_pages.index[i].page_code & 6771 SMPH_PC_MASK, 6772 lun->mode_pages.index[i].page_len); 6773#endif 6774 6775 page_len += lun->mode_pages.index[i].page_len; 6776 } 6777 6778 if (page_len == 0) { 6779 ctl_set_invalid_field(ctsio, 6780 /*sks_valid*/ 1, 6781 /*command*/ 1, 6782 /*field*/ 2, 6783 /*bit_valid*/ 1, 6784 /*bit*/ 5); 6785 ctl_done((union ctl_io *)ctsio); 6786 return (CTL_RETVAL_COMPLETE); 6787 } 6788 break; 6789 } 6790 } 6791 6792 total_len = header_len + page_len; 6793#if 0 6794 printf("header_len = %d, page_len = %d, total_len = %d\n", 6795 header_len, page_len, total_len); 6796#endif 6797 6798 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6799 ctsio->kern_sg_entries = 0; 6800 ctsio->kern_data_resid = 0; 6801 ctsio->kern_rel_offset = 0; 6802 if (total_len < alloc_len) { 6803 ctsio->residual = alloc_len - total_len; 6804 ctsio->kern_data_len = total_len; 6805 ctsio->kern_total_len = total_len; 6806 } else { 6807 ctsio->residual = 0; 6808 ctsio->kern_data_len = alloc_len; 6809 ctsio->kern_total_len = alloc_len; 6810 } 6811 6812 switch (ctsio->cdb[0]) { 6813 case MODE_SENSE_6: { 6814 struct scsi_mode_hdr_6 *header; 6815 6816 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6817 6818 header->datalen = ctl_min(total_len - 1, 254); 6819 if (control_dev == 0) { 6820 header->dev_specific = 0x10; /* DPOFUA */ 6821 if ((lun->flags & CTL_LUN_READONLY) || 6822 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6823 .eca_and_aen & SCP_SWP) != 0) 6824 header->dev_specific |= 0x80; /* WP */ 6825 } 6826 if (dbd) 6827 header->block_descr_len = 0; 6828 else 6829 header->block_descr_len = 6830 sizeof(struct scsi_mode_block_descr); 6831 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6832 break; 6833 } 6834 case MODE_SENSE_10: { 6835 struct scsi_mode_hdr_10 *header; 6836 int datalen; 6837 6838 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6839 6840 datalen = ctl_min(total_len - 2, 65533); 6841 scsi_ulto2b(datalen, header->datalen); 6842 if (control_dev == 0) { 6843 header->dev_specific = 0x10; /* DPOFUA */ 6844 if ((lun->flags & CTL_LUN_READONLY) || 6845 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6846 .eca_and_aen & SCP_SWP) != 0) 6847 header->dev_specific |= 0x80; /* WP */ 6848 } 6849 if (dbd) 6850 scsi_ulto2b(0, header->block_descr_len); 6851 else 6852 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6853 header->block_descr_len); 6854 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6855 break; 6856 } 6857 default: 6858 panic("invalid CDB type %#x", ctsio->cdb[0]); 6859 break; /* NOTREACHED */ 6860 } 6861 6862 /* 6863 * If we've got a disk, use its blocksize in the block 6864 * descriptor. Otherwise, just set it to 0. 6865 */ 6866 if (dbd == 0) { 6867 if (control_dev == 0) 6868 scsi_ulto3b(lun->be_lun->blocksize, 6869 block_desc->block_len); 6870 else 6871 scsi_ulto3b(0, block_desc->block_len); 6872 } 6873 6874 switch (page_code) { 6875 case SMS_ALL_PAGES_PAGE: { 6876 int i, data_used; 6877 6878 data_used = header_len; 6879 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6880 struct ctl_page_index *page_index; 6881 6882 page_index = &lun->mode_pages.index[i]; 6883 6884 if ((control_dev != 0) 6885 && (page_index->page_flags & 6886 CTL_PAGE_FLAG_DISK_ONLY)) 6887 continue; 6888 6889 /* 6890 * We don't use this subpage if the user didn't 6891 * request all subpages. We already checked (above) 6892 * to make sure the user only specified a subpage 6893 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6894 */ 6895 if ((page_index->subpage != 0) 6896 && (subpage == SMS_SUBPAGE_PAGE_0)) 6897 continue; 6898 6899 /* 6900 * Call the handler, if it exists, to update the 6901 * page to the latest values. 6902 */ 6903 if (page_index->sense_handler != NULL) 6904 page_index->sense_handler(ctsio, page_index,pc); 6905 6906 memcpy(ctsio->kern_data_ptr + data_used, 6907 page_index->page_data + 6908 (page_index->page_len * pc), 6909 page_index->page_len); 6910 data_used += page_index->page_len; 6911 } 6912 break; 6913 } 6914 default: { 6915 int i, data_used; 6916 6917 data_used = header_len; 6918 6919 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6920 struct ctl_page_index *page_index; 6921 6922 page_index = &lun->mode_pages.index[i]; 6923 6924 /* Look for the right page code */ 6925 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6926 continue; 6927 6928 /* Look for the right subpage or the subpage wildcard*/ 6929 if ((page_index->subpage != subpage) 6930 && (subpage != SMS_SUBPAGE_ALL)) 6931 continue; 6932 6933 /* Make sure the page is supported for this dev type */ 6934 if ((control_dev != 0) 6935 && (page_index->page_flags & 6936 CTL_PAGE_FLAG_DISK_ONLY)) 6937 continue; 6938 6939 /* 6940 * Call the handler, if it exists, to update the 6941 * page to the latest values. 6942 */ 6943 if (page_index->sense_handler != NULL) 6944 page_index->sense_handler(ctsio, page_index,pc); 6945 6946 memcpy(ctsio->kern_data_ptr + data_used, 6947 page_index->page_data + 6948 (page_index->page_len * pc), 6949 page_index->page_len); 6950 data_used += page_index->page_len; 6951 } 6952 break; 6953 } 6954 } 6955 6956 ctsio->scsi_status = SCSI_STATUS_OK; 6957 6958 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6959 ctsio->be_move_done = ctl_config_move_done; 6960 ctl_datamove((union ctl_io *)ctsio); 6961 6962 return (CTL_RETVAL_COMPLETE); 6963} 6964 6965int 6966ctl_log_sense(struct ctl_scsiio *ctsio) 6967{ 6968 struct ctl_lun *lun; 6969 int i, pc, page_code, subpage; 6970 int alloc_len, total_len; 6971 struct ctl_page_index *page_index; 6972 struct scsi_log_sense *cdb; 6973 struct scsi_log_header *header; 6974 6975 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6976 6977 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6978 cdb = (struct scsi_log_sense *)ctsio->cdb; 6979 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6980 page_code = cdb->page & SLS_PAGE_CODE; 6981 subpage = cdb->subpage; 6982 alloc_len = scsi_2btoul(cdb->length); 6983 6984 page_index = NULL; 6985 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6986 page_index = &lun->log_pages.index[i]; 6987 6988 /* Look for the right page code */ 6989 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6990 continue; 6991 6992 /* Look for the right subpage or the subpage wildcard*/ 6993 if (page_index->subpage != subpage) 6994 continue; 6995 6996 break; 6997 } 6998 if (i >= CTL_NUM_LOG_PAGES) { 6999 ctl_set_invalid_field(ctsio, 7000 /*sks_valid*/ 1, 7001 /*command*/ 1, 7002 /*field*/ 2, 7003 /*bit_valid*/ 0, 7004 /*bit*/ 0); 7005 ctl_done((union ctl_io *)ctsio); 7006 return (CTL_RETVAL_COMPLETE); 7007 } 7008 7009 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7010 7011 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7012 ctsio->kern_sg_entries = 0; 7013 ctsio->kern_data_resid = 0; 7014 ctsio->kern_rel_offset = 0; 7015 if (total_len < alloc_len) { 7016 ctsio->residual = alloc_len - total_len; 7017 ctsio->kern_data_len = total_len; 7018 ctsio->kern_total_len = total_len; 7019 } else { 7020 ctsio->residual = 0; 7021 ctsio->kern_data_len = alloc_len; 7022 ctsio->kern_total_len = alloc_len; 7023 } 7024 7025 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7026 header->page = page_index->page_code; 7027 if (page_index->subpage) { 7028 header->page |= SL_SPF; 7029 header->subpage = page_index->subpage; 7030 } 7031 scsi_ulto2b(page_index->page_len, header->datalen); 7032 7033 /* 7034 * Call the handler, if it exists, to update the 7035 * page to the latest values. 7036 */ 7037 if (page_index->sense_handler != NULL) 7038 page_index->sense_handler(ctsio, page_index, pc); 7039 7040 memcpy(header + 1, page_index->page_data, page_index->page_len); 7041 7042 ctsio->scsi_status = SCSI_STATUS_OK; 7043 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7044 ctsio->be_move_done = ctl_config_move_done; 7045 ctl_datamove((union ctl_io *)ctsio); 7046 7047 return (CTL_RETVAL_COMPLETE); 7048} 7049 7050int 7051ctl_read_capacity(struct ctl_scsiio *ctsio) 7052{ 7053 struct scsi_read_capacity *cdb; 7054 struct scsi_read_capacity_data *data; 7055 struct ctl_lun *lun; 7056 uint32_t lba; 7057 7058 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7059 7060 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7061 7062 lba = scsi_4btoul(cdb->addr); 7063 if (((cdb->pmi & SRC_PMI) == 0) 7064 && (lba != 0)) { 7065 ctl_set_invalid_field(/*ctsio*/ ctsio, 7066 /*sks_valid*/ 1, 7067 /*command*/ 1, 7068 /*field*/ 2, 7069 /*bit_valid*/ 0, 7070 /*bit*/ 0); 7071 ctl_done((union ctl_io *)ctsio); 7072 return (CTL_RETVAL_COMPLETE); 7073 } 7074 7075 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7076 7077 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7078 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7079 ctsio->residual = 0; 7080 ctsio->kern_data_len = sizeof(*data); 7081 ctsio->kern_total_len = sizeof(*data); 7082 ctsio->kern_data_resid = 0; 7083 ctsio->kern_rel_offset = 0; 7084 ctsio->kern_sg_entries = 0; 7085 7086 /* 7087 * If the maximum LBA is greater than 0xfffffffe, the user must 7088 * issue a SERVICE ACTION IN (16) command, with the read capacity 7089 * serivce action set. 7090 */ 7091 if (lun->be_lun->maxlba > 0xfffffffe) 7092 scsi_ulto4b(0xffffffff, data->addr); 7093 else 7094 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7095 7096 /* 7097 * XXX KDM this may not be 512 bytes... 7098 */ 7099 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7100 7101 ctsio->scsi_status = SCSI_STATUS_OK; 7102 7103 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7104 ctsio->be_move_done = ctl_config_move_done; 7105 ctl_datamove((union ctl_io *)ctsio); 7106 7107 return (CTL_RETVAL_COMPLETE); 7108} 7109 7110int 7111ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7112{ 7113 struct scsi_read_capacity_16 *cdb; 7114 struct scsi_read_capacity_data_long *data; 7115 struct ctl_lun *lun; 7116 uint64_t lba; 7117 uint32_t alloc_len; 7118 7119 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7120 7121 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7122 7123 alloc_len = scsi_4btoul(cdb->alloc_len); 7124 lba = scsi_8btou64(cdb->addr); 7125 7126 if ((cdb->reladr & SRC16_PMI) 7127 && (lba != 0)) { 7128 ctl_set_invalid_field(/*ctsio*/ ctsio, 7129 /*sks_valid*/ 1, 7130 /*command*/ 1, 7131 /*field*/ 2, 7132 /*bit_valid*/ 0, 7133 /*bit*/ 0); 7134 ctl_done((union ctl_io *)ctsio); 7135 return (CTL_RETVAL_COMPLETE); 7136 } 7137 7138 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7139 7140 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7141 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7142 7143 if (sizeof(*data) < alloc_len) { 7144 ctsio->residual = alloc_len - sizeof(*data); 7145 ctsio->kern_data_len = sizeof(*data); 7146 ctsio->kern_total_len = sizeof(*data); 7147 } else { 7148 ctsio->residual = 0; 7149 ctsio->kern_data_len = alloc_len; 7150 ctsio->kern_total_len = alloc_len; 7151 } 7152 ctsio->kern_data_resid = 0; 7153 ctsio->kern_rel_offset = 0; 7154 ctsio->kern_sg_entries = 0; 7155 7156 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7157 /* XXX KDM this may not be 512 bytes... */ 7158 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7159 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7160 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7161 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7162 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7163 7164 ctsio->scsi_status = SCSI_STATUS_OK; 7165 7166 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7167 ctsio->be_move_done = ctl_config_move_done; 7168 ctl_datamove((union ctl_io *)ctsio); 7169 7170 return (CTL_RETVAL_COMPLETE); 7171} 7172 7173int 7174ctl_read_defect(struct ctl_scsiio *ctsio) 7175{ 7176 struct scsi_read_defect_data_10 *ccb10; 7177 struct scsi_read_defect_data_12 *ccb12; 7178 struct scsi_read_defect_data_hdr_10 *data10; 7179 struct scsi_read_defect_data_hdr_12 *data12; 7180 struct ctl_lun *lun; 7181 uint32_t alloc_len, data_len; 7182 uint8_t format; 7183 7184 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7185 7186 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7187 if (lun->flags & CTL_LUN_PR_RESERVED) { 7188 uint32_t residx; 7189 7190 /* 7191 * XXX KDM need a lock here. 7192 */ 7193 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 7194 if ((lun->res_type == SPR_TYPE_EX_AC 7195 && residx != lun->pr_res_idx) 7196 || ((lun->res_type == SPR_TYPE_EX_AC_RO 7197 || lun->res_type == SPR_TYPE_EX_AC_AR) 7198 && lun->pr_keys[residx] == 0)) { 7199 ctl_set_reservation_conflict(ctsio); 7200 ctl_done((union ctl_io *)ctsio); 7201 return (CTL_RETVAL_COMPLETE); 7202 } 7203 } 7204 7205 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7206 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7207 format = ccb10->format; 7208 alloc_len = scsi_2btoul(ccb10->alloc_length); 7209 data_len = sizeof(*data10); 7210 } else { 7211 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7212 format = ccb12->format; 7213 alloc_len = scsi_4btoul(ccb12->alloc_length); 7214 data_len = sizeof(*data12); 7215 } 7216 if (alloc_len == 0) { 7217 ctl_set_success(ctsio); 7218 ctl_done((union ctl_io *)ctsio); 7219 return (CTL_RETVAL_COMPLETE); 7220 } 7221 7222 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7223 if (data_len < alloc_len) { 7224 ctsio->residual = alloc_len - data_len; 7225 ctsio->kern_data_len = data_len; 7226 ctsio->kern_total_len = data_len; 7227 } else { 7228 ctsio->residual = 0; 7229 ctsio->kern_data_len = alloc_len; 7230 ctsio->kern_total_len = alloc_len; 7231 } 7232 ctsio->kern_data_resid = 0; 7233 ctsio->kern_rel_offset = 0; 7234 ctsio->kern_sg_entries = 0; 7235 7236 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7237 data10 = (struct scsi_read_defect_data_hdr_10 *) 7238 ctsio->kern_data_ptr; 7239 data10->format = format; 7240 scsi_ulto2b(0, data10->length); 7241 } else { 7242 data12 = (struct scsi_read_defect_data_hdr_12 *) 7243 ctsio->kern_data_ptr; 7244 data12->format = format; 7245 scsi_ulto2b(0, data12->generation); 7246 scsi_ulto4b(0, data12->length); 7247 } 7248 7249 ctsio->scsi_status = SCSI_STATUS_OK; 7250 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7251 ctsio->be_move_done = ctl_config_move_done; 7252 ctl_datamove((union ctl_io *)ctsio); 7253 return (CTL_RETVAL_COMPLETE); 7254} 7255 7256int 7257ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7258{ 7259 struct scsi_maintenance_in *cdb; 7260 int retval; 7261 int alloc_len, ext, total_len = 0, g, p, pc, pg; 7262 int num_target_port_groups, num_target_ports, single; 7263 struct ctl_lun *lun; 7264 struct ctl_softc *softc; 7265 struct ctl_port *port; 7266 struct scsi_target_group_data *rtg_ptr; 7267 struct scsi_target_group_data_extended *rtg_ext_ptr; 7268 struct scsi_target_port_group_descriptor *tpg_desc; 7269 7270 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7271 7272 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7273 softc = control_softc; 7274 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7275 7276 retval = CTL_RETVAL_COMPLETE; 7277 7278 switch (cdb->byte2 & STG_PDF_MASK) { 7279 case STG_PDF_LENGTH: 7280 ext = 0; 7281 break; 7282 case STG_PDF_EXTENDED: 7283 ext = 1; 7284 break; 7285 default: 7286 ctl_set_invalid_field(/*ctsio*/ ctsio, 7287 /*sks_valid*/ 1, 7288 /*command*/ 1, 7289 /*field*/ 2, 7290 /*bit_valid*/ 1, 7291 /*bit*/ 5); 7292 ctl_done((union ctl_io *)ctsio); 7293 return(retval); 7294 } 7295 7296 single = ctl_is_single; 7297 if (single) 7298 num_target_port_groups = 1; 7299 else 7300 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7301 num_target_ports = 0; 7302 mtx_lock(&softc->ctl_lock); 7303 STAILQ_FOREACH(port, &softc->port_list, links) { 7304 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7305 continue; 7306 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7307 continue; 7308 num_target_ports++; 7309 } 7310 mtx_unlock(&softc->ctl_lock); 7311 7312 if (ext) 7313 total_len = sizeof(struct scsi_target_group_data_extended); 7314 else 7315 total_len = sizeof(struct scsi_target_group_data); 7316 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7317 num_target_port_groups + 7318 sizeof(struct scsi_target_port_descriptor) * 7319 num_target_ports * num_target_port_groups; 7320 7321 alloc_len = scsi_4btoul(cdb->length); 7322 7323 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7324 7325 ctsio->kern_sg_entries = 0; 7326 7327 if (total_len < alloc_len) { 7328 ctsio->residual = alloc_len - total_len; 7329 ctsio->kern_data_len = total_len; 7330 ctsio->kern_total_len = total_len; 7331 } else { 7332 ctsio->residual = 0; 7333 ctsio->kern_data_len = alloc_len; 7334 ctsio->kern_total_len = alloc_len; 7335 } 7336 ctsio->kern_data_resid = 0; 7337 ctsio->kern_rel_offset = 0; 7338 7339 if (ext) { 7340 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7341 ctsio->kern_data_ptr; 7342 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7343 rtg_ext_ptr->format_type = 0x10; 7344 rtg_ext_ptr->implicit_transition_time = 0; 7345 tpg_desc = &rtg_ext_ptr->groups[0]; 7346 } else { 7347 rtg_ptr = (struct scsi_target_group_data *) 7348 ctsio->kern_data_ptr; 7349 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7350 tpg_desc = &rtg_ptr->groups[0]; 7351 } 7352 7353 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS; 7354 mtx_lock(&softc->ctl_lock); 7355 for (g = 0; g < num_target_port_groups; g++) { 7356 if (g == pg) 7357 tpg_desc->pref_state = TPG_PRIMARY | 7358 TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7359 else 7360 tpg_desc->pref_state = 7361 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7362 tpg_desc->support = TPG_AO_SUP; 7363 if (!single) 7364 tpg_desc->support |= TPG_AN_SUP; 7365 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7366 tpg_desc->status = TPG_IMPLICIT; 7367 pc = 0; 7368 STAILQ_FOREACH(port, &softc->port_list, links) { 7369 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7370 continue; 7371 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7372 CTL_MAX_LUNS) 7373 continue; 7374 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7375 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7376 relative_target_port_identifier); 7377 pc++; 7378 } 7379 tpg_desc->target_port_count = pc; 7380 tpg_desc = (struct scsi_target_port_group_descriptor *) 7381 &tpg_desc->descriptors[pc]; 7382 } 7383 mtx_unlock(&softc->ctl_lock); 7384 7385 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7386 ctsio->be_move_done = ctl_config_move_done; 7387 7388 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7389 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7390 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7391 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7392 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7393 7394 ctl_datamove((union ctl_io *)ctsio); 7395 return(retval); 7396} 7397 7398int 7399ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7400{ 7401 struct ctl_lun *lun; 7402 struct scsi_report_supported_opcodes *cdb; 7403 const struct ctl_cmd_entry *entry, *sentry; 7404 struct scsi_report_supported_opcodes_all *all; 7405 struct scsi_report_supported_opcodes_descr *descr; 7406 struct scsi_report_supported_opcodes_one *one; 7407 int retval; 7408 int alloc_len, total_len; 7409 int opcode, service_action, i, j, num; 7410 7411 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7412 7413 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7414 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7415 7416 retval = CTL_RETVAL_COMPLETE; 7417 7418 opcode = cdb->requested_opcode; 7419 service_action = scsi_2btoul(cdb->requested_service_action); 7420 switch (cdb->options & RSO_OPTIONS_MASK) { 7421 case RSO_OPTIONS_ALL: 7422 num = 0; 7423 for (i = 0; i < 256; i++) { 7424 entry = &ctl_cmd_table[i]; 7425 if (entry->flags & CTL_CMD_FLAG_SA5) { 7426 for (j = 0; j < 32; j++) { 7427 sentry = &((const struct ctl_cmd_entry *) 7428 entry->execute)[j]; 7429 if (ctl_cmd_applicable( 7430 lun->be_lun->lun_type, sentry)) 7431 num++; 7432 } 7433 } else { 7434 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7435 entry)) 7436 num++; 7437 } 7438 } 7439 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7440 num * sizeof(struct scsi_report_supported_opcodes_descr); 7441 break; 7442 case RSO_OPTIONS_OC: 7443 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7444 ctl_set_invalid_field(/*ctsio*/ ctsio, 7445 /*sks_valid*/ 1, 7446 /*command*/ 1, 7447 /*field*/ 2, 7448 /*bit_valid*/ 1, 7449 /*bit*/ 2); 7450 ctl_done((union ctl_io *)ctsio); 7451 return (CTL_RETVAL_COMPLETE); 7452 } 7453 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7454 break; 7455 case RSO_OPTIONS_OC_SA: 7456 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7457 service_action >= 32) { 7458 ctl_set_invalid_field(/*ctsio*/ ctsio, 7459 /*sks_valid*/ 1, 7460 /*command*/ 1, 7461 /*field*/ 2, 7462 /*bit_valid*/ 1, 7463 /*bit*/ 2); 7464 ctl_done((union ctl_io *)ctsio); 7465 return (CTL_RETVAL_COMPLETE); 7466 } 7467 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7468 break; 7469 default: 7470 ctl_set_invalid_field(/*ctsio*/ ctsio, 7471 /*sks_valid*/ 1, 7472 /*command*/ 1, 7473 /*field*/ 2, 7474 /*bit_valid*/ 1, 7475 /*bit*/ 2); 7476 ctl_done((union ctl_io *)ctsio); 7477 return (CTL_RETVAL_COMPLETE); 7478 } 7479 7480 alloc_len = scsi_4btoul(cdb->length); 7481 7482 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7483 7484 ctsio->kern_sg_entries = 0; 7485 7486 if (total_len < alloc_len) { 7487 ctsio->residual = alloc_len - total_len; 7488 ctsio->kern_data_len = total_len; 7489 ctsio->kern_total_len = total_len; 7490 } else { 7491 ctsio->residual = 0; 7492 ctsio->kern_data_len = alloc_len; 7493 ctsio->kern_total_len = alloc_len; 7494 } 7495 ctsio->kern_data_resid = 0; 7496 ctsio->kern_rel_offset = 0; 7497 7498 switch (cdb->options & RSO_OPTIONS_MASK) { 7499 case RSO_OPTIONS_ALL: 7500 all = (struct scsi_report_supported_opcodes_all *) 7501 ctsio->kern_data_ptr; 7502 num = 0; 7503 for (i = 0; i < 256; i++) { 7504 entry = &ctl_cmd_table[i]; 7505 if (entry->flags & CTL_CMD_FLAG_SA5) { 7506 for (j = 0; j < 32; j++) { 7507 sentry = &((const struct ctl_cmd_entry *) 7508 entry->execute)[j]; 7509 if (!ctl_cmd_applicable( 7510 lun->be_lun->lun_type, sentry)) 7511 continue; 7512 descr = &all->descr[num++]; 7513 descr->opcode = i; 7514 scsi_ulto2b(j, descr->service_action); 7515 descr->flags = RSO_SERVACTV; 7516 scsi_ulto2b(sentry->length, 7517 descr->cdb_length); 7518 } 7519 } else { 7520 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7521 entry)) 7522 continue; 7523 descr = &all->descr[num++]; 7524 descr->opcode = i; 7525 scsi_ulto2b(0, descr->service_action); 7526 descr->flags = 0; 7527 scsi_ulto2b(entry->length, descr->cdb_length); 7528 } 7529 } 7530 scsi_ulto4b( 7531 num * sizeof(struct scsi_report_supported_opcodes_descr), 7532 all->length); 7533 break; 7534 case RSO_OPTIONS_OC: 7535 one = (struct scsi_report_supported_opcodes_one *) 7536 ctsio->kern_data_ptr; 7537 entry = &ctl_cmd_table[opcode]; 7538 goto fill_one; 7539 case RSO_OPTIONS_OC_SA: 7540 one = (struct scsi_report_supported_opcodes_one *) 7541 ctsio->kern_data_ptr; 7542 entry = &ctl_cmd_table[opcode]; 7543 entry = &((const struct ctl_cmd_entry *) 7544 entry->execute)[service_action]; 7545fill_one: 7546 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7547 one->support = 3; 7548 scsi_ulto2b(entry->length, one->cdb_length); 7549 one->cdb_usage[0] = opcode; 7550 memcpy(&one->cdb_usage[1], entry->usage, 7551 entry->length - 1); 7552 } else 7553 one->support = 1; 7554 break; 7555 } 7556 7557 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7558 ctsio->be_move_done = ctl_config_move_done; 7559 7560 ctl_datamove((union ctl_io *)ctsio); 7561 return(retval); 7562} 7563 7564int 7565ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7566{ 7567 struct ctl_lun *lun; 7568 struct scsi_report_supported_tmf *cdb; 7569 struct scsi_report_supported_tmf_data *data; 7570 int retval; 7571 int alloc_len, total_len; 7572 7573 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7574 7575 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7577 7578 retval = CTL_RETVAL_COMPLETE; 7579 7580 total_len = sizeof(struct scsi_report_supported_tmf_data); 7581 alloc_len = scsi_4btoul(cdb->length); 7582 7583 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7584 7585 ctsio->kern_sg_entries = 0; 7586 7587 if (total_len < alloc_len) { 7588 ctsio->residual = alloc_len - total_len; 7589 ctsio->kern_data_len = total_len; 7590 ctsio->kern_total_len = total_len; 7591 } else { 7592 ctsio->residual = 0; 7593 ctsio->kern_data_len = alloc_len; 7594 ctsio->kern_total_len = alloc_len; 7595 } 7596 ctsio->kern_data_resid = 0; 7597 ctsio->kern_rel_offset = 0; 7598 7599 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7600 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7601 data->byte2 |= RST_ITNRS; 7602 7603 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7604 ctsio->be_move_done = ctl_config_move_done; 7605 7606 ctl_datamove((union ctl_io *)ctsio); 7607 return (retval); 7608} 7609 7610int 7611ctl_report_timestamp(struct ctl_scsiio *ctsio) 7612{ 7613 struct ctl_lun *lun; 7614 struct scsi_report_timestamp *cdb; 7615 struct scsi_report_timestamp_data *data; 7616 struct timeval tv; 7617 int64_t timestamp; 7618 int retval; 7619 int alloc_len, total_len; 7620 7621 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7622 7623 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7624 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7625 7626 retval = CTL_RETVAL_COMPLETE; 7627 7628 total_len = sizeof(struct scsi_report_timestamp_data); 7629 alloc_len = scsi_4btoul(cdb->length); 7630 7631 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7632 7633 ctsio->kern_sg_entries = 0; 7634 7635 if (total_len < alloc_len) { 7636 ctsio->residual = alloc_len - total_len; 7637 ctsio->kern_data_len = total_len; 7638 ctsio->kern_total_len = total_len; 7639 } else { 7640 ctsio->residual = 0; 7641 ctsio->kern_data_len = alloc_len; 7642 ctsio->kern_total_len = alloc_len; 7643 } 7644 ctsio->kern_data_resid = 0; 7645 ctsio->kern_rel_offset = 0; 7646 7647 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7648 scsi_ulto2b(sizeof(*data) - 2, data->length); 7649 data->origin = RTS_ORIG_OUTSIDE; 7650 getmicrotime(&tv); 7651 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7652 scsi_ulto4b(timestamp >> 16, data->timestamp); 7653 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7654 7655 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7656 ctsio->be_move_done = ctl_config_move_done; 7657 7658 ctl_datamove((union ctl_io *)ctsio); 7659 return (retval); 7660} 7661 7662int 7663ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7664{ 7665 struct scsi_per_res_in *cdb; 7666 int alloc_len, total_len = 0; 7667 /* struct scsi_per_res_in_rsrv in_data; */ 7668 struct ctl_lun *lun; 7669 struct ctl_softc *softc; 7670 7671 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7672 7673 softc = control_softc; 7674 7675 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7676 7677 alloc_len = scsi_2btoul(cdb->length); 7678 7679 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7680 7681retry: 7682 mtx_lock(&lun->lun_lock); 7683 switch (cdb->action) { 7684 case SPRI_RK: /* read keys */ 7685 total_len = sizeof(struct scsi_per_res_in_keys) + 7686 lun->pr_key_count * 7687 sizeof(struct scsi_per_res_key); 7688 break; 7689 case SPRI_RR: /* read reservation */ 7690 if (lun->flags & CTL_LUN_PR_RESERVED) 7691 total_len = sizeof(struct scsi_per_res_in_rsrv); 7692 else 7693 total_len = sizeof(struct scsi_per_res_in_header); 7694 break; 7695 case SPRI_RC: /* report capabilities */ 7696 total_len = sizeof(struct scsi_per_res_cap); 7697 break; 7698 case SPRI_RS: /* read full status */ 7699 total_len = sizeof(struct scsi_per_res_in_header) + 7700 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7701 lun->pr_key_count; 7702 break; 7703 default: 7704 panic("Invalid PR type %x", cdb->action); 7705 } 7706 mtx_unlock(&lun->lun_lock); 7707 7708 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7709 7710 if (total_len < alloc_len) { 7711 ctsio->residual = alloc_len - total_len; 7712 ctsio->kern_data_len = total_len; 7713 ctsio->kern_total_len = total_len; 7714 } else { 7715 ctsio->residual = 0; 7716 ctsio->kern_data_len = alloc_len; 7717 ctsio->kern_total_len = alloc_len; 7718 } 7719 7720 ctsio->kern_data_resid = 0; 7721 ctsio->kern_rel_offset = 0; 7722 ctsio->kern_sg_entries = 0; 7723 7724 mtx_lock(&lun->lun_lock); 7725 switch (cdb->action) { 7726 case SPRI_RK: { // read keys 7727 struct scsi_per_res_in_keys *res_keys; 7728 int i, key_count; 7729 7730 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7731 7732 /* 7733 * We had to drop the lock to allocate our buffer, which 7734 * leaves time for someone to come in with another 7735 * persistent reservation. (That is unlikely, though, 7736 * since this should be the only persistent reservation 7737 * command active right now.) 7738 */ 7739 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7740 (lun->pr_key_count * 7741 sizeof(struct scsi_per_res_key)))){ 7742 mtx_unlock(&lun->lun_lock); 7743 free(ctsio->kern_data_ptr, M_CTL); 7744 printf("%s: reservation length changed, retrying\n", 7745 __func__); 7746 goto retry; 7747 } 7748 7749 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7750 7751 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7752 lun->pr_key_count, res_keys->header.length); 7753 7754 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7755 if (lun->pr_keys[i] == 0) 7756 continue; 7757 7758 /* 7759 * We used lun->pr_key_count to calculate the 7760 * size to allocate. If it turns out the number of 7761 * initiators with the registered flag set is 7762 * larger than that (i.e. they haven't been kept in 7763 * sync), we've got a problem. 7764 */ 7765 if (key_count >= lun->pr_key_count) { 7766#ifdef NEEDTOPORT 7767 csevent_log(CSC_CTL | CSC_SHELF_SW | 7768 CTL_PR_ERROR, 7769 csevent_LogType_Fault, 7770 csevent_AlertLevel_Yellow, 7771 csevent_FRU_ShelfController, 7772 csevent_FRU_Firmware, 7773 csevent_FRU_Unknown, 7774 "registered keys %d >= key " 7775 "count %d", key_count, 7776 lun->pr_key_count); 7777#endif 7778 key_count++; 7779 continue; 7780 } 7781 scsi_u64to8b(lun->pr_keys[i], 7782 res_keys->keys[key_count].key); 7783 key_count++; 7784 } 7785 break; 7786 } 7787 case SPRI_RR: { // read reservation 7788 struct scsi_per_res_in_rsrv *res; 7789 int tmp_len, header_only; 7790 7791 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7792 7793 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7794 7795 if (lun->flags & CTL_LUN_PR_RESERVED) 7796 { 7797 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7798 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7799 res->header.length); 7800 header_only = 0; 7801 } else { 7802 tmp_len = sizeof(struct scsi_per_res_in_header); 7803 scsi_ulto4b(0, res->header.length); 7804 header_only = 1; 7805 } 7806 7807 /* 7808 * We had to drop the lock to allocate our buffer, which 7809 * leaves time for someone to come in with another 7810 * persistent reservation. (That is unlikely, though, 7811 * since this should be the only persistent reservation 7812 * command active right now.) 7813 */ 7814 if (tmp_len != total_len) { 7815 mtx_unlock(&lun->lun_lock); 7816 free(ctsio->kern_data_ptr, M_CTL); 7817 printf("%s: reservation status changed, retrying\n", 7818 __func__); 7819 goto retry; 7820 } 7821 7822 /* 7823 * No reservation held, so we're done. 7824 */ 7825 if (header_only != 0) 7826 break; 7827 7828 /* 7829 * If the registration is an All Registrants type, the key 7830 * is 0, since it doesn't really matter. 7831 */ 7832 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7833 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 7834 res->data.reservation); 7835 } 7836 res->data.scopetype = lun->res_type; 7837 break; 7838 } 7839 case SPRI_RC: //report capabilities 7840 { 7841 struct scsi_per_res_cap *res_cap; 7842 uint16_t type_mask; 7843 7844 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7845 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7846 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7847 type_mask = SPRI_TM_WR_EX_AR | 7848 SPRI_TM_EX_AC_RO | 7849 SPRI_TM_WR_EX_RO | 7850 SPRI_TM_EX_AC | 7851 SPRI_TM_WR_EX | 7852 SPRI_TM_EX_AC_AR; 7853 scsi_ulto2b(type_mask, res_cap->type_mask); 7854 break; 7855 } 7856 case SPRI_RS: { // read full status 7857 struct scsi_per_res_in_full *res_status; 7858 struct scsi_per_res_in_full_desc *res_desc; 7859 struct ctl_port *port; 7860 int i, len; 7861 7862 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7863 7864 /* 7865 * We had to drop the lock to allocate our buffer, which 7866 * leaves time for someone to come in with another 7867 * persistent reservation. (That is unlikely, though, 7868 * since this should be the only persistent reservation 7869 * command active right now.) 7870 */ 7871 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7872 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7873 lun->pr_key_count)){ 7874 mtx_unlock(&lun->lun_lock); 7875 free(ctsio->kern_data_ptr, M_CTL); 7876 printf("%s: reservation length changed, retrying\n", 7877 __func__); 7878 goto retry; 7879 } 7880 7881 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7882 7883 res_desc = &res_status->desc[0]; 7884 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7885 if (lun->pr_keys[i] == 0) 7886 continue; 7887 7888 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 7889 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7890 (lun->pr_res_idx == i || 7891 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7892 res_desc->flags = SPRI_FULL_R_HOLDER; 7893 res_desc->scopetype = lun->res_type; 7894 } 7895 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7896 res_desc->rel_trgt_port_id); 7897 len = 0; 7898 port = softc->ctl_ports[ 7899 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7900 if (port != NULL) 7901 len = ctl_create_iid(port, 7902 i % CTL_MAX_INIT_PER_PORT, 7903 res_desc->transport_id); 7904 scsi_ulto4b(len, res_desc->additional_length); 7905 res_desc = (struct scsi_per_res_in_full_desc *) 7906 &res_desc->transport_id[len]; 7907 } 7908 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7909 res_status->header.length); 7910 break; 7911 } 7912 default: 7913 /* 7914 * This is a bug, because we just checked for this above, 7915 * and should have returned an error. 7916 */ 7917 panic("Invalid PR type %x", cdb->action); 7918 break; /* NOTREACHED */ 7919 } 7920 mtx_unlock(&lun->lun_lock); 7921 7922 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7923 ctsio->be_move_done = ctl_config_move_done; 7924 7925 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7926 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7927 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7928 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7929 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7930 7931 ctl_datamove((union ctl_io *)ctsio); 7932 7933 return (CTL_RETVAL_COMPLETE); 7934} 7935 7936/* 7937 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7938 * it should return. 7939 */ 7940static int 7941ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7942 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7943 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7944 struct scsi_per_res_out_parms* param) 7945{ 7946 union ctl_ha_msg persis_io; 7947 int retval, i; 7948 int isc_retval; 7949 7950 retval = 0; 7951 7952 mtx_lock(&lun->lun_lock); 7953 if (sa_res_key == 0) { 7954 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7955 /* validate scope and type */ 7956 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7957 SPR_LU_SCOPE) { 7958 mtx_unlock(&lun->lun_lock); 7959 ctl_set_invalid_field(/*ctsio*/ ctsio, 7960 /*sks_valid*/ 1, 7961 /*command*/ 1, 7962 /*field*/ 2, 7963 /*bit_valid*/ 1, 7964 /*bit*/ 4); 7965 ctl_done((union ctl_io *)ctsio); 7966 return (1); 7967 } 7968 7969 if (type>8 || type==2 || type==4 || type==0) { 7970 mtx_unlock(&lun->lun_lock); 7971 ctl_set_invalid_field(/*ctsio*/ ctsio, 7972 /*sks_valid*/ 1, 7973 /*command*/ 1, 7974 /*field*/ 2, 7975 /*bit_valid*/ 1, 7976 /*bit*/ 0); 7977 ctl_done((union ctl_io *)ctsio); 7978 return (1); 7979 } 7980 7981 /* 7982 * Unregister everybody else and build UA for 7983 * them 7984 */ 7985 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7986 if (i == residx || lun->pr_keys[i] == 0) 7987 continue; 7988 7989 if (!persis_offset 7990 && i <CTL_MAX_INITIATORS) 7991 lun->pending_ua[i] |= 7992 CTL_UA_REG_PREEMPT; 7993 else if (persis_offset 7994 && i >= persis_offset) 7995 lun->pending_ua[i-persis_offset] |= 7996 CTL_UA_REG_PREEMPT; 7997 lun->pr_keys[i] = 0; 7998 } 7999 lun->pr_key_count = 1; 8000 lun->res_type = type; 8001 if (lun->res_type != SPR_TYPE_WR_EX_AR 8002 && lun->res_type != SPR_TYPE_EX_AC_AR) 8003 lun->pr_res_idx = residx; 8004 8005 /* send msg to other side */ 8006 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8007 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8008 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8009 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8010 persis_io.pr.pr_info.res_type = type; 8011 memcpy(persis_io.pr.pr_info.sa_res_key, 8012 param->serv_act_res_key, 8013 sizeof(param->serv_act_res_key)); 8014 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8015 &persis_io, sizeof(persis_io), 0)) > 8016 CTL_HA_STATUS_SUCCESS) { 8017 printf("CTL:Persis Out error returned " 8018 "from ctl_ha_msg_send %d\n", 8019 isc_retval); 8020 } 8021 } else { 8022 /* not all registrants */ 8023 mtx_unlock(&lun->lun_lock); 8024 free(ctsio->kern_data_ptr, M_CTL); 8025 ctl_set_invalid_field(ctsio, 8026 /*sks_valid*/ 1, 8027 /*command*/ 0, 8028 /*field*/ 8, 8029 /*bit_valid*/ 0, 8030 /*bit*/ 0); 8031 ctl_done((union ctl_io *)ctsio); 8032 return (1); 8033 } 8034 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8035 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8036 int found = 0; 8037 8038 if (res_key == sa_res_key) { 8039 /* special case */ 8040 /* 8041 * The spec implies this is not good but doesn't 8042 * say what to do. There are two choices either 8043 * generate a res conflict or check condition 8044 * with illegal field in parameter data. Since 8045 * that is what is done when the sa_res_key is 8046 * zero I'll take that approach since this has 8047 * to do with the sa_res_key. 8048 */ 8049 mtx_unlock(&lun->lun_lock); 8050 free(ctsio->kern_data_ptr, M_CTL); 8051 ctl_set_invalid_field(ctsio, 8052 /*sks_valid*/ 1, 8053 /*command*/ 0, 8054 /*field*/ 8, 8055 /*bit_valid*/ 0, 8056 /*bit*/ 0); 8057 ctl_done((union ctl_io *)ctsio); 8058 return (1); 8059 } 8060 8061 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8062 if (lun->pr_keys[i] != sa_res_key) 8063 continue; 8064 8065 found = 1; 8066 lun->pr_keys[i] = 0; 8067 lun->pr_key_count--; 8068 8069 if (!persis_offset && i < CTL_MAX_INITIATORS) 8070 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT; 8071 else if (persis_offset && i >= persis_offset) 8072 lun->pending_ua[i-persis_offset] |= 8073 CTL_UA_REG_PREEMPT; 8074 } 8075 if (!found) { 8076 mtx_unlock(&lun->lun_lock); 8077 free(ctsio->kern_data_ptr, M_CTL); 8078 ctl_set_reservation_conflict(ctsio); 8079 ctl_done((union ctl_io *)ctsio); 8080 return (CTL_RETVAL_COMPLETE); 8081 } 8082 /* send msg to other side */ 8083 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8084 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8085 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8086 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8087 persis_io.pr.pr_info.res_type = type; 8088 memcpy(persis_io.pr.pr_info.sa_res_key, 8089 param->serv_act_res_key, 8090 sizeof(param->serv_act_res_key)); 8091 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8092 &persis_io, sizeof(persis_io), 0)) > 8093 CTL_HA_STATUS_SUCCESS) { 8094 printf("CTL:Persis Out error returned from " 8095 "ctl_ha_msg_send %d\n", isc_retval); 8096 } 8097 } else { 8098 /* Reserved but not all registrants */ 8099 /* sa_res_key is res holder */ 8100 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8101 /* validate scope and type */ 8102 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8103 SPR_LU_SCOPE) { 8104 mtx_unlock(&lun->lun_lock); 8105 ctl_set_invalid_field(/*ctsio*/ ctsio, 8106 /*sks_valid*/ 1, 8107 /*command*/ 1, 8108 /*field*/ 2, 8109 /*bit_valid*/ 1, 8110 /*bit*/ 4); 8111 ctl_done((union ctl_io *)ctsio); 8112 return (1); 8113 } 8114 8115 if (type>8 || type==2 || type==4 || type==0) { 8116 mtx_unlock(&lun->lun_lock); 8117 ctl_set_invalid_field(/*ctsio*/ ctsio, 8118 /*sks_valid*/ 1, 8119 /*command*/ 1, 8120 /*field*/ 2, 8121 /*bit_valid*/ 1, 8122 /*bit*/ 0); 8123 ctl_done((union ctl_io *)ctsio); 8124 return (1); 8125 } 8126 8127 /* 8128 * Do the following: 8129 * if sa_res_key != res_key remove all 8130 * registrants w/sa_res_key and generate UA 8131 * for these registrants(Registrations 8132 * Preempted) if it wasn't an exclusive 8133 * reservation generate UA(Reservations 8134 * Preempted) for all other registered nexuses 8135 * if the type has changed. Establish the new 8136 * reservation and holder. If res_key and 8137 * sa_res_key are the same do the above 8138 * except don't unregister the res holder. 8139 */ 8140 8141 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8142 if (i == residx || lun->pr_keys[i] == 0) 8143 continue; 8144 8145 if (sa_res_key == lun->pr_keys[i]) { 8146 lun->pr_keys[i] = 0; 8147 lun->pr_key_count--; 8148 8149 if (!persis_offset 8150 && i < CTL_MAX_INITIATORS) 8151 lun->pending_ua[i] |= 8152 CTL_UA_REG_PREEMPT; 8153 else if (persis_offset 8154 && i >= persis_offset) 8155 lun->pending_ua[i-persis_offset] |= 8156 CTL_UA_REG_PREEMPT; 8157 } else if (type != lun->res_type 8158 && (lun->res_type == SPR_TYPE_WR_EX_RO 8159 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8160 if (!persis_offset 8161 && i < CTL_MAX_INITIATORS) 8162 lun->pending_ua[i] |= 8163 CTL_UA_RES_RELEASE; 8164 else if (persis_offset 8165 && i >= persis_offset) 8166 lun->pending_ua[ 8167 i-persis_offset] |= 8168 CTL_UA_RES_RELEASE; 8169 } 8170 } 8171 lun->res_type = type; 8172 if (lun->res_type != SPR_TYPE_WR_EX_AR 8173 && lun->res_type != SPR_TYPE_EX_AC_AR) 8174 lun->pr_res_idx = residx; 8175 else 8176 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8177 8178 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8179 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8180 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8181 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8182 persis_io.pr.pr_info.res_type = type; 8183 memcpy(persis_io.pr.pr_info.sa_res_key, 8184 param->serv_act_res_key, 8185 sizeof(param->serv_act_res_key)); 8186 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8187 &persis_io, sizeof(persis_io), 0)) > 8188 CTL_HA_STATUS_SUCCESS) { 8189 printf("CTL:Persis Out error returned " 8190 "from ctl_ha_msg_send %d\n", 8191 isc_retval); 8192 } 8193 } else { 8194 /* 8195 * sa_res_key is not the res holder just 8196 * remove registrants 8197 */ 8198 int found=0; 8199 8200 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8201 if (sa_res_key != lun->pr_keys[i]) 8202 continue; 8203 8204 found = 1; 8205 lun->pr_keys[i] = 0; 8206 lun->pr_key_count--; 8207 8208 if (!persis_offset 8209 && i < CTL_MAX_INITIATORS) 8210 lun->pending_ua[i] |= 8211 CTL_UA_REG_PREEMPT; 8212 else if (persis_offset 8213 && i >= persis_offset) 8214 lun->pending_ua[i-persis_offset] |= 8215 CTL_UA_REG_PREEMPT; 8216 } 8217 8218 if (!found) { 8219 mtx_unlock(&lun->lun_lock); 8220 free(ctsio->kern_data_ptr, M_CTL); 8221 ctl_set_reservation_conflict(ctsio); 8222 ctl_done((union ctl_io *)ctsio); 8223 return (1); 8224 } 8225 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8226 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8227 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8228 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8229 persis_io.pr.pr_info.res_type = type; 8230 memcpy(persis_io.pr.pr_info.sa_res_key, 8231 param->serv_act_res_key, 8232 sizeof(param->serv_act_res_key)); 8233 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8234 &persis_io, sizeof(persis_io), 0)) > 8235 CTL_HA_STATUS_SUCCESS) { 8236 printf("CTL:Persis Out error returned " 8237 "from ctl_ha_msg_send %d\n", 8238 isc_retval); 8239 } 8240 } 8241 } 8242 8243 lun->PRGeneration++; 8244 mtx_unlock(&lun->lun_lock); 8245 8246 return (retval); 8247} 8248 8249static void 8250ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8251{ 8252 uint64_t sa_res_key; 8253 int i; 8254 8255 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8256 8257 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8258 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8259 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8260 if (sa_res_key == 0) { 8261 /* 8262 * Unregister everybody else and build UA for 8263 * them 8264 */ 8265 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8266 if (i == msg->pr.pr_info.residx || 8267 lun->pr_keys[i] == 0) 8268 continue; 8269 8270 if (!persis_offset 8271 && i < CTL_MAX_INITIATORS) 8272 lun->pending_ua[i] |= 8273 CTL_UA_REG_PREEMPT; 8274 else if (persis_offset && i >= persis_offset) 8275 lun->pending_ua[i - persis_offset] |= 8276 CTL_UA_REG_PREEMPT; 8277 lun->pr_keys[i] = 0; 8278 } 8279 8280 lun->pr_key_count = 1; 8281 lun->res_type = msg->pr.pr_info.res_type; 8282 if (lun->res_type != SPR_TYPE_WR_EX_AR 8283 && lun->res_type != SPR_TYPE_EX_AC_AR) 8284 lun->pr_res_idx = msg->pr.pr_info.residx; 8285 } else { 8286 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8287 if (sa_res_key == lun->pr_keys[i]) 8288 continue; 8289 8290 lun->pr_keys[i] = 0; 8291 lun->pr_key_count--; 8292 8293 if (!persis_offset 8294 && i < persis_offset) 8295 lun->pending_ua[i] |= 8296 CTL_UA_REG_PREEMPT; 8297 else if (persis_offset 8298 && i >= persis_offset) 8299 lun->pending_ua[i - persis_offset] |= 8300 CTL_UA_REG_PREEMPT; 8301 } 8302 } 8303 } else { 8304 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8305 if (i == msg->pr.pr_info.residx || 8306 lun->pr_keys[i] == 0) 8307 continue; 8308 8309 if (sa_res_key == lun->pr_keys[i]) { 8310 lun->pr_keys[i] = 0; 8311 lun->pr_key_count--; 8312 if (!persis_offset 8313 && i < CTL_MAX_INITIATORS) 8314 lun->pending_ua[i] |= 8315 CTL_UA_REG_PREEMPT; 8316 else if (persis_offset 8317 && i >= persis_offset) 8318 lun->pending_ua[i - persis_offset] |= 8319 CTL_UA_REG_PREEMPT; 8320 } else if (msg->pr.pr_info.res_type != lun->res_type 8321 && (lun->res_type == SPR_TYPE_WR_EX_RO 8322 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8323 if (!persis_offset 8324 && i < persis_offset) 8325 lun->pending_ua[i] |= 8326 CTL_UA_RES_RELEASE; 8327 else if (persis_offset 8328 && i >= persis_offset) 8329 lun->pending_ua[i - persis_offset] |= 8330 CTL_UA_RES_RELEASE; 8331 } 8332 } 8333 lun->res_type = msg->pr.pr_info.res_type; 8334 if (lun->res_type != SPR_TYPE_WR_EX_AR 8335 && lun->res_type != SPR_TYPE_EX_AC_AR) 8336 lun->pr_res_idx = msg->pr.pr_info.residx; 8337 else 8338 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8339 } 8340 lun->PRGeneration++; 8341 8342} 8343 8344 8345int 8346ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8347{ 8348 int retval; 8349 int isc_retval; 8350 u_int32_t param_len; 8351 struct scsi_per_res_out *cdb; 8352 struct ctl_lun *lun; 8353 struct scsi_per_res_out_parms* param; 8354 struct ctl_softc *softc; 8355 uint32_t residx; 8356 uint64_t res_key, sa_res_key; 8357 uint8_t type; 8358 union ctl_ha_msg persis_io; 8359 int i; 8360 8361 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8362 8363 retval = CTL_RETVAL_COMPLETE; 8364 8365 softc = control_softc; 8366 8367 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8368 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8369 8370 /* 8371 * We only support whole-LUN scope. The scope & type are ignored for 8372 * register, register and ignore existing key and clear. 8373 * We sometimes ignore scope and type on preempts too!! 8374 * Verify reservation type here as well. 8375 */ 8376 type = cdb->scope_type & SPR_TYPE_MASK; 8377 if ((cdb->action == SPRO_RESERVE) 8378 || (cdb->action == SPRO_RELEASE)) { 8379 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8380 ctl_set_invalid_field(/*ctsio*/ ctsio, 8381 /*sks_valid*/ 1, 8382 /*command*/ 1, 8383 /*field*/ 2, 8384 /*bit_valid*/ 1, 8385 /*bit*/ 4); 8386 ctl_done((union ctl_io *)ctsio); 8387 return (CTL_RETVAL_COMPLETE); 8388 } 8389 8390 if (type>8 || type==2 || type==4 || type==0) { 8391 ctl_set_invalid_field(/*ctsio*/ ctsio, 8392 /*sks_valid*/ 1, 8393 /*command*/ 1, 8394 /*field*/ 2, 8395 /*bit_valid*/ 1, 8396 /*bit*/ 0); 8397 ctl_done((union ctl_io *)ctsio); 8398 return (CTL_RETVAL_COMPLETE); 8399 } 8400 } 8401 8402 param_len = scsi_4btoul(cdb->length); 8403 8404 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8405 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8406 ctsio->kern_data_len = param_len; 8407 ctsio->kern_total_len = param_len; 8408 ctsio->kern_data_resid = 0; 8409 ctsio->kern_rel_offset = 0; 8410 ctsio->kern_sg_entries = 0; 8411 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8412 ctsio->be_move_done = ctl_config_move_done; 8413 ctl_datamove((union ctl_io *)ctsio); 8414 8415 return (CTL_RETVAL_COMPLETE); 8416 } 8417 8418 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8419 8420 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8421 res_key = scsi_8btou64(param->res_key.key); 8422 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8423 8424 /* 8425 * Validate the reservation key here except for SPRO_REG_IGNO 8426 * This must be done for all other service actions 8427 */ 8428 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8429 mtx_lock(&lun->lun_lock); 8430 if (lun->pr_keys[residx] != 0) { 8431 if (res_key != lun->pr_keys[residx]) { 8432 /* 8433 * The current key passed in doesn't match 8434 * the one the initiator previously 8435 * registered. 8436 */ 8437 mtx_unlock(&lun->lun_lock); 8438 free(ctsio->kern_data_ptr, M_CTL); 8439 ctl_set_reservation_conflict(ctsio); 8440 ctl_done((union ctl_io *)ctsio); 8441 return (CTL_RETVAL_COMPLETE); 8442 } 8443 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8444 /* 8445 * We are not registered 8446 */ 8447 mtx_unlock(&lun->lun_lock); 8448 free(ctsio->kern_data_ptr, M_CTL); 8449 ctl_set_reservation_conflict(ctsio); 8450 ctl_done((union ctl_io *)ctsio); 8451 return (CTL_RETVAL_COMPLETE); 8452 } else if (res_key != 0) { 8453 /* 8454 * We are not registered and trying to register but 8455 * the register key isn't zero. 8456 */ 8457 mtx_unlock(&lun->lun_lock); 8458 free(ctsio->kern_data_ptr, M_CTL); 8459 ctl_set_reservation_conflict(ctsio); 8460 ctl_done((union ctl_io *)ctsio); 8461 return (CTL_RETVAL_COMPLETE); 8462 } 8463 mtx_unlock(&lun->lun_lock); 8464 } 8465 8466 switch (cdb->action & SPRO_ACTION_MASK) { 8467 case SPRO_REGISTER: 8468 case SPRO_REG_IGNO: { 8469 8470#if 0 8471 printf("Registration received\n"); 8472#endif 8473 8474 /* 8475 * We don't support any of these options, as we report in 8476 * the read capabilities request (see 8477 * ctl_persistent_reserve_in(), above). 8478 */ 8479 if ((param->flags & SPR_SPEC_I_PT) 8480 || (param->flags & SPR_ALL_TG_PT) 8481 || (param->flags & SPR_APTPL)) { 8482 int bit_ptr; 8483 8484 if (param->flags & SPR_APTPL) 8485 bit_ptr = 0; 8486 else if (param->flags & SPR_ALL_TG_PT) 8487 bit_ptr = 2; 8488 else /* SPR_SPEC_I_PT */ 8489 bit_ptr = 3; 8490 8491 free(ctsio->kern_data_ptr, M_CTL); 8492 ctl_set_invalid_field(ctsio, 8493 /*sks_valid*/ 1, 8494 /*command*/ 0, 8495 /*field*/ 20, 8496 /*bit_valid*/ 1, 8497 /*bit*/ bit_ptr); 8498 ctl_done((union ctl_io *)ctsio); 8499 return (CTL_RETVAL_COMPLETE); 8500 } 8501 8502 mtx_lock(&lun->lun_lock); 8503 8504 /* 8505 * The initiator wants to clear the 8506 * key/unregister. 8507 */ 8508 if (sa_res_key == 0) { 8509 if ((res_key == 0 8510 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8511 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8512 && lun->pr_keys[residx] == 0)) { 8513 mtx_unlock(&lun->lun_lock); 8514 goto done; 8515 } 8516 8517 lun->pr_keys[residx] = 0; 8518 lun->pr_key_count--; 8519 8520 if (residx == lun->pr_res_idx) { 8521 lun->flags &= ~CTL_LUN_PR_RESERVED; 8522 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8523 8524 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8525 || lun->res_type == SPR_TYPE_EX_AC_RO) 8526 && lun->pr_key_count) { 8527 /* 8528 * If the reservation is a registrants 8529 * only type we need to generate a UA 8530 * for other registered inits. The 8531 * sense code should be RESERVATIONS 8532 * RELEASED 8533 */ 8534 8535 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8536 if (lun->pr_keys[ 8537 i + persis_offset] == 0) 8538 continue; 8539 lun->pending_ua[i] |= 8540 CTL_UA_RES_RELEASE; 8541 } 8542 } 8543 lun->res_type = 0; 8544 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8545 if (lun->pr_key_count==0) { 8546 lun->flags &= ~CTL_LUN_PR_RESERVED; 8547 lun->res_type = 0; 8548 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8549 } 8550 } 8551 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8552 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8553 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8554 persis_io.pr.pr_info.residx = residx; 8555 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8556 &persis_io, sizeof(persis_io), 0 )) > 8557 CTL_HA_STATUS_SUCCESS) { 8558 printf("CTL:Persis Out error returned from " 8559 "ctl_ha_msg_send %d\n", isc_retval); 8560 } 8561 } else /* sa_res_key != 0 */ { 8562 8563 /* 8564 * If we aren't registered currently then increment 8565 * the key count and set the registered flag. 8566 */ 8567 if (lun->pr_keys[residx] == 0) 8568 lun->pr_key_count++; 8569 lun->pr_keys[residx] = sa_res_key; 8570 8571 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8572 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8573 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8574 persis_io.pr.pr_info.residx = residx; 8575 memcpy(persis_io.pr.pr_info.sa_res_key, 8576 param->serv_act_res_key, 8577 sizeof(param->serv_act_res_key)); 8578 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8579 &persis_io, sizeof(persis_io), 0)) > 8580 CTL_HA_STATUS_SUCCESS) { 8581 printf("CTL:Persis Out error returned from " 8582 "ctl_ha_msg_send %d\n", isc_retval); 8583 } 8584 } 8585 lun->PRGeneration++; 8586 mtx_unlock(&lun->lun_lock); 8587 8588 break; 8589 } 8590 case SPRO_RESERVE: 8591#if 0 8592 printf("Reserve executed type %d\n", type); 8593#endif 8594 mtx_lock(&lun->lun_lock); 8595 if (lun->flags & CTL_LUN_PR_RESERVED) { 8596 /* 8597 * if this isn't the reservation holder and it's 8598 * not a "all registrants" type or if the type is 8599 * different then we have a conflict 8600 */ 8601 if ((lun->pr_res_idx != residx 8602 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8603 || lun->res_type != type) { 8604 mtx_unlock(&lun->lun_lock); 8605 free(ctsio->kern_data_ptr, M_CTL); 8606 ctl_set_reservation_conflict(ctsio); 8607 ctl_done((union ctl_io *)ctsio); 8608 return (CTL_RETVAL_COMPLETE); 8609 } 8610 mtx_unlock(&lun->lun_lock); 8611 } else /* create a reservation */ { 8612 /* 8613 * If it's not an "all registrants" type record 8614 * reservation holder 8615 */ 8616 if (type != SPR_TYPE_WR_EX_AR 8617 && type != SPR_TYPE_EX_AC_AR) 8618 lun->pr_res_idx = residx; /* Res holder */ 8619 else 8620 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8621 8622 lun->flags |= CTL_LUN_PR_RESERVED; 8623 lun->res_type = type; 8624 8625 mtx_unlock(&lun->lun_lock); 8626 8627 /* send msg to other side */ 8628 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8629 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8630 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8631 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8632 persis_io.pr.pr_info.res_type = type; 8633 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8634 &persis_io, sizeof(persis_io), 0)) > 8635 CTL_HA_STATUS_SUCCESS) { 8636 printf("CTL:Persis Out error returned from " 8637 "ctl_ha_msg_send %d\n", isc_retval); 8638 } 8639 } 8640 break; 8641 8642 case SPRO_RELEASE: 8643 mtx_lock(&lun->lun_lock); 8644 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8645 /* No reservation exists return good status */ 8646 mtx_unlock(&lun->lun_lock); 8647 goto done; 8648 } 8649 /* 8650 * Is this nexus a reservation holder? 8651 */ 8652 if (lun->pr_res_idx != residx 8653 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8654 /* 8655 * not a res holder return good status but 8656 * do nothing 8657 */ 8658 mtx_unlock(&lun->lun_lock); 8659 goto done; 8660 } 8661 8662 if (lun->res_type != type) { 8663 mtx_unlock(&lun->lun_lock); 8664 free(ctsio->kern_data_ptr, M_CTL); 8665 ctl_set_illegal_pr_release(ctsio); 8666 ctl_done((union ctl_io *)ctsio); 8667 return (CTL_RETVAL_COMPLETE); 8668 } 8669 8670 /* okay to release */ 8671 lun->flags &= ~CTL_LUN_PR_RESERVED; 8672 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8673 lun->res_type = 0; 8674 8675 /* 8676 * if this isn't an exclusive access 8677 * res generate UA for all other 8678 * registrants. 8679 */ 8680 if (type != SPR_TYPE_EX_AC 8681 && type != SPR_TYPE_WR_EX) { 8682 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8683 if (i == residx || 8684 lun->pr_keys[i + persis_offset] == 0) 8685 continue; 8686 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8687 } 8688 } 8689 mtx_unlock(&lun->lun_lock); 8690 /* Send msg to other side */ 8691 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8692 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8693 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8694 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8695 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8696 printf("CTL:Persis Out error returned from " 8697 "ctl_ha_msg_send %d\n", isc_retval); 8698 } 8699 break; 8700 8701 case SPRO_CLEAR: 8702 /* send msg to other side */ 8703 8704 mtx_lock(&lun->lun_lock); 8705 lun->flags &= ~CTL_LUN_PR_RESERVED; 8706 lun->res_type = 0; 8707 lun->pr_key_count = 0; 8708 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8709 8710 lun->pr_keys[residx] = 0; 8711 8712 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8713 if (lun->pr_keys[i] != 0) { 8714 if (!persis_offset && i < CTL_MAX_INITIATORS) 8715 lun->pending_ua[i] |= 8716 CTL_UA_RES_PREEMPT; 8717 else if (persis_offset && i >= persis_offset) 8718 lun->pending_ua[i-persis_offset] |= 8719 CTL_UA_RES_PREEMPT; 8720 8721 lun->pr_keys[i] = 0; 8722 } 8723 lun->PRGeneration++; 8724 mtx_unlock(&lun->lun_lock); 8725 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8726 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8727 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8728 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8729 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8730 printf("CTL:Persis Out error returned from " 8731 "ctl_ha_msg_send %d\n", isc_retval); 8732 } 8733 break; 8734 8735 case SPRO_PREEMPT: { 8736 int nretval; 8737 8738 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8739 residx, ctsio, cdb, param); 8740 if (nretval != 0) 8741 return (CTL_RETVAL_COMPLETE); 8742 break; 8743 } 8744 default: 8745 panic("Invalid PR type %x", cdb->action); 8746 } 8747 8748done: 8749 free(ctsio->kern_data_ptr, M_CTL); 8750 ctl_set_success(ctsio); 8751 ctl_done((union ctl_io *)ctsio); 8752 8753 return (retval); 8754} 8755 8756/* 8757 * This routine is for handling a message from the other SC pertaining to 8758 * persistent reserve out. All the error checking will have been done 8759 * so only perorming the action need be done here to keep the two 8760 * in sync. 8761 */ 8762static void 8763ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8764{ 8765 struct ctl_lun *lun; 8766 struct ctl_softc *softc; 8767 int i; 8768 uint32_t targ_lun; 8769 8770 softc = control_softc; 8771 8772 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8773 lun = softc->ctl_luns[targ_lun]; 8774 mtx_lock(&lun->lun_lock); 8775 switch(msg->pr.pr_info.action) { 8776 case CTL_PR_REG_KEY: 8777 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8778 lun->pr_key_count++; 8779 lun->pr_keys[msg->pr.pr_info.residx] = 8780 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8781 lun->PRGeneration++; 8782 break; 8783 8784 case CTL_PR_UNREG_KEY: 8785 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8786 lun->pr_key_count--; 8787 8788 /* XXX Need to see if the reservation has been released */ 8789 /* if so do we need to generate UA? */ 8790 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8791 lun->flags &= ~CTL_LUN_PR_RESERVED; 8792 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8793 8794 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8795 || lun->res_type == SPR_TYPE_EX_AC_RO) 8796 && lun->pr_key_count) { 8797 /* 8798 * If the reservation is a registrants 8799 * only type we need to generate a UA 8800 * for other registered inits. The 8801 * sense code should be RESERVATIONS 8802 * RELEASED 8803 */ 8804 8805 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8806 if (lun->pr_keys[i+ 8807 persis_offset] == 0) 8808 continue; 8809 8810 lun->pending_ua[i] |= 8811 CTL_UA_RES_RELEASE; 8812 } 8813 } 8814 lun->res_type = 0; 8815 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8816 if (lun->pr_key_count==0) { 8817 lun->flags &= ~CTL_LUN_PR_RESERVED; 8818 lun->res_type = 0; 8819 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8820 } 8821 } 8822 lun->PRGeneration++; 8823 break; 8824 8825 case CTL_PR_RESERVE: 8826 lun->flags |= CTL_LUN_PR_RESERVED; 8827 lun->res_type = msg->pr.pr_info.res_type; 8828 lun->pr_res_idx = msg->pr.pr_info.residx; 8829 8830 break; 8831 8832 case CTL_PR_RELEASE: 8833 /* 8834 * if this isn't an exclusive access res generate UA for all 8835 * other registrants. 8836 */ 8837 if (lun->res_type != SPR_TYPE_EX_AC 8838 && lun->res_type != SPR_TYPE_WR_EX) { 8839 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8840 if (lun->pr_keys[i+persis_offset] != 0) 8841 lun->pending_ua[i] |= 8842 CTL_UA_RES_RELEASE; 8843 } 8844 8845 lun->flags &= ~CTL_LUN_PR_RESERVED; 8846 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8847 lun->res_type = 0; 8848 break; 8849 8850 case CTL_PR_PREEMPT: 8851 ctl_pro_preempt_other(lun, msg); 8852 break; 8853 case CTL_PR_CLEAR: 8854 lun->flags &= ~CTL_LUN_PR_RESERVED; 8855 lun->res_type = 0; 8856 lun->pr_key_count = 0; 8857 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8858 8859 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8860 if (lun->pr_keys[i] == 0) 8861 continue; 8862 if (!persis_offset 8863 && i < CTL_MAX_INITIATORS) 8864 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT; 8865 else if (persis_offset 8866 && i >= persis_offset) 8867 lun->pending_ua[i-persis_offset] |= 8868 CTL_UA_RES_PREEMPT; 8869 lun->pr_keys[i] = 0; 8870 } 8871 lun->PRGeneration++; 8872 break; 8873 } 8874 8875 mtx_unlock(&lun->lun_lock); 8876} 8877 8878int 8879ctl_read_write(struct ctl_scsiio *ctsio) 8880{ 8881 struct ctl_lun *lun; 8882 struct ctl_lba_len_flags *lbalen; 8883 uint64_t lba; 8884 uint32_t num_blocks; 8885 int flags, retval; 8886 int isread; 8887 8888 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8889 8890 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8891 8892 flags = 0; 8893 retval = CTL_RETVAL_COMPLETE; 8894 8895 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8896 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8897 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8898 uint32_t residx; 8899 8900 /* 8901 * XXX KDM need a lock here. 8902 */ 8903 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8904 if ((lun->res_type == SPR_TYPE_EX_AC 8905 && residx != lun->pr_res_idx) 8906 || ((lun->res_type == SPR_TYPE_EX_AC_RO 8907 || lun->res_type == SPR_TYPE_EX_AC_AR) 8908 && lun->pr_keys[residx] == 0)) { 8909 ctl_set_reservation_conflict(ctsio); 8910 ctl_done((union ctl_io *)ctsio); 8911 return (CTL_RETVAL_COMPLETE); 8912 } 8913 } 8914 8915 switch (ctsio->cdb[0]) { 8916 case READ_6: 8917 case WRITE_6: { 8918 struct scsi_rw_6 *cdb; 8919 8920 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8921 8922 lba = scsi_3btoul(cdb->addr); 8923 /* only 5 bits are valid in the most significant address byte */ 8924 lba &= 0x1fffff; 8925 num_blocks = cdb->length; 8926 /* 8927 * This is correct according to SBC-2. 8928 */ 8929 if (num_blocks == 0) 8930 num_blocks = 256; 8931 break; 8932 } 8933 case READ_10: 8934 case WRITE_10: { 8935 struct scsi_rw_10 *cdb; 8936 8937 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8938 if (cdb->byte2 & SRW10_FUA) 8939 flags |= CTL_LLF_FUA; 8940 if (cdb->byte2 & SRW10_DPO) 8941 flags |= CTL_LLF_DPO; 8942 lba = scsi_4btoul(cdb->addr); 8943 num_blocks = scsi_2btoul(cdb->length); 8944 break; 8945 } 8946 case WRITE_VERIFY_10: { 8947 struct scsi_write_verify_10 *cdb; 8948 8949 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8950 flags |= CTL_LLF_FUA; 8951 if (cdb->byte2 & SWV_DPO) 8952 flags |= CTL_LLF_DPO; 8953 lba = scsi_4btoul(cdb->addr); 8954 num_blocks = scsi_2btoul(cdb->length); 8955 break; 8956 } 8957 case READ_12: 8958 case WRITE_12: { 8959 struct scsi_rw_12 *cdb; 8960 8961 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8962 if (cdb->byte2 & SRW12_FUA) 8963 flags |= CTL_LLF_FUA; 8964 if (cdb->byte2 & SRW12_DPO) 8965 flags |= CTL_LLF_DPO; 8966 lba = scsi_4btoul(cdb->addr); 8967 num_blocks = scsi_4btoul(cdb->length); 8968 break; 8969 } 8970 case WRITE_VERIFY_12: { 8971 struct scsi_write_verify_12 *cdb; 8972 8973 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8974 flags |= CTL_LLF_FUA; 8975 if (cdb->byte2 & SWV_DPO) 8976 flags |= CTL_LLF_DPO; 8977 lba = scsi_4btoul(cdb->addr); 8978 num_blocks = scsi_4btoul(cdb->length); 8979 break; 8980 } 8981 case READ_16: 8982 case WRITE_16: { 8983 struct scsi_rw_16 *cdb; 8984 8985 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8986 if (cdb->byte2 & SRW12_FUA) 8987 flags |= CTL_LLF_FUA; 8988 if (cdb->byte2 & SRW12_DPO) 8989 flags |= CTL_LLF_DPO; 8990 lba = scsi_8btou64(cdb->addr); 8991 num_blocks = scsi_4btoul(cdb->length); 8992 break; 8993 } 8994 case WRITE_ATOMIC_16: { 8995 struct scsi_rw_16 *cdb; 8996 8997 if (lun->be_lun->atomicblock == 0) { 8998 ctl_set_invalid_opcode(ctsio); 8999 ctl_done((union ctl_io *)ctsio); 9000 return (CTL_RETVAL_COMPLETE); 9001 } 9002 9003 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9004 if (cdb->byte2 & SRW12_FUA) 9005 flags |= CTL_LLF_FUA; 9006 if (cdb->byte2 & SRW12_DPO) 9007 flags |= CTL_LLF_DPO; 9008 lba = scsi_8btou64(cdb->addr); 9009 num_blocks = scsi_4btoul(cdb->length); 9010 if (num_blocks > lun->be_lun->atomicblock) { 9011 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9012 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9013 /*bit*/ 0); 9014 ctl_done((union ctl_io *)ctsio); 9015 return (CTL_RETVAL_COMPLETE); 9016 } 9017 break; 9018 } 9019 case WRITE_VERIFY_16: { 9020 struct scsi_write_verify_16 *cdb; 9021 9022 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9023 flags |= CTL_LLF_FUA; 9024 if (cdb->byte2 & SWV_DPO) 9025 flags |= CTL_LLF_DPO; 9026 lba = scsi_8btou64(cdb->addr); 9027 num_blocks = scsi_4btoul(cdb->length); 9028 break; 9029 } 9030 default: 9031 /* 9032 * We got a command we don't support. This shouldn't 9033 * happen, commands should be filtered out above us. 9034 */ 9035 ctl_set_invalid_opcode(ctsio); 9036 ctl_done((union ctl_io *)ctsio); 9037 9038 return (CTL_RETVAL_COMPLETE); 9039 break; /* NOTREACHED */ 9040 } 9041 9042 /* 9043 * The first check is to make sure we're in bounds, the second 9044 * check is to catch wrap-around problems. If the lba + num blocks 9045 * is less than the lba, then we've wrapped around and the block 9046 * range is invalid anyway. 9047 */ 9048 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9049 || ((lba + num_blocks) < lba)) { 9050 ctl_set_lba_out_of_range(ctsio); 9051 ctl_done((union ctl_io *)ctsio); 9052 return (CTL_RETVAL_COMPLETE); 9053 } 9054 9055 /* 9056 * According to SBC-3, a transfer length of 0 is not an error. 9057 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9058 * translates to 256 blocks for those commands. 9059 */ 9060 if (num_blocks == 0) { 9061 ctl_set_success(ctsio); 9062 ctl_done((union ctl_io *)ctsio); 9063 return (CTL_RETVAL_COMPLETE); 9064 } 9065 9066 /* Set FUA and/or DPO if caches are disabled. */ 9067 if (isread) { 9068 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9069 SCP_RCD) != 0) 9070 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9071 } else { 9072 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9073 SCP_WCE) == 0) 9074 flags |= CTL_LLF_FUA; 9075 } 9076 9077 lbalen = (struct ctl_lba_len_flags *) 9078 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9079 lbalen->lba = lba; 9080 lbalen->len = num_blocks; 9081 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9082 9083 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9084 ctsio->kern_rel_offset = 0; 9085 9086 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9087 9088 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9089 9090 return (retval); 9091} 9092 9093static int 9094ctl_cnw_cont(union ctl_io *io) 9095{ 9096 struct ctl_scsiio *ctsio; 9097 struct ctl_lun *lun; 9098 struct ctl_lba_len_flags *lbalen; 9099 int retval; 9100 9101 ctsio = &io->scsiio; 9102 ctsio->io_hdr.status = CTL_STATUS_NONE; 9103 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9104 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9105 lbalen = (struct ctl_lba_len_flags *) 9106 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9107 lbalen->flags &= ~CTL_LLF_COMPARE; 9108 lbalen->flags |= CTL_LLF_WRITE; 9109 9110 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9111 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9112 return (retval); 9113} 9114 9115int 9116ctl_cnw(struct ctl_scsiio *ctsio) 9117{ 9118 struct ctl_lun *lun; 9119 struct ctl_lba_len_flags *lbalen; 9120 uint64_t lba; 9121 uint32_t num_blocks; 9122 int flags, retval; 9123 9124 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9125 9126 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9127 9128 flags = 0; 9129 retval = CTL_RETVAL_COMPLETE; 9130 9131 switch (ctsio->cdb[0]) { 9132 case COMPARE_AND_WRITE: { 9133 struct scsi_compare_and_write *cdb; 9134 9135 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9136 if (cdb->byte2 & SRW10_FUA) 9137 flags |= CTL_LLF_FUA; 9138 if (cdb->byte2 & SRW10_DPO) 9139 flags |= CTL_LLF_DPO; 9140 lba = scsi_8btou64(cdb->addr); 9141 num_blocks = cdb->length; 9142 break; 9143 } 9144 default: 9145 /* 9146 * We got a command we don't support. This shouldn't 9147 * happen, commands should be filtered out above us. 9148 */ 9149 ctl_set_invalid_opcode(ctsio); 9150 ctl_done((union ctl_io *)ctsio); 9151 9152 return (CTL_RETVAL_COMPLETE); 9153 break; /* NOTREACHED */ 9154 } 9155 9156 /* 9157 * The first check is to make sure we're in bounds, the second 9158 * check is to catch wrap-around problems. If the lba + num blocks 9159 * is less than the lba, then we've wrapped around and the block 9160 * range is invalid anyway. 9161 */ 9162 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9163 || ((lba + num_blocks) < lba)) { 9164 ctl_set_lba_out_of_range(ctsio); 9165 ctl_done((union ctl_io *)ctsio); 9166 return (CTL_RETVAL_COMPLETE); 9167 } 9168 9169 /* 9170 * According to SBC-3, a transfer length of 0 is not an error. 9171 */ 9172 if (num_blocks == 0) { 9173 ctl_set_success(ctsio); 9174 ctl_done((union ctl_io *)ctsio); 9175 return (CTL_RETVAL_COMPLETE); 9176 } 9177 9178 /* Set FUA if write cache is disabled. */ 9179 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9180 SCP_WCE) == 0) 9181 flags |= CTL_LLF_FUA; 9182 9183 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9184 ctsio->kern_rel_offset = 0; 9185 9186 /* 9187 * Set the IO_CONT flag, so that if this I/O gets passed to 9188 * ctl_data_submit_done(), it'll get passed back to 9189 * ctl_ctl_cnw_cont() for further processing. 9190 */ 9191 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9192 ctsio->io_cont = ctl_cnw_cont; 9193 9194 lbalen = (struct ctl_lba_len_flags *) 9195 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9196 lbalen->lba = lba; 9197 lbalen->len = num_blocks; 9198 lbalen->flags = CTL_LLF_COMPARE | flags; 9199 9200 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9201 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9202 return (retval); 9203} 9204 9205int 9206ctl_verify(struct ctl_scsiio *ctsio) 9207{ 9208 struct ctl_lun *lun; 9209 struct ctl_lba_len_flags *lbalen; 9210 uint64_t lba; 9211 uint32_t num_blocks; 9212 int bytchk, flags; 9213 int retval; 9214 9215 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9216 9217 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9218 9219 bytchk = 0; 9220 flags = CTL_LLF_FUA; 9221 retval = CTL_RETVAL_COMPLETE; 9222 9223 switch (ctsio->cdb[0]) { 9224 case VERIFY_10: { 9225 struct scsi_verify_10 *cdb; 9226 9227 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9228 if (cdb->byte2 & SVFY_BYTCHK) 9229 bytchk = 1; 9230 if (cdb->byte2 & SVFY_DPO) 9231 flags |= CTL_LLF_DPO; 9232 lba = scsi_4btoul(cdb->addr); 9233 num_blocks = scsi_2btoul(cdb->length); 9234 break; 9235 } 9236 case VERIFY_12: { 9237 struct scsi_verify_12 *cdb; 9238 9239 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9240 if (cdb->byte2 & SVFY_BYTCHK) 9241 bytchk = 1; 9242 if (cdb->byte2 & SVFY_DPO) 9243 flags |= CTL_LLF_DPO; 9244 lba = scsi_4btoul(cdb->addr); 9245 num_blocks = scsi_4btoul(cdb->length); 9246 break; 9247 } 9248 case VERIFY_16: { 9249 struct scsi_rw_16 *cdb; 9250 9251 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9252 if (cdb->byte2 & SVFY_BYTCHK) 9253 bytchk = 1; 9254 if (cdb->byte2 & SVFY_DPO) 9255 flags |= CTL_LLF_DPO; 9256 lba = scsi_8btou64(cdb->addr); 9257 num_blocks = scsi_4btoul(cdb->length); 9258 break; 9259 } 9260 default: 9261 /* 9262 * We got a command we don't support. This shouldn't 9263 * happen, commands should be filtered out above us. 9264 */ 9265 ctl_set_invalid_opcode(ctsio); 9266 ctl_done((union ctl_io *)ctsio); 9267 return (CTL_RETVAL_COMPLETE); 9268 } 9269 9270 /* 9271 * The first check is to make sure we're in bounds, the second 9272 * check is to catch wrap-around problems. If the lba + num blocks 9273 * is less than the lba, then we've wrapped around and the block 9274 * range is invalid anyway. 9275 */ 9276 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9277 || ((lba + num_blocks) < lba)) { 9278 ctl_set_lba_out_of_range(ctsio); 9279 ctl_done((union ctl_io *)ctsio); 9280 return (CTL_RETVAL_COMPLETE); 9281 } 9282 9283 /* 9284 * According to SBC-3, a transfer length of 0 is not an error. 9285 */ 9286 if (num_blocks == 0) { 9287 ctl_set_success(ctsio); 9288 ctl_done((union ctl_io *)ctsio); 9289 return (CTL_RETVAL_COMPLETE); 9290 } 9291 9292 lbalen = (struct ctl_lba_len_flags *) 9293 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9294 lbalen->lba = lba; 9295 lbalen->len = num_blocks; 9296 if (bytchk) { 9297 lbalen->flags = CTL_LLF_COMPARE | flags; 9298 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9299 } else { 9300 lbalen->flags = CTL_LLF_VERIFY | flags; 9301 ctsio->kern_total_len = 0; 9302 } 9303 ctsio->kern_rel_offset = 0; 9304 9305 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9306 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9307 return (retval); 9308} 9309 9310int 9311ctl_report_luns(struct ctl_scsiio *ctsio) 9312{ 9313 struct scsi_report_luns *cdb; 9314 struct scsi_report_luns_data *lun_data; 9315 struct ctl_lun *lun, *request_lun; 9316 int num_luns, retval; 9317 uint32_t alloc_len, lun_datalen; 9318 int num_filled, well_known; 9319 uint32_t initidx, targ_lun_id, lun_id; 9320 9321 retval = CTL_RETVAL_COMPLETE; 9322 well_known = 0; 9323 9324 cdb = (struct scsi_report_luns *)ctsio->cdb; 9325 9326 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9327 9328 mtx_lock(&control_softc->ctl_lock); 9329 num_luns = control_softc->num_luns; 9330 mtx_unlock(&control_softc->ctl_lock); 9331 9332 switch (cdb->select_report) { 9333 case RPL_REPORT_DEFAULT: 9334 case RPL_REPORT_ALL: 9335 break; 9336 case RPL_REPORT_WELLKNOWN: 9337 well_known = 1; 9338 num_luns = 0; 9339 break; 9340 default: 9341 ctl_set_invalid_field(ctsio, 9342 /*sks_valid*/ 1, 9343 /*command*/ 1, 9344 /*field*/ 2, 9345 /*bit_valid*/ 0, 9346 /*bit*/ 0); 9347 ctl_done((union ctl_io *)ctsio); 9348 return (retval); 9349 break; /* NOTREACHED */ 9350 } 9351 9352 alloc_len = scsi_4btoul(cdb->length); 9353 /* 9354 * The initiator has to allocate at least 16 bytes for this request, 9355 * so he can at least get the header and the first LUN. Otherwise 9356 * we reject the request (per SPC-3 rev 14, section 6.21). 9357 */ 9358 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9359 sizeof(struct scsi_report_luns_lundata))) { 9360 ctl_set_invalid_field(ctsio, 9361 /*sks_valid*/ 1, 9362 /*command*/ 1, 9363 /*field*/ 6, 9364 /*bit_valid*/ 0, 9365 /*bit*/ 0); 9366 ctl_done((union ctl_io *)ctsio); 9367 return (retval); 9368 } 9369 9370 request_lun = (struct ctl_lun *) 9371 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9372 9373 lun_datalen = sizeof(*lun_data) + 9374 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9375 9376 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9377 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9378 ctsio->kern_sg_entries = 0; 9379 9380 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9381 9382 mtx_lock(&control_softc->ctl_lock); 9383 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9384 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9385 if (lun_id >= CTL_MAX_LUNS) 9386 continue; 9387 lun = control_softc->ctl_luns[lun_id]; 9388 if (lun == NULL) 9389 continue; 9390 9391 if (targ_lun_id <= 0xff) { 9392 /* 9393 * Peripheral addressing method, bus number 0. 9394 */ 9395 lun_data->luns[num_filled].lundata[0] = 9396 RPL_LUNDATA_ATYP_PERIPH; 9397 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9398 num_filled++; 9399 } else if (targ_lun_id <= 0x3fff) { 9400 /* 9401 * Flat addressing method. 9402 */ 9403 lun_data->luns[num_filled].lundata[0] = 9404 RPL_LUNDATA_ATYP_FLAT | 9405 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9406#ifdef OLDCTLHEADERS 9407 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9408 (targ_lun_id & SRLD_BUS_LUN_MASK); 9409#endif 9410 lun_data->luns[num_filled].lundata[1] = 9411#ifdef OLDCTLHEADERS 9412 targ_lun_id >> SRLD_BUS_LUN_BITS; 9413#endif 9414 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9415 num_filled++; 9416 } else { 9417 printf("ctl_report_luns: bogus LUN number %jd, " 9418 "skipping\n", (intmax_t)targ_lun_id); 9419 } 9420 /* 9421 * According to SPC-3, rev 14 section 6.21: 9422 * 9423 * "The execution of a REPORT LUNS command to any valid and 9424 * installed logical unit shall clear the REPORTED LUNS DATA 9425 * HAS CHANGED unit attention condition for all logical 9426 * units of that target with respect to the requesting 9427 * initiator. A valid and installed logical unit is one 9428 * having a PERIPHERAL QUALIFIER of 000b in the standard 9429 * INQUIRY data (see 6.4.2)." 9430 * 9431 * If request_lun is NULL, the LUN this report luns command 9432 * was issued to is either disabled or doesn't exist. In that 9433 * case, we shouldn't clear any pending lun change unit 9434 * attention. 9435 */ 9436 if (request_lun != NULL) { 9437 mtx_lock(&lun->lun_lock); 9438 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9439 mtx_unlock(&lun->lun_lock); 9440 } 9441 } 9442 mtx_unlock(&control_softc->ctl_lock); 9443 9444 /* 9445 * It's quite possible that we've returned fewer LUNs than we allocated 9446 * space for. Trim it. 9447 */ 9448 lun_datalen = sizeof(*lun_data) + 9449 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9450 9451 if (lun_datalen < alloc_len) { 9452 ctsio->residual = alloc_len - lun_datalen; 9453 ctsio->kern_data_len = lun_datalen; 9454 ctsio->kern_total_len = lun_datalen; 9455 } else { 9456 ctsio->residual = 0; 9457 ctsio->kern_data_len = alloc_len; 9458 ctsio->kern_total_len = alloc_len; 9459 } 9460 ctsio->kern_data_resid = 0; 9461 ctsio->kern_rel_offset = 0; 9462 ctsio->kern_sg_entries = 0; 9463 9464 /* 9465 * We set this to the actual data length, regardless of how much 9466 * space we actually have to return results. If the user looks at 9467 * this value, he'll know whether or not he allocated enough space 9468 * and reissue the command if necessary. We don't support well 9469 * known logical units, so if the user asks for that, return none. 9470 */ 9471 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9472 9473 /* 9474 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9475 * this request. 9476 */ 9477 ctsio->scsi_status = SCSI_STATUS_OK; 9478 9479 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9480 ctsio->be_move_done = ctl_config_move_done; 9481 ctl_datamove((union ctl_io *)ctsio); 9482 9483 return (retval); 9484} 9485 9486int 9487ctl_request_sense(struct ctl_scsiio *ctsio) 9488{ 9489 struct scsi_request_sense *cdb; 9490 struct scsi_sense_data *sense_ptr; 9491 struct ctl_lun *lun; 9492 uint32_t initidx; 9493 int have_error; 9494 scsi_sense_data_type sense_format; 9495 9496 cdb = (struct scsi_request_sense *)ctsio->cdb; 9497 9498 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9499 9500 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9501 9502 /* 9503 * Determine which sense format the user wants. 9504 */ 9505 if (cdb->byte2 & SRS_DESC) 9506 sense_format = SSD_TYPE_DESC; 9507 else 9508 sense_format = SSD_TYPE_FIXED; 9509 9510 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9511 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9512 ctsio->kern_sg_entries = 0; 9513 9514 /* 9515 * struct scsi_sense_data, which is currently set to 256 bytes, is 9516 * larger than the largest allowed value for the length field in the 9517 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9518 */ 9519 ctsio->residual = 0; 9520 ctsio->kern_data_len = cdb->length; 9521 ctsio->kern_total_len = cdb->length; 9522 9523 ctsio->kern_data_resid = 0; 9524 ctsio->kern_rel_offset = 0; 9525 ctsio->kern_sg_entries = 0; 9526 9527 /* 9528 * If we don't have a LUN, we don't have any pending sense. 9529 */ 9530 if (lun == NULL) 9531 goto no_sense; 9532 9533 have_error = 0; 9534 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9535 /* 9536 * Check for pending sense, and then for pending unit attentions. 9537 * Pending sense gets returned first, then pending unit attentions. 9538 */ 9539 mtx_lock(&lun->lun_lock); 9540#ifdef CTL_WITH_CA 9541 if (ctl_is_set(lun->have_ca, initidx)) { 9542 scsi_sense_data_type stored_format; 9543 9544 /* 9545 * Check to see which sense format was used for the stored 9546 * sense data. 9547 */ 9548 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9549 9550 /* 9551 * If the user requested a different sense format than the 9552 * one we stored, then we need to convert it to the other 9553 * format. If we're going from descriptor to fixed format 9554 * sense data, we may lose things in translation, depending 9555 * on what options were used. 9556 * 9557 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9558 * for some reason we'll just copy it out as-is. 9559 */ 9560 if ((stored_format == SSD_TYPE_FIXED) 9561 && (sense_format == SSD_TYPE_DESC)) 9562 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9563 &lun->pending_sense[initidx], 9564 (struct scsi_sense_data_desc *)sense_ptr); 9565 else if ((stored_format == SSD_TYPE_DESC) 9566 && (sense_format == SSD_TYPE_FIXED)) 9567 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9568 &lun->pending_sense[initidx], 9569 (struct scsi_sense_data_fixed *)sense_ptr); 9570 else 9571 memcpy(sense_ptr, &lun->pending_sense[initidx], 9572 ctl_min(sizeof(*sense_ptr), 9573 sizeof(lun->pending_sense[initidx]))); 9574 9575 ctl_clear_mask(lun->have_ca, initidx); 9576 have_error = 1; 9577 } else 9578#endif 9579 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9580 ctl_ua_type ua_type; 9581 9582 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9583 sense_ptr, sense_format); 9584 if (ua_type != CTL_UA_NONE) 9585 have_error = 1; 9586 } 9587 mtx_unlock(&lun->lun_lock); 9588 9589 /* 9590 * We already have a pending error, return it. 9591 */ 9592 if (have_error != 0) { 9593 /* 9594 * We report the SCSI status as OK, since the status of the 9595 * request sense command itself is OK. 9596 */ 9597 ctsio->scsi_status = SCSI_STATUS_OK; 9598 9599 /* 9600 * We report 0 for the sense length, because we aren't doing 9601 * autosense in this case. We're reporting sense as 9602 * parameter data. 9603 */ 9604 ctsio->sense_len = 0; 9605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9606 ctsio->be_move_done = ctl_config_move_done; 9607 ctl_datamove((union ctl_io *)ctsio); 9608 9609 return (CTL_RETVAL_COMPLETE); 9610 } 9611 9612no_sense: 9613 9614 /* 9615 * No sense information to report, so we report that everything is 9616 * okay. 9617 */ 9618 ctl_set_sense_data(sense_ptr, 9619 lun, 9620 sense_format, 9621 /*current_error*/ 1, 9622 /*sense_key*/ SSD_KEY_NO_SENSE, 9623 /*asc*/ 0x00, 9624 /*ascq*/ 0x00, 9625 SSD_ELEM_NONE); 9626 9627 ctsio->scsi_status = SCSI_STATUS_OK; 9628 9629 /* 9630 * We report 0 for the sense length, because we aren't doing 9631 * autosense in this case. We're reporting sense as parameter data. 9632 */ 9633 ctsio->sense_len = 0; 9634 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9635 ctsio->be_move_done = ctl_config_move_done; 9636 ctl_datamove((union ctl_io *)ctsio); 9637 9638 return (CTL_RETVAL_COMPLETE); 9639} 9640 9641int 9642ctl_tur(struct ctl_scsiio *ctsio) 9643{ 9644 struct ctl_lun *lun; 9645 9646 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9647 9648 CTL_DEBUG_PRINT(("ctl_tur\n")); 9649 9650 if (lun == NULL) 9651 return (EINVAL); 9652 9653 ctsio->scsi_status = SCSI_STATUS_OK; 9654 ctsio->io_hdr.status = CTL_SUCCESS; 9655 9656 ctl_done((union ctl_io *)ctsio); 9657 9658 return (CTL_RETVAL_COMPLETE); 9659} 9660 9661#ifdef notyet 9662static int 9663ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9664{ 9665 9666} 9667#endif 9668 9669static int 9670ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9671{ 9672 struct scsi_vpd_supported_pages *pages; 9673 int sup_page_size; 9674 struct ctl_lun *lun; 9675 9676 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9677 9678 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9679 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9680 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9681 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9682 ctsio->kern_sg_entries = 0; 9683 9684 if (sup_page_size < alloc_len) { 9685 ctsio->residual = alloc_len - sup_page_size; 9686 ctsio->kern_data_len = sup_page_size; 9687 ctsio->kern_total_len = sup_page_size; 9688 } else { 9689 ctsio->residual = 0; 9690 ctsio->kern_data_len = alloc_len; 9691 ctsio->kern_total_len = alloc_len; 9692 } 9693 ctsio->kern_data_resid = 0; 9694 ctsio->kern_rel_offset = 0; 9695 ctsio->kern_sg_entries = 0; 9696 9697 /* 9698 * The control device is always connected. The disk device, on the 9699 * other hand, may not be online all the time. Need to change this 9700 * to figure out whether the disk device is actually online or not. 9701 */ 9702 if (lun != NULL) 9703 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9704 lun->be_lun->lun_type; 9705 else 9706 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9707 9708 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9709 /* Supported VPD pages */ 9710 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9711 /* Serial Number */ 9712 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9713 /* Device Identification */ 9714 pages->page_list[2] = SVPD_DEVICE_ID; 9715 /* Extended INQUIRY Data */ 9716 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9717 /* Mode Page Policy */ 9718 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9719 /* SCSI Ports */ 9720 pages->page_list[5] = SVPD_SCSI_PORTS; 9721 /* Third-party Copy */ 9722 pages->page_list[6] = SVPD_SCSI_TPC; 9723 /* Block limits */ 9724 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9725 /* Block Device Characteristics */ 9726 pages->page_list[8] = SVPD_BDC; 9727 /* Logical Block Provisioning */ 9728 pages->page_list[9] = SVPD_LBP; 9729 9730 ctsio->scsi_status = SCSI_STATUS_OK; 9731 9732 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9733 ctsio->be_move_done = ctl_config_move_done; 9734 ctl_datamove((union ctl_io *)ctsio); 9735 9736 return (CTL_RETVAL_COMPLETE); 9737} 9738 9739static int 9740ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9741{ 9742 struct scsi_vpd_unit_serial_number *sn_ptr; 9743 struct ctl_lun *lun; 9744 int data_len; 9745 9746 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9747 9748 data_len = 4 + CTL_SN_LEN; 9749 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9750 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9751 if (data_len < alloc_len) { 9752 ctsio->residual = alloc_len - data_len; 9753 ctsio->kern_data_len = data_len; 9754 ctsio->kern_total_len = data_len; 9755 } else { 9756 ctsio->residual = 0; 9757 ctsio->kern_data_len = alloc_len; 9758 ctsio->kern_total_len = alloc_len; 9759 } 9760 ctsio->kern_data_resid = 0; 9761 ctsio->kern_rel_offset = 0; 9762 ctsio->kern_sg_entries = 0; 9763 9764 /* 9765 * The control device is always connected. The disk device, on the 9766 * other hand, may not be online all the time. Need to change this 9767 * to figure out whether the disk device is actually online or not. 9768 */ 9769 if (lun != NULL) 9770 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9771 lun->be_lun->lun_type; 9772 else 9773 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9774 9775 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9776 sn_ptr->length = CTL_SN_LEN; 9777 /* 9778 * If we don't have a LUN, we just leave the serial number as 9779 * all spaces. 9780 */ 9781 if (lun != NULL) { 9782 strncpy((char *)sn_ptr->serial_num, 9783 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9784 } else 9785 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9786 ctsio->scsi_status = SCSI_STATUS_OK; 9787 9788 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9789 ctsio->be_move_done = ctl_config_move_done; 9790 ctl_datamove((union ctl_io *)ctsio); 9791 9792 return (CTL_RETVAL_COMPLETE); 9793} 9794 9795 9796static int 9797ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9798{ 9799 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9800 struct ctl_lun *lun; 9801 int data_len; 9802 9803 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9804 9805 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9806 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9807 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9808 ctsio->kern_sg_entries = 0; 9809 9810 if (data_len < alloc_len) { 9811 ctsio->residual = alloc_len - data_len; 9812 ctsio->kern_data_len = data_len; 9813 ctsio->kern_total_len = data_len; 9814 } else { 9815 ctsio->residual = 0; 9816 ctsio->kern_data_len = alloc_len; 9817 ctsio->kern_total_len = alloc_len; 9818 } 9819 ctsio->kern_data_resid = 0; 9820 ctsio->kern_rel_offset = 0; 9821 ctsio->kern_sg_entries = 0; 9822 9823 /* 9824 * The control device is always connected. The disk device, on the 9825 * other hand, may not be online all the time. 9826 */ 9827 if (lun != NULL) 9828 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9829 lun->be_lun->lun_type; 9830 else 9831 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9832 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9833 eid_ptr->page_length = data_len - 4; 9834 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9835 eid_ptr->flags3 = SVPD_EID_V_SUP; 9836 9837 ctsio->scsi_status = SCSI_STATUS_OK; 9838 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9839 ctsio->be_move_done = ctl_config_move_done; 9840 ctl_datamove((union ctl_io *)ctsio); 9841 9842 return (CTL_RETVAL_COMPLETE); 9843} 9844 9845static int 9846ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9847{ 9848 struct scsi_vpd_mode_page_policy *mpp_ptr; 9849 struct ctl_lun *lun; 9850 int data_len; 9851 9852 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9853 9854 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9855 sizeof(struct scsi_vpd_mode_page_policy_descr); 9856 9857 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9858 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9859 ctsio->kern_sg_entries = 0; 9860 9861 if (data_len < alloc_len) { 9862 ctsio->residual = alloc_len - data_len; 9863 ctsio->kern_data_len = data_len; 9864 ctsio->kern_total_len = data_len; 9865 } else { 9866 ctsio->residual = 0; 9867 ctsio->kern_data_len = alloc_len; 9868 ctsio->kern_total_len = alloc_len; 9869 } 9870 ctsio->kern_data_resid = 0; 9871 ctsio->kern_rel_offset = 0; 9872 ctsio->kern_sg_entries = 0; 9873 9874 /* 9875 * The control device is always connected. The disk device, on the 9876 * other hand, may not be online all the time. 9877 */ 9878 if (lun != NULL) 9879 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9880 lun->be_lun->lun_type; 9881 else 9882 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9883 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9884 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9885 mpp_ptr->descr[0].page_code = 0x3f; 9886 mpp_ptr->descr[0].subpage_code = 0xff; 9887 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9888 9889 ctsio->scsi_status = SCSI_STATUS_OK; 9890 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9891 ctsio->be_move_done = ctl_config_move_done; 9892 ctl_datamove((union ctl_io *)ctsio); 9893 9894 return (CTL_RETVAL_COMPLETE); 9895} 9896 9897static int 9898ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9899{ 9900 struct scsi_vpd_device_id *devid_ptr; 9901 struct scsi_vpd_id_descriptor *desc; 9902 struct ctl_softc *ctl_softc; 9903 struct ctl_lun *lun; 9904 struct ctl_port *port; 9905 int data_len; 9906 uint8_t proto; 9907 9908 ctl_softc = control_softc; 9909 9910 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9911 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9912 9913 data_len = sizeof(struct scsi_vpd_device_id) + 9914 sizeof(struct scsi_vpd_id_descriptor) + 9915 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9916 sizeof(struct scsi_vpd_id_descriptor) + 9917 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9918 if (lun && lun->lun_devid) 9919 data_len += lun->lun_devid->len; 9920 if (port->port_devid) 9921 data_len += port->port_devid->len; 9922 if (port->target_devid) 9923 data_len += port->target_devid->len; 9924 9925 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9926 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9927 ctsio->kern_sg_entries = 0; 9928 9929 if (data_len < alloc_len) { 9930 ctsio->residual = alloc_len - data_len; 9931 ctsio->kern_data_len = data_len; 9932 ctsio->kern_total_len = data_len; 9933 } else { 9934 ctsio->residual = 0; 9935 ctsio->kern_data_len = alloc_len; 9936 ctsio->kern_total_len = alloc_len; 9937 } 9938 ctsio->kern_data_resid = 0; 9939 ctsio->kern_rel_offset = 0; 9940 ctsio->kern_sg_entries = 0; 9941 9942 /* 9943 * The control device is always connected. The disk device, on the 9944 * other hand, may not be online all the time. 9945 */ 9946 if (lun != NULL) 9947 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9948 lun->be_lun->lun_type; 9949 else 9950 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9951 devid_ptr->page_code = SVPD_DEVICE_ID; 9952 scsi_ulto2b(data_len - 4, devid_ptr->length); 9953 9954 if (port->port_type == CTL_PORT_FC) 9955 proto = SCSI_PROTO_FC << 4; 9956 else if (port->port_type == CTL_PORT_ISCSI) 9957 proto = SCSI_PROTO_ISCSI << 4; 9958 else 9959 proto = SCSI_PROTO_SPI << 4; 9960 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9961 9962 /* 9963 * We're using a LUN association here. i.e., this device ID is a 9964 * per-LUN identifier. 9965 */ 9966 if (lun && lun->lun_devid) { 9967 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9968 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9969 lun->lun_devid->len); 9970 } 9971 9972 /* 9973 * This is for the WWPN which is a port association. 9974 */ 9975 if (port->port_devid) { 9976 memcpy(desc, port->port_devid->data, port->port_devid->len); 9977 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9978 port->port_devid->len); 9979 } 9980 9981 /* 9982 * This is for the Relative Target Port(type 4h) identifier 9983 */ 9984 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9985 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9986 SVPD_ID_TYPE_RELTARG; 9987 desc->length = 4; 9988 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9989 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9990 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9991 9992 /* 9993 * This is for the Target Port Group(type 5h) identifier 9994 */ 9995 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9996 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9997 SVPD_ID_TYPE_TPORTGRP; 9998 desc->length = 4; 9999 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10000 &desc->identifier[2]); 10001 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10002 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10003 10004 /* 10005 * This is for the Target identifier 10006 */ 10007 if (port->target_devid) { 10008 memcpy(desc, port->target_devid->data, port->target_devid->len); 10009 } 10010 10011 ctsio->scsi_status = SCSI_STATUS_OK; 10012 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10013 ctsio->be_move_done = ctl_config_move_done; 10014 ctl_datamove((union ctl_io *)ctsio); 10015 10016 return (CTL_RETVAL_COMPLETE); 10017} 10018 10019static int 10020ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10021{ 10022 struct ctl_softc *softc = control_softc; 10023 struct scsi_vpd_scsi_ports *sp; 10024 struct scsi_vpd_port_designation *pd; 10025 struct scsi_vpd_port_designation_cont *pdc; 10026 struct ctl_lun *lun; 10027 struct ctl_port *port; 10028 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10029 int num_target_port_groups, single; 10030 10031 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10032 10033 single = ctl_is_single; 10034 if (single) 10035 num_target_port_groups = 1; 10036 else 10037 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10038 num_target_ports = 0; 10039 iid_len = 0; 10040 id_len = 0; 10041 mtx_lock(&softc->ctl_lock); 10042 STAILQ_FOREACH(port, &softc->port_list, links) { 10043 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10044 continue; 10045 if (lun != NULL && 10046 ctl_map_lun_back(port->targ_port, lun->lun) >= 10047 CTL_MAX_LUNS) 10048 continue; 10049 num_target_ports++; 10050 if (port->init_devid) 10051 iid_len += port->init_devid->len; 10052 if (port->port_devid) 10053 id_len += port->port_devid->len; 10054 } 10055 mtx_unlock(&softc->ctl_lock); 10056 10057 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10058 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10059 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10060 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10061 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10062 ctsio->kern_sg_entries = 0; 10063 10064 if (data_len < alloc_len) { 10065 ctsio->residual = alloc_len - data_len; 10066 ctsio->kern_data_len = data_len; 10067 ctsio->kern_total_len = data_len; 10068 } else { 10069 ctsio->residual = 0; 10070 ctsio->kern_data_len = alloc_len; 10071 ctsio->kern_total_len = alloc_len; 10072 } 10073 ctsio->kern_data_resid = 0; 10074 ctsio->kern_rel_offset = 0; 10075 ctsio->kern_sg_entries = 0; 10076 10077 /* 10078 * The control device is always connected. The disk device, on the 10079 * other hand, may not be online all the time. Need to change this 10080 * to figure out whether the disk device is actually online or not. 10081 */ 10082 if (lun != NULL) 10083 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10084 lun->be_lun->lun_type; 10085 else 10086 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10087 10088 sp->page_code = SVPD_SCSI_PORTS; 10089 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10090 sp->page_length); 10091 pd = &sp->design[0]; 10092 10093 mtx_lock(&softc->ctl_lock); 10094 if (softc->flags & CTL_FLAG_MASTER_SHELF) 10095 pg = 0; 10096 else 10097 pg = 1; 10098 for (g = 0; g < num_target_port_groups; g++) { 10099 STAILQ_FOREACH(port, &softc->port_list, links) { 10100 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10101 continue; 10102 if (lun != NULL && 10103 ctl_map_lun_back(port->targ_port, lun->lun) >= 10104 CTL_MAX_LUNS) 10105 continue; 10106 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10107 scsi_ulto2b(p, pd->relative_port_id); 10108 if (port->init_devid && g == pg) { 10109 iid_len = port->init_devid->len; 10110 memcpy(pd->initiator_transportid, 10111 port->init_devid->data, port->init_devid->len); 10112 } else 10113 iid_len = 0; 10114 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10115 pdc = (struct scsi_vpd_port_designation_cont *) 10116 (&pd->initiator_transportid[iid_len]); 10117 if (port->port_devid && g == pg) { 10118 id_len = port->port_devid->len; 10119 memcpy(pdc->target_port_descriptors, 10120 port->port_devid->data, port->port_devid->len); 10121 } else 10122 id_len = 0; 10123 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10124 pd = (struct scsi_vpd_port_designation *) 10125 ((uint8_t *)pdc->target_port_descriptors + id_len); 10126 } 10127 } 10128 mtx_unlock(&softc->ctl_lock); 10129 10130 ctsio->scsi_status = SCSI_STATUS_OK; 10131 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10132 ctsio->be_move_done = ctl_config_move_done; 10133 ctl_datamove((union ctl_io *)ctsio); 10134 10135 return (CTL_RETVAL_COMPLETE); 10136} 10137 10138static int 10139ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10140{ 10141 struct scsi_vpd_block_limits *bl_ptr; 10142 struct ctl_lun *lun; 10143 int bs; 10144 10145 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10146 10147 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10148 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10149 ctsio->kern_sg_entries = 0; 10150 10151 if (sizeof(*bl_ptr) < alloc_len) { 10152 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10153 ctsio->kern_data_len = sizeof(*bl_ptr); 10154 ctsio->kern_total_len = sizeof(*bl_ptr); 10155 } else { 10156 ctsio->residual = 0; 10157 ctsio->kern_data_len = alloc_len; 10158 ctsio->kern_total_len = alloc_len; 10159 } 10160 ctsio->kern_data_resid = 0; 10161 ctsio->kern_rel_offset = 0; 10162 ctsio->kern_sg_entries = 0; 10163 10164 /* 10165 * The control device is always connected. The disk device, on the 10166 * other hand, may not be online all the time. Need to change this 10167 * to figure out whether the disk device is actually online or not. 10168 */ 10169 if (lun != NULL) 10170 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10171 lun->be_lun->lun_type; 10172 else 10173 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10174 10175 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10176 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10177 bl_ptr->max_cmp_write_len = 0xff; 10178 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10179 if (lun != NULL) { 10180 bs = lun->be_lun->blocksize; 10181 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10182 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10183 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10184 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10185 if (lun->be_lun->pblockexp != 0) { 10186 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10187 bl_ptr->opt_unmap_grain); 10188 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10189 bl_ptr->unmap_grain_align); 10190 } 10191 } 10192 scsi_ulto4b(lun->be_lun->atomicblock, 10193 bl_ptr->max_atomic_transfer_length); 10194 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10195 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10196 } 10197 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10198 10199 ctsio->scsi_status = SCSI_STATUS_OK; 10200 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10201 ctsio->be_move_done = ctl_config_move_done; 10202 ctl_datamove((union ctl_io *)ctsio); 10203 10204 return (CTL_RETVAL_COMPLETE); 10205} 10206 10207static int 10208ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10209{ 10210 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10211 struct ctl_lun *lun; 10212 10213 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10214 10215 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10216 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10217 ctsio->kern_sg_entries = 0; 10218 10219 if (sizeof(*bdc_ptr) < alloc_len) { 10220 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10221 ctsio->kern_data_len = sizeof(*bdc_ptr); 10222 ctsio->kern_total_len = sizeof(*bdc_ptr); 10223 } else { 10224 ctsio->residual = 0; 10225 ctsio->kern_data_len = alloc_len; 10226 ctsio->kern_total_len = alloc_len; 10227 } 10228 ctsio->kern_data_resid = 0; 10229 ctsio->kern_rel_offset = 0; 10230 ctsio->kern_sg_entries = 0; 10231 10232 /* 10233 * The control device is always connected. The disk device, on the 10234 * other hand, may not be online all the time. Need to change this 10235 * to figure out whether the disk device is actually online or not. 10236 */ 10237 if (lun != NULL) 10238 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10239 lun->be_lun->lun_type; 10240 else 10241 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10242 bdc_ptr->page_code = SVPD_BDC; 10243 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10244 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate); 10245 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10246 10247 ctsio->scsi_status = SCSI_STATUS_OK; 10248 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10249 ctsio->be_move_done = ctl_config_move_done; 10250 ctl_datamove((union ctl_io *)ctsio); 10251 10252 return (CTL_RETVAL_COMPLETE); 10253} 10254 10255static int 10256ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10257{ 10258 struct scsi_vpd_logical_block_prov *lbp_ptr; 10259 struct ctl_lun *lun; 10260 10261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10262 10263 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10264 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10265 ctsio->kern_sg_entries = 0; 10266 10267 if (sizeof(*lbp_ptr) < alloc_len) { 10268 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10269 ctsio->kern_data_len = sizeof(*lbp_ptr); 10270 ctsio->kern_total_len = sizeof(*lbp_ptr); 10271 } else { 10272 ctsio->residual = 0; 10273 ctsio->kern_data_len = alloc_len; 10274 ctsio->kern_total_len = alloc_len; 10275 } 10276 ctsio->kern_data_resid = 0; 10277 ctsio->kern_rel_offset = 0; 10278 ctsio->kern_sg_entries = 0; 10279 10280 /* 10281 * The control device is always connected. The disk device, on the 10282 * other hand, may not be online all the time. Need to change this 10283 * to figure out whether the disk device is actually online or not. 10284 */ 10285 if (lun != NULL) 10286 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10287 lun->be_lun->lun_type; 10288 else 10289 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10290 10291 lbp_ptr->page_code = SVPD_LBP; 10292 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10293 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10294 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10295 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10296 lbp_ptr->prov_type = SVPD_LBP_RESOURCE; 10297 } 10298 10299 ctsio->scsi_status = SCSI_STATUS_OK; 10300 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10301 ctsio->be_move_done = ctl_config_move_done; 10302 ctl_datamove((union ctl_io *)ctsio); 10303 10304 return (CTL_RETVAL_COMPLETE); 10305} 10306 10307static int 10308ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10309{ 10310 struct scsi_inquiry *cdb; 10311 struct ctl_lun *lun; 10312 int alloc_len, retval; 10313 10314 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10315 cdb = (struct scsi_inquiry *)ctsio->cdb; 10316 10317 retval = CTL_RETVAL_COMPLETE; 10318 10319 alloc_len = scsi_2btoul(cdb->length); 10320 10321 switch (cdb->page_code) { 10322 case SVPD_SUPPORTED_PAGES: 10323 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10324 break; 10325 case SVPD_UNIT_SERIAL_NUMBER: 10326 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10327 break; 10328 case SVPD_DEVICE_ID: 10329 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10330 break; 10331 case SVPD_EXTENDED_INQUIRY_DATA: 10332 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10333 break; 10334 case SVPD_MODE_PAGE_POLICY: 10335 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10336 break; 10337 case SVPD_SCSI_PORTS: 10338 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10339 break; 10340 case SVPD_SCSI_TPC: 10341 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10342 break; 10343 case SVPD_BLOCK_LIMITS: 10344 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10345 break; 10346 case SVPD_BDC: 10347 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10348 break; 10349 case SVPD_LBP: 10350 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10351 break; 10352 default: 10353 ctl_set_invalid_field(ctsio, 10354 /*sks_valid*/ 1, 10355 /*command*/ 1, 10356 /*field*/ 2, 10357 /*bit_valid*/ 0, 10358 /*bit*/ 0); 10359 ctl_done((union ctl_io *)ctsio); 10360 retval = CTL_RETVAL_COMPLETE; 10361 break; 10362 } 10363 10364 return (retval); 10365} 10366 10367static int 10368ctl_inquiry_std(struct ctl_scsiio *ctsio) 10369{ 10370 struct scsi_inquiry_data *inq_ptr; 10371 struct scsi_inquiry *cdb; 10372 struct ctl_softc *ctl_softc; 10373 struct ctl_lun *lun; 10374 char *val; 10375 uint32_t alloc_len, data_len; 10376 ctl_port_type port_type; 10377 10378 ctl_softc = control_softc; 10379 10380 /* 10381 * Figure out whether we're talking to a Fibre Channel port or not. 10382 * We treat the ioctl front end, and any SCSI adapters, as packetized 10383 * SCSI front ends. 10384 */ 10385 port_type = ctl_softc->ctl_ports[ 10386 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10387 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10388 port_type = CTL_PORT_SCSI; 10389 10390 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10391 cdb = (struct scsi_inquiry *)ctsio->cdb; 10392 alloc_len = scsi_2btoul(cdb->length); 10393 10394 /* 10395 * We malloc the full inquiry data size here and fill it 10396 * in. If the user only asks for less, we'll give him 10397 * that much. 10398 */ 10399 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10400 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10401 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10402 ctsio->kern_sg_entries = 0; 10403 ctsio->kern_data_resid = 0; 10404 ctsio->kern_rel_offset = 0; 10405 10406 if (data_len < alloc_len) { 10407 ctsio->residual = alloc_len - data_len; 10408 ctsio->kern_data_len = data_len; 10409 ctsio->kern_total_len = data_len; 10410 } else { 10411 ctsio->residual = 0; 10412 ctsio->kern_data_len = alloc_len; 10413 ctsio->kern_total_len = alloc_len; 10414 } 10415 10416 /* 10417 * If we have a LUN configured, report it as connected. Otherwise, 10418 * report that it is offline or no device is supported, depending 10419 * on the value of inquiry_pq_no_lun. 10420 * 10421 * According to the spec (SPC-4 r34), the peripheral qualifier 10422 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10423 * 10424 * "A peripheral device having the specified peripheral device type 10425 * is not connected to this logical unit. However, the device 10426 * server is capable of supporting the specified peripheral device 10427 * type on this logical unit." 10428 * 10429 * According to the same spec, the peripheral qualifier 10430 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10431 * 10432 * "The device server is not capable of supporting a peripheral 10433 * device on this logical unit. For this peripheral qualifier the 10434 * peripheral device type shall be set to 1Fh. All other peripheral 10435 * device type values are reserved for this peripheral qualifier." 10436 * 10437 * Given the text, it would seem that we probably want to report that 10438 * the LUN is offline here. There is no LUN connected, but we can 10439 * support a LUN at the given LUN number. 10440 * 10441 * In the real world, though, it sounds like things are a little 10442 * different: 10443 * 10444 * - Linux, when presented with a LUN with the offline peripheral 10445 * qualifier, will create an sg driver instance for it. So when 10446 * you attach it to CTL, you wind up with a ton of sg driver 10447 * instances. (One for every LUN that Linux bothered to probe.) 10448 * Linux does this despite the fact that it issues a REPORT LUNs 10449 * to LUN 0 to get the inventory of supported LUNs. 10450 * 10451 * - There is other anecdotal evidence (from Emulex folks) about 10452 * arrays that use the offline peripheral qualifier for LUNs that 10453 * are on the "passive" path in an active/passive array. 10454 * 10455 * So the solution is provide a hopefully reasonable default 10456 * (return bad/no LUN) and allow the user to change the behavior 10457 * with a tunable/sysctl variable. 10458 */ 10459 if (lun != NULL) 10460 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10461 lun->be_lun->lun_type; 10462 else if (ctl_softc->inquiry_pq_no_lun == 0) 10463 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10464 else 10465 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10466 10467 /* RMB in byte 2 is 0 */ 10468 inq_ptr->version = SCSI_REV_SPC4; 10469 10470 /* 10471 * According to SAM-3, even if a device only supports a single 10472 * level of LUN addressing, it should still set the HISUP bit: 10473 * 10474 * 4.9.1 Logical unit numbers overview 10475 * 10476 * All logical unit number formats described in this standard are 10477 * hierarchical in structure even when only a single level in that 10478 * hierarchy is used. The HISUP bit shall be set to one in the 10479 * standard INQUIRY data (see SPC-2) when any logical unit number 10480 * format described in this standard is used. Non-hierarchical 10481 * formats are outside the scope of this standard. 10482 * 10483 * Therefore we set the HiSup bit here. 10484 * 10485 * The reponse format is 2, per SPC-3. 10486 */ 10487 inq_ptr->response_format = SID_HiSup | 2; 10488 10489 inq_ptr->additional_length = data_len - 10490 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10491 CTL_DEBUG_PRINT(("additional_length = %d\n", 10492 inq_ptr->additional_length)); 10493 10494 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10495 /* 16 bit addressing */ 10496 if (port_type == CTL_PORT_SCSI) 10497 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10498 /* XXX set the SID_MultiP bit here if we're actually going to 10499 respond on multiple ports */ 10500 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10501 10502 /* 16 bit data bus, synchronous transfers */ 10503 if (port_type == CTL_PORT_SCSI) 10504 inq_ptr->flags = SID_WBus16 | SID_Sync; 10505 /* 10506 * XXX KDM do we want to support tagged queueing on the control 10507 * device at all? 10508 */ 10509 if ((lun == NULL) 10510 || (lun->be_lun->lun_type != T_PROCESSOR)) 10511 inq_ptr->flags |= SID_CmdQue; 10512 /* 10513 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10514 * We have 8 bytes for the vendor name, and 16 bytes for the device 10515 * name and 4 bytes for the revision. 10516 */ 10517 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10518 "vendor")) == NULL) { 10519 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10520 } else { 10521 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10522 strncpy(inq_ptr->vendor, val, 10523 min(sizeof(inq_ptr->vendor), strlen(val))); 10524 } 10525 if (lun == NULL) { 10526 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10527 sizeof(inq_ptr->product)); 10528 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10529 switch (lun->be_lun->lun_type) { 10530 case T_DIRECT: 10531 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10532 sizeof(inq_ptr->product)); 10533 break; 10534 case T_PROCESSOR: 10535 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10536 sizeof(inq_ptr->product)); 10537 break; 10538 default: 10539 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10540 sizeof(inq_ptr->product)); 10541 break; 10542 } 10543 } else { 10544 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10545 strncpy(inq_ptr->product, val, 10546 min(sizeof(inq_ptr->product), strlen(val))); 10547 } 10548 10549 /* 10550 * XXX make this a macro somewhere so it automatically gets 10551 * incremented when we make changes. 10552 */ 10553 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10554 "revision")) == NULL) { 10555 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10556 } else { 10557 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10558 strncpy(inq_ptr->revision, val, 10559 min(sizeof(inq_ptr->revision), strlen(val))); 10560 } 10561 10562 /* 10563 * For parallel SCSI, we support double transition and single 10564 * transition clocking. We also support QAS (Quick Arbitration 10565 * and Selection) and Information Unit transfers on both the 10566 * control and array devices. 10567 */ 10568 if (port_type == CTL_PORT_SCSI) 10569 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10570 SID_SPI_IUS; 10571 10572 /* SAM-5 (no version claimed) */ 10573 scsi_ulto2b(0x00A0, inq_ptr->version1); 10574 /* SPC-4 (no version claimed) */ 10575 scsi_ulto2b(0x0460, inq_ptr->version2); 10576 if (port_type == CTL_PORT_FC) { 10577 /* FCP-2 ANSI INCITS.350:2003 */ 10578 scsi_ulto2b(0x0917, inq_ptr->version3); 10579 } else if (port_type == CTL_PORT_SCSI) { 10580 /* SPI-4 ANSI INCITS.362:200x */ 10581 scsi_ulto2b(0x0B56, inq_ptr->version3); 10582 } else if (port_type == CTL_PORT_ISCSI) { 10583 /* iSCSI (no version claimed) */ 10584 scsi_ulto2b(0x0960, inq_ptr->version3); 10585 } else if (port_type == CTL_PORT_SAS) { 10586 /* SAS (no version claimed) */ 10587 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10588 } 10589 10590 if (lun == NULL) { 10591 /* SBC-4 (no version claimed) */ 10592 scsi_ulto2b(0x0600, inq_ptr->version4); 10593 } else { 10594 switch (lun->be_lun->lun_type) { 10595 case T_DIRECT: 10596 /* SBC-4 (no version claimed) */ 10597 scsi_ulto2b(0x0600, inq_ptr->version4); 10598 break; 10599 case T_PROCESSOR: 10600 default: 10601 break; 10602 } 10603 } 10604 10605 ctsio->scsi_status = SCSI_STATUS_OK; 10606 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10607 ctsio->be_move_done = ctl_config_move_done; 10608 ctl_datamove((union ctl_io *)ctsio); 10609 return (CTL_RETVAL_COMPLETE); 10610} 10611 10612int 10613ctl_inquiry(struct ctl_scsiio *ctsio) 10614{ 10615 struct scsi_inquiry *cdb; 10616 int retval; 10617 10618 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10619 10620 cdb = (struct scsi_inquiry *)ctsio->cdb; 10621 if (cdb->byte2 & SI_EVPD) 10622 retval = ctl_inquiry_evpd(ctsio); 10623 else if (cdb->page_code == 0) 10624 retval = ctl_inquiry_std(ctsio); 10625 else { 10626 ctl_set_invalid_field(ctsio, 10627 /*sks_valid*/ 1, 10628 /*command*/ 1, 10629 /*field*/ 2, 10630 /*bit_valid*/ 0, 10631 /*bit*/ 0); 10632 ctl_done((union ctl_io *)ctsio); 10633 return (CTL_RETVAL_COMPLETE); 10634 } 10635 10636 return (retval); 10637} 10638 10639/* 10640 * For known CDB types, parse the LBA and length. 10641 */ 10642static int 10643ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10644{ 10645 if (io->io_hdr.io_type != CTL_IO_SCSI) 10646 return (1); 10647 10648 switch (io->scsiio.cdb[0]) { 10649 case COMPARE_AND_WRITE: { 10650 struct scsi_compare_and_write *cdb; 10651 10652 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10653 10654 *lba = scsi_8btou64(cdb->addr); 10655 *len = cdb->length; 10656 break; 10657 } 10658 case READ_6: 10659 case WRITE_6: { 10660 struct scsi_rw_6 *cdb; 10661 10662 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10663 10664 *lba = scsi_3btoul(cdb->addr); 10665 /* only 5 bits are valid in the most significant address byte */ 10666 *lba &= 0x1fffff; 10667 *len = cdb->length; 10668 break; 10669 } 10670 case READ_10: 10671 case WRITE_10: { 10672 struct scsi_rw_10 *cdb; 10673 10674 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10675 10676 *lba = scsi_4btoul(cdb->addr); 10677 *len = scsi_2btoul(cdb->length); 10678 break; 10679 } 10680 case WRITE_VERIFY_10: { 10681 struct scsi_write_verify_10 *cdb; 10682 10683 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10684 10685 *lba = scsi_4btoul(cdb->addr); 10686 *len = scsi_2btoul(cdb->length); 10687 break; 10688 } 10689 case READ_12: 10690 case WRITE_12: { 10691 struct scsi_rw_12 *cdb; 10692 10693 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10694 10695 *lba = scsi_4btoul(cdb->addr); 10696 *len = scsi_4btoul(cdb->length); 10697 break; 10698 } 10699 case WRITE_VERIFY_12: { 10700 struct scsi_write_verify_12 *cdb; 10701 10702 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10703 10704 *lba = scsi_4btoul(cdb->addr); 10705 *len = scsi_4btoul(cdb->length); 10706 break; 10707 } 10708 case READ_16: 10709 case WRITE_16: 10710 case WRITE_ATOMIC_16: { 10711 struct scsi_rw_16 *cdb; 10712 10713 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10714 10715 *lba = scsi_8btou64(cdb->addr); 10716 *len = scsi_4btoul(cdb->length); 10717 break; 10718 } 10719 case WRITE_VERIFY_16: { 10720 struct scsi_write_verify_16 *cdb; 10721 10722 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10723 10724 *lba = scsi_8btou64(cdb->addr); 10725 *len = scsi_4btoul(cdb->length); 10726 break; 10727 } 10728 case WRITE_SAME_10: { 10729 struct scsi_write_same_10 *cdb; 10730 10731 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10732 10733 *lba = scsi_4btoul(cdb->addr); 10734 *len = scsi_2btoul(cdb->length); 10735 break; 10736 } 10737 case WRITE_SAME_16: { 10738 struct scsi_write_same_16 *cdb; 10739 10740 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10741 10742 *lba = scsi_8btou64(cdb->addr); 10743 *len = scsi_4btoul(cdb->length); 10744 break; 10745 } 10746 case VERIFY_10: { 10747 struct scsi_verify_10 *cdb; 10748 10749 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10750 10751 *lba = scsi_4btoul(cdb->addr); 10752 *len = scsi_2btoul(cdb->length); 10753 break; 10754 } 10755 case VERIFY_12: { 10756 struct scsi_verify_12 *cdb; 10757 10758 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10759 10760 *lba = scsi_4btoul(cdb->addr); 10761 *len = scsi_4btoul(cdb->length); 10762 break; 10763 } 10764 case VERIFY_16: { 10765 struct scsi_verify_16 *cdb; 10766 10767 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10768 10769 *lba = scsi_8btou64(cdb->addr); 10770 *len = scsi_4btoul(cdb->length); 10771 break; 10772 } 10773 case UNMAP: { 10774 *lba = 0; 10775 *len = UINT64_MAX; 10776 break; 10777 } 10778 default: 10779 return (1); 10780 break; /* NOTREACHED */ 10781 } 10782 10783 return (0); 10784} 10785 10786static ctl_action 10787ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10788{ 10789 uint64_t endlba1, endlba2; 10790 10791 endlba1 = lba1 + len1 - 1; 10792 endlba2 = lba2 + len2 - 1; 10793 10794 if ((endlba1 < lba2) 10795 || (endlba2 < lba1)) 10796 return (CTL_ACTION_PASS); 10797 else 10798 return (CTL_ACTION_BLOCK); 10799} 10800 10801static int 10802ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10803{ 10804 struct ctl_ptr_len_flags *ptrlen; 10805 struct scsi_unmap_desc *buf, *end, *range; 10806 uint64_t lba; 10807 uint32_t len; 10808 10809 /* If not UNMAP -- go other way. */ 10810 if (io->io_hdr.io_type != CTL_IO_SCSI || 10811 io->scsiio.cdb[0] != UNMAP) 10812 return (CTL_ACTION_ERROR); 10813 10814 /* If UNMAP without data -- block and wait for data. */ 10815 ptrlen = (struct ctl_ptr_len_flags *) 10816 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10817 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10818 ptrlen->ptr == NULL) 10819 return (CTL_ACTION_BLOCK); 10820 10821 /* UNMAP with data -- check for collision. */ 10822 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10823 end = buf + ptrlen->len / sizeof(*buf); 10824 for (range = buf; range < end; range++) { 10825 lba = scsi_8btou64(range->lba); 10826 len = scsi_4btoul(range->length); 10827 if ((lba < lba2 + len2) && (lba + len > lba2)) 10828 return (CTL_ACTION_BLOCK); 10829 } 10830 return (CTL_ACTION_PASS); 10831} 10832 10833static ctl_action 10834ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10835{ 10836 uint64_t lba1, lba2; 10837 uint64_t len1, len2; 10838 int retval; 10839 10840 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10841 return (CTL_ACTION_ERROR); 10842 10843 retval = ctl_extent_check_unmap(io2, lba1, len1); 10844 if (retval != CTL_ACTION_ERROR) 10845 return (retval); 10846 10847 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10848 return (CTL_ACTION_ERROR); 10849 10850 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10851} 10852 10853static ctl_action 10854ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10855 union ctl_io *ooa_io) 10856{ 10857 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10858 ctl_serialize_action *serialize_row; 10859 10860 /* 10861 * The initiator attempted multiple untagged commands at the same 10862 * time. Can't do that. 10863 */ 10864 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10865 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10866 && ((pending_io->io_hdr.nexus.targ_port == 10867 ooa_io->io_hdr.nexus.targ_port) 10868 && (pending_io->io_hdr.nexus.initid.id == 10869 ooa_io->io_hdr.nexus.initid.id)) 10870 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10871 return (CTL_ACTION_OVERLAP); 10872 10873 /* 10874 * The initiator attempted to send multiple tagged commands with 10875 * the same ID. (It's fine if different initiators have the same 10876 * tag ID.) 10877 * 10878 * Even if all of those conditions are true, we don't kill the I/O 10879 * if the command ahead of us has been aborted. We won't end up 10880 * sending it to the FETD, and it's perfectly legal to resend a 10881 * command with the same tag number as long as the previous 10882 * instance of this tag number has been aborted somehow. 10883 */ 10884 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10885 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10886 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10887 && ((pending_io->io_hdr.nexus.targ_port == 10888 ooa_io->io_hdr.nexus.targ_port) 10889 && (pending_io->io_hdr.nexus.initid.id == 10890 ooa_io->io_hdr.nexus.initid.id)) 10891 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10892 return (CTL_ACTION_OVERLAP_TAG); 10893 10894 /* 10895 * If we get a head of queue tag, SAM-3 says that we should 10896 * immediately execute it. 10897 * 10898 * What happens if this command would normally block for some other 10899 * reason? e.g. a request sense with a head of queue tag 10900 * immediately after a write. Normally that would block, but this 10901 * will result in its getting executed immediately... 10902 * 10903 * We currently return "pass" instead of "skip", so we'll end up 10904 * going through the rest of the queue to check for overlapped tags. 10905 * 10906 * XXX KDM check for other types of blockage first?? 10907 */ 10908 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10909 return (CTL_ACTION_PASS); 10910 10911 /* 10912 * Ordered tags have to block until all items ahead of them 10913 * have completed. If we get called with an ordered tag, we always 10914 * block, if something else is ahead of us in the queue. 10915 */ 10916 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10917 return (CTL_ACTION_BLOCK); 10918 10919 /* 10920 * Simple tags get blocked until all head of queue and ordered tags 10921 * ahead of them have completed. I'm lumping untagged commands in 10922 * with simple tags here. XXX KDM is that the right thing to do? 10923 */ 10924 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10925 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10926 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10927 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10928 return (CTL_ACTION_BLOCK); 10929 10930 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10931 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10932 10933 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10934 10935 switch (serialize_row[pending_entry->seridx]) { 10936 case CTL_SER_BLOCK: 10937 return (CTL_ACTION_BLOCK); 10938 case CTL_SER_EXTENT: 10939 return (ctl_extent_check(pending_io, ooa_io)); 10940 case CTL_SER_EXTENTOPT: 10941 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10942 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10943 return (ctl_extent_check(pending_io, ooa_io)); 10944 /* FALLTHROUGH */ 10945 case CTL_SER_PASS: 10946 return (CTL_ACTION_PASS); 10947 case CTL_SER_BLOCKOPT: 10948 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10949 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10950 return (CTL_ACTION_BLOCK); 10951 return (CTL_ACTION_PASS); 10952 case CTL_SER_SKIP: 10953 return (CTL_ACTION_SKIP); 10954 default: 10955 panic("invalid serialization value %d", 10956 serialize_row[pending_entry->seridx]); 10957 } 10958 10959 return (CTL_ACTION_ERROR); 10960} 10961 10962/* 10963 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10964 * Assumptions: 10965 * - pending_io is generally either incoming, or on the blocked queue 10966 * - starting I/O is the I/O we want to start the check with. 10967 */ 10968static ctl_action 10969ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10970 union ctl_io *starting_io) 10971{ 10972 union ctl_io *ooa_io; 10973 ctl_action action; 10974 10975 mtx_assert(&lun->lun_lock, MA_OWNED); 10976 10977 /* 10978 * Run back along the OOA queue, starting with the current 10979 * blocked I/O and going through every I/O before it on the 10980 * queue. If starting_io is NULL, we'll just end up returning 10981 * CTL_ACTION_PASS. 10982 */ 10983 for (ooa_io = starting_io; ooa_io != NULL; 10984 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10985 ooa_links)){ 10986 10987 /* 10988 * This routine just checks to see whether 10989 * cur_blocked is blocked by ooa_io, which is ahead 10990 * of it in the queue. It doesn't queue/dequeue 10991 * cur_blocked. 10992 */ 10993 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10994 switch (action) { 10995 case CTL_ACTION_BLOCK: 10996 case CTL_ACTION_OVERLAP: 10997 case CTL_ACTION_OVERLAP_TAG: 10998 case CTL_ACTION_SKIP: 10999 case CTL_ACTION_ERROR: 11000 return (action); 11001 break; /* NOTREACHED */ 11002 case CTL_ACTION_PASS: 11003 break; 11004 default: 11005 panic("invalid action %d", action); 11006 break; /* NOTREACHED */ 11007 } 11008 } 11009 11010 return (CTL_ACTION_PASS); 11011} 11012 11013/* 11014 * Assumptions: 11015 * - An I/O has just completed, and has been removed from the per-LUN OOA 11016 * queue, so some items on the blocked queue may now be unblocked. 11017 */ 11018static int 11019ctl_check_blocked(struct ctl_lun *lun) 11020{ 11021 union ctl_io *cur_blocked, *next_blocked; 11022 11023 mtx_assert(&lun->lun_lock, MA_OWNED); 11024 11025 /* 11026 * Run forward from the head of the blocked queue, checking each 11027 * entry against the I/Os prior to it on the OOA queue to see if 11028 * there is still any blockage. 11029 * 11030 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11031 * with our removing a variable on it while it is traversing the 11032 * list. 11033 */ 11034 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11035 cur_blocked != NULL; cur_blocked = next_blocked) { 11036 union ctl_io *prev_ooa; 11037 ctl_action action; 11038 11039 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11040 blocked_links); 11041 11042 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11043 ctl_ooaq, ooa_links); 11044 11045 /* 11046 * If cur_blocked happens to be the first item in the OOA 11047 * queue now, prev_ooa will be NULL, and the action 11048 * returned will just be CTL_ACTION_PASS. 11049 */ 11050 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11051 11052 switch (action) { 11053 case CTL_ACTION_BLOCK: 11054 /* Nothing to do here, still blocked */ 11055 break; 11056 case CTL_ACTION_OVERLAP: 11057 case CTL_ACTION_OVERLAP_TAG: 11058 /* 11059 * This shouldn't happen! In theory we've already 11060 * checked this command for overlap... 11061 */ 11062 break; 11063 case CTL_ACTION_PASS: 11064 case CTL_ACTION_SKIP: { 11065 struct ctl_softc *softc; 11066 const struct ctl_cmd_entry *entry; 11067 uint32_t initidx; 11068 int isc_retval; 11069 11070 /* 11071 * The skip case shouldn't happen, this transaction 11072 * should have never made it onto the blocked queue. 11073 */ 11074 /* 11075 * This I/O is no longer blocked, we can remove it 11076 * from the blocked queue. Since this is a TAILQ 11077 * (doubly linked list), we can do O(1) removals 11078 * from any place on the list. 11079 */ 11080 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11081 blocked_links); 11082 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11083 11084 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11085 /* 11086 * Need to send IO back to original side to 11087 * run 11088 */ 11089 union ctl_ha_msg msg_info; 11090 11091 msg_info.hdr.original_sc = 11092 cur_blocked->io_hdr.original_sc; 11093 msg_info.hdr.serializing_sc = cur_blocked; 11094 msg_info.hdr.msg_type = CTL_MSG_R2R; 11095 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11096 &msg_info, sizeof(msg_info), 0)) > 11097 CTL_HA_STATUS_SUCCESS) { 11098 printf("CTL:Check Blocked error from " 11099 "ctl_ha_msg_send %d\n", 11100 isc_retval); 11101 } 11102 break; 11103 } 11104 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11105 softc = control_softc; 11106 11107 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 11108 11109 /* 11110 * Check this I/O for LUN state changes that may 11111 * have happened while this command was blocked. 11112 * The LUN state may have been changed by a command 11113 * ahead of us in the queue, so we need to re-check 11114 * for any states that can be caused by SCSI 11115 * commands. 11116 */ 11117 if (ctl_scsiio_lun_check(softc, lun, entry, 11118 &cur_blocked->scsiio) == 0) { 11119 cur_blocked->io_hdr.flags |= 11120 CTL_FLAG_IS_WAS_ON_RTR; 11121 ctl_enqueue_rtr(cur_blocked); 11122 } else 11123 ctl_done(cur_blocked); 11124 break; 11125 } 11126 default: 11127 /* 11128 * This probably shouldn't happen -- we shouldn't 11129 * get CTL_ACTION_ERROR, or anything else. 11130 */ 11131 break; 11132 } 11133 } 11134 11135 return (CTL_RETVAL_COMPLETE); 11136} 11137 11138/* 11139 * This routine (with one exception) checks LUN flags that can be set by 11140 * commands ahead of us in the OOA queue. These flags have to be checked 11141 * when a command initially comes in, and when we pull a command off the 11142 * blocked queue and are preparing to execute it. The reason we have to 11143 * check these flags for commands on the blocked queue is that the LUN 11144 * state may have been changed by a command ahead of us while we're on the 11145 * blocked queue. 11146 * 11147 * Ordering is somewhat important with these checks, so please pay 11148 * careful attention to the placement of any new checks. 11149 */ 11150static int 11151ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 11152 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11153{ 11154 int retval; 11155 uint32_t residx; 11156 11157 retval = 0; 11158 11159 mtx_assert(&lun->lun_lock, MA_OWNED); 11160 11161 /* 11162 * If this shelf is a secondary shelf controller, we have to reject 11163 * any media access commands. 11164 */ 11165#if 0 11166 /* No longer needed for HA */ 11167 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 11168 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 11169 ctl_set_lun_standby(ctsio); 11170 retval = 1; 11171 goto bailout; 11172 } 11173#endif 11174 11175 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11176 if (lun->flags & CTL_LUN_READONLY) { 11177 ctl_set_sense(ctsio, /*current_error*/ 1, 11178 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11179 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11180 retval = 1; 11181 goto bailout; 11182 } 11183 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11184 .eca_and_aen & SCP_SWP) != 0) { 11185 ctl_set_sense(ctsio, /*current_error*/ 1, 11186 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11187 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11188 retval = 1; 11189 goto bailout; 11190 } 11191 } 11192 11193 /* 11194 * Check for a reservation conflict. If this command isn't allowed 11195 * even on reserved LUNs, and if this initiator isn't the one who 11196 * reserved us, reject the command with a reservation conflict. 11197 */ 11198 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11199 if ((lun->flags & CTL_LUN_RESERVED) 11200 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11201 if (lun->res_idx != residx) { 11202 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11203 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11204 retval = 1; 11205 goto bailout; 11206 } 11207 } 11208 11209 if ((lun->flags & CTL_LUN_PR_RESERVED) 11210 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 11211 /* 11212 * if we aren't registered or it's a res holder type 11213 * reservation and this isn't the res holder then set a 11214 * conflict. 11215 * NOTE: Commands which might be allowed on write exclusive 11216 * type reservations are checked in the particular command 11217 * for a conflict. Read and SSU are the only ones. 11218 */ 11219 if (lun->pr_keys[residx] == 0 11220 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11221 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 11222 ctsio->io_hdr.status = CTL_SCSI_ERROR; 11223 retval = 1; 11224 goto bailout; 11225 } 11226 11227 } 11228 11229 if ((lun->flags & CTL_LUN_OFFLINE) 11230 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11231 ctl_set_lun_not_ready(ctsio); 11232 retval = 1; 11233 goto bailout; 11234 } 11235 11236 /* 11237 * If the LUN is stopped, see if this particular command is allowed 11238 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11239 */ 11240 if ((lun->flags & CTL_LUN_STOPPED) 11241 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11242 /* "Logical unit not ready, initializing cmd. required" */ 11243 ctl_set_lun_stopped(ctsio); 11244 retval = 1; 11245 goto bailout; 11246 } 11247 11248 if ((lun->flags & CTL_LUN_INOPERABLE) 11249 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11250 /* "Medium format corrupted" */ 11251 ctl_set_medium_format_corrupted(ctsio); 11252 retval = 1; 11253 goto bailout; 11254 } 11255 11256bailout: 11257 return (retval); 11258 11259} 11260 11261static void 11262ctl_failover_io(union ctl_io *io, int have_lock) 11263{ 11264 ctl_set_busy(&io->scsiio); 11265 ctl_done(io); 11266} 11267 11268static void 11269ctl_failover(void) 11270{ 11271 struct ctl_lun *lun; 11272 struct ctl_softc *ctl_softc; 11273 union ctl_io *next_io, *pending_io; 11274 union ctl_io *io; 11275 int lun_idx; 11276 int i; 11277 11278 ctl_softc = control_softc; 11279 11280 mtx_lock(&ctl_softc->ctl_lock); 11281 /* 11282 * Remove any cmds from the other SC from the rtr queue. These 11283 * will obviously only be for LUNs for which we're the primary. 11284 * We can't send status or get/send data for these commands. 11285 * Since they haven't been executed yet, we can just remove them. 11286 * We'll either abort them or delete them below, depending on 11287 * which HA mode we're in. 11288 */ 11289#ifdef notyet 11290 mtx_lock(&ctl_softc->queue_lock); 11291 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11292 io != NULL; io = next_io) { 11293 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11294 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11295 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11296 ctl_io_hdr, links); 11297 } 11298 mtx_unlock(&ctl_softc->queue_lock); 11299#endif 11300 11301 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11302 lun = ctl_softc->ctl_luns[lun_idx]; 11303 if (lun==NULL) 11304 continue; 11305 11306 /* 11307 * Processor LUNs are primary on both sides. 11308 * XXX will this always be true? 11309 */ 11310 if (lun->be_lun->lun_type == T_PROCESSOR) 11311 continue; 11312 11313 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11314 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11315 printf("FAILOVER: primary lun %d\n", lun_idx); 11316 /* 11317 * Remove all commands from the other SC. First from the 11318 * blocked queue then from the ooa queue. Once we have 11319 * removed them. Call ctl_check_blocked to see if there 11320 * is anything that can run. 11321 */ 11322 for (io = (union ctl_io *)TAILQ_FIRST( 11323 &lun->blocked_queue); io != NULL; io = next_io) { 11324 11325 next_io = (union ctl_io *)TAILQ_NEXT( 11326 &io->io_hdr, blocked_links); 11327 11328 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11329 TAILQ_REMOVE(&lun->blocked_queue, 11330 &io->io_hdr,blocked_links); 11331 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11332 TAILQ_REMOVE(&lun->ooa_queue, 11333 &io->io_hdr, ooa_links); 11334 11335 ctl_free_io(io); 11336 } 11337 } 11338 11339 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11340 io != NULL; io = next_io) { 11341 11342 next_io = (union ctl_io *)TAILQ_NEXT( 11343 &io->io_hdr, ooa_links); 11344 11345 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11346 11347 TAILQ_REMOVE(&lun->ooa_queue, 11348 &io->io_hdr, 11349 ooa_links); 11350 11351 ctl_free_io(io); 11352 } 11353 } 11354 ctl_check_blocked(lun); 11355 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11356 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11357 11358 printf("FAILOVER: primary lun %d\n", lun_idx); 11359 /* 11360 * Abort all commands from the other SC. We can't 11361 * send status back for them now. These should get 11362 * cleaned up when they are completed or come out 11363 * for a datamove operation. 11364 */ 11365 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11366 io != NULL; io = next_io) { 11367 next_io = (union ctl_io *)TAILQ_NEXT( 11368 &io->io_hdr, ooa_links); 11369 11370 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11371 io->io_hdr.flags |= CTL_FLAG_ABORT; 11372 } 11373 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11374 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11375 11376 printf("FAILOVER: secondary lun %d\n", lun_idx); 11377 11378 lun->flags |= CTL_LUN_PRIMARY_SC; 11379 11380 /* 11381 * We send all I/O that was sent to this controller 11382 * and redirected to the other side back with 11383 * busy status, and have the initiator retry it. 11384 * Figuring out how much data has been transferred, 11385 * etc. and picking up where we left off would be 11386 * very tricky. 11387 * 11388 * XXX KDM need to remove I/O from the blocked 11389 * queue as well! 11390 */ 11391 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11392 &lun->ooa_queue); pending_io != NULL; 11393 pending_io = next_io) { 11394 11395 next_io = (union ctl_io *)TAILQ_NEXT( 11396 &pending_io->io_hdr, ooa_links); 11397 11398 pending_io->io_hdr.flags &= 11399 ~CTL_FLAG_SENT_2OTHER_SC; 11400 11401 if (pending_io->io_hdr.flags & 11402 CTL_FLAG_IO_ACTIVE) { 11403 pending_io->io_hdr.flags |= 11404 CTL_FLAG_FAILOVER; 11405 } else { 11406 ctl_set_busy(&pending_io->scsiio); 11407 ctl_done(pending_io); 11408 } 11409 } 11410 11411 /* 11412 * Build Unit Attention 11413 */ 11414 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11415 lun->pending_ua[i] |= 11416 CTL_UA_ASYM_ACC_CHANGE; 11417 } 11418 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11419 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11420 printf("FAILOVER: secondary lun %d\n", lun_idx); 11421 /* 11422 * if the first io on the OOA is not on the RtR queue 11423 * add it. 11424 */ 11425 lun->flags |= CTL_LUN_PRIMARY_SC; 11426 11427 pending_io = (union ctl_io *)TAILQ_FIRST( 11428 &lun->ooa_queue); 11429 if (pending_io==NULL) { 11430 printf("Nothing on OOA queue\n"); 11431 continue; 11432 } 11433 11434 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11435 if ((pending_io->io_hdr.flags & 11436 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11437 pending_io->io_hdr.flags |= 11438 CTL_FLAG_IS_WAS_ON_RTR; 11439 ctl_enqueue_rtr(pending_io); 11440 } 11441#if 0 11442 else 11443 { 11444 printf("Tag 0x%04x is running\n", 11445 pending_io->scsiio.tag_num); 11446 } 11447#endif 11448 11449 next_io = (union ctl_io *)TAILQ_NEXT( 11450 &pending_io->io_hdr, ooa_links); 11451 for (pending_io=next_io; pending_io != NULL; 11452 pending_io = next_io) { 11453 pending_io->io_hdr.flags &= 11454 ~CTL_FLAG_SENT_2OTHER_SC; 11455 next_io = (union ctl_io *)TAILQ_NEXT( 11456 &pending_io->io_hdr, ooa_links); 11457 if (pending_io->io_hdr.flags & 11458 CTL_FLAG_IS_WAS_ON_RTR) { 11459#if 0 11460 printf("Tag 0x%04x is running\n", 11461 pending_io->scsiio.tag_num); 11462#endif 11463 continue; 11464 } 11465 11466 switch (ctl_check_ooa(lun, pending_io, 11467 (union ctl_io *)TAILQ_PREV( 11468 &pending_io->io_hdr, ctl_ooaq, 11469 ooa_links))) { 11470 11471 case CTL_ACTION_BLOCK: 11472 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11473 &pending_io->io_hdr, 11474 blocked_links); 11475 pending_io->io_hdr.flags |= 11476 CTL_FLAG_BLOCKED; 11477 break; 11478 case CTL_ACTION_PASS: 11479 case CTL_ACTION_SKIP: 11480 pending_io->io_hdr.flags |= 11481 CTL_FLAG_IS_WAS_ON_RTR; 11482 ctl_enqueue_rtr(pending_io); 11483 break; 11484 case CTL_ACTION_OVERLAP: 11485 ctl_set_overlapped_cmd( 11486 (struct ctl_scsiio *)pending_io); 11487 ctl_done(pending_io); 11488 break; 11489 case CTL_ACTION_OVERLAP_TAG: 11490 ctl_set_overlapped_tag( 11491 (struct ctl_scsiio *)pending_io, 11492 pending_io->scsiio.tag_num & 0xff); 11493 ctl_done(pending_io); 11494 break; 11495 case CTL_ACTION_ERROR: 11496 default: 11497 ctl_set_internal_failure( 11498 (struct ctl_scsiio *)pending_io, 11499 0, // sks_valid 11500 0); //retry count 11501 ctl_done(pending_io); 11502 break; 11503 } 11504 } 11505 11506 /* 11507 * Build Unit Attention 11508 */ 11509 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11510 lun->pending_ua[i] |= 11511 CTL_UA_ASYM_ACC_CHANGE; 11512 } 11513 } else { 11514 panic("Unhandled HA mode failover, LUN flags = %#x, " 11515 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11516 } 11517 } 11518 ctl_pause_rtr = 0; 11519 mtx_unlock(&ctl_softc->ctl_lock); 11520} 11521 11522static int 11523ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11524{ 11525 struct ctl_lun *lun; 11526 const struct ctl_cmd_entry *entry; 11527 uint32_t initidx, targ_lun; 11528 int retval; 11529 11530 retval = 0; 11531 11532 lun = NULL; 11533 11534 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11535 if ((targ_lun < CTL_MAX_LUNS) 11536 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 11537 lun = ctl_softc->ctl_luns[targ_lun]; 11538 /* 11539 * If the LUN is invalid, pretend that it doesn't exist. 11540 * It will go away as soon as all pending I/O has been 11541 * completed. 11542 */ 11543 if (lun->flags & CTL_LUN_DISABLED) { 11544 lun = NULL; 11545 } else { 11546 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11547 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11548 lun->be_lun; 11549 if (lun->be_lun->lun_type == T_PROCESSOR) { 11550 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11551 } 11552 11553 /* 11554 * Every I/O goes into the OOA queue for a 11555 * particular LUN, and stays there until completion. 11556 */ 11557 mtx_lock(&lun->lun_lock); 11558 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11559 ooa_links); 11560 } 11561 } else { 11562 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11563 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11564 } 11565 11566 /* Get command entry and return error if it is unsuppotyed. */ 11567 entry = ctl_validate_command(ctsio); 11568 if (entry == NULL) { 11569 if (lun) 11570 mtx_unlock(&lun->lun_lock); 11571 return (retval); 11572 } 11573 11574 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11575 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11576 11577 /* 11578 * Check to see whether we can send this command to LUNs that don't 11579 * exist. This should pretty much only be the case for inquiry 11580 * and request sense. Further checks, below, really require having 11581 * a LUN, so we can't really check the command anymore. Just put 11582 * it on the rtr queue. 11583 */ 11584 if (lun == NULL) { 11585 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11586 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11587 ctl_enqueue_rtr((union ctl_io *)ctsio); 11588 return (retval); 11589 } 11590 11591 ctl_set_unsupported_lun(ctsio); 11592 ctl_done((union ctl_io *)ctsio); 11593 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11594 return (retval); 11595 } else { 11596 /* 11597 * Make sure we support this particular command on this LUN. 11598 * e.g., we don't support writes to the control LUN. 11599 */ 11600 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11601 mtx_unlock(&lun->lun_lock); 11602 ctl_set_invalid_opcode(ctsio); 11603 ctl_done((union ctl_io *)ctsio); 11604 return (retval); 11605 } 11606 } 11607 11608 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11609 11610#ifdef CTL_WITH_CA 11611 /* 11612 * If we've got a request sense, it'll clear the contingent 11613 * allegiance condition. Otherwise, if we have a CA condition for 11614 * this initiator, clear it, because it sent down a command other 11615 * than request sense. 11616 */ 11617 if ((ctsio->cdb[0] != REQUEST_SENSE) 11618 && (ctl_is_set(lun->have_ca, initidx))) 11619 ctl_clear_mask(lun->have_ca, initidx); 11620#endif 11621 11622 /* 11623 * If the command has this flag set, it handles its own unit 11624 * attention reporting, we shouldn't do anything. Otherwise we 11625 * check for any pending unit attentions, and send them back to the 11626 * initiator. We only do this when a command initially comes in, 11627 * not when we pull it off the blocked queue. 11628 * 11629 * According to SAM-3, section 5.3.2, the order that things get 11630 * presented back to the host is basically unit attentions caused 11631 * by some sort of reset event, busy status, reservation conflicts 11632 * or task set full, and finally any other status. 11633 * 11634 * One issue here is that some of the unit attentions we report 11635 * don't fall into the "reset" category (e.g. "reported luns data 11636 * has changed"). So reporting it here, before the reservation 11637 * check, may be technically wrong. I guess the only thing to do 11638 * would be to check for and report the reset events here, and then 11639 * check for the other unit attention types after we check for a 11640 * reservation conflict. 11641 * 11642 * XXX KDM need to fix this 11643 */ 11644 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11645 ctl_ua_type ua_type; 11646 11647 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11648 scsi_sense_data_type sense_format; 11649 11650 if (lun != NULL) 11651 sense_format = (lun->flags & 11652 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11653 SSD_TYPE_FIXED; 11654 else 11655 sense_format = SSD_TYPE_FIXED; 11656 11657 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11658 &ctsio->sense_data, sense_format); 11659 if (ua_type != CTL_UA_NONE) { 11660 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11661 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11662 CTL_AUTOSENSE; 11663 ctsio->sense_len = SSD_FULL_SIZE; 11664 mtx_unlock(&lun->lun_lock); 11665 ctl_done((union ctl_io *)ctsio); 11666 return (retval); 11667 } 11668 } 11669 } 11670 11671 11672 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11673 mtx_unlock(&lun->lun_lock); 11674 ctl_done((union ctl_io *)ctsio); 11675 return (retval); 11676 } 11677 11678 /* 11679 * XXX CHD this is where we want to send IO to other side if 11680 * this LUN is secondary on this SC. We will need to make a copy 11681 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11682 * the copy we send as FROM_OTHER. 11683 * We also need to stuff the address of the original IO so we can 11684 * find it easily. Something similar will need be done on the other 11685 * side so when we are done we can find the copy. 11686 */ 11687 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11688 union ctl_ha_msg msg_info; 11689 int isc_retval; 11690 11691 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11692 11693 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11694 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11695#if 0 11696 printf("1. ctsio %p\n", ctsio); 11697#endif 11698 msg_info.hdr.serializing_sc = NULL; 11699 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11700 msg_info.scsi.tag_num = ctsio->tag_num; 11701 msg_info.scsi.tag_type = ctsio->tag_type; 11702 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11703 11704 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11705 11706 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11707 (void *)&msg_info, sizeof(msg_info), 0)) > 11708 CTL_HA_STATUS_SUCCESS) { 11709 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11710 isc_retval); 11711 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11712 } else { 11713#if 0 11714 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11715#endif 11716 } 11717 11718 /* 11719 * XXX KDM this I/O is off the incoming queue, but hasn't 11720 * been inserted on any other queue. We may need to come 11721 * up with a holding queue while we wait for serialization 11722 * so that we have an idea of what we're waiting for from 11723 * the other side. 11724 */ 11725 mtx_unlock(&lun->lun_lock); 11726 return (retval); 11727 } 11728 11729 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11730 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11731 ctl_ooaq, ooa_links))) { 11732 case CTL_ACTION_BLOCK: 11733 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11734 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11735 blocked_links); 11736 mtx_unlock(&lun->lun_lock); 11737 return (retval); 11738 case CTL_ACTION_PASS: 11739 case CTL_ACTION_SKIP: 11740 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11741 mtx_unlock(&lun->lun_lock); 11742 ctl_enqueue_rtr((union ctl_io *)ctsio); 11743 break; 11744 case CTL_ACTION_OVERLAP: 11745 mtx_unlock(&lun->lun_lock); 11746 ctl_set_overlapped_cmd(ctsio); 11747 ctl_done((union ctl_io *)ctsio); 11748 break; 11749 case CTL_ACTION_OVERLAP_TAG: 11750 mtx_unlock(&lun->lun_lock); 11751 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11752 ctl_done((union ctl_io *)ctsio); 11753 break; 11754 case CTL_ACTION_ERROR: 11755 default: 11756 mtx_unlock(&lun->lun_lock); 11757 ctl_set_internal_failure(ctsio, 11758 /*sks_valid*/ 0, 11759 /*retry_count*/ 0); 11760 ctl_done((union ctl_io *)ctsio); 11761 break; 11762 } 11763 return (retval); 11764} 11765 11766const struct ctl_cmd_entry * 11767ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11768{ 11769 const struct ctl_cmd_entry *entry; 11770 int service_action; 11771 11772 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11773 if (sa) 11774 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11775 if (entry->flags & CTL_CMD_FLAG_SA5) { 11776 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11777 entry = &((const struct ctl_cmd_entry *) 11778 entry->execute)[service_action]; 11779 } 11780 return (entry); 11781} 11782 11783const struct ctl_cmd_entry * 11784ctl_validate_command(struct ctl_scsiio *ctsio) 11785{ 11786 const struct ctl_cmd_entry *entry; 11787 int i, sa; 11788 uint8_t diff; 11789 11790 entry = ctl_get_cmd_entry(ctsio, &sa); 11791 if (entry->execute == NULL) { 11792 if (sa) 11793 ctl_set_invalid_field(ctsio, 11794 /*sks_valid*/ 1, 11795 /*command*/ 1, 11796 /*field*/ 1, 11797 /*bit_valid*/ 1, 11798 /*bit*/ 4); 11799 else 11800 ctl_set_invalid_opcode(ctsio); 11801 ctl_done((union ctl_io *)ctsio); 11802 return (NULL); 11803 } 11804 KASSERT(entry->length > 0, 11805 ("Not defined length for command 0x%02x/0x%02x", 11806 ctsio->cdb[0], ctsio->cdb[1])); 11807 for (i = 1; i < entry->length; i++) { 11808 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11809 if (diff == 0) 11810 continue; 11811 ctl_set_invalid_field(ctsio, 11812 /*sks_valid*/ 1, 11813 /*command*/ 1, 11814 /*field*/ i, 11815 /*bit_valid*/ 1, 11816 /*bit*/ fls(diff) - 1); 11817 ctl_done((union ctl_io *)ctsio); 11818 return (NULL); 11819 } 11820 return (entry); 11821} 11822 11823static int 11824ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11825{ 11826 11827 switch (lun_type) { 11828 case T_PROCESSOR: 11829 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11830 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11831 return (0); 11832 break; 11833 case T_DIRECT: 11834 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11835 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11836 return (0); 11837 break; 11838 default: 11839 return (0); 11840 } 11841 return (1); 11842} 11843 11844static int 11845ctl_scsiio(struct ctl_scsiio *ctsio) 11846{ 11847 int retval; 11848 const struct ctl_cmd_entry *entry; 11849 11850 retval = CTL_RETVAL_COMPLETE; 11851 11852 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11853 11854 entry = ctl_get_cmd_entry(ctsio, NULL); 11855 11856 /* 11857 * If this I/O has been aborted, just send it straight to 11858 * ctl_done() without executing it. 11859 */ 11860 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11861 ctl_done((union ctl_io *)ctsio); 11862 goto bailout; 11863 } 11864 11865 /* 11866 * All the checks should have been handled by ctl_scsiio_precheck(). 11867 * We should be clear now to just execute the I/O. 11868 */ 11869 retval = entry->execute(ctsio); 11870 11871bailout: 11872 return (retval); 11873} 11874 11875/* 11876 * Since we only implement one target right now, a bus reset simply resets 11877 * our single target. 11878 */ 11879static int 11880ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11881{ 11882 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11883} 11884 11885static int 11886ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11887 ctl_ua_type ua_type) 11888{ 11889 struct ctl_lun *lun; 11890 int retval; 11891 11892 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11893 union ctl_ha_msg msg_info; 11894 11895 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11896 msg_info.hdr.nexus = io->io_hdr.nexus; 11897 if (ua_type==CTL_UA_TARG_RESET) 11898 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11899 else 11900 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11901 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11902 msg_info.hdr.original_sc = NULL; 11903 msg_info.hdr.serializing_sc = NULL; 11904 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11905 (void *)&msg_info, sizeof(msg_info), 0)) { 11906 } 11907 } 11908 retval = 0; 11909 11910 mtx_lock(&ctl_softc->ctl_lock); 11911 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11912 retval += ctl_lun_reset(lun, io, ua_type); 11913 mtx_unlock(&ctl_softc->ctl_lock); 11914 11915 return (retval); 11916} 11917 11918/* 11919 * The LUN should always be set. The I/O is optional, and is used to 11920 * distinguish between I/Os sent by this initiator, and by other 11921 * initiators. We set unit attention for initiators other than this one. 11922 * SAM-3 is vague on this point. It does say that a unit attention should 11923 * be established for other initiators when a LUN is reset (see section 11924 * 5.7.3), but it doesn't specifically say that the unit attention should 11925 * be established for this particular initiator when a LUN is reset. Here 11926 * is the relevant text, from SAM-3 rev 8: 11927 * 11928 * 5.7.2 When a SCSI initiator port aborts its own tasks 11929 * 11930 * When a SCSI initiator port causes its own task(s) to be aborted, no 11931 * notification that the task(s) have been aborted shall be returned to 11932 * the SCSI initiator port other than the completion response for the 11933 * command or task management function action that caused the task(s) to 11934 * be aborted and notification(s) associated with related effects of the 11935 * action (e.g., a reset unit attention condition). 11936 * 11937 * XXX KDM for now, we're setting unit attention for all initiators. 11938 */ 11939static int 11940ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11941{ 11942 union ctl_io *xio; 11943#if 0 11944 uint32_t initindex; 11945#endif 11946 int i; 11947 11948 mtx_lock(&lun->lun_lock); 11949 /* 11950 * Run through the OOA queue and abort each I/O. 11951 */ 11952#if 0 11953 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11954#endif 11955 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11956 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11957 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11958 } 11959 11960 /* 11961 * This version sets unit attention for every 11962 */ 11963#if 0 11964 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11965 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11966 if (initindex == i) 11967 continue; 11968 lun->pending_ua[i] |= ua_type; 11969 } 11970#endif 11971 11972 /* 11973 * A reset (any kind, really) clears reservations established with 11974 * RESERVE/RELEASE. It does not clear reservations established 11975 * with PERSISTENT RESERVE OUT, but we don't support that at the 11976 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11977 * reservations made with the RESERVE/RELEASE commands, because 11978 * those commands are obsolete in SPC-3. 11979 */ 11980 lun->flags &= ~CTL_LUN_RESERVED; 11981 11982 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11983#ifdef CTL_WITH_CA 11984 ctl_clear_mask(lun->have_ca, i); 11985#endif 11986 lun->pending_ua[i] |= ua_type; 11987 } 11988 mtx_unlock(&lun->lun_lock); 11989 11990 return (0); 11991} 11992 11993static void 11994ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11995 int other_sc) 11996{ 11997 union ctl_io *xio; 11998 11999 mtx_assert(&lun->lun_lock, MA_OWNED); 12000 12001 /* 12002 * Run through the OOA queue and attempt to find the given I/O. 12003 * The target port, initiator ID, tag type and tag number have to 12004 * match the values that we got from the initiator. If we have an 12005 * untagged command to abort, simply abort the first untagged command 12006 * we come to. We only allow one untagged command at a time of course. 12007 */ 12008 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12009 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12010 12011 if ((targ_port == UINT32_MAX || 12012 targ_port == xio->io_hdr.nexus.targ_port) && 12013 (init_id == UINT32_MAX || 12014 init_id == xio->io_hdr.nexus.initid.id)) { 12015 if (targ_port != xio->io_hdr.nexus.targ_port || 12016 init_id != xio->io_hdr.nexus.initid.id) 12017 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12018 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12019 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12020 union ctl_ha_msg msg_info; 12021 12022 msg_info.hdr.nexus = xio->io_hdr.nexus; 12023 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12024 msg_info.task.tag_num = xio->scsiio.tag_num; 12025 msg_info.task.tag_type = xio->scsiio.tag_type; 12026 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12027 msg_info.hdr.original_sc = NULL; 12028 msg_info.hdr.serializing_sc = NULL; 12029 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12030 (void *)&msg_info, sizeof(msg_info), 0); 12031 } 12032 } 12033 } 12034} 12035 12036static int 12037ctl_abort_task_set(union ctl_io *io) 12038{ 12039 struct ctl_softc *softc = control_softc; 12040 struct ctl_lun *lun; 12041 uint32_t targ_lun; 12042 12043 /* 12044 * Look up the LUN. 12045 */ 12046 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12047 mtx_lock(&softc->ctl_lock); 12048 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12049 lun = softc->ctl_luns[targ_lun]; 12050 else { 12051 mtx_unlock(&softc->ctl_lock); 12052 return (1); 12053 } 12054 12055 mtx_lock(&lun->lun_lock); 12056 mtx_unlock(&softc->ctl_lock); 12057 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12058 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12059 io->io_hdr.nexus.initid.id, 12060 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12061 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12062 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12063 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12064 } 12065 mtx_unlock(&lun->lun_lock); 12066 return (0); 12067} 12068 12069static int 12070ctl_i_t_nexus_reset(union ctl_io *io) 12071{ 12072 struct ctl_softc *softc = control_softc; 12073 struct ctl_lun *lun; 12074 uint32_t initindex, residx; 12075 12076 initindex = ctl_get_initindex(&io->io_hdr.nexus); 12077 residx = ctl_get_resindex(&io->io_hdr.nexus); 12078 mtx_lock(&softc->ctl_lock); 12079 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12080 mtx_lock(&lun->lun_lock); 12081 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12082 io->io_hdr.nexus.initid.id, 12083 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12084#ifdef CTL_WITH_CA 12085 ctl_clear_mask(lun->have_ca, initindex); 12086#endif 12087 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12088 lun->flags &= ~CTL_LUN_RESERVED; 12089 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS; 12090 mtx_unlock(&lun->lun_lock); 12091 } 12092 mtx_unlock(&softc->ctl_lock); 12093 return (0); 12094} 12095 12096static int 12097ctl_abort_task(union ctl_io *io) 12098{ 12099 union ctl_io *xio; 12100 struct ctl_lun *lun; 12101 struct ctl_softc *ctl_softc; 12102#if 0 12103 struct sbuf sb; 12104 char printbuf[128]; 12105#endif 12106 int found; 12107 uint32_t targ_lun; 12108 12109 ctl_softc = control_softc; 12110 found = 0; 12111 12112 /* 12113 * Look up the LUN. 12114 */ 12115 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12116 mtx_lock(&ctl_softc->ctl_lock); 12117 if ((targ_lun < CTL_MAX_LUNS) 12118 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12119 lun = ctl_softc->ctl_luns[targ_lun]; 12120 else { 12121 mtx_unlock(&ctl_softc->ctl_lock); 12122 return (1); 12123 } 12124 12125#if 0 12126 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12127 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12128#endif 12129 12130 mtx_lock(&lun->lun_lock); 12131 mtx_unlock(&ctl_softc->ctl_lock); 12132 /* 12133 * Run through the OOA queue and attempt to find the given I/O. 12134 * The target port, initiator ID, tag type and tag number have to 12135 * match the values that we got from the initiator. If we have an 12136 * untagged command to abort, simply abort the first untagged command 12137 * we come to. We only allow one untagged command at a time of course. 12138 */ 12139#if 0 12140 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 12141#endif 12142 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12143 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12144#if 0 12145 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12146 12147 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12148 lun->lun, xio->scsiio.tag_num, 12149 xio->scsiio.tag_type, 12150 (xio->io_hdr.blocked_links.tqe_prev 12151 == NULL) ? "" : " BLOCKED", 12152 (xio->io_hdr.flags & 12153 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12154 (xio->io_hdr.flags & 12155 CTL_FLAG_ABORT) ? " ABORT" : "", 12156 (xio->io_hdr.flags & 12157 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12158 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12159 sbuf_finish(&sb); 12160 printf("%s\n", sbuf_data(&sb)); 12161#endif 12162 12163 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 12164 && (xio->io_hdr.nexus.initid.id == 12165 io->io_hdr.nexus.initid.id)) { 12166 /* 12167 * If the abort says that the task is untagged, the 12168 * task in the queue must be untagged. Otherwise, 12169 * we just check to see whether the tag numbers 12170 * match. This is because the QLogic firmware 12171 * doesn't pass back the tag type in an abort 12172 * request. 12173 */ 12174#if 0 12175 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12176 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12177 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12178#endif 12179 /* 12180 * XXX KDM we've got problems with FC, because it 12181 * doesn't send down a tag type with aborts. So we 12182 * can only really go by the tag number... 12183 * This may cause problems with parallel SCSI. 12184 * Need to figure that out!! 12185 */ 12186 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12187 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12188 found = 1; 12189 if ((io->io_hdr.flags & 12190 CTL_FLAG_FROM_OTHER_SC) == 0 && 12191 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12192 union ctl_ha_msg msg_info; 12193 12194 io->io_hdr.flags |= 12195 CTL_FLAG_SENT_2OTHER_SC; 12196 msg_info.hdr.nexus = io->io_hdr.nexus; 12197 msg_info.task.task_action = 12198 CTL_TASK_ABORT_TASK; 12199 msg_info.task.tag_num = 12200 io->taskio.tag_num; 12201 msg_info.task.tag_type = 12202 io->taskio.tag_type; 12203 msg_info.hdr.msg_type = 12204 CTL_MSG_MANAGE_TASKS; 12205 msg_info.hdr.original_sc = NULL; 12206 msg_info.hdr.serializing_sc = NULL; 12207#if 0 12208 printf("Sent Abort to other side\n"); 12209#endif 12210 if (CTL_HA_STATUS_SUCCESS != 12211 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12212 (void *)&msg_info, 12213 sizeof(msg_info), 0)) { 12214 } 12215 } 12216#if 0 12217 printf("ctl_abort_task: found I/O to abort\n"); 12218#endif 12219 break; 12220 } 12221 } 12222 } 12223 mtx_unlock(&lun->lun_lock); 12224 12225 if (found == 0) { 12226 /* 12227 * This isn't really an error. It's entirely possible for 12228 * the abort and command completion to cross on the wire. 12229 * This is more of an informative/diagnostic error. 12230 */ 12231#if 0 12232 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12233 "%d:%d:%d:%d tag %d type %d\n", 12234 io->io_hdr.nexus.initid.id, 12235 io->io_hdr.nexus.targ_port, 12236 io->io_hdr.nexus.targ_target.id, 12237 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12238 io->taskio.tag_type); 12239#endif 12240 } 12241 return (0); 12242} 12243 12244static void 12245ctl_run_task(union ctl_io *io) 12246{ 12247 struct ctl_softc *ctl_softc = control_softc; 12248 int retval = 1; 12249 const char *task_desc; 12250 12251 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12252 12253 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12254 ("ctl_run_task: Unextected io_type %d\n", 12255 io->io_hdr.io_type)); 12256 12257 task_desc = ctl_scsi_task_string(&io->taskio); 12258 if (task_desc != NULL) { 12259#ifdef NEEDTOPORT 12260 csevent_log(CSC_CTL | CSC_SHELF_SW | 12261 CTL_TASK_REPORT, 12262 csevent_LogType_Trace, 12263 csevent_Severity_Information, 12264 csevent_AlertLevel_Green, 12265 csevent_FRU_Firmware, 12266 csevent_FRU_Unknown, 12267 "CTL: received task: %s",task_desc); 12268#endif 12269 } else { 12270#ifdef NEEDTOPORT 12271 csevent_log(CSC_CTL | CSC_SHELF_SW | 12272 CTL_TASK_REPORT, 12273 csevent_LogType_Trace, 12274 csevent_Severity_Information, 12275 csevent_AlertLevel_Green, 12276 csevent_FRU_Firmware, 12277 csevent_FRU_Unknown, 12278 "CTL: received unknown task " 12279 "type: %d (%#x)", 12280 io->taskio.task_action, 12281 io->taskio.task_action); 12282#endif 12283 } 12284 switch (io->taskio.task_action) { 12285 case CTL_TASK_ABORT_TASK: 12286 retval = ctl_abort_task(io); 12287 break; 12288 case CTL_TASK_ABORT_TASK_SET: 12289 case CTL_TASK_CLEAR_TASK_SET: 12290 retval = ctl_abort_task_set(io); 12291 break; 12292 case CTL_TASK_CLEAR_ACA: 12293 break; 12294 case CTL_TASK_I_T_NEXUS_RESET: 12295 retval = ctl_i_t_nexus_reset(io); 12296 break; 12297 case CTL_TASK_LUN_RESET: { 12298 struct ctl_lun *lun; 12299 uint32_t targ_lun; 12300 12301 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12302 mtx_lock(&ctl_softc->ctl_lock); 12303 if ((targ_lun < CTL_MAX_LUNS) 12304 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12305 lun = ctl_softc->ctl_luns[targ_lun]; 12306 else { 12307 mtx_unlock(&ctl_softc->ctl_lock); 12308 retval = 1; 12309 break; 12310 } 12311 12312 if (!(io->io_hdr.flags & 12313 CTL_FLAG_FROM_OTHER_SC)) { 12314 union ctl_ha_msg msg_info; 12315 12316 io->io_hdr.flags |= 12317 CTL_FLAG_SENT_2OTHER_SC; 12318 msg_info.hdr.msg_type = 12319 CTL_MSG_MANAGE_TASKS; 12320 msg_info.hdr.nexus = io->io_hdr.nexus; 12321 msg_info.task.task_action = 12322 CTL_TASK_LUN_RESET; 12323 msg_info.hdr.original_sc = NULL; 12324 msg_info.hdr.serializing_sc = NULL; 12325 if (CTL_HA_STATUS_SUCCESS != 12326 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12327 (void *)&msg_info, 12328 sizeof(msg_info), 0)) { 12329 } 12330 } 12331 12332 retval = ctl_lun_reset(lun, io, 12333 CTL_UA_LUN_RESET); 12334 mtx_unlock(&ctl_softc->ctl_lock); 12335 break; 12336 } 12337 case CTL_TASK_TARGET_RESET: 12338 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12339 break; 12340 case CTL_TASK_BUS_RESET: 12341 retval = ctl_bus_reset(ctl_softc, io); 12342 break; 12343 case CTL_TASK_PORT_LOGIN: 12344 break; 12345 case CTL_TASK_PORT_LOGOUT: 12346 break; 12347 default: 12348 printf("ctl_run_task: got unknown task management event %d\n", 12349 io->taskio.task_action); 12350 break; 12351 } 12352 if (retval == 0) 12353 io->io_hdr.status = CTL_SUCCESS; 12354 else 12355 io->io_hdr.status = CTL_ERROR; 12356 ctl_done(io); 12357} 12358 12359/* 12360 * For HA operation. Handle commands that come in from the other 12361 * controller. 12362 */ 12363static void 12364ctl_handle_isc(union ctl_io *io) 12365{ 12366 int free_io; 12367 struct ctl_lun *lun; 12368 struct ctl_softc *ctl_softc; 12369 uint32_t targ_lun; 12370 12371 ctl_softc = control_softc; 12372 12373 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12374 lun = ctl_softc->ctl_luns[targ_lun]; 12375 12376 switch (io->io_hdr.msg_type) { 12377 case CTL_MSG_SERIALIZE: 12378 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12379 break; 12380 case CTL_MSG_R2R: { 12381 const struct ctl_cmd_entry *entry; 12382 12383 /* 12384 * This is only used in SER_ONLY mode. 12385 */ 12386 free_io = 0; 12387 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12388 mtx_lock(&lun->lun_lock); 12389 if (ctl_scsiio_lun_check(ctl_softc, lun, 12390 entry, (struct ctl_scsiio *)io) != 0) { 12391 mtx_unlock(&lun->lun_lock); 12392 ctl_done(io); 12393 break; 12394 } 12395 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12396 mtx_unlock(&lun->lun_lock); 12397 ctl_enqueue_rtr(io); 12398 break; 12399 } 12400 case CTL_MSG_FINISH_IO: 12401 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12402 free_io = 0; 12403 ctl_done(io); 12404 } else { 12405 free_io = 1; 12406 mtx_lock(&lun->lun_lock); 12407 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12408 ooa_links); 12409 ctl_check_blocked(lun); 12410 mtx_unlock(&lun->lun_lock); 12411 } 12412 break; 12413 case CTL_MSG_PERS_ACTION: 12414 ctl_hndl_per_res_out_on_other_sc( 12415 (union ctl_ha_msg *)&io->presio.pr_msg); 12416 free_io = 1; 12417 break; 12418 case CTL_MSG_BAD_JUJU: 12419 free_io = 0; 12420 ctl_done(io); 12421 break; 12422 case CTL_MSG_DATAMOVE: 12423 /* Only used in XFER mode */ 12424 free_io = 0; 12425 ctl_datamove_remote(io); 12426 break; 12427 case CTL_MSG_DATAMOVE_DONE: 12428 /* Only used in XFER mode */ 12429 free_io = 0; 12430 io->scsiio.be_move_done(io); 12431 break; 12432 default: 12433 free_io = 1; 12434 printf("%s: Invalid message type %d\n", 12435 __func__, io->io_hdr.msg_type); 12436 break; 12437 } 12438 if (free_io) 12439 ctl_free_io(io); 12440 12441} 12442 12443 12444/* 12445 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12446 * there is no match. 12447 */ 12448static ctl_lun_error_pattern 12449ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12450{ 12451 const struct ctl_cmd_entry *entry; 12452 ctl_lun_error_pattern filtered_pattern, pattern; 12453 12454 pattern = desc->error_pattern; 12455 12456 /* 12457 * XXX KDM we need more data passed into this function to match a 12458 * custom pattern, and we actually need to implement custom pattern 12459 * matching. 12460 */ 12461 if (pattern & CTL_LUN_PAT_CMD) 12462 return (CTL_LUN_PAT_CMD); 12463 12464 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12465 return (CTL_LUN_PAT_ANY); 12466 12467 entry = ctl_get_cmd_entry(ctsio, NULL); 12468 12469 filtered_pattern = entry->pattern & pattern; 12470 12471 /* 12472 * If the user requested specific flags in the pattern (e.g. 12473 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12474 * flags. 12475 * 12476 * If the user did not specify any flags, it doesn't matter whether 12477 * or not the command supports the flags. 12478 */ 12479 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12480 (pattern & ~CTL_LUN_PAT_MASK)) 12481 return (CTL_LUN_PAT_NONE); 12482 12483 /* 12484 * If the user asked for a range check, see if the requested LBA 12485 * range overlaps with this command's LBA range. 12486 */ 12487 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12488 uint64_t lba1; 12489 uint64_t len1; 12490 ctl_action action; 12491 int retval; 12492 12493 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12494 if (retval != 0) 12495 return (CTL_LUN_PAT_NONE); 12496 12497 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12498 desc->lba_range.len); 12499 /* 12500 * A "pass" means that the LBA ranges don't overlap, so 12501 * this doesn't match the user's range criteria. 12502 */ 12503 if (action == CTL_ACTION_PASS) 12504 return (CTL_LUN_PAT_NONE); 12505 } 12506 12507 return (filtered_pattern); 12508} 12509 12510static void 12511ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12512{ 12513 struct ctl_error_desc *desc, *desc2; 12514 12515 mtx_assert(&lun->lun_lock, MA_OWNED); 12516 12517 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12518 ctl_lun_error_pattern pattern; 12519 /* 12520 * Check to see whether this particular command matches 12521 * the pattern in the descriptor. 12522 */ 12523 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12524 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12525 continue; 12526 12527 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12528 case CTL_LUN_INJ_ABORTED: 12529 ctl_set_aborted(&io->scsiio); 12530 break; 12531 case CTL_LUN_INJ_MEDIUM_ERR: 12532 ctl_set_medium_error(&io->scsiio); 12533 break; 12534 case CTL_LUN_INJ_UA: 12535 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12536 * OCCURRED */ 12537 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12538 break; 12539 case CTL_LUN_INJ_CUSTOM: 12540 /* 12541 * We're assuming the user knows what he is doing. 12542 * Just copy the sense information without doing 12543 * checks. 12544 */ 12545 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12546 ctl_min(sizeof(desc->custom_sense), 12547 sizeof(io->scsiio.sense_data))); 12548 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12549 io->scsiio.sense_len = SSD_FULL_SIZE; 12550 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12551 break; 12552 case CTL_LUN_INJ_NONE: 12553 default: 12554 /* 12555 * If this is an error injection type we don't know 12556 * about, clear the continuous flag (if it is set) 12557 * so it will get deleted below. 12558 */ 12559 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12560 break; 12561 } 12562 /* 12563 * By default, each error injection action is a one-shot 12564 */ 12565 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12566 continue; 12567 12568 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12569 12570 free(desc, M_CTL); 12571 } 12572} 12573 12574#ifdef CTL_IO_DELAY 12575static void 12576ctl_datamove_timer_wakeup(void *arg) 12577{ 12578 union ctl_io *io; 12579 12580 io = (union ctl_io *)arg; 12581 12582 ctl_datamove(io); 12583} 12584#endif /* CTL_IO_DELAY */ 12585 12586void 12587ctl_datamove(union ctl_io *io) 12588{ 12589 void (*fe_datamove)(union ctl_io *io); 12590 12591 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12592 12593 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12594 12595#ifdef CTL_TIME_IO 12596 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12597 char str[256]; 12598 char path_str[64]; 12599 struct sbuf sb; 12600 12601 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12602 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12603 12604 sbuf_cat(&sb, path_str); 12605 switch (io->io_hdr.io_type) { 12606 case CTL_IO_SCSI: 12607 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12608 sbuf_printf(&sb, "\n"); 12609 sbuf_cat(&sb, path_str); 12610 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12611 io->scsiio.tag_num, io->scsiio.tag_type); 12612 break; 12613 case CTL_IO_TASK: 12614 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12615 "Tag Type: %d\n", io->taskio.task_action, 12616 io->taskio.tag_num, io->taskio.tag_type); 12617 break; 12618 default: 12619 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12620 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12621 break; 12622 } 12623 sbuf_cat(&sb, path_str); 12624 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12625 (intmax_t)time_uptime - io->io_hdr.start_time); 12626 sbuf_finish(&sb); 12627 printf("%s", sbuf_data(&sb)); 12628 } 12629#endif /* CTL_TIME_IO */ 12630 12631#ifdef CTL_IO_DELAY 12632 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12633 struct ctl_lun *lun; 12634 12635 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12636 12637 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12638 } else { 12639 struct ctl_lun *lun; 12640 12641 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12642 if ((lun != NULL) 12643 && (lun->delay_info.datamove_delay > 0)) { 12644 struct callout *callout; 12645 12646 callout = (struct callout *)&io->io_hdr.timer_bytes; 12647 callout_init(callout, /*mpsafe*/ 1); 12648 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12649 callout_reset(callout, 12650 lun->delay_info.datamove_delay * hz, 12651 ctl_datamove_timer_wakeup, io); 12652 if (lun->delay_info.datamove_type == 12653 CTL_DELAY_TYPE_ONESHOT) 12654 lun->delay_info.datamove_delay = 0; 12655 return; 12656 } 12657 } 12658#endif 12659 12660 /* 12661 * This command has been aborted. Set the port status, so we fail 12662 * the data move. 12663 */ 12664 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12665 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12666 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12667 io->io_hdr.nexus.targ_port, 12668 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12669 io->io_hdr.nexus.targ_lun); 12670 io->io_hdr.port_status = 31337; 12671 /* 12672 * Note that the backend, in this case, will get the 12673 * callback in its context. In other cases it may get 12674 * called in the frontend's interrupt thread context. 12675 */ 12676 io->scsiio.be_move_done(io); 12677 return; 12678 } 12679 12680 /* Don't confuse frontend with zero length data move. */ 12681 if (io->scsiio.kern_data_len == 0) { 12682 io->scsiio.be_move_done(io); 12683 return; 12684 } 12685 12686 /* 12687 * If we're in XFER mode and this I/O is from the other shelf 12688 * controller, we need to send the DMA to the other side to 12689 * actually transfer the data to/from the host. In serialize only 12690 * mode the transfer happens below CTL and ctl_datamove() is only 12691 * called on the machine that originally received the I/O. 12692 */ 12693 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12694 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12695 union ctl_ha_msg msg; 12696 uint32_t sg_entries_sent; 12697 int do_sg_copy; 12698 int i; 12699 12700 memset(&msg, 0, sizeof(msg)); 12701 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12702 msg.hdr.original_sc = io->io_hdr.original_sc; 12703 msg.hdr.serializing_sc = io; 12704 msg.hdr.nexus = io->io_hdr.nexus; 12705 msg.dt.flags = io->io_hdr.flags; 12706 /* 12707 * We convert everything into a S/G list here. We can't 12708 * pass by reference, only by value between controllers. 12709 * So we can't pass a pointer to the S/G list, only as many 12710 * S/G entries as we can fit in here. If it's possible for 12711 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12712 * then we need to break this up into multiple transfers. 12713 */ 12714 if (io->scsiio.kern_sg_entries == 0) { 12715 msg.dt.kern_sg_entries = 1; 12716 /* 12717 * If this is in cached memory, flush the cache 12718 * before we send the DMA request to the other 12719 * controller. We want to do this in either the 12720 * read or the write case. The read case is 12721 * straightforward. In the write case, we want to 12722 * make sure nothing is in the local cache that 12723 * could overwrite the DMAed data. 12724 */ 12725 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12726 /* 12727 * XXX KDM use bus_dmamap_sync() here. 12728 */ 12729 } 12730 12731 /* 12732 * Convert to a physical address if this is a 12733 * virtual address. 12734 */ 12735 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12736 msg.dt.sg_list[0].addr = 12737 io->scsiio.kern_data_ptr; 12738 } else { 12739 /* 12740 * XXX KDM use busdma here! 12741 */ 12742#if 0 12743 msg.dt.sg_list[0].addr = (void *) 12744 vtophys(io->scsiio.kern_data_ptr); 12745#endif 12746 } 12747 12748 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12749 do_sg_copy = 0; 12750 } else { 12751 struct ctl_sg_entry *sgl; 12752 12753 do_sg_copy = 1; 12754 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12755 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12756 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12757 /* 12758 * XXX KDM use bus_dmamap_sync() here. 12759 */ 12760 } 12761 } 12762 12763 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12764 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12765 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12766 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12767 msg.dt.sg_sequence = 0; 12768 12769 /* 12770 * Loop until we've sent all of the S/G entries. On the 12771 * other end, we'll recompose these S/G entries into one 12772 * contiguous list before passing it to the 12773 */ 12774 for (sg_entries_sent = 0; sg_entries_sent < 12775 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12776 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12777 sizeof(msg.dt.sg_list[0])), 12778 msg.dt.kern_sg_entries - sg_entries_sent); 12779 12780 if (do_sg_copy != 0) { 12781 struct ctl_sg_entry *sgl; 12782 int j; 12783 12784 sgl = (struct ctl_sg_entry *) 12785 io->scsiio.kern_data_ptr; 12786 /* 12787 * If this is in cached memory, flush the cache 12788 * before we send the DMA request to the other 12789 * controller. We want to do this in either 12790 * the * read or the write case. The read 12791 * case is straightforward. In the write 12792 * case, we want to make sure nothing is 12793 * in the local cache that could overwrite 12794 * the DMAed data. 12795 */ 12796 12797 for (i = sg_entries_sent, j = 0; 12798 i < msg.dt.cur_sg_entries; i++, j++) { 12799 if ((io->io_hdr.flags & 12800 CTL_FLAG_NO_DATASYNC) == 0) { 12801 /* 12802 * XXX KDM use bus_dmamap_sync() 12803 */ 12804 } 12805 if ((io->io_hdr.flags & 12806 CTL_FLAG_BUS_ADDR) == 0) { 12807 /* 12808 * XXX KDM use busdma. 12809 */ 12810#if 0 12811 msg.dt.sg_list[j].addr =(void *) 12812 vtophys(sgl[i].addr); 12813#endif 12814 } else { 12815 msg.dt.sg_list[j].addr = 12816 sgl[i].addr; 12817 } 12818 msg.dt.sg_list[j].len = sgl[i].len; 12819 } 12820 } 12821 12822 sg_entries_sent += msg.dt.cur_sg_entries; 12823 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12824 msg.dt.sg_last = 1; 12825 else 12826 msg.dt.sg_last = 0; 12827 12828 /* 12829 * XXX KDM drop and reacquire the lock here? 12830 */ 12831 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12832 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12833 /* 12834 * XXX do something here. 12835 */ 12836 } 12837 12838 msg.dt.sent_sg_entries = sg_entries_sent; 12839 } 12840 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12841 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12842 ctl_failover_io(io, /*have_lock*/ 0); 12843 12844 } else { 12845 12846 /* 12847 * Lookup the fe_datamove() function for this particular 12848 * front end. 12849 */ 12850 fe_datamove = 12851 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12852 12853 fe_datamove(io); 12854 } 12855} 12856 12857static void 12858ctl_send_datamove_done(union ctl_io *io, int have_lock) 12859{ 12860 union ctl_ha_msg msg; 12861 int isc_status; 12862 12863 memset(&msg, 0, sizeof(msg)); 12864 12865 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12866 msg.hdr.original_sc = io; 12867 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12868 msg.hdr.nexus = io->io_hdr.nexus; 12869 msg.hdr.status = io->io_hdr.status; 12870 msg.scsi.tag_num = io->scsiio.tag_num; 12871 msg.scsi.tag_type = io->scsiio.tag_type; 12872 msg.scsi.scsi_status = io->scsiio.scsi_status; 12873 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12874 sizeof(io->scsiio.sense_data)); 12875 msg.scsi.sense_len = io->scsiio.sense_len; 12876 msg.scsi.sense_residual = io->scsiio.sense_residual; 12877 msg.scsi.fetd_status = io->io_hdr.port_status; 12878 msg.scsi.residual = io->scsiio.residual; 12879 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12880 12881 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12882 ctl_failover_io(io, /*have_lock*/ have_lock); 12883 return; 12884 } 12885 12886 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12887 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12888 /* XXX do something if this fails */ 12889 } 12890 12891} 12892 12893/* 12894 * The DMA to the remote side is done, now we need to tell the other side 12895 * we're done so it can continue with its data movement. 12896 */ 12897static void 12898ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12899{ 12900 union ctl_io *io; 12901 12902 io = rq->context; 12903 12904 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12905 printf("%s: ISC DMA write failed with error %d", __func__, 12906 rq->ret); 12907 ctl_set_internal_failure(&io->scsiio, 12908 /*sks_valid*/ 1, 12909 /*retry_count*/ rq->ret); 12910 } 12911 12912 ctl_dt_req_free(rq); 12913 12914 /* 12915 * In this case, we had to malloc the memory locally. Free it. 12916 */ 12917 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12918 int i; 12919 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12920 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12921 } 12922 /* 12923 * The data is in local and remote memory, so now we need to send 12924 * status (good or back) back to the other side. 12925 */ 12926 ctl_send_datamove_done(io, /*have_lock*/ 0); 12927} 12928 12929/* 12930 * We've moved the data from the host/controller into local memory. Now we 12931 * need to push it over to the remote controller's memory. 12932 */ 12933static int 12934ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12935{ 12936 int retval; 12937 12938 retval = 0; 12939 12940 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12941 ctl_datamove_remote_write_cb); 12942 12943 return (retval); 12944} 12945 12946static void 12947ctl_datamove_remote_write(union ctl_io *io) 12948{ 12949 int retval; 12950 void (*fe_datamove)(union ctl_io *io); 12951 12952 /* 12953 * - Get the data from the host/HBA into local memory. 12954 * - DMA memory from the local controller to the remote controller. 12955 * - Send status back to the remote controller. 12956 */ 12957 12958 retval = ctl_datamove_remote_sgl_setup(io); 12959 if (retval != 0) 12960 return; 12961 12962 /* Switch the pointer over so the FETD knows what to do */ 12963 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12964 12965 /* 12966 * Use a custom move done callback, since we need to send completion 12967 * back to the other controller, not to the backend on this side. 12968 */ 12969 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12970 12971 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12972 12973 fe_datamove(io); 12974 12975 return; 12976 12977} 12978 12979static int 12980ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12981{ 12982#if 0 12983 char str[256]; 12984 char path_str[64]; 12985 struct sbuf sb; 12986#endif 12987 12988 /* 12989 * In this case, we had to malloc the memory locally. Free it. 12990 */ 12991 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12992 int i; 12993 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12994 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12995 } 12996 12997#if 0 12998 scsi_path_string(io, path_str, sizeof(path_str)); 12999 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13000 sbuf_cat(&sb, path_str); 13001 scsi_command_string(&io->scsiio, NULL, &sb); 13002 sbuf_printf(&sb, "\n"); 13003 sbuf_cat(&sb, path_str); 13004 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13005 io->scsiio.tag_num, io->scsiio.tag_type); 13006 sbuf_cat(&sb, path_str); 13007 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13008 io->io_hdr.flags, io->io_hdr.status); 13009 sbuf_finish(&sb); 13010 printk("%s", sbuf_data(&sb)); 13011#endif 13012 13013 13014 /* 13015 * The read is done, now we need to send status (good or bad) back 13016 * to the other side. 13017 */ 13018 ctl_send_datamove_done(io, /*have_lock*/ 0); 13019 13020 return (0); 13021} 13022 13023static void 13024ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13025{ 13026 union ctl_io *io; 13027 void (*fe_datamove)(union ctl_io *io); 13028 13029 io = rq->context; 13030 13031 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13032 printf("%s: ISC DMA read failed with error %d", __func__, 13033 rq->ret); 13034 ctl_set_internal_failure(&io->scsiio, 13035 /*sks_valid*/ 1, 13036 /*retry_count*/ rq->ret); 13037 } 13038 13039 ctl_dt_req_free(rq); 13040 13041 /* Switch the pointer over so the FETD knows what to do */ 13042 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13043 13044 /* 13045 * Use a custom move done callback, since we need to send completion 13046 * back to the other controller, not to the backend on this side. 13047 */ 13048 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13049 13050 /* XXX KDM add checks like the ones in ctl_datamove? */ 13051 13052 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13053 13054 fe_datamove(io); 13055} 13056 13057static int 13058ctl_datamove_remote_sgl_setup(union ctl_io *io) 13059{ 13060 struct ctl_sg_entry *local_sglist, *remote_sglist; 13061 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13062 struct ctl_softc *softc; 13063 int retval; 13064 int i; 13065 13066 retval = 0; 13067 softc = control_softc; 13068 13069 local_sglist = io->io_hdr.local_sglist; 13070 local_dma_sglist = io->io_hdr.local_dma_sglist; 13071 remote_sglist = io->io_hdr.remote_sglist; 13072 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13073 13074 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13075 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13076 local_sglist[i].len = remote_sglist[i].len; 13077 13078 /* 13079 * XXX Detect the situation where the RS-level I/O 13080 * redirector on the other side has already read the 13081 * data off of the AOR RS on this side, and 13082 * transferred it to remote (mirror) memory on the 13083 * other side. Since we already have the data in 13084 * memory here, we just need to use it. 13085 * 13086 * XXX KDM this can probably be removed once we 13087 * get the cache device code in and take the 13088 * current AOR implementation out. 13089 */ 13090#ifdef NEEDTOPORT 13091 if ((remote_sglist[i].addr >= 13092 (void *)vtophys(softc->mirr->addr)) 13093 && (remote_sglist[i].addr < 13094 ((void *)vtophys(softc->mirr->addr) + 13095 CacheMirrorOffset))) { 13096 local_sglist[i].addr = remote_sglist[i].addr - 13097 CacheMirrorOffset; 13098 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13099 CTL_FLAG_DATA_IN) 13100 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13101 } else { 13102 local_sglist[i].addr = remote_sglist[i].addr + 13103 CacheMirrorOffset; 13104 } 13105#endif 13106#if 0 13107 printf("%s: local %p, remote %p, len %d\n", 13108 __func__, local_sglist[i].addr, 13109 remote_sglist[i].addr, local_sglist[i].len); 13110#endif 13111 } 13112 } else { 13113 uint32_t len_to_go; 13114 13115 /* 13116 * In this case, we don't have automatically allocated 13117 * memory for this I/O on this controller. This typically 13118 * happens with internal CTL I/O -- e.g. inquiry, mode 13119 * sense, etc. Anything coming from RAIDCore will have 13120 * a mirror area available. 13121 */ 13122 len_to_go = io->scsiio.kern_data_len; 13123 13124 /* 13125 * Clear the no datasync flag, we have to use malloced 13126 * buffers. 13127 */ 13128 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13129 13130 /* 13131 * The difficult thing here is that the size of the various 13132 * S/G segments may be different than the size from the 13133 * remote controller. That'll make it harder when DMAing 13134 * the data back to the other side. 13135 */ 13136 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13137 sizeof(io->io_hdr.remote_sglist[0])) && 13138 (len_to_go > 0); i++) { 13139 local_sglist[i].len = ctl_min(len_to_go, 131072); 13140 CTL_SIZE_8B(local_dma_sglist[i].len, 13141 local_sglist[i].len); 13142 local_sglist[i].addr = 13143 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13144 13145 local_dma_sglist[i].addr = local_sglist[i].addr; 13146 13147 if (local_sglist[i].addr == NULL) { 13148 int j; 13149 13150 printf("malloc failed for %zd bytes!", 13151 local_dma_sglist[i].len); 13152 for (j = 0; j < i; j++) { 13153 free(local_sglist[j].addr, M_CTL); 13154 } 13155 ctl_set_internal_failure(&io->scsiio, 13156 /*sks_valid*/ 1, 13157 /*retry_count*/ 4857); 13158 retval = 1; 13159 goto bailout_error; 13160 13161 } 13162 /* XXX KDM do we need a sync here? */ 13163 13164 len_to_go -= local_sglist[i].len; 13165 } 13166 /* 13167 * Reset the number of S/G entries accordingly. The 13168 * original number of S/G entries is available in 13169 * rem_sg_entries. 13170 */ 13171 io->scsiio.kern_sg_entries = i; 13172 13173#if 0 13174 printf("%s: kern_sg_entries = %d\n", __func__, 13175 io->scsiio.kern_sg_entries); 13176 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13177 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13178 local_sglist[i].addr, local_sglist[i].len, 13179 local_dma_sglist[i].len); 13180#endif 13181 } 13182 13183 13184 return (retval); 13185 13186bailout_error: 13187 13188 ctl_send_datamove_done(io, /*have_lock*/ 0); 13189 13190 return (retval); 13191} 13192 13193static int 13194ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13195 ctl_ha_dt_cb callback) 13196{ 13197 struct ctl_ha_dt_req *rq; 13198 struct ctl_sg_entry *remote_sglist, *local_sglist; 13199 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13200 uint32_t local_used, remote_used, total_used; 13201 int retval; 13202 int i, j; 13203 13204 retval = 0; 13205 13206 rq = ctl_dt_req_alloc(); 13207 13208 /* 13209 * If we failed to allocate the request, and if the DMA didn't fail 13210 * anyway, set busy status. This is just a resource allocation 13211 * failure. 13212 */ 13213 if ((rq == NULL) 13214 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13215 ctl_set_busy(&io->scsiio); 13216 13217 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13218 13219 if (rq != NULL) 13220 ctl_dt_req_free(rq); 13221 13222 /* 13223 * The data move failed. We need to return status back 13224 * to the other controller. No point in trying to DMA 13225 * data to the remote controller. 13226 */ 13227 13228 ctl_send_datamove_done(io, /*have_lock*/ 0); 13229 13230 retval = 1; 13231 13232 goto bailout; 13233 } 13234 13235 local_sglist = io->io_hdr.local_sglist; 13236 local_dma_sglist = io->io_hdr.local_dma_sglist; 13237 remote_sglist = io->io_hdr.remote_sglist; 13238 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13239 local_used = 0; 13240 remote_used = 0; 13241 total_used = 0; 13242 13243 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13244 rq->ret = CTL_HA_STATUS_SUCCESS; 13245 rq->context = io; 13246 callback(rq); 13247 goto bailout; 13248 } 13249 13250 /* 13251 * Pull/push the data over the wire from/to the other controller. 13252 * This takes into account the possibility that the local and 13253 * remote sglists may not be identical in terms of the size of 13254 * the elements and the number of elements. 13255 * 13256 * One fundamental assumption here is that the length allocated for 13257 * both the local and remote sglists is identical. Otherwise, we've 13258 * essentially got a coding error of some sort. 13259 */ 13260 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13261 int isc_ret; 13262 uint32_t cur_len, dma_length; 13263 uint8_t *tmp_ptr; 13264 13265 rq->id = CTL_HA_DATA_CTL; 13266 rq->command = command; 13267 rq->context = io; 13268 13269 /* 13270 * Both pointers should be aligned. But it is possible 13271 * that the allocation length is not. They should both 13272 * also have enough slack left over at the end, though, 13273 * to round up to the next 8 byte boundary. 13274 */ 13275 cur_len = ctl_min(local_sglist[i].len - local_used, 13276 remote_sglist[j].len - remote_used); 13277 13278 /* 13279 * In this case, we have a size issue and need to decrease 13280 * the size, except in the case where we actually have less 13281 * than 8 bytes left. In that case, we need to increase 13282 * the DMA length to get the last bit. 13283 */ 13284 if ((cur_len & 0x7) != 0) { 13285 if (cur_len > 0x7) { 13286 cur_len = cur_len - (cur_len & 0x7); 13287 dma_length = cur_len; 13288 } else { 13289 CTL_SIZE_8B(dma_length, cur_len); 13290 } 13291 13292 } else 13293 dma_length = cur_len; 13294 13295 /* 13296 * If we had to allocate memory for this I/O, instead of using 13297 * the non-cached mirror memory, we'll need to flush the cache 13298 * before trying to DMA to the other controller. 13299 * 13300 * We could end up doing this multiple times for the same 13301 * segment if we have a larger local segment than remote 13302 * segment. That shouldn't be an issue. 13303 */ 13304 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13305 /* 13306 * XXX KDM use bus_dmamap_sync() here. 13307 */ 13308 } 13309 13310 rq->size = dma_length; 13311 13312 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13313 tmp_ptr += local_used; 13314 13315 /* Use physical addresses when talking to ISC hardware */ 13316 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13317 /* XXX KDM use busdma */ 13318#if 0 13319 rq->local = vtophys(tmp_ptr); 13320#endif 13321 } else 13322 rq->local = tmp_ptr; 13323 13324 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13325 tmp_ptr += remote_used; 13326 rq->remote = tmp_ptr; 13327 13328 rq->callback = NULL; 13329 13330 local_used += cur_len; 13331 if (local_used >= local_sglist[i].len) { 13332 i++; 13333 local_used = 0; 13334 } 13335 13336 remote_used += cur_len; 13337 if (remote_used >= remote_sglist[j].len) { 13338 j++; 13339 remote_used = 0; 13340 } 13341 total_used += cur_len; 13342 13343 if (total_used >= io->scsiio.kern_data_len) 13344 rq->callback = callback; 13345 13346 if ((rq->size & 0x7) != 0) { 13347 printf("%s: warning: size %d is not on 8b boundary\n", 13348 __func__, rq->size); 13349 } 13350 if (((uintptr_t)rq->local & 0x7) != 0) { 13351 printf("%s: warning: local %p not on 8b boundary\n", 13352 __func__, rq->local); 13353 } 13354 if (((uintptr_t)rq->remote & 0x7) != 0) { 13355 printf("%s: warning: remote %p not on 8b boundary\n", 13356 __func__, rq->local); 13357 } 13358#if 0 13359 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13360 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13361 rq->local, rq->remote, rq->size); 13362#endif 13363 13364 isc_ret = ctl_dt_single(rq); 13365 if (isc_ret == CTL_HA_STATUS_WAIT) 13366 continue; 13367 13368 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13369 rq->ret = CTL_HA_STATUS_SUCCESS; 13370 } else { 13371 rq->ret = isc_ret; 13372 } 13373 callback(rq); 13374 goto bailout; 13375 } 13376 13377bailout: 13378 return (retval); 13379 13380} 13381 13382static void 13383ctl_datamove_remote_read(union ctl_io *io) 13384{ 13385 int retval; 13386 int i; 13387 13388 /* 13389 * This will send an error to the other controller in the case of a 13390 * failure. 13391 */ 13392 retval = ctl_datamove_remote_sgl_setup(io); 13393 if (retval != 0) 13394 return; 13395 13396 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13397 ctl_datamove_remote_read_cb); 13398 if ((retval != 0) 13399 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13400 /* 13401 * Make sure we free memory if there was an error.. The 13402 * ctl_datamove_remote_xfer() function will send the 13403 * datamove done message, or call the callback with an 13404 * error if there is a problem. 13405 */ 13406 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13407 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13408 } 13409 13410 return; 13411} 13412 13413/* 13414 * Process a datamove request from the other controller. This is used for 13415 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13416 * first. Once that is complete, the data gets DMAed into the remote 13417 * controller's memory. For reads, we DMA from the remote controller's 13418 * memory into our memory first, and then move it out to the FETD. 13419 */ 13420static void 13421ctl_datamove_remote(union ctl_io *io) 13422{ 13423 struct ctl_softc *softc; 13424 13425 softc = control_softc; 13426 13427 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13428 13429 /* 13430 * Note that we look for an aborted I/O here, but don't do some of 13431 * the other checks that ctl_datamove() normally does. 13432 * We don't need to run the datamove delay code, since that should 13433 * have been done if need be on the other controller. 13434 */ 13435 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13436 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13437 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13438 io->io_hdr.nexus.targ_port, 13439 io->io_hdr.nexus.targ_target.id, 13440 io->io_hdr.nexus.targ_lun); 13441 io->io_hdr.port_status = 31338; 13442 ctl_send_datamove_done(io, /*have_lock*/ 0); 13443 return; 13444 } 13445 13446 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13447 ctl_datamove_remote_write(io); 13448 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13449 ctl_datamove_remote_read(io); 13450 } else { 13451 union ctl_ha_msg msg; 13452 struct scsi_sense_data *sense; 13453 uint8_t sks[3]; 13454 int retry_count; 13455 13456 memset(&msg, 0, sizeof(msg)); 13457 13458 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13459 msg.hdr.status = CTL_SCSI_ERROR; 13460 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13461 13462 retry_count = 4243; 13463 13464 sense = &msg.scsi.sense_data; 13465 sks[0] = SSD_SCS_VALID; 13466 sks[1] = (retry_count >> 8) & 0xff; 13467 sks[2] = retry_count & 0xff; 13468 13469 /* "Internal target failure" */ 13470 scsi_set_sense_data(sense, 13471 /*sense_format*/ SSD_TYPE_NONE, 13472 /*current_error*/ 1, 13473 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13474 /*asc*/ 0x44, 13475 /*ascq*/ 0x00, 13476 /*type*/ SSD_ELEM_SKS, 13477 /*size*/ sizeof(sks), 13478 /*data*/ sks, 13479 SSD_ELEM_NONE); 13480 13481 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13482 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13483 ctl_failover_io(io, /*have_lock*/ 1); 13484 return; 13485 } 13486 13487 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13488 CTL_HA_STATUS_SUCCESS) { 13489 /* XXX KDM what to do if this fails? */ 13490 } 13491 return; 13492 } 13493 13494} 13495 13496static int 13497ctl_process_done(union ctl_io *io) 13498{ 13499 struct ctl_lun *lun; 13500 struct ctl_softc *ctl_softc; 13501 void (*fe_done)(union ctl_io *io); 13502 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13503 13504 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13505 13506 fe_done = 13507 control_softc->ctl_ports[targ_port]->fe_done; 13508 13509#ifdef CTL_TIME_IO 13510 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13511 char str[256]; 13512 char path_str[64]; 13513 struct sbuf sb; 13514 13515 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13516 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13517 13518 sbuf_cat(&sb, path_str); 13519 switch (io->io_hdr.io_type) { 13520 case CTL_IO_SCSI: 13521 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13522 sbuf_printf(&sb, "\n"); 13523 sbuf_cat(&sb, path_str); 13524 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13525 io->scsiio.tag_num, io->scsiio.tag_type); 13526 break; 13527 case CTL_IO_TASK: 13528 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13529 "Tag Type: %d\n", io->taskio.task_action, 13530 io->taskio.tag_num, io->taskio.tag_type); 13531 break; 13532 default: 13533 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13534 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13535 break; 13536 } 13537 sbuf_cat(&sb, path_str); 13538 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13539 (intmax_t)time_uptime - io->io_hdr.start_time); 13540 sbuf_finish(&sb); 13541 printf("%s", sbuf_data(&sb)); 13542 } 13543#endif /* CTL_TIME_IO */ 13544 13545 switch (io->io_hdr.io_type) { 13546 case CTL_IO_SCSI: 13547 break; 13548 case CTL_IO_TASK: 13549 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13550 ctl_io_error_print(io, NULL); 13551 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13552 ctl_free_io(io); 13553 else 13554 fe_done(io); 13555 return (CTL_RETVAL_COMPLETE); 13556 default: 13557 panic("ctl_process_done: invalid io type %d\n", 13558 io->io_hdr.io_type); 13559 break; /* NOTREACHED */ 13560 } 13561 13562 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13563 if (lun == NULL) { 13564 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13565 io->io_hdr.nexus.targ_mapped_lun)); 13566 fe_done(io); 13567 goto bailout; 13568 } 13569 ctl_softc = lun->ctl_softc; 13570 13571 mtx_lock(&lun->lun_lock); 13572 13573 /* 13574 * Check to see if we have any errors to inject here. We only 13575 * inject errors for commands that don't already have errors set. 13576 */ 13577 if ((STAILQ_FIRST(&lun->error_list) != NULL) 13578 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 13579 ctl_inject_error(lun, io); 13580 13581 /* 13582 * XXX KDM how do we treat commands that aren't completed 13583 * successfully? 13584 * 13585 * XXX KDM should we also track I/O latency? 13586 */ 13587 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13588 io->io_hdr.io_type == CTL_IO_SCSI) { 13589#ifdef CTL_TIME_IO 13590 struct bintime cur_bt; 13591#endif 13592 int type; 13593 13594 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13595 CTL_FLAG_DATA_IN) 13596 type = CTL_STATS_READ; 13597 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13598 CTL_FLAG_DATA_OUT) 13599 type = CTL_STATS_WRITE; 13600 else 13601 type = CTL_STATS_NO_IO; 13602 13603 lun->stats.ports[targ_port].bytes[type] += 13604 io->scsiio.kern_total_len; 13605 lun->stats.ports[targ_port].operations[type]++; 13606#ifdef CTL_TIME_IO 13607 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13608 &io->io_hdr.dma_bt); 13609 lun->stats.ports[targ_port].num_dmas[type] += 13610 io->io_hdr.num_dmas; 13611 getbintime(&cur_bt); 13612 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13613 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13614#endif 13615 } 13616 13617 /* 13618 * Remove this from the OOA queue. 13619 */ 13620 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13621 13622 /* 13623 * Run through the blocked queue on this LUN and see if anything 13624 * has become unblocked, now that this transaction is done. 13625 */ 13626 ctl_check_blocked(lun); 13627 13628 /* 13629 * If the LUN has been invalidated, free it if there is nothing 13630 * left on its OOA queue. 13631 */ 13632 if ((lun->flags & CTL_LUN_INVALID) 13633 && TAILQ_EMPTY(&lun->ooa_queue)) { 13634 mtx_unlock(&lun->lun_lock); 13635 mtx_lock(&ctl_softc->ctl_lock); 13636 ctl_free_lun(lun); 13637 mtx_unlock(&ctl_softc->ctl_lock); 13638 } else 13639 mtx_unlock(&lun->lun_lock); 13640 13641 /* 13642 * If this command has been aborted, make sure we set the status 13643 * properly. The FETD is responsible for freeing the I/O and doing 13644 * whatever it needs to do to clean up its state. 13645 */ 13646 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13647 ctl_set_task_aborted(&io->scsiio); 13648 13649 /* 13650 * If enabled, print command error status. 13651 * We don't print UAs unless debugging was enabled explicitly. 13652 */ 13653 do { 13654 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13655 break; 13656 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13657 break; 13658 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13659 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13660 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13661 int error_code, sense_key, asc, ascq; 13662 13663 scsi_extract_sense_len(&io->scsiio.sense_data, 13664 io->scsiio.sense_len, &error_code, &sense_key, 13665 &asc, &ascq, /*show_errors*/ 0); 13666 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13667 break; 13668 } 13669 13670 ctl_io_error_print(io, NULL); 13671 } while (0); 13672 13673 /* 13674 * Tell the FETD or the other shelf controller we're done with this 13675 * command. Note that only SCSI commands get to this point. Task 13676 * management commands are completed above. 13677 * 13678 * We only send status to the other controller if we're in XFER 13679 * mode. In SER_ONLY mode, the I/O is done on the controller that 13680 * received the I/O (from CTL's perspective), and so the status is 13681 * generated there. 13682 * 13683 * XXX KDM if we hold the lock here, we could cause a deadlock 13684 * if the frontend comes back in in this context to queue 13685 * something. 13686 */ 13687 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13688 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13689 union ctl_ha_msg msg; 13690 13691 memset(&msg, 0, sizeof(msg)); 13692 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13693 msg.hdr.original_sc = io->io_hdr.original_sc; 13694 msg.hdr.nexus = io->io_hdr.nexus; 13695 msg.hdr.status = io->io_hdr.status; 13696 msg.scsi.scsi_status = io->scsiio.scsi_status; 13697 msg.scsi.tag_num = io->scsiio.tag_num; 13698 msg.scsi.tag_type = io->scsiio.tag_type; 13699 msg.scsi.sense_len = io->scsiio.sense_len; 13700 msg.scsi.sense_residual = io->scsiio.sense_residual; 13701 msg.scsi.residual = io->scsiio.residual; 13702 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13703 sizeof(io->scsiio.sense_data)); 13704 /* 13705 * We copy this whether or not this is an I/O-related 13706 * command. Otherwise, we'd have to go and check to see 13707 * whether it's a read/write command, and it really isn't 13708 * worth it. 13709 */ 13710 memcpy(&msg.scsi.lbalen, 13711 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13712 sizeof(msg.scsi.lbalen)); 13713 13714 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13715 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13716 /* XXX do something here */ 13717 } 13718 13719 ctl_free_io(io); 13720 } else 13721 fe_done(io); 13722 13723bailout: 13724 13725 return (CTL_RETVAL_COMPLETE); 13726} 13727 13728#ifdef CTL_WITH_CA 13729/* 13730 * Front end should call this if it doesn't do autosense. When the request 13731 * sense comes back in from the initiator, we'll dequeue this and send it. 13732 */ 13733int 13734ctl_queue_sense(union ctl_io *io) 13735{ 13736 struct ctl_lun *lun; 13737 struct ctl_softc *ctl_softc; 13738 uint32_t initidx, targ_lun; 13739 13740 ctl_softc = control_softc; 13741 13742 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13743 13744 /* 13745 * LUN lookup will likely move to the ctl_work_thread() once we 13746 * have our new queueing infrastructure (that doesn't put things on 13747 * a per-LUN queue initially). That is so that we can handle 13748 * things like an INQUIRY to a LUN that we don't have enabled. We 13749 * can't deal with that right now. 13750 */ 13751 mtx_lock(&ctl_softc->ctl_lock); 13752 13753 /* 13754 * If we don't have a LUN for this, just toss the sense 13755 * information. 13756 */ 13757 targ_lun = io->io_hdr.nexus.targ_lun; 13758 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13759 if ((targ_lun < CTL_MAX_LUNS) 13760 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13761 lun = ctl_softc->ctl_luns[targ_lun]; 13762 else 13763 goto bailout; 13764 13765 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13766 13767 mtx_lock(&lun->lun_lock); 13768 /* 13769 * Already have CA set for this LUN...toss the sense information. 13770 */ 13771 if (ctl_is_set(lun->have_ca, initidx)) { 13772 mtx_unlock(&lun->lun_lock); 13773 goto bailout; 13774 } 13775 13776 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13777 ctl_min(sizeof(lun->pending_sense[initidx]), 13778 sizeof(io->scsiio.sense_data))); 13779 ctl_set_mask(lun->have_ca, initidx); 13780 mtx_unlock(&lun->lun_lock); 13781 13782bailout: 13783 mtx_unlock(&ctl_softc->ctl_lock); 13784 13785 ctl_free_io(io); 13786 13787 return (CTL_RETVAL_COMPLETE); 13788} 13789#endif 13790 13791/* 13792 * Primary command inlet from frontend ports. All SCSI and task I/O 13793 * requests must go through this function. 13794 */ 13795int 13796ctl_queue(union ctl_io *io) 13797{ 13798 struct ctl_softc *ctl_softc; 13799 13800 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13801 13802 ctl_softc = control_softc; 13803 13804#ifdef CTL_TIME_IO 13805 io->io_hdr.start_time = time_uptime; 13806 getbintime(&io->io_hdr.start_bt); 13807#endif /* CTL_TIME_IO */ 13808 13809 /* Map FE-specific LUN ID into global one. */ 13810 io->io_hdr.nexus.targ_mapped_lun = 13811 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13812 13813 switch (io->io_hdr.io_type) { 13814 case CTL_IO_SCSI: 13815 case CTL_IO_TASK: 13816 if (ctl_debug & CTL_DEBUG_CDB) 13817 ctl_io_print(io); 13818 ctl_enqueue_incoming(io); 13819 break; 13820 default: 13821 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13822 return (EINVAL); 13823 } 13824 13825 return (CTL_RETVAL_COMPLETE); 13826} 13827 13828#ifdef CTL_IO_DELAY 13829static void 13830ctl_done_timer_wakeup(void *arg) 13831{ 13832 union ctl_io *io; 13833 13834 io = (union ctl_io *)arg; 13835 ctl_done(io); 13836} 13837#endif /* CTL_IO_DELAY */ 13838 13839void 13840ctl_done(union ctl_io *io) 13841{ 13842 struct ctl_softc *ctl_softc; 13843 13844 ctl_softc = control_softc; 13845 13846 /* 13847 * Enable this to catch duplicate completion issues. 13848 */ 13849#if 0 13850 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13851 printf("%s: type %d msg %d cdb %x iptl: " 13852 "%d:%d:%d:%d tag 0x%04x " 13853 "flag %#x status %x\n", 13854 __func__, 13855 io->io_hdr.io_type, 13856 io->io_hdr.msg_type, 13857 io->scsiio.cdb[0], 13858 io->io_hdr.nexus.initid.id, 13859 io->io_hdr.nexus.targ_port, 13860 io->io_hdr.nexus.targ_target.id, 13861 io->io_hdr.nexus.targ_lun, 13862 (io->io_hdr.io_type == 13863 CTL_IO_TASK) ? 13864 io->taskio.tag_num : 13865 io->scsiio.tag_num, 13866 io->io_hdr.flags, 13867 io->io_hdr.status); 13868 } else 13869 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13870#endif 13871 13872 /* 13873 * This is an internal copy of an I/O, and should not go through 13874 * the normal done processing logic. 13875 */ 13876 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13877 return; 13878 13879 /* 13880 * We need to send a msg to the serializing shelf to finish the IO 13881 * as well. We don't send a finish message to the other shelf if 13882 * this is a task management command. Task management commands 13883 * aren't serialized in the OOA queue, but rather just executed on 13884 * both shelf controllers for commands that originated on that 13885 * controller. 13886 */ 13887 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13888 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13889 union ctl_ha_msg msg_io; 13890 13891 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13892 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13893 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13894 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13895 } 13896 /* continue on to finish IO */ 13897 } 13898#ifdef CTL_IO_DELAY 13899 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13900 struct ctl_lun *lun; 13901 13902 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13903 13904 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13905 } else { 13906 struct ctl_lun *lun; 13907 13908 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13909 13910 if ((lun != NULL) 13911 && (lun->delay_info.done_delay > 0)) { 13912 struct callout *callout; 13913 13914 callout = (struct callout *)&io->io_hdr.timer_bytes; 13915 callout_init(callout, /*mpsafe*/ 1); 13916 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13917 callout_reset(callout, 13918 lun->delay_info.done_delay * hz, 13919 ctl_done_timer_wakeup, io); 13920 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13921 lun->delay_info.done_delay = 0; 13922 return; 13923 } 13924 } 13925#endif /* CTL_IO_DELAY */ 13926 13927 ctl_enqueue_done(io); 13928} 13929 13930int 13931ctl_isc(struct ctl_scsiio *ctsio) 13932{ 13933 struct ctl_lun *lun; 13934 int retval; 13935 13936 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13937 13938 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13939 13940 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13941 13942 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13943 13944 return (retval); 13945} 13946 13947 13948static void 13949ctl_work_thread(void *arg) 13950{ 13951 struct ctl_thread *thr = (struct ctl_thread *)arg; 13952 struct ctl_softc *softc = thr->ctl_softc; 13953 union ctl_io *io; 13954 int retval; 13955 13956 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13957 13958 for (;;) { 13959 retval = 0; 13960 13961 /* 13962 * We handle the queues in this order: 13963 * - ISC 13964 * - done queue (to free up resources, unblock other commands) 13965 * - RtR queue 13966 * - incoming queue 13967 * 13968 * If those queues are empty, we break out of the loop and 13969 * go to sleep. 13970 */ 13971 mtx_lock(&thr->queue_lock); 13972 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13973 if (io != NULL) { 13974 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13975 mtx_unlock(&thr->queue_lock); 13976 ctl_handle_isc(io); 13977 continue; 13978 } 13979 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13980 if (io != NULL) { 13981 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13982 /* clear any blocked commands, call fe_done */ 13983 mtx_unlock(&thr->queue_lock); 13984 retval = ctl_process_done(io); 13985 continue; 13986 } 13987 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13988 if (io != NULL) { 13989 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13990 mtx_unlock(&thr->queue_lock); 13991 if (io->io_hdr.io_type == CTL_IO_TASK) 13992 ctl_run_task(io); 13993 else 13994 ctl_scsiio_precheck(softc, &io->scsiio); 13995 continue; 13996 } 13997 if (!ctl_pause_rtr) { 13998 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13999 if (io != NULL) { 14000 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14001 mtx_unlock(&thr->queue_lock); 14002 retval = ctl_scsiio(&io->scsiio); 14003 if (retval != CTL_RETVAL_COMPLETE) 14004 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14005 continue; 14006 } 14007 } 14008 14009 /* Sleep until we have something to do. */ 14010 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14011 } 14012} 14013 14014static void 14015ctl_lun_thread(void *arg) 14016{ 14017 struct ctl_softc *softc = (struct ctl_softc *)arg; 14018 struct ctl_be_lun *be_lun; 14019 int retval; 14020 14021 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14022 14023 for (;;) { 14024 retval = 0; 14025 mtx_lock(&softc->ctl_lock); 14026 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14027 if (be_lun != NULL) { 14028 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14029 mtx_unlock(&softc->ctl_lock); 14030 ctl_create_lun(be_lun); 14031 continue; 14032 } 14033 14034 /* Sleep until we have something to do. */ 14035 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14036 PDROP | PRIBIO, "-", 0); 14037 } 14038} 14039 14040static void 14041ctl_enqueue_incoming(union ctl_io *io) 14042{ 14043 struct ctl_softc *softc = control_softc; 14044 struct ctl_thread *thr; 14045 u_int idx; 14046 14047 idx = (io->io_hdr.nexus.targ_port * 127 + 14048 io->io_hdr.nexus.initid.id) % worker_threads; 14049 thr = &softc->threads[idx]; 14050 mtx_lock(&thr->queue_lock); 14051 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14052 mtx_unlock(&thr->queue_lock); 14053 wakeup(thr); 14054} 14055 14056static void 14057ctl_enqueue_rtr(union ctl_io *io) 14058{ 14059 struct ctl_softc *softc = control_softc; 14060 struct ctl_thread *thr; 14061 14062 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14063 mtx_lock(&thr->queue_lock); 14064 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14065 mtx_unlock(&thr->queue_lock); 14066 wakeup(thr); 14067} 14068 14069static void 14070ctl_enqueue_done(union ctl_io *io) 14071{ 14072 struct ctl_softc *softc = control_softc; 14073 struct ctl_thread *thr; 14074 14075 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14076 mtx_lock(&thr->queue_lock); 14077 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14078 mtx_unlock(&thr->queue_lock); 14079 wakeup(thr); 14080} 14081 14082static void 14083ctl_enqueue_isc(union ctl_io *io) 14084{ 14085 struct ctl_softc *softc = control_softc; 14086 struct ctl_thread *thr; 14087 14088 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14089 mtx_lock(&thr->queue_lock); 14090 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14091 mtx_unlock(&thr->queue_lock); 14092 wakeup(thr); 14093} 14094 14095/* Initialization and failover */ 14096 14097void 14098ctl_init_isc_msg(void) 14099{ 14100 printf("CTL: Still calling this thing\n"); 14101} 14102 14103/* 14104 * Init component 14105 * Initializes component into configuration defined by bootMode 14106 * (see hasc-sv.c) 14107 * returns hasc_Status: 14108 * OK 14109 * ERROR - fatal error 14110 */ 14111static ctl_ha_comp_status 14112ctl_isc_init(struct ctl_ha_component *c) 14113{ 14114 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14115 14116 c->status = ret; 14117 return ret; 14118} 14119 14120/* Start component 14121 * Starts component in state requested. If component starts successfully, 14122 * it must set its own state to the requestrd state 14123 * When requested state is HASC_STATE_HA, the component may refine it 14124 * by adding _SLAVE or _MASTER flags. 14125 * Currently allowed state transitions are: 14126 * UNKNOWN->HA - initial startup 14127 * UNKNOWN->SINGLE - initial startup when no parter detected 14128 * HA->SINGLE - failover 14129 * returns ctl_ha_comp_status: 14130 * OK - component successfully started in requested state 14131 * FAILED - could not start the requested state, failover may 14132 * be possible 14133 * ERROR - fatal error detected, no future startup possible 14134 */ 14135static ctl_ha_comp_status 14136ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14137{ 14138 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14139 14140 printf("%s: go\n", __func__); 14141 14142 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14143 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14144 ctl_is_single = 0; 14145 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14146 != CTL_HA_STATUS_SUCCESS) { 14147 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14148 ret = CTL_HA_COMP_STATUS_ERROR; 14149 } 14150 } else if (CTL_HA_STATE_IS_HA(c->state) 14151 && CTL_HA_STATE_IS_SINGLE(state)){ 14152 // HA->SINGLE transition 14153 ctl_failover(); 14154 ctl_is_single = 1; 14155 } else { 14156 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14157 c->state, state); 14158 ret = CTL_HA_COMP_STATUS_ERROR; 14159 } 14160 if (CTL_HA_STATE_IS_SINGLE(state)) 14161 ctl_is_single = 1; 14162 14163 c->state = state; 14164 c->status = ret; 14165 return ret; 14166} 14167 14168/* 14169 * Quiesce component 14170 * The component must clear any error conditions (set status to OK) and 14171 * prepare itself to another Start call 14172 * returns ctl_ha_comp_status: 14173 * OK 14174 * ERROR 14175 */ 14176static ctl_ha_comp_status 14177ctl_isc_quiesce(struct ctl_ha_component *c) 14178{ 14179 int ret = CTL_HA_COMP_STATUS_OK; 14180 14181 ctl_pause_rtr = 1; 14182 c->status = ret; 14183 return ret; 14184} 14185 14186struct ctl_ha_component ctl_ha_component_ctlisc = 14187{ 14188 .name = "CTL ISC", 14189 .state = CTL_HA_STATE_UNKNOWN, 14190 .init = ctl_isc_init, 14191 .start = ctl_isc_start, 14192 .quiesce = ctl_isc_quiesce 14193}; 14194 14195/* 14196 * vim: ts=8 14197 */ 14198