ctl.c revision 268674
1167598Srrs/*- 2167598Srrs * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3167598Srrs * Copyright (c) 2012 The FreeBSD Foundation 4167598Srrs * All rights reserved. 5167598Srrs * 6167598Srrs * Portions of this software were developed by Edward Tomasz Napierala 7167598Srrs * under sponsorship from the FreeBSD Foundation. 8167598Srrs * 9167598Srrs * Redistribution and use in source and binary forms, with or without 10167598Srrs * modification, are permitted provided that the following conditions 11167598Srrs * are met: 12167598Srrs * 1. Redistributions of source code must retain the above copyright 13167598Srrs * notice, this list of conditions, and the following disclaimer, 14167598Srrs * without modification. 15167598Srrs * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16167598Srrs * substantially similar to the "NO WARRANTY" disclaimer below 17167598Srrs * ("Disclaimer") and any redistribution must be conditioned upon 18167598Srrs * including a substantially similar Disclaimer requirement for further 19167598Srrs * binary redistribution. 20167598Srrs * 21167598Srrs * NO WARRANTY 22167598Srrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23167598Srrs * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24167598Srrs * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25167598Srrs * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26167598Srrs * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27167598Srrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28167598Srrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29167598Srrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30167598Srrs * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31167598Srrs * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32167598Srrs * POSSIBILITY OF SUCH DAMAGES. 33167598Srrs * 34167598Srrs * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35167598Srrs */ 36167598Srrs/* 37167598Srrs * CAM Target Layer, a SCSI device emulation subsystem. 38167598Srrs * 39167598Srrs * Author: Ken Merry <ken@FreeBSD.org> 40167598Srrs */ 41167598Srrs 42167598Srrs#define _CTL_C 43167598Srrs 44167598Srrs#include <sys/cdefs.h> 45167598Srrs__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 268674 2014-07-15 16:53:04Z mav $"); 46167598Srrs 47167598Srrs#include <sys/param.h> 48167598Srrs#include <sys/systm.h> 49167598Srrs#include <sys/kernel.h> 50167598Srrs#include <sys/types.h> 51167598Srrs#include <sys/kthread.h> 52167598Srrs#include <sys/bio.h> 53167598Srrs#include <sys/fcntl.h> 54167598Srrs#include <sys/lock.h> 55167598Srrs#include <sys/module.h> 56167598Srrs#include <sys/mutex.h> 57167598Srrs#include <sys/condvar.h> 58167598Srrs#include <sys/malloc.h> 59167598Srrs#include <sys/conf.h> 60167598Srrs#include <sys/ioccom.h> 61167598Srrs#include <sys/queue.h> 62167598Srrs#include <sys/sbuf.h> 63167598Srrs#include <sys/smp.h> 64167598Srrs#include <sys/endian.h> 65167598Srrs#include <sys/sysctl.h> 66167598Srrs 67167598Srrs#include <cam/cam.h> 68167598Srrs#include <cam/scsi/scsi_all.h> 69167598Srrs#include <cam/scsi/scsi_da.h> 70167598Srrs#include <cam/ctl/ctl_io.h> 71167598Srrs#include <cam/ctl/ctl.h> 72167598Srrs#include <cam/ctl/ctl_frontend.h> 73167598Srrs#include <cam/ctl/ctl_frontend_internal.h> 74167598Srrs#include <cam/ctl/ctl_util.h> 75167598Srrs#include <cam/ctl/ctl_backend.h> 76167598Srrs#include <cam/ctl/ctl_ioctl.h> 77167598Srrs#include <cam/ctl/ctl_ha.h> 78167598Srrs#include <cam/ctl/ctl_private.h> 79167598Srrs#include <cam/ctl/ctl_debug.h> 80167598Srrs#include <cam/ctl/ctl_scsi_all.h> 81167598Srrs#include <cam/ctl/ctl_error.h> 82167598Srrs 83167598Srrsstruct ctl_softc *control_softc = NULL; 84167598Srrs 85167598Srrs/* 86167598Srrs * Size and alignment macros needed for Copan-specific HA hardware. These 87167598Srrs * can go away when the HA code is re-written, and uses busdma for any 88167598Srrs * hardware. 89167598Srrs */ 90167598Srrs#define CTL_ALIGN_8B(target, source, type) \ 91167598Srrs if (((uint32_t)source & 0x7) != 0) \ 92167598Srrs target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 93167598Srrs else \ 94167598Srrs target = (type)source; 95167598Srrs 96167598Srrs#define CTL_SIZE_8B(target, size) \ 97167598Srrs if ((size & 0x7) != 0) \ 98167598Srrs target = size + (0x8 - (size & 0x7)); \ 99167598Srrs else \ 100167598Srrs target = size; 101167598Srrs 102167598Srrs#define CTL_ALIGN_8B_MARGIN 16 103167598Srrs 104167598Srrs/* 105167598Srrs * Template mode pages. 106167598Srrs */ 107167598Srrs 108167598Srrs/* 109167598Srrs * Note that these are default values only. The actual values will be 110167598Srrs * filled in when the user does a mode sense. 111167598Srrs */ 112167598Srrsstatic struct copan_power_subpage power_page_default = { 113167598Srrs /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 114167598Srrs /*subpage*/ PWR_SUBPAGE_CODE, 115167598Srrs /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 116167598Srrs (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 117167598Srrs /*page_version*/ PWR_VERSION, 118167598Srrs /* total_luns */ 26, 119167598Srrs /* max_active_luns*/ PWR_DFLT_MAX_LUNS, 120167598Srrs /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 121167598Srrs 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 122167598Srrs 0, 0, 0, 0, 0, 0} 123167598Srrs}; 124167598Srrs 125167598Srrsstatic struct copan_power_subpage power_page_changeable = { 126167598Srrs /*page_code*/ PWR_PAGE_CODE | SMPH_SPF, 127167598Srrs /*subpage*/ PWR_SUBPAGE_CODE, 128167598Srrs /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00, 129167598Srrs (sizeof(struct copan_power_subpage) - 4) & 0x00ff}, 130167598Srrs /*page_version*/ 0, 131167598Srrs /* total_luns */ 0, 132167598Srrs /* max_active_luns*/ 0, 133167598Srrs /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0, 134167598Srrs 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 135167598Srrs 0, 0, 0, 0, 0, 0} 136167598Srrs}; 137167598Srrs 138167598Srrsstatic struct copan_aps_subpage aps_page_default = { 139167598Srrs APS_PAGE_CODE | SMPH_SPF, //page_code 140167598Srrs APS_SUBPAGE_CODE, //subpage 141167598Srrs {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 142167598Srrs (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 143167598Srrs APS_VERSION, //page_version 144167598Srrs 0, //lock_active 145167598Srrs {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 146167598Srrs 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 147167598Srrs 0, 0, 0, 0, 0} //reserved 148167598Srrs}; 149167598Srrs 150167598Srrsstatic struct copan_aps_subpage aps_page_changeable = { 151167598Srrs APS_PAGE_CODE | SMPH_SPF, //page_code 152167598Srrs APS_SUBPAGE_CODE, //subpage 153167598Srrs {(sizeof(struct copan_aps_subpage) - 4) & 0xff00, 154167598Srrs (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length 155167598Srrs 0, //page_version 156167598Srrs 0, //lock_active 157167598Srrs {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 158167598Srrs 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 159167598Srrs 0, 0, 0, 0, 0} //reserved 160167598Srrs}; 161167598Srrs 162167598Srrsstatic struct copan_debugconf_subpage debugconf_page_default = { 163167598Srrs DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 164167598Srrs DBGCNF_SUBPAGE_CODE, /* subpage */ 165167598Srrs {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 166167598Srrs (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 167167598Srrs DBGCNF_VERSION, /* page_version */ 168167598Srrs {CTL_TIME_IO_DEFAULT_SECS>>8, 169167598Srrs CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 170167598Srrs}; 171167598Srrs 172167598Srrsstatic struct copan_debugconf_subpage debugconf_page_changeable = { 173167598Srrs DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 174167598Srrs DBGCNF_SUBPAGE_CODE, /* subpage */ 175167598Srrs {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 176167598Srrs (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 177167598Srrs 0, /* page_version */ 178167598Srrs {0xff,0xff}, /* ctl_time_io_secs */ 179167598Srrs}; 180167598Srrs 181167598Srrsstatic struct scsi_format_page format_page_default = { 182167598Srrs /*page_code*/SMS_FORMAT_DEVICE_PAGE, 183167598Srrs /*page_length*/sizeof(struct scsi_format_page) - 2, 184167598Srrs /*tracks_per_zone*/ {0, 0}, 185167598Srrs /*alt_sectors_per_zone*/ {0, 0}, 186167598Srrs /*alt_tracks_per_zone*/ {0, 0}, 187167598Srrs /*alt_tracks_per_lun*/ {0, 0}, 188167598Srrs /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 189167598Srrs CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 190167598Srrs /*bytes_per_sector*/ {0, 0}, 191167598Srrs /*interleave*/ {0, 0}, 192167598Srrs /*track_skew*/ {0, 0}, 193167598Srrs /*cylinder_skew*/ {0, 0}, 194167598Srrs /*flags*/ SFP_HSEC, 195167598Srrs /*reserved*/ {0, 0, 0} 196167598Srrs}; 197167598Srrs 198167598Srrsstatic struct scsi_format_page format_page_changeable = { 199167598Srrs /*page_code*/SMS_FORMAT_DEVICE_PAGE, 200167598Srrs /*page_length*/sizeof(struct scsi_format_page) - 2, 201167598Srrs /*tracks_per_zone*/ {0, 0}, 202167598Srrs /*alt_sectors_per_zone*/ {0, 0}, 203167598Srrs /*alt_tracks_per_zone*/ {0, 0}, 204167598Srrs /*alt_tracks_per_lun*/ {0, 0}, 205167598Srrs /*sectors_per_track*/ {0, 0}, 206167598Srrs /*bytes_per_sector*/ {0, 0}, 207167598Srrs /*interleave*/ {0, 0}, 208167598Srrs /*track_skew*/ {0, 0}, 209167598Srrs /*cylinder_skew*/ {0, 0}, 210167598Srrs /*flags*/ 0, 211167598Srrs /*reserved*/ {0, 0, 0} 212167598Srrs}; 213167598Srrs 214167598Srrsstatic struct scsi_rigid_disk_page rigid_disk_page_default = { 215167598Srrs /*page_code*/SMS_RIGID_DISK_PAGE, 216167598Srrs /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 217167598Srrs /*cylinders*/ {0, 0, 0}, 218167598Srrs /*heads*/ CTL_DEFAULT_HEADS, 219167598Srrs /*start_write_precomp*/ {0, 0, 0}, 220167598Srrs /*start_reduced_current*/ {0, 0, 0}, 221167598Srrs /*step_rate*/ {0, 0}, 222167598Srrs /*landing_zone_cylinder*/ {0, 0, 0}, 223167598Srrs /*rpl*/ SRDP_RPL_DISABLED, 224167598Srrs /*rotational_offset*/ 0, 225167598Srrs /*reserved1*/ 0, 226167598Srrs /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 227167598Srrs CTL_DEFAULT_ROTATION_RATE & 0xff}, 228167598Srrs /*reserved2*/ {0, 0} 229167598Srrs}; 230167598Srrs 231167598Srrsstatic struct scsi_rigid_disk_page rigid_disk_page_changeable = { 232167598Srrs /*page_code*/SMS_RIGID_DISK_PAGE, 233167598Srrs /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 234167598Srrs /*cylinders*/ {0, 0, 0}, 235167598Srrs /*heads*/ 0, 236167598Srrs /*start_write_precomp*/ {0, 0, 0}, 237167598Srrs /*start_reduced_current*/ {0, 0, 0}, 238167598Srrs /*step_rate*/ {0, 0}, 239167598Srrs /*landing_zone_cylinder*/ {0, 0, 0}, 240167598Srrs /*rpl*/ 0, 241167598Srrs /*rotational_offset*/ 0, 242167598Srrs /*reserved1*/ 0, 243167598Srrs /*rotation_rate*/ {0, 0}, 244167598Srrs /*reserved2*/ {0, 0} 245167598Srrs}; 246167598Srrs 247167598Srrsstatic struct scsi_caching_page caching_page_default = { 248167598Srrs /*page_code*/SMS_CACHING_PAGE, 249167598Srrs /*page_length*/sizeof(struct scsi_caching_page) - 2, 250167598Srrs /*flags1*/ SCP_DISC | SCP_WCE, 251167598Srrs /*ret_priority*/ 0, 252167598Srrs /*disable_pf_transfer_len*/ {0xff, 0xff}, 253167598Srrs /*min_prefetch*/ {0, 0}, 254167598Srrs /*max_prefetch*/ {0xff, 0xff}, 255167598Srrs /*max_pf_ceiling*/ {0xff, 0xff}, 256167598Srrs /*flags2*/ 0, 257167598Srrs /*cache_segments*/ 0, 258167598Srrs /*cache_seg_size*/ {0, 0}, 259167598Srrs /*reserved*/ 0, 260167598Srrs /*non_cache_seg_size*/ {0, 0, 0} 261167598Srrs}; 262167598Srrs 263167598Srrsstatic struct scsi_caching_page caching_page_changeable = { 264167598Srrs /*page_code*/SMS_CACHING_PAGE, 265167598Srrs /*page_length*/sizeof(struct scsi_caching_page) - 2, 266167598Srrs /*flags1*/ 0, 267167598Srrs /*ret_priority*/ 0, 268167598Srrs /*disable_pf_transfer_len*/ {0, 0}, 269167598Srrs /*min_prefetch*/ {0, 0}, 270167598Srrs /*max_prefetch*/ {0, 0}, 271167598Srrs /*max_pf_ceiling*/ {0, 0}, 272167598Srrs /*flags2*/ 0, 273167598Srrs /*cache_segments*/ 0, 274167598Srrs /*cache_seg_size*/ {0, 0}, 275167598Srrs /*reserved*/ 0, 276167598Srrs /*non_cache_seg_size*/ {0, 0, 0} 277167598Srrs}; 278167598Srrs 279167598Srrsstatic struct scsi_control_page control_page_default = { 280167598Srrs /*page_code*/SMS_CONTROL_MODE_PAGE, 281167598Srrs /*page_length*/sizeof(struct scsi_control_page) - 2, 282167598Srrs /*rlec*/0, 283167598Srrs /*queue_flags*/0, 284167598Srrs /*eca_and_aen*/0, 285167598Srrs /*reserved*/0, 286167598Srrs /*aen_holdoff_period*/{0, 0} 287167598Srrs}; 288167598Srrs 289167598Srrsstatic struct scsi_control_page control_page_changeable = { 290167598Srrs /*page_code*/SMS_CONTROL_MODE_PAGE, 291167598Srrs /*page_length*/sizeof(struct scsi_control_page) - 2, 292167598Srrs /*rlec*/SCP_DSENSE, 293167598Srrs /*queue_flags*/0, 294167598Srrs /*eca_and_aen*/0, 295167598Srrs /*reserved*/0, 296168124Srrs /*aen_holdoff_period*/{0, 0} 297167598Srrs}; 298167598Srrs 299167598Srrs 300167598Srrs/* 301167598Srrs * XXX KDM move these into the softc. 302167598Srrs */ 303168124Srrsstatic int rcv_sync_msg; 304167598Srrsstatic int persis_offset; 305167598Srrsstatic uint8_t ctl_pause_rtr; 306167598Srrsstatic int ctl_is_single = 1; 307167598Srrsstatic int index_to_aps_page; 308167598Srrs 309167598SrrsSYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 310168124Srrsstatic int worker_threads = -1; 311167598SrrsTUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 312167598SrrsSYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 313167598Srrs &worker_threads, 1, "Number of worker threads"); 314167598Srrsstatic int verbose = 0; 315167598SrrsTUNABLE_INT("kern.cam.ctl.verbose", &verbose); 316167598SrrsSYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN, 317168124Srrs &verbose, 0, "Show SCSI errors returned to initiator"); 318167598Srrs 319167598Srrs/* 320167598Srrs * Serial number (0x80), device id (0x83), supported pages (0x00), 321167598Srrs * Block limits (0xB0) and Logical Block Provisioning (0xB2) 322167598Srrs */ 323167598Srrs#define SCSI_EVPD_NUM_SUPPORTED_PAGES 5 324168124Srrs 325167598Srrsstatic void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 326167598Srrs int param); 327167598Srrsstatic void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 328167598Srrsstatic int ctl_init(void); 329167598Srrsvoid ctl_shutdown(void); 330167598Srrsstatic int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 331168124Srrsstatic int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 332167598Srrsstatic void ctl_ioctl_online(void *arg); 333167598Srrsstatic void ctl_ioctl_offline(void *arg); 334167598Srrsstatic int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id); 335167598Srrsstatic int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id); 336167598Srrsstatic int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 337167598Srrsstatic int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 338168124Srrsstatic int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 339167598Srrsstatic int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 340167598Srrsstatic int ctl_ioctl_submit_wait(union ctl_io *io); 341167598Srrsstatic void ctl_ioctl_datamove(union ctl_io *io); 342167598Srrsstatic void ctl_ioctl_done(union ctl_io *io); 343167598Srrsstatic void ctl_ioctl_hard_startstop_callback(void *arg, 344167598Srrs struct cfi_metatask *metatask); 345168124Srrsstatic void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 346167598Srrsstatic int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 347167598Srrs struct ctl_ooa *ooa_hdr, 348167598Srrs struct ctl_ooa_entry *kern_entries); 349167598Srrsstatic int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 350167598Srrs struct thread *td); 351167598Srrsuint32_t ctl_get_resindex(struct ctl_nexus *nexus); 352168124Srrsuint32_t ctl_port_idx(int port_num); 353167598Srrs#ifdef unused 354167598Srrsstatic union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 355167598Srrs uint32_t targ_target, uint32_t targ_lun, 356167598Srrs int can_wait); 357167598Srrsstatic void ctl_kfree_io(union ctl_io *io); 358167598Srrs#endif /* unused */ 359168124Srrsstatic int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 360167598Srrs struct ctl_be_lun *be_lun, struct ctl_id target_id); 361167598Srrsstatic int ctl_free_lun(struct ctl_lun *lun); 362167598Srrsstatic void ctl_create_lun(struct ctl_be_lun *be_lun); 363167598Srrs/** 364167598Srrsstatic void ctl_failover_change_pages(struct ctl_softc *softc, 365167598Srrs struct ctl_scsiio *ctsio, int master); 366168124Srrs**/ 367167598Srrs 368167598Srrsstatic int ctl_do_mode_select(union ctl_io *io); 369167598Srrsstatic int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 370167598Srrs uint64_t res_key, uint64_t sa_res_key, 371167598Srrs uint8_t type, uint32_t residx, 372167598Srrs struct ctl_scsiio *ctsio, 373168124Srrs struct scsi_per_res_out *cdb, 374167598Srrs struct scsi_per_res_out_parms* param); 375167598Srrsstatic void ctl_pro_preempt_other(struct ctl_lun *lun, 376167598Srrs union ctl_ha_msg *msg); 377167598Srrsstatic void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 378167598Srrsstatic int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 379168124Srrsstatic int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 380168124Srrsstatic int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 381168124Srrsstatic int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 382168124Srrs int alloc_len); 383168124Srrsstatic int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 384168124Srrsstatic int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 385168124Srrsstatic int ctl_inquiry_std(struct ctl_scsiio *ctsio); 386168124Srrsstatic int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len); 387168124Srrsstatic ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 388168124Srrsstatic ctl_action ctl_check_for_blockage(union ctl_io *pending_io, 389168124Srrs union ctl_io *ooa_io); 390168124Srrsstatic ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 391168124Srrs union ctl_io *starting_io); 392168124Srrsstatic int ctl_check_blocked(struct ctl_lun *lun); 393168124Srrsstatic int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 394168124Srrs struct ctl_lun *lun, 395168124Srrs const struct ctl_cmd_entry *entry, 396167598Srrs struct ctl_scsiio *ctsio); 397168124Srrs//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 398167598Srrsstatic void ctl_failover(void); 399167598Srrsstatic int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 400167598Srrs struct ctl_scsiio *ctsio); 401167598Srrsstatic int ctl_scsiio(struct ctl_scsiio *ctsio); 402167598Srrs 403168124Srrsstatic int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 404168124Srrsstatic int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 405168124Srrs ctl_ua_type ua_type); 406167598Srrsstatic int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 407167598Srrs ctl_ua_type ua_type); 408167598Srrsstatic int ctl_abort_task(union ctl_io *io); 409167598Srrsstatic void ctl_run_task(union ctl_io *io); 410167598Srrs#ifdef CTL_IO_DELAY 411167598Srrsstatic void ctl_datamove_timer_wakeup(void *arg); 412167598Srrsstatic void ctl_done_timer_wakeup(void *arg); 413167598Srrs#endif /* CTL_IO_DELAY */ 414167598Srrs 415167598Srrsstatic void ctl_send_datamove_done(union ctl_io *io, int have_lock); 416167598Srrsstatic void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 417167598Srrsstatic int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 418167598Srrsstatic void ctl_datamove_remote_write(union ctl_io *io); 419167598Srrsstatic int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 420167598Srrsstatic void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 421167598Srrsstatic int ctl_datamove_remote_sgl_setup(union ctl_io *io); 422167598Srrsstatic int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 423167598Srrs ctl_ha_dt_cb callback); 424167598Srrsstatic void ctl_datamove_remote_read(union ctl_io *io); 425167598Srrsstatic void ctl_datamove_remote(union ctl_io *io); 426167598Srrsstatic int ctl_process_done(union ctl_io *io); 427167598Srrsstatic void ctl_lun_thread(void *arg); 428167598Srrsstatic void ctl_work_thread(void *arg); 429167598Srrsstatic void ctl_enqueue_incoming(union ctl_io *io); 430167598Srrsstatic void ctl_enqueue_rtr(union ctl_io *io); 431167598Srrsstatic void ctl_enqueue_done(union ctl_io *io); 432167598Srrsstatic void ctl_enqueue_isc(union ctl_io *io); 433167598Srrsstatic const struct ctl_cmd_entry * 434167598Srrs ctl_get_cmd_entry(struct ctl_scsiio *ctsio); 435167598Srrsstatic const struct ctl_cmd_entry * 436167598Srrs ctl_validate_command(struct ctl_scsiio *ctsio); 437167598Srrsstatic int ctl_cmd_applicable(uint8_t lun_type, 438167598Srrs const struct ctl_cmd_entry *entry); 439167598Srrs 440167598Srrs/* 441167598Srrs * Load the serialization table. This isn't very pretty, but is probably 442167598Srrs * the easiest way to do it. 443167598Srrs */ 444167598Srrs#include "ctl_ser_table.c" 445167598Srrs 446167598Srrs/* 447167598Srrs * We only need to define open, close and ioctl routines for this driver. 448167598Srrs */ 449167598Srrsstatic struct cdevsw ctl_cdevsw = { 450167598Srrs .d_version = D_VERSION, 451167598Srrs .d_flags = 0, 452167598Srrs .d_open = ctl_open, 453167598Srrs .d_close = ctl_close, 454167598Srrs .d_ioctl = ctl_ioctl, 455167598Srrs .d_name = "ctl", 456167598Srrs}; 457167598Srrs 458167598Srrs 459167598SrrsMALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 460167598SrrsMALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 461167598Srrs 462167598Srrsstatic int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 463167598Srrs 464168124Srrsstatic moduledata_t ctl_moduledata = { 465168124Srrs "ctl", 466167598Srrs ctl_module_event_handler, 467167598Srrs NULL 468167598Srrs}; 469167598Srrs 470167598SrrsDECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 471167598SrrsMODULE_VERSION(ctl, 1); 472167598Srrs 473167598Srrsstatic void 474167598Srrsctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 475167598Srrs union ctl_ha_msg *msg_info) 476167598Srrs{ 477167598Srrs struct ctl_scsiio *ctsio; 478167598Srrs 479167598Srrs if (msg_info->hdr.original_sc == NULL) { 480167598Srrs printf("%s: original_sc == NULL!\n", __func__); 481167598Srrs /* XXX KDM now what? */ 482167598Srrs return; 483167598Srrs } 484167598Srrs 485167598Srrs ctsio = &msg_info->hdr.original_sc->scsiio; 486167598Srrs ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 487167598Srrs ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 488167598Srrs ctsio->io_hdr.status = msg_info->hdr.status; 489167598Srrs ctsio->scsi_status = msg_info->scsi.scsi_status; 490167598Srrs ctsio->sense_len = msg_info->scsi.sense_len; 491167598Srrs ctsio->sense_residual = msg_info->scsi.sense_residual; 492167598Srrs ctsio->residual = msg_info->scsi.residual; 493167598Srrs memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 494167598Srrs sizeof(ctsio->sense_data)); 495167598Srrs memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 496167598Srrs &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 497167598Srrs ctl_enqueue_isc((union ctl_io *)ctsio); 498167598Srrs} 499167598Srrs 500167598Srrsstatic void 501167598Srrsctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 502167598Srrs union ctl_ha_msg *msg_info) 503167598Srrs{ 504167598Srrs struct ctl_scsiio *ctsio; 505167598Srrs 506167598Srrs if (msg_info->hdr.serializing_sc == NULL) { 507167598Srrs printf("%s: serializing_sc == NULL!\n", __func__); 508167598Srrs /* XXX KDM now what? */ 509167598Srrs return; 510167598Srrs } 511167598Srrs 512167598Srrs ctsio = &msg_info->hdr.serializing_sc->scsiio; 513167598Srrs#if 0 514167598Srrs /* 515167598Srrs * Attempt to catch the situation where an I/O has 516167598Srrs * been freed, and we're using it again. 517167598Srrs */ 518167598Srrs if (ctsio->io_hdr.io_type == 0xff) { 519168124Srrs union ctl_io *tmp_io; 520168124Srrs tmp_io = (union ctl_io *)ctsio; 521167598Srrs printf("%s: %p use after free!\n", __func__, 522167598Srrs ctsio); 523167598Srrs printf("%s: type %d msg %d cdb %x iptl: " 524167598Srrs "%d:%d:%d:%d tag 0x%04x " 525167598Srrs "flag %#x status %x\n", 526167598Srrs __func__, 527167598Srrs tmp_io->io_hdr.io_type, 528167598Srrs tmp_io->io_hdr.msg_type, 529167598Srrs tmp_io->scsiio.cdb[0], 530167598Srrs tmp_io->io_hdr.nexus.initid.id, 531167598Srrs tmp_io->io_hdr.nexus.targ_port, 532167598Srrs tmp_io->io_hdr.nexus.targ_target.id, 533167598Srrs tmp_io->io_hdr.nexus.targ_lun, 534167598Srrs (tmp_io->io_hdr.io_type == 535167598Srrs CTL_IO_TASK) ? 536167598Srrs tmp_io->taskio.tag_num : 537167598Srrs tmp_io->scsiio.tag_num, 538167598Srrs tmp_io->io_hdr.flags, 539167598Srrs tmp_io->io_hdr.status); 540167598Srrs } 541167598Srrs#endif 542167598Srrs ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 543167598Srrs ctl_enqueue_isc((union ctl_io *)ctsio); 544167598Srrs} 545167598Srrs 546167598Srrs/* 547167598Srrs * ISC (Inter Shelf Communication) event handler. Events from the HA 548167598Srrs * subsystem come in here. 549167598Srrs */ 550167598Srrsstatic void 551167598Srrsctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 552167598Srrs{ 553167598Srrs struct ctl_softc *ctl_softc; 554167598Srrs union ctl_io *io; 555167598Srrs struct ctl_prio *presio; 556167598Srrs ctl_ha_status isc_status; 557167598Srrs 558167598Srrs ctl_softc = control_softc; 559167598Srrs io = NULL; 560167598Srrs 561167598Srrs 562167598Srrs#if 0 563167598Srrs printf("CTL: Isc Msg event %d\n", event); 564167598Srrs#endif 565167598Srrs if (event == CTL_HA_EVT_MSG_RECV) { 566167598Srrs union ctl_ha_msg msg_info; 567167598Srrs 568167598Srrs isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 569167598Srrs sizeof(msg_info), /*wait*/ 0); 570167598Srrs#if 0 571167598Srrs printf("CTL: msg_type %d\n", msg_info.msg_type); 572167598Srrs#endif 573167598Srrs if (isc_status != 0) { 574167598Srrs printf("Error receiving message, status = %d\n", 575167598Srrs isc_status); 576167598Srrs return; 577167598Srrs } 578167598Srrs 579168124Srrs switch (msg_info.hdr.msg_type) { 580168124Srrs case CTL_MSG_SERIALIZE: 581167598Srrs#if 0 582167598Srrs printf("Serialize\n"); 583167598Srrs#endif 584167598Srrs io = ctl_alloc_io((void *)ctl_softc->othersc_pool); 585167598Srrs if (io == NULL) { 586167598Srrs printf("ctl_isc_event_handler: can't allocate " 587167598Srrs "ctl_io!\n"); 588167598Srrs /* Bad Juju */ 589167598Srrs /* Need to set busy and send msg back */ 590167598Srrs msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 591167598Srrs msg_info.hdr.status = CTL_SCSI_ERROR; 592167598Srrs msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 593167598Srrs msg_info.scsi.sense_len = 0; 594167598Srrs if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 595167598Srrs sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 596 } 597 goto bailout; 598 } 599 ctl_zero_io(io); 600 // populate ctsio from msg_info 601 io->io_hdr.io_type = CTL_IO_SCSI; 602 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 603 io->io_hdr.original_sc = msg_info.hdr.original_sc; 604#if 0 605 printf("pOrig %x\n", (int)msg_info.original_sc); 606#endif 607 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 608 CTL_FLAG_IO_ACTIVE; 609 /* 610 * If we're in serialization-only mode, we don't 611 * want to go through full done processing. Thus 612 * the COPY flag. 613 * 614 * XXX KDM add another flag that is more specific. 615 */ 616 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 617 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 618 io->io_hdr.nexus = msg_info.hdr.nexus; 619#if 0 620 printf("targ %d, port %d, iid %d, lun %d\n", 621 io->io_hdr.nexus.targ_target.id, 622 io->io_hdr.nexus.targ_port, 623 io->io_hdr.nexus.initid.id, 624 io->io_hdr.nexus.targ_lun); 625#endif 626 io->scsiio.tag_num = msg_info.scsi.tag_num; 627 io->scsiio.tag_type = msg_info.scsi.tag_type; 628 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 629 CTL_MAX_CDBLEN); 630 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 631 const struct ctl_cmd_entry *entry; 632 633 entry = ctl_get_cmd_entry(&io->scsiio); 634 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 635 io->io_hdr.flags |= 636 entry->flags & CTL_FLAG_DATA_MASK; 637 } 638 ctl_enqueue_isc(io); 639 break; 640 641 /* Performed on the Originating SC, XFER mode only */ 642 case CTL_MSG_DATAMOVE: { 643 struct ctl_sg_entry *sgl; 644 int i, j; 645 646 io = msg_info.hdr.original_sc; 647 if (io == NULL) { 648 printf("%s: original_sc == NULL!\n", __func__); 649 /* XXX KDM do something here */ 650 break; 651 } 652 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 653 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 654 /* 655 * Keep track of this, we need to send it back over 656 * when the datamove is complete. 657 */ 658 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 659 660 if (msg_info.dt.sg_sequence == 0) { 661 /* 662 * XXX KDM we use the preallocated S/G list 663 * here, but we'll need to change this to 664 * dynamic allocation if we need larger S/G 665 * lists. 666 */ 667 if (msg_info.dt.kern_sg_entries > 668 sizeof(io->io_hdr.remote_sglist) / 669 sizeof(io->io_hdr.remote_sglist[0])) { 670 printf("%s: number of S/G entries " 671 "needed %u > allocated num %zd\n", 672 __func__, 673 msg_info.dt.kern_sg_entries, 674 sizeof(io->io_hdr.remote_sglist)/ 675 sizeof(io->io_hdr.remote_sglist[0])); 676 677 /* 678 * XXX KDM send a message back to 679 * the other side to shut down the 680 * DMA. The error will come back 681 * through via the normal channel. 682 */ 683 break; 684 } 685 sgl = io->io_hdr.remote_sglist; 686 memset(sgl, 0, 687 sizeof(io->io_hdr.remote_sglist)); 688 689 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 690 691 io->scsiio.kern_sg_entries = 692 msg_info.dt.kern_sg_entries; 693 io->scsiio.rem_sg_entries = 694 msg_info.dt.kern_sg_entries; 695 io->scsiio.kern_data_len = 696 msg_info.dt.kern_data_len; 697 io->scsiio.kern_total_len = 698 msg_info.dt.kern_total_len; 699 io->scsiio.kern_data_resid = 700 msg_info.dt.kern_data_resid; 701 io->scsiio.kern_rel_offset = 702 msg_info.dt.kern_rel_offset; 703 /* 704 * Clear out per-DMA flags. 705 */ 706 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 707 /* 708 * Add per-DMA flags that are set for this 709 * particular DMA request. 710 */ 711 io->io_hdr.flags |= msg_info.dt.flags & 712 CTL_FLAG_RDMA_MASK; 713 } else 714 sgl = (struct ctl_sg_entry *) 715 io->scsiio.kern_data_ptr; 716 717 for (i = msg_info.dt.sent_sg_entries, j = 0; 718 i < (msg_info.dt.sent_sg_entries + 719 msg_info.dt.cur_sg_entries); i++, j++) { 720 sgl[i].addr = msg_info.dt.sg_list[j].addr; 721 sgl[i].len = msg_info.dt.sg_list[j].len; 722 723#if 0 724 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 725 __func__, 726 msg_info.dt.sg_list[j].addr, 727 msg_info.dt.sg_list[j].len, 728 sgl[i].addr, sgl[i].len, j, i); 729#endif 730 } 731#if 0 732 memcpy(&sgl[msg_info.dt.sent_sg_entries], 733 msg_info.dt.sg_list, 734 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 735#endif 736 737 /* 738 * If this is the last piece of the I/O, we've got 739 * the full S/G list. Queue processing in the thread. 740 * Otherwise wait for the next piece. 741 */ 742 if (msg_info.dt.sg_last != 0) 743 ctl_enqueue_isc(io); 744 break; 745 } 746 /* Performed on the Serializing (primary) SC, XFER mode only */ 747 case CTL_MSG_DATAMOVE_DONE: { 748 if (msg_info.hdr.serializing_sc == NULL) { 749 printf("%s: serializing_sc == NULL!\n", 750 __func__); 751 /* XXX KDM now what? */ 752 break; 753 } 754 /* 755 * We grab the sense information here in case 756 * there was a failure, so we can return status 757 * back to the initiator. 758 */ 759 io = msg_info.hdr.serializing_sc; 760 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 761 io->io_hdr.status = msg_info.hdr.status; 762 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 763 io->scsiio.sense_len = msg_info.scsi.sense_len; 764 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 765 io->io_hdr.port_status = msg_info.scsi.fetd_status; 766 io->scsiio.residual = msg_info.scsi.residual; 767 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 768 sizeof(io->scsiio.sense_data)); 769 ctl_enqueue_isc(io); 770 break; 771 } 772 773 /* Preformed on Originating SC, SER_ONLY mode */ 774 case CTL_MSG_R2R: 775 io = msg_info.hdr.original_sc; 776 if (io == NULL) { 777 printf("%s: Major Bummer\n", __func__); 778 return; 779 } else { 780#if 0 781 printf("pOrig %x\n",(int) ctsio); 782#endif 783 } 784 io->io_hdr.msg_type = CTL_MSG_R2R; 785 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 786 ctl_enqueue_isc(io); 787 break; 788 789 /* 790 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 791 * mode. 792 * Performed on the Originating (i.e. secondary) SC in XFER 793 * mode 794 */ 795 case CTL_MSG_FINISH_IO: 796 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 797 ctl_isc_handler_finish_xfer(ctl_softc, 798 &msg_info); 799 else 800 ctl_isc_handler_finish_ser_only(ctl_softc, 801 &msg_info); 802 break; 803 804 /* Preformed on Originating SC */ 805 case CTL_MSG_BAD_JUJU: 806 io = msg_info.hdr.original_sc; 807 if (io == NULL) { 808 printf("%s: Bad JUJU!, original_sc is NULL!\n", 809 __func__); 810 break; 811 } 812 ctl_copy_sense_data(&msg_info, io); 813 /* 814 * IO should have already been cleaned up on other 815 * SC so clear this flag so we won't send a message 816 * back to finish the IO there. 817 */ 818 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 819 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 820 821 /* io = msg_info.hdr.serializing_sc; */ 822 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 823 ctl_enqueue_isc(io); 824 break; 825 826 /* Handle resets sent from the other side */ 827 case CTL_MSG_MANAGE_TASKS: { 828 struct ctl_taskio *taskio; 829 taskio = (struct ctl_taskio *)ctl_alloc_io( 830 (void *)ctl_softc->othersc_pool); 831 if (taskio == NULL) { 832 printf("ctl_isc_event_handler: can't allocate " 833 "ctl_io!\n"); 834 /* Bad Juju */ 835 /* should I just call the proper reset func 836 here??? */ 837 goto bailout; 838 } 839 ctl_zero_io((union ctl_io *)taskio); 840 taskio->io_hdr.io_type = CTL_IO_TASK; 841 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 842 taskio->io_hdr.nexus = msg_info.hdr.nexus; 843 taskio->task_action = msg_info.task.task_action; 844 taskio->tag_num = msg_info.task.tag_num; 845 taskio->tag_type = msg_info.task.tag_type; 846#ifdef CTL_TIME_IO 847 taskio->io_hdr.start_time = time_uptime; 848 getbintime(&taskio->io_hdr.start_bt); 849#if 0 850 cs_prof_gettime(&taskio->io_hdr.start_ticks); 851#endif 852#endif /* CTL_TIME_IO */ 853 ctl_run_task((union ctl_io *)taskio); 854 break; 855 } 856 /* Persistent Reserve action which needs attention */ 857 case CTL_MSG_PERS_ACTION: 858 presio = (struct ctl_prio *)ctl_alloc_io( 859 (void *)ctl_softc->othersc_pool); 860 if (presio == NULL) { 861 printf("ctl_isc_event_handler: can't allocate " 862 "ctl_io!\n"); 863 /* Bad Juju */ 864 /* Need to set busy and send msg back */ 865 goto bailout; 866 } 867 ctl_zero_io((union ctl_io *)presio); 868 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 869 presio->pr_msg = msg_info.pr; 870 ctl_enqueue_isc((union ctl_io *)presio); 871 break; 872 case CTL_MSG_SYNC_FE: 873 rcv_sync_msg = 1; 874 break; 875 case CTL_MSG_APS_LOCK: { 876 // It's quicker to execute this then to 877 // queue it. 878 struct ctl_lun *lun; 879 struct ctl_page_index *page_index; 880 struct copan_aps_subpage *current_sp; 881 uint32_t targ_lun; 882 883 targ_lun = msg_info.hdr.nexus.targ_mapped_lun; 884 lun = ctl_softc->ctl_luns[targ_lun]; 885 mtx_lock(&lun->lun_lock); 886 page_index = &lun->mode_pages.index[index_to_aps_page]; 887 current_sp = (struct copan_aps_subpage *) 888 (page_index->page_data + 889 (page_index->page_len * CTL_PAGE_CURRENT)); 890 891 current_sp->lock_active = msg_info.aps.lock_flag; 892 mtx_unlock(&lun->lun_lock); 893 break; 894 } 895 default: 896 printf("How did I get here?\n"); 897 } 898 } else if (event == CTL_HA_EVT_MSG_SENT) { 899 if (param != CTL_HA_STATUS_SUCCESS) { 900 printf("Bad status from ctl_ha_msg_send status %d\n", 901 param); 902 } 903 return; 904 } else if (event == CTL_HA_EVT_DISCONNECT) { 905 printf("CTL: Got a disconnect from Isc\n"); 906 return; 907 } else { 908 printf("ctl_isc_event_handler: Unknown event %d\n", event); 909 return; 910 } 911 912bailout: 913 return; 914} 915 916static void 917ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 918{ 919 struct scsi_sense_data *sense; 920 921 sense = &dest->scsiio.sense_data; 922 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 923 dest->scsiio.scsi_status = src->scsi.scsi_status; 924 dest->scsiio.sense_len = src->scsi.sense_len; 925 dest->io_hdr.status = src->hdr.status; 926} 927 928static int 929ctl_init(void) 930{ 931 struct ctl_softc *softc; 932 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool; 933 struct ctl_frontend *fe; 934 uint8_t sc_id =0; 935 int i, error, retval; 936 //int isc_retval; 937 938 retval = 0; 939 ctl_pause_rtr = 0; 940 rcv_sync_msg = 0; 941 942 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 943 M_WAITOK | M_ZERO); 944 softc = control_softc; 945 946 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 947 "cam/ctl"); 948 949 softc->dev->si_drv1 = softc; 950 951 /* 952 * By default, return a "bad LUN" peripheral qualifier for unknown 953 * LUNs. The user can override this default using the tunable or 954 * sysctl. See the comment in ctl_inquiry_std() for more details. 955 */ 956 softc->inquiry_pq_no_lun = 1; 957 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 958 &softc->inquiry_pq_no_lun); 959 sysctl_ctx_init(&softc->sysctl_ctx); 960 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 961 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 962 CTLFLAG_RD, 0, "CAM Target Layer"); 963 964 if (softc->sysctl_tree == NULL) { 965 printf("%s: unable to allocate sysctl tree\n", __func__); 966 destroy_dev(softc->dev); 967 free(control_softc, M_DEVBUF); 968 control_softc = NULL; 969 return (ENOMEM); 970 } 971 972 SYSCTL_ADD_INT(&softc->sysctl_ctx, 973 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 974 "inquiry_pq_no_lun", CTLFLAG_RW, 975 &softc->inquiry_pq_no_lun, 0, 976 "Report no lun possible for invalid LUNs"); 977 978 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 979 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF); 980 softc->open_count = 0; 981 982 /* 983 * Default to actually sending a SYNCHRONIZE CACHE command down to 984 * the drive. 985 */ 986 softc->flags = CTL_FLAG_REAL_SYNC; 987 988 /* 989 * In Copan's HA scheme, the "master" and "slave" roles are 990 * figured out through the slot the controller is in. Although it 991 * is an active/active system, someone has to be in charge. 992 */ 993#ifdef NEEDTOPORT 994 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id); 995#endif 996 997 if (sc_id == 0) { 998 softc->flags |= CTL_FLAG_MASTER_SHELF; 999 persis_offset = 0; 1000 } else 1001 persis_offset = CTL_MAX_INITIATORS; 1002 1003 /* 1004 * XXX KDM need to figure out where we want to get our target ID 1005 * and WWID. Is it different on each port? 1006 */ 1007 softc->target.id = 0; 1008 softc->target.wwid[0] = 0x12345678; 1009 softc->target.wwid[1] = 0x87654321; 1010 STAILQ_INIT(&softc->lun_list); 1011 STAILQ_INIT(&softc->pending_lun_queue); 1012 STAILQ_INIT(&softc->fe_list); 1013 STAILQ_INIT(&softc->be_list); 1014 STAILQ_INIT(&softc->io_pools); 1015 1016 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL, 1017 &internal_pool)!= 0){ 1018 printf("ctl: can't allocate %d entry internal pool, " 1019 "exiting\n", CTL_POOL_ENTRIES_INTERNAL); 1020 return (ENOMEM); 1021 } 1022 1023 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY, 1024 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) { 1025 printf("ctl: can't allocate %d entry emergency pool, " 1026 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY); 1027 ctl_pool_free(internal_pool); 1028 return (ENOMEM); 1029 } 1030 1031 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC, 1032 &other_pool) != 0) 1033 { 1034 printf("ctl: can't allocate %d entry other SC pool, " 1035 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1036 ctl_pool_free(internal_pool); 1037 ctl_pool_free(emergency_pool); 1038 return (ENOMEM); 1039 } 1040 1041 softc->internal_pool = internal_pool; 1042 softc->emergency_pool = emergency_pool; 1043 softc->othersc_pool = other_pool; 1044 1045 if (worker_threads <= 0) 1046 worker_threads = max(1, mp_ncpus / 4); 1047 if (worker_threads > CTL_MAX_THREADS) 1048 worker_threads = CTL_MAX_THREADS; 1049 1050 for (i = 0; i < worker_threads; i++) { 1051 struct ctl_thread *thr = &softc->threads[i]; 1052 1053 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1054 thr->ctl_softc = softc; 1055 STAILQ_INIT(&thr->incoming_queue); 1056 STAILQ_INIT(&thr->rtr_queue); 1057 STAILQ_INIT(&thr->done_queue); 1058 STAILQ_INIT(&thr->isc_queue); 1059 1060 error = kproc_kthread_add(ctl_work_thread, thr, 1061 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1062 if (error != 0) { 1063 printf("error creating CTL work thread!\n"); 1064 ctl_pool_free(internal_pool); 1065 ctl_pool_free(emergency_pool); 1066 ctl_pool_free(other_pool); 1067 return (error); 1068 } 1069 } 1070 error = kproc_kthread_add(ctl_lun_thread, softc, 1071 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1072 if (error != 0) { 1073 printf("error creating CTL lun thread!\n"); 1074 ctl_pool_free(internal_pool); 1075 ctl_pool_free(emergency_pool); 1076 ctl_pool_free(other_pool); 1077 return (error); 1078 } 1079 if (bootverbose) 1080 printf("ctl: CAM Target Layer loaded\n"); 1081 1082 /* 1083 * Initialize the initiator and portname mappings 1084 */ 1085 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid)); 1086 1087 /* 1088 * Initialize the ioctl front end. 1089 */ 1090 fe = &softc->ioctl_info.fe; 1091 sprintf(softc->ioctl_info.port_name, "CTL ioctl"); 1092 fe->port_type = CTL_PORT_IOCTL; 1093 fe->num_requested_ctl_io = 100; 1094 fe->port_name = softc->ioctl_info.port_name; 1095 fe->port_online = ctl_ioctl_online; 1096 fe->port_offline = ctl_ioctl_offline; 1097 fe->onoff_arg = &softc->ioctl_info; 1098 fe->targ_enable = ctl_ioctl_targ_enable; 1099 fe->targ_disable = ctl_ioctl_targ_disable; 1100 fe->lun_enable = ctl_ioctl_lun_enable; 1101 fe->lun_disable = ctl_ioctl_lun_disable; 1102 fe->targ_lun_arg = &softc->ioctl_info; 1103 fe->fe_datamove = ctl_ioctl_datamove; 1104 fe->fe_done = ctl_ioctl_done; 1105 fe->max_targets = 15; 1106 fe->max_target_id = 15; 1107 1108 if (ctl_frontend_register(&softc->ioctl_info.fe, 1109 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) { 1110 printf("ctl: ioctl front end registration failed, will " 1111 "continue anyway\n"); 1112 } 1113 1114#ifdef CTL_IO_DELAY 1115 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1116 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1117 sizeof(struct callout), CTL_TIMER_BYTES); 1118 return (EINVAL); 1119 } 1120#endif /* CTL_IO_DELAY */ 1121 1122 return (0); 1123} 1124 1125void 1126ctl_shutdown(void) 1127{ 1128 struct ctl_softc *softc; 1129 struct ctl_lun *lun, *next_lun; 1130 struct ctl_io_pool *pool; 1131 1132 softc = (struct ctl_softc *)control_softc; 1133 1134 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0) 1135 printf("ctl: ioctl front end deregistration failed\n"); 1136 1137 mtx_lock(&softc->ctl_lock); 1138 1139 /* 1140 * Free up each LUN. 1141 */ 1142 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1143 next_lun = STAILQ_NEXT(lun, links); 1144 ctl_free_lun(lun); 1145 } 1146 1147 mtx_unlock(&softc->ctl_lock); 1148 1149 /* 1150 * This will rip the rug out from under any FETDs or anyone else 1151 * that has a pool allocated. Since we increment our module 1152 * refcount any time someone outside the main CTL module allocates 1153 * a pool, we shouldn't have any problems here. The user won't be 1154 * able to unload the CTL module until client modules have 1155 * successfully unloaded. 1156 */ 1157 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL) 1158 ctl_pool_free(pool); 1159 1160#if 0 1161 ctl_shutdown_thread(softc->work_thread); 1162 mtx_destroy(&softc->queue_lock); 1163#endif 1164 1165 mtx_destroy(&softc->pool_lock); 1166 mtx_destroy(&softc->ctl_lock); 1167 1168 destroy_dev(softc->dev); 1169 1170 sysctl_ctx_free(&softc->sysctl_ctx); 1171 1172 free(control_softc, M_DEVBUF); 1173 control_softc = NULL; 1174 1175 if (bootverbose) 1176 printf("ctl: CAM Target Layer unloaded\n"); 1177} 1178 1179static int 1180ctl_module_event_handler(module_t mod, int what, void *arg) 1181{ 1182 1183 switch (what) { 1184 case MOD_LOAD: 1185 return (ctl_init()); 1186 case MOD_UNLOAD: 1187 return (EBUSY); 1188 default: 1189 return (EOPNOTSUPP); 1190 } 1191} 1192 1193/* 1194 * XXX KDM should we do some access checks here? Bump a reference count to 1195 * prevent a CTL module from being unloaded while someone has it open? 1196 */ 1197static int 1198ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1199{ 1200 return (0); 1201} 1202 1203static int 1204ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1205{ 1206 return (0); 1207} 1208 1209int 1210ctl_port_enable(ctl_port_type port_type) 1211{ 1212 struct ctl_softc *softc; 1213 struct ctl_frontend *fe; 1214 1215 if (ctl_is_single == 0) { 1216 union ctl_ha_msg msg_info; 1217 int isc_retval; 1218 1219#if 0 1220 printf("%s: HA mode, synchronizing frontend enable\n", 1221 __func__); 1222#endif 1223 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1224 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1225 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1226 printf("Sync msg send error retval %d\n", isc_retval); 1227 } 1228 if (!rcv_sync_msg) { 1229 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1230 sizeof(msg_info), 1); 1231 } 1232#if 0 1233 printf("CTL:Frontend Enable\n"); 1234 } else { 1235 printf("%s: single mode, skipping frontend synchronization\n", 1236 __func__); 1237#endif 1238 } 1239 1240 softc = control_softc; 1241 1242 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1243 if (port_type & fe->port_type) 1244 { 1245#if 0 1246 printf("port %d\n", fe->targ_port); 1247#endif 1248 ctl_frontend_online(fe); 1249 } 1250 } 1251 1252 return (0); 1253} 1254 1255int 1256ctl_port_disable(ctl_port_type port_type) 1257{ 1258 struct ctl_softc *softc; 1259 struct ctl_frontend *fe; 1260 1261 softc = control_softc; 1262 1263 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1264 if (port_type & fe->port_type) 1265 ctl_frontend_offline(fe); 1266 } 1267 1268 return (0); 1269} 1270 1271/* 1272 * Returns 0 for success, 1 for failure. 1273 * Currently the only failure mode is if there aren't enough entries 1274 * allocated. So, in case of a failure, look at num_entries_dropped, 1275 * reallocate and try again. 1276 */ 1277int 1278ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1279 int *num_entries_filled, int *num_entries_dropped, 1280 ctl_port_type port_type, int no_virtual) 1281{ 1282 struct ctl_softc *softc; 1283 struct ctl_frontend *fe; 1284 int entries_dropped, entries_filled; 1285 int retval; 1286 int i; 1287 1288 softc = control_softc; 1289 1290 retval = 0; 1291 entries_filled = 0; 1292 entries_dropped = 0; 1293 1294 i = 0; 1295 mtx_lock(&softc->ctl_lock); 1296 STAILQ_FOREACH(fe, &softc->fe_list, links) { 1297 struct ctl_port_entry *entry; 1298 1299 if ((fe->port_type & port_type) == 0) 1300 continue; 1301 1302 if ((no_virtual != 0) 1303 && (fe->virtual_port != 0)) 1304 continue; 1305 1306 if (entries_filled >= num_entries_alloced) { 1307 entries_dropped++; 1308 continue; 1309 } 1310 entry = &entries[i]; 1311 1312 entry->port_type = fe->port_type; 1313 strlcpy(entry->port_name, fe->port_name, 1314 sizeof(entry->port_name)); 1315 entry->physical_port = fe->physical_port; 1316 entry->virtual_port = fe->virtual_port; 1317 entry->wwnn = fe->wwnn; 1318 entry->wwpn = fe->wwpn; 1319 1320 i++; 1321 entries_filled++; 1322 } 1323 1324 mtx_unlock(&softc->ctl_lock); 1325 1326 if (entries_dropped > 0) 1327 retval = 1; 1328 1329 *num_entries_dropped = entries_dropped; 1330 *num_entries_filled = entries_filled; 1331 1332 return (retval); 1333} 1334 1335static void 1336ctl_ioctl_online(void *arg) 1337{ 1338 struct ctl_ioctl_info *ioctl_info; 1339 1340 ioctl_info = (struct ctl_ioctl_info *)arg; 1341 1342 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1343} 1344 1345static void 1346ctl_ioctl_offline(void *arg) 1347{ 1348 struct ctl_ioctl_info *ioctl_info; 1349 1350 ioctl_info = (struct ctl_ioctl_info *)arg; 1351 1352 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1353} 1354 1355/* 1356 * Remove an initiator by port number and initiator ID. 1357 * Returns 0 for success, 1 for failure. 1358 */ 1359int 1360ctl_remove_initiator(int32_t targ_port, uint32_t iid) 1361{ 1362 struct ctl_softc *softc; 1363 1364 softc = control_softc; 1365 1366 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1367 1368 if ((targ_port < 0) 1369 || (targ_port > CTL_MAX_PORTS)) { 1370 printf("%s: invalid port number %d\n", __func__, targ_port); 1371 return (1); 1372 } 1373 if (iid > CTL_MAX_INIT_PER_PORT) { 1374 printf("%s: initiator ID %u > maximun %u!\n", 1375 __func__, iid, CTL_MAX_INIT_PER_PORT); 1376 return (1); 1377 } 1378 1379 mtx_lock(&softc->ctl_lock); 1380 1381 softc->wwpn_iid[targ_port][iid].in_use = 0; 1382 1383 mtx_unlock(&softc->ctl_lock); 1384 1385 return (0); 1386} 1387 1388/* 1389 * Add an initiator to the initiator map. 1390 * Returns 0 for success, 1 for failure. 1391 */ 1392int 1393ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid) 1394{ 1395 struct ctl_softc *softc; 1396 int retval; 1397 1398 softc = control_softc; 1399 1400 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1401 1402 retval = 0; 1403 1404 if ((targ_port < 0) 1405 || (targ_port > CTL_MAX_PORTS)) { 1406 printf("%s: invalid port number %d\n", __func__, targ_port); 1407 return (1); 1408 } 1409 if (iid > CTL_MAX_INIT_PER_PORT) { 1410 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n", 1411 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1412 return (1); 1413 } 1414 1415 mtx_lock(&softc->ctl_lock); 1416 1417 if (softc->wwpn_iid[targ_port][iid].in_use != 0) { 1418 /* 1419 * We don't treat this as an error. 1420 */ 1421 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) { 1422 printf("%s: port %d iid %u WWPN %#jx arrived again?\n", 1423 __func__, targ_port, iid, (uintmax_t)wwpn); 1424 goto bailout; 1425 } 1426 1427 /* 1428 * This is an error, but what do we do about it? The 1429 * driver is telling us we have a new WWPN for this 1430 * initiator ID, so we pretty much need to use it. 1431 */ 1432 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is " 1433 "still at that address\n", __func__, targ_port, iid, 1434 (uintmax_t)wwpn, 1435 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn); 1436 1437 /* 1438 * XXX KDM clear have_ca and ua_pending on each LUN for 1439 * this initiator. 1440 */ 1441 } 1442 softc->wwpn_iid[targ_port][iid].in_use = 1; 1443 softc->wwpn_iid[targ_port][iid].iid = iid; 1444 softc->wwpn_iid[targ_port][iid].wwpn = wwpn; 1445 softc->wwpn_iid[targ_port][iid].port = targ_port; 1446 1447bailout: 1448 1449 mtx_unlock(&softc->ctl_lock); 1450 1451 return (retval); 1452} 1453 1454/* 1455 * XXX KDM should we pretend to do something in the target/lun 1456 * enable/disable functions? 1457 */ 1458static int 1459ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id) 1460{ 1461 return (0); 1462} 1463 1464static int 1465ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id) 1466{ 1467 return (0); 1468} 1469 1470static int 1471ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1472{ 1473 return (0); 1474} 1475 1476static int 1477ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1478{ 1479 return (0); 1480} 1481 1482/* 1483 * Data movement routine for the CTL ioctl frontend port. 1484 */ 1485static int 1486ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1487{ 1488 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1489 struct ctl_sg_entry ext_entry, kern_entry; 1490 int ext_sglen, ext_sg_entries, kern_sg_entries; 1491 int ext_sg_start, ext_offset; 1492 int len_to_copy, len_copied; 1493 int kern_watermark, ext_watermark; 1494 int ext_sglist_malloced; 1495 int i, j; 1496 1497 ext_sglist_malloced = 0; 1498 ext_sg_start = 0; 1499 ext_offset = 0; 1500 1501 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1502 1503 /* 1504 * If this flag is set, fake the data transfer. 1505 */ 1506 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1507 ctsio->ext_data_filled = ctsio->ext_data_len; 1508 goto bailout; 1509 } 1510 1511 /* 1512 * To simplify things here, if we have a single buffer, stick it in 1513 * a S/G entry and just make it a single entry S/G list. 1514 */ 1515 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1516 int len_seen; 1517 1518 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1519 1520 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1521 M_WAITOK); 1522 ext_sglist_malloced = 1; 1523 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1524 ext_sglen) != 0) { 1525 ctl_set_internal_failure(ctsio, 1526 /*sks_valid*/ 0, 1527 /*retry_count*/ 0); 1528 goto bailout; 1529 } 1530 ext_sg_entries = ctsio->ext_sg_entries; 1531 len_seen = 0; 1532 for (i = 0; i < ext_sg_entries; i++) { 1533 if ((len_seen + ext_sglist[i].len) >= 1534 ctsio->ext_data_filled) { 1535 ext_sg_start = i; 1536 ext_offset = ctsio->ext_data_filled - len_seen; 1537 break; 1538 } 1539 len_seen += ext_sglist[i].len; 1540 } 1541 } else { 1542 ext_sglist = &ext_entry; 1543 ext_sglist->addr = ctsio->ext_data_ptr; 1544 ext_sglist->len = ctsio->ext_data_len; 1545 ext_sg_entries = 1; 1546 ext_sg_start = 0; 1547 ext_offset = ctsio->ext_data_filled; 1548 } 1549 1550 if (ctsio->kern_sg_entries > 0) { 1551 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1552 kern_sg_entries = ctsio->kern_sg_entries; 1553 } else { 1554 kern_sglist = &kern_entry; 1555 kern_sglist->addr = ctsio->kern_data_ptr; 1556 kern_sglist->len = ctsio->kern_data_len; 1557 kern_sg_entries = 1; 1558 } 1559 1560 1561 kern_watermark = 0; 1562 ext_watermark = ext_offset; 1563 len_copied = 0; 1564 for (i = ext_sg_start, j = 0; 1565 i < ext_sg_entries && j < kern_sg_entries;) { 1566 uint8_t *ext_ptr, *kern_ptr; 1567 1568 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1569 kern_sglist[j].len - kern_watermark); 1570 1571 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1572 ext_ptr = ext_ptr + ext_watermark; 1573 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1574 /* 1575 * XXX KDM fix this! 1576 */ 1577 panic("need to implement bus address support"); 1578#if 0 1579 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1580#endif 1581 } else 1582 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1583 kern_ptr = kern_ptr + kern_watermark; 1584 1585 kern_watermark += len_to_copy; 1586 ext_watermark += len_to_copy; 1587 1588 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1589 CTL_FLAG_DATA_IN) { 1590 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1591 "bytes to user\n", len_to_copy)); 1592 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1593 "to %p\n", kern_ptr, ext_ptr)); 1594 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1595 ctl_set_internal_failure(ctsio, 1596 /*sks_valid*/ 0, 1597 /*retry_count*/ 0); 1598 goto bailout; 1599 } 1600 } else { 1601 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1602 "bytes from user\n", len_to_copy)); 1603 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1604 "to %p\n", ext_ptr, kern_ptr)); 1605 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1606 ctl_set_internal_failure(ctsio, 1607 /*sks_valid*/ 0, 1608 /*retry_count*/0); 1609 goto bailout; 1610 } 1611 } 1612 1613 len_copied += len_to_copy; 1614 1615 if (ext_sglist[i].len == ext_watermark) { 1616 i++; 1617 ext_watermark = 0; 1618 } 1619 1620 if (kern_sglist[j].len == kern_watermark) { 1621 j++; 1622 kern_watermark = 0; 1623 } 1624 } 1625 1626 ctsio->ext_data_filled += len_copied; 1627 1628 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1629 "kern_sg_entries: %d\n", ext_sg_entries, 1630 kern_sg_entries)); 1631 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1632 "kern_data_len = %d\n", ctsio->ext_data_len, 1633 ctsio->kern_data_len)); 1634 1635 1636 /* XXX KDM set residual?? */ 1637bailout: 1638 1639 if (ext_sglist_malloced != 0) 1640 free(ext_sglist, M_CTL); 1641 1642 return (CTL_RETVAL_COMPLETE); 1643} 1644 1645/* 1646 * Serialize a command that went down the "wrong" side, and so was sent to 1647 * this controller for execution. The logic is a little different than the 1648 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1649 * sent back to the other side, but in the success case, we execute the 1650 * command on this side (XFER mode) or tell the other side to execute it 1651 * (SER_ONLY mode). 1652 */ 1653static int 1654ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1655{ 1656 struct ctl_softc *ctl_softc; 1657 union ctl_ha_msg msg_info; 1658 struct ctl_lun *lun; 1659 int retval = 0; 1660 uint32_t targ_lun; 1661 1662 ctl_softc = control_softc; 1663 1664 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1665 lun = ctl_softc->ctl_luns[targ_lun]; 1666 if (lun==NULL) 1667 { 1668 /* 1669 * Why isn't LUN defined? The other side wouldn't 1670 * send a cmd if the LUN is undefined. 1671 */ 1672 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1673 1674 /* "Logical unit not supported" */ 1675 ctl_set_sense_data(&msg_info.scsi.sense_data, 1676 lun, 1677 /*sense_format*/SSD_TYPE_NONE, 1678 /*current_error*/ 1, 1679 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1680 /*asc*/ 0x25, 1681 /*ascq*/ 0x00, 1682 SSD_ELEM_NONE); 1683 1684 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1685 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1686 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1687 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1688 msg_info.hdr.serializing_sc = NULL; 1689 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1690 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1691 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1692 } 1693 return(1); 1694 1695 } 1696 1697 mtx_lock(&lun->lun_lock); 1698 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1699 1700 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1701 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1702 ooa_links))) { 1703 case CTL_ACTION_BLOCK: 1704 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1705 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1706 blocked_links); 1707 break; 1708 case CTL_ACTION_PASS: 1709 case CTL_ACTION_SKIP: 1710 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1711 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1712 ctl_enqueue_rtr((union ctl_io *)ctsio); 1713 } else { 1714 1715 /* send msg back to other side */ 1716 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1717 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1718 msg_info.hdr.msg_type = CTL_MSG_R2R; 1719#if 0 1720 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1721#endif 1722 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1723 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1724 } 1725 } 1726 break; 1727 case CTL_ACTION_OVERLAP: 1728 /* OVERLAPPED COMMANDS ATTEMPTED */ 1729 ctl_set_sense_data(&msg_info.scsi.sense_data, 1730 lun, 1731 /*sense_format*/SSD_TYPE_NONE, 1732 /*current_error*/ 1, 1733 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1734 /*asc*/ 0x4E, 1735 /*ascq*/ 0x00, 1736 SSD_ELEM_NONE); 1737 1738 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1739 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1740 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1741 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1742 msg_info.hdr.serializing_sc = NULL; 1743 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1744#if 0 1745 printf("BAD JUJU:Major Bummer Overlap\n"); 1746#endif 1747 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1748 retval = 1; 1749 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1750 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1751 } 1752 break; 1753 case CTL_ACTION_OVERLAP_TAG: 1754 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1755 ctl_set_sense_data(&msg_info.scsi.sense_data, 1756 lun, 1757 /*sense_format*/SSD_TYPE_NONE, 1758 /*current_error*/ 1, 1759 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1760 /*asc*/ 0x4D, 1761 /*ascq*/ ctsio->tag_num & 0xff, 1762 SSD_ELEM_NONE); 1763 1764 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1765 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1766 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1767 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1768 msg_info.hdr.serializing_sc = NULL; 1769 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1770#if 0 1771 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1772#endif 1773 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1774 retval = 1; 1775 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1776 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1777 } 1778 break; 1779 case CTL_ACTION_ERROR: 1780 default: 1781 /* "Internal target failure" */ 1782 ctl_set_sense_data(&msg_info.scsi.sense_data, 1783 lun, 1784 /*sense_format*/SSD_TYPE_NONE, 1785 /*current_error*/ 1, 1786 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1787 /*asc*/ 0x44, 1788 /*ascq*/ 0x00, 1789 SSD_ELEM_NONE); 1790 1791 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1792 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1793 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1794 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1795 msg_info.hdr.serializing_sc = NULL; 1796 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1797#if 0 1798 printf("BAD JUJU:Major Bummer HW Error\n"); 1799#endif 1800 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1801 retval = 1; 1802 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1803 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1804 } 1805 break; 1806 } 1807 mtx_unlock(&lun->lun_lock); 1808 return (retval); 1809} 1810 1811static int 1812ctl_ioctl_submit_wait(union ctl_io *io) 1813{ 1814 struct ctl_fe_ioctl_params params; 1815 ctl_fe_ioctl_state last_state; 1816 int done, retval; 1817 1818 retval = 0; 1819 1820 bzero(¶ms, sizeof(params)); 1821 1822 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1823 cv_init(¶ms.sem, "ctlioccv"); 1824 params.state = CTL_IOCTL_INPROG; 1825 last_state = params.state; 1826 1827 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1828 1829 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1830 1831 /* This shouldn't happen */ 1832 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1833 return (retval); 1834 1835 done = 0; 1836 1837 do { 1838 mtx_lock(¶ms.ioctl_mtx); 1839 /* 1840 * Check the state here, and don't sleep if the state has 1841 * already changed (i.e. wakeup has already occured, but we 1842 * weren't waiting yet). 1843 */ 1844 if (params.state == last_state) { 1845 /* XXX KDM cv_wait_sig instead? */ 1846 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1847 } 1848 last_state = params.state; 1849 1850 switch (params.state) { 1851 case CTL_IOCTL_INPROG: 1852 /* Why did we wake up? */ 1853 /* XXX KDM error here? */ 1854 mtx_unlock(¶ms.ioctl_mtx); 1855 break; 1856 case CTL_IOCTL_DATAMOVE: 1857 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1858 1859 /* 1860 * change last_state back to INPROG to avoid 1861 * deadlock on subsequent data moves. 1862 */ 1863 params.state = last_state = CTL_IOCTL_INPROG; 1864 1865 mtx_unlock(¶ms.ioctl_mtx); 1866 ctl_ioctl_do_datamove(&io->scsiio); 1867 /* 1868 * Note that in some cases, most notably writes, 1869 * this will queue the I/O and call us back later. 1870 * In other cases, generally reads, this routine 1871 * will immediately call back and wake us up, 1872 * probably using our own context. 1873 */ 1874 io->scsiio.be_move_done(io); 1875 break; 1876 case CTL_IOCTL_DONE: 1877 mtx_unlock(¶ms.ioctl_mtx); 1878 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 1879 done = 1; 1880 break; 1881 default: 1882 mtx_unlock(¶ms.ioctl_mtx); 1883 /* XXX KDM error here? */ 1884 break; 1885 } 1886 } while (done == 0); 1887 1888 mtx_destroy(¶ms.ioctl_mtx); 1889 cv_destroy(¶ms.sem); 1890 1891 return (CTL_RETVAL_COMPLETE); 1892} 1893 1894static void 1895ctl_ioctl_datamove(union ctl_io *io) 1896{ 1897 struct ctl_fe_ioctl_params *params; 1898 1899 params = (struct ctl_fe_ioctl_params *) 1900 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1901 1902 mtx_lock(¶ms->ioctl_mtx); 1903 params->state = CTL_IOCTL_DATAMOVE; 1904 cv_broadcast(¶ms->sem); 1905 mtx_unlock(¶ms->ioctl_mtx); 1906} 1907 1908static void 1909ctl_ioctl_done(union ctl_io *io) 1910{ 1911 struct ctl_fe_ioctl_params *params; 1912 1913 params = (struct ctl_fe_ioctl_params *) 1914 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 1915 1916 mtx_lock(¶ms->ioctl_mtx); 1917 params->state = CTL_IOCTL_DONE; 1918 cv_broadcast(¶ms->sem); 1919 mtx_unlock(¶ms->ioctl_mtx); 1920} 1921 1922static void 1923ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 1924{ 1925 struct ctl_fe_ioctl_startstop_info *sd_info; 1926 1927 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 1928 1929 sd_info->hs_info.status = metatask->status; 1930 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 1931 sd_info->hs_info.luns_complete = 1932 metatask->taskinfo.startstop.luns_complete; 1933 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 1934 1935 cv_broadcast(&sd_info->sem); 1936} 1937 1938static void 1939ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 1940{ 1941 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 1942 1943 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 1944 1945 mtx_lock(fe_bbr_info->lock); 1946 fe_bbr_info->bbr_info->status = metatask->status; 1947 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 1948 fe_bbr_info->wakeup_done = 1; 1949 mtx_unlock(fe_bbr_info->lock); 1950 1951 cv_broadcast(&fe_bbr_info->sem); 1952} 1953 1954/* 1955 * Returns 0 for success, errno for failure. 1956 */ 1957static int 1958ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 1959 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 1960{ 1961 union ctl_io *io; 1962 int retval; 1963 1964 retval = 0; 1965 1966 mtx_lock(&lun->lun_lock); 1967 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 1968 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 1969 ooa_links)) { 1970 struct ctl_ooa_entry *entry; 1971 1972 /* 1973 * If we've got more than we can fit, just count the 1974 * remaining entries. 1975 */ 1976 if (*cur_fill_num >= ooa_hdr->alloc_num) 1977 continue; 1978 1979 entry = &kern_entries[*cur_fill_num]; 1980 1981 entry->tag_num = io->scsiio.tag_num; 1982 entry->lun_num = lun->lun; 1983#ifdef CTL_TIME_IO 1984 entry->start_bt = io->io_hdr.start_bt; 1985#endif 1986 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 1987 entry->cdb_len = io->scsiio.cdb_len; 1988 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 1989 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 1990 1991 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 1992 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 1993 1994 if (io->io_hdr.flags & CTL_FLAG_ABORT) 1995 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 1996 1997 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 1998 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 1999 2000 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2001 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2002 } 2003 mtx_unlock(&lun->lun_lock); 2004 2005 return (retval); 2006} 2007 2008static void * 2009ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2010 size_t error_str_len) 2011{ 2012 void *kptr; 2013 2014 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2015 2016 if (copyin(user_addr, kptr, len) != 0) { 2017 snprintf(error_str, error_str_len, "Error copying %d bytes " 2018 "from user address %p to kernel address %p", len, 2019 user_addr, kptr); 2020 free(kptr, M_CTL); 2021 return (NULL); 2022 } 2023 2024 return (kptr); 2025} 2026 2027static void 2028ctl_free_args(int num_be_args, struct ctl_be_arg *be_args) 2029{ 2030 int i; 2031 2032 if (be_args == NULL) 2033 return; 2034 2035 for (i = 0; i < num_be_args; i++) { 2036 free(be_args[i].kname, M_CTL); 2037 free(be_args[i].kvalue, M_CTL); 2038 } 2039 2040 free(be_args, M_CTL); 2041} 2042 2043static struct ctl_be_arg * 2044ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args, 2045 char *error_str, size_t error_str_len) 2046{ 2047 struct ctl_be_arg *args; 2048 int i; 2049 2050 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args), 2051 error_str, error_str_len); 2052 2053 if (args == NULL) 2054 goto bailout; 2055 2056 for (i = 0; i < num_be_args; i++) { 2057 args[i].kname = NULL; 2058 args[i].kvalue = NULL; 2059 } 2060 2061 for (i = 0; i < num_be_args; i++) { 2062 uint8_t *tmpptr; 2063 2064 args[i].kname = ctl_copyin_alloc(args[i].name, 2065 args[i].namelen, error_str, error_str_len); 2066 if (args[i].kname == NULL) 2067 goto bailout; 2068 2069 if (args[i].kname[args[i].namelen - 1] != '\0') { 2070 snprintf(error_str, error_str_len, "Argument %d " 2071 "name is not NUL-terminated", i); 2072 goto bailout; 2073 } 2074 2075 args[i].kvalue = NULL; 2076 2077 tmpptr = ctl_copyin_alloc(args[i].value, 2078 args[i].vallen, error_str, error_str_len); 2079 if (tmpptr == NULL) 2080 goto bailout; 2081 2082 args[i].kvalue = tmpptr; 2083 2084 if ((args[i].flags & CTL_BEARG_ASCII) 2085 && (tmpptr[args[i].vallen - 1] != '\0')) { 2086 snprintf(error_str, error_str_len, "Argument %d " 2087 "value is not NUL-terminated", i); 2088 goto bailout; 2089 } 2090 } 2091 2092 return (args); 2093bailout: 2094 2095 ctl_free_args(num_be_args, args); 2096 2097 return (NULL); 2098} 2099 2100/* 2101 * Escape characters that are illegal or not recommended in XML. 2102 */ 2103int 2104ctl_sbuf_printf_esc(struct sbuf *sb, char *str) 2105{ 2106 int retval; 2107 2108 retval = 0; 2109 2110 for (; *str; str++) { 2111 switch (*str) { 2112 case '&': 2113 retval = sbuf_printf(sb, "&"); 2114 break; 2115 case '>': 2116 retval = sbuf_printf(sb, ">"); 2117 break; 2118 case '<': 2119 retval = sbuf_printf(sb, "<"); 2120 break; 2121 default: 2122 retval = sbuf_putc(sb, *str); 2123 break; 2124 } 2125 2126 if (retval != 0) 2127 break; 2128 2129 } 2130 2131 return (retval); 2132} 2133 2134static int 2135ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2136 struct thread *td) 2137{ 2138 struct ctl_softc *softc; 2139 int retval; 2140 2141 softc = control_softc; 2142 2143 retval = 0; 2144 2145 switch (cmd) { 2146 case CTL_IO: { 2147 union ctl_io *io; 2148 void *pool_tmp; 2149 2150 /* 2151 * If we haven't been "enabled", don't allow any SCSI I/O 2152 * to this FETD. 2153 */ 2154 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2155 retval = EPERM; 2156 break; 2157 } 2158 2159 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref); 2160 if (io == NULL) { 2161 printf("ctl_ioctl: can't allocate ctl_io!\n"); 2162 retval = ENOSPC; 2163 break; 2164 } 2165 2166 /* 2167 * Need to save the pool reference so it doesn't get 2168 * spammed by the user's ctl_io. 2169 */ 2170 pool_tmp = io->io_hdr.pool; 2171 2172 memcpy(io, (void *)addr, sizeof(*io)); 2173 2174 io->io_hdr.pool = pool_tmp; 2175 /* 2176 * No status yet, so make sure the status is set properly. 2177 */ 2178 io->io_hdr.status = CTL_STATUS_NONE; 2179 2180 /* 2181 * The user sets the initiator ID, target and LUN IDs. 2182 */ 2183 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port; 2184 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2185 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2186 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2187 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2188 2189 retval = ctl_ioctl_submit_wait(io); 2190 2191 if (retval != 0) { 2192 ctl_free_io(io); 2193 break; 2194 } 2195 2196 memcpy((void *)addr, io, sizeof(*io)); 2197 2198 /* return this to our pool */ 2199 ctl_free_io(io); 2200 2201 break; 2202 } 2203 case CTL_ENABLE_PORT: 2204 case CTL_DISABLE_PORT: 2205 case CTL_SET_PORT_WWNS: { 2206 struct ctl_frontend *fe; 2207 struct ctl_port_entry *entry; 2208 2209 entry = (struct ctl_port_entry *)addr; 2210 2211 mtx_lock(&softc->ctl_lock); 2212 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2213 int action, done; 2214 2215 action = 0; 2216 done = 0; 2217 2218 if ((entry->port_type == CTL_PORT_NONE) 2219 && (entry->targ_port == fe->targ_port)) { 2220 /* 2221 * If the user only wants to enable or 2222 * disable or set WWNs on a specific port, 2223 * do the operation and we're done. 2224 */ 2225 action = 1; 2226 done = 1; 2227 } else if (entry->port_type & fe->port_type) { 2228 /* 2229 * Compare the user's type mask with the 2230 * particular frontend type to see if we 2231 * have a match. 2232 */ 2233 action = 1; 2234 done = 0; 2235 2236 /* 2237 * Make sure the user isn't trying to set 2238 * WWNs on multiple ports at the same time. 2239 */ 2240 if (cmd == CTL_SET_PORT_WWNS) { 2241 printf("%s: Can't set WWNs on " 2242 "multiple ports\n", __func__); 2243 retval = EINVAL; 2244 break; 2245 } 2246 } 2247 if (action != 0) { 2248 /* 2249 * XXX KDM we have to drop the lock here, 2250 * because the online/offline operations 2251 * can potentially block. We need to 2252 * reference count the frontends so they 2253 * can't go away, 2254 */ 2255 mtx_unlock(&softc->ctl_lock); 2256 2257 if (cmd == CTL_ENABLE_PORT) { 2258 struct ctl_lun *lun; 2259 2260 STAILQ_FOREACH(lun, &softc->lun_list, 2261 links) { 2262 fe->lun_enable(fe->targ_lun_arg, 2263 lun->target, 2264 lun->lun); 2265 } 2266 2267 ctl_frontend_online(fe); 2268 } else if (cmd == CTL_DISABLE_PORT) { 2269 struct ctl_lun *lun; 2270 2271 ctl_frontend_offline(fe); 2272 2273 STAILQ_FOREACH(lun, &softc->lun_list, 2274 links) { 2275 fe->lun_disable( 2276 fe->targ_lun_arg, 2277 lun->target, 2278 lun->lun); 2279 } 2280 } 2281 2282 mtx_lock(&softc->ctl_lock); 2283 2284 if (cmd == CTL_SET_PORT_WWNS) 2285 ctl_frontend_set_wwns(fe, 2286 (entry->flags & CTL_PORT_WWNN_VALID) ? 2287 1 : 0, entry->wwnn, 2288 (entry->flags & CTL_PORT_WWPN_VALID) ? 2289 1 : 0, entry->wwpn); 2290 } 2291 if (done != 0) 2292 break; 2293 } 2294 mtx_unlock(&softc->ctl_lock); 2295 break; 2296 } 2297 case CTL_GET_PORT_LIST: { 2298 struct ctl_frontend *fe; 2299 struct ctl_port_list *list; 2300 int i; 2301 2302 list = (struct ctl_port_list *)addr; 2303 2304 if (list->alloc_len != (list->alloc_num * 2305 sizeof(struct ctl_port_entry))) { 2306 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2307 "alloc_num %u * sizeof(struct ctl_port_entry) " 2308 "%zu\n", __func__, list->alloc_len, 2309 list->alloc_num, sizeof(struct ctl_port_entry)); 2310 retval = EINVAL; 2311 break; 2312 } 2313 list->fill_len = 0; 2314 list->fill_num = 0; 2315 list->dropped_num = 0; 2316 i = 0; 2317 mtx_lock(&softc->ctl_lock); 2318 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2319 struct ctl_port_entry entry, *list_entry; 2320 2321 if (list->fill_num >= list->alloc_num) { 2322 list->dropped_num++; 2323 continue; 2324 } 2325 2326 entry.port_type = fe->port_type; 2327 strlcpy(entry.port_name, fe->port_name, 2328 sizeof(entry.port_name)); 2329 entry.targ_port = fe->targ_port; 2330 entry.physical_port = fe->physical_port; 2331 entry.virtual_port = fe->virtual_port; 2332 entry.wwnn = fe->wwnn; 2333 entry.wwpn = fe->wwpn; 2334 if (fe->status & CTL_PORT_STATUS_ONLINE) 2335 entry.online = 1; 2336 else 2337 entry.online = 0; 2338 2339 list_entry = &list->entries[i]; 2340 2341 retval = copyout(&entry, list_entry, sizeof(entry)); 2342 if (retval != 0) { 2343 printf("%s: CTL_GET_PORT_LIST: copyout " 2344 "returned %d\n", __func__, retval); 2345 break; 2346 } 2347 i++; 2348 list->fill_num++; 2349 list->fill_len += sizeof(entry); 2350 } 2351 mtx_unlock(&softc->ctl_lock); 2352 2353 /* 2354 * If this is non-zero, we had a copyout fault, so there's 2355 * probably no point in attempting to set the status inside 2356 * the structure. 2357 */ 2358 if (retval != 0) 2359 break; 2360 2361 if (list->dropped_num > 0) 2362 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2363 else 2364 list->status = CTL_PORT_LIST_OK; 2365 break; 2366 } 2367 case CTL_DUMP_OOA: { 2368 struct ctl_lun *lun; 2369 union ctl_io *io; 2370 char printbuf[128]; 2371 struct sbuf sb; 2372 2373 mtx_lock(&softc->ctl_lock); 2374 printf("Dumping OOA queues:\n"); 2375 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2376 mtx_lock(&lun->lun_lock); 2377 for (io = (union ctl_io *)TAILQ_FIRST( 2378 &lun->ooa_queue); io != NULL; 2379 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2380 ooa_links)) { 2381 sbuf_new(&sb, printbuf, sizeof(printbuf), 2382 SBUF_FIXEDLEN); 2383 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2384 (intmax_t)lun->lun, 2385 io->scsiio.tag_num, 2386 (io->io_hdr.flags & 2387 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2388 (io->io_hdr.flags & 2389 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2390 (io->io_hdr.flags & 2391 CTL_FLAG_ABORT) ? " ABORT" : "", 2392 (io->io_hdr.flags & 2393 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2394 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2395 sbuf_finish(&sb); 2396 printf("%s\n", sbuf_data(&sb)); 2397 } 2398 mtx_unlock(&lun->lun_lock); 2399 } 2400 printf("OOA queues dump done\n"); 2401 mtx_unlock(&softc->ctl_lock); 2402 break; 2403 } 2404 case CTL_GET_OOA: { 2405 struct ctl_lun *lun; 2406 struct ctl_ooa *ooa_hdr; 2407 struct ctl_ooa_entry *entries; 2408 uint32_t cur_fill_num; 2409 2410 ooa_hdr = (struct ctl_ooa *)addr; 2411 2412 if ((ooa_hdr->alloc_len == 0) 2413 || (ooa_hdr->alloc_num == 0)) { 2414 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2415 "must be non-zero\n", __func__, 2416 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2417 retval = EINVAL; 2418 break; 2419 } 2420 2421 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2422 sizeof(struct ctl_ooa_entry))) { 2423 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2424 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2425 __func__, ooa_hdr->alloc_len, 2426 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2427 retval = EINVAL; 2428 break; 2429 } 2430 2431 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2432 if (entries == NULL) { 2433 printf("%s: could not allocate %d bytes for OOA " 2434 "dump\n", __func__, ooa_hdr->alloc_len); 2435 retval = ENOMEM; 2436 break; 2437 } 2438 2439 mtx_lock(&softc->ctl_lock); 2440 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2441 && ((ooa_hdr->lun_num > CTL_MAX_LUNS) 2442 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2443 mtx_unlock(&softc->ctl_lock); 2444 free(entries, M_CTL); 2445 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2446 __func__, (uintmax_t)ooa_hdr->lun_num); 2447 retval = EINVAL; 2448 break; 2449 } 2450 2451 cur_fill_num = 0; 2452 2453 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2454 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2455 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2456 ooa_hdr, entries); 2457 if (retval != 0) 2458 break; 2459 } 2460 if (retval != 0) { 2461 mtx_unlock(&softc->ctl_lock); 2462 free(entries, M_CTL); 2463 break; 2464 } 2465 } else { 2466 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2467 2468 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2469 entries); 2470 } 2471 mtx_unlock(&softc->ctl_lock); 2472 2473 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2474 ooa_hdr->fill_len = ooa_hdr->fill_num * 2475 sizeof(struct ctl_ooa_entry); 2476 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2477 if (retval != 0) { 2478 printf("%s: error copying out %d bytes for OOA dump\n", 2479 __func__, ooa_hdr->fill_len); 2480 } 2481 2482 getbintime(&ooa_hdr->cur_bt); 2483 2484 if (cur_fill_num > ooa_hdr->alloc_num) { 2485 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2486 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2487 } else { 2488 ooa_hdr->dropped_num = 0; 2489 ooa_hdr->status = CTL_OOA_OK; 2490 } 2491 2492 free(entries, M_CTL); 2493 break; 2494 } 2495 case CTL_CHECK_OOA: { 2496 union ctl_io *io; 2497 struct ctl_lun *lun; 2498 struct ctl_ooa_info *ooa_info; 2499 2500 2501 ooa_info = (struct ctl_ooa_info *)addr; 2502 2503 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2504 ooa_info->status = CTL_OOA_INVALID_LUN; 2505 break; 2506 } 2507 mtx_lock(&softc->ctl_lock); 2508 lun = softc->ctl_luns[ooa_info->lun_id]; 2509 if (lun == NULL) { 2510 mtx_unlock(&softc->ctl_lock); 2511 ooa_info->status = CTL_OOA_INVALID_LUN; 2512 break; 2513 } 2514 mtx_lock(&lun->lun_lock); 2515 mtx_unlock(&softc->ctl_lock); 2516 ooa_info->num_entries = 0; 2517 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2518 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2519 &io->io_hdr, ooa_links)) { 2520 ooa_info->num_entries++; 2521 } 2522 mtx_unlock(&lun->lun_lock); 2523 2524 ooa_info->status = CTL_OOA_SUCCESS; 2525 2526 break; 2527 } 2528 case CTL_HARD_START: 2529 case CTL_HARD_STOP: { 2530 struct ctl_fe_ioctl_startstop_info ss_info; 2531 struct cfi_metatask *metatask; 2532 struct mtx hs_mtx; 2533 2534 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2535 2536 cv_init(&ss_info.sem, "hard start/stop cv" ); 2537 2538 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2539 if (metatask == NULL) { 2540 retval = ENOMEM; 2541 mtx_destroy(&hs_mtx); 2542 break; 2543 } 2544 2545 if (cmd == CTL_HARD_START) 2546 metatask->tasktype = CFI_TASK_STARTUP; 2547 else 2548 metatask->tasktype = CFI_TASK_SHUTDOWN; 2549 2550 metatask->callback = ctl_ioctl_hard_startstop_callback; 2551 metatask->callback_arg = &ss_info; 2552 2553 cfi_action(metatask); 2554 2555 /* Wait for the callback */ 2556 mtx_lock(&hs_mtx); 2557 cv_wait_sig(&ss_info.sem, &hs_mtx); 2558 mtx_unlock(&hs_mtx); 2559 2560 /* 2561 * All information has been copied from the metatask by the 2562 * time cv_broadcast() is called, so we free the metatask here. 2563 */ 2564 cfi_free_metatask(metatask); 2565 2566 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2567 2568 mtx_destroy(&hs_mtx); 2569 break; 2570 } 2571 case CTL_BBRREAD: { 2572 struct ctl_bbrread_info *bbr_info; 2573 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2574 struct mtx bbr_mtx; 2575 struct cfi_metatask *metatask; 2576 2577 bbr_info = (struct ctl_bbrread_info *)addr; 2578 2579 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2580 2581 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2582 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2583 2584 fe_bbr_info.bbr_info = bbr_info; 2585 fe_bbr_info.lock = &bbr_mtx; 2586 2587 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2588 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2589 2590 if (metatask == NULL) { 2591 mtx_destroy(&bbr_mtx); 2592 cv_destroy(&fe_bbr_info.sem); 2593 retval = ENOMEM; 2594 break; 2595 } 2596 metatask->tasktype = CFI_TASK_BBRREAD; 2597 metatask->callback = ctl_ioctl_bbrread_callback; 2598 metatask->callback_arg = &fe_bbr_info; 2599 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2600 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2601 metatask->taskinfo.bbrread.len = bbr_info->len; 2602 2603 cfi_action(metatask); 2604 2605 mtx_lock(&bbr_mtx); 2606 while (fe_bbr_info.wakeup_done == 0) 2607 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2608 mtx_unlock(&bbr_mtx); 2609 2610 bbr_info->status = metatask->status; 2611 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2612 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2613 memcpy(&bbr_info->sense_data, 2614 &metatask->taskinfo.bbrread.sense_data, 2615 ctl_min(sizeof(bbr_info->sense_data), 2616 sizeof(metatask->taskinfo.bbrread.sense_data))); 2617 2618 cfi_free_metatask(metatask); 2619 2620 mtx_destroy(&bbr_mtx); 2621 cv_destroy(&fe_bbr_info.sem); 2622 2623 break; 2624 } 2625 case CTL_DELAY_IO: { 2626 struct ctl_io_delay_info *delay_info; 2627#ifdef CTL_IO_DELAY 2628 struct ctl_lun *lun; 2629#endif /* CTL_IO_DELAY */ 2630 2631 delay_info = (struct ctl_io_delay_info *)addr; 2632 2633#ifdef CTL_IO_DELAY 2634 mtx_lock(&softc->ctl_lock); 2635 2636 if ((delay_info->lun_id > CTL_MAX_LUNS) 2637 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2638 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2639 } else { 2640 lun = softc->ctl_luns[delay_info->lun_id]; 2641 mtx_lock(&lun->lun_lock); 2642 2643 delay_info->status = CTL_DELAY_STATUS_OK; 2644 2645 switch (delay_info->delay_type) { 2646 case CTL_DELAY_TYPE_CONT: 2647 break; 2648 case CTL_DELAY_TYPE_ONESHOT: 2649 break; 2650 default: 2651 delay_info->status = 2652 CTL_DELAY_STATUS_INVALID_TYPE; 2653 break; 2654 } 2655 2656 switch (delay_info->delay_loc) { 2657 case CTL_DELAY_LOC_DATAMOVE: 2658 lun->delay_info.datamove_type = 2659 delay_info->delay_type; 2660 lun->delay_info.datamove_delay = 2661 delay_info->delay_secs; 2662 break; 2663 case CTL_DELAY_LOC_DONE: 2664 lun->delay_info.done_type = 2665 delay_info->delay_type; 2666 lun->delay_info.done_delay = 2667 delay_info->delay_secs; 2668 break; 2669 default: 2670 delay_info->status = 2671 CTL_DELAY_STATUS_INVALID_LOC; 2672 break; 2673 } 2674 mtx_unlock(&lun->lun_lock); 2675 } 2676 2677 mtx_unlock(&softc->ctl_lock); 2678#else 2679 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2680#endif /* CTL_IO_DELAY */ 2681 break; 2682 } 2683 case CTL_REALSYNC_SET: { 2684 int *syncstate; 2685 2686 syncstate = (int *)addr; 2687 2688 mtx_lock(&softc->ctl_lock); 2689 switch (*syncstate) { 2690 case 0: 2691 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2692 break; 2693 case 1: 2694 softc->flags |= CTL_FLAG_REAL_SYNC; 2695 break; 2696 default: 2697 retval = EINVAL; 2698 break; 2699 } 2700 mtx_unlock(&softc->ctl_lock); 2701 break; 2702 } 2703 case CTL_REALSYNC_GET: { 2704 int *syncstate; 2705 2706 syncstate = (int*)addr; 2707 2708 mtx_lock(&softc->ctl_lock); 2709 if (softc->flags & CTL_FLAG_REAL_SYNC) 2710 *syncstate = 1; 2711 else 2712 *syncstate = 0; 2713 mtx_unlock(&softc->ctl_lock); 2714 2715 break; 2716 } 2717 case CTL_SETSYNC: 2718 case CTL_GETSYNC: { 2719 struct ctl_sync_info *sync_info; 2720 struct ctl_lun *lun; 2721 2722 sync_info = (struct ctl_sync_info *)addr; 2723 2724 mtx_lock(&softc->ctl_lock); 2725 lun = softc->ctl_luns[sync_info->lun_id]; 2726 if (lun == NULL) { 2727 mtx_unlock(&softc->ctl_lock); 2728 sync_info->status = CTL_GS_SYNC_NO_LUN; 2729 } 2730 /* 2731 * Get or set the sync interval. We're not bounds checking 2732 * in the set case, hopefully the user won't do something 2733 * silly. 2734 */ 2735 mtx_lock(&lun->lun_lock); 2736 mtx_unlock(&softc->ctl_lock); 2737 if (cmd == CTL_GETSYNC) 2738 sync_info->sync_interval = lun->sync_interval; 2739 else 2740 lun->sync_interval = sync_info->sync_interval; 2741 mtx_unlock(&lun->lun_lock); 2742 2743 sync_info->status = CTL_GS_SYNC_OK; 2744 2745 break; 2746 } 2747 case CTL_GETSTATS: { 2748 struct ctl_stats *stats; 2749 struct ctl_lun *lun; 2750 int i; 2751 2752 stats = (struct ctl_stats *)addr; 2753 2754 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2755 stats->alloc_len) { 2756 stats->status = CTL_SS_NEED_MORE_SPACE; 2757 stats->num_luns = softc->num_luns; 2758 break; 2759 } 2760 /* 2761 * XXX KDM no locking here. If the LUN list changes, 2762 * things can blow up. 2763 */ 2764 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2765 i++, lun = STAILQ_NEXT(lun, links)) { 2766 retval = copyout(&lun->stats, &stats->lun_stats[i], 2767 sizeof(lun->stats)); 2768 if (retval != 0) 2769 break; 2770 } 2771 stats->num_luns = softc->num_luns; 2772 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2773 softc->num_luns; 2774 stats->status = CTL_SS_OK; 2775#ifdef CTL_TIME_IO 2776 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2777#else 2778 stats->flags = CTL_STATS_FLAG_NONE; 2779#endif 2780 getnanouptime(&stats->timestamp); 2781 break; 2782 } 2783 case CTL_ERROR_INJECT: { 2784 struct ctl_error_desc *err_desc, *new_err_desc; 2785 struct ctl_lun *lun; 2786 2787 err_desc = (struct ctl_error_desc *)addr; 2788 2789 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2790 M_WAITOK | M_ZERO); 2791 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2792 2793 mtx_lock(&softc->ctl_lock); 2794 lun = softc->ctl_luns[err_desc->lun_id]; 2795 if (lun == NULL) { 2796 mtx_unlock(&softc->ctl_lock); 2797 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2798 __func__, (uintmax_t)err_desc->lun_id); 2799 retval = EINVAL; 2800 break; 2801 } 2802 mtx_lock(&lun->lun_lock); 2803 mtx_unlock(&softc->ctl_lock); 2804 2805 /* 2806 * We could do some checking here to verify the validity 2807 * of the request, but given the complexity of error 2808 * injection requests, the checking logic would be fairly 2809 * complex. 2810 * 2811 * For now, if the request is invalid, it just won't get 2812 * executed and might get deleted. 2813 */ 2814 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2815 2816 /* 2817 * XXX KDM check to make sure the serial number is unique, 2818 * in case we somehow manage to wrap. That shouldn't 2819 * happen for a very long time, but it's the right thing to 2820 * do. 2821 */ 2822 new_err_desc->serial = lun->error_serial; 2823 err_desc->serial = lun->error_serial; 2824 lun->error_serial++; 2825 2826 mtx_unlock(&lun->lun_lock); 2827 break; 2828 } 2829 case CTL_ERROR_INJECT_DELETE: { 2830 struct ctl_error_desc *delete_desc, *desc, *desc2; 2831 struct ctl_lun *lun; 2832 int delete_done; 2833 2834 delete_desc = (struct ctl_error_desc *)addr; 2835 delete_done = 0; 2836 2837 mtx_lock(&softc->ctl_lock); 2838 lun = softc->ctl_luns[delete_desc->lun_id]; 2839 if (lun == NULL) { 2840 mtx_unlock(&softc->ctl_lock); 2841 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2842 __func__, (uintmax_t)delete_desc->lun_id); 2843 retval = EINVAL; 2844 break; 2845 } 2846 mtx_lock(&lun->lun_lock); 2847 mtx_unlock(&softc->ctl_lock); 2848 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2849 if (desc->serial != delete_desc->serial) 2850 continue; 2851 2852 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2853 links); 2854 free(desc, M_CTL); 2855 delete_done = 1; 2856 } 2857 mtx_unlock(&lun->lun_lock); 2858 if (delete_done == 0) { 2859 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2860 "error serial %ju on LUN %u\n", __func__, 2861 delete_desc->serial, delete_desc->lun_id); 2862 retval = EINVAL; 2863 break; 2864 } 2865 break; 2866 } 2867 case CTL_DUMP_STRUCTS: { 2868 int i, j, k; 2869 struct ctl_frontend *fe; 2870 2871 printf("CTL IID to WWPN map start:\n"); 2872 for (i = 0; i < CTL_MAX_PORTS; i++) { 2873 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2874 if (softc->wwpn_iid[i][j].in_use == 0) 2875 continue; 2876 2877 printf("port %d iid %u WWPN %#jx\n", 2878 softc->wwpn_iid[i][j].port, 2879 softc->wwpn_iid[i][j].iid, 2880 (uintmax_t)softc->wwpn_iid[i][j].wwpn); 2881 } 2882 } 2883 printf("CTL IID to WWPN map end\n"); 2884 printf("CTL Persistent Reservation information start:\n"); 2885 for (i = 0; i < CTL_MAX_LUNS; i++) { 2886 struct ctl_lun *lun; 2887 2888 lun = softc->ctl_luns[i]; 2889 2890 if ((lun == NULL) 2891 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2892 continue; 2893 2894 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 2895 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2896 if (lun->per_res[j+k].registered == 0) 2897 continue; 2898 printf("LUN %d port %d iid %d key " 2899 "%#jx\n", i, j, k, 2900 (uintmax_t)scsi_8btou64( 2901 lun->per_res[j+k].res_key.key)); 2902 } 2903 } 2904 } 2905 printf("CTL Persistent Reservation information end\n"); 2906 printf("CTL Frontends:\n"); 2907 /* 2908 * XXX KDM calling this without a lock. We'd likely want 2909 * to drop the lock before calling the frontend's dump 2910 * routine anyway. 2911 */ 2912 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2913 printf("Frontend %s Type %u pport %d vport %d WWNN " 2914 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type, 2915 fe->physical_port, fe->virtual_port, 2916 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn); 2917 2918 /* 2919 * Frontends are not required to support the dump 2920 * routine. 2921 */ 2922 if (fe->fe_dump == NULL) 2923 continue; 2924 2925 fe->fe_dump(); 2926 } 2927 printf("CTL Frontend information end\n"); 2928 break; 2929 } 2930 case CTL_LUN_REQ: { 2931 struct ctl_lun_req *lun_req; 2932 struct ctl_backend_driver *backend; 2933 2934 lun_req = (struct ctl_lun_req *)addr; 2935 2936 backend = ctl_backend_find(lun_req->backend); 2937 if (backend == NULL) { 2938 lun_req->status = CTL_LUN_ERROR; 2939 snprintf(lun_req->error_str, 2940 sizeof(lun_req->error_str), 2941 "Backend \"%s\" not found.", 2942 lun_req->backend); 2943 break; 2944 } 2945 if (lun_req->num_be_args > 0) { 2946 lun_req->kern_be_args = ctl_copyin_args( 2947 lun_req->num_be_args, 2948 lun_req->be_args, 2949 lun_req->error_str, 2950 sizeof(lun_req->error_str)); 2951 if (lun_req->kern_be_args == NULL) { 2952 lun_req->status = CTL_LUN_ERROR; 2953 break; 2954 } 2955 } 2956 2957 retval = backend->ioctl(dev, cmd, addr, flag, td); 2958 2959 if (lun_req->num_be_args > 0) { 2960 ctl_free_args(lun_req->num_be_args, 2961 lun_req->kern_be_args); 2962 } 2963 break; 2964 } 2965 case CTL_LUN_LIST: { 2966 struct sbuf *sb; 2967 struct ctl_lun *lun; 2968 struct ctl_lun_list *list; 2969 struct ctl_be_lun_option *opt; 2970 2971 list = (struct ctl_lun_list *)addr; 2972 2973 /* 2974 * Allocate a fixed length sbuf here, based on the length 2975 * of the user's buffer. We could allocate an auto-extending 2976 * buffer, and then tell the user how much larger our 2977 * amount of data is than his buffer, but that presents 2978 * some problems: 2979 * 2980 * 1. The sbuf(9) routines use a blocking malloc, and so 2981 * we can't hold a lock while calling them with an 2982 * auto-extending buffer. 2983 * 2984 * 2. There is not currently a LUN reference counting 2985 * mechanism, outside of outstanding transactions on 2986 * the LUN's OOA queue. So a LUN could go away on us 2987 * while we're getting the LUN number, backend-specific 2988 * information, etc. Thus, given the way things 2989 * currently work, we need to hold the CTL lock while 2990 * grabbing LUN information. 2991 * 2992 * So, from the user's standpoint, the best thing to do is 2993 * allocate what he thinks is a reasonable buffer length, 2994 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 2995 * double the buffer length and try again. (And repeat 2996 * that until he succeeds.) 2997 */ 2998 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 2999 if (sb == NULL) { 3000 list->status = CTL_LUN_LIST_ERROR; 3001 snprintf(list->error_str, sizeof(list->error_str), 3002 "Unable to allocate %d bytes for LUN list", 3003 list->alloc_len); 3004 break; 3005 } 3006 3007 sbuf_printf(sb, "<ctllunlist>\n"); 3008 3009 mtx_lock(&softc->ctl_lock); 3010 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3011 mtx_lock(&lun->lun_lock); 3012 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3013 (uintmax_t)lun->lun); 3014 3015 /* 3016 * Bail out as soon as we see that we've overfilled 3017 * the buffer. 3018 */ 3019 if (retval != 0) 3020 break; 3021 3022 retval = sbuf_printf(sb, "\t<backend_type>%s" 3023 "</backend_type>\n", 3024 (lun->backend == NULL) ? "none" : 3025 lun->backend->name); 3026 3027 if (retval != 0) 3028 break; 3029 3030 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3031 lun->be_lun->lun_type); 3032 3033 if (retval != 0) 3034 break; 3035 3036 if (lun->backend == NULL) { 3037 retval = sbuf_printf(sb, "</lun>\n"); 3038 if (retval != 0) 3039 break; 3040 continue; 3041 } 3042 3043 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3044 (lun->be_lun->maxlba > 0) ? 3045 lun->be_lun->maxlba + 1 : 0); 3046 3047 if (retval != 0) 3048 break; 3049 3050 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3051 lun->be_lun->blocksize); 3052 3053 if (retval != 0) 3054 break; 3055 3056 retval = sbuf_printf(sb, "\t<serial_number>"); 3057 3058 if (retval != 0) 3059 break; 3060 3061 retval = ctl_sbuf_printf_esc(sb, 3062 lun->be_lun->serial_num); 3063 3064 if (retval != 0) 3065 break; 3066 3067 retval = sbuf_printf(sb, "</serial_number>\n"); 3068 3069 if (retval != 0) 3070 break; 3071 3072 retval = sbuf_printf(sb, "\t<device_id>"); 3073 3074 if (retval != 0) 3075 break; 3076 3077 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id); 3078 3079 if (retval != 0) 3080 break; 3081 3082 retval = sbuf_printf(sb, "</device_id>\n"); 3083 3084 if (retval != 0) 3085 break; 3086 3087 if (lun->backend->lun_info != NULL) { 3088 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3089 if (retval != 0) 3090 break; 3091 } 3092 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3093 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3094 opt->name, opt->value, opt->name); 3095 if (retval != 0) 3096 break; 3097 } 3098 3099 retval = sbuf_printf(sb, "</lun>\n"); 3100 3101 if (retval != 0) 3102 break; 3103 mtx_unlock(&lun->lun_lock); 3104 } 3105 if (lun != NULL) 3106 mtx_unlock(&lun->lun_lock); 3107 mtx_unlock(&softc->ctl_lock); 3108 3109 if ((retval != 0) 3110 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3111 retval = 0; 3112 sbuf_delete(sb); 3113 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3114 snprintf(list->error_str, sizeof(list->error_str), 3115 "Out of space, %d bytes is too small", 3116 list->alloc_len); 3117 break; 3118 } 3119 3120 sbuf_finish(sb); 3121 3122 retval = copyout(sbuf_data(sb), list->lun_xml, 3123 sbuf_len(sb) + 1); 3124 3125 list->fill_len = sbuf_len(sb) + 1; 3126 list->status = CTL_LUN_LIST_OK; 3127 sbuf_delete(sb); 3128 break; 3129 } 3130 case CTL_ISCSI: { 3131 struct ctl_iscsi *ci; 3132 struct ctl_frontend *fe; 3133 3134 ci = (struct ctl_iscsi *)addr; 3135 3136 mtx_lock(&softc->ctl_lock); 3137 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3138 if (strcmp(fe->port_name, "iscsi") == 0) 3139 break; 3140 } 3141 mtx_unlock(&softc->ctl_lock); 3142 3143 if (fe == NULL) { 3144 ci->status = CTL_ISCSI_ERROR; 3145 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found."); 3146 break; 3147 } 3148 3149 retval = fe->ioctl(dev, cmd, addr, flag, td); 3150 break; 3151 } 3152 default: { 3153 /* XXX KDM should we fix this? */ 3154#if 0 3155 struct ctl_backend_driver *backend; 3156 unsigned int type; 3157 int found; 3158 3159 found = 0; 3160 3161 /* 3162 * We encode the backend type as the ioctl type for backend 3163 * ioctls. So parse it out here, and then search for a 3164 * backend of this type. 3165 */ 3166 type = _IOC_TYPE(cmd); 3167 3168 STAILQ_FOREACH(backend, &softc->be_list, links) { 3169 if (backend->type == type) { 3170 found = 1; 3171 break; 3172 } 3173 } 3174 if (found == 0) { 3175 printf("ctl: unknown ioctl command %#lx or backend " 3176 "%d\n", cmd, type); 3177 retval = EINVAL; 3178 break; 3179 } 3180 retval = backend->ioctl(dev, cmd, addr, flag, td); 3181#endif 3182 retval = ENOTTY; 3183 break; 3184 } 3185 } 3186 return (retval); 3187} 3188 3189uint32_t 3190ctl_get_initindex(struct ctl_nexus *nexus) 3191{ 3192 if (nexus->targ_port < CTL_MAX_PORTS) 3193 return (nexus->initid.id + 3194 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3195 else 3196 return (nexus->initid.id + 3197 ((nexus->targ_port - CTL_MAX_PORTS) * 3198 CTL_MAX_INIT_PER_PORT)); 3199} 3200 3201uint32_t 3202ctl_get_resindex(struct ctl_nexus *nexus) 3203{ 3204 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3205} 3206 3207uint32_t 3208ctl_port_idx(int port_num) 3209{ 3210 if (port_num < CTL_MAX_PORTS) 3211 return(port_num); 3212 else 3213 return(port_num - CTL_MAX_PORTS); 3214} 3215 3216/* 3217 * Note: This only works for bitmask sizes that are at least 32 bits, and 3218 * that are a power of 2. 3219 */ 3220int 3221ctl_ffz(uint32_t *mask, uint32_t size) 3222{ 3223 uint32_t num_chunks, num_pieces; 3224 int i, j; 3225 3226 num_chunks = (size >> 5); 3227 if (num_chunks == 0) 3228 num_chunks++; 3229 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3230 3231 for (i = 0; i < num_chunks; i++) { 3232 for (j = 0; j < num_pieces; j++) { 3233 if ((mask[i] & (1 << j)) == 0) 3234 return ((i << 5) + j); 3235 } 3236 } 3237 3238 return (-1); 3239} 3240 3241int 3242ctl_set_mask(uint32_t *mask, uint32_t bit) 3243{ 3244 uint32_t chunk, piece; 3245 3246 chunk = bit >> 5; 3247 piece = bit % (sizeof(uint32_t) * 8); 3248 3249 if ((mask[chunk] & (1 << piece)) != 0) 3250 return (-1); 3251 else 3252 mask[chunk] |= (1 << piece); 3253 3254 return (0); 3255} 3256 3257int 3258ctl_clear_mask(uint32_t *mask, uint32_t bit) 3259{ 3260 uint32_t chunk, piece; 3261 3262 chunk = bit >> 5; 3263 piece = bit % (sizeof(uint32_t) * 8); 3264 3265 if ((mask[chunk] & (1 << piece)) == 0) 3266 return (-1); 3267 else 3268 mask[chunk] &= ~(1 << piece); 3269 3270 return (0); 3271} 3272 3273int 3274ctl_is_set(uint32_t *mask, uint32_t bit) 3275{ 3276 uint32_t chunk, piece; 3277 3278 chunk = bit >> 5; 3279 piece = bit % (sizeof(uint32_t) * 8); 3280 3281 if ((mask[chunk] & (1 << piece)) == 0) 3282 return (0); 3283 else 3284 return (1); 3285} 3286 3287#ifdef unused 3288/* 3289 * The bus, target and lun are optional, they can be filled in later. 3290 * can_wait is used to determine whether we can wait on the malloc or not. 3291 */ 3292union ctl_io* 3293ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3294 uint32_t targ_lun, int can_wait) 3295{ 3296 union ctl_io *io; 3297 3298 if (can_wait) 3299 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3300 else 3301 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3302 3303 if (io != NULL) { 3304 io->io_hdr.io_type = io_type; 3305 io->io_hdr.targ_port = targ_port; 3306 /* 3307 * XXX KDM this needs to change/go away. We need to move 3308 * to a preallocated pool of ctl_scsiio structures. 3309 */ 3310 io->io_hdr.nexus.targ_target.id = targ_target; 3311 io->io_hdr.nexus.targ_lun = targ_lun; 3312 } 3313 3314 return (io); 3315} 3316 3317void 3318ctl_kfree_io(union ctl_io *io) 3319{ 3320 free(io, M_CTL); 3321} 3322#endif /* unused */ 3323 3324/* 3325 * ctl_softc, pool_type, total_ctl_io are passed in. 3326 * npool is passed out. 3327 */ 3328int 3329ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type, 3330 uint32_t total_ctl_io, struct ctl_io_pool **npool) 3331{ 3332 uint32_t i; 3333 union ctl_io *cur_io, *next_io; 3334 struct ctl_io_pool *pool; 3335 int retval; 3336 3337 retval = 0; 3338 3339 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3340 M_NOWAIT | M_ZERO); 3341 if (pool == NULL) { 3342 retval = ENOMEM; 3343 goto bailout; 3344 } 3345 3346 pool->type = pool_type; 3347 pool->ctl_softc = ctl_softc; 3348 3349 mtx_lock(&ctl_softc->pool_lock); 3350 pool->id = ctl_softc->cur_pool_id++; 3351 mtx_unlock(&ctl_softc->pool_lock); 3352 3353 pool->flags = CTL_POOL_FLAG_NONE; 3354 pool->refcount = 1; /* Reference for validity. */ 3355 STAILQ_INIT(&pool->free_queue); 3356 3357 /* 3358 * XXX KDM other options here: 3359 * - allocate a page at a time 3360 * - allocate one big chunk of memory. 3361 * Page allocation might work well, but would take a little more 3362 * tracking. 3363 */ 3364 for (i = 0; i < total_ctl_io; i++) { 3365 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO, 3366 M_NOWAIT); 3367 if (cur_io == NULL) { 3368 retval = ENOMEM; 3369 break; 3370 } 3371 cur_io->io_hdr.pool = pool; 3372 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links); 3373 pool->total_ctl_io++; 3374 pool->free_ctl_io++; 3375 } 3376 3377 if (retval != 0) { 3378 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3379 cur_io != NULL; cur_io = next_io) { 3380 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr, 3381 links); 3382 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, 3383 ctl_io_hdr, links); 3384 free(cur_io, M_CTLIO); 3385 } 3386 3387 free(pool, M_CTL); 3388 goto bailout; 3389 } 3390 mtx_lock(&ctl_softc->pool_lock); 3391 ctl_softc->num_pools++; 3392 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links); 3393 /* 3394 * Increment our usage count if this is an external consumer, so we 3395 * can't get unloaded until the external consumer (most likely a 3396 * FETD) unloads and frees his pool. 3397 * 3398 * XXX KDM will this increment the caller's module use count, or 3399 * mine? 3400 */ 3401#if 0 3402 if ((pool_type != CTL_POOL_EMERGENCY) 3403 && (pool_type != CTL_POOL_INTERNAL) 3404 && (pool_type != CTL_POOL_IOCTL) 3405 && (pool_type != CTL_POOL_4OTHERSC)) 3406 MOD_INC_USE_COUNT; 3407#endif 3408 3409 mtx_unlock(&ctl_softc->pool_lock); 3410 3411 *npool = pool; 3412 3413bailout: 3414 3415 return (retval); 3416} 3417 3418static int 3419ctl_pool_acquire(struct ctl_io_pool *pool) 3420{ 3421 3422 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED); 3423 3424 if (pool->flags & CTL_POOL_FLAG_INVALID) 3425 return (EINVAL); 3426 3427 pool->refcount++; 3428 3429 return (0); 3430} 3431 3432static void 3433ctl_pool_release(struct ctl_io_pool *pool) 3434{ 3435 struct ctl_softc *ctl_softc = pool->ctl_softc; 3436 union ctl_io *io; 3437 3438 mtx_assert(&ctl_softc->pool_lock, MA_OWNED); 3439 3440 if (--pool->refcount != 0) 3441 return; 3442 3443 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) { 3444 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr, 3445 links); 3446 free(io, M_CTLIO); 3447 } 3448 3449 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links); 3450 ctl_softc->num_pools--; 3451 3452 /* 3453 * XXX KDM will this decrement the caller's usage count or mine? 3454 */ 3455#if 0 3456 if ((pool->type != CTL_POOL_EMERGENCY) 3457 && (pool->type != CTL_POOL_INTERNAL) 3458 && (pool->type != CTL_POOL_IOCTL)) 3459 MOD_DEC_USE_COUNT; 3460#endif 3461 3462 free(pool, M_CTL); 3463} 3464 3465void 3466ctl_pool_free(struct ctl_io_pool *pool) 3467{ 3468 struct ctl_softc *ctl_softc; 3469 3470 if (pool == NULL) 3471 return; 3472 3473 ctl_softc = pool->ctl_softc; 3474 mtx_lock(&ctl_softc->pool_lock); 3475 pool->flags |= CTL_POOL_FLAG_INVALID; 3476 ctl_pool_release(pool); 3477 mtx_unlock(&ctl_softc->pool_lock); 3478} 3479 3480/* 3481 * This routine does not block (except for spinlocks of course). 3482 * It tries to allocate a ctl_io union from the caller's pool as quickly as 3483 * possible. 3484 */ 3485union ctl_io * 3486ctl_alloc_io(void *pool_ref) 3487{ 3488 union ctl_io *io; 3489 struct ctl_softc *ctl_softc; 3490 struct ctl_io_pool *pool, *npool; 3491 struct ctl_io_pool *emergency_pool; 3492 3493 pool = (struct ctl_io_pool *)pool_ref; 3494 3495 if (pool == NULL) { 3496 printf("%s: pool is NULL\n", __func__); 3497 return (NULL); 3498 } 3499 3500 emergency_pool = NULL; 3501 3502 ctl_softc = pool->ctl_softc; 3503 3504 mtx_lock(&ctl_softc->pool_lock); 3505 /* 3506 * First, try to get the io structure from the user's pool. 3507 */ 3508 if (ctl_pool_acquire(pool) == 0) { 3509 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue); 3510 if (io != NULL) { 3511 STAILQ_REMOVE_HEAD(&pool->free_queue, links); 3512 pool->total_allocated++; 3513 pool->free_ctl_io--; 3514 mtx_unlock(&ctl_softc->pool_lock); 3515 return (io); 3516 } else 3517 ctl_pool_release(pool); 3518 } 3519 /* 3520 * If he doesn't have any io structures left, search for an 3521 * emergency pool and grab one from there. 3522 */ 3523 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) { 3524 if (npool->type != CTL_POOL_EMERGENCY) 3525 continue; 3526 3527 if (ctl_pool_acquire(npool) != 0) 3528 continue; 3529 3530 emergency_pool = npool; 3531 3532 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue); 3533 if (io != NULL) { 3534 STAILQ_REMOVE_HEAD(&npool->free_queue, links); 3535 npool->total_allocated++; 3536 npool->free_ctl_io--; 3537 mtx_unlock(&ctl_softc->pool_lock); 3538 return (io); 3539 } else 3540 ctl_pool_release(npool); 3541 } 3542 3543 /* Drop the spinlock before we malloc */ 3544 mtx_unlock(&ctl_softc->pool_lock); 3545 3546 /* 3547 * The emergency pool (if it exists) didn't have one, so try an 3548 * atomic (i.e. nonblocking) malloc and see if we get lucky. 3549 */ 3550 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT); 3551 if (io != NULL) { 3552 /* 3553 * If the emergency pool exists but is empty, add this 3554 * ctl_io to its list when it gets freed. 3555 */ 3556 if (emergency_pool != NULL) { 3557 mtx_lock(&ctl_softc->pool_lock); 3558 if (ctl_pool_acquire(emergency_pool) == 0) { 3559 io->io_hdr.pool = emergency_pool; 3560 emergency_pool->total_ctl_io++; 3561 /* 3562 * Need to bump this, otherwise 3563 * total_allocated and total_freed won't 3564 * match when we no longer have anything 3565 * outstanding. 3566 */ 3567 emergency_pool->total_allocated++; 3568 } 3569 mtx_unlock(&ctl_softc->pool_lock); 3570 } else 3571 io->io_hdr.pool = NULL; 3572 } 3573 3574 return (io); 3575} 3576 3577void 3578ctl_free_io(union ctl_io *io) 3579{ 3580 if (io == NULL) 3581 return; 3582 3583 /* 3584 * If this ctl_io has a pool, return it to that pool. 3585 */ 3586 if (io->io_hdr.pool != NULL) { 3587 struct ctl_io_pool *pool; 3588 3589 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3590 mtx_lock(&pool->ctl_softc->pool_lock); 3591 io->io_hdr.io_type = 0xff; 3592 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links); 3593 pool->total_freed++; 3594 pool->free_ctl_io++; 3595 ctl_pool_release(pool); 3596 mtx_unlock(&pool->ctl_softc->pool_lock); 3597 } else { 3598 /* 3599 * Otherwise, just free it. We probably malloced it and 3600 * the emergency pool wasn't available. 3601 */ 3602 free(io, M_CTLIO); 3603 } 3604 3605} 3606 3607void 3608ctl_zero_io(union ctl_io *io) 3609{ 3610 void *pool_ref; 3611 3612 if (io == NULL) 3613 return; 3614 3615 /* 3616 * May need to preserve linked list pointers at some point too. 3617 */ 3618 pool_ref = io->io_hdr.pool; 3619 3620 memset(io, 0, sizeof(*io)); 3621 3622 io->io_hdr.pool = pool_ref; 3623} 3624 3625/* 3626 * This routine is currently used for internal copies of ctl_ios that need 3627 * to persist for some reason after we've already returned status to the 3628 * FETD. (Thus the flag set.) 3629 * 3630 * XXX XXX 3631 * Note that this makes a blind copy of all fields in the ctl_io, except 3632 * for the pool reference. This includes any memory that has been 3633 * allocated! That memory will no longer be valid after done has been 3634 * called, so this would be VERY DANGEROUS for command that actually does 3635 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3636 * start and stop commands, which don't transfer any data, so this is not a 3637 * problem. If it is used for anything else, the caller would also need to 3638 * allocate data buffer space and this routine would need to be modified to 3639 * copy the data buffer(s) as well. 3640 */ 3641void 3642ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3643{ 3644 void *pool_ref; 3645 3646 if ((src == NULL) 3647 || (dest == NULL)) 3648 return; 3649 3650 /* 3651 * May need to preserve linked list pointers at some point too. 3652 */ 3653 pool_ref = dest->io_hdr.pool; 3654 3655 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3656 3657 dest->io_hdr.pool = pool_ref; 3658 /* 3659 * We need to know that this is an internal copy, and doesn't need 3660 * to get passed back to the FETD that allocated it. 3661 */ 3662 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3663} 3664 3665#ifdef NEEDTOPORT 3666static void 3667ctl_update_power_subpage(struct copan_power_subpage *page) 3668{ 3669 int num_luns, num_partitions, config_type; 3670 struct ctl_softc *softc; 3671 cs_BOOL_t aor_present, shelf_50pct_power; 3672 cs_raidset_personality_t rs_type; 3673 int max_active_luns; 3674 3675 softc = control_softc; 3676 3677 /* subtract out the processor LUN */ 3678 num_luns = softc->num_luns - 1; 3679 /* 3680 * Default to 7 LUNs active, which was the only number we allowed 3681 * in the past. 3682 */ 3683 max_active_luns = 7; 3684 3685 num_partitions = config_GetRsPartitionInfo(); 3686 config_type = config_GetConfigType(); 3687 shelf_50pct_power = config_GetShelfPowerMode(); 3688 aor_present = config_IsAorRsPresent(); 3689 3690 rs_type = ddb_GetRsRaidType(1); 3691 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5) 3692 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) { 3693 EPRINT(0, "Unsupported RS type %d!", rs_type); 3694 } 3695 3696 3697 page->total_luns = num_luns; 3698 3699 switch (config_type) { 3700 case 40: 3701 /* 3702 * In a 40 drive configuration, it doesn't matter what DC 3703 * cards we have, whether we have AOR enabled or not, 3704 * partitioning or not, or what type of RAIDset we have. 3705 * In that scenario, we can power up every LUN we present 3706 * to the user. 3707 */ 3708 max_active_luns = num_luns; 3709 3710 break; 3711 case 64: 3712 if (shelf_50pct_power == CS_FALSE) { 3713 /* 25% power */ 3714 if (aor_present == CS_TRUE) { 3715 if (rs_type == 3716 CS_RAIDSET_PERSONALITY_RAID5) { 3717 max_active_luns = 7; 3718 } else if (rs_type == 3719 CS_RAIDSET_PERSONALITY_RAID1){ 3720 max_active_luns = 14; 3721 } else { 3722 /* XXX KDM now what?? */ 3723 } 3724 } else { 3725 if (rs_type == 3726 CS_RAIDSET_PERSONALITY_RAID5) { 3727 max_active_luns = 8; 3728 } else if (rs_type == 3729 CS_RAIDSET_PERSONALITY_RAID1){ 3730 max_active_luns = 16; 3731 } else { 3732 /* XXX KDM now what?? */ 3733 } 3734 } 3735 } else { 3736 /* 50% power */ 3737 /* 3738 * With 50% power in a 64 drive configuration, we 3739 * can power all LUNs we present. 3740 */ 3741 max_active_luns = num_luns; 3742 } 3743 break; 3744 case 112: 3745 if (shelf_50pct_power == CS_FALSE) { 3746 /* 25% power */ 3747 if (aor_present == CS_TRUE) { 3748 if (rs_type == 3749 CS_RAIDSET_PERSONALITY_RAID5) { 3750 max_active_luns = 7; 3751 } else if (rs_type == 3752 CS_RAIDSET_PERSONALITY_RAID1){ 3753 max_active_luns = 14; 3754 } else { 3755 /* XXX KDM now what?? */ 3756 } 3757 } else { 3758 if (rs_type == 3759 CS_RAIDSET_PERSONALITY_RAID5) { 3760 max_active_luns = 8; 3761 } else if (rs_type == 3762 CS_RAIDSET_PERSONALITY_RAID1){ 3763 max_active_luns = 16; 3764 } else { 3765 /* XXX KDM now what?? */ 3766 } 3767 } 3768 } else { 3769 /* 50% power */ 3770 if (aor_present == CS_TRUE) { 3771 if (rs_type == 3772 CS_RAIDSET_PERSONALITY_RAID5) { 3773 max_active_luns = 14; 3774 } else if (rs_type == 3775 CS_RAIDSET_PERSONALITY_RAID1){ 3776 /* 3777 * We're assuming here that disk 3778 * caching is enabled, and so we're 3779 * able to power up half of each 3780 * LUN, and cache all writes. 3781 */ 3782 max_active_luns = num_luns; 3783 } else { 3784 /* XXX KDM now what?? */ 3785 } 3786 } else { 3787 if (rs_type == 3788 CS_RAIDSET_PERSONALITY_RAID5) { 3789 max_active_luns = 15; 3790 } else if (rs_type == 3791 CS_RAIDSET_PERSONALITY_RAID1){ 3792 max_active_luns = 30; 3793 } else { 3794 /* XXX KDM now what?? */ 3795 } 3796 } 3797 } 3798 break; 3799 default: 3800 /* 3801 * In this case, we have an unknown configuration, so we 3802 * just use the default from above. 3803 */ 3804 break; 3805 } 3806 3807 page->max_active_luns = max_active_luns; 3808#if 0 3809 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__, 3810 page->total_luns, page->max_active_luns); 3811#endif 3812} 3813#endif /* NEEDTOPORT */ 3814 3815/* 3816 * This routine could be used in the future to load default and/or saved 3817 * mode page parameters for a particuar lun. 3818 */ 3819static int 3820ctl_init_page_index(struct ctl_lun *lun) 3821{ 3822 int i; 3823 struct ctl_page_index *page_index; 3824 struct ctl_softc *softc; 3825 3826 memcpy(&lun->mode_pages.index, page_index_template, 3827 sizeof(page_index_template)); 3828 3829 softc = lun->ctl_softc; 3830 3831 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3832 3833 page_index = &lun->mode_pages.index[i]; 3834 /* 3835 * If this is a disk-only mode page, there's no point in 3836 * setting it up. For some pages, we have to have some 3837 * basic information about the disk in order to calculate the 3838 * mode page data. 3839 */ 3840 if ((lun->be_lun->lun_type != T_DIRECT) 3841 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3842 continue; 3843 3844 switch (page_index->page_code & SMPH_PC_MASK) { 3845 case SMS_FORMAT_DEVICE_PAGE: { 3846 struct scsi_format_page *format_page; 3847 3848 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3849 panic("subpage is incorrect!"); 3850 3851 /* 3852 * Sectors per track are set above. Bytes per 3853 * sector need to be set here on a per-LUN basis. 3854 */ 3855 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3856 &format_page_default, 3857 sizeof(format_page_default)); 3858 memcpy(&lun->mode_pages.format_page[ 3859 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3860 sizeof(format_page_changeable)); 3861 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3862 &format_page_default, 3863 sizeof(format_page_default)); 3864 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3865 &format_page_default, 3866 sizeof(format_page_default)); 3867 3868 format_page = &lun->mode_pages.format_page[ 3869 CTL_PAGE_CURRENT]; 3870 scsi_ulto2b(lun->be_lun->blocksize, 3871 format_page->bytes_per_sector); 3872 3873 format_page = &lun->mode_pages.format_page[ 3874 CTL_PAGE_DEFAULT]; 3875 scsi_ulto2b(lun->be_lun->blocksize, 3876 format_page->bytes_per_sector); 3877 3878 format_page = &lun->mode_pages.format_page[ 3879 CTL_PAGE_SAVED]; 3880 scsi_ulto2b(lun->be_lun->blocksize, 3881 format_page->bytes_per_sector); 3882 3883 page_index->page_data = 3884 (uint8_t *)lun->mode_pages.format_page; 3885 break; 3886 } 3887 case SMS_RIGID_DISK_PAGE: { 3888 struct scsi_rigid_disk_page *rigid_disk_page; 3889 uint32_t sectors_per_cylinder; 3890 uint64_t cylinders; 3891#ifndef __XSCALE__ 3892 int shift; 3893#endif /* !__XSCALE__ */ 3894 3895 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3896 panic("invalid subpage value %d", 3897 page_index->subpage); 3898 3899 /* 3900 * Rotation rate and sectors per track are set 3901 * above. We calculate the cylinders here based on 3902 * capacity. Due to the number of heads and 3903 * sectors per track we're using, smaller arrays 3904 * may turn out to have 0 cylinders. Linux and 3905 * FreeBSD don't pay attention to these mode pages 3906 * to figure out capacity, but Solaris does. It 3907 * seems to deal with 0 cylinders just fine, and 3908 * works out a fake geometry based on the capacity. 3909 */ 3910 memcpy(&lun->mode_pages.rigid_disk_page[ 3911 CTL_PAGE_CURRENT], &rigid_disk_page_default, 3912 sizeof(rigid_disk_page_default)); 3913 memcpy(&lun->mode_pages.rigid_disk_page[ 3914 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3915 sizeof(rigid_disk_page_changeable)); 3916 memcpy(&lun->mode_pages.rigid_disk_page[ 3917 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3918 sizeof(rigid_disk_page_default)); 3919 memcpy(&lun->mode_pages.rigid_disk_page[ 3920 CTL_PAGE_SAVED], &rigid_disk_page_default, 3921 sizeof(rigid_disk_page_default)); 3922 3923 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3924 CTL_DEFAULT_HEADS; 3925 3926 /* 3927 * The divide method here will be more accurate, 3928 * probably, but results in floating point being 3929 * used in the kernel on i386 (__udivdi3()). On the 3930 * XScale, though, __udivdi3() is implemented in 3931 * software. 3932 * 3933 * The shift method for cylinder calculation is 3934 * accurate if sectors_per_cylinder is a power of 3935 * 2. Otherwise it might be slightly off -- you 3936 * might have a bit of a truncation problem. 3937 */ 3938#ifdef __XSCALE__ 3939 cylinders = (lun->be_lun->maxlba + 1) / 3940 sectors_per_cylinder; 3941#else 3942 for (shift = 31; shift > 0; shift--) { 3943 if (sectors_per_cylinder & (1 << shift)) 3944 break; 3945 } 3946 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3947#endif 3948 3949 /* 3950 * We've basically got 3 bytes, or 24 bits for the 3951 * cylinder size in the mode page. If we're over, 3952 * just round down to 2^24. 3953 */ 3954 if (cylinders > 0xffffff) 3955 cylinders = 0xffffff; 3956 3957 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3958 CTL_PAGE_CURRENT]; 3959 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3960 3961 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3962 CTL_PAGE_DEFAULT]; 3963 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3964 3965 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3966 CTL_PAGE_SAVED]; 3967 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3968 3969 page_index->page_data = 3970 (uint8_t *)lun->mode_pages.rigid_disk_page; 3971 break; 3972 } 3973 case SMS_CACHING_PAGE: { 3974 3975 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3976 panic("invalid subpage value %d", 3977 page_index->subpage); 3978 /* 3979 * Defaults should be okay here, no calculations 3980 * needed. 3981 */ 3982 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 3983 &caching_page_default, 3984 sizeof(caching_page_default)); 3985 memcpy(&lun->mode_pages.caching_page[ 3986 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3987 sizeof(caching_page_changeable)); 3988 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3989 &caching_page_default, 3990 sizeof(caching_page_default)); 3991 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 3992 &caching_page_default, 3993 sizeof(caching_page_default)); 3994 page_index->page_data = 3995 (uint8_t *)lun->mode_pages.caching_page; 3996 break; 3997 } 3998 case SMS_CONTROL_MODE_PAGE: { 3999 4000 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4001 panic("invalid subpage value %d", 4002 page_index->subpage); 4003 4004 /* 4005 * Defaults should be okay here, no calculations 4006 * needed. 4007 */ 4008 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4009 &control_page_default, 4010 sizeof(control_page_default)); 4011 memcpy(&lun->mode_pages.control_page[ 4012 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4013 sizeof(control_page_changeable)); 4014 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4015 &control_page_default, 4016 sizeof(control_page_default)); 4017 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4018 &control_page_default, 4019 sizeof(control_page_default)); 4020 page_index->page_data = 4021 (uint8_t *)lun->mode_pages.control_page; 4022 break; 4023 4024 } 4025 case SMS_VENDOR_SPECIFIC_PAGE:{ 4026 switch (page_index->subpage) { 4027 case PWR_SUBPAGE_CODE: { 4028 struct copan_power_subpage *current_page, 4029 *saved_page; 4030 4031 memcpy(&lun->mode_pages.power_subpage[ 4032 CTL_PAGE_CURRENT], 4033 &power_page_default, 4034 sizeof(power_page_default)); 4035 memcpy(&lun->mode_pages.power_subpage[ 4036 CTL_PAGE_CHANGEABLE], 4037 &power_page_changeable, 4038 sizeof(power_page_changeable)); 4039 memcpy(&lun->mode_pages.power_subpage[ 4040 CTL_PAGE_DEFAULT], 4041 &power_page_default, 4042 sizeof(power_page_default)); 4043 memcpy(&lun->mode_pages.power_subpage[ 4044 CTL_PAGE_SAVED], 4045 &power_page_default, 4046 sizeof(power_page_default)); 4047 page_index->page_data = 4048 (uint8_t *)lun->mode_pages.power_subpage; 4049 4050 current_page = (struct copan_power_subpage *) 4051 (page_index->page_data + 4052 (page_index->page_len * 4053 CTL_PAGE_CURRENT)); 4054 saved_page = (struct copan_power_subpage *) 4055 (page_index->page_data + 4056 (page_index->page_len * 4057 CTL_PAGE_SAVED)); 4058 break; 4059 } 4060 case APS_SUBPAGE_CODE: { 4061 struct copan_aps_subpage *current_page, 4062 *saved_page; 4063 4064 // This gets set multiple times but 4065 // it should always be the same. It's 4066 // only done during init so who cares. 4067 index_to_aps_page = i; 4068 4069 memcpy(&lun->mode_pages.aps_subpage[ 4070 CTL_PAGE_CURRENT], 4071 &aps_page_default, 4072 sizeof(aps_page_default)); 4073 memcpy(&lun->mode_pages.aps_subpage[ 4074 CTL_PAGE_CHANGEABLE], 4075 &aps_page_changeable, 4076 sizeof(aps_page_changeable)); 4077 memcpy(&lun->mode_pages.aps_subpage[ 4078 CTL_PAGE_DEFAULT], 4079 &aps_page_default, 4080 sizeof(aps_page_default)); 4081 memcpy(&lun->mode_pages.aps_subpage[ 4082 CTL_PAGE_SAVED], 4083 &aps_page_default, 4084 sizeof(aps_page_default)); 4085 page_index->page_data = 4086 (uint8_t *)lun->mode_pages.aps_subpage; 4087 4088 current_page = (struct copan_aps_subpage *) 4089 (page_index->page_data + 4090 (page_index->page_len * 4091 CTL_PAGE_CURRENT)); 4092 saved_page = (struct copan_aps_subpage *) 4093 (page_index->page_data + 4094 (page_index->page_len * 4095 CTL_PAGE_SAVED)); 4096 break; 4097 } 4098 case DBGCNF_SUBPAGE_CODE: { 4099 struct copan_debugconf_subpage *current_page, 4100 *saved_page; 4101 4102 memcpy(&lun->mode_pages.debugconf_subpage[ 4103 CTL_PAGE_CURRENT], 4104 &debugconf_page_default, 4105 sizeof(debugconf_page_default)); 4106 memcpy(&lun->mode_pages.debugconf_subpage[ 4107 CTL_PAGE_CHANGEABLE], 4108 &debugconf_page_changeable, 4109 sizeof(debugconf_page_changeable)); 4110 memcpy(&lun->mode_pages.debugconf_subpage[ 4111 CTL_PAGE_DEFAULT], 4112 &debugconf_page_default, 4113 sizeof(debugconf_page_default)); 4114 memcpy(&lun->mode_pages.debugconf_subpage[ 4115 CTL_PAGE_SAVED], 4116 &debugconf_page_default, 4117 sizeof(debugconf_page_default)); 4118 page_index->page_data = 4119 (uint8_t *)lun->mode_pages.debugconf_subpage; 4120 4121 current_page = (struct copan_debugconf_subpage *) 4122 (page_index->page_data + 4123 (page_index->page_len * 4124 CTL_PAGE_CURRENT)); 4125 saved_page = (struct copan_debugconf_subpage *) 4126 (page_index->page_data + 4127 (page_index->page_len * 4128 CTL_PAGE_SAVED)); 4129 break; 4130 } 4131 default: 4132 panic("invalid subpage value %d", 4133 page_index->subpage); 4134 break; 4135 } 4136 break; 4137 } 4138 default: 4139 panic("invalid page value %d", 4140 page_index->page_code & SMPH_PC_MASK); 4141 break; 4142 } 4143 } 4144 4145 return (CTL_RETVAL_COMPLETE); 4146} 4147 4148/* 4149 * LUN allocation. 4150 * 4151 * Requirements: 4152 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4153 * wants us to allocate the LUN and he can block. 4154 * - ctl_softc is always set 4155 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4156 * 4157 * Returns 0 for success, non-zero (errno) for failure. 4158 */ 4159static int 4160ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4161 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4162{ 4163 struct ctl_lun *nlun, *lun; 4164 struct ctl_frontend *fe; 4165 int lun_number, i, lun_malloced; 4166 4167 if (be_lun == NULL) 4168 return (EINVAL); 4169 4170 /* 4171 * We currently only support Direct Access or Processor LUN types. 4172 */ 4173 switch (be_lun->lun_type) { 4174 case T_DIRECT: 4175 break; 4176 case T_PROCESSOR: 4177 break; 4178 case T_SEQUENTIAL: 4179 case T_CHANGER: 4180 default: 4181 be_lun->lun_config_status(be_lun->be_lun, 4182 CTL_LUN_CONFIG_FAILURE); 4183 break; 4184 } 4185 if (ctl_lun == NULL) { 4186 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4187 lun_malloced = 1; 4188 } else { 4189 lun_malloced = 0; 4190 lun = ctl_lun; 4191 } 4192 4193 memset(lun, 0, sizeof(*lun)); 4194 if (lun_malloced) 4195 lun->flags = CTL_LUN_MALLOCED; 4196 4197 mtx_lock(&ctl_softc->ctl_lock); 4198 /* 4199 * See if the caller requested a particular LUN number. If so, see 4200 * if it is available. Otherwise, allocate the first available LUN. 4201 */ 4202 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4203 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4204 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4205 mtx_unlock(&ctl_softc->ctl_lock); 4206 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4207 printf("ctl: requested LUN ID %d is higher " 4208 "than CTL_MAX_LUNS - 1 (%d)\n", 4209 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4210 } else { 4211 /* 4212 * XXX KDM return an error, or just assign 4213 * another LUN ID in this case?? 4214 */ 4215 printf("ctl: requested LUN ID %d is already " 4216 "in use\n", be_lun->req_lun_id); 4217 } 4218 if (lun->flags & CTL_LUN_MALLOCED) 4219 free(lun, M_CTL); 4220 be_lun->lun_config_status(be_lun->be_lun, 4221 CTL_LUN_CONFIG_FAILURE); 4222 return (ENOSPC); 4223 } 4224 lun_number = be_lun->req_lun_id; 4225 } else { 4226 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4227 if (lun_number == -1) { 4228 mtx_unlock(&ctl_softc->ctl_lock); 4229 printf("ctl: can't allocate LUN on target %ju, out of " 4230 "LUNs\n", (uintmax_t)target_id.id); 4231 if (lun->flags & CTL_LUN_MALLOCED) 4232 free(lun, M_CTL); 4233 be_lun->lun_config_status(be_lun->be_lun, 4234 CTL_LUN_CONFIG_FAILURE); 4235 return (ENOSPC); 4236 } 4237 } 4238 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4239 4240 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4241 lun->target = target_id; 4242 lun->lun = lun_number; 4243 lun->be_lun = be_lun; 4244 /* 4245 * The processor LUN is always enabled. Disk LUNs come on line 4246 * disabled, and must be enabled by the backend. 4247 */ 4248 lun->flags |= CTL_LUN_DISABLED; 4249 lun->backend = be_lun->be; 4250 be_lun->ctl_lun = lun; 4251 be_lun->lun_id = lun_number; 4252 atomic_add_int(&be_lun->be->num_luns, 1); 4253 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4254 lun->flags |= CTL_LUN_STOPPED; 4255 4256 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4257 lun->flags |= CTL_LUN_INOPERABLE; 4258 4259 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4260 lun->flags |= CTL_LUN_PRIMARY_SC; 4261 4262 lun->ctl_softc = ctl_softc; 4263 TAILQ_INIT(&lun->ooa_queue); 4264 TAILQ_INIT(&lun->blocked_queue); 4265 STAILQ_INIT(&lun->error_list); 4266 4267 /* 4268 * Initialize the mode page index. 4269 */ 4270 ctl_init_page_index(lun); 4271 4272 /* 4273 * Set the poweron UA for all initiators on this LUN only. 4274 */ 4275 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4276 lun->pending_sense[i].ua_pending = CTL_UA_POWERON; 4277 4278 /* 4279 * Now, before we insert this lun on the lun list, set the lun 4280 * inventory changed UA for all other luns. 4281 */ 4282 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4283 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4284 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4285 } 4286 } 4287 4288 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4289 4290 ctl_softc->ctl_luns[lun_number] = lun; 4291 4292 ctl_softc->num_luns++; 4293 4294 /* Setup statistics gathering */ 4295 lun->stats.device_type = be_lun->lun_type; 4296 lun->stats.lun_number = lun_number; 4297 if (lun->stats.device_type == T_DIRECT) 4298 lun->stats.blocksize = be_lun->blocksize; 4299 else 4300 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4301 for (i = 0;i < CTL_MAX_PORTS;i++) 4302 lun->stats.ports[i].targ_port = i; 4303 4304 mtx_unlock(&ctl_softc->ctl_lock); 4305 4306 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4307 4308 /* 4309 * Run through each registered FETD and bring it online if it isn't 4310 * already. Enable the target ID if it hasn't been enabled, and 4311 * enable this particular LUN. 4312 */ 4313 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) { 4314 int retval; 4315 4316 /* 4317 * XXX KDM this only works for ONE TARGET ID. We'll need 4318 * to do things differently if we go to a multiple target 4319 * ID scheme. 4320 */ 4321 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) { 4322 4323 retval = fe->targ_enable(fe->targ_lun_arg, target_id); 4324 if (retval != 0) { 4325 printf("ctl_alloc_lun: FETD %s port %d " 4326 "returned error %d for targ_enable on " 4327 "target %ju\n", fe->port_name, 4328 fe->targ_port, retval, 4329 (uintmax_t)target_id.id); 4330 } else 4331 fe->status |= CTL_PORT_STATUS_TARG_ONLINE; 4332 } 4333 4334 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number); 4335 if (retval != 0) { 4336 printf("ctl_alloc_lun: FETD %s port %d returned error " 4337 "%d for lun_enable on target %ju lun %d\n", 4338 fe->port_name, fe->targ_port, retval, 4339 (uintmax_t)target_id.id, lun_number); 4340 } else 4341 fe->status |= CTL_PORT_STATUS_LUN_ONLINE; 4342 } 4343 return (0); 4344} 4345 4346/* 4347 * Delete a LUN. 4348 * Assumptions: 4349 * - LUN has already been marked invalid and any pending I/O has been taken 4350 * care of. 4351 */ 4352static int 4353ctl_free_lun(struct ctl_lun *lun) 4354{ 4355 struct ctl_softc *softc; 4356#if 0 4357 struct ctl_frontend *fe; 4358#endif 4359 struct ctl_lun *nlun; 4360 int i; 4361 4362 softc = lun->ctl_softc; 4363 4364 mtx_assert(&softc->ctl_lock, MA_OWNED); 4365 4366 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4367 4368 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4369 4370 softc->ctl_luns[lun->lun] = NULL; 4371 4372 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4373 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4374 4375 softc->num_luns--; 4376 4377 /* 4378 * XXX KDM this scheme only works for a single target/multiple LUN 4379 * setup. It needs to be revamped for a multiple target scheme. 4380 * 4381 * XXX KDM this results in fe->lun_disable() getting called twice, 4382 * once when ctl_disable_lun() is called, and a second time here. 4383 * We really need to re-think the LUN disable semantics. There 4384 * should probably be several steps/levels to LUN removal: 4385 * - disable 4386 * - invalidate 4387 * - free 4388 * 4389 * Right now we only have a disable method when communicating to 4390 * the front end ports, at least for individual LUNs. 4391 */ 4392#if 0 4393 STAILQ_FOREACH(fe, &softc->fe_list, links) { 4394 int retval; 4395 4396 retval = fe->lun_disable(fe->targ_lun_arg, lun->target, 4397 lun->lun); 4398 if (retval != 0) { 4399 printf("ctl_free_lun: FETD %s port %d returned error " 4400 "%d for lun_disable on target %ju lun %jd\n", 4401 fe->port_name, fe->targ_port, retval, 4402 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4403 } 4404 4405 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4406 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4407 4408 retval = fe->targ_disable(fe->targ_lun_arg,lun->target); 4409 if (retval != 0) { 4410 printf("ctl_free_lun: FETD %s port %d " 4411 "returned error %d for targ_disable on " 4412 "target %ju\n", fe->port_name, 4413 fe->targ_port, retval, 4414 (uintmax_t)lun->target.id); 4415 } else 4416 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4417 4418 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4419 continue; 4420 4421#if 0 4422 fe->port_offline(fe->onoff_arg); 4423 fe->status &= ~CTL_PORT_STATUS_ONLINE; 4424#endif 4425 } 4426 } 4427#endif 4428 4429 /* 4430 * Tell the backend to free resources, if this LUN has a backend. 4431 */ 4432 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4433 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4434 4435 mtx_destroy(&lun->lun_lock); 4436 if (lun->flags & CTL_LUN_MALLOCED) 4437 free(lun, M_CTL); 4438 4439 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4440 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4441 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE; 4442 } 4443 } 4444 4445 return (0); 4446} 4447 4448static void 4449ctl_create_lun(struct ctl_be_lun *be_lun) 4450{ 4451 struct ctl_softc *ctl_softc; 4452 4453 ctl_softc = control_softc; 4454 4455 /* 4456 * ctl_alloc_lun() should handle all potential failure cases. 4457 */ 4458 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4459} 4460 4461int 4462ctl_add_lun(struct ctl_be_lun *be_lun) 4463{ 4464 struct ctl_softc *ctl_softc = control_softc; 4465 4466 mtx_lock(&ctl_softc->ctl_lock); 4467 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4468 mtx_unlock(&ctl_softc->ctl_lock); 4469 wakeup(&ctl_softc->pending_lun_queue); 4470 4471 return (0); 4472} 4473 4474int 4475ctl_enable_lun(struct ctl_be_lun *be_lun) 4476{ 4477 struct ctl_softc *ctl_softc; 4478 struct ctl_frontend *fe, *nfe; 4479 struct ctl_lun *lun; 4480 int retval; 4481 4482 ctl_softc = control_softc; 4483 4484 lun = (struct ctl_lun *)be_lun->ctl_lun; 4485 4486 mtx_lock(&ctl_softc->ctl_lock); 4487 mtx_lock(&lun->lun_lock); 4488 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4489 /* 4490 * eh? Why did we get called if the LUN is already 4491 * enabled? 4492 */ 4493 mtx_unlock(&lun->lun_lock); 4494 mtx_unlock(&ctl_softc->ctl_lock); 4495 return (0); 4496 } 4497 lun->flags &= ~CTL_LUN_DISABLED; 4498 mtx_unlock(&lun->lun_lock); 4499 4500 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) { 4501 nfe = STAILQ_NEXT(fe, links); 4502 4503 /* 4504 * Drop the lock while we call the FETD's enable routine. 4505 * This can lead to a callback into CTL (at least in the 4506 * case of the internal initiator frontend. 4507 */ 4508 mtx_unlock(&ctl_softc->ctl_lock); 4509 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun); 4510 mtx_lock(&ctl_softc->ctl_lock); 4511 if (retval != 0) { 4512 printf("%s: FETD %s port %d returned error " 4513 "%d for lun_enable on target %ju lun %jd\n", 4514 __func__, fe->port_name, fe->targ_port, retval, 4515 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4516 } 4517#if 0 4518 else { 4519 /* NOTE: TODO: why does lun enable affect port status? */ 4520 fe->status |= CTL_PORT_STATUS_LUN_ONLINE; 4521 } 4522#endif 4523 } 4524 4525 mtx_unlock(&ctl_softc->ctl_lock); 4526 4527 return (0); 4528} 4529 4530int 4531ctl_disable_lun(struct ctl_be_lun *be_lun) 4532{ 4533 struct ctl_softc *ctl_softc; 4534 struct ctl_frontend *fe; 4535 struct ctl_lun *lun; 4536 int retval; 4537 4538 ctl_softc = control_softc; 4539 4540 lun = (struct ctl_lun *)be_lun->ctl_lun; 4541 4542 mtx_lock(&ctl_softc->ctl_lock); 4543 mtx_lock(&lun->lun_lock); 4544 if (lun->flags & CTL_LUN_DISABLED) { 4545 mtx_unlock(&lun->lun_lock); 4546 mtx_unlock(&ctl_softc->ctl_lock); 4547 return (0); 4548 } 4549 lun->flags |= CTL_LUN_DISABLED; 4550 mtx_unlock(&lun->lun_lock); 4551 4552 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) { 4553 mtx_unlock(&ctl_softc->ctl_lock); 4554 /* 4555 * Drop the lock before we call the frontend's disable 4556 * routine, to avoid lock order reversals. 4557 * 4558 * XXX KDM what happens if the frontend list changes while 4559 * we're traversing it? It's unlikely, but should be handled. 4560 */ 4561 retval = fe->lun_disable(fe->targ_lun_arg, lun->target, 4562 lun->lun); 4563 mtx_lock(&ctl_softc->ctl_lock); 4564 if (retval != 0) { 4565 printf("ctl_alloc_lun: FETD %s port %d returned error " 4566 "%d for lun_disable on target %ju lun %jd\n", 4567 fe->port_name, fe->targ_port, retval, 4568 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4569 } 4570 } 4571 4572 mtx_unlock(&ctl_softc->ctl_lock); 4573 4574 return (0); 4575} 4576 4577int 4578ctl_start_lun(struct ctl_be_lun *be_lun) 4579{ 4580 struct ctl_softc *ctl_softc; 4581 struct ctl_lun *lun; 4582 4583 ctl_softc = control_softc; 4584 4585 lun = (struct ctl_lun *)be_lun->ctl_lun; 4586 4587 mtx_lock(&lun->lun_lock); 4588 lun->flags &= ~CTL_LUN_STOPPED; 4589 mtx_unlock(&lun->lun_lock); 4590 4591 return (0); 4592} 4593 4594int 4595ctl_stop_lun(struct ctl_be_lun *be_lun) 4596{ 4597 struct ctl_softc *ctl_softc; 4598 struct ctl_lun *lun; 4599 4600 ctl_softc = control_softc; 4601 4602 lun = (struct ctl_lun *)be_lun->ctl_lun; 4603 4604 mtx_lock(&lun->lun_lock); 4605 lun->flags |= CTL_LUN_STOPPED; 4606 mtx_unlock(&lun->lun_lock); 4607 4608 return (0); 4609} 4610 4611int 4612ctl_lun_offline(struct ctl_be_lun *be_lun) 4613{ 4614 struct ctl_softc *ctl_softc; 4615 struct ctl_lun *lun; 4616 4617 ctl_softc = control_softc; 4618 4619 lun = (struct ctl_lun *)be_lun->ctl_lun; 4620 4621 mtx_lock(&lun->lun_lock); 4622 lun->flags |= CTL_LUN_OFFLINE; 4623 mtx_unlock(&lun->lun_lock); 4624 4625 return (0); 4626} 4627 4628int 4629ctl_lun_online(struct ctl_be_lun *be_lun) 4630{ 4631 struct ctl_softc *ctl_softc; 4632 struct ctl_lun *lun; 4633 4634 ctl_softc = control_softc; 4635 4636 lun = (struct ctl_lun *)be_lun->ctl_lun; 4637 4638 mtx_lock(&lun->lun_lock); 4639 lun->flags &= ~CTL_LUN_OFFLINE; 4640 mtx_unlock(&lun->lun_lock); 4641 4642 return (0); 4643} 4644 4645int 4646ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4647{ 4648 struct ctl_softc *ctl_softc; 4649 struct ctl_lun *lun; 4650 4651 ctl_softc = control_softc; 4652 4653 lun = (struct ctl_lun *)be_lun->ctl_lun; 4654 4655 mtx_lock(&lun->lun_lock); 4656 4657 /* 4658 * The LUN needs to be disabled before it can be marked invalid. 4659 */ 4660 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4661 mtx_unlock(&lun->lun_lock); 4662 return (-1); 4663 } 4664 /* 4665 * Mark the LUN invalid. 4666 */ 4667 lun->flags |= CTL_LUN_INVALID; 4668 4669 /* 4670 * If there is nothing in the OOA queue, go ahead and free the LUN. 4671 * If we have something in the OOA queue, we'll free it when the 4672 * last I/O completes. 4673 */ 4674 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4675 mtx_unlock(&lun->lun_lock); 4676 mtx_lock(&ctl_softc->ctl_lock); 4677 ctl_free_lun(lun); 4678 mtx_unlock(&ctl_softc->ctl_lock); 4679 } else 4680 mtx_unlock(&lun->lun_lock); 4681 4682 return (0); 4683} 4684 4685int 4686ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4687{ 4688 struct ctl_softc *ctl_softc; 4689 struct ctl_lun *lun; 4690 4691 ctl_softc = control_softc; 4692 lun = (struct ctl_lun *)be_lun->ctl_lun; 4693 4694 mtx_lock(&lun->lun_lock); 4695 lun->flags |= CTL_LUN_INOPERABLE; 4696 mtx_unlock(&lun->lun_lock); 4697 4698 return (0); 4699} 4700 4701int 4702ctl_lun_operable(struct ctl_be_lun *be_lun) 4703{ 4704 struct ctl_softc *ctl_softc; 4705 struct ctl_lun *lun; 4706 4707 ctl_softc = control_softc; 4708 lun = (struct ctl_lun *)be_lun->ctl_lun; 4709 4710 mtx_lock(&lun->lun_lock); 4711 lun->flags &= ~CTL_LUN_INOPERABLE; 4712 mtx_unlock(&lun->lun_lock); 4713 4714 return (0); 4715} 4716 4717int 4718ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus, 4719 int lock) 4720{ 4721 struct ctl_softc *softc; 4722 struct ctl_lun *lun; 4723 struct copan_aps_subpage *current_sp; 4724 struct ctl_page_index *page_index; 4725 int i; 4726 4727 softc = control_softc; 4728 4729 mtx_lock(&softc->ctl_lock); 4730 4731 lun = (struct ctl_lun *)be_lun->ctl_lun; 4732 mtx_lock(&lun->lun_lock); 4733 4734 page_index = NULL; 4735 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 4736 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 4737 APS_PAGE_CODE) 4738 continue; 4739 4740 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE) 4741 continue; 4742 page_index = &lun->mode_pages.index[i]; 4743 } 4744 4745 if (page_index == NULL) { 4746 mtx_unlock(&lun->lun_lock); 4747 mtx_unlock(&softc->ctl_lock); 4748 printf("%s: APS subpage not found for lun %ju!\n", __func__, 4749 (uintmax_t)lun->lun); 4750 return (1); 4751 } 4752#if 0 4753 if ((softc->aps_locked_lun != 0) 4754 && (softc->aps_locked_lun != lun->lun)) { 4755 printf("%s: attempt to lock LUN %llu when %llu is already " 4756 "locked\n"); 4757 mtx_unlock(&lun->lun_lock); 4758 mtx_unlock(&softc->ctl_lock); 4759 return (1); 4760 } 4761#endif 4762 4763 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 4764 (page_index->page_len * CTL_PAGE_CURRENT)); 4765 4766 if (lock != 0) { 4767 current_sp->lock_active = APS_LOCK_ACTIVE; 4768 softc->aps_locked_lun = lun->lun; 4769 } else { 4770 current_sp->lock_active = 0; 4771 softc->aps_locked_lun = 0; 4772 } 4773 4774 4775 /* 4776 * If we're in HA mode, try to send the lock message to the other 4777 * side. 4778 */ 4779 if (ctl_is_single == 0) { 4780 int isc_retval; 4781 union ctl_ha_msg lock_msg; 4782 4783 lock_msg.hdr.nexus = *nexus; 4784 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK; 4785 if (lock != 0) 4786 lock_msg.aps.lock_flag = 1; 4787 else 4788 lock_msg.aps.lock_flag = 0; 4789 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg, 4790 sizeof(lock_msg), 0); 4791 if (isc_retval > CTL_HA_STATUS_SUCCESS) { 4792 printf("%s: APS (lock=%d) error returned from " 4793 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval); 4794 mtx_unlock(&lun->lun_lock); 4795 mtx_unlock(&softc->ctl_lock); 4796 return (1); 4797 } 4798 } 4799 4800 mtx_unlock(&lun->lun_lock); 4801 mtx_unlock(&softc->ctl_lock); 4802 4803 return (0); 4804} 4805 4806void 4807ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4808{ 4809 struct ctl_lun *lun; 4810 struct ctl_softc *softc; 4811 int i; 4812 4813 softc = control_softc; 4814 4815 lun = (struct ctl_lun *)be_lun->ctl_lun; 4816 4817 mtx_lock(&lun->lun_lock); 4818 4819 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4820 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED; 4821 4822 mtx_unlock(&lun->lun_lock); 4823} 4824 4825/* 4826 * Backend "memory move is complete" callback for requests that never 4827 * make it down to say RAIDCore's configuration code. 4828 */ 4829int 4830ctl_config_move_done(union ctl_io *io) 4831{ 4832 int retval; 4833 4834 retval = CTL_RETVAL_COMPLETE; 4835 4836 4837 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4838 /* 4839 * XXX KDM this shouldn't happen, but what if it does? 4840 */ 4841 if (io->io_hdr.io_type != CTL_IO_SCSI) 4842 panic("I/O type isn't CTL_IO_SCSI!"); 4843 4844 if ((io->io_hdr.port_status == 0) 4845 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4846 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)) 4847 io->io_hdr.status = CTL_SUCCESS; 4848 else if ((io->io_hdr.port_status != 0) 4849 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0) 4850 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){ 4851 /* 4852 * For hardware error sense keys, the sense key 4853 * specific value is defined to be a retry count, 4854 * but we use it to pass back an internal FETD 4855 * error code. XXX KDM Hopefully the FETD is only 4856 * using 16 bits for an error code, since that's 4857 * all the space we have in the sks field. 4858 */ 4859 ctl_set_internal_failure(&io->scsiio, 4860 /*sks_valid*/ 1, 4861 /*retry_count*/ 4862 io->io_hdr.port_status); 4863 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4864 free(io->scsiio.kern_data_ptr, M_CTL); 4865 ctl_done(io); 4866 goto bailout; 4867 } 4868 4869 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 4870 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 4871 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4872 /* 4873 * XXX KDM just assuming a single pointer here, and not a 4874 * S/G list. If we start using S/G lists for config data, 4875 * we'll need to know how to clean them up here as well. 4876 */ 4877 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4878 free(io->scsiio.kern_data_ptr, M_CTL); 4879 /* Hopefully the user has already set the status... */ 4880 ctl_done(io); 4881 } else { 4882 /* 4883 * XXX KDM now we need to continue data movement. Some 4884 * options: 4885 * - call ctl_scsiio() again? We don't do this for data 4886 * writes, because for those at least we know ahead of 4887 * time where the write will go and how long it is. For 4888 * config writes, though, that information is largely 4889 * contained within the write itself, thus we need to 4890 * parse out the data again. 4891 * 4892 * - Call some other function once the data is in? 4893 */ 4894 4895 /* 4896 * XXX KDM call ctl_scsiio() again for now, and check flag 4897 * bits to see whether we're allocated or not. 4898 */ 4899 retval = ctl_scsiio(&io->scsiio); 4900 } 4901bailout: 4902 return (retval); 4903} 4904 4905/* 4906 * This gets called by a backend driver when it is done with a 4907 * data_submit method. 4908 */ 4909void 4910ctl_data_submit_done(union ctl_io *io) 4911{ 4912 /* 4913 * If the IO_CONT flag is set, we need to call the supplied 4914 * function to continue processing the I/O, instead of completing 4915 * the I/O just yet. 4916 * 4917 * If there is an error, though, we don't want to keep processing. 4918 * Instead, just send status back to the initiator. 4919 */ 4920 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4921 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4922 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4923 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4924 io->scsiio.io_cont(io); 4925 return; 4926 } 4927 ctl_done(io); 4928} 4929 4930/* 4931 * This gets called by a backend driver when it is done with a 4932 * configuration write. 4933 */ 4934void 4935ctl_config_write_done(union ctl_io *io) 4936{ 4937 /* 4938 * If the IO_CONT flag is set, we need to call the supplied 4939 * function to continue processing the I/O, instead of completing 4940 * the I/O just yet. 4941 * 4942 * If there is an error, though, we don't want to keep processing. 4943 * Instead, just send status back to the initiator. 4944 */ 4945 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) 4946 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE) 4947 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) { 4948 io->scsiio.io_cont(io); 4949 return; 4950 } 4951 /* 4952 * Since a configuration write can be done for commands that actually 4953 * have data allocated, like write buffer, and commands that have 4954 * no data, like start/stop unit, we need to check here. 4955 */ 4956 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 4957 free(io->scsiio.kern_data_ptr, M_CTL); 4958 ctl_done(io); 4959} 4960 4961/* 4962 * SCSI release command. 4963 */ 4964int 4965ctl_scsi_release(struct ctl_scsiio *ctsio) 4966{ 4967 int length, longid, thirdparty_id, resv_id; 4968 struct ctl_softc *ctl_softc; 4969 struct ctl_lun *lun; 4970 4971 length = 0; 4972 resv_id = 0; 4973 4974 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 4975 4976 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 4977 ctl_softc = control_softc; 4978 4979 switch (ctsio->cdb[0]) { 4980 case RELEASE_10: { 4981 struct scsi_release_10 *cdb; 4982 4983 cdb = (struct scsi_release_10 *)ctsio->cdb; 4984 4985 if (cdb->byte2 & SR10_LONGID) 4986 longid = 1; 4987 else 4988 thirdparty_id = cdb->thirdparty_id; 4989 4990 resv_id = cdb->resv_id; 4991 length = scsi_2btoul(cdb->length); 4992 break; 4993 } 4994 } 4995 4996 4997 /* 4998 * XXX KDM right now, we only support LUN reservation. We don't 4999 * support 3rd party reservations, or extent reservations, which 5000 * might actually need the parameter list. If we've gotten this 5001 * far, we've got a LUN reservation. Anything else got kicked out 5002 * above. So, according to SPC, ignore the length. 5003 */ 5004 length = 0; 5005 5006 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5007 && (length > 0)) { 5008 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5009 ctsio->kern_data_len = length; 5010 ctsio->kern_total_len = length; 5011 ctsio->kern_data_resid = 0; 5012 ctsio->kern_rel_offset = 0; 5013 ctsio->kern_sg_entries = 0; 5014 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5015 ctsio->be_move_done = ctl_config_move_done; 5016 ctl_datamove((union ctl_io *)ctsio); 5017 5018 return (CTL_RETVAL_COMPLETE); 5019 } 5020 5021 if (length > 0) 5022 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5023 5024 mtx_lock(&lun->lun_lock); 5025 5026 /* 5027 * According to SPC, it is not an error for an intiator to attempt 5028 * to release a reservation on a LUN that isn't reserved, or that 5029 * is reserved by another initiator. The reservation can only be 5030 * released, though, by the initiator who made it or by one of 5031 * several reset type events. 5032 */ 5033 if (lun->flags & CTL_LUN_RESERVED) { 5034 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id) 5035 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port) 5036 && (ctsio->io_hdr.nexus.targ_target.id == 5037 lun->rsv_nexus.targ_target.id)) { 5038 lun->flags &= ~CTL_LUN_RESERVED; 5039 } 5040 } 5041 5042 mtx_unlock(&lun->lun_lock); 5043 5044 ctsio->scsi_status = SCSI_STATUS_OK; 5045 ctsio->io_hdr.status = CTL_SUCCESS; 5046 5047 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5048 free(ctsio->kern_data_ptr, M_CTL); 5049 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5050 } 5051 5052 ctl_done((union ctl_io *)ctsio); 5053 return (CTL_RETVAL_COMPLETE); 5054} 5055 5056int 5057ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5058{ 5059 int extent, thirdparty, longid; 5060 int resv_id, length; 5061 uint64_t thirdparty_id; 5062 struct ctl_softc *ctl_softc; 5063 struct ctl_lun *lun; 5064 5065 extent = 0; 5066 thirdparty = 0; 5067 longid = 0; 5068 resv_id = 0; 5069 length = 0; 5070 thirdparty_id = 0; 5071 5072 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5073 5074 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5075 ctl_softc = control_softc; 5076 5077 switch (ctsio->cdb[0]) { 5078 case RESERVE_10: { 5079 struct scsi_reserve_10 *cdb; 5080 5081 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5082 5083 if (cdb->byte2 & SR10_LONGID) 5084 longid = 1; 5085 else 5086 thirdparty_id = cdb->thirdparty_id; 5087 5088 resv_id = cdb->resv_id; 5089 length = scsi_2btoul(cdb->length); 5090 break; 5091 } 5092 } 5093 5094 /* 5095 * XXX KDM right now, we only support LUN reservation. We don't 5096 * support 3rd party reservations, or extent reservations, which 5097 * might actually need the parameter list. If we've gotten this 5098 * far, we've got a LUN reservation. Anything else got kicked out 5099 * above. So, according to SPC, ignore the length. 5100 */ 5101 length = 0; 5102 5103 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5104 && (length > 0)) { 5105 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5106 ctsio->kern_data_len = length; 5107 ctsio->kern_total_len = length; 5108 ctsio->kern_data_resid = 0; 5109 ctsio->kern_rel_offset = 0; 5110 ctsio->kern_sg_entries = 0; 5111 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5112 ctsio->be_move_done = ctl_config_move_done; 5113 ctl_datamove((union ctl_io *)ctsio); 5114 5115 return (CTL_RETVAL_COMPLETE); 5116 } 5117 5118 if (length > 0) 5119 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5120 5121 mtx_lock(&lun->lun_lock); 5122 if (lun->flags & CTL_LUN_RESERVED) { 5123 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 5124 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 5125 || (ctsio->io_hdr.nexus.targ_target.id != 5126 lun->rsv_nexus.targ_target.id)) { 5127 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 5128 ctsio->io_hdr.status = CTL_SCSI_ERROR; 5129 goto bailout; 5130 } 5131 } 5132 5133 lun->flags |= CTL_LUN_RESERVED; 5134 lun->rsv_nexus = ctsio->io_hdr.nexus; 5135 5136 ctsio->scsi_status = SCSI_STATUS_OK; 5137 ctsio->io_hdr.status = CTL_SUCCESS; 5138 5139bailout: 5140 mtx_unlock(&lun->lun_lock); 5141 5142 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5143 free(ctsio->kern_data_ptr, M_CTL); 5144 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5145 } 5146 5147 ctl_done((union ctl_io *)ctsio); 5148 return (CTL_RETVAL_COMPLETE); 5149} 5150 5151int 5152ctl_start_stop(struct ctl_scsiio *ctsio) 5153{ 5154 struct scsi_start_stop_unit *cdb; 5155 struct ctl_lun *lun; 5156 struct ctl_softc *ctl_softc; 5157 int retval; 5158 5159 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5160 5161 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5162 ctl_softc = control_softc; 5163 retval = 0; 5164 5165 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5166 5167 /* 5168 * XXX KDM 5169 * We don't support the immediate bit on a stop unit. In order to 5170 * do that, we would need to code up a way to know that a stop is 5171 * pending, and hold off any new commands until it completes, one 5172 * way or another. Then we could accept or reject those commands 5173 * depending on its status. We would almost need to do the reverse 5174 * of what we do below for an immediate start -- return the copy of 5175 * the ctl_io to the FETD with status to send to the host (and to 5176 * free the copy!) and then free the original I/O once the stop 5177 * actually completes. That way, the OOA queue mechanism can work 5178 * to block commands that shouldn't proceed. Another alternative 5179 * would be to put the copy in the queue in place of the original, 5180 * and return the original back to the caller. That could be 5181 * slightly safer.. 5182 */ 5183 if ((cdb->byte2 & SSS_IMMED) 5184 && ((cdb->how & SSS_START) == 0)) { 5185 ctl_set_invalid_field(ctsio, 5186 /*sks_valid*/ 1, 5187 /*command*/ 1, 5188 /*field*/ 1, 5189 /*bit_valid*/ 1, 5190 /*bit*/ 0); 5191 ctl_done((union ctl_io *)ctsio); 5192 return (CTL_RETVAL_COMPLETE); 5193 } 5194 5195 if ((lun->flags & CTL_LUN_PR_RESERVED) 5196 && ((cdb->how & SSS_START)==0)) { 5197 uint32_t residx; 5198 5199 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5200 if (!lun->per_res[residx].registered 5201 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5202 5203 ctl_set_reservation_conflict(ctsio); 5204 ctl_done((union ctl_io *)ctsio); 5205 return (CTL_RETVAL_COMPLETE); 5206 } 5207 } 5208 5209 /* 5210 * If there is no backend on this device, we can't start or stop 5211 * it. In theory we shouldn't get any start/stop commands in the 5212 * first place at this level if the LUN doesn't have a backend. 5213 * That should get stopped by the command decode code. 5214 */ 5215 if (lun->backend == NULL) { 5216 ctl_set_invalid_opcode(ctsio); 5217 ctl_done((union ctl_io *)ctsio); 5218 return (CTL_RETVAL_COMPLETE); 5219 } 5220 5221 /* 5222 * XXX KDM Copan-specific offline behavior. 5223 * Figure out a reasonable way to port this? 5224 */ 5225#ifdef NEEDTOPORT 5226 mtx_lock(&lun->lun_lock); 5227 5228 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5229 && (lun->flags & CTL_LUN_OFFLINE)) { 5230 /* 5231 * If the LUN is offline, and the on/offline bit isn't set, 5232 * reject the start or stop. Otherwise, let it through. 5233 */ 5234 mtx_unlock(&lun->lun_lock); 5235 ctl_set_lun_not_ready(ctsio); 5236 ctl_done((union ctl_io *)ctsio); 5237 } else { 5238 mtx_unlock(&lun->lun_lock); 5239#endif /* NEEDTOPORT */ 5240 /* 5241 * This could be a start or a stop when we're online, 5242 * or a stop/offline or start/online. A start or stop when 5243 * we're offline is covered in the case above. 5244 */ 5245 /* 5246 * In the non-immediate case, we send the request to 5247 * the backend and return status to the user when 5248 * it is done. 5249 * 5250 * In the immediate case, we allocate a new ctl_io 5251 * to hold a copy of the request, and send that to 5252 * the backend. We then set good status on the 5253 * user's request and return it immediately. 5254 */ 5255 if (cdb->byte2 & SSS_IMMED) { 5256 union ctl_io *new_io; 5257 5258 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5259 if (new_io == NULL) { 5260 ctl_set_busy(ctsio); 5261 ctl_done((union ctl_io *)ctsio); 5262 } else { 5263 ctl_copy_io((union ctl_io *)ctsio, 5264 new_io); 5265 retval = lun->backend->config_write(new_io); 5266 ctl_set_success(ctsio); 5267 ctl_done((union ctl_io *)ctsio); 5268 } 5269 } else { 5270 retval = lun->backend->config_write( 5271 (union ctl_io *)ctsio); 5272 } 5273#ifdef NEEDTOPORT 5274 } 5275#endif 5276 return (retval); 5277} 5278 5279/* 5280 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5281 * we don't really do anything with the LBA and length fields if the user 5282 * passes them in. Instead we'll just flush out the cache for the entire 5283 * LUN. 5284 */ 5285int 5286ctl_sync_cache(struct ctl_scsiio *ctsio) 5287{ 5288 struct ctl_lun *lun; 5289 struct ctl_softc *ctl_softc; 5290 uint64_t starting_lba; 5291 uint32_t block_count; 5292 int retval; 5293 5294 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5295 5296 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5297 ctl_softc = control_softc; 5298 retval = 0; 5299 5300 switch (ctsio->cdb[0]) { 5301 case SYNCHRONIZE_CACHE: { 5302 struct scsi_sync_cache *cdb; 5303 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5304 5305 starting_lba = scsi_4btoul(cdb->begin_lba); 5306 block_count = scsi_2btoul(cdb->lb_count); 5307 break; 5308 } 5309 case SYNCHRONIZE_CACHE_16: { 5310 struct scsi_sync_cache_16 *cdb; 5311 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5312 5313 starting_lba = scsi_8btou64(cdb->begin_lba); 5314 block_count = scsi_4btoul(cdb->lb_count); 5315 break; 5316 } 5317 default: 5318 ctl_set_invalid_opcode(ctsio); 5319 ctl_done((union ctl_io *)ctsio); 5320 goto bailout; 5321 break; /* NOTREACHED */ 5322 } 5323 5324 /* 5325 * We check the LBA and length, but don't do anything with them. 5326 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5327 * get flushed. This check will just help satisfy anyone who wants 5328 * to see an error for an out of range LBA. 5329 */ 5330 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5331 ctl_set_lba_out_of_range(ctsio); 5332 ctl_done((union ctl_io *)ctsio); 5333 goto bailout; 5334 } 5335 5336 /* 5337 * If this LUN has no backend, we can't flush the cache anyway. 5338 */ 5339 if (lun->backend == NULL) { 5340 ctl_set_invalid_opcode(ctsio); 5341 ctl_done((union ctl_io *)ctsio); 5342 goto bailout; 5343 } 5344 5345 /* 5346 * Check to see whether we're configured to send the SYNCHRONIZE 5347 * CACHE command directly to the back end. 5348 */ 5349 mtx_lock(&lun->lun_lock); 5350 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5351 && (++(lun->sync_count) >= lun->sync_interval)) { 5352 lun->sync_count = 0; 5353 mtx_unlock(&lun->lun_lock); 5354 retval = lun->backend->config_write((union ctl_io *)ctsio); 5355 } else { 5356 mtx_unlock(&lun->lun_lock); 5357 ctl_set_success(ctsio); 5358 ctl_done((union ctl_io *)ctsio); 5359 } 5360 5361bailout: 5362 5363 return (retval); 5364} 5365 5366int 5367ctl_format(struct ctl_scsiio *ctsio) 5368{ 5369 struct scsi_format *cdb; 5370 struct ctl_lun *lun; 5371 struct ctl_softc *ctl_softc; 5372 int length, defect_list_len; 5373 5374 CTL_DEBUG_PRINT(("ctl_format\n")); 5375 5376 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5377 ctl_softc = control_softc; 5378 5379 cdb = (struct scsi_format *)ctsio->cdb; 5380 5381 length = 0; 5382 if (cdb->byte2 & SF_FMTDATA) { 5383 if (cdb->byte2 & SF_LONGLIST) 5384 length = sizeof(struct scsi_format_header_long); 5385 else 5386 length = sizeof(struct scsi_format_header_short); 5387 } 5388 5389 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5390 && (length > 0)) { 5391 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5392 ctsio->kern_data_len = length; 5393 ctsio->kern_total_len = length; 5394 ctsio->kern_data_resid = 0; 5395 ctsio->kern_rel_offset = 0; 5396 ctsio->kern_sg_entries = 0; 5397 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5398 ctsio->be_move_done = ctl_config_move_done; 5399 ctl_datamove((union ctl_io *)ctsio); 5400 5401 return (CTL_RETVAL_COMPLETE); 5402 } 5403 5404 defect_list_len = 0; 5405 5406 if (cdb->byte2 & SF_FMTDATA) { 5407 if (cdb->byte2 & SF_LONGLIST) { 5408 struct scsi_format_header_long *header; 5409 5410 header = (struct scsi_format_header_long *) 5411 ctsio->kern_data_ptr; 5412 5413 defect_list_len = scsi_4btoul(header->defect_list_len); 5414 if (defect_list_len != 0) { 5415 ctl_set_invalid_field(ctsio, 5416 /*sks_valid*/ 1, 5417 /*command*/ 0, 5418 /*field*/ 2, 5419 /*bit_valid*/ 0, 5420 /*bit*/ 0); 5421 goto bailout; 5422 } 5423 } else { 5424 struct scsi_format_header_short *header; 5425 5426 header = (struct scsi_format_header_short *) 5427 ctsio->kern_data_ptr; 5428 5429 defect_list_len = scsi_2btoul(header->defect_list_len); 5430 if (defect_list_len != 0) { 5431 ctl_set_invalid_field(ctsio, 5432 /*sks_valid*/ 1, 5433 /*command*/ 0, 5434 /*field*/ 2, 5435 /*bit_valid*/ 0, 5436 /*bit*/ 0); 5437 goto bailout; 5438 } 5439 } 5440 } 5441 5442 /* 5443 * The format command will clear out the "Medium format corrupted" 5444 * status if set by the configuration code. That status is really 5445 * just a way to notify the host that we have lost the media, and 5446 * get them to issue a command that will basically make them think 5447 * they're blowing away the media. 5448 */ 5449 mtx_lock(&lun->lun_lock); 5450 lun->flags &= ~CTL_LUN_INOPERABLE; 5451 mtx_unlock(&lun->lun_lock); 5452 5453 ctsio->scsi_status = SCSI_STATUS_OK; 5454 ctsio->io_hdr.status = CTL_SUCCESS; 5455bailout: 5456 5457 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5458 free(ctsio->kern_data_ptr, M_CTL); 5459 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5460 } 5461 5462 ctl_done((union ctl_io *)ctsio); 5463 return (CTL_RETVAL_COMPLETE); 5464} 5465 5466int 5467ctl_read_buffer(struct ctl_scsiio *ctsio) 5468{ 5469 struct scsi_read_buffer *cdb; 5470 struct ctl_lun *lun; 5471 int buffer_offset, len; 5472 static uint8_t descr[4]; 5473 static uint8_t echo_descr[4] = { 0 }; 5474 5475 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5476 5477 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5478 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5479 5480 if (lun->flags & CTL_LUN_PR_RESERVED) { 5481 uint32_t residx; 5482 5483 /* 5484 * XXX KDM need a lock here. 5485 */ 5486 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5487 if ((lun->res_type == SPR_TYPE_EX_AC 5488 && residx != lun->pr_res_idx) 5489 || ((lun->res_type == SPR_TYPE_EX_AC_RO 5490 || lun->res_type == SPR_TYPE_EX_AC_AR) 5491 && !lun->per_res[residx].registered)) { 5492 ctl_set_reservation_conflict(ctsio); 5493 ctl_done((union ctl_io *)ctsio); 5494 return (CTL_RETVAL_COMPLETE); 5495 } 5496 } 5497 5498 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5499 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5500 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5501 ctl_set_invalid_field(ctsio, 5502 /*sks_valid*/ 1, 5503 /*command*/ 1, 5504 /*field*/ 1, 5505 /*bit_valid*/ 1, 5506 /*bit*/ 4); 5507 ctl_done((union ctl_io *)ctsio); 5508 return (CTL_RETVAL_COMPLETE); 5509 } 5510 5511 len = scsi_3btoul(cdb->length); 5512 buffer_offset = scsi_3btoul(cdb->offset); 5513 5514 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5515 ctl_set_invalid_field(ctsio, 5516 /*sks_valid*/ 1, 5517 /*command*/ 1, 5518 /*field*/ 6, 5519 /*bit_valid*/ 0, 5520 /*bit*/ 0); 5521 ctl_done((union ctl_io *)ctsio); 5522 return (CTL_RETVAL_COMPLETE); 5523 } 5524 5525 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5526 descr[0] = 0; 5527 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]); 5528 ctsio->kern_data_ptr = descr; 5529 len = min(len, sizeof(descr)); 5530 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5531 ctsio->kern_data_ptr = echo_descr; 5532 len = min(len, sizeof(echo_descr)); 5533 } else 5534 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5535 ctsio->kern_data_len = len; 5536 ctsio->kern_total_len = len; 5537 ctsio->kern_data_resid = 0; 5538 ctsio->kern_rel_offset = 0; 5539 ctsio->kern_sg_entries = 0; 5540 ctsio->be_move_done = ctl_config_move_done; 5541 ctl_datamove((union ctl_io *)ctsio); 5542 5543 return (CTL_RETVAL_COMPLETE); 5544} 5545 5546int 5547ctl_write_buffer(struct ctl_scsiio *ctsio) 5548{ 5549 struct scsi_write_buffer *cdb; 5550 struct ctl_lun *lun; 5551 int buffer_offset, len; 5552 5553 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5554 5555 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5556 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5557 5558 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5559 ctl_set_invalid_field(ctsio, 5560 /*sks_valid*/ 1, 5561 /*command*/ 1, 5562 /*field*/ 1, 5563 /*bit_valid*/ 1, 5564 /*bit*/ 4); 5565 ctl_done((union ctl_io *)ctsio); 5566 return (CTL_RETVAL_COMPLETE); 5567 } 5568 5569 len = scsi_3btoul(cdb->length); 5570 buffer_offset = scsi_3btoul(cdb->offset); 5571 5572 if (buffer_offset + len > sizeof(lun->write_buffer)) { 5573 ctl_set_invalid_field(ctsio, 5574 /*sks_valid*/ 1, 5575 /*command*/ 1, 5576 /*field*/ 6, 5577 /*bit_valid*/ 0, 5578 /*bit*/ 0); 5579 ctl_done((union ctl_io *)ctsio); 5580 return (CTL_RETVAL_COMPLETE); 5581 } 5582 5583 /* 5584 * If we've got a kernel request that hasn't been malloced yet, 5585 * malloc it and tell the caller the data buffer is here. 5586 */ 5587 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5588 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5589 ctsio->kern_data_len = len; 5590 ctsio->kern_total_len = len; 5591 ctsio->kern_data_resid = 0; 5592 ctsio->kern_rel_offset = 0; 5593 ctsio->kern_sg_entries = 0; 5594 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5595 ctsio->be_move_done = ctl_config_move_done; 5596 ctl_datamove((union ctl_io *)ctsio); 5597 5598 return (CTL_RETVAL_COMPLETE); 5599 } 5600 5601 ctl_done((union ctl_io *)ctsio); 5602 5603 return (CTL_RETVAL_COMPLETE); 5604} 5605 5606int 5607ctl_write_same(struct ctl_scsiio *ctsio) 5608{ 5609 struct ctl_lun *lun; 5610 struct ctl_lba_len_flags *lbalen; 5611 uint64_t lba; 5612 uint32_t num_blocks; 5613 int len, retval; 5614 uint8_t byte2; 5615 5616 retval = CTL_RETVAL_COMPLETE; 5617 5618 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5619 5620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5621 5622 switch (ctsio->cdb[0]) { 5623 case WRITE_SAME_10: { 5624 struct scsi_write_same_10 *cdb; 5625 5626 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5627 5628 lba = scsi_4btoul(cdb->addr); 5629 num_blocks = scsi_2btoul(cdb->length); 5630 byte2 = cdb->byte2; 5631 break; 5632 } 5633 case WRITE_SAME_16: { 5634 struct scsi_write_same_16 *cdb; 5635 5636 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5637 5638 lba = scsi_8btou64(cdb->addr); 5639 num_blocks = scsi_4btoul(cdb->length); 5640 byte2 = cdb->byte2; 5641 break; 5642 } 5643 default: 5644 /* 5645 * We got a command we don't support. This shouldn't 5646 * happen, commands should be filtered out above us. 5647 */ 5648 ctl_set_invalid_opcode(ctsio); 5649 ctl_done((union ctl_io *)ctsio); 5650 5651 return (CTL_RETVAL_COMPLETE); 5652 break; /* NOTREACHED */ 5653 } 5654 5655 /* 5656 * The first check is to make sure we're in bounds, the second 5657 * check is to catch wrap-around problems. If the lba + num blocks 5658 * is less than the lba, then we've wrapped around and the block 5659 * range is invalid anyway. 5660 */ 5661 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5662 || ((lba + num_blocks) < lba)) { 5663 ctl_set_lba_out_of_range(ctsio); 5664 ctl_done((union ctl_io *)ctsio); 5665 return (CTL_RETVAL_COMPLETE); 5666 } 5667 5668 /* Zero number of blocks means "to the last logical block" */ 5669 if (num_blocks == 0) { 5670 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5671 ctl_set_invalid_field(ctsio, 5672 /*sks_valid*/ 0, 5673 /*command*/ 1, 5674 /*field*/ 0, 5675 /*bit_valid*/ 0, 5676 /*bit*/ 0); 5677 ctl_done((union ctl_io *)ctsio); 5678 return (CTL_RETVAL_COMPLETE); 5679 } 5680 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5681 } 5682 5683 len = lun->be_lun->blocksize; 5684 5685 /* 5686 * If we've got a kernel request that hasn't been malloced yet, 5687 * malloc it and tell the caller the data buffer is here. 5688 */ 5689 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5690 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5691 ctsio->kern_data_len = len; 5692 ctsio->kern_total_len = len; 5693 ctsio->kern_data_resid = 0; 5694 ctsio->kern_rel_offset = 0; 5695 ctsio->kern_sg_entries = 0; 5696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5697 ctsio->be_move_done = ctl_config_move_done; 5698 ctl_datamove((union ctl_io *)ctsio); 5699 5700 return (CTL_RETVAL_COMPLETE); 5701 } 5702 5703 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5704 lbalen->lba = lba; 5705 lbalen->len = num_blocks; 5706 lbalen->flags = byte2; 5707 retval = lun->backend->config_write((union ctl_io *)ctsio); 5708 5709 return (retval); 5710} 5711 5712int 5713ctl_unmap(struct ctl_scsiio *ctsio) 5714{ 5715 struct ctl_lun *lun; 5716 struct scsi_unmap *cdb; 5717 struct ctl_ptr_len_flags *ptrlen; 5718 struct scsi_unmap_header *hdr; 5719 struct scsi_unmap_desc *buf, *end; 5720 uint64_t lba; 5721 uint32_t num_blocks; 5722 int len, retval; 5723 uint8_t byte2; 5724 5725 retval = CTL_RETVAL_COMPLETE; 5726 5727 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5728 5729 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5730 cdb = (struct scsi_unmap *)ctsio->cdb; 5731 5732 len = scsi_2btoul(cdb->length); 5733 byte2 = cdb->byte2; 5734 5735 /* 5736 * If we've got a kernel request that hasn't been malloced yet, 5737 * malloc it and tell the caller the data buffer is here. 5738 */ 5739 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5740 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5741 ctsio->kern_data_len = len; 5742 ctsio->kern_total_len = len; 5743 ctsio->kern_data_resid = 0; 5744 ctsio->kern_rel_offset = 0; 5745 ctsio->kern_sg_entries = 0; 5746 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5747 ctsio->be_move_done = ctl_config_move_done; 5748 ctl_datamove((union ctl_io *)ctsio); 5749 5750 return (CTL_RETVAL_COMPLETE); 5751 } 5752 5753 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5754 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5755 if (len < sizeof (*hdr) || 5756 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5757 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5758 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5759 ctl_set_invalid_field(ctsio, 5760 /*sks_valid*/ 0, 5761 /*command*/ 0, 5762 /*field*/ 0, 5763 /*bit_valid*/ 0, 5764 /*bit*/ 0); 5765 ctl_done((union ctl_io *)ctsio); 5766 return (CTL_RETVAL_COMPLETE); 5767 } 5768 len = scsi_2btoul(hdr->desc_length); 5769 buf = (struct scsi_unmap_desc *)(hdr + 1); 5770 end = buf + len / sizeof(*buf); 5771 5772 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5773 ptrlen->ptr = (void *)buf; 5774 ptrlen->len = len; 5775 ptrlen->flags = byte2; 5776 5777 for (; buf < end; buf++) { 5778 lba = scsi_8btou64(buf->lba); 5779 num_blocks = scsi_4btoul(buf->length); 5780 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5781 || ((lba + num_blocks) < lba)) { 5782 ctl_set_lba_out_of_range(ctsio); 5783 ctl_done((union ctl_io *)ctsio); 5784 return (CTL_RETVAL_COMPLETE); 5785 } 5786 } 5787 5788 retval = lun->backend->config_write((union ctl_io *)ctsio); 5789 5790 return (retval); 5791} 5792 5793/* 5794 * Note that this function currently doesn't actually do anything inside 5795 * CTL to enforce things if the DQue bit is turned on. 5796 * 5797 * Also note that this function can't be used in the default case, because 5798 * the DQue bit isn't set in the changeable mask for the control mode page 5799 * anyway. This is just here as an example for how to implement a page 5800 * handler, and a placeholder in case we want to allow the user to turn 5801 * tagged queueing on and off. 5802 * 5803 * The D_SENSE bit handling is functional, however, and will turn 5804 * descriptor sense on and off for a given LUN. 5805 */ 5806int 5807ctl_control_page_handler(struct ctl_scsiio *ctsio, 5808 struct ctl_page_index *page_index, uint8_t *page_ptr) 5809{ 5810 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5811 struct ctl_lun *lun; 5812 struct ctl_softc *softc; 5813 int set_ua; 5814 uint32_t initidx; 5815 5816 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5817 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5818 set_ua = 0; 5819 5820 user_cp = (struct scsi_control_page *)page_ptr; 5821 current_cp = (struct scsi_control_page *) 5822 (page_index->page_data + (page_index->page_len * 5823 CTL_PAGE_CURRENT)); 5824 saved_cp = (struct scsi_control_page *) 5825 (page_index->page_data + (page_index->page_len * 5826 CTL_PAGE_SAVED)); 5827 5828 softc = control_softc; 5829 5830 mtx_lock(&lun->lun_lock); 5831 if (((current_cp->rlec & SCP_DSENSE) == 0) 5832 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5833 /* 5834 * Descriptor sense is currently turned off and the user 5835 * wants to turn it on. 5836 */ 5837 current_cp->rlec |= SCP_DSENSE; 5838 saved_cp->rlec |= SCP_DSENSE; 5839 lun->flags |= CTL_LUN_SENSE_DESC; 5840 set_ua = 1; 5841 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5842 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5843 /* 5844 * Descriptor sense is currently turned on, and the user 5845 * wants to turn it off. 5846 */ 5847 current_cp->rlec &= ~SCP_DSENSE; 5848 saved_cp->rlec &= ~SCP_DSENSE; 5849 lun->flags &= ~CTL_LUN_SENSE_DESC; 5850 set_ua = 1; 5851 } 5852 if (current_cp->queue_flags & SCP_QUEUE_DQUE) { 5853 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 5854#ifdef NEEDTOPORT 5855 csevent_log(CSC_CTL | CSC_SHELF_SW | 5856 CTL_UNTAG_TO_UNTAG, 5857 csevent_LogType_Trace, 5858 csevent_Severity_Information, 5859 csevent_AlertLevel_Green, 5860 csevent_FRU_Firmware, 5861 csevent_FRU_Unknown, 5862 "Received untagged to untagged transition"); 5863#endif /* NEEDTOPORT */ 5864 } else { 5865#ifdef NEEDTOPORT 5866 csevent_log(CSC_CTL | CSC_SHELF_SW | 5867 CTL_UNTAG_TO_TAG, 5868 csevent_LogType_ConfigChange, 5869 csevent_Severity_Information, 5870 csevent_AlertLevel_Green, 5871 csevent_FRU_Firmware, 5872 csevent_FRU_Unknown, 5873 "Received untagged to tagged " 5874 "queueing transition"); 5875#endif /* NEEDTOPORT */ 5876 5877 current_cp->queue_flags &= ~SCP_QUEUE_DQUE; 5878 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE; 5879 set_ua = 1; 5880 } 5881 } else { 5882 if (user_cp->queue_flags & SCP_QUEUE_DQUE) { 5883#ifdef NEEDTOPORT 5884 csevent_log(CSC_CTL | CSC_SHELF_SW | 5885 CTL_TAG_TO_UNTAG, 5886 csevent_LogType_ConfigChange, 5887 csevent_Severity_Warning, 5888 csevent_AlertLevel_Yellow, 5889 csevent_FRU_Firmware, 5890 csevent_FRU_Unknown, 5891 "Received tagged queueing to untagged " 5892 "transition"); 5893#endif /* NEEDTOPORT */ 5894 5895 current_cp->queue_flags |= SCP_QUEUE_DQUE; 5896 saved_cp->queue_flags |= SCP_QUEUE_DQUE; 5897 set_ua = 1; 5898 } else { 5899#ifdef NEEDTOPORT 5900 csevent_log(CSC_CTL | CSC_SHELF_SW | 5901 CTL_TAG_TO_TAG, 5902 csevent_LogType_Trace, 5903 csevent_Severity_Information, 5904 csevent_AlertLevel_Green, 5905 csevent_FRU_Firmware, 5906 csevent_FRU_Unknown, 5907 "Received tagged queueing to tagged " 5908 "queueing transition"); 5909#endif /* NEEDTOPORT */ 5910 } 5911 } 5912 if (set_ua != 0) { 5913 int i; 5914 /* 5915 * Let other initiators know that the mode 5916 * parameters for this LUN have changed. 5917 */ 5918 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 5919 if (i == initidx) 5920 continue; 5921 5922 lun->pending_sense[i].ua_pending |= 5923 CTL_UA_MODE_CHANGE; 5924 } 5925 } 5926 mtx_unlock(&lun->lun_lock); 5927 5928 return (0); 5929} 5930 5931int 5932ctl_power_sp_handler(struct ctl_scsiio *ctsio, 5933 struct ctl_page_index *page_index, uint8_t *page_ptr) 5934{ 5935 return (0); 5936} 5937 5938int 5939ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio, 5940 struct ctl_page_index *page_index, int pc) 5941{ 5942 struct copan_power_subpage *page; 5943 5944 page = (struct copan_power_subpage *)page_index->page_data + 5945 (page_index->page_len * pc); 5946 5947 switch (pc) { 5948 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5949 /* 5950 * We don't update the changable bits for this page. 5951 */ 5952 break; 5953 case SMS_PAGE_CTRL_CURRENT >> 6: 5954 case SMS_PAGE_CTRL_DEFAULT >> 6: 5955 case SMS_PAGE_CTRL_SAVED >> 6: 5956#ifdef NEEDTOPORT 5957 ctl_update_power_subpage(page); 5958#endif 5959 break; 5960 default: 5961#ifdef NEEDTOPORT 5962 EPRINT(0, "Invalid PC %d!!", pc); 5963#endif 5964 break; 5965 } 5966 return (0); 5967} 5968 5969 5970int 5971ctl_aps_sp_handler(struct ctl_scsiio *ctsio, 5972 struct ctl_page_index *page_index, uint8_t *page_ptr) 5973{ 5974 struct copan_aps_subpage *user_sp; 5975 struct copan_aps_subpage *current_sp; 5976 union ctl_modepage_info *modepage_info; 5977 struct ctl_softc *softc; 5978 struct ctl_lun *lun; 5979 int retval; 5980 5981 retval = CTL_RETVAL_COMPLETE; 5982 current_sp = (struct copan_aps_subpage *)(page_index->page_data + 5983 (page_index->page_len * CTL_PAGE_CURRENT)); 5984 softc = control_softc; 5985 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5986 5987 user_sp = (struct copan_aps_subpage *)page_ptr; 5988 5989 modepage_info = (union ctl_modepage_info *) 5990 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5991 5992 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK; 5993 modepage_info->header.subpage = page_index->subpage; 5994 modepage_info->aps.lock_active = user_sp->lock_active; 5995 5996 mtx_lock(&softc->ctl_lock); 5997 5998 /* 5999 * If there is a request to lock the LUN and another LUN is locked 6000 * this is an error. If the requested LUN is already locked ignore 6001 * the request. If no LUN is locked attempt to lock it. 6002 * if there is a request to unlock the LUN and the LUN is currently 6003 * locked attempt to unlock it. Otherwise ignore the request. i.e. 6004 * if another LUN is locked or no LUN is locked. 6005 */ 6006 if (user_sp->lock_active & APS_LOCK_ACTIVE) { 6007 if (softc->aps_locked_lun == lun->lun) { 6008 /* 6009 * This LUN is already locked, so we're done. 6010 */ 6011 retval = CTL_RETVAL_COMPLETE; 6012 } else if (softc->aps_locked_lun == 0) { 6013 /* 6014 * No one has the lock, pass the request to the 6015 * backend. 6016 */ 6017 retval = lun->backend->config_write( 6018 (union ctl_io *)ctsio); 6019 } else { 6020 /* 6021 * Someone else has the lock, throw out the request. 6022 */ 6023 ctl_set_already_locked(ctsio); 6024 free(ctsio->kern_data_ptr, M_CTL); 6025 ctl_done((union ctl_io *)ctsio); 6026 6027 /* 6028 * Set the return value so that ctl_do_mode_select() 6029 * won't try to complete the command. We already 6030 * completed it here. 6031 */ 6032 retval = CTL_RETVAL_ERROR; 6033 } 6034 } else if (softc->aps_locked_lun == lun->lun) { 6035 /* 6036 * This LUN is locked, so pass the unlock request to the 6037 * backend. 6038 */ 6039 retval = lun->backend->config_write((union ctl_io *)ctsio); 6040 } 6041 mtx_unlock(&softc->ctl_lock); 6042 6043 return (retval); 6044} 6045 6046int 6047ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6048 struct ctl_page_index *page_index, 6049 uint8_t *page_ptr) 6050{ 6051 uint8_t *c; 6052 int i; 6053 6054 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6055 ctl_time_io_secs = 6056 (c[0] << 8) | 6057 (c[1] << 0) | 6058 0; 6059 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6060 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6061 printf("page data:"); 6062 for (i=0; i<8; i++) 6063 printf(" %.2x",page_ptr[i]); 6064 printf("\n"); 6065 return (0); 6066} 6067 6068int 6069ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6070 struct ctl_page_index *page_index, 6071 int pc) 6072{ 6073 struct copan_debugconf_subpage *page; 6074 6075 page = (struct copan_debugconf_subpage *)page_index->page_data + 6076 (page_index->page_len * pc); 6077 6078 switch (pc) { 6079 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6080 case SMS_PAGE_CTRL_DEFAULT >> 6: 6081 case SMS_PAGE_CTRL_SAVED >> 6: 6082 /* 6083 * We don't update the changable or default bits for this page. 6084 */ 6085 break; 6086 case SMS_PAGE_CTRL_CURRENT >> 6: 6087 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6088 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6089 break; 6090 default: 6091#ifdef NEEDTOPORT 6092 EPRINT(0, "Invalid PC %d!!", pc); 6093#endif /* NEEDTOPORT */ 6094 break; 6095 } 6096 return (0); 6097} 6098 6099 6100static int 6101ctl_do_mode_select(union ctl_io *io) 6102{ 6103 struct scsi_mode_page_header *page_header; 6104 struct ctl_page_index *page_index; 6105 struct ctl_scsiio *ctsio; 6106 int control_dev, page_len; 6107 int page_len_offset, page_len_size; 6108 union ctl_modepage_info *modepage_info; 6109 struct ctl_lun *lun; 6110 int *len_left, *len_used; 6111 int retval, i; 6112 6113 ctsio = &io->scsiio; 6114 page_index = NULL; 6115 page_len = 0; 6116 retval = CTL_RETVAL_COMPLETE; 6117 6118 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6119 6120 if (lun->be_lun->lun_type != T_DIRECT) 6121 control_dev = 1; 6122 else 6123 control_dev = 0; 6124 6125 modepage_info = (union ctl_modepage_info *) 6126 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6127 len_left = &modepage_info->header.len_left; 6128 len_used = &modepage_info->header.len_used; 6129 6130do_next_page: 6131 6132 page_header = (struct scsi_mode_page_header *) 6133 (ctsio->kern_data_ptr + *len_used); 6134 6135 if (*len_left == 0) { 6136 free(ctsio->kern_data_ptr, M_CTL); 6137 ctl_set_success(ctsio); 6138 ctl_done((union ctl_io *)ctsio); 6139 return (CTL_RETVAL_COMPLETE); 6140 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6141 6142 free(ctsio->kern_data_ptr, M_CTL); 6143 ctl_set_param_len_error(ctsio); 6144 ctl_done((union ctl_io *)ctsio); 6145 return (CTL_RETVAL_COMPLETE); 6146 6147 } else if ((page_header->page_code & SMPH_SPF) 6148 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6149 6150 free(ctsio->kern_data_ptr, M_CTL); 6151 ctl_set_param_len_error(ctsio); 6152 ctl_done((union ctl_io *)ctsio); 6153 return (CTL_RETVAL_COMPLETE); 6154 } 6155 6156 6157 /* 6158 * XXX KDM should we do something with the block descriptor? 6159 */ 6160 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6161 6162 if ((control_dev != 0) 6163 && (lun->mode_pages.index[i].page_flags & 6164 CTL_PAGE_FLAG_DISK_ONLY)) 6165 continue; 6166 6167 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6168 (page_header->page_code & SMPH_PC_MASK)) 6169 continue; 6170 6171 /* 6172 * If neither page has a subpage code, then we've got a 6173 * match. 6174 */ 6175 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6176 && ((page_header->page_code & SMPH_SPF) == 0)) { 6177 page_index = &lun->mode_pages.index[i]; 6178 page_len = page_header->page_length; 6179 break; 6180 } 6181 6182 /* 6183 * If both pages have subpages, then the subpage numbers 6184 * have to match. 6185 */ 6186 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6187 && (page_header->page_code & SMPH_SPF)) { 6188 struct scsi_mode_page_header_sp *sph; 6189 6190 sph = (struct scsi_mode_page_header_sp *)page_header; 6191 6192 if (lun->mode_pages.index[i].subpage == 6193 sph->subpage) { 6194 page_index = &lun->mode_pages.index[i]; 6195 page_len = scsi_2btoul(sph->page_length); 6196 break; 6197 } 6198 } 6199 } 6200 6201 /* 6202 * If we couldn't find the page, or if we don't have a mode select 6203 * handler for it, send back an error to the user. 6204 */ 6205 if ((page_index == NULL) 6206 || (page_index->select_handler == NULL)) { 6207 ctl_set_invalid_field(ctsio, 6208 /*sks_valid*/ 1, 6209 /*command*/ 0, 6210 /*field*/ *len_used, 6211 /*bit_valid*/ 0, 6212 /*bit*/ 0); 6213 free(ctsio->kern_data_ptr, M_CTL); 6214 ctl_done((union ctl_io *)ctsio); 6215 return (CTL_RETVAL_COMPLETE); 6216 } 6217 6218 if (page_index->page_code & SMPH_SPF) { 6219 page_len_offset = 2; 6220 page_len_size = 2; 6221 } else { 6222 page_len_size = 1; 6223 page_len_offset = 1; 6224 } 6225 6226 /* 6227 * If the length the initiator gives us isn't the one we specify in 6228 * the mode page header, or if they didn't specify enough data in 6229 * the CDB to avoid truncating this page, kick out the request. 6230 */ 6231 if ((page_len != (page_index->page_len - page_len_offset - 6232 page_len_size)) 6233 || (*len_left < page_index->page_len)) { 6234 6235 6236 ctl_set_invalid_field(ctsio, 6237 /*sks_valid*/ 1, 6238 /*command*/ 0, 6239 /*field*/ *len_used + page_len_offset, 6240 /*bit_valid*/ 0, 6241 /*bit*/ 0); 6242 free(ctsio->kern_data_ptr, M_CTL); 6243 ctl_done((union ctl_io *)ctsio); 6244 return (CTL_RETVAL_COMPLETE); 6245 } 6246 6247 /* 6248 * Run through the mode page, checking to make sure that the bits 6249 * the user changed are actually legal for him to change. 6250 */ 6251 for (i = 0; i < page_index->page_len; i++) { 6252 uint8_t *user_byte, *change_mask, *current_byte; 6253 int bad_bit; 6254 int j; 6255 6256 user_byte = (uint8_t *)page_header + i; 6257 change_mask = page_index->page_data + 6258 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6259 current_byte = page_index->page_data + 6260 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6261 6262 /* 6263 * Check to see whether the user set any bits in this byte 6264 * that he is not allowed to set. 6265 */ 6266 if ((*user_byte & ~(*change_mask)) == 6267 (*current_byte & ~(*change_mask))) 6268 continue; 6269 6270 /* 6271 * Go through bit by bit to determine which one is illegal. 6272 */ 6273 bad_bit = 0; 6274 for (j = 7; j >= 0; j--) { 6275 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6276 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6277 bad_bit = i; 6278 break; 6279 } 6280 } 6281 ctl_set_invalid_field(ctsio, 6282 /*sks_valid*/ 1, 6283 /*command*/ 0, 6284 /*field*/ *len_used + i, 6285 /*bit_valid*/ 1, 6286 /*bit*/ bad_bit); 6287 free(ctsio->kern_data_ptr, M_CTL); 6288 ctl_done((union ctl_io *)ctsio); 6289 return (CTL_RETVAL_COMPLETE); 6290 } 6291 6292 /* 6293 * Decrement these before we call the page handler, since we may 6294 * end up getting called back one way or another before the handler 6295 * returns to this context. 6296 */ 6297 *len_left -= page_index->page_len; 6298 *len_used += page_index->page_len; 6299 6300 retval = page_index->select_handler(ctsio, page_index, 6301 (uint8_t *)page_header); 6302 6303 /* 6304 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6305 * wait until this queued command completes to finish processing 6306 * the mode page. If it returns anything other than 6307 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6308 * already set the sense information, freed the data pointer, and 6309 * completed the io for us. 6310 */ 6311 if (retval != CTL_RETVAL_COMPLETE) 6312 goto bailout_no_done; 6313 6314 /* 6315 * If the initiator sent us more than one page, parse the next one. 6316 */ 6317 if (*len_left > 0) 6318 goto do_next_page; 6319 6320 ctl_set_success(ctsio); 6321 free(ctsio->kern_data_ptr, M_CTL); 6322 ctl_done((union ctl_io *)ctsio); 6323 6324bailout_no_done: 6325 6326 return (CTL_RETVAL_COMPLETE); 6327 6328} 6329 6330int 6331ctl_mode_select(struct ctl_scsiio *ctsio) 6332{ 6333 int param_len, pf, sp; 6334 int header_size, bd_len; 6335 int len_left, len_used; 6336 struct ctl_page_index *page_index; 6337 struct ctl_lun *lun; 6338 int control_dev, page_len; 6339 union ctl_modepage_info *modepage_info; 6340 int retval; 6341 6342 pf = 0; 6343 sp = 0; 6344 page_len = 0; 6345 len_used = 0; 6346 len_left = 0; 6347 retval = 0; 6348 bd_len = 0; 6349 page_index = NULL; 6350 6351 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6352 6353 if (lun->be_lun->lun_type != T_DIRECT) 6354 control_dev = 1; 6355 else 6356 control_dev = 0; 6357 6358 switch (ctsio->cdb[0]) { 6359 case MODE_SELECT_6: { 6360 struct scsi_mode_select_6 *cdb; 6361 6362 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6363 6364 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6365 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6366 6367 param_len = cdb->length; 6368 header_size = sizeof(struct scsi_mode_header_6); 6369 break; 6370 } 6371 case MODE_SELECT_10: { 6372 struct scsi_mode_select_10 *cdb; 6373 6374 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6375 6376 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6377 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6378 6379 param_len = scsi_2btoul(cdb->length); 6380 header_size = sizeof(struct scsi_mode_header_10); 6381 break; 6382 } 6383 default: 6384 ctl_set_invalid_opcode(ctsio); 6385 ctl_done((union ctl_io *)ctsio); 6386 return (CTL_RETVAL_COMPLETE); 6387 break; /* NOTREACHED */ 6388 } 6389 6390 /* 6391 * From SPC-3: 6392 * "A parameter list length of zero indicates that the Data-Out Buffer 6393 * shall be empty. This condition shall not be considered as an error." 6394 */ 6395 if (param_len == 0) { 6396 ctl_set_success(ctsio); 6397 ctl_done((union ctl_io *)ctsio); 6398 return (CTL_RETVAL_COMPLETE); 6399 } 6400 6401 /* 6402 * Since we'll hit this the first time through, prior to 6403 * allocation, we don't need to free a data buffer here. 6404 */ 6405 if (param_len < header_size) { 6406 ctl_set_param_len_error(ctsio); 6407 ctl_done((union ctl_io *)ctsio); 6408 return (CTL_RETVAL_COMPLETE); 6409 } 6410 6411 /* 6412 * Allocate the data buffer and grab the user's data. In theory, 6413 * we shouldn't have to sanity check the parameter list length here 6414 * because the maximum size is 64K. We should be able to malloc 6415 * that much without too many problems. 6416 */ 6417 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6418 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6419 ctsio->kern_data_len = param_len; 6420 ctsio->kern_total_len = param_len; 6421 ctsio->kern_data_resid = 0; 6422 ctsio->kern_rel_offset = 0; 6423 ctsio->kern_sg_entries = 0; 6424 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6425 ctsio->be_move_done = ctl_config_move_done; 6426 ctl_datamove((union ctl_io *)ctsio); 6427 6428 return (CTL_RETVAL_COMPLETE); 6429 } 6430 6431 switch (ctsio->cdb[0]) { 6432 case MODE_SELECT_6: { 6433 struct scsi_mode_header_6 *mh6; 6434 6435 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6436 bd_len = mh6->blk_desc_len; 6437 break; 6438 } 6439 case MODE_SELECT_10: { 6440 struct scsi_mode_header_10 *mh10; 6441 6442 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6443 bd_len = scsi_2btoul(mh10->blk_desc_len); 6444 break; 6445 } 6446 default: 6447 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6448 break; 6449 } 6450 6451 if (param_len < (header_size + bd_len)) { 6452 free(ctsio->kern_data_ptr, M_CTL); 6453 ctl_set_param_len_error(ctsio); 6454 ctl_done((union ctl_io *)ctsio); 6455 return (CTL_RETVAL_COMPLETE); 6456 } 6457 6458 /* 6459 * Set the IO_CONT flag, so that if this I/O gets passed to 6460 * ctl_config_write_done(), it'll get passed back to 6461 * ctl_do_mode_select() for further processing, or completion if 6462 * we're all done. 6463 */ 6464 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6465 ctsio->io_cont = ctl_do_mode_select; 6466 6467 modepage_info = (union ctl_modepage_info *) 6468 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6469 6470 memset(modepage_info, 0, sizeof(*modepage_info)); 6471 6472 len_left = param_len - header_size - bd_len; 6473 len_used = header_size + bd_len; 6474 6475 modepage_info->header.len_left = len_left; 6476 modepage_info->header.len_used = len_used; 6477 6478 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6479} 6480 6481int 6482ctl_mode_sense(struct ctl_scsiio *ctsio) 6483{ 6484 struct ctl_lun *lun; 6485 int pc, page_code, dbd, llba, subpage; 6486 int alloc_len, page_len, header_len, total_len; 6487 struct scsi_mode_block_descr *block_desc; 6488 struct ctl_page_index *page_index; 6489 int control_dev; 6490 6491 dbd = 0; 6492 llba = 0; 6493 block_desc = NULL; 6494 page_index = NULL; 6495 6496 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6497 6498 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6499 6500 if (lun->be_lun->lun_type != T_DIRECT) 6501 control_dev = 1; 6502 else 6503 control_dev = 0; 6504 6505 if (lun->flags & CTL_LUN_PR_RESERVED) { 6506 uint32_t residx; 6507 6508 /* 6509 * XXX KDM need a lock here. 6510 */ 6511 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 6512 if ((lun->res_type == SPR_TYPE_EX_AC 6513 && residx != lun->pr_res_idx) 6514 || ((lun->res_type == SPR_TYPE_EX_AC_RO 6515 || lun->res_type == SPR_TYPE_EX_AC_AR) 6516 && !lun->per_res[residx].registered)) { 6517 ctl_set_reservation_conflict(ctsio); 6518 ctl_done((union ctl_io *)ctsio); 6519 return (CTL_RETVAL_COMPLETE); 6520 } 6521 } 6522 6523 switch (ctsio->cdb[0]) { 6524 case MODE_SENSE_6: { 6525 struct scsi_mode_sense_6 *cdb; 6526 6527 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6528 6529 header_len = sizeof(struct scsi_mode_hdr_6); 6530 if (cdb->byte2 & SMS_DBD) 6531 dbd = 1; 6532 else 6533 header_len += sizeof(struct scsi_mode_block_descr); 6534 6535 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6536 page_code = cdb->page & SMS_PAGE_CODE; 6537 subpage = cdb->subpage; 6538 alloc_len = cdb->length; 6539 break; 6540 } 6541 case MODE_SENSE_10: { 6542 struct scsi_mode_sense_10 *cdb; 6543 6544 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6545 6546 header_len = sizeof(struct scsi_mode_hdr_10); 6547 6548 if (cdb->byte2 & SMS_DBD) 6549 dbd = 1; 6550 else 6551 header_len += sizeof(struct scsi_mode_block_descr); 6552 if (cdb->byte2 & SMS10_LLBAA) 6553 llba = 1; 6554 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6555 page_code = cdb->page & SMS_PAGE_CODE; 6556 subpage = cdb->subpage; 6557 alloc_len = scsi_2btoul(cdb->length); 6558 break; 6559 } 6560 default: 6561 ctl_set_invalid_opcode(ctsio); 6562 ctl_done((union ctl_io *)ctsio); 6563 return (CTL_RETVAL_COMPLETE); 6564 break; /* NOTREACHED */ 6565 } 6566 6567 /* 6568 * We have to make a first pass through to calculate the size of 6569 * the pages that match the user's query. Then we allocate enough 6570 * memory to hold it, and actually copy the data into the buffer. 6571 */ 6572 switch (page_code) { 6573 case SMS_ALL_PAGES_PAGE: { 6574 int i; 6575 6576 page_len = 0; 6577 6578 /* 6579 * At the moment, values other than 0 and 0xff here are 6580 * reserved according to SPC-3. 6581 */ 6582 if ((subpage != SMS_SUBPAGE_PAGE_0) 6583 && (subpage != SMS_SUBPAGE_ALL)) { 6584 ctl_set_invalid_field(ctsio, 6585 /*sks_valid*/ 1, 6586 /*command*/ 1, 6587 /*field*/ 3, 6588 /*bit_valid*/ 0, 6589 /*bit*/ 0); 6590 ctl_done((union ctl_io *)ctsio); 6591 return (CTL_RETVAL_COMPLETE); 6592 } 6593 6594 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6595 if ((control_dev != 0) 6596 && (lun->mode_pages.index[i].page_flags & 6597 CTL_PAGE_FLAG_DISK_ONLY)) 6598 continue; 6599 6600 /* 6601 * We don't use this subpage if the user didn't 6602 * request all subpages. 6603 */ 6604 if ((lun->mode_pages.index[i].subpage != 0) 6605 && (subpage == SMS_SUBPAGE_PAGE_0)) 6606 continue; 6607 6608#if 0 6609 printf("found page %#x len %d\n", 6610 lun->mode_pages.index[i].page_code & 6611 SMPH_PC_MASK, 6612 lun->mode_pages.index[i].page_len); 6613#endif 6614 page_len += lun->mode_pages.index[i].page_len; 6615 } 6616 break; 6617 } 6618 default: { 6619 int i; 6620 6621 page_len = 0; 6622 6623 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6624 /* Look for the right page code */ 6625 if ((lun->mode_pages.index[i].page_code & 6626 SMPH_PC_MASK) != page_code) 6627 continue; 6628 6629 /* Look for the right subpage or the subpage wildcard*/ 6630 if ((lun->mode_pages.index[i].subpage != subpage) 6631 && (subpage != SMS_SUBPAGE_ALL)) 6632 continue; 6633 6634 /* Make sure the page is supported for this dev type */ 6635 if ((control_dev != 0) 6636 && (lun->mode_pages.index[i].page_flags & 6637 CTL_PAGE_FLAG_DISK_ONLY)) 6638 continue; 6639 6640#if 0 6641 printf("found page %#x len %d\n", 6642 lun->mode_pages.index[i].page_code & 6643 SMPH_PC_MASK, 6644 lun->mode_pages.index[i].page_len); 6645#endif 6646 6647 page_len += lun->mode_pages.index[i].page_len; 6648 } 6649 6650 if (page_len == 0) { 6651 ctl_set_invalid_field(ctsio, 6652 /*sks_valid*/ 1, 6653 /*command*/ 1, 6654 /*field*/ 2, 6655 /*bit_valid*/ 1, 6656 /*bit*/ 5); 6657 ctl_done((union ctl_io *)ctsio); 6658 return (CTL_RETVAL_COMPLETE); 6659 } 6660 break; 6661 } 6662 } 6663 6664 total_len = header_len + page_len; 6665#if 0 6666 printf("header_len = %d, page_len = %d, total_len = %d\n", 6667 header_len, page_len, total_len); 6668#endif 6669 6670 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6671 ctsio->kern_sg_entries = 0; 6672 ctsio->kern_data_resid = 0; 6673 ctsio->kern_rel_offset = 0; 6674 if (total_len < alloc_len) { 6675 ctsio->residual = alloc_len - total_len; 6676 ctsio->kern_data_len = total_len; 6677 ctsio->kern_total_len = total_len; 6678 } else { 6679 ctsio->residual = 0; 6680 ctsio->kern_data_len = alloc_len; 6681 ctsio->kern_total_len = alloc_len; 6682 } 6683 6684 switch (ctsio->cdb[0]) { 6685 case MODE_SENSE_6: { 6686 struct scsi_mode_hdr_6 *header; 6687 6688 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6689 6690 header->datalen = ctl_min(total_len - 1, 254); 6691 6692 if (dbd) 6693 header->block_descr_len = 0; 6694 else 6695 header->block_descr_len = 6696 sizeof(struct scsi_mode_block_descr); 6697 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6698 break; 6699 } 6700 case MODE_SENSE_10: { 6701 struct scsi_mode_hdr_10 *header; 6702 int datalen; 6703 6704 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6705 6706 datalen = ctl_min(total_len - 2, 65533); 6707 scsi_ulto2b(datalen, header->datalen); 6708 if (dbd) 6709 scsi_ulto2b(0, header->block_descr_len); 6710 else 6711 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6712 header->block_descr_len); 6713 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6714 break; 6715 } 6716 default: 6717 panic("invalid CDB type %#x", ctsio->cdb[0]); 6718 break; /* NOTREACHED */ 6719 } 6720 6721 /* 6722 * If we've got a disk, use its blocksize in the block 6723 * descriptor. Otherwise, just set it to 0. 6724 */ 6725 if (dbd == 0) { 6726 if (control_dev != 0) 6727 scsi_ulto3b(lun->be_lun->blocksize, 6728 block_desc->block_len); 6729 else 6730 scsi_ulto3b(0, block_desc->block_len); 6731 } 6732 6733 switch (page_code) { 6734 case SMS_ALL_PAGES_PAGE: { 6735 int i, data_used; 6736 6737 data_used = header_len; 6738 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6739 struct ctl_page_index *page_index; 6740 6741 page_index = &lun->mode_pages.index[i]; 6742 6743 if ((control_dev != 0) 6744 && (page_index->page_flags & 6745 CTL_PAGE_FLAG_DISK_ONLY)) 6746 continue; 6747 6748 /* 6749 * We don't use this subpage if the user didn't 6750 * request all subpages. We already checked (above) 6751 * to make sure the user only specified a subpage 6752 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6753 */ 6754 if ((page_index->subpage != 0) 6755 && (subpage == SMS_SUBPAGE_PAGE_0)) 6756 continue; 6757 6758 /* 6759 * Call the handler, if it exists, to update the 6760 * page to the latest values. 6761 */ 6762 if (page_index->sense_handler != NULL) 6763 page_index->sense_handler(ctsio, page_index,pc); 6764 6765 memcpy(ctsio->kern_data_ptr + data_used, 6766 page_index->page_data + 6767 (page_index->page_len * pc), 6768 page_index->page_len); 6769 data_used += page_index->page_len; 6770 } 6771 break; 6772 } 6773 default: { 6774 int i, data_used; 6775 6776 data_used = header_len; 6777 6778 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6779 struct ctl_page_index *page_index; 6780 6781 page_index = &lun->mode_pages.index[i]; 6782 6783 /* Look for the right page code */ 6784 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6785 continue; 6786 6787 /* Look for the right subpage or the subpage wildcard*/ 6788 if ((page_index->subpage != subpage) 6789 && (subpage != SMS_SUBPAGE_ALL)) 6790 continue; 6791 6792 /* Make sure the page is supported for this dev type */ 6793 if ((control_dev != 0) 6794 && (page_index->page_flags & 6795 CTL_PAGE_FLAG_DISK_ONLY)) 6796 continue; 6797 6798 /* 6799 * Call the handler, if it exists, to update the 6800 * page to the latest values. 6801 */ 6802 if (page_index->sense_handler != NULL) 6803 page_index->sense_handler(ctsio, page_index,pc); 6804 6805 memcpy(ctsio->kern_data_ptr + data_used, 6806 page_index->page_data + 6807 (page_index->page_len * pc), 6808 page_index->page_len); 6809 data_used += page_index->page_len; 6810 } 6811 break; 6812 } 6813 } 6814 6815 ctsio->scsi_status = SCSI_STATUS_OK; 6816 6817 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6818 ctsio->be_move_done = ctl_config_move_done; 6819 ctl_datamove((union ctl_io *)ctsio); 6820 6821 return (CTL_RETVAL_COMPLETE); 6822} 6823 6824int 6825ctl_read_capacity(struct ctl_scsiio *ctsio) 6826{ 6827 struct scsi_read_capacity *cdb; 6828 struct scsi_read_capacity_data *data; 6829 struct ctl_lun *lun; 6830 uint32_t lba; 6831 6832 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6833 6834 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6835 6836 lba = scsi_4btoul(cdb->addr); 6837 if (((cdb->pmi & SRC_PMI) == 0) 6838 && (lba != 0)) { 6839 ctl_set_invalid_field(/*ctsio*/ ctsio, 6840 /*sks_valid*/ 1, 6841 /*command*/ 1, 6842 /*field*/ 2, 6843 /*bit_valid*/ 0, 6844 /*bit*/ 0); 6845 ctl_done((union ctl_io *)ctsio); 6846 return (CTL_RETVAL_COMPLETE); 6847 } 6848 6849 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6850 6851 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6852 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6853 ctsio->residual = 0; 6854 ctsio->kern_data_len = sizeof(*data); 6855 ctsio->kern_total_len = sizeof(*data); 6856 ctsio->kern_data_resid = 0; 6857 ctsio->kern_rel_offset = 0; 6858 ctsio->kern_sg_entries = 0; 6859 6860 /* 6861 * If the maximum LBA is greater than 0xfffffffe, the user must 6862 * issue a SERVICE ACTION IN (16) command, with the read capacity 6863 * serivce action set. 6864 */ 6865 if (lun->be_lun->maxlba > 0xfffffffe) 6866 scsi_ulto4b(0xffffffff, data->addr); 6867 else 6868 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6869 6870 /* 6871 * XXX KDM this may not be 512 bytes... 6872 */ 6873 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6874 6875 ctsio->scsi_status = SCSI_STATUS_OK; 6876 6877 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6878 ctsio->be_move_done = ctl_config_move_done; 6879 ctl_datamove((union ctl_io *)ctsio); 6880 6881 return (CTL_RETVAL_COMPLETE); 6882} 6883 6884int 6885ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6886{ 6887 struct scsi_read_capacity_16 *cdb; 6888 struct scsi_read_capacity_data_long *data; 6889 struct ctl_lun *lun; 6890 uint64_t lba; 6891 uint32_t alloc_len; 6892 6893 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6894 6895 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6896 6897 alloc_len = scsi_4btoul(cdb->alloc_len); 6898 lba = scsi_8btou64(cdb->addr); 6899 6900 if ((cdb->reladr & SRC16_PMI) 6901 && (lba != 0)) { 6902 ctl_set_invalid_field(/*ctsio*/ ctsio, 6903 /*sks_valid*/ 1, 6904 /*command*/ 1, 6905 /*field*/ 2, 6906 /*bit_valid*/ 0, 6907 /*bit*/ 0); 6908 ctl_done((union ctl_io *)ctsio); 6909 return (CTL_RETVAL_COMPLETE); 6910 } 6911 6912 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6913 6914 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6915 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6916 6917 if (sizeof(*data) < alloc_len) { 6918 ctsio->residual = alloc_len - sizeof(*data); 6919 ctsio->kern_data_len = sizeof(*data); 6920 ctsio->kern_total_len = sizeof(*data); 6921 } else { 6922 ctsio->residual = 0; 6923 ctsio->kern_data_len = alloc_len; 6924 ctsio->kern_total_len = alloc_len; 6925 } 6926 ctsio->kern_data_resid = 0; 6927 ctsio->kern_rel_offset = 0; 6928 ctsio->kern_sg_entries = 0; 6929 6930 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6931 /* XXX KDM this may not be 512 bytes... */ 6932 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6933 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6934 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6935 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6936 data->lalba_lbp[0] |= SRC16_LBPME; 6937 6938 ctsio->scsi_status = SCSI_STATUS_OK; 6939 6940 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6941 ctsio->be_move_done = ctl_config_move_done; 6942 ctl_datamove((union ctl_io *)ctsio); 6943 6944 return (CTL_RETVAL_COMPLETE); 6945} 6946 6947int 6948ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 6949{ 6950 struct scsi_maintenance_in *cdb; 6951 int retval; 6952 int alloc_len, total_len = 0; 6953 int num_target_port_groups, single; 6954 struct ctl_lun *lun; 6955 struct ctl_softc *softc; 6956 struct scsi_target_group_data *rtg_ptr; 6957 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2; 6958 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2, 6959 *tp_desc_ptr2_1, *tp_desc_ptr2_2; 6960 6961 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 6962 6963 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 6964 softc = control_softc; 6965 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6966 6967 retval = CTL_RETVAL_COMPLETE; 6968 6969 single = ctl_is_single; 6970 if (single) 6971 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1; 6972 else 6973 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 6974 6975 total_len = sizeof(struct scsi_target_group_data) + 6976 sizeof(struct scsi_target_port_group_descriptor) * 6977 num_target_port_groups + 6978 sizeof(struct scsi_target_port_descriptor) * 6979 NUM_PORTS_PER_GRP * num_target_port_groups; 6980 6981 alloc_len = scsi_4btoul(cdb->length); 6982 6983 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6984 6985 ctsio->kern_sg_entries = 0; 6986 6987 if (total_len < alloc_len) { 6988 ctsio->residual = alloc_len - total_len; 6989 ctsio->kern_data_len = total_len; 6990 ctsio->kern_total_len = total_len; 6991 } else { 6992 ctsio->residual = 0; 6993 ctsio->kern_data_len = alloc_len; 6994 ctsio->kern_total_len = alloc_len; 6995 } 6996 ctsio->kern_data_resid = 0; 6997 ctsio->kern_rel_offset = 0; 6998 6999 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr; 7000 7001 tpg_desc_ptr1 = &rtg_ptr->groups[0]; 7002 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0]; 7003 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *) 7004 &tp_desc_ptr1_1->desc_list[0]; 7005 7006 if (single == 0) { 7007 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *) 7008 &tp_desc_ptr1_2->desc_list[0]; 7009 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0]; 7010 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *) 7011 &tp_desc_ptr2_1->desc_list[0]; 7012 } else { 7013 tpg_desc_ptr2 = NULL; 7014 tp_desc_ptr2_1 = NULL; 7015 tp_desc_ptr2_2 = NULL; 7016 } 7017 7018 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7019 if (single == 0) { 7020 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7021 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7022 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7023 tpg_desc_ptr2->pref_state = 7024 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7025 } else { 7026 tpg_desc_ptr1->pref_state = 7027 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7028 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7029 } 7030 } else { 7031 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7032 tpg_desc_ptr1->pref_state = 7033 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7034 tpg_desc_ptr2->pref_state = TPG_PRIMARY; 7035 } else { 7036 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7037 tpg_desc_ptr2->pref_state = 7038 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7039 } 7040 } 7041 } else { 7042 tpg_desc_ptr1->pref_state = TPG_PRIMARY; 7043 } 7044 tpg_desc_ptr1->support = 0; 7045 tpg_desc_ptr1->target_port_group[1] = 1; 7046 tpg_desc_ptr1->status = TPG_IMPLICIT; 7047 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP; 7048 7049 if (single == 0) { 7050 tpg_desc_ptr2->support = 0; 7051 tpg_desc_ptr2->target_port_group[1] = 2; 7052 tpg_desc_ptr2->status = TPG_IMPLICIT; 7053 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP; 7054 7055 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7056 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7057 7058 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9; 7059 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10; 7060 } else { 7061 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) { 7062 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1; 7063 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2; 7064 } else { 7065 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9; 7066 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10; 7067 } 7068 } 7069 7070 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7071 ctsio->be_move_done = ctl_config_move_done; 7072 7073 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7074 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7075 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7076 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7077 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7078 7079 ctl_datamove((union ctl_io *)ctsio); 7080 return(retval); 7081} 7082 7083int 7084ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7085{ 7086 struct ctl_lun *lun; 7087 struct scsi_report_supported_opcodes *cdb; 7088 const struct ctl_cmd_entry *entry, *sentry; 7089 struct scsi_report_supported_opcodes_all *all; 7090 struct scsi_report_supported_opcodes_descr *descr; 7091 struct scsi_report_supported_opcodes_one *one; 7092 int retval; 7093 int alloc_len, total_len; 7094 int opcode, service_action, i, j, num; 7095 7096 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7097 7098 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7099 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7100 7101 retval = CTL_RETVAL_COMPLETE; 7102 7103 opcode = cdb->requested_opcode; 7104 service_action = scsi_2btoul(cdb->requested_service_action); 7105 switch (cdb->options & RSO_OPTIONS_MASK) { 7106 case RSO_OPTIONS_ALL: 7107 num = 0; 7108 for (i = 0; i < 256; i++) { 7109 entry = &ctl_cmd_table[i]; 7110 if (entry->flags & CTL_CMD_FLAG_SA5) { 7111 for (j = 0; j < 32; j++) { 7112 sentry = &((const struct ctl_cmd_entry *) 7113 entry->execute)[j]; 7114 if (ctl_cmd_applicable( 7115 lun->be_lun->lun_type, sentry)) 7116 num++; 7117 } 7118 } else { 7119 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7120 entry)) 7121 num++; 7122 } 7123 } 7124 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7125 num * sizeof(struct scsi_report_supported_opcodes_descr); 7126 break; 7127 case RSO_OPTIONS_OC: 7128 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7129 ctl_set_invalid_field(/*ctsio*/ ctsio, 7130 /*sks_valid*/ 1, 7131 /*command*/ 1, 7132 /*field*/ 2, 7133 /*bit_valid*/ 1, 7134 /*bit*/ 2); 7135 ctl_done((union ctl_io *)ctsio); 7136 return (CTL_RETVAL_COMPLETE); 7137 } 7138 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7139 break; 7140 case RSO_OPTIONS_OC_SA: 7141 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7142 service_action >= 32) { 7143 ctl_set_invalid_field(/*ctsio*/ ctsio, 7144 /*sks_valid*/ 1, 7145 /*command*/ 1, 7146 /*field*/ 2, 7147 /*bit_valid*/ 1, 7148 /*bit*/ 2); 7149 ctl_done((union ctl_io *)ctsio); 7150 return (CTL_RETVAL_COMPLETE); 7151 } 7152 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7153 break; 7154 default: 7155 ctl_set_invalid_field(/*ctsio*/ ctsio, 7156 /*sks_valid*/ 1, 7157 /*command*/ 1, 7158 /*field*/ 2, 7159 /*bit_valid*/ 1, 7160 /*bit*/ 2); 7161 ctl_done((union ctl_io *)ctsio); 7162 return (CTL_RETVAL_COMPLETE); 7163 } 7164 7165 alloc_len = scsi_4btoul(cdb->length); 7166 7167 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7168 7169 ctsio->kern_sg_entries = 0; 7170 7171 if (total_len < alloc_len) { 7172 ctsio->residual = alloc_len - total_len; 7173 ctsio->kern_data_len = total_len; 7174 ctsio->kern_total_len = total_len; 7175 } else { 7176 ctsio->residual = 0; 7177 ctsio->kern_data_len = alloc_len; 7178 ctsio->kern_total_len = alloc_len; 7179 } 7180 ctsio->kern_data_resid = 0; 7181 ctsio->kern_rel_offset = 0; 7182 7183 switch (cdb->options & RSO_OPTIONS_MASK) { 7184 case RSO_OPTIONS_ALL: 7185 all = (struct scsi_report_supported_opcodes_all *) 7186 ctsio->kern_data_ptr; 7187 num = 0; 7188 for (i = 0; i < 256; i++) { 7189 entry = &ctl_cmd_table[i]; 7190 if (entry->flags & CTL_CMD_FLAG_SA5) { 7191 for (j = 0; j < 32; j++) { 7192 sentry = &((const struct ctl_cmd_entry *) 7193 entry->execute)[j]; 7194 if (!ctl_cmd_applicable( 7195 lun->be_lun->lun_type, sentry)) 7196 continue; 7197 descr = &all->descr[num++]; 7198 descr->opcode = i; 7199 scsi_ulto2b(j, descr->service_action); 7200 descr->flags = RSO_SERVACTV; 7201 scsi_ulto2b(sentry->length, 7202 descr->cdb_length); 7203 } 7204 } else { 7205 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7206 entry)) 7207 continue; 7208 descr = &all->descr[num++]; 7209 descr->opcode = i; 7210 scsi_ulto2b(0, descr->service_action); 7211 descr->flags = 0; 7212 scsi_ulto2b(entry->length, descr->cdb_length); 7213 } 7214 } 7215 scsi_ulto4b( 7216 num * sizeof(struct scsi_report_supported_opcodes_descr), 7217 all->length); 7218 break; 7219 case RSO_OPTIONS_OC: 7220 one = (struct scsi_report_supported_opcodes_one *) 7221 ctsio->kern_data_ptr; 7222 entry = &ctl_cmd_table[opcode]; 7223 goto fill_one; 7224 case RSO_OPTIONS_OC_SA: 7225 one = (struct scsi_report_supported_opcodes_one *) 7226 ctsio->kern_data_ptr; 7227 entry = &ctl_cmd_table[opcode]; 7228 entry = &((const struct ctl_cmd_entry *) 7229 entry->execute)[service_action]; 7230fill_one: 7231 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7232 one->support = 3; 7233 scsi_ulto2b(entry->length, one->cdb_length); 7234 one->cdb_usage[0] = opcode; 7235 memcpy(&one->cdb_usage[1], entry->usage, 7236 entry->length - 1); 7237 } else 7238 one->support = 1; 7239 break; 7240 } 7241 7242 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7243 ctsio->be_move_done = ctl_config_move_done; 7244 7245 ctl_datamove((union ctl_io *)ctsio); 7246 return(retval); 7247} 7248 7249int 7250ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7251{ 7252 struct ctl_lun *lun; 7253 struct scsi_report_supported_tmf *cdb; 7254 struct scsi_report_supported_tmf_data *data; 7255 int retval; 7256 int alloc_len, total_len; 7257 7258 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7259 7260 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7262 7263 retval = CTL_RETVAL_COMPLETE; 7264 7265 total_len = sizeof(struct scsi_report_supported_tmf_data); 7266 alloc_len = scsi_4btoul(cdb->length); 7267 7268 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7269 7270 ctsio->kern_sg_entries = 0; 7271 7272 if (total_len < alloc_len) { 7273 ctsio->residual = alloc_len - total_len; 7274 ctsio->kern_data_len = total_len; 7275 ctsio->kern_total_len = total_len; 7276 } else { 7277 ctsio->residual = 0; 7278 ctsio->kern_data_len = alloc_len; 7279 ctsio->kern_total_len = alloc_len; 7280 } 7281 ctsio->kern_data_resid = 0; 7282 ctsio->kern_rel_offset = 0; 7283 7284 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7285 data->byte1 |= RST_ATS | RST_LURS | RST_TRS; 7286 7287 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7288 ctsio->be_move_done = ctl_config_move_done; 7289 7290 ctl_datamove((union ctl_io *)ctsio); 7291 return (retval); 7292} 7293 7294int 7295ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7296{ 7297 struct scsi_per_res_in *cdb; 7298 int alloc_len, total_len = 0; 7299 /* struct scsi_per_res_in_rsrv in_data; */ 7300 struct ctl_lun *lun; 7301 struct ctl_softc *softc; 7302 7303 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7304 7305 softc = control_softc; 7306 7307 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7308 7309 alloc_len = scsi_2btoul(cdb->length); 7310 7311 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7312 7313retry: 7314 mtx_lock(&lun->lun_lock); 7315 switch (cdb->action) { 7316 case SPRI_RK: /* read keys */ 7317 total_len = sizeof(struct scsi_per_res_in_keys) + 7318 lun->pr_key_count * 7319 sizeof(struct scsi_per_res_key); 7320 break; 7321 case SPRI_RR: /* read reservation */ 7322 if (lun->flags & CTL_LUN_PR_RESERVED) 7323 total_len = sizeof(struct scsi_per_res_in_rsrv); 7324 else 7325 total_len = sizeof(struct scsi_per_res_in_header); 7326 break; 7327 case SPRI_RC: /* report capabilities */ 7328 total_len = sizeof(struct scsi_per_res_cap); 7329 break; 7330 default: 7331 panic("Invalid PR type %x", cdb->action); 7332 } 7333 mtx_unlock(&lun->lun_lock); 7334 7335 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7336 7337 if (total_len < alloc_len) { 7338 ctsio->residual = alloc_len - total_len; 7339 ctsio->kern_data_len = total_len; 7340 ctsio->kern_total_len = total_len; 7341 } else { 7342 ctsio->residual = 0; 7343 ctsio->kern_data_len = alloc_len; 7344 ctsio->kern_total_len = alloc_len; 7345 } 7346 7347 ctsio->kern_data_resid = 0; 7348 ctsio->kern_rel_offset = 0; 7349 ctsio->kern_sg_entries = 0; 7350 7351 mtx_lock(&lun->lun_lock); 7352 switch (cdb->action) { 7353 case SPRI_RK: { // read keys 7354 struct scsi_per_res_in_keys *res_keys; 7355 int i, key_count; 7356 7357 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7358 7359 /* 7360 * We had to drop the lock to allocate our buffer, which 7361 * leaves time for someone to come in with another 7362 * persistent reservation. (That is unlikely, though, 7363 * since this should be the only persistent reservation 7364 * command active right now.) 7365 */ 7366 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7367 (lun->pr_key_count * 7368 sizeof(struct scsi_per_res_key)))){ 7369 mtx_unlock(&lun->lun_lock); 7370 free(ctsio->kern_data_ptr, M_CTL); 7371 printf("%s: reservation length changed, retrying\n", 7372 __func__); 7373 goto retry; 7374 } 7375 7376 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7377 7378 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7379 lun->pr_key_count, res_keys->header.length); 7380 7381 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7382 if (!lun->per_res[i].registered) 7383 continue; 7384 7385 /* 7386 * We used lun->pr_key_count to calculate the 7387 * size to allocate. If it turns out the number of 7388 * initiators with the registered flag set is 7389 * larger than that (i.e. they haven't been kept in 7390 * sync), we've got a problem. 7391 */ 7392 if (key_count >= lun->pr_key_count) { 7393#ifdef NEEDTOPORT 7394 csevent_log(CSC_CTL | CSC_SHELF_SW | 7395 CTL_PR_ERROR, 7396 csevent_LogType_Fault, 7397 csevent_AlertLevel_Yellow, 7398 csevent_FRU_ShelfController, 7399 csevent_FRU_Firmware, 7400 csevent_FRU_Unknown, 7401 "registered keys %d >= key " 7402 "count %d", key_count, 7403 lun->pr_key_count); 7404#endif 7405 key_count++; 7406 continue; 7407 } 7408 memcpy(res_keys->keys[key_count].key, 7409 lun->per_res[i].res_key.key, 7410 ctl_min(sizeof(res_keys->keys[key_count].key), 7411 sizeof(lun->per_res[i].res_key))); 7412 key_count++; 7413 } 7414 break; 7415 } 7416 case SPRI_RR: { // read reservation 7417 struct scsi_per_res_in_rsrv *res; 7418 int tmp_len, header_only; 7419 7420 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7421 7422 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7423 7424 if (lun->flags & CTL_LUN_PR_RESERVED) 7425 { 7426 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7427 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7428 res->header.length); 7429 header_only = 0; 7430 } else { 7431 tmp_len = sizeof(struct scsi_per_res_in_header); 7432 scsi_ulto4b(0, res->header.length); 7433 header_only = 1; 7434 } 7435 7436 /* 7437 * We had to drop the lock to allocate our buffer, which 7438 * leaves time for someone to come in with another 7439 * persistent reservation. (That is unlikely, though, 7440 * since this should be the only persistent reservation 7441 * command active right now.) 7442 */ 7443 if (tmp_len != total_len) { 7444 mtx_unlock(&lun->lun_lock); 7445 free(ctsio->kern_data_ptr, M_CTL); 7446 printf("%s: reservation status changed, retrying\n", 7447 __func__); 7448 goto retry; 7449 } 7450 7451 /* 7452 * No reservation held, so we're done. 7453 */ 7454 if (header_only != 0) 7455 break; 7456 7457 /* 7458 * If the registration is an All Registrants type, the key 7459 * is 0, since it doesn't really matter. 7460 */ 7461 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7462 memcpy(res->data.reservation, 7463 &lun->per_res[lun->pr_res_idx].res_key, 7464 sizeof(struct scsi_per_res_key)); 7465 } 7466 res->data.scopetype = lun->res_type; 7467 break; 7468 } 7469 case SPRI_RC: //report capabilities 7470 { 7471 struct scsi_per_res_cap *res_cap; 7472 uint16_t type_mask; 7473 7474 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7475 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7476 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3; 7477 type_mask = SPRI_TM_WR_EX_AR | 7478 SPRI_TM_EX_AC_RO | 7479 SPRI_TM_WR_EX_RO | 7480 SPRI_TM_EX_AC | 7481 SPRI_TM_WR_EX | 7482 SPRI_TM_EX_AC_AR; 7483 scsi_ulto2b(type_mask, res_cap->type_mask); 7484 break; 7485 } 7486 case SPRI_RS: //read full status 7487 default: 7488 /* 7489 * This is a bug, because we just checked for this above, 7490 * and should have returned an error. 7491 */ 7492 panic("Invalid PR type %x", cdb->action); 7493 break; /* NOTREACHED */ 7494 } 7495 mtx_unlock(&lun->lun_lock); 7496 7497 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7498 ctsio->be_move_done = ctl_config_move_done; 7499 7500 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n", 7501 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1], 7502 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3], 7503 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5], 7504 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7])); 7505 7506 ctl_datamove((union ctl_io *)ctsio); 7507 7508 return (CTL_RETVAL_COMPLETE); 7509} 7510 7511/* 7512 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7513 * it should return. 7514 */ 7515static int 7516ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7517 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7518 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7519 struct scsi_per_res_out_parms* param) 7520{ 7521 union ctl_ha_msg persis_io; 7522 int retval, i; 7523 int isc_retval; 7524 7525 retval = 0; 7526 7527 mtx_lock(&lun->lun_lock); 7528 if (sa_res_key == 0) { 7529 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7530 /* validate scope and type */ 7531 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7532 SPR_LU_SCOPE) { 7533 mtx_unlock(&lun->lun_lock); 7534 ctl_set_invalid_field(/*ctsio*/ ctsio, 7535 /*sks_valid*/ 1, 7536 /*command*/ 1, 7537 /*field*/ 2, 7538 /*bit_valid*/ 1, 7539 /*bit*/ 4); 7540 ctl_done((union ctl_io *)ctsio); 7541 return (1); 7542 } 7543 7544 if (type>8 || type==2 || type==4 || type==0) { 7545 mtx_unlock(&lun->lun_lock); 7546 ctl_set_invalid_field(/*ctsio*/ ctsio, 7547 /*sks_valid*/ 1, 7548 /*command*/ 1, 7549 /*field*/ 2, 7550 /*bit_valid*/ 1, 7551 /*bit*/ 0); 7552 ctl_done((union ctl_io *)ctsio); 7553 return (1); 7554 } 7555 7556 /* temporarily unregister this nexus */ 7557 lun->per_res[residx].registered = 0; 7558 7559 /* 7560 * Unregister everybody else and build UA for 7561 * them 7562 */ 7563 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7564 if (lun->per_res[i].registered == 0) 7565 continue; 7566 7567 if (!persis_offset 7568 && i <CTL_MAX_INITIATORS) 7569 lun->pending_sense[i].ua_pending |= 7570 CTL_UA_REG_PREEMPT; 7571 else if (persis_offset 7572 && i >= persis_offset) 7573 lun->pending_sense[i-persis_offset 7574 ].ua_pending |= 7575 CTL_UA_REG_PREEMPT; 7576 lun->per_res[i].registered = 0; 7577 memset(&lun->per_res[i].res_key, 0, 7578 sizeof(struct scsi_per_res_key)); 7579 } 7580 lun->per_res[residx].registered = 1; 7581 lun->pr_key_count = 1; 7582 lun->res_type = type; 7583 if (lun->res_type != SPR_TYPE_WR_EX_AR 7584 && lun->res_type != SPR_TYPE_EX_AC_AR) 7585 lun->pr_res_idx = residx; 7586 7587 /* send msg to other side */ 7588 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7589 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7590 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7591 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7592 persis_io.pr.pr_info.res_type = type; 7593 memcpy(persis_io.pr.pr_info.sa_res_key, 7594 param->serv_act_res_key, 7595 sizeof(param->serv_act_res_key)); 7596 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7597 &persis_io, sizeof(persis_io), 0)) > 7598 CTL_HA_STATUS_SUCCESS) { 7599 printf("CTL:Persis Out error returned " 7600 "from ctl_ha_msg_send %d\n", 7601 isc_retval); 7602 } 7603 } else { 7604 /* not all registrants */ 7605 mtx_unlock(&lun->lun_lock); 7606 free(ctsio->kern_data_ptr, M_CTL); 7607 ctl_set_invalid_field(ctsio, 7608 /*sks_valid*/ 1, 7609 /*command*/ 0, 7610 /*field*/ 8, 7611 /*bit_valid*/ 0, 7612 /*bit*/ 0); 7613 ctl_done((union ctl_io *)ctsio); 7614 return (1); 7615 } 7616 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7617 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7618 int found = 0; 7619 7620 if (res_key == sa_res_key) { 7621 /* special case */ 7622 /* 7623 * The spec implies this is not good but doesn't 7624 * say what to do. There are two choices either 7625 * generate a res conflict or check condition 7626 * with illegal field in parameter data. Since 7627 * that is what is done when the sa_res_key is 7628 * zero I'll take that approach since this has 7629 * to do with the sa_res_key. 7630 */ 7631 mtx_unlock(&lun->lun_lock); 7632 free(ctsio->kern_data_ptr, M_CTL); 7633 ctl_set_invalid_field(ctsio, 7634 /*sks_valid*/ 1, 7635 /*command*/ 0, 7636 /*field*/ 8, 7637 /*bit_valid*/ 0, 7638 /*bit*/ 0); 7639 ctl_done((union ctl_io *)ctsio); 7640 return (1); 7641 } 7642 7643 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7644 if (lun->per_res[i].registered 7645 && memcmp(param->serv_act_res_key, 7646 lun->per_res[i].res_key.key, 7647 sizeof(struct scsi_per_res_key)) != 0) 7648 continue; 7649 7650 found = 1; 7651 lun->per_res[i].registered = 0; 7652 memset(&lun->per_res[i].res_key, 0, 7653 sizeof(struct scsi_per_res_key)); 7654 lun->pr_key_count--; 7655 7656 if (!persis_offset 7657 && i < CTL_MAX_INITIATORS) 7658 lun->pending_sense[i].ua_pending |= 7659 CTL_UA_REG_PREEMPT; 7660 else if (persis_offset 7661 && i >= persis_offset) 7662 lun->pending_sense[i-persis_offset].ua_pending|= 7663 CTL_UA_REG_PREEMPT; 7664 } 7665 if (!found) { 7666 mtx_unlock(&lun->lun_lock); 7667 free(ctsio->kern_data_ptr, M_CTL); 7668 ctl_set_reservation_conflict(ctsio); 7669 ctl_done((union ctl_io *)ctsio); 7670 return (CTL_RETVAL_COMPLETE); 7671 } 7672 /* send msg to other side */ 7673 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7674 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7675 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7676 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7677 persis_io.pr.pr_info.res_type = type; 7678 memcpy(persis_io.pr.pr_info.sa_res_key, 7679 param->serv_act_res_key, 7680 sizeof(param->serv_act_res_key)); 7681 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7682 &persis_io, sizeof(persis_io), 0)) > 7683 CTL_HA_STATUS_SUCCESS) { 7684 printf("CTL:Persis Out error returned from " 7685 "ctl_ha_msg_send %d\n", isc_retval); 7686 } 7687 } else { 7688 /* Reserved but not all registrants */ 7689 /* sa_res_key is res holder */ 7690 if (memcmp(param->serv_act_res_key, 7691 lun->per_res[lun->pr_res_idx].res_key.key, 7692 sizeof(struct scsi_per_res_key)) == 0) { 7693 /* validate scope and type */ 7694 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7695 SPR_LU_SCOPE) { 7696 mtx_unlock(&lun->lun_lock); 7697 ctl_set_invalid_field(/*ctsio*/ ctsio, 7698 /*sks_valid*/ 1, 7699 /*command*/ 1, 7700 /*field*/ 2, 7701 /*bit_valid*/ 1, 7702 /*bit*/ 4); 7703 ctl_done((union ctl_io *)ctsio); 7704 return (1); 7705 } 7706 7707 if (type>8 || type==2 || type==4 || type==0) { 7708 mtx_unlock(&lun->lun_lock); 7709 ctl_set_invalid_field(/*ctsio*/ ctsio, 7710 /*sks_valid*/ 1, 7711 /*command*/ 1, 7712 /*field*/ 2, 7713 /*bit_valid*/ 1, 7714 /*bit*/ 0); 7715 ctl_done((union ctl_io *)ctsio); 7716 return (1); 7717 } 7718 7719 /* 7720 * Do the following: 7721 * if sa_res_key != res_key remove all 7722 * registrants w/sa_res_key and generate UA 7723 * for these registrants(Registrations 7724 * Preempted) if it wasn't an exclusive 7725 * reservation generate UA(Reservations 7726 * Preempted) for all other registered nexuses 7727 * if the type has changed. Establish the new 7728 * reservation and holder. If res_key and 7729 * sa_res_key are the same do the above 7730 * except don't unregister the res holder. 7731 */ 7732 7733 /* 7734 * Temporarily unregister so it won't get 7735 * removed or UA generated 7736 */ 7737 lun->per_res[residx].registered = 0; 7738 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7739 if (lun->per_res[i].registered == 0) 7740 continue; 7741 7742 if (memcmp(param->serv_act_res_key, 7743 lun->per_res[i].res_key.key, 7744 sizeof(struct scsi_per_res_key)) == 0) { 7745 lun->per_res[i].registered = 0; 7746 memset(&lun->per_res[i].res_key, 7747 0, 7748 sizeof(struct scsi_per_res_key)); 7749 lun->pr_key_count--; 7750 7751 if (!persis_offset 7752 && i < CTL_MAX_INITIATORS) 7753 lun->pending_sense[i 7754 ].ua_pending |= 7755 CTL_UA_REG_PREEMPT; 7756 else if (persis_offset 7757 && i >= persis_offset) 7758 lun->pending_sense[ 7759 i-persis_offset].ua_pending |= 7760 CTL_UA_REG_PREEMPT; 7761 } else if (type != lun->res_type 7762 && (lun->res_type == SPR_TYPE_WR_EX_RO 7763 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 7764 if (!persis_offset 7765 && i < CTL_MAX_INITIATORS) 7766 lun->pending_sense[i 7767 ].ua_pending |= 7768 CTL_UA_RES_RELEASE; 7769 else if (persis_offset 7770 && i >= persis_offset) 7771 lun->pending_sense[ 7772 i-persis_offset 7773 ].ua_pending |= 7774 CTL_UA_RES_RELEASE; 7775 } 7776 } 7777 lun->per_res[residx].registered = 1; 7778 lun->res_type = type; 7779 if (lun->res_type != SPR_TYPE_WR_EX_AR 7780 && lun->res_type != SPR_TYPE_EX_AC_AR) 7781 lun->pr_res_idx = residx; 7782 else 7783 lun->pr_res_idx = 7784 CTL_PR_ALL_REGISTRANTS; 7785 7786 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7787 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7788 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7789 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7790 persis_io.pr.pr_info.res_type = type; 7791 memcpy(persis_io.pr.pr_info.sa_res_key, 7792 param->serv_act_res_key, 7793 sizeof(param->serv_act_res_key)); 7794 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7795 &persis_io, sizeof(persis_io), 0)) > 7796 CTL_HA_STATUS_SUCCESS) { 7797 printf("CTL:Persis Out error returned " 7798 "from ctl_ha_msg_send %d\n", 7799 isc_retval); 7800 } 7801 } else { 7802 /* 7803 * sa_res_key is not the res holder just 7804 * remove registrants 7805 */ 7806 int found=0; 7807 7808 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7809 if (memcmp(param->serv_act_res_key, 7810 lun->per_res[i].res_key.key, 7811 sizeof(struct scsi_per_res_key)) != 0) 7812 continue; 7813 7814 found = 1; 7815 lun->per_res[i].registered = 0; 7816 memset(&lun->per_res[i].res_key, 0, 7817 sizeof(struct scsi_per_res_key)); 7818 lun->pr_key_count--; 7819 7820 if (!persis_offset 7821 && i < CTL_MAX_INITIATORS) 7822 lun->pending_sense[i].ua_pending |= 7823 CTL_UA_REG_PREEMPT; 7824 else if (persis_offset 7825 && i >= persis_offset) 7826 lun->pending_sense[ 7827 i-persis_offset].ua_pending |= 7828 CTL_UA_REG_PREEMPT; 7829 } 7830 7831 if (!found) { 7832 mtx_unlock(&lun->lun_lock); 7833 free(ctsio->kern_data_ptr, M_CTL); 7834 ctl_set_reservation_conflict(ctsio); 7835 ctl_done((union ctl_io *)ctsio); 7836 return (1); 7837 } 7838 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7839 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7840 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7841 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7842 persis_io.pr.pr_info.res_type = type; 7843 memcpy(persis_io.pr.pr_info.sa_res_key, 7844 param->serv_act_res_key, 7845 sizeof(param->serv_act_res_key)); 7846 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7847 &persis_io, sizeof(persis_io), 0)) > 7848 CTL_HA_STATUS_SUCCESS) { 7849 printf("CTL:Persis Out error returned " 7850 "from ctl_ha_msg_send %d\n", 7851 isc_retval); 7852 } 7853 } 7854 } 7855 7856 lun->PRGeneration++; 7857 mtx_unlock(&lun->lun_lock); 7858 7859 return (retval); 7860} 7861 7862static void 7863ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7864{ 7865 int i; 7866 7867 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7868 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7869 || memcmp(&lun->per_res[lun->pr_res_idx].res_key, 7870 msg->pr.pr_info.sa_res_key, 7871 sizeof(struct scsi_per_res_key)) != 0) { 7872 uint64_t sa_res_key; 7873 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7874 7875 if (sa_res_key == 0) { 7876 /* temporarily unregister this nexus */ 7877 lun->per_res[msg->pr.pr_info.residx].registered = 0; 7878 7879 /* 7880 * Unregister everybody else and build UA for 7881 * them 7882 */ 7883 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7884 if (lun->per_res[i].registered == 0) 7885 continue; 7886 7887 if (!persis_offset 7888 && i < CTL_MAX_INITIATORS) 7889 lun->pending_sense[i].ua_pending |= 7890 CTL_UA_REG_PREEMPT; 7891 else if (persis_offset && i >= persis_offset) 7892 lun->pending_sense[i - 7893 persis_offset].ua_pending |= 7894 CTL_UA_REG_PREEMPT; 7895 lun->per_res[i].registered = 0; 7896 memset(&lun->per_res[i].res_key, 0, 7897 sizeof(struct scsi_per_res_key)); 7898 } 7899 7900 lun->per_res[msg->pr.pr_info.residx].registered = 1; 7901 lun->pr_key_count = 1; 7902 lun->res_type = msg->pr.pr_info.res_type; 7903 if (lun->res_type != SPR_TYPE_WR_EX_AR 7904 && lun->res_type != SPR_TYPE_EX_AC_AR) 7905 lun->pr_res_idx = msg->pr.pr_info.residx; 7906 } else { 7907 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7908 if (memcmp(msg->pr.pr_info.sa_res_key, 7909 lun->per_res[i].res_key.key, 7910 sizeof(struct scsi_per_res_key)) != 0) 7911 continue; 7912 7913 lun->per_res[i].registered = 0; 7914 memset(&lun->per_res[i].res_key, 0, 7915 sizeof(struct scsi_per_res_key)); 7916 lun->pr_key_count--; 7917 7918 if (!persis_offset 7919 && i < persis_offset) 7920 lun->pending_sense[i].ua_pending |= 7921 CTL_UA_REG_PREEMPT; 7922 else if (persis_offset 7923 && i >= persis_offset) 7924 lun->pending_sense[i - 7925 persis_offset].ua_pending |= 7926 CTL_UA_REG_PREEMPT; 7927 } 7928 } 7929 } else { 7930 /* 7931 * Temporarily unregister so it won't get removed 7932 * or UA generated 7933 */ 7934 lun->per_res[msg->pr.pr_info.residx].registered = 0; 7935 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7936 if (lun->per_res[i].registered == 0) 7937 continue; 7938 7939 if (memcmp(msg->pr.pr_info.sa_res_key, 7940 lun->per_res[i].res_key.key, 7941 sizeof(struct scsi_per_res_key)) == 0) { 7942 lun->per_res[i].registered = 0; 7943 memset(&lun->per_res[i].res_key, 0, 7944 sizeof(struct scsi_per_res_key)); 7945 lun->pr_key_count--; 7946 if (!persis_offset 7947 && i < CTL_MAX_INITIATORS) 7948 lun->pending_sense[i].ua_pending |= 7949 CTL_UA_REG_PREEMPT; 7950 else if (persis_offset 7951 && i >= persis_offset) 7952 lun->pending_sense[i - 7953 persis_offset].ua_pending |= 7954 CTL_UA_REG_PREEMPT; 7955 } else if (msg->pr.pr_info.res_type != lun->res_type 7956 && (lun->res_type == SPR_TYPE_WR_EX_RO 7957 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 7958 if (!persis_offset 7959 && i < persis_offset) 7960 lun->pending_sense[i 7961 ].ua_pending |= 7962 CTL_UA_RES_RELEASE; 7963 else if (persis_offset 7964 && i >= persis_offset) 7965 lun->pending_sense[i - 7966 persis_offset].ua_pending |= 7967 CTL_UA_RES_RELEASE; 7968 } 7969 } 7970 lun->per_res[msg->pr.pr_info.residx].registered = 1; 7971 lun->res_type = msg->pr.pr_info.res_type; 7972 if (lun->res_type != SPR_TYPE_WR_EX_AR 7973 && lun->res_type != SPR_TYPE_EX_AC_AR) 7974 lun->pr_res_idx = msg->pr.pr_info.residx; 7975 else 7976 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7977 } 7978 lun->PRGeneration++; 7979 7980} 7981 7982 7983int 7984ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 7985{ 7986 int retval; 7987 int isc_retval; 7988 u_int32_t param_len; 7989 struct scsi_per_res_out *cdb; 7990 struct ctl_lun *lun; 7991 struct scsi_per_res_out_parms* param; 7992 struct ctl_softc *softc; 7993 uint32_t residx; 7994 uint64_t res_key, sa_res_key; 7995 uint8_t type; 7996 union ctl_ha_msg persis_io; 7997 int i; 7998 7999 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8000 8001 retval = CTL_RETVAL_COMPLETE; 8002 8003 softc = control_softc; 8004 8005 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8006 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8007 8008 /* 8009 * We only support whole-LUN scope. The scope & type are ignored for 8010 * register, register and ignore existing key and clear. 8011 * We sometimes ignore scope and type on preempts too!! 8012 * Verify reservation type here as well. 8013 */ 8014 type = cdb->scope_type & SPR_TYPE_MASK; 8015 if ((cdb->action == SPRO_RESERVE) 8016 || (cdb->action == SPRO_RELEASE)) { 8017 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8018 ctl_set_invalid_field(/*ctsio*/ ctsio, 8019 /*sks_valid*/ 1, 8020 /*command*/ 1, 8021 /*field*/ 2, 8022 /*bit_valid*/ 1, 8023 /*bit*/ 4); 8024 ctl_done((union ctl_io *)ctsio); 8025 return (CTL_RETVAL_COMPLETE); 8026 } 8027 8028 if (type>8 || type==2 || type==4 || type==0) { 8029 ctl_set_invalid_field(/*ctsio*/ ctsio, 8030 /*sks_valid*/ 1, 8031 /*command*/ 1, 8032 /*field*/ 2, 8033 /*bit_valid*/ 1, 8034 /*bit*/ 0); 8035 ctl_done((union ctl_io *)ctsio); 8036 return (CTL_RETVAL_COMPLETE); 8037 } 8038 } 8039 8040 param_len = scsi_4btoul(cdb->length); 8041 8042 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8043 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8044 ctsio->kern_data_len = param_len; 8045 ctsio->kern_total_len = param_len; 8046 ctsio->kern_data_resid = 0; 8047 ctsio->kern_rel_offset = 0; 8048 ctsio->kern_sg_entries = 0; 8049 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8050 ctsio->be_move_done = ctl_config_move_done; 8051 ctl_datamove((union ctl_io *)ctsio); 8052 8053 return (CTL_RETVAL_COMPLETE); 8054 } 8055 8056 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8057 8058 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8059 res_key = scsi_8btou64(param->res_key.key); 8060 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8061 8062 /* 8063 * Validate the reservation key here except for SPRO_REG_IGNO 8064 * This must be done for all other service actions 8065 */ 8066 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8067 mtx_lock(&lun->lun_lock); 8068 if (lun->per_res[residx].registered) { 8069 if (memcmp(param->res_key.key, 8070 lun->per_res[residx].res_key.key, 8071 ctl_min(sizeof(param->res_key), 8072 sizeof(lun->per_res[residx].res_key))) != 0) { 8073 /* 8074 * The current key passed in doesn't match 8075 * the one the initiator previously 8076 * registered. 8077 */ 8078 mtx_unlock(&lun->lun_lock); 8079 free(ctsio->kern_data_ptr, M_CTL); 8080 ctl_set_reservation_conflict(ctsio); 8081 ctl_done((union ctl_io *)ctsio); 8082 return (CTL_RETVAL_COMPLETE); 8083 } 8084 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8085 /* 8086 * We are not registered 8087 */ 8088 mtx_unlock(&lun->lun_lock); 8089 free(ctsio->kern_data_ptr, M_CTL); 8090 ctl_set_reservation_conflict(ctsio); 8091 ctl_done((union ctl_io *)ctsio); 8092 return (CTL_RETVAL_COMPLETE); 8093 } else if (res_key != 0) { 8094 /* 8095 * We are not registered and trying to register but 8096 * the register key isn't zero. 8097 */ 8098 mtx_unlock(&lun->lun_lock); 8099 free(ctsio->kern_data_ptr, M_CTL); 8100 ctl_set_reservation_conflict(ctsio); 8101 ctl_done((union ctl_io *)ctsio); 8102 return (CTL_RETVAL_COMPLETE); 8103 } 8104 mtx_unlock(&lun->lun_lock); 8105 } 8106 8107 switch (cdb->action & SPRO_ACTION_MASK) { 8108 case SPRO_REGISTER: 8109 case SPRO_REG_IGNO: { 8110 8111#if 0 8112 printf("Registration received\n"); 8113#endif 8114 8115 /* 8116 * We don't support any of these options, as we report in 8117 * the read capabilities request (see 8118 * ctl_persistent_reserve_in(), above). 8119 */ 8120 if ((param->flags & SPR_SPEC_I_PT) 8121 || (param->flags & SPR_ALL_TG_PT) 8122 || (param->flags & SPR_APTPL)) { 8123 int bit_ptr; 8124 8125 if (param->flags & SPR_APTPL) 8126 bit_ptr = 0; 8127 else if (param->flags & SPR_ALL_TG_PT) 8128 bit_ptr = 2; 8129 else /* SPR_SPEC_I_PT */ 8130 bit_ptr = 3; 8131 8132 free(ctsio->kern_data_ptr, M_CTL); 8133 ctl_set_invalid_field(ctsio, 8134 /*sks_valid*/ 1, 8135 /*command*/ 0, 8136 /*field*/ 20, 8137 /*bit_valid*/ 1, 8138 /*bit*/ bit_ptr); 8139 ctl_done((union ctl_io *)ctsio); 8140 return (CTL_RETVAL_COMPLETE); 8141 } 8142 8143 mtx_lock(&lun->lun_lock); 8144 8145 /* 8146 * The initiator wants to clear the 8147 * key/unregister. 8148 */ 8149 if (sa_res_key == 0) { 8150 if ((res_key == 0 8151 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8152 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8153 && !lun->per_res[residx].registered)) { 8154 mtx_unlock(&lun->lun_lock); 8155 goto done; 8156 } 8157 8158 lun->per_res[residx].registered = 0; 8159 memset(&lun->per_res[residx].res_key, 8160 0, sizeof(lun->per_res[residx].res_key)); 8161 lun->pr_key_count--; 8162 8163 if (residx == lun->pr_res_idx) { 8164 lun->flags &= ~CTL_LUN_PR_RESERVED; 8165 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8166 8167 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8168 || lun->res_type == SPR_TYPE_EX_AC_RO) 8169 && lun->pr_key_count) { 8170 /* 8171 * If the reservation is a registrants 8172 * only type we need to generate a UA 8173 * for other registered inits. The 8174 * sense code should be RESERVATIONS 8175 * RELEASED 8176 */ 8177 8178 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8179 if (lun->per_res[ 8180 i+persis_offset].registered 8181 == 0) 8182 continue; 8183 lun->pending_sense[i 8184 ].ua_pending |= 8185 CTL_UA_RES_RELEASE; 8186 } 8187 } 8188 lun->res_type = 0; 8189 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8190 if (lun->pr_key_count==0) { 8191 lun->flags &= ~CTL_LUN_PR_RESERVED; 8192 lun->res_type = 0; 8193 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8194 } 8195 } 8196 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8197 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8198 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8199 persis_io.pr.pr_info.residx = residx; 8200 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8201 &persis_io, sizeof(persis_io), 0 )) > 8202 CTL_HA_STATUS_SUCCESS) { 8203 printf("CTL:Persis Out error returned from " 8204 "ctl_ha_msg_send %d\n", isc_retval); 8205 } 8206 } else /* sa_res_key != 0 */ { 8207 8208 /* 8209 * If we aren't registered currently then increment 8210 * the key count and set the registered flag. 8211 */ 8212 if (!lun->per_res[residx].registered) { 8213 lun->pr_key_count++; 8214 lun->per_res[residx].registered = 1; 8215 } 8216 8217 memcpy(&lun->per_res[residx].res_key, 8218 param->serv_act_res_key, 8219 ctl_min(sizeof(param->serv_act_res_key), 8220 sizeof(lun->per_res[residx].res_key))); 8221 8222 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8223 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8224 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8225 persis_io.pr.pr_info.residx = residx; 8226 memcpy(persis_io.pr.pr_info.sa_res_key, 8227 param->serv_act_res_key, 8228 sizeof(param->serv_act_res_key)); 8229 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8230 &persis_io, sizeof(persis_io), 0)) > 8231 CTL_HA_STATUS_SUCCESS) { 8232 printf("CTL:Persis Out error returned from " 8233 "ctl_ha_msg_send %d\n", isc_retval); 8234 } 8235 } 8236 lun->PRGeneration++; 8237 mtx_unlock(&lun->lun_lock); 8238 8239 break; 8240 } 8241 case SPRO_RESERVE: 8242#if 0 8243 printf("Reserve executed type %d\n", type); 8244#endif 8245 mtx_lock(&lun->lun_lock); 8246 if (lun->flags & CTL_LUN_PR_RESERVED) { 8247 /* 8248 * if this isn't the reservation holder and it's 8249 * not a "all registrants" type or if the type is 8250 * different then we have a conflict 8251 */ 8252 if ((lun->pr_res_idx != residx 8253 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8254 || lun->res_type != type) { 8255 mtx_unlock(&lun->lun_lock); 8256 free(ctsio->kern_data_ptr, M_CTL); 8257 ctl_set_reservation_conflict(ctsio); 8258 ctl_done((union ctl_io *)ctsio); 8259 return (CTL_RETVAL_COMPLETE); 8260 } 8261 mtx_unlock(&lun->lun_lock); 8262 } else /* create a reservation */ { 8263 /* 8264 * If it's not an "all registrants" type record 8265 * reservation holder 8266 */ 8267 if (type != SPR_TYPE_WR_EX_AR 8268 && type != SPR_TYPE_EX_AC_AR) 8269 lun->pr_res_idx = residx; /* Res holder */ 8270 else 8271 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8272 8273 lun->flags |= CTL_LUN_PR_RESERVED; 8274 lun->res_type = type; 8275 8276 mtx_unlock(&lun->lun_lock); 8277 8278 /* send msg to other side */ 8279 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8280 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8281 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8282 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8283 persis_io.pr.pr_info.res_type = type; 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 from " 8288 "ctl_ha_msg_send %d\n", isc_retval); 8289 } 8290 } 8291 break; 8292 8293 case SPRO_RELEASE: 8294 mtx_lock(&lun->lun_lock); 8295 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8296 /* No reservation exists return good status */ 8297 mtx_unlock(&lun->lun_lock); 8298 goto done; 8299 } 8300 /* 8301 * Is this nexus a reservation holder? 8302 */ 8303 if (lun->pr_res_idx != residx 8304 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8305 /* 8306 * not a res holder return good status but 8307 * do nothing 8308 */ 8309 mtx_unlock(&lun->lun_lock); 8310 goto done; 8311 } 8312 8313 if (lun->res_type != type) { 8314 mtx_unlock(&lun->lun_lock); 8315 free(ctsio->kern_data_ptr, M_CTL); 8316 ctl_set_illegal_pr_release(ctsio); 8317 ctl_done((union ctl_io *)ctsio); 8318 return (CTL_RETVAL_COMPLETE); 8319 } 8320 8321 /* okay to release */ 8322 lun->flags &= ~CTL_LUN_PR_RESERVED; 8323 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8324 lun->res_type = 0; 8325 8326 /* 8327 * if this isn't an exclusive access 8328 * res generate UA for all other 8329 * registrants. 8330 */ 8331 if (type != SPR_TYPE_EX_AC 8332 && type != SPR_TYPE_WR_EX) { 8333 /* 8334 * temporarily unregister so we don't generate UA 8335 */ 8336 lun->per_res[residx].registered = 0; 8337 8338 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8339 if (lun->per_res[i+persis_offset].registered 8340 == 0) 8341 continue; 8342 lun->pending_sense[i].ua_pending |= 8343 CTL_UA_RES_RELEASE; 8344 } 8345 8346 lun->per_res[residx].registered = 1; 8347 } 8348 mtx_unlock(&lun->lun_lock); 8349 /* Send msg to other side */ 8350 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8351 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8352 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8353 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8354 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8355 printf("CTL:Persis Out error returned from " 8356 "ctl_ha_msg_send %d\n", isc_retval); 8357 } 8358 break; 8359 8360 case SPRO_CLEAR: 8361 /* send msg to other side */ 8362 8363 mtx_lock(&lun->lun_lock); 8364 lun->flags &= ~CTL_LUN_PR_RESERVED; 8365 lun->res_type = 0; 8366 lun->pr_key_count = 0; 8367 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8368 8369 8370 memset(&lun->per_res[residx].res_key, 8371 0, sizeof(lun->per_res[residx].res_key)); 8372 lun->per_res[residx].registered = 0; 8373 8374 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8375 if (lun->per_res[i].registered) { 8376 if (!persis_offset && i < CTL_MAX_INITIATORS) 8377 lun->pending_sense[i].ua_pending |= 8378 CTL_UA_RES_PREEMPT; 8379 else if (persis_offset && i >= persis_offset) 8380 lun->pending_sense[i-persis_offset 8381 ].ua_pending |= CTL_UA_RES_PREEMPT; 8382 8383 memset(&lun->per_res[i].res_key, 8384 0, sizeof(struct scsi_per_res_key)); 8385 lun->per_res[i].registered = 0; 8386 } 8387 lun->PRGeneration++; 8388 mtx_unlock(&lun->lun_lock); 8389 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8390 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8391 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8392 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8393 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8394 printf("CTL:Persis Out error returned from " 8395 "ctl_ha_msg_send %d\n", isc_retval); 8396 } 8397 break; 8398 8399 case SPRO_PREEMPT: { 8400 int nretval; 8401 8402 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8403 residx, ctsio, cdb, param); 8404 if (nretval != 0) 8405 return (CTL_RETVAL_COMPLETE); 8406 break; 8407 } 8408 default: 8409 panic("Invalid PR type %x", cdb->action); 8410 } 8411 8412done: 8413 free(ctsio->kern_data_ptr, M_CTL); 8414 ctl_set_success(ctsio); 8415 ctl_done((union ctl_io *)ctsio); 8416 8417 return (retval); 8418} 8419 8420/* 8421 * This routine is for handling a message from the other SC pertaining to 8422 * persistent reserve out. All the error checking will have been done 8423 * so only perorming the action need be done here to keep the two 8424 * in sync. 8425 */ 8426static void 8427ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8428{ 8429 struct ctl_lun *lun; 8430 struct ctl_softc *softc; 8431 int i; 8432 uint32_t targ_lun; 8433 8434 softc = control_softc; 8435 8436 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8437 lun = softc->ctl_luns[targ_lun]; 8438 mtx_lock(&lun->lun_lock); 8439 switch(msg->pr.pr_info.action) { 8440 case CTL_PR_REG_KEY: 8441 if (!lun->per_res[msg->pr.pr_info.residx].registered) { 8442 lun->per_res[msg->pr.pr_info.residx].registered = 1; 8443 lun->pr_key_count++; 8444 } 8445 lun->PRGeneration++; 8446 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key, 8447 msg->pr.pr_info.sa_res_key, 8448 sizeof(struct scsi_per_res_key)); 8449 break; 8450 8451 case CTL_PR_UNREG_KEY: 8452 lun->per_res[msg->pr.pr_info.residx].registered = 0; 8453 memset(&lun->per_res[msg->pr.pr_info.residx].res_key, 8454 0, sizeof(struct scsi_per_res_key)); 8455 lun->pr_key_count--; 8456 8457 /* XXX Need to see if the reservation has been released */ 8458 /* if so do we need to generate UA? */ 8459 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8460 lun->flags &= ~CTL_LUN_PR_RESERVED; 8461 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8462 8463 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8464 || lun->res_type == SPR_TYPE_EX_AC_RO) 8465 && lun->pr_key_count) { 8466 /* 8467 * If the reservation is a registrants 8468 * only type we need to generate a UA 8469 * for other registered inits. The 8470 * sense code should be RESERVATIONS 8471 * RELEASED 8472 */ 8473 8474 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8475 if (lun->per_res[i+ 8476 persis_offset].registered == 0) 8477 continue; 8478 8479 lun->pending_sense[i 8480 ].ua_pending |= 8481 CTL_UA_RES_RELEASE; 8482 } 8483 } 8484 lun->res_type = 0; 8485 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8486 if (lun->pr_key_count==0) { 8487 lun->flags &= ~CTL_LUN_PR_RESERVED; 8488 lun->res_type = 0; 8489 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8490 } 8491 } 8492 lun->PRGeneration++; 8493 break; 8494 8495 case CTL_PR_RESERVE: 8496 lun->flags |= CTL_LUN_PR_RESERVED; 8497 lun->res_type = msg->pr.pr_info.res_type; 8498 lun->pr_res_idx = msg->pr.pr_info.residx; 8499 8500 break; 8501 8502 case CTL_PR_RELEASE: 8503 /* 8504 * if this isn't an exclusive access res generate UA for all 8505 * other registrants. 8506 */ 8507 if (lun->res_type != SPR_TYPE_EX_AC 8508 && lun->res_type != SPR_TYPE_WR_EX) { 8509 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8510 if (lun->per_res[i+persis_offset].registered) 8511 lun->pending_sense[i].ua_pending |= 8512 CTL_UA_RES_RELEASE; 8513 } 8514 8515 lun->flags &= ~CTL_LUN_PR_RESERVED; 8516 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8517 lun->res_type = 0; 8518 break; 8519 8520 case CTL_PR_PREEMPT: 8521 ctl_pro_preempt_other(lun, msg); 8522 break; 8523 case CTL_PR_CLEAR: 8524 lun->flags &= ~CTL_LUN_PR_RESERVED; 8525 lun->res_type = 0; 8526 lun->pr_key_count = 0; 8527 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8528 8529 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8530 if (lun->per_res[i].registered == 0) 8531 continue; 8532 if (!persis_offset 8533 && i < CTL_MAX_INITIATORS) 8534 lun->pending_sense[i].ua_pending |= 8535 CTL_UA_RES_PREEMPT; 8536 else if (persis_offset 8537 && i >= persis_offset) 8538 lun->pending_sense[i-persis_offset].ua_pending|= 8539 CTL_UA_RES_PREEMPT; 8540 memset(&lun->per_res[i].res_key, 0, 8541 sizeof(struct scsi_per_res_key)); 8542 lun->per_res[i].registered = 0; 8543 } 8544 lun->PRGeneration++; 8545 break; 8546 } 8547 8548 mtx_unlock(&lun->lun_lock); 8549} 8550 8551int 8552ctl_read_write(struct ctl_scsiio *ctsio) 8553{ 8554 struct ctl_lun *lun; 8555 struct ctl_lba_len_flags *lbalen; 8556 uint64_t lba; 8557 uint32_t num_blocks; 8558 int fua, dpo; 8559 int retval; 8560 int isread; 8561 8562 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8563 8564 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8565 8566 fua = 0; 8567 dpo = 0; 8568 8569 retval = CTL_RETVAL_COMPLETE; 8570 8571 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8572 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8573 if (lun->flags & CTL_LUN_PR_RESERVED && isread) { 8574 uint32_t residx; 8575 8576 /* 8577 * XXX KDM need a lock here. 8578 */ 8579 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8580 if ((lun->res_type == SPR_TYPE_EX_AC 8581 && residx != lun->pr_res_idx) 8582 || ((lun->res_type == SPR_TYPE_EX_AC_RO 8583 || lun->res_type == SPR_TYPE_EX_AC_AR) 8584 && !lun->per_res[residx].registered)) { 8585 ctl_set_reservation_conflict(ctsio); 8586 ctl_done((union ctl_io *)ctsio); 8587 return (CTL_RETVAL_COMPLETE); 8588 } 8589 } 8590 8591 switch (ctsio->cdb[0]) { 8592 case READ_6: 8593 case WRITE_6: { 8594 struct scsi_rw_6 *cdb; 8595 8596 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8597 8598 lba = scsi_3btoul(cdb->addr); 8599 /* only 5 bits are valid in the most significant address byte */ 8600 lba &= 0x1fffff; 8601 num_blocks = cdb->length; 8602 /* 8603 * This is correct according to SBC-2. 8604 */ 8605 if (num_blocks == 0) 8606 num_blocks = 256; 8607 break; 8608 } 8609 case READ_10: 8610 case WRITE_10: { 8611 struct scsi_rw_10 *cdb; 8612 8613 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8614 8615 if (cdb->byte2 & SRW10_FUA) 8616 fua = 1; 8617 if (cdb->byte2 & SRW10_DPO) 8618 dpo = 1; 8619 8620 lba = scsi_4btoul(cdb->addr); 8621 num_blocks = scsi_2btoul(cdb->length); 8622 break; 8623 } 8624 case WRITE_VERIFY_10: { 8625 struct scsi_write_verify_10 *cdb; 8626 8627 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8628 8629 /* 8630 * XXX KDM we should do actual write verify support at some 8631 * point. This is obviously fake, we're just translating 8632 * things to a write. So we don't even bother checking the 8633 * BYTCHK field, since we don't do any verification. If 8634 * the user asks for it, we'll just pretend we did it. 8635 */ 8636 if (cdb->byte2 & SWV_DPO) 8637 dpo = 1; 8638 8639 lba = scsi_4btoul(cdb->addr); 8640 num_blocks = scsi_2btoul(cdb->length); 8641 break; 8642 } 8643 case READ_12: 8644 case WRITE_12: { 8645 struct scsi_rw_12 *cdb; 8646 8647 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8648 8649 if (cdb->byte2 & SRW12_FUA) 8650 fua = 1; 8651 if (cdb->byte2 & SRW12_DPO) 8652 dpo = 1; 8653 lba = scsi_4btoul(cdb->addr); 8654 num_blocks = scsi_4btoul(cdb->length); 8655 break; 8656 } 8657 case WRITE_VERIFY_12: { 8658 struct scsi_write_verify_12 *cdb; 8659 8660 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8661 8662 if (cdb->byte2 & SWV_DPO) 8663 dpo = 1; 8664 8665 lba = scsi_4btoul(cdb->addr); 8666 num_blocks = scsi_4btoul(cdb->length); 8667 8668 break; 8669 } 8670 case READ_16: 8671 case WRITE_16: { 8672 struct scsi_rw_16 *cdb; 8673 8674 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8675 8676 if (cdb->byte2 & SRW12_FUA) 8677 fua = 1; 8678 if (cdb->byte2 & SRW12_DPO) 8679 dpo = 1; 8680 8681 lba = scsi_8btou64(cdb->addr); 8682 num_blocks = scsi_4btoul(cdb->length); 8683 break; 8684 } 8685 case WRITE_VERIFY_16: { 8686 struct scsi_write_verify_16 *cdb; 8687 8688 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8689 8690 if (cdb->byte2 & SWV_DPO) 8691 dpo = 1; 8692 8693 lba = scsi_8btou64(cdb->addr); 8694 num_blocks = scsi_4btoul(cdb->length); 8695 break; 8696 } 8697 default: 8698 /* 8699 * We got a command we don't support. This shouldn't 8700 * happen, commands should be filtered out above us. 8701 */ 8702 ctl_set_invalid_opcode(ctsio); 8703 ctl_done((union ctl_io *)ctsio); 8704 8705 return (CTL_RETVAL_COMPLETE); 8706 break; /* NOTREACHED */ 8707 } 8708 8709 /* 8710 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8711 * interesting for us, but if RAIDCore is in write-back mode, 8712 * getting it to do write-through for a particular transaction may 8713 * not be possible. 8714 */ 8715 8716 /* 8717 * The first check is to make sure we're in bounds, the second 8718 * check is to catch wrap-around problems. If the lba + num blocks 8719 * is less than the lba, then we've wrapped around and the block 8720 * range is invalid anyway. 8721 */ 8722 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8723 || ((lba + num_blocks) < lba)) { 8724 ctl_set_lba_out_of_range(ctsio); 8725 ctl_done((union ctl_io *)ctsio); 8726 return (CTL_RETVAL_COMPLETE); 8727 } 8728 8729 /* 8730 * According to SBC-3, a transfer length of 0 is not an error. 8731 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8732 * translates to 256 blocks for those commands. 8733 */ 8734 if (num_blocks == 0) { 8735 ctl_set_success(ctsio); 8736 ctl_done((union ctl_io *)ctsio); 8737 return (CTL_RETVAL_COMPLETE); 8738 } 8739 8740 lbalen = (struct ctl_lba_len_flags *) 8741 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8742 lbalen->lba = lba; 8743 lbalen->len = num_blocks; 8744 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE; 8745 8746 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8747 ctsio->kern_rel_offset = 0; 8748 8749 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8750 8751 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8752 8753 return (retval); 8754} 8755 8756static int 8757ctl_cnw_cont(union ctl_io *io) 8758{ 8759 struct ctl_scsiio *ctsio; 8760 struct ctl_lun *lun; 8761 struct ctl_lba_len_flags *lbalen; 8762 int retval; 8763 8764 ctsio = &io->scsiio; 8765 ctsio->io_hdr.status = CTL_STATUS_NONE; 8766 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8767 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8768 lbalen = (struct ctl_lba_len_flags *) 8769 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8770 lbalen->flags = CTL_LLF_WRITE; 8771 8772 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8773 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8774 return (retval); 8775} 8776 8777int 8778ctl_cnw(struct ctl_scsiio *ctsio) 8779{ 8780 struct ctl_lun *lun; 8781 struct ctl_lba_len_flags *lbalen; 8782 uint64_t lba; 8783 uint32_t num_blocks; 8784 int fua, dpo; 8785 int retval; 8786 8787 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8788 8789 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8790 8791 fua = 0; 8792 dpo = 0; 8793 8794 retval = CTL_RETVAL_COMPLETE; 8795 8796 switch (ctsio->cdb[0]) { 8797 case COMPARE_AND_WRITE: { 8798 struct scsi_compare_and_write *cdb; 8799 8800 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8801 8802 if (cdb->byte2 & SRW10_FUA) 8803 fua = 1; 8804 if (cdb->byte2 & SRW10_DPO) 8805 dpo = 1; 8806 lba = scsi_8btou64(cdb->addr); 8807 num_blocks = cdb->length; 8808 break; 8809 } 8810 default: 8811 /* 8812 * We got a command we don't support. This shouldn't 8813 * happen, commands should be filtered out above us. 8814 */ 8815 ctl_set_invalid_opcode(ctsio); 8816 ctl_done((union ctl_io *)ctsio); 8817 8818 return (CTL_RETVAL_COMPLETE); 8819 break; /* NOTREACHED */ 8820 } 8821 8822 /* 8823 * XXX KDM what do we do with the DPO and FUA bits? FUA might be 8824 * interesting for us, but if RAIDCore is in write-back mode, 8825 * getting it to do write-through for a particular transaction may 8826 * not be possible. 8827 */ 8828 8829 /* 8830 * The first check is to make sure we're in bounds, the second 8831 * check is to catch wrap-around problems. If the lba + num blocks 8832 * is less than the lba, then we've wrapped around and the block 8833 * range is invalid anyway. 8834 */ 8835 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8836 || ((lba + num_blocks) < lba)) { 8837 ctl_set_lba_out_of_range(ctsio); 8838 ctl_done((union ctl_io *)ctsio); 8839 return (CTL_RETVAL_COMPLETE); 8840 } 8841 8842 /* 8843 * According to SBC-3, a transfer length of 0 is not an error. 8844 */ 8845 if (num_blocks == 0) { 8846 ctl_set_success(ctsio); 8847 ctl_done((union ctl_io *)ctsio); 8848 return (CTL_RETVAL_COMPLETE); 8849 } 8850 8851 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8852 ctsio->kern_rel_offset = 0; 8853 8854 /* 8855 * Set the IO_CONT flag, so that if this I/O gets passed to 8856 * ctl_data_submit_done(), it'll get passed back to 8857 * ctl_ctl_cnw_cont() for further processing. 8858 */ 8859 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8860 ctsio->io_cont = ctl_cnw_cont; 8861 8862 lbalen = (struct ctl_lba_len_flags *) 8863 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8864 lbalen->lba = lba; 8865 lbalen->len = num_blocks; 8866 lbalen->flags = CTL_LLF_COMPARE; 8867 8868 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8869 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8870 return (retval); 8871} 8872 8873int 8874ctl_verify(struct ctl_scsiio *ctsio) 8875{ 8876 struct ctl_lun *lun; 8877 struct ctl_lba_len_flags *lbalen; 8878 uint64_t lba; 8879 uint32_t num_blocks; 8880 int bytchk, dpo; 8881 int retval; 8882 8883 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8884 8885 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8886 8887 bytchk = 0; 8888 dpo = 0; 8889 retval = CTL_RETVAL_COMPLETE; 8890 8891 switch (ctsio->cdb[0]) { 8892 case VERIFY_10: { 8893 struct scsi_verify_10 *cdb; 8894 8895 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8896 if (cdb->byte2 & SVFY_BYTCHK) 8897 bytchk = 1; 8898 if (cdb->byte2 & SVFY_DPO) 8899 dpo = 1; 8900 lba = scsi_4btoul(cdb->addr); 8901 num_blocks = scsi_2btoul(cdb->length); 8902 break; 8903 } 8904 case VERIFY_12: { 8905 struct scsi_verify_12 *cdb; 8906 8907 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8908 if (cdb->byte2 & SVFY_BYTCHK) 8909 bytchk = 1; 8910 if (cdb->byte2 & SVFY_DPO) 8911 dpo = 1; 8912 lba = scsi_4btoul(cdb->addr); 8913 num_blocks = scsi_4btoul(cdb->length); 8914 break; 8915 } 8916 case VERIFY_16: { 8917 struct scsi_rw_16 *cdb; 8918 8919 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8920 if (cdb->byte2 & SVFY_BYTCHK) 8921 bytchk = 1; 8922 if (cdb->byte2 & SVFY_DPO) 8923 dpo = 1; 8924 lba = scsi_8btou64(cdb->addr); 8925 num_blocks = scsi_4btoul(cdb->length); 8926 break; 8927 } 8928 default: 8929 /* 8930 * We got a command we don't support. This shouldn't 8931 * happen, commands should be filtered out above us. 8932 */ 8933 ctl_set_invalid_opcode(ctsio); 8934 ctl_done((union ctl_io *)ctsio); 8935 return (CTL_RETVAL_COMPLETE); 8936 } 8937 8938 /* 8939 * The first check is to make sure we're in bounds, the second 8940 * check is to catch wrap-around problems. If the lba + num blocks 8941 * is less than the lba, then we've wrapped around and the block 8942 * range is invalid anyway. 8943 */ 8944 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8945 || ((lba + num_blocks) < lba)) { 8946 ctl_set_lba_out_of_range(ctsio); 8947 ctl_done((union ctl_io *)ctsio); 8948 return (CTL_RETVAL_COMPLETE); 8949 } 8950 8951 /* 8952 * According to SBC-3, a transfer length of 0 is not an error. 8953 */ 8954 if (num_blocks == 0) { 8955 ctl_set_success(ctsio); 8956 ctl_done((union ctl_io *)ctsio); 8957 return (CTL_RETVAL_COMPLETE); 8958 } 8959 8960 lbalen = (struct ctl_lba_len_flags *) 8961 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8962 lbalen->lba = lba; 8963 lbalen->len = num_blocks; 8964 if (bytchk) { 8965 lbalen->flags = CTL_LLF_COMPARE; 8966 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8967 } else { 8968 lbalen->flags = CTL_LLF_VERIFY; 8969 ctsio->kern_total_len = 0; 8970 } 8971 ctsio->kern_rel_offset = 0; 8972 8973 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8974 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8975 return (retval); 8976} 8977 8978int 8979ctl_report_luns(struct ctl_scsiio *ctsio) 8980{ 8981 struct scsi_report_luns *cdb; 8982 struct scsi_report_luns_data *lun_data; 8983 struct ctl_lun *lun, *request_lun; 8984 int num_luns, retval; 8985 uint32_t alloc_len, lun_datalen; 8986 int num_filled, well_known; 8987 uint32_t initidx, targ_lun_id, lun_id; 8988 8989 retval = CTL_RETVAL_COMPLETE; 8990 well_known = 0; 8991 8992 cdb = (struct scsi_report_luns *)ctsio->cdb; 8993 8994 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8995 8996 mtx_lock(&control_softc->ctl_lock); 8997 num_luns = control_softc->num_luns; 8998 mtx_unlock(&control_softc->ctl_lock); 8999 9000 switch (cdb->select_report) { 9001 case RPL_REPORT_DEFAULT: 9002 case RPL_REPORT_ALL: 9003 break; 9004 case RPL_REPORT_WELLKNOWN: 9005 well_known = 1; 9006 num_luns = 0; 9007 break; 9008 default: 9009 ctl_set_invalid_field(ctsio, 9010 /*sks_valid*/ 1, 9011 /*command*/ 1, 9012 /*field*/ 2, 9013 /*bit_valid*/ 0, 9014 /*bit*/ 0); 9015 ctl_done((union ctl_io *)ctsio); 9016 return (retval); 9017 break; /* NOTREACHED */ 9018 } 9019 9020 alloc_len = scsi_4btoul(cdb->length); 9021 /* 9022 * The initiator has to allocate at least 16 bytes for this request, 9023 * so he can at least get the header and the first LUN. Otherwise 9024 * we reject the request (per SPC-3 rev 14, section 6.21). 9025 */ 9026 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9027 sizeof(struct scsi_report_luns_lundata))) { 9028 ctl_set_invalid_field(ctsio, 9029 /*sks_valid*/ 1, 9030 /*command*/ 1, 9031 /*field*/ 6, 9032 /*bit_valid*/ 0, 9033 /*bit*/ 0); 9034 ctl_done((union ctl_io *)ctsio); 9035 return (retval); 9036 } 9037 9038 request_lun = (struct ctl_lun *) 9039 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9040 9041 lun_datalen = sizeof(*lun_data) + 9042 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9043 9044 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9045 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9046 ctsio->kern_sg_entries = 0; 9047 9048 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9049 9050 mtx_lock(&control_softc->ctl_lock); 9051 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9052 lun_id = targ_lun_id; 9053 if (ctsio->io_hdr.nexus.lun_map_fn != NULL) 9054 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id); 9055 if (lun_id >= CTL_MAX_LUNS) 9056 continue; 9057 lun = control_softc->ctl_luns[lun_id]; 9058 if (lun == NULL) 9059 continue; 9060 9061 if (targ_lun_id <= 0xff) { 9062 /* 9063 * Peripheral addressing method, bus number 0. 9064 */ 9065 lun_data->luns[num_filled].lundata[0] = 9066 RPL_LUNDATA_ATYP_PERIPH; 9067 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9068 num_filled++; 9069 } else if (targ_lun_id <= 0x3fff) { 9070 /* 9071 * Flat addressing method. 9072 */ 9073 lun_data->luns[num_filled].lundata[0] = 9074 RPL_LUNDATA_ATYP_FLAT | 9075 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK); 9076#ifdef OLDCTLHEADERS 9077 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) | 9078 (targ_lun_id & SRLD_BUS_LUN_MASK); 9079#endif 9080 lun_data->luns[num_filled].lundata[1] = 9081#ifdef OLDCTLHEADERS 9082 targ_lun_id >> SRLD_BUS_LUN_BITS; 9083#endif 9084 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS; 9085 num_filled++; 9086 } else { 9087 printf("ctl_report_luns: bogus LUN number %jd, " 9088 "skipping\n", (intmax_t)targ_lun_id); 9089 } 9090 /* 9091 * According to SPC-3, rev 14 section 6.21: 9092 * 9093 * "The execution of a REPORT LUNS command to any valid and 9094 * installed logical unit shall clear the REPORTED LUNS DATA 9095 * HAS CHANGED unit attention condition for all logical 9096 * units of that target with respect to the requesting 9097 * initiator. A valid and installed logical unit is one 9098 * having a PERIPHERAL QUALIFIER of 000b in the standard 9099 * INQUIRY data (see 6.4.2)." 9100 * 9101 * If request_lun is NULL, the LUN this report luns command 9102 * was issued to is either disabled or doesn't exist. In that 9103 * case, we shouldn't clear any pending lun change unit 9104 * attention. 9105 */ 9106 if (request_lun != NULL) { 9107 mtx_lock(&lun->lun_lock); 9108 lun->pending_sense[initidx].ua_pending &= 9109 ~CTL_UA_LUN_CHANGE; 9110 mtx_unlock(&lun->lun_lock); 9111 } 9112 } 9113 mtx_unlock(&control_softc->ctl_lock); 9114 9115 /* 9116 * It's quite possible that we've returned fewer LUNs than we allocated 9117 * space for. Trim it. 9118 */ 9119 lun_datalen = sizeof(*lun_data) + 9120 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9121 9122 if (lun_datalen < alloc_len) { 9123 ctsio->residual = alloc_len - lun_datalen; 9124 ctsio->kern_data_len = lun_datalen; 9125 ctsio->kern_total_len = lun_datalen; 9126 } else { 9127 ctsio->residual = 0; 9128 ctsio->kern_data_len = alloc_len; 9129 ctsio->kern_total_len = alloc_len; 9130 } 9131 ctsio->kern_data_resid = 0; 9132 ctsio->kern_rel_offset = 0; 9133 ctsio->kern_sg_entries = 0; 9134 9135 /* 9136 * We set this to the actual data length, regardless of how much 9137 * space we actually have to return results. If the user looks at 9138 * this value, he'll know whether or not he allocated enough space 9139 * and reissue the command if necessary. We don't support well 9140 * known logical units, so if the user asks for that, return none. 9141 */ 9142 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9143 9144 /* 9145 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9146 * this request. 9147 */ 9148 ctsio->scsi_status = SCSI_STATUS_OK; 9149 9150 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9151 ctsio->be_move_done = ctl_config_move_done; 9152 ctl_datamove((union ctl_io *)ctsio); 9153 9154 return (retval); 9155} 9156 9157int 9158ctl_request_sense(struct ctl_scsiio *ctsio) 9159{ 9160 struct scsi_request_sense *cdb; 9161 struct scsi_sense_data *sense_ptr; 9162 struct ctl_lun *lun; 9163 uint32_t initidx; 9164 int have_error; 9165 scsi_sense_data_type sense_format; 9166 9167 cdb = (struct scsi_request_sense *)ctsio->cdb; 9168 9169 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9170 9171 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9172 9173 /* 9174 * Determine which sense format the user wants. 9175 */ 9176 if (cdb->byte2 & SRS_DESC) 9177 sense_format = SSD_TYPE_DESC; 9178 else 9179 sense_format = SSD_TYPE_FIXED; 9180 9181 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9182 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9183 ctsio->kern_sg_entries = 0; 9184 9185 /* 9186 * struct scsi_sense_data, which is currently set to 256 bytes, is 9187 * larger than the largest allowed value for the length field in the 9188 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9189 */ 9190 ctsio->residual = 0; 9191 ctsio->kern_data_len = cdb->length; 9192 ctsio->kern_total_len = cdb->length; 9193 9194 ctsio->kern_data_resid = 0; 9195 ctsio->kern_rel_offset = 0; 9196 ctsio->kern_sg_entries = 0; 9197 9198 /* 9199 * If we don't have a LUN, we don't have any pending sense. 9200 */ 9201 if (lun == NULL) 9202 goto no_sense; 9203 9204 have_error = 0; 9205 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9206 /* 9207 * Check for pending sense, and then for pending unit attentions. 9208 * Pending sense gets returned first, then pending unit attentions. 9209 */ 9210 mtx_lock(&lun->lun_lock); 9211 if (ctl_is_set(lun->have_ca, initidx)) { 9212 scsi_sense_data_type stored_format; 9213 9214 /* 9215 * Check to see which sense format was used for the stored 9216 * sense data. 9217 */ 9218 stored_format = scsi_sense_type( 9219 &lun->pending_sense[initidx].sense); 9220 9221 /* 9222 * If the user requested a different sense format than the 9223 * one we stored, then we need to convert it to the other 9224 * format. If we're going from descriptor to fixed format 9225 * sense data, we may lose things in translation, depending 9226 * on what options were used. 9227 * 9228 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9229 * for some reason we'll just copy it out as-is. 9230 */ 9231 if ((stored_format == SSD_TYPE_FIXED) 9232 && (sense_format == SSD_TYPE_DESC)) 9233 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9234 &lun->pending_sense[initidx].sense, 9235 (struct scsi_sense_data_desc *)sense_ptr); 9236 else if ((stored_format == SSD_TYPE_DESC) 9237 && (sense_format == SSD_TYPE_FIXED)) 9238 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9239 &lun->pending_sense[initidx].sense, 9240 (struct scsi_sense_data_fixed *)sense_ptr); 9241 else 9242 memcpy(sense_ptr, &lun->pending_sense[initidx].sense, 9243 ctl_min(sizeof(*sense_ptr), 9244 sizeof(lun->pending_sense[initidx].sense))); 9245 9246 ctl_clear_mask(lun->have_ca, initidx); 9247 have_error = 1; 9248 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) { 9249 ctl_ua_type ua_type; 9250 9251 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending, 9252 sense_ptr, sense_format); 9253 if (ua_type != CTL_UA_NONE) { 9254 have_error = 1; 9255 /* We're reporting this UA, so clear it */ 9256 lun->pending_sense[initidx].ua_pending &= ~ua_type; 9257 } 9258 } 9259 mtx_unlock(&lun->lun_lock); 9260 9261 /* 9262 * We already have a pending error, return it. 9263 */ 9264 if (have_error != 0) { 9265 /* 9266 * We report the SCSI status as OK, since the status of the 9267 * request sense command itself is OK. 9268 */ 9269 ctsio->scsi_status = SCSI_STATUS_OK; 9270 9271 /* 9272 * We report 0 for the sense length, because we aren't doing 9273 * autosense in this case. We're reporting sense as 9274 * parameter data. 9275 */ 9276 ctsio->sense_len = 0; 9277 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9278 ctsio->be_move_done = ctl_config_move_done; 9279 ctl_datamove((union ctl_io *)ctsio); 9280 9281 return (CTL_RETVAL_COMPLETE); 9282 } 9283 9284no_sense: 9285 9286 /* 9287 * No sense information to report, so we report that everything is 9288 * okay. 9289 */ 9290 ctl_set_sense_data(sense_ptr, 9291 lun, 9292 sense_format, 9293 /*current_error*/ 1, 9294 /*sense_key*/ SSD_KEY_NO_SENSE, 9295 /*asc*/ 0x00, 9296 /*ascq*/ 0x00, 9297 SSD_ELEM_NONE); 9298 9299 ctsio->scsi_status = SCSI_STATUS_OK; 9300 9301 /* 9302 * We report 0 for the sense length, because we aren't doing 9303 * autosense in this case. We're reporting sense as parameter data. 9304 */ 9305 ctsio->sense_len = 0; 9306 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9307 ctsio->be_move_done = ctl_config_move_done; 9308 ctl_datamove((union ctl_io *)ctsio); 9309 9310 return (CTL_RETVAL_COMPLETE); 9311} 9312 9313int 9314ctl_tur(struct ctl_scsiio *ctsio) 9315{ 9316 struct ctl_lun *lun; 9317 9318 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9319 9320 CTL_DEBUG_PRINT(("ctl_tur\n")); 9321 9322 if (lun == NULL) 9323 return (EINVAL); 9324 9325 ctsio->scsi_status = SCSI_STATUS_OK; 9326 ctsio->io_hdr.status = CTL_SUCCESS; 9327 9328 ctl_done((union ctl_io *)ctsio); 9329 9330 return (CTL_RETVAL_COMPLETE); 9331} 9332 9333#ifdef notyet 9334static int 9335ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9336{ 9337 9338} 9339#endif 9340 9341static int 9342ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9343{ 9344 struct scsi_vpd_supported_pages *pages; 9345 int sup_page_size; 9346 struct ctl_lun *lun; 9347 9348 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9349 9350 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9351 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9352 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9353 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9354 ctsio->kern_sg_entries = 0; 9355 9356 if (sup_page_size < alloc_len) { 9357 ctsio->residual = alloc_len - sup_page_size; 9358 ctsio->kern_data_len = sup_page_size; 9359 ctsio->kern_total_len = sup_page_size; 9360 } else { 9361 ctsio->residual = 0; 9362 ctsio->kern_data_len = alloc_len; 9363 ctsio->kern_total_len = alloc_len; 9364 } 9365 ctsio->kern_data_resid = 0; 9366 ctsio->kern_rel_offset = 0; 9367 ctsio->kern_sg_entries = 0; 9368 9369 /* 9370 * The control device is always connected. The disk device, on the 9371 * other hand, may not be online all the time. Need to change this 9372 * to figure out whether the disk device is actually online or not. 9373 */ 9374 if (lun != NULL) 9375 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9376 lun->be_lun->lun_type; 9377 else 9378 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9379 9380 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9381 /* Supported VPD pages */ 9382 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9383 /* Serial Number */ 9384 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9385 /* Device Identification */ 9386 pages->page_list[2] = SVPD_DEVICE_ID; 9387 /* Block limits */ 9388 pages->page_list[3] = SVPD_BLOCK_LIMITS; 9389 /* Logical Block Provisioning */ 9390 pages->page_list[4] = SVPD_LBP; 9391 9392 ctsio->scsi_status = SCSI_STATUS_OK; 9393 9394 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9395 ctsio->be_move_done = ctl_config_move_done; 9396 ctl_datamove((union ctl_io *)ctsio); 9397 9398 return (CTL_RETVAL_COMPLETE); 9399} 9400 9401static int 9402ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9403{ 9404 struct scsi_vpd_unit_serial_number *sn_ptr; 9405 struct ctl_lun *lun; 9406 9407 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9408 9409 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO); 9410 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9411 ctsio->kern_sg_entries = 0; 9412 9413 if (sizeof(*sn_ptr) < alloc_len) { 9414 ctsio->residual = alloc_len - sizeof(*sn_ptr); 9415 ctsio->kern_data_len = sizeof(*sn_ptr); 9416 ctsio->kern_total_len = sizeof(*sn_ptr); 9417 } else { 9418 ctsio->residual = 0; 9419 ctsio->kern_data_len = alloc_len; 9420 ctsio->kern_total_len = alloc_len; 9421 } 9422 ctsio->kern_data_resid = 0; 9423 ctsio->kern_rel_offset = 0; 9424 ctsio->kern_sg_entries = 0; 9425 9426 /* 9427 * The control device is always connected. The disk device, on the 9428 * other hand, may not be online all the time. Need to change this 9429 * to figure out whether the disk device is actually online or not. 9430 */ 9431 if (lun != NULL) 9432 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9433 lun->be_lun->lun_type; 9434 else 9435 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9436 9437 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9438 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN); 9439 /* 9440 * If we don't have a LUN, we just leave the serial number as 9441 * all spaces. 9442 */ 9443 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num)); 9444 if (lun != NULL) { 9445 strncpy((char *)sn_ptr->serial_num, 9446 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9447 } 9448 ctsio->scsi_status = SCSI_STATUS_OK; 9449 9450 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9451 ctsio->be_move_done = ctl_config_move_done; 9452 ctl_datamove((union ctl_io *)ctsio); 9453 9454 return (CTL_RETVAL_COMPLETE); 9455} 9456 9457 9458static int 9459ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9460{ 9461 struct scsi_vpd_device_id *devid_ptr; 9462 struct scsi_vpd_id_descriptor *desc, *desc1; 9463 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */ 9464 struct scsi_vpd_id_t10 *t10id; 9465 struct ctl_softc *ctl_softc; 9466 struct ctl_lun *lun; 9467 struct ctl_frontend *fe; 9468 char *val; 9469 int data_len, devid_len; 9470 9471 ctl_softc = control_softc; 9472 9473 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9474 9475 if (fe->devid != NULL) 9476 return ((fe->devid)(ctsio, alloc_len)); 9477 9478 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9479 9480 if (lun == NULL) { 9481 devid_len = CTL_DEVID_MIN_LEN; 9482 } else { 9483 devid_len = max(CTL_DEVID_MIN_LEN, 9484 strnlen(lun->be_lun->device_id, CTL_DEVID_LEN)); 9485 } 9486 9487 data_len = sizeof(struct scsi_vpd_device_id) + 9488 sizeof(struct scsi_vpd_id_descriptor) + 9489 sizeof(struct scsi_vpd_id_t10) + devid_len + 9490 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN + 9491 sizeof(struct scsi_vpd_id_descriptor) + 9492 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9493 sizeof(struct scsi_vpd_id_descriptor) + 9494 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9495 9496 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9497 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9498 ctsio->kern_sg_entries = 0; 9499 9500 if (data_len < alloc_len) { 9501 ctsio->residual = alloc_len - data_len; 9502 ctsio->kern_data_len = data_len; 9503 ctsio->kern_total_len = data_len; 9504 } else { 9505 ctsio->residual = 0; 9506 ctsio->kern_data_len = alloc_len; 9507 ctsio->kern_total_len = alloc_len; 9508 } 9509 ctsio->kern_data_resid = 0; 9510 ctsio->kern_rel_offset = 0; 9511 ctsio->kern_sg_entries = 0; 9512 9513 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9514 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 9515 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9516 sizeof(struct scsi_vpd_id_t10) + devid_len); 9517 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] + 9518 CTL_WWPN_LEN); 9519 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] + 9520 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9521 9522 /* 9523 * The control device is always connected. The disk device, on the 9524 * other hand, may not be online all the time. 9525 */ 9526 if (lun != NULL) 9527 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9528 lun->be_lun->lun_type; 9529 else 9530 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9531 9532 devid_ptr->page_code = SVPD_DEVICE_ID; 9533 9534 scsi_ulto2b(data_len - 4, devid_ptr->length); 9535 9536 /* 9537 * For Fibre channel, 9538 */ 9539 if (fe->port_type == CTL_PORT_FC) 9540 { 9541 desc->proto_codeset = (SCSI_PROTO_FC << 4) | 9542 SVPD_ID_CODESET_ASCII; 9543 desc1->proto_codeset = (SCSI_PROTO_FC << 4) | 9544 SVPD_ID_CODESET_BINARY; 9545 } 9546 else 9547 { 9548 desc->proto_codeset = (SCSI_PROTO_SPI << 4) | 9549 SVPD_ID_CODESET_ASCII; 9550 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) | 9551 SVPD_ID_CODESET_BINARY; 9552 } 9553 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset; 9554 9555 /* 9556 * We're using a LUN association here. i.e., this device ID is a 9557 * per-LUN identifier. 9558 */ 9559 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 9560 desc->length = sizeof(*t10id) + devid_len; 9561 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) { 9562 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 9563 } else { 9564 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 9565 strncpy(t10id->vendor, val, 9566 min(sizeof(t10id->vendor), strlen(val))); 9567 } 9568 9569 /* 9570 * desc1 is for the WWPN which is a port asscociation. 9571 */ 9572 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA; 9573 desc1->length = CTL_WWPN_LEN; 9574 /* XXX Call Reggie's get_WWNN func here then add port # to the end */ 9575 /* For testing just create the WWPN */ 9576#if 0 9577 ddb_GetWWNN((char *)desc1->identifier); 9578 9579 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9580 /* This is so Copancontrol will return something sane */ 9581 if (ctsio->io_hdr.nexus.targ_port!=0 && 9582 ctsio->io_hdr.nexus.targ_port!=8) 9583 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1; 9584 else 9585 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port; 9586#endif 9587 9588 be64enc(desc1->identifier, fe->wwpn); 9589 9590 /* 9591 * desc2 is for the Relative Target Port(type 4h) identifier 9592 */ 9593 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9594 | SVPD_ID_TYPE_RELTARG; 9595 desc2->length = 4; 9596//#if 0 9597 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */ 9598 /* This is so Copancontrol will return something sane */ 9599 if (ctsio->io_hdr.nexus.targ_port!=0 && 9600 ctsio->io_hdr.nexus.targ_port!=8) 9601 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1; 9602 else 9603 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port; 9604//#endif 9605 9606 /* 9607 * desc3 is for the Target Port Group(type 5h) identifier 9608 */ 9609 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT 9610 | SVPD_ID_TYPE_TPORTGRP; 9611 desc3->length = 4; 9612 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single) 9613 desc3->identifier[3] = 1; 9614 else 9615 desc3->identifier[3] = 2; 9616 9617 /* 9618 * If we've actually got a backend, copy the device id from the 9619 * per-LUN data. Otherwise, set it to all spaces. 9620 */ 9621 if (lun != NULL) { 9622 /* 9623 * Copy the backend's LUN ID. 9624 */ 9625 strncpy((char *)t10id->vendor_spec_id, 9626 (char *)lun->be_lun->device_id, devid_len); 9627 } else { 9628 /* 9629 * No backend, set this to spaces. 9630 */ 9631 memset(t10id->vendor_spec_id, 0x20, devid_len); 9632 } 9633 9634 ctsio->scsi_status = SCSI_STATUS_OK; 9635 9636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9637 ctsio->be_move_done = ctl_config_move_done; 9638 ctl_datamove((union ctl_io *)ctsio); 9639 9640 return (CTL_RETVAL_COMPLETE); 9641} 9642 9643static int 9644ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9645{ 9646 struct scsi_vpd_block_limits *bl_ptr; 9647 struct ctl_lun *lun; 9648 int bs; 9649 9650 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9651 bs = lun->be_lun->blocksize; 9652 9653 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9654 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9655 ctsio->kern_sg_entries = 0; 9656 9657 if (sizeof(*bl_ptr) < alloc_len) { 9658 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9659 ctsio->kern_data_len = sizeof(*bl_ptr); 9660 ctsio->kern_total_len = sizeof(*bl_ptr); 9661 } else { 9662 ctsio->residual = 0; 9663 ctsio->kern_data_len = alloc_len; 9664 ctsio->kern_total_len = alloc_len; 9665 } 9666 ctsio->kern_data_resid = 0; 9667 ctsio->kern_rel_offset = 0; 9668 ctsio->kern_sg_entries = 0; 9669 9670 /* 9671 * The control device is always connected. The disk device, on the 9672 * other hand, may not be online all the time. Need to change this 9673 * to figure out whether the disk device is actually online or not. 9674 */ 9675 if (lun != NULL) 9676 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9677 lun->be_lun->lun_type; 9678 else 9679 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9680 9681 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9682 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length); 9683 bl_ptr->max_cmp_write_len = 0xff; 9684 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9685 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 9686 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9687 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9688 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9689 } 9690 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9691 9692 ctsio->scsi_status = SCSI_STATUS_OK; 9693 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9694 ctsio->be_move_done = ctl_config_move_done; 9695 ctl_datamove((union ctl_io *)ctsio); 9696 9697 return (CTL_RETVAL_COMPLETE); 9698} 9699 9700static int 9701ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9702{ 9703 struct scsi_vpd_logical_block_prov *lbp_ptr; 9704 struct ctl_lun *lun; 9705 int bs; 9706 9707 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9708 bs = lun->be_lun->blocksize; 9709 9710 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9711 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9712 ctsio->kern_sg_entries = 0; 9713 9714 if (sizeof(*lbp_ptr) < alloc_len) { 9715 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9716 ctsio->kern_data_len = sizeof(*lbp_ptr); 9717 ctsio->kern_total_len = sizeof(*lbp_ptr); 9718 } else { 9719 ctsio->residual = 0; 9720 ctsio->kern_data_len = alloc_len; 9721 ctsio->kern_total_len = alloc_len; 9722 } 9723 ctsio->kern_data_resid = 0; 9724 ctsio->kern_rel_offset = 0; 9725 ctsio->kern_sg_entries = 0; 9726 9727 /* 9728 * The control device is always connected. The disk device, on the 9729 * other hand, may not be online all the time. Need to change this 9730 * to figure out whether the disk device is actually online or not. 9731 */ 9732 if (lun != NULL) 9733 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9734 lun->be_lun->lun_type; 9735 else 9736 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9737 9738 lbp_ptr->page_code = SVPD_LBP; 9739 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 9740 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10; 9741 9742 ctsio->scsi_status = SCSI_STATUS_OK; 9743 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9744 ctsio->be_move_done = ctl_config_move_done; 9745 ctl_datamove((union ctl_io *)ctsio); 9746 9747 return (CTL_RETVAL_COMPLETE); 9748} 9749 9750static int 9751ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9752{ 9753 struct scsi_inquiry *cdb; 9754 struct ctl_lun *lun; 9755 int alloc_len, retval; 9756 9757 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9758 cdb = (struct scsi_inquiry *)ctsio->cdb; 9759 9760 retval = CTL_RETVAL_COMPLETE; 9761 9762 alloc_len = scsi_2btoul(cdb->length); 9763 9764 switch (cdb->page_code) { 9765 case SVPD_SUPPORTED_PAGES: 9766 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 9767 break; 9768 case SVPD_UNIT_SERIAL_NUMBER: 9769 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 9770 break; 9771 case SVPD_DEVICE_ID: 9772 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 9773 break; 9774 case SVPD_BLOCK_LIMITS: 9775 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 9776 break; 9777 case SVPD_LBP: 9778 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 9779 break; 9780 default: 9781 ctl_set_invalid_field(ctsio, 9782 /*sks_valid*/ 1, 9783 /*command*/ 1, 9784 /*field*/ 2, 9785 /*bit_valid*/ 0, 9786 /*bit*/ 0); 9787 ctl_done((union ctl_io *)ctsio); 9788 retval = CTL_RETVAL_COMPLETE; 9789 break; 9790 } 9791 9792 return (retval); 9793} 9794 9795static int 9796ctl_inquiry_std(struct ctl_scsiio *ctsio) 9797{ 9798 struct scsi_inquiry_data *inq_ptr; 9799 struct scsi_inquiry *cdb; 9800 struct ctl_softc *ctl_softc; 9801 struct ctl_lun *lun; 9802 char *val; 9803 uint32_t alloc_len; 9804 int is_fc; 9805 9806 ctl_softc = control_softc; 9807 9808 /* 9809 * Figure out whether we're talking to a Fibre Channel port or not. 9810 * We treat the ioctl front end, and any SCSI adapters, as packetized 9811 * SCSI front ends. 9812 */ 9813 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type != 9814 CTL_PORT_FC) 9815 is_fc = 0; 9816 else 9817 is_fc = 1; 9818 9819 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9820 cdb = (struct scsi_inquiry *)ctsio->cdb; 9821 alloc_len = scsi_2btoul(cdb->length); 9822 9823 /* 9824 * We malloc the full inquiry data size here and fill it 9825 * in. If the user only asks for less, we'll give him 9826 * that much. 9827 */ 9828 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO); 9829 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 9830 ctsio->kern_sg_entries = 0; 9831 ctsio->kern_data_resid = 0; 9832 ctsio->kern_rel_offset = 0; 9833 9834 if (sizeof(*inq_ptr) < alloc_len) { 9835 ctsio->residual = alloc_len - sizeof(*inq_ptr); 9836 ctsio->kern_data_len = sizeof(*inq_ptr); 9837 ctsio->kern_total_len = sizeof(*inq_ptr); 9838 } else { 9839 ctsio->residual = 0; 9840 ctsio->kern_data_len = alloc_len; 9841 ctsio->kern_total_len = alloc_len; 9842 } 9843 9844 /* 9845 * If we have a LUN configured, report it as connected. Otherwise, 9846 * report that it is offline or no device is supported, depending 9847 * on the value of inquiry_pq_no_lun. 9848 * 9849 * According to the spec (SPC-4 r34), the peripheral qualifier 9850 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 9851 * 9852 * "A peripheral device having the specified peripheral device type 9853 * is not connected to this logical unit. However, the device 9854 * server is capable of supporting the specified peripheral device 9855 * type on this logical unit." 9856 * 9857 * According to the same spec, the peripheral qualifier 9858 * SID_QUAL_BAD_LU (011b) is used in this scenario: 9859 * 9860 * "The device server is not capable of supporting a peripheral 9861 * device on this logical unit. For this peripheral qualifier the 9862 * peripheral device type shall be set to 1Fh. All other peripheral 9863 * device type values are reserved for this peripheral qualifier." 9864 * 9865 * Given the text, it would seem that we probably want to report that 9866 * the LUN is offline here. There is no LUN connected, but we can 9867 * support a LUN at the given LUN number. 9868 * 9869 * In the real world, though, it sounds like things are a little 9870 * different: 9871 * 9872 * - Linux, when presented with a LUN with the offline peripheral 9873 * qualifier, will create an sg driver instance for it. So when 9874 * you attach it to CTL, you wind up with a ton of sg driver 9875 * instances. (One for every LUN that Linux bothered to probe.) 9876 * Linux does this despite the fact that it issues a REPORT LUNs 9877 * to LUN 0 to get the inventory of supported LUNs. 9878 * 9879 * - There is other anecdotal evidence (from Emulex folks) about 9880 * arrays that use the offline peripheral qualifier for LUNs that 9881 * are on the "passive" path in an active/passive array. 9882 * 9883 * So the solution is provide a hopefully reasonable default 9884 * (return bad/no LUN) and allow the user to change the behavior 9885 * with a tunable/sysctl variable. 9886 */ 9887 if (lun != NULL) 9888 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9889 lun->be_lun->lun_type; 9890 else if (ctl_softc->inquiry_pq_no_lun == 0) 9891 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9892 else 9893 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 9894 9895 /* RMB in byte 2 is 0 */ 9896 inq_ptr->version = SCSI_REV_SPC3; 9897 9898 /* 9899 * According to SAM-3, even if a device only supports a single 9900 * level of LUN addressing, it should still set the HISUP bit: 9901 * 9902 * 4.9.1 Logical unit numbers overview 9903 * 9904 * All logical unit number formats described in this standard are 9905 * hierarchical in structure even when only a single level in that 9906 * hierarchy is used. The HISUP bit shall be set to one in the 9907 * standard INQUIRY data (see SPC-2) when any logical unit number 9908 * format described in this standard is used. Non-hierarchical 9909 * formats are outside the scope of this standard. 9910 * 9911 * Therefore we set the HiSup bit here. 9912 * 9913 * The reponse format is 2, per SPC-3. 9914 */ 9915 inq_ptr->response_format = SID_HiSup | 2; 9916 9917 inq_ptr->additional_length = sizeof(*inq_ptr) - 4; 9918 CTL_DEBUG_PRINT(("additional_length = %d\n", 9919 inq_ptr->additional_length)); 9920 9921 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT; 9922 /* 16 bit addressing */ 9923 if (is_fc == 0) 9924 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 9925 /* XXX set the SID_MultiP bit here if we're actually going to 9926 respond on multiple ports */ 9927 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 9928 9929 /* 16 bit data bus, synchronous transfers */ 9930 /* XXX these flags don't apply for FC */ 9931 if (is_fc == 0) 9932 inq_ptr->flags = SID_WBus16 | SID_Sync; 9933 /* 9934 * XXX KDM do we want to support tagged queueing on the control 9935 * device at all? 9936 */ 9937 if ((lun == NULL) 9938 || (lun->be_lun->lun_type != T_PROCESSOR)) 9939 inq_ptr->flags |= SID_CmdQue; 9940 /* 9941 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 9942 * We have 8 bytes for the vendor name, and 16 bytes for the device 9943 * name and 4 bytes for the revision. 9944 */ 9945 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "vendor")) == NULL) { 9946 strcpy(inq_ptr->vendor, CTL_VENDOR); 9947 } else { 9948 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 9949 strncpy(inq_ptr->vendor, val, 9950 min(sizeof(inq_ptr->vendor), strlen(val))); 9951 } 9952 if (lun == NULL) { 9953 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 9954 } else if ((val = ctl_get_opt(lun->be_lun, "product")) == NULL) { 9955 switch (lun->be_lun->lun_type) { 9956 case T_DIRECT: 9957 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT); 9958 break; 9959 case T_PROCESSOR: 9960 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT); 9961 break; 9962 default: 9963 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT); 9964 break; 9965 } 9966 } else { 9967 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 9968 strncpy(inq_ptr->product, val, 9969 min(sizeof(inq_ptr->product), strlen(val))); 9970 } 9971 9972 /* 9973 * XXX make this a macro somewhere so it automatically gets 9974 * incremented when we make changes. 9975 */ 9976 if (lun == NULL || (val = ctl_get_opt(lun->be_lun, "revision")) == NULL) { 9977 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 9978 } else { 9979 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 9980 strncpy(inq_ptr->revision, val, 9981 min(sizeof(inq_ptr->revision), strlen(val))); 9982 } 9983 9984 /* 9985 * For parallel SCSI, we support double transition and single 9986 * transition clocking. We also support QAS (Quick Arbitration 9987 * and Selection) and Information Unit transfers on both the 9988 * control and array devices. 9989 */ 9990 if (is_fc == 0) 9991 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 9992 SID_SPI_IUS; 9993 9994 /* SAM-3 */ 9995 scsi_ulto2b(0x0060, inq_ptr->version1); 9996 /* SPC-3 (no version claimed) XXX should we claim a version? */ 9997 scsi_ulto2b(0x0300, inq_ptr->version2); 9998 if (is_fc) { 9999 /* FCP-2 ANSI INCITS.350:2003 */ 10000 scsi_ulto2b(0x0917, inq_ptr->version3); 10001 } else { 10002 /* SPI-4 ANSI INCITS.362:200x */ 10003 scsi_ulto2b(0x0B56, inq_ptr->version3); 10004 } 10005 10006 if (lun == NULL) { 10007 /* SBC-2 (no version claimed) XXX should we claim a version? */ 10008 scsi_ulto2b(0x0320, inq_ptr->version4); 10009 } else { 10010 switch (lun->be_lun->lun_type) { 10011 case T_DIRECT: 10012 /* 10013 * SBC-2 (no version claimed) XXX should we claim a 10014 * version? 10015 */ 10016 scsi_ulto2b(0x0320, inq_ptr->version4); 10017 break; 10018 case T_PROCESSOR: 10019 default: 10020 break; 10021 } 10022 } 10023 10024 ctsio->scsi_status = SCSI_STATUS_OK; 10025 if (ctsio->kern_data_len > 0) { 10026 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10027 ctsio->be_move_done = ctl_config_move_done; 10028 ctl_datamove((union ctl_io *)ctsio); 10029 } else { 10030 ctsio->io_hdr.status = CTL_SUCCESS; 10031 ctl_done((union ctl_io *)ctsio); 10032 } 10033 10034 return (CTL_RETVAL_COMPLETE); 10035} 10036 10037int 10038ctl_inquiry(struct ctl_scsiio *ctsio) 10039{ 10040 struct scsi_inquiry *cdb; 10041 int retval; 10042 10043 cdb = (struct scsi_inquiry *)ctsio->cdb; 10044 10045 retval = 0; 10046 10047 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10048 10049 /* 10050 * Right now, we don't support the CmdDt inquiry information. 10051 * This would be nice to support in the future. When we do 10052 * support it, we should change this test so that it checks to make 10053 * sure SI_EVPD and SI_CMDDT aren't both set at the same time. 10054 */ 10055#ifdef notyet 10056 if (((cdb->byte2 & SI_EVPD) 10057 && (cdb->byte2 & SI_CMDDT))) 10058#endif 10059 if (cdb->byte2 & SI_CMDDT) { 10060 /* 10061 * Point to the SI_CMDDT bit. We might change this 10062 * when we support SI_CMDDT, but since both bits would be 10063 * "wrong", this should probably just stay as-is then. 10064 */ 10065 ctl_set_invalid_field(ctsio, 10066 /*sks_valid*/ 1, 10067 /*command*/ 1, 10068 /*field*/ 1, 10069 /*bit_valid*/ 1, 10070 /*bit*/ 1); 10071 ctl_done((union ctl_io *)ctsio); 10072 return (CTL_RETVAL_COMPLETE); 10073 } 10074 if (cdb->byte2 & SI_EVPD) 10075 retval = ctl_inquiry_evpd(ctsio); 10076#ifdef notyet 10077 else if (cdb->byte2 & SI_CMDDT) 10078 retval = ctl_inquiry_cmddt(ctsio); 10079#endif 10080 else 10081 retval = ctl_inquiry_std(ctsio); 10082 10083 return (retval); 10084} 10085 10086/* 10087 * For known CDB types, parse the LBA and length. 10088 */ 10089static int 10090ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len) 10091{ 10092 if (io->io_hdr.io_type != CTL_IO_SCSI) 10093 return (1); 10094 10095 switch (io->scsiio.cdb[0]) { 10096 case COMPARE_AND_WRITE: { 10097 struct scsi_compare_and_write *cdb; 10098 10099 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10100 10101 *lba = scsi_8btou64(cdb->addr); 10102 *len = cdb->length; 10103 break; 10104 } 10105 case READ_6: 10106 case WRITE_6: { 10107 struct scsi_rw_6 *cdb; 10108 10109 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10110 10111 *lba = scsi_3btoul(cdb->addr); 10112 /* only 5 bits are valid in the most significant address byte */ 10113 *lba &= 0x1fffff; 10114 *len = cdb->length; 10115 break; 10116 } 10117 case READ_10: 10118 case WRITE_10: { 10119 struct scsi_rw_10 *cdb; 10120 10121 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10122 10123 *lba = scsi_4btoul(cdb->addr); 10124 *len = scsi_2btoul(cdb->length); 10125 break; 10126 } 10127 case WRITE_VERIFY_10: { 10128 struct scsi_write_verify_10 *cdb; 10129 10130 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10131 10132 *lba = scsi_4btoul(cdb->addr); 10133 *len = scsi_2btoul(cdb->length); 10134 break; 10135 } 10136 case READ_12: 10137 case WRITE_12: { 10138 struct scsi_rw_12 *cdb; 10139 10140 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10141 10142 *lba = scsi_4btoul(cdb->addr); 10143 *len = scsi_4btoul(cdb->length); 10144 break; 10145 } 10146 case WRITE_VERIFY_12: { 10147 struct scsi_write_verify_12 *cdb; 10148 10149 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10150 10151 *lba = scsi_4btoul(cdb->addr); 10152 *len = scsi_4btoul(cdb->length); 10153 break; 10154 } 10155 case READ_16: 10156 case WRITE_16: { 10157 struct scsi_rw_16 *cdb; 10158 10159 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10160 10161 *lba = scsi_8btou64(cdb->addr); 10162 *len = scsi_4btoul(cdb->length); 10163 break; 10164 } 10165 case WRITE_VERIFY_16: { 10166 struct scsi_write_verify_16 *cdb; 10167 10168 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10169 10170 10171 *lba = scsi_8btou64(cdb->addr); 10172 *len = scsi_4btoul(cdb->length); 10173 break; 10174 } 10175 case WRITE_SAME_10: { 10176 struct scsi_write_same_10 *cdb; 10177 10178 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10179 10180 *lba = scsi_4btoul(cdb->addr); 10181 *len = scsi_2btoul(cdb->length); 10182 break; 10183 } 10184 case WRITE_SAME_16: { 10185 struct scsi_write_same_16 *cdb; 10186 10187 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10188 10189 *lba = scsi_8btou64(cdb->addr); 10190 *len = scsi_4btoul(cdb->length); 10191 break; 10192 } 10193 case VERIFY_10: { 10194 struct scsi_verify_10 *cdb; 10195 10196 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10197 10198 *lba = scsi_4btoul(cdb->addr); 10199 *len = scsi_2btoul(cdb->length); 10200 break; 10201 } 10202 case VERIFY_12: { 10203 struct scsi_verify_12 *cdb; 10204 10205 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10206 10207 *lba = scsi_4btoul(cdb->addr); 10208 *len = scsi_4btoul(cdb->length); 10209 break; 10210 } 10211 case VERIFY_16: { 10212 struct scsi_verify_16 *cdb; 10213 10214 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10215 10216 *lba = scsi_8btou64(cdb->addr); 10217 *len = scsi_4btoul(cdb->length); 10218 break; 10219 } 10220 default: 10221 return (1); 10222 break; /* NOTREACHED */ 10223 } 10224 10225 return (0); 10226} 10227 10228static ctl_action 10229ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2) 10230{ 10231 uint64_t endlba1, endlba2; 10232 10233 endlba1 = lba1 + len1 - 1; 10234 endlba2 = lba2 + len2 - 1; 10235 10236 if ((endlba1 < lba2) 10237 || (endlba2 < lba1)) 10238 return (CTL_ACTION_PASS); 10239 else 10240 return (CTL_ACTION_BLOCK); 10241} 10242 10243static ctl_action 10244ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10245{ 10246 uint64_t lba1, lba2; 10247 uint32_t len1, len2; 10248 int retval; 10249 10250 retval = ctl_get_lba_len(io1, &lba1, &len1); 10251 if (retval != 0) 10252 return (CTL_ACTION_ERROR); 10253 10254 retval = ctl_get_lba_len(io2, &lba2, &len2); 10255 if (retval != 0) 10256 return (CTL_ACTION_ERROR); 10257 10258 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10259} 10260 10261static ctl_action 10262ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io) 10263{ 10264 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10265 ctl_serialize_action *serialize_row; 10266 10267 /* 10268 * The initiator attempted multiple untagged commands at the same 10269 * time. Can't do that. 10270 */ 10271 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10272 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10273 && ((pending_io->io_hdr.nexus.targ_port == 10274 ooa_io->io_hdr.nexus.targ_port) 10275 && (pending_io->io_hdr.nexus.initid.id == 10276 ooa_io->io_hdr.nexus.initid.id)) 10277 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10278 return (CTL_ACTION_OVERLAP); 10279 10280 /* 10281 * The initiator attempted to send multiple tagged commands with 10282 * the same ID. (It's fine if different initiators have the same 10283 * tag ID.) 10284 * 10285 * Even if all of those conditions are true, we don't kill the I/O 10286 * if the command ahead of us has been aborted. We won't end up 10287 * sending it to the FETD, and it's perfectly legal to resend a 10288 * command with the same tag number as long as the previous 10289 * instance of this tag number has been aborted somehow. 10290 */ 10291 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10292 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10293 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10294 && ((pending_io->io_hdr.nexus.targ_port == 10295 ooa_io->io_hdr.nexus.targ_port) 10296 && (pending_io->io_hdr.nexus.initid.id == 10297 ooa_io->io_hdr.nexus.initid.id)) 10298 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10299 return (CTL_ACTION_OVERLAP_TAG); 10300 10301 /* 10302 * If we get a head of queue tag, SAM-3 says that we should 10303 * immediately execute it. 10304 * 10305 * What happens if this command would normally block for some other 10306 * reason? e.g. a request sense with a head of queue tag 10307 * immediately after a write. Normally that would block, but this 10308 * will result in its getting executed immediately... 10309 * 10310 * We currently return "pass" instead of "skip", so we'll end up 10311 * going through the rest of the queue to check for overlapped tags. 10312 * 10313 * XXX KDM check for other types of blockage first?? 10314 */ 10315 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10316 return (CTL_ACTION_PASS); 10317 10318 /* 10319 * Ordered tags have to block until all items ahead of them 10320 * have completed. If we get called with an ordered tag, we always 10321 * block, if something else is ahead of us in the queue. 10322 */ 10323 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10324 return (CTL_ACTION_BLOCK); 10325 10326 /* 10327 * Simple tags get blocked until all head of queue and ordered tags 10328 * ahead of them have completed. I'm lumping untagged commands in 10329 * with simple tags here. XXX KDM is that the right thing to do? 10330 */ 10331 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10332 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10333 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10334 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10335 return (CTL_ACTION_BLOCK); 10336 10337 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio); 10338 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio); 10339 10340 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10341 10342 switch (serialize_row[pending_entry->seridx]) { 10343 case CTL_SER_BLOCK: 10344 return (CTL_ACTION_BLOCK); 10345 break; /* NOTREACHED */ 10346 case CTL_SER_EXTENT: 10347 return (ctl_extent_check(pending_io, ooa_io)); 10348 break; /* NOTREACHED */ 10349 case CTL_SER_PASS: 10350 return (CTL_ACTION_PASS); 10351 break; /* NOTREACHED */ 10352 case CTL_SER_SKIP: 10353 return (CTL_ACTION_SKIP); 10354 break; 10355 default: 10356 panic("invalid serialization value %d", 10357 serialize_row[pending_entry->seridx]); 10358 break; /* NOTREACHED */ 10359 } 10360 10361 return (CTL_ACTION_ERROR); 10362} 10363 10364/* 10365 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10366 * Assumptions: 10367 * - pending_io is generally either incoming, or on the blocked queue 10368 * - starting I/O is the I/O we want to start the check with. 10369 */ 10370static ctl_action 10371ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10372 union ctl_io *starting_io) 10373{ 10374 union ctl_io *ooa_io; 10375 ctl_action action; 10376 10377 mtx_assert(&lun->lun_lock, MA_OWNED); 10378 10379 /* 10380 * Run back along the OOA queue, starting with the current 10381 * blocked I/O and going through every I/O before it on the 10382 * queue. If starting_io is NULL, we'll just end up returning 10383 * CTL_ACTION_PASS. 10384 */ 10385 for (ooa_io = starting_io; ooa_io != NULL; 10386 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10387 ooa_links)){ 10388 10389 /* 10390 * This routine just checks to see whether 10391 * cur_blocked is blocked by ooa_io, which is ahead 10392 * of it in the queue. It doesn't queue/dequeue 10393 * cur_blocked. 10394 */ 10395 action = ctl_check_for_blockage(pending_io, ooa_io); 10396 switch (action) { 10397 case CTL_ACTION_BLOCK: 10398 case CTL_ACTION_OVERLAP: 10399 case CTL_ACTION_OVERLAP_TAG: 10400 case CTL_ACTION_SKIP: 10401 case CTL_ACTION_ERROR: 10402 return (action); 10403 break; /* NOTREACHED */ 10404 case CTL_ACTION_PASS: 10405 break; 10406 default: 10407 panic("invalid action %d", action); 10408 break; /* NOTREACHED */ 10409 } 10410 } 10411 10412 return (CTL_ACTION_PASS); 10413} 10414 10415/* 10416 * Assumptions: 10417 * - An I/O has just completed, and has been removed from the per-LUN OOA 10418 * queue, so some items on the blocked queue may now be unblocked. 10419 */ 10420static int 10421ctl_check_blocked(struct ctl_lun *lun) 10422{ 10423 union ctl_io *cur_blocked, *next_blocked; 10424 10425 mtx_assert(&lun->lun_lock, MA_OWNED); 10426 10427 /* 10428 * Run forward from the head of the blocked queue, checking each 10429 * entry against the I/Os prior to it on the OOA queue to see if 10430 * there is still any blockage. 10431 * 10432 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10433 * with our removing a variable on it while it is traversing the 10434 * list. 10435 */ 10436 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10437 cur_blocked != NULL; cur_blocked = next_blocked) { 10438 union ctl_io *prev_ooa; 10439 ctl_action action; 10440 10441 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10442 blocked_links); 10443 10444 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10445 ctl_ooaq, ooa_links); 10446 10447 /* 10448 * If cur_blocked happens to be the first item in the OOA 10449 * queue now, prev_ooa will be NULL, and the action 10450 * returned will just be CTL_ACTION_PASS. 10451 */ 10452 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10453 10454 switch (action) { 10455 case CTL_ACTION_BLOCK: 10456 /* Nothing to do here, still blocked */ 10457 break; 10458 case CTL_ACTION_OVERLAP: 10459 case CTL_ACTION_OVERLAP_TAG: 10460 /* 10461 * This shouldn't happen! In theory we've already 10462 * checked this command for overlap... 10463 */ 10464 break; 10465 case CTL_ACTION_PASS: 10466 case CTL_ACTION_SKIP: { 10467 struct ctl_softc *softc; 10468 const struct ctl_cmd_entry *entry; 10469 uint32_t initidx; 10470 int isc_retval; 10471 10472 /* 10473 * The skip case shouldn't happen, this transaction 10474 * should have never made it onto the blocked queue. 10475 */ 10476 /* 10477 * This I/O is no longer blocked, we can remove it 10478 * from the blocked queue. Since this is a TAILQ 10479 * (doubly linked list), we can do O(1) removals 10480 * from any place on the list. 10481 */ 10482 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10483 blocked_links); 10484 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10485 10486 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10487 /* 10488 * Need to send IO back to original side to 10489 * run 10490 */ 10491 union ctl_ha_msg msg_info; 10492 10493 msg_info.hdr.original_sc = 10494 cur_blocked->io_hdr.original_sc; 10495 msg_info.hdr.serializing_sc = cur_blocked; 10496 msg_info.hdr.msg_type = CTL_MSG_R2R; 10497 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10498 &msg_info, sizeof(msg_info), 0)) > 10499 CTL_HA_STATUS_SUCCESS) { 10500 printf("CTL:Check Blocked error from " 10501 "ctl_ha_msg_send %d\n", 10502 isc_retval); 10503 } 10504 break; 10505 } 10506 entry = ctl_get_cmd_entry(&cur_blocked->scsiio); 10507 softc = control_softc; 10508 10509 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus); 10510 10511 /* 10512 * Check this I/O for LUN state changes that may 10513 * have happened while this command was blocked. 10514 * The LUN state may have been changed by a command 10515 * ahead of us in the queue, so we need to re-check 10516 * for any states that can be caused by SCSI 10517 * commands. 10518 */ 10519 if (ctl_scsiio_lun_check(softc, lun, entry, 10520 &cur_blocked->scsiio) == 0) { 10521 cur_blocked->io_hdr.flags |= 10522 CTL_FLAG_IS_WAS_ON_RTR; 10523 ctl_enqueue_rtr(cur_blocked); 10524 } else 10525 ctl_done(cur_blocked); 10526 break; 10527 } 10528 default: 10529 /* 10530 * This probably shouldn't happen -- we shouldn't 10531 * get CTL_ACTION_ERROR, or anything else. 10532 */ 10533 break; 10534 } 10535 } 10536 10537 return (CTL_RETVAL_COMPLETE); 10538} 10539 10540/* 10541 * This routine (with one exception) checks LUN flags that can be set by 10542 * commands ahead of us in the OOA queue. These flags have to be checked 10543 * when a command initially comes in, and when we pull a command off the 10544 * blocked queue and are preparing to execute it. The reason we have to 10545 * check these flags for commands on the blocked queue is that the LUN 10546 * state may have been changed by a command ahead of us while we're on the 10547 * blocked queue. 10548 * 10549 * Ordering is somewhat important with these checks, so please pay 10550 * careful attention to the placement of any new checks. 10551 */ 10552static int 10553ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 10554 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10555{ 10556 int retval; 10557 10558 retval = 0; 10559 10560 mtx_assert(&lun->lun_lock, MA_OWNED); 10561 10562 /* 10563 * If this shelf is a secondary shelf controller, we have to reject 10564 * any media access commands. 10565 */ 10566#if 0 10567 /* No longer needed for HA */ 10568 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0) 10569 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) { 10570 ctl_set_lun_standby(ctsio); 10571 retval = 1; 10572 goto bailout; 10573 } 10574#endif 10575 10576 /* 10577 * Check for a reservation conflict. If this command isn't allowed 10578 * even on reserved LUNs, and if this initiator isn't the one who 10579 * reserved us, reject the command with a reservation conflict. 10580 */ 10581 if ((lun->flags & CTL_LUN_RESERVED) 10582 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 10583 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id) 10584 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port) 10585 || (ctsio->io_hdr.nexus.targ_target.id != 10586 lun->rsv_nexus.targ_target.id)) { 10587 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10588 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10589 retval = 1; 10590 goto bailout; 10591 } 10592 } 10593 10594 if ( (lun->flags & CTL_LUN_PR_RESERVED) 10595 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) { 10596 uint32_t residx; 10597 10598 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 10599 /* 10600 * if we aren't registered or it's a res holder type 10601 * reservation and this isn't the res holder then set a 10602 * conflict. 10603 * NOTE: Commands which might be allowed on write exclusive 10604 * type reservations are checked in the particular command 10605 * for a conflict. Read and SSU are the only ones. 10606 */ 10607 if (!lun->per_res[residx].registered 10608 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 10609 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT; 10610 ctsio->io_hdr.status = CTL_SCSI_ERROR; 10611 retval = 1; 10612 goto bailout; 10613 } 10614 10615 } 10616 10617 if ((lun->flags & CTL_LUN_OFFLINE) 10618 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 10619 ctl_set_lun_not_ready(ctsio); 10620 retval = 1; 10621 goto bailout; 10622 } 10623 10624 /* 10625 * If the LUN is stopped, see if this particular command is allowed 10626 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 10627 */ 10628 if ((lun->flags & CTL_LUN_STOPPED) 10629 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 10630 /* "Logical unit not ready, initializing cmd. required" */ 10631 ctl_set_lun_stopped(ctsio); 10632 retval = 1; 10633 goto bailout; 10634 } 10635 10636 if ((lun->flags & CTL_LUN_INOPERABLE) 10637 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 10638 /* "Medium format corrupted" */ 10639 ctl_set_medium_format_corrupted(ctsio); 10640 retval = 1; 10641 goto bailout; 10642 } 10643 10644bailout: 10645 return (retval); 10646 10647} 10648 10649static void 10650ctl_failover_io(union ctl_io *io, int have_lock) 10651{ 10652 ctl_set_busy(&io->scsiio); 10653 ctl_done(io); 10654} 10655 10656static void 10657ctl_failover(void) 10658{ 10659 struct ctl_lun *lun; 10660 struct ctl_softc *ctl_softc; 10661 union ctl_io *next_io, *pending_io; 10662 union ctl_io *io; 10663 int lun_idx; 10664 int i; 10665 10666 ctl_softc = control_softc; 10667 10668 mtx_lock(&ctl_softc->ctl_lock); 10669 /* 10670 * Remove any cmds from the other SC from the rtr queue. These 10671 * will obviously only be for LUNs for which we're the primary. 10672 * We can't send status or get/send data for these commands. 10673 * Since they haven't been executed yet, we can just remove them. 10674 * We'll either abort them or delete them below, depending on 10675 * which HA mode we're in. 10676 */ 10677#ifdef notyet 10678 mtx_lock(&ctl_softc->queue_lock); 10679 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 10680 io != NULL; io = next_io) { 10681 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 10682 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10683 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 10684 ctl_io_hdr, links); 10685 } 10686 mtx_unlock(&ctl_softc->queue_lock); 10687#endif 10688 10689 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 10690 lun = ctl_softc->ctl_luns[lun_idx]; 10691 if (lun==NULL) 10692 continue; 10693 10694 /* 10695 * Processor LUNs are primary on both sides. 10696 * XXX will this always be true? 10697 */ 10698 if (lun->be_lun->lun_type == T_PROCESSOR) 10699 continue; 10700 10701 if ((lun->flags & CTL_LUN_PRIMARY_SC) 10702 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10703 printf("FAILOVER: primary lun %d\n", lun_idx); 10704 /* 10705 * Remove all commands from the other SC. First from the 10706 * blocked queue then from the ooa queue. Once we have 10707 * removed them. Call ctl_check_blocked to see if there 10708 * is anything that can run. 10709 */ 10710 for (io = (union ctl_io *)TAILQ_FIRST( 10711 &lun->blocked_queue); io != NULL; io = next_io) { 10712 10713 next_io = (union ctl_io *)TAILQ_NEXT( 10714 &io->io_hdr, blocked_links); 10715 10716 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10717 TAILQ_REMOVE(&lun->blocked_queue, 10718 &io->io_hdr,blocked_links); 10719 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10720 TAILQ_REMOVE(&lun->ooa_queue, 10721 &io->io_hdr, ooa_links); 10722 10723 ctl_free_io(io); 10724 } 10725 } 10726 10727 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10728 io != NULL; io = next_io) { 10729 10730 next_io = (union ctl_io *)TAILQ_NEXT( 10731 &io->io_hdr, ooa_links); 10732 10733 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 10734 10735 TAILQ_REMOVE(&lun->ooa_queue, 10736 &io->io_hdr, 10737 ooa_links); 10738 10739 ctl_free_io(io); 10740 } 10741 } 10742 ctl_check_blocked(lun); 10743 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 10744 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10745 10746 printf("FAILOVER: primary lun %d\n", lun_idx); 10747 /* 10748 * Abort all commands from the other SC. We can't 10749 * send status back for them now. These should get 10750 * cleaned up when they are completed or come out 10751 * for a datamove operation. 10752 */ 10753 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 10754 io != NULL; io = next_io) { 10755 next_io = (union ctl_io *)TAILQ_NEXT( 10756 &io->io_hdr, ooa_links); 10757 10758 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 10759 io->io_hdr.flags |= CTL_FLAG_ABORT; 10760 } 10761 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10762 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 10763 10764 printf("FAILOVER: secondary lun %d\n", lun_idx); 10765 10766 lun->flags |= CTL_LUN_PRIMARY_SC; 10767 10768 /* 10769 * We send all I/O that was sent to this controller 10770 * and redirected to the other side back with 10771 * busy status, and have the initiator retry it. 10772 * Figuring out how much data has been transferred, 10773 * etc. and picking up where we left off would be 10774 * very tricky. 10775 * 10776 * XXX KDM need to remove I/O from the blocked 10777 * queue as well! 10778 */ 10779 for (pending_io = (union ctl_io *)TAILQ_FIRST( 10780 &lun->ooa_queue); pending_io != NULL; 10781 pending_io = next_io) { 10782 10783 next_io = (union ctl_io *)TAILQ_NEXT( 10784 &pending_io->io_hdr, ooa_links); 10785 10786 pending_io->io_hdr.flags &= 10787 ~CTL_FLAG_SENT_2OTHER_SC; 10788 10789 if (pending_io->io_hdr.flags & 10790 CTL_FLAG_IO_ACTIVE) { 10791 pending_io->io_hdr.flags |= 10792 CTL_FLAG_FAILOVER; 10793 } else { 10794 ctl_set_busy(&pending_io->scsiio); 10795 ctl_done(pending_io); 10796 } 10797 } 10798 10799 /* 10800 * Build Unit Attention 10801 */ 10802 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10803 lun->pending_sense[i].ua_pending |= 10804 CTL_UA_ASYM_ACC_CHANGE; 10805 } 10806 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 10807 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 10808 printf("FAILOVER: secondary lun %d\n", lun_idx); 10809 /* 10810 * if the first io on the OOA is not on the RtR queue 10811 * add it. 10812 */ 10813 lun->flags |= CTL_LUN_PRIMARY_SC; 10814 10815 pending_io = (union ctl_io *)TAILQ_FIRST( 10816 &lun->ooa_queue); 10817 if (pending_io==NULL) { 10818 printf("Nothing on OOA queue\n"); 10819 continue; 10820 } 10821 10822 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 10823 if ((pending_io->io_hdr.flags & 10824 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 10825 pending_io->io_hdr.flags |= 10826 CTL_FLAG_IS_WAS_ON_RTR; 10827 ctl_enqueue_rtr(pending_io); 10828 } 10829#if 0 10830 else 10831 { 10832 printf("Tag 0x%04x is running\n", 10833 pending_io->scsiio.tag_num); 10834 } 10835#endif 10836 10837 next_io = (union ctl_io *)TAILQ_NEXT( 10838 &pending_io->io_hdr, ooa_links); 10839 for (pending_io=next_io; pending_io != NULL; 10840 pending_io = next_io) { 10841 pending_io->io_hdr.flags &= 10842 ~CTL_FLAG_SENT_2OTHER_SC; 10843 next_io = (union ctl_io *)TAILQ_NEXT( 10844 &pending_io->io_hdr, ooa_links); 10845 if (pending_io->io_hdr.flags & 10846 CTL_FLAG_IS_WAS_ON_RTR) { 10847#if 0 10848 printf("Tag 0x%04x is running\n", 10849 pending_io->scsiio.tag_num); 10850#endif 10851 continue; 10852 } 10853 10854 switch (ctl_check_ooa(lun, pending_io, 10855 (union ctl_io *)TAILQ_PREV( 10856 &pending_io->io_hdr, ctl_ooaq, 10857 ooa_links))) { 10858 10859 case CTL_ACTION_BLOCK: 10860 TAILQ_INSERT_TAIL(&lun->blocked_queue, 10861 &pending_io->io_hdr, 10862 blocked_links); 10863 pending_io->io_hdr.flags |= 10864 CTL_FLAG_BLOCKED; 10865 break; 10866 case CTL_ACTION_PASS: 10867 case CTL_ACTION_SKIP: 10868 pending_io->io_hdr.flags |= 10869 CTL_FLAG_IS_WAS_ON_RTR; 10870 ctl_enqueue_rtr(pending_io); 10871 break; 10872 case CTL_ACTION_OVERLAP: 10873 ctl_set_overlapped_cmd( 10874 (struct ctl_scsiio *)pending_io); 10875 ctl_done(pending_io); 10876 break; 10877 case CTL_ACTION_OVERLAP_TAG: 10878 ctl_set_overlapped_tag( 10879 (struct ctl_scsiio *)pending_io, 10880 pending_io->scsiio.tag_num & 0xff); 10881 ctl_done(pending_io); 10882 break; 10883 case CTL_ACTION_ERROR: 10884 default: 10885 ctl_set_internal_failure( 10886 (struct ctl_scsiio *)pending_io, 10887 0, // sks_valid 10888 0); //retry count 10889 ctl_done(pending_io); 10890 break; 10891 } 10892 } 10893 10894 /* 10895 * Build Unit Attention 10896 */ 10897 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 10898 lun->pending_sense[i].ua_pending |= 10899 CTL_UA_ASYM_ACC_CHANGE; 10900 } 10901 } else { 10902 panic("Unhandled HA mode failover, LUN flags = %#x, " 10903 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 10904 } 10905 } 10906 ctl_pause_rtr = 0; 10907 mtx_unlock(&ctl_softc->ctl_lock); 10908} 10909 10910static int 10911ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 10912{ 10913 struct ctl_lun *lun; 10914 const struct ctl_cmd_entry *entry; 10915 uint32_t initidx, targ_lun; 10916 int retval; 10917 10918 retval = 0; 10919 10920 lun = NULL; 10921 10922 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 10923 if ((targ_lun < CTL_MAX_LUNS) 10924 && (ctl_softc->ctl_luns[targ_lun] != NULL)) { 10925 lun = ctl_softc->ctl_luns[targ_lun]; 10926 /* 10927 * If the LUN is invalid, pretend that it doesn't exist. 10928 * It will go away as soon as all pending I/O has been 10929 * completed. 10930 */ 10931 if (lun->flags & CTL_LUN_DISABLED) { 10932 lun = NULL; 10933 } else { 10934 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 10935 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 10936 lun->be_lun; 10937 if (lun->be_lun->lun_type == T_PROCESSOR) { 10938 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 10939 } 10940 10941 /* 10942 * Every I/O goes into the OOA queue for a 10943 * particular LUN, and stays there until completion. 10944 */ 10945 mtx_lock(&lun->lun_lock); 10946 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 10947 ooa_links); 10948 } 10949 } else { 10950 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 10951 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 10952 } 10953 10954 /* Get command entry and return error if it is unsuppotyed. */ 10955 entry = ctl_validate_command(ctsio); 10956 if (entry == NULL) { 10957 if (lun) 10958 mtx_unlock(&lun->lun_lock); 10959 return (retval); 10960 } 10961 10962 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 10963 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 10964 10965 /* 10966 * Check to see whether we can send this command to LUNs that don't 10967 * exist. This should pretty much only be the case for inquiry 10968 * and request sense. Further checks, below, really require having 10969 * a LUN, so we can't really check the command anymore. Just put 10970 * it on the rtr queue. 10971 */ 10972 if (lun == NULL) { 10973 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 10974 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 10975 ctl_enqueue_rtr((union ctl_io *)ctsio); 10976 return (retval); 10977 } 10978 10979 ctl_set_unsupported_lun(ctsio); 10980 ctl_done((union ctl_io *)ctsio); 10981 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 10982 return (retval); 10983 } else { 10984 /* 10985 * Make sure we support this particular command on this LUN. 10986 * e.g., we don't support writes to the control LUN. 10987 */ 10988 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 10989 mtx_unlock(&lun->lun_lock); 10990 ctl_set_invalid_opcode(ctsio); 10991 ctl_done((union ctl_io *)ctsio); 10992 return (retval); 10993 } 10994 } 10995 10996 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 10997 10998 /* 10999 * If we've got a request sense, it'll clear the contingent 11000 * allegiance condition. Otherwise, if we have a CA condition for 11001 * this initiator, clear it, because it sent down a command other 11002 * than request sense. 11003 */ 11004 if ((ctsio->cdb[0] != REQUEST_SENSE) 11005 && (ctl_is_set(lun->have_ca, initidx))) 11006 ctl_clear_mask(lun->have_ca, initidx); 11007 11008 /* 11009 * If the command has this flag set, it handles its own unit 11010 * attention reporting, we shouldn't do anything. Otherwise we 11011 * check for any pending unit attentions, and send them back to the 11012 * initiator. We only do this when a command initially comes in, 11013 * not when we pull it off the blocked queue. 11014 * 11015 * According to SAM-3, section 5.3.2, the order that things get 11016 * presented back to the host is basically unit attentions caused 11017 * by some sort of reset event, busy status, reservation conflicts 11018 * or task set full, and finally any other status. 11019 * 11020 * One issue here is that some of the unit attentions we report 11021 * don't fall into the "reset" category (e.g. "reported luns data 11022 * has changed"). So reporting it here, before the reservation 11023 * check, may be technically wrong. I guess the only thing to do 11024 * would be to check for and report the reset events here, and then 11025 * check for the other unit attention types after we check for a 11026 * reservation conflict. 11027 * 11028 * XXX KDM need to fix this 11029 */ 11030 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11031 ctl_ua_type ua_type; 11032 11033 ua_type = lun->pending_sense[initidx].ua_pending; 11034 if (ua_type != CTL_UA_NONE) { 11035 scsi_sense_data_type sense_format; 11036 11037 if (lun != NULL) 11038 sense_format = (lun->flags & 11039 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11040 SSD_TYPE_FIXED; 11041 else 11042 sense_format = SSD_TYPE_FIXED; 11043 11044 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data, 11045 sense_format); 11046 if (ua_type != CTL_UA_NONE) { 11047 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11048 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11049 CTL_AUTOSENSE; 11050 ctsio->sense_len = SSD_FULL_SIZE; 11051 lun->pending_sense[initidx].ua_pending &= 11052 ~ua_type; 11053 mtx_unlock(&lun->lun_lock); 11054 ctl_done((union ctl_io *)ctsio); 11055 return (retval); 11056 } 11057 } 11058 } 11059 11060 11061 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11062 mtx_unlock(&lun->lun_lock); 11063 ctl_done((union ctl_io *)ctsio); 11064 return (retval); 11065 } 11066 11067 /* 11068 * XXX CHD this is where we want to send IO to other side if 11069 * this LUN is secondary on this SC. We will need to make a copy 11070 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11071 * the copy we send as FROM_OTHER. 11072 * We also need to stuff the address of the original IO so we can 11073 * find it easily. Something similar will need be done on the other 11074 * side so when we are done we can find the copy. 11075 */ 11076 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11077 union ctl_ha_msg msg_info; 11078 int isc_retval; 11079 11080 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11081 11082 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11083 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11084#if 0 11085 printf("1. ctsio %p\n", ctsio); 11086#endif 11087 msg_info.hdr.serializing_sc = NULL; 11088 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11089 msg_info.scsi.tag_num = ctsio->tag_num; 11090 msg_info.scsi.tag_type = ctsio->tag_type; 11091 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11092 11093 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11094 11095 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11096 (void *)&msg_info, sizeof(msg_info), 0)) > 11097 CTL_HA_STATUS_SUCCESS) { 11098 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11099 isc_retval); 11100 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11101 } else { 11102#if 0 11103 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11104#endif 11105 } 11106 11107 /* 11108 * XXX KDM this I/O is off the incoming queue, but hasn't 11109 * been inserted on any other queue. We may need to come 11110 * up with a holding queue while we wait for serialization 11111 * so that we have an idea of what we're waiting for from 11112 * the other side. 11113 */ 11114 mtx_unlock(&lun->lun_lock); 11115 return (retval); 11116 } 11117 11118 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11119 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11120 ctl_ooaq, ooa_links))) { 11121 case CTL_ACTION_BLOCK: 11122 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11123 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11124 blocked_links); 11125 mtx_unlock(&lun->lun_lock); 11126 return (retval); 11127 case CTL_ACTION_PASS: 11128 case CTL_ACTION_SKIP: 11129 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11130 mtx_unlock(&lun->lun_lock); 11131 ctl_enqueue_rtr((union ctl_io *)ctsio); 11132 break; 11133 case CTL_ACTION_OVERLAP: 11134 mtx_unlock(&lun->lun_lock); 11135 ctl_set_overlapped_cmd(ctsio); 11136 ctl_done((union ctl_io *)ctsio); 11137 break; 11138 case CTL_ACTION_OVERLAP_TAG: 11139 mtx_unlock(&lun->lun_lock); 11140 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11141 ctl_done((union ctl_io *)ctsio); 11142 break; 11143 case CTL_ACTION_ERROR: 11144 default: 11145 mtx_unlock(&lun->lun_lock); 11146 ctl_set_internal_failure(ctsio, 11147 /*sks_valid*/ 0, 11148 /*retry_count*/ 0); 11149 ctl_done((union ctl_io *)ctsio); 11150 break; 11151 } 11152 return (retval); 11153} 11154 11155const struct ctl_cmd_entry * 11156ctl_get_cmd_entry(struct ctl_scsiio *ctsio) 11157{ 11158 const struct ctl_cmd_entry *entry; 11159 int service_action; 11160 11161 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11162 if (entry->flags & CTL_CMD_FLAG_SA5) { 11163 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11164 entry = &((const struct ctl_cmd_entry *) 11165 entry->execute)[service_action]; 11166 } 11167 return (entry); 11168} 11169 11170const struct ctl_cmd_entry * 11171ctl_validate_command(struct ctl_scsiio *ctsio) 11172{ 11173 const struct ctl_cmd_entry *entry; 11174 int i; 11175 uint8_t diff; 11176 11177 entry = ctl_get_cmd_entry(ctsio); 11178 if (entry->execute == NULL) { 11179 ctl_set_invalid_opcode(ctsio); 11180 ctl_done((union ctl_io *)ctsio); 11181 return (NULL); 11182 } 11183 KASSERT(entry->length > 0, 11184 ("Not defined length for command 0x%02x/0x%02x", 11185 ctsio->cdb[0], ctsio->cdb[1])); 11186 for (i = 1; i < entry->length; i++) { 11187 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11188 if (diff == 0) 11189 continue; 11190 ctl_set_invalid_field(ctsio, 11191 /*sks_valid*/ 1, 11192 /*command*/ 1, 11193 /*field*/ i, 11194 /*bit_valid*/ 1, 11195 /*bit*/ fls(diff) - 1); 11196 ctl_done((union ctl_io *)ctsio); 11197 return (NULL); 11198 } 11199 return (entry); 11200} 11201 11202static int 11203ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11204{ 11205 11206 switch (lun_type) { 11207 case T_PROCESSOR: 11208 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11209 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11210 return (0); 11211 break; 11212 case T_DIRECT: 11213 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11214 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11215 return (0); 11216 break; 11217 default: 11218 return (0); 11219 } 11220 return (1); 11221} 11222 11223static int 11224ctl_scsiio(struct ctl_scsiio *ctsio) 11225{ 11226 int retval; 11227 const struct ctl_cmd_entry *entry; 11228 11229 retval = CTL_RETVAL_COMPLETE; 11230 11231 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11232 11233 entry = ctl_get_cmd_entry(ctsio); 11234 11235 /* 11236 * If this I/O has been aborted, just send it straight to 11237 * ctl_done() without executing it. 11238 */ 11239 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11240 ctl_done((union ctl_io *)ctsio); 11241 goto bailout; 11242 } 11243 11244 /* 11245 * All the checks should have been handled by ctl_scsiio_precheck(). 11246 * We should be clear now to just execute the I/O. 11247 */ 11248 retval = entry->execute(ctsio); 11249 11250bailout: 11251 return (retval); 11252} 11253 11254/* 11255 * Since we only implement one target right now, a bus reset simply resets 11256 * our single target. 11257 */ 11258static int 11259ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11260{ 11261 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11262} 11263 11264static int 11265ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11266 ctl_ua_type ua_type) 11267{ 11268 struct ctl_lun *lun; 11269 int retval; 11270 11271 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11272 union ctl_ha_msg msg_info; 11273 11274 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11275 msg_info.hdr.nexus = io->io_hdr.nexus; 11276 if (ua_type==CTL_UA_TARG_RESET) 11277 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11278 else 11279 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11280 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11281 msg_info.hdr.original_sc = NULL; 11282 msg_info.hdr.serializing_sc = NULL; 11283 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11284 (void *)&msg_info, sizeof(msg_info), 0)) { 11285 } 11286 } 11287 retval = 0; 11288 11289 mtx_lock(&ctl_softc->ctl_lock); 11290 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11291 retval += ctl_lun_reset(lun, io, ua_type); 11292 mtx_unlock(&ctl_softc->ctl_lock); 11293 11294 return (retval); 11295} 11296 11297/* 11298 * The LUN should always be set. The I/O is optional, and is used to 11299 * distinguish between I/Os sent by this initiator, and by other 11300 * initiators. We set unit attention for initiators other than this one. 11301 * SAM-3 is vague on this point. It does say that a unit attention should 11302 * be established for other initiators when a LUN is reset (see section 11303 * 5.7.3), but it doesn't specifically say that the unit attention should 11304 * be established for this particular initiator when a LUN is reset. Here 11305 * is the relevant text, from SAM-3 rev 8: 11306 * 11307 * 5.7.2 When a SCSI initiator port aborts its own tasks 11308 * 11309 * When a SCSI initiator port causes its own task(s) to be aborted, no 11310 * notification that the task(s) have been aborted shall be returned to 11311 * the SCSI initiator port other than the completion response for the 11312 * command or task management function action that caused the task(s) to 11313 * be aborted and notification(s) associated with related effects of the 11314 * action (e.g., a reset unit attention condition). 11315 * 11316 * XXX KDM for now, we're setting unit attention for all initiators. 11317 */ 11318static int 11319ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11320{ 11321 union ctl_io *xio; 11322#if 0 11323 uint32_t initindex; 11324#endif 11325 int i; 11326 11327 mtx_lock(&lun->lun_lock); 11328 /* 11329 * Run through the OOA queue and abort each I/O. 11330 */ 11331#if 0 11332 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11333#endif 11334 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11335 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11336 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11337 } 11338 11339 /* 11340 * This version sets unit attention for every 11341 */ 11342#if 0 11343 initindex = ctl_get_initindex(&io->io_hdr.nexus); 11344 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11345 if (initindex == i) 11346 continue; 11347 lun->pending_sense[i].ua_pending |= ua_type; 11348 } 11349#endif 11350 11351 /* 11352 * A reset (any kind, really) clears reservations established with 11353 * RESERVE/RELEASE. It does not clear reservations established 11354 * with PERSISTENT RESERVE OUT, but we don't support that at the 11355 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11356 * reservations made with the RESERVE/RELEASE commands, because 11357 * those commands are obsolete in SPC-3. 11358 */ 11359 lun->flags &= ~CTL_LUN_RESERVED; 11360 11361 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11362 ctl_clear_mask(lun->have_ca, i); 11363 lun->pending_sense[i].ua_pending |= ua_type; 11364 } 11365 mtx_unlock(&lun->lun_lock); 11366 11367 return (0); 11368} 11369 11370static int 11371ctl_abort_task(union ctl_io *io) 11372{ 11373 union ctl_io *xio; 11374 struct ctl_lun *lun; 11375 struct ctl_softc *ctl_softc; 11376#if 0 11377 struct sbuf sb; 11378 char printbuf[128]; 11379#endif 11380 int found; 11381 uint32_t targ_lun; 11382 11383 ctl_softc = control_softc; 11384 found = 0; 11385 11386 /* 11387 * Look up the LUN. 11388 */ 11389 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11390 mtx_lock(&ctl_softc->ctl_lock); 11391 if ((targ_lun < CTL_MAX_LUNS) 11392 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11393 lun = ctl_softc->ctl_luns[targ_lun]; 11394 else { 11395 mtx_unlock(&ctl_softc->ctl_lock); 11396 goto bailout; 11397 } 11398 11399#if 0 11400 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11401 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11402#endif 11403 11404 mtx_lock(&lun->lun_lock); 11405 mtx_unlock(&ctl_softc->ctl_lock); 11406 /* 11407 * Run through the OOA queue and attempt to find the given I/O. 11408 * The target port, initiator ID, tag type and tag number have to 11409 * match the values that we got from the initiator. If we have an 11410 * untagged command to abort, simply abort the first untagged command 11411 * we come to. We only allow one untagged command at a time of course. 11412 */ 11413#if 0 11414 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11415#endif 11416 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11417 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11418#if 0 11419 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11420 11421 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11422 lun->lun, xio->scsiio.tag_num, 11423 xio->scsiio.tag_type, 11424 (xio->io_hdr.blocked_links.tqe_prev 11425 == NULL) ? "" : " BLOCKED", 11426 (xio->io_hdr.flags & 11427 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11428 (xio->io_hdr.flags & 11429 CTL_FLAG_ABORT) ? " ABORT" : "", 11430 (xio->io_hdr.flags & 11431 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11432 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11433 sbuf_finish(&sb); 11434 printf("%s\n", sbuf_data(&sb)); 11435#endif 11436 11437 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 11438 && (xio->io_hdr.nexus.initid.id == 11439 io->io_hdr.nexus.initid.id)) { 11440 /* 11441 * If the abort says that the task is untagged, the 11442 * task in the queue must be untagged. Otherwise, 11443 * we just check to see whether the tag numbers 11444 * match. This is because the QLogic firmware 11445 * doesn't pass back the tag type in an abort 11446 * request. 11447 */ 11448#if 0 11449 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 11450 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 11451 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 11452#endif 11453 /* 11454 * XXX KDM we've got problems with FC, because it 11455 * doesn't send down a tag type with aborts. So we 11456 * can only really go by the tag number... 11457 * This may cause problems with parallel SCSI. 11458 * Need to figure that out!! 11459 */ 11460 if (xio->scsiio.tag_num == io->taskio.tag_num) { 11461 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11462 found = 1; 11463 if ((io->io_hdr.flags & 11464 CTL_FLAG_FROM_OTHER_SC) == 0 && 11465 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11466 union ctl_ha_msg msg_info; 11467 11468 io->io_hdr.flags |= 11469 CTL_FLAG_SENT_2OTHER_SC; 11470 msg_info.hdr.nexus = io->io_hdr.nexus; 11471 msg_info.task.task_action = 11472 CTL_TASK_ABORT_TASK; 11473 msg_info.task.tag_num = 11474 io->taskio.tag_num; 11475 msg_info.task.tag_type = 11476 io->taskio.tag_type; 11477 msg_info.hdr.msg_type = 11478 CTL_MSG_MANAGE_TASKS; 11479 msg_info.hdr.original_sc = NULL; 11480 msg_info.hdr.serializing_sc = NULL; 11481#if 0 11482 printf("Sent Abort to other side\n"); 11483#endif 11484 if (CTL_HA_STATUS_SUCCESS != 11485 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11486 (void *)&msg_info, 11487 sizeof(msg_info), 0)) { 11488 } 11489 } 11490#if 0 11491 printf("ctl_abort_task: found I/O to abort\n"); 11492#endif 11493 break; 11494 } 11495 } 11496 } 11497 mtx_unlock(&lun->lun_lock); 11498 11499bailout: 11500 11501 if (found == 0) { 11502 /* 11503 * This isn't really an error. It's entirely possible for 11504 * the abort and command completion to cross on the wire. 11505 * This is more of an informative/diagnostic error. 11506 */ 11507#if 0 11508 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 11509 "%d:%d:%d:%d tag %d type %d\n", 11510 io->io_hdr.nexus.initid.id, 11511 io->io_hdr.nexus.targ_port, 11512 io->io_hdr.nexus.targ_target.id, 11513 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 11514 io->taskio.tag_type); 11515#endif 11516 return (1); 11517 } else 11518 return (0); 11519} 11520 11521static void 11522ctl_run_task(union ctl_io *io) 11523{ 11524 struct ctl_softc *ctl_softc; 11525 int retval; 11526 const char *task_desc; 11527 11528 CTL_DEBUG_PRINT(("ctl_run_task\n")); 11529 11530 ctl_softc = control_softc; 11531 retval = 0; 11532 11533 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 11534 ("ctl_run_task: Unextected io_type %d\n", 11535 io->io_hdr.io_type)); 11536 11537 task_desc = ctl_scsi_task_string(&io->taskio); 11538 if (task_desc != NULL) { 11539#ifdef NEEDTOPORT 11540 csevent_log(CSC_CTL | CSC_SHELF_SW | 11541 CTL_TASK_REPORT, 11542 csevent_LogType_Trace, 11543 csevent_Severity_Information, 11544 csevent_AlertLevel_Green, 11545 csevent_FRU_Firmware, 11546 csevent_FRU_Unknown, 11547 "CTL: received task: %s",task_desc); 11548#endif 11549 } else { 11550#ifdef NEEDTOPORT 11551 csevent_log(CSC_CTL | CSC_SHELF_SW | 11552 CTL_TASK_REPORT, 11553 csevent_LogType_Trace, 11554 csevent_Severity_Information, 11555 csevent_AlertLevel_Green, 11556 csevent_FRU_Firmware, 11557 csevent_FRU_Unknown, 11558 "CTL: received unknown task " 11559 "type: %d (%#x)", 11560 io->taskio.task_action, 11561 io->taskio.task_action); 11562#endif 11563 } 11564 switch (io->taskio.task_action) { 11565 case CTL_TASK_ABORT_TASK: 11566 retval = ctl_abort_task(io); 11567 break; 11568 case CTL_TASK_ABORT_TASK_SET: 11569 break; 11570 case CTL_TASK_CLEAR_ACA: 11571 break; 11572 case CTL_TASK_CLEAR_TASK_SET: 11573 break; 11574 case CTL_TASK_LUN_RESET: { 11575 struct ctl_lun *lun; 11576 uint32_t targ_lun; 11577 int retval; 11578 11579 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11580 mtx_lock(&ctl_softc->ctl_lock); 11581 if ((targ_lun < CTL_MAX_LUNS) 11582 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11583 lun = ctl_softc->ctl_luns[targ_lun]; 11584 else { 11585 mtx_unlock(&ctl_softc->ctl_lock); 11586 retval = 1; 11587 break; 11588 } 11589 11590 if (!(io->io_hdr.flags & 11591 CTL_FLAG_FROM_OTHER_SC)) { 11592 union ctl_ha_msg msg_info; 11593 11594 io->io_hdr.flags |= 11595 CTL_FLAG_SENT_2OTHER_SC; 11596 msg_info.hdr.msg_type = 11597 CTL_MSG_MANAGE_TASKS; 11598 msg_info.hdr.nexus = io->io_hdr.nexus; 11599 msg_info.task.task_action = 11600 CTL_TASK_LUN_RESET; 11601 msg_info.hdr.original_sc = NULL; 11602 msg_info.hdr.serializing_sc = NULL; 11603 if (CTL_HA_STATUS_SUCCESS != 11604 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11605 (void *)&msg_info, 11606 sizeof(msg_info), 0)) { 11607 } 11608 } 11609 11610 retval = ctl_lun_reset(lun, io, 11611 CTL_UA_LUN_RESET); 11612 mtx_unlock(&ctl_softc->ctl_lock); 11613 break; 11614 } 11615 case CTL_TASK_TARGET_RESET: 11616 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 11617 break; 11618 case CTL_TASK_BUS_RESET: 11619 retval = ctl_bus_reset(ctl_softc, io); 11620 break; 11621 case CTL_TASK_PORT_LOGIN: 11622 break; 11623 case CTL_TASK_PORT_LOGOUT: 11624 break; 11625 default: 11626 printf("ctl_run_task: got unknown task management event %d\n", 11627 io->taskio.task_action); 11628 break; 11629 } 11630 if (retval == 0) 11631 io->io_hdr.status = CTL_SUCCESS; 11632 else 11633 io->io_hdr.status = CTL_ERROR; 11634 11635 /* 11636 * This will queue this I/O to the done queue, but the 11637 * work thread won't be able to process it until we 11638 * return and the lock is released. 11639 */ 11640 ctl_done(io); 11641} 11642 11643/* 11644 * For HA operation. Handle commands that come in from the other 11645 * controller. 11646 */ 11647static void 11648ctl_handle_isc(union ctl_io *io) 11649{ 11650 int free_io; 11651 struct ctl_lun *lun; 11652 struct ctl_softc *ctl_softc; 11653 uint32_t targ_lun; 11654 11655 ctl_softc = control_softc; 11656 11657 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11658 lun = ctl_softc->ctl_luns[targ_lun]; 11659 11660 switch (io->io_hdr.msg_type) { 11661 case CTL_MSG_SERIALIZE: 11662 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 11663 break; 11664 case CTL_MSG_R2R: { 11665 const struct ctl_cmd_entry *entry; 11666 11667 /* 11668 * This is only used in SER_ONLY mode. 11669 */ 11670 free_io = 0; 11671 entry = ctl_get_cmd_entry(&io->scsiio); 11672 mtx_lock(&lun->lun_lock); 11673 if (ctl_scsiio_lun_check(ctl_softc, lun, 11674 entry, (struct ctl_scsiio *)io) != 0) { 11675 mtx_unlock(&lun->lun_lock); 11676 ctl_done(io); 11677 break; 11678 } 11679 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11680 mtx_unlock(&lun->lun_lock); 11681 ctl_enqueue_rtr(io); 11682 break; 11683 } 11684 case CTL_MSG_FINISH_IO: 11685 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 11686 free_io = 0; 11687 ctl_done(io); 11688 } else { 11689 free_io = 1; 11690 mtx_lock(&lun->lun_lock); 11691 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 11692 ooa_links); 11693 ctl_check_blocked(lun); 11694 mtx_unlock(&lun->lun_lock); 11695 } 11696 break; 11697 case CTL_MSG_PERS_ACTION: 11698 ctl_hndl_per_res_out_on_other_sc( 11699 (union ctl_ha_msg *)&io->presio.pr_msg); 11700 free_io = 1; 11701 break; 11702 case CTL_MSG_BAD_JUJU: 11703 free_io = 0; 11704 ctl_done(io); 11705 break; 11706 case CTL_MSG_DATAMOVE: 11707 /* Only used in XFER mode */ 11708 free_io = 0; 11709 ctl_datamove_remote(io); 11710 break; 11711 case CTL_MSG_DATAMOVE_DONE: 11712 /* Only used in XFER mode */ 11713 free_io = 0; 11714 io->scsiio.be_move_done(io); 11715 break; 11716 default: 11717 free_io = 1; 11718 printf("%s: Invalid message type %d\n", 11719 __func__, io->io_hdr.msg_type); 11720 break; 11721 } 11722 if (free_io) 11723 ctl_free_io(io); 11724 11725} 11726 11727 11728/* 11729 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 11730 * there is no match. 11731 */ 11732static ctl_lun_error_pattern 11733ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 11734{ 11735 const struct ctl_cmd_entry *entry; 11736 ctl_lun_error_pattern filtered_pattern, pattern; 11737 11738 pattern = desc->error_pattern; 11739 11740 /* 11741 * XXX KDM we need more data passed into this function to match a 11742 * custom pattern, and we actually need to implement custom pattern 11743 * matching. 11744 */ 11745 if (pattern & CTL_LUN_PAT_CMD) 11746 return (CTL_LUN_PAT_CMD); 11747 11748 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 11749 return (CTL_LUN_PAT_ANY); 11750 11751 entry = ctl_get_cmd_entry(ctsio); 11752 11753 filtered_pattern = entry->pattern & pattern; 11754 11755 /* 11756 * If the user requested specific flags in the pattern (e.g. 11757 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 11758 * flags. 11759 * 11760 * If the user did not specify any flags, it doesn't matter whether 11761 * or not the command supports the flags. 11762 */ 11763 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 11764 (pattern & ~CTL_LUN_PAT_MASK)) 11765 return (CTL_LUN_PAT_NONE); 11766 11767 /* 11768 * If the user asked for a range check, see if the requested LBA 11769 * range overlaps with this command's LBA range. 11770 */ 11771 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 11772 uint64_t lba1; 11773 uint32_t len1; 11774 ctl_action action; 11775 int retval; 11776 11777 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 11778 if (retval != 0) 11779 return (CTL_LUN_PAT_NONE); 11780 11781 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 11782 desc->lba_range.len); 11783 /* 11784 * A "pass" means that the LBA ranges don't overlap, so 11785 * this doesn't match the user's range criteria. 11786 */ 11787 if (action == CTL_ACTION_PASS) 11788 return (CTL_LUN_PAT_NONE); 11789 } 11790 11791 return (filtered_pattern); 11792} 11793 11794static void 11795ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 11796{ 11797 struct ctl_error_desc *desc, *desc2; 11798 11799 mtx_assert(&lun->lun_lock, MA_OWNED); 11800 11801 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 11802 ctl_lun_error_pattern pattern; 11803 /* 11804 * Check to see whether this particular command matches 11805 * the pattern in the descriptor. 11806 */ 11807 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 11808 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 11809 continue; 11810 11811 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 11812 case CTL_LUN_INJ_ABORTED: 11813 ctl_set_aborted(&io->scsiio); 11814 break; 11815 case CTL_LUN_INJ_MEDIUM_ERR: 11816 ctl_set_medium_error(&io->scsiio); 11817 break; 11818 case CTL_LUN_INJ_UA: 11819 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 11820 * OCCURRED */ 11821 ctl_set_ua(&io->scsiio, 0x29, 0x00); 11822 break; 11823 case CTL_LUN_INJ_CUSTOM: 11824 /* 11825 * We're assuming the user knows what he is doing. 11826 * Just copy the sense information without doing 11827 * checks. 11828 */ 11829 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 11830 ctl_min(sizeof(desc->custom_sense), 11831 sizeof(io->scsiio.sense_data))); 11832 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 11833 io->scsiio.sense_len = SSD_FULL_SIZE; 11834 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11835 break; 11836 case CTL_LUN_INJ_NONE: 11837 default: 11838 /* 11839 * If this is an error injection type we don't know 11840 * about, clear the continuous flag (if it is set) 11841 * so it will get deleted below. 11842 */ 11843 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 11844 break; 11845 } 11846 /* 11847 * By default, each error injection action is a one-shot 11848 */ 11849 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 11850 continue; 11851 11852 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 11853 11854 free(desc, M_CTL); 11855 } 11856} 11857 11858#ifdef CTL_IO_DELAY 11859static void 11860ctl_datamove_timer_wakeup(void *arg) 11861{ 11862 union ctl_io *io; 11863 11864 io = (union ctl_io *)arg; 11865 11866 ctl_datamove(io); 11867} 11868#endif /* CTL_IO_DELAY */ 11869 11870void 11871ctl_datamove(union ctl_io *io) 11872{ 11873 void (*fe_datamove)(union ctl_io *io); 11874 11875 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 11876 11877 CTL_DEBUG_PRINT(("ctl_datamove\n")); 11878 11879#ifdef CTL_TIME_IO 11880 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 11881 char str[256]; 11882 char path_str[64]; 11883 struct sbuf sb; 11884 11885 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 11886 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 11887 11888 sbuf_cat(&sb, path_str); 11889 switch (io->io_hdr.io_type) { 11890 case CTL_IO_SCSI: 11891 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 11892 sbuf_printf(&sb, "\n"); 11893 sbuf_cat(&sb, path_str); 11894 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 11895 io->scsiio.tag_num, io->scsiio.tag_type); 11896 break; 11897 case CTL_IO_TASK: 11898 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 11899 "Tag Type: %d\n", io->taskio.task_action, 11900 io->taskio.tag_num, io->taskio.tag_type); 11901 break; 11902 default: 11903 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 11904 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 11905 break; 11906 } 11907 sbuf_cat(&sb, path_str); 11908 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 11909 (intmax_t)time_uptime - io->io_hdr.start_time); 11910 sbuf_finish(&sb); 11911 printf("%s", sbuf_data(&sb)); 11912 } 11913#endif /* CTL_TIME_IO */ 11914 11915#ifdef CTL_IO_DELAY 11916 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 11917 struct ctl_lun *lun; 11918 11919 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 11920 11921 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 11922 } else { 11923 struct ctl_lun *lun; 11924 11925 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 11926 if ((lun != NULL) 11927 && (lun->delay_info.datamove_delay > 0)) { 11928 struct callout *callout; 11929 11930 callout = (struct callout *)&io->io_hdr.timer_bytes; 11931 callout_init(callout, /*mpsafe*/ 1); 11932 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 11933 callout_reset(callout, 11934 lun->delay_info.datamove_delay * hz, 11935 ctl_datamove_timer_wakeup, io); 11936 if (lun->delay_info.datamove_type == 11937 CTL_DELAY_TYPE_ONESHOT) 11938 lun->delay_info.datamove_delay = 0; 11939 return; 11940 } 11941 } 11942#endif 11943 11944 /* 11945 * This command has been aborted. Set the port status, so we fail 11946 * the data move. 11947 */ 11948 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 11949 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 11950 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 11951 io->io_hdr.nexus.targ_port, 11952 (uintmax_t)io->io_hdr.nexus.targ_target.id, 11953 io->io_hdr.nexus.targ_lun); 11954 io->io_hdr.status = CTL_CMD_ABORTED; 11955 io->io_hdr.port_status = 31337; 11956 /* 11957 * Note that the backend, in this case, will get the 11958 * callback in its context. In other cases it may get 11959 * called in the frontend's interrupt thread context. 11960 */ 11961 io->scsiio.be_move_done(io); 11962 return; 11963 } 11964 11965 /* 11966 * If we're in XFER mode and this I/O is from the other shelf 11967 * controller, we need to send the DMA to the other side to 11968 * actually transfer the data to/from the host. In serialize only 11969 * mode the transfer happens below CTL and ctl_datamove() is only 11970 * called on the machine that originally received the I/O. 11971 */ 11972 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 11973 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11974 union ctl_ha_msg msg; 11975 uint32_t sg_entries_sent; 11976 int do_sg_copy; 11977 int i; 11978 11979 memset(&msg, 0, sizeof(msg)); 11980 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 11981 msg.hdr.original_sc = io->io_hdr.original_sc; 11982 msg.hdr.serializing_sc = io; 11983 msg.hdr.nexus = io->io_hdr.nexus; 11984 msg.dt.flags = io->io_hdr.flags; 11985 /* 11986 * We convert everything into a S/G list here. We can't 11987 * pass by reference, only by value between controllers. 11988 * So we can't pass a pointer to the S/G list, only as many 11989 * S/G entries as we can fit in here. If it's possible for 11990 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 11991 * then we need to break this up into multiple transfers. 11992 */ 11993 if (io->scsiio.kern_sg_entries == 0) { 11994 msg.dt.kern_sg_entries = 1; 11995 /* 11996 * If this is in cached memory, flush the cache 11997 * before we send the DMA request to the other 11998 * controller. We want to do this in either the 11999 * read or the write case. The read case is 12000 * straightforward. In the write case, we want to 12001 * make sure nothing is in the local cache that 12002 * could overwrite the DMAed data. 12003 */ 12004 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12005 /* 12006 * XXX KDM use bus_dmamap_sync() here. 12007 */ 12008 } 12009 12010 /* 12011 * Convert to a physical address if this is a 12012 * virtual address. 12013 */ 12014 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12015 msg.dt.sg_list[0].addr = 12016 io->scsiio.kern_data_ptr; 12017 } else { 12018 /* 12019 * XXX KDM use busdma here! 12020 */ 12021#if 0 12022 msg.dt.sg_list[0].addr = (void *) 12023 vtophys(io->scsiio.kern_data_ptr); 12024#endif 12025 } 12026 12027 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12028 do_sg_copy = 0; 12029 } else { 12030 struct ctl_sg_entry *sgl; 12031 12032 do_sg_copy = 1; 12033 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12034 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12035 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12036 /* 12037 * XXX KDM use bus_dmamap_sync() here. 12038 */ 12039 } 12040 } 12041 12042 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12043 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12044 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12045 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12046 msg.dt.sg_sequence = 0; 12047 12048 /* 12049 * Loop until we've sent all of the S/G entries. On the 12050 * other end, we'll recompose these S/G entries into one 12051 * contiguous list before passing it to the 12052 */ 12053 for (sg_entries_sent = 0; sg_entries_sent < 12054 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12055 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12056 sizeof(msg.dt.sg_list[0])), 12057 msg.dt.kern_sg_entries - sg_entries_sent); 12058 12059 if (do_sg_copy != 0) { 12060 struct ctl_sg_entry *sgl; 12061 int j; 12062 12063 sgl = (struct ctl_sg_entry *) 12064 io->scsiio.kern_data_ptr; 12065 /* 12066 * If this is in cached memory, flush the cache 12067 * before we send the DMA request to the other 12068 * controller. We want to do this in either 12069 * the * read or the write case. The read 12070 * case is straightforward. In the write 12071 * case, we want to make sure nothing is 12072 * in the local cache that could overwrite 12073 * the DMAed data. 12074 */ 12075 12076 for (i = sg_entries_sent, j = 0; 12077 i < msg.dt.cur_sg_entries; i++, j++) { 12078 if ((io->io_hdr.flags & 12079 CTL_FLAG_NO_DATASYNC) == 0) { 12080 /* 12081 * XXX KDM use bus_dmamap_sync() 12082 */ 12083 } 12084 if ((io->io_hdr.flags & 12085 CTL_FLAG_BUS_ADDR) == 0) { 12086 /* 12087 * XXX KDM use busdma. 12088 */ 12089#if 0 12090 msg.dt.sg_list[j].addr =(void *) 12091 vtophys(sgl[i].addr); 12092#endif 12093 } else { 12094 msg.dt.sg_list[j].addr = 12095 sgl[i].addr; 12096 } 12097 msg.dt.sg_list[j].len = sgl[i].len; 12098 } 12099 } 12100 12101 sg_entries_sent += msg.dt.cur_sg_entries; 12102 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12103 msg.dt.sg_last = 1; 12104 else 12105 msg.dt.sg_last = 0; 12106 12107 /* 12108 * XXX KDM drop and reacquire the lock here? 12109 */ 12110 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12111 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12112 /* 12113 * XXX do something here. 12114 */ 12115 } 12116 12117 msg.dt.sent_sg_entries = sg_entries_sent; 12118 } 12119 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12120 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12121 ctl_failover_io(io, /*have_lock*/ 0); 12122 12123 } else { 12124 12125 /* 12126 * Lookup the fe_datamove() function for this particular 12127 * front end. 12128 */ 12129 fe_datamove = 12130 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12131 12132 fe_datamove(io); 12133 } 12134} 12135 12136static void 12137ctl_send_datamove_done(union ctl_io *io, int have_lock) 12138{ 12139 union ctl_ha_msg msg; 12140 int isc_status; 12141 12142 memset(&msg, 0, sizeof(msg)); 12143 12144 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12145 msg.hdr.original_sc = io; 12146 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12147 msg.hdr.nexus = io->io_hdr.nexus; 12148 msg.hdr.status = io->io_hdr.status; 12149 msg.scsi.tag_num = io->scsiio.tag_num; 12150 msg.scsi.tag_type = io->scsiio.tag_type; 12151 msg.scsi.scsi_status = io->scsiio.scsi_status; 12152 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12153 sizeof(io->scsiio.sense_data)); 12154 msg.scsi.sense_len = io->scsiio.sense_len; 12155 msg.scsi.sense_residual = io->scsiio.sense_residual; 12156 msg.scsi.fetd_status = io->io_hdr.port_status; 12157 msg.scsi.residual = io->scsiio.residual; 12158 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12159 12160 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12161 ctl_failover_io(io, /*have_lock*/ have_lock); 12162 return; 12163 } 12164 12165 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12166 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12167 /* XXX do something if this fails */ 12168 } 12169 12170} 12171 12172/* 12173 * The DMA to the remote side is done, now we need to tell the other side 12174 * we're done so it can continue with its data movement. 12175 */ 12176static void 12177ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12178{ 12179 union ctl_io *io; 12180 12181 io = rq->context; 12182 12183 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12184 printf("%s: ISC DMA write failed with error %d", __func__, 12185 rq->ret); 12186 ctl_set_internal_failure(&io->scsiio, 12187 /*sks_valid*/ 1, 12188 /*retry_count*/ rq->ret); 12189 } 12190 12191 ctl_dt_req_free(rq); 12192 12193 /* 12194 * In this case, we had to malloc the memory locally. Free it. 12195 */ 12196 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12197 int i; 12198 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12199 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12200 } 12201 /* 12202 * The data is in local and remote memory, so now we need to send 12203 * status (good or back) back to the other side. 12204 */ 12205 ctl_send_datamove_done(io, /*have_lock*/ 0); 12206} 12207 12208/* 12209 * We've moved the data from the host/controller into local memory. Now we 12210 * need to push it over to the remote controller's memory. 12211 */ 12212static int 12213ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12214{ 12215 int retval; 12216 12217 retval = 0; 12218 12219 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12220 ctl_datamove_remote_write_cb); 12221 12222 return (retval); 12223} 12224 12225static void 12226ctl_datamove_remote_write(union ctl_io *io) 12227{ 12228 int retval; 12229 void (*fe_datamove)(union ctl_io *io); 12230 12231 /* 12232 * - Get the data from the host/HBA into local memory. 12233 * - DMA memory from the local controller to the remote controller. 12234 * - Send status back to the remote controller. 12235 */ 12236 12237 retval = ctl_datamove_remote_sgl_setup(io); 12238 if (retval != 0) 12239 return; 12240 12241 /* Switch the pointer over so the FETD knows what to do */ 12242 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12243 12244 /* 12245 * Use a custom move done callback, since we need to send completion 12246 * back to the other controller, not to the backend on this side. 12247 */ 12248 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12249 12250 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12251 12252 fe_datamove(io); 12253 12254 return; 12255 12256} 12257 12258static int 12259ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12260{ 12261#if 0 12262 char str[256]; 12263 char path_str[64]; 12264 struct sbuf sb; 12265#endif 12266 12267 /* 12268 * In this case, we had to malloc the memory locally. Free it. 12269 */ 12270 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12271 int i; 12272 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12273 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12274 } 12275 12276#if 0 12277 scsi_path_string(io, path_str, sizeof(path_str)); 12278 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12279 sbuf_cat(&sb, path_str); 12280 scsi_command_string(&io->scsiio, NULL, &sb); 12281 sbuf_printf(&sb, "\n"); 12282 sbuf_cat(&sb, path_str); 12283 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12284 io->scsiio.tag_num, io->scsiio.tag_type); 12285 sbuf_cat(&sb, path_str); 12286 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12287 io->io_hdr.flags, io->io_hdr.status); 12288 sbuf_finish(&sb); 12289 printk("%s", sbuf_data(&sb)); 12290#endif 12291 12292 12293 /* 12294 * The read is done, now we need to send status (good or bad) back 12295 * to the other side. 12296 */ 12297 ctl_send_datamove_done(io, /*have_lock*/ 0); 12298 12299 return (0); 12300} 12301 12302static void 12303ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12304{ 12305 union ctl_io *io; 12306 void (*fe_datamove)(union ctl_io *io); 12307 12308 io = rq->context; 12309 12310 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12311 printf("%s: ISC DMA read failed with error %d", __func__, 12312 rq->ret); 12313 ctl_set_internal_failure(&io->scsiio, 12314 /*sks_valid*/ 1, 12315 /*retry_count*/ rq->ret); 12316 } 12317 12318 ctl_dt_req_free(rq); 12319 12320 /* Switch the pointer over so the FETD knows what to do */ 12321 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12322 12323 /* 12324 * Use a custom move done callback, since we need to send completion 12325 * back to the other controller, not to the backend on this side. 12326 */ 12327 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12328 12329 /* XXX KDM add checks like the ones in ctl_datamove? */ 12330 12331 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12332 12333 fe_datamove(io); 12334} 12335 12336static int 12337ctl_datamove_remote_sgl_setup(union ctl_io *io) 12338{ 12339 struct ctl_sg_entry *local_sglist, *remote_sglist; 12340 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12341 struct ctl_softc *softc; 12342 int retval; 12343 int i; 12344 12345 retval = 0; 12346 softc = control_softc; 12347 12348 local_sglist = io->io_hdr.local_sglist; 12349 local_dma_sglist = io->io_hdr.local_dma_sglist; 12350 remote_sglist = io->io_hdr.remote_sglist; 12351 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12352 12353 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12354 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12355 local_sglist[i].len = remote_sglist[i].len; 12356 12357 /* 12358 * XXX Detect the situation where the RS-level I/O 12359 * redirector on the other side has already read the 12360 * data off of the AOR RS on this side, and 12361 * transferred it to remote (mirror) memory on the 12362 * other side. Since we already have the data in 12363 * memory here, we just need to use it. 12364 * 12365 * XXX KDM this can probably be removed once we 12366 * get the cache device code in and take the 12367 * current AOR implementation out. 12368 */ 12369#ifdef NEEDTOPORT 12370 if ((remote_sglist[i].addr >= 12371 (void *)vtophys(softc->mirr->addr)) 12372 && (remote_sglist[i].addr < 12373 ((void *)vtophys(softc->mirr->addr) + 12374 CacheMirrorOffset))) { 12375 local_sglist[i].addr = remote_sglist[i].addr - 12376 CacheMirrorOffset; 12377 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12378 CTL_FLAG_DATA_IN) 12379 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12380 } else { 12381 local_sglist[i].addr = remote_sglist[i].addr + 12382 CacheMirrorOffset; 12383 } 12384#endif 12385#if 0 12386 printf("%s: local %p, remote %p, len %d\n", 12387 __func__, local_sglist[i].addr, 12388 remote_sglist[i].addr, local_sglist[i].len); 12389#endif 12390 } 12391 } else { 12392 uint32_t len_to_go; 12393 12394 /* 12395 * In this case, we don't have automatically allocated 12396 * memory for this I/O on this controller. This typically 12397 * happens with internal CTL I/O -- e.g. inquiry, mode 12398 * sense, etc. Anything coming from RAIDCore will have 12399 * a mirror area available. 12400 */ 12401 len_to_go = io->scsiio.kern_data_len; 12402 12403 /* 12404 * Clear the no datasync flag, we have to use malloced 12405 * buffers. 12406 */ 12407 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12408 12409 /* 12410 * The difficult thing here is that the size of the various 12411 * S/G segments may be different than the size from the 12412 * remote controller. That'll make it harder when DMAing 12413 * the data back to the other side. 12414 */ 12415 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12416 sizeof(io->io_hdr.remote_sglist[0])) && 12417 (len_to_go > 0); i++) { 12418 local_sglist[i].len = ctl_min(len_to_go, 131072); 12419 CTL_SIZE_8B(local_dma_sglist[i].len, 12420 local_sglist[i].len); 12421 local_sglist[i].addr = 12422 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12423 12424 local_dma_sglist[i].addr = local_sglist[i].addr; 12425 12426 if (local_sglist[i].addr == NULL) { 12427 int j; 12428 12429 printf("malloc failed for %zd bytes!", 12430 local_dma_sglist[i].len); 12431 for (j = 0; j < i; j++) { 12432 free(local_sglist[j].addr, M_CTL); 12433 } 12434 ctl_set_internal_failure(&io->scsiio, 12435 /*sks_valid*/ 1, 12436 /*retry_count*/ 4857); 12437 retval = 1; 12438 goto bailout_error; 12439 12440 } 12441 /* XXX KDM do we need a sync here? */ 12442 12443 len_to_go -= local_sglist[i].len; 12444 } 12445 /* 12446 * Reset the number of S/G entries accordingly. The 12447 * original number of S/G entries is available in 12448 * rem_sg_entries. 12449 */ 12450 io->scsiio.kern_sg_entries = i; 12451 12452#if 0 12453 printf("%s: kern_sg_entries = %d\n", __func__, 12454 io->scsiio.kern_sg_entries); 12455 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12456 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 12457 local_sglist[i].addr, local_sglist[i].len, 12458 local_dma_sglist[i].len); 12459#endif 12460 } 12461 12462 12463 return (retval); 12464 12465bailout_error: 12466 12467 ctl_send_datamove_done(io, /*have_lock*/ 0); 12468 12469 return (retval); 12470} 12471 12472static int 12473ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12474 ctl_ha_dt_cb callback) 12475{ 12476 struct ctl_ha_dt_req *rq; 12477 struct ctl_sg_entry *remote_sglist, *local_sglist; 12478 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 12479 uint32_t local_used, remote_used, total_used; 12480 int retval; 12481 int i, j; 12482 12483 retval = 0; 12484 12485 rq = ctl_dt_req_alloc(); 12486 12487 /* 12488 * If we failed to allocate the request, and if the DMA didn't fail 12489 * anyway, set busy status. This is just a resource allocation 12490 * failure. 12491 */ 12492 if ((rq == NULL) 12493 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 12494 ctl_set_busy(&io->scsiio); 12495 12496 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 12497 12498 if (rq != NULL) 12499 ctl_dt_req_free(rq); 12500 12501 /* 12502 * The data move failed. We need to return status back 12503 * to the other controller. No point in trying to DMA 12504 * data to the remote controller. 12505 */ 12506 12507 ctl_send_datamove_done(io, /*have_lock*/ 0); 12508 12509 retval = 1; 12510 12511 goto bailout; 12512 } 12513 12514 local_sglist = io->io_hdr.local_sglist; 12515 local_dma_sglist = io->io_hdr.local_dma_sglist; 12516 remote_sglist = io->io_hdr.remote_sglist; 12517 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12518 local_used = 0; 12519 remote_used = 0; 12520 total_used = 0; 12521 12522 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 12523 rq->ret = CTL_HA_STATUS_SUCCESS; 12524 rq->context = io; 12525 callback(rq); 12526 goto bailout; 12527 } 12528 12529 /* 12530 * Pull/push the data over the wire from/to the other controller. 12531 * This takes into account the possibility that the local and 12532 * remote sglists may not be identical in terms of the size of 12533 * the elements and the number of elements. 12534 * 12535 * One fundamental assumption here is that the length allocated for 12536 * both the local and remote sglists is identical. Otherwise, we've 12537 * essentially got a coding error of some sort. 12538 */ 12539 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12540 int isc_ret; 12541 uint32_t cur_len, dma_length; 12542 uint8_t *tmp_ptr; 12543 12544 rq->id = CTL_HA_DATA_CTL; 12545 rq->command = command; 12546 rq->context = io; 12547 12548 /* 12549 * Both pointers should be aligned. But it is possible 12550 * that the allocation length is not. They should both 12551 * also have enough slack left over at the end, though, 12552 * to round up to the next 8 byte boundary. 12553 */ 12554 cur_len = ctl_min(local_sglist[i].len - local_used, 12555 remote_sglist[j].len - remote_used); 12556 12557 /* 12558 * In this case, we have a size issue and need to decrease 12559 * the size, except in the case where we actually have less 12560 * than 8 bytes left. In that case, we need to increase 12561 * the DMA length to get the last bit. 12562 */ 12563 if ((cur_len & 0x7) != 0) { 12564 if (cur_len > 0x7) { 12565 cur_len = cur_len - (cur_len & 0x7); 12566 dma_length = cur_len; 12567 } else { 12568 CTL_SIZE_8B(dma_length, cur_len); 12569 } 12570 12571 } else 12572 dma_length = cur_len; 12573 12574 /* 12575 * If we had to allocate memory for this I/O, instead of using 12576 * the non-cached mirror memory, we'll need to flush the cache 12577 * before trying to DMA to the other controller. 12578 * 12579 * We could end up doing this multiple times for the same 12580 * segment if we have a larger local segment than remote 12581 * segment. That shouldn't be an issue. 12582 */ 12583 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12584 /* 12585 * XXX KDM use bus_dmamap_sync() here. 12586 */ 12587 } 12588 12589 rq->size = dma_length; 12590 12591 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12592 tmp_ptr += local_used; 12593 12594 /* Use physical addresses when talking to ISC hardware */ 12595 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12596 /* XXX KDM use busdma */ 12597#if 0 12598 rq->local = vtophys(tmp_ptr); 12599#endif 12600 } else 12601 rq->local = tmp_ptr; 12602 12603 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12604 tmp_ptr += remote_used; 12605 rq->remote = tmp_ptr; 12606 12607 rq->callback = NULL; 12608 12609 local_used += cur_len; 12610 if (local_used >= local_sglist[i].len) { 12611 i++; 12612 local_used = 0; 12613 } 12614 12615 remote_used += cur_len; 12616 if (remote_used >= remote_sglist[j].len) { 12617 j++; 12618 remote_used = 0; 12619 } 12620 total_used += cur_len; 12621 12622 if (total_used >= io->scsiio.kern_data_len) 12623 rq->callback = callback; 12624 12625 if ((rq->size & 0x7) != 0) { 12626 printf("%s: warning: size %d is not on 8b boundary\n", 12627 __func__, rq->size); 12628 } 12629 if (((uintptr_t)rq->local & 0x7) != 0) { 12630 printf("%s: warning: local %p not on 8b boundary\n", 12631 __func__, rq->local); 12632 } 12633 if (((uintptr_t)rq->remote & 0x7) != 0) { 12634 printf("%s: warning: remote %p not on 8b boundary\n", 12635 __func__, rq->local); 12636 } 12637#if 0 12638 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 12639 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12640 rq->local, rq->remote, rq->size); 12641#endif 12642 12643 isc_ret = ctl_dt_single(rq); 12644 if (isc_ret == CTL_HA_STATUS_WAIT) 12645 continue; 12646 12647 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 12648 rq->ret = CTL_HA_STATUS_SUCCESS; 12649 } else { 12650 rq->ret = isc_ret; 12651 } 12652 callback(rq); 12653 goto bailout; 12654 } 12655 12656bailout: 12657 return (retval); 12658 12659} 12660 12661static void 12662ctl_datamove_remote_read(union ctl_io *io) 12663{ 12664 int retval; 12665 int i; 12666 12667 /* 12668 * This will send an error to the other controller in the case of a 12669 * failure. 12670 */ 12671 retval = ctl_datamove_remote_sgl_setup(io); 12672 if (retval != 0) 12673 return; 12674 12675 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 12676 ctl_datamove_remote_read_cb); 12677 if ((retval != 0) 12678 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 12679 /* 12680 * Make sure we free memory if there was an error.. The 12681 * ctl_datamove_remote_xfer() function will send the 12682 * datamove done message, or call the callback with an 12683 * error if there is a problem. 12684 */ 12685 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12686 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12687 } 12688 12689 return; 12690} 12691 12692/* 12693 * Process a datamove request from the other controller. This is used for 12694 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 12695 * first. Once that is complete, the data gets DMAed into the remote 12696 * controller's memory. For reads, we DMA from the remote controller's 12697 * memory into our memory first, and then move it out to the FETD. 12698 */ 12699static void 12700ctl_datamove_remote(union ctl_io *io) 12701{ 12702 struct ctl_softc *softc; 12703 12704 softc = control_softc; 12705 12706 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 12707 12708 /* 12709 * Note that we look for an aborted I/O here, but don't do some of 12710 * the other checks that ctl_datamove() normally does. We don't 12711 * need to run the task queue, because this I/O is on the ISC 12712 * queue, which is executed by the work thread after the task queue. 12713 * We don't need to run the datamove delay code, since that should 12714 * have been done if need be on the other controller. 12715 */ 12716 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12717 12718 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 12719 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 12720 io->io_hdr.nexus.targ_port, 12721 io->io_hdr.nexus.targ_target.id, 12722 io->io_hdr.nexus.targ_lun); 12723 io->io_hdr.status = CTL_CMD_ABORTED; 12724 io->io_hdr.port_status = 31338; 12725 12726 ctl_send_datamove_done(io, /*have_lock*/ 0); 12727 12728 return; 12729 } 12730 12731 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 12732 ctl_datamove_remote_write(io); 12733 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 12734 ctl_datamove_remote_read(io); 12735 } else { 12736 union ctl_ha_msg msg; 12737 struct scsi_sense_data *sense; 12738 uint8_t sks[3]; 12739 int retry_count; 12740 12741 memset(&msg, 0, sizeof(msg)); 12742 12743 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 12744 msg.hdr.status = CTL_SCSI_ERROR; 12745 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 12746 12747 retry_count = 4243; 12748 12749 sense = &msg.scsi.sense_data; 12750 sks[0] = SSD_SCS_VALID; 12751 sks[1] = (retry_count >> 8) & 0xff; 12752 sks[2] = retry_count & 0xff; 12753 12754 /* "Internal target failure" */ 12755 scsi_set_sense_data(sense, 12756 /*sense_format*/ SSD_TYPE_NONE, 12757 /*current_error*/ 1, 12758 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 12759 /*asc*/ 0x44, 12760 /*ascq*/ 0x00, 12761 /*type*/ SSD_ELEM_SKS, 12762 /*size*/ sizeof(sks), 12763 /*data*/ sks, 12764 SSD_ELEM_NONE); 12765 12766 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12767 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12768 ctl_failover_io(io, /*have_lock*/ 1); 12769 return; 12770 } 12771 12772 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 12773 CTL_HA_STATUS_SUCCESS) { 12774 /* XXX KDM what to do if this fails? */ 12775 } 12776 return; 12777 } 12778 12779} 12780 12781static int 12782ctl_process_done(union ctl_io *io) 12783{ 12784 struct ctl_lun *lun; 12785 struct ctl_softc *ctl_softc; 12786 void (*fe_done)(union ctl_io *io); 12787 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 12788 12789 CTL_DEBUG_PRINT(("ctl_process_done\n")); 12790 12791 fe_done = 12792 control_softc->ctl_ports[targ_port]->fe_done; 12793 12794#ifdef CTL_TIME_IO 12795 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12796 char str[256]; 12797 char path_str[64]; 12798 struct sbuf sb; 12799 12800 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12801 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12802 12803 sbuf_cat(&sb, path_str); 12804 switch (io->io_hdr.io_type) { 12805 case CTL_IO_SCSI: 12806 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12807 sbuf_printf(&sb, "\n"); 12808 sbuf_cat(&sb, path_str); 12809 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12810 io->scsiio.tag_num, io->scsiio.tag_type); 12811 break; 12812 case CTL_IO_TASK: 12813 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12814 "Tag Type: %d\n", io->taskio.task_action, 12815 io->taskio.tag_num, io->taskio.tag_type); 12816 break; 12817 default: 12818 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12819 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12820 break; 12821 } 12822 sbuf_cat(&sb, path_str); 12823 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 12824 (intmax_t)time_uptime - io->io_hdr.start_time); 12825 sbuf_finish(&sb); 12826 printf("%s", sbuf_data(&sb)); 12827 } 12828#endif /* CTL_TIME_IO */ 12829 12830 switch (io->io_hdr.io_type) { 12831 case CTL_IO_SCSI: 12832 break; 12833 case CTL_IO_TASK: 12834 if (bootverbose || verbose > 0) 12835 ctl_io_error_print(io, NULL); 12836 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 12837 ctl_free_io(io); 12838 else 12839 fe_done(io); 12840 return (CTL_RETVAL_COMPLETE); 12841 break; 12842 default: 12843 printf("ctl_process_done: invalid io type %d\n", 12844 io->io_hdr.io_type); 12845 panic("ctl_process_done: invalid io type %d\n", 12846 io->io_hdr.io_type); 12847 break; /* NOTREACHED */ 12848 } 12849 12850 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12851 if (lun == NULL) { 12852 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 12853 io->io_hdr.nexus.targ_mapped_lun)); 12854 fe_done(io); 12855 goto bailout; 12856 } 12857 ctl_softc = lun->ctl_softc; 12858 12859 mtx_lock(&lun->lun_lock); 12860 12861 /* 12862 * Check to see if we have any errors to inject here. We only 12863 * inject errors for commands that don't already have errors set. 12864 */ 12865 if ((STAILQ_FIRST(&lun->error_list) != NULL) 12866 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) 12867 ctl_inject_error(lun, io); 12868 12869 /* 12870 * XXX KDM how do we treat commands that aren't completed 12871 * successfully? 12872 * 12873 * XXX KDM should we also track I/O latency? 12874 */ 12875 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 12876 io->io_hdr.io_type == CTL_IO_SCSI) { 12877#ifdef CTL_TIME_IO 12878 struct bintime cur_bt; 12879#endif 12880 int type; 12881 12882 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12883 CTL_FLAG_DATA_IN) 12884 type = CTL_STATS_READ; 12885 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12886 CTL_FLAG_DATA_OUT) 12887 type = CTL_STATS_WRITE; 12888 else 12889 type = CTL_STATS_NO_IO; 12890 12891 lun->stats.ports[targ_port].bytes[type] += 12892 io->scsiio.kern_total_len; 12893 lun->stats.ports[targ_port].operations[type]++; 12894#ifdef CTL_TIME_IO 12895 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 12896 &io->io_hdr.dma_bt); 12897 lun->stats.ports[targ_port].num_dmas[type] += 12898 io->io_hdr.num_dmas; 12899 getbintime(&cur_bt); 12900 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 12901 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 12902#endif 12903 } 12904 12905 /* 12906 * Remove this from the OOA queue. 12907 */ 12908 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 12909 12910 /* 12911 * Run through the blocked queue on this LUN and see if anything 12912 * has become unblocked, now that this transaction is done. 12913 */ 12914 ctl_check_blocked(lun); 12915 12916 /* 12917 * If the LUN has been invalidated, free it if there is nothing 12918 * left on its OOA queue. 12919 */ 12920 if ((lun->flags & CTL_LUN_INVALID) 12921 && TAILQ_EMPTY(&lun->ooa_queue)) { 12922 mtx_unlock(&lun->lun_lock); 12923 mtx_lock(&ctl_softc->ctl_lock); 12924 ctl_free_lun(lun); 12925 mtx_unlock(&ctl_softc->ctl_lock); 12926 } else 12927 mtx_unlock(&lun->lun_lock); 12928 12929 /* 12930 * If this command has been aborted, make sure we set the status 12931 * properly. The FETD is responsible for freeing the I/O and doing 12932 * whatever it needs to do to clean up its state. 12933 */ 12934 if (io->io_hdr.flags & CTL_FLAG_ABORT) 12935 io->io_hdr.status = CTL_CMD_ABORTED; 12936 12937 /* 12938 * We print out status for every task management command. For SCSI 12939 * commands, we filter out any unit attention errors; they happen 12940 * on every boot, and would clutter up the log. Note: task 12941 * management commands aren't printed here, they are printed above, 12942 * since they should never even make it down here. 12943 */ 12944 switch (io->io_hdr.io_type) { 12945 case CTL_IO_SCSI: { 12946 int error_code, sense_key, asc, ascq; 12947 12948 sense_key = 0; 12949 12950 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) 12951 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 12952 /* 12953 * Since this is just for printing, no need to 12954 * show errors here. 12955 */ 12956 scsi_extract_sense_len(&io->scsiio.sense_data, 12957 io->scsiio.sense_len, 12958 &error_code, 12959 &sense_key, 12960 &asc, 12961 &ascq, 12962 /*show_errors*/ 0); 12963 } 12964 12965 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) 12966 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) 12967 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) 12968 || (sense_key != SSD_KEY_UNIT_ATTENTION))) { 12969 12970 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){ 12971 ctl_softc->skipped_prints++; 12972 } else { 12973 uint32_t skipped_prints; 12974 12975 skipped_prints = ctl_softc->skipped_prints; 12976 12977 ctl_softc->skipped_prints = 0; 12978 ctl_softc->last_print_jiffies = time_uptime; 12979 12980 if (skipped_prints > 0) { 12981#ifdef NEEDTOPORT 12982 csevent_log(CSC_CTL | CSC_SHELF_SW | 12983 CTL_ERROR_REPORT, 12984 csevent_LogType_Trace, 12985 csevent_Severity_Information, 12986 csevent_AlertLevel_Green, 12987 csevent_FRU_Firmware, 12988 csevent_FRU_Unknown, 12989 "High CTL error volume, %d prints " 12990 "skipped", skipped_prints); 12991#endif 12992 } 12993 if (bootverbose || verbose > 0) 12994 ctl_io_error_print(io, NULL); 12995 } 12996 } 12997 break; 12998 } 12999 case CTL_IO_TASK: 13000 if (bootverbose || verbose > 0) 13001 ctl_io_error_print(io, NULL); 13002 break; 13003 default: 13004 break; 13005 } 13006 13007 /* 13008 * Tell the FETD or the other shelf controller we're done with this 13009 * command. Note that only SCSI commands get to this point. Task 13010 * management commands are completed above. 13011 * 13012 * We only send status to the other controller if we're in XFER 13013 * mode. In SER_ONLY mode, the I/O is done on the controller that 13014 * received the I/O (from CTL's perspective), and so the status is 13015 * generated there. 13016 * 13017 * XXX KDM if we hold the lock here, we could cause a deadlock 13018 * if the frontend comes back in in this context to queue 13019 * something. 13020 */ 13021 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13022 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13023 union ctl_ha_msg msg; 13024 13025 memset(&msg, 0, sizeof(msg)); 13026 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13027 msg.hdr.original_sc = io->io_hdr.original_sc; 13028 msg.hdr.nexus = io->io_hdr.nexus; 13029 msg.hdr.status = io->io_hdr.status; 13030 msg.scsi.scsi_status = io->scsiio.scsi_status; 13031 msg.scsi.tag_num = io->scsiio.tag_num; 13032 msg.scsi.tag_type = io->scsiio.tag_type; 13033 msg.scsi.sense_len = io->scsiio.sense_len; 13034 msg.scsi.sense_residual = io->scsiio.sense_residual; 13035 msg.scsi.residual = io->scsiio.residual; 13036 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13037 sizeof(io->scsiio.sense_data)); 13038 /* 13039 * We copy this whether or not this is an I/O-related 13040 * command. Otherwise, we'd have to go and check to see 13041 * whether it's a read/write command, and it really isn't 13042 * worth it. 13043 */ 13044 memcpy(&msg.scsi.lbalen, 13045 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13046 sizeof(msg.scsi.lbalen)); 13047 13048 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13049 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13050 /* XXX do something here */ 13051 } 13052 13053 ctl_free_io(io); 13054 } else 13055 fe_done(io); 13056 13057bailout: 13058 13059 return (CTL_RETVAL_COMPLETE); 13060} 13061 13062/* 13063 * Front end should call this if it doesn't do autosense. When the request 13064 * sense comes back in from the initiator, we'll dequeue this and send it. 13065 */ 13066int 13067ctl_queue_sense(union ctl_io *io) 13068{ 13069 struct ctl_lun *lun; 13070 struct ctl_softc *ctl_softc; 13071 uint32_t initidx, targ_lun; 13072 13073 ctl_softc = control_softc; 13074 13075 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13076 13077 /* 13078 * LUN lookup will likely move to the ctl_work_thread() once we 13079 * have our new queueing infrastructure (that doesn't put things on 13080 * a per-LUN queue initially). That is so that we can handle 13081 * things like an INQUIRY to a LUN that we don't have enabled. We 13082 * can't deal with that right now. 13083 */ 13084 mtx_lock(&ctl_softc->ctl_lock); 13085 13086 /* 13087 * If we don't have a LUN for this, just toss the sense 13088 * information. 13089 */ 13090 targ_lun = io->io_hdr.nexus.targ_lun; 13091 if (io->io_hdr.nexus.lun_map_fn != NULL) 13092 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun); 13093 if ((targ_lun < CTL_MAX_LUNS) 13094 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13095 lun = ctl_softc->ctl_luns[targ_lun]; 13096 else 13097 goto bailout; 13098 13099 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13100 13101 mtx_lock(&lun->lun_lock); 13102 /* 13103 * Already have CA set for this LUN...toss the sense information. 13104 */ 13105 if (ctl_is_set(lun->have_ca, initidx)) { 13106 mtx_unlock(&lun->lun_lock); 13107 goto bailout; 13108 } 13109 13110 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data, 13111 ctl_min(sizeof(lun->pending_sense[initidx].sense), 13112 sizeof(io->scsiio.sense_data))); 13113 ctl_set_mask(lun->have_ca, initidx); 13114 mtx_unlock(&lun->lun_lock); 13115 13116bailout: 13117 mtx_unlock(&ctl_softc->ctl_lock); 13118 13119 ctl_free_io(io); 13120 13121 return (CTL_RETVAL_COMPLETE); 13122} 13123 13124/* 13125 * Primary command inlet from frontend ports. All SCSI and task I/O 13126 * requests must go through this function. 13127 */ 13128int 13129ctl_queue(union ctl_io *io) 13130{ 13131 struct ctl_softc *ctl_softc; 13132 13133 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13134 13135 ctl_softc = control_softc; 13136 13137#ifdef CTL_TIME_IO 13138 io->io_hdr.start_time = time_uptime; 13139 getbintime(&io->io_hdr.start_bt); 13140#endif /* CTL_TIME_IO */ 13141 13142 /* Map FE-specific LUN ID into global one. */ 13143 if (io->io_hdr.nexus.lun_map_fn != NULL) 13144 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.lun_map_fn( 13145 io->io_hdr.nexus.lun_map_arg, io->io_hdr.nexus.targ_lun); 13146 else 13147 io->io_hdr.nexus.targ_mapped_lun = io->io_hdr.nexus.targ_lun; 13148 13149 switch (io->io_hdr.io_type) { 13150 case CTL_IO_SCSI: 13151 case CTL_IO_TASK: 13152 ctl_enqueue_incoming(io); 13153 break; 13154 default: 13155 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13156 return (EINVAL); 13157 } 13158 13159 return (CTL_RETVAL_COMPLETE); 13160} 13161 13162#ifdef CTL_IO_DELAY 13163static void 13164ctl_done_timer_wakeup(void *arg) 13165{ 13166 union ctl_io *io; 13167 13168 io = (union ctl_io *)arg; 13169 ctl_done(io); 13170} 13171#endif /* CTL_IO_DELAY */ 13172 13173void 13174ctl_done(union ctl_io *io) 13175{ 13176 struct ctl_softc *ctl_softc; 13177 13178 ctl_softc = control_softc; 13179 13180 /* 13181 * Enable this to catch duplicate completion issues. 13182 */ 13183#if 0 13184 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13185 printf("%s: type %d msg %d cdb %x iptl: " 13186 "%d:%d:%d:%d tag 0x%04x " 13187 "flag %#x status %x\n", 13188 __func__, 13189 io->io_hdr.io_type, 13190 io->io_hdr.msg_type, 13191 io->scsiio.cdb[0], 13192 io->io_hdr.nexus.initid.id, 13193 io->io_hdr.nexus.targ_port, 13194 io->io_hdr.nexus.targ_target.id, 13195 io->io_hdr.nexus.targ_lun, 13196 (io->io_hdr.io_type == 13197 CTL_IO_TASK) ? 13198 io->taskio.tag_num : 13199 io->scsiio.tag_num, 13200 io->io_hdr.flags, 13201 io->io_hdr.status); 13202 } else 13203 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13204#endif 13205 13206 /* 13207 * This is an internal copy of an I/O, and should not go through 13208 * the normal done processing logic. 13209 */ 13210 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13211 return; 13212 13213 /* 13214 * We need to send a msg to the serializing shelf to finish the IO 13215 * as well. We don't send a finish message to the other shelf if 13216 * this is a task management command. Task management commands 13217 * aren't serialized in the OOA queue, but rather just executed on 13218 * both shelf controllers for commands that originated on that 13219 * controller. 13220 */ 13221 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13222 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13223 union ctl_ha_msg msg_io; 13224 13225 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13226 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13227 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13228 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13229 } 13230 /* continue on to finish IO */ 13231 } 13232#ifdef CTL_IO_DELAY 13233 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13234 struct ctl_lun *lun; 13235 13236 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13237 13238 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13239 } else { 13240 struct ctl_lun *lun; 13241 13242 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13243 13244 if ((lun != NULL) 13245 && (lun->delay_info.done_delay > 0)) { 13246 struct callout *callout; 13247 13248 callout = (struct callout *)&io->io_hdr.timer_bytes; 13249 callout_init(callout, /*mpsafe*/ 1); 13250 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13251 callout_reset(callout, 13252 lun->delay_info.done_delay * hz, 13253 ctl_done_timer_wakeup, io); 13254 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13255 lun->delay_info.done_delay = 0; 13256 return; 13257 } 13258 } 13259#endif /* CTL_IO_DELAY */ 13260 13261 ctl_enqueue_done(io); 13262} 13263 13264int 13265ctl_isc(struct ctl_scsiio *ctsio) 13266{ 13267 struct ctl_lun *lun; 13268 int retval; 13269 13270 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13271 13272 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13273 13274 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13275 13276 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13277 13278 return (retval); 13279} 13280 13281 13282static void 13283ctl_work_thread(void *arg) 13284{ 13285 struct ctl_thread *thr = (struct ctl_thread *)arg; 13286 struct ctl_softc *softc = thr->ctl_softc; 13287 union ctl_io *io; 13288 int retval; 13289 13290 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13291 13292 for (;;) { 13293 retval = 0; 13294 13295 /* 13296 * We handle the queues in this order: 13297 * - ISC 13298 * - done queue (to free up resources, unblock other commands) 13299 * - RtR queue 13300 * - incoming queue 13301 * 13302 * If those queues are empty, we break out of the loop and 13303 * go to sleep. 13304 */ 13305 mtx_lock(&thr->queue_lock); 13306 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13307 if (io != NULL) { 13308 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13309 mtx_unlock(&thr->queue_lock); 13310 ctl_handle_isc(io); 13311 continue; 13312 } 13313 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13314 if (io != NULL) { 13315 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13316 /* clear any blocked commands, call fe_done */ 13317 mtx_unlock(&thr->queue_lock); 13318 retval = ctl_process_done(io); 13319 continue; 13320 } 13321 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13322 if (io != NULL) { 13323 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13324 mtx_unlock(&thr->queue_lock); 13325 if (io->io_hdr.io_type == CTL_IO_TASK) 13326 ctl_run_task(io); 13327 else 13328 ctl_scsiio_precheck(softc, &io->scsiio); 13329 continue; 13330 } 13331 if (!ctl_pause_rtr) { 13332 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13333 if (io != NULL) { 13334 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13335 mtx_unlock(&thr->queue_lock); 13336 retval = ctl_scsiio(&io->scsiio); 13337 if (retval != CTL_RETVAL_COMPLETE) 13338 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13339 continue; 13340 } 13341 } 13342 13343 /* Sleep until we have something to do. */ 13344 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13345 } 13346} 13347 13348static void 13349ctl_lun_thread(void *arg) 13350{ 13351 struct ctl_softc *softc = (struct ctl_softc *)arg; 13352 struct ctl_be_lun *be_lun; 13353 int retval; 13354 13355 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13356 13357 for (;;) { 13358 retval = 0; 13359 mtx_lock(&softc->ctl_lock); 13360 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13361 if (be_lun != NULL) { 13362 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13363 mtx_unlock(&softc->ctl_lock); 13364 ctl_create_lun(be_lun); 13365 continue; 13366 } 13367 13368 /* Sleep until we have something to do. */ 13369 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13370 PDROP | PRIBIO, "-", 0); 13371 } 13372} 13373 13374static void 13375ctl_enqueue_incoming(union ctl_io *io) 13376{ 13377 struct ctl_softc *softc = control_softc; 13378 struct ctl_thread *thr; 13379 u_int idx; 13380 13381 idx = (io->io_hdr.nexus.targ_port * 127 + 13382 io->io_hdr.nexus.initid.id) % worker_threads; 13383 thr = &softc->threads[idx]; 13384 mtx_lock(&thr->queue_lock); 13385 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13386 mtx_unlock(&thr->queue_lock); 13387 wakeup(thr); 13388} 13389 13390static void 13391ctl_enqueue_rtr(union ctl_io *io) 13392{ 13393 struct ctl_softc *softc = control_softc; 13394 struct ctl_thread *thr; 13395 13396 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13397 mtx_lock(&thr->queue_lock); 13398 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13399 mtx_unlock(&thr->queue_lock); 13400 wakeup(thr); 13401} 13402 13403static void 13404ctl_enqueue_done(union ctl_io *io) 13405{ 13406 struct ctl_softc *softc = control_softc; 13407 struct ctl_thread *thr; 13408 13409 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13410 mtx_lock(&thr->queue_lock); 13411 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13412 mtx_unlock(&thr->queue_lock); 13413 wakeup(thr); 13414} 13415 13416static void 13417ctl_enqueue_isc(union ctl_io *io) 13418{ 13419 struct ctl_softc *softc = control_softc; 13420 struct ctl_thread *thr; 13421 13422 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13423 mtx_lock(&thr->queue_lock); 13424 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13425 mtx_unlock(&thr->queue_lock); 13426 wakeup(thr); 13427} 13428 13429/* Initialization and failover */ 13430 13431void 13432ctl_init_isc_msg(void) 13433{ 13434 printf("CTL: Still calling this thing\n"); 13435} 13436 13437/* 13438 * Init component 13439 * Initializes component into configuration defined by bootMode 13440 * (see hasc-sv.c) 13441 * returns hasc_Status: 13442 * OK 13443 * ERROR - fatal error 13444 */ 13445static ctl_ha_comp_status 13446ctl_isc_init(struct ctl_ha_component *c) 13447{ 13448 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13449 13450 c->status = ret; 13451 return ret; 13452} 13453 13454/* Start component 13455 * Starts component in state requested. If component starts successfully, 13456 * it must set its own state to the requestrd state 13457 * When requested state is HASC_STATE_HA, the component may refine it 13458 * by adding _SLAVE or _MASTER flags. 13459 * Currently allowed state transitions are: 13460 * UNKNOWN->HA - initial startup 13461 * UNKNOWN->SINGLE - initial startup when no parter detected 13462 * HA->SINGLE - failover 13463 * returns ctl_ha_comp_status: 13464 * OK - component successfully started in requested state 13465 * FAILED - could not start the requested state, failover may 13466 * be possible 13467 * ERROR - fatal error detected, no future startup possible 13468 */ 13469static ctl_ha_comp_status 13470ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 13471{ 13472 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 13473 13474 printf("%s: go\n", __func__); 13475 13476 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 13477 if (c->state == CTL_HA_STATE_UNKNOWN ) { 13478 ctl_is_single = 0; 13479 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 13480 != CTL_HA_STATUS_SUCCESS) { 13481 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 13482 ret = CTL_HA_COMP_STATUS_ERROR; 13483 } 13484 } else if (CTL_HA_STATE_IS_HA(c->state) 13485 && CTL_HA_STATE_IS_SINGLE(state)){ 13486 // HA->SINGLE transition 13487 ctl_failover(); 13488 ctl_is_single = 1; 13489 } else { 13490 printf("ctl_isc_start:Invalid state transition %X->%X\n", 13491 c->state, state); 13492 ret = CTL_HA_COMP_STATUS_ERROR; 13493 } 13494 if (CTL_HA_STATE_IS_SINGLE(state)) 13495 ctl_is_single = 1; 13496 13497 c->state = state; 13498 c->status = ret; 13499 return ret; 13500} 13501 13502/* 13503 * Quiesce component 13504 * The component must clear any error conditions (set status to OK) and 13505 * prepare itself to another Start call 13506 * returns ctl_ha_comp_status: 13507 * OK 13508 * ERROR 13509 */ 13510static ctl_ha_comp_status 13511ctl_isc_quiesce(struct ctl_ha_component *c) 13512{ 13513 int ret = CTL_HA_COMP_STATUS_OK; 13514 13515 ctl_pause_rtr = 1; 13516 c->status = ret; 13517 return ret; 13518} 13519 13520struct ctl_ha_component ctl_ha_component_ctlisc = 13521{ 13522 .name = "CTL ISC", 13523 .state = CTL_HA_STATE_UNKNOWN, 13524 .init = ctl_isc_init, 13525 .start = ctl_isc_start, 13526 .quiesce = ctl_isc_quiesce 13527}; 13528 13529/* 13530 * vim: ts=8 13531 */ 13532