ctl.c revision 275404
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Edward Tomasz Napierala 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification. 15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 16 * substantially similar to the "NO WARRANTY" disclaimer below 17 * ("Disclaimer") and any redistribution must be conditioned upon 18 * including a substantially similar Disclaimer requirement for further 19 * binary redistribution. 20 * 21 * NO WARRANTY 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGES. 33 * 34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $ 35 */ 36/* 37 * CAM Target Layer, a SCSI device emulation subsystem. 38 * 39 * Author: Ken Merry <ken@FreeBSD.org> 40 */ 41 42#define _CTL_C 43 44#include <sys/cdefs.h> 45__FBSDID("$FreeBSD: head/sys/cam/ctl/ctl.c 275404 2014-12-02 12:31:28Z mav $"); 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/ctype.h> 50#include <sys/kernel.h> 51#include <sys/types.h> 52#include <sys/kthread.h> 53#include <sys/bio.h> 54#include <sys/fcntl.h> 55#include <sys/lock.h> 56#include <sys/module.h> 57#include <sys/mutex.h> 58#include <sys/condvar.h> 59#include <sys/malloc.h> 60#include <sys/conf.h> 61#include <sys/ioccom.h> 62#include <sys/queue.h> 63#include <sys/sbuf.h> 64#include <sys/smp.h> 65#include <sys/endian.h> 66#include <sys/sysctl.h> 67#include <vm/uma.h> 68 69#include <cam/cam.h> 70#include <cam/scsi/scsi_all.h> 71#include <cam/scsi/scsi_da.h> 72#include <cam/ctl/ctl_io.h> 73#include <cam/ctl/ctl.h> 74#include <cam/ctl/ctl_frontend.h> 75#include <cam/ctl/ctl_frontend_internal.h> 76#include <cam/ctl/ctl_util.h> 77#include <cam/ctl/ctl_backend.h> 78#include <cam/ctl/ctl_ioctl.h> 79#include <cam/ctl/ctl_ha.h> 80#include <cam/ctl/ctl_private.h> 81#include <cam/ctl/ctl_debug.h> 82#include <cam/ctl/ctl_scsi_all.h> 83#include <cam/ctl/ctl_error.h> 84 85struct ctl_softc *control_softc = NULL; 86 87/* 88 * Size and alignment macros needed for Copan-specific HA hardware. These 89 * can go away when the HA code is re-written, and uses busdma for any 90 * hardware. 91 */ 92#define CTL_ALIGN_8B(target, source, type) \ 93 if (((uint32_t)source & 0x7) != 0) \ 94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\ 95 else \ 96 target = (type)source; 97 98#define CTL_SIZE_8B(target, size) \ 99 if ((size & 0x7) != 0) \ 100 target = size + (0x8 - (size & 0x7)); \ 101 else \ 102 target = size; 103 104#define CTL_ALIGN_8B_MARGIN 16 105 106/* 107 * Template mode pages. 108 */ 109 110/* 111 * Note that these are default values only. The actual values will be 112 * filled in when the user does a mode sense. 113 */ 114static struct copan_debugconf_subpage debugconf_page_default = { 115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 116 DBGCNF_SUBPAGE_CODE, /* subpage */ 117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 119 DBGCNF_VERSION, /* page_version */ 120 {CTL_TIME_IO_DEFAULT_SECS>>8, 121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 122}; 123 124static struct copan_debugconf_subpage debugconf_page_changeable = { 125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 126 DBGCNF_SUBPAGE_CODE, /* subpage */ 127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 129 0, /* page_version */ 130 {0xff,0xff}, /* ctl_time_io_secs */ 131}; 132 133static struct scsi_da_rw_recovery_page rw_er_page_default = { 134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 137 /*read_retry_count*/0, 138 /*correction_span*/0, 139 /*head_offset_count*/0, 140 /*data_strobe_offset_cnt*/0, 141 /*byte8*/SMS_RWER_LBPERE, 142 /*write_retry_count*/0, 143 /*reserved2*/0, 144 /*recovery_time_limit*/{0, 0}, 145}; 146 147static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 150 /*byte3*/0, 151 /*read_retry_count*/0, 152 /*correction_span*/0, 153 /*head_offset_count*/0, 154 /*data_strobe_offset_cnt*/0, 155 /*byte8*/0, 156 /*write_retry_count*/0, 157 /*reserved2*/0, 158 /*recovery_time_limit*/{0, 0}, 159}; 160 161static struct scsi_format_page format_page_default = { 162 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 163 /*page_length*/sizeof(struct scsi_format_page) - 2, 164 /*tracks_per_zone*/ {0, 0}, 165 /*alt_sectors_per_zone*/ {0, 0}, 166 /*alt_tracks_per_zone*/ {0, 0}, 167 /*alt_tracks_per_lun*/ {0, 0}, 168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 170 /*bytes_per_sector*/ {0, 0}, 171 /*interleave*/ {0, 0}, 172 /*track_skew*/ {0, 0}, 173 /*cylinder_skew*/ {0, 0}, 174 /*flags*/ SFP_HSEC, 175 /*reserved*/ {0, 0, 0} 176}; 177 178static struct scsi_format_page format_page_changeable = { 179 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 180 /*page_length*/sizeof(struct scsi_format_page) - 2, 181 /*tracks_per_zone*/ {0, 0}, 182 /*alt_sectors_per_zone*/ {0, 0}, 183 /*alt_tracks_per_zone*/ {0, 0}, 184 /*alt_tracks_per_lun*/ {0, 0}, 185 /*sectors_per_track*/ {0, 0}, 186 /*bytes_per_sector*/ {0, 0}, 187 /*interleave*/ {0, 0}, 188 /*track_skew*/ {0, 0}, 189 /*cylinder_skew*/ {0, 0}, 190 /*flags*/ 0, 191 /*reserved*/ {0, 0, 0} 192}; 193 194static struct scsi_rigid_disk_page rigid_disk_page_default = { 195 /*page_code*/SMS_RIGID_DISK_PAGE, 196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 197 /*cylinders*/ {0, 0, 0}, 198 /*heads*/ CTL_DEFAULT_HEADS, 199 /*start_write_precomp*/ {0, 0, 0}, 200 /*start_reduced_current*/ {0, 0, 0}, 201 /*step_rate*/ {0, 0}, 202 /*landing_zone_cylinder*/ {0, 0, 0}, 203 /*rpl*/ SRDP_RPL_DISABLED, 204 /*rotational_offset*/ 0, 205 /*reserved1*/ 0, 206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 207 CTL_DEFAULT_ROTATION_RATE & 0xff}, 208 /*reserved2*/ {0, 0} 209}; 210 211static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 212 /*page_code*/SMS_RIGID_DISK_PAGE, 213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 214 /*cylinders*/ {0, 0, 0}, 215 /*heads*/ 0, 216 /*start_write_precomp*/ {0, 0, 0}, 217 /*start_reduced_current*/ {0, 0, 0}, 218 /*step_rate*/ {0, 0}, 219 /*landing_zone_cylinder*/ {0, 0, 0}, 220 /*rpl*/ 0, 221 /*rotational_offset*/ 0, 222 /*reserved1*/ 0, 223 /*rotation_rate*/ {0, 0}, 224 /*reserved2*/ {0, 0} 225}; 226 227static struct scsi_caching_page caching_page_default = { 228 /*page_code*/SMS_CACHING_PAGE, 229 /*page_length*/sizeof(struct scsi_caching_page) - 2, 230 /*flags1*/ SCP_DISC | SCP_WCE, 231 /*ret_priority*/ 0, 232 /*disable_pf_transfer_len*/ {0xff, 0xff}, 233 /*min_prefetch*/ {0, 0}, 234 /*max_prefetch*/ {0xff, 0xff}, 235 /*max_pf_ceiling*/ {0xff, 0xff}, 236 /*flags2*/ 0, 237 /*cache_segments*/ 0, 238 /*cache_seg_size*/ {0, 0}, 239 /*reserved*/ 0, 240 /*non_cache_seg_size*/ {0, 0, 0} 241}; 242 243static struct scsi_caching_page caching_page_changeable = { 244 /*page_code*/SMS_CACHING_PAGE, 245 /*page_length*/sizeof(struct scsi_caching_page) - 2, 246 /*flags1*/ SCP_WCE | SCP_RCD, 247 /*ret_priority*/ 0, 248 /*disable_pf_transfer_len*/ {0, 0}, 249 /*min_prefetch*/ {0, 0}, 250 /*max_prefetch*/ {0, 0}, 251 /*max_pf_ceiling*/ {0, 0}, 252 /*flags2*/ 0, 253 /*cache_segments*/ 0, 254 /*cache_seg_size*/ {0, 0}, 255 /*reserved*/ 0, 256 /*non_cache_seg_size*/ {0, 0, 0} 257}; 258 259static struct scsi_control_page control_page_default = { 260 /*page_code*/SMS_CONTROL_MODE_PAGE, 261 /*page_length*/sizeof(struct scsi_control_page) - 2, 262 /*rlec*/0, 263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 264 /*eca_and_aen*/0, 265 /*flags4*/SCP_TAS, 266 /*aen_holdoff_period*/{0, 0}, 267 /*busy_timeout_period*/{0, 0}, 268 /*extended_selftest_completion_time*/{0, 0} 269}; 270 271static struct scsi_control_page control_page_changeable = { 272 /*page_code*/SMS_CONTROL_MODE_PAGE, 273 /*page_length*/sizeof(struct scsi_control_page) - 2, 274 /*rlec*/SCP_DSENSE, 275 /*queue_flags*/SCP_QUEUE_ALG_MASK, 276 /*eca_and_aen*/SCP_SWP, 277 /*flags4*/0, 278 /*aen_holdoff_period*/{0, 0}, 279 /*busy_timeout_period*/{0, 0}, 280 /*extended_selftest_completion_time*/{0, 0} 281}; 282 283static struct scsi_info_exceptions_page ie_page_default = { 284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 286 /*info_flags*/SIEP_FLAGS_DEXCPT, 287 /*mrie*/0, 288 /*interval_timer*/{0, 0, 0, 0}, 289 /*report_count*/{0, 0, 0, 0} 290}; 291 292static struct scsi_info_exceptions_page ie_page_changeable = { 293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 295 /*info_flags*/0, 296 /*mrie*/0, 297 /*interval_timer*/{0, 0, 0, 0}, 298 /*report_count*/{0, 0, 0, 0} 299}; 300 301#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 302 303static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 305 /*subpage_code*/0x02, 306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 307 /*flags*/0, 308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 309 /*descr*/{}}, 310 {{/*flags*/0, 311 /*resource*/0x01, 312 /*reserved*/{0, 0}, 313 /*count*/{0, 0, 0, 0}}, 314 {/*flags*/0, 315 /*resource*/0x02, 316 /*reserved*/{0, 0}, 317 /*count*/{0, 0, 0, 0}}, 318 {/*flags*/0, 319 /*resource*/0xf1, 320 /*reserved*/{0, 0}, 321 /*count*/{0, 0, 0, 0}}, 322 {/*flags*/0, 323 /*resource*/0xf2, 324 /*reserved*/{0, 0}, 325 /*count*/{0, 0, 0, 0}} 326 } 327}; 328 329static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 331 /*subpage_code*/0x02, 332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 333 /*flags*/0, 334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 335 /*descr*/{}}, 336 {{/*flags*/0, 337 /*resource*/0, 338 /*reserved*/{0, 0}, 339 /*count*/{0, 0, 0, 0}}, 340 {/*flags*/0, 341 /*resource*/0, 342 /*reserved*/{0, 0}, 343 /*count*/{0, 0, 0, 0}}, 344 {/*flags*/0, 345 /*resource*/0, 346 /*reserved*/{0, 0}, 347 /*count*/{0, 0, 0, 0}}, 348 {/*flags*/0, 349 /*resource*/0, 350 /*reserved*/{0, 0}, 351 /*count*/{0, 0, 0, 0}} 352 } 353}; 354 355/* 356 * XXX KDM move these into the softc. 357 */ 358static int rcv_sync_msg; 359static int persis_offset; 360static uint8_t ctl_pause_rtr; 361 362SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 363static int worker_threads = -1; 364SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 365 &worker_threads, 1, "Number of worker threads"); 366static int ctl_debug = CTL_DEBUG_NONE; 367SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 368 &ctl_debug, 0, "Enabled debug flags"); 369 370/* 371 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 372 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 373 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 374 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 375 */ 376#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 377 378static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 379 int param); 380static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 381static int ctl_init(void); 382void ctl_shutdown(void); 383static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 384static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 385static void ctl_ioctl_online(void *arg); 386static void ctl_ioctl_offline(void *arg); 387static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 388static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 389static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 390static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 391static int ctl_ioctl_submit_wait(union ctl_io *io); 392static void ctl_ioctl_datamove(union ctl_io *io); 393static void ctl_ioctl_done(union ctl_io *io); 394static void ctl_ioctl_hard_startstop_callback(void *arg, 395 struct cfi_metatask *metatask); 396static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 397static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 398 struct ctl_ooa *ooa_hdr, 399 struct ctl_ooa_entry *kern_entries); 400static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 401 struct thread *td); 402static uint32_t ctl_map_lun(int port_num, uint32_t lun); 403static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 404#ifdef unused 405static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 406 uint32_t targ_target, uint32_t targ_lun, 407 int can_wait); 408static void ctl_kfree_io(union ctl_io *io); 409#endif /* unused */ 410static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 411 struct ctl_be_lun *be_lun, struct ctl_id target_id); 412static int ctl_free_lun(struct ctl_lun *lun); 413static void ctl_create_lun(struct ctl_be_lun *be_lun); 414/** 415static void ctl_failover_change_pages(struct ctl_softc *softc, 416 struct ctl_scsiio *ctsio, int master); 417**/ 418 419static int ctl_do_mode_select(union ctl_io *io); 420static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 421 uint64_t res_key, uint64_t sa_res_key, 422 uint8_t type, uint32_t residx, 423 struct ctl_scsiio *ctsio, 424 struct scsi_per_res_out *cdb, 425 struct scsi_per_res_out_parms* param); 426static void ctl_pro_preempt_other(struct ctl_lun *lun, 427 union ctl_ha_msg *msg); 428static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 429static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 430static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 431static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 432static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 433static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 434static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 435 int alloc_len); 436static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 437 int alloc_len); 438static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 439static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 440static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 441static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 442static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 443static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 444static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 445 union ctl_io *pending_io, union ctl_io *ooa_io); 446static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 447 union ctl_io *starting_io); 448static int ctl_check_blocked(struct ctl_lun *lun); 449static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 450 struct ctl_lun *lun, 451 const struct ctl_cmd_entry *entry, 452 struct ctl_scsiio *ctsio); 453//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 454static void ctl_failover(void); 455static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 456 struct ctl_scsiio *ctsio); 457static int ctl_scsiio(struct ctl_scsiio *ctsio); 458 459static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 460static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 461 ctl_ua_type ua_type); 462static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 463 ctl_ua_type ua_type); 464static int ctl_abort_task(union ctl_io *io); 465static int ctl_abort_task_set(union ctl_io *io); 466static int ctl_i_t_nexus_reset(union ctl_io *io); 467static void ctl_run_task(union ctl_io *io); 468#ifdef CTL_IO_DELAY 469static void ctl_datamove_timer_wakeup(void *arg); 470static void ctl_done_timer_wakeup(void *arg); 471#endif /* CTL_IO_DELAY */ 472 473static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 474static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 475static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 476static void ctl_datamove_remote_write(union ctl_io *io); 477static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 478static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 479static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 480static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 481 ctl_ha_dt_cb callback); 482static void ctl_datamove_remote_read(union ctl_io *io); 483static void ctl_datamove_remote(union ctl_io *io); 484static int ctl_process_done(union ctl_io *io); 485static void ctl_lun_thread(void *arg); 486static void ctl_thresh_thread(void *arg); 487static void ctl_work_thread(void *arg); 488static void ctl_enqueue_incoming(union ctl_io *io); 489static void ctl_enqueue_rtr(union ctl_io *io); 490static void ctl_enqueue_done(union ctl_io *io); 491static void ctl_enqueue_isc(union ctl_io *io); 492static const struct ctl_cmd_entry * 493 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 494static const struct ctl_cmd_entry * 495 ctl_validate_command(struct ctl_scsiio *ctsio); 496static int ctl_cmd_applicable(uint8_t lun_type, 497 const struct ctl_cmd_entry *entry); 498 499/* 500 * Load the serialization table. This isn't very pretty, but is probably 501 * the easiest way to do it. 502 */ 503#include "ctl_ser_table.c" 504 505/* 506 * We only need to define open, close and ioctl routines for this driver. 507 */ 508static struct cdevsw ctl_cdevsw = { 509 .d_version = D_VERSION, 510 .d_flags = 0, 511 .d_open = ctl_open, 512 .d_close = ctl_close, 513 .d_ioctl = ctl_ioctl, 514 .d_name = "ctl", 515}; 516 517 518MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 519MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 520 521static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 522 523static moduledata_t ctl_moduledata = { 524 "ctl", 525 ctl_module_event_handler, 526 NULL 527}; 528 529DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 530MODULE_VERSION(ctl, 1); 531 532static struct ctl_frontend ioctl_frontend = 533{ 534 .name = "ioctl", 535}; 536 537static void 538ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 539 union ctl_ha_msg *msg_info) 540{ 541 struct ctl_scsiio *ctsio; 542 543 if (msg_info->hdr.original_sc == NULL) { 544 printf("%s: original_sc == NULL!\n", __func__); 545 /* XXX KDM now what? */ 546 return; 547 } 548 549 ctsio = &msg_info->hdr.original_sc->scsiio; 550 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 551 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 552 ctsio->io_hdr.status = msg_info->hdr.status; 553 ctsio->scsi_status = msg_info->scsi.scsi_status; 554 ctsio->sense_len = msg_info->scsi.sense_len; 555 ctsio->sense_residual = msg_info->scsi.sense_residual; 556 ctsio->residual = msg_info->scsi.residual; 557 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 558 sizeof(ctsio->sense_data)); 559 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 560 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 561 ctl_enqueue_isc((union ctl_io *)ctsio); 562} 563 564static void 565ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 566 union ctl_ha_msg *msg_info) 567{ 568 struct ctl_scsiio *ctsio; 569 570 if (msg_info->hdr.serializing_sc == NULL) { 571 printf("%s: serializing_sc == NULL!\n", __func__); 572 /* XXX KDM now what? */ 573 return; 574 } 575 576 ctsio = &msg_info->hdr.serializing_sc->scsiio; 577#if 0 578 /* 579 * Attempt to catch the situation where an I/O has 580 * been freed, and we're using it again. 581 */ 582 if (ctsio->io_hdr.io_type == 0xff) { 583 union ctl_io *tmp_io; 584 tmp_io = (union ctl_io *)ctsio; 585 printf("%s: %p use after free!\n", __func__, 586 ctsio); 587 printf("%s: type %d msg %d cdb %x iptl: " 588 "%d:%d:%d:%d tag 0x%04x " 589 "flag %#x status %x\n", 590 __func__, 591 tmp_io->io_hdr.io_type, 592 tmp_io->io_hdr.msg_type, 593 tmp_io->scsiio.cdb[0], 594 tmp_io->io_hdr.nexus.initid.id, 595 tmp_io->io_hdr.nexus.targ_port, 596 tmp_io->io_hdr.nexus.targ_target.id, 597 tmp_io->io_hdr.nexus.targ_lun, 598 (tmp_io->io_hdr.io_type == 599 CTL_IO_TASK) ? 600 tmp_io->taskio.tag_num : 601 tmp_io->scsiio.tag_num, 602 tmp_io->io_hdr.flags, 603 tmp_io->io_hdr.status); 604 } 605#endif 606 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 607 ctl_enqueue_isc((union ctl_io *)ctsio); 608} 609 610/* 611 * ISC (Inter Shelf Communication) event handler. Events from the HA 612 * subsystem come in here. 613 */ 614static void 615ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 616{ 617 struct ctl_softc *ctl_softc; 618 union ctl_io *io; 619 struct ctl_prio *presio; 620 ctl_ha_status isc_status; 621 622 ctl_softc = control_softc; 623 io = NULL; 624 625 626#if 0 627 printf("CTL: Isc Msg event %d\n", event); 628#endif 629 if (event == CTL_HA_EVT_MSG_RECV) { 630 union ctl_ha_msg msg_info; 631 632 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 633 sizeof(msg_info), /*wait*/ 0); 634#if 0 635 printf("CTL: msg_type %d\n", msg_info.msg_type); 636#endif 637 if (isc_status != 0) { 638 printf("Error receiving message, status = %d\n", 639 isc_status); 640 return; 641 } 642 643 switch (msg_info.hdr.msg_type) { 644 case CTL_MSG_SERIALIZE: 645#if 0 646 printf("Serialize\n"); 647#endif 648 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool); 649 if (io == NULL) { 650 printf("ctl_isc_event_handler: can't allocate " 651 "ctl_io!\n"); 652 /* Bad Juju */ 653 /* Need to set busy and send msg back */ 654 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 655 msg_info.hdr.status = CTL_SCSI_ERROR; 656 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 657 msg_info.scsi.sense_len = 0; 658 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 659 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 660 } 661 goto bailout; 662 } 663 ctl_zero_io(io); 664 // populate ctsio from msg_info 665 io->io_hdr.io_type = CTL_IO_SCSI; 666 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 667 io->io_hdr.original_sc = msg_info.hdr.original_sc; 668#if 0 669 printf("pOrig %x\n", (int)msg_info.original_sc); 670#endif 671 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 672 CTL_FLAG_IO_ACTIVE; 673 /* 674 * If we're in serialization-only mode, we don't 675 * want to go through full done processing. Thus 676 * the COPY flag. 677 * 678 * XXX KDM add another flag that is more specific. 679 */ 680 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 681 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 682 io->io_hdr.nexus = msg_info.hdr.nexus; 683#if 0 684 printf("targ %d, port %d, iid %d, lun %d\n", 685 io->io_hdr.nexus.targ_target.id, 686 io->io_hdr.nexus.targ_port, 687 io->io_hdr.nexus.initid.id, 688 io->io_hdr.nexus.targ_lun); 689#endif 690 io->scsiio.tag_num = msg_info.scsi.tag_num; 691 io->scsiio.tag_type = msg_info.scsi.tag_type; 692 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 693 CTL_MAX_CDBLEN); 694 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 695 const struct ctl_cmd_entry *entry; 696 697 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 698 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 699 io->io_hdr.flags |= 700 entry->flags & CTL_FLAG_DATA_MASK; 701 } 702 ctl_enqueue_isc(io); 703 break; 704 705 /* Performed on the Originating SC, XFER mode only */ 706 case CTL_MSG_DATAMOVE: { 707 struct ctl_sg_entry *sgl; 708 int i, j; 709 710 io = msg_info.hdr.original_sc; 711 if (io == NULL) { 712 printf("%s: original_sc == NULL!\n", __func__); 713 /* XXX KDM do something here */ 714 break; 715 } 716 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 717 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 718 /* 719 * Keep track of this, we need to send it back over 720 * when the datamove is complete. 721 */ 722 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 723 724 if (msg_info.dt.sg_sequence == 0) { 725 /* 726 * XXX KDM we use the preallocated S/G list 727 * here, but we'll need to change this to 728 * dynamic allocation if we need larger S/G 729 * lists. 730 */ 731 if (msg_info.dt.kern_sg_entries > 732 sizeof(io->io_hdr.remote_sglist) / 733 sizeof(io->io_hdr.remote_sglist[0])) { 734 printf("%s: number of S/G entries " 735 "needed %u > allocated num %zd\n", 736 __func__, 737 msg_info.dt.kern_sg_entries, 738 sizeof(io->io_hdr.remote_sglist)/ 739 sizeof(io->io_hdr.remote_sglist[0])); 740 741 /* 742 * XXX KDM send a message back to 743 * the other side to shut down the 744 * DMA. The error will come back 745 * through via the normal channel. 746 */ 747 break; 748 } 749 sgl = io->io_hdr.remote_sglist; 750 memset(sgl, 0, 751 sizeof(io->io_hdr.remote_sglist)); 752 753 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 754 755 io->scsiio.kern_sg_entries = 756 msg_info.dt.kern_sg_entries; 757 io->scsiio.rem_sg_entries = 758 msg_info.dt.kern_sg_entries; 759 io->scsiio.kern_data_len = 760 msg_info.dt.kern_data_len; 761 io->scsiio.kern_total_len = 762 msg_info.dt.kern_total_len; 763 io->scsiio.kern_data_resid = 764 msg_info.dt.kern_data_resid; 765 io->scsiio.kern_rel_offset = 766 msg_info.dt.kern_rel_offset; 767 /* 768 * Clear out per-DMA flags. 769 */ 770 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 771 /* 772 * Add per-DMA flags that are set for this 773 * particular DMA request. 774 */ 775 io->io_hdr.flags |= msg_info.dt.flags & 776 CTL_FLAG_RDMA_MASK; 777 } else 778 sgl = (struct ctl_sg_entry *) 779 io->scsiio.kern_data_ptr; 780 781 for (i = msg_info.dt.sent_sg_entries, j = 0; 782 i < (msg_info.dt.sent_sg_entries + 783 msg_info.dt.cur_sg_entries); i++, j++) { 784 sgl[i].addr = msg_info.dt.sg_list[j].addr; 785 sgl[i].len = msg_info.dt.sg_list[j].len; 786 787#if 0 788 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 789 __func__, 790 msg_info.dt.sg_list[j].addr, 791 msg_info.dt.sg_list[j].len, 792 sgl[i].addr, sgl[i].len, j, i); 793#endif 794 } 795#if 0 796 memcpy(&sgl[msg_info.dt.sent_sg_entries], 797 msg_info.dt.sg_list, 798 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 799#endif 800 801 /* 802 * If this is the last piece of the I/O, we've got 803 * the full S/G list. Queue processing in the thread. 804 * Otherwise wait for the next piece. 805 */ 806 if (msg_info.dt.sg_last != 0) 807 ctl_enqueue_isc(io); 808 break; 809 } 810 /* Performed on the Serializing (primary) SC, XFER mode only */ 811 case CTL_MSG_DATAMOVE_DONE: { 812 if (msg_info.hdr.serializing_sc == NULL) { 813 printf("%s: serializing_sc == NULL!\n", 814 __func__); 815 /* XXX KDM now what? */ 816 break; 817 } 818 /* 819 * We grab the sense information here in case 820 * there was a failure, so we can return status 821 * back to the initiator. 822 */ 823 io = msg_info.hdr.serializing_sc; 824 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 825 io->io_hdr.status = msg_info.hdr.status; 826 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 827 io->scsiio.sense_len = msg_info.scsi.sense_len; 828 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 829 io->io_hdr.port_status = msg_info.scsi.fetd_status; 830 io->scsiio.residual = msg_info.scsi.residual; 831 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 832 sizeof(io->scsiio.sense_data)); 833 ctl_enqueue_isc(io); 834 break; 835 } 836 837 /* Preformed on Originating SC, SER_ONLY mode */ 838 case CTL_MSG_R2R: 839 io = msg_info.hdr.original_sc; 840 if (io == NULL) { 841 printf("%s: Major Bummer\n", __func__); 842 return; 843 } else { 844#if 0 845 printf("pOrig %x\n",(int) ctsio); 846#endif 847 } 848 io->io_hdr.msg_type = CTL_MSG_R2R; 849 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 850 ctl_enqueue_isc(io); 851 break; 852 853 /* 854 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 855 * mode. 856 * Performed on the Originating (i.e. secondary) SC in XFER 857 * mode 858 */ 859 case CTL_MSG_FINISH_IO: 860 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 861 ctl_isc_handler_finish_xfer(ctl_softc, 862 &msg_info); 863 else 864 ctl_isc_handler_finish_ser_only(ctl_softc, 865 &msg_info); 866 break; 867 868 /* Preformed on Originating SC */ 869 case CTL_MSG_BAD_JUJU: 870 io = msg_info.hdr.original_sc; 871 if (io == NULL) { 872 printf("%s: Bad JUJU!, original_sc is NULL!\n", 873 __func__); 874 break; 875 } 876 ctl_copy_sense_data(&msg_info, io); 877 /* 878 * IO should have already been cleaned up on other 879 * SC so clear this flag so we won't send a message 880 * back to finish the IO there. 881 */ 882 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 883 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 884 885 /* io = msg_info.hdr.serializing_sc; */ 886 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 887 ctl_enqueue_isc(io); 888 break; 889 890 /* Handle resets sent from the other side */ 891 case CTL_MSG_MANAGE_TASKS: { 892 struct ctl_taskio *taskio; 893 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 894 ctl_softc->othersc_pool); 895 if (taskio == NULL) { 896 printf("ctl_isc_event_handler: can't allocate " 897 "ctl_io!\n"); 898 /* Bad Juju */ 899 /* should I just call the proper reset func 900 here??? */ 901 goto bailout; 902 } 903 ctl_zero_io((union ctl_io *)taskio); 904 taskio->io_hdr.io_type = CTL_IO_TASK; 905 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 906 taskio->io_hdr.nexus = msg_info.hdr.nexus; 907 taskio->task_action = msg_info.task.task_action; 908 taskio->tag_num = msg_info.task.tag_num; 909 taskio->tag_type = msg_info.task.tag_type; 910#ifdef CTL_TIME_IO 911 taskio->io_hdr.start_time = time_uptime; 912 getbintime(&taskio->io_hdr.start_bt); 913#if 0 914 cs_prof_gettime(&taskio->io_hdr.start_ticks); 915#endif 916#endif /* CTL_TIME_IO */ 917 ctl_run_task((union ctl_io *)taskio); 918 break; 919 } 920 /* Persistent Reserve action which needs attention */ 921 case CTL_MSG_PERS_ACTION: 922 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 923 ctl_softc->othersc_pool); 924 if (presio == NULL) { 925 printf("ctl_isc_event_handler: can't allocate " 926 "ctl_io!\n"); 927 /* Bad Juju */ 928 /* Need to set busy and send msg back */ 929 goto bailout; 930 } 931 ctl_zero_io((union ctl_io *)presio); 932 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 933 presio->pr_msg = msg_info.pr; 934 ctl_enqueue_isc((union ctl_io *)presio); 935 break; 936 case CTL_MSG_SYNC_FE: 937 rcv_sync_msg = 1; 938 break; 939 default: 940 printf("How did I get here?\n"); 941 } 942 } else if (event == CTL_HA_EVT_MSG_SENT) { 943 if (param != CTL_HA_STATUS_SUCCESS) { 944 printf("Bad status from ctl_ha_msg_send status %d\n", 945 param); 946 } 947 return; 948 } else if (event == CTL_HA_EVT_DISCONNECT) { 949 printf("CTL: Got a disconnect from Isc\n"); 950 return; 951 } else { 952 printf("ctl_isc_event_handler: Unknown event %d\n", event); 953 return; 954 } 955 956bailout: 957 return; 958} 959 960static void 961ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 962{ 963 struct scsi_sense_data *sense; 964 965 sense = &dest->scsiio.sense_data; 966 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 967 dest->scsiio.scsi_status = src->scsi.scsi_status; 968 dest->scsiio.sense_len = src->scsi.sense_len; 969 dest->io_hdr.status = src->hdr.status; 970} 971 972static int 973ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 974{ 975 struct ctl_softc *softc = (struct ctl_softc *)arg1; 976 struct ctl_lun *lun; 977 int error, value, i; 978 979 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 980 value = 0; 981 else 982 value = 1; 983 984 error = sysctl_handle_int(oidp, &value, 0, req); 985 if ((error != 0) || (req->newptr == NULL)) 986 return (error); 987 988 mtx_lock(&softc->ctl_lock); 989 if (value == 0) 990 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 991 else 992 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 993 STAILQ_FOREACH(lun, &softc->lun_list, links) { 994 mtx_lock(&lun->lun_lock); 995 for (i = 0; i < CTL_MAX_INITIATORS; i++) 996 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE; 997 mtx_unlock(&lun->lun_lock); 998 } 999 mtx_unlock(&softc->ctl_lock); 1000 return (0); 1001} 1002 1003static int 1004ctl_init(void) 1005{ 1006 struct ctl_softc *softc; 1007 void *other_pool; 1008 struct ctl_port *port; 1009 int i, error, retval; 1010 //int isc_retval; 1011 1012 retval = 0; 1013 ctl_pause_rtr = 0; 1014 rcv_sync_msg = 0; 1015 1016 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1017 M_WAITOK | M_ZERO); 1018 softc = control_softc; 1019 1020 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1021 "cam/ctl"); 1022 1023 softc->dev->si_drv1 = softc; 1024 1025 /* 1026 * By default, return a "bad LUN" peripheral qualifier for unknown 1027 * LUNs. The user can override this default using the tunable or 1028 * sysctl. See the comment in ctl_inquiry_std() for more details. 1029 */ 1030 softc->inquiry_pq_no_lun = 1; 1031 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1032 &softc->inquiry_pq_no_lun); 1033 sysctl_ctx_init(&softc->sysctl_ctx); 1034 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1035 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1036 CTLFLAG_RD, 0, "CAM Target Layer"); 1037 1038 if (softc->sysctl_tree == NULL) { 1039 printf("%s: unable to allocate sysctl tree\n", __func__); 1040 destroy_dev(softc->dev); 1041 free(control_softc, M_DEVBUF); 1042 control_softc = NULL; 1043 return (ENOMEM); 1044 } 1045 1046 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1047 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1048 "inquiry_pq_no_lun", CTLFLAG_RW, 1049 &softc->inquiry_pq_no_lun, 0, 1050 "Report no lun possible for invalid LUNs"); 1051 1052 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1053 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1054 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1055 softc->open_count = 0; 1056 1057 /* 1058 * Default to actually sending a SYNCHRONIZE CACHE command down to 1059 * the drive. 1060 */ 1061 softc->flags = CTL_FLAG_REAL_SYNC; 1062 1063 /* 1064 * In Copan's HA scheme, the "master" and "slave" roles are 1065 * figured out through the slot the controller is in. Although it 1066 * is an active/active system, someone has to be in charge. 1067 */ 1068 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1069 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1070 "HA head ID (0 - no HA)"); 1071 if (softc->ha_id == 0) { 1072 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1073 softc->is_single = 1; 1074 softc->port_offset = 0; 1075 } else 1076 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1077 persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1078 1079 /* 1080 * XXX KDM need to figure out where we want to get our target ID 1081 * and WWID. Is it different on each port? 1082 */ 1083 softc->target.id = 0; 1084 softc->target.wwid[0] = 0x12345678; 1085 softc->target.wwid[1] = 0x87654321; 1086 STAILQ_INIT(&softc->lun_list); 1087 STAILQ_INIT(&softc->pending_lun_queue); 1088 STAILQ_INIT(&softc->fe_list); 1089 STAILQ_INIT(&softc->port_list); 1090 STAILQ_INIT(&softc->be_list); 1091 ctl_tpc_init(softc); 1092 1093 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1094 &other_pool) != 0) 1095 { 1096 printf("ctl: can't allocate %d entry other SC pool, " 1097 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1098 return (ENOMEM); 1099 } 1100 softc->othersc_pool = other_pool; 1101 1102 if (worker_threads <= 0) 1103 worker_threads = max(1, mp_ncpus / 4); 1104 if (worker_threads > CTL_MAX_THREADS) 1105 worker_threads = CTL_MAX_THREADS; 1106 1107 for (i = 0; i < worker_threads; i++) { 1108 struct ctl_thread *thr = &softc->threads[i]; 1109 1110 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1111 thr->ctl_softc = softc; 1112 STAILQ_INIT(&thr->incoming_queue); 1113 STAILQ_INIT(&thr->rtr_queue); 1114 STAILQ_INIT(&thr->done_queue); 1115 STAILQ_INIT(&thr->isc_queue); 1116 1117 error = kproc_kthread_add(ctl_work_thread, thr, 1118 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1119 if (error != 0) { 1120 printf("error creating CTL work thread!\n"); 1121 ctl_pool_free(other_pool); 1122 return (error); 1123 } 1124 } 1125 error = kproc_kthread_add(ctl_lun_thread, softc, 1126 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1127 if (error != 0) { 1128 printf("error creating CTL lun thread!\n"); 1129 ctl_pool_free(other_pool); 1130 return (error); 1131 } 1132 error = kproc_kthread_add(ctl_thresh_thread, softc, 1133 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1134 if (error != 0) { 1135 printf("error creating CTL threshold thread!\n"); 1136 ctl_pool_free(other_pool); 1137 return (error); 1138 } 1139 if (bootverbose) 1140 printf("ctl: CAM Target Layer loaded\n"); 1141 1142 /* 1143 * Initialize the ioctl front end. 1144 */ 1145 ctl_frontend_register(&ioctl_frontend); 1146 port = &softc->ioctl_info.port; 1147 port->frontend = &ioctl_frontend; 1148 sprintf(softc->ioctl_info.port_name, "ioctl"); 1149 port->port_type = CTL_PORT_IOCTL; 1150 port->num_requested_ctl_io = 100; 1151 port->port_name = softc->ioctl_info.port_name; 1152 port->port_online = ctl_ioctl_online; 1153 port->port_offline = ctl_ioctl_offline; 1154 port->onoff_arg = &softc->ioctl_info; 1155 port->lun_enable = ctl_ioctl_lun_enable; 1156 port->lun_disable = ctl_ioctl_lun_disable; 1157 port->targ_lun_arg = &softc->ioctl_info; 1158 port->fe_datamove = ctl_ioctl_datamove; 1159 port->fe_done = ctl_ioctl_done; 1160 port->max_targets = 15; 1161 port->max_target_id = 15; 1162 1163 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1164 printf("ctl: ioctl front end registration failed, will " 1165 "continue anyway\n"); 1166 } 1167 1168 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1169 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1170 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1171 1172#ifdef CTL_IO_DELAY 1173 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1174 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1175 sizeof(struct callout), CTL_TIMER_BYTES); 1176 return (EINVAL); 1177 } 1178#endif /* CTL_IO_DELAY */ 1179 1180 return (0); 1181} 1182 1183void 1184ctl_shutdown(void) 1185{ 1186 struct ctl_softc *softc; 1187 struct ctl_lun *lun, *next_lun; 1188 1189 softc = (struct ctl_softc *)control_softc; 1190 1191 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1192 printf("ctl: ioctl front end deregistration failed\n"); 1193 1194 mtx_lock(&softc->ctl_lock); 1195 1196 /* 1197 * Free up each LUN. 1198 */ 1199 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1200 next_lun = STAILQ_NEXT(lun, links); 1201 ctl_free_lun(lun); 1202 } 1203 1204 mtx_unlock(&softc->ctl_lock); 1205 1206 ctl_frontend_deregister(&ioctl_frontend); 1207 1208#if 0 1209 ctl_shutdown_thread(softc->work_thread); 1210 mtx_destroy(&softc->queue_lock); 1211#endif 1212 1213 ctl_tpc_shutdown(softc); 1214 uma_zdestroy(softc->io_zone); 1215 mtx_destroy(&softc->ctl_lock); 1216 1217 destroy_dev(softc->dev); 1218 1219 sysctl_ctx_free(&softc->sysctl_ctx); 1220 1221 free(control_softc, M_DEVBUF); 1222 control_softc = NULL; 1223 1224 if (bootverbose) 1225 printf("ctl: CAM Target Layer unloaded\n"); 1226} 1227 1228static int 1229ctl_module_event_handler(module_t mod, int what, void *arg) 1230{ 1231 1232 switch (what) { 1233 case MOD_LOAD: 1234 return (ctl_init()); 1235 case MOD_UNLOAD: 1236 return (EBUSY); 1237 default: 1238 return (EOPNOTSUPP); 1239 } 1240} 1241 1242/* 1243 * XXX KDM should we do some access checks here? Bump a reference count to 1244 * prevent a CTL module from being unloaded while someone has it open? 1245 */ 1246static int 1247ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1248{ 1249 return (0); 1250} 1251 1252static int 1253ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1254{ 1255 return (0); 1256} 1257 1258int 1259ctl_port_enable(ctl_port_type port_type) 1260{ 1261 struct ctl_softc *softc = control_softc; 1262 struct ctl_port *port; 1263 1264 if (softc->is_single == 0) { 1265 union ctl_ha_msg msg_info; 1266 int isc_retval; 1267 1268#if 0 1269 printf("%s: HA mode, synchronizing frontend enable\n", 1270 __func__); 1271#endif 1272 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1273 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1274 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1275 printf("Sync msg send error retval %d\n", isc_retval); 1276 } 1277 if (!rcv_sync_msg) { 1278 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1279 sizeof(msg_info), 1); 1280 } 1281#if 0 1282 printf("CTL:Frontend Enable\n"); 1283 } else { 1284 printf("%s: single mode, skipping frontend synchronization\n", 1285 __func__); 1286#endif 1287 } 1288 1289 STAILQ_FOREACH(port, &softc->port_list, links) { 1290 if (port_type & port->port_type) 1291 { 1292#if 0 1293 printf("port %d\n", port->targ_port); 1294#endif 1295 ctl_port_online(port); 1296 } 1297 } 1298 1299 return (0); 1300} 1301 1302int 1303ctl_port_disable(ctl_port_type port_type) 1304{ 1305 struct ctl_softc *softc; 1306 struct ctl_port *port; 1307 1308 softc = control_softc; 1309 1310 STAILQ_FOREACH(port, &softc->port_list, links) { 1311 if (port_type & port->port_type) 1312 ctl_port_offline(port); 1313 } 1314 1315 return (0); 1316} 1317 1318/* 1319 * Returns 0 for success, 1 for failure. 1320 * Currently the only failure mode is if there aren't enough entries 1321 * allocated. So, in case of a failure, look at num_entries_dropped, 1322 * reallocate and try again. 1323 */ 1324int 1325ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1326 int *num_entries_filled, int *num_entries_dropped, 1327 ctl_port_type port_type, int no_virtual) 1328{ 1329 struct ctl_softc *softc; 1330 struct ctl_port *port; 1331 int entries_dropped, entries_filled; 1332 int retval; 1333 int i; 1334 1335 softc = control_softc; 1336 1337 retval = 0; 1338 entries_filled = 0; 1339 entries_dropped = 0; 1340 1341 i = 0; 1342 mtx_lock(&softc->ctl_lock); 1343 STAILQ_FOREACH(port, &softc->port_list, links) { 1344 struct ctl_port_entry *entry; 1345 1346 if ((port->port_type & port_type) == 0) 1347 continue; 1348 1349 if ((no_virtual != 0) 1350 && (port->virtual_port != 0)) 1351 continue; 1352 1353 if (entries_filled >= num_entries_alloced) { 1354 entries_dropped++; 1355 continue; 1356 } 1357 entry = &entries[i]; 1358 1359 entry->port_type = port->port_type; 1360 strlcpy(entry->port_name, port->port_name, 1361 sizeof(entry->port_name)); 1362 entry->physical_port = port->physical_port; 1363 entry->virtual_port = port->virtual_port; 1364 entry->wwnn = port->wwnn; 1365 entry->wwpn = port->wwpn; 1366 1367 i++; 1368 entries_filled++; 1369 } 1370 1371 mtx_unlock(&softc->ctl_lock); 1372 1373 if (entries_dropped > 0) 1374 retval = 1; 1375 1376 *num_entries_dropped = entries_dropped; 1377 *num_entries_filled = entries_filled; 1378 1379 return (retval); 1380} 1381 1382static void 1383ctl_ioctl_online(void *arg) 1384{ 1385 struct ctl_ioctl_info *ioctl_info; 1386 1387 ioctl_info = (struct ctl_ioctl_info *)arg; 1388 1389 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1390} 1391 1392static void 1393ctl_ioctl_offline(void *arg) 1394{ 1395 struct ctl_ioctl_info *ioctl_info; 1396 1397 ioctl_info = (struct ctl_ioctl_info *)arg; 1398 1399 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1400} 1401 1402/* 1403 * Remove an initiator by port number and initiator ID. 1404 * Returns 0 for success, -1 for failure. 1405 */ 1406int 1407ctl_remove_initiator(struct ctl_port *port, int iid) 1408{ 1409 struct ctl_softc *softc = control_softc; 1410 1411 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1412 1413 if (iid > CTL_MAX_INIT_PER_PORT) { 1414 printf("%s: initiator ID %u > maximun %u!\n", 1415 __func__, iid, CTL_MAX_INIT_PER_PORT); 1416 return (-1); 1417 } 1418 1419 mtx_lock(&softc->ctl_lock); 1420 port->wwpn_iid[iid].in_use--; 1421 port->wwpn_iid[iid].last_use = time_uptime; 1422 mtx_unlock(&softc->ctl_lock); 1423 1424 return (0); 1425} 1426 1427/* 1428 * Add an initiator to the initiator map. 1429 * Returns iid for success, < 0 for failure. 1430 */ 1431int 1432ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1433{ 1434 struct ctl_softc *softc = control_softc; 1435 time_t best_time; 1436 int i, best; 1437 1438 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1439 1440 if (iid >= CTL_MAX_INIT_PER_PORT) { 1441 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1442 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1443 free(name, M_CTL); 1444 return (-1); 1445 } 1446 1447 mtx_lock(&softc->ctl_lock); 1448 1449 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1450 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1451 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1452 iid = i; 1453 break; 1454 } 1455 if (name != NULL && port->wwpn_iid[i].name != NULL && 1456 strcmp(name, port->wwpn_iid[i].name) == 0) { 1457 iid = i; 1458 break; 1459 } 1460 } 1461 } 1462 1463 if (iid < 0) { 1464 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1465 if (port->wwpn_iid[i].in_use == 0 && 1466 port->wwpn_iid[i].wwpn == 0 && 1467 port->wwpn_iid[i].name == NULL) { 1468 iid = i; 1469 break; 1470 } 1471 } 1472 } 1473 1474 if (iid < 0) { 1475 best = -1; 1476 best_time = INT32_MAX; 1477 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1478 if (port->wwpn_iid[i].in_use == 0) { 1479 if (port->wwpn_iid[i].last_use < best_time) { 1480 best = i; 1481 best_time = port->wwpn_iid[i].last_use; 1482 } 1483 } 1484 } 1485 iid = best; 1486 } 1487 1488 if (iid < 0) { 1489 mtx_unlock(&softc->ctl_lock); 1490 free(name, M_CTL); 1491 return (-2); 1492 } 1493 1494 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1495 /* 1496 * This is not an error yet. 1497 */ 1498 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1499#if 0 1500 printf("%s: port %d iid %u WWPN %#jx arrived" 1501 " again\n", __func__, port->targ_port, 1502 iid, (uintmax_t)wwpn); 1503#endif 1504 goto take; 1505 } 1506 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1507 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1508#if 0 1509 printf("%s: port %d iid %u name '%s' arrived" 1510 " again\n", __func__, port->targ_port, 1511 iid, name); 1512#endif 1513 goto take; 1514 } 1515 1516 /* 1517 * This is an error, but what do we do about it? The 1518 * driver is telling us we have a new WWPN for this 1519 * initiator ID, so we pretty much need to use it. 1520 */ 1521 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1522 " but WWPN %#jx '%s' is still at that address\n", 1523 __func__, port->targ_port, iid, wwpn, name, 1524 (uintmax_t)port->wwpn_iid[iid].wwpn, 1525 port->wwpn_iid[iid].name); 1526 1527 /* 1528 * XXX KDM clear have_ca and ua_pending on each LUN for 1529 * this initiator. 1530 */ 1531 } 1532take: 1533 free(port->wwpn_iid[iid].name, M_CTL); 1534 port->wwpn_iid[iid].name = name; 1535 port->wwpn_iid[iid].wwpn = wwpn; 1536 port->wwpn_iid[iid].in_use++; 1537 mtx_unlock(&softc->ctl_lock); 1538 1539 return (iid); 1540} 1541 1542static int 1543ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1544{ 1545 int len; 1546 1547 switch (port->port_type) { 1548 case CTL_PORT_FC: 1549 { 1550 struct scsi_transportid_fcp *id = 1551 (struct scsi_transportid_fcp *)buf; 1552 if (port->wwpn_iid[iid].wwpn == 0) 1553 return (0); 1554 memset(id, 0, sizeof(*id)); 1555 id->format_protocol = SCSI_PROTO_FC; 1556 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1557 return (sizeof(*id)); 1558 } 1559 case CTL_PORT_ISCSI: 1560 { 1561 struct scsi_transportid_iscsi_port *id = 1562 (struct scsi_transportid_iscsi_port *)buf; 1563 if (port->wwpn_iid[iid].name == NULL) 1564 return (0); 1565 memset(id, 0, 256); 1566 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1567 SCSI_PROTO_ISCSI; 1568 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1569 len = roundup2(min(len, 252), 4); 1570 scsi_ulto2b(len, id->additional_length); 1571 return (sizeof(*id) + len); 1572 } 1573 case CTL_PORT_SAS: 1574 { 1575 struct scsi_transportid_sas *id = 1576 (struct scsi_transportid_sas *)buf; 1577 if (port->wwpn_iid[iid].wwpn == 0) 1578 return (0); 1579 memset(id, 0, sizeof(*id)); 1580 id->format_protocol = SCSI_PROTO_SAS; 1581 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1582 return (sizeof(*id)); 1583 } 1584 default: 1585 { 1586 struct scsi_transportid_spi *id = 1587 (struct scsi_transportid_spi *)buf; 1588 memset(id, 0, sizeof(*id)); 1589 id->format_protocol = SCSI_PROTO_SPI; 1590 scsi_ulto2b(iid, id->scsi_addr); 1591 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1592 return (sizeof(*id)); 1593 } 1594 } 1595} 1596 1597static int 1598ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1599{ 1600 return (0); 1601} 1602 1603static int 1604ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1605{ 1606 return (0); 1607} 1608 1609/* 1610 * Data movement routine for the CTL ioctl frontend port. 1611 */ 1612static int 1613ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1614{ 1615 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1616 struct ctl_sg_entry ext_entry, kern_entry; 1617 int ext_sglen, ext_sg_entries, kern_sg_entries; 1618 int ext_sg_start, ext_offset; 1619 int len_to_copy, len_copied; 1620 int kern_watermark, ext_watermark; 1621 int ext_sglist_malloced; 1622 int i, j; 1623 1624 ext_sglist_malloced = 0; 1625 ext_sg_start = 0; 1626 ext_offset = 0; 1627 1628 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1629 1630 /* 1631 * If this flag is set, fake the data transfer. 1632 */ 1633 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1634 ctsio->ext_data_filled = ctsio->ext_data_len; 1635 goto bailout; 1636 } 1637 1638 /* 1639 * To simplify things here, if we have a single buffer, stick it in 1640 * a S/G entry and just make it a single entry S/G list. 1641 */ 1642 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1643 int len_seen; 1644 1645 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1646 1647 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1648 M_WAITOK); 1649 ext_sglist_malloced = 1; 1650 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1651 ext_sglen) != 0) { 1652 ctl_set_internal_failure(ctsio, 1653 /*sks_valid*/ 0, 1654 /*retry_count*/ 0); 1655 goto bailout; 1656 } 1657 ext_sg_entries = ctsio->ext_sg_entries; 1658 len_seen = 0; 1659 for (i = 0; i < ext_sg_entries; i++) { 1660 if ((len_seen + ext_sglist[i].len) >= 1661 ctsio->ext_data_filled) { 1662 ext_sg_start = i; 1663 ext_offset = ctsio->ext_data_filled - len_seen; 1664 break; 1665 } 1666 len_seen += ext_sglist[i].len; 1667 } 1668 } else { 1669 ext_sglist = &ext_entry; 1670 ext_sglist->addr = ctsio->ext_data_ptr; 1671 ext_sglist->len = ctsio->ext_data_len; 1672 ext_sg_entries = 1; 1673 ext_sg_start = 0; 1674 ext_offset = ctsio->ext_data_filled; 1675 } 1676 1677 if (ctsio->kern_sg_entries > 0) { 1678 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1679 kern_sg_entries = ctsio->kern_sg_entries; 1680 } else { 1681 kern_sglist = &kern_entry; 1682 kern_sglist->addr = ctsio->kern_data_ptr; 1683 kern_sglist->len = ctsio->kern_data_len; 1684 kern_sg_entries = 1; 1685 } 1686 1687 1688 kern_watermark = 0; 1689 ext_watermark = ext_offset; 1690 len_copied = 0; 1691 for (i = ext_sg_start, j = 0; 1692 i < ext_sg_entries && j < kern_sg_entries;) { 1693 uint8_t *ext_ptr, *kern_ptr; 1694 1695 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1696 kern_sglist[j].len - kern_watermark); 1697 1698 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1699 ext_ptr = ext_ptr + ext_watermark; 1700 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1701 /* 1702 * XXX KDM fix this! 1703 */ 1704 panic("need to implement bus address support"); 1705#if 0 1706 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1707#endif 1708 } else 1709 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1710 kern_ptr = kern_ptr + kern_watermark; 1711 1712 kern_watermark += len_to_copy; 1713 ext_watermark += len_to_copy; 1714 1715 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1716 CTL_FLAG_DATA_IN) { 1717 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1718 "bytes to user\n", len_to_copy)); 1719 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1720 "to %p\n", kern_ptr, ext_ptr)); 1721 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1722 ctl_set_internal_failure(ctsio, 1723 /*sks_valid*/ 0, 1724 /*retry_count*/ 0); 1725 goto bailout; 1726 } 1727 } else { 1728 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1729 "bytes from user\n", len_to_copy)); 1730 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1731 "to %p\n", ext_ptr, kern_ptr)); 1732 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1733 ctl_set_internal_failure(ctsio, 1734 /*sks_valid*/ 0, 1735 /*retry_count*/0); 1736 goto bailout; 1737 } 1738 } 1739 1740 len_copied += len_to_copy; 1741 1742 if (ext_sglist[i].len == ext_watermark) { 1743 i++; 1744 ext_watermark = 0; 1745 } 1746 1747 if (kern_sglist[j].len == kern_watermark) { 1748 j++; 1749 kern_watermark = 0; 1750 } 1751 } 1752 1753 ctsio->ext_data_filled += len_copied; 1754 1755 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1756 "kern_sg_entries: %d\n", ext_sg_entries, 1757 kern_sg_entries)); 1758 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1759 "kern_data_len = %d\n", ctsio->ext_data_len, 1760 ctsio->kern_data_len)); 1761 1762 1763 /* XXX KDM set residual?? */ 1764bailout: 1765 1766 if (ext_sglist_malloced != 0) 1767 free(ext_sglist, M_CTL); 1768 1769 return (CTL_RETVAL_COMPLETE); 1770} 1771 1772/* 1773 * Serialize a command that went down the "wrong" side, and so was sent to 1774 * this controller for execution. The logic is a little different than the 1775 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1776 * sent back to the other side, but in the success case, we execute the 1777 * command on this side (XFER mode) or tell the other side to execute it 1778 * (SER_ONLY mode). 1779 */ 1780static int 1781ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1782{ 1783 struct ctl_softc *ctl_softc; 1784 union ctl_ha_msg msg_info; 1785 struct ctl_lun *lun; 1786 int retval = 0; 1787 uint32_t targ_lun; 1788 1789 ctl_softc = control_softc; 1790 1791 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1792 lun = ctl_softc->ctl_luns[targ_lun]; 1793 if (lun==NULL) 1794 { 1795 /* 1796 * Why isn't LUN defined? The other side wouldn't 1797 * send a cmd if the LUN is undefined. 1798 */ 1799 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1800 1801 /* "Logical unit not supported" */ 1802 ctl_set_sense_data(&msg_info.scsi.sense_data, 1803 lun, 1804 /*sense_format*/SSD_TYPE_NONE, 1805 /*current_error*/ 1, 1806 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1807 /*asc*/ 0x25, 1808 /*ascq*/ 0x00, 1809 SSD_ELEM_NONE); 1810 1811 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1812 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1813 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1814 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1815 msg_info.hdr.serializing_sc = NULL; 1816 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1817 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1818 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1819 } 1820 return(1); 1821 1822 } 1823 1824 mtx_lock(&lun->lun_lock); 1825 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1826 1827 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1828 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1829 ooa_links))) { 1830 case CTL_ACTION_BLOCK: 1831 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1832 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1833 blocked_links); 1834 break; 1835 case CTL_ACTION_PASS: 1836 case CTL_ACTION_SKIP: 1837 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1838 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1839 ctl_enqueue_rtr((union ctl_io *)ctsio); 1840 } else { 1841 1842 /* send msg back to other side */ 1843 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1844 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1845 msg_info.hdr.msg_type = CTL_MSG_R2R; 1846#if 0 1847 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1848#endif 1849 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1850 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1851 } 1852 } 1853 break; 1854 case CTL_ACTION_OVERLAP: 1855 /* OVERLAPPED COMMANDS ATTEMPTED */ 1856 ctl_set_sense_data(&msg_info.scsi.sense_data, 1857 lun, 1858 /*sense_format*/SSD_TYPE_NONE, 1859 /*current_error*/ 1, 1860 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1861 /*asc*/ 0x4E, 1862 /*ascq*/ 0x00, 1863 SSD_ELEM_NONE); 1864 1865 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1866 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1867 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1868 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1869 msg_info.hdr.serializing_sc = NULL; 1870 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1871#if 0 1872 printf("BAD JUJU:Major Bummer Overlap\n"); 1873#endif 1874 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1875 retval = 1; 1876 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1877 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1878 } 1879 break; 1880 case CTL_ACTION_OVERLAP_TAG: 1881 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1882 ctl_set_sense_data(&msg_info.scsi.sense_data, 1883 lun, 1884 /*sense_format*/SSD_TYPE_NONE, 1885 /*current_error*/ 1, 1886 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1887 /*asc*/ 0x4D, 1888 /*ascq*/ ctsio->tag_num & 0xff, 1889 SSD_ELEM_NONE); 1890 1891 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1892 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1893 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1894 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1895 msg_info.hdr.serializing_sc = NULL; 1896 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1897#if 0 1898 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1899#endif 1900 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1901 retval = 1; 1902 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1903 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1904 } 1905 break; 1906 case CTL_ACTION_ERROR: 1907 default: 1908 /* "Internal target failure" */ 1909 ctl_set_sense_data(&msg_info.scsi.sense_data, 1910 lun, 1911 /*sense_format*/SSD_TYPE_NONE, 1912 /*current_error*/ 1, 1913 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1914 /*asc*/ 0x44, 1915 /*ascq*/ 0x00, 1916 SSD_ELEM_NONE); 1917 1918 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1919 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1920 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1921 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1922 msg_info.hdr.serializing_sc = NULL; 1923 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1924#if 0 1925 printf("BAD JUJU:Major Bummer HW Error\n"); 1926#endif 1927 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1928 retval = 1; 1929 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1930 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1931 } 1932 break; 1933 } 1934 mtx_unlock(&lun->lun_lock); 1935 return (retval); 1936} 1937 1938static int 1939ctl_ioctl_submit_wait(union ctl_io *io) 1940{ 1941 struct ctl_fe_ioctl_params params; 1942 ctl_fe_ioctl_state last_state; 1943 int done, retval; 1944 1945 retval = 0; 1946 1947 bzero(¶ms, sizeof(params)); 1948 1949 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1950 cv_init(¶ms.sem, "ctlioccv"); 1951 params.state = CTL_IOCTL_INPROG; 1952 last_state = params.state; 1953 1954 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1955 1956 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1957 1958 /* This shouldn't happen */ 1959 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1960 return (retval); 1961 1962 done = 0; 1963 1964 do { 1965 mtx_lock(¶ms.ioctl_mtx); 1966 /* 1967 * Check the state here, and don't sleep if the state has 1968 * already changed (i.e. wakeup has already occured, but we 1969 * weren't waiting yet). 1970 */ 1971 if (params.state == last_state) { 1972 /* XXX KDM cv_wait_sig instead? */ 1973 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1974 } 1975 last_state = params.state; 1976 1977 switch (params.state) { 1978 case CTL_IOCTL_INPROG: 1979 /* Why did we wake up? */ 1980 /* XXX KDM error here? */ 1981 mtx_unlock(¶ms.ioctl_mtx); 1982 break; 1983 case CTL_IOCTL_DATAMOVE: 1984 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1985 1986 /* 1987 * change last_state back to INPROG to avoid 1988 * deadlock on subsequent data moves. 1989 */ 1990 params.state = last_state = CTL_IOCTL_INPROG; 1991 1992 mtx_unlock(¶ms.ioctl_mtx); 1993 ctl_ioctl_do_datamove(&io->scsiio); 1994 /* 1995 * Note that in some cases, most notably writes, 1996 * this will queue the I/O and call us back later. 1997 * In other cases, generally reads, this routine 1998 * will immediately call back and wake us up, 1999 * probably using our own context. 2000 */ 2001 io->scsiio.be_move_done(io); 2002 break; 2003 case CTL_IOCTL_DONE: 2004 mtx_unlock(¶ms.ioctl_mtx); 2005 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2006 done = 1; 2007 break; 2008 default: 2009 mtx_unlock(¶ms.ioctl_mtx); 2010 /* XXX KDM error here? */ 2011 break; 2012 } 2013 } while (done == 0); 2014 2015 mtx_destroy(¶ms.ioctl_mtx); 2016 cv_destroy(¶ms.sem); 2017 2018 return (CTL_RETVAL_COMPLETE); 2019} 2020 2021static void 2022ctl_ioctl_datamove(union ctl_io *io) 2023{ 2024 struct ctl_fe_ioctl_params *params; 2025 2026 params = (struct ctl_fe_ioctl_params *) 2027 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2028 2029 mtx_lock(¶ms->ioctl_mtx); 2030 params->state = CTL_IOCTL_DATAMOVE; 2031 cv_broadcast(¶ms->sem); 2032 mtx_unlock(¶ms->ioctl_mtx); 2033} 2034 2035static void 2036ctl_ioctl_done(union ctl_io *io) 2037{ 2038 struct ctl_fe_ioctl_params *params; 2039 2040 params = (struct ctl_fe_ioctl_params *) 2041 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2042 2043 mtx_lock(¶ms->ioctl_mtx); 2044 params->state = CTL_IOCTL_DONE; 2045 cv_broadcast(¶ms->sem); 2046 mtx_unlock(¶ms->ioctl_mtx); 2047} 2048 2049static void 2050ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2051{ 2052 struct ctl_fe_ioctl_startstop_info *sd_info; 2053 2054 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2055 2056 sd_info->hs_info.status = metatask->status; 2057 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2058 sd_info->hs_info.luns_complete = 2059 metatask->taskinfo.startstop.luns_complete; 2060 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2061 2062 cv_broadcast(&sd_info->sem); 2063} 2064 2065static void 2066ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2067{ 2068 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2069 2070 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2071 2072 mtx_lock(fe_bbr_info->lock); 2073 fe_bbr_info->bbr_info->status = metatask->status; 2074 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2075 fe_bbr_info->wakeup_done = 1; 2076 mtx_unlock(fe_bbr_info->lock); 2077 2078 cv_broadcast(&fe_bbr_info->sem); 2079} 2080 2081/* 2082 * Returns 0 for success, errno for failure. 2083 */ 2084static int 2085ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2086 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2087{ 2088 union ctl_io *io; 2089 int retval; 2090 2091 retval = 0; 2092 2093 mtx_lock(&lun->lun_lock); 2094 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2095 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2096 ooa_links)) { 2097 struct ctl_ooa_entry *entry; 2098 2099 /* 2100 * If we've got more than we can fit, just count the 2101 * remaining entries. 2102 */ 2103 if (*cur_fill_num >= ooa_hdr->alloc_num) 2104 continue; 2105 2106 entry = &kern_entries[*cur_fill_num]; 2107 2108 entry->tag_num = io->scsiio.tag_num; 2109 entry->lun_num = lun->lun; 2110#ifdef CTL_TIME_IO 2111 entry->start_bt = io->io_hdr.start_bt; 2112#endif 2113 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2114 entry->cdb_len = io->scsiio.cdb_len; 2115 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2116 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2117 2118 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2119 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2120 2121 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2122 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2123 2124 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2125 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2126 2127 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2128 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2129 } 2130 mtx_unlock(&lun->lun_lock); 2131 2132 return (retval); 2133} 2134 2135static void * 2136ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2137 size_t error_str_len) 2138{ 2139 void *kptr; 2140 2141 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2142 2143 if (copyin(user_addr, kptr, len) != 0) { 2144 snprintf(error_str, error_str_len, "Error copying %d bytes " 2145 "from user address %p to kernel address %p", len, 2146 user_addr, kptr); 2147 free(kptr, M_CTL); 2148 return (NULL); 2149 } 2150 2151 return (kptr); 2152} 2153 2154static void 2155ctl_free_args(int num_args, struct ctl_be_arg *args) 2156{ 2157 int i; 2158 2159 if (args == NULL) 2160 return; 2161 2162 for (i = 0; i < num_args; i++) { 2163 free(args[i].kname, M_CTL); 2164 free(args[i].kvalue, M_CTL); 2165 } 2166 2167 free(args, M_CTL); 2168} 2169 2170static struct ctl_be_arg * 2171ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2172 char *error_str, size_t error_str_len) 2173{ 2174 struct ctl_be_arg *args; 2175 int i; 2176 2177 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2178 error_str, error_str_len); 2179 2180 if (args == NULL) 2181 goto bailout; 2182 2183 for (i = 0; i < num_args; i++) { 2184 args[i].kname = NULL; 2185 args[i].kvalue = NULL; 2186 } 2187 2188 for (i = 0; i < num_args; i++) { 2189 uint8_t *tmpptr; 2190 2191 args[i].kname = ctl_copyin_alloc(args[i].name, 2192 args[i].namelen, error_str, error_str_len); 2193 if (args[i].kname == NULL) 2194 goto bailout; 2195 2196 if (args[i].kname[args[i].namelen - 1] != '\0') { 2197 snprintf(error_str, error_str_len, "Argument %d " 2198 "name is not NUL-terminated", i); 2199 goto bailout; 2200 } 2201 2202 if (args[i].flags & CTL_BEARG_RD) { 2203 tmpptr = ctl_copyin_alloc(args[i].value, 2204 args[i].vallen, error_str, error_str_len); 2205 if (tmpptr == NULL) 2206 goto bailout; 2207 if ((args[i].flags & CTL_BEARG_ASCII) 2208 && (tmpptr[args[i].vallen - 1] != '\0')) { 2209 snprintf(error_str, error_str_len, "Argument " 2210 "%d value is not NUL-terminated", i); 2211 goto bailout; 2212 } 2213 args[i].kvalue = tmpptr; 2214 } else { 2215 args[i].kvalue = malloc(args[i].vallen, 2216 M_CTL, M_WAITOK | M_ZERO); 2217 } 2218 } 2219 2220 return (args); 2221bailout: 2222 2223 ctl_free_args(num_args, args); 2224 2225 return (NULL); 2226} 2227 2228static void 2229ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2230{ 2231 int i; 2232 2233 for (i = 0; i < num_args; i++) { 2234 if (args[i].flags & CTL_BEARG_WR) 2235 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2236 } 2237} 2238 2239/* 2240 * Escape characters that are illegal or not recommended in XML. 2241 */ 2242int 2243ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2244{ 2245 char *end = str + size; 2246 int retval; 2247 2248 retval = 0; 2249 2250 for (; *str && str < end; str++) { 2251 switch (*str) { 2252 case '&': 2253 retval = sbuf_printf(sb, "&"); 2254 break; 2255 case '>': 2256 retval = sbuf_printf(sb, ">"); 2257 break; 2258 case '<': 2259 retval = sbuf_printf(sb, "<"); 2260 break; 2261 default: 2262 retval = sbuf_putc(sb, *str); 2263 break; 2264 } 2265 2266 if (retval != 0) 2267 break; 2268 2269 } 2270 2271 return (retval); 2272} 2273 2274static void 2275ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2276{ 2277 struct scsi_vpd_id_descriptor *desc; 2278 int i; 2279 2280 if (id == NULL || id->len < 4) 2281 return; 2282 desc = (struct scsi_vpd_id_descriptor *)id->data; 2283 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2284 case SVPD_ID_TYPE_T10: 2285 sbuf_printf(sb, "t10."); 2286 break; 2287 case SVPD_ID_TYPE_EUI64: 2288 sbuf_printf(sb, "eui."); 2289 break; 2290 case SVPD_ID_TYPE_NAA: 2291 sbuf_printf(sb, "naa."); 2292 break; 2293 case SVPD_ID_TYPE_SCSI_NAME: 2294 break; 2295 } 2296 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2297 case SVPD_ID_CODESET_BINARY: 2298 for (i = 0; i < desc->length; i++) 2299 sbuf_printf(sb, "%02x", desc->identifier[i]); 2300 break; 2301 case SVPD_ID_CODESET_ASCII: 2302 sbuf_printf(sb, "%.*s", (int)desc->length, 2303 (char *)desc->identifier); 2304 break; 2305 case SVPD_ID_CODESET_UTF8: 2306 sbuf_printf(sb, "%s", (char *)desc->identifier); 2307 break; 2308 } 2309} 2310 2311static int 2312ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2313 struct thread *td) 2314{ 2315 struct ctl_softc *softc; 2316 int retval; 2317 2318 softc = control_softc; 2319 2320 retval = 0; 2321 2322 switch (cmd) { 2323 case CTL_IO: { 2324 union ctl_io *io; 2325 void *pool_tmp; 2326 2327 /* 2328 * If we haven't been "enabled", don't allow any SCSI I/O 2329 * to this FETD. 2330 */ 2331 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2332 retval = EPERM; 2333 break; 2334 } 2335 2336 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2337 2338 /* 2339 * Need to save the pool reference so it doesn't get 2340 * spammed by the user's ctl_io. 2341 */ 2342 pool_tmp = io->io_hdr.pool; 2343 memcpy(io, (void *)addr, sizeof(*io)); 2344 io->io_hdr.pool = pool_tmp; 2345 2346 /* 2347 * No status yet, so make sure the status is set properly. 2348 */ 2349 io->io_hdr.status = CTL_STATUS_NONE; 2350 2351 /* 2352 * The user sets the initiator ID, target and LUN IDs. 2353 */ 2354 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2355 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2356 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2357 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2358 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2359 2360 retval = ctl_ioctl_submit_wait(io); 2361 2362 if (retval != 0) { 2363 ctl_free_io(io); 2364 break; 2365 } 2366 2367 memcpy((void *)addr, io, sizeof(*io)); 2368 2369 /* return this to our pool */ 2370 ctl_free_io(io); 2371 2372 break; 2373 } 2374 case CTL_ENABLE_PORT: 2375 case CTL_DISABLE_PORT: 2376 case CTL_SET_PORT_WWNS: { 2377 struct ctl_port *port; 2378 struct ctl_port_entry *entry; 2379 2380 entry = (struct ctl_port_entry *)addr; 2381 2382 mtx_lock(&softc->ctl_lock); 2383 STAILQ_FOREACH(port, &softc->port_list, links) { 2384 int action, done; 2385 2386 action = 0; 2387 done = 0; 2388 2389 if ((entry->port_type == CTL_PORT_NONE) 2390 && (entry->targ_port == port->targ_port)) { 2391 /* 2392 * If the user only wants to enable or 2393 * disable or set WWNs on a specific port, 2394 * do the operation and we're done. 2395 */ 2396 action = 1; 2397 done = 1; 2398 } else if (entry->port_type & port->port_type) { 2399 /* 2400 * Compare the user's type mask with the 2401 * particular frontend type to see if we 2402 * have a match. 2403 */ 2404 action = 1; 2405 done = 0; 2406 2407 /* 2408 * Make sure the user isn't trying to set 2409 * WWNs on multiple ports at the same time. 2410 */ 2411 if (cmd == CTL_SET_PORT_WWNS) { 2412 printf("%s: Can't set WWNs on " 2413 "multiple ports\n", __func__); 2414 retval = EINVAL; 2415 break; 2416 } 2417 } 2418 if (action != 0) { 2419 /* 2420 * XXX KDM we have to drop the lock here, 2421 * because the online/offline operations 2422 * can potentially block. We need to 2423 * reference count the frontends so they 2424 * can't go away, 2425 */ 2426 mtx_unlock(&softc->ctl_lock); 2427 2428 if (cmd == CTL_ENABLE_PORT) { 2429 struct ctl_lun *lun; 2430 2431 STAILQ_FOREACH(lun, &softc->lun_list, 2432 links) { 2433 port->lun_enable(port->targ_lun_arg, 2434 lun->target, 2435 lun->lun); 2436 } 2437 2438 ctl_port_online(port); 2439 } else if (cmd == CTL_DISABLE_PORT) { 2440 struct ctl_lun *lun; 2441 2442 ctl_port_offline(port); 2443 2444 STAILQ_FOREACH(lun, &softc->lun_list, 2445 links) { 2446 port->lun_disable( 2447 port->targ_lun_arg, 2448 lun->target, 2449 lun->lun); 2450 } 2451 } 2452 2453 mtx_lock(&softc->ctl_lock); 2454 2455 if (cmd == CTL_SET_PORT_WWNS) 2456 ctl_port_set_wwns(port, 2457 (entry->flags & CTL_PORT_WWNN_VALID) ? 2458 1 : 0, entry->wwnn, 2459 (entry->flags & CTL_PORT_WWPN_VALID) ? 2460 1 : 0, entry->wwpn); 2461 } 2462 if (done != 0) 2463 break; 2464 } 2465 mtx_unlock(&softc->ctl_lock); 2466 break; 2467 } 2468 case CTL_GET_PORT_LIST: { 2469 struct ctl_port *port; 2470 struct ctl_port_list *list; 2471 int i; 2472 2473 list = (struct ctl_port_list *)addr; 2474 2475 if (list->alloc_len != (list->alloc_num * 2476 sizeof(struct ctl_port_entry))) { 2477 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2478 "alloc_num %u * sizeof(struct ctl_port_entry) " 2479 "%zu\n", __func__, list->alloc_len, 2480 list->alloc_num, sizeof(struct ctl_port_entry)); 2481 retval = EINVAL; 2482 break; 2483 } 2484 list->fill_len = 0; 2485 list->fill_num = 0; 2486 list->dropped_num = 0; 2487 i = 0; 2488 mtx_lock(&softc->ctl_lock); 2489 STAILQ_FOREACH(port, &softc->port_list, links) { 2490 struct ctl_port_entry entry, *list_entry; 2491 2492 if (list->fill_num >= list->alloc_num) { 2493 list->dropped_num++; 2494 continue; 2495 } 2496 2497 entry.port_type = port->port_type; 2498 strlcpy(entry.port_name, port->port_name, 2499 sizeof(entry.port_name)); 2500 entry.targ_port = port->targ_port; 2501 entry.physical_port = port->physical_port; 2502 entry.virtual_port = port->virtual_port; 2503 entry.wwnn = port->wwnn; 2504 entry.wwpn = port->wwpn; 2505 if (port->status & CTL_PORT_STATUS_ONLINE) 2506 entry.online = 1; 2507 else 2508 entry.online = 0; 2509 2510 list_entry = &list->entries[i]; 2511 2512 retval = copyout(&entry, list_entry, sizeof(entry)); 2513 if (retval != 0) { 2514 printf("%s: CTL_GET_PORT_LIST: copyout " 2515 "returned %d\n", __func__, retval); 2516 break; 2517 } 2518 i++; 2519 list->fill_num++; 2520 list->fill_len += sizeof(entry); 2521 } 2522 mtx_unlock(&softc->ctl_lock); 2523 2524 /* 2525 * If this is non-zero, we had a copyout fault, so there's 2526 * probably no point in attempting to set the status inside 2527 * the structure. 2528 */ 2529 if (retval != 0) 2530 break; 2531 2532 if (list->dropped_num > 0) 2533 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2534 else 2535 list->status = CTL_PORT_LIST_OK; 2536 break; 2537 } 2538 case CTL_DUMP_OOA: { 2539 struct ctl_lun *lun; 2540 union ctl_io *io; 2541 char printbuf[128]; 2542 struct sbuf sb; 2543 2544 mtx_lock(&softc->ctl_lock); 2545 printf("Dumping OOA queues:\n"); 2546 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2547 mtx_lock(&lun->lun_lock); 2548 for (io = (union ctl_io *)TAILQ_FIRST( 2549 &lun->ooa_queue); io != NULL; 2550 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2551 ooa_links)) { 2552 sbuf_new(&sb, printbuf, sizeof(printbuf), 2553 SBUF_FIXEDLEN); 2554 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2555 (intmax_t)lun->lun, 2556 io->scsiio.tag_num, 2557 (io->io_hdr.flags & 2558 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2559 (io->io_hdr.flags & 2560 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2561 (io->io_hdr.flags & 2562 CTL_FLAG_ABORT) ? " ABORT" : "", 2563 (io->io_hdr.flags & 2564 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2565 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2566 sbuf_finish(&sb); 2567 printf("%s\n", sbuf_data(&sb)); 2568 } 2569 mtx_unlock(&lun->lun_lock); 2570 } 2571 printf("OOA queues dump done\n"); 2572 mtx_unlock(&softc->ctl_lock); 2573 break; 2574 } 2575 case CTL_GET_OOA: { 2576 struct ctl_lun *lun; 2577 struct ctl_ooa *ooa_hdr; 2578 struct ctl_ooa_entry *entries; 2579 uint32_t cur_fill_num; 2580 2581 ooa_hdr = (struct ctl_ooa *)addr; 2582 2583 if ((ooa_hdr->alloc_len == 0) 2584 || (ooa_hdr->alloc_num == 0)) { 2585 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2586 "must be non-zero\n", __func__, 2587 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2588 retval = EINVAL; 2589 break; 2590 } 2591 2592 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2593 sizeof(struct ctl_ooa_entry))) { 2594 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2595 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2596 __func__, ooa_hdr->alloc_len, 2597 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2598 retval = EINVAL; 2599 break; 2600 } 2601 2602 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2603 if (entries == NULL) { 2604 printf("%s: could not allocate %d bytes for OOA " 2605 "dump\n", __func__, ooa_hdr->alloc_len); 2606 retval = ENOMEM; 2607 break; 2608 } 2609 2610 mtx_lock(&softc->ctl_lock); 2611 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2612 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2613 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2614 mtx_unlock(&softc->ctl_lock); 2615 free(entries, M_CTL); 2616 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2617 __func__, (uintmax_t)ooa_hdr->lun_num); 2618 retval = EINVAL; 2619 break; 2620 } 2621 2622 cur_fill_num = 0; 2623 2624 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2625 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2626 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2627 ooa_hdr, entries); 2628 if (retval != 0) 2629 break; 2630 } 2631 if (retval != 0) { 2632 mtx_unlock(&softc->ctl_lock); 2633 free(entries, M_CTL); 2634 break; 2635 } 2636 } else { 2637 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2638 2639 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2640 entries); 2641 } 2642 mtx_unlock(&softc->ctl_lock); 2643 2644 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2645 ooa_hdr->fill_len = ooa_hdr->fill_num * 2646 sizeof(struct ctl_ooa_entry); 2647 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2648 if (retval != 0) { 2649 printf("%s: error copying out %d bytes for OOA dump\n", 2650 __func__, ooa_hdr->fill_len); 2651 } 2652 2653 getbintime(&ooa_hdr->cur_bt); 2654 2655 if (cur_fill_num > ooa_hdr->alloc_num) { 2656 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2657 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2658 } else { 2659 ooa_hdr->dropped_num = 0; 2660 ooa_hdr->status = CTL_OOA_OK; 2661 } 2662 2663 free(entries, M_CTL); 2664 break; 2665 } 2666 case CTL_CHECK_OOA: { 2667 union ctl_io *io; 2668 struct ctl_lun *lun; 2669 struct ctl_ooa_info *ooa_info; 2670 2671 2672 ooa_info = (struct ctl_ooa_info *)addr; 2673 2674 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2675 ooa_info->status = CTL_OOA_INVALID_LUN; 2676 break; 2677 } 2678 mtx_lock(&softc->ctl_lock); 2679 lun = softc->ctl_luns[ooa_info->lun_id]; 2680 if (lun == NULL) { 2681 mtx_unlock(&softc->ctl_lock); 2682 ooa_info->status = CTL_OOA_INVALID_LUN; 2683 break; 2684 } 2685 mtx_lock(&lun->lun_lock); 2686 mtx_unlock(&softc->ctl_lock); 2687 ooa_info->num_entries = 0; 2688 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2689 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2690 &io->io_hdr, ooa_links)) { 2691 ooa_info->num_entries++; 2692 } 2693 mtx_unlock(&lun->lun_lock); 2694 2695 ooa_info->status = CTL_OOA_SUCCESS; 2696 2697 break; 2698 } 2699 case CTL_HARD_START: 2700 case CTL_HARD_STOP: { 2701 struct ctl_fe_ioctl_startstop_info ss_info; 2702 struct cfi_metatask *metatask; 2703 struct mtx hs_mtx; 2704 2705 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2706 2707 cv_init(&ss_info.sem, "hard start/stop cv" ); 2708 2709 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2710 if (metatask == NULL) { 2711 retval = ENOMEM; 2712 mtx_destroy(&hs_mtx); 2713 break; 2714 } 2715 2716 if (cmd == CTL_HARD_START) 2717 metatask->tasktype = CFI_TASK_STARTUP; 2718 else 2719 metatask->tasktype = CFI_TASK_SHUTDOWN; 2720 2721 metatask->callback = ctl_ioctl_hard_startstop_callback; 2722 metatask->callback_arg = &ss_info; 2723 2724 cfi_action(metatask); 2725 2726 /* Wait for the callback */ 2727 mtx_lock(&hs_mtx); 2728 cv_wait_sig(&ss_info.sem, &hs_mtx); 2729 mtx_unlock(&hs_mtx); 2730 2731 /* 2732 * All information has been copied from the metatask by the 2733 * time cv_broadcast() is called, so we free the metatask here. 2734 */ 2735 cfi_free_metatask(metatask); 2736 2737 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2738 2739 mtx_destroy(&hs_mtx); 2740 break; 2741 } 2742 case CTL_BBRREAD: { 2743 struct ctl_bbrread_info *bbr_info; 2744 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2745 struct mtx bbr_mtx; 2746 struct cfi_metatask *metatask; 2747 2748 bbr_info = (struct ctl_bbrread_info *)addr; 2749 2750 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2751 2752 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2753 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2754 2755 fe_bbr_info.bbr_info = bbr_info; 2756 fe_bbr_info.lock = &bbr_mtx; 2757 2758 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2759 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2760 2761 if (metatask == NULL) { 2762 mtx_destroy(&bbr_mtx); 2763 cv_destroy(&fe_bbr_info.sem); 2764 retval = ENOMEM; 2765 break; 2766 } 2767 metatask->tasktype = CFI_TASK_BBRREAD; 2768 metatask->callback = ctl_ioctl_bbrread_callback; 2769 metatask->callback_arg = &fe_bbr_info; 2770 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2771 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2772 metatask->taskinfo.bbrread.len = bbr_info->len; 2773 2774 cfi_action(metatask); 2775 2776 mtx_lock(&bbr_mtx); 2777 while (fe_bbr_info.wakeup_done == 0) 2778 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2779 mtx_unlock(&bbr_mtx); 2780 2781 bbr_info->status = metatask->status; 2782 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2783 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2784 memcpy(&bbr_info->sense_data, 2785 &metatask->taskinfo.bbrread.sense_data, 2786 ctl_min(sizeof(bbr_info->sense_data), 2787 sizeof(metatask->taskinfo.bbrread.sense_data))); 2788 2789 cfi_free_metatask(metatask); 2790 2791 mtx_destroy(&bbr_mtx); 2792 cv_destroy(&fe_bbr_info.sem); 2793 2794 break; 2795 } 2796 case CTL_DELAY_IO: { 2797 struct ctl_io_delay_info *delay_info; 2798#ifdef CTL_IO_DELAY 2799 struct ctl_lun *lun; 2800#endif /* CTL_IO_DELAY */ 2801 2802 delay_info = (struct ctl_io_delay_info *)addr; 2803 2804#ifdef CTL_IO_DELAY 2805 mtx_lock(&softc->ctl_lock); 2806 2807 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2808 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2809 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2810 } else { 2811 lun = softc->ctl_luns[delay_info->lun_id]; 2812 mtx_lock(&lun->lun_lock); 2813 2814 delay_info->status = CTL_DELAY_STATUS_OK; 2815 2816 switch (delay_info->delay_type) { 2817 case CTL_DELAY_TYPE_CONT: 2818 break; 2819 case CTL_DELAY_TYPE_ONESHOT: 2820 break; 2821 default: 2822 delay_info->status = 2823 CTL_DELAY_STATUS_INVALID_TYPE; 2824 break; 2825 } 2826 2827 switch (delay_info->delay_loc) { 2828 case CTL_DELAY_LOC_DATAMOVE: 2829 lun->delay_info.datamove_type = 2830 delay_info->delay_type; 2831 lun->delay_info.datamove_delay = 2832 delay_info->delay_secs; 2833 break; 2834 case CTL_DELAY_LOC_DONE: 2835 lun->delay_info.done_type = 2836 delay_info->delay_type; 2837 lun->delay_info.done_delay = 2838 delay_info->delay_secs; 2839 break; 2840 default: 2841 delay_info->status = 2842 CTL_DELAY_STATUS_INVALID_LOC; 2843 break; 2844 } 2845 mtx_unlock(&lun->lun_lock); 2846 } 2847 2848 mtx_unlock(&softc->ctl_lock); 2849#else 2850 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2851#endif /* CTL_IO_DELAY */ 2852 break; 2853 } 2854 case CTL_REALSYNC_SET: { 2855 int *syncstate; 2856 2857 syncstate = (int *)addr; 2858 2859 mtx_lock(&softc->ctl_lock); 2860 switch (*syncstate) { 2861 case 0: 2862 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2863 break; 2864 case 1: 2865 softc->flags |= CTL_FLAG_REAL_SYNC; 2866 break; 2867 default: 2868 retval = EINVAL; 2869 break; 2870 } 2871 mtx_unlock(&softc->ctl_lock); 2872 break; 2873 } 2874 case CTL_REALSYNC_GET: { 2875 int *syncstate; 2876 2877 syncstate = (int*)addr; 2878 2879 mtx_lock(&softc->ctl_lock); 2880 if (softc->flags & CTL_FLAG_REAL_SYNC) 2881 *syncstate = 1; 2882 else 2883 *syncstate = 0; 2884 mtx_unlock(&softc->ctl_lock); 2885 2886 break; 2887 } 2888 case CTL_SETSYNC: 2889 case CTL_GETSYNC: { 2890 struct ctl_sync_info *sync_info; 2891 struct ctl_lun *lun; 2892 2893 sync_info = (struct ctl_sync_info *)addr; 2894 2895 mtx_lock(&softc->ctl_lock); 2896 lun = softc->ctl_luns[sync_info->lun_id]; 2897 if (lun == NULL) { 2898 mtx_unlock(&softc->ctl_lock); 2899 sync_info->status = CTL_GS_SYNC_NO_LUN; 2900 } 2901 /* 2902 * Get or set the sync interval. We're not bounds checking 2903 * in the set case, hopefully the user won't do something 2904 * silly. 2905 */ 2906 mtx_lock(&lun->lun_lock); 2907 mtx_unlock(&softc->ctl_lock); 2908 if (cmd == CTL_GETSYNC) 2909 sync_info->sync_interval = lun->sync_interval; 2910 else 2911 lun->sync_interval = sync_info->sync_interval; 2912 mtx_unlock(&lun->lun_lock); 2913 2914 sync_info->status = CTL_GS_SYNC_OK; 2915 2916 break; 2917 } 2918 case CTL_GETSTATS: { 2919 struct ctl_stats *stats; 2920 struct ctl_lun *lun; 2921 int i; 2922 2923 stats = (struct ctl_stats *)addr; 2924 2925 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2926 stats->alloc_len) { 2927 stats->status = CTL_SS_NEED_MORE_SPACE; 2928 stats->num_luns = softc->num_luns; 2929 break; 2930 } 2931 /* 2932 * XXX KDM no locking here. If the LUN list changes, 2933 * things can blow up. 2934 */ 2935 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2936 i++, lun = STAILQ_NEXT(lun, links)) { 2937 retval = copyout(&lun->stats, &stats->lun_stats[i], 2938 sizeof(lun->stats)); 2939 if (retval != 0) 2940 break; 2941 } 2942 stats->num_luns = softc->num_luns; 2943 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2944 softc->num_luns; 2945 stats->status = CTL_SS_OK; 2946#ifdef CTL_TIME_IO 2947 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2948#else 2949 stats->flags = CTL_STATS_FLAG_NONE; 2950#endif 2951 getnanouptime(&stats->timestamp); 2952 break; 2953 } 2954 case CTL_ERROR_INJECT: { 2955 struct ctl_error_desc *err_desc, *new_err_desc; 2956 struct ctl_lun *lun; 2957 2958 err_desc = (struct ctl_error_desc *)addr; 2959 2960 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2961 M_WAITOK | M_ZERO); 2962 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2963 2964 mtx_lock(&softc->ctl_lock); 2965 lun = softc->ctl_luns[err_desc->lun_id]; 2966 if (lun == NULL) { 2967 mtx_unlock(&softc->ctl_lock); 2968 free(new_err_desc, M_CTL); 2969 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2970 __func__, (uintmax_t)err_desc->lun_id); 2971 retval = EINVAL; 2972 break; 2973 } 2974 mtx_lock(&lun->lun_lock); 2975 mtx_unlock(&softc->ctl_lock); 2976 2977 /* 2978 * We could do some checking here to verify the validity 2979 * of the request, but given the complexity of error 2980 * injection requests, the checking logic would be fairly 2981 * complex. 2982 * 2983 * For now, if the request is invalid, it just won't get 2984 * executed and might get deleted. 2985 */ 2986 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2987 2988 /* 2989 * XXX KDM check to make sure the serial number is unique, 2990 * in case we somehow manage to wrap. That shouldn't 2991 * happen for a very long time, but it's the right thing to 2992 * do. 2993 */ 2994 new_err_desc->serial = lun->error_serial; 2995 err_desc->serial = lun->error_serial; 2996 lun->error_serial++; 2997 2998 mtx_unlock(&lun->lun_lock); 2999 break; 3000 } 3001 case CTL_ERROR_INJECT_DELETE: { 3002 struct ctl_error_desc *delete_desc, *desc, *desc2; 3003 struct ctl_lun *lun; 3004 int delete_done; 3005 3006 delete_desc = (struct ctl_error_desc *)addr; 3007 delete_done = 0; 3008 3009 mtx_lock(&softc->ctl_lock); 3010 lun = softc->ctl_luns[delete_desc->lun_id]; 3011 if (lun == NULL) { 3012 mtx_unlock(&softc->ctl_lock); 3013 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3014 __func__, (uintmax_t)delete_desc->lun_id); 3015 retval = EINVAL; 3016 break; 3017 } 3018 mtx_lock(&lun->lun_lock); 3019 mtx_unlock(&softc->ctl_lock); 3020 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3021 if (desc->serial != delete_desc->serial) 3022 continue; 3023 3024 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3025 links); 3026 free(desc, M_CTL); 3027 delete_done = 1; 3028 } 3029 mtx_unlock(&lun->lun_lock); 3030 if (delete_done == 0) { 3031 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3032 "error serial %ju on LUN %u\n", __func__, 3033 delete_desc->serial, delete_desc->lun_id); 3034 retval = EINVAL; 3035 break; 3036 } 3037 break; 3038 } 3039 case CTL_DUMP_STRUCTS: { 3040 int i, j, k, idx; 3041 struct ctl_port *port; 3042 struct ctl_frontend *fe; 3043 3044 mtx_lock(&softc->ctl_lock); 3045 printf("CTL Persistent Reservation information start:\n"); 3046 for (i = 0; i < CTL_MAX_LUNS; i++) { 3047 struct ctl_lun *lun; 3048 3049 lun = softc->ctl_luns[i]; 3050 3051 if ((lun == NULL) 3052 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3053 continue; 3054 3055 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3056 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3057 idx = j * CTL_MAX_INIT_PER_PORT + k; 3058 if (lun->pr_keys[idx] == 0) 3059 continue; 3060 printf(" LUN %d port %d iid %d key " 3061 "%#jx\n", i, j, k, 3062 (uintmax_t)lun->pr_keys[idx]); 3063 } 3064 } 3065 } 3066 printf("CTL Persistent Reservation information end\n"); 3067 printf("CTL Ports:\n"); 3068 STAILQ_FOREACH(port, &softc->port_list, links) { 3069 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3070 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3071 port->frontend->name, port->port_type, 3072 port->physical_port, port->virtual_port, 3073 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3074 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3075 if (port->wwpn_iid[j].in_use == 0 && 3076 port->wwpn_iid[j].wwpn == 0 && 3077 port->wwpn_iid[j].name == NULL) 3078 continue; 3079 3080 printf(" iid %u use %d WWPN %#jx '%s'\n", 3081 j, port->wwpn_iid[j].in_use, 3082 (uintmax_t)port->wwpn_iid[j].wwpn, 3083 port->wwpn_iid[j].name); 3084 } 3085 } 3086 printf("CTL Port information end\n"); 3087 mtx_unlock(&softc->ctl_lock); 3088 /* 3089 * XXX KDM calling this without a lock. We'd likely want 3090 * to drop the lock before calling the frontend's dump 3091 * routine anyway. 3092 */ 3093 printf("CTL Frontends:\n"); 3094 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3095 printf(" Frontend '%s'\n", fe->name); 3096 if (fe->fe_dump != NULL) 3097 fe->fe_dump(); 3098 } 3099 printf("CTL Frontend information end\n"); 3100 break; 3101 } 3102 case CTL_LUN_REQ: { 3103 struct ctl_lun_req *lun_req; 3104 struct ctl_backend_driver *backend; 3105 3106 lun_req = (struct ctl_lun_req *)addr; 3107 3108 backend = ctl_backend_find(lun_req->backend); 3109 if (backend == NULL) { 3110 lun_req->status = CTL_LUN_ERROR; 3111 snprintf(lun_req->error_str, 3112 sizeof(lun_req->error_str), 3113 "Backend \"%s\" not found.", 3114 lun_req->backend); 3115 break; 3116 } 3117 if (lun_req->num_be_args > 0) { 3118 lun_req->kern_be_args = ctl_copyin_args( 3119 lun_req->num_be_args, 3120 lun_req->be_args, 3121 lun_req->error_str, 3122 sizeof(lun_req->error_str)); 3123 if (lun_req->kern_be_args == NULL) { 3124 lun_req->status = CTL_LUN_ERROR; 3125 break; 3126 } 3127 } 3128 3129 retval = backend->ioctl(dev, cmd, addr, flag, td); 3130 3131 if (lun_req->num_be_args > 0) { 3132 ctl_copyout_args(lun_req->num_be_args, 3133 lun_req->kern_be_args); 3134 ctl_free_args(lun_req->num_be_args, 3135 lun_req->kern_be_args); 3136 } 3137 break; 3138 } 3139 case CTL_LUN_LIST: { 3140 struct sbuf *sb; 3141 struct ctl_lun *lun; 3142 struct ctl_lun_list *list; 3143 struct ctl_option *opt; 3144 3145 list = (struct ctl_lun_list *)addr; 3146 3147 /* 3148 * Allocate a fixed length sbuf here, based on the length 3149 * of the user's buffer. We could allocate an auto-extending 3150 * buffer, and then tell the user how much larger our 3151 * amount of data is than his buffer, but that presents 3152 * some problems: 3153 * 3154 * 1. The sbuf(9) routines use a blocking malloc, and so 3155 * we can't hold a lock while calling them with an 3156 * auto-extending buffer. 3157 * 3158 * 2. There is not currently a LUN reference counting 3159 * mechanism, outside of outstanding transactions on 3160 * the LUN's OOA queue. So a LUN could go away on us 3161 * while we're getting the LUN number, backend-specific 3162 * information, etc. Thus, given the way things 3163 * currently work, we need to hold the CTL lock while 3164 * grabbing LUN information. 3165 * 3166 * So, from the user's standpoint, the best thing to do is 3167 * allocate what he thinks is a reasonable buffer length, 3168 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3169 * double the buffer length and try again. (And repeat 3170 * that until he succeeds.) 3171 */ 3172 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3173 if (sb == NULL) { 3174 list->status = CTL_LUN_LIST_ERROR; 3175 snprintf(list->error_str, sizeof(list->error_str), 3176 "Unable to allocate %d bytes for LUN list", 3177 list->alloc_len); 3178 break; 3179 } 3180 3181 sbuf_printf(sb, "<ctllunlist>\n"); 3182 3183 mtx_lock(&softc->ctl_lock); 3184 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3185 mtx_lock(&lun->lun_lock); 3186 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3187 (uintmax_t)lun->lun); 3188 3189 /* 3190 * Bail out as soon as we see that we've overfilled 3191 * the buffer. 3192 */ 3193 if (retval != 0) 3194 break; 3195 3196 retval = sbuf_printf(sb, "\t<backend_type>%s" 3197 "</backend_type>\n", 3198 (lun->backend == NULL) ? "none" : 3199 lun->backend->name); 3200 3201 if (retval != 0) 3202 break; 3203 3204 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3205 lun->be_lun->lun_type); 3206 3207 if (retval != 0) 3208 break; 3209 3210 if (lun->backend == NULL) { 3211 retval = sbuf_printf(sb, "</lun>\n"); 3212 if (retval != 0) 3213 break; 3214 continue; 3215 } 3216 3217 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3218 (lun->be_lun->maxlba > 0) ? 3219 lun->be_lun->maxlba + 1 : 0); 3220 3221 if (retval != 0) 3222 break; 3223 3224 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3225 lun->be_lun->blocksize); 3226 3227 if (retval != 0) 3228 break; 3229 3230 retval = sbuf_printf(sb, "\t<serial_number>"); 3231 3232 if (retval != 0) 3233 break; 3234 3235 retval = ctl_sbuf_printf_esc(sb, 3236 lun->be_lun->serial_num, 3237 sizeof(lun->be_lun->serial_num)); 3238 3239 if (retval != 0) 3240 break; 3241 3242 retval = sbuf_printf(sb, "</serial_number>\n"); 3243 3244 if (retval != 0) 3245 break; 3246 3247 retval = sbuf_printf(sb, "\t<device_id>"); 3248 3249 if (retval != 0) 3250 break; 3251 3252 retval = ctl_sbuf_printf_esc(sb, 3253 lun->be_lun->device_id, 3254 sizeof(lun->be_lun->device_id)); 3255 3256 if (retval != 0) 3257 break; 3258 3259 retval = sbuf_printf(sb, "</device_id>\n"); 3260 3261 if (retval != 0) 3262 break; 3263 3264 if (lun->backend->lun_info != NULL) { 3265 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3266 if (retval != 0) 3267 break; 3268 } 3269 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3270 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3271 opt->name, opt->value, opt->name); 3272 if (retval != 0) 3273 break; 3274 } 3275 3276 retval = sbuf_printf(sb, "</lun>\n"); 3277 3278 if (retval != 0) 3279 break; 3280 mtx_unlock(&lun->lun_lock); 3281 } 3282 if (lun != NULL) 3283 mtx_unlock(&lun->lun_lock); 3284 mtx_unlock(&softc->ctl_lock); 3285 3286 if ((retval != 0) 3287 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3288 retval = 0; 3289 sbuf_delete(sb); 3290 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3291 snprintf(list->error_str, sizeof(list->error_str), 3292 "Out of space, %d bytes is too small", 3293 list->alloc_len); 3294 break; 3295 } 3296 3297 sbuf_finish(sb); 3298 3299 retval = copyout(sbuf_data(sb), list->lun_xml, 3300 sbuf_len(sb) + 1); 3301 3302 list->fill_len = sbuf_len(sb) + 1; 3303 list->status = CTL_LUN_LIST_OK; 3304 sbuf_delete(sb); 3305 break; 3306 } 3307 case CTL_ISCSI: { 3308 struct ctl_iscsi *ci; 3309 struct ctl_frontend *fe; 3310 3311 ci = (struct ctl_iscsi *)addr; 3312 3313 fe = ctl_frontend_find("iscsi"); 3314 if (fe == NULL) { 3315 ci->status = CTL_ISCSI_ERROR; 3316 snprintf(ci->error_str, sizeof(ci->error_str), 3317 "Frontend \"iscsi\" not found."); 3318 break; 3319 } 3320 3321 retval = fe->ioctl(dev, cmd, addr, flag, td); 3322 break; 3323 } 3324 case CTL_PORT_REQ: { 3325 struct ctl_req *req; 3326 struct ctl_frontend *fe; 3327 3328 req = (struct ctl_req *)addr; 3329 3330 fe = ctl_frontend_find(req->driver); 3331 if (fe == NULL) { 3332 req->status = CTL_LUN_ERROR; 3333 snprintf(req->error_str, sizeof(req->error_str), 3334 "Frontend \"%s\" not found.", req->driver); 3335 break; 3336 } 3337 if (req->num_args > 0) { 3338 req->kern_args = ctl_copyin_args(req->num_args, 3339 req->args, req->error_str, sizeof(req->error_str)); 3340 if (req->kern_args == NULL) { 3341 req->status = CTL_LUN_ERROR; 3342 break; 3343 } 3344 } 3345 3346 retval = fe->ioctl(dev, cmd, addr, flag, td); 3347 3348 if (req->num_args > 0) { 3349 ctl_copyout_args(req->num_args, req->kern_args); 3350 ctl_free_args(req->num_args, req->kern_args); 3351 } 3352 break; 3353 } 3354 case CTL_PORT_LIST: { 3355 struct sbuf *sb; 3356 struct ctl_port *port; 3357 struct ctl_lun_list *list; 3358 struct ctl_option *opt; 3359 int j; 3360 3361 list = (struct ctl_lun_list *)addr; 3362 3363 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3364 if (sb == NULL) { 3365 list->status = CTL_LUN_LIST_ERROR; 3366 snprintf(list->error_str, sizeof(list->error_str), 3367 "Unable to allocate %d bytes for LUN list", 3368 list->alloc_len); 3369 break; 3370 } 3371 3372 sbuf_printf(sb, "<ctlportlist>\n"); 3373 3374 mtx_lock(&softc->ctl_lock); 3375 STAILQ_FOREACH(port, &softc->port_list, links) { 3376 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3377 (uintmax_t)port->targ_port); 3378 3379 /* 3380 * Bail out as soon as we see that we've overfilled 3381 * the buffer. 3382 */ 3383 if (retval != 0) 3384 break; 3385 3386 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3387 "</frontend_type>\n", port->frontend->name); 3388 if (retval != 0) 3389 break; 3390 3391 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3392 port->port_type); 3393 if (retval != 0) 3394 break; 3395 3396 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3397 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3398 if (retval != 0) 3399 break; 3400 3401 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3402 port->port_name); 3403 if (retval != 0) 3404 break; 3405 3406 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3407 port->physical_port); 3408 if (retval != 0) 3409 break; 3410 3411 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3412 port->virtual_port); 3413 if (retval != 0) 3414 break; 3415 3416 if (port->target_devid != NULL) { 3417 sbuf_printf(sb, "\t<target>"); 3418 ctl_id_sbuf(port->target_devid, sb); 3419 sbuf_printf(sb, "</target>\n"); 3420 } 3421 3422 if (port->port_devid != NULL) { 3423 sbuf_printf(sb, "\t<port>"); 3424 ctl_id_sbuf(port->port_devid, sb); 3425 sbuf_printf(sb, "</port>\n"); 3426 } 3427 3428 if (port->port_info != NULL) { 3429 retval = port->port_info(port->onoff_arg, sb); 3430 if (retval != 0) 3431 break; 3432 } 3433 STAILQ_FOREACH(opt, &port->options, links) { 3434 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3435 opt->name, opt->value, opt->name); 3436 if (retval != 0) 3437 break; 3438 } 3439 3440 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3441 if (port->wwpn_iid[j].in_use == 0 || 3442 (port->wwpn_iid[j].wwpn == 0 && 3443 port->wwpn_iid[j].name == NULL)) 3444 continue; 3445 3446 if (port->wwpn_iid[j].name != NULL) 3447 retval = sbuf_printf(sb, 3448 "\t<initiator>%u %s</initiator>\n", 3449 j, port->wwpn_iid[j].name); 3450 else 3451 retval = sbuf_printf(sb, 3452 "\t<initiator>%u naa.%08jx</initiator>\n", 3453 j, port->wwpn_iid[j].wwpn); 3454 if (retval != 0) 3455 break; 3456 } 3457 if (retval != 0) 3458 break; 3459 3460 retval = sbuf_printf(sb, "</targ_port>\n"); 3461 if (retval != 0) 3462 break; 3463 } 3464 mtx_unlock(&softc->ctl_lock); 3465 3466 if ((retval != 0) 3467 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3468 retval = 0; 3469 sbuf_delete(sb); 3470 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3471 snprintf(list->error_str, sizeof(list->error_str), 3472 "Out of space, %d bytes is too small", 3473 list->alloc_len); 3474 break; 3475 } 3476 3477 sbuf_finish(sb); 3478 3479 retval = copyout(sbuf_data(sb), list->lun_xml, 3480 sbuf_len(sb) + 1); 3481 3482 list->fill_len = sbuf_len(sb) + 1; 3483 list->status = CTL_LUN_LIST_OK; 3484 sbuf_delete(sb); 3485 break; 3486 } 3487 default: { 3488 /* XXX KDM should we fix this? */ 3489#if 0 3490 struct ctl_backend_driver *backend; 3491 unsigned int type; 3492 int found; 3493 3494 found = 0; 3495 3496 /* 3497 * We encode the backend type as the ioctl type for backend 3498 * ioctls. So parse it out here, and then search for a 3499 * backend of this type. 3500 */ 3501 type = _IOC_TYPE(cmd); 3502 3503 STAILQ_FOREACH(backend, &softc->be_list, links) { 3504 if (backend->type == type) { 3505 found = 1; 3506 break; 3507 } 3508 } 3509 if (found == 0) { 3510 printf("ctl: unknown ioctl command %#lx or backend " 3511 "%d\n", cmd, type); 3512 retval = EINVAL; 3513 break; 3514 } 3515 retval = backend->ioctl(dev, cmd, addr, flag, td); 3516#endif 3517 retval = ENOTTY; 3518 break; 3519 } 3520 } 3521 return (retval); 3522} 3523 3524uint32_t 3525ctl_get_initindex(struct ctl_nexus *nexus) 3526{ 3527 if (nexus->targ_port < CTL_MAX_PORTS) 3528 return (nexus->initid.id + 3529 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3530 else 3531 return (nexus->initid.id + 3532 ((nexus->targ_port - CTL_MAX_PORTS) * 3533 CTL_MAX_INIT_PER_PORT)); 3534} 3535 3536uint32_t 3537ctl_get_resindex(struct ctl_nexus *nexus) 3538{ 3539 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3540} 3541 3542uint32_t 3543ctl_port_idx(int port_num) 3544{ 3545 if (port_num < CTL_MAX_PORTS) 3546 return(port_num); 3547 else 3548 return(port_num - CTL_MAX_PORTS); 3549} 3550 3551static uint32_t 3552ctl_map_lun(int port_num, uint32_t lun_id) 3553{ 3554 struct ctl_port *port; 3555 3556 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3557 if (port == NULL) 3558 return (UINT32_MAX); 3559 if (port->lun_map == NULL) 3560 return (lun_id); 3561 return (port->lun_map(port->targ_lun_arg, lun_id)); 3562} 3563 3564static uint32_t 3565ctl_map_lun_back(int port_num, uint32_t lun_id) 3566{ 3567 struct ctl_port *port; 3568 uint32_t i; 3569 3570 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3571 if (port->lun_map == NULL) 3572 return (lun_id); 3573 for (i = 0; i < CTL_MAX_LUNS; i++) { 3574 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3575 return (i); 3576 } 3577 return (UINT32_MAX); 3578} 3579 3580/* 3581 * Note: This only works for bitmask sizes that are at least 32 bits, and 3582 * that are a power of 2. 3583 */ 3584int 3585ctl_ffz(uint32_t *mask, uint32_t size) 3586{ 3587 uint32_t num_chunks, num_pieces; 3588 int i, j; 3589 3590 num_chunks = (size >> 5); 3591 if (num_chunks == 0) 3592 num_chunks++; 3593 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3594 3595 for (i = 0; i < num_chunks; i++) { 3596 for (j = 0; j < num_pieces; j++) { 3597 if ((mask[i] & (1 << j)) == 0) 3598 return ((i << 5) + j); 3599 } 3600 } 3601 3602 return (-1); 3603} 3604 3605int 3606ctl_set_mask(uint32_t *mask, uint32_t bit) 3607{ 3608 uint32_t chunk, piece; 3609 3610 chunk = bit >> 5; 3611 piece = bit % (sizeof(uint32_t) * 8); 3612 3613 if ((mask[chunk] & (1 << piece)) != 0) 3614 return (-1); 3615 else 3616 mask[chunk] |= (1 << piece); 3617 3618 return (0); 3619} 3620 3621int 3622ctl_clear_mask(uint32_t *mask, uint32_t bit) 3623{ 3624 uint32_t chunk, piece; 3625 3626 chunk = bit >> 5; 3627 piece = bit % (sizeof(uint32_t) * 8); 3628 3629 if ((mask[chunk] & (1 << piece)) == 0) 3630 return (-1); 3631 else 3632 mask[chunk] &= ~(1 << piece); 3633 3634 return (0); 3635} 3636 3637int 3638ctl_is_set(uint32_t *mask, uint32_t bit) 3639{ 3640 uint32_t chunk, piece; 3641 3642 chunk = bit >> 5; 3643 piece = bit % (sizeof(uint32_t) * 8); 3644 3645 if ((mask[chunk] & (1 << piece)) == 0) 3646 return (0); 3647 else 3648 return (1); 3649} 3650 3651#ifdef unused 3652/* 3653 * The bus, target and lun are optional, they can be filled in later. 3654 * can_wait is used to determine whether we can wait on the malloc or not. 3655 */ 3656union ctl_io* 3657ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3658 uint32_t targ_lun, int can_wait) 3659{ 3660 union ctl_io *io; 3661 3662 if (can_wait) 3663 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3664 else 3665 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3666 3667 if (io != NULL) { 3668 io->io_hdr.io_type = io_type; 3669 io->io_hdr.targ_port = targ_port; 3670 /* 3671 * XXX KDM this needs to change/go away. We need to move 3672 * to a preallocated pool of ctl_scsiio structures. 3673 */ 3674 io->io_hdr.nexus.targ_target.id = targ_target; 3675 io->io_hdr.nexus.targ_lun = targ_lun; 3676 } 3677 3678 return (io); 3679} 3680 3681void 3682ctl_kfree_io(union ctl_io *io) 3683{ 3684 free(io, M_CTL); 3685} 3686#endif /* unused */ 3687 3688/* 3689 * ctl_softc, pool_name, total_ctl_io are passed in. 3690 * npool is passed out. 3691 */ 3692int 3693ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3694 uint32_t total_ctl_io, void **npool) 3695{ 3696#ifdef IO_POOLS 3697 struct ctl_io_pool *pool; 3698 3699 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3700 M_NOWAIT | M_ZERO); 3701 if (pool == NULL) 3702 return (ENOMEM); 3703 3704 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3705 pool->ctl_softc = ctl_softc; 3706 pool->zone = uma_zsecond_create(pool->name, NULL, 3707 NULL, NULL, NULL, ctl_softc->io_zone); 3708 /* uma_prealloc(pool->zone, total_ctl_io); */ 3709 3710 *npool = pool; 3711#else 3712 *npool = ctl_softc->io_zone; 3713#endif 3714 return (0); 3715} 3716 3717void 3718ctl_pool_free(struct ctl_io_pool *pool) 3719{ 3720 3721 if (pool == NULL) 3722 return; 3723 3724#ifdef IO_POOLS 3725 uma_zdestroy(pool->zone); 3726 free(pool, M_CTL); 3727#endif 3728} 3729 3730union ctl_io * 3731ctl_alloc_io(void *pool_ref) 3732{ 3733 union ctl_io *io; 3734#ifdef IO_POOLS 3735 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3736 3737 io = uma_zalloc(pool->zone, M_WAITOK); 3738#else 3739 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3740#endif 3741 if (io != NULL) 3742 io->io_hdr.pool = pool_ref; 3743 return (io); 3744} 3745 3746union ctl_io * 3747ctl_alloc_io_nowait(void *pool_ref) 3748{ 3749 union ctl_io *io; 3750#ifdef IO_POOLS 3751 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3752 3753 io = uma_zalloc(pool->zone, M_NOWAIT); 3754#else 3755 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3756#endif 3757 if (io != NULL) 3758 io->io_hdr.pool = pool_ref; 3759 return (io); 3760} 3761 3762void 3763ctl_free_io(union ctl_io *io) 3764{ 3765#ifdef IO_POOLS 3766 struct ctl_io_pool *pool; 3767#endif 3768 3769 if (io == NULL) 3770 return; 3771 3772#ifdef IO_POOLS 3773 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3774 uma_zfree(pool->zone, io); 3775#else 3776 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3777#endif 3778} 3779 3780void 3781ctl_zero_io(union ctl_io *io) 3782{ 3783 void *pool_ref; 3784 3785 if (io == NULL) 3786 return; 3787 3788 /* 3789 * May need to preserve linked list pointers at some point too. 3790 */ 3791 pool_ref = io->io_hdr.pool; 3792 memset(io, 0, sizeof(*io)); 3793 io->io_hdr.pool = pool_ref; 3794} 3795 3796/* 3797 * This routine is currently used for internal copies of ctl_ios that need 3798 * to persist for some reason after we've already returned status to the 3799 * FETD. (Thus the flag set.) 3800 * 3801 * XXX XXX 3802 * Note that this makes a blind copy of all fields in the ctl_io, except 3803 * for the pool reference. This includes any memory that has been 3804 * allocated! That memory will no longer be valid after done has been 3805 * called, so this would be VERY DANGEROUS for command that actually does 3806 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3807 * start and stop commands, which don't transfer any data, so this is not a 3808 * problem. If it is used for anything else, the caller would also need to 3809 * allocate data buffer space and this routine would need to be modified to 3810 * copy the data buffer(s) as well. 3811 */ 3812void 3813ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3814{ 3815 void *pool_ref; 3816 3817 if ((src == NULL) 3818 || (dest == NULL)) 3819 return; 3820 3821 /* 3822 * May need to preserve linked list pointers at some point too. 3823 */ 3824 pool_ref = dest->io_hdr.pool; 3825 3826 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3827 3828 dest->io_hdr.pool = pool_ref; 3829 /* 3830 * We need to know that this is an internal copy, and doesn't need 3831 * to get passed back to the FETD that allocated it. 3832 */ 3833 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3834} 3835 3836static int 3837ctl_expand_number(const char *buf, uint64_t *num) 3838{ 3839 char *endptr; 3840 uint64_t number; 3841 unsigned shift; 3842 3843 number = strtoq(buf, &endptr, 0); 3844 3845 switch (tolower((unsigned char)*endptr)) { 3846 case 'e': 3847 shift = 60; 3848 break; 3849 case 'p': 3850 shift = 50; 3851 break; 3852 case 't': 3853 shift = 40; 3854 break; 3855 case 'g': 3856 shift = 30; 3857 break; 3858 case 'm': 3859 shift = 20; 3860 break; 3861 case 'k': 3862 shift = 10; 3863 break; 3864 case 'b': 3865 case '\0': /* No unit. */ 3866 *num = number; 3867 return (0); 3868 default: 3869 /* Unrecognized unit. */ 3870 return (-1); 3871 } 3872 3873 if ((number << shift) >> shift != number) { 3874 /* Overflow */ 3875 return (-1); 3876 } 3877 *num = number << shift; 3878 return (0); 3879} 3880 3881 3882/* 3883 * This routine could be used in the future to load default and/or saved 3884 * mode page parameters for a particuar lun. 3885 */ 3886static int 3887ctl_init_page_index(struct ctl_lun *lun) 3888{ 3889 int i; 3890 struct ctl_page_index *page_index; 3891 const char *value; 3892 uint64_t ival; 3893 3894 memcpy(&lun->mode_pages.index, page_index_template, 3895 sizeof(page_index_template)); 3896 3897 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3898 3899 page_index = &lun->mode_pages.index[i]; 3900 /* 3901 * If this is a disk-only mode page, there's no point in 3902 * setting it up. For some pages, we have to have some 3903 * basic information about the disk in order to calculate the 3904 * mode page data. 3905 */ 3906 if ((lun->be_lun->lun_type != T_DIRECT) 3907 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3908 continue; 3909 3910 switch (page_index->page_code & SMPH_PC_MASK) { 3911 case SMS_RW_ERROR_RECOVERY_PAGE: { 3912 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3913 panic("subpage is incorrect!"); 3914 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3915 &rw_er_page_default, 3916 sizeof(rw_er_page_default)); 3917 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3918 &rw_er_page_changeable, 3919 sizeof(rw_er_page_changeable)); 3920 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3921 &rw_er_page_default, 3922 sizeof(rw_er_page_default)); 3923 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3924 &rw_er_page_default, 3925 sizeof(rw_er_page_default)); 3926 page_index->page_data = 3927 (uint8_t *)lun->mode_pages.rw_er_page; 3928 break; 3929 } 3930 case SMS_FORMAT_DEVICE_PAGE: { 3931 struct scsi_format_page *format_page; 3932 3933 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3934 panic("subpage is incorrect!"); 3935 3936 /* 3937 * Sectors per track are set above. Bytes per 3938 * sector need to be set here on a per-LUN basis. 3939 */ 3940 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3941 &format_page_default, 3942 sizeof(format_page_default)); 3943 memcpy(&lun->mode_pages.format_page[ 3944 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3945 sizeof(format_page_changeable)); 3946 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3947 &format_page_default, 3948 sizeof(format_page_default)); 3949 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3950 &format_page_default, 3951 sizeof(format_page_default)); 3952 3953 format_page = &lun->mode_pages.format_page[ 3954 CTL_PAGE_CURRENT]; 3955 scsi_ulto2b(lun->be_lun->blocksize, 3956 format_page->bytes_per_sector); 3957 3958 format_page = &lun->mode_pages.format_page[ 3959 CTL_PAGE_DEFAULT]; 3960 scsi_ulto2b(lun->be_lun->blocksize, 3961 format_page->bytes_per_sector); 3962 3963 format_page = &lun->mode_pages.format_page[ 3964 CTL_PAGE_SAVED]; 3965 scsi_ulto2b(lun->be_lun->blocksize, 3966 format_page->bytes_per_sector); 3967 3968 page_index->page_data = 3969 (uint8_t *)lun->mode_pages.format_page; 3970 break; 3971 } 3972 case SMS_RIGID_DISK_PAGE: { 3973 struct scsi_rigid_disk_page *rigid_disk_page; 3974 uint32_t sectors_per_cylinder; 3975 uint64_t cylinders; 3976#ifndef __XSCALE__ 3977 int shift; 3978#endif /* !__XSCALE__ */ 3979 3980 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3981 panic("invalid subpage value %d", 3982 page_index->subpage); 3983 3984 /* 3985 * Rotation rate and sectors per track are set 3986 * above. We calculate the cylinders here based on 3987 * capacity. Due to the number of heads and 3988 * sectors per track we're using, smaller arrays 3989 * may turn out to have 0 cylinders. Linux and 3990 * FreeBSD don't pay attention to these mode pages 3991 * to figure out capacity, but Solaris does. It 3992 * seems to deal with 0 cylinders just fine, and 3993 * works out a fake geometry based on the capacity. 3994 */ 3995 memcpy(&lun->mode_pages.rigid_disk_page[ 3996 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3997 sizeof(rigid_disk_page_default)); 3998 memcpy(&lun->mode_pages.rigid_disk_page[ 3999 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 4000 sizeof(rigid_disk_page_changeable)); 4001 4002 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4003 CTL_DEFAULT_HEADS; 4004 4005 /* 4006 * The divide method here will be more accurate, 4007 * probably, but results in floating point being 4008 * used in the kernel on i386 (__udivdi3()). On the 4009 * XScale, though, __udivdi3() is implemented in 4010 * software. 4011 * 4012 * The shift method for cylinder calculation is 4013 * accurate if sectors_per_cylinder is a power of 4014 * 2. Otherwise it might be slightly off -- you 4015 * might have a bit of a truncation problem. 4016 */ 4017#ifdef __XSCALE__ 4018 cylinders = (lun->be_lun->maxlba + 1) / 4019 sectors_per_cylinder; 4020#else 4021 for (shift = 31; shift > 0; shift--) { 4022 if (sectors_per_cylinder & (1 << shift)) 4023 break; 4024 } 4025 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4026#endif 4027 4028 /* 4029 * We've basically got 3 bytes, or 24 bits for the 4030 * cylinder size in the mode page. If we're over, 4031 * just round down to 2^24. 4032 */ 4033 if (cylinders > 0xffffff) 4034 cylinders = 0xffffff; 4035 4036 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4037 CTL_PAGE_DEFAULT]; 4038 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4039 4040 if ((value = ctl_get_opt(&lun->be_lun->options, 4041 "rpm")) != NULL) { 4042 scsi_ulto2b(strtol(value, NULL, 0), 4043 rigid_disk_page->rotation_rate); 4044 } 4045 4046 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4047 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4048 sizeof(rigid_disk_page_default)); 4049 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4050 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4051 sizeof(rigid_disk_page_default)); 4052 4053 page_index->page_data = 4054 (uint8_t *)lun->mode_pages.rigid_disk_page; 4055 break; 4056 } 4057 case SMS_CACHING_PAGE: { 4058 struct scsi_caching_page *caching_page; 4059 4060 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4061 panic("invalid subpage value %d", 4062 page_index->subpage); 4063 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4064 &caching_page_default, 4065 sizeof(caching_page_default)); 4066 memcpy(&lun->mode_pages.caching_page[ 4067 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4068 sizeof(caching_page_changeable)); 4069 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4070 &caching_page_default, 4071 sizeof(caching_page_default)); 4072 caching_page = &lun->mode_pages.caching_page[ 4073 CTL_PAGE_SAVED]; 4074 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4075 if (value != NULL && strcmp(value, "off") == 0) 4076 caching_page->flags1 &= ~SCP_WCE; 4077 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4078 if (value != NULL && strcmp(value, "off") == 0) 4079 caching_page->flags1 |= SCP_RCD; 4080 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4081 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4082 sizeof(caching_page_default)); 4083 page_index->page_data = 4084 (uint8_t *)lun->mode_pages.caching_page; 4085 break; 4086 } 4087 case SMS_CONTROL_MODE_PAGE: { 4088 struct scsi_control_page *control_page; 4089 4090 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4091 panic("invalid subpage value %d", 4092 page_index->subpage); 4093 4094 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4095 &control_page_default, 4096 sizeof(control_page_default)); 4097 memcpy(&lun->mode_pages.control_page[ 4098 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4099 sizeof(control_page_changeable)); 4100 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4101 &control_page_default, 4102 sizeof(control_page_default)); 4103 control_page = &lun->mode_pages.control_page[ 4104 CTL_PAGE_SAVED]; 4105 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4106 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4107 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4108 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4109 } 4110 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4111 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4112 sizeof(control_page_default)); 4113 page_index->page_data = 4114 (uint8_t *)lun->mode_pages.control_page; 4115 break; 4116 4117 } 4118 case SMS_INFO_EXCEPTIONS_PAGE: { 4119 switch (page_index->subpage) { 4120 case SMS_SUBPAGE_PAGE_0: 4121 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4122 &ie_page_default, 4123 sizeof(ie_page_default)); 4124 memcpy(&lun->mode_pages.ie_page[ 4125 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4126 sizeof(ie_page_changeable)); 4127 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4128 &ie_page_default, 4129 sizeof(ie_page_default)); 4130 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4131 &ie_page_default, 4132 sizeof(ie_page_default)); 4133 page_index->page_data = 4134 (uint8_t *)lun->mode_pages.ie_page; 4135 break; 4136 case 0x02: { 4137 struct ctl_logical_block_provisioning_page *page; 4138 4139 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4140 &lbp_page_default, 4141 sizeof(lbp_page_default)); 4142 memcpy(&lun->mode_pages.lbp_page[ 4143 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4144 sizeof(lbp_page_changeable)); 4145 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4146 &lbp_page_default, 4147 sizeof(lbp_page_default)); 4148 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4149 value = ctl_get_opt(&lun->be_lun->options, 4150 "avail-threshold"); 4151 if (value != NULL && 4152 ctl_expand_number(value, &ival) == 0) { 4153 page->descr[0].flags |= SLBPPD_ENABLED | 4154 SLBPPD_ARMING_DEC; 4155 if (lun->be_lun->blocksize) 4156 ival /= lun->be_lun->blocksize; 4157 else 4158 ival /= 512; 4159 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4160 page->descr[0].count); 4161 } 4162 value = ctl_get_opt(&lun->be_lun->options, 4163 "used-threshold"); 4164 if (value != NULL && 4165 ctl_expand_number(value, &ival) == 0) { 4166 page->descr[1].flags |= SLBPPD_ENABLED | 4167 SLBPPD_ARMING_INC; 4168 if (lun->be_lun->blocksize) 4169 ival /= lun->be_lun->blocksize; 4170 else 4171 ival /= 512; 4172 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4173 page->descr[1].count); 4174 } 4175 value = ctl_get_opt(&lun->be_lun->options, 4176 "pool-avail-threshold"); 4177 if (value != NULL && 4178 ctl_expand_number(value, &ival) == 0) { 4179 page->descr[2].flags |= SLBPPD_ENABLED | 4180 SLBPPD_ARMING_DEC; 4181 if (lun->be_lun->blocksize) 4182 ival /= lun->be_lun->blocksize; 4183 else 4184 ival /= 512; 4185 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4186 page->descr[2].count); 4187 } 4188 value = ctl_get_opt(&lun->be_lun->options, 4189 "pool-used-threshold"); 4190 if (value != NULL && 4191 ctl_expand_number(value, &ival) == 0) { 4192 page->descr[3].flags |= SLBPPD_ENABLED | 4193 SLBPPD_ARMING_INC; 4194 if (lun->be_lun->blocksize) 4195 ival /= lun->be_lun->blocksize; 4196 else 4197 ival /= 512; 4198 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4199 page->descr[3].count); 4200 } 4201 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4202 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4203 sizeof(lbp_page_default)); 4204 page_index->page_data = 4205 (uint8_t *)lun->mode_pages.lbp_page; 4206 }} 4207 break; 4208 } 4209 case SMS_VENDOR_SPECIFIC_PAGE:{ 4210 switch (page_index->subpage) { 4211 case DBGCNF_SUBPAGE_CODE: { 4212 struct copan_debugconf_subpage *current_page, 4213 *saved_page; 4214 4215 memcpy(&lun->mode_pages.debugconf_subpage[ 4216 CTL_PAGE_CURRENT], 4217 &debugconf_page_default, 4218 sizeof(debugconf_page_default)); 4219 memcpy(&lun->mode_pages.debugconf_subpage[ 4220 CTL_PAGE_CHANGEABLE], 4221 &debugconf_page_changeable, 4222 sizeof(debugconf_page_changeable)); 4223 memcpy(&lun->mode_pages.debugconf_subpage[ 4224 CTL_PAGE_DEFAULT], 4225 &debugconf_page_default, 4226 sizeof(debugconf_page_default)); 4227 memcpy(&lun->mode_pages.debugconf_subpage[ 4228 CTL_PAGE_SAVED], 4229 &debugconf_page_default, 4230 sizeof(debugconf_page_default)); 4231 page_index->page_data = 4232 (uint8_t *)lun->mode_pages.debugconf_subpage; 4233 4234 current_page = (struct copan_debugconf_subpage *) 4235 (page_index->page_data + 4236 (page_index->page_len * 4237 CTL_PAGE_CURRENT)); 4238 saved_page = (struct copan_debugconf_subpage *) 4239 (page_index->page_data + 4240 (page_index->page_len * 4241 CTL_PAGE_SAVED)); 4242 break; 4243 } 4244 default: 4245 panic("invalid subpage value %d", 4246 page_index->subpage); 4247 break; 4248 } 4249 break; 4250 } 4251 default: 4252 panic("invalid page value %d", 4253 page_index->page_code & SMPH_PC_MASK); 4254 break; 4255 } 4256 } 4257 4258 return (CTL_RETVAL_COMPLETE); 4259} 4260 4261static int 4262ctl_init_log_page_index(struct ctl_lun *lun) 4263{ 4264 struct ctl_page_index *page_index; 4265 int i, j, k, prev; 4266 4267 memcpy(&lun->log_pages.index, log_page_index_template, 4268 sizeof(log_page_index_template)); 4269 4270 prev = -1; 4271 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4272 4273 page_index = &lun->log_pages.index[i]; 4274 /* 4275 * If this is a disk-only mode page, there's no point in 4276 * setting it up. For some pages, we have to have some 4277 * basic information about the disk in order to calculate the 4278 * mode page data. 4279 */ 4280 if ((lun->be_lun->lun_type != T_DIRECT) 4281 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4282 continue; 4283 4284 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4285 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 4286 lun->backend->lun_attr == NULL)) 4287 continue; 4288 4289 if (page_index->page_code != prev) { 4290 lun->log_pages.pages_page[j] = page_index->page_code; 4291 prev = page_index->page_code; 4292 j++; 4293 } 4294 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4295 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4296 k++; 4297 } 4298 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4299 lun->log_pages.index[0].page_len = j; 4300 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4301 lun->log_pages.index[1].page_len = k * 2; 4302 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4303 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4304 4305 return (CTL_RETVAL_COMPLETE); 4306} 4307 4308static int 4309hex2bin(const char *str, uint8_t *buf, int buf_size) 4310{ 4311 int i; 4312 u_char c; 4313 4314 memset(buf, 0, buf_size); 4315 while (isspace(str[0])) 4316 str++; 4317 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4318 str += 2; 4319 buf_size *= 2; 4320 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4321 c = str[i]; 4322 if (isdigit(c)) 4323 c -= '0'; 4324 else if (isalpha(c)) 4325 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4326 else 4327 break; 4328 if (c >= 16) 4329 break; 4330 if ((i & 1) == 0) 4331 buf[i / 2] |= (c << 4); 4332 else 4333 buf[i / 2] |= c; 4334 } 4335 return ((i + 1) / 2); 4336} 4337 4338/* 4339 * LUN allocation. 4340 * 4341 * Requirements: 4342 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4343 * wants us to allocate the LUN and he can block. 4344 * - ctl_softc is always set 4345 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4346 * 4347 * Returns 0 for success, non-zero (errno) for failure. 4348 */ 4349static int 4350ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4351 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4352{ 4353 struct ctl_lun *nlun, *lun; 4354 struct ctl_port *port; 4355 struct scsi_vpd_id_descriptor *desc; 4356 struct scsi_vpd_id_t10 *t10id; 4357 const char *eui, *naa, *scsiname, *vendor, *value; 4358 int lun_number, i, lun_malloced; 4359 int devidlen, idlen1, idlen2 = 0, len; 4360 4361 if (be_lun == NULL) 4362 return (EINVAL); 4363 4364 /* 4365 * We currently only support Direct Access or Processor LUN types. 4366 */ 4367 switch (be_lun->lun_type) { 4368 case T_DIRECT: 4369 break; 4370 case T_PROCESSOR: 4371 break; 4372 case T_SEQUENTIAL: 4373 case T_CHANGER: 4374 default: 4375 be_lun->lun_config_status(be_lun->be_lun, 4376 CTL_LUN_CONFIG_FAILURE); 4377 break; 4378 } 4379 if (ctl_lun == NULL) { 4380 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4381 lun_malloced = 1; 4382 } else { 4383 lun_malloced = 0; 4384 lun = ctl_lun; 4385 } 4386 4387 memset(lun, 0, sizeof(*lun)); 4388 if (lun_malloced) 4389 lun->flags = CTL_LUN_MALLOCED; 4390 4391 /* Generate LUN ID. */ 4392 devidlen = max(CTL_DEVID_MIN_LEN, 4393 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4394 idlen1 = sizeof(*t10id) + devidlen; 4395 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4396 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4397 if (scsiname != NULL) { 4398 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4399 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4400 } 4401 eui = ctl_get_opt(&be_lun->options, "eui"); 4402 if (eui != NULL) { 4403 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4404 } 4405 naa = ctl_get_opt(&be_lun->options, "naa"); 4406 if (naa != NULL) { 4407 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4408 } 4409 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4410 M_CTL, M_WAITOK | M_ZERO); 4411 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4412 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4413 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4414 desc->length = idlen1; 4415 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4416 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4417 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4418 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4419 } else { 4420 strncpy(t10id->vendor, vendor, 4421 min(sizeof(t10id->vendor), strlen(vendor))); 4422 } 4423 strncpy((char *)t10id->vendor_spec_id, 4424 (char *)be_lun->device_id, devidlen); 4425 if (scsiname != NULL) { 4426 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4427 desc->length); 4428 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4429 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4430 SVPD_ID_TYPE_SCSI_NAME; 4431 desc->length = idlen2; 4432 strlcpy(desc->identifier, scsiname, idlen2); 4433 } 4434 if (eui != NULL) { 4435 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4436 desc->length); 4437 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4438 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4439 SVPD_ID_TYPE_EUI64; 4440 desc->length = hex2bin(eui, desc->identifier, 16); 4441 desc->length = desc->length > 12 ? 16 : 4442 (desc->length > 8 ? 12 : 8); 4443 len -= 16 - desc->length; 4444 } 4445 if (naa != NULL) { 4446 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4447 desc->length); 4448 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4449 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4450 SVPD_ID_TYPE_NAA; 4451 desc->length = hex2bin(naa, desc->identifier, 16); 4452 desc->length = desc->length > 8 ? 16 : 8; 4453 len -= 16 - desc->length; 4454 } 4455 lun->lun_devid->len = len; 4456 4457 mtx_lock(&ctl_softc->ctl_lock); 4458 /* 4459 * See if the caller requested a particular LUN number. If so, see 4460 * if it is available. Otherwise, allocate the first available LUN. 4461 */ 4462 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4463 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4464 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4465 mtx_unlock(&ctl_softc->ctl_lock); 4466 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4467 printf("ctl: requested LUN ID %d is higher " 4468 "than CTL_MAX_LUNS - 1 (%d)\n", 4469 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4470 } else { 4471 /* 4472 * XXX KDM return an error, or just assign 4473 * another LUN ID in this case?? 4474 */ 4475 printf("ctl: requested LUN ID %d is already " 4476 "in use\n", be_lun->req_lun_id); 4477 } 4478 if (lun->flags & CTL_LUN_MALLOCED) 4479 free(lun, M_CTL); 4480 be_lun->lun_config_status(be_lun->be_lun, 4481 CTL_LUN_CONFIG_FAILURE); 4482 return (ENOSPC); 4483 } 4484 lun_number = be_lun->req_lun_id; 4485 } else { 4486 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4487 if (lun_number == -1) { 4488 mtx_unlock(&ctl_softc->ctl_lock); 4489 printf("ctl: can't allocate LUN on target %ju, out of " 4490 "LUNs\n", (uintmax_t)target_id.id); 4491 if (lun->flags & CTL_LUN_MALLOCED) 4492 free(lun, M_CTL); 4493 be_lun->lun_config_status(be_lun->be_lun, 4494 CTL_LUN_CONFIG_FAILURE); 4495 return (ENOSPC); 4496 } 4497 } 4498 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4499 4500 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4501 lun->target = target_id; 4502 lun->lun = lun_number; 4503 lun->be_lun = be_lun; 4504 /* 4505 * The processor LUN is always enabled. Disk LUNs come on line 4506 * disabled, and must be enabled by the backend. 4507 */ 4508 lun->flags |= CTL_LUN_DISABLED; 4509 lun->backend = be_lun->be; 4510 be_lun->ctl_lun = lun; 4511 be_lun->lun_id = lun_number; 4512 atomic_add_int(&be_lun->be->num_luns, 1); 4513 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4514 lun->flags |= CTL_LUN_OFFLINE; 4515 4516 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4517 lun->flags |= CTL_LUN_STOPPED; 4518 4519 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4520 lun->flags |= CTL_LUN_INOPERABLE; 4521 4522 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4523 lun->flags |= CTL_LUN_PRIMARY_SC; 4524 4525 value = ctl_get_opt(&be_lun->options, "readonly"); 4526 if (value != NULL && strcmp(value, "on") == 0) 4527 lun->flags |= CTL_LUN_READONLY; 4528 4529 lun->ctl_softc = ctl_softc; 4530 TAILQ_INIT(&lun->ooa_queue); 4531 TAILQ_INIT(&lun->blocked_queue); 4532 STAILQ_INIT(&lun->error_list); 4533 ctl_tpc_lun_init(lun); 4534 4535 /* 4536 * Initialize the mode and log page index. 4537 */ 4538 ctl_init_page_index(lun); 4539 ctl_init_log_page_index(lun); 4540 4541 /* 4542 * Set the poweron UA for all initiators on this LUN only. 4543 */ 4544 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4545 lun->pending_ua[i] = CTL_UA_POWERON; 4546 4547 /* 4548 * Now, before we insert this lun on the lun list, set the lun 4549 * inventory changed UA for all other luns. 4550 */ 4551 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4552 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4553 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4554 } 4555 } 4556 4557 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4558 4559 ctl_softc->ctl_luns[lun_number] = lun; 4560 4561 ctl_softc->num_luns++; 4562 4563 /* Setup statistics gathering */ 4564 lun->stats.device_type = be_lun->lun_type; 4565 lun->stats.lun_number = lun_number; 4566 if (lun->stats.device_type == T_DIRECT) 4567 lun->stats.blocksize = be_lun->blocksize; 4568 else 4569 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4570 for (i = 0;i < CTL_MAX_PORTS;i++) 4571 lun->stats.ports[i].targ_port = i; 4572 4573 mtx_unlock(&ctl_softc->ctl_lock); 4574 4575 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4576 4577 /* 4578 * Run through each registered FETD and bring it online if it isn't 4579 * already. Enable the target ID if it hasn't been enabled, and 4580 * enable this particular LUN. 4581 */ 4582 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4583 int retval; 4584 4585 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4586 if (retval != 0) { 4587 printf("ctl_alloc_lun: FETD %s port %d returned error " 4588 "%d for lun_enable on target %ju lun %d\n", 4589 port->port_name, port->targ_port, retval, 4590 (uintmax_t)target_id.id, lun_number); 4591 } else 4592 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4593 } 4594 return (0); 4595} 4596 4597/* 4598 * Delete a LUN. 4599 * Assumptions: 4600 * - LUN has already been marked invalid and any pending I/O has been taken 4601 * care of. 4602 */ 4603static int 4604ctl_free_lun(struct ctl_lun *lun) 4605{ 4606 struct ctl_softc *softc; 4607#if 0 4608 struct ctl_port *port; 4609#endif 4610 struct ctl_lun *nlun; 4611 int i; 4612 4613 softc = lun->ctl_softc; 4614 4615 mtx_assert(&softc->ctl_lock, MA_OWNED); 4616 4617 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4618 4619 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4620 4621 softc->ctl_luns[lun->lun] = NULL; 4622 4623 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4624 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4625 4626 softc->num_luns--; 4627 4628 /* 4629 * XXX KDM this scheme only works for a single target/multiple LUN 4630 * setup. It needs to be revamped for a multiple target scheme. 4631 * 4632 * XXX KDM this results in port->lun_disable() getting called twice, 4633 * once when ctl_disable_lun() is called, and a second time here. 4634 * We really need to re-think the LUN disable semantics. There 4635 * should probably be several steps/levels to LUN removal: 4636 * - disable 4637 * - invalidate 4638 * - free 4639 * 4640 * Right now we only have a disable method when communicating to 4641 * the front end ports, at least for individual LUNs. 4642 */ 4643#if 0 4644 STAILQ_FOREACH(port, &softc->port_list, links) { 4645 int retval; 4646 4647 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4648 lun->lun); 4649 if (retval != 0) { 4650 printf("ctl_free_lun: FETD %s port %d returned error " 4651 "%d for lun_disable on target %ju lun %jd\n", 4652 port->port_name, port->targ_port, retval, 4653 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4654 } 4655 4656 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4657 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4658 4659 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4660 if (retval != 0) { 4661 printf("ctl_free_lun: FETD %s port %d " 4662 "returned error %d for targ_disable on " 4663 "target %ju\n", port->port_name, 4664 port->targ_port, retval, 4665 (uintmax_t)lun->target.id); 4666 } else 4667 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4668 4669 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4670 continue; 4671 4672#if 0 4673 port->port_offline(port->onoff_arg); 4674 port->status &= ~CTL_PORT_STATUS_ONLINE; 4675#endif 4676 } 4677 } 4678#endif 4679 4680 /* 4681 * Tell the backend to free resources, if this LUN has a backend. 4682 */ 4683 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4684 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4685 4686 ctl_tpc_lun_shutdown(lun); 4687 mtx_destroy(&lun->lun_lock); 4688 free(lun->lun_devid, M_CTL); 4689 free(lun->write_buffer, M_CTL); 4690 if (lun->flags & CTL_LUN_MALLOCED) 4691 free(lun, M_CTL); 4692 4693 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4694 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4695 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4696 } 4697 } 4698 4699 return (0); 4700} 4701 4702static void 4703ctl_create_lun(struct ctl_be_lun *be_lun) 4704{ 4705 struct ctl_softc *ctl_softc; 4706 4707 ctl_softc = control_softc; 4708 4709 /* 4710 * ctl_alloc_lun() should handle all potential failure cases. 4711 */ 4712 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4713} 4714 4715int 4716ctl_add_lun(struct ctl_be_lun *be_lun) 4717{ 4718 struct ctl_softc *ctl_softc = control_softc; 4719 4720 mtx_lock(&ctl_softc->ctl_lock); 4721 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4722 mtx_unlock(&ctl_softc->ctl_lock); 4723 wakeup(&ctl_softc->pending_lun_queue); 4724 4725 return (0); 4726} 4727 4728int 4729ctl_enable_lun(struct ctl_be_lun *be_lun) 4730{ 4731 struct ctl_softc *ctl_softc; 4732 struct ctl_port *port, *nport; 4733 struct ctl_lun *lun; 4734 int retval; 4735 4736 ctl_softc = control_softc; 4737 4738 lun = (struct ctl_lun *)be_lun->ctl_lun; 4739 4740 mtx_lock(&ctl_softc->ctl_lock); 4741 mtx_lock(&lun->lun_lock); 4742 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4743 /* 4744 * eh? Why did we get called if the LUN is already 4745 * enabled? 4746 */ 4747 mtx_unlock(&lun->lun_lock); 4748 mtx_unlock(&ctl_softc->ctl_lock); 4749 return (0); 4750 } 4751 lun->flags &= ~CTL_LUN_DISABLED; 4752 mtx_unlock(&lun->lun_lock); 4753 4754 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4755 nport = STAILQ_NEXT(port, links); 4756 4757 /* 4758 * Drop the lock while we call the FETD's enable routine. 4759 * This can lead to a callback into CTL (at least in the 4760 * case of the internal initiator frontend. 4761 */ 4762 mtx_unlock(&ctl_softc->ctl_lock); 4763 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4764 mtx_lock(&ctl_softc->ctl_lock); 4765 if (retval != 0) { 4766 printf("%s: FETD %s port %d returned error " 4767 "%d for lun_enable on target %ju lun %jd\n", 4768 __func__, port->port_name, port->targ_port, retval, 4769 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4770 } 4771#if 0 4772 else { 4773 /* NOTE: TODO: why does lun enable affect port status? */ 4774 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4775 } 4776#endif 4777 } 4778 4779 mtx_unlock(&ctl_softc->ctl_lock); 4780 4781 return (0); 4782} 4783 4784int 4785ctl_disable_lun(struct ctl_be_lun *be_lun) 4786{ 4787 struct ctl_softc *ctl_softc; 4788 struct ctl_port *port; 4789 struct ctl_lun *lun; 4790 int retval; 4791 4792 ctl_softc = control_softc; 4793 4794 lun = (struct ctl_lun *)be_lun->ctl_lun; 4795 4796 mtx_lock(&ctl_softc->ctl_lock); 4797 mtx_lock(&lun->lun_lock); 4798 if (lun->flags & CTL_LUN_DISABLED) { 4799 mtx_unlock(&lun->lun_lock); 4800 mtx_unlock(&ctl_softc->ctl_lock); 4801 return (0); 4802 } 4803 lun->flags |= CTL_LUN_DISABLED; 4804 mtx_unlock(&lun->lun_lock); 4805 4806 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4807 mtx_unlock(&ctl_softc->ctl_lock); 4808 /* 4809 * Drop the lock before we call the frontend's disable 4810 * routine, to avoid lock order reversals. 4811 * 4812 * XXX KDM what happens if the frontend list changes while 4813 * we're traversing it? It's unlikely, but should be handled. 4814 */ 4815 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4816 lun->lun); 4817 mtx_lock(&ctl_softc->ctl_lock); 4818 if (retval != 0) { 4819 printf("ctl_alloc_lun: FETD %s port %d returned error " 4820 "%d for lun_disable on target %ju lun %jd\n", 4821 port->port_name, port->targ_port, retval, 4822 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4823 } 4824 } 4825 4826 mtx_unlock(&ctl_softc->ctl_lock); 4827 4828 return (0); 4829} 4830 4831int 4832ctl_start_lun(struct ctl_be_lun *be_lun) 4833{ 4834 struct ctl_softc *ctl_softc; 4835 struct ctl_lun *lun; 4836 4837 ctl_softc = control_softc; 4838 4839 lun = (struct ctl_lun *)be_lun->ctl_lun; 4840 4841 mtx_lock(&lun->lun_lock); 4842 lun->flags &= ~CTL_LUN_STOPPED; 4843 mtx_unlock(&lun->lun_lock); 4844 4845 return (0); 4846} 4847 4848int 4849ctl_stop_lun(struct ctl_be_lun *be_lun) 4850{ 4851 struct ctl_softc *ctl_softc; 4852 struct ctl_lun *lun; 4853 4854 ctl_softc = control_softc; 4855 4856 lun = (struct ctl_lun *)be_lun->ctl_lun; 4857 4858 mtx_lock(&lun->lun_lock); 4859 lun->flags |= CTL_LUN_STOPPED; 4860 mtx_unlock(&lun->lun_lock); 4861 4862 return (0); 4863} 4864 4865int 4866ctl_lun_offline(struct ctl_be_lun *be_lun) 4867{ 4868 struct ctl_softc *ctl_softc; 4869 struct ctl_lun *lun; 4870 4871 ctl_softc = control_softc; 4872 4873 lun = (struct ctl_lun *)be_lun->ctl_lun; 4874 4875 mtx_lock(&lun->lun_lock); 4876 lun->flags |= CTL_LUN_OFFLINE; 4877 mtx_unlock(&lun->lun_lock); 4878 4879 return (0); 4880} 4881 4882int 4883ctl_lun_online(struct ctl_be_lun *be_lun) 4884{ 4885 struct ctl_softc *ctl_softc; 4886 struct ctl_lun *lun; 4887 4888 ctl_softc = control_softc; 4889 4890 lun = (struct ctl_lun *)be_lun->ctl_lun; 4891 4892 mtx_lock(&lun->lun_lock); 4893 lun->flags &= ~CTL_LUN_OFFLINE; 4894 mtx_unlock(&lun->lun_lock); 4895 4896 return (0); 4897} 4898 4899int 4900ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4901{ 4902 struct ctl_softc *ctl_softc; 4903 struct ctl_lun *lun; 4904 4905 ctl_softc = control_softc; 4906 4907 lun = (struct ctl_lun *)be_lun->ctl_lun; 4908 4909 mtx_lock(&lun->lun_lock); 4910 4911 /* 4912 * The LUN needs to be disabled before it can be marked invalid. 4913 */ 4914 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4915 mtx_unlock(&lun->lun_lock); 4916 return (-1); 4917 } 4918 /* 4919 * Mark the LUN invalid. 4920 */ 4921 lun->flags |= CTL_LUN_INVALID; 4922 4923 /* 4924 * If there is nothing in the OOA queue, go ahead and free the LUN. 4925 * If we have something in the OOA queue, we'll free it when the 4926 * last I/O completes. 4927 */ 4928 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4929 mtx_unlock(&lun->lun_lock); 4930 mtx_lock(&ctl_softc->ctl_lock); 4931 ctl_free_lun(lun); 4932 mtx_unlock(&ctl_softc->ctl_lock); 4933 } else 4934 mtx_unlock(&lun->lun_lock); 4935 4936 return (0); 4937} 4938 4939int 4940ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4941{ 4942 struct ctl_softc *ctl_softc; 4943 struct ctl_lun *lun; 4944 4945 ctl_softc = control_softc; 4946 lun = (struct ctl_lun *)be_lun->ctl_lun; 4947 4948 mtx_lock(&lun->lun_lock); 4949 lun->flags |= CTL_LUN_INOPERABLE; 4950 mtx_unlock(&lun->lun_lock); 4951 4952 return (0); 4953} 4954 4955int 4956ctl_lun_operable(struct ctl_be_lun *be_lun) 4957{ 4958 struct ctl_softc *ctl_softc; 4959 struct ctl_lun *lun; 4960 4961 ctl_softc = control_softc; 4962 lun = (struct ctl_lun *)be_lun->ctl_lun; 4963 4964 mtx_lock(&lun->lun_lock); 4965 lun->flags &= ~CTL_LUN_INOPERABLE; 4966 mtx_unlock(&lun->lun_lock); 4967 4968 return (0); 4969} 4970 4971void 4972ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4973{ 4974 struct ctl_lun *lun; 4975 struct ctl_softc *softc; 4976 int i; 4977 4978 softc = control_softc; 4979 4980 lun = (struct ctl_lun *)be_lun->ctl_lun; 4981 4982 mtx_lock(&lun->lun_lock); 4983 4984 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4985 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 4986 4987 mtx_unlock(&lun->lun_lock); 4988} 4989 4990/* 4991 * Backend "memory move is complete" callback for requests that never 4992 * make it down to say RAIDCore's configuration code. 4993 */ 4994int 4995ctl_config_move_done(union ctl_io *io) 4996{ 4997 int retval; 4998 4999 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 5000 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5001 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5002 5003 if ((io->io_hdr.port_status != 0) && 5004 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5005 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5006 /* 5007 * For hardware error sense keys, the sense key 5008 * specific value is defined to be a retry count, 5009 * but we use it to pass back an internal FETD 5010 * error code. XXX KDM Hopefully the FETD is only 5011 * using 16 bits for an error code, since that's 5012 * all the space we have in the sks field. 5013 */ 5014 ctl_set_internal_failure(&io->scsiio, 5015 /*sks_valid*/ 1, 5016 /*retry_count*/ 5017 io->io_hdr.port_status); 5018 } 5019 5020 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5021 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5022 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5023 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5024 /* 5025 * XXX KDM just assuming a single pointer here, and not a 5026 * S/G list. If we start using S/G lists for config data, 5027 * we'll need to know how to clean them up here as well. 5028 */ 5029 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5030 free(io->scsiio.kern_data_ptr, M_CTL); 5031 ctl_done(io); 5032 retval = CTL_RETVAL_COMPLETE; 5033 } else { 5034 /* 5035 * XXX KDM now we need to continue data movement. Some 5036 * options: 5037 * - call ctl_scsiio() again? We don't do this for data 5038 * writes, because for those at least we know ahead of 5039 * time where the write will go and how long it is. For 5040 * config writes, though, that information is largely 5041 * contained within the write itself, thus we need to 5042 * parse out the data again. 5043 * 5044 * - Call some other function once the data is in? 5045 */ 5046 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5047 ctl_data_print(io); 5048 5049 /* 5050 * XXX KDM call ctl_scsiio() again for now, and check flag 5051 * bits to see whether we're allocated or not. 5052 */ 5053 retval = ctl_scsiio(&io->scsiio); 5054 } 5055 return (retval); 5056} 5057 5058/* 5059 * This gets called by a backend driver when it is done with a 5060 * data_submit method. 5061 */ 5062void 5063ctl_data_submit_done(union ctl_io *io) 5064{ 5065 /* 5066 * If the IO_CONT flag is set, we need to call the supplied 5067 * function to continue processing the I/O, instead of completing 5068 * the I/O just yet. 5069 * 5070 * If there is an error, though, we don't want to keep processing. 5071 * Instead, just send status back to the initiator. 5072 */ 5073 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5074 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5075 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5076 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5077 io->scsiio.io_cont(io); 5078 return; 5079 } 5080 ctl_done(io); 5081} 5082 5083/* 5084 * This gets called by a backend driver when it is done with a 5085 * configuration write. 5086 */ 5087void 5088ctl_config_write_done(union ctl_io *io) 5089{ 5090 uint8_t *buf; 5091 5092 /* 5093 * If the IO_CONT flag is set, we need to call the supplied 5094 * function to continue processing the I/O, instead of completing 5095 * the I/O just yet. 5096 * 5097 * If there is an error, though, we don't want to keep processing. 5098 * Instead, just send status back to the initiator. 5099 */ 5100 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5101 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5102 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5103 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5104 io->scsiio.io_cont(io); 5105 return; 5106 } 5107 /* 5108 * Since a configuration write can be done for commands that actually 5109 * have data allocated, like write buffer, and commands that have 5110 * no data, like start/stop unit, we need to check here. 5111 */ 5112 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5113 buf = io->scsiio.kern_data_ptr; 5114 else 5115 buf = NULL; 5116 ctl_done(io); 5117 if (buf) 5118 free(buf, M_CTL); 5119} 5120 5121/* 5122 * SCSI release command. 5123 */ 5124int 5125ctl_scsi_release(struct ctl_scsiio *ctsio) 5126{ 5127 int length, longid, thirdparty_id, resv_id; 5128 struct ctl_softc *ctl_softc; 5129 struct ctl_lun *lun; 5130 uint32_t residx; 5131 5132 length = 0; 5133 resv_id = 0; 5134 5135 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5136 5137 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5138 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5139 ctl_softc = control_softc; 5140 5141 switch (ctsio->cdb[0]) { 5142 case RELEASE_10: { 5143 struct scsi_release_10 *cdb; 5144 5145 cdb = (struct scsi_release_10 *)ctsio->cdb; 5146 5147 if (cdb->byte2 & SR10_LONGID) 5148 longid = 1; 5149 else 5150 thirdparty_id = cdb->thirdparty_id; 5151 5152 resv_id = cdb->resv_id; 5153 length = scsi_2btoul(cdb->length); 5154 break; 5155 } 5156 } 5157 5158 5159 /* 5160 * XXX KDM right now, we only support LUN reservation. We don't 5161 * support 3rd party reservations, or extent reservations, which 5162 * might actually need the parameter list. If we've gotten this 5163 * far, we've got a LUN reservation. Anything else got kicked out 5164 * above. So, according to SPC, ignore the length. 5165 */ 5166 length = 0; 5167 5168 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5169 && (length > 0)) { 5170 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5171 ctsio->kern_data_len = length; 5172 ctsio->kern_total_len = length; 5173 ctsio->kern_data_resid = 0; 5174 ctsio->kern_rel_offset = 0; 5175 ctsio->kern_sg_entries = 0; 5176 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5177 ctsio->be_move_done = ctl_config_move_done; 5178 ctl_datamove((union ctl_io *)ctsio); 5179 5180 return (CTL_RETVAL_COMPLETE); 5181 } 5182 5183 if (length > 0) 5184 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5185 5186 mtx_lock(&lun->lun_lock); 5187 5188 /* 5189 * According to SPC, it is not an error for an intiator to attempt 5190 * to release a reservation on a LUN that isn't reserved, or that 5191 * is reserved by another initiator. The reservation can only be 5192 * released, though, by the initiator who made it or by one of 5193 * several reset type events. 5194 */ 5195 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5196 lun->flags &= ~CTL_LUN_RESERVED; 5197 5198 mtx_unlock(&lun->lun_lock); 5199 5200 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5201 free(ctsio->kern_data_ptr, M_CTL); 5202 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5203 } 5204 5205 ctl_set_success(ctsio); 5206 ctl_done((union ctl_io *)ctsio); 5207 return (CTL_RETVAL_COMPLETE); 5208} 5209 5210int 5211ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5212{ 5213 int extent, thirdparty, longid; 5214 int resv_id, length; 5215 uint64_t thirdparty_id; 5216 struct ctl_softc *ctl_softc; 5217 struct ctl_lun *lun; 5218 uint32_t residx; 5219 5220 extent = 0; 5221 thirdparty = 0; 5222 longid = 0; 5223 resv_id = 0; 5224 length = 0; 5225 thirdparty_id = 0; 5226 5227 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5228 5229 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5230 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5231 ctl_softc = control_softc; 5232 5233 switch (ctsio->cdb[0]) { 5234 case RESERVE_10: { 5235 struct scsi_reserve_10 *cdb; 5236 5237 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5238 5239 if (cdb->byte2 & SR10_LONGID) 5240 longid = 1; 5241 else 5242 thirdparty_id = cdb->thirdparty_id; 5243 5244 resv_id = cdb->resv_id; 5245 length = scsi_2btoul(cdb->length); 5246 break; 5247 } 5248 } 5249 5250 /* 5251 * XXX KDM right now, we only support LUN reservation. We don't 5252 * support 3rd party reservations, or extent reservations, which 5253 * might actually need the parameter list. If we've gotten this 5254 * far, we've got a LUN reservation. Anything else got kicked out 5255 * above. So, according to SPC, ignore the length. 5256 */ 5257 length = 0; 5258 5259 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5260 && (length > 0)) { 5261 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5262 ctsio->kern_data_len = length; 5263 ctsio->kern_total_len = length; 5264 ctsio->kern_data_resid = 0; 5265 ctsio->kern_rel_offset = 0; 5266 ctsio->kern_sg_entries = 0; 5267 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5268 ctsio->be_move_done = ctl_config_move_done; 5269 ctl_datamove((union ctl_io *)ctsio); 5270 5271 return (CTL_RETVAL_COMPLETE); 5272 } 5273 5274 if (length > 0) 5275 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5276 5277 mtx_lock(&lun->lun_lock); 5278 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5279 ctl_set_reservation_conflict(ctsio); 5280 goto bailout; 5281 } 5282 5283 lun->flags |= CTL_LUN_RESERVED; 5284 lun->res_idx = residx; 5285 5286 ctl_set_success(ctsio); 5287 5288bailout: 5289 mtx_unlock(&lun->lun_lock); 5290 5291 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5292 free(ctsio->kern_data_ptr, M_CTL); 5293 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5294 } 5295 5296 ctl_done((union ctl_io *)ctsio); 5297 return (CTL_RETVAL_COMPLETE); 5298} 5299 5300int 5301ctl_start_stop(struct ctl_scsiio *ctsio) 5302{ 5303 struct scsi_start_stop_unit *cdb; 5304 struct ctl_lun *lun; 5305 struct ctl_softc *ctl_softc; 5306 int retval; 5307 5308 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5309 5310 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5311 ctl_softc = control_softc; 5312 retval = 0; 5313 5314 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5315 5316 /* 5317 * XXX KDM 5318 * We don't support the immediate bit on a stop unit. In order to 5319 * do that, we would need to code up a way to know that a stop is 5320 * pending, and hold off any new commands until it completes, one 5321 * way or another. Then we could accept or reject those commands 5322 * depending on its status. We would almost need to do the reverse 5323 * of what we do below for an immediate start -- return the copy of 5324 * the ctl_io to the FETD with status to send to the host (and to 5325 * free the copy!) and then free the original I/O once the stop 5326 * actually completes. That way, the OOA queue mechanism can work 5327 * to block commands that shouldn't proceed. Another alternative 5328 * would be to put the copy in the queue in place of the original, 5329 * and return the original back to the caller. That could be 5330 * slightly safer.. 5331 */ 5332 if ((cdb->byte2 & SSS_IMMED) 5333 && ((cdb->how & SSS_START) == 0)) { 5334 ctl_set_invalid_field(ctsio, 5335 /*sks_valid*/ 1, 5336 /*command*/ 1, 5337 /*field*/ 1, 5338 /*bit_valid*/ 1, 5339 /*bit*/ 0); 5340 ctl_done((union ctl_io *)ctsio); 5341 return (CTL_RETVAL_COMPLETE); 5342 } 5343 5344 if ((lun->flags & CTL_LUN_PR_RESERVED) 5345 && ((cdb->how & SSS_START)==0)) { 5346 uint32_t residx; 5347 5348 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5349 if (lun->pr_keys[residx] == 0 5350 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5351 5352 ctl_set_reservation_conflict(ctsio); 5353 ctl_done((union ctl_io *)ctsio); 5354 return (CTL_RETVAL_COMPLETE); 5355 } 5356 } 5357 5358 /* 5359 * If there is no backend on this device, we can't start or stop 5360 * it. In theory we shouldn't get any start/stop commands in the 5361 * first place at this level if the LUN doesn't have a backend. 5362 * That should get stopped by the command decode code. 5363 */ 5364 if (lun->backend == NULL) { 5365 ctl_set_invalid_opcode(ctsio); 5366 ctl_done((union ctl_io *)ctsio); 5367 return (CTL_RETVAL_COMPLETE); 5368 } 5369 5370 /* 5371 * XXX KDM Copan-specific offline behavior. 5372 * Figure out a reasonable way to port this? 5373 */ 5374#ifdef NEEDTOPORT 5375 mtx_lock(&lun->lun_lock); 5376 5377 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5378 && (lun->flags & CTL_LUN_OFFLINE)) { 5379 /* 5380 * If the LUN is offline, and the on/offline bit isn't set, 5381 * reject the start or stop. Otherwise, let it through. 5382 */ 5383 mtx_unlock(&lun->lun_lock); 5384 ctl_set_lun_not_ready(ctsio); 5385 ctl_done((union ctl_io *)ctsio); 5386 } else { 5387 mtx_unlock(&lun->lun_lock); 5388#endif /* NEEDTOPORT */ 5389 /* 5390 * This could be a start or a stop when we're online, 5391 * or a stop/offline or start/online. A start or stop when 5392 * we're offline is covered in the case above. 5393 */ 5394 /* 5395 * In the non-immediate case, we send the request to 5396 * the backend and return status to the user when 5397 * it is done. 5398 * 5399 * In the immediate case, we allocate a new ctl_io 5400 * to hold a copy of the request, and send that to 5401 * the backend. We then set good status on the 5402 * user's request and return it immediately. 5403 */ 5404 if (cdb->byte2 & SSS_IMMED) { 5405 union ctl_io *new_io; 5406 5407 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5408 ctl_copy_io((union ctl_io *)ctsio, new_io); 5409 retval = lun->backend->config_write(new_io); 5410 ctl_set_success(ctsio); 5411 ctl_done((union ctl_io *)ctsio); 5412 } else { 5413 retval = lun->backend->config_write( 5414 (union ctl_io *)ctsio); 5415 } 5416#ifdef NEEDTOPORT 5417 } 5418#endif 5419 return (retval); 5420} 5421 5422/* 5423 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5424 * we don't really do anything with the LBA and length fields if the user 5425 * passes them in. Instead we'll just flush out the cache for the entire 5426 * LUN. 5427 */ 5428int 5429ctl_sync_cache(struct ctl_scsiio *ctsio) 5430{ 5431 struct ctl_lun *lun; 5432 struct ctl_softc *ctl_softc; 5433 uint64_t starting_lba; 5434 uint32_t block_count; 5435 int retval; 5436 5437 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5438 5439 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5440 ctl_softc = control_softc; 5441 retval = 0; 5442 5443 switch (ctsio->cdb[0]) { 5444 case SYNCHRONIZE_CACHE: { 5445 struct scsi_sync_cache *cdb; 5446 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5447 5448 starting_lba = scsi_4btoul(cdb->begin_lba); 5449 block_count = scsi_2btoul(cdb->lb_count); 5450 break; 5451 } 5452 case SYNCHRONIZE_CACHE_16: { 5453 struct scsi_sync_cache_16 *cdb; 5454 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5455 5456 starting_lba = scsi_8btou64(cdb->begin_lba); 5457 block_count = scsi_4btoul(cdb->lb_count); 5458 break; 5459 } 5460 default: 5461 ctl_set_invalid_opcode(ctsio); 5462 ctl_done((union ctl_io *)ctsio); 5463 goto bailout; 5464 break; /* NOTREACHED */ 5465 } 5466 5467 /* 5468 * We check the LBA and length, but don't do anything with them. 5469 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5470 * get flushed. This check will just help satisfy anyone who wants 5471 * to see an error for an out of range LBA. 5472 */ 5473 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5474 ctl_set_lba_out_of_range(ctsio); 5475 ctl_done((union ctl_io *)ctsio); 5476 goto bailout; 5477 } 5478 5479 /* 5480 * If this LUN has no backend, we can't flush the cache anyway. 5481 */ 5482 if (lun->backend == NULL) { 5483 ctl_set_invalid_opcode(ctsio); 5484 ctl_done((union ctl_io *)ctsio); 5485 goto bailout; 5486 } 5487 5488 /* 5489 * Check to see whether we're configured to send the SYNCHRONIZE 5490 * CACHE command directly to the back end. 5491 */ 5492 mtx_lock(&lun->lun_lock); 5493 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5494 && (++(lun->sync_count) >= lun->sync_interval)) { 5495 lun->sync_count = 0; 5496 mtx_unlock(&lun->lun_lock); 5497 retval = lun->backend->config_write((union ctl_io *)ctsio); 5498 } else { 5499 mtx_unlock(&lun->lun_lock); 5500 ctl_set_success(ctsio); 5501 ctl_done((union ctl_io *)ctsio); 5502 } 5503 5504bailout: 5505 5506 return (retval); 5507} 5508 5509int 5510ctl_format(struct ctl_scsiio *ctsio) 5511{ 5512 struct scsi_format *cdb; 5513 struct ctl_lun *lun; 5514 struct ctl_softc *ctl_softc; 5515 int length, defect_list_len; 5516 5517 CTL_DEBUG_PRINT(("ctl_format\n")); 5518 5519 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5520 ctl_softc = control_softc; 5521 5522 cdb = (struct scsi_format *)ctsio->cdb; 5523 5524 length = 0; 5525 if (cdb->byte2 & SF_FMTDATA) { 5526 if (cdb->byte2 & SF_LONGLIST) 5527 length = sizeof(struct scsi_format_header_long); 5528 else 5529 length = sizeof(struct scsi_format_header_short); 5530 } 5531 5532 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5533 && (length > 0)) { 5534 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5535 ctsio->kern_data_len = length; 5536 ctsio->kern_total_len = length; 5537 ctsio->kern_data_resid = 0; 5538 ctsio->kern_rel_offset = 0; 5539 ctsio->kern_sg_entries = 0; 5540 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5541 ctsio->be_move_done = ctl_config_move_done; 5542 ctl_datamove((union ctl_io *)ctsio); 5543 5544 return (CTL_RETVAL_COMPLETE); 5545 } 5546 5547 defect_list_len = 0; 5548 5549 if (cdb->byte2 & SF_FMTDATA) { 5550 if (cdb->byte2 & SF_LONGLIST) { 5551 struct scsi_format_header_long *header; 5552 5553 header = (struct scsi_format_header_long *) 5554 ctsio->kern_data_ptr; 5555 5556 defect_list_len = scsi_4btoul(header->defect_list_len); 5557 if (defect_list_len != 0) { 5558 ctl_set_invalid_field(ctsio, 5559 /*sks_valid*/ 1, 5560 /*command*/ 0, 5561 /*field*/ 2, 5562 /*bit_valid*/ 0, 5563 /*bit*/ 0); 5564 goto bailout; 5565 } 5566 } else { 5567 struct scsi_format_header_short *header; 5568 5569 header = (struct scsi_format_header_short *) 5570 ctsio->kern_data_ptr; 5571 5572 defect_list_len = scsi_2btoul(header->defect_list_len); 5573 if (defect_list_len != 0) { 5574 ctl_set_invalid_field(ctsio, 5575 /*sks_valid*/ 1, 5576 /*command*/ 0, 5577 /*field*/ 2, 5578 /*bit_valid*/ 0, 5579 /*bit*/ 0); 5580 goto bailout; 5581 } 5582 } 5583 } 5584 5585 /* 5586 * The format command will clear out the "Medium format corrupted" 5587 * status if set by the configuration code. That status is really 5588 * just a way to notify the host that we have lost the media, and 5589 * get them to issue a command that will basically make them think 5590 * they're blowing away the media. 5591 */ 5592 mtx_lock(&lun->lun_lock); 5593 lun->flags &= ~CTL_LUN_INOPERABLE; 5594 mtx_unlock(&lun->lun_lock); 5595 5596 ctl_set_success(ctsio); 5597bailout: 5598 5599 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5600 free(ctsio->kern_data_ptr, M_CTL); 5601 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5602 } 5603 5604 ctl_done((union ctl_io *)ctsio); 5605 return (CTL_RETVAL_COMPLETE); 5606} 5607 5608int 5609ctl_read_buffer(struct ctl_scsiio *ctsio) 5610{ 5611 struct scsi_read_buffer *cdb; 5612 struct ctl_lun *lun; 5613 int buffer_offset, len; 5614 static uint8_t descr[4]; 5615 static uint8_t echo_descr[4] = { 0 }; 5616 5617 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5618 5619 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5620 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5621 5622 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5623 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5624 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5625 ctl_set_invalid_field(ctsio, 5626 /*sks_valid*/ 1, 5627 /*command*/ 1, 5628 /*field*/ 1, 5629 /*bit_valid*/ 1, 5630 /*bit*/ 4); 5631 ctl_done((union ctl_io *)ctsio); 5632 return (CTL_RETVAL_COMPLETE); 5633 } 5634 5635 len = scsi_3btoul(cdb->length); 5636 buffer_offset = scsi_3btoul(cdb->offset); 5637 5638 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5639 ctl_set_invalid_field(ctsio, 5640 /*sks_valid*/ 1, 5641 /*command*/ 1, 5642 /*field*/ 6, 5643 /*bit_valid*/ 0, 5644 /*bit*/ 0); 5645 ctl_done((union ctl_io *)ctsio); 5646 return (CTL_RETVAL_COMPLETE); 5647 } 5648 5649 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5650 descr[0] = 0; 5651 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5652 ctsio->kern_data_ptr = descr; 5653 len = min(len, sizeof(descr)); 5654 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5655 ctsio->kern_data_ptr = echo_descr; 5656 len = min(len, sizeof(echo_descr)); 5657 } else { 5658 if (lun->write_buffer == NULL) { 5659 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5660 M_CTL, M_WAITOK); 5661 } 5662 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5663 } 5664 ctsio->kern_data_len = len; 5665 ctsio->kern_total_len = len; 5666 ctsio->kern_data_resid = 0; 5667 ctsio->kern_rel_offset = 0; 5668 ctsio->kern_sg_entries = 0; 5669 ctl_set_success(ctsio); 5670 ctsio->be_move_done = ctl_config_move_done; 5671 ctl_datamove((union ctl_io *)ctsio); 5672 return (CTL_RETVAL_COMPLETE); 5673} 5674 5675int 5676ctl_write_buffer(struct ctl_scsiio *ctsio) 5677{ 5678 struct scsi_write_buffer *cdb; 5679 struct ctl_lun *lun; 5680 int buffer_offset, len; 5681 5682 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5683 5684 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5685 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5686 5687 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5688 ctl_set_invalid_field(ctsio, 5689 /*sks_valid*/ 1, 5690 /*command*/ 1, 5691 /*field*/ 1, 5692 /*bit_valid*/ 1, 5693 /*bit*/ 4); 5694 ctl_done((union ctl_io *)ctsio); 5695 return (CTL_RETVAL_COMPLETE); 5696 } 5697 5698 len = scsi_3btoul(cdb->length); 5699 buffer_offset = scsi_3btoul(cdb->offset); 5700 5701 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5702 ctl_set_invalid_field(ctsio, 5703 /*sks_valid*/ 1, 5704 /*command*/ 1, 5705 /*field*/ 6, 5706 /*bit_valid*/ 0, 5707 /*bit*/ 0); 5708 ctl_done((union ctl_io *)ctsio); 5709 return (CTL_RETVAL_COMPLETE); 5710 } 5711 5712 /* 5713 * If we've got a kernel request that hasn't been malloced yet, 5714 * malloc it and tell the caller the data buffer is here. 5715 */ 5716 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5717 if (lun->write_buffer == NULL) { 5718 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5719 M_CTL, M_WAITOK); 5720 } 5721 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5722 ctsio->kern_data_len = len; 5723 ctsio->kern_total_len = len; 5724 ctsio->kern_data_resid = 0; 5725 ctsio->kern_rel_offset = 0; 5726 ctsio->kern_sg_entries = 0; 5727 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5728 ctsio->be_move_done = ctl_config_move_done; 5729 ctl_datamove((union ctl_io *)ctsio); 5730 5731 return (CTL_RETVAL_COMPLETE); 5732 } 5733 5734 ctl_set_success(ctsio); 5735 ctl_done((union ctl_io *)ctsio); 5736 return (CTL_RETVAL_COMPLETE); 5737} 5738 5739int 5740ctl_write_same(struct ctl_scsiio *ctsio) 5741{ 5742 struct ctl_lun *lun; 5743 struct ctl_lba_len_flags *lbalen; 5744 uint64_t lba; 5745 uint32_t num_blocks; 5746 int len, retval; 5747 uint8_t byte2; 5748 5749 retval = CTL_RETVAL_COMPLETE; 5750 5751 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5752 5753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5754 5755 switch (ctsio->cdb[0]) { 5756 case WRITE_SAME_10: { 5757 struct scsi_write_same_10 *cdb; 5758 5759 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5760 5761 lba = scsi_4btoul(cdb->addr); 5762 num_blocks = scsi_2btoul(cdb->length); 5763 byte2 = cdb->byte2; 5764 break; 5765 } 5766 case WRITE_SAME_16: { 5767 struct scsi_write_same_16 *cdb; 5768 5769 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5770 5771 lba = scsi_8btou64(cdb->addr); 5772 num_blocks = scsi_4btoul(cdb->length); 5773 byte2 = cdb->byte2; 5774 break; 5775 } 5776 default: 5777 /* 5778 * We got a command we don't support. This shouldn't 5779 * happen, commands should be filtered out above us. 5780 */ 5781 ctl_set_invalid_opcode(ctsio); 5782 ctl_done((union ctl_io *)ctsio); 5783 5784 return (CTL_RETVAL_COMPLETE); 5785 break; /* NOTREACHED */ 5786 } 5787 5788 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5789 if ((byte2 & SWS_UNMAP) == 0 && 5790 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5791 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5792 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5793 ctl_done((union ctl_io *)ctsio); 5794 return (CTL_RETVAL_COMPLETE); 5795 } 5796 5797 /* 5798 * The first check is to make sure we're in bounds, the second 5799 * check is to catch wrap-around problems. If the lba + num blocks 5800 * is less than the lba, then we've wrapped around and the block 5801 * range is invalid anyway. 5802 */ 5803 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5804 || ((lba + num_blocks) < lba)) { 5805 ctl_set_lba_out_of_range(ctsio); 5806 ctl_done((union ctl_io *)ctsio); 5807 return (CTL_RETVAL_COMPLETE); 5808 } 5809 5810 /* Zero number of blocks means "to the last logical block" */ 5811 if (num_blocks == 0) { 5812 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5813 ctl_set_invalid_field(ctsio, 5814 /*sks_valid*/ 0, 5815 /*command*/ 1, 5816 /*field*/ 0, 5817 /*bit_valid*/ 0, 5818 /*bit*/ 0); 5819 ctl_done((union ctl_io *)ctsio); 5820 return (CTL_RETVAL_COMPLETE); 5821 } 5822 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5823 } 5824 5825 len = lun->be_lun->blocksize; 5826 5827 /* 5828 * If we've got a kernel request that hasn't been malloced yet, 5829 * malloc it and tell the caller the data buffer is here. 5830 */ 5831 if ((byte2 & SWS_NDOB) == 0 && 5832 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5833 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5834 ctsio->kern_data_len = len; 5835 ctsio->kern_total_len = len; 5836 ctsio->kern_data_resid = 0; 5837 ctsio->kern_rel_offset = 0; 5838 ctsio->kern_sg_entries = 0; 5839 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5840 ctsio->be_move_done = ctl_config_move_done; 5841 ctl_datamove((union ctl_io *)ctsio); 5842 5843 return (CTL_RETVAL_COMPLETE); 5844 } 5845 5846 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5847 lbalen->lba = lba; 5848 lbalen->len = num_blocks; 5849 lbalen->flags = byte2; 5850 retval = lun->backend->config_write((union ctl_io *)ctsio); 5851 5852 return (retval); 5853} 5854 5855int 5856ctl_unmap(struct ctl_scsiio *ctsio) 5857{ 5858 struct ctl_lun *lun; 5859 struct scsi_unmap *cdb; 5860 struct ctl_ptr_len_flags *ptrlen; 5861 struct scsi_unmap_header *hdr; 5862 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5863 uint64_t lba; 5864 uint32_t num_blocks; 5865 int len, retval; 5866 uint8_t byte2; 5867 5868 retval = CTL_RETVAL_COMPLETE; 5869 5870 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5871 5872 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5873 cdb = (struct scsi_unmap *)ctsio->cdb; 5874 5875 len = scsi_2btoul(cdb->length); 5876 byte2 = cdb->byte2; 5877 5878 /* 5879 * If we've got a kernel request that hasn't been malloced yet, 5880 * malloc it and tell the caller the data buffer is here. 5881 */ 5882 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5883 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5884 ctsio->kern_data_len = len; 5885 ctsio->kern_total_len = len; 5886 ctsio->kern_data_resid = 0; 5887 ctsio->kern_rel_offset = 0; 5888 ctsio->kern_sg_entries = 0; 5889 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5890 ctsio->be_move_done = ctl_config_move_done; 5891 ctl_datamove((union ctl_io *)ctsio); 5892 5893 return (CTL_RETVAL_COMPLETE); 5894 } 5895 5896 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5897 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5898 if (len < sizeof (*hdr) || 5899 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5900 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5901 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5902 ctl_set_invalid_field(ctsio, 5903 /*sks_valid*/ 0, 5904 /*command*/ 0, 5905 /*field*/ 0, 5906 /*bit_valid*/ 0, 5907 /*bit*/ 0); 5908 ctl_done((union ctl_io *)ctsio); 5909 return (CTL_RETVAL_COMPLETE); 5910 } 5911 len = scsi_2btoul(hdr->desc_length); 5912 buf = (struct scsi_unmap_desc *)(hdr + 1); 5913 end = buf + len / sizeof(*buf); 5914 5915 endnz = buf; 5916 for (range = buf; range < end; range++) { 5917 lba = scsi_8btou64(range->lba); 5918 num_blocks = scsi_4btoul(range->length); 5919 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5920 || ((lba + num_blocks) < lba)) { 5921 ctl_set_lba_out_of_range(ctsio); 5922 ctl_done((union ctl_io *)ctsio); 5923 return (CTL_RETVAL_COMPLETE); 5924 } 5925 if (num_blocks != 0) 5926 endnz = range + 1; 5927 } 5928 5929 /* 5930 * Block backend can not handle zero last range. 5931 * Filter it out and return if there is nothing left. 5932 */ 5933 len = (uint8_t *)endnz - (uint8_t *)buf; 5934 if (len == 0) { 5935 ctl_set_success(ctsio); 5936 ctl_done((union ctl_io *)ctsio); 5937 return (CTL_RETVAL_COMPLETE); 5938 } 5939 5940 mtx_lock(&lun->lun_lock); 5941 ptrlen = (struct ctl_ptr_len_flags *) 5942 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5943 ptrlen->ptr = (void *)buf; 5944 ptrlen->len = len; 5945 ptrlen->flags = byte2; 5946 ctl_check_blocked(lun); 5947 mtx_unlock(&lun->lun_lock); 5948 5949 retval = lun->backend->config_write((union ctl_io *)ctsio); 5950 return (retval); 5951} 5952 5953/* 5954 * Note that this function currently doesn't actually do anything inside 5955 * CTL to enforce things if the DQue bit is turned on. 5956 * 5957 * Also note that this function can't be used in the default case, because 5958 * the DQue bit isn't set in the changeable mask for the control mode page 5959 * anyway. This is just here as an example for how to implement a page 5960 * handler, and a placeholder in case we want to allow the user to turn 5961 * tagged queueing on and off. 5962 * 5963 * The D_SENSE bit handling is functional, however, and will turn 5964 * descriptor sense on and off for a given LUN. 5965 */ 5966int 5967ctl_control_page_handler(struct ctl_scsiio *ctsio, 5968 struct ctl_page_index *page_index, uint8_t *page_ptr) 5969{ 5970 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5971 struct ctl_lun *lun; 5972 struct ctl_softc *softc; 5973 int set_ua; 5974 uint32_t initidx; 5975 5976 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5977 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5978 set_ua = 0; 5979 5980 user_cp = (struct scsi_control_page *)page_ptr; 5981 current_cp = (struct scsi_control_page *) 5982 (page_index->page_data + (page_index->page_len * 5983 CTL_PAGE_CURRENT)); 5984 saved_cp = (struct scsi_control_page *) 5985 (page_index->page_data + (page_index->page_len * 5986 CTL_PAGE_SAVED)); 5987 5988 softc = control_softc; 5989 5990 mtx_lock(&lun->lun_lock); 5991 if (((current_cp->rlec & SCP_DSENSE) == 0) 5992 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5993 /* 5994 * Descriptor sense is currently turned off and the user 5995 * wants to turn it on. 5996 */ 5997 current_cp->rlec |= SCP_DSENSE; 5998 saved_cp->rlec |= SCP_DSENSE; 5999 lun->flags |= CTL_LUN_SENSE_DESC; 6000 set_ua = 1; 6001 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6002 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6003 /* 6004 * Descriptor sense is currently turned on, and the user 6005 * wants to turn it off. 6006 */ 6007 current_cp->rlec &= ~SCP_DSENSE; 6008 saved_cp->rlec &= ~SCP_DSENSE; 6009 lun->flags &= ~CTL_LUN_SENSE_DESC; 6010 set_ua = 1; 6011 } 6012 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6013 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6014 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6015 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6016 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6017 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6018 set_ua = 1; 6019 } 6020 if ((current_cp->eca_and_aen & SCP_SWP) != 6021 (user_cp->eca_and_aen & SCP_SWP)) { 6022 current_cp->eca_and_aen &= ~SCP_SWP; 6023 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6024 saved_cp->eca_and_aen &= ~SCP_SWP; 6025 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6026 set_ua = 1; 6027 } 6028 if (set_ua != 0) { 6029 int i; 6030 /* 6031 * Let other initiators know that the mode 6032 * parameters for this LUN have changed. 6033 */ 6034 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6035 if (i == initidx) 6036 continue; 6037 6038 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6039 } 6040 } 6041 mtx_unlock(&lun->lun_lock); 6042 6043 return (0); 6044} 6045 6046int 6047ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6048 struct ctl_page_index *page_index, uint8_t *page_ptr) 6049{ 6050 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6051 struct ctl_lun *lun; 6052 int set_ua; 6053 uint32_t initidx; 6054 6055 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6056 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6057 set_ua = 0; 6058 6059 user_cp = (struct scsi_caching_page *)page_ptr; 6060 current_cp = (struct scsi_caching_page *) 6061 (page_index->page_data + (page_index->page_len * 6062 CTL_PAGE_CURRENT)); 6063 saved_cp = (struct scsi_caching_page *) 6064 (page_index->page_data + (page_index->page_len * 6065 CTL_PAGE_SAVED)); 6066 6067 mtx_lock(&lun->lun_lock); 6068 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6069 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6070 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6071 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6072 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6073 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6074 set_ua = 1; 6075 } 6076 if (set_ua != 0) { 6077 int i; 6078 /* 6079 * Let other initiators know that the mode 6080 * parameters for this LUN have changed. 6081 */ 6082 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6083 if (i == initidx) 6084 continue; 6085 6086 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6087 } 6088 } 6089 mtx_unlock(&lun->lun_lock); 6090 6091 return (0); 6092} 6093 6094int 6095ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6096 struct ctl_page_index *page_index, 6097 uint8_t *page_ptr) 6098{ 6099 uint8_t *c; 6100 int i; 6101 6102 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6103 ctl_time_io_secs = 6104 (c[0] << 8) | 6105 (c[1] << 0) | 6106 0; 6107 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6108 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6109 printf("page data:"); 6110 for (i=0; i<8; i++) 6111 printf(" %.2x",page_ptr[i]); 6112 printf("\n"); 6113 return (0); 6114} 6115 6116int 6117ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6118 struct ctl_page_index *page_index, 6119 int pc) 6120{ 6121 struct copan_debugconf_subpage *page; 6122 6123 page = (struct copan_debugconf_subpage *)page_index->page_data + 6124 (page_index->page_len * pc); 6125 6126 switch (pc) { 6127 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6128 case SMS_PAGE_CTRL_DEFAULT >> 6: 6129 case SMS_PAGE_CTRL_SAVED >> 6: 6130 /* 6131 * We don't update the changable or default bits for this page. 6132 */ 6133 break; 6134 case SMS_PAGE_CTRL_CURRENT >> 6: 6135 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6136 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6137 break; 6138 default: 6139#ifdef NEEDTOPORT 6140 EPRINT(0, "Invalid PC %d!!", pc); 6141#endif /* NEEDTOPORT */ 6142 break; 6143 } 6144 return (0); 6145} 6146 6147 6148static int 6149ctl_do_mode_select(union ctl_io *io) 6150{ 6151 struct scsi_mode_page_header *page_header; 6152 struct ctl_page_index *page_index; 6153 struct ctl_scsiio *ctsio; 6154 int control_dev, page_len; 6155 int page_len_offset, page_len_size; 6156 union ctl_modepage_info *modepage_info; 6157 struct ctl_lun *lun; 6158 int *len_left, *len_used; 6159 int retval, i; 6160 6161 ctsio = &io->scsiio; 6162 page_index = NULL; 6163 page_len = 0; 6164 retval = CTL_RETVAL_COMPLETE; 6165 6166 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6167 6168 if (lun->be_lun->lun_type != T_DIRECT) 6169 control_dev = 1; 6170 else 6171 control_dev = 0; 6172 6173 modepage_info = (union ctl_modepage_info *) 6174 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6175 len_left = &modepage_info->header.len_left; 6176 len_used = &modepage_info->header.len_used; 6177 6178do_next_page: 6179 6180 page_header = (struct scsi_mode_page_header *) 6181 (ctsio->kern_data_ptr + *len_used); 6182 6183 if (*len_left == 0) { 6184 free(ctsio->kern_data_ptr, M_CTL); 6185 ctl_set_success(ctsio); 6186 ctl_done((union ctl_io *)ctsio); 6187 return (CTL_RETVAL_COMPLETE); 6188 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6189 6190 free(ctsio->kern_data_ptr, M_CTL); 6191 ctl_set_param_len_error(ctsio); 6192 ctl_done((union ctl_io *)ctsio); 6193 return (CTL_RETVAL_COMPLETE); 6194 6195 } else if ((page_header->page_code & SMPH_SPF) 6196 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6197 6198 free(ctsio->kern_data_ptr, M_CTL); 6199 ctl_set_param_len_error(ctsio); 6200 ctl_done((union ctl_io *)ctsio); 6201 return (CTL_RETVAL_COMPLETE); 6202 } 6203 6204 6205 /* 6206 * XXX KDM should we do something with the block descriptor? 6207 */ 6208 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6209 6210 if ((control_dev != 0) 6211 && (lun->mode_pages.index[i].page_flags & 6212 CTL_PAGE_FLAG_DISK_ONLY)) 6213 continue; 6214 6215 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6216 (page_header->page_code & SMPH_PC_MASK)) 6217 continue; 6218 6219 /* 6220 * If neither page has a subpage code, then we've got a 6221 * match. 6222 */ 6223 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6224 && ((page_header->page_code & SMPH_SPF) == 0)) { 6225 page_index = &lun->mode_pages.index[i]; 6226 page_len = page_header->page_length; 6227 break; 6228 } 6229 6230 /* 6231 * If both pages have subpages, then the subpage numbers 6232 * have to match. 6233 */ 6234 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6235 && (page_header->page_code & SMPH_SPF)) { 6236 struct scsi_mode_page_header_sp *sph; 6237 6238 sph = (struct scsi_mode_page_header_sp *)page_header; 6239 6240 if (lun->mode_pages.index[i].subpage == 6241 sph->subpage) { 6242 page_index = &lun->mode_pages.index[i]; 6243 page_len = scsi_2btoul(sph->page_length); 6244 break; 6245 } 6246 } 6247 } 6248 6249 /* 6250 * If we couldn't find the page, or if we don't have a mode select 6251 * handler for it, send back an error to the user. 6252 */ 6253 if ((page_index == NULL) 6254 || (page_index->select_handler == NULL)) { 6255 ctl_set_invalid_field(ctsio, 6256 /*sks_valid*/ 1, 6257 /*command*/ 0, 6258 /*field*/ *len_used, 6259 /*bit_valid*/ 0, 6260 /*bit*/ 0); 6261 free(ctsio->kern_data_ptr, M_CTL); 6262 ctl_done((union ctl_io *)ctsio); 6263 return (CTL_RETVAL_COMPLETE); 6264 } 6265 6266 if (page_index->page_code & SMPH_SPF) { 6267 page_len_offset = 2; 6268 page_len_size = 2; 6269 } else { 6270 page_len_size = 1; 6271 page_len_offset = 1; 6272 } 6273 6274 /* 6275 * If the length the initiator gives us isn't the one we specify in 6276 * the mode page header, or if they didn't specify enough data in 6277 * the CDB to avoid truncating this page, kick out the request. 6278 */ 6279 if ((page_len != (page_index->page_len - page_len_offset - 6280 page_len_size)) 6281 || (*len_left < page_index->page_len)) { 6282 6283 6284 ctl_set_invalid_field(ctsio, 6285 /*sks_valid*/ 1, 6286 /*command*/ 0, 6287 /*field*/ *len_used + page_len_offset, 6288 /*bit_valid*/ 0, 6289 /*bit*/ 0); 6290 free(ctsio->kern_data_ptr, M_CTL); 6291 ctl_done((union ctl_io *)ctsio); 6292 return (CTL_RETVAL_COMPLETE); 6293 } 6294 6295 /* 6296 * Run through the mode page, checking to make sure that the bits 6297 * the user changed are actually legal for him to change. 6298 */ 6299 for (i = 0; i < page_index->page_len; i++) { 6300 uint8_t *user_byte, *change_mask, *current_byte; 6301 int bad_bit; 6302 int j; 6303 6304 user_byte = (uint8_t *)page_header + i; 6305 change_mask = page_index->page_data + 6306 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6307 current_byte = page_index->page_data + 6308 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6309 6310 /* 6311 * Check to see whether the user set any bits in this byte 6312 * that he is not allowed to set. 6313 */ 6314 if ((*user_byte & ~(*change_mask)) == 6315 (*current_byte & ~(*change_mask))) 6316 continue; 6317 6318 /* 6319 * Go through bit by bit to determine which one is illegal. 6320 */ 6321 bad_bit = 0; 6322 for (j = 7; j >= 0; j--) { 6323 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6324 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6325 bad_bit = i; 6326 break; 6327 } 6328 } 6329 ctl_set_invalid_field(ctsio, 6330 /*sks_valid*/ 1, 6331 /*command*/ 0, 6332 /*field*/ *len_used + i, 6333 /*bit_valid*/ 1, 6334 /*bit*/ bad_bit); 6335 free(ctsio->kern_data_ptr, M_CTL); 6336 ctl_done((union ctl_io *)ctsio); 6337 return (CTL_RETVAL_COMPLETE); 6338 } 6339 6340 /* 6341 * Decrement these before we call the page handler, since we may 6342 * end up getting called back one way or another before the handler 6343 * returns to this context. 6344 */ 6345 *len_left -= page_index->page_len; 6346 *len_used += page_index->page_len; 6347 6348 retval = page_index->select_handler(ctsio, page_index, 6349 (uint8_t *)page_header); 6350 6351 /* 6352 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6353 * wait until this queued command completes to finish processing 6354 * the mode page. If it returns anything other than 6355 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6356 * already set the sense information, freed the data pointer, and 6357 * completed the io for us. 6358 */ 6359 if (retval != CTL_RETVAL_COMPLETE) 6360 goto bailout_no_done; 6361 6362 /* 6363 * If the initiator sent us more than one page, parse the next one. 6364 */ 6365 if (*len_left > 0) 6366 goto do_next_page; 6367 6368 ctl_set_success(ctsio); 6369 free(ctsio->kern_data_ptr, M_CTL); 6370 ctl_done((union ctl_io *)ctsio); 6371 6372bailout_no_done: 6373 6374 return (CTL_RETVAL_COMPLETE); 6375 6376} 6377 6378int 6379ctl_mode_select(struct ctl_scsiio *ctsio) 6380{ 6381 int param_len, pf, sp; 6382 int header_size, bd_len; 6383 int len_left, len_used; 6384 struct ctl_page_index *page_index; 6385 struct ctl_lun *lun; 6386 int control_dev, page_len; 6387 union ctl_modepage_info *modepage_info; 6388 int retval; 6389 6390 pf = 0; 6391 sp = 0; 6392 page_len = 0; 6393 len_used = 0; 6394 len_left = 0; 6395 retval = 0; 6396 bd_len = 0; 6397 page_index = NULL; 6398 6399 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6400 6401 if (lun->be_lun->lun_type != T_DIRECT) 6402 control_dev = 1; 6403 else 6404 control_dev = 0; 6405 6406 switch (ctsio->cdb[0]) { 6407 case MODE_SELECT_6: { 6408 struct scsi_mode_select_6 *cdb; 6409 6410 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6411 6412 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6413 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6414 6415 param_len = cdb->length; 6416 header_size = sizeof(struct scsi_mode_header_6); 6417 break; 6418 } 6419 case MODE_SELECT_10: { 6420 struct scsi_mode_select_10 *cdb; 6421 6422 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6423 6424 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6425 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6426 6427 param_len = scsi_2btoul(cdb->length); 6428 header_size = sizeof(struct scsi_mode_header_10); 6429 break; 6430 } 6431 default: 6432 ctl_set_invalid_opcode(ctsio); 6433 ctl_done((union ctl_io *)ctsio); 6434 return (CTL_RETVAL_COMPLETE); 6435 break; /* NOTREACHED */ 6436 } 6437 6438 /* 6439 * From SPC-3: 6440 * "A parameter list length of zero indicates that the Data-Out Buffer 6441 * shall be empty. This condition shall not be considered as an error." 6442 */ 6443 if (param_len == 0) { 6444 ctl_set_success(ctsio); 6445 ctl_done((union ctl_io *)ctsio); 6446 return (CTL_RETVAL_COMPLETE); 6447 } 6448 6449 /* 6450 * Since we'll hit this the first time through, prior to 6451 * allocation, we don't need to free a data buffer here. 6452 */ 6453 if (param_len < header_size) { 6454 ctl_set_param_len_error(ctsio); 6455 ctl_done((union ctl_io *)ctsio); 6456 return (CTL_RETVAL_COMPLETE); 6457 } 6458 6459 /* 6460 * Allocate the data buffer and grab the user's data. In theory, 6461 * we shouldn't have to sanity check the parameter list length here 6462 * because the maximum size is 64K. We should be able to malloc 6463 * that much without too many problems. 6464 */ 6465 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6466 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6467 ctsio->kern_data_len = param_len; 6468 ctsio->kern_total_len = param_len; 6469 ctsio->kern_data_resid = 0; 6470 ctsio->kern_rel_offset = 0; 6471 ctsio->kern_sg_entries = 0; 6472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6473 ctsio->be_move_done = ctl_config_move_done; 6474 ctl_datamove((union ctl_io *)ctsio); 6475 6476 return (CTL_RETVAL_COMPLETE); 6477 } 6478 6479 switch (ctsio->cdb[0]) { 6480 case MODE_SELECT_6: { 6481 struct scsi_mode_header_6 *mh6; 6482 6483 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6484 bd_len = mh6->blk_desc_len; 6485 break; 6486 } 6487 case MODE_SELECT_10: { 6488 struct scsi_mode_header_10 *mh10; 6489 6490 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6491 bd_len = scsi_2btoul(mh10->blk_desc_len); 6492 break; 6493 } 6494 default: 6495 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6496 break; 6497 } 6498 6499 if (param_len < (header_size + bd_len)) { 6500 free(ctsio->kern_data_ptr, M_CTL); 6501 ctl_set_param_len_error(ctsio); 6502 ctl_done((union ctl_io *)ctsio); 6503 return (CTL_RETVAL_COMPLETE); 6504 } 6505 6506 /* 6507 * Set the IO_CONT flag, so that if this I/O gets passed to 6508 * ctl_config_write_done(), it'll get passed back to 6509 * ctl_do_mode_select() for further processing, or completion if 6510 * we're all done. 6511 */ 6512 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6513 ctsio->io_cont = ctl_do_mode_select; 6514 6515 modepage_info = (union ctl_modepage_info *) 6516 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6517 6518 memset(modepage_info, 0, sizeof(*modepage_info)); 6519 6520 len_left = param_len - header_size - bd_len; 6521 len_used = header_size + bd_len; 6522 6523 modepage_info->header.len_left = len_left; 6524 modepage_info->header.len_used = len_used; 6525 6526 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6527} 6528 6529int 6530ctl_mode_sense(struct ctl_scsiio *ctsio) 6531{ 6532 struct ctl_lun *lun; 6533 int pc, page_code, dbd, llba, subpage; 6534 int alloc_len, page_len, header_len, total_len; 6535 struct scsi_mode_block_descr *block_desc; 6536 struct ctl_page_index *page_index; 6537 int control_dev; 6538 6539 dbd = 0; 6540 llba = 0; 6541 block_desc = NULL; 6542 page_index = NULL; 6543 6544 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6545 6546 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6547 6548 if (lun->be_lun->lun_type != T_DIRECT) 6549 control_dev = 1; 6550 else 6551 control_dev = 0; 6552 6553 switch (ctsio->cdb[0]) { 6554 case MODE_SENSE_6: { 6555 struct scsi_mode_sense_6 *cdb; 6556 6557 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6558 6559 header_len = sizeof(struct scsi_mode_hdr_6); 6560 if (cdb->byte2 & SMS_DBD) 6561 dbd = 1; 6562 else 6563 header_len += sizeof(struct scsi_mode_block_descr); 6564 6565 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6566 page_code = cdb->page & SMS_PAGE_CODE; 6567 subpage = cdb->subpage; 6568 alloc_len = cdb->length; 6569 break; 6570 } 6571 case MODE_SENSE_10: { 6572 struct scsi_mode_sense_10 *cdb; 6573 6574 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6575 6576 header_len = sizeof(struct scsi_mode_hdr_10); 6577 6578 if (cdb->byte2 & SMS_DBD) 6579 dbd = 1; 6580 else 6581 header_len += sizeof(struct scsi_mode_block_descr); 6582 if (cdb->byte2 & SMS10_LLBAA) 6583 llba = 1; 6584 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6585 page_code = cdb->page & SMS_PAGE_CODE; 6586 subpage = cdb->subpage; 6587 alloc_len = scsi_2btoul(cdb->length); 6588 break; 6589 } 6590 default: 6591 ctl_set_invalid_opcode(ctsio); 6592 ctl_done((union ctl_io *)ctsio); 6593 return (CTL_RETVAL_COMPLETE); 6594 break; /* NOTREACHED */ 6595 } 6596 6597 /* 6598 * We have to make a first pass through to calculate the size of 6599 * the pages that match the user's query. Then we allocate enough 6600 * memory to hold it, and actually copy the data into the buffer. 6601 */ 6602 switch (page_code) { 6603 case SMS_ALL_PAGES_PAGE: { 6604 int i; 6605 6606 page_len = 0; 6607 6608 /* 6609 * At the moment, values other than 0 and 0xff here are 6610 * reserved according to SPC-3. 6611 */ 6612 if ((subpage != SMS_SUBPAGE_PAGE_0) 6613 && (subpage != SMS_SUBPAGE_ALL)) { 6614 ctl_set_invalid_field(ctsio, 6615 /*sks_valid*/ 1, 6616 /*command*/ 1, 6617 /*field*/ 3, 6618 /*bit_valid*/ 0, 6619 /*bit*/ 0); 6620 ctl_done((union ctl_io *)ctsio); 6621 return (CTL_RETVAL_COMPLETE); 6622 } 6623 6624 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6625 if ((control_dev != 0) 6626 && (lun->mode_pages.index[i].page_flags & 6627 CTL_PAGE_FLAG_DISK_ONLY)) 6628 continue; 6629 6630 /* 6631 * We don't use this subpage if the user didn't 6632 * request all subpages. 6633 */ 6634 if ((lun->mode_pages.index[i].subpage != 0) 6635 && (subpage == SMS_SUBPAGE_PAGE_0)) 6636 continue; 6637 6638#if 0 6639 printf("found page %#x len %d\n", 6640 lun->mode_pages.index[i].page_code & 6641 SMPH_PC_MASK, 6642 lun->mode_pages.index[i].page_len); 6643#endif 6644 page_len += lun->mode_pages.index[i].page_len; 6645 } 6646 break; 6647 } 6648 default: { 6649 int i; 6650 6651 page_len = 0; 6652 6653 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6654 /* Look for the right page code */ 6655 if ((lun->mode_pages.index[i].page_code & 6656 SMPH_PC_MASK) != page_code) 6657 continue; 6658 6659 /* Look for the right subpage or the subpage wildcard*/ 6660 if ((lun->mode_pages.index[i].subpage != subpage) 6661 && (subpage != SMS_SUBPAGE_ALL)) 6662 continue; 6663 6664 /* Make sure the page is supported for this dev type */ 6665 if ((control_dev != 0) 6666 && (lun->mode_pages.index[i].page_flags & 6667 CTL_PAGE_FLAG_DISK_ONLY)) 6668 continue; 6669 6670#if 0 6671 printf("found page %#x len %d\n", 6672 lun->mode_pages.index[i].page_code & 6673 SMPH_PC_MASK, 6674 lun->mode_pages.index[i].page_len); 6675#endif 6676 6677 page_len += lun->mode_pages.index[i].page_len; 6678 } 6679 6680 if (page_len == 0) { 6681 ctl_set_invalid_field(ctsio, 6682 /*sks_valid*/ 1, 6683 /*command*/ 1, 6684 /*field*/ 2, 6685 /*bit_valid*/ 1, 6686 /*bit*/ 5); 6687 ctl_done((union ctl_io *)ctsio); 6688 return (CTL_RETVAL_COMPLETE); 6689 } 6690 break; 6691 } 6692 } 6693 6694 total_len = header_len + page_len; 6695#if 0 6696 printf("header_len = %d, page_len = %d, total_len = %d\n", 6697 header_len, page_len, total_len); 6698#endif 6699 6700 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6701 ctsio->kern_sg_entries = 0; 6702 ctsio->kern_data_resid = 0; 6703 ctsio->kern_rel_offset = 0; 6704 if (total_len < alloc_len) { 6705 ctsio->residual = alloc_len - total_len; 6706 ctsio->kern_data_len = total_len; 6707 ctsio->kern_total_len = total_len; 6708 } else { 6709 ctsio->residual = 0; 6710 ctsio->kern_data_len = alloc_len; 6711 ctsio->kern_total_len = alloc_len; 6712 } 6713 6714 switch (ctsio->cdb[0]) { 6715 case MODE_SENSE_6: { 6716 struct scsi_mode_hdr_6 *header; 6717 6718 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6719 6720 header->datalen = ctl_min(total_len - 1, 254); 6721 if (control_dev == 0) { 6722 header->dev_specific = 0x10; /* DPOFUA */ 6723 if ((lun->flags & CTL_LUN_READONLY) || 6724 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6725 .eca_and_aen & SCP_SWP) != 0) 6726 header->dev_specific |= 0x80; /* WP */ 6727 } 6728 if (dbd) 6729 header->block_descr_len = 0; 6730 else 6731 header->block_descr_len = 6732 sizeof(struct scsi_mode_block_descr); 6733 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6734 break; 6735 } 6736 case MODE_SENSE_10: { 6737 struct scsi_mode_hdr_10 *header; 6738 int datalen; 6739 6740 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6741 6742 datalen = ctl_min(total_len - 2, 65533); 6743 scsi_ulto2b(datalen, header->datalen); 6744 if (control_dev == 0) { 6745 header->dev_specific = 0x10; /* DPOFUA */ 6746 if ((lun->flags & CTL_LUN_READONLY) || 6747 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6748 .eca_and_aen & SCP_SWP) != 0) 6749 header->dev_specific |= 0x80; /* WP */ 6750 } 6751 if (dbd) 6752 scsi_ulto2b(0, header->block_descr_len); 6753 else 6754 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6755 header->block_descr_len); 6756 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6757 break; 6758 } 6759 default: 6760 panic("invalid CDB type %#x", ctsio->cdb[0]); 6761 break; /* NOTREACHED */ 6762 } 6763 6764 /* 6765 * If we've got a disk, use its blocksize in the block 6766 * descriptor. Otherwise, just set it to 0. 6767 */ 6768 if (dbd == 0) { 6769 if (control_dev == 0) 6770 scsi_ulto3b(lun->be_lun->blocksize, 6771 block_desc->block_len); 6772 else 6773 scsi_ulto3b(0, block_desc->block_len); 6774 } 6775 6776 switch (page_code) { 6777 case SMS_ALL_PAGES_PAGE: { 6778 int i, data_used; 6779 6780 data_used = header_len; 6781 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6782 struct ctl_page_index *page_index; 6783 6784 page_index = &lun->mode_pages.index[i]; 6785 6786 if ((control_dev != 0) 6787 && (page_index->page_flags & 6788 CTL_PAGE_FLAG_DISK_ONLY)) 6789 continue; 6790 6791 /* 6792 * We don't use this subpage if the user didn't 6793 * request all subpages. We already checked (above) 6794 * to make sure the user only specified a subpage 6795 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6796 */ 6797 if ((page_index->subpage != 0) 6798 && (subpage == SMS_SUBPAGE_PAGE_0)) 6799 continue; 6800 6801 /* 6802 * Call the handler, if it exists, to update the 6803 * page to the latest values. 6804 */ 6805 if (page_index->sense_handler != NULL) 6806 page_index->sense_handler(ctsio, page_index,pc); 6807 6808 memcpy(ctsio->kern_data_ptr + data_used, 6809 page_index->page_data + 6810 (page_index->page_len * pc), 6811 page_index->page_len); 6812 data_used += page_index->page_len; 6813 } 6814 break; 6815 } 6816 default: { 6817 int i, data_used; 6818 6819 data_used = header_len; 6820 6821 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6822 struct ctl_page_index *page_index; 6823 6824 page_index = &lun->mode_pages.index[i]; 6825 6826 /* Look for the right page code */ 6827 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6828 continue; 6829 6830 /* Look for the right subpage or the subpage wildcard*/ 6831 if ((page_index->subpage != subpage) 6832 && (subpage != SMS_SUBPAGE_ALL)) 6833 continue; 6834 6835 /* Make sure the page is supported for this dev type */ 6836 if ((control_dev != 0) 6837 && (page_index->page_flags & 6838 CTL_PAGE_FLAG_DISK_ONLY)) 6839 continue; 6840 6841 /* 6842 * Call the handler, if it exists, to update the 6843 * page to the latest values. 6844 */ 6845 if (page_index->sense_handler != NULL) 6846 page_index->sense_handler(ctsio, page_index,pc); 6847 6848 memcpy(ctsio->kern_data_ptr + data_used, 6849 page_index->page_data + 6850 (page_index->page_len * pc), 6851 page_index->page_len); 6852 data_used += page_index->page_len; 6853 } 6854 break; 6855 } 6856 } 6857 6858 ctl_set_success(ctsio); 6859 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6860 ctsio->be_move_done = ctl_config_move_done; 6861 ctl_datamove((union ctl_io *)ctsio); 6862 return (CTL_RETVAL_COMPLETE); 6863} 6864 6865int 6866ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6867 struct ctl_page_index *page_index, 6868 int pc) 6869{ 6870 struct ctl_lun *lun; 6871 struct scsi_log_param_header *phdr; 6872 uint8_t *data; 6873 uint64_t val; 6874 6875 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6876 data = page_index->page_data; 6877 6878 if (lun->backend->lun_attr != NULL && 6879 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6880 != UINT64_MAX) { 6881 phdr = (struct scsi_log_param_header *)data; 6882 scsi_ulto2b(0x0001, phdr->param_code); 6883 phdr->param_control = SLP_LBIN | SLP_LP; 6884 phdr->param_len = 8; 6885 data = (uint8_t *)(phdr + 1); 6886 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6887 data[4] = 0x01; /* per-LUN */ 6888 data += phdr->param_len; 6889 } 6890 6891 if (lun->backend->lun_attr != NULL && 6892 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6893 != UINT64_MAX) { 6894 phdr = (struct scsi_log_param_header *)data; 6895 scsi_ulto2b(0x0002, phdr->param_code); 6896 phdr->param_control = SLP_LBIN | SLP_LP; 6897 phdr->param_len = 8; 6898 data = (uint8_t *)(phdr + 1); 6899 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6900 data[4] = 0x02; /* per-pool */ 6901 data += phdr->param_len; 6902 } 6903 6904 if (lun->backend->lun_attr != NULL && 6905 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6906 != UINT64_MAX) { 6907 phdr = (struct scsi_log_param_header *)data; 6908 scsi_ulto2b(0x00f1, phdr->param_code); 6909 phdr->param_control = SLP_LBIN | SLP_LP; 6910 phdr->param_len = 8; 6911 data = (uint8_t *)(phdr + 1); 6912 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6913 data[4] = 0x02; /* per-pool */ 6914 data += phdr->param_len; 6915 } 6916 6917 if (lun->backend->lun_attr != NULL && 6918 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6919 != UINT64_MAX) { 6920 phdr = (struct scsi_log_param_header *)data; 6921 scsi_ulto2b(0x00f2, phdr->param_code); 6922 phdr->param_control = SLP_LBIN | SLP_LP; 6923 phdr->param_len = 8; 6924 data = (uint8_t *)(phdr + 1); 6925 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6926 data[4] = 0x02; /* per-pool */ 6927 data += phdr->param_len; 6928 } 6929 6930 page_index->page_len = data - page_index->page_data; 6931 return (0); 6932} 6933 6934int 6935ctl_log_sense(struct ctl_scsiio *ctsio) 6936{ 6937 struct ctl_lun *lun; 6938 int i, pc, page_code, subpage; 6939 int alloc_len, total_len; 6940 struct ctl_page_index *page_index; 6941 struct scsi_log_sense *cdb; 6942 struct scsi_log_header *header; 6943 6944 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6945 6946 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6947 cdb = (struct scsi_log_sense *)ctsio->cdb; 6948 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6949 page_code = cdb->page & SLS_PAGE_CODE; 6950 subpage = cdb->subpage; 6951 alloc_len = scsi_2btoul(cdb->length); 6952 6953 page_index = NULL; 6954 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6955 page_index = &lun->log_pages.index[i]; 6956 6957 /* Look for the right page code */ 6958 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6959 continue; 6960 6961 /* Look for the right subpage or the subpage wildcard*/ 6962 if (page_index->subpage != subpage) 6963 continue; 6964 6965 break; 6966 } 6967 if (i >= CTL_NUM_LOG_PAGES) { 6968 ctl_set_invalid_field(ctsio, 6969 /*sks_valid*/ 1, 6970 /*command*/ 1, 6971 /*field*/ 2, 6972 /*bit_valid*/ 0, 6973 /*bit*/ 0); 6974 ctl_done((union ctl_io *)ctsio); 6975 return (CTL_RETVAL_COMPLETE); 6976 } 6977 6978 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6979 6980 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6981 ctsio->kern_sg_entries = 0; 6982 ctsio->kern_data_resid = 0; 6983 ctsio->kern_rel_offset = 0; 6984 if (total_len < alloc_len) { 6985 ctsio->residual = alloc_len - total_len; 6986 ctsio->kern_data_len = total_len; 6987 ctsio->kern_total_len = total_len; 6988 } else { 6989 ctsio->residual = 0; 6990 ctsio->kern_data_len = alloc_len; 6991 ctsio->kern_total_len = alloc_len; 6992 } 6993 6994 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6995 header->page = page_index->page_code; 6996 if (page_index->subpage) { 6997 header->page |= SL_SPF; 6998 header->subpage = page_index->subpage; 6999 } 7000 scsi_ulto2b(page_index->page_len, header->datalen); 7001 7002 /* 7003 * Call the handler, if it exists, to update the 7004 * page to the latest values. 7005 */ 7006 if (page_index->sense_handler != NULL) 7007 page_index->sense_handler(ctsio, page_index, pc); 7008 7009 memcpy(header + 1, page_index->page_data, page_index->page_len); 7010 7011 ctl_set_success(ctsio); 7012 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7013 ctsio->be_move_done = ctl_config_move_done; 7014 ctl_datamove((union ctl_io *)ctsio); 7015 return (CTL_RETVAL_COMPLETE); 7016} 7017 7018int 7019ctl_read_capacity(struct ctl_scsiio *ctsio) 7020{ 7021 struct scsi_read_capacity *cdb; 7022 struct scsi_read_capacity_data *data; 7023 struct ctl_lun *lun; 7024 uint32_t lba; 7025 7026 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7027 7028 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7029 7030 lba = scsi_4btoul(cdb->addr); 7031 if (((cdb->pmi & SRC_PMI) == 0) 7032 && (lba != 0)) { 7033 ctl_set_invalid_field(/*ctsio*/ ctsio, 7034 /*sks_valid*/ 1, 7035 /*command*/ 1, 7036 /*field*/ 2, 7037 /*bit_valid*/ 0, 7038 /*bit*/ 0); 7039 ctl_done((union ctl_io *)ctsio); 7040 return (CTL_RETVAL_COMPLETE); 7041 } 7042 7043 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7044 7045 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7046 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7047 ctsio->residual = 0; 7048 ctsio->kern_data_len = sizeof(*data); 7049 ctsio->kern_total_len = sizeof(*data); 7050 ctsio->kern_data_resid = 0; 7051 ctsio->kern_rel_offset = 0; 7052 ctsio->kern_sg_entries = 0; 7053 7054 /* 7055 * If the maximum LBA is greater than 0xfffffffe, the user must 7056 * issue a SERVICE ACTION IN (16) command, with the read capacity 7057 * serivce action set. 7058 */ 7059 if (lun->be_lun->maxlba > 0xfffffffe) 7060 scsi_ulto4b(0xffffffff, data->addr); 7061 else 7062 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7063 7064 /* 7065 * XXX KDM this may not be 512 bytes... 7066 */ 7067 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7068 7069 ctl_set_success(ctsio); 7070 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7071 ctsio->be_move_done = ctl_config_move_done; 7072 ctl_datamove((union ctl_io *)ctsio); 7073 return (CTL_RETVAL_COMPLETE); 7074} 7075 7076int 7077ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7078{ 7079 struct scsi_read_capacity_16 *cdb; 7080 struct scsi_read_capacity_data_long *data; 7081 struct ctl_lun *lun; 7082 uint64_t lba; 7083 uint32_t alloc_len; 7084 7085 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7086 7087 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7088 7089 alloc_len = scsi_4btoul(cdb->alloc_len); 7090 lba = scsi_8btou64(cdb->addr); 7091 7092 if ((cdb->reladr & SRC16_PMI) 7093 && (lba != 0)) { 7094 ctl_set_invalid_field(/*ctsio*/ ctsio, 7095 /*sks_valid*/ 1, 7096 /*command*/ 1, 7097 /*field*/ 2, 7098 /*bit_valid*/ 0, 7099 /*bit*/ 0); 7100 ctl_done((union ctl_io *)ctsio); 7101 return (CTL_RETVAL_COMPLETE); 7102 } 7103 7104 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7105 7106 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7107 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7108 7109 if (sizeof(*data) < alloc_len) { 7110 ctsio->residual = alloc_len - sizeof(*data); 7111 ctsio->kern_data_len = sizeof(*data); 7112 ctsio->kern_total_len = sizeof(*data); 7113 } else { 7114 ctsio->residual = 0; 7115 ctsio->kern_data_len = alloc_len; 7116 ctsio->kern_total_len = alloc_len; 7117 } 7118 ctsio->kern_data_resid = 0; 7119 ctsio->kern_rel_offset = 0; 7120 ctsio->kern_sg_entries = 0; 7121 7122 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7123 /* XXX KDM this may not be 512 bytes... */ 7124 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7125 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7126 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7127 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7128 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7129 7130 ctl_set_success(ctsio); 7131 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7132 ctsio->be_move_done = ctl_config_move_done; 7133 ctl_datamove((union ctl_io *)ctsio); 7134 return (CTL_RETVAL_COMPLETE); 7135} 7136 7137int 7138ctl_read_defect(struct ctl_scsiio *ctsio) 7139{ 7140 struct scsi_read_defect_data_10 *ccb10; 7141 struct scsi_read_defect_data_12 *ccb12; 7142 struct scsi_read_defect_data_hdr_10 *data10; 7143 struct scsi_read_defect_data_hdr_12 *data12; 7144 uint32_t alloc_len, data_len; 7145 uint8_t format; 7146 7147 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7148 7149 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7150 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7151 format = ccb10->format; 7152 alloc_len = scsi_2btoul(ccb10->alloc_length); 7153 data_len = sizeof(*data10); 7154 } else { 7155 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7156 format = ccb12->format; 7157 alloc_len = scsi_4btoul(ccb12->alloc_length); 7158 data_len = sizeof(*data12); 7159 } 7160 if (alloc_len == 0) { 7161 ctl_set_success(ctsio); 7162 ctl_done((union ctl_io *)ctsio); 7163 return (CTL_RETVAL_COMPLETE); 7164 } 7165 7166 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7167 if (data_len < alloc_len) { 7168 ctsio->residual = alloc_len - data_len; 7169 ctsio->kern_data_len = data_len; 7170 ctsio->kern_total_len = data_len; 7171 } else { 7172 ctsio->residual = 0; 7173 ctsio->kern_data_len = alloc_len; 7174 ctsio->kern_total_len = alloc_len; 7175 } 7176 ctsio->kern_data_resid = 0; 7177 ctsio->kern_rel_offset = 0; 7178 ctsio->kern_sg_entries = 0; 7179 7180 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7181 data10 = (struct scsi_read_defect_data_hdr_10 *) 7182 ctsio->kern_data_ptr; 7183 data10->format = format; 7184 scsi_ulto2b(0, data10->length); 7185 } else { 7186 data12 = (struct scsi_read_defect_data_hdr_12 *) 7187 ctsio->kern_data_ptr; 7188 data12->format = format; 7189 scsi_ulto2b(0, data12->generation); 7190 scsi_ulto4b(0, data12->length); 7191 } 7192 7193 ctl_set_success(ctsio); 7194 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7195 ctsio->be_move_done = ctl_config_move_done; 7196 ctl_datamove((union ctl_io *)ctsio); 7197 return (CTL_RETVAL_COMPLETE); 7198} 7199 7200int 7201ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7202{ 7203 struct scsi_maintenance_in *cdb; 7204 int retval; 7205 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7206 int num_target_port_groups, num_target_ports; 7207 struct ctl_lun *lun; 7208 struct ctl_softc *softc; 7209 struct ctl_port *port; 7210 struct scsi_target_group_data *rtg_ptr; 7211 struct scsi_target_group_data_extended *rtg_ext_ptr; 7212 struct scsi_target_port_group_descriptor *tpg_desc; 7213 7214 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7215 7216 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7217 softc = control_softc; 7218 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7219 7220 retval = CTL_RETVAL_COMPLETE; 7221 7222 switch (cdb->byte2 & STG_PDF_MASK) { 7223 case STG_PDF_LENGTH: 7224 ext = 0; 7225 break; 7226 case STG_PDF_EXTENDED: 7227 ext = 1; 7228 break; 7229 default: 7230 ctl_set_invalid_field(/*ctsio*/ ctsio, 7231 /*sks_valid*/ 1, 7232 /*command*/ 1, 7233 /*field*/ 2, 7234 /*bit_valid*/ 1, 7235 /*bit*/ 5); 7236 ctl_done((union ctl_io *)ctsio); 7237 return(retval); 7238 } 7239 7240 if (softc->is_single) 7241 num_target_port_groups = 1; 7242 else 7243 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7244 num_target_ports = 0; 7245 mtx_lock(&softc->ctl_lock); 7246 STAILQ_FOREACH(port, &softc->port_list, links) { 7247 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7248 continue; 7249 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7250 continue; 7251 num_target_ports++; 7252 } 7253 mtx_unlock(&softc->ctl_lock); 7254 7255 if (ext) 7256 total_len = sizeof(struct scsi_target_group_data_extended); 7257 else 7258 total_len = sizeof(struct scsi_target_group_data); 7259 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7260 num_target_port_groups + 7261 sizeof(struct scsi_target_port_descriptor) * 7262 num_target_ports * num_target_port_groups; 7263 7264 alloc_len = scsi_4btoul(cdb->length); 7265 7266 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7267 7268 ctsio->kern_sg_entries = 0; 7269 7270 if (total_len < alloc_len) { 7271 ctsio->residual = alloc_len - total_len; 7272 ctsio->kern_data_len = total_len; 7273 ctsio->kern_total_len = total_len; 7274 } else { 7275 ctsio->residual = 0; 7276 ctsio->kern_data_len = alloc_len; 7277 ctsio->kern_total_len = alloc_len; 7278 } 7279 ctsio->kern_data_resid = 0; 7280 ctsio->kern_rel_offset = 0; 7281 7282 if (ext) { 7283 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7284 ctsio->kern_data_ptr; 7285 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7286 rtg_ext_ptr->format_type = 0x10; 7287 rtg_ext_ptr->implicit_transition_time = 0; 7288 tpg_desc = &rtg_ext_ptr->groups[0]; 7289 } else { 7290 rtg_ptr = (struct scsi_target_group_data *) 7291 ctsio->kern_data_ptr; 7292 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7293 tpg_desc = &rtg_ptr->groups[0]; 7294 } 7295 7296 mtx_lock(&softc->ctl_lock); 7297 pg = softc->port_offset / CTL_MAX_PORTS; 7298 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7299 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7300 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7301 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7302 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7303 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7304 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7305 } else { 7306 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7307 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7308 } 7309 } else { 7310 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7311 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7312 } 7313 for (g = 0; g < num_target_port_groups; g++) { 7314 tpg_desc->pref_state = (g == pg) ? gs : os; 7315 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7316 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7317 tpg_desc->status = TPG_IMPLICIT; 7318 pc = 0; 7319 STAILQ_FOREACH(port, &softc->port_list, links) { 7320 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7321 continue; 7322 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7323 CTL_MAX_LUNS) 7324 continue; 7325 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7326 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7327 relative_target_port_identifier); 7328 pc++; 7329 } 7330 tpg_desc->target_port_count = pc; 7331 tpg_desc = (struct scsi_target_port_group_descriptor *) 7332 &tpg_desc->descriptors[pc]; 7333 } 7334 mtx_unlock(&softc->ctl_lock); 7335 7336 ctl_set_success(ctsio); 7337 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7338 ctsio->be_move_done = ctl_config_move_done; 7339 ctl_datamove((union ctl_io *)ctsio); 7340 return(retval); 7341} 7342 7343int 7344ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7345{ 7346 struct ctl_lun *lun; 7347 struct scsi_report_supported_opcodes *cdb; 7348 const struct ctl_cmd_entry *entry, *sentry; 7349 struct scsi_report_supported_opcodes_all *all; 7350 struct scsi_report_supported_opcodes_descr *descr; 7351 struct scsi_report_supported_opcodes_one *one; 7352 int retval; 7353 int alloc_len, total_len; 7354 int opcode, service_action, i, j, num; 7355 7356 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7357 7358 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7359 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7360 7361 retval = CTL_RETVAL_COMPLETE; 7362 7363 opcode = cdb->requested_opcode; 7364 service_action = scsi_2btoul(cdb->requested_service_action); 7365 switch (cdb->options & RSO_OPTIONS_MASK) { 7366 case RSO_OPTIONS_ALL: 7367 num = 0; 7368 for (i = 0; i < 256; i++) { 7369 entry = &ctl_cmd_table[i]; 7370 if (entry->flags & CTL_CMD_FLAG_SA5) { 7371 for (j = 0; j < 32; j++) { 7372 sentry = &((const struct ctl_cmd_entry *) 7373 entry->execute)[j]; 7374 if (ctl_cmd_applicable( 7375 lun->be_lun->lun_type, sentry)) 7376 num++; 7377 } 7378 } else { 7379 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7380 entry)) 7381 num++; 7382 } 7383 } 7384 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7385 num * sizeof(struct scsi_report_supported_opcodes_descr); 7386 break; 7387 case RSO_OPTIONS_OC: 7388 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7389 ctl_set_invalid_field(/*ctsio*/ ctsio, 7390 /*sks_valid*/ 1, 7391 /*command*/ 1, 7392 /*field*/ 2, 7393 /*bit_valid*/ 1, 7394 /*bit*/ 2); 7395 ctl_done((union ctl_io *)ctsio); 7396 return (CTL_RETVAL_COMPLETE); 7397 } 7398 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7399 break; 7400 case RSO_OPTIONS_OC_SA: 7401 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7402 service_action >= 32) { 7403 ctl_set_invalid_field(/*ctsio*/ ctsio, 7404 /*sks_valid*/ 1, 7405 /*command*/ 1, 7406 /*field*/ 2, 7407 /*bit_valid*/ 1, 7408 /*bit*/ 2); 7409 ctl_done((union ctl_io *)ctsio); 7410 return (CTL_RETVAL_COMPLETE); 7411 } 7412 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7413 break; 7414 default: 7415 ctl_set_invalid_field(/*ctsio*/ ctsio, 7416 /*sks_valid*/ 1, 7417 /*command*/ 1, 7418 /*field*/ 2, 7419 /*bit_valid*/ 1, 7420 /*bit*/ 2); 7421 ctl_done((union ctl_io *)ctsio); 7422 return (CTL_RETVAL_COMPLETE); 7423 } 7424 7425 alloc_len = scsi_4btoul(cdb->length); 7426 7427 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7428 7429 ctsio->kern_sg_entries = 0; 7430 7431 if (total_len < alloc_len) { 7432 ctsio->residual = alloc_len - total_len; 7433 ctsio->kern_data_len = total_len; 7434 ctsio->kern_total_len = total_len; 7435 } else { 7436 ctsio->residual = 0; 7437 ctsio->kern_data_len = alloc_len; 7438 ctsio->kern_total_len = alloc_len; 7439 } 7440 ctsio->kern_data_resid = 0; 7441 ctsio->kern_rel_offset = 0; 7442 7443 switch (cdb->options & RSO_OPTIONS_MASK) { 7444 case RSO_OPTIONS_ALL: 7445 all = (struct scsi_report_supported_opcodes_all *) 7446 ctsio->kern_data_ptr; 7447 num = 0; 7448 for (i = 0; i < 256; i++) { 7449 entry = &ctl_cmd_table[i]; 7450 if (entry->flags & CTL_CMD_FLAG_SA5) { 7451 for (j = 0; j < 32; j++) { 7452 sentry = &((const struct ctl_cmd_entry *) 7453 entry->execute)[j]; 7454 if (!ctl_cmd_applicable( 7455 lun->be_lun->lun_type, sentry)) 7456 continue; 7457 descr = &all->descr[num++]; 7458 descr->opcode = i; 7459 scsi_ulto2b(j, descr->service_action); 7460 descr->flags = RSO_SERVACTV; 7461 scsi_ulto2b(sentry->length, 7462 descr->cdb_length); 7463 } 7464 } else { 7465 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7466 entry)) 7467 continue; 7468 descr = &all->descr[num++]; 7469 descr->opcode = i; 7470 scsi_ulto2b(0, descr->service_action); 7471 descr->flags = 0; 7472 scsi_ulto2b(entry->length, descr->cdb_length); 7473 } 7474 } 7475 scsi_ulto4b( 7476 num * sizeof(struct scsi_report_supported_opcodes_descr), 7477 all->length); 7478 break; 7479 case RSO_OPTIONS_OC: 7480 one = (struct scsi_report_supported_opcodes_one *) 7481 ctsio->kern_data_ptr; 7482 entry = &ctl_cmd_table[opcode]; 7483 goto fill_one; 7484 case RSO_OPTIONS_OC_SA: 7485 one = (struct scsi_report_supported_opcodes_one *) 7486 ctsio->kern_data_ptr; 7487 entry = &ctl_cmd_table[opcode]; 7488 entry = &((const struct ctl_cmd_entry *) 7489 entry->execute)[service_action]; 7490fill_one: 7491 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7492 one->support = 3; 7493 scsi_ulto2b(entry->length, one->cdb_length); 7494 one->cdb_usage[0] = opcode; 7495 memcpy(&one->cdb_usage[1], entry->usage, 7496 entry->length - 1); 7497 } else 7498 one->support = 1; 7499 break; 7500 } 7501 7502 ctl_set_success(ctsio); 7503 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7504 ctsio->be_move_done = ctl_config_move_done; 7505 ctl_datamove((union ctl_io *)ctsio); 7506 return(retval); 7507} 7508 7509int 7510ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7511{ 7512 struct scsi_report_supported_tmf *cdb; 7513 struct scsi_report_supported_tmf_data *data; 7514 int retval; 7515 int alloc_len, total_len; 7516 7517 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7518 7519 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7520 7521 retval = CTL_RETVAL_COMPLETE; 7522 7523 total_len = sizeof(struct scsi_report_supported_tmf_data); 7524 alloc_len = scsi_4btoul(cdb->length); 7525 7526 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7527 7528 ctsio->kern_sg_entries = 0; 7529 7530 if (total_len < alloc_len) { 7531 ctsio->residual = alloc_len - total_len; 7532 ctsio->kern_data_len = total_len; 7533 ctsio->kern_total_len = total_len; 7534 } else { 7535 ctsio->residual = 0; 7536 ctsio->kern_data_len = alloc_len; 7537 ctsio->kern_total_len = alloc_len; 7538 } 7539 ctsio->kern_data_resid = 0; 7540 ctsio->kern_rel_offset = 0; 7541 7542 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7543 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7544 data->byte2 |= RST_ITNRS; 7545 7546 ctl_set_success(ctsio); 7547 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7548 ctsio->be_move_done = ctl_config_move_done; 7549 ctl_datamove((union ctl_io *)ctsio); 7550 return (retval); 7551} 7552 7553int 7554ctl_report_timestamp(struct ctl_scsiio *ctsio) 7555{ 7556 struct scsi_report_timestamp *cdb; 7557 struct scsi_report_timestamp_data *data; 7558 struct timeval tv; 7559 int64_t timestamp; 7560 int retval; 7561 int alloc_len, total_len; 7562 7563 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7564 7565 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7566 7567 retval = CTL_RETVAL_COMPLETE; 7568 7569 total_len = sizeof(struct scsi_report_timestamp_data); 7570 alloc_len = scsi_4btoul(cdb->length); 7571 7572 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7573 7574 ctsio->kern_sg_entries = 0; 7575 7576 if (total_len < alloc_len) { 7577 ctsio->residual = alloc_len - total_len; 7578 ctsio->kern_data_len = total_len; 7579 ctsio->kern_total_len = total_len; 7580 } else { 7581 ctsio->residual = 0; 7582 ctsio->kern_data_len = alloc_len; 7583 ctsio->kern_total_len = alloc_len; 7584 } 7585 ctsio->kern_data_resid = 0; 7586 ctsio->kern_rel_offset = 0; 7587 7588 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7589 scsi_ulto2b(sizeof(*data) - 2, data->length); 7590 data->origin = RTS_ORIG_OUTSIDE; 7591 getmicrotime(&tv); 7592 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7593 scsi_ulto4b(timestamp >> 16, data->timestamp); 7594 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7595 7596 ctl_set_success(ctsio); 7597 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7598 ctsio->be_move_done = ctl_config_move_done; 7599 ctl_datamove((union ctl_io *)ctsio); 7600 return (retval); 7601} 7602 7603int 7604ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7605{ 7606 struct scsi_per_res_in *cdb; 7607 int alloc_len, total_len = 0; 7608 /* struct scsi_per_res_in_rsrv in_data; */ 7609 struct ctl_lun *lun; 7610 struct ctl_softc *softc; 7611 7612 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7613 7614 softc = control_softc; 7615 7616 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7617 7618 alloc_len = scsi_2btoul(cdb->length); 7619 7620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7621 7622retry: 7623 mtx_lock(&lun->lun_lock); 7624 switch (cdb->action) { 7625 case SPRI_RK: /* read keys */ 7626 total_len = sizeof(struct scsi_per_res_in_keys) + 7627 lun->pr_key_count * 7628 sizeof(struct scsi_per_res_key); 7629 break; 7630 case SPRI_RR: /* read reservation */ 7631 if (lun->flags & CTL_LUN_PR_RESERVED) 7632 total_len = sizeof(struct scsi_per_res_in_rsrv); 7633 else 7634 total_len = sizeof(struct scsi_per_res_in_header); 7635 break; 7636 case SPRI_RC: /* report capabilities */ 7637 total_len = sizeof(struct scsi_per_res_cap); 7638 break; 7639 case SPRI_RS: /* read full status */ 7640 total_len = sizeof(struct scsi_per_res_in_header) + 7641 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7642 lun->pr_key_count; 7643 break; 7644 default: 7645 panic("Invalid PR type %x", cdb->action); 7646 } 7647 mtx_unlock(&lun->lun_lock); 7648 7649 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7650 7651 if (total_len < alloc_len) { 7652 ctsio->residual = alloc_len - total_len; 7653 ctsio->kern_data_len = total_len; 7654 ctsio->kern_total_len = total_len; 7655 } else { 7656 ctsio->residual = 0; 7657 ctsio->kern_data_len = alloc_len; 7658 ctsio->kern_total_len = alloc_len; 7659 } 7660 7661 ctsio->kern_data_resid = 0; 7662 ctsio->kern_rel_offset = 0; 7663 ctsio->kern_sg_entries = 0; 7664 7665 mtx_lock(&lun->lun_lock); 7666 switch (cdb->action) { 7667 case SPRI_RK: { // read keys 7668 struct scsi_per_res_in_keys *res_keys; 7669 int i, key_count; 7670 7671 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7672 7673 /* 7674 * We had to drop the lock to allocate our buffer, which 7675 * leaves time for someone to come in with another 7676 * persistent reservation. (That is unlikely, though, 7677 * since this should be the only persistent reservation 7678 * command active right now.) 7679 */ 7680 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7681 (lun->pr_key_count * 7682 sizeof(struct scsi_per_res_key)))){ 7683 mtx_unlock(&lun->lun_lock); 7684 free(ctsio->kern_data_ptr, M_CTL); 7685 printf("%s: reservation length changed, retrying\n", 7686 __func__); 7687 goto retry; 7688 } 7689 7690 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7691 7692 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7693 lun->pr_key_count, res_keys->header.length); 7694 7695 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7696 if (lun->pr_keys[i] == 0) 7697 continue; 7698 7699 /* 7700 * We used lun->pr_key_count to calculate the 7701 * size to allocate. If it turns out the number of 7702 * initiators with the registered flag set is 7703 * larger than that (i.e. they haven't been kept in 7704 * sync), we've got a problem. 7705 */ 7706 if (key_count >= lun->pr_key_count) { 7707#ifdef NEEDTOPORT 7708 csevent_log(CSC_CTL | CSC_SHELF_SW | 7709 CTL_PR_ERROR, 7710 csevent_LogType_Fault, 7711 csevent_AlertLevel_Yellow, 7712 csevent_FRU_ShelfController, 7713 csevent_FRU_Firmware, 7714 csevent_FRU_Unknown, 7715 "registered keys %d >= key " 7716 "count %d", key_count, 7717 lun->pr_key_count); 7718#endif 7719 key_count++; 7720 continue; 7721 } 7722 scsi_u64to8b(lun->pr_keys[i], 7723 res_keys->keys[key_count].key); 7724 key_count++; 7725 } 7726 break; 7727 } 7728 case SPRI_RR: { // read reservation 7729 struct scsi_per_res_in_rsrv *res; 7730 int tmp_len, header_only; 7731 7732 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7733 7734 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7735 7736 if (lun->flags & CTL_LUN_PR_RESERVED) 7737 { 7738 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7739 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7740 res->header.length); 7741 header_only = 0; 7742 } else { 7743 tmp_len = sizeof(struct scsi_per_res_in_header); 7744 scsi_ulto4b(0, res->header.length); 7745 header_only = 1; 7746 } 7747 7748 /* 7749 * We had to drop the lock to allocate our buffer, which 7750 * leaves time for someone to come in with another 7751 * persistent reservation. (That is unlikely, though, 7752 * since this should be the only persistent reservation 7753 * command active right now.) 7754 */ 7755 if (tmp_len != total_len) { 7756 mtx_unlock(&lun->lun_lock); 7757 free(ctsio->kern_data_ptr, M_CTL); 7758 printf("%s: reservation status changed, retrying\n", 7759 __func__); 7760 goto retry; 7761 } 7762 7763 /* 7764 * No reservation held, so we're done. 7765 */ 7766 if (header_only != 0) 7767 break; 7768 7769 /* 7770 * If the registration is an All Registrants type, the key 7771 * is 0, since it doesn't really matter. 7772 */ 7773 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7774 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 7775 res->data.reservation); 7776 } 7777 res->data.scopetype = lun->res_type; 7778 break; 7779 } 7780 case SPRI_RC: //report capabilities 7781 { 7782 struct scsi_per_res_cap *res_cap; 7783 uint16_t type_mask; 7784 7785 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7786 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7787 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7788 type_mask = SPRI_TM_WR_EX_AR | 7789 SPRI_TM_EX_AC_RO | 7790 SPRI_TM_WR_EX_RO | 7791 SPRI_TM_EX_AC | 7792 SPRI_TM_WR_EX | 7793 SPRI_TM_EX_AC_AR; 7794 scsi_ulto2b(type_mask, res_cap->type_mask); 7795 break; 7796 } 7797 case SPRI_RS: { // read full status 7798 struct scsi_per_res_in_full *res_status; 7799 struct scsi_per_res_in_full_desc *res_desc; 7800 struct ctl_port *port; 7801 int i, len; 7802 7803 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7804 7805 /* 7806 * We had to drop the lock to allocate our buffer, which 7807 * leaves time for someone to come in with another 7808 * persistent reservation. (That is unlikely, though, 7809 * since this should be the only persistent reservation 7810 * command active right now.) 7811 */ 7812 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7813 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7814 lun->pr_key_count)){ 7815 mtx_unlock(&lun->lun_lock); 7816 free(ctsio->kern_data_ptr, M_CTL); 7817 printf("%s: reservation length changed, retrying\n", 7818 __func__); 7819 goto retry; 7820 } 7821 7822 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7823 7824 res_desc = &res_status->desc[0]; 7825 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7826 if (lun->pr_keys[i] == 0) 7827 continue; 7828 7829 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 7830 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7831 (lun->pr_res_idx == i || 7832 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7833 res_desc->flags = SPRI_FULL_R_HOLDER; 7834 res_desc->scopetype = lun->res_type; 7835 } 7836 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7837 res_desc->rel_trgt_port_id); 7838 len = 0; 7839 port = softc->ctl_ports[ 7840 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7841 if (port != NULL) 7842 len = ctl_create_iid(port, 7843 i % CTL_MAX_INIT_PER_PORT, 7844 res_desc->transport_id); 7845 scsi_ulto4b(len, res_desc->additional_length); 7846 res_desc = (struct scsi_per_res_in_full_desc *) 7847 &res_desc->transport_id[len]; 7848 } 7849 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7850 res_status->header.length); 7851 break; 7852 } 7853 default: 7854 /* 7855 * This is a bug, because we just checked for this above, 7856 * and should have returned an error. 7857 */ 7858 panic("Invalid PR type %x", cdb->action); 7859 break; /* NOTREACHED */ 7860 } 7861 mtx_unlock(&lun->lun_lock); 7862 7863 ctl_set_success(ctsio); 7864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7865 ctsio->be_move_done = ctl_config_move_done; 7866 ctl_datamove((union ctl_io *)ctsio); 7867 return (CTL_RETVAL_COMPLETE); 7868} 7869 7870static void 7871ctl_set_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 7872{ 7873 7874 if (residx >= persis_offset && 7875 residx < persis_offset + CTL_MAX_INITIATORS) 7876 lun->pending_ua[residx - persis_offset] |= ua; 7877} 7878 7879/* 7880 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7881 * it should return. 7882 */ 7883static int 7884ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7885 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7886 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7887 struct scsi_per_res_out_parms* param) 7888{ 7889 union ctl_ha_msg persis_io; 7890 int retval, i; 7891 int isc_retval; 7892 7893 retval = 0; 7894 7895 mtx_lock(&lun->lun_lock); 7896 if (sa_res_key == 0) { 7897 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7898 /* validate scope and type */ 7899 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7900 SPR_LU_SCOPE) { 7901 mtx_unlock(&lun->lun_lock); 7902 ctl_set_invalid_field(/*ctsio*/ ctsio, 7903 /*sks_valid*/ 1, 7904 /*command*/ 1, 7905 /*field*/ 2, 7906 /*bit_valid*/ 1, 7907 /*bit*/ 4); 7908 ctl_done((union ctl_io *)ctsio); 7909 return (1); 7910 } 7911 7912 if (type>8 || type==2 || type==4 || type==0) { 7913 mtx_unlock(&lun->lun_lock); 7914 ctl_set_invalid_field(/*ctsio*/ ctsio, 7915 /*sks_valid*/ 1, 7916 /*command*/ 1, 7917 /*field*/ 2, 7918 /*bit_valid*/ 1, 7919 /*bit*/ 0); 7920 ctl_done((union ctl_io *)ctsio); 7921 return (1); 7922 } 7923 7924 /* 7925 * Unregister everybody else and build UA for 7926 * them 7927 */ 7928 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7929 if (i == residx || lun->pr_keys[i] == 0) 7930 continue; 7931 7932 lun->pr_keys[i] = 0; 7933 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7934 } 7935 lun->pr_key_count = 1; 7936 lun->res_type = type; 7937 if (lun->res_type != SPR_TYPE_WR_EX_AR 7938 && lun->res_type != SPR_TYPE_EX_AC_AR) 7939 lun->pr_res_idx = residx; 7940 7941 /* send msg to other side */ 7942 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7943 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7944 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7945 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7946 persis_io.pr.pr_info.res_type = type; 7947 memcpy(persis_io.pr.pr_info.sa_res_key, 7948 param->serv_act_res_key, 7949 sizeof(param->serv_act_res_key)); 7950 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7951 &persis_io, sizeof(persis_io), 0)) > 7952 CTL_HA_STATUS_SUCCESS) { 7953 printf("CTL:Persis Out error returned " 7954 "from ctl_ha_msg_send %d\n", 7955 isc_retval); 7956 } 7957 } else { 7958 /* not all registrants */ 7959 mtx_unlock(&lun->lun_lock); 7960 free(ctsio->kern_data_ptr, M_CTL); 7961 ctl_set_invalid_field(ctsio, 7962 /*sks_valid*/ 1, 7963 /*command*/ 0, 7964 /*field*/ 8, 7965 /*bit_valid*/ 0, 7966 /*bit*/ 0); 7967 ctl_done((union ctl_io *)ctsio); 7968 return (1); 7969 } 7970 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7971 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7972 int found = 0; 7973 7974 if (res_key == sa_res_key) { 7975 /* special case */ 7976 /* 7977 * The spec implies this is not good but doesn't 7978 * say what to do. There are two choices either 7979 * generate a res conflict or check condition 7980 * with illegal field in parameter data. Since 7981 * that is what is done when the sa_res_key is 7982 * zero I'll take that approach since this has 7983 * to do with the sa_res_key. 7984 */ 7985 mtx_unlock(&lun->lun_lock); 7986 free(ctsio->kern_data_ptr, M_CTL); 7987 ctl_set_invalid_field(ctsio, 7988 /*sks_valid*/ 1, 7989 /*command*/ 0, 7990 /*field*/ 8, 7991 /*bit_valid*/ 0, 7992 /*bit*/ 0); 7993 ctl_done((union ctl_io *)ctsio); 7994 return (1); 7995 } 7996 7997 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7998 if (lun->pr_keys[i] != sa_res_key) 7999 continue; 8000 8001 found = 1; 8002 lun->pr_keys[i] = 0; 8003 lun->pr_key_count--; 8004 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8005 } 8006 if (!found) { 8007 mtx_unlock(&lun->lun_lock); 8008 free(ctsio->kern_data_ptr, M_CTL); 8009 ctl_set_reservation_conflict(ctsio); 8010 ctl_done((union ctl_io *)ctsio); 8011 return (CTL_RETVAL_COMPLETE); 8012 } 8013 /* send msg to other side */ 8014 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8015 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8016 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8017 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8018 persis_io.pr.pr_info.res_type = type; 8019 memcpy(persis_io.pr.pr_info.sa_res_key, 8020 param->serv_act_res_key, 8021 sizeof(param->serv_act_res_key)); 8022 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8023 &persis_io, sizeof(persis_io), 0)) > 8024 CTL_HA_STATUS_SUCCESS) { 8025 printf("CTL:Persis Out error returned from " 8026 "ctl_ha_msg_send %d\n", isc_retval); 8027 } 8028 } else { 8029 /* Reserved but not all registrants */ 8030 /* sa_res_key is res holder */ 8031 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8032 /* validate scope and type */ 8033 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8034 SPR_LU_SCOPE) { 8035 mtx_unlock(&lun->lun_lock); 8036 ctl_set_invalid_field(/*ctsio*/ ctsio, 8037 /*sks_valid*/ 1, 8038 /*command*/ 1, 8039 /*field*/ 2, 8040 /*bit_valid*/ 1, 8041 /*bit*/ 4); 8042 ctl_done((union ctl_io *)ctsio); 8043 return (1); 8044 } 8045 8046 if (type>8 || type==2 || type==4 || type==0) { 8047 mtx_unlock(&lun->lun_lock); 8048 ctl_set_invalid_field(/*ctsio*/ ctsio, 8049 /*sks_valid*/ 1, 8050 /*command*/ 1, 8051 /*field*/ 2, 8052 /*bit_valid*/ 1, 8053 /*bit*/ 0); 8054 ctl_done((union ctl_io *)ctsio); 8055 return (1); 8056 } 8057 8058 /* 8059 * Do the following: 8060 * if sa_res_key != res_key remove all 8061 * registrants w/sa_res_key and generate UA 8062 * for these registrants(Registrations 8063 * Preempted) if it wasn't an exclusive 8064 * reservation generate UA(Reservations 8065 * Preempted) for all other registered nexuses 8066 * if the type has changed. Establish the new 8067 * reservation and holder. If res_key and 8068 * sa_res_key are the same do the above 8069 * except don't unregister the res holder. 8070 */ 8071 8072 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8073 if (i == residx || lun->pr_keys[i] == 0) 8074 continue; 8075 8076 if (sa_res_key == lun->pr_keys[i]) { 8077 lun->pr_keys[i] = 0; 8078 lun->pr_key_count--; 8079 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8080 } else if (type != lun->res_type 8081 && (lun->res_type == SPR_TYPE_WR_EX_RO 8082 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8083 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE); 8084 } 8085 } 8086 lun->res_type = type; 8087 if (lun->res_type != SPR_TYPE_WR_EX_AR 8088 && lun->res_type != SPR_TYPE_EX_AC_AR) 8089 lun->pr_res_idx = residx; 8090 else 8091 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8092 8093 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8094 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8095 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8096 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8097 persis_io.pr.pr_info.res_type = type; 8098 memcpy(persis_io.pr.pr_info.sa_res_key, 8099 param->serv_act_res_key, 8100 sizeof(param->serv_act_res_key)); 8101 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8102 &persis_io, sizeof(persis_io), 0)) > 8103 CTL_HA_STATUS_SUCCESS) { 8104 printf("CTL:Persis Out error returned " 8105 "from ctl_ha_msg_send %d\n", 8106 isc_retval); 8107 } 8108 } else { 8109 /* 8110 * sa_res_key is not the res holder just 8111 * remove registrants 8112 */ 8113 int found=0; 8114 8115 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8116 if (sa_res_key != lun->pr_keys[i]) 8117 continue; 8118 8119 found = 1; 8120 lun->pr_keys[i] = 0; 8121 lun->pr_key_count--; 8122 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8123 } 8124 8125 if (!found) { 8126 mtx_unlock(&lun->lun_lock); 8127 free(ctsio->kern_data_ptr, M_CTL); 8128 ctl_set_reservation_conflict(ctsio); 8129 ctl_done((union ctl_io *)ctsio); 8130 return (1); 8131 } 8132 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8133 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8134 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8135 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8136 persis_io.pr.pr_info.res_type = type; 8137 memcpy(persis_io.pr.pr_info.sa_res_key, 8138 param->serv_act_res_key, 8139 sizeof(param->serv_act_res_key)); 8140 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8141 &persis_io, sizeof(persis_io), 0)) > 8142 CTL_HA_STATUS_SUCCESS) { 8143 printf("CTL:Persis Out error returned " 8144 "from ctl_ha_msg_send %d\n", 8145 isc_retval); 8146 } 8147 } 8148 } 8149 8150 lun->PRGeneration++; 8151 mtx_unlock(&lun->lun_lock); 8152 8153 return (retval); 8154} 8155 8156static void 8157ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8158{ 8159 uint64_t sa_res_key; 8160 int i; 8161 8162 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8163 8164 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8165 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8166 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8167 if (sa_res_key == 0) { 8168 /* 8169 * Unregister everybody else and build UA for 8170 * them 8171 */ 8172 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8173 if (i == msg->pr.pr_info.residx || 8174 lun->pr_keys[i] == 0) 8175 continue; 8176 8177 lun->pr_keys[i] = 0; 8178 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8179 } 8180 8181 lun->pr_key_count = 1; 8182 lun->res_type = msg->pr.pr_info.res_type; 8183 if (lun->res_type != SPR_TYPE_WR_EX_AR 8184 && lun->res_type != SPR_TYPE_EX_AC_AR) 8185 lun->pr_res_idx = msg->pr.pr_info.residx; 8186 } else { 8187 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8188 if (sa_res_key == lun->pr_keys[i]) 8189 continue; 8190 8191 lun->pr_keys[i] = 0; 8192 lun->pr_key_count--; 8193 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8194 } 8195 } 8196 } else { 8197 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8198 if (i == msg->pr.pr_info.residx || 8199 lun->pr_keys[i] == 0) 8200 continue; 8201 8202 if (sa_res_key == lun->pr_keys[i]) { 8203 lun->pr_keys[i] = 0; 8204 lun->pr_key_count--; 8205 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8206 } else if (msg->pr.pr_info.res_type != lun->res_type 8207 && (lun->res_type == SPR_TYPE_WR_EX_RO 8208 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8209 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE); 8210 } 8211 } 8212 lun->res_type = msg->pr.pr_info.res_type; 8213 if (lun->res_type != SPR_TYPE_WR_EX_AR 8214 && lun->res_type != SPR_TYPE_EX_AC_AR) 8215 lun->pr_res_idx = msg->pr.pr_info.residx; 8216 else 8217 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8218 } 8219 lun->PRGeneration++; 8220 8221} 8222 8223 8224int 8225ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8226{ 8227 int retval; 8228 int isc_retval; 8229 u_int32_t param_len; 8230 struct scsi_per_res_out *cdb; 8231 struct ctl_lun *lun; 8232 struct scsi_per_res_out_parms* param; 8233 struct ctl_softc *softc; 8234 uint32_t residx; 8235 uint64_t res_key, sa_res_key; 8236 uint8_t type; 8237 union ctl_ha_msg persis_io; 8238 int i; 8239 8240 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8241 8242 retval = CTL_RETVAL_COMPLETE; 8243 8244 softc = control_softc; 8245 8246 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8247 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8248 8249 /* 8250 * We only support whole-LUN scope. The scope & type are ignored for 8251 * register, register and ignore existing key and clear. 8252 * We sometimes ignore scope and type on preempts too!! 8253 * Verify reservation type here as well. 8254 */ 8255 type = cdb->scope_type & SPR_TYPE_MASK; 8256 if ((cdb->action == SPRO_RESERVE) 8257 || (cdb->action == SPRO_RELEASE)) { 8258 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8259 ctl_set_invalid_field(/*ctsio*/ ctsio, 8260 /*sks_valid*/ 1, 8261 /*command*/ 1, 8262 /*field*/ 2, 8263 /*bit_valid*/ 1, 8264 /*bit*/ 4); 8265 ctl_done((union ctl_io *)ctsio); 8266 return (CTL_RETVAL_COMPLETE); 8267 } 8268 8269 if (type>8 || type==2 || type==4 || type==0) { 8270 ctl_set_invalid_field(/*ctsio*/ ctsio, 8271 /*sks_valid*/ 1, 8272 /*command*/ 1, 8273 /*field*/ 2, 8274 /*bit_valid*/ 1, 8275 /*bit*/ 0); 8276 ctl_done((union ctl_io *)ctsio); 8277 return (CTL_RETVAL_COMPLETE); 8278 } 8279 } 8280 8281 param_len = scsi_4btoul(cdb->length); 8282 8283 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8284 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8285 ctsio->kern_data_len = param_len; 8286 ctsio->kern_total_len = param_len; 8287 ctsio->kern_data_resid = 0; 8288 ctsio->kern_rel_offset = 0; 8289 ctsio->kern_sg_entries = 0; 8290 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8291 ctsio->be_move_done = ctl_config_move_done; 8292 ctl_datamove((union ctl_io *)ctsio); 8293 8294 return (CTL_RETVAL_COMPLETE); 8295 } 8296 8297 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8298 8299 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8300 res_key = scsi_8btou64(param->res_key.key); 8301 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8302 8303 /* 8304 * Validate the reservation key here except for SPRO_REG_IGNO 8305 * This must be done for all other service actions 8306 */ 8307 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8308 mtx_lock(&lun->lun_lock); 8309 if (lun->pr_keys[residx] != 0) { 8310 if (res_key != lun->pr_keys[residx]) { 8311 /* 8312 * The current key passed in doesn't match 8313 * the one the initiator previously 8314 * registered. 8315 */ 8316 mtx_unlock(&lun->lun_lock); 8317 free(ctsio->kern_data_ptr, M_CTL); 8318 ctl_set_reservation_conflict(ctsio); 8319 ctl_done((union ctl_io *)ctsio); 8320 return (CTL_RETVAL_COMPLETE); 8321 } 8322 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8323 /* 8324 * We are not registered 8325 */ 8326 mtx_unlock(&lun->lun_lock); 8327 free(ctsio->kern_data_ptr, M_CTL); 8328 ctl_set_reservation_conflict(ctsio); 8329 ctl_done((union ctl_io *)ctsio); 8330 return (CTL_RETVAL_COMPLETE); 8331 } else if (res_key != 0) { 8332 /* 8333 * We are not registered and trying to register but 8334 * the register key isn't zero. 8335 */ 8336 mtx_unlock(&lun->lun_lock); 8337 free(ctsio->kern_data_ptr, M_CTL); 8338 ctl_set_reservation_conflict(ctsio); 8339 ctl_done((union ctl_io *)ctsio); 8340 return (CTL_RETVAL_COMPLETE); 8341 } 8342 mtx_unlock(&lun->lun_lock); 8343 } 8344 8345 switch (cdb->action & SPRO_ACTION_MASK) { 8346 case SPRO_REGISTER: 8347 case SPRO_REG_IGNO: { 8348 8349#if 0 8350 printf("Registration received\n"); 8351#endif 8352 8353 /* 8354 * We don't support any of these options, as we report in 8355 * the read capabilities request (see 8356 * ctl_persistent_reserve_in(), above). 8357 */ 8358 if ((param->flags & SPR_SPEC_I_PT) 8359 || (param->flags & SPR_ALL_TG_PT) 8360 || (param->flags & SPR_APTPL)) { 8361 int bit_ptr; 8362 8363 if (param->flags & SPR_APTPL) 8364 bit_ptr = 0; 8365 else if (param->flags & SPR_ALL_TG_PT) 8366 bit_ptr = 2; 8367 else /* SPR_SPEC_I_PT */ 8368 bit_ptr = 3; 8369 8370 free(ctsio->kern_data_ptr, M_CTL); 8371 ctl_set_invalid_field(ctsio, 8372 /*sks_valid*/ 1, 8373 /*command*/ 0, 8374 /*field*/ 20, 8375 /*bit_valid*/ 1, 8376 /*bit*/ bit_ptr); 8377 ctl_done((union ctl_io *)ctsio); 8378 return (CTL_RETVAL_COMPLETE); 8379 } 8380 8381 mtx_lock(&lun->lun_lock); 8382 8383 /* 8384 * The initiator wants to clear the 8385 * key/unregister. 8386 */ 8387 if (sa_res_key == 0) { 8388 if ((res_key == 0 8389 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8390 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8391 && lun->pr_keys[residx] == 0)) { 8392 mtx_unlock(&lun->lun_lock); 8393 goto done; 8394 } 8395 8396 lun->pr_keys[residx] = 0; 8397 lun->pr_key_count--; 8398 8399 if (residx == lun->pr_res_idx) { 8400 lun->flags &= ~CTL_LUN_PR_RESERVED; 8401 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8402 8403 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8404 || lun->res_type == SPR_TYPE_EX_AC_RO) 8405 && lun->pr_key_count) { 8406 /* 8407 * If the reservation is a registrants 8408 * only type we need to generate a UA 8409 * for other registered inits. The 8410 * sense code should be RESERVATIONS 8411 * RELEASED 8412 */ 8413 8414 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8415 if (lun->pr_keys[ 8416 i + persis_offset] == 0) 8417 continue; 8418 lun->pending_ua[i] |= 8419 CTL_UA_RES_RELEASE; 8420 } 8421 } 8422 lun->res_type = 0; 8423 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8424 if (lun->pr_key_count==0) { 8425 lun->flags &= ~CTL_LUN_PR_RESERVED; 8426 lun->res_type = 0; 8427 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8428 } 8429 } 8430 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8431 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8432 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8433 persis_io.pr.pr_info.residx = residx; 8434 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8435 &persis_io, sizeof(persis_io), 0 )) > 8436 CTL_HA_STATUS_SUCCESS) { 8437 printf("CTL:Persis Out error returned from " 8438 "ctl_ha_msg_send %d\n", isc_retval); 8439 } 8440 } else /* sa_res_key != 0 */ { 8441 8442 /* 8443 * If we aren't registered currently then increment 8444 * the key count and set the registered flag. 8445 */ 8446 if (lun->pr_keys[residx] == 0) 8447 lun->pr_key_count++; 8448 lun->pr_keys[residx] = sa_res_key; 8449 8450 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8451 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8452 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8453 persis_io.pr.pr_info.residx = residx; 8454 memcpy(persis_io.pr.pr_info.sa_res_key, 8455 param->serv_act_res_key, 8456 sizeof(param->serv_act_res_key)); 8457 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8458 &persis_io, sizeof(persis_io), 0)) > 8459 CTL_HA_STATUS_SUCCESS) { 8460 printf("CTL:Persis Out error returned from " 8461 "ctl_ha_msg_send %d\n", isc_retval); 8462 } 8463 } 8464 lun->PRGeneration++; 8465 mtx_unlock(&lun->lun_lock); 8466 8467 break; 8468 } 8469 case SPRO_RESERVE: 8470#if 0 8471 printf("Reserve executed type %d\n", type); 8472#endif 8473 mtx_lock(&lun->lun_lock); 8474 if (lun->flags & CTL_LUN_PR_RESERVED) { 8475 /* 8476 * if this isn't the reservation holder and it's 8477 * not a "all registrants" type or if the type is 8478 * different then we have a conflict 8479 */ 8480 if ((lun->pr_res_idx != residx 8481 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8482 || lun->res_type != type) { 8483 mtx_unlock(&lun->lun_lock); 8484 free(ctsio->kern_data_ptr, M_CTL); 8485 ctl_set_reservation_conflict(ctsio); 8486 ctl_done((union ctl_io *)ctsio); 8487 return (CTL_RETVAL_COMPLETE); 8488 } 8489 mtx_unlock(&lun->lun_lock); 8490 } else /* create a reservation */ { 8491 /* 8492 * If it's not an "all registrants" type record 8493 * reservation holder 8494 */ 8495 if (type != SPR_TYPE_WR_EX_AR 8496 && type != SPR_TYPE_EX_AC_AR) 8497 lun->pr_res_idx = residx; /* Res holder */ 8498 else 8499 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8500 8501 lun->flags |= CTL_LUN_PR_RESERVED; 8502 lun->res_type = type; 8503 8504 mtx_unlock(&lun->lun_lock); 8505 8506 /* send msg to other side */ 8507 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8508 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8509 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8510 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8511 persis_io.pr.pr_info.res_type = type; 8512 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8513 &persis_io, sizeof(persis_io), 0)) > 8514 CTL_HA_STATUS_SUCCESS) { 8515 printf("CTL:Persis Out error returned from " 8516 "ctl_ha_msg_send %d\n", isc_retval); 8517 } 8518 } 8519 break; 8520 8521 case SPRO_RELEASE: 8522 mtx_lock(&lun->lun_lock); 8523 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8524 /* No reservation exists return good status */ 8525 mtx_unlock(&lun->lun_lock); 8526 goto done; 8527 } 8528 /* 8529 * Is this nexus a reservation holder? 8530 */ 8531 if (lun->pr_res_idx != residx 8532 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8533 /* 8534 * not a res holder return good status but 8535 * do nothing 8536 */ 8537 mtx_unlock(&lun->lun_lock); 8538 goto done; 8539 } 8540 8541 if (lun->res_type != type) { 8542 mtx_unlock(&lun->lun_lock); 8543 free(ctsio->kern_data_ptr, M_CTL); 8544 ctl_set_illegal_pr_release(ctsio); 8545 ctl_done((union ctl_io *)ctsio); 8546 return (CTL_RETVAL_COMPLETE); 8547 } 8548 8549 /* okay to release */ 8550 lun->flags &= ~CTL_LUN_PR_RESERVED; 8551 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8552 lun->res_type = 0; 8553 8554 /* 8555 * if this isn't an exclusive access 8556 * res generate UA for all other 8557 * registrants. 8558 */ 8559 if (type != SPR_TYPE_EX_AC 8560 && type != SPR_TYPE_WR_EX) { 8561 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8562 if (i == residx || 8563 lun->pr_keys[i + persis_offset] == 0) 8564 continue; 8565 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8566 } 8567 } 8568 mtx_unlock(&lun->lun_lock); 8569 /* Send msg to other side */ 8570 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8571 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8572 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8573 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8574 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8575 printf("CTL:Persis Out error returned from " 8576 "ctl_ha_msg_send %d\n", isc_retval); 8577 } 8578 break; 8579 8580 case SPRO_CLEAR: 8581 /* send msg to other side */ 8582 8583 mtx_lock(&lun->lun_lock); 8584 lun->flags &= ~CTL_LUN_PR_RESERVED; 8585 lun->res_type = 0; 8586 lun->pr_key_count = 0; 8587 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8588 8589 lun->pr_keys[residx] = 0; 8590 8591 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8592 if (lun->pr_keys[i] != 0) { 8593 lun->pr_keys[i] = 0; 8594 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8595 } 8596 lun->PRGeneration++; 8597 mtx_unlock(&lun->lun_lock); 8598 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8599 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8600 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8601 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8602 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8603 printf("CTL:Persis Out error returned from " 8604 "ctl_ha_msg_send %d\n", isc_retval); 8605 } 8606 break; 8607 8608 case SPRO_PREEMPT: 8609 case SPRO_PRE_ABO: { 8610 int nretval; 8611 8612 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8613 residx, ctsio, cdb, param); 8614 if (nretval != 0) 8615 return (CTL_RETVAL_COMPLETE); 8616 break; 8617 } 8618 default: 8619 panic("Invalid PR type %x", cdb->action); 8620 } 8621 8622done: 8623 free(ctsio->kern_data_ptr, M_CTL); 8624 ctl_set_success(ctsio); 8625 ctl_done((union ctl_io *)ctsio); 8626 8627 return (retval); 8628} 8629 8630/* 8631 * This routine is for handling a message from the other SC pertaining to 8632 * persistent reserve out. All the error checking will have been done 8633 * so only perorming the action need be done here to keep the two 8634 * in sync. 8635 */ 8636static void 8637ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8638{ 8639 struct ctl_lun *lun; 8640 struct ctl_softc *softc; 8641 int i; 8642 uint32_t targ_lun; 8643 8644 softc = control_softc; 8645 8646 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8647 lun = softc->ctl_luns[targ_lun]; 8648 mtx_lock(&lun->lun_lock); 8649 switch(msg->pr.pr_info.action) { 8650 case CTL_PR_REG_KEY: 8651 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8652 lun->pr_key_count++; 8653 lun->pr_keys[msg->pr.pr_info.residx] = 8654 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8655 lun->PRGeneration++; 8656 break; 8657 8658 case CTL_PR_UNREG_KEY: 8659 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8660 lun->pr_key_count--; 8661 8662 /* XXX Need to see if the reservation has been released */ 8663 /* if so do we need to generate UA? */ 8664 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8665 lun->flags &= ~CTL_LUN_PR_RESERVED; 8666 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8667 8668 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8669 || lun->res_type == SPR_TYPE_EX_AC_RO) 8670 && lun->pr_key_count) { 8671 /* 8672 * If the reservation is a registrants 8673 * only type we need to generate a UA 8674 * for other registered inits. The 8675 * sense code should be RESERVATIONS 8676 * RELEASED 8677 */ 8678 8679 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8680 if (lun->pr_keys[i+ 8681 persis_offset] == 0) 8682 continue; 8683 8684 lun->pending_ua[i] |= 8685 CTL_UA_RES_RELEASE; 8686 } 8687 } 8688 lun->res_type = 0; 8689 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8690 if (lun->pr_key_count==0) { 8691 lun->flags &= ~CTL_LUN_PR_RESERVED; 8692 lun->res_type = 0; 8693 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8694 } 8695 } 8696 lun->PRGeneration++; 8697 break; 8698 8699 case CTL_PR_RESERVE: 8700 lun->flags |= CTL_LUN_PR_RESERVED; 8701 lun->res_type = msg->pr.pr_info.res_type; 8702 lun->pr_res_idx = msg->pr.pr_info.residx; 8703 8704 break; 8705 8706 case CTL_PR_RELEASE: 8707 /* 8708 * if this isn't an exclusive access res generate UA for all 8709 * other registrants. 8710 */ 8711 if (lun->res_type != SPR_TYPE_EX_AC 8712 && lun->res_type != SPR_TYPE_WR_EX) { 8713 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8714 if (lun->pr_keys[i+persis_offset] != 0) 8715 lun->pending_ua[i] |= 8716 CTL_UA_RES_RELEASE; 8717 } 8718 8719 lun->flags &= ~CTL_LUN_PR_RESERVED; 8720 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8721 lun->res_type = 0; 8722 break; 8723 8724 case CTL_PR_PREEMPT: 8725 ctl_pro_preempt_other(lun, msg); 8726 break; 8727 case CTL_PR_CLEAR: 8728 lun->flags &= ~CTL_LUN_PR_RESERVED; 8729 lun->res_type = 0; 8730 lun->pr_key_count = 0; 8731 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8732 8733 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8734 if (lun->pr_keys[i] == 0) 8735 continue; 8736 lun->pr_keys[i] = 0; 8737 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8738 } 8739 lun->PRGeneration++; 8740 break; 8741 } 8742 8743 mtx_unlock(&lun->lun_lock); 8744} 8745 8746int 8747ctl_read_write(struct ctl_scsiio *ctsio) 8748{ 8749 struct ctl_lun *lun; 8750 struct ctl_lba_len_flags *lbalen; 8751 uint64_t lba; 8752 uint32_t num_blocks; 8753 int flags, retval; 8754 int isread; 8755 8756 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8757 8758 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8759 8760 flags = 0; 8761 retval = CTL_RETVAL_COMPLETE; 8762 8763 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8764 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8765 switch (ctsio->cdb[0]) { 8766 case READ_6: 8767 case WRITE_6: { 8768 struct scsi_rw_6 *cdb; 8769 8770 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8771 8772 lba = scsi_3btoul(cdb->addr); 8773 /* only 5 bits are valid in the most significant address byte */ 8774 lba &= 0x1fffff; 8775 num_blocks = cdb->length; 8776 /* 8777 * This is correct according to SBC-2. 8778 */ 8779 if (num_blocks == 0) 8780 num_blocks = 256; 8781 break; 8782 } 8783 case READ_10: 8784 case WRITE_10: { 8785 struct scsi_rw_10 *cdb; 8786 8787 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8788 if (cdb->byte2 & SRW10_FUA) 8789 flags |= CTL_LLF_FUA; 8790 if (cdb->byte2 & SRW10_DPO) 8791 flags |= CTL_LLF_DPO; 8792 lba = scsi_4btoul(cdb->addr); 8793 num_blocks = scsi_2btoul(cdb->length); 8794 break; 8795 } 8796 case WRITE_VERIFY_10: { 8797 struct scsi_write_verify_10 *cdb; 8798 8799 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8800 flags |= CTL_LLF_FUA; 8801 if (cdb->byte2 & SWV_DPO) 8802 flags |= CTL_LLF_DPO; 8803 lba = scsi_4btoul(cdb->addr); 8804 num_blocks = scsi_2btoul(cdb->length); 8805 break; 8806 } 8807 case READ_12: 8808 case WRITE_12: { 8809 struct scsi_rw_12 *cdb; 8810 8811 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8812 if (cdb->byte2 & SRW12_FUA) 8813 flags |= CTL_LLF_FUA; 8814 if (cdb->byte2 & SRW12_DPO) 8815 flags |= CTL_LLF_DPO; 8816 lba = scsi_4btoul(cdb->addr); 8817 num_blocks = scsi_4btoul(cdb->length); 8818 break; 8819 } 8820 case WRITE_VERIFY_12: { 8821 struct scsi_write_verify_12 *cdb; 8822 8823 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8824 flags |= CTL_LLF_FUA; 8825 if (cdb->byte2 & SWV_DPO) 8826 flags |= CTL_LLF_DPO; 8827 lba = scsi_4btoul(cdb->addr); 8828 num_blocks = scsi_4btoul(cdb->length); 8829 break; 8830 } 8831 case READ_16: 8832 case WRITE_16: { 8833 struct scsi_rw_16 *cdb; 8834 8835 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8836 if (cdb->byte2 & SRW12_FUA) 8837 flags |= CTL_LLF_FUA; 8838 if (cdb->byte2 & SRW12_DPO) 8839 flags |= CTL_LLF_DPO; 8840 lba = scsi_8btou64(cdb->addr); 8841 num_blocks = scsi_4btoul(cdb->length); 8842 break; 8843 } 8844 case WRITE_ATOMIC_16: { 8845 struct scsi_rw_16 *cdb; 8846 8847 if (lun->be_lun->atomicblock == 0) { 8848 ctl_set_invalid_opcode(ctsio); 8849 ctl_done((union ctl_io *)ctsio); 8850 return (CTL_RETVAL_COMPLETE); 8851 } 8852 8853 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8854 if (cdb->byte2 & SRW12_FUA) 8855 flags |= CTL_LLF_FUA; 8856 if (cdb->byte2 & SRW12_DPO) 8857 flags |= CTL_LLF_DPO; 8858 lba = scsi_8btou64(cdb->addr); 8859 num_blocks = scsi_4btoul(cdb->length); 8860 if (num_blocks > lun->be_lun->atomicblock) { 8861 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8862 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8863 /*bit*/ 0); 8864 ctl_done((union ctl_io *)ctsio); 8865 return (CTL_RETVAL_COMPLETE); 8866 } 8867 break; 8868 } 8869 case WRITE_VERIFY_16: { 8870 struct scsi_write_verify_16 *cdb; 8871 8872 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8873 flags |= CTL_LLF_FUA; 8874 if (cdb->byte2 & SWV_DPO) 8875 flags |= CTL_LLF_DPO; 8876 lba = scsi_8btou64(cdb->addr); 8877 num_blocks = scsi_4btoul(cdb->length); 8878 break; 8879 } 8880 default: 8881 /* 8882 * We got a command we don't support. This shouldn't 8883 * happen, commands should be filtered out above us. 8884 */ 8885 ctl_set_invalid_opcode(ctsio); 8886 ctl_done((union ctl_io *)ctsio); 8887 8888 return (CTL_RETVAL_COMPLETE); 8889 break; /* NOTREACHED */ 8890 } 8891 8892 /* 8893 * The first check is to make sure we're in bounds, the second 8894 * check is to catch wrap-around problems. If the lba + num blocks 8895 * is less than the lba, then we've wrapped around and the block 8896 * range is invalid anyway. 8897 */ 8898 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8899 || ((lba + num_blocks) < lba)) { 8900 ctl_set_lba_out_of_range(ctsio); 8901 ctl_done((union ctl_io *)ctsio); 8902 return (CTL_RETVAL_COMPLETE); 8903 } 8904 8905 /* 8906 * According to SBC-3, a transfer length of 0 is not an error. 8907 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8908 * translates to 256 blocks for those commands. 8909 */ 8910 if (num_blocks == 0) { 8911 ctl_set_success(ctsio); 8912 ctl_done((union ctl_io *)ctsio); 8913 return (CTL_RETVAL_COMPLETE); 8914 } 8915 8916 /* Set FUA and/or DPO if caches are disabled. */ 8917 if (isread) { 8918 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8919 SCP_RCD) != 0) 8920 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8921 } else { 8922 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8923 SCP_WCE) == 0) 8924 flags |= CTL_LLF_FUA; 8925 } 8926 8927 lbalen = (struct ctl_lba_len_flags *) 8928 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8929 lbalen->lba = lba; 8930 lbalen->len = num_blocks; 8931 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8932 8933 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8934 ctsio->kern_rel_offset = 0; 8935 8936 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8937 8938 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8939 8940 return (retval); 8941} 8942 8943static int 8944ctl_cnw_cont(union ctl_io *io) 8945{ 8946 struct ctl_scsiio *ctsio; 8947 struct ctl_lun *lun; 8948 struct ctl_lba_len_flags *lbalen; 8949 int retval; 8950 8951 ctsio = &io->scsiio; 8952 ctsio->io_hdr.status = CTL_STATUS_NONE; 8953 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8954 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8955 lbalen = (struct ctl_lba_len_flags *) 8956 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8957 lbalen->flags &= ~CTL_LLF_COMPARE; 8958 lbalen->flags |= CTL_LLF_WRITE; 8959 8960 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8961 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8962 return (retval); 8963} 8964 8965int 8966ctl_cnw(struct ctl_scsiio *ctsio) 8967{ 8968 struct ctl_lun *lun; 8969 struct ctl_lba_len_flags *lbalen; 8970 uint64_t lba; 8971 uint32_t num_blocks; 8972 int flags, retval; 8973 8974 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8975 8976 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8977 8978 flags = 0; 8979 retval = CTL_RETVAL_COMPLETE; 8980 8981 switch (ctsio->cdb[0]) { 8982 case COMPARE_AND_WRITE: { 8983 struct scsi_compare_and_write *cdb; 8984 8985 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8986 if (cdb->byte2 & SRW10_FUA) 8987 flags |= CTL_LLF_FUA; 8988 if (cdb->byte2 & SRW10_DPO) 8989 flags |= CTL_LLF_DPO; 8990 lba = scsi_8btou64(cdb->addr); 8991 num_blocks = cdb->length; 8992 break; 8993 } 8994 default: 8995 /* 8996 * We got a command we don't support. This shouldn't 8997 * happen, commands should be filtered out above us. 8998 */ 8999 ctl_set_invalid_opcode(ctsio); 9000 ctl_done((union ctl_io *)ctsio); 9001 9002 return (CTL_RETVAL_COMPLETE); 9003 break; /* NOTREACHED */ 9004 } 9005 9006 /* 9007 * The first check is to make sure we're in bounds, the second 9008 * check is to catch wrap-around problems. If the lba + num blocks 9009 * is less than the lba, then we've wrapped around and the block 9010 * range is invalid anyway. 9011 */ 9012 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9013 || ((lba + num_blocks) < lba)) { 9014 ctl_set_lba_out_of_range(ctsio); 9015 ctl_done((union ctl_io *)ctsio); 9016 return (CTL_RETVAL_COMPLETE); 9017 } 9018 9019 /* 9020 * According to SBC-3, a transfer length of 0 is not an error. 9021 */ 9022 if (num_blocks == 0) { 9023 ctl_set_success(ctsio); 9024 ctl_done((union ctl_io *)ctsio); 9025 return (CTL_RETVAL_COMPLETE); 9026 } 9027 9028 /* Set FUA if write cache is disabled. */ 9029 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9030 SCP_WCE) == 0) 9031 flags |= CTL_LLF_FUA; 9032 9033 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9034 ctsio->kern_rel_offset = 0; 9035 9036 /* 9037 * Set the IO_CONT flag, so that if this I/O gets passed to 9038 * ctl_data_submit_done(), it'll get passed back to 9039 * ctl_ctl_cnw_cont() for further processing. 9040 */ 9041 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9042 ctsio->io_cont = ctl_cnw_cont; 9043 9044 lbalen = (struct ctl_lba_len_flags *) 9045 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9046 lbalen->lba = lba; 9047 lbalen->len = num_blocks; 9048 lbalen->flags = CTL_LLF_COMPARE | flags; 9049 9050 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9051 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9052 return (retval); 9053} 9054 9055int 9056ctl_verify(struct ctl_scsiio *ctsio) 9057{ 9058 struct ctl_lun *lun; 9059 struct ctl_lba_len_flags *lbalen; 9060 uint64_t lba; 9061 uint32_t num_blocks; 9062 int bytchk, flags; 9063 int retval; 9064 9065 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9066 9067 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9068 9069 bytchk = 0; 9070 flags = CTL_LLF_FUA; 9071 retval = CTL_RETVAL_COMPLETE; 9072 9073 switch (ctsio->cdb[0]) { 9074 case VERIFY_10: { 9075 struct scsi_verify_10 *cdb; 9076 9077 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9078 if (cdb->byte2 & SVFY_BYTCHK) 9079 bytchk = 1; 9080 if (cdb->byte2 & SVFY_DPO) 9081 flags |= CTL_LLF_DPO; 9082 lba = scsi_4btoul(cdb->addr); 9083 num_blocks = scsi_2btoul(cdb->length); 9084 break; 9085 } 9086 case VERIFY_12: { 9087 struct scsi_verify_12 *cdb; 9088 9089 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9090 if (cdb->byte2 & SVFY_BYTCHK) 9091 bytchk = 1; 9092 if (cdb->byte2 & SVFY_DPO) 9093 flags |= CTL_LLF_DPO; 9094 lba = scsi_4btoul(cdb->addr); 9095 num_blocks = scsi_4btoul(cdb->length); 9096 break; 9097 } 9098 case VERIFY_16: { 9099 struct scsi_rw_16 *cdb; 9100 9101 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9102 if (cdb->byte2 & SVFY_BYTCHK) 9103 bytchk = 1; 9104 if (cdb->byte2 & SVFY_DPO) 9105 flags |= CTL_LLF_DPO; 9106 lba = scsi_8btou64(cdb->addr); 9107 num_blocks = scsi_4btoul(cdb->length); 9108 break; 9109 } 9110 default: 9111 /* 9112 * We got a command we don't support. This shouldn't 9113 * happen, commands should be filtered out above us. 9114 */ 9115 ctl_set_invalid_opcode(ctsio); 9116 ctl_done((union ctl_io *)ctsio); 9117 return (CTL_RETVAL_COMPLETE); 9118 } 9119 9120 /* 9121 * The first check is to make sure we're in bounds, the second 9122 * check is to catch wrap-around problems. If the lba + num blocks 9123 * is less than the lba, then we've wrapped around and the block 9124 * range is invalid anyway. 9125 */ 9126 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9127 || ((lba + num_blocks) < lba)) { 9128 ctl_set_lba_out_of_range(ctsio); 9129 ctl_done((union ctl_io *)ctsio); 9130 return (CTL_RETVAL_COMPLETE); 9131 } 9132 9133 /* 9134 * According to SBC-3, a transfer length of 0 is not an error. 9135 */ 9136 if (num_blocks == 0) { 9137 ctl_set_success(ctsio); 9138 ctl_done((union ctl_io *)ctsio); 9139 return (CTL_RETVAL_COMPLETE); 9140 } 9141 9142 lbalen = (struct ctl_lba_len_flags *) 9143 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9144 lbalen->lba = lba; 9145 lbalen->len = num_blocks; 9146 if (bytchk) { 9147 lbalen->flags = CTL_LLF_COMPARE | flags; 9148 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9149 } else { 9150 lbalen->flags = CTL_LLF_VERIFY | flags; 9151 ctsio->kern_total_len = 0; 9152 } 9153 ctsio->kern_rel_offset = 0; 9154 9155 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9156 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9157 return (retval); 9158} 9159 9160int 9161ctl_report_luns(struct ctl_scsiio *ctsio) 9162{ 9163 struct scsi_report_luns *cdb; 9164 struct scsi_report_luns_data *lun_data; 9165 struct ctl_lun *lun, *request_lun; 9166 int num_luns, retval; 9167 uint32_t alloc_len, lun_datalen; 9168 int num_filled, well_known; 9169 uint32_t initidx, targ_lun_id, lun_id; 9170 9171 retval = CTL_RETVAL_COMPLETE; 9172 well_known = 0; 9173 9174 cdb = (struct scsi_report_luns *)ctsio->cdb; 9175 9176 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9177 9178 mtx_lock(&control_softc->ctl_lock); 9179 num_luns = control_softc->num_luns; 9180 mtx_unlock(&control_softc->ctl_lock); 9181 9182 switch (cdb->select_report) { 9183 case RPL_REPORT_DEFAULT: 9184 case RPL_REPORT_ALL: 9185 break; 9186 case RPL_REPORT_WELLKNOWN: 9187 well_known = 1; 9188 num_luns = 0; 9189 break; 9190 default: 9191 ctl_set_invalid_field(ctsio, 9192 /*sks_valid*/ 1, 9193 /*command*/ 1, 9194 /*field*/ 2, 9195 /*bit_valid*/ 0, 9196 /*bit*/ 0); 9197 ctl_done((union ctl_io *)ctsio); 9198 return (retval); 9199 break; /* NOTREACHED */ 9200 } 9201 9202 alloc_len = scsi_4btoul(cdb->length); 9203 /* 9204 * The initiator has to allocate at least 16 bytes for this request, 9205 * so he can at least get the header and the first LUN. Otherwise 9206 * we reject the request (per SPC-3 rev 14, section 6.21). 9207 */ 9208 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9209 sizeof(struct scsi_report_luns_lundata))) { 9210 ctl_set_invalid_field(ctsio, 9211 /*sks_valid*/ 1, 9212 /*command*/ 1, 9213 /*field*/ 6, 9214 /*bit_valid*/ 0, 9215 /*bit*/ 0); 9216 ctl_done((union ctl_io *)ctsio); 9217 return (retval); 9218 } 9219 9220 request_lun = (struct ctl_lun *) 9221 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9222 9223 lun_datalen = sizeof(*lun_data) + 9224 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9225 9226 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9227 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9228 ctsio->kern_sg_entries = 0; 9229 9230 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9231 9232 mtx_lock(&control_softc->ctl_lock); 9233 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9234 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9235 if (lun_id >= CTL_MAX_LUNS) 9236 continue; 9237 lun = control_softc->ctl_luns[lun_id]; 9238 if (lun == NULL) 9239 continue; 9240 9241 if (targ_lun_id <= 0xff) { 9242 /* 9243 * Peripheral addressing method, bus number 0. 9244 */ 9245 lun_data->luns[num_filled].lundata[0] = 9246 RPL_LUNDATA_ATYP_PERIPH; 9247 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9248 num_filled++; 9249 } else if (targ_lun_id <= 0x3fff) { 9250 /* 9251 * Flat addressing method. 9252 */ 9253 lun_data->luns[num_filled].lundata[0] = 9254 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9255 lun_data->luns[num_filled].lundata[1] = 9256 (targ_lun_id & 0xff); 9257 num_filled++; 9258 } else if (targ_lun_id <= 0xffffff) { 9259 /* 9260 * Extended flat addressing method. 9261 */ 9262 lun_data->luns[num_filled].lundata[0] = 9263 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9264 scsi_ulto3b(targ_lun_id, 9265 &lun_data->luns[num_filled].lundata[1]); 9266 num_filled++; 9267 } else { 9268 printf("ctl_report_luns: bogus LUN number %jd, " 9269 "skipping\n", (intmax_t)targ_lun_id); 9270 } 9271 /* 9272 * According to SPC-3, rev 14 section 6.21: 9273 * 9274 * "The execution of a REPORT LUNS command to any valid and 9275 * installed logical unit shall clear the REPORTED LUNS DATA 9276 * HAS CHANGED unit attention condition for all logical 9277 * units of that target with respect to the requesting 9278 * initiator. A valid and installed logical unit is one 9279 * having a PERIPHERAL QUALIFIER of 000b in the standard 9280 * INQUIRY data (see 6.4.2)." 9281 * 9282 * If request_lun is NULL, the LUN this report luns command 9283 * was issued to is either disabled or doesn't exist. In that 9284 * case, we shouldn't clear any pending lun change unit 9285 * attention. 9286 */ 9287 if (request_lun != NULL) { 9288 mtx_lock(&lun->lun_lock); 9289 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9290 mtx_unlock(&lun->lun_lock); 9291 } 9292 } 9293 mtx_unlock(&control_softc->ctl_lock); 9294 9295 /* 9296 * It's quite possible that we've returned fewer LUNs than we allocated 9297 * space for. Trim it. 9298 */ 9299 lun_datalen = sizeof(*lun_data) + 9300 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9301 9302 if (lun_datalen < alloc_len) { 9303 ctsio->residual = alloc_len - lun_datalen; 9304 ctsio->kern_data_len = lun_datalen; 9305 ctsio->kern_total_len = lun_datalen; 9306 } else { 9307 ctsio->residual = 0; 9308 ctsio->kern_data_len = alloc_len; 9309 ctsio->kern_total_len = alloc_len; 9310 } 9311 ctsio->kern_data_resid = 0; 9312 ctsio->kern_rel_offset = 0; 9313 ctsio->kern_sg_entries = 0; 9314 9315 /* 9316 * We set this to the actual data length, regardless of how much 9317 * space we actually have to return results. If the user looks at 9318 * this value, he'll know whether or not he allocated enough space 9319 * and reissue the command if necessary. We don't support well 9320 * known logical units, so if the user asks for that, return none. 9321 */ 9322 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9323 9324 /* 9325 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9326 * this request. 9327 */ 9328 ctl_set_success(ctsio); 9329 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9330 ctsio->be_move_done = ctl_config_move_done; 9331 ctl_datamove((union ctl_io *)ctsio); 9332 return (retval); 9333} 9334 9335int 9336ctl_request_sense(struct ctl_scsiio *ctsio) 9337{ 9338 struct scsi_request_sense *cdb; 9339 struct scsi_sense_data *sense_ptr; 9340 struct ctl_lun *lun; 9341 uint32_t initidx; 9342 int have_error; 9343 scsi_sense_data_type sense_format; 9344 9345 cdb = (struct scsi_request_sense *)ctsio->cdb; 9346 9347 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9348 9349 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9350 9351 /* 9352 * Determine which sense format the user wants. 9353 */ 9354 if (cdb->byte2 & SRS_DESC) 9355 sense_format = SSD_TYPE_DESC; 9356 else 9357 sense_format = SSD_TYPE_FIXED; 9358 9359 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9360 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9361 ctsio->kern_sg_entries = 0; 9362 9363 /* 9364 * struct scsi_sense_data, which is currently set to 256 bytes, is 9365 * larger than the largest allowed value for the length field in the 9366 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9367 */ 9368 ctsio->residual = 0; 9369 ctsio->kern_data_len = cdb->length; 9370 ctsio->kern_total_len = cdb->length; 9371 9372 ctsio->kern_data_resid = 0; 9373 ctsio->kern_rel_offset = 0; 9374 ctsio->kern_sg_entries = 0; 9375 9376 /* 9377 * If we don't have a LUN, we don't have any pending sense. 9378 */ 9379 if (lun == NULL) 9380 goto no_sense; 9381 9382 have_error = 0; 9383 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9384 /* 9385 * Check for pending sense, and then for pending unit attentions. 9386 * Pending sense gets returned first, then pending unit attentions. 9387 */ 9388 mtx_lock(&lun->lun_lock); 9389#ifdef CTL_WITH_CA 9390 if (ctl_is_set(lun->have_ca, initidx)) { 9391 scsi_sense_data_type stored_format; 9392 9393 /* 9394 * Check to see which sense format was used for the stored 9395 * sense data. 9396 */ 9397 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9398 9399 /* 9400 * If the user requested a different sense format than the 9401 * one we stored, then we need to convert it to the other 9402 * format. If we're going from descriptor to fixed format 9403 * sense data, we may lose things in translation, depending 9404 * on what options were used. 9405 * 9406 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9407 * for some reason we'll just copy it out as-is. 9408 */ 9409 if ((stored_format == SSD_TYPE_FIXED) 9410 && (sense_format == SSD_TYPE_DESC)) 9411 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9412 &lun->pending_sense[initidx], 9413 (struct scsi_sense_data_desc *)sense_ptr); 9414 else if ((stored_format == SSD_TYPE_DESC) 9415 && (sense_format == SSD_TYPE_FIXED)) 9416 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9417 &lun->pending_sense[initidx], 9418 (struct scsi_sense_data_fixed *)sense_ptr); 9419 else 9420 memcpy(sense_ptr, &lun->pending_sense[initidx], 9421 ctl_min(sizeof(*sense_ptr), 9422 sizeof(lun->pending_sense[initidx]))); 9423 9424 ctl_clear_mask(lun->have_ca, initidx); 9425 have_error = 1; 9426 } else 9427#endif 9428 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9429 ctl_ua_type ua_type; 9430 9431 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9432 sense_ptr, sense_format); 9433 if (ua_type != CTL_UA_NONE) 9434 have_error = 1; 9435 } 9436 mtx_unlock(&lun->lun_lock); 9437 9438 /* 9439 * We already have a pending error, return it. 9440 */ 9441 if (have_error != 0) { 9442 /* 9443 * We report the SCSI status as OK, since the status of the 9444 * request sense command itself is OK. 9445 * We report 0 for the sense length, because we aren't doing 9446 * autosense in this case. We're reporting sense as 9447 * parameter data. 9448 */ 9449 ctl_set_success(ctsio); 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 return (CTL_RETVAL_COMPLETE); 9454 } 9455 9456no_sense: 9457 9458 /* 9459 * No sense information to report, so we report that everything is 9460 * okay. 9461 */ 9462 ctl_set_sense_data(sense_ptr, 9463 lun, 9464 sense_format, 9465 /*current_error*/ 1, 9466 /*sense_key*/ SSD_KEY_NO_SENSE, 9467 /*asc*/ 0x00, 9468 /*ascq*/ 0x00, 9469 SSD_ELEM_NONE); 9470 9471 /* 9472 * We report 0 for the sense length, because we aren't doing 9473 * autosense in this case. We're reporting sense as parameter data. 9474 */ 9475 ctl_set_success(ctsio); 9476 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9477 ctsio->be_move_done = ctl_config_move_done; 9478 ctl_datamove((union ctl_io *)ctsio); 9479 return (CTL_RETVAL_COMPLETE); 9480} 9481 9482int 9483ctl_tur(struct ctl_scsiio *ctsio) 9484{ 9485 9486 CTL_DEBUG_PRINT(("ctl_tur\n")); 9487 9488 ctl_set_success(ctsio); 9489 ctl_done((union ctl_io *)ctsio); 9490 9491 return (CTL_RETVAL_COMPLETE); 9492} 9493 9494#ifdef notyet 9495static int 9496ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9497{ 9498 9499} 9500#endif 9501 9502static int 9503ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9504{ 9505 struct scsi_vpd_supported_pages *pages; 9506 int sup_page_size; 9507 struct ctl_lun *lun; 9508 9509 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9510 9511 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9512 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9513 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9514 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9515 ctsio->kern_sg_entries = 0; 9516 9517 if (sup_page_size < alloc_len) { 9518 ctsio->residual = alloc_len - sup_page_size; 9519 ctsio->kern_data_len = sup_page_size; 9520 ctsio->kern_total_len = sup_page_size; 9521 } else { 9522 ctsio->residual = 0; 9523 ctsio->kern_data_len = alloc_len; 9524 ctsio->kern_total_len = alloc_len; 9525 } 9526 ctsio->kern_data_resid = 0; 9527 ctsio->kern_rel_offset = 0; 9528 ctsio->kern_sg_entries = 0; 9529 9530 /* 9531 * The control device is always connected. The disk device, on the 9532 * other hand, may not be online all the time. Need to change this 9533 * to figure out whether the disk device is actually online or not. 9534 */ 9535 if (lun != NULL) 9536 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9537 lun->be_lun->lun_type; 9538 else 9539 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9540 9541 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9542 /* Supported VPD pages */ 9543 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9544 /* Serial Number */ 9545 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9546 /* Device Identification */ 9547 pages->page_list[2] = SVPD_DEVICE_ID; 9548 /* Extended INQUIRY Data */ 9549 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9550 /* Mode Page Policy */ 9551 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9552 /* SCSI Ports */ 9553 pages->page_list[5] = SVPD_SCSI_PORTS; 9554 /* Third-party Copy */ 9555 pages->page_list[6] = SVPD_SCSI_TPC; 9556 /* Block limits */ 9557 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9558 /* Block Device Characteristics */ 9559 pages->page_list[8] = SVPD_BDC; 9560 /* Logical Block Provisioning */ 9561 pages->page_list[9] = SVPD_LBP; 9562 9563 ctl_set_success(ctsio); 9564 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9565 ctsio->be_move_done = ctl_config_move_done; 9566 ctl_datamove((union ctl_io *)ctsio); 9567 return (CTL_RETVAL_COMPLETE); 9568} 9569 9570static int 9571ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9572{ 9573 struct scsi_vpd_unit_serial_number *sn_ptr; 9574 struct ctl_lun *lun; 9575 int data_len; 9576 9577 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9578 9579 data_len = 4 + CTL_SN_LEN; 9580 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9581 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9582 if (data_len < alloc_len) { 9583 ctsio->residual = alloc_len - data_len; 9584 ctsio->kern_data_len = data_len; 9585 ctsio->kern_total_len = data_len; 9586 } else { 9587 ctsio->residual = 0; 9588 ctsio->kern_data_len = alloc_len; 9589 ctsio->kern_total_len = alloc_len; 9590 } 9591 ctsio->kern_data_resid = 0; 9592 ctsio->kern_rel_offset = 0; 9593 ctsio->kern_sg_entries = 0; 9594 9595 /* 9596 * The control device is always connected. The disk device, on the 9597 * other hand, may not be online all the time. Need to change this 9598 * to figure out whether the disk device is actually online or not. 9599 */ 9600 if (lun != NULL) 9601 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9602 lun->be_lun->lun_type; 9603 else 9604 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9605 9606 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9607 sn_ptr->length = CTL_SN_LEN; 9608 /* 9609 * If we don't have a LUN, we just leave the serial number as 9610 * all spaces. 9611 */ 9612 if (lun != NULL) { 9613 strncpy((char *)sn_ptr->serial_num, 9614 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9615 } else 9616 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9617 9618 ctl_set_success(ctsio); 9619 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9620 ctsio->be_move_done = ctl_config_move_done; 9621 ctl_datamove((union ctl_io *)ctsio); 9622 return (CTL_RETVAL_COMPLETE); 9623} 9624 9625 9626static int 9627ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9628{ 9629 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9630 struct ctl_lun *lun; 9631 int data_len; 9632 9633 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9634 9635 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9636 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9637 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9638 ctsio->kern_sg_entries = 0; 9639 9640 if (data_len < alloc_len) { 9641 ctsio->residual = alloc_len - data_len; 9642 ctsio->kern_data_len = data_len; 9643 ctsio->kern_total_len = data_len; 9644 } else { 9645 ctsio->residual = 0; 9646 ctsio->kern_data_len = alloc_len; 9647 ctsio->kern_total_len = alloc_len; 9648 } 9649 ctsio->kern_data_resid = 0; 9650 ctsio->kern_rel_offset = 0; 9651 ctsio->kern_sg_entries = 0; 9652 9653 /* 9654 * The control device is always connected. The disk device, on the 9655 * other hand, may not be online all the time. 9656 */ 9657 if (lun != NULL) 9658 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9659 lun->be_lun->lun_type; 9660 else 9661 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9662 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9663 eid_ptr->page_length = data_len - 4; 9664 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9665 eid_ptr->flags3 = SVPD_EID_V_SUP; 9666 9667 ctl_set_success(ctsio); 9668 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9669 ctsio->be_move_done = ctl_config_move_done; 9670 ctl_datamove((union ctl_io *)ctsio); 9671 return (CTL_RETVAL_COMPLETE); 9672} 9673 9674static int 9675ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9676{ 9677 struct scsi_vpd_mode_page_policy *mpp_ptr; 9678 struct ctl_lun *lun; 9679 int data_len; 9680 9681 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9682 9683 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9684 sizeof(struct scsi_vpd_mode_page_policy_descr); 9685 9686 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9687 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9688 ctsio->kern_sg_entries = 0; 9689 9690 if (data_len < alloc_len) { 9691 ctsio->residual = alloc_len - data_len; 9692 ctsio->kern_data_len = data_len; 9693 ctsio->kern_total_len = data_len; 9694 } else { 9695 ctsio->residual = 0; 9696 ctsio->kern_data_len = alloc_len; 9697 ctsio->kern_total_len = alloc_len; 9698 } 9699 ctsio->kern_data_resid = 0; 9700 ctsio->kern_rel_offset = 0; 9701 ctsio->kern_sg_entries = 0; 9702 9703 /* 9704 * The control device is always connected. The disk device, on the 9705 * other hand, may not be online all the time. 9706 */ 9707 if (lun != NULL) 9708 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9709 lun->be_lun->lun_type; 9710 else 9711 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9712 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9713 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9714 mpp_ptr->descr[0].page_code = 0x3f; 9715 mpp_ptr->descr[0].subpage_code = 0xff; 9716 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9717 9718 ctl_set_success(ctsio); 9719 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9720 ctsio->be_move_done = ctl_config_move_done; 9721 ctl_datamove((union ctl_io *)ctsio); 9722 return (CTL_RETVAL_COMPLETE); 9723} 9724 9725static int 9726ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9727{ 9728 struct scsi_vpd_device_id *devid_ptr; 9729 struct scsi_vpd_id_descriptor *desc; 9730 struct ctl_softc *ctl_softc; 9731 struct ctl_lun *lun; 9732 struct ctl_port *port; 9733 int data_len; 9734 uint8_t proto; 9735 9736 ctl_softc = control_softc; 9737 9738 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9739 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9740 9741 data_len = sizeof(struct scsi_vpd_device_id) + 9742 sizeof(struct scsi_vpd_id_descriptor) + 9743 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9744 sizeof(struct scsi_vpd_id_descriptor) + 9745 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9746 if (lun && lun->lun_devid) 9747 data_len += lun->lun_devid->len; 9748 if (port->port_devid) 9749 data_len += port->port_devid->len; 9750 if (port->target_devid) 9751 data_len += port->target_devid->len; 9752 9753 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9754 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9755 ctsio->kern_sg_entries = 0; 9756 9757 if (data_len < alloc_len) { 9758 ctsio->residual = alloc_len - data_len; 9759 ctsio->kern_data_len = data_len; 9760 ctsio->kern_total_len = data_len; 9761 } else { 9762 ctsio->residual = 0; 9763 ctsio->kern_data_len = alloc_len; 9764 ctsio->kern_total_len = alloc_len; 9765 } 9766 ctsio->kern_data_resid = 0; 9767 ctsio->kern_rel_offset = 0; 9768 ctsio->kern_sg_entries = 0; 9769 9770 /* 9771 * The control device is always connected. The disk device, on the 9772 * other hand, may not be online all the time. 9773 */ 9774 if (lun != NULL) 9775 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9776 lun->be_lun->lun_type; 9777 else 9778 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9779 devid_ptr->page_code = SVPD_DEVICE_ID; 9780 scsi_ulto2b(data_len - 4, devid_ptr->length); 9781 9782 if (port->port_type == CTL_PORT_FC) 9783 proto = SCSI_PROTO_FC << 4; 9784 else if (port->port_type == CTL_PORT_ISCSI) 9785 proto = SCSI_PROTO_ISCSI << 4; 9786 else 9787 proto = SCSI_PROTO_SPI << 4; 9788 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9789 9790 /* 9791 * We're using a LUN association here. i.e., this device ID is a 9792 * per-LUN identifier. 9793 */ 9794 if (lun && lun->lun_devid) { 9795 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9796 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9797 lun->lun_devid->len); 9798 } 9799 9800 /* 9801 * This is for the WWPN which is a port association. 9802 */ 9803 if (port->port_devid) { 9804 memcpy(desc, port->port_devid->data, port->port_devid->len); 9805 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9806 port->port_devid->len); 9807 } 9808 9809 /* 9810 * This is for the Relative Target Port(type 4h) identifier 9811 */ 9812 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9813 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9814 SVPD_ID_TYPE_RELTARG; 9815 desc->length = 4; 9816 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9817 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9818 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9819 9820 /* 9821 * This is for the Target Port Group(type 5h) identifier 9822 */ 9823 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9824 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9825 SVPD_ID_TYPE_TPORTGRP; 9826 desc->length = 4; 9827 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9828 &desc->identifier[2]); 9829 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9830 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9831 9832 /* 9833 * This is for the Target identifier 9834 */ 9835 if (port->target_devid) { 9836 memcpy(desc, port->target_devid->data, port->target_devid->len); 9837 } 9838 9839 ctl_set_success(ctsio); 9840 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9841 ctsio->be_move_done = ctl_config_move_done; 9842 ctl_datamove((union ctl_io *)ctsio); 9843 return (CTL_RETVAL_COMPLETE); 9844} 9845 9846static int 9847ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9848{ 9849 struct ctl_softc *softc = control_softc; 9850 struct scsi_vpd_scsi_ports *sp; 9851 struct scsi_vpd_port_designation *pd; 9852 struct scsi_vpd_port_designation_cont *pdc; 9853 struct ctl_lun *lun; 9854 struct ctl_port *port; 9855 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9856 int num_target_port_groups; 9857 9858 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9859 9860 if (softc->is_single) 9861 num_target_port_groups = 1; 9862 else 9863 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9864 num_target_ports = 0; 9865 iid_len = 0; 9866 id_len = 0; 9867 mtx_lock(&softc->ctl_lock); 9868 STAILQ_FOREACH(port, &softc->port_list, links) { 9869 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9870 continue; 9871 if (lun != NULL && 9872 ctl_map_lun_back(port->targ_port, lun->lun) >= 9873 CTL_MAX_LUNS) 9874 continue; 9875 num_target_ports++; 9876 if (port->init_devid) 9877 iid_len += port->init_devid->len; 9878 if (port->port_devid) 9879 id_len += port->port_devid->len; 9880 } 9881 mtx_unlock(&softc->ctl_lock); 9882 9883 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 9884 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9885 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9886 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9887 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9888 ctsio->kern_sg_entries = 0; 9889 9890 if (data_len < alloc_len) { 9891 ctsio->residual = alloc_len - data_len; 9892 ctsio->kern_data_len = data_len; 9893 ctsio->kern_total_len = data_len; 9894 } else { 9895 ctsio->residual = 0; 9896 ctsio->kern_data_len = alloc_len; 9897 ctsio->kern_total_len = alloc_len; 9898 } 9899 ctsio->kern_data_resid = 0; 9900 ctsio->kern_rel_offset = 0; 9901 ctsio->kern_sg_entries = 0; 9902 9903 /* 9904 * The control device is always connected. The disk device, on the 9905 * other hand, may not be online all the time. Need to change this 9906 * to figure out whether the disk device is actually online or not. 9907 */ 9908 if (lun != NULL) 9909 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9910 lun->be_lun->lun_type; 9911 else 9912 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9913 9914 sp->page_code = SVPD_SCSI_PORTS; 9915 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9916 sp->page_length); 9917 pd = &sp->design[0]; 9918 9919 mtx_lock(&softc->ctl_lock); 9920 pg = softc->port_offset / CTL_MAX_PORTS; 9921 for (g = 0; g < num_target_port_groups; g++) { 9922 STAILQ_FOREACH(port, &softc->port_list, links) { 9923 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9924 continue; 9925 if (lun != NULL && 9926 ctl_map_lun_back(port->targ_port, lun->lun) >= 9927 CTL_MAX_LUNS) 9928 continue; 9929 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 9930 scsi_ulto2b(p, pd->relative_port_id); 9931 if (port->init_devid && g == pg) { 9932 iid_len = port->init_devid->len; 9933 memcpy(pd->initiator_transportid, 9934 port->init_devid->data, port->init_devid->len); 9935 } else 9936 iid_len = 0; 9937 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9938 pdc = (struct scsi_vpd_port_designation_cont *) 9939 (&pd->initiator_transportid[iid_len]); 9940 if (port->port_devid && g == pg) { 9941 id_len = port->port_devid->len; 9942 memcpy(pdc->target_port_descriptors, 9943 port->port_devid->data, port->port_devid->len); 9944 } else 9945 id_len = 0; 9946 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9947 pd = (struct scsi_vpd_port_designation *) 9948 ((uint8_t *)pdc->target_port_descriptors + id_len); 9949 } 9950 } 9951 mtx_unlock(&softc->ctl_lock); 9952 9953 ctl_set_success(ctsio); 9954 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9955 ctsio->be_move_done = ctl_config_move_done; 9956 ctl_datamove((union ctl_io *)ctsio); 9957 return (CTL_RETVAL_COMPLETE); 9958} 9959 9960static int 9961ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9962{ 9963 struct scsi_vpd_block_limits *bl_ptr; 9964 struct ctl_lun *lun; 9965 int bs; 9966 9967 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9968 9969 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9970 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9971 ctsio->kern_sg_entries = 0; 9972 9973 if (sizeof(*bl_ptr) < alloc_len) { 9974 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9975 ctsio->kern_data_len = sizeof(*bl_ptr); 9976 ctsio->kern_total_len = sizeof(*bl_ptr); 9977 } else { 9978 ctsio->residual = 0; 9979 ctsio->kern_data_len = alloc_len; 9980 ctsio->kern_total_len = alloc_len; 9981 } 9982 ctsio->kern_data_resid = 0; 9983 ctsio->kern_rel_offset = 0; 9984 ctsio->kern_sg_entries = 0; 9985 9986 /* 9987 * The control device is always connected. The disk device, on the 9988 * other hand, may not be online all the time. Need to change this 9989 * to figure out whether the disk device is actually online or not. 9990 */ 9991 if (lun != NULL) 9992 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9993 lun->be_lun->lun_type; 9994 else 9995 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9996 9997 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9998 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9999 bl_ptr->max_cmp_write_len = 0xff; 10000 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10001 if (lun != NULL) { 10002 bs = lun->be_lun->blocksize; 10003 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10004 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10005 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10006 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10007 if (lun->be_lun->pblockexp != 0) { 10008 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10009 bl_ptr->opt_unmap_grain); 10010 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10011 bl_ptr->unmap_grain_align); 10012 } 10013 } 10014 scsi_ulto4b(lun->be_lun->atomicblock, 10015 bl_ptr->max_atomic_transfer_length); 10016 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10017 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10018 } 10019 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10020 10021 ctl_set_success(ctsio); 10022 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10023 ctsio->be_move_done = ctl_config_move_done; 10024 ctl_datamove((union ctl_io *)ctsio); 10025 return (CTL_RETVAL_COMPLETE); 10026} 10027 10028static int 10029ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10030{ 10031 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10032 struct ctl_lun *lun; 10033 const char *value; 10034 u_int i; 10035 10036 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10037 10038 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10039 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10040 ctsio->kern_sg_entries = 0; 10041 10042 if (sizeof(*bdc_ptr) < alloc_len) { 10043 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10044 ctsio->kern_data_len = sizeof(*bdc_ptr); 10045 ctsio->kern_total_len = sizeof(*bdc_ptr); 10046 } else { 10047 ctsio->residual = 0; 10048 ctsio->kern_data_len = alloc_len; 10049 ctsio->kern_total_len = alloc_len; 10050 } 10051 ctsio->kern_data_resid = 0; 10052 ctsio->kern_rel_offset = 0; 10053 ctsio->kern_sg_entries = 0; 10054 10055 /* 10056 * The control device is always connected. The disk device, on the 10057 * other hand, may not be online all the time. Need to change this 10058 * to figure out whether the disk device is actually online or not. 10059 */ 10060 if (lun != NULL) 10061 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10062 lun->be_lun->lun_type; 10063 else 10064 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10065 bdc_ptr->page_code = SVPD_BDC; 10066 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10067 if (lun != NULL && 10068 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10069 i = strtol(value, NULL, 0); 10070 else 10071 i = CTL_DEFAULT_ROTATION_RATE; 10072 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10073 if (lun != NULL && 10074 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10075 i = strtol(value, NULL, 0); 10076 else 10077 i = 0; 10078 bdc_ptr->wab_wac_ff = (i & 0x0f); 10079 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10080 10081 ctl_set_success(ctsio); 10082 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10083 ctsio->be_move_done = ctl_config_move_done; 10084 ctl_datamove((union ctl_io *)ctsio); 10085 return (CTL_RETVAL_COMPLETE); 10086} 10087 10088static int 10089ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10090{ 10091 struct scsi_vpd_logical_block_prov *lbp_ptr; 10092 struct ctl_lun *lun; 10093 10094 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10095 10096 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10097 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10098 ctsio->kern_sg_entries = 0; 10099 10100 if (sizeof(*lbp_ptr) < alloc_len) { 10101 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10102 ctsio->kern_data_len = sizeof(*lbp_ptr); 10103 ctsio->kern_total_len = sizeof(*lbp_ptr); 10104 } else { 10105 ctsio->residual = 0; 10106 ctsio->kern_data_len = alloc_len; 10107 ctsio->kern_total_len = alloc_len; 10108 } 10109 ctsio->kern_data_resid = 0; 10110 ctsio->kern_rel_offset = 0; 10111 ctsio->kern_sg_entries = 0; 10112 10113 /* 10114 * The control device is always connected. The disk device, on the 10115 * other hand, may not be online all the time. Need to change this 10116 * to figure out whether the disk device is actually online or not. 10117 */ 10118 if (lun != NULL) 10119 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10120 lun->be_lun->lun_type; 10121 else 10122 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10123 10124 lbp_ptr->page_code = SVPD_LBP; 10125 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10126 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10127 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10128 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10129 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10130 lbp_ptr->prov_type = SVPD_LBP_THIN; 10131 } 10132 10133 ctl_set_success(ctsio); 10134 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10135 ctsio->be_move_done = ctl_config_move_done; 10136 ctl_datamove((union ctl_io *)ctsio); 10137 return (CTL_RETVAL_COMPLETE); 10138} 10139 10140static int 10141ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10142{ 10143 struct scsi_inquiry *cdb; 10144 int alloc_len, retval; 10145 10146 cdb = (struct scsi_inquiry *)ctsio->cdb; 10147 10148 retval = CTL_RETVAL_COMPLETE; 10149 10150 alloc_len = scsi_2btoul(cdb->length); 10151 10152 switch (cdb->page_code) { 10153 case SVPD_SUPPORTED_PAGES: 10154 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10155 break; 10156 case SVPD_UNIT_SERIAL_NUMBER: 10157 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10158 break; 10159 case SVPD_DEVICE_ID: 10160 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10161 break; 10162 case SVPD_EXTENDED_INQUIRY_DATA: 10163 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10164 break; 10165 case SVPD_MODE_PAGE_POLICY: 10166 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10167 break; 10168 case SVPD_SCSI_PORTS: 10169 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10170 break; 10171 case SVPD_SCSI_TPC: 10172 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10173 break; 10174 case SVPD_BLOCK_LIMITS: 10175 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10176 break; 10177 case SVPD_BDC: 10178 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10179 break; 10180 case SVPD_LBP: 10181 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10182 break; 10183 default: 10184 ctl_set_invalid_field(ctsio, 10185 /*sks_valid*/ 1, 10186 /*command*/ 1, 10187 /*field*/ 2, 10188 /*bit_valid*/ 0, 10189 /*bit*/ 0); 10190 ctl_done((union ctl_io *)ctsio); 10191 retval = CTL_RETVAL_COMPLETE; 10192 break; 10193 } 10194 10195 return (retval); 10196} 10197 10198static int 10199ctl_inquiry_std(struct ctl_scsiio *ctsio) 10200{ 10201 struct scsi_inquiry_data *inq_ptr; 10202 struct scsi_inquiry *cdb; 10203 struct ctl_softc *ctl_softc; 10204 struct ctl_lun *lun; 10205 char *val; 10206 uint32_t alloc_len, data_len; 10207 ctl_port_type port_type; 10208 10209 ctl_softc = control_softc; 10210 10211 /* 10212 * Figure out whether we're talking to a Fibre Channel port or not. 10213 * We treat the ioctl front end, and any SCSI adapters, as packetized 10214 * SCSI front ends. 10215 */ 10216 port_type = ctl_softc->ctl_ports[ 10217 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10218 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10219 port_type = CTL_PORT_SCSI; 10220 10221 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10222 cdb = (struct scsi_inquiry *)ctsio->cdb; 10223 alloc_len = scsi_2btoul(cdb->length); 10224 10225 /* 10226 * We malloc the full inquiry data size here and fill it 10227 * in. If the user only asks for less, we'll give him 10228 * that much. 10229 */ 10230 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10231 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10232 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10233 ctsio->kern_sg_entries = 0; 10234 ctsio->kern_data_resid = 0; 10235 ctsio->kern_rel_offset = 0; 10236 10237 if (data_len < alloc_len) { 10238 ctsio->residual = alloc_len - data_len; 10239 ctsio->kern_data_len = data_len; 10240 ctsio->kern_total_len = data_len; 10241 } else { 10242 ctsio->residual = 0; 10243 ctsio->kern_data_len = alloc_len; 10244 ctsio->kern_total_len = alloc_len; 10245 } 10246 10247 /* 10248 * If we have a LUN configured, report it as connected. Otherwise, 10249 * report that it is offline or no device is supported, depending 10250 * on the value of inquiry_pq_no_lun. 10251 * 10252 * According to the spec (SPC-4 r34), the peripheral qualifier 10253 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10254 * 10255 * "A peripheral device having the specified peripheral device type 10256 * is not connected to this logical unit. However, the device 10257 * server is capable of supporting the specified peripheral device 10258 * type on this logical unit." 10259 * 10260 * According to the same spec, the peripheral qualifier 10261 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10262 * 10263 * "The device server is not capable of supporting a peripheral 10264 * device on this logical unit. For this peripheral qualifier the 10265 * peripheral device type shall be set to 1Fh. All other peripheral 10266 * device type values are reserved for this peripheral qualifier." 10267 * 10268 * Given the text, it would seem that we probably want to report that 10269 * the LUN is offline here. There is no LUN connected, but we can 10270 * support a LUN at the given LUN number. 10271 * 10272 * In the real world, though, it sounds like things are a little 10273 * different: 10274 * 10275 * - Linux, when presented with a LUN with the offline peripheral 10276 * qualifier, will create an sg driver instance for it. So when 10277 * you attach it to CTL, you wind up with a ton of sg driver 10278 * instances. (One for every LUN that Linux bothered to probe.) 10279 * Linux does this despite the fact that it issues a REPORT LUNs 10280 * to LUN 0 to get the inventory of supported LUNs. 10281 * 10282 * - There is other anecdotal evidence (from Emulex folks) about 10283 * arrays that use the offline peripheral qualifier for LUNs that 10284 * are on the "passive" path in an active/passive array. 10285 * 10286 * So the solution is provide a hopefully reasonable default 10287 * (return bad/no LUN) and allow the user to change the behavior 10288 * with a tunable/sysctl variable. 10289 */ 10290 if (lun != NULL) 10291 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10292 lun->be_lun->lun_type; 10293 else if (ctl_softc->inquiry_pq_no_lun == 0) 10294 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10295 else 10296 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10297 10298 /* RMB in byte 2 is 0 */ 10299 inq_ptr->version = SCSI_REV_SPC4; 10300 10301 /* 10302 * According to SAM-3, even if a device only supports a single 10303 * level of LUN addressing, it should still set the HISUP bit: 10304 * 10305 * 4.9.1 Logical unit numbers overview 10306 * 10307 * All logical unit number formats described in this standard are 10308 * hierarchical in structure even when only a single level in that 10309 * hierarchy is used. The HISUP bit shall be set to one in the 10310 * standard INQUIRY data (see SPC-2) when any logical unit number 10311 * format described in this standard is used. Non-hierarchical 10312 * formats are outside the scope of this standard. 10313 * 10314 * Therefore we set the HiSup bit here. 10315 * 10316 * The reponse format is 2, per SPC-3. 10317 */ 10318 inq_ptr->response_format = SID_HiSup | 2; 10319 10320 inq_ptr->additional_length = data_len - 10321 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10322 CTL_DEBUG_PRINT(("additional_length = %d\n", 10323 inq_ptr->additional_length)); 10324 10325 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10326 /* 16 bit addressing */ 10327 if (port_type == CTL_PORT_SCSI) 10328 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10329 /* XXX set the SID_MultiP bit here if we're actually going to 10330 respond on multiple ports */ 10331 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10332 10333 /* 16 bit data bus, synchronous transfers */ 10334 if (port_type == CTL_PORT_SCSI) 10335 inq_ptr->flags = SID_WBus16 | SID_Sync; 10336 /* 10337 * XXX KDM do we want to support tagged queueing on the control 10338 * device at all? 10339 */ 10340 if ((lun == NULL) 10341 || (lun->be_lun->lun_type != T_PROCESSOR)) 10342 inq_ptr->flags |= SID_CmdQue; 10343 /* 10344 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10345 * We have 8 bytes for the vendor name, and 16 bytes for the device 10346 * name and 4 bytes for the revision. 10347 */ 10348 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10349 "vendor")) == NULL) { 10350 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10351 } else { 10352 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10353 strncpy(inq_ptr->vendor, val, 10354 min(sizeof(inq_ptr->vendor), strlen(val))); 10355 } 10356 if (lun == NULL) { 10357 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10358 sizeof(inq_ptr->product)); 10359 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10360 switch (lun->be_lun->lun_type) { 10361 case T_DIRECT: 10362 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10363 sizeof(inq_ptr->product)); 10364 break; 10365 case T_PROCESSOR: 10366 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10367 sizeof(inq_ptr->product)); 10368 break; 10369 default: 10370 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10371 sizeof(inq_ptr->product)); 10372 break; 10373 } 10374 } else { 10375 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10376 strncpy(inq_ptr->product, val, 10377 min(sizeof(inq_ptr->product), strlen(val))); 10378 } 10379 10380 /* 10381 * XXX make this a macro somewhere so it automatically gets 10382 * incremented when we make changes. 10383 */ 10384 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10385 "revision")) == NULL) { 10386 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10387 } else { 10388 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10389 strncpy(inq_ptr->revision, val, 10390 min(sizeof(inq_ptr->revision), strlen(val))); 10391 } 10392 10393 /* 10394 * For parallel SCSI, we support double transition and single 10395 * transition clocking. We also support QAS (Quick Arbitration 10396 * and Selection) and Information Unit transfers on both the 10397 * control and array devices. 10398 */ 10399 if (port_type == CTL_PORT_SCSI) 10400 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10401 SID_SPI_IUS; 10402 10403 /* SAM-5 (no version claimed) */ 10404 scsi_ulto2b(0x00A0, inq_ptr->version1); 10405 /* SPC-4 (no version claimed) */ 10406 scsi_ulto2b(0x0460, inq_ptr->version2); 10407 if (port_type == CTL_PORT_FC) { 10408 /* FCP-2 ANSI INCITS.350:2003 */ 10409 scsi_ulto2b(0x0917, inq_ptr->version3); 10410 } else if (port_type == CTL_PORT_SCSI) { 10411 /* SPI-4 ANSI INCITS.362:200x */ 10412 scsi_ulto2b(0x0B56, inq_ptr->version3); 10413 } else if (port_type == CTL_PORT_ISCSI) { 10414 /* iSCSI (no version claimed) */ 10415 scsi_ulto2b(0x0960, inq_ptr->version3); 10416 } else if (port_type == CTL_PORT_SAS) { 10417 /* SAS (no version claimed) */ 10418 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10419 } 10420 10421 if (lun == NULL) { 10422 /* SBC-4 (no version claimed) */ 10423 scsi_ulto2b(0x0600, inq_ptr->version4); 10424 } else { 10425 switch (lun->be_lun->lun_type) { 10426 case T_DIRECT: 10427 /* SBC-4 (no version claimed) */ 10428 scsi_ulto2b(0x0600, inq_ptr->version4); 10429 break; 10430 case T_PROCESSOR: 10431 default: 10432 break; 10433 } 10434 } 10435 10436 ctl_set_success(ctsio); 10437 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10438 ctsio->be_move_done = ctl_config_move_done; 10439 ctl_datamove((union ctl_io *)ctsio); 10440 return (CTL_RETVAL_COMPLETE); 10441} 10442 10443int 10444ctl_inquiry(struct ctl_scsiio *ctsio) 10445{ 10446 struct scsi_inquiry *cdb; 10447 int retval; 10448 10449 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10450 10451 cdb = (struct scsi_inquiry *)ctsio->cdb; 10452 if (cdb->byte2 & SI_EVPD) 10453 retval = ctl_inquiry_evpd(ctsio); 10454 else if (cdb->page_code == 0) 10455 retval = ctl_inquiry_std(ctsio); 10456 else { 10457 ctl_set_invalid_field(ctsio, 10458 /*sks_valid*/ 1, 10459 /*command*/ 1, 10460 /*field*/ 2, 10461 /*bit_valid*/ 0, 10462 /*bit*/ 0); 10463 ctl_done((union ctl_io *)ctsio); 10464 return (CTL_RETVAL_COMPLETE); 10465 } 10466 10467 return (retval); 10468} 10469 10470/* 10471 * For known CDB types, parse the LBA and length. 10472 */ 10473static int 10474ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10475{ 10476 if (io->io_hdr.io_type != CTL_IO_SCSI) 10477 return (1); 10478 10479 switch (io->scsiio.cdb[0]) { 10480 case COMPARE_AND_WRITE: { 10481 struct scsi_compare_and_write *cdb; 10482 10483 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10484 10485 *lba = scsi_8btou64(cdb->addr); 10486 *len = cdb->length; 10487 break; 10488 } 10489 case READ_6: 10490 case WRITE_6: { 10491 struct scsi_rw_6 *cdb; 10492 10493 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10494 10495 *lba = scsi_3btoul(cdb->addr); 10496 /* only 5 bits are valid in the most significant address byte */ 10497 *lba &= 0x1fffff; 10498 *len = cdb->length; 10499 break; 10500 } 10501 case READ_10: 10502 case WRITE_10: { 10503 struct scsi_rw_10 *cdb; 10504 10505 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10506 10507 *lba = scsi_4btoul(cdb->addr); 10508 *len = scsi_2btoul(cdb->length); 10509 break; 10510 } 10511 case WRITE_VERIFY_10: { 10512 struct scsi_write_verify_10 *cdb; 10513 10514 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10515 10516 *lba = scsi_4btoul(cdb->addr); 10517 *len = scsi_2btoul(cdb->length); 10518 break; 10519 } 10520 case READ_12: 10521 case WRITE_12: { 10522 struct scsi_rw_12 *cdb; 10523 10524 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10525 10526 *lba = scsi_4btoul(cdb->addr); 10527 *len = scsi_4btoul(cdb->length); 10528 break; 10529 } 10530 case WRITE_VERIFY_12: { 10531 struct scsi_write_verify_12 *cdb; 10532 10533 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10534 10535 *lba = scsi_4btoul(cdb->addr); 10536 *len = scsi_4btoul(cdb->length); 10537 break; 10538 } 10539 case READ_16: 10540 case WRITE_16: 10541 case WRITE_ATOMIC_16: { 10542 struct scsi_rw_16 *cdb; 10543 10544 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10545 10546 *lba = scsi_8btou64(cdb->addr); 10547 *len = scsi_4btoul(cdb->length); 10548 break; 10549 } 10550 case WRITE_VERIFY_16: { 10551 struct scsi_write_verify_16 *cdb; 10552 10553 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10554 10555 *lba = scsi_8btou64(cdb->addr); 10556 *len = scsi_4btoul(cdb->length); 10557 break; 10558 } 10559 case WRITE_SAME_10: { 10560 struct scsi_write_same_10 *cdb; 10561 10562 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10563 10564 *lba = scsi_4btoul(cdb->addr); 10565 *len = scsi_2btoul(cdb->length); 10566 break; 10567 } 10568 case WRITE_SAME_16: { 10569 struct scsi_write_same_16 *cdb; 10570 10571 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10572 10573 *lba = scsi_8btou64(cdb->addr); 10574 *len = scsi_4btoul(cdb->length); 10575 break; 10576 } 10577 case VERIFY_10: { 10578 struct scsi_verify_10 *cdb; 10579 10580 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10581 10582 *lba = scsi_4btoul(cdb->addr); 10583 *len = scsi_2btoul(cdb->length); 10584 break; 10585 } 10586 case VERIFY_12: { 10587 struct scsi_verify_12 *cdb; 10588 10589 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10590 10591 *lba = scsi_4btoul(cdb->addr); 10592 *len = scsi_4btoul(cdb->length); 10593 break; 10594 } 10595 case VERIFY_16: { 10596 struct scsi_verify_16 *cdb; 10597 10598 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10599 10600 *lba = scsi_8btou64(cdb->addr); 10601 *len = scsi_4btoul(cdb->length); 10602 break; 10603 } 10604 case UNMAP: { 10605 *lba = 0; 10606 *len = UINT64_MAX; 10607 break; 10608 } 10609 default: 10610 return (1); 10611 break; /* NOTREACHED */ 10612 } 10613 10614 return (0); 10615} 10616 10617static ctl_action 10618ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10619{ 10620 uint64_t endlba1, endlba2; 10621 10622 endlba1 = lba1 + len1 - 1; 10623 endlba2 = lba2 + len2 - 1; 10624 10625 if ((endlba1 < lba2) 10626 || (endlba2 < lba1)) 10627 return (CTL_ACTION_PASS); 10628 else 10629 return (CTL_ACTION_BLOCK); 10630} 10631 10632static int 10633ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10634{ 10635 struct ctl_ptr_len_flags *ptrlen; 10636 struct scsi_unmap_desc *buf, *end, *range; 10637 uint64_t lba; 10638 uint32_t len; 10639 10640 /* If not UNMAP -- go other way. */ 10641 if (io->io_hdr.io_type != CTL_IO_SCSI || 10642 io->scsiio.cdb[0] != UNMAP) 10643 return (CTL_ACTION_ERROR); 10644 10645 /* If UNMAP without data -- block and wait for data. */ 10646 ptrlen = (struct ctl_ptr_len_flags *) 10647 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10648 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10649 ptrlen->ptr == NULL) 10650 return (CTL_ACTION_BLOCK); 10651 10652 /* UNMAP with data -- check for collision. */ 10653 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10654 end = buf + ptrlen->len / sizeof(*buf); 10655 for (range = buf; range < end; range++) { 10656 lba = scsi_8btou64(range->lba); 10657 len = scsi_4btoul(range->length); 10658 if ((lba < lba2 + len2) && (lba + len > lba2)) 10659 return (CTL_ACTION_BLOCK); 10660 } 10661 return (CTL_ACTION_PASS); 10662} 10663 10664static ctl_action 10665ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10666{ 10667 uint64_t lba1, lba2; 10668 uint64_t len1, len2; 10669 int retval; 10670 10671 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10672 return (CTL_ACTION_ERROR); 10673 10674 retval = ctl_extent_check_unmap(io2, lba1, len1); 10675 if (retval != CTL_ACTION_ERROR) 10676 return (retval); 10677 10678 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10679 return (CTL_ACTION_ERROR); 10680 10681 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10682} 10683 10684static ctl_action 10685ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10686 union ctl_io *ooa_io) 10687{ 10688 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10689 ctl_serialize_action *serialize_row; 10690 10691 /* 10692 * The initiator attempted multiple untagged commands at the same 10693 * time. Can't do that. 10694 */ 10695 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10696 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10697 && ((pending_io->io_hdr.nexus.targ_port == 10698 ooa_io->io_hdr.nexus.targ_port) 10699 && (pending_io->io_hdr.nexus.initid.id == 10700 ooa_io->io_hdr.nexus.initid.id)) 10701 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10702 return (CTL_ACTION_OVERLAP); 10703 10704 /* 10705 * The initiator attempted to send multiple tagged commands with 10706 * the same ID. (It's fine if different initiators have the same 10707 * tag ID.) 10708 * 10709 * Even if all of those conditions are true, we don't kill the I/O 10710 * if the command ahead of us has been aborted. We won't end up 10711 * sending it to the FETD, and it's perfectly legal to resend a 10712 * command with the same tag number as long as the previous 10713 * instance of this tag number has been aborted somehow. 10714 */ 10715 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10716 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10717 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10718 && ((pending_io->io_hdr.nexus.targ_port == 10719 ooa_io->io_hdr.nexus.targ_port) 10720 && (pending_io->io_hdr.nexus.initid.id == 10721 ooa_io->io_hdr.nexus.initid.id)) 10722 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10723 return (CTL_ACTION_OVERLAP_TAG); 10724 10725 /* 10726 * If we get a head of queue tag, SAM-3 says that we should 10727 * immediately execute it. 10728 * 10729 * What happens if this command would normally block for some other 10730 * reason? e.g. a request sense with a head of queue tag 10731 * immediately after a write. Normally that would block, but this 10732 * will result in its getting executed immediately... 10733 * 10734 * We currently return "pass" instead of "skip", so we'll end up 10735 * going through the rest of the queue to check for overlapped tags. 10736 * 10737 * XXX KDM check for other types of blockage first?? 10738 */ 10739 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10740 return (CTL_ACTION_PASS); 10741 10742 /* 10743 * Ordered tags have to block until all items ahead of them 10744 * have completed. If we get called with an ordered tag, we always 10745 * block, if something else is ahead of us in the queue. 10746 */ 10747 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10748 return (CTL_ACTION_BLOCK); 10749 10750 /* 10751 * Simple tags get blocked until all head of queue and ordered tags 10752 * ahead of them have completed. I'm lumping untagged commands in 10753 * with simple tags here. XXX KDM is that the right thing to do? 10754 */ 10755 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10756 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10757 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10758 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10759 return (CTL_ACTION_BLOCK); 10760 10761 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10762 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10763 10764 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10765 10766 switch (serialize_row[pending_entry->seridx]) { 10767 case CTL_SER_BLOCK: 10768 return (CTL_ACTION_BLOCK); 10769 case CTL_SER_EXTENT: 10770 return (ctl_extent_check(pending_io, ooa_io)); 10771 case CTL_SER_EXTENTOPT: 10772 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10773 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10774 return (ctl_extent_check(pending_io, ooa_io)); 10775 /* FALLTHROUGH */ 10776 case CTL_SER_PASS: 10777 return (CTL_ACTION_PASS); 10778 case CTL_SER_BLOCKOPT: 10779 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10780 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10781 return (CTL_ACTION_BLOCK); 10782 return (CTL_ACTION_PASS); 10783 case CTL_SER_SKIP: 10784 return (CTL_ACTION_SKIP); 10785 default: 10786 panic("invalid serialization value %d", 10787 serialize_row[pending_entry->seridx]); 10788 } 10789 10790 return (CTL_ACTION_ERROR); 10791} 10792 10793/* 10794 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10795 * Assumptions: 10796 * - pending_io is generally either incoming, or on the blocked queue 10797 * - starting I/O is the I/O we want to start the check with. 10798 */ 10799static ctl_action 10800ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10801 union ctl_io *starting_io) 10802{ 10803 union ctl_io *ooa_io; 10804 ctl_action action; 10805 10806 mtx_assert(&lun->lun_lock, MA_OWNED); 10807 10808 /* 10809 * Run back along the OOA queue, starting with the current 10810 * blocked I/O and going through every I/O before it on the 10811 * queue. If starting_io is NULL, we'll just end up returning 10812 * CTL_ACTION_PASS. 10813 */ 10814 for (ooa_io = starting_io; ooa_io != NULL; 10815 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10816 ooa_links)){ 10817 10818 /* 10819 * This routine just checks to see whether 10820 * cur_blocked is blocked by ooa_io, which is ahead 10821 * of it in the queue. It doesn't queue/dequeue 10822 * cur_blocked. 10823 */ 10824 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10825 switch (action) { 10826 case CTL_ACTION_BLOCK: 10827 case CTL_ACTION_OVERLAP: 10828 case CTL_ACTION_OVERLAP_TAG: 10829 case CTL_ACTION_SKIP: 10830 case CTL_ACTION_ERROR: 10831 return (action); 10832 break; /* NOTREACHED */ 10833 case CTL_ACTION_PASS: 10834 break; 10835 default: 10836 panic("invalid action %d", action); 10837 break; /* NOTREACHED */ 10838 } 10839 } 10840 10841 return (CTL_ACTION_PASS); 10842} 10843 10844/* 10845 * Assumptions: 10846 * - An I/O has just completed, and has been removed from the per-LUN OOA 10847 * queue, so some items on the blocked queue may now be unblocked. 10848 */ 10849static int 10850ctl_check_blocked(struct ctl_lun *lun) 10851{ 10852 union ctl_io *cur_blocked, *next_blocked; 10853 10854 mtx_assert(&lun->lun_lock, MA_OWNED); 10855 10856 /* 10857 * Run forward from the head of the blocked queue, checking each 10858 * entry against the I/Os prior to it on the OOA queue to see if 10859 * there is still any blockage. 10860 * 10861 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10862 * with our removing a variable on it while it is traversing the 10863 * list. 10864 */ 10865 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10866 cur_blocked != NULL; cur_blocked = next_blocked) { 10867 union ctl_io *prev_ooa; 10868 ctl_action action; 10869 10870 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10871 blocked_links); 10872 10873 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10874 ctl_ooaq, ooa_links); 10875 10876 /* 10877 * If cur_blocked happens to be the first item in the OOA 10878 * queue now, prev_ooa will be NULL, and the action 10879 * returned will just be CTL_ACTION_PASS. 10880 */ 10881 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10882 10883 switch (action) { 10884 case CTL_ACTION_BLOCK: 10885 /* Nothing to do here, still blocked */ 10886 break; 10887 case CTL_ACTION_OVERLAP: 10888 case CTL_ACTION_OVERLAP_TAG: 10889 /* 10890 * This shouldn't happen! In theory we've already 10891 * checked this command for overlap... 10892 */ 10893 break; 10894 case CTL_ACTION_PASS: 10895 case CTL_ACTION_SKIP: { 10896 struct ctl_softc *softc; 10897 const struct ctl_cmd_entry *entry; 10898 int isc_retval; 10899 10900 /* 10901 * The skip case shouldn't happen, this transaction 10902 * should have never made it onto the blocked queue. 10903 */ 10904 /* 10905 * This I/O is no longer blocked, we can remove it 10906 * from the blocked queue. Since this is a TAILQ 10907 * (doubly linked list), we can do O(1) removals 10908 * from any place on the list. 10909 */ 10910 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10911 blocked_links); 10912 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10913 10914 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10915 /* 10916 * Need to send IO back to original side to 10917 * run 10918 */ 10919 union ctl_ha_msg msg_info; 10920 10921 msg_info.hdr.original_sc = 10922 cur_blocked->io_hdr.original_sc; 10923 msg_info.hdr.serializing_sc = cur_blocked; 10924 msg_info.hdr.msg_type = CTL_MSG_R2R; 10925 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10926 &msg_info, sizeof(msg_info), 0)) > 10927 CTL_HA_STATUS_SUCCESS) { 10928 printf("CTL:Check Blocked error from " 10929 "ctl_ha_msg_send %d\n", 10930 isc_retval); 10931 } 10932 break; 10933 } 10934 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 10935 softc = control_softc; 10936 10937 /* 10938 * Check this I/O for LUN state changes that may 10939 * have happened while this command was blocked. 10940 * The LUN state may have been changed by a command 10941 * ahead of us in the queue, so we need to re-check 10942 * for any states that can be caused by SCSI 10943 * commands. 10944 */ 10945 if (ctl_scsiio_lun_check(softc, lun, entry, 10946 &cur_blocked->scsiio) == 0) { 10947 cur_blocked->io_hdr.flags |= 10948 CTL_FLAG_IS_WAS_ON_RTR; 10949 ctl_enqueue_rtr(cur_blocked); 10950 } else 10951 ctl_done(cur_blocked); 10952 break; 10953 } 10954 default: 10955 /* 10956 * This probably shouldn't happen -- we shouldn't 10957 * get CTL_ACTION_ERROR, or anything else. 10958 */ 10959 break; 10960 } 10961 } 10962 10963 return (CTL_RETVAL_COMPLETE); 10964} 10965 10966/* 10967 * This routine (with one exception) checks LUN flags that can be set by 10968 * commands ahead of us in the OOA queue. These flags have to be checked 10969 * when a command initially comes in, and when we pull a command off the 10970 * blocked queue and are preparing to execute it. The reason we have to 10971 * check these flags for commands on the blocked queue is that the LUN 10972 * state may have been changed by a command ahead of us while we're on the 10973 * blocked queue. 10974 * 10975 * Ordering is somewhat important with these checks, so please pay 10976 * careful attention to the placement of any new checks. 10977 */ 10978static int 10979ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 10980 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10981{ 10982 int retval; 10983 uint32_t residx; 10984 10985 retval = 0; 10986 10987 mtx_assert(&lun->lun_lock, MA_OWNED); 10988 10989 /* 10990 * If this shelf is a secondary shelf controller, we have to reject 10991 * any media access commands. 10992 */ 10993 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 10994 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 10995 ctl_set_lun_standby(ctsio); 10996 retval = 1; 10997 goto bailout; 10998 } 10999 11000 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11001 if (lun->flags & CTL_LUN_READONLY) { 11002 ctl_set_sense(ctsio, /*current_error*/ 1, 11003 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11004 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11005 retval = 1; 11006 goto bailout; 11007 } 11008 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11009 .eca_and_aen & SCP_SWP) != 0) { 11010 ctl_set_sense(ctsio, /*current_error*/ 1, 11011 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11012 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11013 retval = 1; 11014 goto bailout; 11015 } 11016 } 11017 11018 /* 11019 * Check for a reservation conflict. If this command isn't allowed 11020 * even on reserved LUNs, and if this initiator isn't the one who 11021 * reserved us, reject the command with a reservation conflict. 11022 */ 11023 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11024 if ((lun->flags & CTL_LUN_RESERVED) 11025 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11026 if (lun->res_idx != residx) { 11027 ctl_set_reservation_conflict(ctsio); 11028 retval = 1; 11029 goto bailout; 11030 } 11031 } 11032 11033 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11034 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11035 /* No reservation or command is allowed. */; 11036 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11037 (lun->res_type == SPR_TYPE_WR_EX || 11038 lun->res_type == SPR_TYPE_WR_EX_RO || 11039 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11040 /* The command is allowed for Write Exclusive resv. */; 11041 } else { 11042 /* 11043 * if we aren't registered or it's a res holder type 11044 * reservation and this isn't the res holder then set a 11045 * conflict. 11046 */ 11047 if (lun->pr_keys[residx] == 0 11048 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11049 ctl_set_reservation_conflict(ctsio); 11050 retval = 1; 11051 goto bailout; 11052 } 11053 11054 } 11055 11056 if ((lun->flags & CTL_LUN_OFFLINE) 11057 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11058 ctl_set_lun_not_ready(ctsio); 11059 retval = 1; 11060 goto bailout; 11061 } 11062 11063 /* 11064 * If the LUN is stopped, see if this particular command is allowed 11065 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11066 */ 11067 if ((lun->flags & CTL_LUN_STOPPED) 11068 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11069 /* "Logical unit not ready, initializing cmd. required" */ 11070 ctl_set_lun_stopped(ctsio); 11071 retval = 1; 11072 goto bailout; 11073 } 11074 11075 if ((lun->flags & CTL_LUN_INOPERABLE) 11076 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11077 /* "Medium format corrupted" */ 11078 ctl_set_medium_format_corrupted(ctsio); 11079 retval = 1; 11080 goto bailout; 11081 } 11082 11083bailout: 11084 return (retval); 11085 11086} 11087 11088static void 11089ctl_failover_io(union ctl_io *io, int have_lock) 11090{ 11091 ctl_set_busy(&io->scsiio); 11092 ctl_done(io); 11093} 11094 11095static void 11096ctl_failover(void) 11097{ 11098 struct ctl_lun *lun; 11099 struct ctl_softc *ctl_softc; 11100 union ctl_io *next_io, *pending_io; 11101 union ctl_io *io; 11102 int lun_idx; 11103 int i; 11104 11105 ctl_softc = control_softc; 11106 11107 mtx_lock(&ctl_softc->ctl_lock); 11108 /* 11109 * Remove any cmds from the other SC from the rtr queue. These 11110 * will obviously only be for LUNs for which we're the primary. 11111 * We can't send status or get/send data for these commands. 11112 * Since they haven't been executed yet, we can just remove them. 11113 * We'll either abort them or delete them below, depending on 11114 * which HA mode we're in. 11115 */ 11116#ifdef notyet 11117 mtx_lock(&ctl_softc->queue_lock); 11118 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11119 io != NULL; io = next_io) { 11120 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11121 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11122 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11123 ctl_io_hdr, links); 11124 } 11125 mtx_unlock(&ctl_softc->queue_lock); 11126#endif 11127 11128 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11129 lun = ctl_softc->ctl_luns[lun_idx]; 11130 if (lun==NULL) 11131 continue; 11132 11133 /* 11134 * Processor LUNs are primary on both sides. 11135 * XXX will this always be true? 11136 */ 11137 if (lun->be_lun->lun_type == T_PROCESSOR) 11138 continue; 11139 11140 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11141 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11142 printf("FAILOVER: primary lun %d\n", lun_idx); 11143 /* 11144 * Remove all commands from the other SC. First from the 11145 * blocked queue then from the ooa queue. Once we have 11146 * removed them. Call ctl_check_blocked to see if there 11147 * is anything that can run. 11148 */ 11149 for (io = (union ctl_io *)TAILQ_FIRST( 11150 &lun->blocked_queue); io != NULL; io = next_io) { 11151 11152 next_io = (union ctl_io *)TAILQ_NEXT( 11153 &io->io_hdr, blocked_links); 11154 11155 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11156 TAILQ_REMOVE(&lun->blocked_queue, 11157 &io->io_hdr,blocked_links); 11158 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11159 TAILQ_REMOVE(&lun->ooa_queue, 11160 &io->io_hdr, ooa_links); 11161 11162 ctl_free_io(io); 11163 } 11164 } 11165 11166 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11167 io != NULL; io = next_io) { 11168 11169 next_io = (union ctl_io *)TAILQ_NEXT( 11170 &io->io_hdr, ooa_links); 11171 11172 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11173 11174 TAILQ_REMOVE(&lun->ooa_queue, 11175 &io->io_hdr, 11176 ooa_links); 11177 11178 ctl_free_io(io); 11179 } 11180 } 11181 ctl_check_blocked(lun); 11182 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11183 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11184 11185 printf("FAILOVER: primary lun %d\n", lun_idx); 11186 /* 11187 * Abort all commands from the other SC. We can't 11188 * send status back for them now. These should get 11189 * cleaned up when they are completed or come out 11190 * for a datamove operation. 11191 */ 11192 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11193 io != NULL; io = next_io) { 11194 next_io = (union ctl_io *)TAILQ_NEXT( 11195 &io->io_hdr, ooa_links); 11196 11197 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11198 io->io_hdr.flags |= CTL_FLAG_ABORT; 11199 } 11200 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11201 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11202 11203 printf("FAILOVER: secondary lun %d\n", lun_idx); 11204 11205 lun->flags |= CTL_LUN_PRIMARY_SC; 11206 11207 /* 11208 * We send all I/O that was sent to this controller 11209 * and redirected to the other side back with 11210 * busy status, and have the initiator retry it. 11211 * Figuring out how much data has been transferred, 11212 * etc. and picking up where we left off would be 11213 * very tricky. 11214 * 11215 * XXX KDM need to remove I/O from the blocked 11216 * queue as well! 11217 */ 11218 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11219 &lun->ooa_queue); pending_io != NULL; 11220 pending_io = next_io) { 11221 11222 next_io = (union ctl_io *)TAILQ_NEXT( 11223 &pending_io->io_hdr, ooa_links); 11224 11225 pending_io->io_hdr.flags &= 11226 ~CTL_FLAG_SENT_2OTHER_SC; 11227 11228 if (pending_io->io_hdr.flags & 11229 CTL_FLAG_IO_ACTIVE) { 11230 pending_io->io_hdr.flags |= 11231 CTL_FLAG_FAILOVER; 11232 } else { 11233 ctl_set_busy(&pending_io->scsiio); 11234 ctl_done(pending_io); 11235 } 11236 } 11237 11238 /* 11239 * Build Unit Attention 11240 */ 11241 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11242 lun->pending_ua[i] |= 11243 CTL_UA_ASYM_ACC_CHANGE; 11244 } 11245 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11246 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11247 printf("FAILOVER: secondary lun %d\n", lun_idx); 11248 /* 11249 * if the first io on the OOA is not on the RtR queue 11250 * add it. 11251 */ 11252 lun->flags |= CTL_LUN_PRIMARY_SC; 11253 11254 pending_io = (union ctl_io *)TAILQ_FIRST( 11255 &lun->ooa_queue); 11256 if (pending_io==NULL) { 11257 printf("Nothing on OOA queue\n"); 11258 continue; 11259 } 11260 11261 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11262 if ((pending_io->io_hdr.flags & 11263 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11264 pending_io->io_hdr.flags |= 11265 CTL_FLAG_IS_WAS_ON_RTR; 11266 ctl_enqueue_rtr(pending_io); 11267 } 11268#if 0 11269 else 11270 { 11271 printf("Tag 0x%04x is running\n", 11272 pending_io->scsiio.tag_num); 11273 } 11274#endif 11275 11276 next_io = (union ctl_io *)TAILQ_NEXT( 11277 &pending_io->io_hdr, ooa_links); 11278 for (pending_io=next_io; pending_io != NULL; 11279 pending_io = next_io) { 11280 pending_io->io_hdr.flags &= 11281 ~CTL_FLAG_SENT_2OTHER_SC; 11282 next_io = (union ctl_io *)TAILQ_NEXT( 11283 &pending_io->io_hdr, ooa_links); 11284 if (pending_io->io_hdr.flags & 11285 CTL_FLAG_IS_WAS_ON_RTR) { 11286#if 0 11287 printf("Tag 0x%04x is running\n", 11288 pending_io->scsiio.tag_num); 11289#endif 11290 continue; 11291 } 11292 11293 switch (ctl_check_ooa(lun, pending_io, 11294 (union ctl_io *)TAILQ_PREV( 11295 &pending_io->io_hdr, ctl_ooaq, 11296 ooa_links))) { 11297 11298 case CTL_ACTION_BLOCK: 11299 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11300 &pending_io->io_hdr, 11301 blocked_links); 11302 pending_io->io_hdr.flags |= 11303 CTL_FLAG_BLOCKED; 11304 break; 11305 case CTL_ACTION_PASS: 11306 case CTL_ACTION_SKIP: 11307 pending_io->io_hdr.flags |= 11308 CTL_FLAG_IS_WAS_ON_RTR; 11309 ctl_enqueue_rtr(pending_io); 11310 break; 11311 case CTL_ACTION_OVERLAP: 11312 ctl_set_overlapped_cmd( 11313 (struct ctl_scsiio *)pending_io); 11314 ctl_done(pending_io); 11315 break; 11316 case CTL_ACTION_OVERLAP_TAG: 11317 ctl_set_overlapped_tag( 11318 (struct ctl_scsiio *)pending_io, 11319 pending_io->scsiio.tag_num & 0xff); 11320 ctl_done(pending_io); 11321 break; 11322 case CTL_ACTION_ERROR: 11323 default: 11324 ctl_set_internal_failure( 11325 (struct ctl_scsiio *)pending_io, 11326 0, // sks_valid 11327 0); //retry count 11328 ctl_done(pending_io); 11329 break; 11330 } 11331 } 11332 11333 /* 11334 * Build Unit Attention 11335 */ 11336 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11337 lun->pending_ua[i] |= 11338 CTL_UA_ASYM_ACC_CHANGE; 11339 } 11340 } else { 11341 panic("Unhandled HA mode failover, LUN flags = %#x, " 11342 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11343 } 11344 } 11345 ctl_pause_rtr = 0; 11346 mtx_unlock(&ctl_softc->ctl_lock); 11347} 11348 11349static int 11350ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11351{ 11352 struct ctl_lun *lun; 11353 const struct ctl_cmd_entry *entry; 11354 uint32_t initidx, targ_lun; 11355 int retval; 11356 11357 retval = 0; 11358 11359 lun = NULL; 11360 11361 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11362 if ((targ_lun < CTL_MAX_LUNS) 11363 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11364 /* 11365 * If the LUN is invalid, pretend that it doesn't exist. 11366 * It will go away as soon as all pending I/O has been 11367 * completed. 11368 */ 11369 mtx_lock(&lun->lun_lock); 11370 if (lun->flags & CTL_LUN_DISABLED) { 11371 mtx_unlock(&lun->lun_lock); 11372 lun = NULL; 11373 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11374 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11375 } else { 11376 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11377 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11378 lun->be_lun; 11379 if (lun->be_lun->lun_type == T_PROCESSOR) { 11380 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11381 } 11382 11383 /* 11384 * Every I/O goes into the OOA queue for a 11385 * particular LUN, and stays there until completion. 11386 */ 11387 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11388 ooa_links); 11389 } 11390 } else { 11391 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11392 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11393 } 11394 11395 /* Get command entry and return error if it is unsuppotyed. */ 11396 entry = ctl_validate_command(ctsio); 11397 if (entry == NULL) { 11398 if (lun) 11399 mtx_unlock(&lun->lun_lock); 11400 return (retval); 11401 } 11402 11403 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11404 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11405 11406 /* 11407 * Check to see whether we can send this command to LUNs that don't 11408 * exist. This should pretty much only be the case for inquiry 11409 * and request sense. Further checks, below, really require having 11410 * a LUN, so we can't really check the command anymore. Just put 11411 * it on the rtr queue. 11412 */ 11413 if (lun == NULL) { 11414 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11415 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11416 ctl_enqueue_rtr((union ctl_io *)ctsio); 11417 return (retval); 11418 } 11419 11420 ctl_set_unsupported_lun(ctsio); 11421 ctl_done((union ctl_io *)ctsio); 11422 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11423 return (retval); 11424 } else { 11425 /* 11426 * Make sure we support this particular command on this LUN. 11427 * e.g., we don't support writes to the control LUN. 11428 */ 11429 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11430 mtx_unlock(&lun->lun_lock); 11431 ctl_set_invalid_opcode(ctsio); 11432 ctl_done((union ctl_io *)ctsio); 11433 return (retval); 11434 } 11435 } 11436 11437 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11438 11439#ifdef CTL_WITH_CA 11440 /* 11441 * If we've got a request sense, it'll clear the contingent 11442 * allegiance condition. Otherwise, if we have a CA condition for 11443 * this initiator, clear it, because it sent down a command other 11444 * than request sense. 11445 */ 11446 if ((ctsio->cdb[0] != REQUEST_SENSE) 11447 && (ctl_is_set(lun->have_ca, initidx))) 11448 ctl_clear_mask(lun->have_ca, initidx); 11449#endif 11450 11451 /* 11452 * If the command has this flag set, it handles its own unit 11453 * attention reporting, we shouldn't do anything. Otherwise we 11454 * check for any pending unit attentions, and send them back to the 11455 * initiator. We only do this when a command initially comes in, 11456 * not when we pull it off the blocked queue. 11457 * 11458 * According to SAM-3, section 5.3.2, the order that things get 11459 * presented back to the host is basically unit attentions caused 11460 * by some sort of reset event, busy status, reservation conflicts 11461 * or task set full, and finally any other status. 11462 * 11463 * One issue here is that some of the unit attentions we report 11464 * don't fall into the "reset" category (e.g. "reported luns data 11465 * has changed"). So reporting it here, before the reservation 11466 * check, may be technically wrong. I guess the only thing to do 11467 * would be to check for and report the reset events here, and then 11468 * check for the other unit attention types after we check for a 11469 * reservation conflict. 11470 * 11471 * XXX KDM need to fix this 11472 */ 11473 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11474 ctl_ua_type ua_type; 11475 11476 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11477 scsi_sense_data_type sense_format; 11478 11479 if (lun != NULL) 11480 sense_format = (lun->flags & 11481 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11482 SSD_TYPE_FIXED; 11483 else 11484 sense_format = SSD_TYPE_FIXED; 11485 11486 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11487 &ctsio->sense_data, sense_format); 11488 if (ua_type != CTL_UA_NONE) { 11489 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11490 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11491 CTL_AUTOSENSE; 11492 ctsio->sense_len = SSD_FULL_SIZE; 11493 mtx_unlock(&lun->lun_lock); 11494 ctl_done((union ctl_io *)ctsio); 11495 return (retval); 11496 } 11497 } 11498 } 11499 11500 11501 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11502 mtx_unlock(&lun->lun_lock); 11503 ctl_done((union ctl_io *)ctsio); 11504 return (retval); 11505 } 11506 11507 /* 11508 * XXX CHD this is where we want to send IO to other side if 11509 * this LUN is secondary on this SC. We will need to make a copy 11510 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11511 * the copy we send as FROM_OTHER. 11512 * We also need to stuff the address of the original IO so we can 11513 * find it easily. Something similar will need be done on the other 11514 * side so when we are done we can find the copy. 11515 */ 11516 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11517 union ctl_ha_msg msg_info; 11518 int isc_retval; 11519 11520 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11521 11522 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11523 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11524#if 0 11525 printf("1. ctsio %p\n", ctsio); 11526#endif 11527 msg_info.hdr.serializing_sc = NULL; 11528 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11529 msg_info.scsi.tag_num = ctsio->tag_num; 11530 msg_info.scsi.tag_type = ctsio->tag_type; 11531 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11532 11533 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11534 11535 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11536 (void *)&msg_info, sizeof(msg_info), 0)) > 11537 CTL_HA_STATUS_SUCCESS) { 11538 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11539 isc_retval); 11540 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11541 } else { 11542#if 0 11543 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11544#endif 11545 } 11546 11547 /* 11548 * XXX KDM this I/O is off the incoming queue, but hasn't 11549 * been inserted on any other queue. We may need to come 11550 * up with a holding queue while we wait for serialization 11551 * so that we have an idea of what we're waiting for from 11552 * the other side. 11553 */ 11554 mtx_unlock(&lun->lun_lock); 11555 return (retval); 11556 } 11557 11558 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11559 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11560 ctl_ooaq, ooa_links))) { 11561 case CTL_ACTION_BLOCK: 11562 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11563 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11564 blocked_links); 11565 mtx_unlock(&lun->lun_lock); 11566 return (retval); 11567 case CTL_ACTION_PASS: 11568 case CTL_ACTION_SKIP: 11569 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11570 mtx_unlock(&lun->lun_lock); 11571 ctl_enqueue_rtr((union ctl_io *)ctsio); 11572 break; 11573 case CTL_ACTION_OVERLAP: 11574 mtx_unlock(&lun->lun_lock); 11575 ctl_set_overlapped_cmd(ctsio); 11576 ctl_done((union ctl_io *)ctsio); 11577 break; 11578 case CTL_ACTION_OVERLAP_TAG: 11579 mtx_unlock(&lun->lun_lock); 11580 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11581 ctl_done((union ctl_io *)ctsio); 11582 break; 11583 case CTL_ACTION_ERROR: 11584 default: 11585 mtx_unlock(&lun->lun_lock); 11586 ctl_set_internal_failure(ctsio, 11587 /*sks_valid*/ 0, 11588 /*retry_count*/ 0); 11589 ctl_done((union ctl_io *)ctsio); 11590 break; 11591 } 11592 return (retval); 11593} 11594 11595const struct ctl_cmd_entry * 11596ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11597{ 11598 const struct ctl_cmd_entry *entry; 11599 int service_action; 11600 11601 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11602 if (sa) 11603 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11604 if (entry->flags & CTL_CMD_FLAG_SA5) { 11605 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11606 entry = &((const struct ctl_cmd_entry *) 11607 entry->execute)[service_action]; 11608 } 11609 return (entry); 11610} 11611 11612const struct ctl_cmd_entry * 11613ctl_validate_command(struct ctl_scsiio *ctsio) 11614{ 11615 const struct ctl_cmd_entry *entry; 11616 int i, sa; 11617 uint8_t diff; 11618 11619 entry = ctl_get_cmd_entry(ctsio, &sa); 11620 if (entry->execute == NULL) { 11621 if (sa) 11622 ctl_set_invalid_field(ctsio, 11623 /*sks_valid*/ 1, 11624 /*command*/ 1, 11625 /*field*/ 1, 11626 /*bit_valid*/ 1, 11627 /*bit*/ 4); 11628 else 11629 ctl_set_invalid_opcode(ctsio); 11630 ctl_done((union ctl_io *)ctsio); 11631 return (NULL); 11632 } 11633 KASSERT(entry->length > 0, 11634 ("Not defined length for command 0x%02x/0x%02x", 11635 ctsio->cdb[0], ctsio->cdb[1])); 11636 for (i = 1; i < entry->length; i++) { 11637 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11638 if (diff == 0) 11639 continue; 11640 ctl_set_invalid_field(ctsio, 11641 /*sks_valid*/ 1, 11642 /*command*/ 1, 11643 /*field*/ i, 11644 /*bit_valid*/ 1, 11645 /*bit*/ fls(diff) - 1); 11646 ctl_done((union ctl_io *)ctsio); 11647 return (NULL); 11648 } 11649 return (entry); 11650} 11651 11652static int 11653ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11654{ 11655 11656 switch (lun_type) { 11657 case T_PROCESSOR: 11658 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11659 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11660 return (0); 11661 break; 11662 case T_DIRECT: 11663 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11664 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11665 return (0); 11666 break; 11667 default: 11668 return (0); 11669 } 11670 return (1); 11671} 11672 11673static int 11674ctl_scsiio(struct ctl_scsiio *ctsio) 11675{ 11676 int retval; 11677 const struct ctl_cmd_entry *entry; 11678 11679 retval = CTL_RETVAL_COMPLETE; 11680 11681 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11682 11683 entry = ctl_get_cmd_entry(ctsio, NULL); 11684 11685 /* 11686 * If this I/O has been aborted, just send it straight to 11687 * ctl_done() without executing it. 11688 */ 11689 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11690 ctl_done((union ctl_io *)ctsio); 11691 goto bailout; 11692 } 11693 11694 /* 11695 * All the checks should have been handled by ctl_scsiio_precheck(). 11696 * We should be clear now to just execute the I/O. 11697 */ 11698 retval = entry->execute(ctsio); 11699 11700bailout: 11701 return (retval); 11702} 11703 11704/* 11705 * Since we only implement one target right now, a bus reset simply resets 11706 * our single target. 11707 */ 11708static int 11709ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11710{ 11711 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11712} 11713 11714static int 11715ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11716 ctl_ua_type ua_type) 11717{ 11718 struct ctl_lun *lun; 11719 int retval; 11720 11721 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11722 union ctl_ha_msg msg_info; 11723 11724 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11725 msg_info.hdr.nexus = io->io_hdr.nexus; 11726 if (ua_type==CTL_UA_TARG_RESET) 11727 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11728 else 11729 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11730 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11731 msg_info.hdr.original_sc = NULL; 11732 msg_info.hdr.serializing_sc = NULL; 11733 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11734 (void *)&msg_info, sizeof(msg_info), 0)) { 11735 } 11736 } 11737 retval = 0; 11738 11739 mtx_lock(&ctl_softc->ctl_lock); 11740 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11741 retval += ctl_lun_reset(lun, io, ua_type); 11742 mtx_unlock(&ctl_softc->ctl_lock); 11743 11744 return (retval); 11745} 11746 11747/* 11748 * The LUN should always be set. The I/O is optional, and is used to 11749 * distinguish between I/Os sent by this initiator, and by other 11750 * initiators. We set unit attention for initiators other than this one. 11751 * SAM-3 is vague on this point. It does say that a unit attention should 11752 * be established for other initiators when a LUN is reset (see section 11753 * 5.7.3), but it doesn't specifically say that the unit attention should 11754 * be established for this particular initiator when a LUN is reset. Here 11755 * is the relevant text, from SAM-3 rev 8: 11756 * 11757 * 5.7.2 When a SCSI initiator port aborts its own tasks 11758 * 11759 * When a SCSI initiator port causes its own task(s) to be aborted, no 11760 * notification that the task(s) have been aborted shall be returned to 11761 * the SCSI initiator port other than the completion response for the 11762 * command or task management function action that caused the task(s) to 11763 * be aborted and notification(s) associated with related effects of the 11764 * action (e.g., a reset unit attention condition). 11765 * 11766 * XXX KDM for now, we're setting unit attention for all initiators. 11767 */ 11768static int 11769ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11770{ 11771 union ctl_io *xio; 11772#if 0 11773 uint32_t initidx; 11774#endif 11775 int i; 11776 11777 mtx_lock(&lun->lun_lock); 11778 /* 11779 * Run through the OOA queue and abort each I/O. 11780 */ 11781#if 0 11782 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11783#endif 11784 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11785 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11786 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11787 } 11788 11789 /* 11790 * This version sets unit attention for every 11791 */ 11792#if 0 11793 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11794 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11795 if (initidx == i) 11796 continue; 11797 lun->pending_ua[i] |= ua_type; 11798 } 11799#endif 11800 11801 /* 11802 * A reset (any kind, really) clears reservations established with 11803 * RESERVE/RELEASE. It does not clear reservations established 11804 * with PERSISTENT RESERVE OUT, but we don't support that at the 11805 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11806 * reservations made with the RESERVE/RELEASE commands, because 11807 * those commands are obsolete in SPC-3. 11808 */ 11809 lun->flags &= ~CTL_LUN_RESERVED; 11810 11811 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11812#ifdef CTL_WITH_CA 11813 ctl_clear_mask(lun->have_ca, i); 11814#endif 11815 lun->pending_ua[i] |= ua_type; 11816 } 11817 mtx_unlock(&lun->lun_lock); 11818 11819 return (0); 11820} 11821 11822static void 11823ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11824 int other_sc) 11825{ 11826 union ctl_io *xio; 11827 11828 mtx_assert(&lun->lun_lock, MA_OWNED); 11829 11830 /* 11831 * Run through the OOA queue and attempt to find the given I/O. 11832 * The target port, initiator ID, tag type and tag number have to 11833 * match the values that we got from the initiator. If we have an 11834 * untagged command to abort, simply abort the first untagged command 11835 * we come to. We only allow one untagged command at a time of course. 11836 */ 11837 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11838 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11839 11840 if ((targ_port == UINT32_MAX || 11841 targ_port == xio->io_hdr.nexus.targ_port) && 11842 (init_id == UINT32_MAX || 11843 init_id == xio->io_hdr.nexus.initid.id)) { 11844 if (targ_port != xio->io_hdr.nexus.targ_port || 11845 init_id != xio->io_hdr.nexus.initid.id) 11846 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11847 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11848 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11849 union ctl_ha_msg msg_info; 11850 11851 msg_info.hdr.nexus = xio->io_hdr.nexus; 11852 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11853 msg_info.task.tag_num = xio->scsiio.tag_num; 11854 msg_info.task.tag_type = xio->scsiio.tag_type; 11855 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11856 msg_info.hdr.original_sc = NULL; 11857 msg_info.hdr.serializing_sc = NULL; 11858 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11859 (void *)&msg_info, sizeof(msg_info), 0); 11860 } 11861 } 11862 } 11863} 11864 11865static int 11866ctl_abort_task_set(union ctl_io *io) 11867{ 11868 struct ctl_softc *softc = control_softc; 11869 struct ctl_lun *lun; 11870 uint32_t targ_lun; 11871 11872 /* 11873 * Look up the LUN. 11874 */ 11875 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11876 mtx_lock(&softc->ctl_lock); 11877 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11878 lun = softc->ctl_luns[targ_lun]; 11879 else { 11880 mtx_unlock(&softc->ctl_lock); 11881 return (1); 11882 } 11883 11884 mtx_lock(&lun->lun_lock); 11885 mtx_unlock(&softc->ctl_lock); 11886 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11887 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11888 io->io_hdr.nexus.initid.id, 11889 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11890 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11891 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11892 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11893 } 11894 mtx_unlock(&lun->lun_lock); 11895 return (0); 11896} 11897 11898static int 11899ctl_i_t_nexus_reset(union ctl_io *io) 11900{ 11901 struct ctl_softc *softc = control_softc; 11902 struct ctl_lun *lun; 11903 uint32_t initidx, residx; 11904 11905 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11906 residx = ctl_get_resindex(&io->io_hdr.nexus); 11907 mtx_lock(&softc->ctl_lock); 11908 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11909 mtx_lock(&lun->lun_lock); 11910 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11911 io->io_hdr.nexus.initid.id, 11912 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11913#ifdef CTL_WITH_CA 11914 ctl_clear_mask(lun->have_ca, initidx); 11915#endif 11916 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 11917 lun->flags &= ~CTL_LUN_RESERVED; 11918 lun->pending_ua[initidx] |= CTL_UA_I_T_NEXUS_LOSS; 11919 mtx_unlock(&lun->lun_lock); 11920 } 11921 mtx_unlock(&softc->ctl_lock); 11922 return (0); 11923} 11924 11925static int 11926ctl_abort_task(union ctl_io *io) 11927{ 11928 union ctl_io *xio; 11929 struct ctl_lun *lun; 11930 struct ctl_softc *ctl_softc; 11931#if 0 11932 struct sbuf sb; 11933 char printbuf[128]; 11934#endif 11935 int found; 11936 uint32_t targ_lun; 11937 11938 ctl_softc = control_softc; 11939 found = 0; 11940 11941 /* 11942 * Look up the LUN. 11943 */ 11944 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11945 mtx_lock(&ctl_softc->ctl_lock); 11946 if ((targ_lun < CTL_MAX_LUNS) 11947 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11948 lun = ctl_softc->ctl_luns[targ_lun]; 11949 else { 11950 mtx_unlock(&ctl_softc->ctl_lock); 11951 return (1); 11952 } 11953 11954#if 0 11955 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11956 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11957#endif 11958 11959 mtx_lock(&lun->lun_lock); 11960 mtx_unlock(&ctl_softc->ctl_lock); 11961 /* 11962 * Run through the OOA queue and attempt to find the given I/O. 11963 * The target port, initiator ID, tag type and tag number have to 11964 * match the values that we got from the initiator. If we have an 11965 * untagged command to abort, simply abort the first untagged command 11966 * we come to. We only allow one untagged command at a time of course. 11967 */ 11968#if 0 11969 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11970#endif 11971 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11972 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11973#if 0 11974 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11975 11976 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11977 lun->lun, xio->scsiio.tag_num, 11978 xio->scsiio.tag_type, 11979 (xio->io_hdr.blocked_links.tqe_prev 11980 == NULL) ? "" : " BLOCKED", 11981 (xio->io_hdr.flags & 11982 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11983 (xio->io_hdr.flags & 11984 CTL_FLAG_ABORT) ? " ABORT" : "", 11985 (xio->io_hdr.flags & 11986 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11987 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11988 sbuf_finish(&sb); 11989 printf("%s\n", sbuf_data(&sb)); 11990#endif 11991 11992 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 11993 && (xio->io_hdr.nexus.initid.id == 11994 io->io_hdr.nexus.initid.id)) { 11995 /* 11996 * If the abort says that the task is untagged, the 11997 * task in the queue must be untagged. Otherwise, 11998 * we just check to see whether the tag numbers 11999 * match. This is because the QLogic firmware 12000 * doesn't pass back the tag type in an abort 12001 * request. 12002 */ 12003#if 0 12004 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12005 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12006 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12007#endif 12008 /* 12009 * XXX KDM we've got problems with FC, because it 12010 * doesn't send down a tag type with aborts. So we 12011 * can only really go by the tag number... 12012 * This may cause problems with parallel SCSI. 12013 * Need to figure that out!! 12014 */ 12015 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12016 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12017 found = 1; 12018 if ((io->io_hdr.flags & 12019 CTL_FLAG_FROM_OTHER_SC) == 0 && 12020 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12021 union ctl_ha_msg msg_info; 12022 12023 io->io_hdr.flags |= 12024 CTL_FLAG_SENT_2OTHER_SC; 12025 msg_info.hdr.nexus = io->io_hdr.nexus; 12026 msg_info.task.task_action = 12027 CTL_TASK_ABORT_TASK; 12028 msg_info.task.tag_num = 12029 io->taskio.tag_num; 12030 msg_info.task.tag_type = 12031 io->taskio.tag_type; 12032 msg_info.hdr.msg_type = 12033 CTL_MSG_MANAGE_TASKS; 12034 msg_info.hdr.original_sc = NULL; 12035 msg_info.hdr.serializing_sc = NULL; 12036#if 0 12037 printf("Sent Abort to other side\n"); 12038#endif 12039 if (CTL_HA_STATUS_SUCCESS != 12040 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12041 (void *)&msg_info, 12042 sizeof(msg_info), 0)) { 12043 } 12044 } 12045#if 0 12046 printf("ctl_abort_task: found I/O to abort\n"); 12047#endif 12048 break; 12049 } 12050 } 12051 } 12052 mtx_unlock(&lun->lun_lock); 12053 12054 if (found == 0) { 12055 /* 12056 * This isn't really an error. It's entirely possible for 12057 * the abort and command completion to cross on the wire. 12058 * This is more of an informative/diagnostic error. 12059 */ 12060#if 0 12061 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12062 "%d:%d:%d:%d tag %d type %d\n", 12063 io->io_hdr.nexus.initid.id, 12064 io->io_hdr.nexus.targ_port, 12065 io->io_hdr.nexus.targ_target.id, 12066 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12067 io->taskio.tag_type); 12068#endif 12069 } 12070 return (0); 12071} 12072 12073static void 12074ctl_run_task(union ctl_io *io) 12075{ 12076 struct ctl_softc *ctl_softc = control_softc; 12077 int retval = 1; 12078 const char *task_desc; 12079 12080 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12081 12082 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12083 ("ctl_run_task: Unextected io_type %d\n", 12084 io->io_hdr.io_type)); 12085 12086 task_desc = ctl_scsi_task_string(&io->taskio); 12087 if (task_desc != NULL) { 12088#ifdef NEEDTOPORT 12089 csevent_log(CSC_CTL | CSC_SHELF_SW | 12090 CTL_TASK_REPORT, 12091 csevent_LogType_Trace, 12092 csevent_Severity_Information, 12093 csevent_AlertLevel_Green, 12094 csevent_FRU_Firmware, 12095 csevent_FRU_Unknown, 12096 "CTL: received task: %s",task_desc); 12097#endif 12098 } else { 12099#ifdef NEEDTOPORT 12100 csevent_log(CSC_CTL | CSC_SHELF_SW | 12101 CTL_TASK_REPORT, 12102 csevent_LogType_Trace, 12103 csevent_Severity_Information, 12104 csevent_AlertLevel_Green, 12105 csevent_FRU_Firmware, 12106 csevent_FRU_Unknown, 12107 "CTL: received unknown task " 12108 "type: %d (%#x)", 12109 io->taskio.task_action, 12110 io->taskio.task_action); 12111#endif 12112 } 12113 switch (io->taskio.task_action) { 12114 case CTL_TASK_ABORT_TASK: 12115 retval = ctl_abort_task(io); 12116 break; 12117 case CTL_TASK_ABORT_TASK_SET: 12118 case CTL_TASK_CLEAR_TASK_SET: 12119 retval = ctl_abort_task_set(io); 12120 break; 12121 case CTL_TASK_CLEAR_ACA: 12122 break; 12123 case CTL_TASK_I_T_NEXUS_RESET: 12124 retval = ctl_i_t_nexus_reset(io); 12125 break; 12126 case CTL_TASK_LUN_RESET: { 12127 struct ctl_lun *lun; 12128 uint32_t targ_lun; 12129 12130 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12131 mtx_lock(&ctl_softc->ctl_lock); 12132 if ((targ_lun < CTL_MAX_LUNS) 12133 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12134 lun = ctl_softc->ctl_luns[targ_lun]; 12135 else { 12136 mtx_unlock(&ctl_softc->ctl_lock); 12137 retval = 1; 12138 break; 12139 } 12140 12141 if (!(io->io_hdr.flags & 12142 CTL_FLAG_FROM_OTHER_SC)) { 12143 union ctl_ha_msg msg_info; 12144 12145 io->io_hdr.flags |= 12146 CTL_FLAG_SENT_2OTHER_SC; 12147 msg_info.hdr.msg_type = 12148 CTL_MSG_MANAGE_TASKS; 12149 msg_info.hdr.nexus = io->io_hdr.nexus; 12150 msg_info.task.task_action = 12151 CTL_TASK_LUN_RESET; 12152 msg_info.hdr.original_sc = NULL; 12153 msg_info.hdr.serializing_sc = NULL; 12154 if (CTL_HA_STATUS_SUCCESS != 12155 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12156 (void *)&msg_info, 12157 sizeof(msg_info), 0)) { 12158 } 12159 } 12160 12161 retval = ctl_lun_reset(lun, io, 12162 CTL_UA_LUN_RESET); 12163 mtx_unlock(&ctl_softc->ctl_lock); 12164 break; 12165 } 12166 case CTL_TASK_TARGET_RESET: 12167 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12168 break; 12169 case CTL_TASK_BUS_RESET: 12170 retval = ctl_bus_reset(ctl_softc, io); 12171 break; 12172 case CTL_TASK_PORT_LOGIN: 12173 break; 12174 case CTL_TASK_PORT_LOGOUT: 12175 break; 12176 default: 12177 printf("ctl_run_task: got unknown task management event %d\n", 12178 io->taskio.task_action); 12179 break; 12180 } 12181 if (retval == 0) 12182 io->io_hdr.status = CTL_SUCCESS; 12183 else 12184 io->io_hdr.status = CTL_ERROR; 12185 ctl_done(io); 12186} 12187 12188/* 12189 * For HA operation. Handle commands that come in from the other 12190 * controller. 12191 */ 12192static void 12193ctl_handle_isc(union ctl_io *io) 12194{ 12195 int free_io; 12196 struct ctl_lun *lun; 12197 struct ctl_softc *ctl_softc; 12198 uint32_t targ_lun; 12199 12200 ctl_softc = control_softc; 12201 12202 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12203 lun = ctl_softc->ctl_luns[targ_lun]; 12204 12205 switch (io->io_hdr.msg_type) { 12206 case CTL_MSG_SERIALIZE: 12207 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12208 break; 12209 case CTL_MSG_R2R: { 12210 const struct ctl_cmd_entry *entry; 12211 12212 /* 12213 * This is only used in SER_ONLY mode. 12214 */ 12215 free_io = 0; 12216 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12217 mtx_lock(&lun->lun_lock); 12218 if (ctl_scsiio_lun_check(ctl_softc, lun, 12219 entry, (struct ctl_scsiio *)io) != 0) { 12220 mtx_unlock(&lun->lun_lock); 12221 ctl_done(io); 12222 break; 12223 } 12224 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12225 mtx_unlock(&lun->lun_lock); 12226 ctl_enqueue_rtr(io); 12227 break; 12228 } 12229 case CTL_MSG_FINISH_IO: 12230 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12231 free_io = 0; 12232 ctl_done(io); 12233 } else { 12234 free_io = 1; 12235 mtx_lock(&lun->lun_lock); 12236 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12237 ooa_links); 12238 ctl_check_blocked(lun); 12239 mtx_unlock(&lun->lun_lock); 12240 } 12241 break; 12242 case CTL_MSG_PERS_ACTION: 12243 ctl_hndl_per_res_out_on_other_sc( 12244 (union ctl_ha_msg *)&io->presio.pr_msg); 12245 free_io = 1; 12246 break; 12247 case CTL_MSG_BAD_JUJU: 12248 free_io = 0; 12249 ctl_done(io); 12250 break; 12251 case CTL_MSG_DATAMOVE: 12252 /* Only used in XFER mode */ 12253 free_io = 0; 12254 ctl_datamove_remote(io); 12255 break; 12256 case CTL_MSG_DATAMOVE_DONE: 12257 /* Only used in XFER mode */ 12258 free_io = 0; 12259 io->scsiio.be_move_done(io); 12260 break; 12261 default: 12262 free_io = 1; 12263 printf("%s: Invalid message type %d\n", 12264 __func__, io->io_hdr.msg_type); 12265 break; 12266 } 12267 if (free_io) 12268 ctl_free_io(io); 12269 12270} 12271 12272 12273/* 12274 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12275 * there is no match. 12276 */ 12277static ctl_lun_error_pattern 12278ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12279{ 12280 const struct ctl_cmd_entry *entry; 12281 ctl_lun_error_pattern filtered_pattern, pattern; 12282 12283 pattern = desc->error_pattern; 12284 12285 /* 12286 * XXX KDM we need more data passed into this function to match a 12287 * custom pattern, and we actually need to implement custom pattern 12288 * matching. 12289 */ 12290 if (pattern & CTL_LUN_PAT_CMD) 12291 return (CTL_LUN_PAT_CMD); 12292 12293 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12294 return (CTL_LUN_PAT_ANY); 12295 12296 entry = ctl_get_cmd_entry(ctsio, NULL); 12297 12298 filtered_pattern = entry->pattern & pattern; 12299 12300 /* 12301 * If the user requested specific flags in the pattern (e.g. 12302 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12303 * flags. 12304 * 12305 * If the user did not specify any flags, it doesn't matter whether 12306 * or not the command supports the flags. 12307 */ 12308 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12309 (pattern & ~CTL_LUN_PAT_MASK)) 12310 return (CTL_LUN_PAT_NONE); 12311 12312 /* 12313 * If the user asked for a range check, see if the requested LBA 12314 * range overlaps with this command's LBA range. 12315 */ 12316 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12317 uint64_t lba1; 12318 uint64_t len1; 12319 ctl_action action; 12320 int retval; 12321 12322 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12323 if (retval != 0) 12324 return (CTL_LUN_PAT_NONE); 12325 12326 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12327 desc->lba_range.len); 12328 /* 12329 * A "pass" means that the LBA ranges don't overlap, so 12330 * this doesn't match the user's range criteria. 12331 */ 12332 if (action == CTL_ACTION_PASS) 12333 return (CTL_LUN_PAT_NONE); 12334 } 12335 12336 return (filtered_pattern); 12337} 12338 12339static void 12340ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12341{ 12342 struct ctl_error_desc *desc, *desc2; 12343 12344 mtx_assert(&lun->lun_lock, MA_OWNED); 12345 12346 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12347 ctl_lun_error_pattern pattern; 12348 /* 12349 * Check to see whether this particular command matches 12350 * the pattern in the descriptor. 12351 */ 12352 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12353 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12354 continue; 12355 12356 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12357 case CTL_LUN_INJ_ABORTED: 12358 ctl_set_aborted(&io->scsiio); 12359 break; 12360 case CTL_LUN_INJ_MEDIUM_ERR: 12361 ctl_set_medium_error(&io->scsiio); 12362 break; 12363 case CTL_LUN_INJ_UA: 12364 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12365 * OCCURRED */ 12366 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12367 break; 12368 case CTL_LUN_INJ_CUSTOM: 12369 /* 12370 * We're assuming the user knows what he is doing. 12371 * Just copy the sense information without doing 12372 * checks. 12373 */ 12374 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12375 ctl_min(sizeof(desc->custom_sense), 12376 sizeof(io->scsiio.sense_data))); 12377 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12378 io->scsiio.sense_len = SSD_FULL_SIZE; 12379 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12380 break; 12381 case CTL_LUN_INJ_NONE: 12382 default: 12383 /* 12384 * If this is an error injection type we don't know 12385 * about, clear the continuous flag (if it is set) 12386 * so it will get deleted below. 12387 */ 12388 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12389 break; 12390 } 12391 /* 12392 * By default, each error injection action is a one-shot 12393 */ 12394 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12395 continue; 12396 12397 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12398 12399 free(desc, M_CTL); 12400 } 12401} 12402 12403#ifdef CTL_IO_DELAY 12404static void 12405ctl_datamove_timer_wakeup(void *arg) 12406{ 12407 union ctl_io *io; 12408 12409 io = (union ctl_io *)arg; 12410 12411 ctl_datamove(io); 12412} 12413#endif /* CTL_IO_DELAY */ 12414 12415void 12416ctl_datamove(union ctl_io *io) 12417{ 12418 void (*fe_datamove)(union ctl_io *io); 12419 12420 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12421 12422 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12423 12424#ifdef CTL_TIME_IO 12425 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12426 char str[256]; 12427 char path_str[64]; 12428 struct sbuf sb; 12429 12430 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12431 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12432 12433 sbuf_cat(&sb, path_str); 12434 switch (io->io_hdr.io_type) { 12435 case CTL_IO_SCSI: 12436 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12437 sbuf_printf(&sb, "\n"); 12438 sbuf_cat(&sb, path_str); 12439 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12440 io->scsiio.tag_num, io->scsiio.tag_type); 12441 break; 12442 case CTL_IO_TASK: 12443 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12444 "Tag Type: %d\n", io->taskio.task_action, 12445 io->taskio.tag_num, io->taskio.tag_type); 12446 break; 12447 default: 12448 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12449 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12450 break; 12451 } 12452 sbuf_cat(&sb, path_str); 12453 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12454 (intmax_t)time_uptime - io->io_hdr.start_time); 12455 sbuf_finish(&sb); 12456 printf("%s", sbuf_data(&sb)); 12457 } 12458#endif /* CTL_TIME_IO */ 12459 12460#ifdef CTL_IO_DELAY 12461 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12462 struct ctl_lun *lun; 12463 12464 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12465 12466 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12467 } else { 12468 struct ctl_lun *lun; 12469 12470 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12471 if ((lun != NULL) 12472 && (lun->delay_info.datamove_delay > 0)) { 12473 struct callout *callout; 12474 12475 callout = (struct callout *)&io->io_hdr.timer_bytes; 12476 callout_init(callout, /*mpsafe*/ 1); 12477 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12478 callout_reset(callout, 12479 lun->delay_info.datamove_delay * hz, 12480 ctl_datamove_timer_wakeup, io); 12481 if (lun->delay_info.datamove_type == 12482 CTL_DELAY_TYPE_ONESHOT) 12483 lun->delay_info.datamove_delay = 0; 12484 return; 12485 } 12486 } 12487#endif 12488 12489 /* 12490 * This command has been aborted. Set the port status, so we fail 12491 * the data move. 12492 */ 12493 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12494 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12495 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12496 io->io_hdr.nexus.targ_port, 12497 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12498 io->io_hdr.nexus.targ_lun); 12499 io->io_hdr.port_status = 31337; 12500 /* 12501 * Note that the backend, in this case, will get the 12502 * callback in its context. In other cases it may get 12503 * called in the frontend's interrupt thread context. 12504 */ 12505 io->scsiio.be_move_done(io); 12506 return; 12507 } 12508 12509 /* Don't confuse frontend with zero length data move. */ 12510 if (io->scsiio.kern_data_len == 0) { 12511 io->scsiio.be_move_done(io); 12512 return; 12513 } 12514 12515 /* 12516 * If we're in XFER mode and this I/O is from the other shelf 12517 * controller, we need to send the DMA to the other side to 12518 * actually transfer the data to/from the host. In serialize only 12519 * mode the transfer happens below CTL and ctl_datamove() is only 12520 * called on the machine that originally received the I/O. 12521 */ 12522 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12523 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12524 union ctl_ha_msg msg; 12525 uint32_t sg_entries_sent; 12526 int do_sg_copy; 12527 int i; 12528 12529 memset(&msg, 0, sizeof(msg)); 12530 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12531 msg.hdr.original_sc = io->io_hdr.original_sc; 12532 msg.hdr.serializing_sc = io; 12533 msg.hdr.nexus = io->io_hdr.nexus; 12534 msg.dt.flags = io->io_hdr.flags; 12535 /* 12536 * We convert everything into a S/G list here. We can't 12537 * pass by reference, only by value between controllers. 12538 * So we can't pass a pointer to the S/G list, only as many 12539 * S/G entries as we can fit in here. If it's possible for 12540 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12541 * then we need to break this up into multiple transfers. 12542 */ 12543 if (io->scsiio.kern_sg_entries == 0) { 12544 msg.dt.kern_sg_entries = 1; 12545 /* 12546 * If this is in cached memory, flush the cache 12547 * before we send the DMA request to the other 12548 * controller. We want to do this in either the 12549 * read or the write case. The read case is 12550 * straightforward. In the write case, we want to 12551 * make sure nothing is in the local cache that 12552 * could overwrite the DMAed data. 12553 */ 12554 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12555 /* 12556 * XXX KDM use bus_dmamap_sync() here. 12557 */ 12558 } 12559 12560 /* 12561 * Convert to a physical address if this is a 12562 * virtual address. 12563 */ 12564 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12565 msg.dt.sg_list[0].addr = 12566 io->scsiio.kern_data_ptr; 12567 } else { 12568 /* 12569 * XXX KDM use busdma here! 12570 */ 12571#if 0 12572 msg.dt.sg_list[0].addr = (void *) 12573 vtophys(io->scsiio.kern_data_ptr); 12574#endif 12575 } 12576 12577 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12578 do_sg_copy = 0; 12579 } else { 12580 struct ctl_sg_entry *sgl; 12581 12582 do_sg_copy = 1; 12583 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12584 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12585 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12586 /* 12587 * XXX KDM use bus_dmamap_sync() here. 12588 */ 12589 } 12590 } 12591 12592 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12593 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12594 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12595 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12596 msg.dt.sg_sequence = 0; 12597 12598 /* 12599 * Loop until we've sent all of the S/G entries. On the 12600 * other end, we'll recompose these S/G entries into one 12601 * contiguous list before passing it to the 12602 */ 12603 for (sg_entries_sent = 0; sg_entries_sent < 12604 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12605 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12606 sizeof(msg.dt.sg_list[0])), 12607 msg.dt.kern_sg_entries - sg_entries_sent); 12608 12609 if (do_sg_copy != 0) { 12610 struct ctl_sg_entry *sgl; 12611 int j; 12612 12613 sgl = (struct ctl_sg_entry *) 12614 io->scsiio.kern_data_ptr; 12615 /* 12616 * If this is in cached memory, flush the cache 12617 * before we send the DMA request to the other 12618 * controller. We want to do this in either 12619 * the * read or the write case. The read 12620 * case is straightforward. In the write 12621 * case, we want to make sure nothing is 12622 * in the local cache that could overwrite 12623 * the DMAed data. 12624 */ 12625 12626 for (i = sg_entries_sent, j = 0; 12627 i < msg.dt.cur_sg_entries; i++, j++) { 12628 if ((io->io_hdr.flags & 12629 CTL_FLAG_NO_DATASYNC) == 0) { 12630 /* 12631 * XXX KDM use bus_dmamap_sync() 12632 */ 12633 } 12634 if ((io->io_hdr.flags & 12635 CTL_FLAG_BUS_ADDR) == 0) { 12636 /* 12637 * XXX KDM use busdma. 12638 */ 12639#if 0 12640 msg.dt.sg_list[j].addr =(void *) 12641 vtophys(sgl[i].addr); 12642#endif 12643 } else { 12644 msg.dt.sg_list[j].addr = 12645 sgl[i].addr; 12646 } 12647 msg.dt.sg_list[j].len = sgl[i].len; 12648 } 12649 } 12650 12651 sg_entries_sent += msg.dt.cur_sg_entries; 12652 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12653 msg.dt.sg_last = 1; 12654 else 12655 msg.dt.sg_last = 0; 12656 12657 /* 12658 * XXX KDM drop and reacquire the lock here? 12659 */ 12660 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12661 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12662 /* 12663 * XXX do something here. 12664 */ 12665 } 12666 12667 msg.dt.sent_sg_entries = sg_entries_sent; 12668 } 12669 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12670 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12671 ctl_failover_io(io, /*have_lock*/ 0); 12672 12673 } else { 12674 12675 /* 12676 * Lookup the fe_datamove() function for this particular 12677 * front end. 12678 */ 12679 fe_datamove = 12680 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12681 12682 fe_datamove(io); 12683 } 12684} 12685 12686static void 12687ctl_send_datamove_done(union ctl_io *io, int have_lock) 12688{ 12689 union ctl_ha_msg msg; 12690 int isc_status; 12691 12692 memset(&msg, 0, sizeof(msg)); 12693 12694 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12695 msg.hdr.original_sc = io; 12696 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12697 msg.hdr.nexus = io->io_hdr.nexus; 12698 msg.hdr.status = io->io_hdr.status; 12699 msg.scsi.tag_num = io->scsiio.tag_num; 12700 msg.scsi.tag_type = io->scsiio.tag_type; 12701 msg.scsi.scsi_status = io->scsiio.scsi_status; 12702 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12703 sizeof(io->scsiio.sense_data)); 12704 msg.scsi.sense_len = io->scsiio.sense_len; 12705 msg.scsi.sense_residual = io->scsiio.sense_residual; 12706 msg.scsi.fetd_status = io->io_hdr.port_status; 12707 msg.scsi.residual = io->scsiio.residual; 12708 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12709 12710 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12711 ctl_failover_io(io, /*have_lock*/ have_lock); 12712 return; 12713 } 12714 12715 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12716 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12717 /* XXX do something if this fails */ 12718 } 12719 12720} 12721 12722/* 12723 * The DMA to the remote side is done, now we need to tell the other side 12724 * we're done so it can continue with its data movement. 12725 */ 12726static void 12727ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12728{ 12729 union ctl_io *io; 12730 12731 io = rq->context; 12732 12733 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12734 printf("%s: ISC DMA write failed with error %d", __func__, 12735 rq->ret); 12736 ctl_set_internal_failure(&io->scsiio, 12737 /*sks_valid*/ 1, 12738 /*retry_count*/ rq->ret); 12739 } 12740 12741 ctl_dt_req_free(rq); 12742 12743 /* 12744 * In this case, we had to malloc the memory locally. Free it. 12745 */ 12746 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12747 int i; 12748 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12749 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12750 } 12751 /* 12752 * The data is in local and remote memory, so now we need to send 12753 * status (good or back) back to the other side. 12754 */ 12755 ctl_send_datamove_done(io, /*have_lock*/ 0); 12756} 12757 12758/* 12759 * We've moved the data from the host/controller into local memory. Now we 12760 * need to push it over to the remote controller's memory. 12761 */ 12762static int 12763ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12764{ 12765 int retval; 12766 12767 retval = 0; 12768 12769 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12770 ctl_datamove_remote_write_cb); 12771 12772 return (retval); 12773} 12774 12775static void 12776ctl_datamove_remote_write(union ctl_io *io) 12777{ 12778 int retval; 12779 void (*fe_datamove)(union ctl_io *io); 12780 12781 /* 12782 * - Get the data from the host/HBA into local memory. 12783 * - DMA memory from the local controller to the remote controller. 12784 * - Send status back to the remote controller. 12785 */ 12786 12787 retval = ctl_datamove_remote_sgl_setup(io); 12788 if (retval != 0) 12789 return; 12790 12791 /* Switch the pointer over so the FETD knows what to do */ 12792 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12793 12794 /* 12795 * Use a custom move done callback, since we need to send completion 12796 * back to the other controller, not to the backend on this side. 12797 */ 12798 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12799 12800 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12801 12802 fe_datamove(io); 12803 12804 return; 12805 12806} 12807 12808static int 12809ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12810{ 12811#if 0 12812 char str[256]; 12813 char path_str[64]; 12814 struct sbuf sb; 12815#endif 12816 12817 /* 12818 * In this case, we had to malloc the memory locally. Free it. 12819 */ 12820 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12821 int i; 12822 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12823 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12824 } 12825 12826#if 0 12827 scsi_path_string(io, path_str, sizeof(path_str)); 12828 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12829 sbuf_cat(&sb, path_str); 12830 scsi_command_string(&io->scsiio, NULL, &sb); 12831 sbuf_printf(&sb, "\n"); 12832 sbuf_cat(&sb, path_str); 12833 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12834 io->scsiio.tag_num, io->scsiio.tag_type); 12835 sbuf_cat(&sb, path_str); 12836 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12837 io->io_hdr.flags, io->io_hdr.status); 12838 sbuf_finish(&sb); 12839 printk("%s", sbuf_data(&sb)); 12840#endif 12841 12842 12843 /* 12844 * The read is done, now we need to send status (good or bad) back 12845 * to the other side. 12846 */ 12847 ctl_send_datamove_done(io, /*have_lock*/ 0); 12848 12849 return (0); 12850} 12851 12852static void 12853ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12854{ 12855 union ctl_io *io; 12856 void (*fe_datamove)(union ctl_io *io); 12857 12858 io = rq->context; 12859 12860 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12861 printf("%s: ISC DMA read failed with error %d", __func__, 12862 rq->ret); 12863 ctl_set_internal_failure(&io->scsiio, 12864 /*sks_valid*/ 1, 12865 /*retry_count*/ rq->ret); 12866 } 12867 12868 ctl_dt_req_free(rq); 12869 12870 /* Switch the pointer over so the FETD knows what to do */ 12871 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12872 12873 /* 12874 * Use a custom move done callback, since we need to send completion 12875 * back to the other controller, not to the backend on this side. 12876 */ 12877 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12878 12879 /* XXX KDM add checks like the ones in ctl_datamove? */ 12880 12881 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12882 12883 fe_datamove(io); 12884} 12885 12886static int 12887ctl_datamove_remote_sgl_setup(union ctl_io *io) 12888{ 12889 struct ctl_sg_entry *local_sglist, *remote_sglist; 12890 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12891 struct ctl_softc *softc; 12892 int retval; 12893 int i; 12894 12895 retval = 0; 12896 softc = control_softc; 12897 12898 local_sglist = io->io_hdr.local_sglist; 12899 local_dma_sglist = io->io_hdr.local_dma_sglist; 12900 remote_sglist = io->io_hdr.remote_sglist; 12901 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12902 12903 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12904 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12905 local_sglist[i].len = remote_sglist[i].len; 12906 12907 /* 12908 * XXX Detect the situation where the RS-level I/O 12909 * redirector on the other side has already read the 12910 * data off of the AOR RS on this side, and 12911 * transferred it to remote (mirror) memory on the 12912 * other side. Since we already have the data in 12913 * memory here, we just need to use it. 12914 * 12915 * XXX KDM this can probably be removed once we 12916 * get the cache device code in and take the 12917 * current AOR implementation out. 12918 */ 12919#ifdef NEEDTOPORT 12920 if ((remote_sglist[i].addr >= 12921 (void *)vtophys(softc->mirr->addr)) 12922 && (remote_sglist[i].addr < 12923 ((void *)vtophys(softc->mirr->addr) + 12924 CacheMirrorOffset))) { 12925 local_sglist[i].addr = remote_sglist[i].addr - 12926 CacheMirrorOffset; 12927 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12928 CTL_FLAG_DATA_IN) 12929 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12930 } else { 12931 local_sglist[i].addr = remote_sglist[i].addr + 12932 CacheMirrorOffset; 12933 } 12934#endif 12935#if 0 12936 printf("%s: local %p, remote %p, len %d\n", 12937 __func__, local_sglist[i].addr, 12938 remote_sglist[i].addr, local_sglist[i].len); 12939#endif 12940 } 12941 } else { 12942 uint32_t len_to_go; 12943 12944 /* 12945 * In this case, we don't have automatically allocated 12946 * memory for this I/O on this controller. This typically 12947 * happens with internal CTL I/O -- e.g. inquiry, mode 12948 * sense, etc. Anything coming from RAIDCore will have 12949 * a mirror area available. 12950 */ 12951 len_to_go = io->scsiio.kern_data_len; 12952 12953 /* 12954 * Clear the no datasync flag, we have to use malloced 12955 * buffers. 12956 */ 12957 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12958 12959 /* 12960 * The difficult thing here is that the size of the various 12961 * S/G segments may be different than the size from the 12962 * remote controller. That'll make it harder when DMAing 12963 * the data back to the other side. 12964 */ 12965 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12966 sizeof(io->io_hdr.remote_sglist[0])) && 12967 (len_to_go > 0); i++) { 12968 local_sglist[i].len = ctl_min(len_to_go, 131072); 12969 CTL_SIZE_8B(local_dma_sglist[i].len, 12970 local_sglist[i].len); 12971 local_sglist[i].addr = 12972 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12973 12974 local_dma_sglist[i].addr = local_sglist[i].addr; 12975 12976 if (local_sglist[i].addr == NULL) { 12977 int j; 12978 12979 printf("malloc failed for %zd bytes!", 12980 local_dma_sglist[i].len); 12981 for (j = 0; j < i; j++) { 12982 free(local_sglist[j].addr, M_CTL); 12983 } 12984 ctl_set_internal_failure(&io->scsiio, 12985 /*sks_valid*/ 1, 12986 /*retry_count*/ 4857); 12987 retval = 1; 12988 goto bailout_error; 12989 12990 } 12991 /* XXX KDM do we need a sync here? */ 12992 12993 len_to_go -= local_sglist[i].len; 12994 } 12995 /* 12996 * Reset the number of S/G entries accordingly. The 12997 * original number of S/G entries is available in 12998 * rem_sg_entries. 12999 */ 13000 io->scsiio.kern_sg_entries = i; 13001 13002#if 0 13003 printf("%s: kern_sg_entries = %d\n", __func__, 13004 io->scsiio.kern_sg_entries); 13005 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13006 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13007 local_sglist[i].addr, local_sglist[i].len, 13008 local_dma_sglist[i].len); 13009#endif 13010 } 13011 13012 13013 return (retval); 13014 13015bailout_error: 13016 13017 ctl_send_datamove_done(io, /*have_lock*/ 0); 13018 13019 return (retval); 13020} 13021 13022static int 13023ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13024 ctl_ha_dt_cb callback) 13025{ 13026 struct ctl_ha_dt_req *rq; 13027 struct ctl_sg_entry *remote_sglist, *local_sglist; 13028 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13029 uint32_t local_used, remote_used, total_used; 13030 int retval; 13031 int i, j; 13032 13033 retval = 0; 13034 13035 rq = ctl_dt_req_alloc(); 13036 13037 /* 13038 * If we failed to allocate the request, and if the DMA didn't fail 13039 * anyway, set busy status. This is just a resource allocation 13040 * failure. 13041 */ 13042 if ((rq == NULL) 13043 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13044 ctl_set_busy(&io->scsiio); 13045 13046 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13047 13048 if (rq != NULL) 13049 ctl_dt_req_free(rq); 13050 13051 /* 13052 * The data move failed. We need to return status back 13053 * to the other controller. No point in trying to DMA 13054 * data to the remote controller. 13055 */ 13056 13057 ctl_send_datamove_done(io, /*have_lock*/ 0); 13058 13059 retval = 1; 13060 13061 goto bailout; 13062 } 13063 13064 local_sglist = io->io_hdr.local_sglist; 13065 local_dma_sglist = io->io_hdr.local_dma_sglist; 13066 remote_sglist = io->io_hdr.remote_sglist; 13067 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13068 local_used = 0; 13069 remote_used = 0; 13070 total_used = 0; 13071 13072 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13073 rq->ret = CTL_HA_STATUS_SUCCESS; 13074 rq->context = io; 13075 callback(rq); 13076 goto bailout; 13077 } 13078 13079 /* 13080 * Pull/push the data over the wire from/to the other controller. 13081 * This takes into account the possibility that the local and 13082 * remote sglists may not be identical in terms of the size of 13083 * the elements and the number of elements. 13084 * 13085 * One fundamental assumption here is that the length allocated for 13086 * both the local and remote sglists is identical. Otherwise, we've 13087 * essentially got a coding error of some sort. 13088 */ 13089 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13090 int isc_ret; 13091 uint32_t cur_len, dma_length; 13092 uint8_t *tmp_ptr; 13093 13094 rq->id = CTL_HA_DATA_CTL; 13095 rq->command = command; 13096 rq->context = io; 13097 13098 /* 13099 * Both pointers should be aligned. But it is possible 13100 * that the allocation length is not. They should both 13101 * also have enough slack left over at the end, though, 13102 * to round up to the next 8 byte boundary. 13103 */ 13104 cur_len = ctl_min(local_sglist[i].len - local_used, 13105 remote_sglist[j].len - remote_used); 13106 13107 /* 13108 * In this case, we have a size issue and need to decrease 13109 * the size, except in the case where we actually have less 13110 * than 8 bytes left. In that case, we need to increase 13111 * the DMA length to get the last bit. 13112 */ 13113 if ((cur_len & 0x7) != 0) { 13114 if (cur_len > 0x7) { 13115 cur_len = cur_len - (cur_len & 0x7); 13116 dma_length = cur_len; 13117 } else { 13118 CTL_SIZE_8B(dma_length, cur_len); 13119 } 13120 13121 } else 13122 dma_length = cur_len; 13123 13124 /* 13125 * If we had to allocate memory for this I/O, instead of using 13126 * the non-cached mirror memory, we'll need to flush the cache 13127 * before trying to DMA to the other controller. 13128 * 13129 * We could end up doing this multiple times for the same 13130 * segment if we have a larger local segment than remote 13131 * segment. That shouldn't be an issue. 13132 */ 13133 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13134 /* 13135 * XXX KDM use bus_dmamap_sync() here. 13136 */ 13137 } 13138 13139 rq->size = dma_length; 13140 13141 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13142 tmp_ptr += local_used; 13143 13144 /* Use physical addresses when talking to ISC hardware */ 13145 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13146 /* XXX KDM use busdma */ 13147#if 0 13148 rq->local = vtophys(tmp_ptr); 13149#endif 13150 } else 13151 rq->local = tmp_ptr; 13152 13153 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13154 tmp_ptr += remote_used; 13155 rq->remote = tmp_ptr; 13156 13157 rq->callback = NULL; 13158 13159 local_used += cur_len; 13160 if (local_used >= local_sglist[i].len) { 13161 i++; 13162 local_used = 0; 13163 } 13164 13165 remote_used += cur_len; 13166 if (remote_used >= remote_sglist[j].len) { 13167 j++; 13168 remote_used = 0; 13169 } 13170 total_used += cur_len; 13171 13172 if (total_used >= io->scsiio.kern_data_len) 13173 rq->callback = callback; 13174 13175 if ((rq->size & 0x7) != 0) { 13176 printf("%s: warning: size %d is not on 8b boundary\n", 13177 __func__, rq->size); 13178 } 13179 if (((uintptr_t)rq->local & 0x7) != 0) { 13180 printf("%s: warning: local %p not on 8b boundary\n", 13181 __func__, rq->local); 13182 } 13183 if (((uintptr_t)rq->remote & 0x7) != 0) { 13184 printf("%s: warning: remote %p not on 8b boundary\n", 13185 __func__, rq->local); 13186 } 13187#if 0 13188 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13189 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13190 rq->local, rq->remote, rq->size); 13191#endif 13192 13193 isc_ret = ctl_dt_single(rq); 13194 if (isc_ret == CTL_HA_STATUS_WAIT) 13195 continue; 13196 13197 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13198 rq->ret = CTL_HA_STATUS_SUCCESS; 13199 } else { 13200 rq->ret = isc_ret; 13201 } 13202 callback(rq); 13203 goto bailout; 13204 } 13205 13206bailout: 13207 return (retval); 13208 13209} 13210 13211static void 13212ctl_datamove_remote_read(union ctl_io *io) 13213{ 13214 int retval; 13215 int i; 13216 13217 /* 13218 * This will send an error to the other controller in the case of a 13219 * failure. 13220 */ 13221 retval = ctl_datamove_remote_sgl_setup(io); 13222 if (retval != 0) 13223 return; 13224 13225 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13226 ctl_datamove_remote_read_cb); 13227 if ((retval != 0) 13228 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13229 /* 13230 * Make sure we free memory if there was an error.. The 13231 * ctl_datamove_remote_xfer() function will send the 13232 * datamove done message, or call the callback with an 13233 * error if there is a problem. 13234 */ 13235 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13236 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13237 } 13238 13239 return; 13240} 13241 13242/* 13243 * Process a datamove request from the other controller. This is used for 13244 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13245 * first. Once that is complete, the data gets DMAed into the remote 13246 * controller's memory. For reads, we DMA from the remote controller's 13247 * memory into our memory first, and then move it out to the FETD. 13248 */ 13249static void 13250ctl_datamove_remote(union ctl_io *io) 13251{ 13252 struct ctl_softc *softc; 13253 13254 softc = control_softc; 13255 13256 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13257 13258 /* 13259 * Note that we look for an aborted I/O here, but don't do some of 13260 * the other checks that ctl_datamove() normally does. 13261 * We don't need to run the datamove delay code, since that should 13262 * have been done if need be on the other controller. 13263 */ 13264 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13265 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13266 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13267 io->io_hdr.nexus.targ_port, 13268 io->io_hdr.nexus.targ_target.id, 13269 io->io_hdr.nexus.targ_lun); 13270 io->io_hdr.port_status = 31338; 13271 ctl_send_datamove_done(io, /*have_lock*/ 0); 13272 return; 13273 } 13274 13275 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13276 ctl_datamove_remote_write(io); 13277 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13278 ctl_datamove_remote_read(io); 13279 } else { 13280 union ctl_ha_msg msg; 13281 struct scsi_sense_data *sense; 13282 uint8_t sks[3]; 13283 int retry_count; 13284 13285 memset(&msg, 0, sizeof(msg)); 13286 13287 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13288 msg.hdr.status = CTL_SCSI_ERROR; 13289 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13290 13291 retry_count = 4243; 13292 13293 sense = &msg.scsi.sense_data; 13294 sks[0] = SSD_SCS_VALID; 13295 sks[1] = (retry_count >> 8) & 0xff; 13296 sks[2] = retry_count & 0xff; 13297 13298 /* "Internal target failure" */ 13299 scsi_set_sense_data(sense, 13300 /*sense_format*/ SSD_TYPE_NONE, 13301 /*current_error*/ 1, 13302 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13303 /*asc*/ 0x44, 13304 /*ascq*/ 0x00, 13305 /*type*/ SSD_ELEM_SKS, 13306 /*size*/ sizeof(sks), 13307 /*data*/ sks, 13308 SSD_ELEM_NONE); 13309 13310 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13311 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13312 ctl_failover_io(io, /*have_lock*/ 1); 13313 return; 13314 } 13315 13316 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13317 CTL_HA_STATUS_SUCCESS) { 13318 /* XXX KDM what to do if this fails? */ 13319 } 13320 return; 13321 } 13322 13323} 13324 13325static int 13326ctl_process_done(union ctl_io *io) 13327{ 13328 struct ctl_lun *lun; 13329 struct ctl_softc *ctl_softc = control_softc; 13330 void (*fe_done)(union ctl_io *io); 13331 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13332 13333 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13334 13335 fe_done = 13336 control_softc->ctl_ports[targ_port]->fe_done; 13337 13338#ifdef CTL_TIME_IO 13339 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13340 char str[256]; 13341 char path_str[64]; 13342 struct sbuf sb; 13343 13344 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13345 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13346 13347 sbuf_cat(&sb, path_str); 13348 switch (io->io_hdr.io_type) { 13349 case CTL_IO_SCSI: 13350 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13351 sbuf_printf(&sb, "\n"); 13352 sbuf_cat(&sb, path_str); 13353 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13354 io->scsiio.tag_num, io->scsiio.tag_type); 13355 break; 13356 case CTL_IO_TASK: 13357 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13358 "Tag Type: %d\n", io->taskio.task_action, 13359 io->taskio.tag_num, io->taskio.tag_type); 13360 break; 13361 default: 13362 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13363 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13364 break; 13365 } 13366 sbuf_cat(&sb, path_str); 13367 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13368 (intmax_t)time_uptime - io->io_hdr.start_time); 13369 sbuf_finish(&sb); 13370 printf("%s", sbuf_data(&sb)); 13371 } 13372#endif /* CTL_TIME_IO */ 13373 13374 switch (io->io_hdr.io_type) { 13375 case CTL_IO_SCSI: 13376 break; 13377 case CTL_IO_TASK: 13378 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13379 ctl_io_error_print(io, NULL); 13380 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13381 ctl_free_io(io); 13382 else 13383 fe_done(io); 13384 return (CTL_RETVAL_COMPLETE); 13385 default: 13386 panic("ctl_process_done: invalid io type %d\n", 13387 io->io_hdr.io_type); 13388 break; /* NOTREACHED */ 13389 } 13390 13391 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13392 if (lun == NULL) { 13393 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13394 io->io_hdr.nexus.targ_mapped_lun)); 13395 goto bailout; 13396 } 13397 13398 mtx_lock(&lun->lun_lock); 13399 13400 /* 13401 * Check to see if we have any errors to inject here. We only 13402 * inject errors for commands that don't already have errors set. 13403 */ 13404 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13405 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13406 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13407 ctl_inject_error(lun, io); 13408 13409 /* 13410 * XXX KDM how do we treat commands that aren't completed 13411 * successfully? 13412 * 13413 * XXX KDM should we also track I/O latency? 13414 */ 13415 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13416 io->io_hdr.io_type == CTL_IO_SCSI) { 13417#ifdef CTL_TIME_IO 13418 struct bintime cur_bt; 13419#endif 13420 int type; 13421 13422 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13423 CTL_FLAG_DATA_IN) 13424 type = CTL_STATS_READ; 13425 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13426 CTL_FLAG_DATA_OUT) 13427 type = CTL_STATS_WRITE; 13428 else 13429 type = CTL_STATS_NO_IO; 13430 13431 lun->stats.ports[targ_port].bytes[type] += 13432 io->scsiio.kern_total_len; 13433 lun->stats.ports[targ_port].operations[type]++; 13434#ifdef CTL_TIME_IO 13435 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13436 &io->io_hdr.dma_bt); 13437 lun->stats.ports[targ_port].num_dmas[type] += 13438 io->io_hdr.num_dmas; 13439 getbintime(&cur_bt); 13440 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13441 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13442#endif 13443 } 13444 13445 /* 13446 * Remove this from the OOA queue. 13447 */ 13448 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13449 13450 /* 13451 * Run through the blocked queue on this LUN and see if anything 13452 * has become unblocked, now that this transaction is done. 13453 */ 13454 ctl_check_blocked(lun); 13455 13456 /* 13457 * If the LUN has been invalidated, free it if there is nothing 13458 * left on its OOA queue. 13459 */ 13460 if ((lun->flags & CTL_LUN_INVALID) 13461 && TAILQ_EMPTY(&lun->ooa_queue)) { 13462 mtx_unlock(&lun->lun_lock); 13463 mtx_lock(&ctl_softc->ctl_lock); 13464 ctl_free_lun(lun); 13465 mtx_unlock(&ctl_softc->ctl_lock); 13466 } else 13467 mtx_unlock(&lun->lun_lock); 13468 13469bailout: 13470 13471 /* 13472 * If this command has been aborted, make sure we set the status 13473 * properly. The FETD is responsible for freeing the I/O and doing 13474 * whatever it needs to do to clean up its state. 13475 */ 13476 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13477 ctl_set_task_aborted(&io->scsiio); 13478 13479 /* 13480 * If enabled, print command error status. 13481 * We don't print UAs unless debugging was enabled explicitly. 13482 */ 13483 do { 13484 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13485 break; 13486 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13487 break; 13488 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13489 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13490 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13491 int error_code, sense_key, asc, ascq; 13492 13493 scsi_extract_sense_len(&io->scsiio.sense_data, 13494 io->scsiio.sense_len, &error_code, &sense_key, 13495 &asc, &ascq, /*show_errors*/ 0); 13496 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13497 break; 13498 } 13499 13500 ctl_io_error_print(io, NULL); 13501 } while (0); 13502 13503 /* 13504 * Tell the FETD or the other shelf controller we're done with this 13505 * command. Note that only SCSI commands get to this point. Task 13506 * management commands are completed above. 13507 * 13508 * We only send status to the other controller if we're in XFER 13509 * mode. In SER_ONLY mode, the I/O is done on the controller that 13510 * received the I/O (from CTL's perspective), and so the status is 13511 * generated there. 13512 * 13513 * XXX KDM if we hold the lock here, we could cause a deadlock 13514 * if the frontend comes back in in this context to queue 13515 * something. 13516 */ 13517 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13518 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13519 union ctl_ha_msg msg; 13520 13521 memset(&msg, 0, sizeof(msg)); 13522 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13523 msg.hdr.original_sc = io->io_hdr.original_sc; 13524 msg.hdr.nexus = io->io_hdr.nexus; 13525 msg.hdr.status = io->io_hdr.status; 13526 msg.scsi.scsi_status = io->scsiio.scsi_status; 13527 msg.scsi.tag_num = io->scsiio.tag_num; 13528 msg.scsi.tag_type = io->scsiio.tag_type; 13529 msg.scsi.sense_len = io->scsiio.sense_len; 13530 msg.scsi.sense_residual = io->scsiio.sense_residual; 13531 msg.scsi.residual = io->scsiio.residual; 13532 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13533 sizeof(io->scsiio.sense_data)); 13534 /* 13535 * We copy this whether or not this is an I/O-related 13536 * command. Otherwise, we'd have to go and check to see 13537 * whether it's a read/write command, and it really isn't 13538 * worth it. 13539 */ 13540 memcpy(&msg.scsi.lbalen, 13541 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13542 sizeof(msg.scsi.lbalen)); 13543 13544 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13545 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13546 /* XXX do something here */ 13547 } 13548 13549 ctl_free_io(io); 13550 } else 13551 fe_done(io); 13552 13553 return (CTL_RETVAL_COMPLETE); 13554} 13555 13556#ifdef CTL_WITH_CA 13557/* 13558 * Front end should call this if it doesn't do autosense. When the request 13559 * sense comes back in from the initiator, we'll dequeue this and send it. 13560 */ 13561int 13562ctl_queue_sense(union ctl_io *io) 13563{ 13564 struct ctl_lun *lun; 13565 struct ctl_softc *ctl_softc; 13566 uint32_t initidx, targ_lun; 13567 13568 ctl_softc = control_softc; 13569 13570 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13571 13572 /* 13573 * LUN lookup will likely move to the ctl_work_thread() once we 13574 * have our new queueing infrastructure (that doesn't put things on 13575 * a per-LUN queue initially). That is so that we can handle 13576 * things like an INQUIRY to a LUN that we don't have enabled. We 13577 * can't deal with that right now. 13578 */ 13579 mtx_lock(&ctl_softc->ctl_lock); 13580 13581 /* 13582 * If we don't have a LUN for this, just toss the sense 13583 * information. 13584 */ 13585 targ_lun = io->io_hdr.nexus.targ_lun; 13586 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13587 if ((targ_lun < CTL_MAX_LUNS) 13588 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13589 lun = ctl_softc->ctl_luns[targ_lun]; 13590 else 13591 goto bailout; 13592 13593 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13594 13595 mtx_lock(&lun->lun_lock); 13596 /* 13597 * Already have CA set for this LUN...toss the sense information. 13598 */ 13599 if (ctl_is_set(lun->have_ca, initidx)) { 13600 mtx_unlock(&lun->lun_lock); 13601 goto bailout; 13602 } 13603 13604 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13605 ctl_min(sizeof(lun->pending_sense[initidx]), 13606 sizeof(io->scsiio.sense_data))); 13607 ctl_set_mask(lun->have_ca, initidx); 13608 mtx_unlock(&lun->lun_lock); 13609 13610bailout: 13611 mtx_unlock(&ctl_softc->ctl_lock); 13612 13613 ctl_free_io(io); 13614 13615 return (CTL_RETVAL_COMPLETE); 13616} 13617#endif 13618 13619/* 13620 * Primary command inlet from frontend ports. All SCSI and task I/O 13621 * requests must go through this function. 13622 */ 13623int 13624ctl_queue(union ctl_io *io) 13625{ 13626 struct ctl_softc *ctl_softc; 13627 13628 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13629 13630 ctl_softc = control_softc; 13631 13632#ifdef CTL_TIME_IO 13633 io->io_hdr.start_time = time_uptime; 13634 getbintime(&io->io_hdr.start_bt); 13635#endif /* CTL_TIME_IO */ 13636 13637 /* Map FE-specific LUN ID into global one. */ 13638 io->io_hdr.nexus.targ_mapped_lun = 13639 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13640 13641 switch (io->io_hdr.io_type) { 13642 case CTL_IO_SCSI: 13643 case CTL_IO_TASK: 13644 if (ctl_debug & CTL_DEBUG_CDB) 13645 ctl_io_print(io); 13646 ctl_enqueue_incoming(io); 13647 break; 13648 default: 13649 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13650 return (EINVAL); 13651 } 13652 13653 return (CTL_RETVAL_COMPLETE); 13654} 13655 13656#ifdef CTL_IO_DELAY 13657static void 13658ctl_done_timer_wakeup(void *arg) 13659{ 13660 union ctl_io *io; 13661 13662 io = (union ctl_io *)arg; 13663 ctl_done(io); 13664} 13665#endif /* CTL_IO_DELAY */ 13666 13667void 13668ctl_done(union ctl_io *io) 13669{ 13670 struct ctl_softc *ctl_softc; 13671 13672 ctl_softc = control_softc; 13673 13674 /* 13675 * Enable this to catch duplicate completion issues. 13676 */ 13677#if 0 13678 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13679 printf("%s: type %d msg %d cdb %x iptl: " 13680 "%d:%d:%d:%d tag 0x%04x " 13681 "flag %#x status %x\n", 13682 __func__, 13683 io->io_hdr.io_type, 13684 io->io_hdr.msg_type, 13685 io->scsiio.cdb[0], 13686 io->io_hdr.nexus.initid.id, 13687 io->io_hdr.nexus.targ_port, 13688 io->io_hdr.nexus.targ_target.id, 13689 io->io_hdr.nexus.targ_lun, 13690 (io->io_hdr.io_type == 13691 CTL_IO_TASK) ? 13692 io->taskio.tag_num : 13693 io->scsiio.tag_num, 13694 io->io_hdr.flags, 13695 io->io_hdr.status); 13696 } else 13697 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13698#endif 13699 13700 /* 13701 * This is an internal copy of an I/O, and should not go through 13702 * the normal done processing logic. 13703 */ 13704 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13705 return; 13706 13707 /* 13708 * We need to send a msg to the serializing shelf to finish the IO 13709 * as well. We don't send a finish message to the other shelf if 13710 * this is a task management command. Task management commands 13711 * aren't serialized in the OOA queue, but rather just executed on 13712 * both shelf controllers for commands that originated on that 13713 * controller. 13714 */ 13715 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13716 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13717 union ctl_ha_msg msg_io; 13718 13719 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13720 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13721 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13722 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13723 } 13724 /* continue on to finish IO */ 13725 } 13726#ifdef CTL_IO_DELAY 13727 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13728 struct ctl_lun *lun; 13729 13730 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13731 13732 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13733 } else { 13734 struct ctl_lun *lun; 13735 13736 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13737 13738 if ((lun != NULL) 13739 && (lun->delay_info.done_delay > 0)) { 13740 struct callout *callout; 13741 13742 callout = (struct callout *)&io->io_hdr.timer_bytes; 13743 callout_init(callout, /*mpsafe*/ 1); 13744 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13745 callout_reset(callout, 13746 lun->delay_info.done_delay * hz, 13747 ctl_done_timer_wakeup, io); 13748 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13749 lun->delay_info.done_delay = 0; 13750 return; 13751 } 13752 } 13753#endif /* CTL_IO_DELAY */ 13754 13755 ctl_enqueue_done(io); 13756} 13757 13758int 13759ctl_isc(struct ctl_scsiio *ctsio) 13760{ 13761 struct ctl_lun *lun; 13762 int retval; 13763 13764 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13765 13766 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13767 13768 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13769 13770 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13771 13772 return (retval); 13773} 13774 13775 13776static void 13777ctl_work_thread(void *arg) 13778{ 13779 struct ctl_thread *thr = (struct ctl_thread *)arg; 13780 struct ctl_softc *softc = thr->ctl_softc; 13781 union ctl_io *io; 13782 int retval; 13783 13784 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13785 13786 for (;;) { 13787 retval = 0; 13788 13789 /* 13790 * We handle the queues in this order: 13791 * - ISC 13792 * - done queue (to free up resources, unblock other commands) 13793 * - RtR queue 13794 * - incoming queue 13795 * 13796 * If those queues are empty, we break out of the loop and 13797 * go to sleep. 13798 */ 13799 mtx_lock(&thr->queue_lock); 13800 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13801 if (io != NULL) { 13802 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13803 mtx_unlock(&thr->queue_lock); 13804 ctl_handle_isc(io); 13805 continue; 13806 } 13807 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13808 if (io != NULL) { 13809 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13810 /* clear any blocked commands, call fe_done */ 13811 mtx_unlock(&thr->queue_lock); 13812 retval = ctl_process_done(io); 13813 continue; 13814 } 13815 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13816 if (io != NULL) { 13817 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13818 mtx_unlock(&thr->queue_lock); 13819 if (io->io_hdr.io_type == CTL_IO_TASK) 13820 ctl_run_task(io); 13821 else 13822 ctl_scsiio_precheck(softc, &io->scsiio); 13823 continue; 13824 } 13825 if (!ctl_pause_rtr) { 13826 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13827 if (io != NULL) { 13828 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13829 mtx_unlock(&thr->queue_lock); 13830 retval = ctl_scsiio(&io->scsiio); 13831 if (retval != CTL_RETVAL_COMPLETE) 13832 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13833 continue; 13834 } 13835 } 13836 13837 /* Sleep until we have something to do. */ 13838 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13839 } 13840} 13841 13842static void 13843ctl_lun_thread(void *arg) 13844{ 13845 struct ctl_softc *softc = (struct ctl_softc *)arg; 13846 struct ctl_be_lun *be_lun; 13847 int retval; 13848 13849 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13850 13851 for (;;) { 13852 retval = 0; 13853 mtx_lock(&softc->ctl_lock); 13854 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13855 if (be_lun != NULL) { 13856 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13857 mtx_unlock(&softc->ctl_lock); 13858 ctl_create_lun(be_lun); 13859 continue; 13860 } 13861 13862 /* Sleep until we have something to do. */ 13863 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13864 PDROP | PRIBIO, "-", 0); 13865 } 13866} 13867 13868static void 13869ctl_thresh_thread(void *arg) 13870{ 13871 struct ctl_softc *softc = (struct ctl_softc *)arg; 13872 struct ctl_lun *lun; 13873 struct ctl_be_lun *be_lun; 13874 struct scsi_da_rw_recovery_page *rwpage; 13875 struct ctl_logical_block_provisioning_page *page; 13876 const char *attr; 13877 uint64_t thres, val; 13878 int i, e; 13879 13880 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13881 13882 for (;;) { 13883 mtx_lock(&softc->ctl_lock); 13884 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13885 be_lun = lun->be_lun; 13886 if ((lun->flags & CTL_LUN_DISABLED) || 13887 (lun->flags & CTL_LUN_OFFLINE) || 13888 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 13889 lun->backend->lun_attr == NULL) 13890 continue; 13891 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13892 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13893 continue; 13894 e = 0; 13895 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13896 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13897 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13898 continue; 13899 thres = scsi_4btoul(page->descr[i].count); 13900 thres <<= CTL_LBP_EXPONENT; 13901 switch (page->descr[i].resource) { 13902 case 0x01: 13903 attr = "blocksavail"; 13904 break; 13905 case 0x02: 13906 attr = "blocksused"; 13907 break; 13908 case 0xf1: 13909 attr = "poolblocksavail"; 13910 break; 13911 case 0xf2: 13912 attr = "poolblocksused"; 13913 break; 13914 default: 13915 continue; 13916 } 13917 mtx_unlock(&softc->ctl_lock); // XXX 13918 val = lun->backend->lun_attr( 13919 lun->be_lun->be_lun, attr); 13920 mtx_lock(&softc->ctl_lock); 13921 if (val == UINT64_MAX) 13922 continue; 13923 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13924 == SLBPPD_ARMING_INC) 13925 e |= (val >= thres); 13926 else 13927 e |= (val <= thres); 13928 } 13929 mtx_lock(&lun->lun_lock); 13930 if (e) { 13931 if (lun->lasttpt == 0 || 13932 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13933 lun->lasttpt = time_uptime; 13934 for (i = 0; i < CTL_MAX_INITIATORS; i++) 13935 lun->pending_ua[i] |= 13936 CTL_UA_THIN_PROV_THRES; 13937 } 13938 } else { 13939 lun->lasttpt = 0; 13940 for (i = 0; i < CTL_MAX_INITIATORS; i++) 13941 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES; 13942 } 13943 mtx_unlock(&lun->lun_lock); 13944 } 13945 mtx_unlock(&softc->ctl_lock); 13946 pause("-", CTL_LBP_PERIOD * hz); 13947 } 13948} 13949 13950static void 13951ctl_enqueue_incoming(union ctl_io *io) 13952{ 13953 struct ctl_softc *softc = control_softc; 13954 struct ctl_thread *thr; 13955 u_int idx; 13956 13957 idx = (io->io_hdr.nexus.targ_port * 127 + 13958 io->io_hdr.nexus.initid.id) % worker_threads; 13959 thr = &softc->threads[idx]; 13960 mtx_lock(&thr->queue_lock); 13961 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13962 mtx_unlock(&thr->queue_lock); 13963 wakeup(thr); 13964} 13965 13966static void 13967ctl_enqueue_rtr(union ctl_io *io) 13968{ 13969 struct ctl_softc *softc = control_softc; 13970 struct ctl_thread *thr; 13971 13972 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13973 mtx_lock(&thr->queue_lock); 13974 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13975 mtx_unlock(&thr->queue_lock); 13976 wakeup(thr); 13977} 13978 13979static void 13980ctl_enqueue_done(union ctl_io *io) 13981{ 13982 struct ctl_softc *softc = control_softc; 13983 struct ctl_thread *thr; 13984 13985 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13986 mtx_lock(&thr->queue_lock); 13987 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13988 mtx_unlock(&thr->queue_lock); 13989 wakeup(thr); 13990} 13991 13992static void 13993ctl_enqueue_isc(union ctl_io *io) 13994{ 13995 struct ctl_softc *softc = control_softc; 13996 struct ctl_thread *thr; 13997 13998 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13999 mtx_lock(&thr->queue_lock); 14000 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14001 mtx_unlock(&thr->queue_lock); 14002 wakeup(thr); 14003} 14004 14005/* Initialization and failover */ 14006 14007void 14008ctl_init_isc_msg(void) 14009{ 14010 printf("CTL: Still calling this thing\n"); 14011} 14012 14013/* 14014 * Init component 14015 * Initializes component into configuration defined by bootMode 14016 * (see hasc-sv.c) 14017 * returns hasc_Status: 14018 * OK 14019 * ERROR - fatal error 14020 */ 14021static ctl_ha_comp_status 14022ctl_isc_init(struct ctl_ha_component *c) 14023{ 14024 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14025 14026 c->status = ret; 14027 return ret; 14028} 14029 14030/* Start component 14031 * Starts component in state requested. If component starts successfully, 14032 * it must set its own state to the requestrd state 14033 * When requested state is HASC_STATE_HA, the component may refine it 14034 * by adding _SLAVE or _MASTER flags. 14035 * Currently allowed state transitions are: 14036 * UNKNOWN->HA - initial startup 14037 * UNKNOWN->SINGLE - initial startup when no parter detected 14038 * HA->SINGLE - failover 14039 * returns ctl_ha_comp_status: 14040 * OK - component successfully started in requested state 14041 * FAILED - could not start the requested state, failover may 14042 * be possible 14043 * ERROR - fatal error detected, no future startup possible 14044 */ 14045static ctl_ha_comp_status 14046ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14047{ 14048 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14049 14050 printf("%s: go\n", __func__); 14051 14052 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14053 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14054 control_softc->is_single = 0; 14055 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14056 != CTL_HA_STATUS_SUCCESS) { 14057 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14058 ret = CTL_HA_COMP_STATUS_ERROR; 14059 } 14060 } else if (CTL_HA_STATE_IS_HA(c->state) 14061 && CTL_HA_STATE_IS_SINGLE(state)){ 14062 // HA->SINGLE transition 14063 ctl_failover(); 14064 control_softc->is_single = 1; 14065 } else { 14066 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14067 c->state, state); 14068 ret = CTL_HA_COMP_STATUS_ERROR; 14069 } 14070 if (CTL_HA_STATE_IS_SINGLE(state)) 14071 control_softc->is_single = 1; 14072 14073 c->state = state; 14074 c->status = ret; 14075 return ret; 14076} 14077 14078/* 14079 * Quiesce component 14080 * The component must clear any error conditions (set status to OK) and 14081 * prepare itself to another Start call 14082 * returns ctl_ha_comp_status: 14083 * OK 14084 * ERROR 14085 */ 14086static ctl_ha_comp_status 14087ctl_isc_quiesce(struct ctl_ha_component *c) 14088{ 14089 int ret = CTL_HA_COMP_STATUS_OK; 14090 14091 ctl_pause_rtr = 1; 14092 c->status = ret; 14093 return ret; 14094} 14095 14096struct ctl_ha_component ctl_ha_component_ctlisc = 14097{ 14098 .name = "CTL ISC", 14099 .state = CTL_HA_STATE_UNKNOWN, 14100 .init = ctl_isc_init, 14101 .start = ctl_isc_start, 14102 .quiesce = ctl_isc_quiesce 14103}; 14104 14105/* 14106 * vim: ts=8 14107 */ 14108