ctl.c revision 286406
177701Sbrian/*- 277701Sbrian * Copyright (c) 2003-2009 Silicon Graphics International Corp. 377701Sbrian * Copyright (c) 2012 The FreeBSD Foundation 477701Sbrian * All rights reserved. 577701Sbrian * 677701Sbrian * Portions of this software were developed by Edward Tomasz Napierala 777701Sbrian * under sponsorship from the FreeBSD Foundation. 877701Sbrian * 977701Sbrian * Redistribution and use in source and binary forms, with or without 1077701Sbrian * modification, are permitted provided that the following conditions 1177701Sbrian * are met: 1277701Sbrian * 1. Redistributions of source code must retain the above copyright 1377701Sbrian * notice, this list of conditions, and the following disclaimer, 1477701Sbrian * without modification. 1577701Sbrian * 2. Redistributions in binary form must reproduce at minimum a disclaimer 1677701Sbrian * substantially similar to the "NO WARRANTY" disclaimer below 1777701Sbrian * ("Disclaimer") and any redistribution must be conditioned upon 1877701Sbrian * including a substantially similar Disclaimer requirement for further 1977701Sbrian * binary redistribution. 2077701Sbrian * 2177701Sbrian * NO WARRANTY 2277701Sbrian * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 2377701Sbrian * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 2477701Sbrian * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 2577701Sbrian * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2677701Sbrian * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2777701Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2877701Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2926026Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 3026026Sbrian * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 3126026Sbrian * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3226026Sbrian * POSSIBILITY OF SUCH DAMAGES. 3326026Sbrian * 3426026Sbrian * $Id$ 3526026Sbrian */ 3626026Sbrian/* 3726026Sbrian * CAM Target Layer, a SCSI device emulation subsystem. 3826026Sbrian * 3926026Sbrian * Author: Ken Merry <ken@FreeBSD.org> 4026026Sbrian */ 4126026Sbrian 4226026Sbrian#define _CTL_C 4326026Sbrian 4426026Sbrian#include <sys/cdefs.h> 4526026Sbrian__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 286406 2015-08-07 08:30:43Z araujo $"); 4626026Sbrian 4726026Sbrian#include <sys/param.h> 4826026Sbrian#include <sys/systm.h> 4926026Sbrian#include <sys/ctype.h> 5026026Sbrian#include <sys/kernel.h> 5126026Sbrian#include <sys/types.h> 5226026Sbrian#include <sys/kthread.h> 5326026Sbrian#include <sys/bio.h> 5426026Sbrian#include <sys/fcntl.h> 5526026Sbrian#include <sys/lock.h> 5626026Sbrian#include <sys/module.h> 5726026Sbrian#include <sys/mutex.h> 5826026Sbrian#include <sys/condvar.h> 5926026Sbrian#include <sys/malloc.h> 6026026Sbrian#include <sys/conf.h> 6126026Sbrian#include <sys/ioccom.h> 6226026Sbrian#include <sys/queue.h> 6326026Sbrian#include <sys/sbuf.h> 6426026Sbrian#include <sys/smp.h> 6526026Sbrian#include <sys/endian.h> 6626026Sbrian#include <sys/sysctl.h> 6726026Sbrian#include <vm/uma.h> 6826026Sbrian 6926026Sbrian#include <cam/cam.h> 7026026Sbrian#include <cam/scsi/scsi_all.h> 7126026Sbrian#include <cam/scsi/scsi_da.h> 7226026Sbrian#include <cam/ctl/ctl_io.h> 7326026Sbrian#include <cam/ctl/ctl.h> 7426026Sbrian#include <cam/ctl/ctl_frontend.h> 7526026Sbrian#include <cam/ctl/ctl_frontend_internal.h> 7626026Sbrian#include <cam/ctl/ctl_util.h> 7726026Sbrian#include <cam/ctl/ctl_backend.h> 7826026Sbrian#include <cam/ctl/ctl_ioctl.h> 7926026Sbrian#include <cam/ctl/ctl_ha.h> 8026026Sbrian#include <cam/ctl/ctl_private.h> 8126026Sbrian#include <cam/ctl/ctl_debug.h> 8226026Sbrian#include <cam/ctl/ctl_scsi_all.h> 8326026Sbrian#include <cam/ctl/ctl_error.h> 8426026Sbrian 8526026Sbrianstruct ctl_softc *control_softc = NULL; 8626026Sbrian 8726026Sbrian/* 8826026Sbrian * Size and alignment macros needed for Copan-specific HA hardware. These 8926026Sbrian * can go away when the HA code is re-written, and uses busdma for any 9026026Sbrian * hardware. 9126026Sbrian */ 9226026Sbrian#define CTL_ALIGN_8B(target, source, type) \ 9326026Sbrian if (((uint32_t)source & 0x7) != 0) \ 9426026Sbrian target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 9526026Sbrian else \ 9626026Sbrian target = (type)source; 9726026Sbrian 9826026Sbrian#define CTL_SIZE_8B(target, size) \ 9926026Sbrian if ((size & 0x7) != 0) \ 10026026Sbrian target = size + (0x8 - (size & 0x7)); \ 10126026Sbrian else \ 10226026Sbrian target = size; 10326026Sbrian 10426026Sbrian#define CTL_ALIGN_8B_MARGIN 16 10526026Sbrian 10626026Sbrian/* 10726026Sbrian * Template mode pages. 10826026Sbrian */ 10926026Sbrian 11026026Sbrian/* 11126026Sbrian * Note that these are default values only. The actual values will be 11226026Sbrian * filled in when the user does a mode sense. 11326026Sbrian */ 11426026Sbrianconst static struct copan_debugconf_subpage debugconf_page_default = { 11526026Sbrian DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 11626026Sbrian DBGCNF_SUBPAGE_CODE, /* subpage */ 11726026Sbrian {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 11826026Sbrian (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 11926026Sbrian DBGCNF_VERSION, /* page_version */ 12026026Sbrian {CTL_TIME_IO_DEFAULT_SECS>>8, 12126026Sbrian CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 12226026Sbrian}; 12326026Sbrian 12426026Sbrianconst static struct copan_debugconf_subpage debugconf_page_changeable = { 12526026Sbrian DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 12626026Sbrian DBGCNF_SUBPAGE_CODE, /* subpage */ 12726026Sbrian {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 12826026Sbrian (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 12926026Sbrian 0, /* page_version */ 13026026Sbrian {0xff,0xff}, /* ctl_time_io_secs */ 13126026Sbrian}; 13226026Sbrian 13326026Sbrianconst static struct scsi_da_rw_recovery_page rw_er_page_default = { 13426026Sbrian /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 13526026Sbrian /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 13626026Sbrian /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 13726026Sbrian /*read_retry_count*/0, 13826026Sbrian /*correction_span*/0, 13926026Sbrian /*head_offset_count*/0, 14026026Sbrian /*data_strobe_offset_cnt*/0, 14126026Sbrian /*byte8*/SMS_RWER_LBPERE, 14226026Sbrian /*write_retry_count*/0, 14326026Sbrian /*reserved2*/0, 14426026Sbrian /*recovery_time_limit*/{0, 0}, 14526026Sbrian}; 14626026Sbrian 14726026Sbrianconst static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 14826026Sbrian /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 14926026Sbrian /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 15026026Sbrian /*byte3*/0, 15126026Sbrian /*read_retry_count*/0, 15226026Sbrian /*correction_span*/0, 15326026Sbrian /*head_offset_count*/0, 15426026Sbrian /*data_strobe_offset_cnt*/0, 15526026Sbrian /*byte8*/0, 15626026Sbrian /*write_retry_count*/0, 15726026Sbrian /*reserved2*/0, 15826026Sbrian /*recovery_time_limit*/{0, 0}, 15926026Sbrian}; 16026026Sbrian 16126026Sbrianconst static struct scsi_format_page format_page_default = { 16226026Sbrian /*page_code*/SMS_FORMAT_DEVICE_PAGE, 16326026Sbrian /*page_length*/sizeof(struct scsi_format_page) - 2, 16426026Sbrian /*tracks_per_zone*/ {0, 0}, 16526026Sbrian /*alt_sectors_per_zone*/ {0, 0}, 16626026Sbrian /*alt_tracks_per_zone*/ {0, 0}, 16726026Sbrian /*alt_tracks_per_lun*/ {0, 0}, 16826026Sbrian /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 16926026Sbrian CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 17026026Sbrian /*bytes_per_sector*/ {0, 0}, 17126026Sbrian /*interleave*/ {0, 0}, 17226026Sbrian /*track_skew*/ {0, 0}, 17326026Sbrian /*cylinder_skew*/ {0, 0}, 17426026Sbrian /*flags*/ SFP_HSEC, 17526026Sbrian /*reserved*/ {0, 0, 0} 17626026Sbrian}; 17726026Sbrian 17826026Sbrianconst static struct scsi_format_page format_page_changeable = { 17926026Sbrian /*page_code*/SMS_FORMAT_DEVICE_PAGE, 18026026Sbrian /*page_length*/sizeof(struct scsi_format_page) - 2, 18126026Sbrian /*tracks_per_zone*/ {0, 0}, 18226026Sbrian /*alt_sectors_per_zone*/ {0, 0}, 18326026Sbrian /*alt_tracks_per_zone*/ {0, 0}, 18426026Sbrian /*alt_tracks_per_lun*/ {0, 0}, 18526026Sbrian /*sectors_per_track*/ {0, 0}, 18626026Sbrian /*bytes_per_sector*/ {0, 0}, 18726026Sbrian /*interleave*/ {0, 0}, 18826026Sbrian /*track_skew*/ {0, 0}, 18926026Sbrian /*cylinder_skew*/ {0, 0}, 19026026Sbrian /*flags*/ 0, 19126026Sbrian /*reserved*/ {0, 0, 0} 19226026Sbrian}; 19326026Sbrian 19426026Sbrianconst static struct scsi_rigid_disk_page rigid_disk_page_default = { 19526026Sbrian /*page_code*/SMS_RIGID_DISK_PAGE, 19626026Sbrian /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 19726026Sbrian /*cylinders*/ {0, 0, 0}, 19826026Sbrian /*heads*/ CTL_DEFAULT_HEADS, 19926026Sbrian /*start_write_precomp*/ {0, 0, 0}, 20026026Sbrian /*start_reduced_current*/ {0, 0, 0}, 20126026Sbrian /*step_rate*/ {0, 0}, 20226026Sbrian /*landing_zone_cylinder*/ {0, 0, 0}, 20326026Sbrian /*rpl*/ SRDP_RPL_DISABLED, 20426026Sbrian /*rotational_offset*/ 0, 20526026Sbrian /*reserved1*/ 0, 20626026Sbrian /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 20726026Sbrian CTL_DEFAULT_ROTATION_RATE & 0xff}, 20826026Sbrian /*reserved2*/ {0, 0} 20926026Sbrian}; 21026026Sbrian 21126026Sbrianconst static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 21226026Sbrian /*page_code*/SMS_RIGID_DISK_PAGE, 21326026Sbrian /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 21426026Sbrian /*cylinders*/ {0, 0, 0}, 21526026Sbrian /*heads*/ 0, 21626026Sbrian /*start_write_precomp*/ {0, 0, 0}, 21726026Sbrian /*start_reduced_current*/ {0, 0, 0}, 21826026Sbrian /*step_rate*/ {0, 0}, 21926026Sbrian /*landing_zone_cylinder*/ {0, 0, 0}, 22026026Sbrian /*rpl*/ 0, 22126026Sbrian /*rotational_offset*/ 0, 22226026Sbrian /*reserved1*/ 0, 22326026Sbrian /*rotation_rate*/ {0, 0}, 22426026Sbrian /*reserved2*/ {0, 0} 22526026Sbrian}; 22626026Sbrian 22726026Sbrianconst static struct scsi_caching_page caching_page_default = { 22826026Sbrian /*page_code*/SMS_CACHING_PAGE, 22926026Sbrian /*page_length*/sizeof(struct scsi_caching_page) - 2, 23026026Sbrian /*flags1*/ SCP_DISC | SCP_WCE, 23126026Sbrian /*ret_priority*/ 0, 23226026Sbrian /*disable_pf_transfer_len*/ {0xff, 0xff}, 23326026Sbrian /*min_prefetch*/ {0, 0}, 23426026Sbrian /*max_prefetch*/ {0xff, 0xff}, 23526026Sbrian /*max_pf_ceiling*/ {0xff, 0xff}, 23626026Sbrian /*flags2*/ 0, 23726026Sbrian /*cache_segments*/ 0, 23826026Sbrian /*cache_seg_size*/ {0, 0}, 23982050Sru /*reserved*/ 0, 24082050Sru /*non_cache_seg_size*/ {0, 0, 0} 24182050Sru}; 24282050Sru 24382050Sruconst static struct scsi_caching_page caching_page_changeable = { 24482050Sru /*page_code*/SMS_CACHING_PAGE, 24526026Sbrian /*page_length*/sizeof(struct scsi_caching_page) - 2, 24626026Sbrian /*flags1*/ SCP_WCE | SCP_RCD, 24726026Sbrian /*ret_priority*/ 0, 24867980Sru /*disable_pf_transfer_len*/ {0, 0}, 24967980Sru /*min_prefetch*/ {0, 0}, 25067980Sru /*max_prefetch*/ {0, 0}, 25126026Sbrian /*max_pf_ceiling*/ {0, 0}, 25226026Sbrian /*flags2*/ 0, 25326026Sbrian /*cache_segments*/ 0, 25426026Sbrian /*cache_seg_size*/ {0, 0}, 25581962Sbrian /*reserved*/ 0, 25626026Sbrian /*non_cache_seg_size*/ {0, 0, 0} 25736711Sbrian}; 25832377Seivind 25932377Seivindconst static struct scsi_control_page control_page_default = { 26036711Sbrian /*page_code*/SMS_CONTROL_MODE_PAGE, 26132377Seivind /*page_length*/sizeof(struct scsi_control_page) - 2, 26226026Sbrian /*rlec*/0, 26381962Sbrian /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 26426026Sbrian /*eca_and_aen*/0, 26535314Sbrian /*flags4*/SCP_TAS, 26681962Sbrian /*aen_holdoff_period*/{0, 0}, 26781962Sbrian /*busy_timeout_period*/{0, 0}, 26881962Sbrian /*extended_selftest_completion_time*/{0, 0} 26981962Sbrian}; 27081962Sbrian 27126026Sbrianconst static struct scsi_control_page control_page_changeable = { 27226026Sbrian /*page_code*/SMS_CONTROL_MODE_PAGE, 27326026Sbrian /*page_length*/sizeof(struct scsi_control_page) - 2, 27426026Sbrian /*rlec*/SCP_DSENSE, 27581962Sbrian /*queue_flags*/SCP_QUEUE_ALG_MASK, 27626026Sbrian /*eca_and_aen*/SCP_SWP, 27726026Sbrian /*flags4*/0, 27881962Sbrian /*aen_holdoff_period*/{0, 0}, 27981962Sbrian /*busy_timeout_period*/{0, 0}, 28081962Sbrian /*extended_selftest_completion_time*/{0, 0} 28181962Sbrian}; 28281962Sbrian 28326026Sbrianconst static struct scsi_info_exceptions_page ie_page_default = { 28426026Sbrian /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 28526026Sbrian /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 28626026Sbrian /*info_flags*/SIEP_FLAGS_DEXCPT, 28726026Sbrian /*mrie*/0, 28826026Sbrian /*interval_timer*/{0, 0, 0, 0}, 28926026Sbrian /*report_count*/{0, 0, 0, 0} 29026026Sbrian}; 29126026Sbrian 29226026Sbrianconst static struct scsi_info_exceptions_page ie_page_changeable = { 29326026Sbrian /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 29426026Sbrian /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 29526026Sbrian /*info_flags*/0, 29626026Sbrian /*mrie*/0, 29726026Sbrian /*interval_timer*/{0, 0, 0, 0}, 29826026Sbrian /*report_count*/{0, 0, 0, 0} 29926026Sbrian}; 30026026Sbrian 30126026Sbrian#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 30226026Sbrian 30326026Sbrianconst static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 30426026Sbrian /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 30526026Sbrian /*subpage_code*/0x02, 30626026Sbrian /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 30726026Sbrian /*flags*/0, 30826026Sbrian /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 30926026Sbrian /*descr*/{}}, 31026026Sbrian {{/*flags*/0, 31126026Sbrian /*resource*/0x01, 31226026Sbrian /*reserved*/{0, 0}, 31326026Sbrian /*count*/{0, 0, 0, 0}}, 31426026Sbrian {/*flags*/0, 31526026Sbrian /*resource*/0x02, 31626026Sbrian /*reserved*/{0, 0}, 31726026Sbrian /*count*/{0, 0, 0, 0}}, 31826026Sbrian {/*flags*/0, 31926026Sbrian /*resource*/0xf1, 32026026Sbrian /*reserved*/{0, 0}, 32126026Sbrian /*count*/{0, 0, 0, 0}}, 32226026Sbrian {/*flags*/0, 32326026Sbrian /*resource*/0xf2, 32426026Sbrian /*reserved*/{0, 0}, 32526026Sbrian /*count*/{0, 0, 0, 0}} 32626026Sbrian } 32726026Sbrian}; 32826026Sbrian 32926026Sbrianconst static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 33026026Sbrian /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 33126026Sbrian /*subpage_code*/0x02, 33226026Sbrian /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 33326026Sbrian /*flags*/0, 33426026Sbrian /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 33526026Sbrian /*descr*/{}}, 33626026Sbrian {{/*flags*/0, 33726026Sbrian /*resource*/0, 33826026Sbrian /*reserved*/{0, 0}, 33926026Sbrian /*count*/{0, 0, 0, 0}}, 34026026Sbrian {/*flags*/0, 34126026Sbrian /*resource*/0, 34226026Sbrian /*reserved*/{0, 0}, 34326026Sbrian /*count*/{0, 0, 0, 0}}, 34426026Sbrian {/*flags*/0, 34526026Sbrian /*resource*/0, 34626026Sbrian /*reserved*/{0, 0}, 34726026Sbrian /*count*/{0, 0, 0, 0}}, 34826026Sbrian {/*flags*/0, 34926026Sbrian /*resource*/0, 35026026Sbrian /*reserved*/{0, 0}, 35126026Sbrian /*count*/{0, 0, 0, 0}} 35226026Sbrian } 35326026Sbrian}; 35426026Sbrian 35526026Sbrian/* 35626026Sbrian * XXX KDM move these into the softc. 357 */ 358static int rcv_sync_msg; 359static uint8_t ctl_pause_rtr; 360 361SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 362static int worker_threads = -1; 363SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 364 &worker_threads, 1, "Number of worker threads"); 365static int ctl_debug = CTL_DEBUG_NONE; 366SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 367 &ctl_debug, 0, "Enabled debug flags"); 368 369/* 370 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 371 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 372 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 373 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 374 */ 375#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 376 377static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 378 int param); 379static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 380static int ctl_init(void); 381void ctl_shutdown(void); 382static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 383static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 384static void ctl_ioctl_online(void *arg); 385static void ctl_ioctl_offline(void *arg); 386static int ctl_ioctl_lun_enable(void *arg, int lun_id); 387static int ctl_ioctl_lun_disable(void *arg, int lun_id); 388static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 389static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 390static int ctl_ioctl_submit_wait(union ctl_io *io); 391static void ctl_ioctl_datamove(union ctl_io *io); 392static void ctl_ioctl_done(union ctl_io *io); 393static void ctl_ioctl_hard_startstop_callback(void *arg, 394 struct cfi_metatask *metatask); 395static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 396static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 397 struct ctl_ooa *ooa_hdr, 398 struct ctl_ooa_entry *kern_entries); 399static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 400 struct thread *td); 401static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 402 struct ctl_be_lun *be_lun); 403static int ctl_free_lun(struct ctl_lun *lun); 404static void ctl_create_lun(struct ctl_be_lun *be_lun); 405static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 406/** 407static void ctl_failover_change_pages(struct ctl_softc *softc, 408 struct ctl_scsiio *ctsio, int master); 409**/ 410 411static int ctl_do_mode_select(union ctl_io *io); 412static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 413 uint64_t res_key, uint64_t sa_res_key, 414 uint8_t type, uint32_t residx, 415 struct ctl_scsiio *ctsio, 416 struct scsi_per_res_out *cdb, 417 struct scsi_per_res_out_parms* param); 418static void ctl_pro_preempt_other(struct ctl_lun *lun, 419 union ctl_ha_msg *msg); 420static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 421static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 422static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 423static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 424static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 425static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 426static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 427 int alloc_len); 428static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 429 int alloc_len); 430static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 431static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 432static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 433static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 434static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 435static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 436 bool seq); 437static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 438static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 439 union ctl_io *pending_io, union ctl_io *ooa_io); 440static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 441 union ctl_io *starting_io); 442static int ctl_check_blocked(struct ctl_lun *lun); 443static int ctl_scsiio_lun_check(struct ctl_lun *lun, 444 const struct ctl_cmd_entry *entry, 445 struct ctl_scsiio *ctsio); 446//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 447static void ctl_failover(void); 448static void ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 449 ctl_ua_type ua_type); 450static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 451 struct ctl_scsiio *ctsio); 452static int ctl_scsiio(struct ctl_scsiio *ctsio); 453 454static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 455static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 456 ctl_ua_type ua_type); 457static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 458 ctl_ua_type ua_type); 459static int ctl_abort_task(union ctl_io *io); 460static int ctl_abort_task_set(union ctl_io *io); 461static int ctl_i_t_nexus_reset(union ctl_io *io); 462static void ctl_run_task(union ctl_io *io); 463#ifdef CTL_IO_DELAY 464static void ctl_datamove_timer_wakeup(void *arg); 465static void ctl_done_timer_wakeup(void *arg); 466#endif /* CTL_IO_DELAY */ 467 468static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 469static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 470static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 471static void ctl_datamove_remote_write(union ctl_io *io); 472static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 473static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 474static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 475static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 476 ctl_ha_dt_cb callback); 477static void ctl_datamove_remote_read(union ctl_io *io); 478static void ctl_datamove_remote(union ctl_io *io); 479static int ctl_process_done(union ctl_io *io); 480static void ctl_lun_thread(void *arg); 481static void ctl_thresh_thread(void *arg); 482static void ctl_work_thread(void *arg); 483static void ctl_enqueue_incoming(union ctl_io *io); 484static void ctl_enqueue_rtr(union ctl_io *io); 485static void ctl_enqueue_done(union ctl_io *io); 486static void ctl_enqueue_isc(union ctl_io *io); 487static const struct ctl_cmd_entry * 488 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 489static const struct ctl_cmd_entry * 490 ctl_validate_command(struct ctl_scsiio *ctsio); 491static int ctl_cmd_applicable(uint8_t lun_type, 492 const struct ctl_cmd_entry *entry); 493 494/* 495 * Load the serialization table. This isn't very pretty, but is probably 496 * the easiest way to do it. 497 */ 498#include "ctl_ser_table.c" 499 500/* 501 * We only need to define open, close and ioctl routines for this driver. 502 */ 503static struct cdevsw ctl_cdevsw = { 504 .d_version = D_VERSION, 505 .d_flags = 0, 506 .d_open = ctl_open, 507 .d_close = ctl_close, 508 .d_ioctl = ctl_ioctl, 509 .d_name = "ctl", 510}; 511 512 513MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 514 515static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 516 517static moduledata_t ctl_moduledata = { 518 "ctl", 519 ctl_module_event_handler, 520 NULL 521}; 522 523DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 524MODULE_VERSION(ctl, 1); 525 526static struct ctl_frontend ioctl_frontend = 527{ 528 .name = "ioctl", 529}; 530 531static void 532ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 533 union ctl_ha_msg *msg_info) 534{ 535 struct ctl_scsiio *ctsio; 536 537 if (msg_info->hdr.original_sc == NULL) { 538 printf("%s: original_sc == NULL!\n", __func__); 539 /* XXX KDM now what? */ 540 return; 541 } 542 543 ctsio = &msg_info->hdr.original_sc->scsiio; 544 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 545 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 546 ctsio->io_hdr.status = msg_info->hdr.status; 547 ctsio->scsi_status = msg_info->scsi.scsi_status; 548 ctsio->sense_len = msg_info->scsi.sense_len; 549 ctsio->sense_residual = msg_info->scsi.sense_residual; 550 ctsio->residual = msg_info->scsi.residual; 551 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 552 sizeof(ctsio->sense_data)); 553 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 554 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 555 ctl_enqueue_isc((union ctl_io *)ctsio); 556} 557 558static void 559ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 560 union ctl_ha_msg *msg_info) 561{ 562 struct ctl_scsiio *ctsio; 563 564 if (msg_info->hdr.serializing_sc == NULL) { 565 printf("%s: serializing_sc == NULL!\n", __func__); 566 /* XXX KDM now what? */ 567 return; 568 } 569 570 ctsio = &msg_info->hdr.serializing_sc->scsiio; 571#if 0 572 /* 573 * Attempt to catch the situation where an I/O has 574 * been freed, and we're using it again. 575 */ 576 if (ctsio->io_hdr.io_type == 0xff) { 577 union ctl_io *tmp_io; 578 tmp_io = (union ctl_io *)ctsio; 579 printf("%s: %p use after free!\n", __func__, 580 ctsio); 581 printf("%s: type %d msg %d cdb %x iptl: " 582 "%d:%d:%d:%d tag 0x%04x " 583 "flag %#x status %x\n", 584 __func__, 585 tmp_io->io_hdr.io_type, 586 tmp_io->io_hdr.msg_type, 587 tmp_io->scsiio.cdb[0], 588 tmp_io->io_hdr.nexus.initid.id, 589 tmp_io->io_hdr.nexus.targ_port, 590 tmp_io->io_hdr.nexus.targ_target.id, 591 tmp_io->io_hdr.nexus.targ_lun, 592 (tmp_io->io_hdr.io_type == 593 CTL_IO_TASK) ? 594 tmp_io->taskio.tag_num : 595 tmp_io->scsiio.tag_num, 596 tmp_io->io_hdr.flags, 597 tmp_io->io_hdr.status); 598 } 599#endif 600 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 601 ctl_enqueue_isc((union ctl_io *)ctsio); 602} 603 604/* 605 * ISC (Inter Shelf Communication) event handler. Events from the HA 606 * subsystem come in here. 607 */ 608static void 609ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 610{ 611 struct ctl_softc *softc; 612 union ctl_io *io; 613 struct ctl_prio *presio; 614 ctl_ha_status isc_status; 615 616 softc = control_softc; 617 io = NULL; 618 619 620#if 0 621 printf("CTL: Isc Msg event %d\n", event); 622#endif 623 if (event == CTL_HA_EVT_MSG_RECV) { 624 union ctl_ha_msg msg_info; 625 626 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 627 sizeof(msg_info), /*wait*/ 0); 628#if 0 629 printf("CTL: msg_type %d\n", msg_info.msg_type); 630#endif 631 if (isc_status != 0) { 632 printf("Error receiving message, status = %d\n", 633 isc_status); 634 return; 635 } 636 637 switch (msg_info.hdr.msg_type) { 638 case CTL_MSG_SERIALIZE: 639#if 0 640 printf("Serialize\n"); 641#endif 642 io = ctl_alloc_io_nowait(softc->othersc_pool); 643 if (io == NULL) { 644 printf("ctl_isc_event_handler: can't allocate " 645 "ctl_io!\n"); 646 /* Bad Juju */ 647 /* Need to set busy and send msg back */ 648 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 649 msg_info.hdr.status = CTL_SCSI_ERROR; 650 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 651 msg_info.scsi.sense_len = 0; 652 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 653 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 654 } 655 goto bailout; 656 } 657 ctl_zero_io(io); 658 // populate ctsio from msg_info 659 io->io_hdr.io_type = CTL_IO_SCSI; 660 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 661 io->io_hdr.original_sc = msg_info.hdr.original_sc; 662#if 0 663 printf("pOrig %x\n", (int)msg_info.original_sc); 664#endif 665 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 666 CTL_FLAG_IO_ACTIVE; 667 /* 668 * If we're in serialization-only mode, we don't 669 * want to go through full done processing. Thus 670 * the COPY flag. 671 * 672 * XXX KDM add another flag that is more specific. 673 */ 674 if (softc->ha_mode == CTL_HA_MODE_SER_ONLY) 675 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 676 io->io_hdr.nexus = msg_info.hdr.nexus; 677#if 0 678 printf("targ %d, port %d, iid %d, lun %d\n", 679 io->io_hdr.nexus.targ_target.id, 680 io->io_hdr.nexus.targ_port, 681 io->io_hdr.nexus.initid.id, 682 io->io_hdr.nexus.targ_lun); 683#endif 684 io->scsiio.tag_num = msg_info.scsi.tag_num; 685 io->scsiio.tag_type = msg_info.scsi.tag_type; 686 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 687 CTL_MAX_CDBLEN); 688 if (softc->ha_mode == CTL_HA_MODE_XFER) { 689 const struct ctl_cmd_entry *entry; 690 691 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 692 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 693 io->io_hdr.flags |= 694 entry->flags & CTL_FLAG_DATA_MASK; 695 } 696 ctl_enqueue_isc(io); 697 break; 698 699 /* Performed on the Originating SC, XFER mode only */ 700 case CTL_MSG_DATAMOVE: { 701 struct ctl_sg_entry *sgl; 702 int i, j; 703 704 io = msg_info.hdr.original_sc; 705 if (io == NULL) { 706 printf("%s: original_sc == NULL!\n", __func__); 707 /* XXX KDM do something here */ 708 break; 709 } 710 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 711 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 712 /* 713 * Keep track of this, we need to send it back over 714 * when the datamove is complete. 715 */ 716 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 717 718 if (msg_info.dt.sg_sequence == 0) { 719 /* 720 * XXX KDM we use the preallocated S/G list 721 * here, but we'll need to change this to 722 * dynamic allocation if we need larger S/G 723 * lists. 724 */ 725 if (msg_info.dt.kern_sg_entries > 726 sizeof(io->io_hdr.remote_sglist) / 727 sizeof(io->io_hdr.remote_sglist[0])) { 728 printf("%s: number of S/G entries " 729 "needed %u > allocated num %zd\n", 730 __func__, 731 msg_info.dt.kern_sg_entries, 732 sizeof(io->io_hdr.remote_sglist)/ 733 sizeof(io->io_hdr.remote_sglist[0])); 734 735 /* 736 * XXX KDM send a message back to 737 * the other side to shut down the 738 * DMA. The error will come back 739 * through via the normal channel. 740 */ 741 break; 742 } 743 sgl = io->io_hdr.remote_sglist; 744 memset(sgl, 0, 745 sizeof(io->io_hdr.remote_sglist)); 746 747 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 748 749 io->scsiio.kern_sg_entries = 750 msg_info.dt.kern_sg_entries; 751 io->scsiio.rem_sg_entries = 752 msg_info.dt.kern_sg_entries; 753 io->scsiio.kern_data_len = 754 msg_info.dt.kern_data_len; 755 io->scsiio.kern_total_len = 756 msg_info.dt.kern_total_len; 757 io->scsiio.kern_data_resid = 758 msg_info.dt.kern_data_resid; 759 io->scsiio.kern_rel_offset = 760 msg_info.dt.kern_rel_offset; 761 /* 762 * Clear out per-DMA flags. 763 */ 764 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 765 /* 766 * Add per-DMA flags that are set for this 767 * particular DMA request. 768 */ 769 io->io_hdr.flags |= msg_info.dt.flags & 770 CTL_FLAG_RDMA_MASK; 771 } else 772 sgl = (struct ctl_sg_entry *) 773 io->scsiio.kern_data_ptr; 774 775 for (i = msg_info.dt.sent_sg_entries, j = 0; 776 i < (msg_info.dt.sent_sg_entries + 777 msg_info.dt.cur_sg_entries); i++, j++) { 778 sgl[i].addr = msg_info.dt.sg_list[j].addr; 779 sgl[i].len = msg_info.dt.sg_list[j].len; 780 781#if 0 782 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 783 __func__, 784 msg_info.dt.sg_list[j].addr, 785 msg_info.dt.sg_list[j].len, 786 sgl[i].addr, sgl[i].len, j, i); 787#endif 788 } 789#if 0 790 memcpy(&sgl[msg_info.dt.sent_sg_entries], 791 msg_info.dt.sg_list, 792 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 793#endif 794 795 /* 796 * If this is the last piece of the I/O, we've got 797 * the full S/G list. Queue processing in the thread. 798 * Otherwise wait for the next piece. 799 */ 800 if (msg_info.dt.sg_last != 0) 801 ctl_enqueue_isc(io); 802 break; 803 } 804 /* Performed on the Serializing (primary) SC, XFER mode only */ 805 case CTL_MSG_DATAMOVE_DONE: { 806 if (msg_info.hdr.serializing_sc == NULL) { 807 printf("%s: serializing_sc == NULL!\n", 808 __func__); 809 /* XXX KDM now what? */ 810 break; 811 } 812 /* 813 * We grab the sense information here in case 814 * there was a failure, so we can return status 815 * back to the initiator. 816 */ 817 io = msg_info.hdr.serializing_sc; 818 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 819 io->io_hdr.status = msg_info.hdr.status; 820 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 821 io->scsiio.sense_len = msg_info.scsi.sense_len; 822 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 823 io->io_hdr.port_status = msg_info.scsi.fetd_status; 824 io->scsiio.residual = msg_info.scsi.residual; 825 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 826 sizeof(io->scsiio.sense_data)); 827 ctl_enqueue_isc(io); 828 break; 829 } 830 831 /* Preformed on Originating SC, SER_ONLY mode */ 832 case CTL_MSG_R2R: 833 io = msg_info.hdr.original_sc; 834 if (io == NULL) { 835 printf("%s: Major Bummer\n", __func__); 836 return; 837 } else { 838#if 0 839 printf("pOrig %x\n",(int) ctsio); 840#endif 841 } 842 io->io_hdr.msg_type = CTL_MSG_R2R; 843 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 844 ctl_enqueue_isc(io); 845 break; 846 847 /* 848 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 849 * mode. 850 * Performed on the Originating (i.e. secondary) SC in XFER 851 * mode 852 */ 853 case CTL_MSG_FINISH_IO: 854 if (softc->ha_mode == CTL_HA_MODE_XFER) 855 ctl_isc_handler_finish_xfer(softc, 856 &msg_info); 857 else 858 ctl_isc_handler_finish_ser_only(softc, 859 &msg_info); 860 break; 861 862 /* Preformed on Originating SC */ 863 case CTL_MSG_BAD_JUJU: 864 io = msg_info.hdr.original_sc; 865 if (io == NULL) { 866 printf("%s: Bad JUJU!, original_sc is NULL!\n", 867 __func__); 868 break; 869 } 870 ctl_copy_sense_data(&msg_info, io); 871 /* 872 * IO should have already been cleaned up on other 873 * SC so clear this flag so we won't send a message 874 * back to finish the IO there. 875 */ 876 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 877 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 878 879 /* io = msg_info.hdr.serializing_sc; */ 880 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 881 ctl_enqueue_isc(io); 882 break; 883 884 /* Handle resets sent from the other side */ 885 case CTL_MSG_MANAGE_TASKS: { 886 struct ctl_taskio *taskio; 887 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 888 softc->othersc_pool); 889 if (taskio == NULL) { 890 printf("ctl_isc_event_handler: can't allocate " 891 "ctl_io!\n"); 892 /* Bad Juju */ 893 /* should I just call the proper reset func 894 here??? */ 895 goto bailout; 896 } 897 ctl_zero_io((union ctl_io *)taskio); 898 taskio->io_hdr.io_type = CTL_IO_TASK; 899 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 900 taskio->io_hdr.nexus = msg_info.hdr.nexus; 901 taskio->task_action = msg_info.task.task_action; 902 taskio->tag_num = msg_info.task.tag_num; 903 taskio->tag_type = msg_info.task.tag_type; 904#ifdef CTL_TIME_IO 905 taskio->io_hdr.start_time = time_uptime; 906 getbintime(&taskio->io_hdr.start_bt); 907#if 0 908 cs_prof_gettime(&taskio->io_hdr.start_ticks); 909#endif 910#endif /* CTL_TIME_IO */ 911 ctl_run_task((union ctl_io *)taskio); 912 break; 913 } 914 /* Persistent Reserve action which needs attention */ 915 case CTL_MSG_PERS_ACTION: 916 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 917 softc->othersc_pool); 918 if (presio == NULL) { 919 printf("ctl_isc_event_handler: can't allocate " 920 "ctl_io!\n"); 921 /* Bad Juju */ 922 /* Need to set busy and send msg back */ 923 goto bailout; 924 } 925 ctl_zero_io((union ctl_io *)presio); 926 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 927 presio->pr_msg = msg_info.pr; 928 ctl_enqueue_isc((union ctl_io *)presio); 929 break; 930 case CTL_MSG_SYNC_FE: 931 rcv_sync_msg = 1; 932 break; 933 default: 934 printf("How did I get here?\n"); 935 } 936 } else if (event == CTL_HA_EVT_MSG_SENT) { 937 if (param != CTL_HA_STATUS_SUCCESS) { 938 printf("Bad status from ctl_ha_msg_send status %d\n", 939 param); 940 } 941 return; 942 } else if (event == CTL_HA_EVT_DISCONNECT) { 943 printf("CTL: Got a disconnect from Isc\n"); 944 return; 945 } else { 946 printf("ctl_isc_event_handler: Unknown event %d\n", event); 947 return; 948 } 949 950bailout: 951 return; 952} 953 954static void 955ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 956{ 957 struct scsi_sense_data *sense; 958 959 sense = &dest->scsiio.sense_data; 960 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 961 dest->scsiio.scsi_status = src->scsi.scsi_status; 962 dest->scsiio.sense_len = src->scsi.sense_len; 963 dest->io_hdr.status = src->hdr.status; 964} 965 966static void 967ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 968{ 969 ctl_ua_type *pu; 970 971 mtx_assert(&lun->lun_lock, MA_OWNED); 972 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 973 if (pu == NULL) 974 return; 975 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 976} 977 978static void 979ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 980{ 981 int i, j; 982 983 mtx_assert(&lun->lun_lock, MA_OWNED); 984 for (i = 0; i < CTL_MAX_PORTS; i++) { 985 if (lun->pending_ua[i] == NULL) 986 continue; 987 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 988 if (i * CTL_MAX_INIT_PER_PORT + j == except) 989 continue; 990 lun->pending_ua[i][j] |= ua; 991 } 992 } 993} 994 995static void 996ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 997{ 998 ctl_ua_type *pu; 999 1000 mtx_assert(&lun->lun_lock, MA_OWNED); 1001 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1002 if (pu == NULL) 1003 return; 1004 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1005} 1006 1007static void 1008ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1009{ 1010 int i, j; 1011 1012 mtx_assert(&lun->lun_lock, MA_OWNED); 1013 for (i = 0; i < CTL_MAX_PORTS; i++) { 1014 if (lun->pending_ua[i] == NULL) 1015 continue; 1016 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1017 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1018 continue; 1019 lun->pending_ua[i][j] &= ~ua; 1020 } 1021 } 1022} 1023 1024static int 1025ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 1026{ 1027 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1028 struct ctl_lun *lun; 1029 int error, value; 1030 1031 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 1032 value = 0; 1033 else 1034 value = 1; 1035 1036 error = sysctl_handle_int(oidp, &value, 0, req); 1037 if ((error != 0) || (req->newptr == NULL)) 1038 return (error); 1039 1040 mtx_lock(&softc->ctl_lock); 1041 if (value == 0) 1042 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1043 else 1044 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1045 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1046 mtx_lock(&lun->lun_lock); 1047 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1048 mtx_unlock(&lun->lun_lock); 1049 } 1050 mtx_unlock(&softc->ctl_lock); 1051 return (0); 1052} 1053 1054static int 1055ctl_init(void) 1056{ 1057 struct ctl_softc *softc; 1058 void *other_pool; 1059 struct ctl_port *port; 1060 int i, error, retval; 1061 //int isc_retval; 1062 1063 retval = 0; 1064 ctl_pause_rtr = 0; 1065 rcv_sync_msg = 0; 1066 1067 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1068 M_WAITOK | M_ZERO); 1069 softc = control_softc; 1070 1071 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1072 "cam/ctl"); 1073 1074 softc->dev->si_drv1 = softc; 1075 1076 /* 1077 * By default, return a "bad LUN" peripheral qualifier for unknown 1078 * LUNs. The user can override this default using the tunable or 1079 * sysctl. See the comment in ctl_inquiry_std() for more details. 1080 */ 1081 softc->inquiry_pq_no_lun = 1; 1082 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1083 &softc->inquiry_pq_no_lun); 1084 sysctl_ctx_init(&softc->sysctl_ctx); 1085 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1086 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1087 CTLFLAG_RD, 0, "CAM Target Layer"); 1088 1089 if (softc->sysctl_tree == NULL) { 1090 printf("%s: unable to allocate sysctl tree\n", __func__); 1091 destroy_dev(softc->dev); 1092 free(control_softc, M_DEVBUF); 1093 control_softc = NULL; 1094 return (ENOMEM); 1095 } 1096 1097 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1098 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1099 "inquiry_pq_no_lun", CTLFLAG_RW, 1100 &softc->inquiry_pq_no_lun, 0, 1101 "Report no lun possible for invalid LUNs"); 1102 1103 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1104 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1105 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1106 softc->open_count = 0; 1107 1108 /* 1109 * Default to actually sending a SYNCHRONIZE CACHE command down to 1110 * the drive. 1111 */ 1112 softc->flags = CTL_FLAG_REAL_SYNC; 1113 1114 /* 1115 * In Copan's HA scheme, the "master" and "slave" roles are 1116 * figured out through the slot the controller is in. Although it 1117 * is an active/active system, someone has to be in charge. 1118 */ 1119 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1120 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1121 "HA head ID (0 - no HA)"); 1122 if (softc->ha_id == 0) { 1123 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1124 softc->is_single = 1; 1125 softc->port_offset = 0; 1126 } else 1127 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1128 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1129 1130 STAILQ_INIT(&softc->lun_list); 1131 STAILQ_INIT(&softc->pending_lun_queue); 1132 STAILQ_INIT(&softc->fe_list); 1133 STAILQ_INIT(&softc->port_list); 1134 STAILQ_INIT(&softc->be_list); 1135 ctl_tpc_init(softc); 1136 1137 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1138 &other_pool) != 0) 1139 { 1140 printf("ctl: can't allocate %d entry other SC pool, " 1141 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1142 return (ENOMEM); 1143 } 1144 softc->othersc_pool = other_pool; 1145 1146 if (worker_threads <= 0) 1147 worker_threads = max(1, mp_ncpus / 4); 1148 if (worker_threads > CTL_MAX_THREADS) 1149 worker_threads = CTL_MAX_THREADS; 1150 1151 for (i = 0; i < worker_threads; i++) { 1152 struct ctl_thread *thr = &softc->threads[i]; 1153 1154 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1155 thr->ctl_softc = softc; 1156 STAILQ_INIT(&thr->incoming_queue); 1157 STAILQ_INIT(&thr->rtr_queue); 1158 STAILQ_INIT(&thr->done_queue); 1159 STAILQ_INIT(&thr->isc_queue); 1160 1161 error = kproc_kthread_add(ctl_work_thread, thr, 1162 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1163 if (error != 0) { 1164 printf("error creating CTL work thread!\n"); 1165 ctl_pool_free(other_pool); 1166 return (error); 1167 } 1168 } 1169 error = kproc_kthread_add(ctl_lun_thread, softc, 1170 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1171 if (error != 0) { 1172 printf("error creating CTL lun thread!\n"); 1173 ctl_pool_free(other_pool); 1174 return (error); 1175 } 1176 error = kproc_kthread_add(ctl_thresh_thread, softc, 1177 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1178 if (error != 0) { 1179 printf("error creating CTL threshold thread!\n"); 1180 ctl_pool_free(other_pool); 1181 return (error); 1182 } 1183 if (bootverbose) 1184 printf("ctl: CAM Target Layer loaded\n"); 1185 1186 /* 1187 * Initialize the ioctl front end. 1188 */ 1189 ctl_frontend_register(&ioctl_frontend); 1190 port = &softc->ioctl_info.port; 1191 port->frontend = &ioctl_frontend; 1192 sprintf(softc->ioctl_info.port_name, "ioctl"); 1193 port->port_type = CTL_PORT_IOCTL; 1194 port->num_requested_ctl_io = 100; 1195 port->port_name = softc->ioctl_info.port_name; 1196 port->port_online = ctl_ioctl_online; 1197 port->port_offline = ctl_ioctl_offline; 1198 port->onoff_arg = &softc->ioctl_info; 1199 port->lun_enable = ctl_ioctl_lun_enable; 1200 port->lun_disable = ctl_ioctl_lun_disable; 1201 port->targ_lun_arg = &softc->ioctl_info; 1202 port->fe_datamove = ctl_ioctl_datamove; 1203 port->fe_done = ctl_ioctl_done; 1204 port->max_targets = 15; 1205 port->max_target_id = 15; 1206 1207 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1208 printf("ctl: ioctl front end registration failed, will " 1209 "continue anyway\n"); 1210 } 1211 1212 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1213 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1214 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1215 1216#ifdef CTL_IO_DELAY 1217 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1218 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1219 sizeof(struct callout), CTL_TIMER_BYTES); 1220 return (EINVAL); 1221 } 1222#endif /* CTL_IO_DELAY */ 1223 1224 return (0); 1225} 1226 1227void 1228ctl_shutdown(void) 1229{ 1230 struct ctl_softc *softc; 1231 struct ctl_lun *lun, *next_lun; 1232 1233 softc = (struct ctl_softc *)control_softc; 1234 1235 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1236 printf("ctl: ioctl front end deregistration failed\n"); 1237 1238 mtx_lock(&softc->ctl_lock); 1239 1240 /* 1241 * Free up each LUN. 1242 */ 1243 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1244 next_lun = STAILQ_NEXT(lun, links); 1245 ctl_free_lun(lun); 1246 } 1247 1248 mtx_unlock(&softc->ctl_lock); 1249 1250 ctl_frontend_deregister(&ioctl_frontend); 1251 1252#if 0 1253 ctl_shutdown_thread(softc->work_thread); 1254 mtx_destroy(&softc->queue_lock); 1255#endif 1256 1257 ctl_tpc_shutdown(softc); 1258 uma_zdestroy(softc->io_zone); 1259 mtx_destroy(&softc->ctl_lock); 1260 1261 destroy_dev(softc->dev); 1262 1263 sysctl_ctx_free(&softc->sysctl_ctx); 1264 1265 free(control_softc, M_DEVBUF); 1266 control_softc = NULL; 1267 1268 if (bootverbose) 1269 printf("ctl: CAM Target Layer unloaded\n"); 1270} 1271 1272static int 1273ctl_module_event_handler(module_t mod, int what, void *arg) 1274{ 1275 1276 switch (what) { 1277 case MOD_LOAD: 1278 return (ctl_init()); 1279 case MOD_UNLOAD: 1280 return (EBUSY); 1281 default: 1282 return (EOPNOTSUPP); 1283 } 1284} 1285 1286/* 1287 * XXX KDM should we do some access checks here? Bump a reference count to 1288 * prevent a CTL module from being unloaded while someone has it open? 1289 */ 1290static int 1291ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1292{ 1293 return (0); 1294} 1295 1296static int 1297ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1298{ 1299 return (0); 1300} 1301 1302int 1303ctl_port_enable(ctl_port_type port_type) 1304{ 1305 struct ctl_softc *softc = control_softc; 1306 struct ctl_port *port; 1307 1308 if (softc->is_single == 0) { 1309 union ctl_ha_msg msg_info; 1310 int isc_retval; 1311 1312#if 0 1313 printf("%s: HA mode, synchronizing frontend enable\n", 1314 __func__); 1315#endif 1316 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1317 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1318 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1319 printf("Sync msg send error retval %d\n", isc_retval); 1320 } 1321 if (!rcv_sync_msg) { 1322 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1323 sizeof(msg_info), 1); 1324 } 1325#if 0 1326 printf("CTL:Frontend Enable\n"); 1327 } else { 1328 printf("%s: single mode, skipping frontend synchronization\n", 1329 __func__); 1330#endif 1331 } 1332 1333 STAILQ_FOREACH(port, &softc->port_list, links) { 1334 if (port_type & port->port_type) 1335 { 1336#if 0 1337 printf("port %d\n", port->targ_port); 1338#endif 1339 ctl_port_online(port); 1340 } 1341 } 1342 1343 return (0); 1344} 1345 1346int 1347ctl_port_disable(ctl_port_type port_type) 1348{ 1349 struct ctl_softc *softc; 1350 struct ctl_port *port; 1351 1352 softc = control_softc; 1353 1354 STAILQ_FOREACH(port, &softc->port_list, links) { 1355 if (port_type & port->port_type) 1356 ctl_port_offline(port); 1357 } 1358 1359 return (0); 1360} 1361 1362/* 1363 * Returns 0 for success, 1 for failure. 1364 * Currently the only failure mode is if there aren't enough entries 1365 * allocated. So, in case of a failure, look at num_entries_dropped, 1366 * reallocate and try again. 1367 */ 1368int 1369ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1370 int *num_entries_filled, int *num_entries_dropped, 1371 ctl_port_type port_type, int no_virtual) 1372{ 1373 struct ctl_softc *softc; 1374 struct ctl_port *port; 1375 int entries_dropped, entries_filled; 1376 int retval; 1377 int i; 1378 1379 softc = control_softc; 1380 1381 retval = 0; 1382 entries_filled = 0; 1383 entries_dropped = 0; 1384 1385 i = 0; 1386 mtx_lock(&softc->ctl_lock); 1387 STAILQ_FOREACH(port, &softc->port_list, links) { 1388 struct ctl_port_entry *entry; 1389 1390 if ((port->port_type & port_type) == 0) 1391 continue; 1392 1393 if ((no_virtual != 0) 1394 && (port->virtual_port != 0)) 1395 continue; 1396 1397 if (entries_filled >= num_entries_alloced) { 1398 entries_dropped++; 1399 continue; 1400 } 1401 entry = &entries[i]; 1402 1403 entry->port_type = port->port_type; 1404 strlcpy(entry->port_name, port->port_name, 1405 sizeof(entry->port_name)); 1406 entry->physical_port = port->physical_port; 1407 entry->virtual_port = port->virtual_port; 1408 entry->wwnn = port->wwnn; 1409 entry->wwpn = port->wwpn; 1410 1411 i++; 1412 entries_filled++; 1413 } 1414 1415 mtx_unlock(&softc->ctl_lock); 1416 1417 if (entries_dropped > 0) 1418 retval = 1; 1419 1420 *num_entries_dropped = entries_dropped; 1421 *num_entries_filled = entries_filled; 1422 1423 return (retval); 1424} 1425 1426static void 1427ctl_ioctl_online(void *arg) 1428{ 1429 struct ctl_ioctl_info *ioctl_info; 1430 1431 ioctl_info = (struct ctl_ioctl_info *)arg; 1432 1433 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1434} 1435 1436static void 1437ctl_ioctl_offline(void *arg) 1438{ 1439 struct ctl_ioctl_info *ioctl_info; 1440 1441 ioctl_info = (struct ctl_ioctl_info *)arg; 1442 1443 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1444} 1445 1446/* 1447 * Remove an initiator by port number and initiator ID. 1448 * Returns 0 for success, -1 for failure. 1449 */ 1450int 1451ctl_remove_initiator(struct ctl_port *port, int iid) 1452{ 1453 struct ctl_softc *softc = control_softc; 1454 1455 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1456 1457 if (iid > CTL_MAX_INIT_PER_PORT) { 1458 printf("%s: initiator ID %u > maximun %u!\n", 1459 __func__, iid, CTL_MAX_INIT_PER_PORT); 1460 return (-1); 1461 } 1462 1463 mtx_lock(&softc->ctl_lock); 1464 port->wwpn_iid[iid].in_use--; 1465 port->wwpn_iid[iid].last_use = time_uptime; 1466 mtx_unlock(&softc->ctl_lock); 1467 1468 return (0); 1469} 1470 1471/* 1472 * Add an initiator to the initiator map. 1473 * Returns iid for success, < 0 for failure. 1474 */ 1475int 1476ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1477{ 1478 struct ctl_softc *softc = control_softc; 1479 time_t best_time; 1480 int i, best; 1481 1482 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1483 1484 if (iid >= CTL_MAX_INIT_PER_PORT) { 1485 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1486 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1487 free(name, M_CTL); 1488 return (-1); 1489 } 1490 1491 mtx_lock(&softc->ctl_lock); 1492 1493 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1494 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1495 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1496 iid = i; 1497 break; 1498 } 1499 if (name != NULL && port->wwpn_iid[i].name != NULL && 1500 strcmp(name, port->wwpn_iid[i].name) == 0) { 1501 iid = i; 1502 break; 1503 } 1504 } 1505 } 1506 1507 if (iid < 0) { 1508 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1509 if (port->wwpn_iid[i].in_use == 0 && 1510 port->wwpn_iid[i].wwpn == 0 && 1511 port->wwpn_iid[i].name == NULL) { 1512 iid = i; 1513 break; 1514 } 1515 } 1516 } 1517 1518 if (iid < 0) { 1519 best = -1; 1520 best_time = INT32_MAX; 1521 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1522 if (port->wwpn_iid[i].in_use == 0) { 1523 if (port->wwpn_iid[i].last_use < best_time) { 1524 best = i; 1525 best_time = port->wwpn_iid[i].last_use; 1526 } 1527 } 1528 } 1529 iid = best; 1530 } 1531 1532 if (iid < 0) { 1533 mtx_unlock(&softc->ctl_lock); 1534 free(name, M_CTL); 1535 return (-2); 1536 } 1537 1538 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1539 /* 1540 * This is not an error yet. 1541 */ 1542 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1543#if 0 1544 printf("%s: port %d iid %u WWPN %#jx arrived" 1545 " again\n", __func__, port->targ_port, 1546 iid, (uintmax_t)wwpn); 1547#endif 1548 goto take; 1549 } 1550 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1551 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1552#if 0 1553 printf("%s: port %d iid %u name '%s' arrived" 1554 " again\n", __func__, port->targ_port, 1555 iid, name); 1556#endif 1557 goto take; 1558 } 1559 1560 /* 1561 * This is an error, but what do we do about it? The 1562 * driver is telling us we have a new WWPN for this 1563 * initiator ID, so we pretty much need to use it. 1564 */ 1565 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1566 " but WWPN %#jx '%s' is still at that address\n", 1567 __func__, port->targ_port, iid, wwpn, name, 1568 (uintmax_t)port->wwpn_iid[iid].wwpn, 1569 port->wwpn_iid[iid].name); 1570 1571 /* 1572 * XXX KDM clear have_ca and ua_pending on each LUN for 1573 * this initiator. 1574 */ 1575 } 1576take: 1577 free(port->wwpn_iid[iid].name, M_CTL); 1578 port->wwpn_iid[iid].name = name; 1579 port->wwpn_iid[iid].wwpn = wwpn; 1580 port->wwpn_iid[iid].in_use++; 1581 mtx_unlock(&softc->ctl_lock); 1582 1583 return (iid); 1584} 1585 1586static int 1587ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1588{ 1589 int len; 1590 1591 switch (port->port_type) { 1592 case CTL_PORT_FC: 1593 { 1594 struct scsi_transportid_fcp *id = 1595 (struct scsi_transportid_fcp *)buf; 1596 if (port->wwpn_iid[iid].wwpn == 0) 1597 return (0); 1598 memset(id, 0, sizeof(*id)); 1599 id->format_protocol = SCSI_PROTO_FC; 1600 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1601 return (sizeof(*id)); 1602 } 1603 case CTL_PORT_ISCSI: 1604 { 1605 struct scsi_transportid_iscsi_port *id = 1606 (struct scsi_transportid_iscsi_port *)buf; 1607 if (port->wwpn_iid[iid].name == NULL) 1608 return (0); 1609 memset(id, 0, 256); 1610 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1611 SCSI_PROTO_ISCSI; 1612 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1613 len = roundup2(min(len, 252), 4); 1614 scsi_ulto2b(len, id->additional_length); 1615 return (sizeof(*id) + len); 1616 } 1617 case CTL_PORT_SAS: 1618 { 1619 struct scsi_transportid_sas *id = 1620 (struct scsi_transportid_sas *)buf; 1621 if (port->wwpn_iid[iid].wwpn == 0) 1622 return (0); 1623 memset(id, 0, sizeof(*id)); 1624 id->format_protocol = SCSI_PROTO_SAS; 1625 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1626 return (sizeof(*id)); 1627 } 1628 default: 1629 { 1630 struct scsi_transportid_spi *id = 1631 (struct scsi_transportid_spi *)buf; 1632 memset(id, 0, sizeof(*id)); 1633 id->format_protocol = SCSI_PROTO_SPI; 1634 scsi_ulto2b(iid, id->scsi_addr); 1635 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1636 return (sizeof(*id)); 1637 } 1638 } 1639} 1640 1641static int 1642ctl_ioctl_lun_enable(void *arg, int lun_id) 1643{ 1644 return (0); 1645} 1646 1647static int 1648ctl_ioctl_lun_disable(void *arg, int lun_id) 1649{ 1650 return (0); 1651} 1652 1653/* 1654 * Data movement routine for the CTL ioctl frontend port. 1655 */ 1656static int 1657ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1658{ 1659 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1660 struct ctl_sg_entry ext_entry, kern_entry; 1661 int ext_sglen, ext_sg_entries, kern_sg_entries; 1662 int ext_sg_start, ext_offset; 1663 int len_to_copy, len_copied; 1664 int kern_watermark, ext_watermark; 1665 int ext_sglist_malloced; 1666 int i, j; 1667 1668 ext_sglist_malloced = 0; 1669 ext_sg_start = 0; 1670 ext_offset = 0; 1671 1672 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1673 1674 /* 1675 * If this flag is set, fake the data transfer. 1676 */ 1677 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1678 ctsio->ext_data_filled = ctsio->ext_data_len; 1679 goto bailout; 1680 } 1681 1682 /* 1683 * To simplify things here, if we have a single buffer, stick it in 1684 * a S/G entry and just make it a single entry S/G list. 1685 */ 1686 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1687 int len_seen; 1688 1689 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1690 1691 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1692 M_WAITOK); 1693 ext_sglist_malloced = 1; 1694 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1695 ext_sglen) != 0) { 1696 ctl_set_internal_failure(ctsio, 1697 /*sks_valid*/ 0, 1698 /*retry_count*/ 0); 1699 goto bailout; 1700 } 1701 ext_sg_entries = ctsio->ext_sg_entries; 1702 len_seen = 0; 1703 for (i = 0; i < ext_sg_entries; i++) { 1704 if ((len_seen + ext_sglist[i].len) >= 1705 ctsio->ext_data_filled) { 1706 ext_sg_start = i; 1707 ext_offset = ctsio->ext_data_filled - len_seen; 1708 break; 1709 } 1710 len_seen += ext_sglist[i].len; 1711 } 1712 } else { 1713 ext_sglist = &ext_entry; 1714 ext_sglist->addr = ctsio->ext_data_ptr; 1715 ext_sglist->len = ctsio->ext_data_len; 1716 ext_sg_entries = 1; 1717 ext_sg_start = 0; 1718 ext_offset = ctsio->ext_data_filled; 1719 } 1720 1721 if (ctsio->kern_sg_entries > 0) { 1722 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1723 kern_sg_entries = ctsio->kern_sg_entries; 1724 } else { 1725 kern_sglist = &kern_entry; 1726 kern_sglist->addr = ctsio->kern_data_ptr; 1727 kern_sglist->len = ctsio->kern_data_len; 1728 kern_sg_entries = 1; 1729 } 1730 1731 1732 kern_watermark = 0; 1733 ext_watermark = ext_offset; 1734 len_copied = 0; 1735 for (i = ext_sg_start, j = 0; 1736 i < ext_sg_entries && j < kern_sg_entries;) { 1737 uint8_t *ext_ptr, *kern_ptr; 1738 1739 len_to_copy = MIN(ext_sglist[i].len - ext_watermark, 1740 kern_sglist[j].len - kern_watermark); 1741 1742 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1743 ext_ptr = ext_ptr + ext_watermark; 1744 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1745 /* 1746 * XXX KDM fix this! 1747 */ 1748 panic("need to implement bus address support"); 1749#if 0 1750 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1751#endif 1752 } else 1753 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1754 kern_ptr = kern_ptr + kern_watermark; 1755 1756 kern_watermark += len_to_copy; 1757 ext_watermark += len_to_copy; 1758 1759 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1760 CTL_FLAG_DATA_IN) { 1761 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1762 "bytes to user\n", len_to_copy)); 1763 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1764 "to %p\n", kern_ptr, ext_ptr)); 1765 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1766 ctl_set_internal_failure(ctsio, 1767 /*sks_valid*/ 0, 1768 /*retry_count*/ 0); 1769 goto bailout; 1770 } 1771 } else { 1772 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1773 "bytes from user\n", len_to_copy)); 1774 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1775 "to %p\n", ext_ptr, kern_ptr)); 1776 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1777 ctl_set_internal_failure(ctsio, 1778 /*sks_valid*/ 0, 1779 /*retry_count*/0); 1780 goto bailout; 1781 } 1782 } 1783 1784 len_copied += len_to_copy; 1785 1786 if (ext_sglist[i].len == ext_watermark) { 1787 i++; 1788 ext_watermark = 0; 1789 } 1790 1791 if (kern_sglist[j].len == kern_watermark) { 1792 j++; 1793 kern_watermark = 0; 1794 } 1795 } 1796 1797 ctsio->ext_data_filled += len_copied; 1798 1799 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1800 "kern_sg_entries: %d\n", ext_sg_entries, 1801 kern_sg_entries)); 1802 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1803 "kern_data_len = %d\n", ctsio->ext_data_len, 1804 ctsio->kern_data_len)); 1805 1806 1807 /* XXX KDM set residual?? */ 1808bailout: 1809 1810 if (ext_sglist_malloced != 0) 1811 free(ext_sglist, M_CTL); 1812 1813 return (CTL_RETVAL_COMPLETE); 1814} 1815 1816/* 1817 * Serialize a command that went down the "wrong" side, and so was sent to 1818 * this controller for execution. The logic is a little different than the 1819 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1820 * sent back to the other side, but in the success case, we execute the 1821 * command on this side (XFER mode) or tell the other side to execute it 1822 * (SER_ONLY mode). 1823 */ 1824static int 1825ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1826{ 1827 struct ctl_softc *softc; 1828 union ctl_ha_msg msg_info; 1829 struct ctl_lun *lun; 1830 int retval = 0; 1831 uint32_t targ_lun; 1832 1833 softc = control_softc; 1834 1835 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1836 lun = softc->ctl_luns[targ_lun]; 1837 if (lun==NULL) 1838 { 1839 /* 1840 * Why isn't LUN defined? The other side wouldn't 1841 * send a cmd if the LUN is undefined. 1842 */ 1843 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1844 1845 /* "Logical unit not supported" */ 1846 ctl_set_sense_data(&msg_info.scsi.sense_data, 1847 lun, 1848 /*sense_format*/SSD_TYPE_NONE, 1849 /*current_error*/ 1, 1850 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1851 /*asc*/ 0x25, 1852 /*ascq*/ 0x00, 1853 SSD_ELEM_NONE); 1854 1855 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1856 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1857 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1858 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1859 msg_info.hdr.serializing_sc = NULL; 1860 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1861 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1862 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1863 } 1864 return(1); 1865 1866 } 1867 1868 mtx_lock(&lun->lun_lock); 1869 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1870 1871 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1872 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1873 ooa_links))) { 1874 case CTL_ACTION_BLOCK: 1875 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1876 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1877 blocked_links); 1878 break; 1879 case CTL_ACTION_PASS: 1880 case CTL_ACTION_SKIP: 1881 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1882 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1883 ctl_enqueue_rtr((union ctl_io *)ctsio); 1884 } else { 1885 1886 /* send msg back to other side */ 1887 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1888 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1889 msg_info.hdr.msg_type = CTL_MSG_R2R; 1890#if 0 1891 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1892#endif 1893 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1894 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1895 } 1896 } 1897 break; 1898 case CTL_ACTION_OVERLAP: 1899 /* OVERLAPPED COMMANDS ATTEMPTED */ 1900 ctl_set_sense_data(&msg_info.scsi.sense_data, 1901 lun, 1902 /*sense_format*/SSD_TYPE_NONE, 1903 /*current_error*/ 1, 1904 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1905 /*asc*/ 0x4E, 1906 /*ascq*/ 0x00, 1907 SSD_ELEM_NONE); 1908 1909 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1910 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1911 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1912 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1913 msg_info.hdr.serializing_sc = NULL; 1914 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1915#if 0 1916 printf("BAD JUJU:Major Bummer Overlap\n"); 1917#endif 1918 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1919 retval = 1; 1920 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1921 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1922 } 1923 break; 1924 case CTL_ACTION_OVERLAP_TAG: 1925 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1926 ctl_set_sense_data(&msg_info.scsi.sense_data, 1927 lun, 1928 /*sense_format*/SSD_TYPE_NONE, 1929 /*current_error*/ 1, 1930 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1931 /*asc*/ 0x4D, 1932 /*ascq*/ ctsio->tag_num & 0xff, 1933 SSD_ELEM_NONE); 1934 1935 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1936 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1937 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1938 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1939 msg_info.hdr.serializing_sc = NULL; 1940 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1941#if 0 1942 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1943#endif 1944 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1945 retval = 1; 1946 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1947 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1948 } 1949 break; 1950 case CTL_ACTION_ERROR: 1951 default: 1952 /* "Internal target failure" */ 1953 ctl_set_sense_data(&msg_info.scsi.sense_data, 1954 lun, 1955 /*sense_format*/SSD_TYPE_NONE, 1956 /*current_error*/ 1, 1957 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1958 /*asc*/ 0x44, 1959 /*ascq*/ 0x00, 1960 SSD_ELEM_NONE); 1961 1962 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1963 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1964 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1965 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1966 msg_info.hdr.serializing_sc = NULL; 1967 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1968#if 0 1969 printf("BAD JUJU:Major Bummer HW Error\n"); 1970#endif 1971 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1972 retval = 1; 1973 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1974 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1975 } 1976 break; 1977 } 1978 mtx_unlock(&lun->lun_lock); 1979 return (retval); 1980} 1981 1982static int 1983ctl_ioctl_submit_wait(union ctl_io *io) 1984{ 1985 struct ctl_fe_ioctl_params params; 1986 ctl_fe_ioctl_state last_state; 1987 int done, retval; 1988 1989 retval = 0; 1990 1991 bzero(¶ms, sizeof(params)); 1992 1993 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1994 cv_init(¶ms.sem, "ctlioccv"); 1995 params.state = CTL_IOCTL_INPROG; 1996 last_state = params.state; 1997 1998 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1999 2000 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 2001 2002 /* This shouldn't happen */ 2003 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 2004 return (retval); 2005 2006 done = 0; 2007 2008 do { 2009 mtx_lock(¶ms.ioctl_mtx); 2010 /* 2011 * Check the state here, and don't sleep if the state has 2012 * already changed (i.e. wakeup has already occured, but we 2013 * weren't waiting yet). 2014 */ 2015 if (params.state == last_state) { 2016 /* XXX KDM cv_wait_sig instead? */ 2017 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 2018 } 2019 last_state = params.state; 2020 2021 switch (params.state) { 2022 case CTL_IOCTL_INPROG: 2023 /* Why did we wake up? */ 2024 /* XXX KDM error here? */ 2025 mtx_unlock(¶ms.ioctl_mtx); 2026 break; 2027 case CTL_IOCTL_DATAMOVE: 2028 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 2029 2030 /* 2031 * change last_state back to INPROG to avoid 2032 * deadlock on subsequent data moves. 2033 */ 2034 params.state = last_state = CTL_IOCTL_INPROG; 2035 2036 mtx_unlock(¶ms.ioctl_mtx); 2037 ctl_ioctl_do_datamove(&io->scsiio); 2038 /* 2039 * Note that in some cases, most notably writes, 2040 * this will queue the I/O and call us back later. 2041 * In other cases, generally reads, this routine 2042 * will immediately call back and wake us up, 2043 * probably using our own context. 2044 */ 2045 io->scsiio.be_move_done(io); 2046 break; 2047 case CTL_IOCTL_DONE: 2048 mtx_unlock(¶ms.ioctl_mtx); 2049 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2050 done = 1; 2051 break; 2052 default: 2053 mtx_unlock(¶ms.ioctl_mtx); 2054 /* XXX KDM error here? */ 2055 break; 2056 } 2057 } while (done == 0); 2058 2059 mtx_destroy(¶ms.ioctl_mtx); 2060 cv_destroy(¶ms.sem); 2061 2062 return (CTL_RETVAL_COMPLETE); 2063} 2064 2065static void 2066ctl_ioctl_datamove(union ctl_io *io) 2067{ 2068 struct ctl_fe_ioctl_params *params; 2069 2070 params = (struct ctl_fe_ioctl_params *) 2071 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2072 2073 mtx_lock(¶ms->ioctl_mtx); 2074 params->state = CTL_IOCTL_DATAMOVE; 2075 cv_broadcast(¶ms->sem); 2076 mtx_unlock(¶ms->ioctl_mtx); 2077} 2078 2079static void 2080ctl_ioctl_done(union ctl_io *io) 2081{ 2082 struct ctl_fe_ioctl_params *params; 2083 2084 params = (struct ctl_fe_ioctl_params *) 2085 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2086 2087 mtx_lock(¶ms->ioctl_mtx); 2088 params->state = CTL_IOCTL_DONE; 2089 cv_broadcast(¶ms->sem); 2090 mtx_unlock(¶ms->ioctl_mtx); 2091} 2092 2093static void 2094ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2095{ 2096 struct ctl_fe_ioctl_startstop_info *sd_info; 2097 2098 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2099 2100 sd_info->hs_info.status = metatask->status; 2101 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2102 sd_info->hs_info.luns_complete = 2103 metatask->taskinfo.startstop.luns_complete; 2104 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2105 2106 cv_broadcast(&sd_info->sem); 2107} 2108 2109static void 2110ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2111{ 2112 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2113 2114 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2115 2116 mtx_lock(fe_bbr_info->lock); 2117 fe_bbr_info->bbr_info->status = metatask->status; 2118 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2119 fe_bbr_info->wakeup_done = 1; 2120 mtx_unlock(fe_bbr_info->lock); 2121 2122 cv_broadcast(&fe_bbr_info->sem); 2123} 2124 2125/* 2126 * Returns 0 for success, errno for failure. 2127 */ 2128static int 2129ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2130 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2131{ 2132 union ctl_io *io; 2133 int retval; 2134 2135 retval = 0; 2136 2137 mtx_lock(&lun->lun_lock); 2138 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2139 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2140 ooa_links)) { 2141 struct ctl_ooa_entry *entry; 2142 2143 /* 2144 * If we've got more than we can fit, just count the 2145 * remaining entries. 2146 */ 2147 if (*cur_fill_num >= ooa_hdr->alloc_num) 2148 continue; 2149 2150 entry = &kern_entries[*cur_fill_num]; 2151 2152 entry->tag_num = io->scsiio.tag_num; 2153 entry->lun_num = lun->lun; 2154#ifdef CTL_TIME_IO 2155 entry->start_bt = io->io_hdr.start_bt; 2156#endif 2157 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2158 entry->cdb_len = io->scsiio.cdb_len; 2159 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2160 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2161 2162 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2163 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2164 2165 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2166 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2167 2168 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2169 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2170 2171 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2172 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2173 } 2174 mtx_unlock(&lun->lun_lock); 2175 2176 return (retval); 2177} 2178 2179static void * 2180ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2181 size_t error_str_len) 2182{ 2183 void *kptr; 2184 2185 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2186 2187 if (copyin(user_addr, kptr, len) != 0) { 2188 snprintf(error_str, error_str_len, "Error copying %d bytes " 2189 "from user address %p to kernel address %p", len, 2190 user_addr, kptr); 2191 free(kptr, M_CTL); 2192 return (NULL); 2193 } 2194 2195 return (kptr); 2196} 2197 2198static void 2199ctl_free_args(int num_args, struct ctl_be_arg *args) 2200{ 2201 int i; 2202 2203 if (args == NULL) 2204 return; 2205 2206 for (i = 0; i < num_args; i++) { 2207 free(args[i].kname, M_CTL); 2208 free(args[i].kvalue, M_CTL); 2209 } 2210 2211 free(args, M_CTL); 2212} 2213 2214static struct ctl_be_arg * 2215ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2216 char *error_str, size_t error_str_len) 2217{ 2218 struct ctl_be_arg *args; 2219 int i; 2220 2221 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2222 error_str, error_str_len); 2223 2224 if (args == NULL) 2225 goto bailout; 2226 2227 for (i = 0; i < num_args; i++) { 2228 args[i].kname = NULL; 2229 args[i].kvalue = NULL; 2230 } 2231 2232 for (i = 0; i < num_args; i++) { 2233 uint8_t *tmpptr; 2234 2235 args[i].kname = ctl_copyin_alloc(args[i].name, 2236 args[i].namelen, error_str, error_str_len); 2237 if (args[i].kname == NULL) 2238 goto bailout; 2239 2240 if (args[i].kname[args[i].namelen - 1] != '\0') { 2241 snprintf(error_str, error_str_len, "Argument %d " 2242 "name is not NUL-terminated", i); 2243 goto bailout; 2244 } 2245 2246 if (args[i].flags & CTL_BEARG_RD) { 2247 tmpptr = ctl_copyin_alloc(args[i].value, 2248 args[i].vallen, error_str, error_str_len); 2249 if (tmpptr == NULL) 2250 goto bailout; 2251 if ((args[i].flags & CTL_BEARG_ASCII) 2252 && (tmpptr[args[i].vallen - 1] != '\0')) { 2253 snprintf(error_str, error_str_len, "Argument " 2254 "%d value is not NUL-terminated", i); 2255 goto bailout; 2256 } 2257 args[i].kvalue = tmpptr; 2258 } else { 2259 args[i].kvalue = malloc(args[i].vallen, 2260 M_CTL, M_WAITOK | M_ZERO); 2261 } 2262 } 2263 2264 return (args); 2265bailout: 2266 2267 ctl_free_args(num_args, args); 2268 2269 return (NULL); 2270} 2271 2272static void 2273ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2274{ 2275 int i; 2276 2277 for (i = 0; i < num_args; i++) { 2278 if (args[i].flags & CTL_BEARG_WR) 2279 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2280 } 2281} 2282 2283/* 2284 * Escape characters that are illegal or not recommended in XML. 2285 */ 2286int 2287ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2288{ 2289 char *end = str + size; 2290 int retval; 2291 2292 retval = 0; 2293 2294 for (; *str && str < end; str++) { 2295 switch (*str) { 2296 case '&': 2297 retval = sbuf_printf(sb, "&"); 2298 break; 2299 case '>': 2300 retval = sbuf_printf(sb, ">"); 2301 break; 2302 case '<': 2303 retval = sbuf_printf(sb, "<"); 2304 break; 2305 default: 2306 retval = sbuf_putc(sb, *str); 2307 break; 2308 } 2309 2310 if (retval != 0) 2311 break; 2312 2313 } 2314 2315 return (retval); 2316} 2317 2318static void 2319ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2320{ 2321 struct scsi_vpd_id_descriptor *desc; 2322 int i; 2323 2324 if (id == NULL || id->len < 4) 2325 return; 2326 desc = (struct scsi_vpd_id_descriptor *)id->data; 2327 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2328 case SVPD_ID_TYPE_T10: 2329 sbuf_printf(sb, "t10."); 2330 break; 2331 case SVPD_ID_TYPE_EUI64: 2332 sbuf_printf(sb, "eui."); 2333 break; 2334 case SVPD_ID_TYPE_NAA: 2335 sbuf_printf(sb, "naa."); 2336 break; 2337 case SVPD_ID_TYPE_SCSI_NAME: 2338 break; 2339 } 2340 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2341 case SVPD_ID_CODESET_BINARY: 2342 for (i = 0; i < desc->length; i++) 2343 sbuf_printf(sb, "%02x", desc->identifier[i]); 2344 break; 2345 case SVPD_ID_CODESET_ASCII: 2346 sbuf_printf(sb, "%.*s", (int)desc->length, 2347 (char *)desc->identifier); 2348 break; 2349 case SVPD_ID_CODESET_UTF8: 2350 sbuf_printf(sb, "%s", (char *)desc->identifier); 2351 break; 2352 } 2353} 2354 2355static int 2356ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2357 struct thread *td) 2358{ 2359 struct ctl_softc *softc; 2360 int retval; 2361 2362 softc = control_softc; 2363 2364 retval = 0; 2365 2366 switch (cmd) { 2367 case CTL_IO: { 2368 union ctl_io *io; 2369 void *pool_tmp; 2370 2371 /* 2372 * If we haven't been "enabled", don't allow any SCSI I/O 2373 * to this FETD. 2374 */ 2375 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2376 retval = EPERM; 2377 break; 2378 } 2379 2380 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2381 2382 /* 2383 * Need to save the pool reference so it doesn't get 2384 * spammed by the user's ctl_io. 2385 */ 2386 pool_tmp = io->io_hdr.pool; 2387 memcpy(io, (void *)addr, sizeof(*io)); 2388 io->io_hdr.pool = pool_tmp; 2389 2390 /* 2391 * No status yet, so make sure the status is set properly. 2392 */ 2393 io->io_hdr.status = CTL_STATUS_NONE; 2394 2395 /* 2396 * The user sets the initiator ID, target and LUN IDs. 2397 */ 2398 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2399 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2400 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2401 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2402 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2403 2404 retval = ctl_ioctl_submit_wait(io); 2405 2406 if (retval != 0) { 2407 ctl_free_io(io); 2408 break; 2409 } 2410 2411 memcpy((void *)addr, io, sizeof(*io)); 2412 2413 /* return this to our pool */ 2414 ctl_free_io(io); 2415 2416 break; 2417 } 2418 case CTL_ENABLE_PORT: 2419 case CTL_DISABLE_PORT: 2420 case CTL_SET_PORT_WWNS: { 2421 struct ctl_port *port; 2422 struct ctl_port_entry *entry; 2423 2424 entry = (struct ctl_port_entry *)addr; 2425 2426 mtx_lock(&softc->ctl_lock); 2427 STAILQ_FOREACH(port, &softc->port_list, links) { 2428 int action, done; 2429 2430 action = 0; 2431 done = 0; 2432 2433 if ((entry->port_type == CTL_PORT_NONE) 2434 && (entry->targ_port == port->targ_port)) { 2435 /* 2436 * If the user only wants to enable or 2437 * disable or set WWNs on a specific port, 2438 * do the operation and we're done. 2439 */ 2440 action = 1; 2441 done = 1; 2442 } else if (entry->port_type & port->port_type) { 2443 /* 2444 * Compare the user's type mask with the 2445 * particular frontend type to see if we 2446 * have a match. 2447 */ 2448 action = 1; 2449 done = 0; 2450 2451 /* 2452 * Make sure the user isn't trying to set 2453 * WWNs on multiple ports at the same time. 2454 */ 2455 if (cmd == CTL_SET_PORT_WWNS) { 2456 printf("%s: Can't set WWNs on " 2457 "multiple ports\n", __func__); 2458 retval = EINVAL; 2459 break; 2460 } 2461 } 2462 if (action != 0) { 2463 /* 2464 * XXX KDM we have to drop the lock here, 2465 * because the online/offline operations 2466 * can potentially block. We need to 2467 * reference count the frontends so they 2468 * can't go away, 2469 */ 2470 mtx_unlock(&softc->ctl_lock); 2471 2472 if (cmd == CTL_ENABLE_PORT) { 2473 ctl_port_online(port); 2474 } else if (cmd == CTL_DISABLE_PORT) { 2475 ctl_port_offline(port); 2476 } 2477 2478 mtx_lock(&softc->ctl_lock); 2479 2480 if (cmd == CTL_SET_PORT_WWNS) 2481 ctl_port_set_wwns(port, 2482 (entry->flags & CTL_PORT_WWNN_VALID) ? 2483 1 : 0, entry->wwnn, 2484 (entry->flags & CTL_PORT_WWPN_VALID) ? 2485 1 : 0, entry->wwpn); 2486 } 2487 if (done != 0) 2488 break; 2489 } 2490 mtx_unlock(&softc->ctl_lock); 2491 break; 2492 } 2493 case CTL_GET_PORT_LIST: { 2494 struct ctl_port *port; 2495 struct ctl_port_list *list; 2496 int i; 2497 2498 list = (struct ctl_port_list *)addr; 2499 2500 if (list->alloc_len != (list->alloc_num * 2501 sizeof(struct ctl_port_entry))) { 2502 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2503 "alloc_num %u * sizeof(struct ctl_port_entry) " 2504 "%zu\n", __func__, list->alloc_len, 2505 list->alloc_num, sizeof(struct ctl_port_entry)); 2506 retval = EINVAL; 2507 break; 2508 } 2509 list->fill_len = 0; 2510 list->fill_num = 0; 2511 list->dropped_num = 0; 2512 i = 0; 2513 mtx_lock(&softc->ctl_lock); 2514 STAILQ_FOREACH(port, &softc->port_list, links) { 2515 struct ctl_port_entry entry, *list_entry; 2516 2517 if (list->fill_num >= list->alloc_num) { 2518 list->dropped_num++; 2519 continue; 2520 } 2521 2522 entry.port_type = port->port_type; 2523 strlcpy(entry.port_name, port->port_name, 2524 sizeof(entry.port_name)); 2525 entry.targ_port = port->targ_port; 2526 entry.physical_port = port->physical_port; 2527 entry.virtual_port = port->virtual_port; 2528 entry.wwnn = port->wwnn; 2529 entry.wwpn = port->wwpn; 2530 if (port->status & CTL_PORT_STATUS_ONLINE) 2531 entry.online = 1; 2532 else 2533 entry.online = 0; 2534 2535 list_entry = &list->entries[i]; 2536 2537 retval = copyout(&entry, list_entry, sizeof(entry)); 2538 if (retval != 0) { 2539 printf("%s: CTL_GET_PORT_LIST: copyout " 2540 "returned %d\n", __func__, retval); 2541 break; 2542 } 2543 i++; 2544 list->fill_num++; 2545 list->fill_len += sizeof(entry); 2546 } 2547 mtx_unlock(&softc->ctl_lock); 2548 2549 /* 2550 * If this is non-zero, we had a copyout fault, so there's 2551 * probably no point in attempting to set the status inside 2552 * the structure. 2553 */ 2554 if (retval != 0) 2555 break; 2556 2557 if (list->dropped_num > 0) 2558 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2559 else 2560 list->status = CTL_PORT_LIST_OK; 2561 break; 2562 } 2563 case CTL_DUMP_OOA: { 2564 struct ctl_lun *lun; 2565 union ctl_io *io; 2566 char printbuf[128]; 2567 struct sbuf sb; 2568 2569 mtx_lock(&softc->ctl_lock); 2570 printf("Dumping OOA queues:\n"); 2571 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2572 mtx_lock(&lun->lun_lock); 2573 for (io = (union ctl_io *)TAILQ_FIRST( 2574 &lun->ooa_queue); io != NULL; 2575 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2576 ooa_links)) { 2577 sbuf_new(&sb, printbuf, sizeof(printbuf), 2578 SBUF_FIXEDLEN); 2579 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2580 (intmax_t)lun->lun, 2581 io->scsiio.tag_num, 2582 (io->io_hdr.flags & 2583 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2584 (io->io_hdr.flags & 2585 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2586 (io->io_hdr.flags & 2587 CTL_FLAG_ABORT) ? " ABORT" : "", 2588 (io->io_hdr.flags & 2589 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2590 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2591 sbuf_finish(&sb); 2592 printf("%s\n", sbuf_data(&sb)); 2593 } 2594 mtx_unlock(&lun->lun_lock); 2595 } 2596 printf("OOA queues dump done\n"); 2597 mtx_unlock(&softc->ctl_lock); 2598 break; 2599 } 2600 case CTL_GET_OOA: { 2601 struct ctl_lun *lun; 2602 struct ctl_ooa *ooa_hdr; 2603 struct ctl_ooa_entry *entries; 2604 uint32_t cur_fill_num; 2605 2606 ooa_hdr = (struct ctl_ooa *)addr; 2607 2608 if ((ooa_hdr->alloc_len == 0) 2609 || (ooa_hdr->alloc_num == 0)) { 2610 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2611 "must be non-zero\n", __func__, 2612 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2613 retval = EINVAL; 2614 break; 2615 } 2616 2617 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2618 sizeof(struct ctl_ooa_entry))) { 2619 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2620 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2621 __func__, ooa_hdr->alloc_len, 2622 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2623 retval = EINVAL; 2624 break; 2625 } 2626 2627 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2628 if (entries == NULL) { 2629 printf("%s: could not allocate %d bytes for OOA " 2630 "dump\n", __func__, ooa_hdr->alloc_len); 2631 retval = ENOMEM; 2632 break; 2633 } 2634 2635 mtx_lock(&softc->ctl_lock); 2636 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2637 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2638 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2639 mtx_unlock(&softc->ctl_lock); 2640 free(entries, M_CTL); 2641 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2642 __func__, (uintmax_t)ooa_hdr->lun_num); 2643 retval = EINVAL; 2644 break; 2645 } 2646 2647 cur_fill_num = 0; 2648 2649 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2650 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2651 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2652 ooa_hdr, entries); 2653 if (retval != 0) 2654 break; 2655 } 2656 if (retval != 0) { 2657 mtx_unlock(&softc->ctl_lock); 2658 free(entries, M_CTL); 2659 break; 2660 } 2661 } else { 2662 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2663 2664 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2665 entries); 2666 } 2667 mtx_unlock(&softc->ctl_lock); 2668 2669 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2670 ooa_hdr->fill_len = ooa_hdr->fill_num * 2671 sizeof(struct ctl_ooa_entry); 2672 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2673 if (retval != 0) { 2674 printf("%s: error copying out %d bytes for OOA dump\n", 2675 __func__, ooa_hdr->fill_len); 2676 } 2677 2678 getbintime(&ooa_hdr->cur_bt); 2679 2680 if (cur_fill_num > ooa_hdr->alloc_num) { 2681 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2682 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2683 } else { 2684 ooa_hdr->dropped_num = 0; 2685 ooa_hdr->status = CTL_OOA_OK; 2686 } 2687 2688 free(entries, M_CTL); 2689 break; 2690 } 2691 case CTL_CHECK_OOA: { 2692 union ctl_io *io; 2693 struct ctl_lun *lun; 2694 struct ctl_ooa_info *ooa_info; 2695 2696 2697 ooa_info = (struct ctl_ooa_info *)addr; 2698 2699 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2700 ooa_info->status = CTL_OOA_INVALID_LUN; 2701 break; 2702 } 2703 mtx_lock(&softc->ctl_lock); 2704 lun = softc->ctl_luns[ooa_info->lun_id]; 2705 if (lun == NULL) { 2706 mtx_unlock(&softc->ctl_lock); 2707 ooa_info->status = CTL_OOA_INVALID_LUN; 2708 break; 2709 } 2710 mtx_lock(&lun->lun_lock); 2711 mtx_unlock(&softc->ctl_lock); 2712 ooa_info->num_entries = 0; 2713 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2714 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2715 &io->io_hdr, ooa_links)) { 2716 ooa_info->num_entries++; 2717 } 2718 mtx_unlock(&lun->lun_lock); 2719 2720 ooa_info->status = CTL_OOA_SUCCESS; 2721 2722 break; 2723 } 2724 case CTL_HARD_START: 2725 case CTL_HARD_STOP: { 2726 struct ctl_fe_ioctl_startstop_info ss_info; 2727 struct cfi_metatask *metatask; 2728 struct mtx hs_mtx; 2729 2730 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2731 2732 cv_init(&ss_info.sem, "hard start/stop cv" ); 2733 2734 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2735 if (metatask == NULL) { 2736 retval = ENOMEM; 2737 mtx_destroy(&hs_mtx); 2738 break; 2739 } 2740 2741 if (cmd == CTL_HARD_START) 2742 metatask->tasktype = CFI_TASK_STARTUP; 2743 else 2744 metatask->tasktype = CFI_TASK_SHUTDOWN; 2745 2746 metatask->callback = ctl_ioctl_hard_startstop_callback; 2747 metatask->callback_arg = &ss_info; 2748 2749 cfi_action(metatask); 2750 2751 /* Wait for the callback */ 2752 mtx_lock(&hs_mtx); 2753 cv_wait_sig(&ss_info.sem, &hs_mtx); 2754 mtx_unlock(&hs_mtx); 2755 2756 /* 2757 * All information has been copied from the metatask by the 2758 * time cv_broadcast() is called, so we free the metatask here. 2759 */ 2760 cfi_free_metatask(metatask); 2761 2762 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2763 2764 mtx_destroy(&hs_mtx); 2765 break; 2766 } 2767 case CTL_BBRREAD: { 2768 struct ctl_bbrread_info *bbr_info; 2769 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2770 struct mtx bbr_mtx; 2771 struct cfi_metatask *metatask; 2772 2773 bbr_info = (struct ctl_bbrread_info *)addr; 2774 2775 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2776 2777 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2778 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2779 2780 fe_bbr_info.bbr_info = bbr_info; 2781 fe_bbr_info.lock = &bbr_mtx; 2782 2783 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2784 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2785 2786 if (metatask == NULL) { 2787 mtx_destroy(&bbr_mtx); 2788 cv_destroy(&fe_bbr_info.sem); 2789 retval = ENOMEM; 2790 break; 2791 } 2792 metatask->tasktype = CFI_TASK_BBRREAD; 2793 metatask->callback = ctl_ioctl_bbrread_callback; 2794 metatask->callback_arg = &fe_bbr_info; 2795 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2796 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2797 metatask->taskinfo.bbrread.len = bbr_info->len; 2798 2799 cfi_action(metatask); 2800 2801 mtx_lock(&bbr_mtx); 2802 while (fe_bbr_info.wakeup_done == 0) 2803 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2804 mtx_unlock(&bbr_mtx); 2805 2806 bbr_info->status = metatask->status; 2807 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2808 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2809 memcpy(&bbr_info->sense_data, 2810 &metatask->taskinfo.bbrread.sense_data, 2811 MIN(sizeof(bbr_info->sense_data), 2812 sizeof(metatask->taskinfo.bbrread.sense_data))); 2813 2814 cfi_free_metatask(metatask); 2815 2816 mtx_destroy(&bbr_mtx); 2817 cv_destroy(&fe_bbr_info.sem); 2818 2819 break; 2820 } 2821 case CTL_DELAY_IO: { 2822 struct ctl_io_delay_info *delay_info; 2823#ifdef CTL_IO_DELAY 2824 struct ctl_lun *lun; 2825#endif /* CTL_IO_DELAY */ 2826 2827 delay_info = (struct ctl_io_delay_info *)addr; 2828 2829#ifdef CTL_IO_DELAY 2830 mtx_lock(&softc->ctl_lock); 2831 2832 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2833 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2834 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2835 } else { 2836 lun = softc->ctl_luns[delay_info->lun_id]; 2837 mtx_lock(&lun->lun_lock); 2838 2839 delay_info->status = CTL_DELAY_STATUS_OK; 2840 2841 switch (delay_info->delay_type) { 2842 case CTL_DELAY_TYPE_CONT: 2843 break; 2844 case CTL_DELAY_TYPE_ONESHOT: 2845 break; 2846 default: 2847 delay_info->status = 2848 CTL_DELAY_STATUS_INVALID_TYPE; 2849 break; 2850 } 2851 2852 switch (delay_info->delay_loc) { 2853 case CTL_DELAY_LOC_DATAMOVE: 2854 lun->delay_info.datamove_type = 2855 delay_info->delay_type; 2856 lun->delay_info.datamove_delay = 2857 delay_info->delay_secs; 2858 break; 2859 case CTL_DELAY_LOC_DONE: 2860 lun->delay_info.done_type = 2861 delay_info->delay_type; 2862 lun->delay_info.done_delay = 2863 delay_info->delay_secs; 2864 break; 2865 default: 2866 delay_info->status = 2867 CTL_DELAY_STATUS_INVALID_LOC; 2868 break; 2869 } 2870 mtx_unlock(&lun->lun_lock); 2871 } 2872 2873 mtx_unlock(&softc->ctl_lock); 2874#else 2875 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2876#endif /* CTL_IO_DELAY */ 2877 break; 2878 } 2879 case CTL_REALSYNC_SET: { 2880 int *syncstate; 2881 2882 syncstate = (int *)addr; 2883 2884 mtx_lock(&softc->ctl_lock); 2885 switch (*syncstate) { 2886 case 0: 2887 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2888 break; 2889 case 1: 2890 softc->flags |= CTL_FLAG_REAL_SYNC; 2891 break; 2892 default: 2893 retval = EINVAL; 2894 break; 2895 } 2896 mtx_unlock(&softc->ctl_lock); 2897 break; 2898 } 2899 case CTL_REALSYNC_GET: { 2900 int *syncstate; 2901 2902 syncstate = (int*)addr; 2903 2904 mtx_lock(&softc->ctl_lock); 2905 if (softc->flags & CTL_FLAG_REAL_SYNC) 2906 *syncstate = 1; 2907 else 2908 *syncstate = 0; 2909 mtx_unlock(&softc->ctl_lock); 2910 2911 break; 2912 } 2913 case CTL_SETSYNC: 2914 case CTL_GETSYNC: { 2915 struct ctl_sync_info *sync_info; 2916 struct ctl_lun *lun; 2917 2918 sync_info = (struct ctl_sync_info *)addr; 2919 2920 mtx_lock(&softc->ctl_lock); 2921 lun = softc->ctl_luns[sync_info->lun_id]; 2922 if (lun == NULL) { 2923 mtx_unlock(&softc->ctl_lock); 2924 sync_info->status = CTL_GS_SYNC_NO_LUN; 2925 } 2926 /* 2927 * Get or set the sync interval. We're not bounds checking 2928 * in the set case, hopefully the user won't do something 2929 * silly. 2930 */ 2931 mtx_lock(&lun->lun_lock); 2932 mtx_unlock(&softc->ctl_lock); 2933 if (cmd == CTL_GETSYNC) 2934 sync_info->sync_interval = lun->sync_interval; 2935 else 2936 lun->sync_interval = sync_info->sync_interval; 2937 mtx_unlock(&lun->lun_lock); 2938 2939 sync_info->status = CTL_GS_SYNC_OK; 2940 2941 break; 2942 } 2943 case CTL_GETSTATS: { 2944 struct ctl_stats *stats; 2945 struct ctl_lun *lun; 2946 int i; 2947 2948 stats = (struct ctl_stats *)addr; 2949 2950 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2951 stats->alloc_len) { 2952 stats->status = CTL_SS_NEED_MORE_SPACE; 2953 stats->num_luns = softc->num_luns; 2954 break; 2955 } 2956 /* 2957 * XXX KDM no locking here. If the LUN list changes, 2958 * things can blow up. 2959 */ 2960 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2961 i++, lun = STAILQ_NEXT(lun, links)) { 2962 retval = copyout(&lun->stats, &stats->lun_stats[i], 2963 sizeof(lun->stats)); 2964 if (retval != 0) 2965 break; 2966 } 2967 stats->num_luns = softc->num_luns; 2968 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2969 softc->num_luns; 2970 stats->status = CTL_SS_OK; 2971#ifdef CTL_TIME_IO 2972 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2973#else 2974 stats->flags = CTL_STATS_FLAG_NONE; 2975#endif 2976 getnanouptime(&stats->timestamp); 2977 break; 2978 } 2979 case CTL_ERROR_INJECT: { 2980 struct ctl_error_desc *err_desc, *new_err_desc; 2981 struct ctl_lun *lun; 2982 2983 err_desc = (struct ctl_error_desc *)addr; 2984 2985 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2986 M_WAITOK | M_ZERO); 2987 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2988 2989 mtx_lock(&softc->ctl_lock); 2990 lun = softc->ctl_luns[err_desc->lun_id]; 2991 if (lun == NULL) { 2992 mtx_unlock(&softc->ctl_lock); 2993 free(new_err_desc, M_CTL); 2994 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2995 __func__, (uintmax_t)err_desc->lun_id); 2996 retval = EINVAL; 2997 break; 2998 } 2999 mtx_lock(&lun->lun_lock); 3000 mtx_unlock(&softc->ctl_lock); 3001 3002 /* 3003 * We could do some checking here to verify the validity 3004 * of the request, but given the complexity of error 3005 * injection requests, the checking logic would be fairly 3006 * complex. 3007 * 3008 * For now, if the request is invalid, it just won't get 3009 * executed and might get deleted. 3010 */ 3011 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 3012 3013 /* 3014 * XXX KDM check to make sure the serial number is unique, 3015 * in case we somehow manage to wrap. That shouldn't 3016 * happen for a very long time, but it's the right thing to 3017 * do. 3018 */ 3019 new_err_desc->serial = lun->error_serial; 3020 err_desc->serial = lun->error_serial; 3021 lun->error_serial++; 3022 3023 mtx_unlock(&lun->lun_lock); 3024 break; 3025 } 3026 case CTL_ERROR_INJECT_DELETE: { 3027 struct ctl_error_desc *delete_desc, *desc, *desc2; 3028 struct ctl_lun *lun; 3029 int delete_done; 3030 3031 delete_desc = (struct ctl_error_desc *)addr; 3032 delete_done = 0; 3033 3034 mtx_lock(&softc->ctl_lock); 3035 lun = softc->ctl_luns[delete_desc->lun_id]; 3036 if (lun == NULL) { 3037 mtx_unlock(&softc->ctl_lock); 3038 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3039 __func__, (uintmax_t)delete_desc->lun_id); 3040 retval = EINVAL; 3041 break; 3042 } 3043 mtx_lock(&lun->lun_lock); 3044 mtx_unlock(&softc->ctl_lock); 3045 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3046 if (desc->serial != delete_desc->serial) 3047 continue; 3048 3049 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3050 links); 3051 free(desc, M_CTL); 3052 delete_done = 1; 3053 } 3054 mtx_unlock(&lun->lun_lock); 3055 if (delete_done == 0) { 3056 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3057 "error serial %ju on LUN %u\n", __func__, 3058 delete_desc->serial, delete_desc->lun_id); 3059 retval = EINVAL; 3060 break; 3061 } 3062 break; 3063 } 3064 case CTL_DUMP_STRUCTS: { 3065 int i, j, k; 3066 struct ctl_port *port; 3067 struct ctl_frontend *fe; 3068 3069 mtx_lock(&softc->ctl_lock); 3070 printf("CTL Persistent Reservation information start:\n"); 3071 for (i = 0; i < CTL_MAX_LUNS; i++) { 3072 struct ctl_lun *lun; 3073 3074 lun = softc->ctl_luns[i]; 3075 3076 if ((lun == NULL) 3077 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3078 continue; 3079 3080 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3081 if (lun->pr_keys[j] == NULL) 3082 continue; 3083 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3084 if (lun->pr_keys[j][k] == 0) 3085 continue; 3086 printf(" LUN %d port %d iid %d key " 3087 "%#jx\n", i, j, k, 3088 (uintmax_t)lun->pr_keys[j][k]); 3089 } 3090 } 3091 } 3092 printf("CTL Persistent Reservation information end\n"); 3093 printf("CTL Ports:\n"); 3094 STAILQ_FOREACH(port, &softc->port_list, links) { 3095 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3096 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3097 port->frontend->name, port->port_type, 3098 port->physical_port, port->virtual_port, 3099 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3100 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3101 if (port->wwpn_iid[j].in_use == 0 && 3102 port->wwpn_iid[j].wwpn == 0 && 3103 port->wwpn_iid[j].name == NULL) 3104 continue; 3105 3106 printf(" iid %u use %d WWPN %#jx '%s'\n", 3107 j, port->wwpn_iid[j].in_use, 3108 (uintmax_t)port->wwpn_iid[j].wwpn, 3109 port->wwpn_iid[j].name); 3110 } 3111 } 3112 printf("CTL Port information end\n"); 3113 mtx_unlock(&softc->ctl_lock); 3114 /* 3115 * XXX KDM calling this without a lock. We'd likely want 3116 * to drop the lock before calling the frontend's dump 3117 * routine anyway. 3118 */ 3119 printf("CTL Frontends:\n"); 3120 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3121 printf(" Frontend '%s'\n", fe->name); 3122 if (fe->fe_dump != NULL) 3123 fe->fe_dump(); 3124 } 3125 printf("CTL Frontend information end\n"); 3126 break; 3127 } 3128 case CTL_LUN_REQ: { 3129 struct ctl_lun_req *lun_req; 3130 struct ctl_backend_driver *backend; 3131 3132 lun_req = (struct ctl_lun_req *)addr; 3133 3134 backend = ctl_backend_find(lun_req->backend); 3135 if (backend == NULL) { 3136 lun_req->status = CTL_LUN_ERROR; 3137 snprintf(lun_req->error_str, 3138 sizeof(lun_req->error_str), 3139 "Backend \"%s\" not found.", 3140 lun_req->backend); 3141 break; 3142 } 3143 if (lun_req->num_be_args > 0) { 3144 lun_req->kern_be_args = ctl_copyin_args( 3145 lun_req->num_be_args, 3146 lun_req->be_args, 3147 lun_req->error_str, 3148 sizeof(lun_req->error_str)); 3149 if (lun_req->kern_be_args == NULL) { 3150 lun_req->status = CTL_LUN_ERROR; 3151 break; 3152 } 3153 } 3154 3155 retval = backend->ioctl(dev, cmd, addr, flag, td); 3156 3157 if (lun_req->num_be_args > 0) { 3158 ctl_copyout_args(lun_req->num_be_args, 3159 lun_req->kern_be_args); 3160 ctl_free_args(lun_req->num_be_args, 3161 lun_req->kern_be_args); 3162 } 3163 break; 3164 } 3165 case CTL_LUN_LIST: { 3166 struct sbuf *sb; 3167 struct ctl_lun *lun; 3168 struct ctl_lun_list *list; 3169 struct ctl_option *opt; 3170 3171 list = (struct ctl_lun_list *)addr; 3172 3173 /* 3174 * Allocate a fixed length sbuf here, based on the length 3175 * of the user's buffer. We could allocate an auto-extending 3176 * buffer, and then tell the user how much larger our 3177 * amount of data is than his buffer, but that presents 3178 * some problems: 3179 * 3180 * 1. The sbuf(9) routines use a blocking malloc, and so 3181 * we can't hold a lock while calling them with an 3182 * auto-extending buffer. 3183 * 3184 * 2. There is not currently a LUN reference counting 3185 * mechanism, outside of outstanding transactions on 3186 * the LUN's OOA queue. So a LUN could go away on us 3187 * while we're getting the LUN number, backend-specific 3188 * information, etc. Thus, given the way things 3189 * currently work, we need to hold the CTL lock while 3190 * grabbing LUN information. 3191 * 3192 * So, from the user's standpoint, the best thing to do is 3193 * allocate what he thinks is a reasonable buffer length, 3194 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3195 * double the buffer length and try again. (And repeat 3196 * that until he succeeds.) 3197 */ 3198 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3199 if (sb == NULL) { 3200 list->status = CTL_LUN_LIST_ERROR; 3201 snprintf(list->error_str, sizeof(list->error_str), 3202 "Unable to allocate %d bytes for LUN list", 3203 list->alloc_len); 3204 break; 3205 } 3206 3207 sbuf_printf(sb, "<ctllunlist>\n"); 3208 3209 mtx_lock(&softc->ctl_lock); 3210 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3211 mtx_lock(&lun->lun_lock); 3212 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3213 (uintmax_t)lun->lun); 3214 3215 /* 3216 * Bail out as soon as we see that we've overfilled 3217 * the buffer. 3218 */ 3219 if (retval != 0) 3220 break; 3221 3222 retval = sbuf_printf(sb, "\t<backend_type>%s" 3223 "</backend_type>\n", 3224 (lun->backend == NULL) ? "none" : 3225 lun->backend->name); 3226 3227 if (retval != 0) 3228 break; 3229 3230 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3231 lun->be_lun->lun_type); 3232 3233 if (retval != 0) 3234 break; 3235 3236 if (lun->backend == NULL) { 3237 retval = sbuf_printf(sb, "</lun>\n"); 3238 if (retval != 0) 3239 break; 3240 continue; 3241 } 3242 3243 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3244 (lun->be_lun->maxlba > 0) ? 3245 lun->be_lun->maxlba + 1 : 0); 3246 3247 if (retval != 0) 3248 break; 3249 3250 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3251 lun->be_lun->blocksize); 3252 3253 if (retval != 0) 3254 break; 3255 3256 retval = sbuf_printf(sb, "\t<serial_number>"); 3257 3258 if (retval != 0) 3259 break; 3260 3261 retval = ctl_sbuf_printf_esc(sb, 3262 lun->be_lun->serial_num, 3263 sizeof(lun->be_lun->serial_num)); 3264 3265 if (retval != 0) 3266 break; 3267 3268 retval = sbuf_printf(sb, "</serial_number>\n"); 3269 3270 if (retval != 0) 3271 break; 3272 3273 retval = sbuf_printf(sb, "\t<device_id>"); 3274 3275 if (retval != 0) 3276 break; 3277 3278 retval = ctl_sbuf_printf_esc(sb, 3279 lun->be_lun->device_id, 3280 sizeof(lun->be_lun->device_id)); 3281 3282 if (retval != 0) 3283 break; 3284 3285 retval = sbuf_printf(sb, "</device_id>\n"); 3286 3287 if (retval != 0) 3288 break; 3289 3290 if (lun->backend->lun_info != NULL) { 3291 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3292 if (retval != 0) 3293 break; 3294 } 3295 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3296 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3297 opt->name, opt->value, opt->name); 3298 if (retval != 0) 3299 break; 3300 } 3301 3302 retval = sbuf_printf(sb, "</lun>\n"); 3303 3304 if (retval != 0) 3305 break; 3306 mtx_unlock(&lun->lun_lock); 3307 } 3308 if (lun != NULL) 3309 mtx_unlock(&lun->lun_lock); 3310 mtx_unlock(&softc->ctl_lock); 3311 3312 if ((retval != 0) 3313 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3314 retval = 0; 3315 sbuf_delete(sb); 3316 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3317 snprintf(list->error_str, sizeof(list->error_str), 3318 "Out of space, %d bytes is too small", 3319 list->alloc_len); 3320 break; 3321 } 3322 3323 sbuf_finish(sb); 3324 3325 retval = copyout(sbuf_data(sb), list->lun_xml, 3326 sbuf_len(sb) + 1); 3327 3328 list->fill_len = sbuf_len(sb) + 1; 3329 list->status = CTL_LUN_LIST_OK; 3330 sbuf_delete(sb); 3331 break; 3332 } 3333 case CTL_ISCSI: { 3334 struct ctl_iscsi *ci; 3335 struct ctl_frontend *fe; 3336 3337 ci = (struct ctl_iscsi *)addr; 3338 3339 fe = ctl_frontend_find("iscsi"); 3340 if (fe == NULL) { 3341 ci->status = CTL_ISCSI_ERROR; 3342 snprintf(ci->error_str, sizeof(ci->error_str), 3343 "Frontend \"iscsi\" not found."); 3344 break; 3345 } 3346 3347 retval = fe->ioctl(dev, cmd, addr, flag, td); 3348 break; 3349 } 3350 case CTL_PORT_REQ: { 3351 struct ctl_req *req; 3352 struct ctl_frontend *fe; 3353 3354 req = (struct ctl_req *)addr; 3355 3356 fe = ctl_frontend_find(req->driver); 3357 if (fe == NULL) { 3358 req->status = CTL_LUN_ERROR; 3359 snprintf(req->error_str, sizeof(req->error_str), 3360 "Frontend \"%s\" not found.", req->driver); 3361 break; 3362 } 3363 if (req->num_args > 0) { 3364 req->kern_args = ctl_copyin_args(req->num_args, 3365 req->args, req->error_str, sizeof(req->error_str)); 3366 if (req->kern_args == NULL) { 3367 req->status = CTL_LUN_ERROR; 3368 break; 3369 } 3370 } 3371 3372 retval = fe->ioctl(dev, cmd, addr, flag, td); 3373 3374 if (req->num_args > 0) { 3375 ctl_copyout_args(req->num_args, req->kern_args); 3376 ctl_free_args(req->num_args, req->kern_args); 3377 } 3378 break; 3379 } 3380 case CTL_PORT_LIST: { 3381 struct sbuf *sb; 3382 struct ctl_port *port; 3383 struct ctl_lun_list *list; 3384 struct ctl_option *opt; 3385 int j; 3386 uint32_t plun; 3387 3388 list = (struct ctl_lun_list *)addr; 3389 3390 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3391 if (sb == NULL) { 3392 list->status = CTL_LUN_LIST_ERROR; 3393 snprintf(list->error_str, sizeof(list->error_str), 3394 "Unable to allocate %d bytes for LUN list", 3395 list->alloc_len); 3396 break; 3397 } 3398 3399 sbuf_printf(sb, "<ctlportlist>\n"); 3400 3401 mtx_lock(&softc->ctl_lock); 3402 STAILQ_FOREACH(port, &softc->port_list, links) { 3403 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3404 (uintmax_t)port->targ_port); 3405 3406 /* 3407 * Bail out as soon as we see that we've overfilled 3408 * the buffer. 3409 */ 3410 if (retval != 0) 3411 break; 3412 3413 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3414 "</frontend_type>\n", port->frontend->name); 3415 if (retval != 0) 3416 break; 3417 3418 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3419 port->port_type); 3420 if (retval != 0) 3421 break; 3422 3423 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3424 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3425 if (retval != 0) 3426 break; 3427 3428 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3429 port->port_name); 3430 if (retval != 0) 3431 break; 3432 3433 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3434 port->physical_port); 3435 if (retval != 0) 3436 break; 3437 3438 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3439 port->virtual_port); 3440 if (retval != 0) 3441 break; 3442 3443 if (port->target_devid != NULL) { 3444 sbuf_printf(sb, "\t<target>"); 3445 ctl_id_sbuf(port->target_devid, sb); 3446 sbuf_printf(sb, "</target>\n"); 3447 } 3448 3449 if (port->port_devid != NULL) { 3450 sbuf_printf(sb, "\t<port>"); 3451 ctl_id_sbuf(port->port_devid, sb); 3452 sbuf_printf(sb, "</port>\n"); 3453 } 3454 3455 if (port->port_info != NULL) { 3456 retval = port->port_info(port->onoff_arg, sb); 3457 if (retval != 0) 3458 break; 3459 } 3460 STAILQ_FOREACH(opt, &port->options, links) { 3461 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3462 opt->name, opt->value, opt->name); 3463 if (retval != 0) 3464 break; 3465 } 3466 3467 if (port->lun_map != NULL) { 3468 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3469 for (j = 0; j < CTL_MAX_LUNS; j++) { 3470 plun = ctl_lun_map_from_port(port, j); 3471 if (plun >= CTL_MAX_LUNS) 3472 continue; 3473 sbuf_printf(sb, 3474 "\t<lun id=\"%u\">%u</lun>\n", 3475 j, plun); 3476 } 3477 } 3478 3479 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3480 if (port->wwpn_iid[j].in_use == 0 || 3481 (port->wwpn_iid[j].wwpn == 0 && 3482 port->wwpn_iid[j].name == NULL)) 3483 continue; 3484 3485 if (port->wwpn_iid[j].name != NULL) 3486 retval = sbuf_printf(sb, 3487 "\t<initiator id=\"%u\">%s</initiator>\n", 3488 j, port->wwpn_iid[j].name); 3489 else 3490 retval = sbuf_printf(sb, 3491 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3492 j, port->wwpn_iid[j].wwpn); 3493 if (retval != 0) 3494 break; 3495 } 3496 if (retval != 0) 3497 break; 3498 3499 retval = sbuf_printf(sb, "</targ_port>\n"); 3500 if (retval != 0) 3501 break; 3502 } 3503 mtx_unlock(&softc->ctl_lock); 3504 3505 if ((retval != 0) 3506 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3507 retval = 0; 3508 sbuf_delete(sb); 3509 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3510 snprintf(list->error_str, sizeof(list->error_str), 3511 "Out of space, %d bytes is too small", 3512 list->alloc_len); 3513 break; 3514 } 3515 3516 sbuf_finish(sb); 3517 3518 retval = copyout(sbuf_data(sb), list->lun_xml, 3519 sbuf_len(sb) + 1); 3520 3521 list->fill_len = sbuf_len(sb) + 1; 3522 list->status = CTL_LUN_LIST_OK; 3523 sbuf_delete(sb); 3524 break; 3525 } 3526 case CTL_LUN_MAP: { 3527 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3528 struct ctl_port *port; 3529 3530 mtx_lock(&softc->ctl_lock); 3531 if (lm->port >= CTL_MAX_PORTS || 3532 (port = softc->ctl_ports[lm->port]) == NULL) { 3533 mtx_unlock(&softc->ctl_lock); 3534 return (ENXIO); 3535 } 3536 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3537 if (lm->plun < CTL_MAX_LUNS) { 3538 if (lm->lun == UINT32_MAX) 3539 retval = ctl_lun_map_unset(port, lm->plun); 3540 else if (lm->lun < CTL_MAX_LUNS && 3541 softc->ctl_luns[lm->lun] != NULL) 3542 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3543 else 3544 return (ENXIO); 3545 } else if (lm->plun == UINT32_MAX) { 3546 if (lm->lun == UINT32_MAX) 3547 retval = ctl_lun_map_deinit(port); 3548 else 3549 retval = ctl_lun_map_init(port); 3550 } else 3551 return (ENXIO); 3552 break; 3553 } 3554 default: { 3555 /* XXX KDM should we fix this? */ 3556#if 0 3557 struct ctl_backend_driver *backend; 3558 unsigned int type; 3559 int found; 3560 3561 found = 0; 3562 3563 /* 3564 * We encode the backend type as the ioctl type for backend 3565 * ioctls. So parse it out here, and then search for a 3566 * backend of this type. 3567 */ 3568 type = _IOC_TYPE(cmd); 3569 3570 STAILQ_FOREACH(backend, &softc->be_list, links) { 3571 if (backend->type == type) { 3572 found = 1; 3573 break; 3574 } 3575 } 3576 if (found == 0) { 3577 printf("ctl: unknown ioctl command %#lx or backend " 3578 "%d\n", cmd, type); 3579 retval = EINVAL; 3580 break; 3581 } 3582 retval = backend->ioctl(dev, cmd, addr, flag, td); 3583#endif 3584 retval = ENOTTY; 3585 break; 3586 } 3587 } 3588 return (retval); 3589} 3590 3591uint32_t 3592ctl_get_initindex(struct ctl_nexus *nexus) 3593{ 3594 if (nexus->targ_port < CTL_MAX_PORTS) 3595 return (nexus->initid.id + 3596 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3597 else 3598 return (nexus->initid.id + 3599 ((nexus->targ_port - CTL_MAX_PORTS) * 3600 CTL_MAX_INIT_PER_PORT)); 3601} 3602 3603uint32_t 3604ctl_get_resindex(struct ctl_nexus *nexus) 3605{ 3606 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3607} 3608 3609uint32_t 3610ctl_port_idx(int port_num) 3611{ 3612 if (port_num < CTL_MAX_PORTS) 3613 return(port_num); 3614 else 3615 return(port_num - CTL_MAX_PORTS); 3616} 3617 3618int 3619ctl_lun_map_init(struct ctl_port *port) 3620{ 3621 struct ctl_softc *softc = control_softc; 3622 struct ctl_lun *lun; 3623 uint32_t i; 3624 3625 if (port->lun_map == NULL) 3626 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3627 M_CTL, M_NOWAIT); 3628 if (port->lun_map == NULL) 3629 return (ENOMEM); 3630 for (i = 0; i < CTL_MAX_LUNS; i++) 3631 port->lun_map[i] = UINT32_MAX; 3632 if (port->status & CTL_PORT_STATUS_ONLINE) { 3633 STAILQ_FOREACH(lun, &softc->lun_list, links) 3634 port->lun_disable(port->targ_lun_arg, lun->lun); 3635 } 3636 return (0); 3637} 3638 3639int 3640ctl_lun_map_deinit(struct ctl_port *port) 3641{ 3642 struct ctl_softc *softc = control_softc; 3643 struct ctl_lun *lun; 3644 3645 if (port->lun_map == NULL) 3646 return (0); 3647 free(port->lun_map, M_CTL); 3648 port->lun_map = NULL; 3649 if (port->status & CTL_PORT_STATUS_ONLINE) { 3650 STAILQ_FOREACH(lun, &softc->lun_list, links) 3651 port->lun_enable(port->targ_lun_arg, lun->lun); 3652 } 3653 return (0); 3654} 3655 3656int 3657ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3658{ 3659 int status; 3660 uint32_t old; 3661 3662 if (port->lun_map == NULL) { 3663 status = ctl_lun_map_init(port); 3664 if (status != 0) 3665 return (status); 3666 } 3667 old = port->lun_map[plun]; 3668 port->lun_map[plun] = glun; 3669 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) 3670 port->lun_enable(port->targ_lun_arg, plun); 3671 return (0); 3672} 3673 3674int 3675ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3676{ 3677 uint32_t old; 3678 3679 if (port->lun_map == NULL) 3680 return (0); 3681 old = port->lun_map[plun]; 3682 port->lun_map[plun] = UINT32_MAX; 3683 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) 3684 port->lun_disable(port->targ_lun_arg, plun); 3685 return (0); 3686} 3687 3688uint32_t 3689ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3690{ 3691 3692 if (port == NULL) 3693 return (UINT32_MAX); 3694 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3695 return (lun_id); 3696 return (port->lun_map[lun_id]); 3697} 3698 3699uint32_t 3700ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3701{ 3702 uint32_t i; 3703 3704 if (port == NULL) 3705 return (UINT32_MAX); 3706 if (port->lun_map == NULL) 3707 return (lun_id); 3708 for (i = 0; i < CTL_MAX_LUNS; i++) { 3709 if (port->lun_map[i] == lun_id) 3710 return (i); 3711 } 3712 return (UINT32_MAX); 3713} 3714 3715static struct ctl_port * 3716ctl_io_port(struct ctl_io_hdr *io_hdr) 3717{ 3718 int port_num; 3719 3720 port_num = io_hdr->nexus.targ_port; 3721 return (control_softc->ctl_ports[ctl_port_idx(port_num)]); 3722} 3723 3724/* 3725 * Note: This only works for bitmask sizes that are at least 32 bits, and 3726 * that are a power of 2. 3727 */ 3728int 3729ctl_ffz(uint32_t *mask, uint32_t size) 3730{ 3731 uint32_t num_chunks, num_pieces; 3732 int i, j; 3733 3734 num_chunks = (size >> 5); 3735 if (num_chunks == 0) 3736 num_chunks++; 3737 num_pieces = MIN((sizeof(uint32_t) * 8), size); 3738 3739 for (i = 0; i < num_chunks; i++) { 3740 for (j = 0; j < num_pieces; j++) { 3741 if ((mask[i] & (1 << j)) == 0) 3742 return ((i << 5) + j); 3743 } 3744 } 3745 3746 return (-1); 3747} 3748 3749int 3750ctl_set_mask(uint32_t *mask, uint32_t bit) 3751{ 3752 uint32_t chunk, piece; 3753 3754 chunk = bit >> 5; 3755 piece = bit % (sizeof(uint32_t) * 8); 3756 3757 if ((mask[chunk] & (1 << piece)) != 0) 3758 return (-1); 3759 else 3760 mask[chunk] |= (1 << piece); 3761 3762 return (0); 3763} 3764 3765int 3766ctl_clear_mask(uint32_t *mask, uint32_t bit) 3767{ 3768 uint32_t chunk, piece; 3769 3770 chunk = bit >> 5; 3771 piece = bit % (sizeof(uint32_t) * 8); 3772 3773 if ((mask[chunk] & (1 << piece)) == 0) 3774 return (-1); 3775 else 3776 mask[chunk] &= ~(1 << piece); 3777 3778 return (0); 3779} 3780 3781int 3782ctl_is_set(uint32_t *mask, uint32_t bit) 3783{ 3784 uint32_t chunk, piece; 3785 3786 chunk = bit >> 5; 3787 piece = bit % (sizeof(uint32_t) * 8); 3788 3789 if ((mask[chunk] & (1 << piece)) == 0) 3790 return (0); 3791 else 3792 return (1); 3793} 3794 3795static uint64_t 3796ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3797{ 3798 uint64_t *t; 3799 3800 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3801 if (t == NULL) 3802 return (0); 3803 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3804} 3805 3806static void 3807ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3808{ 3809 uint64_t *t; 3810 3811 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3812 if (t == NULL) 3813 return; 3814 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3815} 3816 3817static void 3818ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3819{ 3820 uint64_t *p; 3821 u_int i; 3822 3823 i = residx/CTL_MAX_INIT_PER_PORT; 3824 if (lun->pr_keys[i] != NULL) 3825 return; 3826 mtx_unlock(&lun->lun_lock); 3827 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3828 M_WAITOK | M_ZERO); 3829 mtx_lock(&lun->lun_lock); 3830 if (lun->pr_keys[i] == NULL) 3831 lun->pr_keys[i] = p; 3832 else 3833 free(p, M_CTL); 3834} 3835 3836static void 3837ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3838{ 3839 uint64_t *t; 3840 3841 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3842 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3843 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3844} 3845 3846/* 3847 * ctl_softc, pool_name, total_ctl_io are passed in. 3848 * npool is passed out. 3849 */ 3850int 3851ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3852 uint32_t total_ctl_io, void **npool) 3853{ 3854#ifdef IO_POOLS 3855 struct ctl_io_pool *pool; 3856 3857 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3858 M_NOWAIT | M_ZERO); 3859 if (pool == NULL) 3860 return (ENOMEM); 3861 3862 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3863 pool->ctl_softc = ctl_softc; 3864 pool->zone = uma_zsecond_create(pool->name, NULL, 3865 NULL, NULL, NULL, ctl_softc->io_zone); 3866 /* uma_prealloc(pool->zone, total_ctl_io); */ 3867 3868 *npool = pool; 3869#else 3870 *npool = ctl_softc->io_zone; 3871#endif 3872 return (0); 3873} 3874 3875void 3876ctl_pool_free(struct ctl_io_pool *pool) 3877{ 3878 3879 if (pool == NULL) 3880 return; 3881 3882#ifdef IO_POOLS 3883 uma_zdestroy(pool->zone); 3884 free(pool, M_CTL); 3885#endif 3886} 3887 3888union ctl_io * 3889ctl_alloc_io(void *pool_ref) 3890{ 3891 union ctl_io *io; 3892#ifdef IO_POOLS 3893 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3894 3895 io = uma_zalloc(pool->zone, M_WAITOK); 3896#else 3897 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3898#endif 3899 if (io != NULL) 3900 io->io_hdr.pool = pool_ref; 3901 return (io); 3902} 3903 3904union ctl_io * 3905ctl_alloc_io_nowait(void *pool_ref) 3906{ 3907 union ctl_io *io; 3908#ifdef IO_POOLS 3909 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3910 3911 io = uma_zalloc(pool->zone, M_NOWAIT); 3912#else 3913 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3914#endif 3915 if (io != NULL) 3916 io->io_hdr.pool = pool_ref; 3917 return (io); 3918} 3919 3920void 3921ctl_free_io(union ctl_io *io) 3922{ 3923#ifdef IO_POOLS 3924 struct ctl_io_pool *pool; 3925#endif 3926 3927 if (io == NULL) 3928 return; 3929 3930#ifdef IO_POOLS 3931 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3932 uma_zfree(pool->zone, io); 3933#else 3934 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3935#endif 3936} 3937 3938void 3939ctl_zero_io(union ctl_io *io) 3940{ 3941 void *pool_ref; 3942 3943 if (io == NULL) 3944 return; 3945 3946 /* 3947 * May need to preserve linked list pointers at some point too. 3948 */ 3949 pool_ref = io->io_hdr.pool; 3950 memset(io, 0, sizeof(*io)); 3951 io->io_hdr.pool = pool_ref; 3952} 3953 3954/* 3955 * This routine is currently used for internal copies of ctl_ios that need 3956 * to persist for some reason after we've already returned status to the 3957 * FETD. (Thus the flag set.) 3958 * 3959 * XXX XXX 3960 * Note that this makes a blind copy of all fields in the ctl_io, except 3961 * for the pool reference. This includes any memory that has been 3962 * allocated! That memory will no longer be valid after done has been 3963 * called, so this would be VERY DANGEROUS for command that actually does 3964 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3965 * start and stop commands, which don't transfer any data, so this is not a 3966 * problem. If it is used for anything else, the caller would also need to 3967 * allocate data buffer space and this routine would need to be modified to 3968 * copy the data buffer(s) as well. 3969 */ 3970void 3971ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3972{ 3973 void *pool_ref; 3974 3975 if ((src == NULL) 3976 || (dest == NULL)) 3977 return; 3978 3979 /* 3980 * May need to preserve linked list pointers at some point too. 3981 */ 3982 pool_ref = dest->io_hdr.pool; 3983 3984 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest))); 3985 3986 dest->io_hdr.pool = pool_ref; 3987 /* 3988 * We need to know that this is an internal copy, and doesn't need 3989 * to get passed back to the FETD that allocated it. 3990 */ 3991 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3992} 3993 3994int 3995ctl_expand_number(const char *buf, uint64_t *num) 3996{ 3997 char *endptr; 3998 uint64_t number; 3999 unsigned shift; 4000 4001 number = strtoq(buf, &endptr, 0); 4002 4003 switch (tolower((unsigned char)*endptr)) { 4004 case 'e': 4005 shift = 60; 4006 break; 4007 case 'p': 4008 shift = 50; 4009 break; 4010 case 't': 4011 shift = 40; 4012 break; 4013 case 'g': 4014 shift = 30; 4015 break; 4016 case 'm': 4017 shift = 20; 4018 break; 4019 case 'k': 4020 shift = 10; 4021 break; 4022 case 'b': 4023 case '\0': /* No unit. */ 4024 *num = number; 4025 return (0); 4026 default: 4027 /* Unrecognized unit. */ 4028 return (-1); 4029 } 4030 4031 if ((number << shift) >> shift != number) { 4032 /* Overflow */ 4033 return (-1); 4034 } 4035 *num = number << shift; 4036 return (0); 4037} 4038 4039 4040/* 4041 * This routine could be used in the future to load default and/or saved 4042 * mode page parameters for a particuar lun. 4043 */ 4044static int 4045ctl_init_page_index(struct ctl_lun *lun) 4046{ 4047 int i; 4048 struct ctl_page_index *page_index; 4049 const char *value; 4050 uint64_t ival; 4051 4052 memcpy(&lun->mode_pages.index, page_index_template, 4053 sizeof(page_index_template)); 4054 4055 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4056 4057 page_index = &lun->mode_pages.index[i]; 4058 /* 4059 * If this is a disk-only mode page, there's no point in 4060 * setting it up. For some pages, we have to have some 4061 * basic information about the disk in order to calculate the 4062 * mode page data. 4063 */ 4064 if ((lun->be_lun->lun_type != T_DIRECT) 4065 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4066 continue; 4067 4068 switch (page_index->page_code & SMPH_PC_MASK) { 4069 case SMS_RW_ERROR_RECOVERY_PAGE: { 4070 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4071 panic("subpage is incorrect!"); 4072 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 4073 &rw_er_page_default, 4074 sizeof(rw_er_page_default)); 4075 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 4076 &rw_er_page_changeable, 4077 sizeof(rw_er_page_changeable)); 4078 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 4079 &rw_er_page_default, 4080 sizeof(rw_er_page_default)); 4081 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 4082 &rw_er_page_default, 4083 sizeof(rw_er_page_default)); 4084 page_index->page_data = 4085 (uint8_t *)lun->mode_pages.rw_er_page; 4086 break; 4087 } 4088 case SMS_FORMAT_DEVICE_PAGE: { 4089 struct scsi_format_page *format_page; 4090 4091 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4092 panic("subpage is incorrect!"); 4093 4094 /* 4095 * Sectors per track are set above. Bytes per 4096 * sector need to be set here on a per-LUN basis. 4097 */ 4098 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 4099 &format_page_default, 4100 sizeof(format_page_default)); 4101 memcpy(&lun->mode_pages.format_page[ 4102 CTL_PAGE_CHANGEABLE], &format_page_changeable, 4103 sizeof(format_page_changeable)); 4104 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 4105 &format_page_default, 4106 sizeof(format_page_default)); 4107 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 4108 &format_page_default, 4109 sizeof(format_page_default)); 4110 4111 format_page = &lun->mode_pages.format_page[ 4112 CTL_PAGE_CURRENT]; 4113 scsi_ulto2b(lun->be_lun->blocksize, 4114 format_page->bytes_per_sector); 4115 4116 format_page = &lun->mode_pages.format_page[ 4117 CTL_PAGE_DEFAULT]; 4118 scsi_ulto2b(lun->be_lun->blocksize, 4119 format_page->bytes_per_sector); 4120 4121 format_page = &lun->mode_pages.format_page[ 4122 CTL_PAGE_SAVED]; 4123 scsi_ulto2b(lun->be_lun->blocksize, 4124 format_page->bytes_per_sector); 4125 4126 page_index->page_data = 4127 (uint8_t *)lun->mode_pages.format_page; 4128 break; 4129 } 4130 case SMS_RIGID_DISK_PAGE: { 4131 struct scsi_rigid_disk_page *rigid_disk_page; 4132 uint32_t sectors_per_cylinder; 4133 uint64_t cylinders; 4134#ifndef __XSCALE__ 4135 int shift; 4136#endif /* !__XSCALE__ */ 4137 4138 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4139 panic("invalid subpage value %d", 4140 page_index->subpage); 4141 4142 /* 4143 * Rotation rate and sectors per track are set 4144 * above. We calculate the cylinders here based on 4145 * capacity. Due to the number of heads and 4146 * sectors per track we're using, smaller arrays 4147 * may turn out to have 0 cylinders. Linux and 4148 * FreeBSD don't pay attention to these mode pages 4149 * to figure out capacity, but Solaris does. It 4150 * seems to deal with 0 cylinders just fine, and 4151 * works out a fake geometry based on the capacity. 4152 */ 4153 memcpy(&lun->mode_pages.rigid_disk_page[ 4154 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 4155 sizeof(rigid_disk_page_default)); 4156 memcpy(&lun->mode_pages.rigid_disk_page[ 4157 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4158 sizeof(rigid_disk_page_changeable)); 4159 4160 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4161 CTL_DEFAULT_HEADS; 4162 4163 /* 4164 * The divide method here will be more accurate, 4165 * probably, but results in floating point being 4166 * used in the kernel on i386 (__udivdi3()). On the 4167 * XScale, though, __udivdi3() is implemented in 4168 * software. 4169 * 4170 * The shift method for cylinder calculation is 4171 * accurate if sectors_per_cylinder is a power of 4172 * 2. Otherwise it might be slightly off -- you 4173 * might have a bit of a truncation problem. 4174 */ 4175#ifdef __XSCALE__ 4176 cylinders = (lun->be_lun->maxlba + 1) / 4177 sectors_per_cylinder; 4178#else 4179 for (shift = 31; shift > 0; shift--) { 4180 if (sectors_per_cylinder & (1 << shift)) 4181 break; 4182 } 4183 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4184#endif 4185 4186 /* 4187 * We've basically got 3 bytes, or 24 bits for the 4188 * cylinder size in the mode page. If we're over, 4189 * just round down to 2^24. 4190 */ 4191 if (cylinders > 0xffffff) 4192 cylinders = 0xffffff; 4193 4194 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4195 CTL_PAGE_DEFAULT]; 4196 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4197 4198 if ((value = ctl_get_opt(&lun->be_lun->options, 4199 "rpm")) != NULL) { 4200 scsi_ulto2b(strtol(value, NULL, 0), 4201 rigid_disk_page->rotation_rate); 4202 } 4203 4204 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4205 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4206 sizeof(rigid_disk_page_default)); 4207 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4208 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4209 sizeof(rigid_disk_page_default)); 4210 4211 page_index->page_data = 4212 (uint8_t *)lun->mode_pages.rigid_disk_page; 4213 break; 4214 } 4215 case SMS_CACHING_PAGE: { 4216 struct scsi_caching_page *caching_page; 4217 4218 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4219 panic("invalid subpage value %d", 4220 page_index->subpage); 4221 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4222 &caching_page_default, 4223 sizeof(caching_page_default)); 4224 memcpy(&lun->mode_pages.caching_page[ 4225 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4226 sizeof(caching_page_changeable)); 4227 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4228 &caching_page_default, 4229 sizeof(caching_page_default)); 4230 caching_page = &lun->mode_pages.caching_page[ 4231 CTL_PAGE_SAVED]; 4232 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4233 if (value != NULL && strcmp(value, "off") == 0) 4234 caching_page->flags1 &= ~SCP_WCE; 4235 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4236 if (value != NULL && strcmp(value, "off") == 0) 4237 caching_page->flags1 |= SCP_RCD; 4238 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4239 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4240 sizeof(caching_page_default)); 4241 page_index->page_data = 4242 (uint8_t *)lun->mode_pages.caching_page; 4243 break; 4244 } 4245 case SMS_CONTROL_MODE_PAGE: { 4246 struct scsi_control_page *control_page; 4247 4248 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4249 panic("invalid subpage value %d", 4250 page_index->subpage); 4251 4252 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4253 &control_page_default, 4254 sizeof(control_page_default)); 4255 memcpy(&lun->mode_pages.control_page[ 4256 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4257 sizeof(control_page_changeable)); 4258 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4259 &control_page_default, 4260 sizeof(control_page_default)); 4261 control_page = &lun->mode_pages.control_page[ 4262 CTL_PAGE_SAVED]; 4263 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4264 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4265 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4266 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4267 } 4268 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4269 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4270 sizeof(control_page_default)); 4271 page_index->page_data = 4272 (uint8_t *)lun->mode_pages.control_page; 4273 break; 4274 4275 } 4276 case SMS_INFO_EXCEPTIONS_PAGE: { 4277 switch (page_index->subpage) { 4278 case SMS_SUBPAGE_PAGE_0: 4279 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4280 &ie_page_default, 4281 sizeof(ie_page_default)); 4282 memcpy(&lun->mode_pages.ie_page[ 4283 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4284 sizeof(ie_page_changeable)); 4285 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4286 &ie_page_default, 4287 sizeof(ie_page_default)); 4288 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4289 &ie_page_default, 4290 sizeof(ie_page_default)); 4291 page_index->page_data = 4292 (uint8_t *)lun->mode_pages.ie_page; 4293 break; 4294 case 0x02: { 4295 struct ctl_logical_block_provisioning_page *page; 4296 4297 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4298 &lbp_page_default, 4299 sizeof(lbp_page_default)); 4300 memcpy(&lun->mode_pages.lbp_page[ 4301 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4302 sizeof(lbp_page_changeable)); 4303 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4304 &lbp_page_default, 4305 sizeof(lbp_page_default)); 4306 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4307 value = ctl_get_opt(&lun->be_lun->options, 4308 "avail-threshold"); 4309 if (value != NULL && 4310 ctl_expand_number(value, &ival) == 0) { 4311 page->descr[0].flags |= SLBPPD_ENABLED | 4312 SLBPPD_ARMING_DEC; 4313 if (lun->be_lun->blocksize) 4314 ival /= lun->be_lun->blocksize; 4315 else 4316 ival /= 512; 4317 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4318 page->descr[0].count); 4319 } 4320 value = ctl_get_opt(&lun->be_lun->options, 4321 "used-threshold"); 4322 if (value != NULL && 4323 ctl_expand_number(value, &ival) == 0) { 4324 page->descr[1].flags |= SLBPPD_ENABLED | 4325 SLBPPD_ARMING_INC; 4326 if (lun->be_lun->blocksize) 4327 ival /= lun->be_lun->blocksize; 4328 else 4329 ival /= 512; 4330 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4331 page->descr[1].count); 4332 } 4333 value = ctl_get_opt(&lun->be_lun->options, 4334 "pool-avail-threshold"); 4335 if (value != NULL && 4336 ctl_expand_number(value, &ival) == 0) { 4337 page->descr[2].flags |= SLBPPD_ENABLED | 4338 SLBPPD_ARMING_DEC; 4339 if (lun->be_lun->blocksize) 4340 ival /= lun->be_lun->blocksize; 4341 else 4342 ival /= 512; 4343 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4344 page->descr[2].count); 4345 } 4346 value = ctl_get_opt(&lun->be_lun->options, 4347 "pool-used-threshold"); 4348 if (value != NULL && 4349 ctl_expand_number(value, &ival) == 0) { 4350 page->descr[3].flags |= SLBPPD_ENABLED | 4351 SLBPPD_ARMING_INC; 4352 if (lun->be_lun->blocksize) 4353 ival /= lun->be_lun->blocksize; 4354 else 4355 ival /= 512; 4356 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4357 page->descr[3].count); 4358 } 4359 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4360 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4361 sizeof(lbp_page_default)); 4362 page_index->page_data = 4363 (uint8_t *)lun->mode_pages.lbp_page; 4364 }} 4365 break; 4366 } 4367 case SMS_VENDOR_SPECIFIC_PAGE:{ 4368 switch (page_index->subpage) { 4369 case DBGCNF_SUBPAGE_CODE: { 4370 struct copan_debugconf_subpage *current_page, 4371 *saved_page; 4372 4373 memcpy(&lun->mode_pages.debugconf_subpage[ 4374 CTL_PAGE_CURRENT], 4375 &debugconf_page_default, 4376 sizeof(debugconf_page_default)); 4377 memcpy(&lun->mode_pages.debugconf_subpage[ 4378 CTL_PAGE_CHANGEABLE], 4379 &debugconf_page_changeable, 4380 sizeof(debugconf_page_changeable)); 4381 memcpy(&lun->mode_pages.debugconf_subpage[ 4382 CTL_PAGE_DEFAULT], 4383 &debugconf_page_default, 4384 sizeof(debugconf_page_default)); 4385 memcpy(&lun->mode_pages.debugconf_subpage[ 4386 CTL_PAGE_SAVED], 4387 &debugconf_page_default, 4388 sizeof(debugconf_page_default)); 4389 page_index->page_data = 4390 (uint8_t *)lun->mode_pages.debugconf_subpage; 4391 4392 current_page = (struct copan_debugconf_subpage *) 4393 (page_index->page_data + 4394 (page_index->page_len * 4395 CTL_PAGE_CURRENT)); 4396 saved_page = (struct copan_debugconf_subpage *) 4397 (page_index->page_data + 4398 (page_index->page_len * 4399 CTL_PAGE_SAVED)); 4400 break; 4401 } 4402 default: 4403 panic("invalid subpage value %d", 4404 page_index->subpage); 4405 break; 4406 } 4407 break; 4408 } 4409 default: 4410 panic("invalid page value %d", 4411 page_index->page_code & SMPH_PC_MASK); 4412 break; 4413 } 4414 } 4415 4416 return (CTL_RETVAL_COMPLETE); 4417} 4418 4419static int 4420ctl_init_log_page_index(struct ctl_lun *lun) 4421{ 4422 struct ctl_page_index *page_index; 4423 int i, j, k, prev; 4424 4425 memcpy(&lun->log_pages.index, log_page_index_template, 4426 sizeof(log_page_index_template)); 4427 4428 prev = -1; 4429 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4430 4431 page_index = &lun->log_pages.index[i]; 4432 /* 4433 * If this is a disk-only mode page, there's no point in 4434 * setting it up. For some pages, we have to have some 4435 * basic information about the disk in order to calculate the 4436 * mode page data. 4437 */ 4438 if ((lun->be_lun->lun_type != T_DIRECT) 4439 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4440 continue; 4441 4442 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4443 lun->backend->lun_attr == NULL) 4444 continue; 4445 4446 if (page_index->page_code != prev) { 4447 lun->log_pages.pages_page[j] = page_index->page_code; 4448 prev = page_index->page_code; 4449 j++; 4450 } 4451 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4452 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4453 k++; 4454 } 4455 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4456 lun->log_pages.index[0].page_len = j; 4457 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4458 lun->log_pages.index[1].page_len = k * 2; 4459 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4460 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4461 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4462 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4463 4464 return (CTL_RETVAL_COMPLETE); 4465} 4466 4467static int 4468hex2bin(const char *str, uint8_t *buf, int buf_size) 4469{ 4470 int i; 4471 u_char c; 4472 4473 memset(buf, 0, buf_size); 4474 while (isspace(str[0])) 4475 str++; 4476 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4477 str += 2; 4478 buf_size *= 2; 4479 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4480 c = str[i]; 4481 if (isdigit(c)) 4482 c -= '0'; 4483 else if (isalpha(c)) 4484 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4485 else 4486 break; 4487 if (c >= 16) 4488 break; 4489 if ((i & 1) == 0) 4490 buf[i / 2] |= (c << 4); 4491 else 4492 buf[i / 2] |= c; 4493 } 4494 return ((i + 1) / 2); 4495} 4496 4497/* 4498 * LUN allocation. 4499 * 4500 * Requirements: 4501 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4502 * wants us to allocate the LUN and he can block. 4503 * - ctl_softc is always set 4504 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4505 * 4506 * Returns 0 for success, non-zero (errno) for failure. 4507 */ 4508static int 4509ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4510 struct ctl_be_lun *const be_lun) 4511{ 4512 struct ctl_lun *nlun, *lun; 4513 struct scsi_vpd_id_descriptor *desc; 4514 struct scsi_vpd_id_t10 *t10id; 4515 const char *eui, *naa, *scsiname, *vendor, *value; 4516 int lun_number, i, lun_malloced; 4517 int devidlen, idlen1, idlen2 = 0, len; 4518 4519 if (be_lun == NULL) 4520 return (EINVAL); 4521 4522 /* 4523 * We currently only support Direct Access or Processor LUN types. 4524 */ 4525 switch (be_lun->lun_type) { 4526 case T_DIRECT: 4527 break; 4528 case T_PROCESSOR: 4529 break; 4530 case T_SEQUENTIAL: 4531 case T_CHANGER: 4532 default: 4533 be_lun->lun_config_status(be_lun->be_lun, 4534 CTL_LUN_CONFIG_FAILURE); 4535 break; 4536 } 4537 if (ctl_lun == NULL) { 4538 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4539 lun_malloced = 1; 4540 } else { 4541 lun_malloced = 0; 4542 lun = ctl_lun; 4543 } 4544 4545 memset(lun, 0, sizeof(*lun)); 4546 if (lun_malloced) 4547 lun->flags = CTL_LUN_MALLOCED; 4548 4549 /* Generate LUN ID. */ 4550 devidlen = max(CTL_DEVID_MIN_LEN, 4551 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4552 idlen1 = sizeof(*t10id) + devidlen; 4553 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4554 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4555 if (scsiname != NULL) { 4556 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4557 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4558 } 4559 eui = ctl_get_opt(&be_lun->options, "eui"); 4560 if (eui != NULL) { 4561 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4562 } 4563 naa = ctl_get_opt(&be_lun->options, "naa"); 4564 if (naa != NULL) { 4565 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4566 } 4567 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4568 M_CTL, M_WAITOK | M_ZERO); 4569 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4570 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4571 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4572 desc->length = idlen1; 4573 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4574 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4575 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4576 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4577 } else { 4578 strncpy(t10id->vendor, vendor, 4579 min(sizeof(t10id->vendor), strlen(vendor))); 4580 } 4581 strncpy((char *)t10id->vendor_spec_id, 4582 (char *)be_lun->device_id, devidlen); 4583 if (scsiname != NULL) { 4584 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4585 desc->length); 4586 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4587 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4588 SVPD_ID_TYPE_SCSI_NAME; 4589 desc->length = idlen2; 4590 strlcpy(desc->identifier, scsiname, idlen2); 4591 } 4592 if (eui != NULL) { 4593 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4594 desc->length); 4595 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4596 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4597 SVPD_ID_TYPE_EUI64; 4598 desc->length = hex2bin(eui, desc->identifier, 16); 4599 desc->length = desc->length > 12 ? 16 : 4600 (desc->length > 8 ? 12 : 8); 4601 len -= 16 - desc->length; 4602 } 4603 if (naa != NULL) { 4604 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4605 desc->length); 4606 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4607 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4608 SVPD_ID_TYPE_NAA; 4609 desc->length = hex2bin(naa, desc->identifier, 16); 4610 desc->length = desc->length > 8 ? 16 : 8; 4611 len -= 16 - desc->length; 4612 } 4613 lun->lun_devid->len = len; 4614 4615 mtx_lock(&ctl_softc->ctl_lock); 4616 /* 4617 * See if the caller requested a particular LUN number. If so, see 4618 * if it is available. Otherwise, allocate the first available LUN. 4619 */ 4620 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4621 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4622 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4623 mtx_unlock(&ctl_softc->ctl_lock); 4624 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4625 printf("ctl: requested LUN ID %d is higher " 4626 "than CTL_MAX_LUNS - 1 (%d)\n", 4627 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4628 } else { 4629 /* 4630 * XXX KDM return an error, or just assign 4631 * another LUN ID in this case?? 4632 */ 4633 printf("ctl: requested LUN ID %d is already " 4634 "in use\n", be_lun->req_lun_id); 4635 } 4636 if (lun->flags & CTL_LUN_MALLOCED) 4637 free(lun, M_CTL); 4638 be_lun->lun_config_status(be_lun->be_lun, 4639 CTL_LUN_CONFIG_FAILURE); 4640 return (ENOSPC); 4641 } 4642 lun_number = be_lun->req_lun_id; 4643 } else { 4644 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4645 if (lun_number == -1) { 4646 mtx_unlock(&ctl_softc->ctl_lock); 4647 printf("ctl: can't allocate LUN, out of LUNs\n"); 4648 if (lun->flags & CTL_LUN_MALLOCED) 4649 free(lun, M_CTL); 4650 be_lun->lun_config_status(be_lun->be_lun, 4651 CTL_LUN_CONFIG_FAILURE); 4652 return (ENOSPC); 4653 } 4654 } 4655 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4656 4657 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4658 lun->lun = lun_number; 4659 lun->be_lun = be_lun; 4660 /* 4661 * The processor LUN is always enabled. Disk LUNs come on line 4662 * disabled, and must be enabled by the backend. 4663 */ 4664 lun->flags |= CTL_LUN_DISABLED; 4665 lun->backend = be_lun->be; 4666 be_lun->ctl_lun = lun; 4667 be_lun->lun_id = lun_number; 4668 atomic_add_int(&be_lun->be->num_luns, 1); 4669 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4670 lun->flags |= CTL_LUN_OFFLINE; 4671 4672 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4673 lun->flags |= CTL_LUN_STOPPED; 4674 4675 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4676 lun->flags |= CTL_LUN_INOPERABLE; 4677 4678 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4679 lun->flags |= CTL_LUN_PRIMARY_SC; 4680 4681 value = ctl_get_opt(&be_lun->options, "readonly"); 4682 if (value != NULL && strcmp(value, "on") == 0) 4683 lun->flags |= CTL_LUN_READONLY; 4684 4685 lun->serseq = CTL_LUN_SERSEQ_OFF; 4686 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ) 4687 lun->serseq = CTL_LUN_SERSEQ_READ; 4688 value = ctl_get_opt(&be_lun->options, "serseq"); 4689 if (value != NULL && strcmp(value, "on") == 0) 4690 lun->serseq = CTL_LUN_SERSEQ_ON; 4691 else if (value != NULL && strcmp(value, "read") == 0) 4692 lun->serseq = CTL_LUN_SERSEQ_READ; 4693 else if (value != NULL && strcmp(value, "off") == 0) 4694 lun->serseq = CTL_LUN_SERSEQ_OFF; 4695 4696 lun->ctl_softc = ctl_softc; 4697#ifdef CTL_TIME_IO 4698 lun->last_busy = getsbinuptime(); 4699#endif 4700 TAILQ_INIT(&lun->ooa_queue); 4701 TAILQ_INIT(&lun->blocked_queue); 4702 STAILQ_INIT(&lun->error_list); 4703 ctl_tpc_lun_init(lun); 4704 4705 /* 4706 * Initialize the mode and log page index. 4707 */ 4708 ctl_init_page_index(lun); 4709 ctl_init_log_page_index(lun); 4710 4711 /* 4712 * Now, before we insert this lun on the lun list, set the lun 4713 * inventory changed UA for all other luns. 4714 */ 4715 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4716 mtx_lock(&nlun->lun_lock); 4717 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4718 mtx_unlock(&nlun->lun_lock); 4719 } 4720 4721 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4722 4723 ctl_softc->ctl_luns[lun_number] = lun; 4724 4725 ctl_softc->num_luns++; 4726 4727 /* Setup statistics gathering */ 4728 lun->stats.device_type = be_lun->lun_type; 4729 lun->stats.lun_number = lun_number; 4730 if (lun->stats.device_type == T_DIRECT) 4731 lun->stats.blocksize = be_lun->blocksize; 4732 else 4733 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4734 for (i = 0;i < CTL_MAX_PORTS;i++) 4735 lun->stats.ports[i].targ_port = i; 4736 4737 mtx_unlock(&ctl_softc->ctl_lock); 4738 4739 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4740 return (0); 4741} 4742 4743/* 4744 * Delete a LUN. 4745 * Assumptions: 4746 * - LUN has already been marked invalid and any pending I/O has been taken 4747 * care of. 4748 */ 4749static int 4750ctl_free_lun(struct ctl_lun *lun) 4751{ 4752 struct ctl_softc *softc; 4753 struct ctl_lun *nlun; 4754 int i; 4755 4756 softc = lun->ctl_softc; 4757 4758 mtx_assert(&softc->ctl_lock, MA_OWNED); 4759 4760 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4761 4762 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4763 4764 softc->ctl_luns[lun->lun] = NULL; 4765 4766 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4767 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4768 4769 softc->num_luns--; 4770 4771 /* 4772 * Tell the backend to free resources, if this LUN has a backend. 4773 */ 4774 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4775 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4776 4777 ctl_tpc_lun_shutdown(lun); 4778 mtx_destroy(&lun->lun_lock); 4779 free(lun->lun_devid, M_CTL); 4780 for (i = 0; i < CTL_MAX_PORTS; i++) 4781 free(lun->pending_ua[i], M_CTL); 4782 for (i = 0; i < 2 * CTL_MAX_PORTS; i++) 4783 free(lun->pr_keys[i], M_CTL); 4784 free(lun->write_buffer, M_CTL); 4785 if (lun->flags & CTL_LUN_MALLOCED) 4786 free(lun, M_CTL); 4787 4788 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4789 mtx_lock(&nlun->lun_lock); 4790 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4791 mtx_unlock(&nlun->lun_lock); 4792 } 4793 4794 return (0); 4795} 4796 4797static void 4798ctl_create_lun(struct ctl_be_lun *be_lun) 4799{ 4800 struct ctl_softc *softc; 4801 4802 softc = control_softc; 4803 4804 /* 4805 * ctl_alloc_lun() should handle all potential failure cases. 4806 */ 4807 ctl_alloc_lun(softc, NULL, be_lun); 4808} 4809 4810int 4811ctl_add_lun(struct ctl_be_lun *be_lun) 4812{ 4813 struct ctl_softc *softc = control_softc; 4814 4815 mtx_lock(&softc->ctl_lock); 4816 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4817 mtx_unlock(&softc->ctl_lock); 4818 wakeup(&softc->pending_lun_queue); 4819 4820 return (0); 4821} 4822 4823int 4824ctl_enable_lun(struct ctl_be_lun *be_lun) 4825{ 4826 struct ctl_softc *softc; 4827 struct ctl_port *port, *nport; 4828 struct ctl_lun *lun; 4829 int retval; 4830 4831 lun = (struct ctl_lun *)be_lun->ctl_lun; 4832 softc = lun->ctl_softc; 4833 4834 mtx_lock(&softc->ctl_lock); 4835 mtx_lock(&lun->lun_lock); 4836 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4837 /* 4838 * eh? Why did we get called if the LUN is already 4839 * enabled? 4840 */ 4841 mtx_unlock(&lun->lun_lock); 4842 mtx_unlock(&softc->ctl_lock); 4843 return (0); 4844 } 4845 lun->flags &= ~CTL_LUN_DISABLED; 4846 mtx_unlock(&lun->lun_lock); 4847 4848 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) { 4849 nport = STAILQ_NEXT(port, links); 4850 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4851 port->lun_map != NULL) 4852 continue; 4853 4854 /* 4855 * Drop the lock while we call the FETD's enable routine. 4856 * This can lead to a callback into CTL (at least in the 4857 * case of the internal initiator frontend. 4858 */ 4859 mtx_unlock(&softc->ctl_lock); 4860 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4861 mtx_lock(&softc->ctl_lock); 4862 if (retval != 0) { 4863 printf("%s: FETD %s port %d returned error " 4864 "%d for lun_enable on lun %jd\n", 4865 __func__, port->port_name, port->targ_port, 4866 retval, (intmax_t)lun->lun); 4867 } 4868 } 4869 4870 mtx_unlock(&softc->ctl_lock); 4871 4872 return (0); 4873} 4874 4875int 4876ctl_disable_lun(struct ctl_be_lun *be_lun) 4877{ 4878 struct ctl_softc *softc; 4879 struct ctl_port *port; 4880 struct ctl_lun *lun; 4881 int retval; 4882 4883 lun = (struct ctl_lun *)be_lun->ctl_lun; 4884 softc = lun->ctl_softc; 4885 4886 mtx_lock(&softc->ctl_lock); 4887 mtx_lock(&lun->lun_lock); 4888 if (lun->flags & CTL_LUN_DISABLED) { 4889 mtx_unlock(&lun->lun_lock); 4890 mtx_unlock(&softc->ctl_lock); 4891 return (0); 4892 } 4893 lun->flags |= CTL_LUN_DISABLED; 4894 mtx_unlock(&lun->lun_lock); 4895 4896 STAILQ_FOREACH(port, &softc->port_list, links) { 4897 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4898 port->lun_map != NULL) 4899 continue; 4900 mtx_unlock(&softc->ctl_lock); 4901 /* 4902 * Drop the lock before we call the frontend's disable 4903 * routine, to avoid lock order reversals. 4904 * 4905 * XXX KDM what happens if the frontend list changes while 4906 * we're traversing it? It's unlikely, but should be handled. 4907 */ 4908 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4909 mtx_lock(&softc->ctl_lock); 4910 if (retval != 0) { 4911 printf("%s: FETD %s port %d returned error " 4912 "%d for lun_disable on lun %jd\n", 4913 __func__, port->port_name, port->targ_port, 4914 retval, (intmax_t)lun->lun); 4915 } 4916 } 4917 4918 mtx_unlock(&softc->ctl_lock); 4919 4920 return (0); 4921} 4922 4923int 4924ctl_start_lun(struct ctl_be_lun *be_lun) 4925{ 4926 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4927 4928 mtx_lock(&lun->lun_lock); 4929 lun->flags &= ~CTL_LUN_STOPPED; 4930 mtx_unlock(&lun->lun_lock); 4931 return (0); 4932} 4933 4934int 4935ctl_stop_lun(struct ctl_be_lun *be_lun) 4936{ 4937 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4938 4939 mtx_lock(&lun->lun_lock); 4940 lun->flags |= CTL_LUN_STOPPED; 4941 mtx_unlock(&lun->lun_lock); 4942 return (0); 4943} 4944 4945int 4946ctl_lun_offline(struct ctl_be_lun *be_lun) 4947{ 4948 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4949 4950 mtx_lock(&lun->lun_lock); 4951 lun->flags |= CTL_LUN_OFFLINE; 4952 mtx_unlock(&lun->lun_lock); 4953 return (0); 4954} 4955 4956int 4957ctl_lun_online(struct ctl_be_lun *be_lun) 4958{ 4959 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4960 4961 mtx_lock(&lun->lun_lock); 4962 lun->flags &= ~CTL_LUN_OFFLINE; 4963 mtx_unlock(&lun->lun_lock); 4964 return (0); 4965} 4966 4967int 4968ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4969{ 4970 struct ctl_softc *softc; 4971 struct ctl_lun *lun; 4972 4973 lun = (struct ctl_lun *)be_lun->ctl_lun; 4974 softc = lun->ctl_softc; 4975 4976 mtx_lock(&lun->lun_lock); 4977 4978 /* 4979 * The LUN needs to be disabled before it can be marked invalid. 4980 */ 4981 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4982 mtx_unlock(&lun->lun_lock); 4983 return (-1); 4984 } 4985 /* 4986 * Mark the LUN invalid. 4987 */ 4988 lun->flags |= CTL_LUN_INVALID; 4989 4990 /* 4991 * If there is nothing in the OOA queue, go ahead and free the LUN. 4992 * If we have something in the OOA queue, we'll free it when the 4993 * last I/O completes. 4994 */ 4995 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4996 mtx_unlock(&lun->lun_lock); 4997 mtx_lock(&softc->ctl_lock); 4998 ctl_free_lun(lun); 4999 mtx_unlock(&softc->ctl_lock); 5000 } else 5001 mtx_unlock(&lun->lun_lock); 5002 5003 return (0); 5004} 5005 5006int 5007ctl_lun_inoperable(struct ctl_be_lun *be_lun) 5008{ 5009 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5010 5011 mtx_lock(&lun->lun_lock); 5012 lun->flags |= CTL_LUN_INOPERABLE; 5013 mtx_unlock(&lun->lun_lock); 5014 return (0); 5015} 5016 5017int 5018ctl_lun_operable(struct ctl_be_lun *be_lun) 5019{ 5020 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5021 5022 mtx_lock(&lun->lun_lock); 5023 lun->flags &= ~CTL_LUN_INOPERABLE; 5024 mtx_unlock(&lun->lun_lock); 5025 return (0); 5026} 5027 5028void 5029ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 5030{ 5031 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 5032 5033 mtx_lock(&lun->lun_lock); 5034 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED); 5035 mtx_unlock(&lun->lun_lock); 5036} 5037 5038/* 5039 * Backend "memory move is complete" callback for requests that never 5040 * make it down to say RAIDCore's configuration code. 5041 */ 5042int 5043ctl_config_move_done(union ctl_io *io) 5044{ 5045 int retval; 5046 5047 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5048 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5049 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5050 5051 if ((io->io_hdr.port_status != 0) && 5052 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5053 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5054 /* 5055 * For hardware error sense keys, the sense key 5056 * specific value is defined to be a retry count, 5057 * but we use it to pass back an internal FETD 5058 * error code. XXX KDM Hopefully the FETD is only 5059 * using 16 bits for an error code, since that's 5060 * all the space we have in the sks field. 5061 */ 5062 ctl_set_internal_failure(&io->scsiio, 5063 /*sks_valid*/ 1, 5064 /*retry_count*/ 5065 io->io_hdr.port_status); 5066 } 5067 5068 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5069 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5070 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5071 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5072 /* 5073 * XXX KDM just assuming a single pointer here, and not a 5074 * S/G list. If we start using S/G lists for config data, 5075 * we'll need to know how to clean them up here as well. 5076 */ 5077 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5078 free(io->scsiio.kern_data_ptr, M_CTL); 5079 ctl_done(io); 5080 retval = CTL_RETVAL_COMPLETE; 5081 } else { 5082 /* 5083 * XXX KDM now we need to continue data movement. Some 5084 * options: 5085 * - call ctl_scsiio() again? We don't do this for data 5086 * writes, because for those at least we know ahead of 5087 * time where the write will go and how long it is. For 5088 * config writes, though, that information is largely 5089 * contained within the write itself, thus we need to 5090 * parse out the data again. 5091 * 5092 * - Call some other function once the data is in? 5093 */ 5094 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5095 ctl_data_print(io); 5096 5097 /* 5098 * XXX KDM call ctl_scsiio() again for now, and check flag 5099 * bits to see whether we're allocated or not. 5100 */ 5101 retval = ctl_scsiio(&io->scsiio); 5102 } 5103 return (retval); 5104} 5105 5106/* 5107 * This gets called by a backend driver when it is done with a 5108 * data_submit method. 5109 */ 5110void 5111ctl_data_submit_done(union ctl_io *io) 5112{ 5113 /* 5114 * If the IO_CONT flag is set, we need to call the supplied 5115 * function to continue processing the I/O, instead of completing 5116 * the I/O just yet. 5117 * 5118 * If there is an error, though, we don't want to keep processing. 5119 * Instead, just send status back to the initiator. 5120 */ 5121 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5122 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5123 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5124 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5125 io->scsiio.io_cont(io); 5126 return; 5127 } 5128 ctl_done(io); 5129} 5130 5131/* 5132 * This gets called by a backend driver when it is done with a 5133 * configuration write. 5134 */ 5135void 5136ctl_config_write_done(union ctl_io *io) 5137{ 5138 uint8_t *buf; 5139 5140 /* 5141 * If the IO_CONT flag is set, we need to call the supplied 5142 * function to continue processing the I/O, instead of completing 5143 * the I/O just yet. 5144 * 5145 * If there is an error, though, we don't want to keep processing. 5146 * Instead, just send status back to the initiator. 5147 */ 5148 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5149 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5150 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5151 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5152 io->scsiio.io_cont(io); 5153 return; 5154 } 5155 /* 5156 * Since a configuration write can be done for commands that actually 5157 * have data allocated, like write buffer, and commands that have 5158 * no data, like start/stop unit, we need to check here. 5159 */ 5160 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5161 buf = io->scsiio.kern_data_ptr; 5162 else 5163 buf = NULL; 5164 ctl_done(io); 5165 if (buf) 5166 free(buf, M_CTL); 5167} 5168 5169void 5170ctl_config_read_done(union ctl_io *io) 5171{ 5172 uint8_t *buf; 5173 5174 /* 5175 * If there is some error -- we are done, skip data transfer. 5176 */ 5177 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5178 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5179 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5180 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5181 buf = io->scsiio.kern_data_ptr; 5182 else 5183 buf = NULL; 5184 ctl_done(io); 5185 if (buf) 5186 free(buf, M_CTL); 5187 return; 5188 } 5189 5190 /* 5191 * If the IO_CONT flag is set, we need to call the supplied 5192 * function to continue processing the I/O, instead of completing 5193 * the I/O just yet. 5194 */ 5195 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5196 io->scsiio.io_cont(io); 5197 return; 5198 } 5199 5200 ctl_datamove(io); 5201} 5202 5203/* 5204 * SCSI release command. 5205 */ 5206int 5207ctl_scsi_release(struct ctl_scsiio *ctsio) 5208{ 5209 int length, longid, thirdparty_id, resv_id; 5210 struct ctl_lun *lun; 5211 uint32_t residx; 5212 5213 length = 0; 5214 resv_id = 0; 5215 5216 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5217 5218 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5219 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5220 5221 switch (ctsio->cdb[0]) { 5222 case RELEASE_10: { 5223 struct scsi_release_10 *cdb; 5224 5225 cdb = (struct scsi_release_10 *)ctsio->cdb; 5226 5227 if (cdb->byte2 & SR10_LONGID) 5228 longid = 1; 5229 else 5230 thirdparty_id = cdb->thirdparty_id; 5231 5232 resv_id = cdb->resv_id; 5233 length = scsi_2btoul(cdb->length); 5234 break; 5235 } 5236 } 5237 5238 5239 /* 5240 * XXX KDM right now, we only support LUN reservation. We don't 5241 * support 3rd party reservations, or extent reservations, which 5242 * might actually need the parameter list. If we've gotten this 5243 * far, we've got a LUN reservation. Anything else got kicked out 5244 * above. So, according to SPC, ignore the length. 5245 */ 5246 length = 0; 5247 5248 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5249 && (length > 0)) { 5250 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5251 ctsio->kern_data_len = length; 5252 ctsio->kern_total_len = length; 5253 ctsio->kern_data_resid = 0; 5254 ctsio->kern_rel_offset = 0; 5255 ctsio->kern_sg_entries = 0; 5256 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5257 ctsio->be_move_done = ctl_config_move_done; 5258 ctl_datamove((union ctl_io *)ctsio); 5259 5260 return (CTL_RETVAL_COMPLETE); 5261 } 5262 5263 if (length > 0) 5264 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5265 5266 mtx_lock(&lun->lun_lock); 5267 5268 /* 5269 * According to SPC, it is not an error for an intiator to attempt 5270 * to release a reservation on a LUN that isn't reserved, or that 5271 * is reserved by another initiator. The reservation can only be 5272 * released, though, by the initiator who made it or by one of 5273 * several reset type events. 5274 */ 5275 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5276 lun->flags &= ~CTL_LUN_RESERVED; 5277 5278 mtx_unlock(&lun->lun_lock); 5279 5280 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5281 free(ctsio->kern_data_ptr, M_CTL); 5282 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5283 } 5284 5285 ctl_set_success(ctsio); 5286 ctl_done((union ctl_io *)ctsio); 5287 return (CTL_RETVAL_COMPLETE); 5288} 5289 5290int 5291ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5292{ 5293 int extent, thirdparty, longid; 5294 int resv_id, length; 5295 uint64_t thirdparty_id; 5296 struct ctl_lun *lun; 5297 uint32_t residx; 5298 5299 extent = 0; 5300 thirdparty = 0; 5301 longid = 0; 5302 resv_id = 0; 5303 length = 0; 5304 thirdparty_id = 0; 5305 5306 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5307 5308 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5309 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5310 5311 switch (ctsio->cdb[0]) { 5312 case RESERVE_10: { 5313 struct scsi_reserve_10 *cdb; 5314 5315 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5316 5317 if (cdb->byte2 & SR10_LONGID) 5318 longid = 1; 5319 else 5320 thirdparty_id = cdb->thirdparty_id; 5321 5322 resv_id = cdb->resv_id; 5323 length = scsi_2btoul(cdb->length); 5324 break; 5325 } 5326 } 5327 5328 /* 5329 * XXX KDM right now, we only support LUN reservation. We don't 5330 * support 3rd party reservations, or extent reservations, which 5331 * might actually need the parameter list. If we've gotten this 5332 * far, we've got a LUN reservation. Anything else got kicked out 5333 * above. So, according to SPC, ignore the length. 5334 */ 5335 length = 0; 5336 5337 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5338 && (length > 0)) { 5339 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5340 ctsio->kern_data_len = length; 5341 ctsio->kern_total_len = length; 5342 ctsio->kern_data_resid = 0; 5343 ctsio->kern_rel_offset = 0; 5344 ctsio->kern_sg_entries = 0; 5345 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5346 ctsio->be_move_done = ctl_config_move_done; 5347 ctl_datamove((union ctl_io *)ctsio); 5348 5349 return (CTL_RETVAL_COMPLETE); 5350 } 5351 5352 if (length > 0) 5353 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5354 5355 mtx_lock(&lun->lun_lock); 5356 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5357 ctl_set_reservation_conflict(ctsio); 5358 goto bailout; 5359 } 5360 5361 lun->flags |= CTL_LUN_RESERVED; 5362 lun->res_idx = residx; 5363 5364 ctl_set_success(ctsio); 5365 5366bailout: 5367 mtx_unlock(&lun->lun_lock); 5368 5369 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5370 free(ctsio->kern_data_ptr, M_CTL); 5371 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5372 } 5373 5374 ctl_done((union ctl_io *)ctsio); 5375 return (CTL_RETVAL_COMPLETE); 5376} 5377 5378int 5379ctl_start_stop(struct ctl_scsiio *ctsio) 5380{ 5381 struct scsi_start_stop_unit *cdb; 5382 struct ctl_lun *lun; 5383 int retval; 5384 5385 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5386 5387 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5388 retval = 0; 5389 5390 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5391 5392 /* 5393 * XXX KDM 5394 * We don't support the immediate bit on a stop unit. In order to 5395 * do that, we would need to code up a way to know that a stop is 5396 * pending, and hold off any new commands until it completes, one 5397 * way or another. Then we could accept or reject those commands 5398 * depending on its status. We would almost need to do the reverse 5399 * of what we do below for an immediate start -- return the copy of 5400 * the ctl_io to the FETD with status to send to the host (and to 5401 * free the copy!) and then free the original I/O once the stop 5402 * actually completes. That way, the OOA queue mechanism can work 5403 * to block commands that shouldn't proceed. Another alternative 5404 * would be to put the copy in the queue in place of the original, 5405 * and return the original back to the caller. That could be 5406 * slightly safer.. 5407 */ 5408 if ((cdb->byte2 & SSS_IMMED) 5409 && ((cdb->how & SSS_START) == 0)) { 5410 ctl_set_invalid_field(ctsio, 5411 /*sks_valid*/ 1, 5412 /*command*/ 1, 5413 /*field*/ 1, 5414 /*bit_valid*/ 1, 5415 /*bit*/ 0); 5416 ctl_done((union ctl_io *)ctsio); 5417 return (CTL_RETVAL_COMPLETE); 5418 } 5419 5420 if ((lun->flags & CTL_LUN_PR_RESERVED) 5421 && ((cdb->how & SSS_START)==0)) { 5422 uint32_t residx; 5423 5424 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5425 if (ctl_get_prkey(lun, residx) == 0 5426 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5427 5428 ctl_set_reservation_conflict(ctsio); 5429 ctl_done((union ctl_io *)ctsio); 5430 return (CTL_RETVAL_COMPLETE); 5431 } 5432 } 5433 5434 /* 5435 * If there is no backend on this device, we can't start or stop 5436 * it. In theory we shouldn't get any start/stop commands in the 5437 * first place at this level if the LUN doesn't have a backend. 5438 * That should get stopped by the command decode code. 5439 */ 5440 if (lun->backend == NULL) { 5441 ctl_set_invalid_opcode(ctsio); 5442 ctl_done((union ctl_io *)ctsio); 5443 return (CTL_RETVAL_COMPLETE); 5444 } 5445 5446 /* 5447 * XXX KDM Copan-specific offline behavior. 5448 * Figure out a reasonable way to port this? 5449 */ 5450#ifdef NEEDTOPORT 5451 mtx_lock(&lun->lun_lock); 5452 5453 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5454 && (lun->flags & CTL_LUN_OFFLINE)) { 5455 /* 5456 * If the LUN is offline, and the on/offline bit isn't set, 5457 * reject the start or stop. Otherwise, let it through. 5458 */ 5459 mtx_unlock(&lun->lun_lock); 5460 ctl_set_lun_not_ready(ctsio); 5461 ctl_done((union ctl_io *)ctsio); 5462 } else { 5463 mtx_unlock(&lun->lun_lock); 5464#endif /* NEEDTOPORT */ 5465 /* 5466 * This could be a start or a stop when we're online, 5467 * or a stop/offline or start/online. A start or stop when 5468 * we're offline is covered in the case above. 5469 */ 5470 /* 5471 * In the non-immediate case, we send the request to 5472 * the backend and return status to the user when 5473 * it is done. 5474 * 5475 * In the immediate case, we allocate a new ctl_io 5476 * to hold a copy of the request, and send that to 5477 * the backend. We then set good status on the 5478 * user's request and return it immediately. 5479 */ 5480 if (cdb->byte2 & SSS_IMMED) { 5481 union ctl_io *new_io; 5482 5483 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5484 ctl_copy_io((union ctl_io *)ctsio, new_io); 5485 retval = lun->backend->config_write(new_io); 5486 ctl_set_success(ctsio); 5487 ctl_done((union ctl_io *)ctsio); 5488 } else { 5489 retval = lun->backend->config_write( 5490 (union ctl_io *)ctsio); 5491 } 5492#ifdef NEEDTOPORT 5493 } 5494#endif 5495 return (retval); 5496} 5497 5498/* 5499 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5500 * we don't really do anything with the LBA and length fields if the user 5501 * passes them in. Instead we'll just flush out the cache for the entire 5502 * LUN. 5503 */ 5504int 5505ctl_sync_cache(struct ctl_scsiio *ctsio) 5506{ 5507 struct ctl_lun *lun; 5508 struct ctl_softc *softc; 5509 struct ctl_lba_len_flags *lbalen; 5510 uint64_t starting_lba; 5511 uint32_t block_count; 5512 int retval; 5513 uint8_t byte2; 5514 5515 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5516 5517 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5518 softc = lun->ctl_softc; 5519 retval = 0; 5520 5521 switch (ctsio->cdb[0]) { 5522 case SYNCHRONIZE_CACHE: { 5523 struct scsi_sync_cache *cdb; 5524 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5525 5526 starting_lba = scsi_4btoul(cdb->begin_lba); 5527 block_count = scsi_2btoul(cdb->lb_count); 5528 byte2 = cdb->byte2; 5529 break; 5530 } 5531 case SYNCHRONIZE_CACHE_16: { 5532 struct scsi_sync_cache_16 *cdb; 5533 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5534 5535 starting_lba = scsi_8btou64(cdb->begin_lba); 5536 block_count = scsi_4btoul(cdb->lb_count); 5537 byte2 = cdb->byte2; 5538 break; 5539 } 5540 default: 5541 ctl_set_invalid_opcode(ctsio); 5542 ctl_done((union ctl_io *)ctsio); 5543 goto bailout; 5544 break; /* NOTREACHED */ 5545 } 5546 5547 /* 5548 * We check the LBA and length, but don't do anything with them. 5549 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5550 * get flushed. This check will just help satisfy anyone who wants 5551 * to see an error for an out of range LBA. 5552 */ 5553 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5554 ctl_set_lba_out_of_range(ctsio); 5555 ctl_done((union ctl_io *)ctsio); 5556 goto bailout; 5557 } 5558 5559 /* 5560 * If this LUN has no backend, we can't flush the cache anyway. 5561 */ 5562 if (lun->backend == NULL) { 5563 ctl_set_invalid_opcode(ctsio); 5564 ctl_done((union ctl_io *)ctsio); 5565 goto bailout; 5566 } 5567 5568 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5569 lbalen->lba = starting_lba; 5570 lbalen->len = block_count; 5571 lbalen->flags = byte2; 5572 5573 /* 5574 * Check to see whether we're configured to send the SYNCHRONIZE 5575 * CACHE command directly to the back end. 5576 */ 5577 mtx_lock(&lun->lun_lock); 5578 if ((softc->flags & CTL_FLAG_REAL_SYNC) 5579 && (++(lun->sync_count) >= lun->sync_interval)) { 5580 lun->sync_count = 0; 5581 mtx_unlock(&lun->lun_lock); 5582 retval = lun->backend->config_write((union ctl_io *)ctsio); 5583 } else { 5584 mtx_unlock(&lun->lun_lock); 5585 ctl_set_success(ctsio); 5586 ctl_done((union ctl_io *)ctsio); 5587 } 5588 5589bailout: 5590 5591 return (retval); 5592} 5593 5594int 5595ctl_format(struct ctl_scsiio *ctsio) 5596{ 5597 struct scsi_format *cdb; 5598 struct ctl_lun *lun; 5599 int length, defect_list_len; 5600 5601 CTL_DEBUG_PRINT(("ctl_format\n")); 5602 5603 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5604 5605 cdb = (struct scsi_format *)ctsio->cdb; 5606 5607 length = 0; 5608 if (cdb->byte2 & SF_FMTDATA) { 5609 if (cdb->byte2 & SF_LONGLIST) 5610 length = sizeof(struct scsi_format_header_long); 5611 else 5612 length = sizeof(struct scsi_format_header_short); 5613 } 5614 5615 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5616 && (length > 0)) { 5617 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5618 ctsio->kern_data_len = length; 5619 ctsio->kern_total_len = length; 5620 ctsio->kern_data_resid = 0; 5621 ctsio->kern_rel_offset = 0; 5622 ctsio->kern_sg_entries = 0; 5623 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5624 ctsio->be_move_done = ctl_config_move_done; 5625 ctl_datamove((union ctl_io *)ctsio); 5626 5627 return (CTL_RETVAL_COMPLETE); 5628 } 5629 5630 defect_list_len = 0; 5631 5632 if (cdb->byte2 & SF_FMTDATA) { 5633 if (cdb->byte2 & SF_LONGLIST) { 5634 struct scsi_format_header_long *header; 5635 5636 header = (struct scsi_format_header_long *) 5637 ctsio->kern_data_ptr; 5638 5639 defect_list_len = scsi_4btoul(header->defect_list_len); 5640 if (defect_list_len != 0) { 5641 ctl_set_invalid_field(ctsio, 5642 /*sks_valid*/ 1, 5643 /*command*/ 0, 5644 /*field*/ 2, 5645 /*bit_valid*/ 0, 5646 /*bit*/ 0); 5647 goto bailout; 5648 } 5649 } else { 5650 struct scsi_format_header_short *header; 5651 5652 header = (struct scsi_format_header_short *) 5653 ctsio->kern_data_ptr; 5654 5655 defect_list_len = scsi_2btoul(header->defect_list_len); 5656 if (defect_list_len != 0) { 5657 ctl_set_invalid_field(ctsio, 5658 /*sks_valid*/ 1, 5659 /*command*/ 0, 5660 /*field*/ 2, 5661 /*bit_valid*/ 0, 5662 /*bit*/ 0); 5663 goto bailout; 5664 } 5665 } 5666 } 5667 5668 /* 5669 * The format command will clear out the "Medium format corrupted" 5670 * status if set by the configuration code. That status is really 5671 * just a way to notify the host that we have lost the media, and 5672 * get them to issue a command that will basically make them think 5673 * they're blowing away the media. 5674 */ 5675 mtx_lock(&lun->lun_lock); 5676 lun->flags &= ~CTL_LUN_INOPERABLE; 5677 mtx_unlock(&lun->lun_lock); 5678 5679 ctl_set_success(ctsio); 5680bailout: 5681 5682 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5683 free(ctsio->kern_data_ptr, M_CTL); 5684 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5685 } 5686 5687 ctl_done((union ctl_io *)ctsio); 5688 return (CTL_RETVAL_COMPLETE); 5689} 5690 5691int 5692ctl_read_buffer(struct ctl_scsiio *ctsio) 5693{ 5694 struct scsi_read_buffer *cdb; 5695 struct ctl_lun *lun; 5696 int buffer_offset, len; 5697 static uint8_t descr[4]; 5698 static uint8_t echo_descr[4] = { 0 }; 5699 5700 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5701 5702 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5703 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5704 5705 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5706 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5707 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5708 ctl_set_invalid_field(ctsio, 5709 /*sks_valid*/ 1, 5710 /*command*/ 1, 5711 /*field*/ 1, 5712 /*bit_valid*/ 1, 5713 /*bit*/ 4); 5714 ctl_done((union ctl_io *)ctsio); 5715 return (CTL_RETVAL_COMPLETE); 5716 } 5717 5718 len = scsi_3btoul(cdb->length); 5719 buffer_offset = scsi_3btoul(cdb->offset); 5720 5721 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5722 ctl_set_invalid_field(ctsio, 5723 /*sks_valid*/ 1, 5724 /*command*/ 1, 5725 /*field*/ 6, 5726 /*bit_valid*/ 0, 5727 /*bit*/ 0); 5728 ctl_done((union ctl_io *)ctsio); 5729 return (CTL_RETVAL_COMPLETE); 5730 } 5731 5732 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5733 descr[0] = 0; 5734 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5735 ctsio->kern_data_ptr = descr; 5736 len = min(len, sizeof(descr)); 5737 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5738 ctsio->kern_data_ptr = echo_descr; 5739 len = min(len, sizeof(echo_descr)); 5740 } else { 5741 if (lun->write_buffer == NULL) { 5742 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5743 M_CTL, M_WAITOK); 5744 } 5745 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5746 } 5747 ctsio->kern_data_len = len; 5748 ctsio->kern_total_len = len; 5749 ctsio->kern_data_resid = 0; 5750 ctsio->kern_rel_offset = 0; 5751 ctsio->kern_sg_entries = 0; 5752 ctl_set_success(ctsio); 5753 ctsio->be_move_done = ctl_config_move_done; 5754 ctl_datamove((union ctl_io *)ctsio); 5755 return (CTL_RETVAL_COMPLETE); 5756} 5757 5758int 5759ctl_write_buffer(struct ctl_scsiio *ctsio) 5760{ 5761 struct scsi_write_buffer *cdb; 5762 struct ctl_lun *lun; 5763 int buffer_offset, len; 5764 5765 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5766 5767 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5768 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5769 5770 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5771 ctl_set_invalid_field(ctsio, 5772 /*sks_valid*/ 1, 5773 /*command*/ 1, 5774 /*field*/ 1, 5775 /*bit_valid*/ 1, 5776 /*bit*/ 4); 5777 ctl_done((union ctl_io *)ctsio); 5778 return (CTL_RETVAL_COMPLETE); 5779 } 5780 5781 len = scsi_3btoul(cdb->length); 5782 buffer_offset = scsi_3btoul(cdb->offset); 5783 5784 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5785 ctl_set_invalid_field(ctsio, 5786 /*sks_valid*/ 1, 5787 /*command*/ 1, 5788 /*field*/ 6, 5789 /*bit_valid*/ 0, 5790 /*bit*/ 0); 5791 ctl_done((union ctl_io *)ctsio); 5792 return (CTL_RETVAL_COMPLETE); 5793 } 5794 5795 /* 5796 * If we've got a kernel request that hasn't been malloced yet, 5797 * malloc it and tell the caller the data buffer is here. 5798 */ 5799 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5800 if (lun->write_buffer == NULL) { 5801 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5802 M_CTL, M_WAITOK); 5803 } 5804 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5805 ctsio->kern_data_len = len; 5806 ctsio->kern_total_len = len; 5807 ctsio->kern_data_resid = 0; 5808 ctsio->kern_rel_offset = 0; 5809 ctsio->kern_sg_entries = 0; 5810 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5811 ctsio->be_move_done = ctl_config_move_done; 5812 ctl_datamove((union ctl_io *)ctsio); 5813 5814 return (CTL_RETVAL_COMPLETE); 5815 } 5816 5817 ctl_set_success(ctsio); 5818 ctl_done((union ctl_io *)ctsio); 5819 return (CTL_RETVAL_COMPLETE); 5820} 5821 5822int 5823ctl_write_same(struct ctl_scsiio *ctsio) 5824{ 5825 struct ctl_lun *lun; 5826 struct ctl_lba_len_flags *lbalen; 5827 uint64_t lba; 5828 uint32_t num_blocks; 5829 int len, retval; 5830 uint8_t byte2; 5831 5832 retval = CTL_RETVAL_COMPLETE; 5833 5834 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5835 5836 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5837 5838 switch (ctsio->cdb[0]) { 5839 case WRITE_SAME_10: { 5840 struct scsi_write_same_10 *cdb; 5841 5842 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5843 5844 lba = scsi_4btoul(cdb->addr); 5845 num_blocks = scsi_2btoul(cdb->length); 5846 byte2 = cdb->byte2; 5847 break; 5848 } 5849 case WRITE_SAME_16: { 5850 struct scsi_write_same_16 *cdb; 5851 5852 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5853 5854 lba = scsi_8btou64(cdb->addr); 5855 num_blocks = scsi_4btoul(cdb->length); 5856 byte2 = cdb->byte2; 5857 break; 5858 } 5859 default: 5860 /* 5861 * We got a command we don't support. This shouldn't 5862 * happen, commands should be filtered out above us. 5863 */ 5864 ctl_set_invalid_opcode(ctsio); 5865 ctl_done((union ctl_io *)ctsio); 5866 5867 return (CTL_RETVAL_COMPLETE); 5868 break; /* NOTREACHED */ 5869 } 5870 5871 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5872 if ((byte2 & SWS_UNMAP) == 0 && 5873 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5874 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5875 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5876 ctl_done((union ctl_io *)ctsio); 5877 return (CTL_RETVAL_COMPLETE); 5878 } 5879 5880 /* 5881 * The first check is to make sure we're in bounds, the second 5882 * check is to catch wrap-around problems. If the lba + num blocks 5883 * is less than the lba, then we've wrapped around and the block 5884 * range is invalid anyway. 5885 */ 5886 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5887 || ((lba + num_blocks) < lba)) { 5888 ctl_set_lba_out_of_range(ctsio); 5889 ctl_done((union ctl_io *)ctsio); 5890 return (CTL_RETVAL_COMPLETE); 5891 } 5892 5893 /* Zero number of blocks means "to the last logical block" */ 5894 if (num_blocks == 0) { 5895 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5896 ctl_set_invalid_field(ctsio, 5897 /*sks_valid*/ 0, 5898 /*command*/ 1, 5899 /*field*/ 0, 5900 /*bit_valid*/ 0, 5901 /*bit*/ 0); 5902 ctl_done((union ctl_io *)ctsio); 5903 return (CTL_RETVAL_COMPLETE); 5904 } 5905 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5906 } 5907 5908 len = lun->be_lun->blocksize; 5909 5910 /* 5911 * If we've got a kernel request that hasn't been malloced yet, 5912 * malloc it and tell the caller the data buffer is here. 5913 */ 5914 if ((byte2 & SWS_NDOB) == 0 && 5915 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5916 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5917 ctsio->kern_data_len = len; 5918 ctsio->kern_total_len = len; 5919 ctsio->kern_data_resid = 0; 5920 ctsio->kern_rel_offset = 0; 5921 ctsio->kern_sg_entries = 0; 5922 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5923 ctsio->be_move_done = ctl_config_move_done; 5924 ctl_datamove((union ctl_io *)ctsio); 5925 5926 return (CTL_RETVAL_COMPLETE); 5927 } 5928 5929 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5930 lbalen->lba = lba; 5931 lbalen->len = num_blocks; 5932 lbalen->flags = byte2; 5933 retval = lun->backend->config_write((union ctl_io *)ctsio); 5934 5935 return (retval); 5936} 5937 5938int 5939ctl_unmap(struct ctl_scsiio *ctsio) 5940{ 5941 struct ctl_lun *lun; 5942 struct scsi_unmap *cdb; 5943 struct ctl_ptr_len_flags *ptrlen; 5944 struct scsi_unmap_header *hdr; 5945 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5946 uint64_t lba; 5947 uint32_t num_blocks; 5948 int len, retval; 5949 uint8_t byte2; 5950 5951 retval = CTL_RETVAL_COMPLETE; 5952 5953 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5954 5955 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5956 cdb = (struct scsi_unmap *)ctsio->cdb; 5957 5958 len = scsi_2btoul(cdb->length); 5959 byte2 = cdb->byte2; 5960 5961 /* 5962 * If we've got a kernel request that hasn't been malloced yet, 5963 * malloc it and tell the caller the data buffer is here. 5964 */ 5965 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5966 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5967 ctsio->kern_data_len = len; 5968 ctsio->kern_total_len = len; 5969 ctsio->kern_data_resid = 0; 5970 ctsio->kern_rel_offset = 0; 5971 ctsio->kern_sg_entries = 0; 5972 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5973 ctsio->be_move_done = ctl_config_move_done; 5974 ctl_datamove((union ctl_io *)ctsio); 5975 5976 return (CTL_RETVAL_COMPLETE); 5977 } 5978 5979 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5980 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5981 if (len < sizeof (*hdr) || 5982 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5983 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5984 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5985 ctl_set_invalid_field(ctsio, 5986 /*sks_valid*/ 0, 5987 /*command*/ 0, 5988 /*field*/ 0, 5989 /*bit_valid*/ 0, 5990 /*bit*/ 0); 5991 goto done; 5992 } 5993 len = scsi_2btoul(hdr->desc_length); 5994 buf = (struct scsi_unmap_desc *)(hdr + 1); 5995 end = buf + len / sizeof(*buf); 5996 5997 endnz = buf; 5998 for (range = buf; range < end; range++) { 5999 lba = scsi_8btou64(range->lba); 6000 num_blocks = scsi_4btoul(range->length); 6001 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 6002 || ((lba + num_blocks) < lba)) { 6003 ctl_set_lba_out_of_range(ctsio); 6004 ctl_done((union ctl_io *)ctsio); 6005 return (CTL_RETVAL_COMPLETE); 6006 } 6007 if (num_blocks != 0) 6008 endnz = range + 1; 6009 } 6010 6011 /* 6012 * Block backend can not handle zero last range. 6013 * Filter it out and return if there is nothing left. 6014 */ 6015 len = (uint8_t *)endnz - (uint8_t *)buf; 6016 if (len == 0) { 6017 ctl_set_success(ctsio); 6018 goto done; 6019 } 6020 6021 mtx_lock(&lun->lun_lock); 6022 ptrlen = (struct ctl_ptr_len_flags *) 6023 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6024 ptrlen->ptr = (void *)buf; 6025 ptrlen->len = len; 6026 ptrlen->flags = byte2; 6027 ctl_check_blocked(lun); 6028 mtx_unlock(&lun->lun_lock); 6029 6030 retval = lun->backend->config_write((union ctl_io *)ctsio); 6031 return (retval); 6032 6033done: 6034 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 6035 free(ctsio->kern_data_ptr, M_CTL); 6036 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 6037 } 6038 ctl_done((union ctl_io *)ctsio); 6039 return (CTL_RETVAL_COMPLETE); 6040} 6041 6042/* 6043 * Note that this function currently doesn't actually do anything inside 6044 * CTL to enforce things if the DQue bit is turned on. 6045 * 6046 * Also note that this function can't be used in the default case, because 6047 * the DQue bit isn't set in the changeable mask for the control mode page 6048 * anyway. This is just here as an example for how to implement a page 6049 * handler, and a placeholder in case we want to allow the user to turn 6050 * tagged queueing on and off. 6051 * 6052 * The D_SENSE bit handling is functional, however, and will turn 6053 * descriptor sense on and off for a given LUN. 6054 */ 6055int 6056ctl_control_page_handler(struct ctl_scsiio *ctsio, 6057 struct ctl_page_index *page_index, uint8_t *page_ptr) 6058{ 6059 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 6060 struct ctl_lun *lun; 6061 int set_ua; 6062 uint32_t initidx; 6063 6064 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6065 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6066 set_ua = 0; 6067 6068 user_cp = (struct scsi_control_page *)page_ptr; 6069 current_cp = (struct scsi_control_page *) 6070 (page_index->page_data + (page_index->page_len * 6071 CTL_PAGE_CURRENT)); 6072 saved_cp = (struct scsi_control_page *) 6073 (page_index->page_data + (page_index->page_len * 6074 CTL_PAGE_SAVED)); 6075 6076 mtx_lock(&lun->lun_lock); 6077 if (((current_cp->rlec & SCP_DSENSE) == 0) 6078 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 6079 /* 6080 * Descriptor sense is currently turned off and the user 6081 * wants to turn it on. 6082 */ 6083 current_cp->rlec |= SCP_DSENSE; 6084 saved_cp->rlec |= SCP_DSENSE; 6085 lun->flags |= CTL_LUN_SENSE_DESC; 6086 set_ua = 1; 6087 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6088 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6089 /* 6090 * Descriptor sense is currently turned on, and the user 6091 * wants to turn it off. 6092 */ 6093 current_cp->rlec &= ~SCP_DSENSE; 6094 saved_cp->rlec &= ~SCP_DSENSE; 6095 lun->flags &= ~CTL_LUN_SENSE_DESC; 6096 set_ua = 1; 6097 } 6098 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6099 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6100 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6101 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6102 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6103 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6104 set_ua = 1; 6105 } 6106 if ((current_cp->eca_and_aen & SCP_SWP) != 6107 (user_cp->eca_and_aen & SCP_SWP)) { 6108 current_cp->eca_and_aen &= ~SCP_SWP; 6109 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6110 saved_cp->eca_and_aen &= ~SCP_SWP; 6111 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6112 set_ua = 1; 6113 } 6114 if (set_ua != 0) 6115 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6116 mtx_unlock(&lun->lun_lock); 6117 6118 return (0); 6119} 6120 6121int 6122ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6123 struct ctl_page_index *page_index, uint8_t *page_ptr) 6124{ 6125 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6126 struct ctl_lun *lun; 6127 int set_ua; 6128 uint32_t initidx; 6129 6130 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6131 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6132 set_ua = 0; 6133 6134 user_cp = (struct scsi_caching_page *)page_ptr; 6135 current_cp = (struct scsi_caching_page *) 6136 (page_index->page_data + (page_index->page_len * 6137 CTL_PAGE_CURRENT)); 6138 saved_cp = (struct scsi_caching_page *) 6139 (page_index->page_data + (page_index->page_len * 6140 CTL_PAGE_SAVED)); 6141 6142 mtx_lock(&lun->lun_lock); 6143 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6144 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6145 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6146 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6147 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6148 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6149 set_ua = 1; 6150 } 6151 if (set_ua != 0) 6152 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 6153 mtx_unlock(&lun->lun_lock); 6154 6155 return (0); 6156} 6157 6158int 6159ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6160 struct ctl_page_index *page_index, 6161 uint8_t *page_ptr) 6162{ 6163 uint8_t *c; 6164 int i; 6165 6166 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6167 ctl_time_io_secs = 6168 (c[0] << 8) | 6169 (c[1] << 0) | 6170 0; 6171 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6172 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6173 printf("page data:"); 6174 for (i=0; i<8; i++) 6175 printf(" %.2x",page_ptr[i]); 6176 printf("\n"); 6177 return (0); 6178} 6179 6180int 6181ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6182 struct ctl_page_index *page_index, 6183 int pc) 6184{ 6185 struct copan_debugconf_subpage *page; 6186 6187 page = (struct copan_debugconf_subpage *)page_index->page_data + 6188 (page_index->page_len * pc); 6189 6190 switch (pc) { 6191 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6192 case SMS_PAGE_CTRL_DEFAULT >> 6: 6193 case SMS_PAGE_CTRL_SAVED >> 6: 6194 /* 6195 * We don't update the changable or default bits for this page. 6196 */ 6197 break; 6198 case SMS_PAGE_CTRL_CURRENT >> 6: 6199 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6200 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6201 break; 6202 default: 6203#ifdef NEEDTOPORT 6204 EPRINT(0, "Invalid PC %d!!", pc); 6205#endif /* NEEDTOPORT */ 6206 break; 6207 } 6208 return (0); 6209} 6210 6211 6212static int 6213ctl_do_mode_select(union ctl_io *io) 6214{ 6215 struct scsi_mode_page_header *page_header; 6216 struct ctl_page_index *page_index; 6217 struct ctl_scsiio *ctsio; 6218 int control_dev, page_len; 6219 int page_len_offset, page_len_size; 6220 union ctl_modepage_info *modepage_info; 6221 struct ctl_lun *lun; 6222 int *len_left, *len_used; 6223 int retval, i; 6224 6225 ctsio = &io->scsiio; 6226 page_index = NULL; 6227 page_len = 0; 6228 retval = CTL_RETVAL_COMPLETE; 6229 6230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6231 6232 if (lun->be_lun->lun_type != T_DIRECT) 6233 control_dev = 1; 6234 else 6235 control_dev = 0; 6236 6237 modepage_info = (union ctl_modepage_info *) 6238 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6239 len_left = &modepage_info->header.len_left; 6240 len_used = &modepage_info->header.len_used; 6241 6242do_next_page: 6243 6244 page_header = (struct scsi_mode_page_header *) 6245 (ctsio->kern_data_ptr + *len_used); 6246 6247 if (*len_left == 0) { 6248 free(ctsio->kern_data_ptr, M_CTL); 6249 ctl_set_success(ctsio); 6250 ctl_done((union ctl_io *)ctsio); 6251 return (CTL_RETVAL_COMPLETE); 6252 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6253 6254 free(ctsio->kern_data_ptr, M_CTL); 6255 ctl_set_param_len_error(ctsio); 6256 ctl_done((union ctl_io *)ctsio); 6257 return (CTL_RETVAL_COMPLETE); 6258 6259 } else if ((page_header->page_code & SMPH_SPF) 6260 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6261 6262 free(ctsio->kern_data_ptr, M_CTL); 6263 ctl_set_param_len_error(ctsio); 6264 ctl_done((union ctl_io *)ctsio); 6265 return (CTL_RETVAL_COMPLETE); 6266 } 6267 6268 6269 /* 6270 * XXX KDM should we do something with the block descriptor? 6271 */ 6272 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6273 6274 if ((control_dev != 0) 6275 && (lun->mode_pages.index[i].page_flags & 6276 CTL_PAGE_FLAG_DISK_ONLY)) 6277 continue; 6278 6279 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6280 (page_header->page_code & SMPH_PC_MASK)) 6281 continue; 6282 6283 /* 6284 * If neither page has a subpage code, then we've got a 6285 * match. 6286 */ 6287 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6288 && ((page_header->page_code & SMPH_SPF) == 0)) { 6289 page_index = &lun->mode_pages.index[i]; 6290 page_len = page_header->page_length; 6291 break; 6292 } 6293 6294 /* 6295 * If both pages have subpages, then the subpage numbers 6296 * have to match. 6297 */ 6298 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6299 && (page_header->page_code & SMPH_SPF)) { 6300 struct scsi_mode_page_header_sp *sph; 6301 6302 sph = (struct scsi_mode_page_header_sp *)page_header; 6303 6304 if (lun->mode_pages.index[i].subpage == 6305 sph->subpage) { 6306 page_index = &lun->mode_pages.index[i]; 6307 page_len = scsi_2btoul(sph->page_length); 6308 break; 6309 } 6310 } 6311 } 6312 6313 /* 6314 * If we couldn't find the page, or if we don't have a mode select 6315 * handler for it, send back an error to the user. 6316 */ 6317 if ((page_index == NULL) 6318 || (page_index->select_handler == NULL)) { 6319 ctl_set_invalid_field(ctsio, 6320 /*sks_valid*/ 1, 6321 /*command*/ 0, 6322 /*field*/ *len_used, 6323 /*bit_valid*/ 0, 6324 /*bit*/ 0); 6325 free(ctsio->kern_data_ptr, M_CTL); 6326 ctl_done((union ctl_io *)ctsio); 6327 return (CTL_RETVAL_COMPLETE); 6328 } 6329 6330 if (page_index->page_code & SMPH_SPF) { 6331 page_len_offset = 2; 6332 page_len_size = 2; 6333 } else { 6334 page_len_size = 1; 6335 page_len_offset = 1; 6336 } 6337 6338 /* 6339 * If the length the initiator gives us isn't the one we specify in 6340 * the mode page header, or if they didn't specify enough data in 6341 * the CDB to avoid truncating this page, kick out the request. 6342 */ 6343 if ((page_len != (page_index->page_len - page_len_offset - 6344 page_len_size)) 6345 || (*len_left < page_index->page_len)) { 6346 6347 6348 ctl_set_invalid_field(ctsio, 6349 /*sks_valid*/ 1, 6350 /*command*/ 0, 6351 /*field*/ *len_used + page_len_offset, 6352 /*bit_valid*/ 0, 6353 /*bit*/ 0); 6354 free(ctsio->kern_data_ptr, M_CTL); 6355 ctl_done((union ctl_io *)ctsio); 6356 return (CTL_RETVAL_COMPLETE); 6357 } 6358 6359 /* 6360 * Run through the mode page, checking to make sure that the bits 6361 * the user changed are actually legal for him to change. 6362 */ 6363 for (i = 0; i < page_index->page_len; i++) { 6364 uint8_t *user_byte, *change_mask, *current_byte; 6365 int bad_bit; 6366 int j; 6367 6368 user_byte = (uint8_t *)page_header + i; 6369 change_mask = page_index->page_data + 6370 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6371 current_byte = page_index->page_data + 6372 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6373 6374 /* 6375 * Check to see whether the user set any bits in this byte 6376 * that he is not allowed to set. 6377 */ 6378 if ((*user_byte & ~(*change_mask)) == 6379 (*current_byte & ~(*change_mask))) 6380 continue; 6381 6382 /* 6383 * Go through bit by bit to determine which one is illegal. 6384 */ 6385 bad_bit = 0; 6386 for (j = 7; j >= 0; j--) { 6387 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6388 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6389 bad_bit = i; 6390 break; 6391 } 6392 } 6393 ctl_set_invalid_field(ctsio, 6394 /*sks_valid*/ 1, 6395 /*command*/ 0, 6396 /*field*/ *len_used + i, 6397 /*bit_valid*/ 1, 6398 /*bit*/ bad_bit); 6399 free(ctsio->kern_data_ptr, M_CTL); 6400 ctl_done((union ctl_io *)ctsio); 6401 return (CTL_RETVAL_COMPLETE); 6402 } 6403 6404 /* 6405 * Decrement these before we call the page handler, since we may 6406 * end up getting called back one way or another before the handler 6407 * returns to this context. 6408 */ 6409 *len_left -= page_index->page_len; 6410 *len_used += page_index->page_len; 6411 6412 retval = page_index->select_handler(ctsio, page_index, 6413 (uint8_t *)page_header); 6414 6415 /* 6416 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6417 * wait until this queued command completes to finish processing 6418 * the mode page. If it returns anything other than 6419 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6420 * already set the sense information, freed the data pointer, and 6421 * completed the io for us. 6422 */ 6423 if (retval != CTL_RETVAL_COMPLETE) 6424 goto bailout_no_done; 6425 6426 /* 6427 * If the initiator sent us more than one page, parse the next one. 6428 */ 6429 if (*len_left > 0) 6430 goto do_next_page; 6431 6432 ctl_set_success(ctsio); 6433 free(ctsio->kern_data_ptr, M_CTL); 6434 ctl_done((union ctl_io *)ctsio); 6435 6436bailout_no_done: 6437 6438 return (CTL_RETVAL_COMPLETE); 6439 6440} 6441 6442int 6443ctl_mode_select(struct ctl_scsiio *ctsio) 6444{ 6445 int param_len, pf, sp; 6446 int header_size, bd_len; 6447 int len_left, len_used; 6448 struct ctl_page_index *page_index; 6449 struct ctl_lun *lun; 6450 int control_dev, page_len; 6451 union ctl_modepage_info *modepage_info; 6452 int retval; 6453 6454 pf = 0; 6455 sp = 0; 6456 page_len = 0; 6457 len_used = 0; 6458 len_left = 0; 6459 retval = 0; 6460 bd_len = 0; 6461 page_index = NULL; 6462 6463 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6464 6465 if (lun->be_lun->lun_type != T_DIRECT) 6466 control_dev = 1; 6467 else 6468 control_dev = 0; 6469 6470 switch (ctsio->cdb[0]) { 6471 case MODE_SELECT_6: { 6472 struct scsi_mode_select_6 *cdb; 6473 6474 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6475 6476 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6477 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6478 6479 param_len = cdb->length; 6480 header_size = sizeof(struct scsi_mode_header_6); 6481 break; 6482 } 6483 case MODE_SELECT_10: { 6484 struct scsi_mode_select_10 *cdb; 6485 6486 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6487 6488 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6489 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6490 6491 param_len = scsi_2btoul(cdb->length); 6492 header_size = sizeof(struct scsi_mode_header_10); 6493 break; 6494 } 6495 default: 6496 ctl_set_invalid_opcode(ctsio); 6497 ctl_done((union ctl_io *)ctsio); 6498 return (CTL_RETVAL_COMPLETE); 6499 break; /* NOTREACHED */ 6500 } 6501 6502 /* 6503 * From SPC-3: 6504 * "A parameter list length of zero indicates that the Data-Out Buffer 6505 * shall be empty. This condition shall not be considered as an error." 6506 */ 6507 if (param_len == 0) { 6508 ctl_set_success(ctsio); 6509 ctl_done((union ctl_io *)ctsio); 6510 return (CTL_RETVAL_COMPLETE); 6511 } 6512 6513 /* 6514 * Since we'll hit this the first time through, prior to 6515 * allocation, we don't need to free a data buffer here. 6516 */ 6517 if (param_len < header_size) { 6518 ctl_set_param_len_error(ctsio); 6519 ctl_done((union ctl_io *)ctsio); 6520 return (CTL_RETVAL_COMPLETE); 6521 } 6522 6523 /* 6524 * Allocate the data buffer and grab the user's data. In theory, 6525 * we shouldn't have to sanity check the parameter list length here 6526 * because the maximum size is 64K. We should be able to malloc 6527 * that much without too many problems. 6528 */ 6529 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6530 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6531 ctsio->kern_data_len = param_len; 6532 ctsio->kern_total_len = param_len; 6533 ctsio->kern_data_resid = 0; 6534 ctsio->kern_rel_offset = 0; 6535 ctsio->kern_sg_entries = 0; 6536 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6537 ctsio->be_move_done = ctl_config_move_done; 6538 ctl_datamove((union ctl_io *)ctsio); 6539 6540 return (CTL_RETVAL_COMPLETE); 6541 } 6542 6543 switch (ctsio->cdb[0]) { 6544 case MODE_SELECT_6: { 6545 struct scsi_mode_header_6 *mh6; 6546 6547 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6548 bd_len = mh6->blk_desc_len; 6549 break; 6550 } 6551 case MODE_SELECT_10: { 6552 struct scsi_mode_header_10 *mh10; 6553 6554 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6555 bd_len = scsi_2btoul(mh10->blk_desc_len); 6556 break; 6557 } 6558 default: 6559 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6560 break; 6561 } 6562 6563 if (param_len < (header_size + bd_len)) { 6564 free(ctsio->kern_data_ptr, M_CTL); 6565 ctl_set_param_len_error(ctsio); 6566 ctl_done((union ctl_io *)ctsio); 6567 return (CTL_RETVAL_COMPLETE); 6568 } 6569 6570 /* 6571 * Set the IO_CONT flag, so that if this I/O gets passed to 6572 * ctl_config_write_done(), it'll get passed back to 6573 * ctl_do_mode_select() for further processing, or completion if 6574 * we're all done. 6575 */ 6576 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6577 ctsio->io_cont = ctl_do_mode_select; 6578 6579 modepage_info = (union ctl_modepage_info *) 6580 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6581 6582 memset(modepage_info, 0, sizeof(*modepage_info)); 6583 6584 len_left = param_len - header_size - bd_len; 6585 len_used = header_size + bd_len; 6586 6587 modepage_info->header.len_left = len_left; 6588 modepage_info->header.len_used = len_used; 6589 6590 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6591} 6592 6593int 6594ctl_mode_sense(struct ctl_scsiio *ctsio) 6595{ 6596 struct ctl_lun *lun; 6597 int pc, page_code, dbd, llba, subpage; 6598 int alloc_len, page_len, header_len, total_len; 6599 struct scsi_mode_block_descr *block_desc; 6600 struct ctl_page_index *page_index; 6601 int control_dev; 6602 6603 dbd = 0; 6604 llba = 0; 6605 block_desc = NULL; 6606 page_index = NULL; 6607 6608 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6609 6610 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6611 6612 if (lun->be_lun->lun_type != T_DIRECT) 6613 control_dev = 1; 6614 else 6615 control_dev = 0; 6616 6617 switch (ctsio->cdb[0]) { 6618 case MODE_SENSE_6: { 6619 struct scsi_mode_sense_6 *cdb; 6620 6621 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6622 6623 header_len = sizeof(struct scsi_mode_hdr_6); 6624 if (cdb->byte2 & SMS_DBD) 6625 dbd = 1; 6626 else 6627 header_len += sizeof(struct scsi_mode_block_descr); 6628 6629 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6630 page_code = cdb->page & SMS_PAGE_CODE; 6631 subpage = cdb->subpage; 6632 alloc_len = cdb->length; 6633 break; 6634 } 6635 case MODE_SENSE_10: { 6636 struct scsi_mode_sense_10 *cdb; 6637 6638 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6639 6640 header_len = sizeof(struct scsi_mode_hdr_10); 6641 6642 if (cdb->byte2 & SMS_DBD) 6643 dbd = 1; 6644 else 6645 header_len += sizeof(struct scsi_mode_block_descr); 6646 if (cdb->byte2 & SMS10_LLBAA) 6647 llba = 1; 6648 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6649 page_code = cdb->page & SMS_PAGE_CODE; 6650 subpage = cdb->subpage; 6651 alloc_len = scsi_2btoul(cdb->length); 6652 break; 6653 } 6654 default: 6655 ctl_set_invalid_opcode(ctsio); 6656 ctl_done((union ctl_io *)ctsio); 6657 return (CTL_RETVAL_COMPLETE); 6658 break; /* NOTREACHED */ 6659 } 6660 6661 /* 6662 * We have to make a first pass through to calculate the size of 6663 * the pages that match the user's query. Then we allocate enough 6664 * memory to hold it, and actually copy the data into the buffer. 6665 */ 6666 switch (page_code) { 6667 case SMS_ALL_PAGES_PAGE: { 6668 int i; 6669 6670 page_len = 0; 6671 6672 /* 6673 * At the moment, values other than 0 and 0xff here are 6674 * reserved according to SPC-3. 6675 */ 6676 if ((subpage != SMS_SUBPAGE_PAGE_0) 6677 && (subpage != SMS_SUBPAGE_ALL)) { 6678 ctl_set_invalid_field(ctsio, 6679 /*sks_valid*/ 1, 6680 /*command*/ 1, 6681 /*field*/ 3, 6682 /*bit_valid*/ 0, 6683 /*bit*/ 0); 6684 ctl_done((union ctl_io *)ctsio); 6685 return (CTL_RETVAL_COMPLETE); 6686 } 6687 6688 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6689 if ((control_dev != 0) 6690 && (lun->mode_pages.index[i].page_flags & 6691 CTL_PAGE_FLAG_DISK_ONLY)) 6692 continue; 6693 6694 /* 6695 * We don't use this subpage if the user didn't 6696 * request all subpages. 6697 */ 6698 if ((lun->mode_pages.index[i].subpage != 0) 6699 && (subpage == SMS_SUBPAGE_PAGE_0)) 6700 continue; 6701 6702#if 0 6703 printf("found page %#x len %d\n", 6704 lun->mode_pages.index[i].page_code & 6705 SMPH_PC_MASK, 6706 lun->mode_pages.index[i].page_len); 6707#endif 6708 page_len += lun->mode_pages.index[i].page_len; 6709 } 6710 break; 6711 } 6712 default: { 6713 int i; 6714 6715 page_len = 0; 6716 6717 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6718 /* Look for the right page code */ 6719 if ((lun->mode_pages.index[i].page_code & 6720 SMPH_PC_MASK) != page_code) 6721 continue; 6722 6723 /* Look for the right subpage or the subpage wildcard*/ 6724 if ((lun->mode_pages.index[i].subpage != subpage) 6725 && (subpage != SMS_SUBPAGE_ALL)) 6726 continue; 6727 6728 /* Make sure the page is supported for this dev type */ 6729 if ((control_dev != 0) 6730 && (lun->mode_pages.index[i].page_flags & 6731 CTL_PAGE_FLAG_DISK_ONLY)) 6732 continue; 6733 6734#if 0 6735 printf("found page %#x len %d\n", 6736 lun->mode_pages.index[i].page_code & 6737 SMPH_PC_MASK, 6738 lun->mode_pages.index[i].page_len); 6739#endif 6740 6741 page_len += lun->mode_pages.index[i].page_len; 6742 } 6743 6744 if (page_len == 0) { 6745 ctl_set_invalid_field(ctsio, 6746 /*sks_valid*/ 1, 6747 /*command*/ 1, 6748 /*field*/ 2, 6749 /*bit_valid*/ 1, 6750 /*bit*/ 5); 6751 ctl_done((union ctl_io *)ctsio); 6752 return (CTL_RETVAL_COMPLETE); 6753 } 6754 break; 6755 } 6756 } 6757 6758 total_len = header_len + page_len; 6759#if 0 6760 printf("header_len = %d, page_len = %d, total_len = %d\n", 6761 header_len, page_len, total_len); 6762#endif 6763 6764 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6765 ctsio->kern_sg_entries = 0; 6766 ctsio->kern_data_resid = 0; 6767 ctsio->kern_rel_offset = 0; 6768 if (total_len < alloc_len) { 6769 ctsio->residual = alloc_len - total_len; 6770 ctsio->kern_data_len = total_len; 6771 ctsio->kern_total_len = total_len; 6772 } else { 6773 ctsio->residual = 0; 6774 ctsio->kern_data_len = alloc_len; 6775 ctsio->kern_total_len = alloc_len; 6776 } 6777 6778 switch (ctsio->cdb[0]) { 6779 case MODE_SENSE_6: { 6780 struct scsi_mode_hdr_6 *header; 6781 6782 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6783 6784 header->datalen = MIN(total_len - 1, 254); 6785 if (control_dev == 0) { 6786 header->dev_specific = 0x10; /* DPOFUA */ 6787 if ((lun->flags & CTL_LUN_READONLY) || 6788 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6789 .eca_and_aen & SCP_SWP) != 0) 6790 header->dev_specific |= 0x80; /* WP */ 6791 } 6792 if (dbd) 6793 header->block_descr_len = 0; 6794 else 6795 header->block_descr_len = 6796 sizeof(struct scsi_mode_block_descr); 6797 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6798 break; 6799 } 6800 case MODE_SENSE_10: { 6801 struct scsi_mode_hdr_10 *header; 6802 int datalen; 6803 6804 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6805 6806 datalen = MIN(total_len - 2, 65533); 6807 scsi_ulto2b(datalen, header->datalen); 6808 if (control_dev == 0) { 6809 header->dev_specific = 0x10; /* DPOFUA */ 6810 if ((lun->flags & CTL_LUN_READONLY) || 6811 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6812 .eca_and_aen & SCP_SWP) != 0) 6813 header->dev_specific |= 0x80; /* WP */ 6814 } 6815 if (dbd) 6816 scsi_ulto2b(0, header->block_descr_len); 6817 else 6818 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6819 header->block_descr_len); 6820 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6821 break; 6822 } 6823 default: 6824 panic("invalid CDB type %#x", ctsio->cdb[0]); 6825 break; /* NOTREACHED */ 6826 } 6827 6828 /* 6829 * If we've got a disk, use its blocksize in the block 6830 * descriptor. Otherwise, just set it to 0. 6831 */ 6832 if (dbd == 0) { 6833 if (control_dev == 0) 6834 scsi_ulto3b(lun->be_lun->blocksize, 6835 block_desc->block_len); 6836 else 6837 scsi_ulto3b(0, block_desc->block_len); 6838 } 6839 6840 switch (page_code) { 6841 case SMS_ALL_PAGES_PAGE: { 6842 int i, data_used; 6843 6844 data_used = header_len; 6845 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6846 struct ctl_page_index *page_index; 6847 6848 page_index = &lun->mode_pages.index[i]; 6849 6850 if ((control_dev != 0) 6851 && (page_index->page_flags & 6852 CTL_PAGE_FLAG_DISK_ONLY)) 6853 continue; 6854 6855 /* 6856 * We don't use this subpage if the user didn't 6857 * request all subpages. We already checked (above) 6858 * to make sure the user only specified a subpage 6859 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6860 */ 6861 if ((page_index->subpage != 0) 6862 && (subpage == SMS_SUBPAGE_PAGE_0)) 6863 continue; 6864 6865 /* 6866 * Call the handler, if it exists, to update the 6867 * page to the latest values. 6868 */ 6869 if (page_index->sense_handler != NULL) 6870 page_index->sense_handler(ctsio, page_index,pc); 6871 6872 memcpy(ctsio->kern_data_ptr + data_used, 6873 page_index->page_data + 6874 (page_index->page_len * pc), 6875 page_index->page_len); 6876 data_used += page_index->page_len; 6877 } 6878 break; 6879 } 6880 default: { 6881 int i, data_used; 6882 6883 data_used = header_len; 6884 6885 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6886 struct ctl_page_index *page_index; 6887 6888 page_index = &lun->mode_pages.index[i]; 6889 6890 /* Look for the right page code */ 6891 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6892 continue; 6893 6894 /* Look for the right subpage or the subpage wildcard*/ 6895 if ((page_index->subpage != subpage) 6896 && (subpage != SMS_SUBPAGE_ALL)) 6897 continue; 6898 6899 /* Make sure the page is supported for this dev type */ 6900 if ((control_dev != 0) 6901 && (page_index->page_flags & 6902 CTL_PAGE_FLAG_DISK_ONLY)) 6903 continue; 6904 6905 /* 6906 * Call the handler, if it exists, to update the 6907 * page to the latest values. 6908 */ 6909 if (page_index->sense_handler != NULL) 6910 page_index->sense_handler(ctsio, page_index,pc); 6911 6912 memcpy(ctsio->kern_data_ptr + data_used, 6913 page_index->page_data + 6914 (page_index->page_len * pc), 6915 page_index->page_len); 6916 data_used += page_index->page_len; 6917 } 6918 break; 6919 } 6920 } 6921 6922 ctl_set_success(ctsio); 6923 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6924 ctsio->be_move_done = ctl_config_move_done; 6925 ctl_datamove((union ctl_io *)ctsio); 6926 return (CTL_RETVAL_COMPLETE); 6927} 6928 6929int 6930ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6931 struct ctl_page_index *page_index, 6932 int pc) 6933{ 6934 struct ctl_lun *lun; 6935 struct scsi_log_param_header *phdr; 6936 uint8_t *data; 6937 uint64_t val; 6938 6939 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6940 data = page_index->page_data; 6941 6942 if (lun->backend->lun_attr != NULL && 6943 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6944 != UINT64_MAX) { 6945 phdr = (struct scsi_log_param_header *)data; 6946 scsi_ulto2b(0x0001, phdr->param_code); 6947 phdr->param_control = SLP_LBIN | SLP_LP; 6948 phdr->param_len = 8; 6949 data = (uint8_t *)(phdr + 1); 6950 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6951 data[4] = 0x02; /* per-pool */ 6952 data += phdr->param_len; 6953 } 6954 6955 if (lun->backend->lun_attr != NULL && 6956 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6957 != UINT64_MAX) { 6958 phdr = (struct scsi_log_param_header *)data; 6959 scsi_ulto2b(0x0002, phdr->param_code); 6960 phdr->param_control = SLP_LBIN | SLP_LP; 6961 phdr->param_len = 8; 6962 data = (uint8_t *)(phdr + 1); 6963 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6964 data[4] = 0x01; /* per-LUN */ 6965 data += phdr->param_len; 6966 } 6967 6968 if (lun->backend->lun_attr != NULL && 6969 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6970 != UINT64_MAX) { 6971 phdr = (struct scsi_log_param_header *)data; 6972 scsi_ulto2b(0x00f1, phdr->param_code); 6973 phdr->param_control = SLP_LBIN | SLP_LP; 6974 phdr->param_len = 8; 6975 data = (uint8_t *)(phdr + 1); 6976 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6977 data[4] = 0x02; /* per-pool */ 6978 data += phdr->param_len; 6979 } 6980 6981 if (lun->backend->lun_attr != NULL && 6982 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6983 != UINT64_MAX) { 6984 phdr = (struct scsi_log_param_header *)data; 6985 scsi_ulto2b(0x00f2, phdr->param_code); 6986 phdr->param_control = SLP_LBIN | SLP_LP; 6987 phdr->param_len = 8; 6988 data = (uint8_t *)(phdr + 1); 6989 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6990 data[4] = 0x02; /* per-pool */ 6991 data += phdr->param_len; 6992 } 6993 6994 page_index->page_len = data - page_index->page_data; 6995 return (0); 6996} 6997 6998int 6999ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 7000 struct ctl_page_index *page_index, 7001 int pc) 7002{ 7003 struct ctl_lun *lun; 7004 struct stat_page *data; 7005 uint64_t rn, wn, rb, wb; 7006 struct bintime rt, wt; 7007 int i; 7008 7009 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7010 data = (struct stat_page *)page_index->page_data; 7011 7012 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 7013 data->sap.hdr.param_control = SLP_LBIN; 7014 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 7015 sizeof(struct scsi_log_param_header); 7016 rn = wn = rb = wb = 0; 7017 bintime_clear(&rt); 7018 bintime_clear(&wt); 7019 for (i = 0; i < CTL_MAX_PORTS; i++) { 7020 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 7021 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 7022 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 7023 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 7024 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 7025 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 7026 } 7027 scsi_u64to8b(rn, data->sap.read_num); 7028 scsi_u64to8b(wn, data->sap.write_num); 7029 if (lun->stats.blocksize > 0) { 7030 scsi_u64to8b(wb / lun->stats.blocksize, 7031 data->sap.recvieved_lba); 7032 scsi_u64to8b(rb / lun->stats.blocksize, 7033 data->sap.transmitted_lba); 7034 } 7035 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 7036 data->sap.read_int); 7037 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 7038 data->sap.write_int); 7039 scsi_u64to8b(0, data->sap.weighted_num); 7040 scsi_u64to8b(0, data->sap.weighted_int); 7041 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 7042 data->it.hdr.param_control = SLP_LBIN; 7043 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 7044 sizeof(struct scsi_log_param_header); 7045#ifdef CTL_TIME_IO 7046 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 7047#endif 7048 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 7049 data->it.hdr.param_control = SLP_LBIN; 7050 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 7051 sizeof(struct scsi_log_param_header); 7052 scsi_ulto4b(3, data->ti.exponent); 7053 scsi_ulto4b(1, data->ti.integer); 7054 7055 page_index->page_len = sizeof(*data); 7056 return (0); 7057} 7058 7059int 7060ctl_log_sense(struct ctl_scsiio *ctsio) 7061{ 7062 struct ctl_lun *lun; 7063 int i, pc, page_code, subpage; 7064 int alloc_len, total_len; 7065 struct ctl_page_index *page_index; 7066 struct scsi_log_sense *cdb; 7067 struct scsi_log_header *header; 7068 7069 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 7070 7071 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7072 cdb = (struct scsi_log_sense *)ctsio->cdb; 7073 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 7074 page_code = cdb->page & SLS_PAGE_CODE; 7075 subpage = cdb->subpage; 7076 alloc_len = scsi_2btoul(cdb->length); 7077 7078 page_index = NULL; 7079 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 7080 page_index = &lun->log_pages.index[i]; 7081 7082 /* Look for the right page code */ 7083 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 7084 continue; 7085 7086 /* Look for the right subpage or the subpage wildcard*/ 7087 if (page_index->subpage != subpage) 7088 continue; 7089 7090 break; 7091 } 7092 if (i >= CTL_NUM_LOG_PAGES) { 7093 ctl_set_invalid_field(ctsio, 7094 /*sks_valid*/ 1, 7095 /*command*/ 1, 7096 /*field*/ 2, 7097 /*bit_valid*/ 0, 7098 /*bit*/ 0); 7099 ctl_done((union ctl_io *)ctsio); 7100 return (CTL_RETVAL_COMPLETE); 7101 } 7102 7103 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 7104 7105 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7106 ctsio->kern_sg_entries = 0; 7107 ctsio->kern_data_resid = 0; 7108 ctsio->kern_rel_offset = 0; 7109 if (total_len < alloc_len) { 7110 ctsio->residual = alloc_len - total_len; 7111 ctsio->kern_data_len = total_len; 7112 ctsio->kern_total_len = total_len; 7113 } else { 7114 ctsio->residual = 0; 7115 ctsio->kern_data_len = alloc_len; 7116 ctsio->kern_total_len = alloc_len; 7117 } 7118 7119 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 7120 header->page = page_index->page_code; 7121 if (page_index->subpage) { 7122 header->page |= SL_SPF; 7123 header->subpage = page_index->subpage; 7124 } 7125 scsi_ulto2b(page_index->page_len, header->datalen); 7126 7127 /* 7128 * Call the handler, if it exists, to update the 7129 * page to the latest values. 7130 */ 7131 if (page_index->sense_handler != NULL) 7132 page_index->sense_handler(ctsio, page_index, pc); 7133 7134 memcpy(header + 1, page_index->page_data, page_index->page_len); 7135 7136 ctl_set_success(ctsio); 7137 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7138 ctsio->be_move_done = ctl_config_move_done; 7139 ctl_datamove((union ctl_io *)ctsio); 7140 return (CTL_RETVAL_COMPLETE); 7141} 7142 7143int 7144ctl_read_capacity(struct ctl_scsiio *ctsio) 7145{ 7146 struct scsi_read_capacity *cdb; 7147 struct scsi_read_capacity_data *data; 7148 struct ctl_lun *lun; 7149 uint32_t lba; 7150 7151 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7152 7153 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7154 7155 lba = scsi_4btoul(cdb->addr); 7156 if (((cdb->pmi & SRC_PMI) == 0) 7157 && (lba != 0)) { 7158 ctl_set_invalid_field(/*ctsio*/ ctsio, 7159 /*sks_valid*/ 1, 7160 /*command*/ 1, 7161 /*field*/ 2, 7162 /*bit_valid*/ 0, 7163 /*bit*/ 0); 7164 ctl_done((union ctl_io *)ctsio); 7165 return (CTL_RETVAL_COMPLETE); 7166 } 7167 7168 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7169 7170 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7171 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7172 ctsio->residual = 0; 7173 ctsio->kern_data_len = sizeof(*data); 7174 ctsio->kern_total_len = sizeof(*data); 7175 ctsio->kern_data_resid = 0; 7176 ctsio->kern_rel_offset = 0; 7177 ctsio->kern_sg_entries = 0; 7178 7179 /* 7180 * If the maximum LBA is greater than 0xfffffffe, the user must 7181 * issue a SERVICE ACTION IN (16) command, with the read capacity 7182 * serivce action set. 7183 */ 7184 if (lun->be_lun->maxlba > 0xfffffffe) 7185 scsi_ulto4b(0xffffffff, data->addr); 7186 else 7187 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7188 7189 /* 7190 * XXX KDM this may not be 512 bytes... 7191 */ 7192 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7193 7194 ctl_set_success(ctsio); 7195 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7196 ctsio->be_move_done = ctl_config_move_done; 7197 ctl_datamove((union ctl_io *)ctsio); 7198 return (CTL_RETVAL_COMPLETE); 7199} 7200 7201int 7202ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7203{ 7204 struct scsi_read_capacity_16 *cdb; 7205 struct scsi_read_capacity_data_long *data; 7206 struct ctl_lun *lun; 7207 uint64_t lba; 7208 uint32_t alloc_len; 7209 7210 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7211 7212 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7213 7214 alloc_len = scsi_4btoul(cdb->alloc_len); 7215 lba = scsi_8btou64(cdb->addr); 7216 7217 if ((cdb->reladr & SRC16_PMI) 7218 && (lba != 0)) { 7219 ctl_set_invalid_field(/*ctsio*/ ctsio, 7220 /*sks_valid*/ 1, 7221 /*command*/ 1, 7222 /*field*/ 2, 7223 /*bit_valid*/ 0, 7224 /*bit*/ 0); 7225 ctl_done((union ctl_io *)ctsio); 7226 return (CTL_RETVAL_COMPLETE); 7227 } 7228 7229 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7230 7231 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7232 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7233 7234 if (sizeof(*data) < alloc_len) { 7235 ctsio->residual = alloc_len - sizeof(*data); 7236 ctsio->kern_data_len = sizeof(*data); 7237 ctsio->kern_total_len = sizeof(*data); 7238 } else { 7239 ctsio->residual = 0; 7240 ctsio->kern_data_len = alloc_len; 7241 ctsio->kern_total_len = alloc_len; 7242 } 7243 ctsio->kern_data_resid = 0; 7244 ctsio->kern_rel_offset = 0; 7245 ctsio->kern_sg_entries = 0; 7246 7247 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7248 /* XXX KDM this may not be 512 bytes... */ 7249 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7250 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7251 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7252 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7253 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7254 7255 ctl_set_success(ctsio); 7256 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7257 ctsio->be_move_done = ctl_config_move_done; 7258 ctl_datamove((union ctl_io *)ctsio); 7259 return (CTL_RETVAL_COMPLETE); 7260} 7261 7262int 7263ctl_get_lba_status(struct ctl_scsiio *ctsio) 7264{ 7265 struct scsi_get_lba_status *cdb; 7266 struct scsi_get_lba_status_data *data; 7267 struct ctl_lun *lun; 7268 struct ctl_lba_len_flags *lbalen; 7269 uint64_t lba; 7270 uint32_t alloc_len, total_len; 7271 int retval; 7272 7273 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 7274 7275 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7276 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 7277 lba = scsi_8btou64(cdb->addr); 7278 alloc_len = scsi_4btoul(cdb->alloc_len); 7279 7280 if (lba > lun->be_lun->maxlba) { 7281 ctl_set_lba_out_of_range(ctsio); 7282 ctl_done((union ctl_io *)ctsio); 7283 return (CTL_RETVAL_COMPLETE); 7284 } 7285 7286 total_len = sizeof(*data) + sizeof(data->descr[0]); 7287 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7288 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 7289 7290 if (total_len < alloc_len) { 7291 ctsio->residual = alloc_len - total_len; 7292 ctsio->kern_data_len = total_len; 7293 ctsio->kern_total_len = total_len; 7294 } else { 7295 ctsio->residual = 0; 7296 ctsio->kern_data_len = alloc_len; 7297 ctsio->kern_total_len = alloc_len; 7298 } 7299 ctsio->kern_data_resid = 0; 7300 ctsio->kern_rel_offset = 0; 7301 ctsio->kern_sg_entries = 0; 7302 7303 /* Fill dummy data in case backend can't tell anything. */ 7304 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 7305 scsi_u64to8b(lba, data->descr[0].addr); 7306 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 7307 data->descr[0].length); 7308 data->descr[0].status = 0; /* Mapped or unknown. */ 7309 7310 ctl_set_success(ctsio); 7311 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7312 ctsio->be_move_done = ctl_config_move_done; 7313 7314 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 7315 lbalen->lba = lba; 7316 lbalen->len = total_len; 7317 lbalen->flags = 0; 7318 retval = lun->backend->config_read((union ctl_io *)ctsio); 7319 return (CTL_RETVAL_COMPLETE); 7320} 7321 7322int 7323ctl_read_defect(struct ctl_scsiio *ctsio) 7324{ 7325 struct scsi_read_defect_data_10 *ccb10; 7326 struct scsi_read_defect_data_12 *ccb12; 7327 struct scsi_read_defect_data_hdr_10 *data10; 7328 struct scsi_read_defect_data_hdr_12 *data12; 7329 uint32_t alloc_len, data_len; 7330 uint8_t format; 7331 7332 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7333 7334 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7335 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7336 format = ccb10->format; 7337 alloc_len = scsi_2btoul(ccb10->alloc_length); 7338 data_len = sizeof(*data10); 7339 } else { 7340 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7341 format = ccb12->format; 7342 alloc_len = scsi_4btoul(ccb12->alloc_length); 7343 data_len = sizeof(*data12); 7344 } 7345 if (alloc_len == 0) { 7346 ctl_set_success(ctsio); 7347 ctl_done((union ctl_io *)ctsio); 7348 return (CTL_RETVAL_COMPLETE); 7349 } 7350 7351 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7352 if (data_len < alloc_len) { 7353 ctsio->residual = alloc_len - data_len; 7354 ctsio->kern_data_len = data_len; 7355 ctsio->kern_total_len = data_len; 7356 } else { 7357 ctsio->residual = 0; 7358 ctsio->kern_data_len = alloc_len; 7359 ctsio->kern_total_len = alloc_len; 7360 } 7361 ctsio->kern_data_resid = 0; 7362 ctsio->kern_rel_offset = 0; 7363 ctsio->kern_sg_entries = 0; 7364 7365 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7366 data10 = (struct scsi_read_defect_data_hdr_10 *) 7367 ctsio->kern_data_ptr; 7368 data10->format = format; 7369 scsi_ulto2b(0, data10->length); 7370 } else { 7371 data12 = (struct scsi_read_defect_data_hdr_12 *) 7372 ctsio->kern_data_ptr; 7373 data12->format = format; 7374 scsi_ulto2b(0, data12->generation); 7375 scsi_ulto4b(0, data12->length); 7376 } 7377 7378 ctl_set_success(ctsio); 7379 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7380 ctsio->be_move_done = ctl_config_move_done; 7381 ctl_datamove((union ctl_io *)ctsio); 7382 return (CTL_RETVAL_COMPLETE); 7383} 7384 7385int 7386ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7387{ 7388 struct scsi_maintenance_in *cdb; 7389 int retval; 7390 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7391 int num_target_port_groups, num_target_ports; 7392 struct ctl_lun *lun; 7393 struct ctl_softc *softc; 7394 struct ctl_port *port; 7395 struct scsi_target_group_data *rtg_ptr; 7396 struct scsi_target_group_data_extended *rtg_ext_ptr; 7397 struct scsi_target_port_group_descriptor *tpg_desc; 7398 7399 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7400 7401 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7402 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7403 softc = lun->ctl_softc; 7404 7405 retval = CTL_RETVAL_COMPLETE; 7406 7407 switch (cdb->byte2 & STG_PDF_MASK) { 7408 case STG_PDF_LENGTH: 7409 ext = 0; 7410 break; 7411 case STG_PDF_EXTENDED: 7412 ext = 1; 7413 break; 7414 default: 7415 ctl_set_invalid_field(/*ctsio*/ ctsio, 7416 /*sks_valid*/ 1, 7417 /*command*/ 1, 7418 /*field*/ 2, 7419 /*bit_valid*/ 1, 7420 /*bit*/ 5); 7421 ctl_done((union ctl_io *)ctsio); 7422 return(retval); 7423 } 7424 7425 if (softc->is_single) 7426 num_target_port_groups = 1; 7427 else 7428 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7429 num_target_ports = 0; 7430 mtx_lock(&softc->ctl_lock); 7431 STAILQ_FOREACH(port, &softc->port_list, links) { 7432 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7433 continue; 7434 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7435 continue; 7436 num_target_ports++; 7437 } 7438 mtx_unlock(&softc->ctl_lock); 7439 7440 if (ext) 7441 total_len = sizeof(struct scsi_target_group_data_extended); 7442 else 7443 total_len = sizeof(struct scsi_target_group_data); 7444 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7445 num_target_port_groups + 7446 sizeof(struct scsi_target_port_descriptor) * 7447 num_target_ports * num_target_port_groups; 7448 7449 alloc_len = scsi_4btoul(cdb->length); 7450 7451 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7452 7453 ctsio->kern_sg_entries = 0; 7454 7455 if (total_len < alloc_len) { 7456 ctsio->residual = alloc_len - total_len; 7457 ctsio->kern_data_len = total_len; 7458 ctsio->kern_total_len = total_len; 7459 } else { 7460 ctsio->residual = 0; 7461 ctsio->kern_data_len = alloc_len; 7462 ctsio->kern_total_len = alloc_len; 7463 } 7464 ctsio->kern_data_resid = 0; 7465 ctsio->kern_rel_offset = 0; 7466 7467 if (ext) { 7468 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7469 ctsio->kern_data_ptr; 7470 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7471 rtg_ext_ptr->format_type = 0x10; 7472 rtg_ext_ptr->implicit_transition_time = 0; 7473 tpg_desc = &rtg_ext_ptr->groups[0]; 7474 } else { 7475 rtg_ptr = (struct scsi_target_group_data *) 7476 ctsio->kern_data_ptr; 7477 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7478 tpg_desc = &rtg_ptr->groups[0]; 7479 } 7480 7481 mtx_lock(&softc->ctl_lock); 7482 pg = softc->port_offset / CTL_MAX_PORTS; 7483 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7484 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7485 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7486 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7487 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7488 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7489 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7490 } else { 7491 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7492 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7493 } 7494 } else { 7495 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7496 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7497 } 7498 for (g = 0; g < num_target_port_groups; g++) { 7499 tpg_desc->pref_state = (g == pg) ? gs : os; 7500 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7501 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7502 tpg_desc->status = TPG_IMPLICIT; 7503 pc = 0; 7504 STAILQ_FOREACH(port, &softc->port_list, links) { 7505 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7506 continue; 7507 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7508 continue; 7509 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7510 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7511 relative_target_port_identifier); 7512 pc++; 7513 } 7514 tpg_desc->target_port_count = pc; 7515 tpg_desc = (struct scsi_target_port_group_descriptor *) 7516 &tpg_desc->descriptors[pc]; 7517 } 7518 mtx_unlock(&softc->ctl_lock); 7519 7520 ctl_set_success(ctsio); 7521 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7522 ctsio->be_move_done = ctl_config_move_done; 7523 ctl_datamove((union ctl_io *)ctsio); 7524 return(retval); 7525} 7526 7527int 7528ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7529{ 7530 struct ctl_lun *lun; 7531 struct scsi_report_supported_opcodes *cdb; 7532 const struct ctl_cmd_entry *entry, *sentry; 7533 struct scsi_report_supported_opcodes_all *all; 7534 struct scsi_report_supported_opcodes_descr *descr; 7535 struct scsi_report_supported_opcodes_one *one; 7536 int retval; 7537 int alloc_len, total_len; 7538 int opcode, service_action, i, j, num; 7539 7540 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7541 7542 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7543 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7544 7545 retval = CTL_RETVAL_COMPLETE; 7546 7547 opcode = cdb->requested_opcode; 7548 service_action = scsi_2btoul(cdb->requested_service_action); 7549 switch (cdb->options & RSO_OPTIONS_MASK) { 7550 case RSO_OPTIONS_ALL: 7551 num = 0; 7552 for (i = 0; i < 256; i++) { 7553 entry = &ctl_cmd_table[i]; 7554 if (entry->flags & CTL_CMD_FLAG_SA5) { 7555 for (j = 0; j < 32; j++) { 7556 sentry = &((const struct ctl_cmd_entry *) 7557 entry->execute)[j]; 7558 if (ctl_cmd_applicable( 7559 lun->be_lun->lun_type, sentry)) 7560 num++; 7561 } 7562 } else { 7563 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7564 entry)) 7565 num++; 7566 } 7567 } 7568 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7569 num * sizeof(struct scsi_report_supported_opcodes_descr); 7570 break; 7571 case RSO_OPTIONS_OC: 7572 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7573 ctl_set_invalid_field(/*ctsio*/ ctsio, 7574 /*sks_valid*/ 1, 7575 /*command*/ 1, 7576 /*field*/ 2, 7577 /*bit_valid*/ 1, 7578 /*bit*/ 2); 7579 ctl_done((union ctl_io *)ctsio); 7580 return (CTL_RETVAL_COMPLETE); 7581 } 7582 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7583 break; 7584 case RSO_OPTIONS_OC_SA: 7585 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7586 service_action >= 32) { 7587 ctl_set_invalid_field(/*ctsio*/ ctsio, 7588 /*sks_valid*/ 1, 7589 /*command*/ 1, 7590 /*field*/ 2, 7591 /*bit_valid*/ 1, 7592 /*bit*/ 2); 7593 ctl_done((union ctl_io *)ctsio); 7594 return (CTL_RETVAL_COMPLETE); 7595 } 7596 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7597 break; 7598 default: 7599 ctl_set_invalid_field(/*ctsio*/ ctsio, 7600 /*sks_valid*/ 1, 7601 /*command*/ 1, 7602 /*field*/ 2, 7603 /*bit_valid*/ 1, 7604 /*bit*/ 2); 7605 ctl_done((union ctl_io *)ctsio); 7606 return (CTL_RETVAL_COMPLETE); 7607 } 7608 7609 alloc_len = scsi_4btoul(cdb->length); 7610 7611 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7612 7613 ctsio->kern_sg_entries = 0; 7614 7615 if (total_len < alloc_len) { 7616 ctsio->residual = alloc_len - total_len; 7617 ctsio->kern_data_len = total_len; 7618 ctsio->kern_total_len = total_len; 7619 } else { 7620 ctsio->residual = 0; 7621 ctsio->kern_data_len = alloc_len; 7622 ctsio->kern_total_len = alloc_len; 7623 } 7624 ctsio->kern_data_resid = 0; 7625 ctsio->kern_rel_offset = 0; 7626 7627 switch (cdb->options & RSO_OPTIONS_MASK) { 7628 case RSO_OPTIONS_ALL: 7629 all = (struct scsi_report_supported_opcodes_all *) 7630 ctsio->kern_data_ptr; 7631 num = 0; 7632 for (i = 0; i < 256; i++) { 7633 entry = &ctl_cmd_table[i]; 7634 if (entry->flags & CTL_CMD_FLAG_SA5) { 7635 for (j = 0; j < 32; j++) { 7636 sentry = &((const struct ctl_cmd_entry *) 7637 entry->execute)[j]; 7638 if (!ctl_cmd_applicable( 7639 lun->be_lun->lun_type, sentry)) 7640 continue; 7641 descr = &all->descr[num++]; 7642 descr->opcode = i; 7643 scsi_ulto2b(j, descr->service_action); 7644 descr->flags = RSO_SERVACTV; 7645 scsi_ulto2b(sentry->length, 7646 descr->cdb_length); 7647 } 7648 } else { 7649 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7650 entry)) 7651 continue; 7652 descr = &all->descr[num++]; 7653 descr->opcode = i; 7654 scsi_ulto2b(0, descr->service_action); 7655 descr->flags = 0; 7656 scsi_ulto2b(entry->length, descr->cdb_length); 7657 } 7658 } 7659 scsi_ulto4b( 7660 num * sizeof(struct scsi_report_supported_opcodes_descr), 7661 all->length); 7662 break; 7663 case RSO_OPTIONS_OC: 7664 one = (struct scsi_report_supported_opcodes_one *) 7665 ctsio->kern_data_ptr; 7666 entry = &ctl_cmd_table[opcode]; 7667 goto fill_one; 7668 case RSO_OPTIONS_OC_SA: 7669 one = (struct scsi_report_supported_opcodes_one *) 7670 ctsio->kern_data_ptr; 7671 entry = &ctl_cmd_table[opcode]; 7672 entry = &((const struct ctl_cmd_entry *) 7673 entry->execute)[service_action]; 7674fill_one: 7675 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7676 one->support = 3; 7677 scsi_ulto2b(entry->length, one->cdb_length); 7678 one->cdb_usage[0] = opcode; 7679 memcpy(&one->cdb_usage[1], entry->usage, 7680 entry->length - 1); 7681 } else 7682 one->support = 1; 7683 break; 7684 } 7685 7686 ctl_set_success(ctsio); 7687 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7688 ctsio->be_move_done = ctl_config_move_done; 7689 ctl_datamove((union ctl_io *)ctsio); 7690 return(retval); 7691} 7692 7693int 7694ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7695{ 7696 struct scsi_report_supported_tmf *cdb; 7697 struct scsi_report_supported_tmf_data *data; 7698 int retval; 7699 int alloc_len, total_len; 7700 7701 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7702 7703 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7704 7705 retval = CTL_RETVAL_COMPLETE; 7706 7707 total_len = sizeof(struct scsi_report_supported_tmf_data); 7708 alloc_len = scsi_4btoul(cdb->length); 7709 7710 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7711 7712 ctsio->kern_sg_entries = 0; 7713 7714 if (total_len < alloc_len) { 7715 ctsio->residual = alloc_len - total_len; 7716 ctsio->kern_data_len = total_len; 7717 ctsio->kern_total_len = total_len; 7718 } else { 7719 ctsio->residual = 0; 7720 ctsio->kern_data_len = alloc_len; 7721 ctsio->kern_total_len = alloc_len; 7722 } 7723 ctsio->kern_data_resid = 0; 7724 ctsio->kern_rel_offset = 0; 7725 7726 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7727 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7728 data->byte2 |= RST_ITNRS; 7729 7730 ctl_set_success(ctsio); 7731 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7732 ctsio->be_move_done = ctl_config_move_done; 7733 ctl_datamove((union ctl_io *)ctsio); 7734 return (retval); 7735} 7736 7737int 7738ctl_report_timestamp(struct ctl_scsiio *ctsio) 7739{ 7740 struct scsi_report_timestamp *cdb; 7741 struct scsi_report_timestamp_data *data; 7742 struct timeval tv; 7743 int64_t timestamp; 7744 int retval; 7745 int alloc_len, total_len; 7746 7747 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7748 7749 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7750 7751 retval = CTL_RETVAL_COMPLETE; 7752 7753 total_len = sizeof(struct scsi_report_timestamp_data); 7754 alloc_len = scsi_4btoul(cdb->length); 7755 7756 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7757 7758 ctsio->kern_sg_entries = 0; 7759 7760 if (total_len < alloc_len) { 7761 ctsio->residual = alloc_len - total_len; 7762 ctsio->kern_data_len = total_len; 7763 ctsio->kern_total_len = total_len; 7764 } else { 7765 ctsio->residual = 0; 7766 ctsio->kern_data_len = alloc_len; 7767 ctsio->kern_total_len = alloc_len; 7768 } 7769 ctsio->kern_data_resid = 0; 7770 ctsio->kern_rel_offset = 0; 7771 7772 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7773 scsi_ulto2b(sizeof(*data) - 2, data->length); 7774 data->origin = RTS_ORIG_OUTSIDE; 7775 getmicrotime(&tv); 7776 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7777 scsi_ulto4b(timestamp >> 16, data->timestamp); 7778 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7779 7780 ctl_set_success(ctsio); 7781 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7782 ctsio->be_move_done = ctl_config_move_done; 7783 ctl_datamove((union ctl_io *)ctsio); 7784 return (retval); 7785} 7786 7787int 7788ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7789{ 7790 struct scsi_per_res_in *cdb; 7791 int alloc_len, total_len = 0; 7792 /* struct scsi_per_res_in_rsrv in_data; */ 7793 struct ctl_lun *lun; 7794 struct ctl_softc *softc; 7795 uint64_t key; 7796 7797 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7798 7799 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7800 7801 alloc_len = scsi_2btoul(cdb->length); 7802 7803 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7804 softc = lun->ctl_softc; 7805 7806retry: 7807 mtx_lock(&lun->lun_lock); 7808 switch (cdb->action) { 7809 case SPRI_RK: /* read keys */ 7810 total_len = sizeof(struct scsi_per_res_in_keys) + 7811 lun->pr_key_count * 7812 sizeof(struct scsi_per_res_key); 7813 break; 7814 case SPRI_RR: /* read reservation */ 7815 if (lun->flags & CTL_LUN_PR_RESERVED) 7816 total_len = sizeof(struct scsi_per_res_in_rsrv); 7817 else 7818 total_len = sizeof(struct scsi_per_res_in_header); 7819 break; 7820 case SPRI_RC: /* report capabilities */ 7821 total_len = sizeof(struct scsi_per_res_cap); 7822 break; 7823 case SPRI_RS: /* read full status */ 7824 total_len = sizeof(struct scsi_per_res_in_header) + 7825 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7826 lun->pr_key_count; 7827 break; 7828 default: 7829 panic("Invalid PR type %x", cdb->action); 7830 } 7831 mtx_unlock(&lun->lun_lock); 7832 7833 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7834 7835 if (total_len < alloc_len) { 7836 ctsio->residual = alloc_len - total_len; 7837 ctsio->kern_data_len = total_len; 7838 ctsio->kern_total_len = total_len; 7839 } else { 7840 ctsio->residual = 0; 7841 ctsio->kern_data_len = alloc_len; 7842 ctsio->kern_total_len = alloc_len; 7843 } 7844 7845 ctsio->kern_data_resid = 0; 7846 ctsio->kern_rel_offset = 0; 7847 ctsio->kern_sg_entries = 0; 7848 7849 mtx_lock(&lun->lun_lock); 7850 switch (cdb->action) { 7851 case SPRI_RK: { // read keys 7852 struct scsi_per_res_in_keys *res_keys; 7853 int i, key_count; 7854 7855 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7856 7857 /* 7858 * We had to drop the lock to allocate our buffer, which 7859 * leaves time for someone to come in with another 7860 * persistent reservation. (That is unlikely, though, 7861 * since this should be the only persistent reservation 7862 * command active right now.) 7863 */ 7864 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7865 (lun->pr_key_count * 7866 sizeof(struct scsi_per_res_key)))){ 7867 mtx_unlock(&lun->lun_lock); 7868 free(ctsio->kern_data_ptr, M_CTL); 7869 printf("%s: reservation length changed, retrying\n", 7870 __func__); 7871 goto retry; 7872 } 7873 7874 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7875 7876 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7877 lun->pr_key_count, res_keys->header.length); 7878 7879 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7880 if ((key = ctl_get_prkey(lun, i)) == 0) 7881 continue; 7882 7883 /* 7884 * We used lun->pr_key_count to calculate the 7885 * size to allocate. If it turns out the number of 7886 * initiators with the registered flag set is 7887 * larger than that (i.e. they haven't been kept in 7888 * sync), we've got a problem. 7889 */ 7890 if (key_count >= lun->pr_key_count) { 7891#ifdef NEEDTOPORT 7892 csevent_log(CSC_CTL | CSC_SHELF_SW | 7893 CTL_PR_ERROR, 7894 csevent_LogType_Fault, 7895 csevent_AlertLevel_Yellow, 7896 csevent_FRU_ShelfController, 7897 csevent_FRU_Firmware, 7898 csevent_FRU_Unknown, 7899 "registered keys %d >= key " 7900 "count %d", key_count, 7901 lun->pr_key_count); 7902#endif 7903 key_count++; 7904 continue; 7905 } 7906 scsi_u64to8b(key, res_keys->keys[key_count].key); 7907 key_count++; 7908 } 7909 break; 7910 } 7911 case SPRI_RR: { // read reservation 7912 struct scsi_per_res_in_rsrv *res; 7913 int tmp_len, header_only; 7914 7915 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7916 7917 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7918 7919 if (lun->flags & CTL_LUN_PR_RESERVED) 7920 { 7921 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7922 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7923 res->header.length); 7924 header_only = 0; 7925 } else { 7926 tmp_len = sizeof(struct scsi_per_res_in_header); 7927 scsi_ulto4b(0, res->header.length); 7928 header_only = 1; 7929 } 7930 7931 /* 7932 * We had to drop the lock to allocate our buffer, which 7933 * leaves time for someone to come in with another 7934 * persistent reservation. (That is unlikely, though, 7935 * since this should be the only persistent reservation 7936 * command active right now.) 7937 */ 7938 if (tmp_len != total_len) { 7939 mtx_unlock(&lun->lun_lock); 7940 free(ctsio->kern_data_ptr, M_CTL); 7941 printf("%s: reservation status changed, retrying\n", 7942 __func__); 7943 goto retry; 7944 } 7945 7946 /* 7947 * No reservation held, so we're done. 7948 */ 7949 if (header_only != 0) 7950 break; 7951 7952 /* 7953 * If the registration is an All Registrants type, the key 7954 * is 0, since it doesn't really matter. 7955 */ 7956 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7957 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7958 res->data.reservation); 7959 } 7960 res->data.scopetype = lun->res_type; 7961 break; 7962 } 7963 case SPRI_RC: //report capabilities 7964 { 7965 struct scsi_per_res_cap *res_cap; 7966 uint16_t type_mask; 7967 7968 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7969 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7970 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7971 type_mask = SPRI_TM_WR_EX_AR | 7972 SPRI_TM_EX_AC_RO | 7973 SPRI_TM_WR_EX_RO | 7974 SPRI_TM_EX_AC | 7975 SPRI_TM_WR_EX | 7976 SPRI_TM_EX_AC_AR; 7977 scsi_ulto2b(type_mask, res_cap->type_mask); 7978 break; 7979 } 7980 case SPRI_RS: { // read full status 7981 struct scsi_per_res_in_full *res_status; 7982 struct scsi_per_res_in_full_desc *res_desc; 7983 struct ctl_port *port; 7984 int i, len; 7985 7986 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7987 7988 /* 7989 * We had to drop the lock to allocate our buffer, which 7990 * leaves time for someone to come in with another 7991 * persistent reservation. (That is unlikely, though, 7992 * since this should be the only persistent reservation 7993 * command active right now.) 7994 */ 7995 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7996 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7997 lun->pr_key_count)){ 7998 mtx_unlock(&lun->lun_lock); 7999 free(ctsio->kern_data_ptr, M_CTL); 8000 printf("%s: reservation length changed, retrying\n", 8001 __func__); 8002 goto retry; 8003 } 8004 8005 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 8006 8007 res_desc = &res_status->desc[0]; 8008 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 8009 if ((key = ctl_get_prkey(lun, i)) == 0) 8010 continue; 8011 8012 scsi_u64to8b(key, res_desc->res_key.key); 8013 if ((lun->flags & CTL_LUN_PR_RESERVED) && 8014 (lun->pr_res_idx == i || 8015 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 8016 res_desc->flags = SPRI_FULL_R_HOLDER; 8017 res_desc->scopetype = lun->res_type; 8018 } 8019 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 8020 res_desc->rel_trgt_port_id); 8021 len = 0; 8022 port = softc->ctl_ports[ 8023 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 8024 if (port != NULL) 8025 len = ctl_create_iid(port, 8026 i % CTL_MAX_INIT_PER_PORT, 8027 res_desc->transport_id); 8028 scsi_ulto4b(len, res_desc->additional_length); 8029 res_desc = (struct scsi_per_res_in_full_desc *) 8030 &res_desc->transport_id[len]; 8031 } 8032 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 8033 res_status->header.length); 8034 break; 8035 } 8036 default: 8037 /* 8038 * This is a bug, because we just checked for this above, 8039 * and should have returned an error. 8040 */ 8041 panic("Invalid PR type %x", cdb->action); 8042 break; /* NOTREACHED */ 8043 } 8044 mtx_unlock(&lun->lun_lock); 8045 8046 ctl_set_success(ctsio); 8047 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8048 ctsio->be_move_done = ctl_config_move_done; 8049 ctl_datamove((union ctl_io *)ctsio); 8050 return (CTL_RETVAL_COMPLETE); 8051} 8052 8053static void 8054ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 8055{ 8056 int off = lun->ctl_softc->persis_offset; 8057 8058 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 8059 ctl_est_ua(lun, residx - off, ua); 8060} 8061 8062/* 8063 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 8064 * it should return. 8065 */ 8066static int 8067ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 8068 uint64_t sa_res_key, uint8_t type, uint32_t residx, 8069 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 8070 struct scsi_per_res_out_parms* param) 8071{ 8072 union ctl_ha_msg persis_io; 8073 int retval, i; 8074 int isc_retval; 8075 8076 retval = 0; 8077 8078 mtx_lock(&lun->lun_lock); 8079 if (sa_res_key == 0) { 8080 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8081 /* validate scope and type */ 8082 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8083 SPR_LU_SCOPE) { 8084 mtx_unlock(&lun->lun_lock); 8085 ctl_set_invalid_field(/*ctsio*/ ctsio, 8086 /*sks_valid*/ 1, 8087 /*command*/ 1, 8088 /*field*/ 2, 8089 /*bit_valid*/ 1, 8090 /*bit*/ 4); 8091 ctl_done((union ctl_io *)ctsio); 8092 return (1); 8093 } 8094 8095 if (type>8 || type==2 || type==4 || type==0) { 8096 mtx_unlock(&lun->lun_lock); 8097 ctl_set_invalid_field(/*ctsio*/ ctsio, 8098 /*sks_valid*/ 1, 8099 /*command*/ 1, 8100 /*field*/ 2, 8101 /*bit_valid*/ 1, 8102 /*bit*/ 0); 8103 ctl_done((union ctl_io *)ctsio); 8104 return (1); 8105 } 8106 8107 /* 8108 * Unregister everybody else and build UA for 8109 * them 8110 */ 8111 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8112 if (i == residx || ctl_get_prkey(lun, i) == 0) 8113 continue; 8114 8115 ctl_clr_prkey(lun, i); 8116 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8117 } 8118 lun->pr_key_count = 1; 8119 lun->res_type = type; 8120 if (lun->res_type != SPR_TYPE_WR_EX_AR 8121 && lun->res_type != SPR_TYPE_EX_AC_AR) 8122 lun->pr_res_idx = residx; 8123 8124 /* send msg to other side */ 8125 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8126 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8127 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8128 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8129 persis_io.pr.pr_info.res_type = type; 8130 memcpy(persis_io.pr.pr_info.sa_res_key, 8131 param->serv_act_res_key, 8132 sizeof(param->serv_act_res_key)); 8133 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8134 &persis_io, sizeof(persis_io), 0)) > 8135 CTL_HA_STATUS_SUCCESS) { 8136 printf("CTL:Persis Out error returned " 8137 "from ctl_ha_msg_send %d\n", 8138 isc_retval); 8139 } 8140 } else { 8141 /* not all registrants */ 8142 mtx_unlock(&lun->lun_lock); 8143 free(ctsio->kern_data_ptr, M_CTL); 8144 ctl_set_invalid_field(ctsio, 8145 /*sks_valid*/ 1, 8146 /*command*/ 0, 8147 /*field*/ 8, 8148 /*bit_valid*/ 0, 8149 /*bit*/ 0); 8150 ctl_done((union ctl_io *)ctsio); 8151 return (1); 8152 } 8153 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8154 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 8155 int found = 0; 8156 8157 if (res_key == sa_res_key) { 8158 /* special case */ 8159 /* 8160 * The spec implies this is not good but doesn't 8161 * say what to do. There are two choices either 8162 * generate a res conflict or check condition 8163 * with illegal field in parameter data. Since 8164 * that is what is done when the sa_res_key is 8165 * zero I'll take that approach since this has 8166 * to do with the sa_res_key. 8167 */ 8168 mtx_unlock(&lun->lun_lock); 8169 free(ctsio->kern_data_ptr, M_CTL); 8170 ctl_set_invalid_field(ctsio, 8171 /*sks_valid*/ 1, 8172 /*command*/ 0, 8173 /*field*/ 8, 8174 /*bit_valid*/ 0, 8175 /*bit*/ 0); 8176 ctl_done((union ctl_io *)ctsio); 8177 return (1); 8178 } 8179 8180 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8181 if (ctl_get_prkey(lun, i) != sa_res_key) 8182 continue; 8183 8184 found = 1; 8185 ctl_clr_prkey(lun, i); 8186 lun->pr_key_count--; 8187 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8188 } 8189 if (!found) { 8190 mtx_unlock(&lun->lun_lock); 8191 free(ctsio->kern_data_ptr, M_CTL); 8192 ctl_set_reservation_conflict(ctsio); 8193 ctl_done((union ctl_io *)ctsio); 8194 return (CTL_RETVAL_COMPLETE); 8195 } 8196 /* send msg to other side */ 8197 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8198 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8199 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8200 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8201 persis_io.pr.pr_info.res_type = type; 8202 memcpy(persis_io.pr.pr_info.sa_res_key, 8203 param->serv_act_res_key, 8204 sizeof(param->serv_act_res_key)); 8205 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8206 &persis_io, sizeof(persis_io), 0)) > 8207 CTL_HA_STATUS_SUCCESS) { 8208 printf("CTL:Persis Out error returned from " 8209 "ctl_ha_msg_send %d\n", isc_retval); 8210 } 8211 } else { 8212 /* Reserved but not all registrants */ 8213 /* sa_res_key is res holder */ 8214 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 8215 /* validate scope and type */ 8216 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8217 SPR_LU_SCOPE) { 8218 mtx_unlock(&lun->lun_lock); 8219 ctl_set_invalid_field(/*ctsio*/ ctsio, 8220 /*sks_valid*/ 1, 8221 /*command*/ 1, 8222 /*field*/ 2, 8223 /*bit_valid*/ 1, 8224 /*bit*/ 4); 8225 ctl_done((union ctl_io *)ctsio); 8226 return (1); 8227 } 8228 8229 if (type>8 || type==2 || type==4 || type==0) { 8230 mtx_unlock(&lun->lun_lock); 8231 ctl_set_invalid_field(/*ctsio*/ ctsio, 8232 /*sks_valid*/ 1, 8233 /*command*/ 1, 8234 /*field*/ 2, 8235 /*bit_valid*/ 1, 8236 /*bit*/ 0); 8237 ctl_done((union ctl_io *)ctsio); 8238 return (1); 8239 } 8240 8241 /* 8242 * Do the following: 8243 * if sa_res_key != res_key remove all 8244 * registrants w/sa_res_key and generate UA 8245 * for these registrants(Registrations 8246 * Preempted) if it wasn't an exclusive 8247 * reservation generate UA(Reservations 8248 * Preempted) for all other registered nexuses 8249 * if the type has changed. Establish the new 8250 * reservation and holder. If res_key and 8251 * sa_res_key are the same do the above 8252 * except don't unregister the res holder. 8253 */ 8254 8255 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8256 if (i == residx || ctl_get_prkey(lun, i) == 0) 8257 continue; 8258 8259 if (sa_res_key == ctl_get_prkey(lun, i)) { 8260 ctl_clr_prkey(lun, i); 8261 lun->pr_key_count--; 8262 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8263 } else if (type != lun->res_type 8264 && (lun->res_type == SPR_TYPE_WR_EX_RO 8265 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8266 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8267 } 8268 } 8269 lun->res_type = type; 8270 if (lun->res_type != SPR_TYPE_WR_EX_AR 8271 && lun->res_type != SPR_TYPE_EX_AC_AR) 8272 lun->pr_res_idx = residx; 8273 else 8274 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8275 8276 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8277 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8278 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8279 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8280 persis_io.pr.pr_info.res_type = type; 8281 memcpy(persis_io.pr.pr_info.sa_res_key, 8282 param->serv_act_res_key, 8283 sizeof(param->serv_act_res_key)); 8284 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8285 &persis_io, sizeof(persis_io), 0)) > 8286 CTL_HA_STATUS_SUCCESS) { 8287 printf("CTL:Persis Out error returned " 8288 "from ctl_ha_msg_send %d\n", 8289 isc_retval); 8290 } 8291 } else { 8292 /* 8293 * sa_res_key is not the res holder just 8294 * remove registrants 8295 */ 8296 int found=0; 8297 8298 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8299 if (sa_res_key != ctl_get_prkey(lun, i)) 8300 continue; 8301 8302 found = 1; 8303 ctl_clr_prkey(lun, i); 8304 lun->pr_key_count--; 8305 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8306 } 8307 8308 if (!found) { 8309 mtx_unlock(&lun->lun_lock); 8310 free(ctsio->kern_data_ptr, M_CTL); 8311 ctl_set_reservation_conflict(ctsio); 8312 ctl_done((union ctl_io *)ctsio); 8313 return (1); 8314 } 8315 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8316 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8317 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8318 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8319 persis_io.pr.pr_info.res_type = type; 8320 memcpy(persis_io.pr.pr_info.sa_res_key, 8321 param->serv_act_res_key, 8322 sizeof(param->serv_act_res_key)); 8323 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8324 &persis_io, sizeof(persis_io), 0)) > 8325 CTL_HA_STATUS_SUCCESS) { 8326 printf("CTL:Persis Out error returned " 8327 "from ctl_ha_msg_send %d\n", 8328 isc_retval); 8329 } 8330 } 8331 } 8332 8333 lun->PRGeneration++; 8334 mtx_unlock(&lun->lun_lock); 8335 8336 return (retval); 8337} 8338 8339static void 8340ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8341{ 8342 uint64_t sa_res_key; 8343 int i; 8344 8345 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8346 8347 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8348 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8349 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 8350 if (sa_res_key == 0) { 8351 /* 8352 * Unregister everybody else and build UA for 8353 * them 8354 */ 8355 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8356 if (i == msg->pr.pr_info.residx || 8357 ctl_get_prkey(lun, i) == 0) 8358 continue; 8359 8360 ctl_clr_prkey(lun, i); 8361 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8362 } 8363 8364 lun->pr_key_count = 1; 8365 lun->res_type = msg->pr.pr_info.res_type; 8366 if (lun->res_type != SPR_TYPE_WR_EX_AR 8367 && lun->res_type != SPR_TYPE_EX_AC_AR) 8368 lun->pr_res_idx = msg->pr.pr_info.residx; 8369 } else { 8370 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8371 if (sa_res_key == ctl_get_prkey(lun, i)) 8372 continue; 8373 8374 ctl_clr_prkey(lun, i); 8375 lun->pr_key_count--; 8376 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8377 } 8378 } 8379 } else { 8380 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8381 if (i == msg->pr.pr_info.residx || 8382 ctl_get_prkey(lun, i) == 0) 8383 continue; 8384 8385 if (sa_res_key == ctl_get_prkey(lun, i)) { 8386 ctl_clr_prkey(lun, i); 8387 lun->pr_key_count--; 8388 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8389 } else if (msg->pr.pr_info.res_type != lun->res_type 8390 && (lun->res_type == SPR_TYPE_WR_EX_RO 8391 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8392 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE); 8393 } 8394 } 8395 lun->res_type = msg->pr.pr_info.res_type; 8396 if (lun->res_type != SPR_TYPE_WR_EX_AR 8397 && lun->res_type != SPR_TYPE_EX_AC_AR) 8398 lun->pr_res_idx = msg->pr.pr_info.residx; 8399 else 8400 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8401 } 8402 lun->PRGeneration++; 8403 8404} 8405 8406 8407int 8408ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8409{ 8410 int retval; 8411 int isc_retval; 8412 u_int32_t param_len; 8413 struct scsi_per_res_out *cdb; 8414 struct ctl_lun *lun; 8415 struct scsi_per_res_out_parms* param; 8416 struct ctl_softc *softc; 8417 uint32_t residx; 8418 uint64_t res_key, sa_res_key, key; 8419 uint8_t type; 8420 union ctl_ha_msg persis_io; 8421 int i; 8422 8423 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8424 8425 retval = CTL_RETVAL_COMPLETE; 8426 8427 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8428 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8429 softc = lun->ctl_softc; 8430 8431 /* 8432 * We only support whole-LUN scope. The scope & type are ignored for 8433 * register, register and ignore existing key and clear. 8434 * We sometimes ignore scope and type on preempts too!! 8435 * Verify reservation type here as well. 8436 */ 8437 type = cdb->scope_type & SPR_TYPE_MASK; 8438 if ((cdb->action == SPRO_RESERVE) 8439 || (cdb->action == SPRO_RELEASE)) { 8440 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8441 ctl_set_invalid_field(/*ctsio*/ ctsio, 8442 /*sks_valid*/ 1, 8443 /*command*/ 1, 8444 /*field*/ 2, 8445 /*bit_valid*/ 1, 8446 /*bit*/ 4); 8447 ctl_done((union ctl_io *)ctsio); 8448 return (CTL_RETVAL_COMPLETE); 8449 } 8450 8451 if (type>8 || type==2 || type==4 || type==0) { 8452 ctl_set_invalid_field(/*ctsio*/ ctsio, 8453 /*sks_valid*/ 1, 8454 /*command*/ 1, 8455 /*field*/ 2, 8456 /*bit_valid*/ 1, 8457 /*bit*/ 0); 8458 ctl_done((union ctl_io *)ctsio); 8459 return (CTL_RETVAL_COMPLETE); 8460 } 8461 } 8462 8463 param_len = scsi_4btoul(cdb->length); 8464 8465 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8466 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8467 ctsio->kern_data_len = param_len; 8468 ctsio->kern_total_len = param_len; 8469 ctsio->kern_data_resid = 0; 8470 ctsio->kern_rel_offset = 0; 8471 ctsio->kern_sg_entries = 0; 8472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8473 ctsio->be_move_done = ctl_config_move_done; 8474 ctl_datamove((union ctl_io *)ctsio); 8475 8476 return (CTL_RETVAL_COMPLETE); 8477 } 8478 8479 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8480 8481 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8482 res_key = scsi_8btou64(param->res_key.key); 8483 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8484 8485 /* 8486 * Validate the reservation key here except for SPRO_REG_IGNO 8487 * This must be done for all other service actions 8488 */ 8489 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8490 mtx_lock(&lun->lun_lock); 8491 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8492 if (res_key != key) { 8493 /* 8494 * The current key passed in doesn't match 8495 * the one the initiator previously 8496 * registered. 8497 */ 8498 mtx_unlock(&lun->lun_lock); 8499 free(ctsio->kern_data_ptr, M_CTL); 8500 ctl_set_reservation_conflict(ctsio); 8501 ctl_done((union ctl_io *)ctsio); 8502 return (CTL_RETVAL_COMPLETE); 8503 } 8504 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8505 /* 8506 * We are not registered 8507 */ 8508 mtx_unlock(&lun->lun_lock); 8509 free(ctsio->kern_data_ptr, M_CTL); 8510 ctl_set_reservation_conflict(ctsio); 8511 ctl_done((union ctl_io *)ctsio); 8512 return (CTL_RETVAL_COMPLETE); 8513 } else if (res_key != 0) { 8514 /* 8515 * We are not registered and trying to register but 8516 * the register key isn't zero. 8517 */ 8518 mtx_unlock(&lun->lun_lock); 8519 free(ctsio->kern_data_ptr, M_CTL); 8520 ctl_set_reservation_conflict(ctsio); 8521 ctl_done((union ctl_io *)ctsio); 8522 return (CTL_RETVAL_COMPLETE); 8523 } 8524 mtx_unlock(&lun->lun_lock); 8525 } 8526 8527 switch (cdb->action & SPRO_ACTION_MASK) { 8528 case SPRO_REGISTER: 8529 case SPRO_REG_IGNO: { 8530 8531#if 0 8532 printf("Registration received\n"); 8533#endif 8534 8535 /* 8536 * We don't support any of these options, as we report in 8537 * the read capabilities request (see 8538 * ctl_persistent_reserve_in(), above). 8539 */ 8540 if ((param->flags & SPR_SPEC_I_PT) 8541 || (param->flags & SPR_ALL_TG_PT) 8542 || (param->flags & SPR_APTPL)) { 8543 int bit_ptr; 8544 8545 if (param->flags & SPR_APTPL) 8546 bit_ptr = 0; 8547 else if (param->flags & SPR_ALL_TG_PT) 8548 bit_ptr = 2; 8549 else /* SPR_SPEC_I_PT */ 8550 bit_ptr = 3; 8551 8552 free(ctsio->kern_data_ptr, M_CTL); 8553 ctl_set_invalid_field(ctsio, 8554 /*sks_valid*/ 1, 8555 /*command*/ 0, 8556 /*field*/ 20, 8557 /*bit_valid*/ 1, 8558 /*bit*/ bit_ptr); 8559 ctl_done((union ctl_io *)ctsio); 8560 return (CTL_RETVAL_COMPLETE); 8561 } 8562 8563 mtx_lock(&lun->lun_lock); 8564 8565 /* 8566 * The initiator wants to clear the 8567 * key/unregister. 8568 */ 8569 if (sa_res_key == 0) { 8570 if ((res_key == 0 8571 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8572 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8573 && ctl_get_prkey(lun, residx) == 0)) { 8574 mtx_unlock(&lun->lun_lock); 8575 goto done; 8576 } 8577 8578 ctl_clr_prkey(lun, residx); 8579 lun->pr_key_count--; 8580 8581 if (residx == lun->pr_res_idx) { 8582 lun->flags &= ~CTL_LUN_PR_RESERVED; 8583 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8584 8585 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8586 || lun->res_type == SPR_TYPE_EX_AC_RO) 8587 && lun->pr_key_count) { 8588 /* 8589 * If the reservation is a registrants 8590 * only type we need to generate a UA 8591 * for other registered inits. The 8592 * sense code should be RESERVATIONS 8593 * RELEASED 8594 */ 8595 8596 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8597 if (ctl_get_prkey(lun, i + 8598 softc->persis_offset) == 0) 8599 continue; 8600 ctl_est_ua(lun, i, 8601 CTL_UA_RES_RELEASE); 8602 } 8603 } 8604 lun->res_type = 0; 8605 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8606 if (lun->pr_key_count==0) { 8607 lun->flags &= ~CTL_LUN_PR_RESERVED; 8608 lun->res_type = 0; 8609 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8610 } 8611 } 8612 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8613 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8614 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8615 persis_io.pr.pr_info.residx = residx; 8616 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8617 &persis_io, sizeof(persis_io), 0 )) > 8618 CTL_HA_STATUS_SUCCESS) { 8619 printf("CTL:Persis Out error returned from " 8620 "ctl_ha_msg_send %d\n", isc_retval); 8621 } 8622 } else /* sa_res_key != 0 */ { 8623 8624 /* 8625 * If we aren't registered currently then increment 8626 * the key count and set the registered flag. 8627 */ 8628 ctl_alloc_prkey(lun, residx); 8629 if (ctl_get_prkey(lun, residx) == 0) 8630 lun->pr_key_count++; 8631 ctl_set_prkey(lun, residx, sa_res_key); 8632 8633 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8634 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8635 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8636 persis_io.pr.pr_info.residx = residx; 8637 memcpy(persis_io.pr.pr_info.sa_res_key, 8638 param->serv_act_res_key, 8639 sizeof(param->serv_act_res_key)); 8640 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8641 &persis_io, sizeof(persis_io), 0)) > 8642 CTL_HA_STATUS_SUCCESS) { 8643 printf("CTL:Persis Out error returned from " 8644 "ctl_ha_msg_send %d\n", isc_retval); 8645 } 8646 } 8647 lun->PRGeneration++; 8648 mtx_unlock(&lun->lun_lock); 8649 8650 break; 8651 } 8652 case SPRO_RESERVE: 8653#if 0 8654 printf("Reserve executed type %d\n", type); 8655#endif 8656 mtx_lock(&lun->lun_lock); 8657 if (lun->flags & CTL_LUN_PR_RESERVED) { 8658 /* 8659 * if this isn't the reservation holder and it's 8660 * not a "all registrants" type or if the type is 8661 * different then we have a conflict 8662 */ 8663 if ((lun->pr_res_idx != residx 8664 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8665 || lun->res_type != type) { 8666 mtx_unlock(&lun->lun_lock); 8667 free(ctsio->kern_data_ptr, M_CTL); 8668 ctl_set_reservation_conflict(ctsio); 8669 ctl_done((union ctl_io *)ctsio); 8670 return (CTL_RETVAL_COMPLETE); 8671 } 8672 mtx_unlock(&lun->lun_lock); 8673 } else /* create a reservation */ { 8674 /* 8675 * If it's not an "all registrants" type record 8676 * reservation holder 8677 */ 8678 if (type != SPR_TYPE_WR_EX_AR 8679 && type != SPR_TYPE_EX_AC_AR) 8680 lun->pr_res_idx = residx; /* Res holder */ 8681 else 8682 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8683 8684 lun->flags |= CTL_LUN_PR_RESERVED; 8685 lun->res_type = type; 8686 8687 mtx_unlock(&lun->lun_lock); 8688 8689 /* send msg to other side */ 8690 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8691 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8692 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8693 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8694 persis_io.pr.pr_info.res_type = type; 8695 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8696 &persis_io, sizeof(persis_io), 0)) > 8697 CTL_HA_STATUS_SUCCESS) { 8698 printf("CTL:Persis Out error returned from " 8699 "ctl_ha_msg_send %d\n", isc_retval); 8700 } 8701 } 8702 break; 8703 8704 case SPRO_RELEASE: 8705 mtx_lock(&lun->lun_lock); 8706 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8707 /* No reservation exists return good status */ 8708 mtx_unlock(&lun->lun_lock); 8709 goto done; 8710 } 8711 /* 8712 * Is this nexus a reservation holder? 8713 */ 8714 if (lun->pr_res_idx != residx 8715 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8716 /* 8717 * not a res holder return good status but 8718 * do nothing 8719 */ 8720 mtx_unlock(&lun->lun_lock); 8721 goto done; 8722 } 8723 8724 if (lun->res_type != type) { 8725 mtx_unlock(&lun->lun_lock); 8726 free(ctsio->kern_data_ptr, M_CTL); 8727 ctl_set_illegal_pr_release(ctsio); 8728 ctl_done((union ctl_io *)ctsio); 8729 return (CTL_RETVAL_COMPLETE); 8730 } 8731 8732 /* okay to release */ 8733 lun->flags &= ~CTL_LUN_PR_RESERVED; 8734 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8735 lun->res_type = 0; 8736 8737 /* 8738 * if this isn't an exclusive access 8739 * res generate UA for all other 8740 * registrants. 8741 */ 8742 if (type != SPR_TYPE_EX_AC 8743 && type != SPR_TYPE_WR_EX) { 8744 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8745 if (i == residx || 8746 ctl_get_prkey(lun, 8747 i + softc->persis_offset) == 0) 8748 continue; 8749 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8750 } 8751 } 8752 mtx_unlock(&lun->lun_lock); 8753 /* Send msg to other side */ 8754 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8755 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8756 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8757 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8758 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8759 printf("CTL:Persis Out error returned from " 8760 "ctl_ha_msg_send %d\n", isc_retval); 8761 } 8762 break; 8763 8764 case SPRO_CLEAR: 8765 /* send msg to other side */ 8766 8767 mtx_lock(&lun->lun_lock); 8768 lun->flags &= ~CTL_LUN_PR_RESERVED; 8769 lun->res_type = 0; 8770 lun->pr_key_count = 0; 8771 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8772 8773 ctl_clr_prkey(lun, residx); 8774 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8775 if (ctl_get_prkey(lun, i) != 0) { 8776 ctl_clr_prkey(lun, i); 8777 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8778 } 8779 lun->PRGeneration++; 8780 mtx_unlock(&lun->lun_lock); 8781 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8782 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8783 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8784 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8785 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8786 printf("CTL:Persis Out error returned from " 8787 "ctl_ha_msg_send %d\n", isc_retval); 8788 } 8789 break; 8790 8791 case SPRO_PREEMPT: 8792 case SPRO_PRE_ABO: { 8793 int nretval; 8794 8795 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8796 residx, ctsio, cdb, param); 8797 if (nretval != 0) 8798 return (CTL_RETVAL_COMPLETE); 8799 break; 8800 } 8801 default: 8802 panic("Invalid PR type %x", cdb->action); 8803 } 8804 8805done: 8806 free(ctsio->kern_data_ptr, M_CTL); 8807 ctl_set_success(ctsio); 8808 ctl_done((union ctl_io *)ctsio); 8809 8810 return (retval); 8811} 8812 8813/* 8814 * This routine is for handling a message from the other SC pertaining to 8815 * persistent reserve out. All the error checking will have been done 8816 * so only perorming the action need be done here to keep the two 8817 * in sync. 8818 */ 8819static void 8820ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8821{ 8822 struct ctl_lun *lun; 8823 struct ctl_softc *softc; 8824 int i; 8825 uint32_t targ_lun; 8826 8827 softc = control_softc; 8828 8829 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8830 lun = softc->ctl_luns[targ_lun]; 8831 mtx_lock(&lun->lun_lock); 8832 switch(msg->pr.pr_info.action) { 8833 case CTL_PR_REG_KEY: 8834 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8835 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8836 lun->pr_key_count++; 8837 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8838 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8839 lun->PRGeneration++; 8840 break; 8841 8842 case CTL_PR_UNREG_KEY: 8843 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8844 lun->pr_key_count--; 8845 8846 /* XXX Need to see if the reservation has been released */ 8847 /* if so do we need to generate UA? */ 8848 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8849 lun->flags &= ~CTL_LUN_PR_RESERVED; 8850 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8851 8852 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8853 || lun->res_type == SPR_TYPE_EX_AC_RO) 8854 && lun->pr_key_count) { 8855 /* 8856 * If the reservation is a registrants 8857 * only type we need to generate a UA 8858 * for other registered inits. The 8859 * sense code should be RESERVATIONS 8860 * RELEASED 8861 */ 8862 8863 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8864 if (ctl_get_prkey(lun, i + 8865 softc->persis_offset) == 0) 8866 continue; 8867 8868 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8869 } 8870 } 8871 lun->res_type = 0; 8872 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8873 if (lun->pr_key_count==0) { 8874 lun->flags &= ~CTL_LUN_PR_RESERVED; 8875 lun->res_type = 0; 8876 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8877 } 8878 } 8879 lun->PRGeneration++; 8880 break; 8881 8882 case CTL_PR_RESERVE: 8883 lun->flags |= CTL_LUN_PR_RESERVED; 8884 lun->res_type = msg->pr.pr_info.res_type; 8885 lun->pr_res_idx = msg->pr.pr_info.residx; 8886 8887 break; 8888 8889 case CTL_PR_RELEASE: 8890 /* 8891 * if this isn't an exclusive access res generate UA for all 8892 * other registrants. 8893 */ 8894 if (lun->res_type != SPR_TYPE_EX_AC 8895 && lun->res_type != SPR_TYPE_WR_EX) { 8896 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8897 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0) 8898 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8899 } 8900 8901 lun->flags &= ~CTL_LUN_PR_RESERVED; 8902 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8903 lun->res_type = 0; 8904 break; 8905 8906 case CTL_PR_PREEMPT: 8907 ctl_pro_preempt_other(lun, msg); 8908 break; 8909 case CTL_PR_CLEAR: 8910 lun->flags &= ~CTL_LUN_PR_RESERVED; 8911 lun->res_type = 0; 8912 lun->pr_key_count = 0; 8913 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8914 8915 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8916 if (ctl_get_prkey(lun, i) == 0) 8917 continue; 8918 ctl_clr_prkey(lun, i); 8919 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8920 } 8921 lun->PRGeneration++; 8922 break; 8923 } 8924 8925 mtx_unlock(&lun->lun_lock); 8926} 8927 8928int 8929ctl_read_write(struct ctl_scsiio *ctsio) 8930{ 8931 struct ctl_lun *lun; 8932 struct ctl_lba_len_flags *lbalen; 8933 uint64_t lba; 8934 uint32_t num_blocks; 8935 int flags, retval; 8936 int isread; 8937 8938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8939 8940 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8941 8942 flags = 0; 8943 retval = CTL_RETVAL_COMPLETE; 8944 8945 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8946 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8947 switch (ctsio->cdb[0]) { 8948 case READ_6: 8949 case WRITE_6: { 8950 struct scsi_rw_6 *cdb; 8951 8952 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8953 8954 lba = scsi_3btoul(cdb->addr); 8955 /* only 5 bits are valid in the most significant address byte */ 8956 lba &= 0x1fffff; 8957 num_blocks = cdb->length; 8958 /* 8959 * This is correct according to SBC-2. 8960 */ 8961 if (num_blocks == 0) 8962 num_blocks = 256; 8963 break; 8964 } 8965 case READ_10: 8966 case WRITE_10: { 8967 struct scsi_rw_10 *cdb; 8968 8969 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8970 if (cdb->byte2 & SRW10_FUA) 8971 flags |= CTL_LLF_FUA; 8972 if (cdb->byte2 & SRW10_DPO) 8973 flags |= CTL_LLF_DPO; 8974 lba = scsi_4btoul(cdb->addr); 8975 num_blocks = scsi_2btoul(cdb->length); 8976 break; 8977 } 8978 case WRITE_VERIFY_10: { 8979 struct scsi_write_verify_10 *cdb; 8980 8981 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8982 flags |= CTL_LLF_FUA; 8983 if (cdb->byte2 & SWV_DPO) 8984 flags |= CTL_LLF_DPO; 8985 lba = scsi_4btoul(cdb->addr); 8986 num_blocks = scsi_2btoul(cdb->length); 8987 break; 8988 } 8989 case READ_12: 8990 case WRITE_12: { 8991 struct scsi_rw_12 *cdb; 8992 8993 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8994 if (cdb->byte2 & SRW12_FUA) 8995 flags |= CTL_LLF_FUA; 8996 if (cdb->byte2 & SRW12_DPO) 8997 flags |= CTL_LLF_DPO; 8998 lba = scsi_4btoul(cdb->addr); 8999 num_blocks = scsi_4btoul(cdb->length); 9000 break; 9001 } 9002 case WRITE_VERIFY_12: { 9003 struct scsi_write_verify_12 *cdb; 9004 9005 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 9006 flags |= CTL_LLF_FUA; 9007 if (cdb->byte2 & SWV_DPO) 9008 flags |= CTL_LLF_DPO; 9009 lba = scsi_4btoul(cdb->addr); 9010 num_blocks = scsi_4btoul(cdb->length); 9011 break; 9012 } 9013 case READ_16: 9014 case WRITE_16: { 9015 struct scsi_rw_16 *cdb; 9016 9017 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9018 if (cdb->byte2 & SRW12_FUA) 9019 flags |= CTL_LLF_FUA; 9020 if (cdb->byte2 & SRW12_DPO) 9021 flags |= CTL_LLF_DPO; 9022 lba = scsi_8btou64(cdb->addr); 9023 num_blocks = scsi_4btoul(cdb->length); 9024 break; 9025 } 9026 case WRITE_ATOMIC_16: { 9027 struct scsi_rw_16 *cdb; 9028 9029 if (lun->be_lun->atomicblock == 0) { 9030 ctl_set_invalid_opcode(ctsio); 9031 ctl_done((union ctl_io *)ctsio); 9032 return (CTL_RETVAL_COMPLETE); 9033 } 9034 9035 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9036 if (cdb->byte2 & SRW12_FUA) 9037 flags |= CTL_LLF_FUA; 9038 if (cdb->byte2 & SRW12_DPO) 9039 flags |= CTL_LLF_DPO; 9040 lba = scsi_8btou64(cdb->addr); 9041 num_blocks = scsi_4btoul(cdb->length); 9042 if (num_blocks > lun->be_lun->atomicblock) { 9043 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 9044 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 9045 /*bit*/ 0); 9046 ctl_done((union ctl_io *)ctsio); 9047 return (CTL_RETVAL_COMPLETE); 9048 } 9049 break; 9050 } 9051 case WRITE_VERIFY_16: { 9052 struct scsi_write_verify_16 *cdb; 9053 9054 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 9055 flags |= CTL_LLF_FUA; 9056 if (cdb->byte2 & SWV_DPO) 9057 flags |= CTL_LLF_DPO; 9058 lba = scsi_8btou64(cdb->addr); 9059 num_blocks = scsi_4btoul(cdb->length); 9060 break; 9061 } 9062 default: 9063 /* 9064 * We got a command we don't support. This shouldn't 9065 * happen, commands should be filtered out above us. 9066 */ 9067 ctl_set_invalid_opcode(ctsio); 9068 ctl_done((union ctl_io *)ctsio); 9069 9070 return (CTL_RETVAL_COMPLETE); 9071 break; /* NOTREACHED */ 9072 } 9073 9074 /* 9075 * The first check is to make sure we're in bounds, the second 9076 * check is to catch wrap-around problems. If the lba + num blocks 9077 * is less than the lba, then we've wrapped around and the block 9078 * range is invalid anyway. 9079 */ 9080 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9081 || ((lba + num_blocks) < lba)) { 9082 ctl_set_lba_out_of_range(ctsio); 9083 ctl_done((union ctl_io *)ctsio); 9084 return (CTL_RETVAL_COMPLETE); 9085 } 9086 9087 /* 9088 * According to SBC-3, a transfer length of 0 is not an error. 9089 * Note that this cannot happen with WRITE(6) or READ(6), since 0 9090 * translates to 256 blocks for those commands. 9091 */ 9092 if (num_blocks == 0) { 9093 ctl_set_success(ctsio); 9094 ctl_done((union ctl_io *)ctsio); 9095 return (CTL_RETVAL_COMPLETE); 9096 } 9097 9098 /* Set FUA and/or DPO if caches are disabled. */ 9099 if (isread) { 9100 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9101 SCP_RCD) != 0) 9102 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 9103 } else { 9104 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9105 SCP_WCE) == 0) 9106 flags |= CTL_LLF_FUA; 9107 } 9108 9109 lbalen = (struct ctl_lba_len_flags *) 9110 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9111 lbalen->lba = lba; 9112 lbalen->len = num_blocks; 9113 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 9114 9115 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9116 ctsio->kern_rel_offset = 0; 9117 9118 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 9119 9120 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9121 9122 return (retval); 9123} 9124 9125static int 9126ctl_cnw_cont(union ctl_io *io) 9127{ 9128 struct ctl_scsiio *ctsio; 9129 struct ctl_lun *lun; 9130 struct ctl_lba_len_flags *lbalen; 9131 int retval; 9132 9133 ctsio = &io->scsiio; 9134 ctsio->io_hdr.status = CTL_STATUS_NONE; 9135 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 9136 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9137 lbalen = (struct ctl_lba_len_flags *) 9138 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9139 lbalen->flags &= ~CTL_LLF_COMPARE; 9140 lbalen->flags |= CTL_LLF_WRITE; 9141 9142 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 9143 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9144 return (retval); 9145} 9146 9147int 9148ctl_cnw(struct ctl_scsiio *ctsio) 9149{ 9150 struct ctl_lun *lun; 9151 struct ctl_lba_len_flags *lbalen; 9152 uint64_t lba; 9153 uint32_t num_blocks; 9154 int flags, retval; 9155 9156 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9157 9158 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 9159 9160 flags = 0; 9161 retval = CTL_RETVAL_COMPLETE; 9162 9163 switch (ctsio->cdb[0]) { 9164 case COMPARE_AND_WRITE: { 9165 struct scsi_compare_and_write *cdb; 9166 9167 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 9168 if (cdb->byte2 & SRW10_FUA) 9169 flags |= CTL_LLF_FUA; 9170 if (cdb->byte2 & SRW10_DPO) 9171 flags |= CTL_LLF_DPO; 9172 lba = scsi_8btou64(cdb->addr); 9173 num_blocks = cdb->length; 9174 break; 9175 } 9176 default: 9177 /* 9178 * We got a command we don't support. This shouldn't 9179 * happen, commands should be filtered out above us. 9180 */ 9181 ctl_set_invalid_opcode(ctsio); 9182 ctl_done((union ctl_io *)ctsio); 9183 9184 return (CTL_RETVAL_COMPLETE); 9185 break; /* NOTREACHED */ 9186 } 9187 9188 /* 9189 * The first check is to make sure we're in bounds, the second 9190 * check is to catch wrap-around problems. If the lba + num blocks 9191 * is less than the lba, then we've wrapped around and the block 9192 * range is invalid anyway. 9193 */ 9194 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9195 || ((lba + num_blocks) < lba)) { 9196 ctl_set_lba_out_of_range(ctsio); 9197 ctl_done((union ctl_io *)ctsio); 9198 return (CTL_RETVAL_COMPLETE); 9199 } 9200 9201 /* 9202 * According to SBC-3, a transfer length of 0 is not an error. 9203 */ 9204 if (num_blocks == 0) { 9205 ctl_set_success(ctsio); 9206 ctl_done((union ctl_io *)ctsio); 9207 return (CTL_RETVAL_COMPLETE); 9208 } 9209 9210 /* Set FUA if write cache is disabled. */ 9211 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9212 SCP_WCE) == 0) 9213 flags |= CTL_LLF_FUA; 9214 9215 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9216 ctsio->kern_rel_offset = 0; 9217 9218 /* 9219 * Set the IO_CONT flag, so that if this I/O gets passed to 9220 * ctl_data_submit_done(), it'll get passed back to 9221 * ctl_ctl_cnw_cont() for further processing. 9222 */ 9223 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9224 ctsio->io_cont = ctl_cnw_cont; 9225 9226 lbalen = (struct ctl_lba_len_flags *) 9227 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9228 lbalen->lba = lba; 9229 lbalen->len = num_blocks; 9230 lbalen->flags = CTL_LLF_COMPARE | flags; 9231 9232 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9233 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9234 return (retval); 9235} 9236 9237int 9238ctl_verify(struct ctl_scsiio *ctsio) 9239{ 9240 struct ctl_lun *lun; 9241 struct ctl_lba_len_flags *lbalen; 9242 uint64_t lba; 9243 uint32_t num_blocks; 9244 int bytchk, flags; 9245 int retval; 9246 9247 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9248 9249 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9250 9251 bytchk = 0; 9252 flags = CTL_LLF_FUA; 9253 retval = CTL_RETVAL_COMPLETE; 9254 9255 switch (ctsio->cdb[0]) { 9256 case VERIFY_10: { 9257 struct scsi_verify_10 *cdb; 9258 9259 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9260 if (cdb->byte2 & SVFY_BYTCHK) 9261 bytchk = 1; 9262 if (cdb->byte2 & SVFY_DPO) 9263 flags |= CTL_LLF_DPO; 9264 lba = scsi_4btoul(cdb->addr); 9265 num_blocks = scsi_2btoul(cdb->length); 9266 break; 9267 } 9268 case VERIFY_12: { 9269 struct scsi_verify_12 *cdb; 9270 9271 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9272 if (cdb->byte2 & SVFY_BYTCHK) 9273 bytchk = 1; 9274 if (cdb->byte2 & SVFY_DPO) 9275 flags |= CTL_LLF_DPO; 9276 lba = scsi_4btoul(cdb->addr); 9277 num_blocks = scsi_4btoul(cdb->length); 9278 break; 9279 } 9280 case VERIFY_16: { 9281 struct scsi_rw_16 *cdb; 9282 9283 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9284 if (cdb->byte2 & SVFY_BYTCHK) 9285 bytchk = 1; 9286 if (cdb->byte2 & SVFY_DPO) 9287 flags |= CTL_LLF_DPO; 9288 lba = scsi_8btou64(cdb->addr); 9289 num_blocks = scsi_4btoul(cdb->length); 9290 break; 9291 } 9292 default: 9293 /* 9294 * We got a command we don't support. This shouldn't 9295 * happen, commands should be filtered out above us. 9296 */ 9297 ctl_set_invalid_opcode(ctsio); 9298 ctl_done((union ctl_io *)ctsio); 9299 return (CTL_RETVAL_COMPLETE); 9300 } 9301 9302 /* 9303 * The first check is to make sure we're in bounds, the second 9304 * check is to catch wrap-around problems. If the lba + num blocks 9305 * is less than the lba, then we've wrapped around and the block 9306 * range is invalid anyway. 9307 */ 9308 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9309 || ((lba + num_blocks) < lba)) { 9310 ctl_set_lba_out_of_range(ctsio); 9311 ctl_done((union ctl_io *)ctsio); 9312 return (CTL_RETVAL_COMPLETE); 9313 } 9314 9315 /* 9316 * According to SBC-3, a transfer length of 0 is not an error. 9317 */ 9318 if (num_blocks == 0) { 9319 ctl_set_success(ctsio); 9320 ctl_done((union ctl_io *)ctsio); 9321 return (CTL_RETVAL_COMPLETE); 9322 } 9323 9324 lbalen = (struct ctl_lba_len_flags *) 9325 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9326 lbalen->lba = lba; 9327 lbalen->len = num_blocks; 9328 if (bytchk) { 9329 lbalen->flags = CTL_LLF_COMPARE | flags; 9330 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9331 } else { 9332 lbalen->flags = CTL_LLF_VERIFY | flags; 9333 ctsio->kern_total_len = 0; 9334 } 9335 ctsio->kern_rel_offset = 0; 9336 9337 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9338 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9339 return (retval); 9340} 9341 9342int 9343ctl_report_luns(struct ctl_scsiio *ctsio) 9344{ 9345 struct ctl_softc *softc = control_softc; 9346 struct scsi_report_luns *cdb; 9347 struct scsi_report_luns_data *lun_data; 9348 struct ctl_lun *lun, *request_lun; 9349 struct ctl_port *port; 9350 int num_luns, retval; 9351 uint32_t alloc_len, lun_datalen; 9352 int num_filled, well_known; 9353 uint32_t initidx, targ_lun_id, lun_id; 9354 9355 retval = CTL_RETVAL_COMPLETE; 9356 well_known = 0; 9357 9358 cdb = (struct scsi_report_luns *)ctsio->cdb; 9359 port = ctl_io_port(&ctsio->io_hdr); 9360 9361 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9362 9363 mtx_lock(&softc->ctl_lock); 9364 num_luns = 0; 9365 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 9366 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 9367 num_luns++; 9368 } 9369 mtx_unlock(&softc->ctl_lock); 9370 9371 switch (cdb->select_report) { 9372 case RPL_REPORT_DEFAULT: 9373 case RPL_REPORT_ALL: 9374 break; 9375 case RPL_REPORT_WELLKNOWN: 9376 well_known = 1; 9377 num_luns = 0; 9378 break; 9379 default: 9380 ctl_set_invalid_field(ctsio, 9381 /*sks_valid*/ 1, 9382 /*command*/ 1, 9383 /*field*/ 2, 9384 /*bit_valid*/ 0, 9385 /*bit*/ 0); 9386 ctl_done((union ctl_io *)ctsio); 9387 return (retval); 9388 break; /* NOTREACHED */ 9389 } 9390 9391 alloc_len = scsi_4btoul(cdb->length); 9392 /* 9393 * The initiator has to allocate at least 16 bytes for this request, 9394 * so he can at least get the header and the first LUN. Otherwise 9395 * we reject the request (per SPC-3 rev 14, section 6.21). 9396 */ 9397 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9398 sizeof(struct scsi_report_luns_lundata))) { 9399 ctl_set_invalid_field(ctsio, 9400 /*sks_valid*/ 1, 9401 /*command*/ 1, 9402 /*field*/ 6, 9403 /*bit_valid*/ 0, 9404 /*bit*/ 0); 9405 ctl_done((union ctl_io *)ctsio); 9406 return (retval); 9407 } 9408 9409 request_lun = (struct ctl_lun *) 9410 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9411 9412 lun_datalen = sizeof(*lun_data) + 9413 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9414 9415 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9416 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9417 ctsio->kern_sg_entries = 0; 9418 9419 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9420 9421 mtx_lock(&softc->ctl_lock); 9422 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9423 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9424 if (lun_id >= CTL_MAX_LUNS) 9425 continue; 9426 lun = softc->ctl_luns[lun_id]; 9427 if (lun == NULL) 9428 continue; 9429 9430 if (targ_lun_id <= 0xff) { 9431 /* 9432 * Peripheral addressing method, bus number 0. 9433 */ 9434 lun_data->luns[num_filled].lundata[0] = 9435 RPL_LUNDATA_ATYP_PERIPH; 9436 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9437 num_filled++; 9438 } else if (targ_lun_id <= 0x3fff) { 9439 /* 9440 * Flat addressing method. 9441 */ 9442 lun_data->luns[num_filled].lundata[0] = 9443 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9444 lun_data->luns[num_filled].lundata[1] = 9445 (targ_lun_id & 0xff); 9446 num_filled++; 9447 } else if (targ_lun_id <= 0xffffff) { 9448 /* 9449 * Extended flat addressing method. 9450 */ 9451 lun_data->luns[num_filled].lundata[0] = 9452 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9453 scsi_ulto3b(targ_lun_id, 9454 &lun_data->luns[num_filled].lundata[1]); 9455 num_filled++; 9456 } else { 9457 printf("ctl_report_luns: bogus LUN number %jd, " 9458 "skipping\n", (intmax_t)targ_lun_id); 9459 } 9460 /* 9461 * According to SPC-3, rev 14 section 6.21: 9462 * 9463 * "The execution of a REPORT LUNS command to any valid and 9464 * installed logical unit shall clear the REPORTED LUNS DATA 9465 * HAS CHANGED unit attention condition for all logical 9466 * units of that target with respect to the requesting 9467 * initiator. A valid and installed logical unit is one 9468 * having a PERIPHERAL QUALIFIER of 000b in the standard 9469 * INQUIRY data (see 6.4.2)." 9470 * 9471 * If request_lun is NULL, the LUN this report luns command 9472 * was issued to is either disabled or doesn't exist. In that 9473 * case, we shouldn't clear any pending lun change unit 9474 * attention. 9475 */ 9476 if (request_lun != NULL) { 9477 mtx_lock(&lun->lun_lock); 9478 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE); 9479 mtx_unlock(&lun->lun_lock); 9480 } 9481 } 9482 mtx_unlock(&softc->ctl_lock); 9483 9484 /* 9485 * It's quite possible that we've returned fewer LUNs than we allocated 9486 * space for. Trim it. 9487 */ 9488 lun_datalen = sizeof(*lun_data) + 9489 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9490 9491 if (lun_datalen < alloc_len) { 9492 ctsio->residual = alloc_len - lun_datalen; 9493 ctsio->kern_data_len = lun_datalen; 9494 ctsio->kern_total_len = lun_datalen; 9495 } else { 9496 ctsio->residual = 0; 9497 ctsio->kern_data_len = alloc_len; 9498 ctsio->kern_total_len = alloc_len; 9499 } 9500 ctsio->kern_data_resid = 0; 9501 ctsio->kern_rel_offset = 0; 9502 ctsio->kern_sg_entries = 0; 9503 9504 /* 9505 * We set this to the actual data length, regardless of how much 9506 * space we actually have to return results. If the user looks at 9507 * this value, he'll know whether or not he allocated enough space 9508 * and reissue the command if necessary. We don't support well 9509 * known logical units, so if the user asks for that, return none. 9510 */ 9511 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9512 9513 /* 9514 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9515 * this request. 9516 */ 9517 ctl_set_success(ctsio); 9518 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9519 ctsio->be_move_done = ctl_config_move_done; 9520 ctl_datamove((union ctl_io *)ctsio); 9521 return (retval); 9522} 9523 9524int 9525ctl_request_sense(struct ctl_scsiio *ctsio) 9526{ 9527 struct scsi_request_sense *cdb; 9528 struct scsi_sense_data *sense_ptr; 9529 struct ctl_softc *ctl_softc; 9530 struct ctl_lun *lun; 9531 uint32_t initidx; 9532 int have_error; 9533 scsi_sense_data_type sense_format; 9534 ctl_ua_type ua_type; 9535 9536 cdb = (struct scsi_request_sense *)ctsio->cdb; 9537 9538 ctl_softc = control_softc; 9539 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9540 9541 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9542 9543 /* 9544 * Determine which sense format the user wants. 9545 */ 9546 if (cdb->byte2 & SRS_DESC) 9547 sense_format = SSD_TYPE_DESC; 9548 else 9549 sense_format = SSD_TYPE_FIXED; 9550 9551 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9552 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9553 ctsio->kern_sg_entries = 0; 9554 9555 /* 9556 * struct scsi_sense_data, which is currently set to 256 bytes, is 9557 * larger than the largest allowed value for the length field in the 9558 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9559 */ 9560 ctsio->residual = 0; 9561 ctsio->kern_data_len = cdb->length; 9562 ctsio->kern_total_len = cdb->length; 9563 9564 ctsio->kern_data_resid = 0; 9565 ctsio->kern_rel_offset = 0; 9566 ctsio->kern_sg_entries = 0; 9567 9568 /* 9569 * If we don't have a LUN, we don't have any pending sense. 9570 */ 9571 if (lun == NULL) 9572 goto no_sense; 9573 9574 have_error = 0; 9575 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9576 /* 9577 * Check for pending sense, and then for pending unit attentions. 9578 * Pending sense gets returned first, then pending unit attentions. 9579 */ 9580 mtx_lock(&lun->lun_lock); 9581#ifdef CTL_WITH_CA 9582 if (ctl_is_set(lun->have_ca, initidx)) { 9583 scsi_sense_data_type stored_format; 9584 9585 /* 9586 * Check to see which sense format was used for the stored 9587 * sense data. 9588 */ 9589 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9590 9591 /* 9592 * If the user requested a different sense format than the 9593 * one we stored, then we need to convert it to the other 9594 * format. If we're going from descriptor to fixed format 9595 * sense data, we may lose things in translation, depending 9596 * on what options were used. 9597 * 9598 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9599 * for some reason we'll just copy it out as-is. 9600 */ 9601 if ((stored_format == SSD_TYPE_FIXED) 9602 && (sense_format == SSD_TYPE_DESC)) 9603 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9604 &lun->pending_sense[initidx], 9605 (struct scsi_sense_data_desc *)sense_ptr); 9606 else if ((stored_format == SSD_TYPE_DESC) 9607 && (sense_format == SSD_TYPE_FIXED)) 9608 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9609 &lun->pending_sense[initidx], 9610 (struct scsi_sense_data_fixed *)sense_ptr); 9611 else 9612 memcpy(sense_ptr, &lun->pending_sense[initidx], 9613 MIN(sizeof(*sense_ptr), 9614 sizeof(lun->pending_sense[initidx]))); 9615 9616 ctl_clear_mask(lun->have_ca, initidx); 9617 have_error = 1; 9618 } else 9619#endif 9620 { 9621 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9622 if (ua_type != CTL_UA_NONE) 9623 have_error = 1; 9624 if (ua_type == CTL_UA_LUN_CHANGE) { 9625 mtx_unlock(&lun->lun_lock); 9626 mtx_lock(&ctl_softc->ctl_lock); 9627 ctl_clear_ua(ctl_softc, initidx, ua_type); 9628 mtx_unlock(&ctl_softc->ctl_lock); 9629 mtx_lock(&lun->lun_lock); 9630 } 9631 9632 } 9633 mtx_unlock(&lun->lun_lock); 9634 9635 /* 9636 * We already have a pending error, return it. 9637 */ 9638 if (have_error != 0) { 9639 /* 9640 * We report the SCSI status as OK, since the status of the 9641 * request sense command itself is OK. 9642 * We report 0 for the sense length, because we aren't doing 9643 * autosense in this case. We're reporting sense as 9644 * parameter data. 9645 */ 9646 ctl_set_success(ctsio); 9647 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9648 ctsio->be_move_done = ctl_config_move_done; 9649 ctl_datamove((union ctl_io *)ctsio); 9650 return (CTL_RETVAL_COMPLETE); 9651 } 9652 9653no_sense: 9654 9655 /* 9656 * No sense information to report, so we report that everything is 9657 * okay. 9658 */ 9659 ctl_set_sense_data(sense_ptr, 9660 lun, 9661 sense_format, 9662 /*current_error*/ 1, 9663 /*sense_key*/ SSD_KEY_NO_SENSE, 9664 /*asc*/ 0x00, 9665 /*ascq*/ 0x00, 9666 SSD_ELEM_NONE); 9667 9668 /* 9669 * We report 0 for the sense length, because we aren't doing 9670 * autosense in this case. We're reporting sense as parameter data. 9671 */ 9672 ctl_set_success(ctsio); 9673 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9674 ctsio->be_move_done = ctl_config_move_done; 9675 ctl_datamove((union ctl_io *)ctsio); 9676 return (CTL_RETVAL_COMPLETE); 9677} 9678 9679int 9680ctl_tur(struct ctl_scsiio *ctsio) 9681{ 9682 9683 CTL_DEBUG_PRINT(("ctl_tur\n")); 9684 9685 ctl_set_success(ctsio); 9686 ctl_done((union ctl_io *)ctsio); 9687 9688 return (CTL_RETVAL_COMPLETE); 9689} 9690 9691#ifdef notyet 9692static int 9693ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9694{ 9695 9696} 9697#endif 9698 9699/* 9700 * SCSI VPD page 0x00, the Supported VPD Pages page. 9701 */ 9702static int 9703ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9704{ 9705 struct scsi_vpd_supported_pages *pages; 9706 int sup_page_size; 9707 struct ctl_lun *lun; 9708 int p; 9709 9710 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9711 9712 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9713 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9714 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9715 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9716 ctsio->kern_sg_entries = 0; 9717 9718 if (sup_page_size < alloc_len) { 9719 ctsio->residual = alloc_len - sup_page_size; 9720 ctsio->kern_data_len = sup_page_size; 9721 ctsio->kern_total_len = sup_page_size; 9722 } else { 9723 ctsio->residual = 0; 9724 ctsio->kern_data_len = alloc_len; 9725 ctsio->kern_total_len = alloc_len; 9726 } 9727 ctsio->kern_data_resid = 0; 9728 ctsio->kern_rel_offset = 0; 9729 ctsio->kern_sg_entries = 0; 9730 9731 /* 9732 * The control device is always connected. The disk device, on the 9733 * other hand, may not be online all the time. Need to change this 9734 * to figure out whether the disk device is actually online or not. 9735 */ 9736 if (lun != NULL) 9737 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9738 lun->be_lun->lun_type; 9739 else 9740 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9741 9742 p = 0; 9743 /* Supported VPD pages */ 9744 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9745 /* Serial Number */ 9746 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9747 /* Device Identification */ 9748 pages->page_list[p++] = SVPD_DEVICE_ID; 9749 /* Extended INQUIRY Data */ 9750 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9751 /* Mode Page Policy */ 9752 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9753 /* SCSI Ports */ 9754 pages->page_list[p++] = SVPD_SCSI_PORTS; 9755 /* Third-party Copy */ 9756 pages->page_list[p++] = SVPD_SCSI_TPC; 9757 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9758 /* Block limits */ 9759 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9760 /* Block Device Characteristics */ 9761 pages->page_list[p++] = SVPD_BDC; 9762 /* Logical Block Provisioning */ 9763 pages->page_list[p++] = SVPD_LBP; 9764 } 9765 pages->length = p; 9766 9767 ctl_set_success(ctsio); 9768 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9769 ctsio->be_move_done = ctl_config_move_done; 9770 ctl_datamove((union ctl_io *)ctsio); 9771 return (CTL_RETVAL_COMPLETE); 9772} 9773 9774/* 9775 * SCSI VPD page 0x80, the Unit Serial Number page. 9776 */ 9777static int 9778ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9779{ 9780 struct scsi_vpd_unit_serial_number *sn_ptr; 9781 struct ctl_lun *lun; 9782 int data_len; 9783 9784 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9785 9786 data_len = 4 + CTL_SN_LEN; 9787 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9788 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9789 if (data_len < alloc_len) { 9790 ctsio->residual = alloc_len - data_len; 9791 ctsio->kern_data_len = data_len; 9792 ctsio->kern_total_len = data_len; 9793 } else { 9794 ctsio->residual = 0; 9795 ctsio->kern_data_len = alloc_len; 9796 ctsio->kern_total_len = alloc_len; 9797 } 9798 ctsio->kern_data_resid = 0; 9799 ctsio->kern_rel_offset = 0; 9800 ctsio->kern_sg_entries = 0; 9801 9802 /* 9803 * The control device is always connected. The disk device, on the 9804 * other hand, may not be online all the time. Need to change this 9805 * to figure out whether the disk device is actually online or not. 9806 */ 9807 if (lun != NULL) 9808 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9809 lun->be_lun->lun_type; 9810 else 9811 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9812 9813 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9814 sn_ptr->length = CTL_SN_LEN; 9815 /* 9816 * If we don't have a LUN, we just leave the serial number as 9817 * all spaces. 9818 */ 9819 if (lun != NULL) { 9820 strncpy((char *)sn_ptr->serial_num, 9821 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9822 } else 9823 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9824 9825 ctl_set_success(ctsio); 9826 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9827 ctsio->be_move_done = ctl_config_move_done; 9828 ctl_datamove((union ctl_io *)ctsio); 9829 return (CTL_RETVAL_COMPLETE); 9830} 9831 9832 9833/* 9834 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9835 */ 9836static int 9837ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9838{ 9839 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9840 struct ctl_lun *lun; 9841 int data_len; 9842 9843 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9844 9845 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9846 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9847 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9848 ctsio->kern_sg_entries = 0; 9849 9850 if (data_len < alloc_len) { 9851 ctsio->residual = alloc_len - data_len; 9852 ctsio->kern_data_len = data_len; 9853 ctsio->kern_total_len = data_len; 9854 } else { 9855 ctsio->residual = 0; 9856 ctsio->kern_data_len = alloc_len; 9857 ctsio->kern_total_len = alloc_len; 9858 } 9859 ctsio->kern_data_resid = 0; 9860 ctsio->kern_rel_offset = 0; 9861 ctsio->kern_sg_entries = 0; 9862 9863 /* 9864 * The control device is always connected. The disk device, on the 9865 * other hand, may not be online all the time. 9866 */ 9867 if (lun != NULL) 9868 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9869 lun->be_lun->lun_type; 9870 else 9871 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9872 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9873 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9874 /* 9875 * We support head of queue, ordered and simple tags. 9876 */ 9877 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9878 /* 9879 * Volatile cache supported. 9880 */ 9881 eid_ptr->flags3 = SVPD_EID_V_SUP; 9882 9883 /* 9884 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9885 * attention for a particular IT nexus on all LUNs once we report 9886 * it to that nexus once. This bit is required as of SPC-4. 9887 */ 9888 eid_ptr->flags4 = SVPD_EID_LUICLT; 9889 9890 /* 9891 * XXX KDM in order to correctly answer this, we would need 9892 * information from the SIM to determine how much sense data it 9893 * can send. So this would really be a path inquiry field, most 9894 * likely. This can be set to a maximum of 252 according to SPC-4, 9895 * but the hardware may or may not be able to support that much. 9896 * 0 just means that the maximum sense data length is not reported. 9897 */ 9898 eid_ptr->max_sense_length = 0; 9899 9900 ctl_set_success(ctsio); 9901 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9902 ctsio->be_move_done = ctl_config_move_done; 9903 ctl_datamove((union ctl_io *)ctsio); 9904 return (CTL_RETVAL_COMPLETE); 9905} 9906 9907static int 9908ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9909{ 9910 struct scsi_vpd_mode_page_policy *mpp_ptr; 9911 struct ctl_lun *lun; 9912 int data_len; 9913 9914 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9915 9916 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9917 sizeof(struct scsi_vpd_mode_page_policy_descr); 9918 9919 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9920 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9921 ctsio->kern_sg_entries = 0; 9922 9923 if (data_len < alloc_len) { 9924 ctsio->residual = alloc_len - data_len; 9925 ctsio->kern_data_len = data_len; 9926 ctsio->kern_total_len = data_len; 9927 } else { 9928 ctsio->residual = 0; 9929 ctsio->kern_data_len = alloc_len; 9930 ctsio->kern_total_len = alloc_len; 9931 } 9932 ctsio->kern_data_resid = 0; 9933 ctsio->kern_rel_offset = 0; 9934 ctsio->kern_sg_entries = 0; 9935 9936 /* 9937 * The control device is always connected. The disk device, on the 9938 * other hand, may not be online all the time. 9939 */ 9940 if (lun != NULL) 9941 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9942 lun->be_lun->lun_type; 9943 else 9944 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9945 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9946 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9947 mpp_ptr->descr[0].page_code = 0x3f; 9948 mpp_ptr->descr[0].subpage_code = 0xff; 9949 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9950 9951 ctl_set_success(ctsio); 9952 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9953 ctsio->be_move_done = ctl_config_move_done; 9954 ctl_datamove((union ctl_io *)ctsio); 9955 return (CTL_RETVAL_COMPLETE); 9956} 9957 9958/* 9959 * SCSI VPD page 0x83, the Device Identification page. 9960 */ 9961static int 9962ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9963{ 9964 struct scsi_vpd_device_id *devid_ptr; 9965 struct scsi_vpd_id_descriptor *desc; 9966 struct ctl_softc *softc; 9967 struct ctl_lun *lun; 9968 struct ctl_port *port; 9969 int data_len; 9970 uint8_t proto; 9971 9972 softc = control_softc; 9973 9974 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9975 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9976 9977 data_len = sizeof(struct scsi_vpd_device_id) + 9978 sizeof(struct scsi_vpd_id_descriptor) + 9979 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9980 sizeof(struct scsi_vpd_id_descriptor) + 9981 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9982 if (lun && lun->lun_devid) 9983 data_len += lun->lun_devid->len; 9984 if (port->port_devid) 9985 data_len += port->port_devid->len; 9986 if (port->target_devid) 9987 data_len += port->target_devid->len; 9988 9989 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9990 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9991 ctsio->kern_sg_entries = 0; 9992 9993 if (data_len < alloc_len) { 9994 ctsio->residual = alloc_len - data_len; 9995 ctsio->kern_data_len = data_len; 9996 ctsio->kern_total_len = data_len; 9997 } else { 9998 ctsio->residual = 0; 9999 ctsio->kern_data_len = alloc_len; 10000 ctsio->kern_total_len = alloc_len; 10001 } 10002 ctsio->kern_data_resid = 0; 10003 ctsio->kern_rel_offset = 0; 10004 ctsio->kern_sg_entries = 0; 10005 10006 /* 10007 * The control device is always connected. The disk device, on the 10008 * other hand, may not be online all the time. 10009 */ 10010 if (lun != NULL) 10011 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10012 lun->be_lun->lun_type; 10013 else 10014 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10015 devid_ptr->page_code = SVPD_DEVICE_ID; 10016 scsi_ulto2b(data_len - 4, devid_ptr->length); 10017 10018 if (port->port_type == CTL_PORT_FC) 10019 proto = SCSI_PROTO_FC << 4; 10020 else if (port->port_type == CTL_PORT_ISCSI) 10021 proto = SCSI_PROTO_ISCSI << 4; 10022 else 10023 proto = SCSI_PROTO_SPI << 4; 10024 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 10025 10026 /* 10027 * We're using a LUN association here. i.e., this device ID is a 10028 * per-LUN identifier. 10029 */ 10030 if (lun && lun->lun_devid) { 10031 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 10032 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10033 lun->lun_devid->len); 10034 } 10035 10036 /* 10037 * This is for the WWPN which is a port association. 10038 */ 10039 if (port->port_devid) { 10040 memcpy(desc, port->port_devid->data, port->port_devid->len); 10041 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 10042 port->port_devid->len); 10043 } 10044 10045 /* 10046 * This is for the Relative Target Port(type 4h) identifier 10047 */ 10048 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10049 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10050 SVPD_ID_TYPE_RELTARG; 10051 desc->length = 4; 10052 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 10053 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10054 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 10055 10056 /* 10057 * This is for the Target Port Group(type 5h) identifier 10058 */ 10059 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 10060 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 10061 SVPD_ID_TYPE_TPORTGRP; 10062 desc->length = 4; 10063 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 10064 &desc->identifier[2]); 10065 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 10066 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 10067 10068 /* 10069 * This is for the Target identifier 10070 */ 10071 if (port->target_devid) { 10072 memcpy(desc, port->target_devid->data, port->target_devid->len); 10073 } 10074 10075 ctl_set_success(ctsio); 10076 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10077 ctsio->be_move_done = ctl_config_move_done; 10078 ctl_datamove((union ctl_io *)ctsio); 10079 return (CTL_RETVAL_COMPLETE); 10080} 10081 10082static int 10083ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 10084{ 10085 struct ctl_softc *softc = control_softc; 10086 struct scsi_vpd_scsi_ports *sp; 10087 struct scsi_vpd_port_designation *pd; 10088 struct scsi_vpd_port_designation_cont *pdc; 10089 struct ctl_lun *lun; 10090 struct ctl_port *port; 10091 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 10092 int num_target_port_groups; 10093 10094 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10095 10096 if (softc->is_single) 10097 num_target_port_groups = 1; 10098 else 10099 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 10100 num_target_ports = 0; 10101 iid_len = 0; 10102 id_len = 0; 10103 mtx_lock(&softc->ctl_lock); 10104 STAILQ_FOREACH(port, &softc->port_list, links) { 10105 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10106 continue; 10107 if (lun != NULL && 10108 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10109 continue; 10110 num_target_ports++; 10111 if (port->init_devid) 10112 iid_len += port->init_devid->len; 10113 if (port->port_devid) 10114 id_len += port->port_devid->len; 10115 } 10116 mtx_unlock(&softc->ctl_lock); 10117 10118 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 10119 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 10120 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 10121 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10122 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 10123 ctsio->kern_sg_entries = 0; 10124 10125 if (data_len < alloc_len) { 10126 ctsio->residual = alloc_len - data_len; 10127 ctsio->kern_data_len = data_len; 10128 ctsio->kern_total_len = data_len; 10129 } else { 10130 ctsio->residual = 0; 10131 ctsio->kern_data_len = alloc_len; 10132 ctsio->kern_total_len = alloc_len; 10133 } 10134 ctsio->kern_data_resid = 0; 10135 ctsio->kern_rel_offset = 0; 10136 ctsio->kern_sg_entries = 0; 10137 10138 /* 10139 * The control device is always connected. The disk device, on the 10140 * other hand, may not be online all the time. Need to change this 10141 * to figure out whether the disk device is actually online or not. 10142 */ 10143 if (lun != NULL) 10144 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 10145 lun->be_lun->lun_type; 10146 else 10147 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10148 10149 sp->page_code = SVPD_SCSI_PORTS; 10150 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 10151 sp->page_length); 10152 pd = &sp->design[0]; 10153 10154 mtx_lock(&softc->ctl_lock); 10155 pg = softc->port_offset / CTL_MAX_PORTS; 10156 for (g = 0; g < num_target_port_groups; g++) { 10157 STAILQ_FOREACH(port, &softc->port_list, links) { 10158 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 10159 continue; 10160 if (lun != NULL && 10161 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 10162 continue; 10163 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 10164 scsi_ulto2b(p, pd->relative_port_id); 10165 if (port->init_devid && g == pg) { 10166 iid_len = port->init_devid->len; 10167 memcpy(pd->initiator_transportid, 10168 port->init_devid->data, port->init_devid->len); 10169 } else 10170 iid_len = 0; 10171 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 10172 pdc = (struct scsi_vpd_port_designation_cont *) 10173 (&pd->initiator_transportid[iid_len]); 10174 if (port->port_devid && g == pg) { 10175 id_len = port->port_devid->len; 10176 memcpy(pdc->target_port_descriptors, 10177 port->port_devid->data, port->port_devid->len); 10178 } else 10179 id_len = 0; 10180 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 10181 pd = (struct scsi_vpd_port_designation *) 10182 ((uint8_t *)pdc->target_port_descriptors + id_len); 10183 } 10184 } 10185 mtx_unlock(&softc->ctl_lock); 10186 10187 ctl_set_success(ctsio); 10188 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10189 ctsio->be_move_done = ctl_config_move_done; 10190 ctl_datamove((union ctl_io *)ctsio); 10191 return (CTL_RETVAL_COMPLETE); 10192} 10193 10194static int 10195ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 10196{ 10197 struct scsi_vpd_block_limits *bl_ptr; 10198 struct ctl_lun *lun; 10199 int bs; 10200 10201 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10202 10203 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 10204 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 10205 ctsio->kern_sg_entries = 0; 10206 10207 if (sizeof(*bl_ptr) < alloc_len) { 10208 ctsio->residual = alloc_len - sizeof(*bl_ptr); 10209 ctsio->kern_data_len = sizeof(*bl_ptr); 10210 ctsio->kern_total_len = sizeof(*bl_ptr); 10211 } else { 10212 ctsio->residual = 0; 10213 ctsio->kern_data_len = alloc_len; 10214 ctsio->kern_total_len = alloc_len; 10215 } 10216 ctsio->kern_data_resid = 0; 10217 ctsio->kern_rel_offset = 0; 10218 ctsio->kern_sg_entries = 0; 10219 10220 /* 10221 * The control device is always connected. The disk device, on the 10222 * other hand, may not be online all the time. Need to change this 10223 * to figure out whether the disk device is actually online or not. 10224 */ 10225 if (lun != NULL) 10226 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10227 lun->be_lun->lun_type; 10228 else 10229 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10230 10231 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 10232 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 10233 bl_ptr->max_cmp_write_len = 0xff; 10234 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10235 if (lun != NULL) { 10236 bs = lun->be_lun->blocksize; 10237 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 10238 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10239 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10240 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10241 if (lun->be_lun->ublockexp != 0) { 10242 scsi_ulto4b((1 << lun->be_lun->ublockexp), 10243 bl_ptr->opt_unmap_grain); 10244 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 10245 bl_ptr->unmap_grain_align); 10246 } 10247 } 10248 scsi_ulto4b(lun->be_lun->atomicblock, 10249 bl_ptr->max_atomic_transfer_length); 10250 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10251 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10252 } 10253 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10254 10255 ctl_set_success(ctsio); 10256 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10257 ctsio->be_move_done = ctl_config_move_done; 10258 ctl_datamove((union ctl_io *)ctsio); 10259 return (CTL_RETVAL_COMPLETE); 10260} 10261 10262static int 10263ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10264{ 10265 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10266 struct ctl_lun *lun; 10267 const char *value; 10268 u_int i; 10269 10270 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10271 10272 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10273 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10274 ctsio->kern_sg_entries = 0; 10275 10276 if (sizeof(*bdc_ptr) < alloc_len) { 10277 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10278 ctsio->kern_data_len = sizeof(*bdc_ptr); 10279 ctsio->kern_total_len = sizeof(*bdc_ptr); 10280 } else { 10281 ctsio->residual = 0; 10282 ctsio->kern_data_len = alloc_len; 10283 ctsio->kern_total_len = alloc_len; 10284 } 10285 ctsio->kern_data_resid = 0; 10286 ctsio->kern_rel_offset = 0; 10287 ctsio->kern_sg_entries = 0; 10288 10289 /* 10290 * The control device is always connected. The disk device, on the 10291 * other hand, may not be online all the time. Need to change this 10292 * to figure out whether the disk device is actually online or not. 10293 */ 10294 if (lun != NULL) 10295 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10296 lun->be_lun->lun_type; 10297 else 10298 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10299 bdc_ptr->page_code = SVPD_BDC; 10300 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10301 if (lun != NULL && 10302 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10303 i = strtol(value, NULL, 0); 10304 else 10305 i = CTL_DEFAULT_ROTATION_RATE; 10306 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10307 if (lun != NULL && 10308 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10309 i = strtol(value, NULL, 0); 10310 else 10311 i = 0; 10312 bdc_ptr->wab_wac_ff = (i & 0x0f); 10313 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10314 10315 ctl_set_success(ctsio); 10316 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10317 ctsio->be_move_done = ctl_config_move_done; 10318 ctl_datamove((union ctl_io *)ctsio); 10319 return (CTL_RETVAL_COMPLETE); 10320} 10321 10322static int 10323ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10324{ 10325 struct scsi_vpd_logical_block_prov *lbp_ptr; 10326 struct ctl_lun *lun; 10327 10328 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10329 10330 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10331 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10332 ctsio->kern_sg_entries = 0; 10333 10334 if (sizeof(*lbp_ptr) < alloc_len) { 10335 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10336 ctsio->kern_data_len = sizeof(*lbp_ptr); 10337 ctsio->kern_total_len = sizeof(*lbp_ptr); 10338 } else { 10339 ctsio->residual = 0; 10340 ctsio->kern_data_len = alloc_len; 10341 ctsio->kern_total_len = alloc_len; 10342 } 10343 ctsio->kern_data_resid = 0; 10344 ctsio->kern_rel_offset = 0; 10345 ctsio->kern_sg_entries = 0; 10346 10347 /* 10348 * The control device is always connected. The disk device, on the 10349 * other hand, may not be online all the time. Need to change this 10350 * to figure out whether the disk device is actually online or not. 10351 */ 10352 if (lun != NULL) 10353 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10354 lun->be_lun->lun_type; 10355 else 10356 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10357 10358 lbp_ptr->page_code = SVPD_LBP; 10359 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10360 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10361 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10362 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10363 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10364 lbp_ptr->prov_type = SVPD_LBP_THIN; 10365 } 10366 10367 ctl_set_success(ctsio); 10368 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10369 ctsio->be_move_done = ctl_config_move_done; 10370 ctl_datamove((union ctl_io *)ctsio); 10371 return (CTL_RETVAL_COMPLETE); 10372} 10373 10374/* 10375 * INQUIRY with the EVPD bit set. 10376 */ 10377static int 10378ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10379{ 10380 struct ctl_lun *lun; 10381 struct scsi_inquiry *cdb; 10382 int alloc_len, retval; 10383 10384 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10385 cdb = (struct scsi_inquiry *)ctsio->cdb; 10386 alloc_len = scsi_2btoul(cdb->length); 10387 10388 switch (cdb->page_code) { 10389 case SVPD_SUPPORTED_PAGES: 10390 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10391 break; 10392 case SVPD_UNIT_SERIAL_NUMBER: 10393 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10394 break; 10395 case SVPD_DEVICE_ID: 10396 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10397 break; 10398 case SVPD_EXTENDED_INQUIRY_DATA: 10399 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10400 break; 10401 case SVPD_MODE_PAGE_POLICY: 10402 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10403 break; 10404 case SVPD_SCSI_PORTS: 10405 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10406 break; 10407 case SVPD_SCSI_TPC: 10408 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10409 break; 10410 case SVPD_BLOCK_LIMITS: 10411 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10412 goto err; 10413 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10414 break; 10415 case SVPD_BDC: 10416 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10417 goto err; 10418 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10419 break; 10420 case SVPD_LBP: 10421 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10422 goto err; 10423 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10424 break; 10425 default: 10426err: 10427 ctl_set_invalid_field(ctsio, 10428 /*sks_valid*/ 1, 10429 /*command*/ 1, 10430 /*field*/ 2, 10431 /*bit_valid*/ 0, 10432 /*bit*/ 0); 10433 ctl_done((union ctl_io *)ctsio); 10434 retval = CTL_RETVAL_COMPLETE; 10435 break; 10436 } 10437 10438 return (retval); 10439} 10440 10441/* 10442 * Standard INQUIRY data. 10443 */ 10444static int 10445ctl_inquiry_std(struct ctl_scsiio *ctsio) 10446{ 10447 struct scsi_inquiry_data *inq_ptr; 10448 struct scsi_inquiry *cdb; 10449 struct ctl_softc *softc; 10450 struct ctl_lun *lun; 10451 char *val; 10452 uint32_t alloc_len, data_len; 10453 ctl_port_type port_type; 10454 10455 softc = control_softc; 10456 10457 /* 10458 * Figure out whether we're talking to a Fibre Channel port or not. 10459 * We treat the ioctl front end, and any SCSI adapters, as packetized 10460 * SCSI front ends. 10461 */ 10462 port_type = softc->ctl_ports[ 10463 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10464 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10465 port_type = CTL_PORT_SCSI; 10466 10467 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10468 cdb = (struct scsi_inquiry *)ctsio->cdb; 10469 alloc_len = scsi_2btoul(cdb->length); 10470 10471 /* 10472 * We malloc the full inquiry data size here and fill it 10473 * in. If the user only asks for less, we'll give him 10474 * that much. 10475 */ 10476 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10477 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10478 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10479 ctsio->kern_sg_entries = 0; 10480 ctsio->kern_data_resid = 0; 10481 ctsio->kern_rel_offset = 0; 10482 10483 if (data_len < alloc_len) { 10484 ctsio->residual = alloc_len - data_len; 10485 ctsio->kern_data_len = data_len; 10486 ctsio->kern_total_len = data_len; 10487 } else { 10488 ctsio->residual = 0; 10489 ctsio->kern_data_len = alloc_len; 10490 ctsio->kern_total_len = alloc_len; 10491 } 10492 10493 /* 10494 * If we have a LUN configured, report it as connected. Otherwise, 10495 * report that it is offline or no device is supported, depending 10496 * on the value of inquiry_pq_no_lun. 10497 * 10498 * According to the spec (SPC-4 r34), the peripheral qualifier 10499 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10500 * 10501 * "A peripheral device having the specified peripheral device type 10502 * is not connected to this logical unit. However, the device 10503 * server is capable of supporting the specified peripheral device 10504 * type on this logical unit." 10505 * 10506 * According to the same spec, the peripheral qualifier 10507 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10508 * 10509 * "The device server is not capable of supporting a peripheral 10510 * device on this logical unit. For this peripheral qualifier the 10511 * peripheral device type shall be set to 1Fh. All other peripheral 10512 * device type values are reserved for this peripheral qualifier." 10513 * 10514 * Given the text, it would seem that we probably want to report that 10515 * the LUN is offline here. There is no LUN connected, but we can 10516 * support a LUN at the given LUN number. 10517 * 10518 * In the real world, though, it sounds like things are a little 10519 * different: 10520 * 10521 * - Linux, when presented with a LUN with the offline peripheral 10522 * qualifier, will create an sg driver instance for it. So when 10523 * you attach it to CTL, you wind up with a ton of sg driver 10524 * instances. (One for every LUN that Linux bothered to probe.) 10525 * Linux does this despite the fact that it issues a REPORT LUNs 10526 * to LUN 0 to get the inventory of supported LUNs. 10527 * 10528 * - There is other anecdotal evidence (from Emulex folks) about 10529 * arrays that use the offline peripheral qualifier for LUNs that 10530 * are on the "passive" path in an active/passive array. 10531 * 10532 * So the solution is provide a hopefully reasonable default 10533 * (return bad/no LUN) and allow the user to change the behavior 10534 * with a tunable/sysctl variable. 10535 */ 10536 if (lun != NULL) 10537 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10538 lun->be_lun->lun_type; 10539 else if (softc->inquiry_pq_no_lun == 0) 10540 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10541 else 10542 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10543 10544 /* RMB in byte 2 is 0 */ 10545 inq_ptr->version = SCSI_REV_SPC4; 10546 10547 /* 10548 * According to SAM-3, even if a device only supports a single 10549 * level of LUN addressing, it should still set the HISUP bit: 10550 * 10551 * 4.9.1 Logical unit numbers overview 10552 * 10553 * All logical unit number formats described in this standard are 10554 * hierarchical in structure even when only a single level in that 10555 * hierarchy is used. The HISUP bit shall be set to one in the 10556 * standard INQUIRY data (see SPC-2) when any logical unit number 10557 * format described in this standard is used. Non-hierarchical 10558 * formats are outside the scope of this standard. 10559 * 10560 * Therefore we set the HiSup bit here. 10561 * 10562 * The reponse format is 2, per SPC-3. 10563 */ 10564 inq_ptr->response_format = SID_HiSup | 2; 10565 10566 inq_ptr->additional_length = data_len - 10567 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10568 CTL_DEBUG_PRINT(("additional_length = %d\n", 10569 inq_ptr->additional_length)); 10570 10571 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10572 /* 16 bit addressing */ 10573 if (port_type == CTL_PORT_SCSI) 10574 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10575 /* XXX set the SID_MultiP bit here if we're actually going to 10576 respond on multiple ports */ 10577 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10578 10579 /* 16 bit data bus, synchronous transfers */ 10580 if (port_type == CTL_PORT_SCSI) 10581 inq_ptr->flags = SID_WBus16 | SID_Sync; 10582 /* 10583 * XXX KDM do we want to support tagged queueing on the control 10584 * device at all? 10585 */ 10586 if ((lun == NULL) 10587 || (lun->be_lun->lun_type != T_PROCESSOR)) 10588 inq_ptr->flags |= SID_CmdQue; 10589 /* 10590 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10591 * We have 8 bytes for the vendor name, and 16 bytes for the device 10592 * name and 4 bytes for the revision. 10593 */ 10594 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10595 "vendor")) == NULL) { 10596 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10597 } else { 10598 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10599 strncpy(inq_ptr->vendor, val, 10600 min(sizeof(inq_ptr->vendor), strlen(val))); 10601 } 10602 if (lun == NULL) { 10603 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10604 sizeof(inq_ptr->product)); 10605 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10606 switch (lun->be_lun->lun_type) { 10607 case T_DIRECT: 10608 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10609 sizeof(inq_ptr->product)); 10610 break; 10611 case T_PROCESSOR: 10612 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10613 sizeof(inq_ptr->product)); 10614 break; 10615 default: 10616 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10617 sizeof(inq_ptr->product)); 10618 break; 10619 } 10620 } else { 10621 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10622 strncpy(inq_ptr->product, val, 10623 min(sizeof(inq_ptr->product), strlen(val))); 10624 } 10625 10626 /* 10627 * XXX make this a macro somewhere so it automatically gets 10628 * incremented when we make changes. 10629 */ 10630 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10631 "revision")) == NULL) { 10632 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10633 } else { 10634 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10635 strncpy(inq_ptr->revision, val, 10636 min(sizeof(inq_ptr->revision), strlen(val))); 10637 } 10638 10639 /* 10640 * For parallel SCSI, we support double transition and single 10641 * transition clocking. We also support QAS (Quick Arbitration 10642 * and Selection) and Information Unit transfers on both the 10643 * control and array devices. 10644 */ 10645 if (port_type == CTL_PORT_SCSI) 10646 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10647 SID_SPI_IUS; 10648 10649 /* SAM-5 (no version claimed) */ 10650 scsi_ulto2b(0x00A0, inq_ptr->version1); 10651 /* SPC-4 (no version claimed) */ 10652 scsi_ulto2b(0x0460, inq_ptr->version2); 10653 if (port_type == CTL_PORT_FC) { 10654 /* FCP-2 ANSI INCITS.350:2003 */ 10655 scsi_ulto2b(0x0917, inq_ptr->version3); 10656 } else if (port_type == CTL_PORT_SCSI) { 10657 /* SPI-4 ANSI INCITS.362:200x */ 10658 scsi_ulto2b(0x0B56, inq_ptr->version3); 10659 } else if (port_type == CTL_PORT_ISCSI) { 10660 /* iSCSI (no version claimed) */ 10661 scsi_ulto2b(0x0960, inq_ptr->version3); 10662 } else if (port_type == CTL_PORT_SAS) { 10663 /* SAS (no version claimed) */ 10664 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10665 } 10666 10667 if (lun == NULL) { 10668 /* SBC-4 (no version claimed) */ 10669 scsi_ulto2b(0x0600, inq_ptr->version4); 10670 } else { 10671 switch (lun->be_lun->lun_type) { 10672 case T_DIRECT: 10673 /* SBC-4 (no version claimed) */ 10674 scsi_ulto2b(0x0600, inq_ptr->version4); 10675 break; 10676 case T_PROCESSOR: 10677 default: 10678 break; 10679 } 10680 } 10681 10682 ctl_set_success(ctsio); 10683 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10684 ctsio->be_move_done = ctl_config_move_done; 10685 ctl_datamove((union ctl_io *)ctsio); 10686 return (CTL_RETVAL_COMPLETE); 10687} 10688 10689int 10690ctl_inquiry(struct ctl_scsiio *ctsio) 10691{ 10692 struct scsi_inquiry *cdb; 10693 int retval; 10694 10695 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10696 10697 cdb = (struct scsi_inquiry *)ctsio->cdb; 10698 if (cdb->byte2 & SI_EVPD) 10699 retval = ctl_inquiry_evpd(ctsio); 10700 else if (cdb->page_code == 0) 10701 retval = ctl_inquiry_std(ctsio); 10702 else { 10703 ctl_set_invalid_field(ctsio, 10704 /*sks_valid*/ 1, 10705 /*command*/ 1, 10706 /*field*/ 2, 10707 /*bit_valid*/ 0, 10708 /*bit*/ 0); 10709 ctl_done((union ctl_io *)ctsio); 10710 return (CTL_RETVAL_COMPLETE); 10711 } 10712 10713 return (retval); 10714} 10715 10716/* 10717 * For known CDB types, parse the LBA and length. 10718 */ 10719static int 10720ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10721{ 10722 if (io->io_hdr.io_type != CTL_IO_SCSI) 10723 return (1); 10724 10725 switch (io->scsiio.cdb[0]) { 10726 case COMPARE_AND_WRITE: { 10727 struct scsi_compare_and_write *cdb; 10728 10729 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10730 10731 *lba = scsi_8btou64(cdb->addr); 10732 *len = cdb->length; 10733 break; 10734 } 10735 case READ_6: 10736 case WRITE_6: { 10737 struct scsi_rw_6 *cdb; 10738 10739 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10740 10741 *lba = scsi_3btoul(cdb->addr); 10742 /* only 5 bits are valid in the most significant address byte */ 10743 *lba &= 0x1fffff; 10744 *len = cdb->length; 10745 break; 10746 } 10747 case READ_10: 10748 case WRITE_10: { 10749 struct scsi_rw_10 *cdb; 10750 10751 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10752 10753 *lba = scsi_4btoul(cdb->addr); 10754 *len = scsi_2btoul(cdb->length); 10755 break; 10756 } 10757 case WRITE_VERIFY_10: { 10758 struct scsi_write_verify_10 *cdb; 10759 10760 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10761 10762 *lba = scsi_4btoul(cdb->addr); 10763 *len = scsi_2btoul(cdb->length); 10764 break; 10765 } 10766 case READ_12: 10767 case WRITE_12: { 10768 struct scsi_rw_12 *cdb; 10769 10770 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10771 10772 *lba = scsi_4btoul(cdb->addr); 10773 *len = scsi_4btoul(cdb->length); 10774 break; 10775 } 10776 case WRITE_VERIFY_12: { 10777 struct scsi_write_verify_12 *cdb; 10778 10779 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10780 10781 *lba = scsi_4btoul(cdb->addr); 10782 *len = scsi_4btoul(cdb->length); 10783 break; 10784 } 10785 case READ_16: 10786 case WRITE_16: 10787 case WRITE_ATOMIC_16: { 10788 struct scsi_rw_16 *cdb; 10789 10790 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10791 10792 *lba = scsi_8btou64(cdb->addr); 10793 *len = scsi_4btoul(cdb->length); 10794 break; 10795 } 10796 case WRITE_VERIFY_16: { 10797 struct scsi_write_verify_16 *cdb; 10798 10799 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10800 10801 *lba = scsi_8btou64(cdb->addr); 10802 *len = scsi_4btoul(cdb->length); 10803 break; 10804 } 10805 case WRITE_SAME_10: { 10806 struct scsi_write_same_10 *cdb; 10807 10808 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10809 10810 *lba = scsi_4btoul(cdb->addr); 10811 *len = scsi_2btoul(cdb->length); 10812 break; 10813 } 10814 case WRITE_SAME_16: { 10815 struct scsi_write_same_16 *cdb; 10816 10817 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10818 10819 *lba = scsi_8btou64(cdb->addr); 10820 *len = scsi_4btoul(cdb->length); 10821 break; 10822 } 10823 case VERIFY_10: { 10824 struct scsi_verify_10 *cdb; 10825 10826 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10827 10828 *lba = scsi_4btoul(cdb->addr); 10829 *len = scsi_2btoul(cdb->length); 10830 break; 10831 } 10832 case VERIFY_12: { 10833 struct scsi_verify_12 *cdb; 10834 10835 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10836 10837 *lba = scsi_4btoul(cdb->addr); 10838 *len = scsi_4btoul(cdb->length); 10839 break; 10840 } 10841 case VERIFY_16: { 10842 struct scsi_verify_16 *cdb; 10843 10844 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10845 10846 *lba = scsi_8btou64(cdb->addr); 10847 *len = scsi_4btoul(cdb->length); 10848 break; 10849 } 10850 case UNMAP: { 10851 *lba = 0; 10852 *len = UINT64_MAX; 10853 break; 10854 } 10855 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10856 struct scsi_get_lba_status *cdb; 10857 10858 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10859 *lba = scsi_8btou64(cdb->addr); 10860 *len = UINT32_MAX; 10861 break; 10862 } 10863 default: 10864 return (1); 10865 break; /* NOTREACHED */ 10866 } 10867 10868 return (0); 10869} 10870 10871static ctl_action 10872ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10873 bool seq) 10874{ 10875 uint64_t endlba1, endlba2; 10876 10877 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10878 endlba2 = lba2 + len2 - 1; 10879 10880 if ((endlba1 < lba2) || (endlba2 < lba1)) 10881 return (CTL_ACTION_PASS); 10882 else 10883 return (CTL_ACTION_BLOCK); 10884} 10885 10886static int 10887ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10888{ 10889 struct ctl_ptr_len_flags *ptrlen; 10890 struct scsi_unmap_desc *buf, *end, *range; 10891 uint64_t lba; 10892 uint32_t len; 10893 10894 /* If not UNMAP -- go other way. */ 10895 if (io->io_hdr.io_type != CTL_IO_SCSI || 10896 io->scsiio.cdb[0] != UNMAP) 10897 return (CTL_ACTION_ERROR); 10898 10899 /* If UNMAP without data -- block and wait for data. */ 10900 ptrlen = (struct ctl_ptr_len_flags *) 10901 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10902 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10903 ptrlen->ptr == NULL) 10904 return (CTL_ACTION_BLOCK); 10905 10906 /* UNMAP with data -- check for collision. */ 10907 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10908 end = buf + ptrlen->len / sizeof(*buf); 10909 for (range = buf; range < end; range++) { 10910 lba = scsi_8btou64(range->lba); 10911 len = scsi_4btoul(range->length); 10912 if ((lba < lba2 + len2) && (lba + len > lba2)) 10913 return (CTL_ACTION_BLOCK); 10914 } 10915 return (CTL_ACTION_PASS); 10916} 10917 10918static ctl_action 10919ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10920{ 10921 uint64_t lba1, lba2; 10922 uint64_t len1, len2; 10923 int retval; 10924 10925 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10926 return (CTL_ACTION_ERROR); 10927 10928 retval = ctl_extent_check_unmap(io1, lba2, len2); 10929 if (retval != CTL_ACTION_ERROR) 10930 return (retval); 10931 10932 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10933 return (CTL_ACTION_ERROR); 10934 10935 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10936} 10937 10938static ctl_action 10939ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10940{ 10941 uint64_t lba1, lba2; 10942 uint64_t len1, len2; 10943 10944 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10945 return (CTL_ACTION_ERROR); 10946 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10947 return (CTL_ACTION_ERROR); 10948 10949 if (lba1 + len1 == lba2) 10950 return (CTL_ACTION_BLOCK); 10951 return (CTL_ACTION_PASS); 10952} 10953 10954static ctl_action 10955ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10956 union ctl_io *ooa_io) 10957{ 10958 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10959 ctl_serialize_action *serialize_row; 10960 10961 /* 10962 * The initiator attempted multiple untagged commands at the same 10963 * time. Can't do that. 10964 */ 10965 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10966 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10967 && ((pending_io->io_hdr.nexus.targ_port == 10968 ooa_io->io_hdr.nexus.targ_port) 10969 && (pending_io->io_hdr.nexus.initid.id == 10970 ooa_io->io_hdr.nexus.initid.id)) 10971 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10972 CTL_FLAG_STATUS_SENT)) == 0)) 10973 return (CTL_ACTION_OVERLAP); 10974 10975 /* 10976 * The initiator attempted to send multiple tagged commands with 10977 * the same ID. (It's fine if different initiators have the same 10978 * tag ID.) 10979 * 10980 * Even if all of those conditions are true, we don't kill the I/O 10981 * if the command ahead of us has been aborted. We won't end up 10982 * sending it to the FETD, and it's perfectly legal to resend a 10983 * command with the same tag number as long as the previous 10984 * instance of this tag number has been aborted somehow. 10985 */ 10986 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10987 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10988 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10989 && ((pending_io->io_hdr.nexus.targ_port == 10990 ooa_io->io_hdr.nexus.targ_port) 10991 && (pending_io->io_hdr.nexus.initid.id == 10992 ooa_io->io_hdr.nexus.initid.id)) 10993 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10994 CTL_FLAG_STATUS_SENT)) == 0)) 10995 return (CTL_ACTION_OVERLAP_TAG); 10996 10997 /* 10998 * If we get a head of queue tag, SAM-3 says that we should 10999 * immediately execute it. 11000 * 11001 * What happens if this command would normally block for some other 11002 * reason? e.g. a request sense with a head of queue tag 11003 * immediately after a write. Normally that would block, but this 11004 * will result in its getting executed immediately... 11005 * 11006 * We currently return "pass" instead of "skip", so we'll end up 11007 * going through the rest of the queue to check for overlapped tags. 11008 * 11009 * XXX KDM check for other types of blockage first?? 11010 */ 11011 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11012 return (CTL_ACTION_PASS); 11013 11014 /* 11015 * Ordered tags have to block until all items ahead of them 11016 * have completed. If we get called with an ordered tag, we always 11017 * block, if something else is ahead of us in the queue. 11018 */ 11019 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 11020 return (CTL_ACTION_BLOCK); 11021 11022 /* 11023 * Simple tags get blocked until all head of queue and ordered tags 11024 * ahead of them have completed. I'm lumping untagged commands in 11025 * with simple tags here. XXX KDM is that the right thing to do? 11026 */ 11027 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11028 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11029 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11030 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11031 return (CTL_ACTION_BLOCK); 11032 11033 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11034 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11035 11036 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11037 11038 switch (serialize_row[pending_entry->seridx]) { 11039 case CTL_SER_BLOCK: 11040 return (CTL_ACTION_BLOCK); 11041 case CTL_SER_EXTENT: 11042 return (ctl_extent_check(ooa_io, pending_io, 11043 (lun->serseq == CTL_LUN_SERSEQ_ON))); 11044 case CTL_SER_EXTENTOPT: 11045 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11046 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11047 return (ctl_extent_check(ooa_io, pending_io, 11048 (lun->serseq == CTL_LUN_SERSEQ_ON))); 11049 return (CTL_ACTION_PASS); 11050 case CTL_SER_EXTENTSEQ: 11051 if (lun->serseq != CTL_LUN_SERSEQ_OFF) 11052 return (ctl_extent_check_seq(ooa_io, pending_io)); 11053 return (CTL_ACTION_PASS); 11054 case CTL_SER_PASS: 11055 return (CTL_ACTION_PASS); 11056 case CTL_SER_BLOCKOPT: 11057 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11058 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11059 return (CTL_ACTION_BLOCK); 11060 return (CTL_ACTION_PASS); 11061 case CTL_SER_SKIP: 11062 return (CTL_ACTION_SKIP); 11063 default: 11064 panic("invalid serialization value %d", 11065 serialize_row[pending_entry->seridx]); 11066 } 11067 11068 return (CTL_ACTION_ERROR); 11069} 11070 11071/* 11072 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11073 * Assumptions: 11074 * - pending_io is generally either incoming, or on the blocked queue 11075 * - starting I/O is the I/O we want to start the check with. 11076 */ 11077static ctl_action 11078ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11079 union ctl_io *starting_io) 11080{ 11081 union ctl_io *ooa_io; 11082 ctl_action action; 11083 11084 mtx_assert(&lun->lun_lock, MA_OWNED); 11085 11086 /* 11087 * Run back along the OOA queue, starting with the current 11088 * blocked I/O and going through every I/O before it on the 11089 * queue. If starting_io is NULL, we'll just end up returning 11090 * CTL_ACTION_PASS. 11091 */ 11092 for (ooa_io = starting_io; ooa_io != NULL; 11093 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11094 ooa_links)){ 11095 11096 /* 11097 * This routine just checks to see whether 11098 * cur_blocked is blocked by ooa_io, which is ahead 11099 * of it in the queue. It doesn't queue/dequeue 11100 * cur_blocked. 11101 */ 11102 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11103 switch (action) { 11104 case CTL_ACTION_BLOCK: 11105 case CTL_ACTION_OVERLAP: 11106 case CTL_ACTION_OVERLAP_TAG: 11107 case CTL_ACTION_SKIP: 11108 case CTL_ACTION_ERROR: 11109 return (action); 11110 break; /* NOTREACHED */ 11111 case CTL_ACTION_PASS: 11112 break; 11113 default: 11114 panic("invalid action %d", action); 11115 break; /* NOTREACHED */ 11116 } 11117 } 11118 11119 return (CTL_ACTION_PASS); 11120} 11121 11122/* 11123 * Assumptions: 11124 * - An I/O has just completed, and has been removed from the per-LUN OOA 11125 * queue, so some items on the blocked queue may now be unblocked. 11126 */ 11127static int 11128ctl_check_blocked(struct ctl_lun *lun) 11129{ 11130 union ctl_io *cur_blocked, *next_blocked; 11131 11132 mtx_assert(&lun->lun_lock, MA_OWNED); 11133 11134 /* 11135 * Run forward from the head of the blocked queue, checking each 11136 * entry against the I/Os prior to it on the OOA queue to see if 11137 * there is still any blockage. 11138 * 11139 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11140 * with our removing a variable on it while it is traversing the 11141 * list. 11142 */ 11143 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11144 cur_blocked != NULL; cur_blocked = next_blocked) { 11145 union ctl_io *prev_ooa; 11146 ctl_action action; 11147 11148 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11149 blocked_links); 11150 11151 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11152 ctl_ooaq, ooa_links); 11153 11154 /* 11155 * If cur_blocked happens to be the first item in the OOA 11156 * queue now, prev_ooa will be NULL, and the action 11157 * returned will just be CTL_ACTION_PASS. 11158 */ 11159 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11160 11161 switch (action) { 11162 case CTL_ACTION_BLOCK: 11163 /* Nothing to do here, still blocked */ 11164 break; 11165 case CTL_ACTION_OVERLAP: 11166 case CTL_ACTION_OVERLAP_TAG: 11167 /* 11168 * This shouldn't happen! In theory we've already 11169 * checked this command for overlap... 11170 */ 11171 break; 11172 case CTL_ACTION_PASS: 11173 case CTL_ACTION_SKIP: { 11174 const struct ctl_cmd_entry *entry; 11175 int isc_retval; 11176 11177 /* 11178 * The skip case shouldn't happen, this transaction 11179 * should have never made it onto the blocked queue. 11180 */ 11181 /* 11182 * This I/O is no longer blocked, we can remove it 11183 * from the blocked queue. Since this is a TAILQ 11184 * (doubly linked list), we can do O(1) removals 11185 * from any place on the list. 11186 */ 11187 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11188 blocked_links); 11189 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11190 11191 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 11192 /* 11193 * Need to send IO back to original side to 11194 * run 11195 */ 11196 union ctl_ha_msg msg_info; 11197 11198 msg_info.hdr.original_sc = 11199 cur_blocked->io_hdr.original_sc; 11200 msg_info.hdr.serializing_sc = cur_blocked; 11201 msg_info.hdr.msg_type = CTL_MSG_R2R; 11202 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11203 &msg_info, sizeof(msg_info), 0)) > 11204 CTL_HA_STATUS_SUCCESS) { 11205 printf("CTL:Check Blocked error from " 11206 "ctl_ha_msg_send %d\n", 11207 isc_retval); 11208 } 11209 break; 11210 } 11211 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11212 11213 /* 11214 * Check this I/O for LUN state changes that may 11215 * have happened while this command was blocked. 11216 * The LUN state may have been changed by a command 11217 * ahead of us in the queue, so we need to re-check 11218 * for any states that can be caused by SCSI 11219 * commands. 11220 */ 11221 if (ctl_scsiio_lun_check(lun, entry, 11222 &cur_blocked->scsiio) == 0) { 11223 cur_blocked->io_hdr.flags |= 11224 CTL_FLAG_IS_WAS_ON_RTR; 11225 ctl_enqueue_rtr(cur_blocked); 11226 } else 11227 ctl_done(cur_blocked); 11228 break; 11229 } 11230 default: 11231 /* 11232 * This probably shouldn't happen -- we shouldn't 11233 * get CTL_ACTION_ERROR, or anything else. 11234 */ 11235 break; 11236 } 11237 } 11238 11239 return (CTL_RETVAL_COMPLETE); 11240} 11241 11242/* 11243 * This routine (with one exception) checks LUN flags that can be set by 11244 * commands ahead of us in the OOA queue. These flags have to be checked 11245 * when a command initially comes in, and when we pull a command off the 11246 * blocked queue and are preparing to execute it. The reason we have to 11247 * check these flags for commands on the blocked queue is that the LUN 11248 * state may have been changed by a command ahead of us while we're on the 11249 * blocked queue. 11250 * 11251 * Ordering is somewhat important with these checks, so please pay 11252 * careful attention to the placement of any new checks. 11253 */ 11254static int 11255ctl_scsiio_lun_check(struct ctl_lun *lun, 11256 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11257{ 11258 struct ctl_softc *softc = lun->ctl_softc; 11259 int retval; 11260 uint32_t residx; 11261 11262 retval = 0; 11263 11264 mtx_assert(&lun->lun_lock, MA_OWNED); 11265 11266 /* 11267 * If this shelf is a secondary shelf controller, we have to reject 11268 * any media access commands. 11269 */ 11270 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 11271 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 11272 ctl_set_lun_standby(ctsio); 11273 retval = 1; 11274 goto bailout; 11275 } 11276 11277 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11278 if (lun->flags & CTL_LUN_READONLY) { 11279 ctl_set_sense(ctsio, /*current_error*/ 1, 11280 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11281 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11282 retval = 1; 11283 goto bailout; 11284 } 11285 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11286 .eca_and_aen & SCP_SWP) != 0) { 11287 ctl_set_sense(ctsio, /*current_error*/ 1, 11288 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11289 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11290 retval = 1; 11291 goto bailout; 11292 } 11293 } 11294 11295 /* 11296 * Check for a reservation conflict. If this command isn't allowed 11297 * even on reserved LUNs, and if this initiator isn't the one who 11298 * reserved us, reject the command with a reservation conflict. 11299 */ 11300 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11301 if ((lun->flags & CTL_LUN_RESERVED) 11302 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11303 if (lun->res_idx != residx) { 11304 ctl_set_reservation_conflict(ctsio); 11305 retval = 1; 11306 goto bailout; 11307 } 11308 } 11309 11310 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11311 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11312 /* No reservation or command is allowed. */; 11313 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11314 (lun->res_type == SPR_TYPE_WR_EX || 11315 lun->res_type == SPR_TYPE_WR_EX_RO || 11316 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11317 /* The command is allowed for Write Exclusive resv. */; 11318 } else { 11319 /* 11320 * if we aren't registered or it's a res holder type 11321 * reservation and this isn't the res holder then set a 11322 * conflict. 11323 */ 11324 if (ctl_get_prkey(lun, residx) == 0 11325 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11326 ctl_set_reservation_conflict(ctsio); 11327 retval = 1; 11328 goto bailout; 11329 } 11330 11331 } 11332 11333 if ((lun->flags & CTL_LUN_OFFLINE) 11334 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11335 ctl_set_lun_not_ready(ctsio); 11336 retval = 1; 11337 goto bailout; 11338 } 11339 11340 /* 11341 * If the LUN is stopped, see if this particular command is allowed 11342 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11343 */ 11344 if ((lun->flags & CTL_LUN_STOPPED) 11345 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11346 /* "Logical unit not ready, initializing cmd. required" */ 11347 ctl_set_lun_stopped(ctsio); 11348 retval = 1; 11349 goto bailout; 11350 } 11351 11352 if ((lun->flags & CTL_LUN_INOPERABLE) 11353 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11354 /* "Medium format corrupted" */ 11355 ctl_set_medium_format_corrupted(ctsio); 11356 retval = 1; 11357 goto bailout; 11358 } 11359 11360bailout: 11361 return (retval); 11362 11363} 11364 11365static void 11366ctl_failover_io(union ctl_io *io, int have_lock) 11367{ 11368 ctl_set_busy(&io->scsiio); 11369 ctl_done(io); 11370} 11371 11372static void 11373ctl_failover(void) 11374{ 11375 struct ctl_lun *lun; 11376 struct ctl_softc *softc; 11377 union ctl_io *next_io, *pending_io; 11378 union ctl_io *io; 11379 int lun_idx; 11380 11381 softc = control_softc; 11382 11383 mtx_lock(&softc->ctl_lock); 11384 /* 11385 * Remove any cmds from the other SC from the rtr queue. These 11386 * will obviously only be for LUNs for which we're the primary. 11387 * We can't send status or get/send data for these commands. 11388 * Since they haven't been executed yet, we can just remove them. 11389 * We'll either abort them or delete them below, depending on 11390 * which HA mode we're in. 11391 */ 11392#ifdef notyet 11393 mtx_lock(&softc->queue_lock); 11394 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); 11395 io != NULL; io = next_io) { 11396 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11397 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11398 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr, 11399 ctl_io_hdr, links); 11400 } 11401 mtx_unlock(&softc->queue_lock); 11402#endif 11403 11404 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) { 11405 lun = softc->ctl_luns[lun_idx]; 11406 if (lun==NULL) 11407 continue; 11408 11409 /* 11410 * Processor LUNs are primary on both sides. 11411 * XXX will this always be true? 11412 */ 11413 if (lun->be_lun->lun_type == T_PROCESSOR) 11414 continue; 11415 11416 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11417 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11418 printf("FAILOVER: primary lun %d\n", lun_idx); 11419 /* 11420 * Remove all commands from the other SC. First from the 11421 * blocked queue then from the ooa queue. Once we have 11422 * removed them. Call ctl_check_blocked to see if there 11423 * is anything that can run. 11424 */ 11425 for (io = (union ctl_io *)TAILQ_FIRST( 11426 &lun->blocked_queue); io != NULL; io = next_io) { 11427 11428 next_io = (union ctl_io *)TAILQ_NEXT( 11429 &io->io_hdr, blocked_links); 11430 11431 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11432 TAILQ_REMOVE(&lun->blocked_queue, 11433 &io->io_hdr,blocked_links); 11434 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11435 TAILQ_REMOVE(&lun->ooa_queue, 11436 &io->io_hdr, ooa_links); 11437 11438 ctl_free_io(io); 11439 } 11440 } 11441 11442 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11443 io != NULL; io = next_io) { 11444 11445 next_io = (union ctl_io *)TAILQ_NEXT( 11446 &io->io_hdr, ooa_links); 11447 11448 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11449 11450 TAILQ_REMOVE(&lun->ooa_queue, 11451 &io->io_hdr, 11452 ooa_links); 11453 11454 ctl_free_io(io); 11455 } 11456 } 11457 ctl_check_blocked(lun); 11458 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11459 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11460 11461 printf("FAILOVER: primary lun %d\n", lun_idx); 11462 /* 11463 * Abort all commands from the other SC. We can't 11464 * send status back for them now. These should get 11465 * cleaned up when they are completed or come out 11466 * for a datamove operation. 11467 */ 11468 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11469 io != NULL; io = next_io) { 11470 next_io = (union ctl_io *)TAILQ_NEXT( 11471 &io->io_hdr, ooa_links); 11472 11473 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11474 io->io_hdr.flags |= CTL_FLAG_ABORT; 11475 } 11476 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11477 && (softc->ha_mode == CTL_HA_MODE_XFER)) { 11478 11479 printf("FAILOVER: secondary lun %d\n", lun_idx); 11480 11481 lun->flags |= CTL_LUN_PRIMARY_SC; 11482 11483 /* 11484 * We send all I/O that was sent to this controller 11485 * and redirected to the other side back with 11486 * busy status, and have the initiator retry it. 11487 * Figuring out how much data has been transferred, 11488 * etc. and picking up where we left off would be 11489 * very tricky. 11490 * 11491 * XXX KDM need to remove I/O from the blocked 11492 * queue as well! 11493 */ 11494 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11495 &lun->ooa_queue); pending_io != NULL; 11496 pending_io = next_io) { 11497 11498 next_io = (union ctl_io *)TAILQ_NEXT( 11499 &pending_io->io_hdr, ooa_links); 11500 11501 pending_io->io_hdr.flags &= 11502 ~CTL_FLAG_SENT_2OTHER_SC; 11503 11504 if (pending_io->io_hdr.flags & 11505 CTL_FLAG_IO_ACTIVE) { 11506 pending_io->io_hdr.flags |= 11507 CTL_FLAG_FAILOVER; 11508 } else { 11509 ctl_set_busy(&pending_io->scsiio); 11510 ctl_done(pending_io); 11511 } 11512 } 11513 11514 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11515 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11516 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11517 printf("FAILOVER: secondary lun %d\n", lun_idx); 11518 /* 11519 * if the first io on the OOA is not on the RtR queue 11520 * add it. 11521 */ 11522 lun->flags |= CTL_LUN_PRIMARY_SC; 11523 11524 pending_io = (union ctl_io *)TAILQ_FIRST( 11525 &lun->ooa_queue); 11526 if (pending_io==NULL) { 11527 printf("Nothing on OOA queue\n"); 11528 continue; 11529 } 11530 11531 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11532 if ((pending_io->io_hdr.flags & 11533 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11534 pending_io->io_hdr.flags |= 11535 CTL_FLAG_IS_WAS_ON_RTR; 11536 ctl_enqueue_rtr(pending_io); 11537 } 11538#if 0 11539 else 11540 { 11541 printf("Tag 0x%04x is running\n", 11542 pending_io->scsiio.tag_num); 11543 } 11544#endif 11545 11546 next_io = (union ctl_io *)TAILQ_NEXT( 11547 &pending_io->io_hdr, ooa_links); 11548 for (pending_io=next_io; pending_io != NULL; 11549 pending_io = next_io) { 11550 pending_io->io_hdr.flags &= 11551 ~CTL_FLAG_SENT_2OTHER_SC; 11552 next_io = (union ctl_io *)TAILQ_NEXT( 11553 &pending_io->io_hdr, ooa_links); 11554 if (pending_io->io_hdr.flags & 11555 CTL_FLAG_IS_WAS_ON_RTR) { 11556#if 0 11557 printf("Tag 0x%04x is running\n", 11558 pending_io->scsiio.tag_num); 11559#endif 11560 continue; 11561 } 11562 11563 switch (ctl_check_ooa(lun, pending_io, 11564 (union ctl_io *)TAILQ_PREV( 11565 &pending_io->io_hdr, ctl_ooaq, 11566 ooa_links))) { 11567 11568 case CTL_ACTION_BLOCK: 11569 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11570 &pending_io->io_hdr, 11571 blocked_links); 11572 pending_io->io_hdr.flags |= 11573 CTL_FLAG_BLOCKED; 11574 break; 11575 case CTL_ACTION_PASS: 11576 case CTL_ACTION_SKIP: 11577 pending_io->io_hdr.flags |= 11578 CTL_FLAG_IS_WAS_ON_RTR; 11579 ctl_enqueue_rtr(pending_io); 11580 break; 11581 case CTL_ACTION_OVERLAP: 11582 ctl_set_overlapped_cmd( 11583 (struct ctl_scsiio *)pending_io); 11584 ctl_done(pending_io); 11585 break; 11586 case CTL_ACTION_OVERLAP_TAG: 11587 ctl_set_overlapped_tag( 11588 (struct ctl_scsiio *)pending_io, 11589 pending_io->scsiio.tag_num & 0xff); 11590 ctl_done(pending_io); 11591 break; 11592 case CTL_ACTION_ERROR: 11593 default: 11594 ctl_set_internal_failure( 11595 (struct ctl_scsiio *)pending_io, 11596 0, // sks_valid 11597 0); //retry count 11598 ctl_done(pending_io); 11599 break; 11600 } 11601 } 11602 11603 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 11604 } else { 11605 panic("Unhandled HA mode failover, LUN flags = %#x, " 11606 "ha_mode = #%x", lun->flags, softc->ha_mode); 11607 } 11608 } 11609 ctl_pause_rtr = 0; 11610 mtx_unlock(&softc->ctl_lock); 11611} 11612 11613static void 11614ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx, 11615 ctl_ua_type ua_type) 11616{ 11617 struct ctl_lun *lun; 11618 ctl_ua_type *pu; 11619 11620 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 11621 11622 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 11623 mtx_lock(&lun->lun_lock); 11624 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 11625 if (pu != NULL) 11626 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua_type; 11627 mtx_unlock(&lun->lun_lock); 11628 } 11629} 11630 11631static int 11632ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11633{ 11634 struct ctl_lun *lun; 11635 const struct ctl_cmd_entry *entry; 11636 uint32_t initidx, targ_lun; 11637 int retval; 11638 11639 retval = 0; 11640 11641 lun = NULL; 11642 11643 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11644 if ((targ_lun < CTL_MAX_LUNS) 11645 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11646 /* 11647 * If the LUN is invalid, pretend that it doesn't exist. 11648 * It will go away as soon as all pending I/O has been 11649 * completed. 11650 */ 11651 mtx_lock(&lun->lun_lock); 11652 if (lun->flags & CTL_LUN_DISABLED) { 11653 mtx_unlock(&lun->lun_lock); 11654 lun = NULL; 11655 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11656 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11657 } else { 11658 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11659 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11660 lun->be_lun; 11661 if (lun->be_lun->lun_type == T_PROCESSOR) { 11662 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11663 } 11664 11665 /* 11666 * Every I/O goes into the OOA queue for a 11667 * particular LUN, and stays there until completion. 11668 */ 11669#ifdef CTL_TIME_IO 11670 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11671 lun->idle_time += getsbinuptime() - 11672 lun->last_busy; 11673 } 11674#endif 11675 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11676 ooa_links); 11677 } 11678 } else { 11679 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11680 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11681 } 11682 11683 /* Get command entry and return error if it is unsuppotyed. */ 11684 entry = ctl_validate_command(ctsio); 11685 if (entry == NULL) { 11686 if (lun) 11687 mtx_unlock(&lun->lun_lock); 11688 return (retval); 11689 } 11690 11691 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11692 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11693 11694 /* 11695 * Check to see whether we can send this command to LUNs that don't 11696 * exist. This should pretty much only be the case for inquiry 11697 * and request sense. Further checks, below, really require having 11698 * a LUN, so we can't really check the command anymore. Just put 11699 * it on the rtr queue. 11700 */ 11701 if (lun == NULL) { 11702 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11703 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11704 ctl_enqueue_rtr((union ctl_io *)ctsio); 11705 return (retval); 11706 } 11707 11708 ctl_set_unsupported_lun(ctsio); 11709 ctl_done((union ctl_io *)ctsio); 11710 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11711 return (retval); 11712 } else { 11713 /* 11714 * Make sure we support this particular command on this LUN. 11715 * e.g., we don't support writes to the control LUN. 11716 */ 11717 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11718 mtx_unlock(&lun->lun_lock); 11719 ctl_set_invalid_opcode(ctsio); 11720 ctl_done((union ctl_io *)ctsio); 11721 return (retval); 11722 } 11723 } 11724 11725 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11726 11727#ifdef CTL_WITH_CA 11728 /* 11729 * If we've got a request sense, it'll clear the contingent 11730 * allegiance condition. Otherwise, if we have a CA condition for 11731 * this initiator, clear it, because it sent down a command other 11732 * than request sense. 11733 */ 11734 if ((ctsio->cdb[0] != REQUEST_SENSE) 11735 && (ctl_is_set(lun->have_ca, initidx))) 11736 ctl_clear_mask(lun->have_ca, initidx); 11737#endif 11738 11739 /* 11740 * If the command has this flag set, it handles its own unit 11741 * attention reporting, we shouldn't do anything. Otherwise we 11742 * check for any pending unit attentions, and send them back to the 11743 * initiator. We only do this when a command initially comes in, 11744 * not when we pull it off the blocked queue. 11745 * 11746 * According to SAM-3, section 5.3.2, the order that things get 11747 * presented back to the host is basically unit attentions caused 11748 * by some sort of reset event, busy status, reservation conflicts 11749 * or task set full, and finally any other status. 11750 * 11751 * One issue here is that some of the unit attentions we report 11752 * don't fall into the "reset" category (e.g. "reported luns data 11753 * has changed"). So reporting it here, before the reservation 11754 * check, may be technically wrong. I guess the only thing to do 11755 * would be to check for and report the reset events here, and then 11756 * check for the other unit attention types after we check for a 11757 * reservation conflict. 11758 * 11759 * XXX KDM need to fix this 11760 */ 11761 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11762 ctl_ua_type ua_type; 11763 scsi_sense_data_type sense_format; 11764 11765 if (lun->flags & CTL_LUN_SENSE_DESC) 11766 sense_format = SSD_TYPE_DESC; 11767 else 11768 sense_format = SSD_TYPE_FIXED; 11769 11770 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11771 sense_format); 11772 if (ua_type != CTL_UA_NONE) { 11773 mtx_unlock(&lun->lun_lock); 11774 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11775 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11776 ctsio->sense_len = SSD_FULL_SIZE; 11777 ctl_done((union ctl_io *)ctsio); 11778 return (retval); 11779 } 11780 } 11781 11782 11783 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11784 mtx_unlock(&lun->lun_lock); 11785 ctl_done((union ctl_io *)ctsio); 11786 return (retval); 11787 } 11788 11789 /* 11790 * XXX CHD this is where we want to send IO to other side if 11791 * this LUN is secondary on this SC. We will need to make a copy 11792 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11793 * the copy we send as FROM_OTHER. 11794 * We also need to stuff the address of the original IO so we can 11795 * find it easily. Something similar will need be done on the other 11796 * side so when we are done we can find the copy. 11797 */ 11798 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11799 union ctl_ha_msg msg_info; 11800 int isc_retval; 11801 11802 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11803 11804 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11805 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11806#if 0 11807 printf("1. ctsio %p\n", ctsio); 11808#endif 11809 msg_info.hdr.serializing_sc = NULL; 11810 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11811 msg_info.scsi.tag_num = ctsio->tag_num; 11812 msg_info.scsi.tag_type = ctsio->tag_type; 11813 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11814 11815 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11816 11817 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11818 (void *)&msg_info, sizeof(msg_info), 0)) > 11819 CTL_HA_STATUS_SUCCESS) { 11820 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11821 isc_retval); 11822 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11823 } else { 11824#if 0 11825 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11826#endif 11827 } 11828 11829 /* 11830 * XXX KDM this I/O is off the incoming queue, but hasn't 11831 * been inserted on any other queue. We may need to come 11832 * up with a holding queue while we wait for serialization 11833 * so that we have an idea of what we're waiting for from 11834 * the other side. 11835 */ 11836 mtx_unlock(&lun->lun_lock); 11837 return (retval); 11838 } 11839 11840 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11841 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11842 ctl_ooaq, ooa_links))) { 11843 case CTL_ACTION_BLOCK: 11844 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11845 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11846 blocked_links); 11847 mtx_unlock(&lun->lun_lock); 11848 return (retval); 11849 case CTL_ACTION_PASS: 11850 case CTL_ACTION_SKIP: 11851 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11852 mtx_unlock(&lun->lun_lock); 11853 ctl_enqueue_rtr((union ctl_io *)ctsio); 11854 break; 11855 case CTL_ACTION_OVERLAP: 11856 mtx_unlock(&lun->lun_lock); 11857 ctl_set_overlapped_cmd(ctsio); 11858 ctl_done((union ctl_io *)ctsio); 11859 break; 11860 case CTL_ACTION_OVERLAP_TAG: 11861 mtx_unlock(&lun->lun_lock); 11862 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11863 ctl_done((union ctl_io *)ctsio); 11864 break; 11865 case CTL_ACTION_ERROR: 11866 default: 11867 mtx_unlock(&lun->lun_lock); 11868 ctl_set_internal_failure(ctsio, 11869 /*sks_valid*/ 0, 11870 /*retry_count*/ 0); 11871 ctl_done((union ctl_io *)ctsio); 11872 break; 11873 } 11874 return (retval); 11875} 11876 11877const struct ctl_cmd_entry * 11878ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11879{ 11880 const struct ctl_cmd_entry *entry; 11881 int service_action; 11882 11883 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11884 if (sa) 11885 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11886 if (entry->flags & CTL_CMD_FLAG_SA5) { 11887 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11888 entry = &((const struct ctl_cmd_entry *) 11889 entry->execute)[service_action]; 11890 } 11891 return (entry); 11892} 11893 11894const struct ctl_cmd_entry * 11895ctl_validate_command(struct ctl_scsiio *ctsio) 11896{ 11897 const struct ctl_cmd_entry *entry; 11898 int i, sa; 11899 uint8_t diff; 11900 11901 entry = ctl_get_cmd_entry(ctsio, &sa); 11902 if (entry->execute == NULL) { 11903 if (sa) 11904 ctl_set_invalid_field(ctsio, 11905 /*sks_valid*/ 1, 11906 /*command*/ 1, 11907 /*field*/ 1, 11908 /*bit_valid*/ 1, 11909 /*bit*/ 4); 11910 else 11911 ctl_set_invalid_opcode(ctsio); 11912 ctl_done((union ctl_io *)ctsio); 11913 return (NULL); 11914 } 11915 KASSERT(entry->length > 0, 11916 ("Not defined length for command 0x%02x/0x%02x", 11917 ctsio->cdb[0], ctsio->cdb[1])); 11918 for (i = 1; i < entry->length; i++) { 11919 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11920 if (diff == 0) 11921 continue; 11922 ctl_set_invalid_field(ctsio, 11923 /*sks_valid*/ 1, 11924 /*command*/ 1, 11925 /*field*/ i, 11926 /*bit_valid*/ 1, 11927 /*bit*/ fls(diff) - 1); 11928 ctl_done((union ctl_io *)ctsio); 11929 return (NULL); 11930 } 11931 return (entry); 11932} 11933 11934static int 11935ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11936{ 11937 11938 switch (lun_type) { 11939 case T_PROCESSOR: 11940 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11941 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11942 return (0); 11943 break; 11944 case T_DIRECT: 11945 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11946 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11947 return (0); 11948 break; 11949 default: 11950 return (0); 11951 } 11952 return (1); 11953} 11954 11955static int 11956ctl_scsiio(struct ctl_scsiio *ctsio) 11957{ 11958 int retval; 11959 const struct ctl_cmd_entry *entry; 11960 11961 retval = CTL_RETVAL_COMPLETE; 11962 11963 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11964 11965 entry = ctl_get_cmd_entry(ctsio, NULL); 11966 11967 /* 11968 * If this I/O has been aborted, just send it straight to 11969 * ctl_done() without executing it. 11970 */ 11971 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11972 ctl_done((union ctl_io *)ctsio); 11973 goto bailout; 11974 } 11975 11976 /* 11977 * All the checks should have been handled by ctl_scsiio_precheck(). 11978 * We should be clear now to just execute the I/O. 11979 */ 11980 retval = entry->execute(ctsio); 11981 11982bailout: 11983 return (retval); 11984} 11985 11986/* 11987 * Since we only implement one target right now, a bus reset simply resets 11988 * our single target. 11989 */ 11990static int 11991ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11992{ 11993 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11994} 11995 11996static int 11997ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11998 ctl_ua_type ua_type) 11999{ 12000 struct ctl_lun *lun; 12001 int retval; 12002 12003 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12004 union ctl_ha_msg msg_info; 12005 12006 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12007 msg_info.hdr.nexus = io->io_hdr.nexus; 12008 if (ua_type==CTL_UA_TARG_RESET) 12009 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 12010 else 12011 msg_info.task.task_action = CTL_TASK_BUS_RESET; 12012 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12013 msg_info.hdr.original_sc = NULL; 12014 msg_info.hdr.serializing_sc = NULL; 12015 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12016 (void *)&msg_info, sizeof(msg_info), 0)) { 12017 } 12018 } 12019 retval = 0; 12020 12021 mtx_lock(&softc->ctl_lock); 12022 STAILQ_FOREACH(lun, &softc->lun_list, links) 12023 retval += ctl_lun_reset(lun, io, ua_type); 12024 mtx_unlock(&softc->ctl_lock); 12025 12026 return (retval); 12027} 12028 12029/* 12030 * The LUN should always be set. The I/O is optional, and is used to 12031 * distinguish between I/Os sent by this initiator, and by other 12032 * initiators. We set unit attention for initiators other than this one. 12033 * SAM-3 is vague on this point. It does say that a unit attention should 12034 * be established for other initiators when a LUN is reset (see section 12035 * 5.7.3), but it doesn't specifically say that the unit attention should 12036 * be established for this particular initiator when a LUN is reset. Here 12037 * is the relevant text, from SAM-3 rev 8: 12038 * 12039 * 5.7.2 When a SCSI initiator port aborts its own tasks 12040 * 12041 * When a SCSI initiator port causes its own task(s) to be aborted, no 12042 * notification that the task(s) have been aborted shall be returned to 12043 * the SCSI initiator port other than the completion response for the 12044 * command or task management function action that caused the task(s) to 12045 * be aborted and notification(s) associated with related effects of the 12046 * action (e.g., a reset unit attention condition). 12047 * 12048 * XXX KDM for now, we're setting unit attention for all initiators. 12049 */ 12050static int 12051ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 12052{ 12053 union ctl_io *xio; 12054#if 0 12055 uint32_t initidx; 12056#endif 12057#ifdef CTL_WITH_CA 12058 int i; 12059#endif 12060 12061 mtx_lock(&lun->lun_lock); 12062 /* 12063 * Run through the OOA queue and abort each I/O. 12064 */ 12065 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12066 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12067 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 12068 } 12069 12070 /* 12071 * This version sets unit attention for every 12072 */ 12073#if 0 12074 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12075 ctl_est_ua_all(lun, initidx, ua_type); 12076#else 12077 ctl_est_ua_all(lun, -1, ua_type); 12078#endif 12079 12080 /* 12081 * A reset (any kind, really) clears reservations established with 12082 * RESERVE/RELEASE. It does not clear reservations established 12083 * with PERSISTENT RESERVE OUT, but we don't support that at the 12084 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 12085 * reservations made with the RESERVE/RELEASE commands, because 12086 * those commands are obsolete in SPC-3. 12087 */ 12088 lun->flags &= ~CTL_LUN_RESERVED; 12089 12090#ifdef CTL_WITH_CA 12091 for (i = 0; i < CTL_MAX_INITIATORS; i++) 12092 ctl_clear_mask(lun->have_ca, i); 12093#endif 12094 mtx_unlock(&lun->lun_lock); 12095 12096 return (0); 12097} 12098 12099static void 12100ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 12101 int other_sc) 12102{ 12103 union ctl_io *xio; 12104 12105 mtx_assert(&lun->lun_lock, MA_OWNED); 12106 12107 /* 12108 * Run through the OOA queue and attempt to find the given I/O. 12109 * The target port, initiator ID, tag type and tag number have to 12110 * match the values that we got from the initiator. If we have an 12111 * untagged command to abort, simply abort the first untagged command 12112 * we come to. We only allow one untagged command at a time of course. 12113 */ 12114 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12115 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12116 12117 if ((targ_port == UINT32_MAX || 12118 targ_port == xio->io_hdr.nexus.targ_port) && 12119 (init_id == UINT32_MAX || 12120 init_id == xio->io_hdr.nexus.initid.id)) { 12121 if (targ_port != xio->io_hdr.nexus.targ_port || 12122 init_id != xio->io_hdr.nexus.initid.id) 12123 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 12124 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12125 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12126 union ctl_ha_msg msg_info; 12127 12128 msg_info.hdr.nexus = xio->io_hdr.nexus; 12129 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12130 msg_info.task.tag_num = xio->scsiio.tag_num; 12131 msg_info.task.tag_type = xio->scsiio.tag_type; 12132 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12133 msg_info.hdr.original_sc = NULL; 12134 msg_info.hdr.serializing_sc = NULL; 12135 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12136 (void *)&msg_info, sizeof(msg_info), 0); 12137 } 12138 } 12139 } 12140} 12141 12142static int 12143ctl_abort_task_set(union ctl_io *io) 12144{ 12145 struct ctl_softc *softc = control_softc; 12146 struct ctl_lun *lun; 12147 uint32_t targ_lun; 12148 12149 /* 12150 * Look up the LUN. 12151 */ 12152 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12153 mtx_lock(&softc->ctl_lock); 12154 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 12155 lun = softc->ctl_luns[targ_lun]; 12156 else { 12157 mtx_unlock(&softc->ctl_lock); 12158 return (1); 12159 } 12160 12161 mtx_lock(&lun->lun_lock); 12162 mtx_unlock(&softc->ctl_lock); 12163 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 12164 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12165 io->io_hdr.nexus.initid.id, 12166 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12167 } else { /* CTL_TASK_CLEAR_TASK_SET */ 12168 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 12169 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12170 } 12171 mtx_unlock(&lun->lun_lock); 12172 return (0); 12173} 12174 12175static int 12176ctl_i_t_nexus_reset(union ctl_io *io) 12177{ 12178 struct ctl_softc *softc = control_softc; 12179 struct ctl_lun *lun; 12180 uint32_t initidx, residx; 12181 12182 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12183 residx = ctl_get_resindex(&io->io_hdr.nexus); 12184 mtx_lock(&softc->ctl_lock); 12185 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12186 mtx_lock(&lun->lun_lock); 12187 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12188 io->io_hdr.nexus.initid.id, 12189 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 12190#ifdef CTL_WITH_CA 12191 ctl_clear_mask(lun->have_ca, initidx); 12192#endif 12193 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 12194 lun->flags &= ~CTL_LUN_RESERVED; 12195 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12196 mtx_unlock(&lun->lun_lock); 12197 } 12198 mtx_unlock(&softc->ctl_lock); 12199 return (0); 12200} 12201 12202static int 12203ctl_abort_task(union ctl_io *io) 12204{ 12205 union ctl_io *xio; 12206 struct ctl_lun *lun; 12207 struct ctl_softc *softc; 12208#if 0 12209 struct sbuf sb; 12210 char printbuf[128]; 12211#endif 12212 int found; 12213 uint32_t targ_lun; 12214 12215 softc = control_softc; 12216 found = 0; 12217 12218 /* 12219 * Look up the LUN. 12220 */ 12221 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12222 mtx_lock(&softc->ctl_lock); 12223 if ((targ_lun < CTL_MAX_LUNS) 12224 && (softc->ctl_luns[targ_lun] != NULL)) 12225 lun = softc->ctl_luns[targ_lun]; 12226 else { 12227 mtx_unlock(&softc->ctl_lock); 12228 return (1); 12229 } 12230 12231#if 0 12232 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12233 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12234#endif 12235 12236 mtx_lock(&lun->lun_lock); 12237 mtx_unlock(&softc->ctl_lock); 12238 /* 12239 * Run through the OOA queue and attempt to find the given I/O. 12240 * The target port, initiator ID, tag type and tag number have to 12241 * match the values that we got from the initiator. If we have an 12242 * untagged command to abort, simply abort the first untagged command 12243 * we come to. We only allow one untagged command at a time of course. 12244 */ 12245 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12246 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12247#if 0 12248 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12249 12250 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12251 lun->lun, xio->scsiio.tag_num, 12252 xio->scsiio.tag_type, 12253 (xio->io_hdr.blocked_links.tqe_prev 12254 == NULL) ? "" : " BLOCKED", 12255 (xio->io_hdr.flags & 12256 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12257 (xio->io_hdr.flags & 12258 CTL_FLAG_ABORT) ? " ABORT" : "", 12259 (xio->io_hdr.flags & 12260 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12261 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12262 sbuf_finish(&sb); 12263 printf("%s\n", sbuf_data(&sb)); 12264#endif 12265 12266 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12267 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id) 12268 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12269 continue; 12270 12271 /* 12272 * If the abort says that the task is untagged, the 12273 * task in the queue must be untagged. Otherwise, 12274 * we just check to see whether the tag numbers 12275 * match. This is because the QLogic firmware 12276 * doesn't pass back the tag type in an abort 12277 * request. 12278 */ 12279#if 0 12280 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12281 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12282 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12283#endif 12284 /* 12285 * XXX KDM we've got problems with FC, because it 12286 * doesn't send down a tag type with aborts. So we 12287 * can only really go by the tag number... 12288 * This may cause problems with parallel SCSI. 12289 * Need to figure that out!! 12290 */ 12291 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12292 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12293 found = 1; 12294 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12295 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12296 union ctl_ha_msg msg_info; 12297 12298 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 12299 msg_info.hdr.nexus = io->io_hdr.nexus; 12300 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12301 msg_info.task.tag_num = io->taskio.tag_num; 12302 msg_info.task.tag_type = io->taskio.tag_type; 12303 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12304 msg_info.hdr.original_sc = NULL; 12305 msg_info.hdr.serializing_sc = NULL; 12306#if 0 12307 printf("Sent Abort to other side\n"); 12308#endif 12309 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12310 (void *)&msg_info, sizeof(msg_info), 0) != 12311 CTL_HA_STATUS_SUCCESS) { 12312 } 12313 } 12314#if 0 12315 printf("ctl_abort_task: found I/O to abort\n"); 12316#endif 12317 } 12318 } 12319 mtx_unlock(&lun->lun_lock); 12320 12321 if (found == 0) { 12322 /* 12323 * This isn't really an error. It's entirely possible for 12324 * the abort and command completion to cross on the wire. 12325 * This is more of an informative/diagnostic error. 12326 */ 12327#if 0 12328 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12329 "%d:%d:%d:%d tag %d type %d\n", 12330 io->io_hdr.nexus.initid.id, 12331 io->io_hdr.nexus.targ_port, 12332 io->io_hdr.nexus.targ_target.id, 12333 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12334 io->taskio.tag_type); 12335#endif 12336 } 12337 return (0); 12338} 12339 12340static void 12341ctl_run_task(union ctl_io *io) 12342{ 12343 struct ctl_softc *softc = control_softc; 12344 int retval = 1; 12345 const char *task_desc; 12346 12347 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12348 12349 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12350 ("ctl_run_task: Unextected io_type %d\n", 12351 io->io_hdr.io_type)); 12352 12353 task_desc = ctl_scsi_task_string(&io->taskio); 12354 if (task_desc != NULL) { 12355#ifdef NEEDTOPORT 12356 csevent_log(CSC_CTL | CSC_SHELF_SW | 12357 CTL_TASK_REPORT, 12358 csevent_LogType_Trace, 12359 csevent_Severity_Information, 12360 csevent_AlertLevel_Green, 12361 csevent_FRU_Firmware, 12362 csevent_FRU_Unknown, 12363 "CTL: received task: %s",task_desc); 12364#endif 12365 } else { 12366#ifdef NEEDTOPORT 12367 csevent_log(CSC_CTL | CSC_SHELF_SW | 12368 CTL_TASK_REPORT, 12369 csevent_LogType_Trace, 12370 csevent_Severity_Information, 12371 csevent_AlertLevel_Green, 12372 csevent_FRU_Firmware, 12373 csevent_FRU_Unknown, 12374 "CTL: received unknown task " 12375 "type: %d (%#x)", 12376 io->taskio.task_action, 12377 io->taskio.task_action); 12378#endif 12379 } 12380 switch (io->taskio.task_action) { 12381 case CTL_TASK_ABORT_TASK: 12382 retval = ctl_abort_task(io); 12383 break; 12384 case CTL_TASK_ABORT_TASK_SET: 12385 case CTL_TASK_CLEAR_TASK_SET: 12386 retval = ctl_abort_task_set(io); 12387 break; 12388 case CTL_TASK_CLEAR_ACA: 12389 break; 12390 case CTL_TASK_I_T_NEXUS_RESET: 12391 retval = ctl_i_t_nexus_reset(io); 12392 break; 12393 case CTL_TASK_LUN_RESET: { 12394 struct ctl_lun *lun; 12395 uint32_t targ_lun; 12396 12397 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12398 mtx_lock(&softc->ctl_lock); 12399 if ((targ_lun < CTL_MAX_LUNS) 12400 && (softc->ctl_luns[targ_lun] != NULL)) 12401 lun = softc->ctl_luns[targ_lun]; 12402 else { 12403 mtx_unlock(&softc->ctl_lock); 12404 retval = 1; 12405 break; 12406 } 12407 12408 if (!(io->io_hdr.flags & 12409 CTL_FLAG_FROM_OTHER_SC)) { 12410 union ctl_ha_msg msg_info; 12411 12412 io->io_hdr.flags |= 12413 CTL_FLAG_SENT_2OTHER_SC; 12414 msg_info.hdr.msg_type = 12415 CTL_MSG_MANAGE_TASKS; 12416 msg_info.hdr.nexus = io->io_hdr.nexus; 12417 msg_info.task.task_action = 12418 CTL_TASK_LUN_RESET; 12419 msg_info.hdr.original_sc = NULL; 12420 msg_info.hdr.serializing_sc = NULL; 12421 if (CTL_HA_STATUS_SUCCESS != 12422 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12423 (void *)&msg_info, 12424 sizeof(msg_info), 0)) { 12425 } 12426 } 12427 12428 retval = ctl_lun_reset(lun, io, 12429 CTL_UA_LUN_RESET); 12430 mtx_unlock(&softc->ctl_lock); 12431 break; 12432 } 12433 case CTL_TASK_TARGET_RESET: 12434 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12435 break; 12436 case CTL_TASK_BUS_RESET: 12437 retval = ctl_bus_reset(softc, io); 12438 break; 12439 case CTL_TASK_PORT_LOGIN: 12440 break; 12441 case CTL_TASK_PORT_LOGOUT: 12442 break; 12443 default: 12444 printf("ctl_run_task: got unknown task management event %d\n", 12445 io->taskio.task_action); 12446 break; 12447 } 12448 if (retval == 0) 12449 io->io_hdr.status = CTL_SUCCESS; 12450 else 12451 io->io_hdr.status = CTL_ERROR; 12452 ctl_done(io); 12453} 12454 12455/* 12456 * For HA operation. Handle commands that come in from the other 12457 * controller. 12458 */ 12459static void 12460ctl_handle_isc(union ctl_io *io) 12461{ 12462 int free_io; 12463 struct ctl_lun *lun; 12464 struct ctl_softc *softc; 12465 uint32_t targ_lun; 12466 12467 softc = control_softc; 12468 12469 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12470 lun = softc->ctl_luns[targ_lun]; 12471 12472 switch (io->io_hdr.msg_type) { 12473 case CTL_MSG_SERIALIZE: 12474 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12475 break; 12476 case CTL_MSG_R2R: { 12477 const struct ctl_cmd_entry *entry; 12478 12479 /* 12480 * This is only used in SER_ONLY mode. 12481 */ 12482 free_io = 0; 12483 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12484 mtx_lock(&lun->lun_lock); 12485 if (ctl_scsiio_lun_check(lun, 12486 entry, (struct ctl_scsiio *)io) != 0) { 12487 mtx_unlock(&lun->lun_lock); 12488 ctl_done(io); 12489 break; 12490 } 12491 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12492 mtx_unlock(&lun->lun_lock); 12493 ctl_enqueue_rtr(io); 12494 break; 12495 } 12496 case CTL_MSG_FINISH_IO: 12497 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12498 free_io = 0; 12499 ctl_done(io); 12500 } else { 12501 free_io = 1; 12502 mtx_lock(&lun->lun_lock); 12503 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12504 ooa_links); 12505 ctl_check_blocked(lun); 12506 mtx_unlock(&lun->lun_lock); 12507 } 12508 break; 12509 case CTL_MSG_PERS_ACTION: 12510 ctl_hndl_per_res_out_on_other_sc( 12511 (union ctl_ha_msg *)&io->presio.pr_msg); 12512 free_io = 1; 12513 break; 12514 case CTL_MSG_BAD_JUJU: 12515 free_io = 0; 12516 ctl_done(io); 12517 break; 12518 case CTL_MSG_DATAMOVE: 12519 /* Only used in XFER mode */ 12520 free_io = 0; 12521 ctl_datamove_remote(io); 12522 break; 12523 case CTL_MSG_DATAMOVE_DONE: 12524 /* Only used in XFER mode */ 12525 free_io = 0; 12526 io->scsiio.be_move_done(io); 12527 break; 12528 default: 12529 free_io = 1; 12530 printf("%s: Invalid message type %d\n", 12531 __func__, io->io_hdr.msg_type); 12532 break; 12533 } 12534 if (free_io) 12535 ctl_free_io(io); 12536 12537} 12538 12539 12540/* 12541 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12542 * there is no match. 12543 */ 12544static ctl_lun_error_pattern 12545ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12546{ 12547 const struct ctl_cmd_entry *entry; 12548 ctl_lun_error_pattern filtered_pattern, pattern; 12549 12550 pattern = desc->error_pattern; 12551 12552 /* 12553 * XXX KDM we need more data passed into this function to match a 12554 * custom pattern, and we actually need to implement custom pattern 12555 * matching. 12556 */ 12557 if (pattern & CTL_LUN_PAT_CMD) 12558 return (CTL_LUN_PAT_CMD); 12559 12560 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12561 return (CTL_LUN_PAT_ANY); 12562 12563 entry = ctl_get_cmd_entry(ctsio, NULL); 12564 12565 filtered_pattern = entry->pattern & pattern; 12566 12567 /* 12568 * If the user requested specific flags in the pattern (e.g. 12569 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12570 * flags. 12571 * 12572 * If the user did not specify any flags, it doesn't matter whether 12573 * or not the command supports the flags. 12574 */ 12575 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12576 (pattern & ~CTL_LUN_PAT_MASK)) 12577 return (CTL_LUN_PAT_NONE); 12578 12579 /* 12580 * If the user asked for a range check, see if the requested LBA 12581 * range overlaps with this command's LBA range. 12582 */ 12583 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12584 uint64_t lba1; 12585 uint64_t len1; 12586 ctl_action action; 12587 int retval; 12588 12589 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12590 if (retval != 0) 12591 return (CTL_LUN_PAT_NONE); 12592 12593 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12594 desc->lba_range.len, FALSE); 12595 /* 12596 * A "pass" means that the LBA ranges don't overlap, so 12597 * this doesn't match the user's range criteria. 12598 */ 12599 if (action == CTL_ACTION_PASS) 12600 return (CTL_LUN_PAT_NONE); 12601 } 12602 12603 return (filtered_pattern); 12604} 12605 12606static void 12607ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12608{ 12609 struct ctl_error_desc *desc, *desc2; 12610 12611 mtx_assert(&lun->lun_lock, MA_OWNED); 12612 12613 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12614 ctl_lun_error_pattern pattern; 12615 /* 12616 * Check to see whether this particular command matches 12617 * the pattern in the descriptor. 12618 */ 12619 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12620 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12621 continue; 12622 12623 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12624 case CTL_LUN_INJ_ABORTED: 12625 ctl_set_aborted(&io->scsiio); 12626 break; 12627 case CTL_LUN_INJ_MEDIUM_ERR: 12628 ctl_set_medium_error(&io->scsiio); 12629 break; 12630 case CTL_LUN_INJ_UA: 12631 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12632 * OCCURRED */ 12633 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12634 break; 12635 case CTL_LUN_INJ_CUSTOM: 12636 /* 12637 * We're assuming the user knows what he is doing. 12638 * Just copy the sense information without doing 12639 * checks. 12640 */ 12641 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12642 MIN(sizeof(desc->custom_sense), 12643 sizeof(io->scsiio.sense_data))); 12644 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12645 io->scsiio.sense_len = SSD_FULL_SIZE; 12646 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12647 break; 12648 case CTL_LUN_INJ_NONE: 12649 default: 12650 /* 12651 * If this is an error injection type we don't know 12652 * about, clear the continuous flag (if it is set) 12653 * so it will get deleted below. 12654 */ 12655 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12656 break; 12657 } 12658 /* 12659 * By default, each error injection action is a one-shot 12660 */ 12661 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12662 continue; 12663 12664 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12665 12666 free(desc, M_CTL); 12667 } 12668} 12669 12670#ifdef CTL_IO_DELAY 12671static void 12672ctl_datamove_timer_wakeup(void *arg) 12673{ 12674 union ctl_io *io; 12675 12676 io = (union ctl_io *)arg; 12677 12678 ctl_datamove(io); 12679} 12680#endif /* CTL_IO_DELAY */ 12681 12682void 12683ctl_datamove(union ctl_io *io) 12684{ 12685 void (*fe_datamove)(union ctl_io *io); 12686 12687 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12688 12689 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12690 12691#ifdef CTL_TIME_IO 12692 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12693 char str[256]; 12694 char path_str[64]; 12695 struct sbuf sb; 12696 12697 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12698 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12699 12700 sbuf_cat(&sb, path_str); 12701 switch (io->io_hdr.io_type) { 12702 case CTL_IO_SCSI: 12703 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12704 sbuf_printf(&sb, "\n"); 12705 sbuf_cat(&sb, path_str); 12706 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12707 io->scsiio.tag_num, io->scsiio.tag_type); 12708 break; 12709 case CTL_IO_TASK: 12710 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12711 "Tag Type: %d\n", io->taskio.task_action, 12712 io->taskio.tag_num, io->taskio.tag_type); 12713 break; 12714 default: 12715 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12716 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12717 break; 12718 } 12719 sbuf_cat(&sb, path_str); 12720 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12721 (intmax_t)time_uptime - io->io_hdr.start_time); 12722 sbuf_finish(&sb); 12723 printf("%s", sbuf_data(&sb)); 12724 } 12725#endif /* CTL_TIME_IO */ 12726 12727#ifdef CTL_IO_DELAY 12728 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12729 struct ctl_lun *lun; 12730 12731 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12732 12733 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12734 } else { 12735 struct ctl_lun *lun; 12736 12737 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12738 if ((lun != NULL) 12739 && (lun->delay_info.datamove_delay > 0)) { 12740 struct callout *callout; 12741 12742 callout = (struct callout *)&io->io_hdr.timer_bytes; 12743 callout_init(callout, /*mpsafe*/ 1); 12744 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12745 callout_reset(callout, 12746 lun->delay_info.datamove_delay * hz, 12747 ctl_datamove_timer_wakeup, io); 12748 if (lun->delay_info.datamove_type == 12749 CTL_DELAY_TYPE_ONESHOT) 12750 lun->delay_info.datamove_delay = 0; 12751 return; 12752 } 12753 } 12754#endif 12755 12756 /* 12757 * This command has been aborted. Set the port status, so we fail 12758 * the data move. 12759 */ 12760 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12761 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12762 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12763 io->io_hdr.nexus.targ_port, 12764 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12765 io->io_hdr.nexus.targ_lun); 12766 io->io_hdr.port_status = 31337; 12767 /* 12768 * Note that the backend, in this case, will get the 12769 * callback in its context. In other cases it may get 12770 * called in the frontend's interrupt thread context. 12771 */ 12772 io->scsiio.be_move_done(io); 12773 return; 12774 } 12775 12776 /* Don't confuse frontend with zero length data move. */ 12777 if (io->scsiio.kern_data_len == 0) { 12778 io->scsiio.be_move_done(io); 12779 return; 12780 } 12781 12782 /* 12783 * If we're in XFER mode and this I/O is from the other shelf 12784 * controller, we need to send the DMA to the other side to 12785 * actually transfer the data to/from the host. In serialize only 12786 * mode the transfer happens below CTL and ctl_datamove() is only 12787 * called on the machine that originally received the I/O. 12788 */ 12789 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12790 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12791 union ctl_ha_msg msg; 12792 uint32_t sg_entries_sent; 12793 int do_sg_copy; 12794 int i; 12795 12796 memset(&msg, 0, sizeof(msg)); 12797 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12798 msg.hdr.original_sc = io->io_hdr.original_sc; 12799 msg.hdr.serializing_sc = io; 12800 msg.hdr.nexus = io->io_hdr.nexus; 12801 msg.dt.flags = io->io_hdr.flags; 12802 /* 12803 * We convert everything into a S/G list here. We can't 12804 * pass by reference, only by value between controllers. 12805 * So we can't pass a pointer to the S/G list, only as many 12806 * S/G entries as we can fit in here. If it's possible for 12807 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12808 * then we need to break this up into multiple transfers. 12809 */ 12810 if (io->scsiio.kern_sg_entries == 0) { 12811 msg.dt.kern_sg_entries = 1; 12812 /* 12813 * If this is in cached memory, flush the cache 12814 * before we send the DMA request to the other 12815 * controller. We want to do this in either the 12816 * read or the write case. The read case is 12817 * straightforward. In the write case, we want to 12818 * make sure nothing is in the local cache that 12819 * could overwrite the DMAed data. 12820 */ 12821 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12822 /* 12823 * XXX KDM use bus_dmamap_sync() here. 12824 */ 12825 } 12826 12827 /* 12828 * Convert to a physical address if this is a 12829 * virtual address. 12830 */ 12831 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12832 msg.dt.sg_list[0].addr = 12833 io->scsiio.kern_data_ptr; 12834 } else { 12835 /* 12836 * XXX KDM use busdma here! 12837 */ 12838#if 0 12839 msg.dt.sg_list[0].addr = (void *) 12840 vtophys(io->scsiio.kern_data_ptr); 12841#endif 12842 } 12843 12844 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12845 do_sg_copy = 0; 12846 } else { 12847 struct ctl_sg_entry *sgl; 12848 12849 do_sg_copy = 1; 12850 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12851 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12852 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12853 /* 12854 * XXX KDM use bus_dmamap_sync() here. 12855 */ 12856 } 12857 } 12858 12859 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12860 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12861 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12862 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12863 msg.dt.sg_sequence = 0; 12864 12865 /* 12866 * Loop until we've sent all of the S/G entries. On the 12867 * other end, we'll recompose these S/G entries into one 12868 * contiguous list before passing it to the 12869 */ 12870 for (sg_entries_sent = 0; sg_entries_sent < 12871 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12872 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/ 12873 sizeof(msg.dt.sg_list[0])), 12874 msg.dt.kern_sg_entries - sg_entries_sent); 12875 12876 if (do_sg_copy != 0) { 12877 struct ctl_sg_entry *sgl; 12878 int j; 12879 12880 sgl = (struct ctl_sg_entry *) 12881 io->scsiio.kern_data_ptr; 12882 /* 12883 * If this is in cached memory, flush the cache 12884 * before we send the DMA request to the other 12885 * controller. We want to do this in either 12886 * the * read or the write case. The read 12887 * case is straightforward. In the write 12888 * case, we want to make sure nothing is 12889 * in the local cache that could overwrite 12890 * the DMAed data. 12891 */ 12892 12893 for (i = sg_entries_sent, j = 0; 12894 i < msg.dt.cur_sg_entries; i++, j++) { 12895 if ((io->io_hdr.flags & 12896 CTL_FLAG_NO_DATASYNC) == 0) { 12897 /* 12898 * XXX KDM use bus_dmamap_sync() 12899 */ 12900 } 12901 if ((io->io_hdr.flags & 12902 CTL_FLAG_BUS_ADDR) == 0) { 12903 /* 12904 * XXX KDM use busdma. 12905 */ 12906#if 0 12907 msg.dt.sg_list[j].addr =(void *) 12908 vtophys(sgl[i].addr); 12909#endif 12910 } else { 12911 msg.dt.sg_list[j].addr = 12912 sgl[i].addr; 12913 } 12914 msg.dt.sg_list[j].len = sgl[i].len; 12915 } 12916 } 12917 12918 sg_entries_sent += msg.dt.cur_sg_entries; 12919 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12920 msg.dt.sg_last = 1; 12921 else 12922 msg.dt.sg_last = 0; 12923 12924 /* 12925 * XXX KDM drop and reacquire the lock here? 12926 */ 12927 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12928 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12929 /* 12930 * XXX do something here. 12931 */ 12932 } 12933 12934 msg.dt.sent_sg_entries = sg_entries_sent; 12935 } 12936 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12937 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12938 ctl_failover_io(io, /*have_lock*/ 0); 12939 12940 } else { 12941 12942 /* 12943 * Lookup the fe_datamove() function for this particular 12944 * front end. 12945 */ 12946 fe_datamove = 12947 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12948 12949 fe_datamove(io); 12950 } 12951} 12952 12953static void 12954ctl_send_datamove_done(union ctl_io *io, int have_lock) 12955{ 12956 union ctl_ha_msg msg; 12957 int isc_status; 12958 12959 memset(&msg, 0, sizeof(msg)); 12960 12961 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12962 msg.hdr.original_sc = io; 12963 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12964 msg.hdr.nexus = io->io_hdr.nexus; 12965 msg.hdr.status = io->io_hdr.status; 12966 msg.scsi.tag_num = io->scsiio.tag_num; 12967 msg.scsi.tag_type = io->scsiio.tag_type; 12968 msg.scsi.scsi_status = io->scsiio.scsi_status; 12969 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12970 sizeof(io->scsiio.sense_data)); 12971 msg.scsi.sense_len = io->scsiio.sense_len; 12972 msg.scsi.sense_residual = io->scsiio.sense_residual; 12973 msg.scsi.fetd_status = io->io_hdr.port_status; 12974 msg.scsi.residual = io->scsiio.residual; 12975 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12976 12977 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12978 ctl_failover_io(io, /*have_lock*/ have_lock); 12979 return; 12980 } 12981 12982 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12983 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12984 /* XXX do something if this fails */ 12985 } 12986 12987} 12988 12989/* 12990 * The DMA to the remote side is done, now we need to tell the other side 12991 * we're done so it can continue with its data movement. 12992 */ 12993static void 12994ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12995{ 12996 union ctl_io *io; 12997 12998 io = rq->context; 12999 13000 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13001 printf("%s: ISC DMA write failed with error %d", __func__, 13002 rq->ret); 13003 ctl_set_internal_failure(&io->scsiio, 13004 /*sks_valid*/ 1, 13005 /*retry_count*/ rq->ret); 13006 } 13007 13008 ctl_dt_req_free(rq); 13009 13010 /* 13011 * In this case, we had to malloc the memory locally. Free it. 13012 */ 13013 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13014 int i; 13015 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13016 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13017 } 13018 /* 13019 * The data is in local and remote memory, so now we need to send 13020 * status (good or back) back to the other side. 13021 */ 13022 ctl_send_datamove_done(io, /*have_lock*/ 0); 13023} 13024 13025/* 13026 * We've moved the data from the host/controller into local memory. Now we 13027 * need to push it over to the remote controller's memory. 13028 */ 13029static int 13030ctl_datamove_remote_dm_write_cb(union ctl_io *io) 13031{ 13032 int retval; 13033 13034 retval = 0; 13035 13036 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 13037 ctl_datamove_remote_write_cb); 13038 13039 return (retval); 13040} 13041 13042static void 13043ctl_datamove_remote_write(union ctl_io *io) 13044{ 13045 int retval; 13046 void (*fe_datamove)(union ctl_io *io); 13047 13048 /* 13049 * - Get the data from the host/HBA into local memory. 13050 * - DMA memory from the local controller to the remote controller. 13051 * - Send status back to the remote controller. 13052 */ 13053 13054 retval = ctl_datamove_remote_sgl_setup(io); 13055 if (retval != 0) 13056 return; 13057 13058 /* Switch the pointer over so the FETD knows what to do */ 13059 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13060 13061 /* 13062 * Use a custom move done callback, since we need to send completion 13063 * back to the other controller, not to the backend on this side. 13064 */ 13065 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 13066 13067 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13068 13069 fe_datamove(io); 13070 13071 return; 13072 13073} 13074 13075static int 13076ctl_datamove_remote_dm_read_cb(union ctl_io *io) 13077{ 13078#if 0 13079 char str[256]; 13080 char path_str[64]; 13081 struct sbuf sb; 13082#endif 13083 13084 /* 13085 * In this case, we had to malloc the memory locally. Free it. 13086 */ 13087 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 13088 int i; 13089 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13090 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13091 } 13092 13093#if 0 13094 scsi_path_string(io, path_str, sizeof(path_str)); 13095 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13096 sbuf_cat(&sb, path_str); 13097 scsi_command_string(&io->scsiio, NULL, &sb); 13098 sbuf_printf(&sb, "\n"); 13099 sbuf_cat(&sb, path_str); 13100 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13101 io->scsiio.tag_num, io->scsiio.tag_type); 13102 sbuf_cat(&sb, path_str); 13103 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 13104 io->io_hdr.flags, io->io_hdr.status); 13105 sbuf_finish(&sb); 13106 printk("%s", sbuf_data(&sb)); 13107#endif 13108 13109 13110 /* 13111 * The read is done, now we need to send status (good or bad) back 13112 * to the other side. 13113 */ 13114 ctl_send_datamove_done(io, /*have_lock*/ 0); 13115 13116 return (0); 13117} 13118 13119static void 13120ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 13121{ 13122 union ctl_io *io; 13123 void (*fe_datamove)(union ctl_io *io); 13124 13125 io = rq->context; 13126 13127 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 13128 printf("%s: ISC DMA read failed with error %d", __func__, 13129 rq->ret); 13130 ctl_set_internal_failure(&io->scsiio, 13131 /*sks_valid*/ 1, 13132 /*retry_count*/ rq->ret); 13133 } 13134 13135 ctl_dt_req_free(rq); 13136 13137 /* Switch the pointer over so the FETD knows what to do */ 13138 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 13139 13140 /* 13141 * Use a custom move done callback, since we need to send completion 13142 * back to the other controller, not to the backend on this side. 13143 */ 13144 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 13145 13146 /* XXX KDM add checks like the ones in ctl_datamove? */ 13147 13148 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 13149 13150 fe_datamove(io); 13151} 13152 13153static int 13154ctl_datamove_remote_sgl_setup(union ctl_io *io) 13155{ 13156 struct ctl_sg_entry *local_sglist, *remote_sglist; 13157 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 13158 struct ctl_softc *softc; 13159 int retval; 13160 int i; 13161 13162 retval = 0; 13163 softc = control_softc; 13164 13165 local_sglist = io->io_hdr.local_sglist; 13166 local_dma_sglist = io->io_hdr.local_dma_sglist; 13167 remote_sglist = io->io_hdr.remote_sglist; 13168 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13169 13170 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 13171 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 13172 local_sglist[i].len = remote_sglist[i].len; 13173 13174 /* 13175 * XXX Detect the situation where the RS-level I/O 13176 * redirector on the other side has already read the 13177 * data off of the AOR RS on this side, and 13178 * transferred it to remote (mirror) memory on the 13179 * other side. Since we already have the data in 13180 * memory here, we just need to use it. 13181 * 13182 * XXX KDM this can probably be removed once we 13183 * get the cache device code in and take the 13184 * current AOR implementation out. 13185 */ 13186#ifdef NEEDTOPORT 13187 if ((remote_sglist[i].addr >= 13188 (void *)vtophys(softc->mirr->addr)) 13189 && (remote_sglist[i].addr < 13190 ((void *)vtophys(softc->mirr->addr) + 13191 CacheMirrorOffset))) { 13192 local_sglist[i].addr = remote_sglist[i].addr - 13193 CacheMirrorOffset; 13194 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13195 CTL_FLAG_DATA_IN) 13196 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 13197 } else { 13198 local_sglist[i].addr = remote_sglist[i].addr + 13199 CacheMirrorOffset; 13200 } 13201#endif 13202#if 0 13203 printf("%s: local %p, remote %p, len %d\n", 13204 __func__, local_sglist[i].addr, 13205 remote_sglist[i].addr, local_sglist[i].len); 13206#endif 13207 } 13208 } else { 13209 uint32_t len_to_go; 13210 13211 /* 13212 * In this case, we don't have automatically allocated 13213 * memory for this I/O on this controller. This typically 13214 * happens with internal CTL I/O -- e.g. inquiry, mode 13215 * sense, etc. Anything coming from RAIDCore will have 13216 * a mirror area available. 13217 */ 13218 len_to_go = io->scsiio.kern_data_len; 13219 13220 /* 13221 * Clear the no datasync flag, we have to use malloced 13222 * buffers. 13223 */ 13224 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 13225 13226 /* 13227 * The difficult thing here is that the size of the various 13228 * S/G segments may be different than the size from the 13229 * remote controller. That'll make it harder when DMAing 13230 * the data back to the other side. 13231 */ 13232 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 13233 sizeof(io->io_hdr.remote_sglist[0])) && 13234 (len_to_go > 0); i++) { 13235 local_sglist[i].len = MIN(len_to_go, 131072); 13236 CTL_SIZE_8B(local_dma_sglist[i].len, 13237 local_sglist[i].len); 13238 local_sglist[i].addr = 13239 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 13240 13241 local_dma_sglist[i].addr = local_sglist[i].addr; 13242 13243 if (local_sglist[i].addr == NULL) { 13244 int j; 13245 13246 printf("malloc failed for %zd bytes!", 13247 local_dma_sglist[i].len); 13248 for (j = 0; j < i; j++) { 13249 free(local_sglist[j].addr, M_CTL); 13250 } 13251 ctl_set_internal_failure(&io->scsiio, 13252 /*sks_valid*/ 1, 13253 /*retry_count*/ 4857); 13254 retval = 1; 13255 goto bailout_error; 13256 13257 } 13258 /* XXX KDM do we need a sync here? */ 13259 13260 len_to_go -= local_sglist[i].len; 13261 } 13262 /* 13263 * Reset the number of S/G entries accordingly. The 13264 * original number of S/G entries is available in 13265 * rem_sg_entries. 13266 */ 13267 io->scsiio.kern_sg_entries = i; 13268 13269#if 0 13270 printf("%s: kern_sg_entries = %d\n", __func__, 13271 io->scsiio.kern_sg_entries); 13272 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13273 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13274 local_sglist[i].addr, local_sglist[i].len, 13275 local_dma_sglist[i].len); 13276#endif 13277 } 13278 13279 13280 return (retval); 13281 13282bailout_error: 13283 13284 ctl_send_datamove_done(io, /*have_lock*/ 0); 13285 13286 return (retval); 13287} 13288 13289static int 13290ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13291 ctl_ha_dt_cb callback) 13292{ 13293 struct ctl_ha_dt_req *rq; 13294 struct ctl_sg_entry *remote_sglist, *local_sglist; 13295 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13296 uint32_t local_used, remote_used, total_used; 13297 int retval; 13298 int i, j; 13299 13300 retval = 0; 13301 13302 rq = ctl_dt_req_alloc(); 13303 13304 /* 13305 * If we failed to allocate the request, and if the DMA didn't fail 13306 * anyway, set busy status. This is just a resource allocation 13307 * failure. 13308 */ 13309 if ((rq == NULL) 13310 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13311 ctl_set_busy(&io->scsiio); 13312 13313 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13314 13315 if (rq != NULL) 13316 ctl_dt_req_free(rq); 13317 13318 /* 13319 * The data move failed. We need to return status back 13320 * to the other controller. No point in trying to DMA 13321 * data to the remote controller. 13322 */ 13323 13324 ctl_send_datamove_done(io, /*have_lock*/ 0); 13325 13326 retval = 1; 13327 13328 goto bailout; 13329 } 13330 13331 local_sglist = io->io_hdr.local_sglist; 13332 local_dma_sglist = io->io_hdr.local_dma_sglist; 13333 remote_sglist = io->io_hdr.remote_sglist; 13334 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13335 local_used = 0; 13336 remote_used = 0; 13337 total_used = 0; 13338 13339 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13340 rq->ret = CTL_HA_STATUS_SUCCESS; 13341 rq->context = io; 13342 callback(rq); 13343 goto bailout; 13344 } 13345 13346 /* 13347 * Pull/push the data over the wire from/to the other controller. 13348 * This takes into account the possibility that the local and 13349 * remote sglists may not be identical in terms of the size of 13350 * the elements and the number of elements. 13351 * 13352 * One fundamental assumption here is that the length allocated for 13353 * both the local and remote sglists is identical. Otherwise, we've 13354 * essentially got a coding error of some sort. 13355 */ 13356 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13357 int isc_ret; 13358 uint32_t cur_len, dma_length; 13359 uint8_t *tmp_ptr; 13360 13361 rq->id = CTL_HA_DATA_CTL; 13362 rq->command = command; 13363 rq->context = io; 13364 13365 /* 13366 * Both pointers should be aligned. But it is possible 13367 * that the allocation length is not. They should both 13368 * also have enough slack left over at the end, though, 13369 * to round up to the next 8 byte boundary. 13370 */ 13371 cur_len = MIN(local_sglist[i].len - local_used, 13372 remote_sglist[j].len - remote_used); 13373 13374 /* 13375 * In this case, we have a size issue and need to decrease 13376 * the size, except in the case where we actually have less 13377 * than 8 bytes left. In that case, we need to increase 13378 * the DMA length to get the last bit. 13379 */ 13380 if ((cur_len & 0x7) != 0) { 13381 if (cur_len > 0x7) { 13382 cur_len = cur_len - (cur_len & 0x7); 13383 dma_length = cur_len; 13384 } else { 13385 CTL_SIZE_8B(dma_length, cur_len); 13386 } 13387 13388 } else 13389 dma_length = cur_len; 13390 13391 /* 13392 * If we had to allocate memory for this I/O, instead of using 13393 * the non-cached mirror memory, we'll need to flush the cache 13394 * before trying to DMA to the other controller. 13395 * 13396 * We could end up doing this multiple times for the same 13397 * segment if we have a larger local segment than remote 13398 * segment. That shouldn't be an issue. 13399 */ 13400 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13401 /* 13402 * XXX KDM use bus_dmamap_sync() here. 13403 */ 13404 } 13405 13406 rq->size = dma_length; 13407 13408 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13409 tmp_ptr += local_used; 13410 13411 /* Use physical addresses when talking to ISC hardware */ 13412 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13413 /* XXX KDM use busdma */ 13414#if 0 13415 rq->local = vtophys(tmp_ptr); 13416#endif 13417 } else 13418 rq->local = tmp_ptr; 13419 13420 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13421 tmp_ptr += remote_used; 13422 rq->remote = tmp_ptr; 13423 13424 rq->callback = NULL; 13425 13426 local_used += cur_len; 13427 if (local_used >= local_sglist[i].len) { 13428 i++; 13429 local_used = 0; 13430 } 13431 13432 remote_used += cur_len; 13433 if (remote_used >= remote_sglist[j].len) { 13434 j++; 13435 remote_used = 0; 13436 } 13437 total_used += cur_len; 13438 13439 if (total_used >= io->scsiio.kern_data_len) 13440 rq->callback = callback; 13441 13442 if ((rq->size & 0x7) != 0) { 13443 printf("%s: warning: size %d is not on 8b boundary\n", 13444 __func__, rq->size); 13445 } 13446 if (((uintptr_t)rq->local & 0x7) != 0) { 13447 printf("%s: warning: local %p not on 8b boundary\n", 13448 __func__, rq->local); 13449 } 13450 if (((uintptr_t)rq->remote & 0x7) != 0) { 13451 printf("%s: warning: remote %p not on 8b boundary\n", 13452 __func__, rq->local); 13453 } 13454#if 0 13455 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13456 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13457 rq->local, rq->remote, rq->size); 13458#endif 13459 13460 isc_ret = ctl_dt_single(rq); 13461 if (isc_ret == CTL_HA_STATUS_WAIT) 13462 continue; 13463 13464 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13465 rq->ret = CTL_HA_STATUS_SUCCESS; 13466 } else { 13467 rq->ret = isc_ret; 13468 } 13469 callback(rq); 13470 goto bailout; 13471 } 13472 13473bailout: 13474 return (retval); 13475 13476} 13477 13478static void 13479ctl_datamove_remote_read(union ctl_io *io) 13480{ 13481 int retval; 13482 int i; 13483 13484 /* 13485 * This will send an error to the other controller in the case of a 13486 * failure. 13487 */ 13488 retval = ctl_datamove_remote_sgl_setup(io); 13489 if (retval != 0) 13490 return; 13491 13492 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13493 ctl_datamove_remote_read_cb); 13494 if ((retval != 0) 13495 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13496 /* 13497 * Make sure we free memory if there was an error.. The 13498 * ctl_datamove_remote_xfer() function will send the 13499 * datamove done message, or call the callback with an 13500 * error if there is a problem. 13501 */ 13502 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13503 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13504 } 13505 13506 return; 13507} 13508 13509/* 13510 * Process a datamove request from the other controller. This is used for 13511 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13512 * first. Once that is complete, the data gets DMAed into the remote 13513 * controller's memory. For reads, we DMA from the remote controller's 13514 * memory into our memory first, and then move it out to the FETD. 13515 */ 13516static void 13517ctl_datamove_remote(union ctl_io *io) 13518{ 13519 struct ctl_softc *softc; 13520 13521 softc = control_softc; 13522 13523 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13524 13525 /* 13526 * Note that we look for an aborted I/O here, but don't do some of 13527 * the other checks that ctl_datamove() normally does. 13528 * We don't need to run the datamove delay code, since that should 13529 * have been done if need be on the other controller. 13530 */ 13531 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13532 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13533 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13534 io->io_hdr.nexus.targ_port, 13535 io->io_hdr.nexus.targ_target.id, 13536 io->io_hdr.nexus.targ_lun); 13537 io->io_hdr.port_status = 31338; 13538 ctl_send_datamove_done(io, /*have_lock*/ 0); 13539 return; 13540 } 13541 13542 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13543 ctl_datamove_remote_write(io); 13544 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13545 ctl_datamove_remote_read(io); 13546 } else { 13547 union ctl_ha_msg msg; 13548 struct scsi_sense_data *sense; 13549 uint8_t sks[3]; 13550 int retry_count; 13551 13552 memset(&msg, 0, sizeof(msg)); 13553 13554 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13555 msg.hdr.status = CTL_SCSI_ERROR; 13556 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13557 13558 retry_count = 4243; 13559 13560 sense = &msg.scsi.sense_data; 13561 sks[0] = SSD_SCS_VALID; 13562 sks[1] = (retry_count >> 8) & 0xff; 13563 sks[2] = retry_count & 0xff; 13564 13565 /* "Internal target failure" */ 13566 scsi_set_sense_data(sense, 13567 /*sense_format*/ SSD_TYPE_NONE, 13568 /*current_error*/ 1, 13569 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13570 /*asc*/ 0x44, 13571 /*ascq*/ 0x00, 13572 /*type*/ SSD_ELEM_SKS, 13573 /*size*/ sizeof(sks), 13574 /*data*/ sks, 13575 SSD_ELEM_NONE); 13576 13577 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13578 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13579 ctl_failover_io(io, /*have_lock*/ 1); 13580 return; 13581 } 13582 13583 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13584 CTL_HA_STATUS_SUCCESS) { 13585 /* XXX KDM what to do if this fails? */ 13586 } 13587 return; 13588 } 13589 13590} 13591 13592static int 13593ctl_process_done(union ctl_io *io) 13594{ 13595 struct ctl_lun *lun; 13596 struct ctl_softc *softc = control_softc; 13597 void (*fe_done)(union ctl_io *io); 13598 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13599 13600 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13601 13602 fe_done = softc->ctl_ports[targ_port]->fe_done; 13603 13604#ifdef CTL_TIME_IO 13605 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13606 char str[256]; 13607 char path_str[64]; 13608 struct sbuf sb; 13609 13610 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13611 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13612 13613 sbuf_cat(&sb, path_str); 13614 switch (io->io_hdr.io_type) { 13615 case CTL_IO_SCSI: 13616 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13617 sbuf_printf(&sb, "\n"); 13618 sbuf_cat(&sb, path_str); 13619 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13620 io->scsiio.tag_num, io->scsiio.tag_type); 13621 break; 13622 case CTL_IO_TASK: 13623 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13624 "Tag Type: %d\n", io->taskio.task_action, 13625 io->taskio.tag_num, io->taskio.tag_type); 13626 break; 13627 default: 13628 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13629 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13630 break; 13631 } 13632 sbuf_cat(&sb, path_str); 13633 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13634 (intmax_t)time_uptime - io->io_hdr.start_time); 13635 sbuf_finish(&sb); 13636 printf("%s", sbuf_data(&sb)); 13637 } 13638#endif /* CTL_TIME_IO */ 13639 13640 switch (io->io_hdr.io_type) { 13641 case CTL_IO_SCSI: 13642 break; 13643 case CTL_IO_TASK: 13644 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13645 ctl_io_error_print(io, NULL); 13646 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13647 ctl_free_io(io); 13648 else 13649 fe_done(io); 13650 return (CTL_RETVAL_COMPLETE); 13651 default: 13652 panic("ctl_process_done: invalid io type %d\n", 13653 io->io_hdr.io_type); 13654 break; /* NOTREACHED */ 13655 } 13656 13657 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13658 if (lun == NULL) { 13659 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13660 io->io_hdr.nexus.targ_mapped_lun)); 13661 goto bailout; 13662 } 13663 13664 mtx_lock(&lun->lun_lock); 13665 13666 /* 13667 * Check to see if we have any errors to inject here. We only 13668 * inject errors for commands that don't already have errors set. 13669 */ 13670 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13671 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13672 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13673 ctl_inject_error(lun, io); 13674 13675 /* 13676 * XXX KDM how do we treat commands that aren't completed 13677 * successfully? 13678 * 13679 * XXX KDM should we also track I/O latency? 13680 */ 13681 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13682 io->io_hdr.io_type == CTL_IO_SCSI) { 13683#ifdef CTL_TIME_IO 13684 struct bintime cur_bt; 13685#endif 13686 int type; 13687 13688 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13689 CTL_FLAG_DATA_IN) 13690 type = CTL_STATS_READ; 13691 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13692 CTL_FLAG_DATA_OUT) 13693 type = CTL_STATS_WRITE; 13694 else 13695 type = CTL_STATS_NO_IO; 13696 13697 lun->stats.ports[targ_port].bytes[type] += 13698 io->scsiio.kern_total_len; 13699 lun->stats.ports[targ_port].operations[type]++; 13700#ifdef CTL_TIME_IO 13701 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13702 &io->io_hdr.dma_bt); 13703 lun->stats.ports[targ_port].num_dmas[type] += 13704 io->io_hdr.num_dmas; 13705 getbintime(&cur_bt); 13706 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13707 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13708#endif 13709 } 13710 13711 /* 13712 * Remove this from the OOA queue. 13713 */ 13714 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13715#ifdef CTL_TIME_IO 13716 if (TAILQ_EMPTY(&lun->ooa_queue)) 13717 lun->last_busy = getsbinuptime(); 13718#endif 13719 13720 /* 13721 * Run through the blocked queue on this LUN and see if anything 13722 * has become unblocked, now that this transaction is done. 13723 */ 13724 ctl_check_blocked(lun); 13725 13726 /* 13727 * If the LUN has been invalidated, free it if there is nothing 13728 * left on its OOA queue. 13729 */ 13730 if ((lun->flags & CTL_LUN_INVALID) 13731 && TAILQ_EMPTY(&lun->ooa_queue)) { 13732 mtx_unlock(&lun->lun_lock); 13733 mtx_lock(&softc->ctl_lock); 13734 ctl_free_lun(lun); 13735 mtx_unlock(&softc->ctl_lock); 13736 } else 13737 mtx_unlock(&lun->lun_lock); 13738 13739bailout: 13740 13741 /* 13742 * If this command has been aborted, make sure we set the status 13743 * properly. The FETD is responsible for freeing the I/O and doing 13744 * whatever it needs to do to clean up its state. 13745 */ 13746 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13747 ctl_set_task_aborted(&io->scsiio); 13748 13749 /* 13750 * If enabled, print command error status. 13751 * We don't print UAs unless debugging was enabled explicitly. 13752 */ 13753 do { 13754 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13755 break; 13756 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13757 break; 13758 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13759 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13760 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13761 int error_code, sense_key, asc, ascq; 13762 13763 scsi_extract_sense_len(&io->scsiio.sense_data, 13764 io->scsiio.sense_len, &error_code, &sense_key, 13765 &asc, &ascq, /*show_errors*/ 0); 13766 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13767 break; 13768 } 13769 13770 ctl_io_error_print(io, NULL); 13771 } while (0); 13772 13773 /* 13774 * Tell the FETD or the other shelf controller we're done with this 13775 * command. Note that only SCSI commands get to this point. Task 13776 * management commands are completed above. 13777 * 13778 * We only send status to the other controller if we're in XFER 13779 * mode. In SER_ONLY mode, the I/O is done on the controller that 13780 * received the I/O (from CTL's perspective), and so the status is 13781 * generated there. 13782 * 13783 * XXX KDM if we hold the lock here, we could cause a deadlock 13784 * if the frontend comes back in in this context to queue 13785 * something. 13786 */ 13787 if ((softc->ha_mode == CTL_HA_MODE_XFER) 13788 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13789 union ctl_ha_msg msg; 13790 13791 memset(&msg, 0, sizeof(msg)); 13792 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13793 msg.hdr.original_sc = io->io_hdr.original_sc; 13794 msg.hdr.nexus = io->io_hdr.nexus; 13795 msg.hdr.status = io->io_hdr.status; 13796 msg.scsi.scsi_status = io->scsiio.scsi_status; 13797 msg.scsi.tag_num = io->scsiio.tag_num; 13798 msg.scsi.tag_type = io->scsiio.tag_type; 13799 msg.scsi.sense_len = io->scsiio.sense_len; 13800 msg.scsi.sense_residual = io->scsiio.sense_residual; 13801 msg.scsi.residual = io->scsiio.residual; 13802 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13803 sizeof(io->scsiio.sense_data)); 13804 /* 13805 * We copy this whether or not this is an I/O-related 13806 * command. Otherwise, we'd have to go and check to see 13807 * whether it's a read/write command, and it really isn't 13808 * worth it. 13809 */ 13810 memcpy(&msg.scsi.lbalen, 13811 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13812 sizeof(msg.scsi.lbalen)); 13813 13814 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13815 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13816 /* XXX do something here */ 13817 } 13818 13819 ctl_free_io(io); 13820 } else 13821 fe_done(io); 13822 13823 return (CTL_RETVAL_COMPLETE); 13824} 13825 13826#ifdef CTL_WITH_CA 13827/* 13828 * Front end should call this if it doesn't do autosense. When the request 13829 * sense comes back in from the initiator, we'll dequeue this and send it. 13830 */ 13831int 13832ctl_queue_sense(union ctl_io *io) 13833{ 13834 struct ctl_lun *lun; 13835 struct ctl_port *port; 13836 struct ctl_softc *softc; 13837 uint32_t initidx, targ_lun; 13838 13839 softc = control_softc; 13840 13841 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13842 13843 /* 13844 * LUN lookup will likely move to the ctl_work_thread() once we 13845 * have our new queueing infrastructure (that doesn't put things on 13846 * a per-LUN queue initially). That is so that we can handle 13847 * things like an INQUIRY to a LUN that we don't have enabled. We 13848 * can't deal with that right now. 13849 */ 13850 mtx_lock(&softc->ctl_lock); 13851 13852 /* 13853 * If we don't have a LUN for this, just toss the sense 13854 * information. 13855 */ 13856 port = ctl_io_port(&ctsio->io_hdr); 13857 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13858 if ((targ_lun < CTL_MAX_LUNS) 13859 && (softc->ctl_luns[targ_lun] != NULL)) 13860 lun = softc->ctl_luns[targ_lun]; 13861 else 13862 goto bailout; 13863 13864 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13865 13866 mtx_lock(&lun->lun_lock); 13867 /* 13868 * Already have CA set for this LUN...toss the sense information. 13869 */ 13870 if (ctl_is_set(lun->have_ca, initidx)) { 13871 mtx_unlock(&lun->lun_lock); 13872 goto bailout; 13873 } 13874 13875 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13876 MIN(sizeof(lun->pending_sense[initidx]), 13877 sizeof(io->scsiio.sense_data))); 13878 ctl_set_mask(lun->have_ca, initidx); 13879 mtx_unlock(&lun->lun_lock); 13880 13881bailout: 13882 mtx_unlock(&softc->ctl_lock); 13883 13884 ctl_free_io(io); 13885 13886 return (CTL_RETVAL_COMPLETE); 13887} 13888#endif 13889 13890/* 13891 * Primary command inlet from frontend ports. All SCSI and task I/O 13892 * requests must go through this function. 13893 */ 13894int 13895ctl_queue(union ctl_io *io) 13896{ 13897 struct ctl_port *port; 13898 13899 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13900 13901#ifdef CTL_TIME_IO 13902 io->io_hdr.start_time = time_uptime; 13903 getbintime(&io->io_hdr.start_bt); 13904#endif /* CTL_TIME_IO */ 13905 13906 /* Map FE-specific LUN ID into global one. */ 13907 port = ctl_io_port(&io->io_hdr); 13908 io->io_hdr.nexus.targ_mapped_lun = 13909 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13910 13911 switch (io->io_hdr.io_type) { 13912 case CTL_IO_SCSI: 13913 case CTL_IO_TASK: 13914 if (ctl_debug & CTL_DEBUG_CDB) 13915 ctl_io_print(io); 13916 ctl_enqueue_incoming(io); 13917 break; 13918 default: 13919 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13920 return (EINVAL); 13921 } 13922 13923 return (CTL_RETVAL_COMPLETE); 13924} 13925 13926#ifdef CTL_IO_DELAY 13927static void 13928ctl_done_timer_wakeup(void *arg) 13929{ 13930 union ctl_io *io; 13931 13932 io = (union ctl_io *)arg; 13933 ctl_done(io); 13934} 13935#endif /* CTL_IO_DELAY */ 13936 13937void 13938ctl_done(union ctl_io *io) 13939{ 13940 13941 /* 13942 * Enable this to catch duplicate completion issues. 13943 */ 13944#if 0 13945 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13946 printf("%s: type %d msg %d cdb %x iptl: " 13947 "%d:%d:%d:%d tag 0x%04x " 13948 "flag %#x status %x\n", 13949 __func__, 13950 io->io_hdr.io_type, 13951 io->io_hdr.msg_type, 13952 io->scsiio.cdb[0], 13953 io->io_hdr.nexus.initid.id, 13954 io->io_hdr.nexus.targ_port, 13955 io->io_hdr.nexus.targ_target.id, 13956 io->io_hdr.nexus.targ_lun, 13957 (io->io_hdr.io_type == 13958 CTL_IO_TASK) ? 13959 io->taskio.tag_num : 13960 io->scsiio.tag_num, 13961 io->io_hdr.flags, 13962 io->io_hdr.status); 13963 } else 13964 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13965#endif 13966 13967 /* 13968 * This is an internal copy of an I/O, and should not go through 13969 * the normal done processing logic. 13970 */ 13971 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13972 return; 13973 13974 /* 13975 * We need to send a msg to the serializing shelf to finish the IO 13976 * as well. We don't send a finish message to the other shelf if 13977 * this is a task management command. Task management commands 13978 * aren't serialized in the OOA queue, but rather just executed on 13979 * both shelf controllers for commands that originated on that 13980 * controller. 13981 */ 13982 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13983 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13984 union ctl_ha_msg msg_io; 13985 13986 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13987 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13988 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13989 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13990 } 13991 /* continue on to finish IO */ 13992 } 13993#ifdef CTL_IO_DELAY 13994 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13995 struct ctl_lun *lun; 13996 13997 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13998 13999 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 14000 } else { 14001 struct ctl_lun *lun; 14002 14003 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14004 14005 if ((lun != NULL) 14006 && (lun->delay_info.done_delay > 0)) { 14007 struct callout *callout; 14008 14009 callout = (struct callout *)&io->io_hdr.timer_bytes; 14010 callout_init(callout, /*mpsafe*/ 1); 14011 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 14012 callout_reset(callout, 14013 lun->delay_info.done_delay * hz, 14014 ctl_done_timer_wakeup, io); 14015 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 14016 lun->delay_info.done_delay = 0; 14017 return; 14018 } 14019 } 14020#endif /* CTL_IO_DELAY */ 14021 14022 ctl_enqueue_done(io); 14023} 14024 14025int 14026ctl_isc(struct ctl_scsiio *ctsio) 14027{ 14028 struct ctl_lun *lun; 14029 int retval; 14030 14031 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 14032 14033 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 14034 14035 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 14036 14037 retval = lun->backend->data_submit((union ctl_io *)ctsio); 14038 14039 return (retval); 14040} 14041 14042 14043static void 14044ctl_work_thread(void *arg) 14045{ 14046 struct ctl_thread *thr = (struct ctl_thread *)arg; 14047 struct ctl_softc *softc = thr->ctl_softc; 14048 union ctl_io *io; 14049 int retval; 14050 14051 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 14052 14053 for (;;) { 14054 retval = 0; 14055 14056 /* 14057 * We handle the queues in this order: 14058 * - ISC 14059 * - done queue (to free up resources, unblock other commands) 14060 * - RtR queue 14061 * - incoming queue 14062 * 14063 * If those queues are empty, we break out of the loop and 14064 * go to sleep. 14065 */ 14066 mtx_lock(&thr->queue_lock); 14067 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 14068 if (io != NULL) { 14069 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 14070 mtx_unlock(&thr->queue_lock); 14071 ctl_handle_isc(io); 14072 continue; 14073 } 14074 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 14075 if (io != NULL) { 14076 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 14077 /* clear any blocked commands, call fe_done */ 14078 mtx_unlock(&thr->queue_lock); 14079 retval = ctl_process_done(io); 14080 continue; 14081 } 14082 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 14083 if (io != NULL) { 14084 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 14085 mtx_unlock(&thr->queue_lock); 14086 if (io->io_hdr.io_type == CTL_IO_TASK) 14087 ctl_run_task(io); 14088 else 14089 ctl_scsiio_precheck(softc, &io->scsiio); 14090 continue; 14091 } 14092 if (!ctl_pause_rtr) { 14093 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 14094 if (io != NULL) { 14095 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 14096 mtx_unlock(&thr->queue_lock); 14097 retval = ctl_scsiio(&io->scsiio); 14098 if (retval != CTL_RETVAL_COMPLETE) 14099 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 14100 continue; 14101 } 14102 } 14103 14104 /* Sleep until we have something to do. */ 14105 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 14106 } 14107} 14108 14109static void 14110ctl_lun_thread(void *arg) 14111{ 14112 struct ctl_softc *softc = (struct ctl_softc *)arg; 14113 struct ctl_be_lun *be_lun; 14114 int retval; 14115 14116 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 14117 14118 for (;;) { 14119 retval = 0; 14120 mtx_lock(&softc->ctl_lock); 14121 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 14122 if (be_lun != NULL) { 14123 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 14124 mtx_unlock(&softc->ctl_lock); 14125 ctl_create_lun(be_lun); 14126 continue; 14127 } 14128 14129 /* Sleep until we have something to do. */ 14130 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 14131 PDROP | PRIBIO, "-", 0); 14132 } 14133} 14134 14135static void 14136ctl_thresh_thread(void *arg) 14137{ 14138 struct ctl_softc *softc = (struct ctl_softc *)arg; 14139 struct ctl_lun *lun; 14140 struct ctl_be_lun *be_lun; 14141 struct scsi_da_rw_recovery_page *rwpage; 14142 struct ctl_logical_block_provisioning_page *page; 14143 const char *attr; 14144 uint64_t thres, val; 14145 int i, e; 14146 14147 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 14148 14149 for (;;) { 14150 mtx_lock(&softc->ctl_lock); 14151 STAILQ_FOREACH(lun, &softc->lun_list, links) { 14152 be_lun = lun->be_lun; 14153 if ((lun->flags & CTL_LUN_DISABLED) || 14154 (lun->flags & CTL_LUN_OFFLINE) || 14155 lun->backend->lun_attr == NULL) 14156 continue; 14157 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 14158 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 14159 continue; 14160 e = 0; 14161 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 14162 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 14163 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 14164 continue; 14165 thres = scsi_4btoul(page->descr[i].count); 14166 thres <<= CTL_LBP_EXPONENT; 14167 switch (page->descr[i].resource) { 14168 case 0x01: 14169 attr = "blocksavail"; 14170 break; 14171 case 0x02: 14172 attr = "blocksused"; 14173 break; 14174 case 0xf1: 14175 attr = "poolblocksavail"; 14176 break; 14177 case 0xf2: 14178 attr = "poolblocksused"; 14179 break; 14180 default: 14181 continue; 14182 } 14183 mtx_unlock(&softc->ctl_lock); // XXX 14184 val = lun->backend->lun_attr( 14185 lun->be_lun->be_lun, attr); 14186 mtx_lock(&softc->ctl_lock); 14187 if (val == UINT64_MAX) 14188 continue; 14189 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 14190 == SLBPPD_ARMING_INC) 14191 e |= (val >= thres); 14192 else 14193 e |= (val <= thres); 14194 } 14195 mtx_lock(&lun->lun_lock); 14196 if (e) { 14197 if (lun->lasttpt == 0 || 14198 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 14199 lun->lasttpt = time_uptime; 14200 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14201 } 14202 } else { 14203 lun->lasttpt = 0; 14204 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 14205 } 14206 mtx_unlock(&lun->lun_lock); 14207 } 14208 mtx_unlock(&softc->ctl_lock); 14209 pause("-", CTL_LBP_PERIOD * hz); 14210 } 14211} 14212 14213static void 14214ctl_enqueue_incoming(union ctl_io *io) 14215{ 14216 struct ctl_softc *softc = control_softc; 14217 struct ctl_thread *thr; 14218 u_int idx; 14219 14220 idx = (io->io_hdr.nexus.targ_port * 127 + 14221 io->io_hdr.nexus.initid.id) % worker_threads; 14222 thr = &softc->threads[idx]; 14223 mtx_lock(&thr->queue_lock); 14224 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 14225 mtx_unlock(&thr->queue_lock); 14226 wakeup(thr); 14227} 14228 14229static void 14230ctl_enqueue_rtr(union ctl_io *io) 14231{ 14232 struct ctl_softc *softc = control_softc; 14233 struct ctl_thread *thr; 14234 14235 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14236 mtx_lock(&thr->queue_lock); 14237 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 14238 mtx_unlock(&thr->queue_lock); 14239 wakeup(thr); 14240} 14241 14242static void 14243ctl_enqueue_done(union ctl_io *io) 14244{ 14245 struct ctl_softc *softc = control_softc; 14246 struct ctl_thread *thr; 14247 14248 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14249 mtx_lock(&thr->queue_lock); 14250 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 14251 mtx_unlock(&thr->queue_lock); 14252 wakeup(thr); 14253} 14254 14255static void 14256ctl_enqueue_isc(union ctl_io *io) 14257{ 14258 struct ctl_softc *softc = control_softc; 14259 struct ctl_thread *thr; 14260 14261 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 14262 mtx_lock(&thr->queue_lock); 14263 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14264 mtx_unlock(&thr->queue_lock); 14265 wakeup(thr); 14266} 14267 14268/* Initialization and failover */ 14269 14270void 14271ctl_init_isc_msg(void) 14272{ 14273 printf("CTL: Still calling this thing\n"); 14274} 14275 14276#ifdef notyet 14277/* 14278 * Init component 14279 * Initializes component into configuration defined by bootMode 14280 * (see hasc-sv.c) 14281 * returns hasc_Status: 14282 * OK 14283 * ERROR - fatal error 14284 */ 14285static ctl_ha_comp_status 14286ctl_isc_init(struct ctl_ha_component *c) 14287{ 14288 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14289 14290 c->status = ret; 14291 return ret; 14292} 14293 14294/* Start component 14295 * Starts component in state requested. If component starts successfully, 14296 * it must set its own state to the requestrd state 14297 * When requested state is HASC_STATE_HA, the component may refine it 14298 * by adding _SLAVE or _MASTER flags. 14299 * Currently allowed state transitions are: 14300 * UNKNOWN->HA - initial startup 14301 * UNKNOWN->SINGLE - initial startup when no parter detected 14302 * HA->SINGLE - failover 14303 * returns ctl_ha_comp_status: 14304 * OK - component successfully started in requested state 14305 * FAILED - could not start the requested state, failover may 14306 * be possible 14307 * ERROR - fatal error detected, no future startup possible 14308 */ 14309static ctl_ha_comp_status 14310ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14311{ 14312 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14313 14314 printf("%s: go\n", __func__); 14315 14316 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14317 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14318 control_softc->is_single = 0; 14319 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14320 != CTL_HA_STATUS_SUCCESS) { 14321 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14322 ret = CTL_HA_COMP_STATUS_ERROR; 14323 } 14324 } else if (CTL_HA_STATE_IS_HA(c->state) 14325 && CTL_HA_STATE_IS_SINGLE(state)){ 14326 // HA->SINGLE transition 14327 ctl_failover(); 14328 control_softc->is_single = 1; 14329 } else { 14330 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14331 c->state, state); 14332 ret = CTL_HA_COMP_STATUS_ERROR; 14333 } 14334 if (CTL_HA_STATE_IS_SINGLE(state)) 14335 control_softc->is_single = 1; 14336 14337 c->state = state; 14338 c->status = ret; 14339 return ret; 14340} 14341 14342/* 14343 * Quiesce component 14344 * The component must clear any error conditions (set status to OK) and 14345 * prepare itself to another Start call 14346 * returns ctl_ha_comp_status: 14347 * OK 14348 * ERROR 14349 */ 14350static ctl_ha_comp_status 14351ctl_isc_quiesce(struct ctl_ha_component *c) 14352{ 14353 int ret = CTL_HA_COMP_STATUS_OK; 14354 14355 ctl_pause_rtr = 1; 14356 c->status = ret; 14357 return ret; 14358} 14359 14360struct ctl_ha_component ctl_ha_component_ctlisc = 14361{ 14362 .name = "CTL ISC", 14363 .state = CTL_HA_STATE_UNKNOWN, 14364 .init = ctl_isc_init, 14365 .start = ctl_isc_start, 14366 .quiesce = ctl_isc_quiesce 14367}; 14368#endif 14369 14370/* 14371 * vim: ts=8 14372 */ 14373