ctl.c revision 275405
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 275405 2014-12-02 12:38:22Z 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 uint8_t ctl_pause_rtr; 360 361SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 362static int worker_threads = -1; 363SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 364 &worker_threads, 1, "Number of worker threads"); 365static int ctl_debug = CTL_DEBUG_NONE; 366SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 367 &ctl_debug, 0, "Enabled debug flags"); 368 369/* 370 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 371 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 372 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 373 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 374 */ 375#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 376 377static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 378 int param); 379static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 380static int ctl_init(void); 381void ctl_shutdown(void); 382static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 383static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 384static void ctl_ioctl_online(void *arg); 385static void ctl_ioctl_offline(void *arg); 386static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id); 387static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id); 388static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio); 389static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 390static int ctl_ioctl_submit_wait(union ctl_io *io); 391static void ctl_ioctl_datamove(union ctl_io *io); 392static void ctl_ioctl_done(union ctl_io *io); 393static void ctl_ioctl_hard_startstop_callback(void *arg, 394 struct cfi_metatask *metatask); 395static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask); 396static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 397 struct ctl_ooa *ooa_hdr, 398 struct ctl_ooa_entry *kern_entries); 399static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 400 struct thread *td); 401static uint32_t ctl_map_lun(int port_num, uint32_t lun); 402static uint32_t ctl_map_lun_back(int port_num, uint32_t lun); 403#ifdef unused 404static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, 405 uint32_t targ_target, uint32_t targ_lun, 406 int can_wait); 407static void ctl_kfree_io(union ctl_io *io); 408#endif /* unused */ 409static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 410 struct ctl_be_lun *be_lun, struct ctl_id target_id); 411static int ctl_free_lun(struct ctl_lun *lun); 412static void ctl_create_lun(struct ctl_be_lun *be_lun); 413/** 414static void ctl_failover_change_pages(struct ctl_softc *softc, 415 struct ctl_scsiio *ctsio, int master); 416**/ 417 418static int ctl_do_mode_select(union ctl_io *io); 419static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 420 uint64_t res_key, uint64_t sa_res_key, 421 uint8_t type, uint32_t residx, 422 struct ctl_scsiio *ctsio, 423 struct scsi_per_res_out *cdb, 424 struct scsi_per_res_out_parms* param); 425static void ctl_pro_preempt_other(struct ctl_lun *lun, 426 union ctl_ha_msg *msg); 427static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 428static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 429static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 430static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 431static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 432static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 433static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 434 int alloc_len); 435static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 436 int alloc_len); 437static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 438static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 439static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 440static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 441static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 442static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2); 443static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 444 union ctl_io *pending_io, union ctl_io *ooa_io); 445static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 446 union ctl_io *starting_io); 447static int ctl_check_blocked(struct ctl_lun *lun); 448static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, 449 struct ctl_lun *lun, 450 const struct ctl_cmd_entry *entry, 451 struct ctl_scsiio *ctsio); 452//static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc); 453static void ctl_failover(void); 454static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 455 struct ctl_scsiio *ctsio); 456static int ctl_scsiio(struct ctl_scsiio *ctsio); 457 458static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 459static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 460 ctl_ua_type ua_type); 461static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, 462 ctl_ua_type ua_type); 463static int ctl_abort_task(union ctl_io *io); 464static int ctl_abort_task_set(union ctl_io *io); 465static int ctl_i_t_nexus_reset(union ctl_io *io); 466static void ctl_run_task(union ctl_io *io); 467#ifdef CTL_IO_DELAY 468static void ctl_datamove_timer_wakeup(void *arg); 469static void ctl_done_timer_wakeup(void *arg); 470#endif /* CTL_IO_DELAY */ 471 472static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 473static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 474static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 475static void ctl_datamove_remote_write(union ctl_io *io); 476static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 477static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 478static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 479static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 480 ctl_ha_dt_cb callback); 481static void ctl_datamove_remote_read(union ctl_io *io); 482static void ctl_datamove_remote(union ctl_io *io); 483static int ctl_process_done(union ctl_io *io); 484static void ctl_lun_thread(void *arg); 485static void ctl_thresh_thread(void *arg); 486static void ctl_work_thread(void *arg); 487static void ctl_enqueue_incoming(union ctl_io *io); 488static void ctl_enqueue_rtr(union ctl_io *io); 489static void ctl_enqueue_done(union ctl_io *io); 490static void ctl_enqueue_isc(union ctl_io *io); 491static const struct ctl_cmd_entry * 492 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 493static const struct ctl_cmd_entry * 494 ctl_validate_command(struct ctl_scsiio *ctsio); 495static int ctl_cmd_applicable(uint8_t lun_type, 496 const struct ctl_cmd_entry *entry); 497 498/* 499 * Load the serialization table. This isn't very pretty, but is probably 500 * the easiest way to do it. 501 */ 502#include "ctl_ser_table.c" 503 504/* 505 * We only need to define open, close and ioctl routines for this driver. 506 */ 507static struct cdevsw ctl_cdevsw = { 508 .d_version = D_VERSION, 509 .d_flags = 0, 510 .d_open = ctl_open, 511 .d_close = ctl_close, 512 .d_ioctl = ctl_ioctl, 513 .d_name = "ctl", 514}; 515 516 517MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 518MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests"); 519 520static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 521 522static moduledata_t ctl_moduledata = { 523 "ctl", 524 ctl_module_event_handler, 525 NULL 526}; 527 528DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 529MODULE_VERSION(ctl, 1); 530 531static struct ctl_frontend ioctl_frontend = 532{ 533 .name = "ioctl", 534}; 535 536static void 537ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 538 union ctl_ha_msg *msg_info) 539{ 540 struct ctl_scsiio *ctsio; 541 542 if (msg_info->hdr.original_sc == NULL) { 543 printf("%s: original_sc == NULL!\n", __func__); 544 /* XXX KDM now what? */ 545 return; 546 } 547 548 ctsio = &msg_info->hdr.original_sc->scsiio; 549 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 550 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 551 ctsio->io_hdr.status = msg_info->hdr.status; 552 ctsio->scsi_status = msg_info->scsi.scsi_status; 553 ctsio->sense_len = msg_info->scsi.sense_len; 554 ctsio->sense_residual = msg_info->scsi.sense_residual; 555 ctsio->residual = msg_info->scsi.residual; 556 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 557 sizeof(ctsio->sense_data)); 558 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 559 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen)); 560 ctl_enqueue_isc((union ctl_io *)ctsio); 561} 562 563static void 564ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 565 union ctl_ha_msg *msg_info) 566{ 567 struct ctl_scsiio *ctsio; 568 569 if (msg_info->hdr.serializing_sc == NULL) { 570 printf("%s: serializing_sc == NULL!\n", __func__); 571 /* XXX KDM now what? */ 572 return; 573 } 574 575 ctsio = &msg_info->hdr.serializing_sc->scsiio; 576#if 0 577 /* 578 * Attempt to catch the situation where an I/O has 579 * been freed, and we're using it again. 580 */ 581 if (ctsio->io_hdr.io_type == 0xff) { 582 union ctl_io *tmp_io; 583 tmp_io = (union ctl_io *)ctsio; 584 printf("%s: %p use after free!\n", __func__, 585 ctsio); 586 printf("%s: type %d msg %d cdb %x iptl: " 587 "%d:%d:%d:%d tag 0x%04x " 588 "flag %#x status %x\n", 589 __func__, 590 tmp_io->io_hdr.io_type, 591 tmp_io->io_hdr.msg_type, 592 tmp_io->scsiio.cdb[0], 593 tmp_io->io_hdr.nexus.initid.id, 594 tmp_io->io_hdr.nexus.targ_port, 595 tmp_io->io_hdr.nexus.targ_target.id, 596 tmp_io->io_hdr.nexus.targ_lun, 597 (tmp_io->io_hdr.io_type == 598 CTL_IO_TASK) ? 599 tmp_io->taskio.tag_num : 600 tmp_io->scsiio.tag_num, 601 tmp_io->io_hdr.flags, 602 tmp_io->io_hdr.status); 603 } 604#endif 605 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 606 ctl_enqueue_isc((union ctl_io *)ctsio); 607} 608 609/* 610 * ISC (Inter Shelf Communication) event handler. Events from the HA 611 * subsystem come in here. 612 */ 613static void 614ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 615{ 616 struct ctl_softc *ctl_softc; 617 union ctl_io *io; 618 struct ctl_prio *presio; 619 ctl_ha_status isc_status; 620 621 ctl_softc = control_softc; 622 io = NULL; 623 624 625#if 0 626 printf("CTL: Isc Msg event %d\n", event); 627#endif 628 if (event == CTL_HA_EVT_MSG_RECV) { 629 union ctl_ha_msg msg_info; 630 631 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 632 sizeof(msg_info), /*wait*/ 0); 633#if 0 634 printf("CTL: msg_type %d\n", msg_info.msg_type); 635#endif 636 if (isc_status != 0) { 637 printf("Error receiving message, status = %d\n", 638 isc_status); 639 return; 640 } 641 642 switch (msg_info.hdr.msg_type) { 643 case CTL_MSG_SERIALIZE: 644#if 0 645 printf("Serialize\n"); 646#endif 647 io = ctl_alloc_io_nowait(ctl_softc->othersc_pool); 648 if (io == NULL) { 649 printf("ctl_isc_event_handler: can't allocate " 650 "ctl_io!\n"); 651 /* Bad Juju */ 652 /* Need to set busy and send msg back */ 653 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 654 msg_info.hdr.status = CTL_SCSI_ERROR; 655 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY; 656 msg_info.scsi.sense_len = 0; 657 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 658 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){ 659 } 660 goto bailout; 661 } 662 ctl_zero_io(io); 663 // populate ctsio from msg_info 664 io->io_hdr.io_type = CTL_IO_SCSI; 665 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 666 io->io_hdr.original_sc = msg_info.hdr.original_sc; 667#if 0 668 printf("pOrig %x\n", (int)msg_info.original_sc); 669#endif 670 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 671 CTL_FLAG_IO_ACTIVE; 672 /* 673 * If we're in serialization-only mode, we don't 674 * want to go through full done processing. Thus 675 * the COPY flag. 676 * 677 * XXX KDM add another flag that is more specific. 678 */ 679 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY) 680 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 681 io->io_hdr.nexus = msg_info.hdr.nexus; 682#if 0 683 printf("targ %d, port %d, iid %d, lun %d\n", 684 io->io_hdr.nexus.targ_target.id, 685 io->io_hdr.nexus.targ_port, 686 io->io_hdr.nexus.initid.id, 687 io->io_hdr.nexus.targ_lun); 688#endif 689 io->scsiio.tag_num = msg_info.scsi.tag_num; 690 io->scsiio.tag_type = msg_info.scsi.tag_type; 691 memcpy(io->scsiio.cdb, msg_info.scsi.cdb, 692 CTL_MAX_CDBLEN); 693 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 694 const struct ctl_cmd_entry *entry; 695 696 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 697 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 698 io->io_hdr.flags |= 699 entry->flags & CTL_FLAG_DATA_MASK; 700 } 701 ctl_enqueue_isc(io); 702 break; 703 704 /* Performed on the Originating SC, XFER mode only */ 705 case CTL_MSG_DATAMOVE: { 706 struct ctl_sg_entry *sgl; 707 int i, j; 708 709 io = msg_info.hdr.original_sc; 710 if (io == NULL) { 711 printf("%s: original_sc == NULL!\n", __func__); 712 /* XXX KDM do something here */ 713 break; 714 } 715 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 716 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 717 /* 718 * Keep track of this, we need to send it back over 719 * when the datamove is complete. 720 */ 721 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 722 723 if (msg_info.dt.sg_sequence == 0) { 724 /* 725 * XXX KDM we use the preallocated S/G list 726 * here, but we'll need to change this to 727 * dynamic allocation if we need larger S/G 728 * lists. 729 */ 730 if (msg_info.dt.kern_sg_entries > 731 sizeof(io->io_hdr.remote_sglist) / 732 sizeof(io->io_hdr.remote_sglist[0])) { 733 printf("%s: number of S/G entries " 734 "needed %u > allocated num %zd\n", 735 __func__, 736 msg_info.dt.kern_sg_entries, 737 sizeof(io->io_hdr.remote_sglist)/ 738 sizeof(io->io_hdr.remote_sglist[0])); 739 740 /* 741 * XXX KDM send a message back to 742 * the other side to shut down the 743 * DMA. The error will come back 744 * through via the normal channel. 745 */ 746 break; 747 } 748 sgl = io->io_hdr.remote_sglist; 749 memset(sgl, 0, 750 sizeof(io->io_hdr.remote_sglist)); 751 752 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 753 754 io->scsiio.kern_sg_entries = 755 msg_info.dt.kern_sg_entries; 756 io->scsiio.rem_sg_entries = 757 msg_info.dt.kern_sg_entries; 758 io->scsiio.kern_data_len = 759 msg_info.dt.kern_data_len; 760 io->scsiio.kern_total_len = 761 msg_info.dt.kern_total_len; 762 io->scsiio.kern_data_resid = 763 msg_info.dt.kern_data_resid; 764 io->scsiio.kern_rel_offset = 765 msg_info.dt.kern_rel_offset; 766 /* 767 * Clear out per-DMA flags. 768 */ 769 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK; 770 /* 771 * Add per-DMA flags that are set for this 772 * particular DMA request. 773 */ 774 io->io_hdr.flags |= msg_info.dt.flags & 775 CTL_FLAG_RDMA_MASK; 776 } else 777 sgl = (struct ctl_sg_entry *) 778 io->scsiio.kern_data_ptr; 779 780 for (i = msg_info.dt.sent_sg_entries, j = 0; 781 i < (msg_info.dt.sent_sg_entries + 782 msg_info.dt.cur_sg_entries); i++, j++) { 783 sgl[i].addr = msg_info.dt.sg_list[j].addr; 784 sgl[i].len = msg_info.dt.sg_list[j].len; 785 786#if 0 787 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n", 788 __func__, 789 msg_info.dt.sg_list[j].addr, 790 msg_info.dt.sg_list[j].len, 791 sgl[i].addr, sgl[i].len, j, i); 792#endif 793 } 794#if 0 795 memcpy(&sgl[msg_info.dt.sent_sg_entries], 796 msg_info.dt.sg_list, 797 sizeof(*sgl) * msg_info.dt.cur_sg_entries); 798#endif 799 800 /* 801 * If this is the last piece of the I/O, we've got 802 * the full S/G list. Queue processing in the thread. 803 * Otherwise wait for the next piece. 804 */ 805 if (msg_info.dt.sg_last != 0) 806 ctl_enqueue_isc(io); 807 break; 808 } 809 /* Performed on the Serializing (primary) SC, XFER mode only */ 810 case CTL_MSG_DATAMOVE_DONE: { 811 if (msg_info.hdr.serializing_sc == NULL) { 812 printf("%s: serializing_sc == NULL!\n", 813 __func__); 814 /* XXX KDM now what? */ 815 break; 816 } 817 /* 818 * We grab the sense information here in case 819 * there was a failure, so we can return status 820 * back to the initiator. 821 */ 822 io = msg_info.hdr.serializing_sc; 823 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 824 io->io_hdr.status = msg_info.hdr.status; 825 io->scsiio.scsi_status = msg_info.scsi.scsi_status; 826 io->scsiio.sense_len = msg_info.scsi.sense_len; 827 io->scsiio.sense_residual =msg_info.scsi.sense_residual; 828 io->io_hdr.port_status = msg_info.scsi.fetd_status; 829 io->scsiio.residual = msg_info.scsi.residual; 830 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data, 831 sizeof(io->scsiio.sense_data)); 832 ctl_enqueue_isc(io); 833 break; 834 } 835 836 /* Preformed on Originating SC, SER_ONLY mode */ 837 case CTL_MSG_R2R: 838 io = msg_info.hdr.original_sc; 839 if (io == NULL) { 840 printf("%s: Major Bummer\n", __func__); 841 return; 842 } else { 843#if 0 844 printf("pOrig %x\n",(int) ctsio); 845#endif 846 } 847 io->io_hdr.msg_type = CTL_MSG_R2R; 848 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc; 849 ctl_enqueue_isc(io); 850 break; 851 852 /* 853 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 854 * mode. 855 * Performed on the Originating (i.e. secondary) SC in XFER 856 * mode 857 */ 858 case CTL_MSG_FINISH_IO: 859 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) 860 ctl_isc_handler_finish_xfer(ctl_softc, 861 &msg_info); 862 else 863 ctl_isc_handler_finish_ser_only(ctl_softc, 864 &msg_info); 865 break; 866 867 /* Preformed on Originating SC */ 868 case CTL_MSG_BAD_JUJU: 869 io = msg_info.hdr.original_sc; 870 if (io == NULL) { 871 printf("%s: Bad JUJU!, original_sc is NULL!\n", 872 __func__); 873 break; 874 } 875 ctl_copy_sense_data(&msg_info, io); 876 /* 877 * IO should have already been cleaned up on other 878 * SC so clear this flag so we won't send a message 879 * back to finish the IO there. 880 */ 881 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 882 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 883 884 /* io = msg_info.hdr.serializing_sc; */ 885 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 886 ctl_enqueue_isc(io); 887 break; 888 889 /* Handle resets sent from the other side */ 890 case CTL_MSG_MANAGE_TASKS: { 891 struct ctl_taskio *taskio; 892 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait( 893 ctl_softc->othersc_pool); 894 if (taskio == NULL) { 895 printf("ctl_isc_event_handler: can't allocate " 896 "ctl_io!\n"); 897 /* Bad Juju */ 898 /* should I just call the proper reset func 899 here??? */ 900 goto bailout; 901 } 902 ctl_zero_io((union ctl_io *)taskio); 903 taskio->io_hdr.io_type = CTL_IO_TASK; 904 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 905 taskio->io_hdr.nexus = msg_info.hdr.nexus; 906 taskio->task_action = msg_info.task.task_action; 907 taskio->tag_num = msg_info.task.tag_num; 908 taskio->tag_type = msg_info.task.tag_type; 909#ifdef CTL_TIME_IO 910 taskio->io_hdr.start_time = time_uptime; 911 getbintime(&taskio->io_hdr.start_bt); 912#if 0 913 cs_prof_gettime(&taskio->io_hdr.start_ticks); 914#endif 915#endif /* CTL_TIME_IO */ 916 ctl_run_task((union ctl_io *)taskio); 917 break; 918 } 919 /* Persistent Reserve action which needs attention */ 920 case CTL_MSG_PERS_ACTION: 921 presio = (struct ctl_prio *)ctl_alloc_io_nowait( 922 ctl_softc->othersc_pool); 923 if (presio == NULL) { 924 printf("ctl_isc_event_handler: can't allocate " 925 "ctl_io!\n"); 926 /* Bad Juju */ 927 /* Need to set busy and send msg back */ 928 goto bailout; 929 } 930 ctl_zero_io((union ctl_io *)presio); 931 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 932 presio->pr_msg = msg_info.pr; 933 ctl_enqueue_isc((union ctl_io *)presio); 934 break; 935 case CTL_MSG_SYNC_FE: 936 rcv_sync_msg = 1; 937 break; 938 default: 939 printf("How did I get here?\n"); 940 } 941 } else if (event == CTL_HA_EVT_MSG_SENT) { 942 if (param != CTL_HA_STATUS_SUCCESS) { 943 printf("Bad status from ctl_ha_msg_send status %d\n", 944 param); 945 } 946 return; 947 } else if (event == CTL_HA_EVT_DISCONNECT) { 948 printf("CTL: Got a disconnect from Isc\n"); 949 return; 950 } else { 951 printf("ctl_isc_event_handler: Unknown event %d\n", event); 952 return; 953 } 954 955bailout: 956 return; 957} 958 959static void 960ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 961{ 962 struct scsi_sense_data *sense; 963 964 sense = &dest->scsiio.sense_data; 965 bcopy(&src->scsi.sense_data, sense, sizeof(*sense)); 966 dest->scsiio.scsi_status = src->scsi.scsi_status; 967 dest->scsiio.sense_len = src->scsi.sense_len; 968 dest->io_hdr.status = src->hdr.status; 969} 970 971static int 972ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS) 973{ 974 struct ctl_softc *softc = (struct ctl_softc *)arg1; 975 struct ctl_lun *lun; 976 int error, value, i; 977 978 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) 979 value = 0; 980 else 981 value = 1; 982 983 error = sysctl_handle_int(oidp, &value, 0, req); 984 if ((error != 0) || (req->newptr == NULL)) 985 return (error); 986 987 mtx_lock(&softc->ctl_lock); 988 if (value == 0) 989 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 990 else 991 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 992 STAILQ_FOREACH(lun, &softc->lun_list, links) { 993 mtx_lock(&lun->lun_lock); 994 for (i = 0; i < CTL_MAX_INITIATORS; i++) 995 lun->pending_ua[i] |= CTL_UA_ASYM_ACC_CHANGE; 996 mtx_unlock(&lun->lun_lock); 997 } 998 mtx_unlock(&softc->ctl_lock); 999 return (0); 1000} 1001 1002static int 1003ctl_init(void) 1004{ 1005 struct ctl_softc *softc; 1006 void *other_pool; 1007 struct ctl_port *port; 1008 int i, error, retval; 1009 //int isc_retval; 1010 1011 retval = 0; 1012 ctl_pause_rtr = 0; 1013 rcv_sync_msg = 0; 1014 1015 control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1016 M_WAITOK | M_ZERO); 1017 softc = control_softc; 1018 1019 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1020 "cam/ctl"); 1021 1022 softc->dev->si_drv1 = softc; 1023 1024 /* 1025 * By default, return a "bad LUN" peripheral qualifier for unknown 1026 * LUNs. The user can override this default using the tunable or 1027 * sysctl. See the comment in ctl_inquiry_std() for more details. 1028 */ 1029 softc->inquiry_pq_no_lun = 1; 1030 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun", 1031 &softc->inquiry_pq_no_lun); 1032 sysctl_ctx_init(&softc->sysctl_ctx); 1033 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1034 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1035 CTLFLAG_RD, 0, "CAM Target Layer"); 1036 1037 if (softc->sysctl_tree == NULL) { 1038 printf("%s: unable to allocate sysctl tree\n", __func__); 1039 destroy_dev(softc->dev); 1040 free(control_softc, M_DEVBUF); 1041 control_softc = NULL; 1042 return (ENOMEM); 1043 } 1044 1045 SYSCTL_ADD_INT(&softc->sysctl_ctx, 1046 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO, 1047 "inquiry_pq_no_lun", CTLFLAG_RW, 1048 &softc->inquiry_pq_no_lun, 0, 1049 "Report no lun possible for invalid LUNs"); 1050 1051 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1052 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1053 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1054 softc->open_count = 0; 1055 1056 /* 1057 * Default to actually sending a SYNCHRONIZE CACHE command down to 1058 * the drive. 1059 */ 1060 softc->flags = CTL_FLAG_REAL_SYNC; 1061 1062 /* 1063 * In Copan's HA scheme, the "master" and "slave" roles are 1064 * figured out through the slot the controller is in. Although it 1065 * is an active/active system, someone has to be in charge. 1066 */ 1067 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1068 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1069 "HA head ID (0 - no HA)"); 1070 if (softc->ha_id == 0) { 1071 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1072 softc->is_single = 1; 1073 softc->port_offset = 0; 1074 } else 1075 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS; 1076 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT; 1077 1078 /* 1079 * XXX KDM need to figure out where we want to get our target ID 1080 * and WWID. Is it different on each port? 1081 */ 1082 softc->target.id = 0; 1083 softc->target.wwid[0] = 0x12345678; 1084 softc->target.wwid[1] = 0x87654321; 1085 STAILQ_INIT(&softc->lun_list); 1086 STAILQ_INIT(&softc->pending_lun_queue); 1087 STAILQ_INIT(&softc->fe_list); 1088 STAILQ_INIT(&softc->port_list); 1089 STAILQ_INIT(&softc->be_list); 1090 ctl_tpc_init(softc); 1091 1092 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1093 &other_pool) != 0) 1094 { 1095 printf("ctl: can't allocate %d entry other SC pool, " 1096 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1097 return (ENOMEM); 1098 } 1099 softc->othersc_pool = other_pool; 1100 1101 if (worker_threads <= 0) 1102 worker_threads = max(1, mp_ncpus / 4); 1103 if (worker_threads > CTL_MAX_THREADS) 1104 worker_threads = CTL_MAX_THREADS; 1105 1106 for (i = 0; i < worker_threads; i++) { 1107 struct ctl_thread *thr = &softc->threads[i]; 1108 1109 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1110 thr->ctl_softc = softc; 1111 STAILQ_INIT(&thr->incoming_queue); 1112 STAILQ_INIT(&thr->rtr_queue); 1113 STAILQ_INIT(&thr->done_queue); 1114 STAILQ_INIT(&thr->isc_queue); 1115 1116 error = kproc_kthread_add(ctl_work_thread, thr, 1117 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1118 if (error != 0) { 1119 printf("error creating CTL work thread!\n"); 1120 ctl_pool_free(other_pool); 1121 return (error); 1122 } 1123 } 1124 error = kproc_kthread_add(ctl_lun_thread, softc, 1125 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1126 if (error != 0) { 1127 printf("error creating CTL lun thread!\n"); 1128 ctl_pool_free(other_pool); 1129 return (error); 1130 } 1131 error = kproc_kthread_add(ctl_thresh_thread, softc, 1132 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1133 if (error != 0) { 1134 printf("error creating CTL threshold thread!\n"); 1135 ctl_pool_free(other_pool); 1136 return (error); 1137 } 1138 if (bootverbose) 1139 printf("ctl: CAM Target Layer loaded\n"); 1140 1141 /* 1142 * Initialize the ioctl front end. 1143 */ 1144 ctl_frontend_register(&ioctl_frontend); 1145 port = &softc->ioctl_info.port; 1146 port->frontend = &ioctl_frontend; 1147 sprintf(softc->ioctl_info.port_name, "ioctl"); 1148 port->port_type = CTL_PORT_IOCTL; 1149 port->num_requested_ctl_io = 100; 1150 port->port_name = softc->ioctl_info.port_name; 1151 port->port_online = ctl_ioctl_online; 1152 port->port_offline = ctl_ioctl_offline; 1153 port->onoff_arg = &softc->ioctl_info; 1154 port->lun_enable = ctl_ioctl_lun_enable; 1155 port->lun_disable = ctl_ioctl_lun_disable; 1156 port->targ_lun_arg = &softc->ioctl_info; 1157 port->fe_datamove = ctl_ioctl_datamove; 1158 port->fe_done = ctl_ioctl_done; 1159 port->max_targets = 15; 1160 port->max_target_id = 15; 1161 1162 if (ctl_port_register(&softc->ioctl_info.port) != 0) { 1163 printf("ctl: ioctl front end registration failed, will " 1164 "continue anyway\n"); 1165 } 1166 1167 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1168 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN, 1169 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head"); 1170 1171#ifdef CTL_IO_DELAY 1172 if (sizeof(struct callout) > CTL_TIMER_BYTES) { 1173 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n", 1174 sizeof(struct callout), CTL_TIMER_BYTES); 1175 return (EINVAL); 1176 } 1177#endif /* CTL_IO_DELAY */ 1178 1179 return (0); 1180} 1181 1182void 1183ctl_shutdown(void) 1184{ 1185 struct ctl_softc *softc; 1186 struct ctl_lun *lun, *next_lun; 1187 1188 softc = (struct ctl_softc *)control_softc; 1189 1190 if (ctl_port_deregister(&softc->ioctl_info.port) != 0) 1191 printf("ctl: ioctl front end deregistration failed\n"); 1192 1193 mtx_lock(&softc->ctl_lock); 1194 1195 /* 1196 * Free up each LUN. 1197 */ 1198 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){ 1199 next_lun = STAILQ_NEXT(lun, links); 1200 ctl_free_lun(lun); 1201 } 1202 1203 mtx_unlock(&softc->ctl_lock); 1204 1205 ctl_frontend_deregister(&ioctl_frontend); 1206 1207#if 0 1208 ctl_shutdown_thread(softc->work_thread); 1209 mtx_destroy(&softc->queue_lock); 1210#endif 1211 1212 ctl_tpc_shutdown(softc); 1213 uma_zdestroy(softc->io_zone); 1214 mtx_destroy(&softc->ctl_lock); 1215 1216 destroy_dev(softc->dev); 1217 1218 sysctl_ctx_free(&softc->sysctl_ctx); 1219 1220 free(control_softc, M_DEVBUF); 1221 control_softc = NULL; 1222 1223 if (bootverbose) 1224 printf("ctl: CAM Target Layer unloaded\n"); 1225} 1226 1227static int 1228ctl_module_event_handler(module_t mod, int what, void *arg) 1229{ 1230 1231 switch (what) { 1232 case MOD_LOAD: 1233 return (ctl_init()); 1234 case MOD_UNLOAD: 1235 return (EBUSY); 1236 default: 1237 return (EOPNOTSUPP); 1238 } 1239} 1240 1241/* 1242 * XXX KDM should we do some access checks here? Bump a reference count to 1243 * prevent a CTL module from being unloaded while someone has it open? 1244 */ 1245static int 1246ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1247{ 1248 return (0); 1249} 1250 1251static int 1252ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1253{ 1254 return (0); 1255} 1256 1257int 1258ctl_port_enable(ctl_port_type port_type) 1259{ 1260 struct ctl_softc *softc = control_softc; 1261 struct ctl_port *port; 1262 1263 if (softc->is_single == 0) { 1264 union ctl_ha_msg msg_info; 1265 int isc_retval; 1266 1267#if 0 1268 printf("%s: HA mode, synchronizing frontend enable\n", 1269 __func__); 1270#endif 1271 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE; 1272 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1273 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) { 1274 printf("Sync msg send error retval %d\n", isc_retval); 1275 } 1276 if (!rcv_sync_msg) { 1277 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info, 1278 sizeof(msg_info), 1); 1279 } 1280#if 0 1281 printf("CTL:Frontend Enable\n"); 1282 } else { 1283 printf("%s: single mode, skipping frontend synchronization\n", 1284 __func__); 1285#endif 1286 } 1287 1288 STAILQ_FOREACH(port, &softc->port_list, links) { 1289 if (port_type & port->port_type) 1290 { 1291#if 0 1292 printf("port %d\n", port->targ_port); 1293#endif 1294 ctl_port_online(port); 1295 } 1296 } 1297 1298 return (0); 1299} 1300 1301int 1302ctl_port_disable(ctl_port_type port_type) 1303{ 1304 struct ctl_softc *softc; 1305 struct ctl_port *port; 1306 1307 softc = control_softc; 1308 1309 STAILQ_FOREACH(port, &softc->port_list, links) { 1310 if (port_type & port->port_type) 1311 ctl_port_offline(port); 1312 } 1313 1314 return (0); 1315} 1316 1317/* 1318 * Returns 0 for success, 1 for failure. 1319 * Currently the only failure mode is if there aren't enough entries 1320 * allocated. So, in case of a failure, look at num_entries_dropped, 1321 * reallocate and try again. 1322 */ 1323int 1324ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced, 1325 int *num_entries_filled, int *num_entries_dropped, 1326 ctl_port_type port_type, int no_virtual) 1327{ 1328 struct ctl_softc *softc; 1329 struct ctl_port *port; 1330 int entries_dropped, entries_filled; 1331 int retval; 1332 int i; 1333 1334 softc = control_softc; 1335 1336 retval = 0; 1337 entries_filled = 0; 1338 entries_dropped = 0; 1339 1340 i = 0; 1341 mtx_lock(&softc->ctl_lock); 1342 STAILQ_FOREACH(port, &softc->port_list, links) { 1343 struct ctl_port_entry *entry; 1344 1345 if ((port->port_type & port_type) == 0) 1346 continue; 1347 1348 if ((no_virtual != 0) 1349 && (port->virtual_port != 0)) 1350 continue; 1351 1352 if (entries_filled >= num_entries_alloced) { 1353 entries_dropped++; 1354 continue; 1355 } 1356 entry = &entries[i]; 1357 1358 entry->port_type = port->port_type; 1359 strlcpy(entry->port_name, port->port_name, 1360 sizeof(entry->port_name)); 1361 entry->physical_port = port->physical_port; 1362 entry->virtual_port = port->virtual_port; 1363 entry->wwnn = port->wwnn; 1364 entry->wwpn = port->wwpn; 1365 1366 i++; 1367 entries_filled++; 1368 } 1369 1370 mtx_unlock(&softc->ctl_lock); 1371 1372 if (entries_dropped > 0) 1373 retval = 1; 1374 1375 *num_entries_dropped = entries_dropped; 1376 *num_entries_filled = entries_filled; 1377 1378 return (retval); 1379} 1380 1381static void 1382ctl_ioctl_online(void *arg) 1383{ 1384 struct ctl_ioctl_info *ioctl_info; 1385 1386 ioctl_info = (struct ctl_ioctl_info *)arg; 1387 1388 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED; 1389} 1390 1391static void 1392ctl_ioctl_offline(void *arg) 1393{ 1394 struct ctl_ioctl_info *ioctl_info; 1395 1396 ioctl_info = (struct ctl_ioctl_info *)arg; 1397 1398 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED; 1399} 1400 1401/* 1402 * Remove an initiator by port number and initiator ID. 1403 * Returns 0 for success, -1 for failure. 1404 */ 1405int 1406ctl_remove_initiator(struct ctl_port *port, int iid) 1407{ 1408 struct ctl_softc *softc = control_softc; 1409 1410 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1411 1412 if (iid > CTL_MAX_INIT_PER_PORT) { 1413 printf("%s: initiator ID %u > maximun %u!\n", 1414 __func__, iid, CTL_MAX_INIT_PER_PORT); 1415 return (-1); 1416 } 1417 1418 mtx_lock(&softc->ctl_lock); 1419 port->wwpn_iid[iid].in_use--; 1420 port->wwpn_iid[iid].last_use = time_uptime; 1421 mtx_unlock(&softc->ctl_lock); 1422 1423 return (0); 1424} 1425 1426/* 1427 * Add an initiator to the initiator map. 1428 * Returns iid for success, < 0 for failure. 1429 */ 1430int 1431ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 1432{ 1433 struct ctl_softc *softc = control_softc; 1434 time_t best_time; 1435 int i, best; 1436 1437 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1438 1439 if (iid >= CTL_MAX_INIT_PER_PORT) { 1440 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 1441 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 1442 free(name, M_CTL); 1443 return (-1); 1444 } 1445 1446 mtx_lock(&softc->ctl_lock); 1447 1448 if (iid < 0 && (wwpn != 0 || name != NULL)) { 1449 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1450 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 1451 iid = i; 1452 break; 1453 } 1454 if (name != NULL && port->wwpn_iid[i].name != NULL && 1455 strcmp(name, port->wwpn_iid[i].name) == 0) { 1456 iid = i; 1457 break; 1458 } 1459 } 1460 } 1461 1462 if (iid < 0) { 1463 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1464 if (port->wwpn_iid[i].in_use == 0 && 1465 port->wwpn_iid[i].wwpn == 0 && 1466 port->wwpn_iid[i].name == NULL) { 1467 iid = i; 1468 break; 1469 } 1470 } 1471 } 1472 1473 if (iid < 0) { 1474 best = -1; 1475 best_time = INT32_MAX; 1476 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1477 if (port->wwpn_iid[i].in_use == 0) { 1478 if (port->wwpn_iid[i].last_use < best_time) { 1479 best = i; 1480 best_time = port->wwpn_iid[i].last_use; 1481 } 1482 } 1483 } 1484 iid = best; 1485 } 1486 1487 if (iid < 0) { 1488 mtx_unlock(&softc->ctl_lock); 1489 free(name, M_CTL); 1490 return (-2); 1491 } 1492 1493 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 1494 /* 1495 * This is not an error yet. 1496 */ 1497 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 1498#if 0 1499 printf("%s: port %d iid %u WWPN %#jx arrived" 1500 " again\n", __func__, port->targ_port, 1501 iid, (uintmax_t)wwpn); 1502#endif 1503 goto take; 1504 } 1505 if (name != NULL && port->wwpn_iid[iid].name != NULL && 1506 strcmp(name, port->wwpn_iid[iid].name) == 0) { 1507#if 0 1508 printf("%s: port %d iid %u name '%s' arrived" 1509 " again\n", __func__, port->targ_port, 1510 iid, name); 1511#endif 1512 goto take; 1513 } 1514 1515 /* 1516 * This is an error, but what do we do about it? The 1517 * driver is telling us we have a new WWPN for this 1518 * initiator ID, so we pretty much need to use it. 1519 */ 1520 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 1521 " but WWPN %#jx '%s' is still at that address\n", 1522 __func__, port->targ_port, iid, wwpn, name, 1523 (uintmax_t)port->wwpn_iid[iid].wwpn, 1524 port->wwpn_iid[iid].name); 1525 1526 /* 1527 * XXX KDM clear have_ca and ua_pending on each LUN for 1528 * this initiator. 1529 */ 1530 } 1531take: 1532 free(port->wwpn_iid[iid].name, M_CTL); 1533 port->wwpn_iid[iid].name = name; 1534 port->wwpn_iid[iid].wwpn = wwpn; 1535 port->wwpn_iid[iid].in_use++; 1536 mtx_unlock(&softc->ctl_lock); 1537 1538 return (iid); 1539} 1540 1541static int 1542ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 1543{ 1544 int len; 1545 1546 switch (port->port_type) { 1547 case CTL_PORT_FC: 1548 { 1549 struct scsi_transportid_fcp *id = 1550 (struct scsi_transportid_fcp *)buf; 1551 if (port->wwpn_iid[iid].wwpn == 0) 1552 return (0); 1553 memset(id, 0, sizeof(*id)); 1554 id->format_protocol = SCSI_PROTO_FC; 1555 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 1556 return (sizeof(*id)); 1557 } 1558 case CTL_PORT_ISCSI: 1559 { 1560 struct scsi_transportid_iscsi_port *id = 1561 (struct scsi_transportid_iscsi_port *)buf; 1562 if (port->wwpn_iid[iid].name == NULL) 1563 return (0); 1564 memset(id, 0, 256); 1565 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 1566 SCSI_PROTO_ISCSI; 1567 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 1568 len = roundup2(min(len, 252), 4); 1569 scsi_ulto2b(len, id->additional_length); 1570 return (sizeof(*id) + len); 1571 } 1572 case CTL_PORT_SAS: 1573 { 1574 struct scsi_transportid_sas *id = 1575 (struct scsi_transportid_sas *)buf; 1576 if (port->wwpn_iid[iid].wwpn == 0) 1577 return (0); 1578 memset(id, 0, sizeof(*id)); 1579 id->format_protocol = SCSI_PROTO_SAS; 1580 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 1581 return (sizeof(*id)); 1582 } 1583 default: 1584 { 1585 struct scsi_transportid_spi *id = 1586 (struct scsi_transportid_spi *)buf; 1587 memset(id, 0, sizeof(*id)); 1588 id->format_protocol = SCSI_PROTO_SPI; 1589 scsi_ulto2b(iid, id->scsi_addr); 1590 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 1591 return (sizeof(*id)); 1592 } 1593 } 1594} 1595 1596static int 1597ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id) 1598{ 1599 return (0); 1600} 1601 1602static int 1603ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id) 1604{ 1605 return (0); 1606} 1607 1608/* 1609 * Data movement routine for the CTL ioctl frontend port. 1610 */ 1611static int 1612ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio) 1613{ 1614 struct ctl_sg_entry *ext_sglist, *kern_sglist; 1615 struct ctl_sg_entry ext_entry, kern_entry; 1616 int ext_sglen, ext_sg_entries, kern_sg_entries; 1617 int ext_sg_start, ext_offset; 1618 int len_to_copy, len_copied; 1619 int kern_watermark, ext_watermark; 1620 int ext_sglist_malloced; 1621 int i, j; 1622 1623 ext_sglist_malloced = 0; 1624 ext_sg_start = 0; 1625 ext_offset = 0; 1626 1627 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n")); 1628 1629 /* 1630 * If this flag is set, fake the data transfer. 1631 */ 1632 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) { 1633 ctsio->ext_data_filled = ctsio->ext_data_len; 1634 goto bailout; 1635 } 1636 1637 /* 1638 * To simplify things here, if we have a single buffer, stick it in 1639 * a S/G entry and just make it a single entry S/G list. 1640 */ 1641 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) { 1642 int len_seen; 1643 1644 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist); 1645 1646 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL, 1647 M_WAITOK); 1648 ext_sglist_malloced = 1; 1649 if (copyin(ctsio->ext_data_ptr, ext_sglist, 1650 ext_sglen) != 0) { 1651 ctl_set_internal_failure(ctsio, 1652 /*sks_valid*/ 0, 1653 /*retry_count*/ 0); 1654 goto bailout; 1655 } 1656 ext_sg_entries = ctsio->ext_sg_entries; 1657 len_seen = 0; 1658 for (i = 0; i < ext_sg_entries; i++) { 1659 if ((len_seen + ext_sglist[i].len) >= 1660 ctsio->ext_data_filled) { 1661 ext_sg_start = i; 1662 ext_offset = ctsio->ext_data_filled - len_seen; 1663 break; 1664 } 1665 len_seen += ext_sglist[i].len; 1666 } 1667 } else { 1668 ext_sglist = &ext_entry; 1669 ext_sglist->addr = ctsio->ext_data_ptr; 1670 ext_sglist->len = ctsio->ext_data_len; 1671 ext_sg_entries = 1; 1672 ext_sg_start = 0; 1673 ext_offset = ctsio->ext_data_filled; 1674 } 1675 1676 if (ctsio->kern_sg_entries > 0) { 1677 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr; 1678 kern_sg_entries = ctsio->kern_sg_entries; 1679 } else { 1680 kern_sglist = &kern_entry; 1681 kern_sglist->addr = ctsio->kern_data_ptr; 1682 kern_sglist->len = ctsio->kern_data_len; 1683 kern_sg_entries = 1; 1684 } 1685 1686 1687 kern_watermark = 0; 1688 ext_watermark = ext_offset; 1689 len_copied = 0; 1690 for (i = ext_sg_start, j = 0; 1691 i < ext_sg_entries && j < kern_sg_entries;) { 1692 uint8_t *ext_ptr, *kern_ptr; 1693 1694 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark, 1695 kern_sglist[j].len - kern_watermark); 1696 1697 ext_ptr = (uint8_t *)ext_sglist[i].addr; 1698 ext_ptr = ext_ptr + ext_watermark; 1699 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 1700 /* 1701 * XXX KDM fix this! 1702 */ 1703 panic("need to implement bus address support"); 1704#if 0 1705 kern_ptr = bus_to_virt(kern_sglist[j].addr); 1706#endif 1707 } else 1708 kern_ptr = (uint8_t *)kern_sglist[j].addr; 1709 kern_ptr = kern_ptr + kern_watermark; 1710 1711 kern_watermark += len_to_copy; 1712 ext_watermark += len_to_copy; 1713 1714 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) == 1715 CTL_FLAG_DATA_IN) { 1716 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1717 "bytes to user\n", len_to_copy)); 1718 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1719 "to %p\n", kern_ptr, ext_ptr)); 1720 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) { 1721 ctl_set_internal_failure(ctsio, 1722 /*sks_valid*/ 0, 1723 /*retry_count*/ 0); 1724 goto bailout; 1725 } 1726 } else { 1727 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d " 1728 "bytes from user\n", len_to_copy)); 1729 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p " 1730 "to %p\n", ext_ptr, kern_ptr)); 1731 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){ 1732 ctl_set_internal_failure(ctsio, 1733 /*sks_valid*/ 0, 1734 /*retry_count*/0); 1735 goto bailout; 1736 } 1737 } 1738 1739 len_copied += len_to_copy; 1740 1741 if (ext_sglist[i].len == ext_watermark) { 1742 i++; 1743 ext_watermark = 0; 1744 } 1745 1746 if (kern_sglist[j].len == kern_watermark) { 1747 j++; 1748 kern_watermark = 0; 1749 } 1750 } 1751 1752 ctsio->ext_data_filled += len_copied; 1753 1754 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, " 1755 "kern_sg_entries: %d\n", ext_sg_entries, 1756 kern_sg_entries)); 1757 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, " 1758 "kern_data_len = %d\n", ctsio->ext_data_len, 1759 ctsio->kern_data_len)); 1760 1761 1762 /* XXX KDM set residual?? */ 1763bailout: 1764 1765 if (ext_sglist_malloced != 0) 1766 free(ext_sglist, M_CTL); 1767 1768 return (CTL_RETVAL_COMPLETE); 1769} 1770 1771/* 1772 * Serialize a command that went down the "wrong" side, and so was sent to 1773 * this controller for execution. The logic is a little different than the 1774 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 1775 * sent back to the other side, but in the success case, we execute the 1776 * command on this side (XFER mode) or tell the other side to execute it 1777 * (SER_ONLY mode). 1778 */ 1779static int 1780ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 1781{ 1782 struct ctl_softc *ctl_softc; 1783 union ctl_ha_msg msg_info; 1784 struct ctl_lun *lun; 1785 int retval = 0; 1786 uint32_t targ_lun; 1787 1788 ctl_softc = control_softc; 1789 1790 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 1791 lun = ctl_softc->ctl_luns[targ_lun]; 1792 if (lun==NULL) 1793 { 1794 /* 1795 * Why isn't LUN defined? The other side wouldn't 1796 * send a cmd if the LUN is undefined. 1797 */ 1798 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__); 1799 1800 /* "Logical unit not supported" */ 1801 ctl_set_sense_data(&msg_info.scsi.sense_data, 1802 lun, 1803 /*sense_format*/SSD_TYPE_NONE, 1804 /*current_error*/ 1, 1805 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1806 /*asc*/ 0x25, 1807 /*ascq*/ 0x00, 1808 SSD_ELEM_NONE); 1809 1810 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1811 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1812 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1813 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1814 msg_info.hdr.serializing_sc = NULL; 1815 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1816 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1817 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1818 } 1819 return(1); 1820 1821 } 1822 1823 mtx_lock(&lun->lun_lock); 1824 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1825 1826 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 1827 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 1828 ooa_links))) { 1829 case CTL_ACTION_BLOCK: 1830 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 1831 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 1832 blocked_links); 1833 break; 1834 case CTL_ACTION_PASS: 1835 case CTL_ACTION_SKIP: 1836 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 1837 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 1838 ctl_enqueue_rtr((union ctl_io *)ctsio); 1839 } else { 1840 1841 /* send msg back to other side */ 1842 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1843 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 1844 msg_info.hdr.msg_type = CTL_MSG_R2R; 1845#if 0 1846 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc); 1847#endif 1848 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1849 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1850 } 1851 } 1852 break; 1853 case CTL_ACTION_OVERLAP: 1854 /* OVERLAPPED COMMANDS ATTEMPTED */ 1855 ctl_set_sense_data(&msg_info.scsi.sense_data, 1856 lun, 1857 /*sense_format*/SSD_TYPE_NONE, 1858 /*current_error*/ 1, 1859 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1860 /*asc*/ 0x4E, 1861 /*ascq*/ 0x00, 1862 SSD_ELEM_NONE); 1863 1864 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1865 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1866 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1867 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1868 msg_info.hdr.serializing_sc = NULL; 1869 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1870#if 0 1871 printf("BAD JUJU:Major Bummer Overlap\n"); 1872#endif 1873 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1874 retval = 1; 1875 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1876 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1877 } 1878 break; 1879 case CTL_ACTION_OVERLAP_TAG: 1880 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */ 1881 ctl_set_sense_data(&msg_info.scsi.sense_data, 1882 lun, 1883 /*sense_format*/SSD_TYPE_NONE, 1884 /*current_error*/ 1, 1885 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST, 1886 /*asc*/ 0x4D, 1887 /*ascq*/ ctsio->tag_num & 0xff, 1888 SSD_ELEM_NONE); 1889 1890 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1891 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1892 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1893 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1894 msg_info.hdr.serializing_sc = NULL; 1895 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1896#if 0 1897 printf("BAD JUJU:Major Bummer Overlap Tag\n"); 1898#endif 1899 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1900 retval = 1; 1901 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1902 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1903 } 1904 break; 1905 case CTL_ACTION_ERROR: 1906 default: 1907 /* "Internal target failure" */ 1908 ctl_set_sense_data(&msg_info.scsi.sense_data, 1909 lun, 1910 /*sense_format*/SSD_TYPE_NONE, 1911 /*current_error*/ 1, 1912 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 1913 /*asc*/ 0x44, 1914 /*ascq*/ 0x00, 1915 SSD_ELEM_NONE); 1916 1917 msg_info.scsi.sense_len = SSD_FULL_SIZE; 1918 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 1919 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 1920 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 1921 msg_info.hdr.serializing_sc = NULL; 1922 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 1923#if 0 1924 printf("BAD JUJU:Major Bummer HW Error\n"); 1925#endif 1926 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 1927 retval = 1; 1928 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 1929 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) { 1930 } 1931 break; 1932 } 1933 mtx_unlock(&lun->lun_lock); 1934 return (retval); 1935} 1936 1937static int 1938ctl_ioctl_submit_wait(union ctl_io *io) 1939{ 1940 struct ctl_fe_ioctl_params params; 1941 ctl_fe_ioctl_state last_state; 1942 int done, retval; 1943 1944 retval = 0; 1945 1946 bzero(¶ms, sizeof(params)); 1947 1948 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF); 1949 cv_init(¶ms.sem, "ctlioccv"); 1950 params.state = CTL_IOCTL_INPROG; 1951 last_state = params.state; 1952 1953 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms; 1954 1955 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n")); 1956 1957 /* This shouldn't happen */ 1958 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE) 1959 return (retval); 1960 1961 done = 0; 1962 1963 do { 1964 mtx_lock(¶ms.ioctl_mtx); 1965 /* 1966 * Check the state here, and don't sleep if the state has 1967 * already changed (i.e. wakeup has already occured, but we 1968 * weren't waiting yet). 1969 */ 1970 if (params.state == last_state) { 1971 /* XXX KDM cv_wait_sig instead? */ 1972 cv_wait(¶ms.sem, ¶ms.ioctl_mtx); 1973 } 1974 last_state = params.state; 1975 1976 switch (params.state) { 1977 case CTL_IOCTL_INPROG: 1978 /* Why did we wake up? */ 1979 /* XXX KDM error here? */ 1980 mtx_unlock(¶ms.ioctl_mtx); 1981 break; 1982 case CTL_IOCTL_DATAMOVE: 1983 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n")); 1984 1985 /* 1986 * change last_state back to INPROG to avoid 1987 * deadlock on subsequent data moves. 1988 */ 1989 params.state = last_state = CTL_IOCTL_INPROG; 1990 1991 mtx_unlock(¶ms.ioctl_mtx); 1992 ctl_ioctl_do_datamove(&io->scsiio); 1993 /* 1994 * Note that in some cases, most notably writes, 1995 * this will queue the I/O and call us back later. 1996 * In other cases, generally reads, this routine 1997 * will immediately call back and wake us up, 1998 * probably using our own context. 1999 */ 2000 io->scsiio.be_move_done(io); 2001 break; 2002 case CTL_IOCTL_DONE: 2003 mtx_unlock(¶ms.ioctl_mtx); 2004 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n")); 2005 done = 1; 2006 break; 2007 default: 2008 mtx_unlock(¶ms.ioctl_mtx); 2009 /* XXX KDM error here? */ 2010 break; 2011 } 2012 } while (done == 0); 2013 2014 mtx_destroy(¶ms.ioctl_mtx); 2015 cv_destroy(¶ms.sem); 2016 2017 return (CTL_RETVAL_COMPLETE); 2018} 2019 2020static void 2021ctl_ioctl_datamove(union ctl_io *io) 2022{ 2023 struct ctl_fe_ioctl_params *params; 2024 2025 params = (struct ctl_fe_ioctl_params *) 2026 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2027 2028 mtx_lock(¶ms->ioctl_mtx); 2029 params->state = CTL_IOCTL_DATAMOVE; 2030 cv_broadcast(¶ms->sem); 2031 mtx_unlock(¶ms->ioctl_mtx); 2032} 2033 2034static void 2035ctl_ioctl_done(union ctl_io *io) 2036{ 2037 struct ctl_fe_ioctl_params *params; 2038 2039 params = (struct ctl_fe_ioctl_params *) 2040 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr; 2041 2042 mtx_lock(¶ms->ioctl_mtx); 2043 params->state = CTL_IOCTL_DONE; 2044 cv_broadcast(¶ms->sem); 2045 mtx_unlock(¶ms->ioctl_mtx); 2046} 2047 2048static void 2049ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask) 2050{ 2051 struct ctl_fe_ioctl_startstop_info *sd_info; 2052 2053 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg; 2054 2055 sd_info->hs_info.status = metatask->status; 2056 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns; 2057 sd_info->hs_info.luns_complete = 2058 metatask->taskinfo.startstop.luns_complete; 2059 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed; 2060 2061 cv_broadcast(&sd_info->sem); 2062} 2063 2064static void 2065ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask) 2066{ 2067 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info; 2068 2069 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg; 2070 2071 mtx_lock(fe_bbr_info->lock); 2072 fe_bbr_info->bbr_info->status = metatask->status; 2073 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2074 fe_bbr_info->wakeup_done = 1; 2075 mtx_unlock(fe_bbr_info->lock); 2076 2077 cv_broadcast(&fe_bbr_info->sem); 2078} 2079 2080/* 2081 * Returns 0 for success, errno for failure. 2082 */ 2083static int 2084ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2085 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2086{ 2087 union ctl_io *io; 2088 int retval; 2089 2090 retval = 0; 2091 2092 mtx_lock(&lun->lun_lock); 2093 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2094 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2095 ooa_links)) { 2096 struct ctl_ooa_entry *entry; 2097 2098 /* 2099 * If we've got more than we can fit, just count the 2100 * remaining entries. 2101 */ 2102 if (*cur_fill_num >= ooa_hdr->alloc_num) 2103 continue; 2104 2105 entry = &kern_entries[*cur_fill_num]; 2106 2107 entry->tag_num = io->scsiio.tag_num; 2108 entry->lun_num = lun->lun; 2109#ifdef CTL_TIME_IO 2110 entry->start_bt = io->io_hdr.start_bt; 2111#endif 2112 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2113 entry->cdb_len = io->scsiio.cdb_len; 2114 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2115 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2116 2117 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2118 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2119 2120 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2121 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2122 2123 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2124 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2125 2126 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2127 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2128 } 2129 mtx_unlock(&lun->lun_lock); 2130 2131 return (retval); 2132} 2133 2134static void * 2135ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2136 size_t error_str_len) 2137{ 2138 void *kptr; 2139 2140 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2141 2142 if (copyin(user_addr, kptr, len) != 0) { 2143 snprintf(error_str, error_str_len, "Error copying %d bytes " 2144 "from user address %p to kernel address %p", len, 2145 user_addr, kptr); 2146 free(kptr, M_CTL); 2147 return (NULL); 2148 } 2149 2150 return (kptr); 2151} 2152 2153static void 2154ctl_free_args(int num_args, struct ctl_be_arg *args) 2155{ 2156 int i; 2157 2158 if (args == NULL) 2159 return; 2160 2161 for (i = 0; i < num_args; i++) { 2162 free(args[i].kname, M_CTL); 2163 free(args[i].kvalue, M_CTL); 2164 } 2165 2166 free(args, M_CTL); 2167} 2168 2169static struct ctl_be_arg * 2170ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2171 char *error_str, size_t error_str_len) 2172{ 2173 struct ctl_be_arg *args; 2174 int i; 2175 2176 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2177 error_str, error_str_len); 2178 2179 if (args == NULL) 2180 goto bailout; 2181 2182 for (i = 0; i < num_args; i++) { 2183 args[i].kname = NULL; 2184 args[i].kvalue = NULL; 2185 } 2186 2187 for (i = 0; i < num_args; i++) { 2188 uint8_t *tmpptr; 2189 2190 args[i].kname = ctl_copyin_alloc(args[i].name, 2191 args[i].namelen, error_str, error_str_len); 2192 if (args[i].kname == NULL) 2193 goto bailout; 2194 2195 if (args[i].kname[args[i].namelen - 1] != '\0') { 2196 snprintf(error_str, error_str_len, "Argument %d " 2197 "name is not NUL-terminated", i); 2198 goto bailout; 2199 } 2200 2201 if (args[i].flags & CTL_BEARG_RD) { 2202 tmpptr = ctl_copyin_alloc(args[i].value, 2203 args[i].vallen, error_str, error_str_len); 2204 if (tmpptr == NULL) 2205 goto bailout; 2206 if ((args[i].flags & CTL_BEARG_ASCII) 2207 && (tmpptr[args[i].vallen - 1] != '\0')) { 2208 snprintf(error_str, error_str_len, "Argument " 2209 "%d value is not NUL-terminated", i); 2210 goto bailout; 2211 } 2212 args[i].kvalue = tmpptr; 2213 } else { 2214 args[i].kvalue = malloc(args[i].vallen, 2215 M_CTL, M_WAITOK | M_ZERO); 2216 } 2217 } 2218 2219 return (args); 2220bailout: 2221 2222 ctl_free_args(num_args, args); 2223 2224 return (NULL); 2225} 2226 2227static void 2228ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2229{ 2230 int i; 2231 2232 for (i = 0; i < num_args; i++) { 2233 if (args[i].flags & CTL_BEARG_WR) 2234 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2235 } 2236} 2237 2238/* 2239 * Escape characters that are illegal or not recommended in XML. 2240 */ 2241int 2242ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2243{ 2244 char *end = str + size; 2245 int retval; 2246 2247 retval = 0; 2248 2249 for (; *str && str < end; str++) { 2250 switch (*str) { 2251 case '&': 2252 retval = sbuf_printf(sb, "&"); 2253 break; 2254 case '>': 2255 retval = sbuf_printf(sb, ">"); 2256 break; 2257 case '<': 2258 retval = sbuf_printf(sb, "<"); 2259 break; 2260 default: 2261 retval = sbuf_putc(sb, *str); 2262 break; 2263 } 2264 2265 if (retval != 0) 2266 break; 2267 2268 } 2269 2270 return (retval); 2271} 2272 2273static void 2274ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2275{ 2276 struct scsi_vpd_id_descriptor *desc; 2277 int i; 2278 2279 if (id == NULL || id->len < 4) 2280 return; 2281 desc = (struct scsi_vpd_id_descriptor *)id->data; 2282 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2283 case SVPD_ID_TYPE_T10: 2284 sbuf_printf(sb, "t10."); 2285 break; 2286 case SVPD_ID_TYPE_EUI64: 2287 sbuf_printf(sb, "eui."); 2288 break; 2289 case SVPD_ID_TYPE_NAA: 2290 sbuf_printf(sb, "naa."); 2291 break; 2292 case SVPD_ID_TYPE_SCSI_NAME: 2293 break; 2294 } 2295 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2296 case SVPD_ID_CODESET_BINARY: 2297 for (i = 0; i < desc->length; i++) 2298 sbuf_printf(sb, "%02x", desc->identifier[i]); 2299 break; 2300 case SVPD_ID_CODESET_ASCII: 2301 sbuf_printf(sb, "%.*s", (int)desc->length, 2302 (char *)desc->identifier); 2303 break; 2304 case SVPD_ID_CODESET_UTF8: 2305 sbuf_printf(sb, "%s", (char *)desc->identifier); 2306 break; 2307 } 2308} 2309 2310static int 2311ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2312 struct thread *td) 2313{ 2314 struct ctl_softc *softc; 2315 int retval; 2316 2317 softc = control_softc; 2318 2319 retval = 0; 2320 2321 switch (cmd) { 2322 case CTL_IO: { 2323 union ctl_io *io; 2324 void *pool_tmp; 2325 2326 /* 2327 * If we haven't been "enabled", don't allow any SCSI I/O 2328 * to this FETD. 2329 */ 2330 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) { 2331 retval = EPERM; 2332 break; 2333 } 2334 2335 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref); 2336 2337 /* 2338 * Need to save the pool reference so it doesn't get 2339 * spammed by the user's ctl_io. 2340 */ 2341 pool_tmp = io->io_hdr.pool; 2342 memcpy(io, (void *)addr, sizeof(*io)); 2343 io->io_hdr.pool = pool_tmp; 2344 2345 /* 2346 * No status yet, so make sure the status is set properly. 2347 */ 2348 io->io_hdr.status = CTL_STATUS_NONE; 2349 2350 /* 2351 * The user sets the initiator ID, target and LUN IDs. 2352 */ 2353 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port; 2354 io->io_hdr.flags |= CTL_FLAG_USER_REQ; 2355 if ((io->io_hdr.io_type == CTL_IO_SCSI) 2356 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED)) 2357 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++; 2358 2359 retval = ctl_ioctl_submit_wait(io); 2360 2361 if (retval != 0) { 2362 ctl_free_io(io); 2363 break; 2364 } 2365 2366 memcpy((void *)addr, io, sizeof(*io)); 2367 2368 /* return this to our pool */ 2369 ctl_free_io(io); 2370 2371 break; 2372 } 2373 case CTL_ENABLE_PORT: 2374 case CTL_DISABLE_PORT: 2375 case CTL_SET_PORT_WWNS: { 2376 struct ctl_port *port; 2377 struct ctl_port_entry *entry; 2378 2379 entry = (struct ctl_port_entry *)addr; 2380 2381 mtx_lock(&softc->ctl_lock); 2382 STAILQ_FOREACH(port, &softc->port_list, links) { 2383 int action, done; 2384 2385 action = 0; 2386 done = 0; 2387 2388 if ((entry->port_type == CTL_PORT_NONE) 2389 && (entry->targ_port == port->targ_port)) { 2390 /* 2391 * If the user only wants to enable or 2392 * disable or set WWNs on a specific port, 2393 * do the operation and we're done. 2394 */ 2395 action = 1; 2396 done = 1; 2397 } else if (entry->port_type & port->port_type) { 2398 /* 2399 * Compare the user's type mask with the 2400 * particular frontend type to see if we 2401 * have a match. 2402 */ 2403 action = 1; 2404 done = 0; 2405 2406 /* 2407 * Make sure the user isn't trying to set 2408 * WWNs on multiple ports at the same time. 2409 */ 2410 if (cmd == CTL_SET_PORT_WWNS) { 2411 printf("%s: Can't set WWNs on " 2412 "multiple ports\n", __func__); 2413 retval = EINVAL; 2414 break; 2415 } 2416 } 2417 if (action != 0) { 2418 /* 2419 * XXX KDM we have to drop the lock here, 2420 * because the online/offline operations 2421 * can potentially block. We need to 2422 * reference count the frontends so they 2423 * can't go away, 2424 */ 2425 mtx_unlock(&softc->ctl_lock); 2426 2427 if (cmd == CTL_ENABLE_PORT) { 2428 struct ctl_lun *lun; 2429 2430 STAILQ_FOREACH(lun, &softc->lun_list, 2431 links) { 2432 port->lun_enable(port->targ_lun_arg, 2433 lun->target, 2434 lun->lun); 2435 } 2436 2437 ctl_port_online(port); 2438 } else if (cmd == CTL_DISABLE_PORT) { 2439 struct ctl_lun *lun; 2440 2441 ctl_port_offline(port); 2442 2443 STAILQ_FOREACH(lun, &softc->lun_list, 2444 links) { 2445 port->lun_disable( 2446 port->targ_lun_arg, 2447 lun->target, 2448 lun->lun); 2449 } 2450 } 2451 2452 mtx_lock(&softc->ctl_lock); 2453 2454 if (cmd == CTL_SET_PORT_WWNS) 2455 ctl_port_set_wwns(port, 2456 (entry->flags & CTL_PORT_WWNN_VALID) ? 2457 1 : 0, entry->wwnn, 2458 (entry->flags & CTL_PORT_WWPN_VALID) ? 2459 1 : 0, entry->wwpn); 2460 } 2461 if (done != 0) 2462 break; 2463 } 2464 mtx_unlock(&softc->ctl_lock); 2465 break; 2466 } 2467 case CTL_GET_PORT_LIST: { 2468 struct ctl_port *port; 2469 struct ctl_port_list *list; 2470 int i; 2471 2472 list = (struct ctl_port_list *)addr; 2473 2474 if (list->alloc_len != (list->alloc_num * 2475 sizeof(struct ctl_port_entry))) { 2476 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != " 2477 "alloc_num %u * sizeof(struct ctl_port_entry) " 2478 "%zu\n", __func__, list->alloc_len, 2479 list->alloc_num, sizeof(struct ctl_port_entry)); 2480 retval = EINVAL; 2481 break; 2482 } 2483 list->fill_len = 0; 2484 list->fill_num = 0; 2485 list->dropped_num = 0; 2486 i = 0; 2487 mtx_lock(&softc->ctl_lock); 2488 STAILQ_FOREACH(port, &softc->port_list, links) { 2489 struct ctl_port_entry entry, *list_entry; 2490 2491 if (list->fill_num >= list->alloc_num) { 2492 list->dropped_num++; 2493 continue; 2494 } 2495 2496 entry.port_type = port->port_type; 2497 strlcpy(entry.port_name, port->port_name, 2498 sizeof(entry.port_name)); 2499 entry.targ_port = port->targ_port; 2500 entry.physical_port = port->physical_port; 2501 entry.virtual_port = port->virtual_port; 2502 entry.wwnn = port->wwnn; 2503 entry.wwpn = port->wwpn; 2504 if (port->status & CTL_PORT_STATUS_ONLINE) 2505 entry.online = 1; 2506 else 2507 entry.online = 0; 2508 2509 list_entry = &list->entries[i]; 2510 2511 retval = copyout(&entry, list_entry, sizeof(entry)); 2512 if (retval != 0) { 2513 printf("%s: CTL_GET_PORT_LIST: copyout " 2514 "returned %d\n", __func__, retval); 2515 break; 2516 } 2517 i++; 2518 list->fill_num++; 2519 list->fill_len += sizeof(entry); 2520 } 2521 mtx_unlock(&softc->ctl_lock); 2522 2523 /* 2524 * If this is non-zero, we had a copyout fault, so there's 2525 * probably no point in attempting to set the status inside 2526 * the structure. 2527 */ 2528 if (retval != 0) 2529 break; 2530 2531 if (list->dropped_num > 0) 2532 list->status = CTL_PORT_LIST_NEED_MORE_SPACE; 2533 else 2534 list->status = CTL_PORT_LIST_OK; 2535 break; 2536 } 2537 case CTL_DUMP_OOA: { 2538 struct ctl_lun *lun; 2539 union ctl_io *io; 2540 char printbuf[128]; 2541 struct sbuf sb; 2542 2543 mtx_lock(&softc->ctl_lock); 2544 printf("Dumping OOA queues:\n"); 2545 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2546 mtx_lock(&lun->lun_lock); 2547 for (io = (union ctl_io *)TAILQ_FIRST( 2548 &lun->ooa_queue); io != NULL; 2549 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2550 ooa_links)) { 2551 sbuf_new(&sb, printbuf, sizeof(printbuf), 2552 SBUF_FIXEDLEN); 2553 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ", 2554 (intmax_t)lun->lun, 2555 io->scsiio.tag_num, 2556 (io->io_hdr.flags & 2557 CTL_FLAG_BLOCKED) ? "" : " BLOCKED", 2558 (io->io_hdr.flags & 2559 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 2560 (io->io_hdr.flags & 2561 CTL_FLAG_ABORT) ? " ABORT" : "", 2562 (io->io_hdr.flags & 2563 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : ""); 2564 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 2565 sbuf_finish(&sb); 2566 printf("%s\n", sbuf_data(&sb)); 2567 } 2568 mtx_unlock(&lun->lun_lock); 2569 } 2570 printf("OOA queues dump done\n"); 2571 mtx_unlock(&softc->ctl_lock); 2572 break; 2573 } 2574 case CTL_GET_OOA: { 2575 struct ctl_lun *lun; 2576 struct ctl_ooa *ooa_hdr; 2577 struct ctl_ooa_entry *entries; 2578 uint32_t cur_fill_num; 2579 2580 ooa_hdr = (struct ctl_ooa *)addr; 2581 2582 if ((ooa_hdr->alloc_len == 0) 2583 || (ooa_hdr->alloc_num == 0)) { 2584 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2585 "must be non-zero\n", __func__, 2586 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2587 retval = EINVAL; 2588 break; 2589 } 2590 2591 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2592 sizeof(struct ctl_ooa_entry))) { 2593 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2594 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2595 __func__, ooa_hdr->alloc_len, 2596 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2597 retval = EINVAL; 2598 break; 2599 } 2600 2601 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2602 if (entries == NULL) { 2603 printf("%s: could not allocate %d bytes for OOA " 2604 "dump\n", __func__, ooa_hdr->alloc_len); 2605 retval = ENOMEM; 2606 break; 2607 } 2608 2609 mtx_lock(&softc->ctl_lock); 2610 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2611 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2612 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2613 mtx_unlock(&softc->ctl_lock); 2614 free(entries, M_CTL); 2615 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2616 __func__, (uintmax_t)ooa_hdr->lun_num); 2617 retval = EINVAL; 2618 break; 2619 } 2620 2621 cur_fill_num = 0; 2622 2623 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2624 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2625 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2626 ooa_hdr, entries); 2627 if (retval != 0) 2628 break; 2629 } 2630 if (retval != 0) { 2631 mtx_unlock(&softc->ctl_lock); 2632 free(entries, M_CTL); 2633 break; 2634 } 2635 } else { 2636 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2637 2638 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr, 2639 entries); 2640 } 2641 mtx_unlock(&softc->ctl_lock); 2642 2643 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2644 ooa_hdr->fill_len = ooa_hdr->fill_num * 2645 sizeof(struct ctl_ooa_entry); 2646 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2647 if (retval != 0) { 2648 printf("%s: error copying out %d bytes for OOA dump\n", 2649 __func__, ooa_hdr->fill_len); 2650 } 2651 2652 getbintime(&ooa_hdr->cur_bt); 2653 2654 if (cur_fill_num > ooa_hdr->alloc_num) { 2655 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2656 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2657 } else { 2658 ooa_hdr->dropped_num = 0; 2659 ooa_hdr->status = CTL_OOA_OK; 2660 } 2661 2662 free(entries, M_CTL); 2663 break; 2664 } 2665 case CTL_CHECK_OOA: { 2666 union ctl_io *io; 2667 struct ctl_lun *lun; 2668 struct ctl_ooa_info *ooa_info; 2669 2670 2671 ooa_info = (struct ctl_ooa_info *)addr; 2672 2673 if (ooa_info->lun_id >= CTL_MAX_LUNS) { 2674 ooa_info->status = CTL_OOA_INVALID_LUN; 2675 break; 2676 } 2677 mtx_lock(&softc->ctl_lock); 2678 lun = softc->ctl_luns[ooa_info->lun_id]; 2679 if (lun == NULL) { 2680 mtx_unlock(&softc->ctl_lock); 2681 ooa_info->status = CTL_OOA_INVALID_LUN; 2682 break; 2683 } 2684 mtx_lock(&lun->lun_lock); 2685 mtx_unlock(&softc->ctl_lock); 2686 ooa_info->num_entries = 0; 2687 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 2688 io != NULL; io = (union ctl_io *)TAILQ_NEXT( 2689 &io->io_hdr, ooa_links)) { 2690 ooa_info->num_entries++; 2691 } 2692 mtx_unlock(&lun->lun_lock); 2693 2694 ooa_info->status = CTL_OOA_SUCCESS; 2695 2696 break; 2697 } 2698 case CTL_HARD_START: 2699 case CTL_HARD_STOP: { 2700 struct ctl_fe_ioctl_startstop_info ss_info; 2701 struct cfi_metatask *metatask; 2702 struct mtx hs_mtx; 2703 2704 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF); 2705 2706 cv_init(&ss_info.sem, "hard start/stop cv" ); 2707 2708 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2709 if (metatask == NULL) { 2710 retval = ENOMEM; 2711 mtx_destroy(&hs_mtx); 2712 break; 2713 } 2714 2715 if (cmd == CTL_HARD_START) 2716 metatask->tasktype = CFI_TASK_STARTUP; 2717 else 2718 metatask->tasktype = CFI_TASK_SHUTDOWN; 2719 2720 metatask->callback = ctl_ioctl_hard_startstop_callback; 2721 metatask->callback_arg = &ss_info; 2722 2723 cfi_action(metatask); 2724 2725 /* Wait for the callback */ 2726 mtx_lock(&hs_mtx); 2727 cv_wait_sig(&ss_info.sem, &hs_mtx); 2728 mtx_unlock(&hs_mtx); 2729 2730 /* 2731 * All information has been copied from the metatask by the 2732 * time cv_broadcast() is called, so we free the metatask here. 2733 */ 2734 cfi_free_metatask(metatask); 2735 2736 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info)); 2737 2738 mtx_destroy(&hs_mtx); 2739 break; 2740 } 2741 case CTL_BBRREAD: { 2742 struct ctl_bbrread_info *bbr_info; 2743 struct ctl_fe_ioctl_bbrread_info fe_bbr_info; 2744 struct mtx bbr_mtx; 2745 struct cfi_metatask *metatask; 2746 2747 bbr_info = (struct ctl_bbrread_info *)addr; 2748 2749 bzero(&fe_bbr_info, sizeof(fe_bbr_info)); 2750 2751 bzero(&bbr_mtx, sizeof(bbr_mtx)); 2752 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF); 2753 2754 fe_bbr_info.bbr_info = bbr_info; 2755 fe_bbr_info.lock = &bbr_mtx; 2756 2757 cv_init(&fe_bbr_info.sem, "BBR read cv"); 2758 metatask = cfi_alloc_metatask(/*can_wait*/ 1); 2759 2760 if (metatask == NULL) { 2761 mtx_destroy(&bbr_mtx); 2762 cv_destroy(&fe_bbr_info.sem); 2763 retval = ENOMEM; 2764 break; 2765 } 2766 metatask->tasktype = CFI_TASK_BBRREAD; 2767 metatask->callback = ctl_ioctl_bbrread_callback; 2768 metatask->callback_arg = &fe_bbr_info; 2769 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num; 2770 metatask->taskinfo.bbrread.lba = bbr_info->lba; 2771 metatask->taskinfo.bbrread.len = bbr_info->len; 2772 2773 cfi_action(metatask); 2774 2775 mtx_lock(&bbr_mtx); 2776 while (fe_bbr_info.wakeup_done == 0) 2777 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx); 2778 mtx_unlock(&bbr_mtx); 2779 2780 bbr_info->status = metatask->status; 2781 bbr_info->bbr_status = metatask->taskinfo.bbrread.status; 2782 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status; 2783 memcpy(&bbr_info->sense_data, 2784 &metatask->taskinfo.bbrread.sense_data, 2785 ctl_min(sizeof(bbr_info->sense_data), 2786 sizeof(metatask->taskinfo.bbrread.sense_data))); 2787 2788 cfi_free_metatask(metatask); 2789 2790 mtx_destroy(&bbr_mtx); 2791 cv_destroy(&fe_bbr_info.sem); 2792 2793 break; 2794 } 2795 case CTL_DELAY_IO: { 2796 struct ctl_io_delay_info *delay_info; 2797#ifdef CTL_IO_DELAY 2798 struct ctl_lun *lun; 2799#endif /* CTL_IO_DELAY */ 2800 2801 delay_info = (struct ctl_io_delay_info *)addr; 2802 2803#ifdef CTL_IO_DELAY 2804 mtx_lock(&softc->ctl_lock); 2805 2806 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2807 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2808 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2809 } else { 2810 lun = softc->ctl_luns[delay_info->lun_id]; 2811 mtx_lock(&lun->lun_lock); 2812 2813 delay_info->status = CTL_DELAY_STATUS_OK; 2814 2815 switch (delay_info->delay_type) { 2816 case CTL_DELAY_TYPE_CONT: 2817 break; 2818 case CTL_DELAY_TYPE_ONESHOT: 2819 break; 2820 default: 2821 delay_info->status = 2822 CTL_DELAY_STATUS_INVALID_TYPE; 2823 break; 2824 } 2825 2826 switch (delay_info->delay_loc) { 2827 case CTL_DELAY_LOC_DATAMOVE: 2828 lun->delay_info.datamove_type = 2829 delay_info->delay_type; 2830 lun->delay_info.datamove_delay = 2831 delay_info->delay_secs; 2832 break; 2833 case CTL_DELAY_LOC_DONE: 2834 lun->delay_info.done_type = 2835 delay_info->delay_type; 2836 lun->delay_info.done_delay = 2837 delay_info->delay_secs; 2838 break; 2839 default: 2840 delay_info->status = 2841 CTL_DELAY_STATUS_INVALID_LOC; 2842 break; 2843 } 2844 mtx_unlock(&lun->lun_lock); 2845 } 2846 2847 mtx_unlock(&softc->ctl_lock); 2848#else 2849 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2850#endif /* CTL_IO_DELAY */ 2851 break; 2852 } 2853 case CTL_REALSYNC_SET: { 2854 int *syncstate; 2855 2856 syncstate = (int *)addr; 2857 2858 mtx_lock(&softc->ctl_lock); 2859 switch (*syncstate) { 2860 case 0: 2861 softc->flags &= ~CTL_FLAG_REAL_SYNC; 2862 break; 2863 case 1: 2864 softc->flags |= CTL_FLAG_REAL_SYNC; 2865 break; 2866 default: 2867 retval = EINVAL; 2868 break; 2869 } 2870 mtx_unlock(&softc->ctl_lock); 2871 break; 2872 } 2873 case CTL_REALSYNC_GET: { 2874 int *syncstate; 2875 2876 syncstate = (int*)addr; 2877 2878 mtx_lock(&softc->ctl_lock); 2879 if (softc->flags & CTL_FLAG_REAL_SYNC) 2880 *syncstate = 1; 2881 else 2882 *syncstate = 0; 2883 mtx_unlock(&softc->ctl_lock); 2884 2885 break; 2886 } 2887 case CTL_SETSYNC: 2888 case CTL_GETSYNC: { 2889 struct ctl_sync_info *sync_info; 2890 struct ctl_lun *lun; 2891 2892 sync_info = (struct ctl_sync_info *)addr; 2893 2894 mtx_lock(&softc->ctl_lock); 2895 lun = softc->ctl_luns[sync_info->lun_id]; 2896 if (lun == NULL) { 2897 mtx_unlock(&softc->ctl_lock); 2898 sync_info->status = CTL_GS_SYNC_NO_LUN; 2899 } 2900 /* 2901 * Get or set the sync interval. We're not bounds checking 2902 * in the set case, hopefully the user won't do something 2903 * silly. 2904 */ 2905 mtx_lock(&lun->lun_lock); 2906 mtx_unlock(&softc->ctl_lock); 2907 if (cmd == CTL_GETSYNC) 2908 sync_info->sync_interval = lun->sync_interval; 2909 else 2910 lun->sync_interval = sync_info->sync_interval; 2911 mtx_unlock(&lun->lun_lock); 2912 2913 sync_info->status = CTL_GS_SYNC_OK; 2914 2915 break; 2916 } 2917 case CTL_GETSTATS: { 2918 struct ctl_stats *stats; 2919 struct ctl_lun *lun; 2920 int i; 2921 2922 stats = (struct ctl_stats *)addr; 2923 2924 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2925 stats->alloc_len) { 2926 stats->status = CTL_SS_NEED_MORE_SPACE; 2927 stats->num_luns = softc->num_luns; 2928 break; 2929 } 2930 /* 2931 * XXX KDM no locking here. If the LUN list changes, 2932 * things can blow up. 2933 */ 2934 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; 2935 i++, lun = STAILQ_NEXT(lun, links)) { 2936 retval = copyout(&lun->stats, &stats->lun_stats[i], 2937 sizeof(lun->stats)); 2938 if (retval != 0) 2939 break; 2940 } 2941 stats->num_luns = softc->num_luns; 2942 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2943 softc->num_luns; 2944 stats->status = CTL_SS_OK; 2945#ifdef CTL_TIME_IO 2946 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2947#else 2948 stats->flags = CTL_STATS_FLAG_NONE; 2949#endif 2950 getnanouptime(&stats->timestamp); 2951 break; 2952 } 2953 case CTL_ERROR_INJECT: { 2954 struct ctl_error_desc *err_desc, *new_err_desc; 2955 struct ctl_lun *lun; 2956 2957 err_desc = (struct ctl_error_desc *)addr; 2958 2959 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2960 M_WAITOK | M_ZERO); 2961 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2962 2963 mtx_lock(&softc->ctl_lock); 2964 lun = softc->ctl_luns[err_desc->lun_id]; 2965 if (lun == NULL) { 2966 mtx_unlock(&softc->ctl_lock); 2967 free(new_err_desc, M_CTL); 2968 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2969 __func__, (uintmax_t)err_desc->lun_id); 2970 retval = EINVAL; 2971 break; 2972 } 2973 mtx_lock(&lun->lun_lock); 2974 mtx_unlock(&softc->ctl_lock); 2975 2976 /* 2977 * We could do some checking here to verify the validity 2978 * of the request, but given the complexity of error 2979 * injection requests, the checking logic would be fairly 2980 * complex. 2981 * 2982 * For now, if the request is invalid, it just won't get 2983 * executed and might get deleted. 2984 */ 2985 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2986 2987 /* 2988 * XXX KDM check to make sure the serial number is unique, 2989 * in case we somehow manage to wrap. That shouldn't 2990 * happen for a very long time, but it's the right thing to 2991 * do. 2992 */ 2993 new_err_desc->serial = lun->error_serial; 2994 err_desc->serial = lun->error_serial; 2995 lun->error_serial++; 2996 2997 mtx_unlock(&lun->lun_lock); 2998 break; 2999 } 3000 case CTL_ERROR_INJECT_DELETE: { 3001 struct ctl_error_desc *delete_desc, *desc, *desc2; 3002 struct ctl_lun *lun; 3003 int delete_done; 3004 3005 delete_desc = (struct ctl_error_desc *)addr; 3006 delete_done = 0; 3007 3008 mtx_lock(&softc->ctl_lock); 3009 lun = softc->ctl_luns[delete_desc->lun_id]; 3010 if (lun == NULL) { 3011 mtx_unlock(&softc->ctl_lock); 3012 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 3013 __func__, (uintmax_t)delete_desc->lun_id); 3014 retval = EINVAL; 3015 break; 3016 } 3017 mtx_lock(&lun->lun_lock); 3018 mtx_unlock(&softc->ctl_lock); 3019 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 3020 if (desc->serial != delete_desc->serial) 3021 continue; 3022 3023 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 3024 links); 3025 free(desc, M_CTL); 3026 delete_done = 1; 3027 } 3028 mtx_unlock(&lun->lun_lock); 3029 if (delete_done == 0) { 3030 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 3031 "error serial %ju on LUN %u\n", __func__, 3032 delete_desc->serial, delete_desc->lun_id); 3033 retval = EINVAL; 3034 break; 3035 } 3036 break; 3037 } 3038 case CTL_DUMP_STRUCTS: { 3039 int i, j, k, idx; 3040 struct ctl_port *port; 3041 struct ctl_frontend *fe; 3042 3043 mtx_lock(&softc->ctl_lock); 3044 printf("CTL Persistent Reservation information start:\n"); 3045 for (i = 0; i < CTL_MAX_LUNS; i++) { 3046 struct ctl_lun *lun; 3047 3048 lun = softc->ctl_luns[i]; 3049 3050 if ((lun == NULL) 3051 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 3052 continue; 3053 3054 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) { 3055 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 3056 idx = j * CTL_MAX_INIT_PER_PORT + k; 3057 if (lun->pr_keys[idx] == 0) 3058 continue; 3059 printf(" LUN %d port %d iid %d key " 3060 "%#jx\n", i, j, k, 3061 (uintmax_t)lun->pr_keys[idx]); 3062 } 3063 } 3064 } 3065 printf("CTL Persistent Reservation information end\n"); 3066 printf("CTL Ports:\n"); 3067 STAILQ_FOREACH(port, &softc->port_list, links) { 3068 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 3069 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 3070 port->frontend->name, port->port_type, 3071 port->physical_port, port->virtual_port, 3072 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 3073 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3074 if (port->wwpn_iid[j].in_use == 0 && 3075 port->wwpn_iid[j].wwpn == 0 && 3076 port->wwpn_iid[j].name == NULL) 3077 continue; 3078 3079 printf(" iid %u use %d WWPN %#jx '%s'\n", 3080 j, port->wwpn_iid[j].in_use, 3081 (uintmax_t)port->wwpn_iid[j].wwpn, 3082 port->wwpn_iid[j].name); 3083 } 3084 } 3085 printf("CTL Port information end\n"); 3086 mtx_unlock(&softc->ctl_lock); 3087 /* 3088 * XXX KDM calling this without a lock. We'd likely want 3089 * to drop the lock before calling the frontend's dump 3090 * routine anyway. 3091 */ 3092 printf("CTL Frontends:\n"); 3093 STAILQ_FOREACH(fe, &softc->fe_list, links) { 3094 printf(" Frontend '%s'\n", fe->name); 3095 if (fe->fe_dump != NULL) 3096 fe->fe_dump(); 3097 } 3098 printf("CTL Frontend information end\n"); 3099 break; 3100 } 3101 case CTL_LUN_REQ: { 3102 struct ctl_lun_req *lun_req; 3103 struct ctl_backend_driver *backend; 3104 3105 lun_req = (struct ctl_lun_req *)addr; 3106 3107 backend = ctl_backend_find(lun_req->backend); 3108 if (backend == NULL) { 3109 lun_req->status = CTL_LUN_ERROR; 3110 snprintf(lun_req->error_str, 3111 sizeof(lun_req->error_str), 3112 "Backend \"%s\" not found.", 3113 lun_req->backend); 3114 break; 3115 } 3116 if (lun_req->num_be_args > 0) { 3117 lun_req->kern_be_args = ctl_copyin_args( 3118 lun_req->num_be_args, 3119 lun_req->be_args, 3120 lun_req->error_str, 3121 sizeof(lun_req->error_str)); 3122 if (lun_req->kern_be_args == NULL) { 3123 lun_req->status = CTL_LUN_ERROR; 3124 break; 3125 } 3126 } 3127 3128 retval = backend->ioctl(dev, cmd, addr, flag, td); 3129 3130 if (lun_req->num_be_args > 0) { 3131 ctl_copyout_args(lun_req->num_be_args, 3132 lun_req->kern_be_args); 3133 ctl_free_args(lun_req->num_be_args, 3134 lun_req->kern_be_args); 3135 } 3136 break; 3137 } 3138 case CTL_LUN_LIST: { 3139 struct sbuf *sb; 3140 struct ctl_lun *lun; 3141 struct ctl_lun_list *list; 3142 struct ctl_option *opt; 3143 3144 list = (struct ctl_lun_list *)addr; 3145 3146 /* 3147 * Allocate a fixed length sbuf here, based on the length 3148 * of the user's buffer. We could allocate an auto-extending 3149 * buffer, and then tell the user how much larger our 3150 * amount of data is than his buffer, but that presents 3151 * some problems: 3152 * 3153 * 1. The sbuf(9) routines use a blocking malloc, and so 3154 * we can't hold a lock while calling them with an 3155 * auto-extending buffer. 3156 * 3157 * 2. There is not currently a LUN reference counting 3158 * mechanism, outside of outstanding transactions on 3159 * the LUN's OOA queue. So a LUN could go away on us 3160 * while we're getting the LUN number, backend-specific 3161 * information, etc. Thus, given the way things 3162 * currently work, we need to hold the CTL lock while 3163 * grabbing LUN information. 3164 * 3165 * So, from the user's standpoint, the best thing to do is 3166 * allocate what he thinks is a reasonable buffer length, 3167 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3168 * double the buffer length and try again. (And repeat 3169 * that until he succeeds.) 3170 */ 3171 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3172 if (sb == NULL) { 3173 list->status = CTL_LUN_LIST_ERROR; 3174 snprintf(list->error_str, sizeof(list->error_str), 3175 "Unable to allocate %d bytes for LUN list", 3176 list->alloc_len); 3177 break; 3178 } 3179 3180 sbuf_printf(sb, "<ctllunlist>\n"); 3181 3182 mtx_lock(&softc->ctl_lock); 3183 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3184 mtx_lock(&lun->lun_lock); 3185 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3186 (uintmax_t)lun->lun); 3187 3188 /* 3189 * Bail out as soon as we see that we've overfilled 3190 * the buffer. 3191 */ 3192 if (retval != 0) 3193 break; 3194 3195 retval = sbuf_printf(sb, "\t<backend_type>%s" 3196 "</backend_type>\n", 3197 (lun->backend == NULL) ? "none" : 3198 lun->backend->name); 3199 3200 if (retval != 0) 3201 break; 3202 3203 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3204 lun->be_lun->lun_type); 3205 3206 if (retval != 0) 3207 break; 3208 3209 if (lun->backend == NULL) { 3210 retval = sbuf_printf(sb, "</lun>\n"); 3211 if (retval != 0) 3212 break; 3213 continue; 3214 } 3215 3216 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3217 (lun->be_lun->maxlba > 0) ? 3218 lun->be_lun->maxlba + 1 : 0); 3219 3220 if (retval != 0) 3221 break; 3222 3223 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3224 lun->be_lun->blocksize); 3225 3226 if (retval != 0) 3227 break; 3228 3229 retval = sbuf_printf(sb, "\t<serial_number>"); 3230 3231 if (retval != 0) 3232 break; 3233 3234 retval = ctl_sbuf_printf_esc(sb, 3235 lun->be_lun->serial_num, 3236 sizeof(lun->be_lun->serial_num)); 3237 3238 if (retval != 0) 3239 break; 3240 3241 retval = sbuf_printf(sb, "</serial_number>\n"); 3242 3243 if (retval != 0) 3244 break; 3245 3246 retval = sbuf_printf(sb, "\t<device_id>"); 3247 3248 if (retval != 0) 3249 break; 3250 3251 retval = ctl_sbuf_printf_esc(sb, 3252 lun->be_lun->device_id, 3253 sizeof(lun->be_lun->device_id)); 3254 3255 if (retval != 0) 3256 break; 3257 3258 retval = sbuf_printf(sb, "</device_id>\n"); 3259 3260 if (retval != 0) 3261 break; 3262 3263 if (lun->backend->lun_info != NULL) { 3264 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3265 if (retval != 0) 3266 break; 3267 } 3268 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3269 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3270 opt->name, opt->value, opt->name); 3271 if (retval != 0) 3272 break; 3273 } 3274 3275 retval = sbuf_printf(sb, "</lun>\n"); 3276 3277 if (retval != 0) 3278 break; 3279 mtx_unlock(&lun->lun_lock); 3280 } 3281 if (lun != NULL) 3282 mtx_unlock(&lun->lun_lock); 3283 mtx_unlock(&softc->ctl_lock); 3284 3285 if ((retval != 0) 3286 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3287 retval = 0; 3288 sbuf_delete(sb); 3289 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3290 snprintf(list->error_str, sizeof(list->error_str), 3291 "Out of space, %d bytes is too small", 3292 list->alloc_len); 3293 break; 3294 } 3295 3296 sbuf_finish(sb); 3297 3298 retval = copyout(sbuf_data(sb), list->lun_xml, 3299 sbuf_len(sb) + 1); 3300 3301 list->fill_len = sbuf_len(sb) + 1; 3302 list->status = CTL_LUN_LIST_OK; 3303 sbuf_delete(sb); 3304 break; 3305 } 3306 case CTL_ISCSI: { 3307 struct ctl_iscsi *ci; 3308 struct ctl_frontend *fe; 3309 3310 ci = (struct ctl_iscsi *)addr; 3311 3312 fe = ctl_frontend_find("iscsi"); 3313 if (fe == NULL) { 3314 ci->status = CTL_ISCSI_ERROR; 3315 snprintf(ci->error_str, sizeof(ci->error_str), 3316 "Frontend \"iscsi\" not found."); 3317 break; 3318 } 3319 3320 retval = fe->ioctl(dev, cmd, addr, flag, td); 3321 break; 3322 } 3323 case CTL_PORT_REQ: { 3324 struct ctl_req *req; 3325 struct ctl_frontend *fe; 3326 3327 req = (struct ctl_req *)addr; 3328 3329 fe = ctl_frontend_find(req->driver); 3330 if (fe == NULL) { 3331 req->status = CTL_LUN_ERROR; 3332 snprintf(req->error_str, sizeof(req->error_str), 3333 "Frontend \"%s\" not found.", req->driver); 3334 break; 3335 } 3336 if (req->num_args > 0) { 3337 req->kern_args = ctl_copyin_args(req->num_args, 3338 req->args, req->error_str, sizeof(req->error_str)); 3339 if (req->kern_args == NULL) { 3340 req->status = CTL_LUN_ERROR; 3341 break; 3342 } 3343 } 3344 3345 retval = fe->ioctl(dev, cmd, addr, flag, td); 3346 3347 if (req->num_args > 0) { 3348 ctl_copyout_args(req->num_args, req->kern_args); 3349 ctl_free_args(req->num_args, req->kern_args); 3350 } 3351 break; 3352 } 3353 case CTL_PORT_LIST: { 3354 struct sbuf *sb; 3355 struct ctl_port *port; 3356 struct ctl_lun_list *list; 3357 struct ctl_option *opt; 3358 int j; 3359 3360 list = (struct ctl_lun_list *)addr; 3361 3362 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3363 if (sb == NULL) { 3364 list->status = CTL_LUN_LIST_ERROR; 3365 snprintf(list->error_str, sizeof(list->error_str), 3366 "Unable to allocate %d bytes for LUN list", 3367 list->alloc_len); 3368 break; 3369 } 3370 3371 sbuf_printf(sb, "<ctlportlist>\n"); 3372 3373 mtx_lock(&softc->ctl_lock); 3374 STAILQ_FOREACH(port, &softc->port_list, links) { 3375 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3376 (uintmax_t)port->targ_port); 3377 3378 /* 3379 * Bail out as soon as we see that we've overfilled 3380 * the buffer. 3381 */ 3382 if (retval != 0) 3383 break; 3384 3385 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3386 "</frontend_type>\n", port->frontend->name); 3387 if (retval != 0) 3388 break; 3389 3390 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3391 port->port_type); 3392 if (retval != 0) 3393 break; 3394 3395 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3396 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3397 if (retval != 0) 3398 break; 3399 3400 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3401 port->port_name); 3402 if (retval != 0) 3403 break; 3404 3405 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3406 port->physical_port); 3407 if (retval != 0) 3408 break; 3409 3410 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3411 port->virtual_port); 3412 if (retval != 0) 3413 break; 3414 3415 if (port->target_devid != NULL) { 3416 sbuf_printf(sb, "\t<target>"); 3417 ctl_id_sbuf(port->target_devid, sb); 3418 sbuf_printf(sb, "</target>\n"); 3419 } 3420 3421 if (port->port_devid != NULL) { 3422 sbuf_printf(sb, "\t<port>"); 3423 ctl_id_sbuf(port->port_devid, sb); 3424 sbuf_printf(sb, "</port>\n"); 3425 } 3426 3427 if (port->port_info != NULL) { 3428 retval = port->port_info(port->onoff_arg, sb); 3429 if (retval != 0) 3430 break; 3431 } 3432 STAILQ_FOREACH(opt, &port->options, links) { 3433 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3434 opt->name, opt->value, opt->name); 3435 if (retval != 0) 3436 break; 3437 } 3438 3439 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3440 if (port->wwpn_iid[j].in_use == 0 || 3441 (port->wwpn_iid[j].wwpn == 0 && 3442 port->wwpn_iid[j].name == NULL)) 3443 continue; 3444 3445 if (port->wwpn_iid[j].name != NULL) 3446 retval = sbuf_printf(sb, 3447 "\t<initiator>%u %s</initiator>\n", 3448 j, port->wwpn_iid[j].name); 3449 else 3450 retval = sbuf_printf(sb, 3451 "\t<initiator>%u naa.%08jx</initiator>\n", 3452 j, port->wwpn_iid[j].wwpn); 3453 if (retval != 0) 3454 break; 3455 } 3456 if (retval != 0) 3457 break; 3458 3459 retval = sbuf_printf(sb, "</targ_port>\n"); 3460 if (retval != 0) 3461 break; 3462 } 3463 mtx_unlock(&softc->ctl_lock); 3464 3465 if ((retval != 0) 3466 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3467 retval = 0; 3468 sbuf_delete(sb); 3469 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3470 snprintf(list->error_str, sizeof(list->error_str), 3471 "Out of space, %d bytes is too small", 3472 list->alloc_len); 3473 break; 3474 } 3475 3476 sbuf_finish(sb); 3477 3478 retval = copyout(sbuf_data(sb), list->lun_xml, 3479 sbuf_len(sb) + 1); 3480 3481 list->fill_len = sbuf_len(sb) + 1; 3482 list->status = CTL_LUN_LIST_OK; 3483 sbuf_delete(sb); 3484 break; 3485 } 3486 default: { 3487 /* XXX KDM should we fix this? */ 3488#if 0 3489 struct ctl_backend_driver *backend; 3490 unsigned int type; 3491 int found; 3492 3493 found = 0; 3494 3495 /* 3496 * We encode the backend type as the ioctl type for backend 3497 * ioctls. So parse it out here, and then search for a 3498 * backend of this type. 3499 */ 3500 type = _IOC_TYPE(cmd); 3501 3502 STAILQ_FOREACH(backend, &softc->be_list, links) { 3503 if (backend->type == type) { 3504 found = 1; 3505 break; 3506 } 3507 } 3508 if (found == 0) { 3509 printf("ctl: unknown ioctl command %#lx or backend " 3510 "%d\n", cmd, type); 3511 retval = EINVAL; 3512 break; 3513 } 3514 retval = backend->ioctl(dev, cmd, addr, flag, td); 3515#endif 3516 retval = ENOTTY; 3517 break; 3518 } 3519 } 3520 return (retval); 3521} 3522 3523uint32_t 3524ctl_get_initindex(struct ctl_nexus *nexus) 3525{ 3526 if (nexus->targ_port < CTL_MAX_PORTS) 3527 return (nexus->initid.id + 3528 (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3529 else 3530 return (nexus->initid.id + 3531 ((nexus->targ_port - CTL_MAX_PORTS) * 3532 CTL_MAX_INIT_PER_PORT)); 3533} 3534 3535uint32_t 3536ctl_get_resindex(struct ctl_nexus *nexus) 3537{ 3538 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3539} 3540 3541uint32_t 3542ctl_port_idx(int port_num) 3543{ 3544 if (port_num < CTL_MAX_PORTS) 3545 return(port_num); 3546 else 3547 return(port_num - CTL_MAX_PORTS); 3548} 3549 3550static uint32_t 3551ctl_map_lun(int port_num, uint32_t lun_id) 3552{ 3553 struct ctl_port *port; 3554 3555 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3556 if (port == NULL) 3557 return (UINT32_MAX); 3558 if (port->lun_map == NULL) 3559 return (lun_id); 3560 return (port->lun_map(port->targ_lun_arg, lun_id)); 3561} 3562 3563static uint32_t 3564ctl_map_lun_back(int port_num, uint32_t lun_id) 3565{ 3566 struct ctl_port *port; 3567 uint32_t i; 3568 3569 port = control_softc->ctl_ports[ctl_port_idx(port_num)]; 3570 if (port->lun_map == NULL) 3571 return (lun_id); 3572 for (i = 0; i < CTL_MAX_LUNS; i++) { 3573 if (port->lun_map(port->targ_lun_arg, i) == lun_id) 3574 return (i); 3575 } 3576 return (UINT32_MAX); 3577} 3578 3579/* 3580 * Note: This only works for bitmask sizes that are at least 32 bits, and 3581 * that are a power of 2. 3582 */ 3583int 3584ctl_ffz(uint32_t *mask, uint32_t size) 3585{ 3586 uint32_t num_chunks, num_pieces; 3587 int i, j; 3588 3589 num_chunks = (size >> 5); 3590 if (num_chunks == 0) 3591 num_chunks++; 3592 num_pieces = ctl_min((sizeof(uint32_t) * 8), size); 3593 3594 for (i = 0; i < num_chunks; i++) { 3595 for (j = 0; j < num_pieces; j++) { 3596 if ((mask[i] & (1 << j)) == 0) 3597 return ((i << 5) + j); 3598 } 3599 } 3600 3601 return (-1); 3602} 3603 3604int 3605ctl_set_mask(uint32_t *mask, uint32_t bit) 3606{ 3607 uint32_t chunk, piece; 3608 3609 chunk = bit >> 5; 3610 piece = bit % (sizeof(uint32_t) * 8); 3611 3612 if ((mask[chunk] & (1 << piece)) != 0) 3613 return (-1); 3614 else 3615 mask[chunk] |= (1 << piece); 3616 3617 return (0); 3618} 3619 3620int 3621ctl_clear_mask(uint32_t *mask, uint32_t bit) 3622{ 3623 uint32_t chunk, piece; 3624 3625 chunk = bit >> 5; 3626 piece = bit % (sizeof(uint32_t) * 8); 3627 3628 if ((mask[chunk] & (1 << piece)) == 0) 3629 return (-1); 3630 else 3631 mask[chunk] &= ~(1 << piece); 3632 3633 return (0); 3634} 3635 3636int 3637ctl_is_set(uint32_t *mask, uint32_t bit) 3638{ 3639 uint32_t chunk, piece; 3640 3641 chunk = bit >> 5; 3642 piece = bit % (sizeof(uint32_t) * 8); 3643 3644 if ((mask[chunk] & (1 << piece)) == 0) 3645 return (0); 3646 else 3647 return (1); 3648} 3649 3650#ifdef unused 3651/* 3652 * The bus, target and lun are optional, they can be filled in later. 3653 * can_wait is used to determine whether we can wait on the malloc or not. 3654 */ 3655union ctl_io* 3656ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target, 3657 uint32_t targ_lun, int can_wait) 3658{ 3659 union ctl_io *io; 3660 3661 if (can_wait) 3662 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK); 3663 else 3664 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT); 3665 3666 if (io != NULL) { 3667 io->io_hdr.io_type = io_type; 3668 io->io_hdr.targ_port = targ_port; 3669 /* 3670 * XXX KDM this needs to change/go away. We need to move 3671 * to a preallocated pool of ctl_scsiio structures. 3672 */ 3673 io->io_hdr.nexus.targ_target.id = targ_target; 3674 io->io_hdr.nexus.targ_lun = targ_lun; 3675 } 3676 3677 return (io); 3678} 3679 3680void 3681ctl_kfree_io(union ctl_io *io) 3682{ 3683 free(io, M_CTL); 3684} 3685#endif /* unused */ 3686 3687/* 3688 * ctl_softc, pool_name, total_ctl_io are passed in. 3689 * npool is passed out. 3690 */ 3691int 3692ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3693 uint32_t total_ctl_io, void **npool) 3694{ 3695#ifdef IO_POOLS 3696 struct ctl_io_pool *pool; 3697 3698 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3699 M_NOWAIT | M_ZERO); 3700 if (pool == NULL) 3701 return (ENOMEM); 3702 3703 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3704 pool->ctl_softc = ctl_softc; 3705 pool->zone = uma_zsecond_create(pool->name, NULL, 3706 NULL, NULL, NULL, ctl_softc->io_zone); 3707 /* uma_prealloc(pool->zone, total_ctl_io); */ 3708 3709 *npool = pool; 3710#else 3711 *npool = ctl_softc->io_zone; 3712#endif 3713 return (0); 3714} 3715 3716void 3717ctl_pool_free(struct ctl_io_pool *pool) 3718{ 3719 3720 if (pool == NULL) 3721 return; 3722 3723#ifdef IO_POOLS 3724 uma_zdestroy(pool->zone); 3725 free(pool, M_CTL); 3726#endif 3727} 3728 3729union ctl_io * 3730ctl_alloc_io(void *pool_ref) 3731{ 3732 union ctl_io *io; 3733#ifdef IO_POOLS 3734 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3735 3736 io = uma_zalloc(pool->zone, M_WAITOK); 3737#else 3738 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3739#endif 3740 if (io != NULL) 3741 io->io_hdr.pool = pool_ref; 3742 return (io); 3743} 3744 3745union ctl_io * 3746ctl_alloc_io_nowait(void *pool_ref) 3747{ 3748 union ctl_io *io; 3749#ifdef IO_POOLS 3750 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3751 3752 io = uma_zalloc(pool->zone, M_NOWAIT); 3753#else 3754 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3755#endif 3756 if (io != NULL) 3757 io->io_hdr.pool = pool_ref; 3758 return (io); 3759} 3760 3761void 3762ctl_free_io(union ctl_io *io) 3763{ 3764#ifdef IO_POOLS 3765 struct ctl_io_pool *pool; 3766#endif 3767 3768 if (io == NULL) 3769 return; 3770 3771#ifdef IO_POOLS 3772 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3773 uma_zfree(pool->zone, io); 3774#else 3775 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3776#endif 3777} 3778 3779void 3780ctl_zero_io(union ctl_io *io) 3781{ 3782 void *pool_ref; 3783 3784 if (io == NULL) 3785 return; 3786 3787 /* 3788 * May need to preserve linked list pointers at some point too. 3789 */ 3790 pool_ref = io->io_hdr.pool; 3791 memset(io, 0, sizeof(*io)); 3792 io->io_hdr.pool = pool_ref; 3793} 3794 3795/* 3796 * This routine is currently used for internal copies of ctl_ios that need 3797 * to persist for some reason after we've already returned status to the 3798 * FETD. (Thus the flag set.) 3799 * 3800 * XXX XXX 3801 * Note that this makes a blind copy of all fields in the ctl_io, except 3802 * for the pool reference. This includes any memory that has been 3803 * allocated! That memory will no longer be valid after done has been 3804 * called, so this would be VERY DANGEROUS for command that actually does 3805 * any reads or writes. Right now (11/7/2005), this is only used for immediate 3806 * start and stop commands, which don't transfer any data, so this is not a 3807 * problem. If it is used for anything else, the caller would also need to 3808 * allocate data buffer space and this routine would need to be modified to 3809 * copy the data buffer(s) as well. 3810 */ 3811void 3812ctl_copy_io(union ctl_io *src, union ctl_io *dest) 3813{ 3814 void *pool_ref; 3815 3816 if ((src == NULL) 3817 || (dest == NULL)) 3818 return; 3819 3820 /* 3821 * May need to preserve linked list pointers at some point too. 3822 */ 3823 pool_ref = dest->io_hdr.pool; 3824 3825 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest))); 3826 3827 dest->io_hdr.pool = pool_ref; 3828 /* 3829 * We need to know that this is an internal copy, and doesn't need 3830 * to get passed back to the FETD that allocated it. 3831 */ 3832 dest->io_hdr.flags |= CTL_FLAG_INT_COPY; 3833} 3834 3835static int 3836ctl_expand_number(const char *buf, uint64_t *num) 3837{ 3838 char *endptr; 3839 uint64_t number; 3840 unsigned shift; 3841 3842 number = strtoq(buf, &endptr, 0); 3843 3844 switch (tolower((unsigned char)*endptr)) { 3845 case 'e': 3846 shift = 60; 3847 break; 3848 case 'p': 3849 shift = 50; 3850 break; 3851 case 't': 3852 shift = 40; 3853 break; 3854 case 'g': 3855 shift = 30; 3856 break; 3857 case 'm': 3858 shift = 20; 3859 break; 3860 case 'k': 3861 shift = 10; 3862 break; 3863 case 'b': 3864 case '\0': /* No unit. */ 3865 *num = number; 3866 return (0); 3867 default: 3868 /* Unrecognized unit. */ 3869 return (-1); 3870 } 3871 3872 if ((number << shift) >> shift != number) { 3873 /* Overflow */ 3874 return (-1); 3875 } 3876 *num = number << shift; 3877 return (0); 3878} 3879 3880 3881/* 3882 * This routine could be used in the future to load default and/or saved 3883 * mode page parameters for a particuar lun. 3884 */ 3885static int 3886ctl_init_page_index(struct ctl_lun *lun) 3887{ 3888 int i; 3889 struct ctl_page_index *page_index; 3890 const char *value; 3891 uint64_t ival; 3892 3893 memcpy(&lun->mode_pages.index, page_index_template, 3894 sizeof(page_index_template)); 3895 3896 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3897 3898 page_index = &lun->mode_pages.index[i]; 3899 /* 3900 * If this is a disk-only mode page, there's no point in 3901 * setting it up. For some pages, we have to have some 3902 * basic information about the disk in order to calculate the 3903 * mode page data. 3904 */ 3905 if ((lun->be_lun->lun_type != T_DIRECT) 3906 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 3907 continue; 3908 3909 switch (page_index->page_code & SMPH_PC_MASK) { 3910 case SMS_RW_ERROR_RECOVERY_PAGE: { 3911 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3912 panic("subpage is incorrect!"); 3913 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3914 &rw_er_page_default, 3915 sizeof(rw_er_page_default)); 3916 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3917 &rw_er_page_changeable, 3918 sizeof(rw_er_page_changeable)); 3919 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3920 &rw_er_page_default, 3921 sizeof(rw_er_page_default)); 3922 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3923 &rw_er_page_default, 3924 sizeof(rw_er_page_default)); 3925 page_index->page_data = 3926 (uint8_t *)lun->mode_pages.rw_er_page; 3927 break; 3928 } 3929 case SMS_FORMAT_DEVICE_PAGE: { 3930 struct scsi_format_page *format_page; 3931 3932 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3933 panic("subpage is incorrect!"); 3934 3935 /* 3936 * Sectors per track are set above. Bytes per 3937 * sector need to be set here on a per-LUN basis. 3938 */ 3939 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3940 &format_page_default, 3941 sizeof(format_page_default)); 3942 memcpy(&lun->mode_pages.format_page[ 3943 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3944 sizeof(format_page_changeable)); 3945 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3946 &format_page_default, 3947 sizeof(format_page_default)); 3948 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3949 &format_page_default, 3950 sizeof(format_page_default)); 3951 3952 format_page = &lun->mode_pages.format_page[ 3953 CTL_PAGE_CURRENT]; 3954 scsi_ulto2b(lun->be_lun->blocksize, 3955 format_page->bytes_per_sector); 3956 3957 format_page = &lun->mode_pages.format_page[ 3958 CTL_PAGE_DEFAULT]; 3959 scsi_ulto2b(lun->be_lun->blocksize, 3960 format_page->bytes_per_sector); 3961 3962 format_page = &lun->mode_pages.format_page[ 3963 CTL_PAGE_SAVED]; 3964 scsi_ulto2b(lun->be_lun->blocksize, 3965 format_page->bytes_per_sector); 3966 3967 page_index->page_data = 3968 (uint8_t *)lun->mode_pages.format_page; 3969 break; 3970 } 3971 case SMS_RIGID_DISK_PAGE: { 3972 struct scsi_rigid_disk_page *rigid_disk_page; 3973 uint32_t sectors_per_cylinder; 3974 uint64_t cylinders; 3975#ifndef __XSCALE__ 3976 int shift; 3977#endif /* !__XSCALE__ */ 3978 3979 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3980 panic("invalid subpage value %d", 3981 page_index->subpage); 3982 3983 /* 3984 * Rotation rate and sectors per track are set 3985 * above. We calculate the cylinders here based on 3986 * capacity. Due to the number of heads and 3987 * sectors per track we're using, smaller arrays 3988 * may turn out to have 0 cylinders. Linux and 3989 * FreeBSD don't pay attention to these mode pages 3990 * to figure out capacity, but Solaris does. It 3991 * seems to deal with 0 cylinders just fine, and 3992 * works out a fake geometry based on the capacity. 3993 */ 3994 memcpy(&lun->mode_pages.rigid_disk_page[ 3995 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3996 sizeof(rigid_disk_page_default)); 3997 memcpy(&lun->mode_pages.rigid_disk_page[ 3998 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3999 sizeof(rigid_disk_page_changeable)); 4000 4001 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 4002 CTL_DEFAULT_HEADS; 4003 4004 /* 4005 * The divide method here will be more accurate, 4006 * probably, but results in floating point being 4007 * used in the kernel on i386 (__udivdi3()). On the 4008 * XScale, though, __udivdi3() is implemented in 4009 * software. 4010 * 4011 * The shift method for cylinder calculation is 4012 * accurate if sectors_per_cylinder is a power of 4013 * 2. Otherwise it might be slightly off -- you 4014 * might have a bit of a truncation problem. 4015 */ 4016#ifdef __XSCALE__ 4017 cylinders = (lun->be_lun->maxlba + 1) / 4018 sectors_per_cylinder; 4019#else 4020 for (shift = 31; shift > 0; shift--) { 4021 if (sectors_per_cylinder & (1 << shift)) 4022 break; 4023 } 4024 cylinders = (lun->be_lun->maxlba + 1) >> shift; 4025#endif 4026 4027 /* 4028 * We've basically got 3 bytes, or 24 bits for the 4029 * cylinder size in the mode page. If we're over, 4030 * just round down to 2^24. 4031 */ 4032 if (cylinders > 0xffffff) 4033 cylinders = 0xffffff; 4034 4035 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 4036 CTL_PAGE_DEFAULT]; 4037 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 4038 4039 if ((value = ctl_get_opt(&lun->be_lun->options, 4040 "rpm")) != NULL) { 4041 scsi_ulto2b(strtol(value, NULL, 0), 4042 rigid_disk_page->rotation_rate); 4043 } 4044 4045 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 4046 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4047 sizeof(rigid_disk_page_default)); 4048 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 4049 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 4050 sizeof(rigid_disk_page_default)); 4051 4052 page_index->page_data = 4053 (uint8_t *)lun->mode_pages.rigid_disk_page; 4054 break; 4055 } 4056 case SMS_CACHING_PAGE: { 4057 struct scsi_caching_page *caching_page; 4058 4059 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4060 panic("invalid subpage value %d", 4061 page_index->subpage); 4062 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 4063 &caching_page_default, 4064 sizeof(caching_page_default)); 4065 memcpy(&lun->mode_pages.caching_page[ 4066 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 4067 sizeof(caching_page_changeable)); 4068 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4069 &caching_page_default, 4070 sizeof(caching_page_default)); 4071 caching_page = &lun->mode_pages.caching_page[ 4072 CTL_PAGE_SAVED]; 4073 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4074 if (value != NULL && strcmp(value, "off") == 0) 4075 caching_page->flags1 &= ~SCP_WCE; 4076 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4077 if (value != NULL && strcmp(value, "off") == 0) 4078 caching_page->flags1 |= SCP_RCD; 4079 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4080 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4081 sizeof(caching_page_default)); 4082 page_index->page_data = 4083 (uint8_t *)lun->mode_pages.caching_page; 4084 break; 4085 } 4086 case SMS_CONTROL_MODE_PAGE: { 4087 struct scsi_control_page *control_page; 4088 4089 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 4090 panic("invalid subpage value %d", 4091 page_index->subpage); 4092 4093 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT], 4094 &control_page_default, 4095 sizeof(control_page_default)); 4096 memcpy(&lun->mode_pages.control_page[ 4097 CTL_PAGE_CHANGEABLE], &control_page_changeable, 4098 sizeof(control_page_changeable)); 4099 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED], 4100 &control_page_default, 4101 sizeof(control_page_default)); 4102 control_page = &lun->mode_pages.control_page[ 4103 CTL_PAGE_SAVED]; 4104 value = ctl_get_opt(&lun->be_lun->options, "reordering"); 4105 if (value != NULL && strcmp(value, "unrestricted") == 0) { 4106 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK; 4107 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED; 4108 } 4109 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT], 4110 &lun->mode_pages.control_page[CTL_PAGE_SAVED], 4111 sizeof(control_page_default)); 4112 page_index->page_data = 4113 (uint8_t *)lun->mode_pages.control_page; 4114 break; 4115 4116 } 4117 case SMS_INFO_EXCEPTIONS_PAGE: { 4118 switch (page_index->subpage) { 4119 case SMS_SUBPAGE_PAGE_0: 4120 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4121 &ie_page_default, 4122 sizeof(ie_page_default)); 4123 memcpy(&lun->mode_pages.ie_page[ 4124 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4125 sizeof(ie_page_changeable)); 4126 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4127 &ie_page_default, 4128 sizeof(ie_page_default)); 4129 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4130 &ie_page_default, 4131 sizeof(ie_page_default)); 4132 page_index->page_data = 4133 (uint8_t *)lun->mode_pages.ie_page; 4134 break; 4135 case 0x02: { 4136 struct ctl_logical_block_provisioning_page *page; 4137 4138 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4139 &lbp_page_default, 4140 sizeof(lbp_page_default)); 4141 memcpy(&lun->mode_pages.lbp_page[ 4142 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4143 sizeof(lbp_page_changeable)); 4144 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4145 &lbp_page_default, 4146 sizeof(lbp_page_default)); 4147 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4148 value = ctl_get_opt(&lun->be_lun->options, 4149 "avail-threshold"); 4150 if (value != NULL && 4151 ctl_expand_number(value, &ival) == 0) { 4152 page->descr[0].flags |= SLBPPD_ENABLED | 4153 SLBPPD_ARMING_DEC; 4154 if (lun->be_lun->blocksize) 4155 ival /= lun->be_lun->blocksize; 4156 else 4157 ival /= 512; 4158 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4159 page->descr[0].count); 4160 } 4161 value = ctl_get_opt(&lun->be_lun->options, 4162 "used-threshold"); 4163 if (value != NULL && 4164 ctl_expand_number(value, &ival) == 0) { 4165 page->descr[1].flags |= SLBPPD_ENABLED | 4166 SLBPPD_ARMING_INC; 4167 if (lun->be_lun->blocksize) 4168 ival /= lun->be_lun->blocksize; 4169 else 4170 ival /= 512; 4171 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4172 page->descr[1].count); 4173 } 4174 value = ctl_get_opt(&lun->be_lun->options, 4175 "pool-avail-threshold"); 4176 if (value != NULL && 4177 ctl_expand_number(value, &ival) == 0) { 4178 page->descr[2].flags |= SLBPPD_ENABLED | 4179 SLBPPD_ARMING_DEC; 4180 if (lun->be_lun->blocksize) 4181 ival /= lun->be_lun->blocksize; 4182 else 4183 ival /= 512; 4184 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4185 page->descr[2].count); 4186 } 4187 value = ctl_get_opt(&lun->be_lun->options, 4188 "pool-used-threshold"); 4189 if (value != NULL && 4190 ctl_expand_number(value, &ival) == 0) { 4191 page->descr[3].flags |= SLBPPD_ENABLED | 4192 SLBPPD_ARMING_INC; 4193 if (lun->be_lun->blocksize) 4194 ival /= lun->be_lun->blocksize; 4195 else 4196 ival /= 512; 4197 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4198 page->descr[3].count); 4199 } 4200 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4201 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4202 sizeof(lbp_page_default)); 4203 page_index->page_data = 4204 (uint8_t *)lun->mode_pages.lbp_page; 4205 }} 4206 break; 4207 } 4208 case SMS_VENDOR_SPECIFIC_PAGE:{ 4209 switch (page_index->subpage) { 4210 case DBGCNF_SUBPAGE_CODE: { 4211 struct copan_debugconf_subpage *current_page, 4212 *saved_page; 4213 4214 memcpy(&lun->mode_pages.debugconf_subpage[ 4215 CTL_PAGE_CURRENT], 4216 &debugconf_page_default, 4217 sizeof(debugconf_page_default)); 4218 memcpy(&lun->mode_pages.debugconf_subpage[ 4219 CTL_PAGE_CHANGEABLE], 4220 &debugconf_page_changeable, 4221 sizeof(debugconf_page_changeable)); 4222 memcpy(&lun->mode_pages.debugconf_subpage[ 4223 CTL_PAGE_DEFAULT], 4224 &debugconf_page_default, 4225 sizeof(debugconf_page_default)); 4226 memcpy(&lun->mode_pages.debugconf_subpage[ 4227 CTL_PAGE_SAVED], 4228 &debugconf_page_default, 4229 sizeof(debugconf_page_default)); 4230 page_index->page_data = 4231 (uint8_t *)lun->mode_pages.debugconf_subpage; 4232 4233 current_page = (struct copan_debugconf_subpage *) 4234 (page_index->page_data + 4235 (page_index->page_len * 4236 CTL_PAGE_CURRENT)); 4237 saved_page = (struct copan_debugconf_subpage *) 4238 (page_index->page_data + 4239 (page_index->page_len * 4240 CTL_PAGE_SAVED)); 4241 break; 4242 } 4243 default: 4244 panic("invalid subpage value %d", 4245 page_index->subpage); 4246 break; 4247 } 4248 break; 4249 } 4250 default: 4251 panic("invalid page value %d", 4252 page_index->page_code & SMPH_PC_MASK); 4253 break; 4254 } 4255 } 4256 4257 return (CTL_RETVAL_COMPLETE); 4258} 4259 4260static int 4261ctl_init_log_page_index(struct ctl_lun *lun) 4262{ 4263 struct ctl_page_index *page_index; 4264 int i, j, k, prev; 4265 4266 memcpy(&lun->log_pages.index, log_page_index_template, 4267 sizeof(log_page_index_template)); 4268 4269 prev = -1; 4270 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4271 4272 page_index = &lun->log_pages.index[i]; 4273 /* 4274 * If this is a disk-only mode page, there's no point in 4275 * setting it up. For some pages, we have to have some 4276 * basic information about the disk in order to calculate the 4277 * mode page data. 4278 */ 4279 if ((lun->be_lun->lun_type != T_DIRECT) 4280 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY)) 4281 continue; 4282 4283 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4284 ((lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 4285 lun->backend->lun_attr == NULL)) 4286 continue; 4287 4288 if (page_index->page_code != prev) { 4289 lun->log_pages.pages_page[j] = page_index->page_code; 4290 prev = page_index->page_code; 4291 j++; 4292 } 4293 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4294 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4295 k++; 4296 } 4297 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4298 lun->log_pages.index[0].page_len = j; 4299 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4300 lun->log_pages.index[1].page_len = k * 2; 4301 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4302 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4303 4304 return (CTL_RETVAL_COMPLETE); 4305} 4306 4307static int 4308hex2bin(const char *str, uint8_t *buf, int buf_size) 4309{ 4310 int i; 4311 u_char c; 4312 4313 memset(buf, 0, buf_size); 4314 while (isspace(str[0])) 4315 str++; 4316 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4317 str += 2; 4318 buf_size *= 2; 4319 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4320 c = str[i]; 4321 if (isdigit(c)) 4322 c -= '0'; 4323 else if (isalpha(c)) 4324 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4325 else 4326 break; 4327 if (c >= 16) 4328 break; 4329 if ((i & 1) == 0) 4330 buf[i / 2] |= (c << 4); 4331 else 4332 buf[i / 2] |= c; 4333 } 4334 return ((i + 1) / 2); 4335} 4336 4337/* 4338 * LUN allocation. 4339 * 4340 * Requirements: 4341 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4342 * wants us to allocate the LUN and he can block. 4343 * - ctl_softc is always set 4344 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4345 * 4346 * Returns 0 for success, non-zero (errno) for failure. 4347 */ 4348static int 4349ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4350 struct ctl_be_lun *const be_lun, struct ctl_id target_id) 4351{ 4352 struct ctl_lun *nlun, *lun; 4353 struct ctl_port *port; 4354 struct scsi_vpd_id_descriptor *desc; 4355 struct scsi_vpd_id_t10 *t10id; 4356 const char *eui, *naa, *scsiname, *vendor, *value; 4357 int lun_number, i, lun_malloced; 4358 int devidlen, idlen1, idlen2 = 0, len; 4359 4360 if (be_lun == NULL) 4361 return (EINVAL); 4362 4363 /* 4364 * We currently only support Direct Access or Processor LUN types. 4365 */ 4366 switch (be_lun->lun_type) { 4367 case T_DIRECT: 4368 break; 4369 case T_PROCESSOR: 4370 break; 4371 case T_SEQUENTIAL: 4372 case T_CHANGER: 4373 default: 4374 be_lun->lun_config_status(be_lun->be_lun, 4375 CTL_LUN_CONFIG_FAILURE); 4376 break; 4377 } 4378 if (ctl_lun == NULL) { 4379 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4380 lun_malloced = 1; 4381 } else { 4382 lun_malloced = 0; 4383 lun = ctl_lun; 4384 } 4385 4386 memset(lun, 0, sizeof(*lun)); 4387 if (lun_malloced) 4388 lun->flags = CTL_LUN_MALLOCED; 4389 4390 /* Generate LUN ID. */ 4391 devidlen = max(CTL_DEVID_MIN_LEN, 4392 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4393 idlen1 = sizeof(*t10id) + devidlen; 4394 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4395 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4396 if (scsiname != NULL) { 4397 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4398 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4399 } 4400 eui = ctl_get_opt(&be_lun->options, "eui"); 4401 if (eui != NULL) { 4402 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4403 } 4404 naa = ctl_get_opt(&be_lun->options, "naa"); 4405 if (naa != NULL) { 4406 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4407 } 4408 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4409 M_CTL, M_WAITOK | M_ZERO); 4410 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4411 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4412 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4413 desc->length = idlen1; 4414 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4415 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4416 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4417 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4418 } else { 4419 strncpy(t10id->vendor, vendor, 4420 min(sizeof(t10id->vendor), strlen(vendor))); 4421 } 4422 strncpy((char *)t10id->vendor_spec_id, 4423 (char *)be_lun->device_id, devidlen); 4424 if (scsiname != NULL) { 4425 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4426 desc->length); 4427 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4428 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4429 SVPD_ID_TYPE_SCSI_NAME; 4430 desc->length = idlen2; 4431 strlcpy(desc->identifier, scsiname, idlen2); 4432 } 4433 if (eui != NULL) { 4434 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4435 desc->length); 4436 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4437 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4438 SVPD_ID_TYPE_EUI64; 4439 desc->length = hex2bin(eui, desc->identifier, 16); 4440 desc->length = desc->length > 12 ? 16 : 4441 (desc->length > 8 ? 12 : 8); 4442 len -= 16 - desc->length; 4443 } 4444 if (naa != NULL) { 4445 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4446 desc->length); 4447 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4448 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4449 SVPD_ID_TYPE_NAA; 4450 desc->length = hex2bin(naa, desc->identifier, 16); 4451 desc->length = desc->length > 8 ? 16 : 8; 4452 len -= 16 - desc->length; 4453 } 4454 lun->lun_devid->len = len; 4455 4456 mtx_lock(&ctl_softc->ctl_lock); 4457 /* 4458 * See if the caller requested a particular LUN number. If so, see 4459 * if it is available. Otherwise, allocate the first available LUN. 4460 */ 4461 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4462 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4463 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4464 mtx_unlock(&ctl_softc->ctl_lock); 4465 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4466 printf("ctl: requested LUN ID %d is higher " 4467 "than CTL_MAX_LUNS - 1 (%d)\n", 4468 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4469 } else { 4470 /* 4471 * XXX KDM return an error, or just assign 4472 * another LUN ID in this case?? 4473 */ 4474 printf("ctl: requested LUN ID %d is already " 4475 "in use\n", be_lun->req_lun_id); 4476 } 4477 if (lun->flags & CTL_LUN_MALLOCED) 4478 free(lun, M_CTL); 4479 be_lun->lun_config_status(be_lun->be_lun, 4480 CTL_LUN_CONFIG_FAILURE); 4481 return (ENOSPC); 4482 } 4483 lun_number = be_lun->req_lun_id; 4484 } else { 4485 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS); 4486 if (lun_number == -1) { 4487 mtx_unlock(&ctl_softc->ctl_lock); 4488 printf("ctl: can't allocate LUN on target %ju, out of " 4489 "LUNs\n", (uintmax_t)target_id.id); 4490 if (lun->flags & CTL_LUN_MALLOCED) 4491 free(lun, M_CTL); 4492 be_lun->lun_config_status(be_lun->be_lun, 4493 CTL_LUN_CONFIG_FAILURE); 4494 return (ENOSPC); 4495 } 4496 } 4497 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4498 4499 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4500 lun->target = target_id; 4501 lun->lun = lun_number; 4502 lun->be_lun = be_lun; 4503 /* 4504 * The processor LUN is always enabled. Disk LUNs come on line 4505 * disabled, and must be enabled by the backend. 4506 */ 4507 lun->flags |= CTL_LUN_DISABLED; 4508 lun->backend = be_lun->be; 4509 be_lun->ctl_lun = lun; 4510 be_lun->lun_id = lun_number; 4511 atomic_add_int(&be_lun->be->num_luns, 1); 4512 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE) 4513 lun->flags |= CTL_LUN_OFFLINE; 4514 4515 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF) 4516 lun->flags |= CTL_LUN_STOPPED; 4517 4518 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE) 4519 lun->flags |= CTL_LUN_INOPERABLE; 4520 4521 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4522 lun->flags |= CTL_LUN_PRIMARY_SC; 4523 4524 value = ctl_get_opt(&be_lun->options, "readonly"); 4525 if (value != NULL && strcmp(value, "on") == 0) 4526 lun->flags |= CTL_LUN_READONLY; 4527 4528 lun->ctl_softc = ctl_softc; 4529 TAILQ_INIT(&lun->ooa_queue); 4530 TAILQ_INIT(&lun->blocked_queue); 4531 STAILQ_INIT(&lun->error_list); 4532 ctl_tpc_lun_init(lun); 4533 4534 /* 4535 * Initialize the mode and log page index. 4536 */ 4537 ctl_init_page_index(lun); 4538 ctl_init_log_page_index(lun); 4539 4540 /* 4541 * Set the poweron UA for all initiators on this LUN only. 4542 */ 4543 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4544 lun->pending_ua[i] = CTL_UA_POWERON; 4545 4546 /* 4547 * Now, before we insert this lun on the lun list, set the lun 4548 * inventory changed UA for all other luns. 4549 */ 4550 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4551 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4552 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4553 } 4554 } 4555 4556 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4557 4558 ctl_softc->ctl_luns[lun_number] = lun; 4559 4560 ctl_softc->num_luns++; 4561 4562 /* Setup statistics gathering */ 4563 lun->stats.device_type = be_lun->lun_type; 4564 lun->stats.lun_number = lun_number; 4565 if (lun->stats.device_type == T_DIRECT) 4566 lun->stats.blocksize = be_lun->blocksize; 4567 else 4568 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4569 for (i = 0;i < CTL_MAX_PORTS;i++) 4570 lun->stats.ports[i].targ_port = i; 4571 4572 mtx_unlock(&ctl_softc->ctl_lock); 4573 4574 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4575 4576 /* 4577 * Run through each registered FETD and bring it online if it isn't 4578 * already. Enable the target ID if it hasn't been enabled, and 4579 * enable this particular LUN. 4580 */ 4581 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4582 int retval; 4583 4584 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number); 4585 if (retval != 0) { 4586 printf("ctl_alloc_lun: FETD %s port %d returned error " 4587 "%d for lun_enable on target %ju lun %d\n", 4588 port->port_name, port->targ_port, retval, 4589 (uintmax_t)target_id.id, lun_number); 4590 } else 4591 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4592 } 4593 return (0); 4594} 4595 4596/* 4597 * Delete a LUN. 4598 * Assumptions: 4599 * - LUN has already been marked invalid and any pending I/O has been taken 4600 * care of. 4601 */ 4602static int 4603ctl_free_lun(struct ctl_lun *lun) 4604{ 4605 struct ctl_softc *softc; 4606#if 0 4607 struct ctl_port *port; 4608#endif 4609 struct ctl_lun *nlun; 4610 int i; 4611 4612 softc = lun->ctl_softc; 4613 4614 mtx_assert(&softc->ctl_lock, MA_OWNED); 4615 4616 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4617 4618 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4619 4620 softc->ctl_luns[lun->lun] = NULL; 4621 4622 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4623 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4624 4625 softc->num_luns--; 4626 4627 /* 4628 * XXX KDM this scheme only works for a single target/multiple LUN 4629 * setup. It needs to be revamped for a multiple target scheme. 4630 * 4631 * XXX KDM this results in port->lun_disable() getting called twice, 4632 * once when ctl_disable_lun() is called, and a second time here. 4633 * We really need to re-think the LUN disable semantics. There 4634 * should probably be several steps/levels to LUN removal: 4635 * - disable 4636 * - invalidate 4637 * - free 4638 * 4639 * Right now we only have a disable method when communicating to 4640 * the front end ports, at least for individual LUNs. 4641 */ 4642#if 0 4643 STAILQ_FOREACH(port, &softc->port_list, links) { 4644 int retval; 4645 4646 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4647 lun->lun); 4648 if (retval != 0) { 4649 printf("ctl_free_lun: FETD %s port %d returned error " 4650 "%d for lun_disable on target %ju lun %jd\n", 4651 port->port_name, port->targ_port, retval, 4652 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4653 } 4654 4655 if (STAILQ_FIRST(&softc->lun_list) == NULL) { 4656 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE; 4657 4658 retval = port->targ_disable(port->targ_lun_arg,lun->target); 4659 if (retval != 0) { 4660 printf("ctl_free_lun: FETD %s port %d " 4661 "returned error %d for targ_disable on " 4662 "target %ju\n", port->port_name, 4663 port->targ_port, retval, 4664 (uintmax_t)lun->target.id); 4665 } else 4666 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE; 4667 4668 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0) 4669 continue; 4670 4671#if 0 4672 port->port_offline(port->onoff_arg); 4673 port->status &= ~CTL_PORT_STATUS_ONLINE; 4674#endif 4675 } 4676 } 4677#endif 4678 4679 /* 4680 * Tell the backend to free resources, if this LUN has a backend. 4681 */ 4682 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4683 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4684 4685 ctl_tpc_lun_shutdown(lun); 4686 mtx_destroy(&lun->lun_lock); 4687 free(lun->lun_devid, M_CTL); 4688 free(lun->write_buffer, M_CTL); 4689 if (lun->flags & CTL_LUN_MALLOCED) 4690 free(lun, M_CTL); 4691 4692 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4693 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 4694 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE; 4695 } 4696 } 4697 4698 return (0); 4699} 4700 4701static void 4702ctl_create_lun(struct ctl_be_lun *be_lun) 4703{ 4704 struct ctl_softc *ctl_softc; 4705 4706 ctl_softc = control_softc; 4707 4708 /* 4709 * ctl_alloc_lun() should handle all potential failure cases. 4710 */ 4711 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target); 4712} 4713 4714int 4715ctl_add_lun(struct ctl_be_lun *be_lun) 4716{ 4717 struct ctl_softc *ctl_softc = control_softc; 4718 4719 mtx_lock(&ctl_softc->ctl_lock); 4720 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links); 4721 mtx_unlock(&ctl_softc->ctl_lock); 4722 wakeup(&ctl_softc->pending_lun_queue); 4723 4724 return (0); 4725} 4726 4727int 4728ctl_enable_lun(struct ctl_be_lun *be_lun) 4729{ 4730 struct ctl_softc *ctl_softc; 4731 struct ctl_port *port, *nport; 4732 struct ctl_lun *lun; 4733 int retval; 4734 4735 ctl_softc = control_softc; 4736 4737 lun = (struct ctl_lun *)be_lun->ctl_lun; 4738 4739 mtx_lock(&ctl_softc->ctl_lock); 4740 mtx_lock(&lun->lun_lock); 4741 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4742 /* 4743 * eh? Why did we get called if the LUN is already 4744 * enabled? 4745 */ 4746 mtx_unlock(&lun->lun_lock); 4747 mtx_unlock(&ctl_softc->ctl_lock); 4748 return (0); 4749 } 4750 lun->flags &= ~CTL_LUN_DISABLED; 4751 mtx_unlock(&lun->lun_lock); 4752 4753 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) { 4754 nport = STAILQ_NEXT(port, links); 4755 4756 /* 4757 * Drop the lock while we call the FETD's enable routine. 4758 * This can lead to a callback into CTL (at least in the 4759 * case of the internal initiator frontend. 4760 */ 4761 mtx_unlock(&ctl_softc->ctl_lock); 4762 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun); 4763 mtx_lock(&ctl_softc->ctl_lock); 4764 if (retval != 0) { 4765 printf("%s: FETD %s port %d returned error " 4766 "%d for lun_enable on target %ju lun %jd\n", 4767 __func__, port->port_name, port->targ_port, retval, 4768 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4769 } 4770#if 0 4771 else { 4772 /* NOTE: TODO: why does lun enable affect port status? */ 4773 port->status |= CTL_PORT_STATUS_LUN_ONLINE; 4774 } 4775#endif 4776 } 4777 4778 mtx_unlock(&ctl_softc->ctl_lock); 4779 4780 return (0); 4781} 4782 4783int 4784ctl_disable_lun(struct ctl_be_lun *be_lun) 4785{ 4786 struct ctl_softc *ctl_softc; 4787 struct ctl_port *port; 4788 struct ctl_lun *lun; 4789 int retval; 4790 4791 ctl_softc = control_softc; 4792 4793 lun = (struct ctl_lun *)be_lun->ctl_lun; 4794 4795 mtx_lock(&ctl_softc->ctl_lock); 4796 mtx_lock(&lun->lun_lock); 4797 if (lun->flags & CTL_LUN_DISABLED) { 4798 mtx_unlock(&lun->lun_lock); 4799 mtx_unlock(&ctl_softc->ctl_lock); 4800 return (0); 4801 } 4802 lun->flags |= CTL_LUN_DISABLED; 4803 mtx_unlock(&lun->lun_lock); 4804 4805 STAILQ_FOREACH(port, &ctl_softc->port_list, links) { 4806 mtx_unlock(&ctl_softc->ctl_lock); 4807 /* 4808 * Drop the lock before we call the frontend's disable 4809 * routine, to avoid lock order reversals. 4810 * 4811 * XXX KDM what happens if the frontend list changes while 4812 * we're traversing it? It's unlikely, but should be handled. 4813 */ 4814 retval = port->lun_disable(port->targ_lun_arg, lun->target, 4815 lun->lun); 4816 mtx_lock(&ctl_softc->ctl_lock); 4817 if (retval != 0) { 4818 printf("ctl_alloc_lun: FETD %s port %d returned error " 4819 "%d for lun_disable on target %ju lun %jd\n", 4820 port->port_name, port->targ_port, retval, 4821 (uintmax_t)lun->target.id, (intmax_t)lun->lun); 4822 } 4823 } 4824 4825 mtx_unlock(&ctl_softc->ctl_lock); 4826 4827 return (0); 4828} 4829 4830int 4831ctl_start_lun(struct ctl_be_lun *be_lun) 4832{ 4833 struct ctl_softc *ctl_softc; 4834 struct ctl_lun *lun; 4835 4836 ctl_softc = control_softc; 4837 4838 lun = (struct ctl_lun *)be_lun->ctl_lun; 4839 4840 mtx_lock(&lun->lun_lock); 4841 lun->flags &= ~CTL_LUN_STOPPED; 4842 mtx_unlock(&lun->lun_lock); 4843 4844 return (0); 4845} 4846 4847int 4848ctl_stop_lun(struct ctl_be_lun *be_lun) 4849{ 4850 struct ctl_softc *ctl_softc; 4851 struct ctl_lun *lun; 4852 4853 ctl_softc = control_softc; 4854 4855 lun = (struct ctl_lun *)be_lun->ctl_lun; 4856 4857 mtx_lock(&lun->lun_lock); 4858 lun->flags |= CTL_LUN_STOPPED; 4859 mtx_unlock(&lun->lun_lock); 4860 4861 return (0); 4862} 4863 4864int 4865ctl_lun_offline(struct ctl_be_lun *be_lun) 4866{ 4867 struct ctl_softc *ctl_softc; 4868 struct ctl_lun *lun; 4869 4870 ctl_softc = control_softc; 4871 4872 lun = (struct ctl_lun *)be_lun->ctl_lun; 4873 4874 mtx_lock(&lun->lun_lock); 4875 lun->flags |= CTL_LUN_OFFLINE; 4876 mtx_unlock(&lun->lun_lock); 4877 4878 return (0); 4879} 4880 4881int 4882ctl_lun_online(struct ctl_be_lun *be_lun) 4883{ 4884 struct ctl_softc *ctl_softc; 4885 struct ctl_lun *lun; 4886 4887 ctl_softc = control_softc; 4888 4889 lun = (struct ctl_lun *)be_lun->ctl_lun; 4890 4891 mtx_lock(&lun->lun_lock); 4892 lun->flags &= ~CTL_LUN_OFFLINE; 4893 mtx_unlock(&lun->lun_lock); 4894 4895 return (0); 4896} 4897 4898int 4899ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4900{ 4901 struct ctl_softc *ctl_softc; 4902 struct ctl_lun *lun; 4903 4904 ctl_softc = control_softc; 4905 4906 lun = (struct ctl_lun *)be_lun->ctl_lun; 4907 4908 mtx_lock(&lun->lun_lock); 4909 4910 /* 4911 * The LUN needs to be disabled before it can be marked invalid. 4912 */ 4913 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4914 mtx_unlock(&lun->lun_lock); 4915 return (-1); 4916 } 4917 /* 4918 * Mark the LUN invalid. 4919 */ 4920 lun->flags |= CTL_LUN_INVALID; 4921 4922 /* 4923 * If there is nothing in the OOA queue, go ahead and free the LUN. 4924 * If we have something in the OOA queue, we'll free it when the 4925 * last I/O completes. 4926 */ 4927 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4928 mtx_unlock(&lun->lun_lock); 4929 mtx_lock(&ctl_softc->ctl_lock); 4930 ctl_free_lun(lun); 4931 mtx_unlock(&ctl_softc->ctl_lock); 4932 } else 4933 mtx_unlock(&lun->lun_lock); 4934 4935 return (0); 4936} 4937 4938int 4939ctl_lun_inoperable(struct ctl_be_lun *be_lun) 4940{ 4941 struct ctl_softc *ctl_softc; 4942 struct ctl_lun *lun; 4943 4944 ctl_softc = control_softc; 4945 lun = (struct ctl_lun *)be_lun->ctl_lun; 4946 4947 mtx_lock(&lun->lun_lock); 4948 lun->flags |= CTL_LUN_INOPERABLE; 4949 mtx_unlock(&lun->lun_lock); 4950 4951 return (0); 4952} 4953 4954int 4955ctl_lun_operable(struct ctl_be_lun *be_lun) 4956{ 4957 struct ctl_softc *ctl_softc; 4958 struct ctl_lun *lun; 4959 4960 ctl_softc = control_softc; 4961 lun = (struct ctl_lun *)be_lun->ctl_lun; 4962 4963 mtx_lock(&lun->lun_lock); 4964 lun->flags &= ~CTL_LUN_INOPERABLE; 4965 mtx_unlock(&lun->lun_lock); 4966 4967 return (0); 4968} 4969 4970void 4971ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4972{ 4973 struct ctl_lun *lun; 4974 struct ctl_softc *softc; 4975 int i; 4976 4977 softc = control_softc; 4978 4979 lun = (struct ctl_lun *)be_lun->ctl_lun; 4980 4981 mtx_lock(&lun->lun_lock); 4982 4983 for (i = 0; i < CTL_MAX_INITIATORS; i++) 4984 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED; 4985 4986 mtx_unlock(&lun->lun_lock); 4987} 4988 4989/* 4990 * Backend "memory move is complete" callback for requests that never 4991 * make it down to say RAIDCore's configuration code. 4992 */ 4993int 4994ctl_config_move_done(union ctl_io *io) 4995{ 4996 int retval; 4997 4998 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4999 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 5000 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 5001 5002 if ((io->io_hdr.port_status != 0) && 5003 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5004 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5005 /* 5006 * For hardware error sense keys, the sense key 5007 * specific value is defined to be a retry count, 5008 * but we use it to pass back an internal FETD 5009 * error code. XXX KDM Hopefully the FETD is only 5010 * using 16 bits for an error code, since that's 5011 * all the space we have in the sks field. 5012 */ 5013 ctl_set_internal_failure(&io->scsiio, 5014 /*sks_valid*/ 1, 5015 /*retry_count*/ 5016 io->io_hdr.port_status); 5017 } 5018 5019 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 5020 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5021 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 5022 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 5023 /* 5024 * XXX KDM just assuming a single pointer here, and not a 5025 * S/G list. If we start using S/G lists for config data, 5026 * we'll need to know how to clean them up here as well. 5027 */ 5028 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5029 free(io->scsiio.kern_data_ptr, M_CTL); 5030 ctl_done(io); 5031 retval = CTL_RETVAL_COMPLETE; 5032 } else { 5033 /* 5034 * XXX KDM now we need to continue data movement. Some 5035 * options: 5036 * - call ctl_scsiio() again? We don't do this for data 5037 * writes, because for those at least we know ahead of 5038 * time where the write will go and how long it is. For 5039 * config writes, though, that information is largely 5040 * contained within the write itself, thus we need to 5041 * parse out the data again. 5042 * 5043 * - Call some other function once the data is in? 5044 */ 5045 if (ctl_debug & CTL_DEBUG_CDB_DATA) 5046 ctl_data_print(io); 5047 5048 /* 5049 * XXX KDM call ctl_scsiio() again for now, and check flag 5050 * bits to see whether we're allocated or not. 5051 */ 5052 retval = ctl_scsiio(&io->scsiio); 5053 } 5054 return (retval); 5055} 5056 5057/* 5058 * This gets called by a backend driver when it is done with a 5059 * data_submit method. 5060 */ 5061void 5062ctl_data_submit_done(union ctl_io *io) 5063{ 5064 /* 5065 * If the IO_CONT flag is set, we need to call the supplied 5066 * function to continue processing the I/O, instead of completing 5067 * the I/O just yet. 5068 * 5069 * If there is an error, though, we don't want to keep processing. 5070 * Instead, just send status back to the initiator. 5071 */ 5072 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5073 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5074 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5075 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5076 io->scsiio.io_cont(io); 5077 return; 5078 } 5079 ctl_done(io); 5080} 5081 5082/* 5083 * This gets called by a backend driver when it is done with a 5084 * configuration write. 5085 */ 5086void 5087ctl_config_write_done(union ctl_io *io) 5088{ 5089 uint8_t *buf; 5090 5091 /* 5092 * If the IO_CONT flag is set, we need to call the supplied 5093 * function to continue processing the I/O, instead of completing 5094 * the I/O just yet. 5095 * 5096 * If there is an error, though, we don't want to keep processing. 5097 * Instead, just send status back to the initiator. 5098 */ 5099 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 5100 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 5101 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 5102 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 5103 io->scsiio.io_cont(io); 5104 return; 5105 } 5106 /* 5107 * Since a configuration write can be done for commands that actually 5108 * have data allocated, like write buffer, and commands that have 5109 * no data, like start/stop unit, we need to check here. 5110 */ 5111 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5112 buf = io->scsiio.kern_data_ptr; 5113 else 5114 buf = NULL; 5115 ctl_done(io); 5116 if (buf) 5117 free(buf, M_CTL); 5118} 5119 5120/* 5121 * SCSI release command. 5122 */ 5123int 5124ctl_scsi_release(struct ctl_scsiio *ctsio) 5125{ 5126 int length, longid, thirdparty_id, resv_id; 5127 struct ctl_softc *ctl_softc; 5128 struct ctl_lun *lun; 5129 uint32_t residx; 5130 5131 length = 0; 5132 resv_id = 0; 5133 5134 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5135 5136 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5137 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5138 ctl_softc = control_softc; 5139 5140 switch (ctsio->cdb[0]) { 5141 case RELEASE_10: { 5142 struct scsi_release_10 *cdb; 5143 5144 cdb = (struct scsi_release_10 *)ctsio->cdb; 5145 5146 if (cdb->byte2 & SR10_LONGID) 5147 longid = 1; 5148 else 5149 thirdparty_id = cdb->thirdparty_id; 5150 5151 resv_id = cdb->resv_id; 5152 length = scsi_2btoul(cdb->length); 5153 break; 5154 } 5155 } 5156 5157 5158 /* 5159 * XXX KDM right now, we only support LUN reservation. We don't 5160 * support 3rd party reservations, or extent reservations, which 5161 * might actually need the parameter list. If we've gotten this 5162 * far, we've got a LUN reservation. Anything else got kicked out 5163 * above. So, according to SPC, ignore the length. 5164 */ 5165 length = 0; 5166 5167 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5168 && (length > 0)) { 5169 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5170 ctsio->kern_data_len = length; 5171 ctsio->kern_total_len = length; 5172 ctsio->kern_data_resid = 0; 5173 ctsio->kern_rel_offset = 0; 5174 ctsio->kern_sg_entries = 0; 5175 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5176 ctsio->be_move_done = ctl_config_move_done; 5177 ctl_datamove((union ctl_io *)ctsio); 5178 5179 return (CTL_RETVAL_COMPLETE); 5180 } 5181 5182 if (length > 0) 5183 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5184 5185 mtx_lock(&lun->lun_lock); 5186 5187 /* 5188 * According to SPC, it is not an error for an intiator to attempt 5189 * to release a reservation on a LUN that isn't reserved, or that 5190 * is reserved by another initiator. The reservation can only be 5191 * released, though, by the initiator who made it or by one of 5192 * several reset type events. 5193 */ 5194 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5195 lun->flags &= ~CTL_LUN_RESERVED; 5196 5197 mtx_unlock(&lun->lun_lock); 5198 5199 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5200 free(ctsio->kern_data_ptr, M_CTL); 5201 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5202 } 5203 5204 ctl_set_success(ctsio); 5205 ctl_done((union ctl_io *)ctsio); 5206 return (CTL_RETVAL_COMPLETE); 5207} 5208 5209int 5210ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5211{ 5212 int extent, thirdparty, longid; 5213 int resv_id, length; 5214 uint64_t thirdparty_id; 5215 struct ctl_softc *ctl_softc; 5216 struct ctl_lun *lun; 5217 uint32_t residx; 5218 5219 extent = 0; 5220 thirdparty = 0; 5221 longid = 0; 5222 resv_id = 0; 5223 length = 0; 5224 thirdparty_id = 0; 5225 5226 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5227 5228 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5229 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5230 ctl_softc = control_softc; 5231 5232 switch (ctsio->cdb[0]) { 5233 case RESERVE_10: { 5234 struct scsi_reserve_10 *cdb; 5235 5236 cdb = (struct scsi_reserve_10 *)ctsio->cdb; 5237 5238 if (cdb->byte2 & SR10_LONGID) 5239 longid = 1; 5240 else 5241 thirdparty_id = cdb->thirdparty_id; 5242 5243 resv_id = cdb->resv_id; 5244 length = scsi_2btoul(cdb->length); 5245 break; 5246 } 5247 } 5248 5249 /* 5250 * XXX KDM right now, we only support LUN reservation. We don't 5251 * support 3rd party reservations, or extent reservations, which 5252 * might actually need the parameter list. If we've gotten this 5253 * far, we've got a LUN reservation. Anything else got kicked out 5254 * above. So, according to SPC, ignore the length. 5255 */ 5256 length = 0; 5257 5258 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5259 && (length > 0)) { 5260 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5261 ctsio->kern_data_len = length; 5262 ctsio->kern_total_len = length; 5263 ctsio->kern_data_resid = 0; 5264 ctsio->kern_rel_offset = 0; 5265 ctsio->kern_sg_entries = 0; 5266 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5267 ctsio->be_move_done = ctl_config_move_done; 5268 ctl_datamove((union ctl_io *)ctsio); 5269 5270 return (CTL_RETVAL_COMPLETE); 5271 } 5272 5273 if (length > 0) 5274 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr); 5275 5276 mtx_lock(&lun->lun_lock); 5277 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5278 ctl_set_reservation_conflict(ctsio); 5279 goto bailout; 5280 } 5281 5282 lun->flags |= CTL_LUN_RESERVED; 5283 lun->res_idx = residx; 5284 5285 ctl_set_success(ctsio); 5286 5287bailout: 5288 mtx_unlock(&lun->lun_lock); 5289 5290 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5291 free(ctsio->kern_data_ptr, M_CTL); 5292 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5293 } 5294 5295 ctl_done((union ctl_io *)ctsio); 5296 return (CTL_RETVAL_COMPLETE); 5297} 5298 5299int 5300ctl_start_stop(struct ctl_scsiio *ctsio) 5301{ 5302 struct scsi_start_stop_unit *cdb; 5303 struct ctl_lun *lun; 5304 struct ctl_softc *ctl_softc; 5305 int retval; 5306 5307 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5308 5309 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5310 ctl_softc = control_softc; 5311 retval = 0; 5312 5313 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5314 5315 /* 5316 * XXX KDM 5317 * We don't support the immediate bit on a stop unit. In order to 5318 * do that, we would need to code up a way to know that a stop is 5319 * pending, and hold off any new commands until it completes, one 5320 * way or another. Then we could accept or reject those commands 5321 * depending on its status. We would almost need to do the reverse 5322 * of what we do below for an immediate start -- return the copy of 5323 * the ctl_io to the FETD with status to send to the host (and to 5324 * free the copy!) and then free the original I/O once the stop 5325 * actually completes. That way, the OOA queue mechanism can work 5326 * to block commands that shouldn't proceed. Another alternative 5327 * would be to put the copy in the queue in place of the original, 5328 * and return the original back to the caller. That could be 5329 * slightly safer.. 5330 */ 5331 if ((cdb->byte2 & SSS_IMMED) 5332 && ((cdb->how & SSS_START) == 0)) { 5333 ctl_set_invalid_field(ctsio, 5334 /*sks_valid*/ 1, 5335 /*command*/ 1, 5336 /*field*/ 1, 5337 /*bit_valid*/ 1, 5338 /*bit*/ 0); 5339 ctl_done((union ctl_io *)ctsio); 5340 return (CTL_RETVAL_COMPLETE); 5341 } 5342 5343 if ((lun->flags & CTL_LUN_PR_RESERVED) 5344 && ((cdb->how & SSS_START)==0)) { 5345 uint32_t residx; 5346 5347 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 5348 if (lun->pr_keys[residx] == 0 5349 || (lun->pr_res_idx!=residx && lun->res_type < 4)) { 5350 5351 ctl_set_reservation_conflict(ctsio); 5352 ctl_done((union ctl_io *)ctsio); 5353 return (CTL_RETVAL_COMPLETE); 5354 } 5355 } 5356 5357 /* 5358 * If there is no backend on this device, we can't start or stop 5359 * it. In theory we shouldn't get any start/stop commands in the 5360 * first place at this level if the LUN doesn't have a backend. 5361 * That should get stopped by the command decode code. 5362 */ 5363 if (lun->backend == NULL) { 5364 ctl_set_invalid_opcode(ctsio); 5365 ctl_done((union ctl_io *)ctsio); 5366 return (CTL_RETVAL_COMPLETE); 5367 } 5368 5369 /* 5370 * XXX KDM Copan-specific offline behavior. 5371 * Figure out a reasonable way to port this? 5372 */ 5373#ifdef NEEDTOPORT 5374 mtx_lock(&lun->lun_lock); 5375 5376 if (((cdb->byte2 & SSS_ONOFFLINE) == 0) 5377 && (lun->flags & CTL_LUN_OFFLINE)) { 5378 /* 5379 * If the LUN is offline, and the on/offline bit isn't set, 5380 * reject the start or stop. Otherwise, let it through. 5381 */ 5382 mtx_unlock(&lun->lun_lock); 5383 ctl_set_lun_not_ready(ctsio); 5384 ctl_done((union ctl_io *)ctsio); 5385 } else { 5386 mtx_unlock(&lun->lun_lock); 5387#endif /* NEEDTOPORT */ 5388 /* 5389 * This could be a start or a stop when we're online, 5390 * or a stop/offline or start/online. A start or stop when 5391 * we're offline is covered in the case above. 5392 */ 5393 /* 5394 * In the non-immediate case, we send the request to 5395 * the backend and return status to the user when 5396 * it is done. 5397 * 5398 * In the immediate case, we allocate a new ctl_io 5399 * to hold a copy of the request, and send that to 5400 * the backend. We then set good status on the 5401 * user's request and return it immediately. 5402 */ 5403 if (cdb->byte2 & SSS_IMMED) { 5404 union ctl_io *new_io; 5405 5406 new_io = ctl_alloc_io(ctsio->io_hdr.pool); 5407 ctl_copy_io((union ctl_io *)ctsio, new_io); 5408 retval = lun->backend->config_write(new_io); 5409 ctl_set_success(ctsio); 5410 ctl_done((union ctl_io *)ctsio); 5411 } else { 5412 retval = lun->backend->config_write( 5413 (union ctl_io *)ctsio); 5414 } 5415#ifdef NEEDTOPORT 5416 } 5417#endif 5418 return (retval); 5419} 5420 5421/* 5422 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5423 * we don't really do anything with the LBA and length fields if the user 5424 * passes them in. Instead we'll just flush out the cache for the entire 5425 * LUN. 5426 */ 5427int 5428ctl_sync_cache(struct ctl_scsiio *ctsio) 5429{ 5430 struct ctl_lun *lun; 5431 struct ctl_softc *ctl_softc; 5432 uint64_t starting_lba; 5433 uint32_t block_count; 5434 int retval; 5435 5436 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5437 5438 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5439 ctl_softc = control_softc; 5440 retval = 0; 5441 5442 switch (ctsio->cdb[0]) { 5443 case SYNCHRONIZE_CACHE: { 5444 struct scsi_sync_cache *cdb; 5445 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5446 5447 starting_lba = scsi_4btoul(cdb->begin_lba); 5448 block_count = scsi_2btoul(cdb->lb_count); 5449 break; 5450 } 5451 case SYNCHRONIZE_CACHE_16: { 5452 struct scsi_sync_cache_16 *cdb; 5453 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5454 5455 starting_lba = scsi_8btou64(cdb->begin_lba); 5456 block_count = scsi_4btoul(cdb->lb_count); 5457 break; 5458 } 5459 default: 5460 ctl_set_invalid_opcode(ctsio); 5461 ctl_done((union ctl_io *)ctsio); 5462 goto bailout; 5463 break; /* NOTREACHED */ 5464 } 5465 5466 /* 5467 * We check the LBA and length, but don't do anything with them. 5468 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5469 * get flushed. This check will just help satisfy anyone who wants 5470 * to see an error for an out of range LBA. 5471 */ 5472 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5473 ctl_set_lba_out_of_range(ctsio); 5474 ctl_done((union ctl_io *)ctsio); 5475 goto bailout; 5476 } 5477 5478 /* 5479 * If this LUN has no backend, we can't flush the cache anyway. 5480 */ 5481 if (lun->backend == NULL) { 5482 ctl_set_invalid_opcode(ctsio); 5483 ctl_done((union ctl_io *)ctsio); 5484 goto bailout; 5485 } 5486 5487 /* 5488 * Check to see whether we're configured to send the SYNCHRONIZE 5489 * CACHE command directly to the back end. 5490 */ 5491 mtx_lock(&lun->lun_lock); 5492 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC) 5493 && (++(lun->sync_count) >= lun->sync_interval)) { 5494 lun->sync_count = 0; 5495 mtx_unlock(&lun->lun_lock); 5496 retval = lun->backend->config_write((union ctl_io *)ctsio); 5497 } else { 5498 mtx_unlock(&lun->lun_lock); 5499 ctl_set_success(ctsio); 5500 ctl_done((union ctl_io *)ctsio); 5501 } 5502 5503bailout: 5504 5505 return (retval); 5506} 5507 5508int 5509ctl_format(struct ctl_scsiio *ctsio) 5510{ 5511 struct scsi_format *cdb; 5512 struct ctl_lun *lun; 5513 struct ctl_softc *ctl_softc; 5514 int length, defect_list_len; 5515 5516 CTL_DEBUG_PRINT(("ctl_format\n")); 5517 5518 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5519 ctl_softc = control_softc; 5520 5521 cdb = (struct scsi_format *)ctsio->cdb; 5522 5523 length = 0; 5524 if (cdb->byte2 & SF_FMTDATA) { 5525 if (cdb->byte2 & SF_LONGLIST) 5526 length = sizeof(struct scsi_format_header_long); 5527 else 5528 length = sizeof(struct scsi_format_header_short); 5529 } 5530 5531 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5532 && (length > 0)) { 5533 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5534 ctsio->kern_data_len = length; 5535 ctsio->kern_total_len = length; 5536 ctsio->kern_data_resid = 0; 5537 ctsio->kern_rel_offset = 0; 5538 ctsio->kern_sg_entries = 0; 5539 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5540 ctsio->be_move_done = ctl_config_move_done; 5541 ctl_datamove((union ctl_io *)ctsio); 5542 5543 return (CTL_RETVAL_COMPLETE); 5544 } 5545 5546 defect_list_len = 0; 5547 5548 if (cdb->byte2 & SF_FMTDATA) { 5549 if (cdb->byte2 & SF_LONGLIST) { 5550 struct scsi_format_header_long *header; 5551 5552 header = (struct scsi_format_header_long *) 5553 ctsio->kern_data_ptr; 5554 5555 defect_list_len = scsi_4btoul(header->defect_list_len); 5556 if (defect_list_len != 0) { 5557 ctl_set_invalid_field(ctsio, 5558 /*sks_valid*/ 1, 5559 /*command*/ 0, 5560 /*field*/ 2, 5561 /*bit_valid*/ 0, 5562 /*bit*/ 0); 5563 goto bailout; 5564 } 5565 } else { 5566 struct scsi_format_header_short *header; 5567 5568 header = (struct scsi_format_header_short *) 5569 ctsio->kern_data_ptr; 5570 5571 defect_list_len = scsi_2btoul(header->defect_list_len); 5572 if (defect_list_len != 0) { 5573 ctl_set_invalid_field(ctsio, 5574 /*sks_valid*/ 1, 5575 /*command*/ 0, 5576 /*field*/ 2, 5577 /*bit_valid*/ 0, 5578 /*bit*/ 0); 5579 goto bailout; 5580 } 5581 } 5582 } 5583 5584 /* 5585 * The format command will clear out the "Medium format corrupted" 5586 * status if set by the configuration code. That status is really 5587 * just a way to notify the host that we have lost the media, and 5588 * get them to issue a command that will basically make them think 5589 * they're blowing away the media. 5590 */ 5591 mtx_lock(&lun->lun_lock); 5592 lun->flags &= ~CTL_LUN_INOPERABLE; 5593 mtx_unlock(&lun->lun_lock); 5594 5595 ctl_set_success(ctsio); 5596bailout: 5597 5598 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5599 free(ctsio->kern_data_ptr, M_CTL); 5600 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5601 } 5602 5603 ctl_done((union ctl_io *)ctsio); 5604 return (CTL_RETVAL_COMPLETE); 5605} 5606 5607int 5608ctl_read_buffer(struct ctl_scsiio *ctsio) 5609{ 5610 struct scsi_read_buffer *cdb; 5611 struct ctl_lun *lun; 5612 int buffer_offset, len; 5613 static uint8_t descr[4]; 5614 static uint8_t echo_descr[4] = { 0 }; 5615 5616 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5617 5618 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5619 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5620 5621 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA && 5622 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5623 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5624 ctl_set_invalid_field(ctsio, 5625 /*sks_valid*/ 1, 5626 /*command*/ 1, 5627 /*field*/ 1, 5628 /*bit_valid*/ 1, 5629 /*bit*/ 4); 5630 ctl_done((union ctl_io *)ctsio); 5631 return (CTL_RETVAL_COMPLETE); 5632 } 5633 5634 len = scsi_3btoul(cdb->length); 5635 buffer_offset = scsi_3btoul(cdb->offset); 5636 5637 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5638 ctl_set_invalid_field(ctsio, 5639 /*sks_valid*/ 1, 5640 /*command*/ 1, 5641 /*field*/ 6, 5642 /*bit_valid*/ 0, 5643 /*bit*/ 0); 5644 ctl_done((union ctl_io *)ctsio); 5645 return (CTL_RETVAL_COMPLETE); 5646 } 5647 5648 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5649 descr[0] = 0; 5650 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5651 ctsio->kern_data_ptr = descr; 5652 len = min(len, sizeof(descr)); 5653 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5654 ctsio->kern_data_ptr = echo_descr; 5655 len = min(len, sizeof(echo_descr)); 5656 } else { 5657 if (lun->write_buffer == NULL) { 5658 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5659 M_CTL, M_WAITOK); 5660 } 5661 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5662 } 5663 ctsio->kern_data_len = len; 5664 ctsio->kern_total_len = len; 5665 ctsio->kern_data_resid = 0; 5666 ctsio->kern_rel_offset = 0; 5667 ctsio->kern_sg_entries = 0; 5668 ctl_set_success(ctsio); 5669 ctsio->be_move_done = ctl_config_move_done; 5670 ctl_datamove((union ctl_io *)ctsio); 5671 return (CTL_RETVAL_COMPLETE); 5672} 5673 5674int 5675ctl_write_buffer(struct ctl_scsiio *ctsio) 5676{ 5677 struct scsi_write_buffer *cdb; 5678 struct ctl_lun *lun; 5679 int buffer_offset, len; 5680 5681 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5682 5683 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5684 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5685 5686 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5687 ctl_set_invalid_field(ctsio, 5688 /*sks_valid*/ 1, 5689 /*command*/ 1, 5690 /*field*/ 1, 5691 /*bit_valid*/ 1, 5692 /*bit*/ 4); 5693 ctl_done((union ctl_io *)ctsio); 5694 return (CTL_RETVAL_COMPLETE); 5695 } 5696 5697 len = scsi_3btoul(cdb->length); 5698 buffer_offset = scsi_3btoul(cdb->offset); 5699 5700 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5701 ctl_set_invalid_field(ctsio, 5702 /*sks_valid*/ 1, 5703 /*command*/ 1, 5704 /*field*/ 6, 5705 /*bit_valid*/ 0, 5706 /*bit*/ 0); 5707 ctl_done((union ctl_io *)ctsio); 5708 return (CTL_RETVAL_COMPLETE); 5709 } 5710 5711 /* 5712 * If we've got a kernel request that hasn't been malloced yet, 5713 * malloc it and tell the caller the data buffer is here. 5714 */ 5715 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5716 if (lun->write_buffer == NULL) { 5717 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5718 M_CTL, M_WAITOK); 5719 } 5720 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5721 ctsio->kern_data_len = len; 5722 ctsio->kern_total_len = len; 5723 ctsio->kern_data_resid = 0; 5724 ctsio->kern_rel_offset = 0; 5725 ctsio->kern_sg_entries = 0; 5726 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5727 ctsio->be_move_done = ctl_config_move_done; 5728 ctl_datamove((union ctl_io *)ctsio); 5729 5730 return (CTL_RETVAL_COMPLETE); 5731 } 5732 5733 ctl_set_success(ctsio); 5734 ctl_done((union ctl_io *)ctsio); 5735 return (CTL_RETVAL_COMPLETE); 5736} 5737 5738int 5739ctl_write_same(struct ctl_scsiio *ctsio) 5740{ 5741 struct ctl_lun *lun; 5742 struct ctl_lba_len_flags *lbalen; 5743 uint64_t lba; 5744 uint32_t num_blocks; 5745 int len, retval; 5746 uint8_t byte2; 5747 5748 retval = CTL_RETVAL_COMPLETE; 5749 5750 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5751 5752 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5753 5754 switch (ctsio->cdb[0]) { 5755 case WRITE_SAME_10: { 5756 struct scsi_write_same_10 *cdb; 5757 5758 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5759 5760 lba = scsi_4btoul(cdb->addr); 5761 num_blocks = scsi_2btoul(cdb->length); 5762 byte2 = cdb->byte2; 5763 break; 5764 } 5765 case WRITE_SAME_16: { 5766 struct scsi_write_same_16 *cdb; 5767 5768 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5769 5770 lba = scsi_8btou64(cdb->addr); 5771 num_blocks = scsi_4btoul(cdb->length); 5772 byte2 = cdb->byte2; 5773 break; 5774 } 5775 default: 5776 /* 5777 * We got a command we don't support. This shouldn't 5778 * happen, commands should be filtered out above us. 5779 */ 5780 ctl_set_invalid_opcode(ctsio); 5781 ctl_done((union ctl_io *)ctsio); 5782 5783 return (CTL_RETVAL_COMPLETE); 5784 break; /* NOTREACHED */ 5785 } 5786 5787 /* NDOB and ANCHOR flags can be used only together with UNMAP */ 5788 if ((byte2 & SWS_UNMAP) == 0 && 5789 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) { 5790 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5791 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5792 ctl_done((union ctl_io *)ctsio); 5793 return (CTL_RETVAL_COMPLETE); 5794 } 5795 5796 /* 5797 * The first check is to make sure we're in bounds, the second 5798 * check is to catch wrap-around problems. If the lba + num blocks 5799 * is less than the lba, then we've wrapped around and the block 5800 * range is invalid anyway. 5801 */ 5802 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5803 || ((lba + num_blocks) < lba)) { 5804 ctl_set_lba_out_of_range(ctsio); 5805 ctl_done((union ctl_io *)ctsio); 5806 return (CTL_RETVAL_COMPLETE); 5807 } 5808 5809 /* Zero number of blocks means "to the last logical block" */ 5810 if (num_blocks == 0) { 5811 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5812 ctl_set_invalid_field(ctsio, 5813 /*sks_valid*/ 0, 5814 /*command*/ 1, 5815 /*field*/ 0, 5816 /*bit_valid*/ 0, 5817 /*bit*/ 0); 5818 ctl_done((union ctl_io *)ctsio); 5819 return (CTL_RETVAL_COMPLETE); 5820 } 5821 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5822 } 5823 5824 len = lun->be_lun->blocksize; 5825 5826 /* 5827 * If we've got a kernel request that hasn't been malloced yet, 5828 * malloc it and tell the caller the data buffer is here. 5829 */ 5830 if ((byte2 & SWS_NDOB) == 0 && 5831 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5832 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5833 ctsio->kern_data_len = len; 5834 ctsio->kern_total_len = len; 5835 ctsio->kern_data_resid = 0; 5836 ctsio->kern_rel_offset = 0; 5837 ctsio->kern_sg_entries = 0; 5838 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5839 ctsio->be_move_done = ctl_config_move_done; 5840 ctl_datamove((union ctl_io *)ctsio); 5841 5842 return (CTL_RETVAL_COMPLETE); 5843 } 5844 5845 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5846 lbalen->lba = lba; 5847 lbalen->len = num_blocks; 5848 lbalen->flags = byte2; 5849 retval = lun->backend->config_write((union ctl_io *)ctsio); 5850 5851 return (retval); 5852} 5853 5854int 5855ctl_unmap(struct ctl_scsiio *ctsio) 5856{ 5857 struct ctl_lun *lun; 5858 struct scsi_unmap *cdb; 5859 struct ctl_ptr_len_flags *ptrlen; 5860 struct scsi_unmap_header *hdr; 5861 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5862 uint64_t lba; 5863 uint32_t num_blocks; 5864 int len, retval; 5865 uint8_t byte2; 5866 5867 retval = CTL_RETVAL_COMPLETE; 5868 5869 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5870 5871 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5872 cdb = (struct scsi_unmap *)ctsio->cdb; 5873 5874 len = scsi_2btoul(cdb->length); 5875 byte2 = cdb->byte2; 5876 5877 /* 5878 * If we've got a kernel request that hasn't been malloced yet, 5879 * malloc it and tell the caller the data buffer is here. 5880 */ 5881 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5882 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5883 ctsio->kern_data_len = len; 5884 ctsio->kern_total_len = len; 5885 ctsio->kern_data_resid = 0; 5886 ctsio->kern_rel_offset = 0; 5887 ctsio->kern_sg_entries = 0; 5888 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5889 ctsio->be_move_done = ctl_config_move_done; 5890 ctl_datamove((union ctl_io *)ctsio); 5891 5892 return (CTL_RETVAL_COMPLETE); 5893 } 5894 5895 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5896 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5897 if (len < sizeof (*hdr) || 5898 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5899 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5900 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5901 ctl_set_invalid_field(ctsio, 5902 /*sks_valid*/ 0, 5903 /*command*/ 0, 5904 /*field*/ 0, 5905 /*bit_valid*/ 0, 5906 /*bit*/ 0); 5907 ctl_done((union ctl_io *)ctsio); 5908 return (CTL_RETVAL_COMPLETE); 5909 } 5910 len = scsi_2btoul(hdr->desc_length); 5911 buf = (struct scsi_unmap_desc *)(hdr + 1); 5912 end = buf + len / sizeof(*buf); 5913 5914 endnz = buf; 5915 for (range = buf; range < end; range++) { 5916 lba = scsi_8btou64(range->lba); 5917 num_blocks = scsi_4btoul(range->length); 5918 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5919 || ((lba + num_blocks) < lba)) { 5920 ctl_set_lba_out_of_range(ctsio); 5921 ctl_done((union ctl_io *)ctsio); 5922 return (CTL_RETVAL_COMPLETE); 5923 } 5924 if (num_blocks != 0) 5925 endnz = range + 1; 5926 } 5927 5928 /* 5929 * Block backend can not handle zero last range. 5930 * Filter it out and return if there is nothing left. 5931 */ 5932 len = (uint8_t *)endnz - (uint8_t *)buf; 5933 if (len == 0) { 5934 ctl_set_success(ctsio); 5935 ctl_done((union ctl_io *)ctsio); 5936 return (CTL_RETVAL_COMPLETE); 5937 } 5938 5939 mtx_lock(&lun->lun_lock); 5940 ptrlen = (struct ctl_ptr_len_flags *) 5941 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5942 ptrlen->ptr = (void *)buf; 5943 ptrlen->len = len; 5944 ptrlen->flags = byte2; 5945 ctl_check_blocked(lun); 5946 mtx_unlock(&lun->lun_lock); 5947 5948 retval = lun->backend->config_write((union ctl_io *)ctsio); 5949 return (retval); 5950} 5951 5952/* 5953 * Note that this function currently doesn't actually do anything inside 5954 * CTL to enforce things if the DQue bit is turned on. 5955 * 5956 * Also note that this function can't be used in the default case, because 5957 * the DQue bit isn't set in the changeable mask for the control mode page 5958 * anyway. This is just here as an example for how to implement a page 5959 * handler, and a placeholder in case we want to allow the user to turn 5960 * tagged queueing on and off. 5961 * 5962 * The D_SENSE bit handling is functional, however, and will turn 5963 * descriptor sense on and off for a given LUN. 5964 */ 5965int 5966ctl_control_page_handler(struct ctl_scsiio *ctsio, 5967 struct ctl_page_index *page_index, uint8_t *page_ptr) 5968{ 5969 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5970 struct ctl_lun *lun; 5971 struct ctl_softc *softc; 5972 int set_ua; 5973 uint32_t initidx; 5974 5975 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5976 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5977 set_ua = 0; 5978 5979 user_cp = (struct scsi_control_page *)page_ptr; 5980 current_cp = (struct scsi_control_page *) 5981 (page_index->page_data + (page_index->page_len * 5982 CTL_PAGE_CURRENT)); 5983 saved_cp = (struct scsi_control_page *) 5984 (page_index->page_data + (page_index->page_len * 5985 CTL_PAGE_SAVED)); 5986 5987 softc = control_softc; 5988 5989 mtx_lock(&lun->lun_lock); 5990 if (((current_cp->rlec & SCP_DSENSE) == 0) 5991 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5992 /* 5993 * Descriptor sense is currently turned off and the user 5994 * wants to turn it on. 5995 */ 5996 current_cp->rlec |= SCP_DSENSE; 5997 saved_cp->rlec |= SCP_DSENSE; 5998 lun->flags |= CTL_LUN_SENSE_DESC; 5999 set_ua = 1; 6000 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 6001 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 6002 /* 6003 * Descriptor sense is currently turned on, and the user 6004 * wants to turn it off. 6005 */ 6006 current_cp->rlec &= ~SCP_DSENSE; 6007 saved_cp->rlec &= ~SCP_DSENSE; 6008 lun->flags &= ~CTL_LUN_SENSE_DESC; 6009 set_ua = 1; 6010 } 6011 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 6012 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 6013 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6014 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6015 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 6016 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 6017 set_ua = 1; 6018 } 6019 if ((current_cp->eca_and_aen & SCP_SWP) != 6020 (user_cp->eca_and_aen & SCP_SWP)) { 6021 current_cp->eca_and_aen &= ~SCP_SWP; 6022 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6023 saved_cp->eca_and_aen &= ~SCP_SWP; 6024 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 6025 set_ua = 1; 6026 } 6027 if (set_ua != 0) { 6028 int i; 6029 /* 6030 * Let other initiators know that the mode 6031 * parameters for this LUN have changed. 6032 */ 6033 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6034 if (i == initidx) 6035 continue; 6036 6037 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6038 } 6039 } 6040 mtx_unlock(&lun->lun_lock); 6041 6042 return (0); 6043} 6044 6045int 6046ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 6047 struct ctl_page_index *page_index, uint8_t *page_ptr) 6048{ 6049 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 6050 struct ctl_lun *lun; 6051 int set_ua; 6052 uint32_t initidx; 6053 6054 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6055 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 6056 set_ua = 0; 6057 6058 user_cp = (struct scsi_caching_page *)page_ptr; 6059 current_cp = (struct scsi_caching_page *) 6060 (page_index->page_data + (page_index->page_len * 6061 CTL_PAGE_CURRENT)); 6062 saved_cp = (struct scsi_caching_page *) 6063 (page_index->page_data + (page_index->page_len * 6064 CTL_PAGE_SAVED)); 6065 6066 mtx_lock(&lun->lun_lock); 6067 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 6068 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 6069 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6070 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6071 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 6072 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 6073 set_ua = 1; 6074 } 6075 if (set_ua != 0) { 6076 int i; 6077 /* 6078 * Let other initiators know that the mode 6079 * parameters for this LUN have changed. 6080 */ 6081 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 6082 if (i == initidx) 6083 continue; 6084 6085 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE; 6086 } 6087 } 6088 mtx_unlock(&lun->lun_lock); 6089 6090 return (0); 6091} 6092 6093int 6094ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 6095 struct ctl_page_index *page_index, 6096 uint8_t *page_ptr) 6097{ 6098 uint8_t *c; 6099 int i; 6100 6101 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 6102 ctl_time_io_secs = 6103 (c[0] << 8) | 6104 (c[1] << 0) | 6105 0; 6106 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 6107 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 6108 printf("page data:"); 6109 for (i=0; i<8; i++) 6110 printf(" %.2x",page_ptr[i]); 6111 printf("\n"); 6112 return (0); 6113} 6114 6115int 6116ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 6117 struct ctl_page_index *page_index, 6118 int pc) 6119{ 6120 struct copan_debugconf_subpage *page; 6121 6122 page = (struct copan_debugconf_subpage *)page_index->page_data + 6123 (page_index->page_len * pc); 6124 6125 switch (pc) { 6126 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 6127 case SMS_PAGE_CTRL_DEFAULT >> 6: 6128 case SMS_PAGE_CTRL_SAVED >> 6: 6129 /* 6130 * We don't update the changable or default bits for this page. 6131 */ 6132 break; 6133 case SMS_PAGE_CTRL_CURRENT >> 6: 6134 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 6135 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 6136 break; 6137 default: 6138#ifdef NEEDTOPORT 6139 EPRINT(0, "Invalid PC %d!!", pc); 6140#endif /* NEEDTOPORT */ 6141 break; 6142 } 6143 return (0); 6144} 6145 6146 6147static int 6148ctl_do_mode_select(union ctl_io *io) 6149{ 6150 struct scsi_mode_page_header *page_header; 6151 struct ctl_page_index *page_index; 6152 struct ctl_scsiio *ctsio; 6153 int control_dev, page_len; 6154 int page_len_offset, page_len_size; 6155 union ctl_modepage_info *modepage_info; 6156 struct ctl_lun *lun; 6157 int *len_left, *len_used; 6158 int retval, i; 6159 6160 ctsio = &io->scsiio; 6161 page_index = NULL; 6162 page_len = 0; 6163 retval = CTL_RETVAL_COMPLETE; 6164 6165 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6166 6167 if (lun->be_lun->lun_type != T_DIRECT) 6168 control_dev = 1; 6169 else 6170 control_dev = 0; 6171 6172 modepage_info = (union ctl_modepage_info *) 6173 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6174 len_left = &modepage_info->header.len_left; 6175 len_used = &modepage_info->header.len_used; 6176 6177do_next_page: 6178 6179 page_header = (struct scsi_mode_page_header *) 6180 (ctsio->kern_data_ptr + *len_used); 6181 6182 if (*len_left == 0) { 6183 free(ctsio->kern_data_ptr, M_CTL); 6184 ctl_set_success(ctsio); 6185 ctl_done((union ctl_io *)ctsio); 6186 return (CTL_RETVAL_COMPLETE); 6187 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 6188 6189 free(ctsio->kern_data_ptr, M_CTL); 6190 ctl_set_param_len_error(ctsio); 6191 ctl_done((union ctl_io *)ctsio); 6192 return (CTL_RETVAL_COMPLETE); 6193 6194 } else if ((page_header->page_code & SMPH_SPF) 6195 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 6196 6197 free(ctsio->kern_data_ptr, M_CTL); 6198 ctl_set_param_len_error(ctsio); 6199 ctl_done((union ctl_io *)ctsio); 6200 return (CTL_RETVAL_COMPLETE); 6201 } 6202 6203 6204 /* 6205 * XXX KDM should we do something with the block descriptor? 6206 */ 6207 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6208 6209 if ((control_dev != 0) 6210 && (lun->mode_pages.index[i].page_flags & 6211 CTL_PAGE_FLAG_DISK_ONLY)) 6212 continue; 6213 6214 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) != 6215 (page_header->page_code & SMPH_PC_MASK)) 6216 continue; 6217 6218 /* 6219 * If neither page has a subpage code, then we've got a 6220 * match. 6221 */ 6222 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0) 6223 && ((page_header->page_code & SMPH_SPF) == 0)) { 6224 page_index = &lun->mode_pages.index[i]; 6225 page_len = page_header->page_length; 6226 break; 6227 } 6228 6229 /* 6230 * If both pages have subpages, then the subpage numbers 6231 * have to match. 6232 */ 6233 if ((lun->mode_pages.index[i].page_code & SMPH_SPF) 6234 && (page_header->page_code & SMPH_SPF)) { 6235 struct scsi_mode_page_header_sp *sph; 6236 6237 sph = (struct scsi_mode_page_header_sp *)page_header; 6238 6239 if (lun->mode_pages.index[i].subpage == 6240 sph->subpage) { 6241 page_index = &lun->mode_pages.index[i]; 6242 page_len = scsi_2btoul(sph->page_length); 6243 break; 6244 } 6245 } 6246 } 6247 6248 /* 6249 * If we couldn't find the page, or if we don't have a mode select 6250 * handler for it, send back an error to the user. 6251 */ 6252 if ((page_index == NULL) 6253 || (page_index->select_handler == NULL)) { 6254 ctl_set_invalid_field(ctsio, 6255 /*sks_valid*/ 1, 6256 /*command*/ 0, 6257 /*field*/ *len_used, 6258 /*bit_valid*/ 0, 6259 /*bit*/ 0); 6260 free(ctsio->kern_data_ptr, M_CTL); 6261 ctl_done((union ctl_io *)ctsio); 6262 return (CTL_RETVAL_COMPLETE); 6263 } 6264 6265 if (page_index->page_code & SMPH_SPF) { 6266 page_len_offset = 2; 6267 page_len_size = 2; 6268 } else { 6269 page_len_size = 1; 6270 page_len_offset = 1; 6271 } 6272 6273 /* 6274 * If the length the initiator gives us isn't the one we specify in 6275 * the mode page header, or if they didn't specify enough data in 6276 * the CDB to avoid truncating this page, kick out the request. 6277 */ 6278 if ((page_len != (page_index->page_len - page_len_offset - 6279 page_len_size)) 6280 || (*len_left < page_index->page_len)) { 6281 6282 6283 ctl_set_invalid_field(ctsio, 6284 /*sks_valid*/ 1, 6285 /*command*/ 0, 6286 /*field*/ *len_used + page_len_offset, 6287 /*bit_valid*/ 0, 6288 /*bit*/ 0); 6289 free(ctsio->kern_data_ptr, M_CTL); 6290 ctl_done((union ctl_io *)ctsio); 6291 return (CTL_RETVAL_COMPLETE); 6292 } 6293 6294 /* 6295 * Run through the mode page, checking to make sure that the bits 6296 * the user changed are actually legal for him to change. 6297 */ 6298 for (i = 0; i < page_index->page_len; i++) { 6299 uint8_t *user_byte, *change_mask, *current_byte; 6300 int bad_bit; 6301 int j; 6302 6303 user_byte = (uint8_t *)page_header + i; 6304 change_mask = page_index->page_data + 6305 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6306 current_byte = page_index->page_data + 6307 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6308 6309 /* 6310 * Check to see whether the user set any bits in this byte 6311 * that he is not allowed to set. 6312 */ 6313 if ((*user_byte & ~(*change_mask)) == 6314 (*current_byte & ~(*change_mask))) 6315 continue; 6316 6317 /* 6318 * Go through bit by bit to determine which one is illegal. 6319 */ 6320 bad_bit = 0; 6321 for (j = 7; j >= 0; j--) { 6322 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6323 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6324 bad_bit = i; 6325 break; 6326 } 6327 } 6328 ctl_set_invalid_field(ctsio, 6329 /*sks_valid*/ 1, 6330 /*command*/ 0, 6331 /*field*/ *len_used + i, 6332 /*bit_valid*/ 1, 6333 /*bit*/ bad_bit); 6334 free(ctsio->kern_data_ptr, M_CTL); 6335 ctl_done((union ctl_io *)ctsio); 6336 return (CTL_RETVAL_COMPLETE); 6337 } 6338 6339 /* 6340 * Decrement these before we call the page handler, since we may 6341 * end up getting called back one way or another before the handler 6342 * returns to this context. 6343 */ 6344 *len_left -= page_index->page_len; 6345 *len_used += page_index->page_len; 6346 6347 retval = page_index->select_handler(ctsio, page_index, 6348 (uint8_t *)page_header); 6349 6350 /* 6351 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6352 * wait until this queued command completes to finish processing 6353 * the mode page. If it returns anything other than 6354 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6355 * already set the sense information, freed the data pointer, and 6356 * completed the io for us. 6357 */ 6358 if (retval != CTL_RETVAL_COMPLETE) 6359 goto bailout_no_done; 6360 6361 /* 6362 * If the initiator sent us more than one page, parse the next one. 6363 */ 6364 if (*len_left > 0) 6365 goto do_next_page; 6366 6367 ctl_set_success(ctsio); 6368 free(ctsio->kern_data_ptr, M_CTL); 6369 ctl_done((union ctl_io *)ctsio); 6370 6371bailout_no_done: 6372 6373 return (CTL_RETVAL_COMPLETE); 6374 6375} 6376 6377int 6378ctl_mode_select(struct ctl_scsiio *ctsio) 6379{ 6380 int param_len, pf, sp; 6381 int header_size, bd_len; 6382 int len_left, len_used; 6383 struct ctl_page_index *page_index; 6384 struct ctl_lun *lun; 6385 int control_dev, page_len; 6386 union ctl_modepage_info *modepage_info; 6387 int retval; 6388 6389 pf = 0; 6390 sp = 0; 6391 page_len = 0; 6392 len_used = 0; 6393 len_left = 0; 6394 retval = 0; 6395 bd_len = 0; 6396 page_index = NULL; 6397 6398 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6399 6400 if (lun->be_lun->lun_type != T_DIRECT) 6401 control_dev = 1; 6402 else 6403 control_dev = 0; 6404 6405 switch (ctsio->cdb[0]) { 6406 case MODE_SELECT_6: { 6407 struct scsi_mode_select_6 *cdb; 6408 6409 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6410 6411 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6412 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6413 6414 param_len = cdb->length; 6415 header_size = sizeof(struct scsi_mode_header_6); 6416 break; 6417 } 6418 case MODE_SELECT_10: { 6419 struct scsi_mode_select_10 *cdb; 6420 6421 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6422 6423 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6424 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6425 6426 param_len = scsi_2btoul(cdb->length); 6427 header_size = sizeof(struct scsi_mode_header_10); 6428 break; 6429 } 6430 default: 6431 ctl_set_invalid_opcode(ctsio); 6432 ctl_done((union ctl_io *)ctsio); 6433 return (CTL_RETVAL_COMPLETE); 6434 break; /* NOTREACHED */ 6435 } 6436 6437 /* 6438 * From SPC-3: 6439 * "A parameter list length of zero indicates that the Data-Out Buffer 6440 * shall be empty. This condition shall not be considered as an error." 6441 */ 6442 if (param_len == 0) { 6443 ctl_set_success(ctsio); 6444 ctl_done((union ctl_io *)ctsio); 6445 return (CTL_RETVAL_COMPLETE); 6446 } 6447 6448 /* 6449 * Since we'll hit this the first time through, prior to 6450 * allocation, we don't need to free a data buffer here. 6451 */ 6452 if (param_len < header_size) { 6453 ctl_set_param_len_error(ctsio); 6454 ctl_done((union ctl_io *)ctsio); 6455 return (CTL_RETVAL_COMPLETE); 6456 } 6457 6458 /* 6459 * Allocate the data buffer and grab the user's data. In theory, 6460 * we shouldn't have to sanity check the parameter list length here 6461 * because the maximum size is 64K. We should be able to malloc 6462 * that much without too many problems. 6463 */ 6464 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6465 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6466 ctsio->kern_data_len = param_len; 6467 ctsio->kern_total_len = param_len; 6468 ctsio->kern_data_resid = 0; 6469 ctsio->kern_rel_offset = 0; 6470 ctsio->kern_sg_entries = 0; 6471 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6472 ctsio->be_move_done = ctl_config_move_done; 6473 ctl_datamove((union ctl_io *)ctsio); 6474 6475 return (CTL_RETVAL_COMPLETE); 6476 } 6477 6478 switch (ctsio->cdb[0]) { 6479 case MODE_SELECT_6: { 6480 struct scsi_mode_header_6 *mh6; 6481 6482 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6483 bd_len = mh6->blk_desc_len; 6484 break; 6485 } 6486 case MODE_SELECT_10: { 6487 struct scsi_mode_header_10 *mh10; 6488 6489 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6490 bd_len = scsi_2btoul(mh10->blk_desc_len); 6491 break; 6492 } 6493 default: 6494 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6495 break; 6496 } 6497 6498 if (param_len < (header_size + bd_len)) { 6499 free(ctsio->kern_data_ptr, M_CTL); 6500 ctl_set_param_len_error(ctsio); 6501 ctl_done((union ctl_io *)ctsio); 6502 return (CTL_RETVAL_COMPLETE); 6503 } 6504 6505 /* 6506 * Set the IO_CONT flag, so that if this I/O gets passed to 6507 * ctl_config_write_done(), it'll get passed back to 6508 * ctl_do_mode_select() for further processing, or completion if 6509 * we're all done. 6510 */ 6511 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6512 ctsio->io_cont = ctl_do_mode_select; 6513 6514 modepage_info = (union ctl_modepage_info *) 6515 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6516 6517 memset(modepage_info, 0, sizeof(*modepage_info)); 6518 6519 len_left = param_len - header_size - bd_len; 6520 len_used = header_size + bd_len; 6521 6522 modepage_info->header.len_left = len_left; 6523 modepage_info->header.len_used = len_used; 6524 6525 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6526} 6527 6528int 6529ctl_mode_sense(struct ctl_scsiio *ctsio) 6530{ 6531 struct ctl_lun *lun; 6532 int pc, page_code, dbd, llba, subpage; 6533 int alloc_len, page_len, header_len, total_len; 6534 struct scsi_mode_block_descr *block_desc; 6535 struct ctl_page_index *page_index; 6536 int control_dev; 6537 6538 dbd = 0; 6539 llba = 0; 6540 block_desc = NULL; 6541 page_index = NULL; 6542 6543 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6544 6545 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6546 6547 if (lun->be_lun->lun_type != T_DIRECT) 6548 control_dev = 1; 6549 else 6550 control_dev = 0; 6551 6552 switch (ctsio->cdb[0]) { 6553 case MODE_SENSE_6: { 6554 struct scsi_mode_sense_6 *cdb; 6555 6556 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6557 6558 header_len = sizeof(struct scsi_mode_hdr_6); 6559 if (cdb->byte2 & SMS_DBD) 6560 dbd = 1; 6561 else 6562 header_len += sizeof(struct scsi_mode_block_descr); 6563 6564 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6565 page_code = cdb->page & SMS_PAGE_CODE; 6566 subpage = cdb->subpage; 6567 alloc_len = cdb->length; 6568 break; 6569 } 6570 case MODE_SENSE_10: { 6571 struct scsi_mode_sense_10 *cdb; 6572 6573 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6574 6575 header_len = sizeof(struct scsi_mode_hdr_10); 6576 6577 if (cdb->byte2 & SMS_DBD) 6578 dbd = 1; 6579 else 6580 header_len += sizeof(struct scsi_mode_block_descr); 6581 if (cdb->byte2 & SMS10_LLBAA) 6582 llba = 1; 6583 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6584 page_code = cdb->page & SMS_PAGE_CODE; 6585 subpage = cdb->subpage; 6586 alloc_len = scsi_2btoul(cdb->length); 6587 break; 6588 } 6589 default: 6590 ctl_set_invalid_opcode(ctsio); 6591 ctl_done((union ctl_io *)ctsio); 6592 return (CTL_RETVAL_COMPLETE); 6593 break; /* NOTREACHED */ 6594 } 6595 6596 /* 6597 * We have to make a first pass through to calculate the size of 6598 * the pages that match the user's query. Then we allocate enough 6599 * memory to hold it, and actually copy the data into the buffer. 6600 */ 6601 switch (page_code) { 6602 case SMS_ALL_PAGES_PAGE: { 6603 int i; 6604 6605 page_len = 0; 6606 6607 /* 6608 * At the moment, values other than 0 and 0xff here are 6609 * reserved according to SPC-3. 6610 */ 6611 if ((subpage != SMS_SUBPAGE_PAGE_0) 6612 && (subpage != SMS_SUBPAGE_ALL)) { 6613 ctl_set_invalid_field(ctsio, 6614 /*sks_valid*/ 1, 6615 /*command*/ 1, 6616 /*field*/ 3, 6617 /*bit_valid*/ 0, 6618 /*bit*/ 0); 6619 ctl_done((union ctl_io *)ctsio); 6620 return (CTL_RETVAL_COMPLETE); 6621 } 6622 6623 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6624 if ((control_dev != 0) 6625 && (lun->mode_pages.index[i].page_flags & 6626 CTL_PAGE_FLAG_DISK_ONLY)) 6627 continue; 6628 6629 /* 6630 * We don't use this subpage if the user didn't 6631 * request all subpages. 6632 */ 6633 if ((lun->mode_pages.index[i].subpage != 0) 6634 && (subpage == SMS_SUBPAGE_PAGE_0)) 6635 continue; 6636 6637#if 0 6638 printf("found page %#x len %d\n", 6639 lun->mode_pages.index[i].page_code & 6640 SMPH_PC_MASK, 6641 lun->mode_pages.index[i].page_len); 6642#endif 6643 page_len += lun->mode_pages.index[i].page_len; 6644 } 6645 break; 6646 } 6647 default: { 6648 int i; 6649 6650 page_len = 0; 6651 6652 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6653 /* Look for the right page code */ 6654 if ((lun->mode_pages.index[i].page_code & 6655 SMPH_PC_MASK) != page_code) 6656 continue; 6657 6658 /* Look for the right subpage or the subpage wildcard*/ 6659 if ((lun->mode_pages.index[i].subpage != subpage) 6660 && (subpage != SMS_SUBPAGE_ALL)) 6661 continue; 6662 6663 /* Make sure the page is supported for this dev type */ 6664 if ((control_dev != 0) 6665 && (lun->mode_pages.index[i].page_flags & 6666 CTL_PAGE_FLAG_DISK_ONLY)) 6667 continue; 6668 6669#if 0 6670 printf("found page %#x len %d\n", 6671 lun->mode_pages.index[i].page_code & 6672 SMPH_PC_MASK, 6673 lun->mode_pages.index[i].page_len); 6674#endif 6675 6676 page_len += lun->mode_pages.index[i].page_len; 6677 } 6678 6679 if (page_len == 0) { 6680 ctl_set_invalid_field(ctsio, 6681 /*sks_valid*/ 1, 6682 /*command*/ 1, 6683 /*field*/ 2, 6684 /*bit_valid*/ 1, 6685 /*bit*/ 5); 6686 ctl_done((union ctl_io *)ctsio); 6687 return (CTL_RETVAL_COMPLETE); 6688 } 6689 break; 6690 } 6691 } 6692 6693 total_len = header_len + page_len; 6694#if 0 6695 printf("header_len = %d, page_len = %d, total_len = %d\n", 6696 header_len, page_len, total_len); 6697#endif 6698 6699 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6700 ctsio->kern_sg_entries = 0; 6701 ctsio->kern_data_resid = 0; 6702 ctsio->kern_rel_offset = 0; 6703 if (total_len < alloc_len) { 6704 ctsio->residual = alloc_len - total_len; 6705 ctsio->kern_data_len = total_len; 6706 ctsio->kern_total_len = total_len; 6707 } else { 6708 ctsio->residual = 0; 6709 ctsio->kern_data_len = alloc_len; 6710 ctsio->kern_total_len = alloc_len; 6711 } 6712 6713 switch (ctsio->cdb[0]) { 6714 case MODE_SENSE_6: { 6715 struct scsi_mode_hdr_6 *header; 6716 6717 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6718 6719 header->datalen = ctl_min(total_len - 1, 254); 6720 if (control_dev == 0) { 6721 header->dev_specific = 0x10; /* DPOFUA */ 6722 if ((lun->flags & CTL_LUN_READONLY) || 6723 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6724 .eca_and_aen & SCP_SWP) != 0) 6725 header->dev_specific |= 0x80; /* WP */ 6726 } 6727 if (dbd) 6728 header->block_descr_len = 0; 6729 else 6730 header->block_descr_len = 6731 sizeof(struct scsi_mode_block_descr); 6732 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6733 break; 6734 } 6735 case MODE_SENSE_10: { 6736 struct scsi_mode_hdr_10 *header; 6737 int datalen; 6738 6739 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6740 6741 datalen = ctl_min(total_len - 2, 65533); 6742 scsi_ulto2b(datalen, header->datalen); 6743 if (control_dev == 0) { 6744 header->dev_specific = 0x10; /* DPOFUA */ 6745 if ((lun->flags & CTL_LUN_READONLY) || 6746 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6747 .eca_and_aen & SCP_SWP) != 0) 6748 header->dev_specific |= 0x80; /* WP */ 6749 } 6750 if (dbd) 6751 scsi_ulto2b(0, header->block_descr_len); 6752 else 6753 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6754 header->block_descr_len); 6755 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6756 break; 6757 } 6758 default: 6759 panic("invalid CDB type %#x", ctsio->cdb[0]); 6760 break; /* NOTREACHED */ 6761 } 6762 6763 /* 6764 * If we've got a disk, use its blocksize in the block 6765 * descriptor. Otherwise, just set it to 0. 6766 */ 6767 if (dbd == 0) { 6768 if (control_dev == 0) 6769 scsi_ulto3b(lun->be_lun->blocksize, 6770 block_desc->block_len); 6771 else 6772 scsi_ulto3b(0, block_desc->block_len); 6773 } 6774 6775 switch (page_code) { 6776 case SMS_ALL_PAGES_PAGE: { 6777 int i, data_used; 6778 6779 data_used = header_len; 6780 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6781 struct ctl_page_index *page_index; 6782 6783 page_index = &lun->mode_pages.index[i]; 6784 6785 if ((control_dev != 0) 6786 && (page_index->page_flags & 6787 CTL_PAGE_FLAG_DISK_ONLY)) 6788 continue; 6789 6790 /* 6791 * We don't use this subpage if the user didn't 6792 * request all subpages. We already checked (above) 6793 * to make sure the user only specified a subpage 6794 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6795 */ 6796 if ((page_index->subpage != 0) 6797 && (subpage == SMS_SUBPAGE_PAGE_0)) 6798 continue; 6799 6800 /* 6801 * Call the handler, if it exists, to update the 6802 * page to the latest values. 6803 */ 6804 if (page_index->sense_handler != NULL) 6805 page_index->sense_handler(ctsio, page_index,pc); 6806 6807 memcpy(ctsio->kern_data_ptr + data_used, 6808 page_index->page_data + 6809 (page_index->page_len * pc), 6810 page_index->page_len); 6811 data_used += page_index->page_len; 6812 } 6813 break; 6814 } 6815 default: { 6816 int i, data_used; 6817 6818 data_used = header_len; 6819 6820 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6821 struct ctl_page_index *page_index; 6822 6823 page_index = &lun->mode_pages.index[i]; 6824 6825 /* Look for the right page code */ 6826 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6827 continue; 6828 6829 /* Look for the right subpage or the subpage wildcard*/ 6830 if ((page_index->subpage != subpage) 6831 && (subpage != SMS_SUBPAGE_ALL)) 6832 continue; 6833 6834 /* Make sure the page is supported for this dev type */ 6835 if ((control_dev != 0) 6836 && (page_index->page_flags & 6837 CTL_PAGE_FLAG_DISK_ONLY)) 6838 continue; 6839 6840 /* 6841 * Call the handler, if it exists, to update the 6842 * page to the latest values. 6843 */ 6844 if (page_index->sense_handler != NULL) 6845 page_index->sense_handler(ctsio, page_index,pc); 6846 6847 memcpy(ctsio->kern_data_ptr + data_used, 6848 page_index->page_data + 6849 (page_index->page_len * pc), 6850 page_index->page_len); 6851 data_used += page_index->page_len; 6852 } 6853 break; 6854 } 6855 } 6856 6857 ctl_set_success(ctsio); 6858 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6859 ctsio->be_move_done = ctl_config_move_done; 6860 ctl_datamove((union ctl_io *)ctsio); 6861 return (CTL_RETVAL_COMPLETE); 6862} 6863 6864int 6865ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6866 struct ctl_page_index *page_index, 6867 int pc) 6868{ 6869 struct ctl_lun *lun; 6870 struct scsi_log_param_header *phdr; 6871 uint8_t *data; 6872 uint64_t val; 6873 6874 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6875 data = page_index->page_data; 6876 6877 if (lun->backend->lun_attr != NULL && 6878 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6879 != UINT64_MAX) { 6880 phdr = (struct scsi_log_param_header *)data; 6881 scsi_ulto2b(0x0001, phdr->param_code); 6882 phdr->param_control = SLP_LBIN | SLP_LP; 6883 phdr->param_len = 8; 6884 data = (uint8_t *)(phdr + 1); 6885 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6886 data[4] = 0x01; /* per-LUN */ 6887 data += phdr->param_len; 6888 } 6889 6890 if (lun->backend->lun_attr != NULL && 6891 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6892 != UINT64_MAX) { 6893 phdr = (struct scsi_log_param_header *)data; 6894 scsi_ulto2b(0x0002, phdr->param_code); 6895 phdr->param_control = SLP_LBIN | SLP_LP; 6896 phdr->param_len = 8; 6897 data = (uint8_t *)(phdr + 1); 6898 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6899 data[4] = 0x02; /* per-pool */ 6900 data += phdr->param_len; 6901 } 6902 6903 if (lun->backend->lun_attr != NULL && 6904 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6905 != UINT64_MAX) { 6906 phdr = (struct scsi_log_param_header *)data; 6907 scsi_ulto2b(0x00f1, phdr->param_code); 6908 phdr->param_control = SLP_LBIN | SLP_LP; 6909 phdr->param_len = 8; 6910 data = (uint8_t *)(phdr + 1); 6911 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6912 data[4] = 0x02; /* per-pool */ 6913 data += phdr->param_len; 6914 } 6915 6916 if (lun->backend->lun_attr != NULL && 6917 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6918 != UINT64_MAX) { 6919 phdr = (struct scsi_log_param_header *)data; 6920 scsi_ulto2b(0x00f2, phdr->param_code); 6921 phdr->param_control = SLP_LBIN | SLP_LP; 6922 phdr->param_len = 8; 6923 data = (uint8_t *)(phdr + 1); 6924 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6925 data[4] = 0x02; /* per-pool */ 6926 data += phdr->param_len; 6927 } 6928 6929 page_index->page_len = data - page_index->page_data; 6930 return (0); 6931} 6932 6933int 6934ctl_log_sense(struct ctl_scsiio *ctsio) 6935{ 6936 struct ctl_lun *lun; 6937 int i, pc, page_code, subpage; 6938 int alloc_len, total_len; 6939 struct ctl_page_index *page_index; 6940 struct scsi_log_sense *cdb; 6941 struct scsi_log_header *header; 6942 6943 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6944 6945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6946 cdb = (struct scsi_log_sense *)ctsio->cdb; 6947 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6948 page_code = cdb->page & SLS_PAGE_CODE; 6949 subpage = cdb->subpage; 6950 alloc_len = scsi_2btoul(cdb->length); 6951 6952 page_index = NULL; 6953 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6954 page_index = &lun->log_pages.index[i]; 6955 6956 /* Look for the right page code */ 6957 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6958 continue; 6959 6960 /* Look for the right subpage or the subpage wildcard*/ 6961 if (page_index->subpage != subpage) 6962 continue; 6963 6964 break; 6965 } 6966 if (i >= CTL_NUM_LOG_PAGES) { 6967 ctl_set_invalid_field(ctsio, 6968 /*sks_valid*/ 1, 6969 /*command*/ 1, 6970 /*field*/ 2, 6971 /*bit_valid*/ 0, 6972 /*bit*/ 0); 6973 ctl_done((union ctl_io *)ctsio); 6974 return (CTL_RETVAL_COMPLETE); 6975 } 6976 6977 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6978 6979 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6980 ctsio->kern_sg_entries = 0; 6981 ctsio->kern_data_resid = 0; 6982 ctsio->kern_rel_offset = 0; 6983 if (total_len < alloc_len) { 6984 ctsio->residual = alloc_len - total_len; 6985 ctsio->kern_data_len = total_len; 6986 ctsio->kern_total_len = total_len; 6987 } else { 6988 ctsio->residual = 0; 6989 ctsio->kern_data_len = alloc_len; 6990 ctsio->kern_total_len = alloc_len; 6991 } 6992 6993 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6994 header->page = page_index->page_code; 6995 if (page_index->subpage) { 6996 header->page |= SL_SPF; 6997 header->subpage = page_index->subpage; 6998 } 6999 scsi_ulto2b(page_index->page_len, header->datalen); 7000 7001 /* 7002 * Call the handler, if it exists, to update the 7003 * page to the latest values. 7004 */ 7005 if (page_index->sense_handler != NULL) 7006 page_index->sense_handler(ctsio, page_index, pc); 7007 7008 memcpy(header + 1, page_index->page_data, page_index->page_len); 7009 7010 ctl_set_success(ctsio); 7011 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7012 ctsio->be_move_done = ctl_config_move_done; 7013 ctl_datamove((union ctl_io *)ctsio); 7014 return (CTL_RETVAL_COMPLETE); 7015} 7016 7017int 7018ctl_read_capacity(struct ctl_scsiio *ctsio) 7019{ 7020 struct scsi_read_capacity *cdb; 7021 struct scsi_read_capacity_data *data; 7022 struct ctl_lun *lun; 7023 uint32_t lba; 7024 7025 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 7026 7027 cdb = (struct scsi_read_capacity *)ctsio->cdb; 7028 7029 lba = scsi_4btoul(cdb->addr); 7030 if (((cdb->pmi & SRC_PMI) == 0) 7031 && (lba != 0)) { 7032 ctl_set_invalid_field(/*ctsio*/ ctsio, 7033 /*sks_valid*/ 1, 7034 /*command*/ 1, 7035 /*field*/ 2, 7036 /*bit_valid*/ 0, 7037 /*bit*/ 0); 7038 ctl_done((union ctl_io *)ctsio); 7039 return (CTL_RETVAL_COMPLETE); 7040 } 7041 7042 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7043 7044 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7045 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 7046 ctsio->residual = 0; 7047 ctsio->kern_data_len = sizeof(*data); 7048 ctsio->kern_total_len = sizeof(*data); 7049 ctsio->kern_data_resid = 0; 7050 ctsio->kern_rel_offset = 0; 7051 ctsio->kern_sg_entries = 0; 7052 7053 /* 7054 * If the maximum LBA is greater than 0xfffffffe, the user must 7055 * issue a SERVICE ACTION IN (16) command, with the read capacity 7056 * serivce action set. 7057 */ 7058 if (lun->be_lun->maxlba > 0xfffffffe) 7059 scsi_ulto4b(0xffffffff, data->addr); 7060 else 7061 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 7062 7063 /* 7064 * XXX KDM this may not be 512 bytes... 7065 */ 7066 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7067 7068 ctl_set_success(ctsio); 7069 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7070 ctsio->be_move_done = ctl_config_move_done; 7071 ctl_datamove((union ctl_io *)ctsio); 7072 return (CTL_RETVAL_COMPLETE); 7073} 7074 7075int 7076ctl_read_capacity_16(struct ctl_scsiio *ctsio) 7077{ 7078 struct scsi_read_capacity_16 *cdb; 7079 struct scsi_read_capacity_data_long *data; 7080 struct ctl_lun *lun; 7081 uint64_t lba; 7082 uint32_t alloc_len; 7083 7084 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 7085 7086 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 7087 7088 alloc_len = scsi_4btoul(cdb->alloc_len); 7089 lba = scsi_8btou64(cdb->addr); 7090 7091 if ((cdb->reladr & SRC16_PMI) 7092 && (lba != 0)) { 7093 ctl_set_invalid_field(/*ctsio*/ ctsio, 7094 /*sks_valid*/ 1, 7095 /*command*/ 1, 7096 /*field*/ 2, 7097 /*bit_valid*/ 0, 7098 /*bit*/ 0); 7099 ctl_done((union ctl_io *)ctsio); 7100 return (CTL_RETVAL_COMPLETE); 7101 } 7102 7103 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7104 7105 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 7106 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 7107 7108 if (sizeof(*data) < alloc_len) { 7109 ctsio->residual = alloc_len - sizeof(*data); 7110 ctsio->kern_data_len = sizeof(*data); 7111 ctsio->kern_total_len = sizeof(*data); 7112 } else { 7113 ctsio->residual = 0; 7114 ctsio->kern_data_len = alloc_len; 7115 ctsio->kern_total_len = alloc_len; 7116 } 7117 ctsio->kern_data_resid = 0; 7118 ctsio->kern_rel_offset = 0; 7119 ctsio->kern_sg_entries = 0; 7120 7121 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 7122 /* XXX KDM this may not be 512 bytes... */ 7123 scsi_ulto4b(lun->be_lun->blocksize, data->length); 7124 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 7125 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 7126 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 7127 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 7128 7129 ctl_set_success(ctsio); 7130 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7131 ctsio->be_move_done = ctl_config_move_done; 7132 ctl_datamove((union ctl_io *)ctsio); 7133 return (CTL_RETVAL_COMPLETE); 7134} 7135 7136int 7137ctl_read_defect(struct ctl_scsiio *ctsio) 7138{ 7139 struct scsi_read_defect_data_10 *ccb10; 7140 struct scsi_read_defect_data_12 *ccb12; 7141 struct scsi_read_defect_data_hdr_10 *data10; 7142 struct scsi_read_defect_data_hdr_12 *data12; 7143 uint32_t alloc_len, data_len; 7144 uint8_t format; 7145 7146 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7147 7148 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7149 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7150 format = ccb10->format; 7151 alloc_len = scsi_2btoul(ccb10->alloc_length); 7152 data_len = sizeof(*data10); 7153 } else { 7154 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7155 format = ccb12->format; 7156 alloc_len = scsi_4btoul(ccb12->alloc_length); 7157 data_len = sizeof(*data12); 7158 } 7159 if (alloc_len == 0) { 7160 ctl_set_success(ctsio); 7161 ctl_done((union ctl_io *)ctsio); 7162 return (CTL_RETVAL_COMPLETE); 7163 } 7164 7165 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7166 if (data_len < alloc_len) { 7167 ctsio->residual = alloc_len - data_len; 7168 ctsio->kern_data_len = data_len; 7169 ctsio->kern_total_len = data_len; 7170 } else { 7171 ctsio->residual = 0; 7172 ctsio->kern_data_len = alloc_len; 7173 ctsio->kern_total_len = alloc_len; 7174 } 7175 ctsio->kern_data_resid = 0; 7176 ctsio->kern_rel_offset = 0; 7177 ctsio->kern_sg_entries = 0; 7178 7179 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7180 data10 = (struct scsi_read_defect_data_hdr_10 *) 7181 ctsio->kern_data_ptr; 7182 data10->format = format; 7183 scsi_ulto2b(0, data10->length); 7184 } else { 7185 data12 = (struct scsi_read_defect_data_hdr_12 *) 7186 ctsio->kern_data_ptr; 7187 data12->format = format; 7188 scsi_ulto2b(0, data12->generation); 7189 scsi_ulto4b(0, data12->length); 7190 } 7191 7192 ctl_set_success(ctsio); 7193 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7194 ctsio->be_move_done = ctl_config_move_done; 7195 ctl_datamove((union ctl_io *)ctsio); 7196 return (CTL_RETVAL_COMPLETE); 7197} 7198 7199int 7200ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7201{ 7202 struct scsi_maintenance_in *cdb; 7203 int retval; 7204 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os; 7205 int num_target_port_groups, num_target_ports; 7206 struct ctl_lun *lun; 7207 struct ctl_softc *softc; 7208 struct ctl_port *port; 7209 struct scsi_target_group_data *rtg_ptr; 7210 struct scsi_target_group_data_extended *rtg_ext_ptr; 7211 struct scsi_target_port_group_descriptor *tpg_desc; 7212 7213 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7214 7215 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7216 softc = control_softc; 7217 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7218 7219 retval = CTL_RETVAL_COMPLETE; 7220 7221 switch (cdb->byte2 & STG_PDF_MASK) { 7222 case STG_PDF_LENGTH: 7223 ext = 0; 7224 break; 7225 case STG_PDF_EXTENDED: 7226 ext = 1; 7227 break; 7228 default: 7229 ctl_set_invalid_field(/*ctsio*/ ctsio, 7230 /*sks_valid*/ 1, 7231 /*command*/ 1, 7232 /*field*/ 2, 7233 /*bit_valid*/ 1, 7234 /*bit*/ 5); 7235 ctl_done((union ctl_io *)ctsio); 7236 return(retval); 7237 } 7238 7239 if (softc->is_single) 7240 num_target_port_groups = 1; 7241 else 7242 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7243 num_target_ports = 0; 7244 mtx_lock(&softc->ctl_lock); 7245 STAILQ_FOREACH(port, &softc->port_list, links) { 7246 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7247 continue; 7248 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS) 7249 continue; 7250 num_target_ports++; 7251 } 7252 mtx_unlock(&softc->ctl_lock); 7253 7254 if (ext) 7255 total_len = sizeof(struct scsi_target_group_data_extended); 7256 else 7257 total_len = sizeof(struct scsi_target_group_data); 7258 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7259 num_target_port_groups + 7260 sizeof(struct scsi_target_port_descriptor) * 7261 num_target_ports * num_target_port_groups; 7262 7263 alloc_len = scsi_4btoul(cdb->length); 7264 7265 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7266 7267 ctsio->kern_sg_entries = 0; 7268 7269 if (total_len < alloc_len) { 7270 ctsio->residual = alloc_len - total_len; 7271 ctsio->kern_data_len = total_len; 7272 ctsio->kern_total_len = total_len; 7273 } else { 7274 ctsio->residual = 0; 7275 ctsio->kern_data_len = alloc_len; 7276 ctsio->kern_total_len = alloc_len; 7277 } 7278 ctsio->kern_data_resid = 0; 7279 ctsio->kern_rel_offset = 0; 7280 7281 if (ext) { 7282 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7283 ctsio->kern_data_ptr; 7284 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7285 rtg_ext_ptr->format_type = 0x10; 7286 rtg_ext_ptr->implicit_transition_time = 0; 7287 tpg_desc = &rtg_ext_ptr->groups[0]; 7288 } else { 7289 rtg_ptr = (struct scsi_target_group_data *) 7290 ctsio->kern_data_ptr; 7291 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7292 tpg_desc = &rtg_ptr->groups[0]; 7293 } 7294 7295 mtx_lock(&softc->ctl_lock); 7296 pg = softc->port_offset / CTL_MAX_PORTS; 7297 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) { 7298 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) { 7299 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7300 os = TPG_ASYMMETRIC_ACCESS_STANDBY; 7301 } else if (lun->flags & CTL_LUN_PRIMARY_SC) { 7302 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7303 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7304 } else { 7305 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7306 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7307 } 7308 } else { 7309 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7310 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7311 } 7312 for (g = 0; g < num_target_port_groups; g++) { 7313 tpg_desc->pref_state = (g == pg) ? gs : os; 7314 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP; 7315 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7316 tpg_desc->status = TPG_IMPLICIT; 7317 pc = 0; 7318 STAILQ_FOREACH(port, &softc->port_list, links) { 7319 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7320 continue; 7321 if (ctl_map_lun_back(port->targ_port, lun->lun) >= 7322 CTL_MAX_LUNS) 7323 continue; 7324 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 7325 scsi_ulto2b(p, tpg_desc->descriptors[pc]. 7326 relative_target_port_identifier); 7327 pc++; 7328 } 7329 tpg_desc->target_port_count = pc; 7330 tpg_desc = (struct scsi_target_port_group_descriptor *) 7331 &tpg_desc->descriptors[pc]; 7332 } 7333 mtx_unlock(&softc->ctl_lock); 7334 7335 ctl_set_success(ctsio); 7336 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7337 ctsio->be_move_done = ctl_config_move_done; 7338 ctl_datamove((union ctl_io *)ctsio); 7339 return(retval); 7340} 7341 7342int 7343ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7344{ 7345 struct ctl_lun *lun; 7346 struct scsi_report_supported_opcodes *cdb; 7347 const struct ctl_cmd_entry *entry, *sentry; 7348 struct scsi_report_supported_opcodes_all *all; 7349 struct scsi_report_supported_opcodes_descr *descr; 7350 struct scsi_report_supported_opcodes_one *one; 7351 int retval; 7352 int alloc_len, total_len; 7353 int opcode, service_action, i, j, num; 7354 7355 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7356 7357 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7358 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7359 7360 retval = CTL_RETVAL_COMPLETE; 7361 7362 opcode = cdb->requested_opcode; 7363 service_action = scsi_2btoul(cdb->requested_service_action); 7364 switch (cdb->options & RSO_OPTIONS_MASK) { 7365 case RSO_OPTIONS_ALL: 7366 num = 0; 7367 for (i = 0; i < 256; i++) { 7368 entry = &ctl_cmd_table[i]; 7369 if (entry->flags & CTL_CMD_FLAG_SA5) { 7370 for (j = 0; j < 32; j++) { 7371 sentry = &((const struct ctl_cmd_entry *) 7372 entry->execute)[j]; 7373 if (ctl_cmd_applicable( 7374 lun->be_lun->lun_type, sentry)) 7375 num++; 7376 } 7377 } else { 7378 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7379 entry)) 7380 num++; 7381 } 7382 } 7383 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7384 num * sizeof(struct scsi_report_supported_opcodes_descr); 7385 break; 7386 case RSO_OPTIONS_OC: 7387 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7388 ctl_set_invalid_field(/*ctsio*/ ctsio, 7389 /*sks_valid*/ 1, 7390 /*command*/ 1, 7391 /*field*/ 2, 7392 /*bit_valid*/ 1, 7393 /*bit*/ 2); 7394 ctl_done((union ctl_io *)ctsio); 7395 return (CTL_RETVAL_COMPLETE); 7396 } 7397 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7398 break; 7399 case RSO_OPTIONS_OC_SA: 7400 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7401 service_action >= 32) { 7402 ctl_set_invalid_field(/*ctsio*/ ctsio, 7403 /*sks_valid*/ 1, 7404 /*command*/ 1, 7405 /*field*/ 2, 7406 /*bit_valid*/ 1, 7407 /*bit*/ 2); 7408 ctl_done((union ctl_io *)ctsio); 7409 return (CTL_RETVAL_COMPLETE); 7410 } 7411 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7412 break; 7413 default: 7414 ctl_set_invalid_field(/*ctsio*/ ctsio, 7415 /*sks_valid*/ 1, 7416 /*command*/ 1, 7417 /*field*/ 2, 7418 /*bit_valid*/ 1, 7419 /*bit*/ 2); 7420 ctl_done((union ctl_io *)ctsio); 7421 return (CTL_RETVAL_COMPLETE); 7422 } 7423 7424 alloc_len = scsi_4btoul(cdb->length); 7425 7426 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7427 7428 ctsio->kern_sg_entries = 0; 7429 7430 if (total_len < alloc_len) { 7431 ctsio->residual = alloc_len - total_len; 7432 ctsio->kern_data_len = total_len; 7433 ctsio->kern_total_len = total_len; 7434 } else { 7435 ctsio->residual = 0; 7436 ctsio->kern_data_len = alloc_len; 7437 ctsio->kern_total_len = alloc_len; 7438 } 7439 ctsio->kern_data_resid = 0; 7440 ctsio->kern_rel_offset = 0; 7441 7442 switch (cdb->options & RSO_OPTIONS_MASK) { 7443 case RSO_OPTIONS_ALL: 7444 all = (struct scsi_report_supported_opcodes_all *) 7445 ctsio->kern_data_ptr; 7446 num = 0; 7447 for (i = 0; i < 256; i++) { 7448 entry = &ctl_cmd_table[i]; 7449 if (entry->flags & CTL_CMD_FLAG_SA5) { 7450 for (j = 0; j < 32; j++) { 7451 sentry = &((const struct ctl_cmd_entry *) 7452 entry->execute)[j]; 7453 if (!ctl_cmd_applicable( 7454 lun->be_lun->lun_type, sentry)) 7455 continue; 7456 descr = &all->descr[num++]; 7457 descr->opcode = i; 7458 scsi_ulto2b(j, descr->service_action); 7459 descr->flags = RSO_SERVACTV; 7460 scsi_ulto2b(sentry->length, 7461 descr->cdb_length); 7462 } 7463 } else { 7464 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7465 entry)) 7466 continue; 7467 descr = &all->descr[num++]; 7468 descr->opcode = i; 7469 scsi_ulto2b(0, descr->service_action); 7470 descr->flags = 0; 7471 scsi_ulto2b(entry->length, descr->cdb_length); 7472 } 7473 } 7474 scsi_ulto4b( 7475 num * sizeof(struct scsi_report_supported_opcodes_descr), 7476 all->length); 7477 break; 7478 case RSO_OPTIONS_OC: 7479 one = (struct scsi_report_supported_opcodes_one *) 7480 ctsio->kern_data_ptr; 7481 entry = &ctl_cmd_table[opcode]; 7482 goto fill_one; 7483 case RSO_OPTIONS_OC_SA: 7484 one = (struct scsi_report_supported_opcodes_one *) 7485 ctsio->kern_data_ptr; 7486 entry = &ctl_cmd_table[opcode]; 7487 entry = &((const struct ctl_cmd_entry *) 7488 entry->execute)[service_action]; 7489fill_one: 7490 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7491 one->support = 3; 7492 scsi_ulto2b(entry->length, one->cdb_length); 7493 one->cdb_usage[0] = opcode; 7494 memcpy(&one->cdb_usage[1], entry->usage, 7495 entry->length - 1); 7496 } else 7497 one->support = 1; 7498 break; 7499 } 7500 7501 ctl_set_success(ctsio); 7502 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7503 ctsio->be_move_done = ctl_config_move_done; 7504 ctl_datamove((union ctl_io *)ctsio); 7505 return(retval); 7506} 7507 7508int 7509ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7510{ 7511 struct scsi_report_supported_tmf *cdb; 7512 struct scsi_report_supported_tmf_data *data; 7513 int retval; 7514 int alloc_len, total_len; 7515 7516 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7517 7518 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7519 7520 retval = CTL_RETVAL_COMPLETE; 7521 7522 total_len = sizeof(struct scsi_report_supported_tmf_data); 7523 alloc_len = scsi_4btoul(cdb->length); 7524 7525 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7526 7527 ctsio->kern_sg_entries = 0; 7528 7529 if (total_len < alloc_len) { 7530 ctsio->residual = alloc_len - total_len; 7531 ctsio->kern_data_len = total_len; 7532 ctsio->kern_total_len = total_len; 7533 } else { 7534 ctsio->residual = 0; 7535 ctsio->kern_data_len = alloc_len; 7536 ctsio->kern_total_len = alloc_len; 7537 } 7538 ctsio->kern_data_resid = 0; 7539 ctsio->kern_rel_offset = 0; 7540 7541 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7542 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS; 7543 data->byte2 |= RST_ITNRS; 7544 7545 ctl_set_success(ctsio); 7546 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7547 ctsio->be_move_done = ctl_config_move_done; 7548 ctl_datamove((union ctl_io *)ctsio); 7549 return (retval); 7550} 7551 7552int 7553ctl_report_timestamp(struct ctl_scsiio *ctsio) 7554{ 7555 struct scsi_report_timestamp *cdb; 7556 struct scsi_report_timestamp_data *data; 7557 struct timeval tv; 7558 int64_t timestamp; 7559 int retval; 7560 int alloc_len, total_len; 7561 7562 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7563 7564 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7565 7566 retval = CTL_RETVAL_COMPLETE; 7567 7568 total_len = sizeof(struct scsi_report_timestamp_data); 7569 alloc_len = scsi_4btoul(cdb->length); 7570 7571 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7572 7573 ctsio->kern_sg_entries = 0; 7574 7575 if (total_len < alloc_len) { 7576 ctsio->residual = alloc_len - total_len; 7577 ctsio->kern_data_len = total_len; 7578 ctsio->kern_total_len = total_len; 7579 } else { 7580 ctsio->residual = 0; 7581 ctsio->kern_data_len = alloc_len; 7582 ctsio->kern_total_len = alloc_len; 7583 } 7584 ctsio->kern_data_resid = 0; 7585 ctsio->kern_rel_offset = 0; 7586 7587 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7588 scsi_ulto2b(sizeof(*data) - 2, data->length); 7589 data->origin = RTS_ORIG_OUTSIDE; 7590 getmicrotime(&tv); 7591 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7592 scsi_ulto4b(timestamp >> 16, data->timestamp); 7593 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7594 7595 ctl_set_success(ctsio); 7596 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7597 ctsio->be_move_done = ctl_config_move_done; 7598 ctl_datamove((union ctl_io *)ctsio); 7599 return (retval); 7600} 7601 7602int 7603ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7604{ 7605 struct scsi_per_res_in *cdb; 7606 int alloc_len, total_len = 0; 7607 /* struct scsi_per_res_in_rsrv in_data; */ 7608 struct ctl_lun *lun; 7609 struct ctl_softc *softc; 7610 7611 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7612 7613 softc = control_softc; 7614 7615 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7616 7617 alloc_len = scsi_2btoul(cdb->length); 7618 7619 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7620 7621retry: 7622 mtx_lock(&lun->lun_lock); 7623 switch (cdb->action) { 7624 case SPRI_RK: /* read keys */ 7625 total_len = sizeof(struct scsi_per_res_in_keys) + 7626 lun->pr_key_count * 7627 sizeof(struct scsi_per_res_key); 7628 break; 7629 case SPRI_RR: /* read reservation */ 7630 if (lun->flags & CTL_LUN_PR_RESERVED) 7631 total_len = sizeof(struct scsi_per_res_in_rsrv); 7632 else 7633 total_len = sizeof(struct scsi_per_res_in_header); 7634 break; 7635 case SPRI_RC: /* report capabilities */ 7636 total_len = sizeof(struct scsi_per_res_cap); 7637 break; 7638 case SPRI_RS: /* read full status */ 7639 total_len = sizeof(struct scsi_per_res_in_header) + 7640 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7641 lun->pr_key_count; 7642 break; 7643 default: 7644 panic("Invalid PR type %x", cdb->action); 7645 } 7646 mtx_unlock(&lun->lun_lock); 7647 7648 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7649 7650 if (total_len < alloc_len) { 7651 ctsio->residual = alloc_len - total_len; 7652 ctsio->kern_data_len = total_len; 7653 ctsio->kern_total_len = total_len; 7654 } else { 7655 ctsio->residual = 0; 7656 ctsio->kern_data_len = alloc_len; 7657 ctsio->kern_total_len = alloc_len; 7658 } 7659 7660 ctsio->kern_data_resid = 0; 7661 ctsio->kern_rel_offset = 0; 7662 ctsio->kern_sg_entries = 0; 7663 7664 mtx_lock(&lun->lun_lock); 7665 switch (cdb->action) { 7666 case SPRI_RK: { // read keys 7667 struct scsi_per_res_in_keys *res_keys; 7668 int i, key_count; 7669 7670 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7671 7672 /* 7673 * We had to drop the lock to allocate our buffer, which 7674 * leaves time for someone to come in with another 7675 * persistent reservation. (That is unlikely, though, 7676 * since this should be the only persistent reservation 7677 * command active right now.) 7678 */ 7679 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7680 (lun->pr_key_count * 7681 sizeof(struct scsi_per_res_key)))){ 7682 mtx_unlock(&lun->lun_lock); 7683 free(ctsio->kern_data_ptr, M_CTL); 7684 printf("%s: reservation length changed, retrying\n", 7685 __func__); 7686 goto retry; 7687 } 7688 7689 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation); 7690 7691 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7692 lun->pr_key_count, res_keys->header.length); 7693 7694 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7695 if (lun->pr_keys[i] == 0) 7696 continue; 7697 7698 /* 7699 * We used lun->pr_key_count to calculate the 7700 * size to allocate. If it turns out the number of 7701 * initiators with the registered flag set is 7702 * larger than that (i.e. they haven't been kept in 7703 * sync), we've got a problem. 7704 */ 7705 if (key_count >= lun->pr_key_count) { 7706#ifdef NEEDTOPORT 7707 csevent_log(CSC_CTL | CSC_SHELF_SW | 7708 CTL_PR_ERROR, 7709 csevent_LogType_Fault, 7710 csevent_AlertLevel_Yellow, 7711 csevent_FRU_ShelfController, 7712 csevent_FRU_Firmware, 7713 csevent_FRU_Unknown, 7714 "registered keys %d >= key " 7715 "count %d", key_count, 7716 lun->pr_key_count); 7717#endif 7718 key_count++; 7719 continue; 7720 } 7721 scsi_u64to8b(lun->pr_keys[i], 7722 res_keys->keys[key_count].key); 7723 key_count++; 7724 } 7725 break; 7726 } 7727 case SPRI_RR: { // read reservation 7728 struct scsi_per_res_in_rsrv *res; 7729 int tmp_len, header_only; 7730 7731 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7732 7733 scsi_ulto4b(lun->PRGeneration, res->header.generation); 7734 7735 if (lun->flags & CTL_LUN_PR_RESERVED) 7736 { 7737 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7738 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7739 res->header.length); 7740 header_only = 0; 7741 } else { 7742 tmp_len = sizeof(struct scsi_per_res_in_header); 7743 scsi_ulto4b(0, res->header.length); 7744 header_only = 1; 7745 } 7746 7747 /* 7748 * We had to drop the lock to allocate our buffer, which 7749 * leaves time for someone to come in with another 7750 * persistent reservation. (That is unlikely, though, 7751 * since this should be the only persistent reservation 7752 * command active right now.) 7753 */ 7754 if (tmp_len != total_len) { 7755 mtx_unlock(&lun->lun_lock); 7756 free(ctsio->kern_data_ptr, M_CTL); 7757 printf("%s: reservation status changed, retrying\n", 7758 __func__); 7759 goto retry; 7760 } 7761 7762 /* 7763 * No reservation held, so we're done. 7764 */ 7765 if (header_only != 0) 7766 break; 7767 7768 /* 7769 * If the registration is an All Registrants type, the key 7770 * is 0, since it doesn't really matter. 7771 */ 7772 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7773 scsi_u64to8b(lun->pr_keys[lun->pr_res_idx], 7774 res->data.reservation); 7775 } 7776 res->data.scopetype = lun->res_type; 7777 break; 7778 } 7779 case SPRI_RC: //report capabilities 7780 { 7781 struct scsi_per_res_cap *res_cap; 7782 uint16_t type_mask; 7783 7784 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7785 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7786 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5; 7787 type_mask = SPRI_TM_WR_EX_AR | 7788 SPRI_TM_EX_AC_RO | 7789 SPRI_TM_WR_EX_RO | 7790 SPRI_TM_EX_AC | 7791 SPRI_TM_WR_EX | 7792 SPRI_TM_EX_AC_AR; 7793 scsi_ulto2b(type_mask, res_cap->type_mask); 7794 break; 7795 } 7796 case SPRI_RS: { // read full status 7797 struct scsi_per_res_in_full *res_status; 7798 struct scsi_per_res_in_full_desc *res_desc; 7799 struct ctl_port *port; 7800 int i, len; 7801 7802 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7803 7804 /* 7805 * We had to drop the lock to allocate our buffer, which 7806 * leaves time for someone to come in with another 7807 * persistent reservation. (That is unlikely, though, 7808 * since this should be the only persistent reservation 7809 * command active right now.) 7810 */ 7811 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7812 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7813 lun->pr_key_count)){ 7814 mtx_unlock(&lun->lun_lock); 7815 free(ctsio->kern_data_ptr, M_CTL); 7816 printf("%s: reservation length changed, retrying\n", 7817 __func__); 7818 goto retry; 7819 } 7820 7821 scsi_ulto4b(lun->PRGeneration, res_status->header.generation); 7822 7823 res_desc = &res_status->desc[0]; 7824 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) { 7825 if (lun->pr_keys[i] == 0) 7826 continue; 7827 7828 scsi_u64to8b(lun->pr_keys[i], res_desc->res_key.key); 7829 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7830 (lun->pr_res_idx == i || 7831 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7832 res_desc->flags = SPRI_FULL_R_HOLDER; 7833 res_desc->scopetype = lun->res_type; 7834 } 7835 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7836 res_desc->rel_trgt_port_id); 7837 len = 0; 7838 port = softc->ctl_ports[ 7839 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)]; 7840 if (port != NULL) 7841 len = ctl_create_iid(port, 7842 i % CTL_MAX_INIT_PER_PORT, 7843 res_desc->transport_id); 7844 scsi_ulto4b(len, res_desc->additional_length); 7845 res_desc = (struct scsi_per_res_in_full_desc *) 7846 &res_desc->transport_id[len]; 7847 } 7848 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7849 res_status->header.length); 7850 break; 7851 } 7852 default: 7853 /* 7854 * This is a bug, because we just checked for this above, 7855 * and should have returned an error. 7856 */ 7857 panic("Invalid PR type %x", cdb->action); 7858 break; /* NOTREACHED */ 7859 } 7860 mtx_unlock(&lun->lun_lock); 7861 7862 ctl_set_success(ctsio); 7863 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7864 ctsio->be_move_done = ctl_config_move_done; 7865 ctl_datamove((union ctl_io *)ctsio); 7866 return (CTL_RETVAL_COMPLETE); 7867} 7868 7869static void 7870ctl_set_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua) 7871{ 7872 int off = lun->ctl_softc->persis_offset; 7873 7874 if (residx >= off && residx < off + CTL_MAX_INITIATORS) 7875 lun->pending_ua[residx - off] |= ua; 7876} 7877 7878/* 7879 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7880 * it should return. 7881 */ 7882static int 7883ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7884 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7885 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7886 struct scsi_per_res_out_parms* param) 7887{ 7888 union ctl_ha_msg persis_io; 7889 int retval, i; 7890 int isc_retval; 7891 7892 retval = 0; 7893 7894 mtx_lock(&lun->lun_lock); 7895 if (sa_res_key == 0) { 7896 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7897 /* validate scope and type */ 7898 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7899 SPR_LU_SCOPE) { 7900 mtx_unlock(&lun->lun_lock); 7901 ctl_set_invalid_field(/*ctsio*/ ctsio, 7902 /*sks_valid*/ 1, 7903 /*command*/ 1, 7904 /*field*/ 2, 7905 /*bit_valid*/ 1, 7906 /*bit*/ 4); 7907 ctl_done((union ctl_io *)ctsio); 7908 return (1); 7909 } 7910 7911 if (type>8 || type==2 || type==4 || type==0) { 7912 mtx_unlock(&lun->lun_lock); 7913 ctl_set_invalid_field(/*ctsio*/ ctsio, 7914 /*sks_valid*/ 1, 7915 /*command*/ 1, 7916 /*field*/ 2, 7917 /*bit_valid*/ 1, 7918 /*bit*/ 0); 7919 ctl_done((union ctl_io *)ctsio); 7920 return (1); 7921 } 7922 7923 /* 7924 * Unregister everybody else and build UA for 7925 * them 7926 */ 7927 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7928 if (i == residx || lun->pr_keys[i] == 0) 7929 continue; 7930 7931 lun->pr_keys[i] = 0; 7932 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 7933 } 7934 lun->pr_key_count = 1; 7935 lun->res_type = type; 7936 if (lun->res_type != SPR_TYPE_WR_EX_AR 7937 && lun->res_type != SPR_TYPE_EX_AC_AR) 7938 lun->pr_res_idx = residx; 7939 7940 /* send msg to other side */ 7941 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7942 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7943 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7944 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7945 persis_io.pr.pr_info.res_type = type; 7946 memcpy(persis_io.pr.pr_info.sa_res_key, 7947 param->serv_act_res_key, 7948 sizeof(param->serv_act_res_key)); 7949 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 7950 &persis_io, sizeof(persis_io), 0)) > 7951 CTL_HA_STATUS_SUCCESS) { 7952 printf("CTL:Persis Out error returned " 7953 "from ctl_ha_msg_send %d\n", 7954 isc_retval); 7955 } 7956 } else { 7957 /* not all registrants */ 7958 mtx_unlock(&lun->lun_lock); 7959 free(ctsio->kern_data_ptr, M_CTL); 7960 ctl_set_invalid_field(ctsio, 7961 /*sks_valid*/ 1, 7962 /*command*/ 0, 7963 /*field*/ 8, 7964 /*bit_valid*/ 0, 7965 /*bit*/ 0); 7966 ctl_done((union ctl_io *)ctsio); 7967 return (1); 7968 } 7969 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7970 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7971 int found = 0; 7972 7973 if (res_key == sa_res_key) { 7974 /* special case */ 7975 /* 7976 * The spec implies this is not good but doesn't 7977 * say what to do. There are two choices either 7978 * generate a res conflict or check condition 7979 * with illegal field in parameter data. Since 7980 * that is what is done when the sa_res_key is 7981 * zero I'll take that approach since this has 7982 * to do with the sa_res_key. 7983 */ 7984 mtx_unlock(&lun->lun_lock); 7985 free(ctsio->kern_data_ptr, M_CTL); 7986 ctl_set_invalid_field(ctsio, 7987 /*sks_valid*/ 1, 7988 /*command*/ 0, 7989 /*field*/ 8, 7990 /*bit_valid*/ 0, 7991 /*bit*/ 0); 7992 ctl_done((union ctl_io *)ctsio); 7993 return (1); 7994 } 7995 7996 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 7997 if (lun->pr_keys[i] != sa_res_key) 7998 continue; 7999 8000 found = 1; 8001 lun->pr_keys[i] = 0; 8002 lun->pr_key_count--; 8003 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8004 } 8005 if (!found) { 8006 mtx_unlock(&lun->lun_lock); 8007 free(ctsio->kern_data_ptr, M_CTL); 8008 ctl_set_reservation_conflict(ctsio); 8009 ctl_done((union ctl_io *)ctsio); 8010 return (CTL_RETVAL_COMPLETE); 8011 } 8012 /* send msg to other side */ 8013 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8014 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8015 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8016 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8017 persis_io.pr.pr_info.res_type = type; 8018 memcpy(persis_io.pr.pr_info.sa_res_key, 8019 param->serv_act_res_key, 8020 sizeof(param->serv_act_res_key)); 8021 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8022 &persis_io, sizeof(persis_io), 0)) > 8023 CTL_HA_STATUS_SUCCESS) { 8024 printf("CTL:Persis Out error returned from " 8025 "ctl_ha_msg_send %d\n", isc_retval); 8026 } 8027 } else { 8028 /* Reserved but not all registrants */ 8029 /* sa_res_key is res holder */ 8030 if (sa_res_key == lun->pr_keys[lun->pr_res_idx]) { 8031 /* validate scope and type */ 8032 if ((cdb->scope_type & SPR_SCOPE_MASK) != 8033 SPR_LU_SCOPE) { 8034 mtx_unlock(&lun->lun_lock); 8035 ctl_set_invalid_field(/*ctsio*/ ctsio, 8036 /*sks_valid*/ 1, 8037 /*command*/ 1, 8038 /*field*/ 2, 8039 /*bit_valid*/ 1, 8040 /*bit*/ 4); 8041 ctl_done((union ctl_io *)ctsio); 8042 return (1); 8043 } 8044 8045 if (type>8 || type==2 || type==4 || type==0) { 8046 mtx_unlock(&lun->lun_lock); 8047 ctl_set_invalid_field(/*ctsio*/ ctsio, 8048 /*sks_valid*/ 1, 8049 /*command*/ 1, 8050 /*field*/ 2, 8051 /*bit_valid*/ 1, 8052 /*bit*/ 0); 8053 ctl_done((union ctl_io *)ctsio); 8054 return (1); 8055 } 8056 8057 /* 8058 * Do the following: 8059 * if sa_res_key != res_key remove all 8060 * registrants w/sa_res_key and generate UA 8061 * for these registrants(Registrations 8062 * Preempted) if it wasn't an exclusive 8063 * reservation generate UA(Reservations 8064 * Preempted) for all other registered nexuses 8065 * if the type has changed. Establish the new 8066 * reservation and holder. If res_key and 8067 * sa_res_key are the same do the above 8068 * except don't unregister the res holder. 8069 */ 8070 8071 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8072 if (i == residx || lun->pr_keys[i] == 0) 8073 continue; 8074 8075 if (sa_res_key == lun->pr_keys[i]) { 8076 lun->pr_keys[i] = 0; 8077 lun->pr_key_count--; 8078 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8079 } else if (type != lun->res_type 8080 && (lun->res_type == SPR_TYPE_WR_EX_RO 8081 || lun->res_type ==SPR_TYPE_EX_AC_RO)){ 8082 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE); 8083 } 8084 } 8085 lun->res_type = type; 8086 if (lun->res_type != SPR_TYPE_WR_EX_AR 8087 && lun->res_type != SPR_TYPE_EX_AC_AR) 8088 lun->pr_res_idx = residx; 8089 else 8090 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8091 8092 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8093 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8094 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8095 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8096 persis_io.pr.pr_info.res_type = type; 8097 memcpy(persis_io.pr.pr_info.sa_res_key, 8098 param->serv_act_res_key, 8099 sizeof(param->serv_act_res_key)); 8100 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8101 &persis_io, sizeof(persis_io), 0)) > 8102 CTL_HA_STATUS_SUCCESS) { 8103 printf("CTL:Persis Out error returned " 8104 "from ctl_ha_msg_send %d\n", 8105 isc_retval); 8106 } 8107 } else { 8108 /* 8109 * sa_res_key is not the res holder just 8110 * remove registrants 8111 */ 8112 int found=0; 8113 8114 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8115 if (sa_res_key != lun->pr_keys[i]) 8116 continue; 8117 8118 found = 1; 8119 lun->pr_keys[i] = 0; 8120 lun->pr_key_count--; 8121 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8122 } 8123 8124 if (!found) { 8125 mtx_unlock(&lun->lun_lock); 8126 free(ctsio->kern_data_ptr, M_CTL); 8127 ctl_set_reservation_conflict(ctsio); 8128 ctl_done((union ctl_io *)ctsio); 8129 return (1); 8130 } 8131 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8132 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8133 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 8134 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8135 persis_io.pr.pr_info.res_type = type; 8136 memcpy(persis_io.pr.pr_info.sa_res_key, 8137 param->serv_act_res_key, 8138 sizeof(param->serv_act_res_key)); 8139 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8140 &persis_io, sizeof(persis_io), 0)) > 8141 CTL_HA_STATUS_SUCCESS) { 8142 printf("CTL:Persis Out error returned " 8143 "from ctl_ha_msg_send %d\n", 8144 isc_retval); 8145 } 8146 } 8147 } 8148 8149 lun->PRGeneration++; 8150 mtx_unlock(&lun->lun_lock); 8151 8152 return (retval); 8153} 8154 8155static void 8156ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 8157{ 8158 uint64_t sa_res_key; 8159 int i; 8160 8161 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 8162 8163 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 8164 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 8165 || sa_res_key != lun->pr_keys[lun->pr_res_idx]) { 8166 if (sa_res_key == 0) { 8167 /* 8168 * Unregister everybody else and build UA for 8169 * them 8170 */ 8171 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8172 if (i == msg->pr.pr_info.residx || 8173 lun->pr_keys[i] == 0) 8174 continue; 8175 8176 lun->pr_keys[i] = 0; 8177 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8178 } 8179 8180 lun->pr_key_count = 1; 8181 lun->res_type = msg->pr.pr_info.res_type; 8182 if (lun->res_type != SPR_TYPE_WR_EX_AR 8183 && lun->res_type != SPR_TYPE_EX_AC_AR) 8184 lun->pr_res_idx = msg->pr.pr_info.residx; 8185 } else { 8186 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8187 if (sa_res_key == lun->pr_keys[i]) 8188 continue; 8189 8190 lun->pr_keys[i] = 0; 8191 lun->pr_key_count--; 8192 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8193 } 8194 } 8195 } else { 8196 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8197 if (i == msg->pr.pr_info.residx || 8198 lun->pr_keys[i] == 0) 8199 continue; 8200 8201 if (sa_res_key == lun->pr_keys[i]) { 8202 lun->pr_keys[i] = 0; 8203 lun->pr_key_count--; 8204 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8205 } else if (msg->pr.pr_info.res_type != lun->res_type 8206 && (lun->res_type == SPR_TYPE_WR_EX_RO 8207 || lun->res_type == SPR_TYPE_EX_AC_RO)) { 8208 ctl_set_res_ua(lun, i, CTL_UA_RES_RELEASE); 8209 } 8210 } 8211 lun->res_type = msg->pr.pr_info.res_type; 8212 if (lun->res_type != SPR_TYPE_WR_EX_AR 8213 && lun->res_type != SPR_TYPE_EX_AC_AR) 8214 lun->pr_res_idx = msg->pr.pr_info.residx; 8215 else 8216 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8217 } 8218 lun->PRGeneration++; 8219 8220} 8221 8222 8223int 8224ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8225{ 8226 int retval; 8227 int isc_retval; 8228 u_int32_t param_len; 8229 struct scsi_per_res_out *cdb; 8230 struct ctl_lun *lun; 8231 struct scsi_per_res_out_parms* param; 8232 struct ctl_softc *softc; 8233 uint32_t residx; 8234 uint64_t res_key, sa_res_key; 8235 uint8_t type; 8236 union ctl_ha_msg persis_io; 8237 int i; 8238 8239 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8240 8241 retval = CTL_RETVAL_COMPLETE; 8242 8243 softc = control_softc; 8244 8245 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8246 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8247 8248 /* 8249 * We only support whole-LUN scope. The scope & type are ignored for 8250 * register, register and ignore existing key and clear. 8251 * We sometimes ignore scope and type on preempts too!! 8252 * Verify reservation type here as well. 8253 */ 8254 type = cdb->scope_type & SPR_TYPE_MASK; 8255 if ((cdb->action == SPRO_RESERVE) 8256 || (cdb->action == SPRO_RELEASE)) { 8257 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8258 ctl_set_invalid_field(/*ctsio*/ ctsio, 8259 /*sks_valid*/ 1, 8260 /*command*/ 1, 8261 /*field*/ 2, 8262 /*bit_valid*/ 1, 8263 /*bit*/ 4); 8264 ctl_done((union ctl_io *)ctsio); 8265 return (CTL_RETVAL_COMPLETE); 8266 } 8267 8268 if (type>8 || type==2 || type==4 || type==0) { 8269 ctl_set_invalid_field(/*ctsio*/ ctsio, 8270 /*sks_valid*/ 1, 8271 /*command*/ 1, 8272 /*field*/ 2, 8273 /*bit_valid*/ 1, 8274 /*bit*/ 0); 8275 ctl_done((union ctl_io *)ctsio); 8276 return (CTL_RETVAL_COMPLETE); 8277 } 8278 } 8279 8280 param_len = scsi_4btoul(cdb->length); 8281 8282 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8283 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8284 ctsio->kern_data_len = param_len; 8285 ctsio->kern_total_len = param_len; 8286 ctsio->kern_data_resid = 0; 8287 ctsio->kern_rel_offset = 0; 8288 ctsio->kern_sg_entries = 0; 8289 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8290 ctsio->be_move_done = ctl_config_move_done; 8291 ctl_datamove((union ctl_io *)ctsio); 8292 8293 return (CTL_RETVAL_COMPLETE); 8294 } 8295 8296 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8297 8298 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 8299 res_key = scsi_8btou64(param->res_key.key); 8300 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8301 8302 /* 8303 * Validate the reservation key here except for SPRO_REG_IGNO 8304 * This must be done for all other service actions 8305 */ 8306 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8307 mtx_lock(&lun->lun_lock); 8308 if (lun->pr_keys[residx] != 0) { 8309 if (res_key != lun->pr_keys[residx]) { 8310 /* 8311 * The current key passed in doesn't match 8312 * the one the initiator previously 8313 * registered. 8314 */ 8315 mtx_unlock(&lun->lun_lock); 8316 free(ctsio->kern_data_ptr, M_CTL); 8317 ctl_set_reservation_conflict(ctsio); 8318 ctl_done((union ctl_io *)ctsio); 8319 return (CTL_RETVAL_COMPLETE); 8320 } 8321 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8322 /* 8323 * We are not registered 8324 */ 8325 mtx_unlock(&lun->lun_lock); 8326 free(ctsio->kern_data_ptr, M_CTL); 8327 ctl_set_reservation_conflict(ctsio); 8328 ctl_done((union ctl_io *)ctsio); 8329 return (CTL_RETVAL_COMPLETE); 8330 } else if (res_key != 0) { 8331 /* 8332 * We are not registered and trying to register but 8333 * the register key isn't zero. 8334 */ 8335 mtx_unlock(&lun->lun_lock); 8336 free(ctsio->kern_data_ptr, M_CTL); 8337 ctl_set_reservation_conflict(ctsio); 8338 ctl_done((union ctl_io *)ctsio); 8339 return (CTL_RETVAL_COMPLETE); 8340 } 8341 mtx_unlock(&lun->lun_lock); 8342 } 8343 8344 switch (cdb->action & SPRO_ACTION_MASK) { 8345 case SPRO_REGISTER: 8346 case SPRO_REG_IGNO: { 8347 8348#if 0 8349 printf("Registration received\n"); 8350#endif 8351 8352 /* 8353 * We don't support any of these options, as we report in 8354 * the read capabilities request (see 8355 * ctl_persistent_reserve_in(), above). 8356 */ 8357 if ((param->flags & SPR_SPEC_I_PT) 8358 || (param->flags & SPR_ALL_TG_PT) 8359 || (param->flags & SPR_APTPL)) { 8360 int bit_ptr; 8361 8362 if (param->flags & SPR_APTPL) 8363 bit_ptr = 0; 8364 else if (param->flags & SPR_ALL_TG_PT) 8365 bit_ptr = 2; 8366 else /* SPR_SPEC_I_PT */ 8367 bit_ptr = 3; 8368 8369 free(ctsio->kern_data_ptr, M_CTL); 8370 ctl_set_invalid_field(ctsio, 8371 /*sks_valid*/ 1, 8372 /*command*/ 0, 8373 /*field*/ 20, 8374 /*bit_valid*/ 1, 8375 /*bit*/ bit_ptr); 8376 ctl_done((union ctl_io *)ctsio); 8377 return (CTL_RETVAL_COMPLETE); 8378 } 8379 8380 mtx_lock(&lun->lun_lock); 8381 8382 /* 8383 * The initiator wants to clear the 8384 * key/unregister. 8385 */ 8386 if (sa_res_key == 0) { 8387 if ((res_key == 0 8388 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8389 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8390 && lun->pr_keys[residx] == 0)) { 8391 mtx_unlock(&lun->lun_lock); 8392 goto done; 8393 } 8394 8395 lun->pr_keys[residx] = 0; 8396 lun->pr_key_count--; 8397 8398 if (residx == lun->pr_res_idx) { 8399 lun->flags &= ~CTL_LUN_PR_RESERVED; 8400 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8401 8402 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8403 || lun->res_type == SPR_TYPE_EX_AC_RO) 8404 && lun->pr_key_count) { 8405 /* 8406 * If the reservation is a registrants 8407 * only type we need to generate a UA 8408 * for other registered inits. The 8409 * sense code should be RESERVATIONS 8410 * RELEASED 8411 */ 8412 8413 for (i = 0; i < CTL_MAX_INITIATORS;i++){ 8414 if (lun->pr_keys[i + 8415 softc->persis_offset] == 0) 8416 continue; 8417 lun->pending_ua[i] |= 8418 CTL_UA_RES_RELEASE; 8419 } 8420 } 8421 lun->res_type = 0; 8422 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8423 if (lun->pr_key_count==0) { 8424 lun->flags &= ~CTL_LUN_PR_RESERVED; 8425 lun->res_type = 0; 8426 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8427 } 8428 } 8429 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8430 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8431 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8432 persis_io.pr.pr_info.residx = residx; 8433 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8434 &persis_io, sizeof(persis_io), 0 )) > 8435 CTL_HA_STATUS_SUCCESS) { 8436 printf("CTL:Persis Out error returned from " 8437 "ctl_ha_msg_send %d\n", isc_retval); 8438 } 8439 } else /* sa_res_key != 0 */ { 8440 8441 /* 8442 * If we aren't registered currently then increment 8443 * the key count and set the registered flag. 8444 */ 8445 if (lun->pr_keys[residx] == 0) 8446 lun->pr_key_count++; 8447 lun->pr_keys[residx] = sa_res_key; 8448 8449 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8450 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8451 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8452 persis_io.pr.pr_info.residx = residx; 8453 memcpy(persis_io.pr.pr_info.sa_res_key, 8454 param->serv_act_res_key, 8455 sizeof(param->serv_act_res_key)); 8456 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8457 &persis_io, sizeof(persis_io), 0)) > 8458 CTL_HA_STATUS_SUCCESS) { 8459 printf("CTL:Persis Out error returned from " 8460 "ctl_ha_msg_send %d\n", isc_retval); 8461 } 8462 } 8463 lun->PRGeneration++; 8464 mtx_unlock(&lun->lun_lock); 8465 8466 break; 8467 } 8468 case SPRO_RESERVE: 8469#if 0 8470 printf("Reserve executed type %d\n", type); 8471#endif 8472 mtx_lock(&lun->lun_lock); 8473 if (lun->flags & CTL_LUN_PR_RESERVED) { 8474 /* 8475 * if this isn't the reservation holder and it's 8476 * not a "all registrants" type or if the type is 8477 * different then we have a conflict 8478 */ 8479 if ((lun->pr_res_idx != residx 8480 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8481 || lun->res_type != type) { 8482 mtx_unlock(&lun->lun_lock); 8483 free(ctsio->kern_data_ptr, M_CTL); 8484 ctl_set_reservation_conflict(ctsio); 8485 ctl_done((union ctl_io *)ctsio); 8486 return (CTL_RETVAL_COMPLETE); 8487 } 8488 mtx_unlock(&lun->lun_lock); 8489 } else /* create a reservation */ { 8490 /* 8491 * If it's not an "all registrants" type record 8492 * reservation holder 8493 */ 8494 if (type != SPR_TYPE_WR_EX_AR 8495 && type != SPR_TYPE_EX_AC_AR) 8496 lun->pr_res_idx = residx; /* Res holder */ 8497 else 8498 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8499 8500 lun->flags |= CTL_LUN_PR_RESERVED; 8501 lun->res_type = type; 8502 8503 mtx_unlock(&lun->lun_lock); 8504 8505 /* send msg to other side */ 8506 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8507 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8508 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8509 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8510 persis_io.pr.pr_info.res_type = type; 8511 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 8512 &persis_io, sizeof(persis_io), 0)) > 8513 CTL_HA_STATUS_SUCCESS) { 8514 printf("CTL:Persis Out error returned from " 8515 "ctl_ha_msg_send %d\n", isc_retval); 8516 } 8517 } 8518 break; 8519 8520 case SPRO_RELEASE: 8521 mtx_lock(&lun->lun_lock); 8522 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8523 /* No reservation exists return good status */ 8524 mtx_unlock(&lun->lun_lock); 8525 goto done; 8526 } 8527 /* 8528 * Is this nexus a reservation holder? 8529 */ 8530 if (lun->pr_res_idx != residx 8531 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8532 /* 8533 * not a res holder return good status but 8534 * do nothing 8535 */ 8536 mtx_unlock(&lun->lun_lock); 8537 goto done; 8538 } 8539 8540 if (lun->res_type != type) { 8541 mtx_unlock(&lun->lun_lock); 8542 free(ctsio->kern_data_ptr, M_CTL); 8543 ctl_set_illegal_pr_release(ctsio); 8544 ctl_done((union ctl_io *)ctsio); 8545 return (CTL_RETVAL_COMPLETE); 8546 } 8547 8548 /* okay to release */ 8549 lun->flags &= ~CTL_LUN_PR_RESERVED; 8550 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8551 lun->res_type = 0; 8552 8553 /* 8554 * if this isn't an exclusive access 8555 * res generate UA for all other 8556 * registrants. 8557 */ 8558 if (type != SPR_TYPE_EX_AC 8559 && type != SPR_TYPE_WR_EX) { 8560 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8561 if (i == residx || 8562 lun->pr_keys[i + softc->persis_offset] == 0) 8563 continue; 8564 lun->pending_ua[i] |= CTL_UA_RES_RELEASE; 8565 } 8566 } 8567 mtx_unlock(&lun->lun_lock); 8568 /* Send msg to other side */ 8569 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8570 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8571 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8572 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io, 8573 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8574 printf("CTL:Persis Out error returned from " 8575 "ctl_ha_msg_send %d\n", isc_retval); 8576 } 8577 break; 8578 8579 case SPRO_CLEAR: 8580 /* send msg to other side */ 8581 8582 mtx_lock(&lun->lun_lock); 8583 lun->flags &= ~CTL_LUN_PR_RESERVED; 8584 lun->res_type = 0; 8585 lun->pr_key_count = 0; 8586 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8587 8588 lun->pr_keys[residx] = 0; 8589 8590 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) 8591 if (lun->pr_keys[i] != 0) { 8592 lun->pr_keys[i] = 0; 8593 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8594 } 8595 lun->PRGeneration++; 8596 mtx_unlock(&lun->lun_lock); 8597 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8598 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8599 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8600 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8601 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) { 8602 printf("CTL:Persis Out error returned from " 8603 "ctl_ha_msg_send %d\n", isc_retval); 8604 } 8605 break; 8606 8607 case SPRO_PREEMPT: 8608 case SPRO_PRE_ABO: { 8609 int nretval; 8610 8611 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8612 residx, ctsio, cdb, param); 8613 if (nretval != 0) 8614 return (CTL_RETVAL_COMPLETE); 8615 break; 8616 } 8617 default: 8618 panic("Invalid PR type %x", cdb->action); 8619 } 8620 8621done: 8622 free(ctsio->kern_data_ptr, M_CTL); 8623 ctl_set_success(ctsio); 8624 ctl_done((union ctl_io *)ctsio); 8625 8626 return (retval); 8627} 8628 8629/* 8630 * This routine is for handling a message from the other SC pertaining to 8631 * persistent reserve out. All the error checking will have been done 8632 * so only perorming the action need be done here to keep the two 8633 * in sync. 8634 */ 8635static void 8636ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8637{ 8638 struct ctl_lun *lun; 8639 struct ctl_softc *softc; 8640 int i; 8641 uint32_t targ_lun; 8642 8643 softc = control_softc; 8644 8645 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8646 lun = softc->ctl_luns[targ_lun]; 8647 mtx_lock(&lun->lun_lock); 8648 switch(msg->pr.pr_info.action) { 8649 case CTL_PR_REG_KEY: 8650 if (lun->pr_keys[msg->pr.pr_info.residx] == 0) 8651 lun->pr_key_count++; 8652 lun->pr_keys[msg->pr.pr_info.residx] = 8653 scsi_8btou64(msg->pr.pr_info.sa_res_key); 8654 lun->PRGeneration++; 8655 break; 8656 8657 case CTL_PR_UNREG_KEY: 8658 lun->pr_keys[msg->pr.pr_info.residx] = 0; 8659 lun->pr_key_count--; 8660 8661 /* XXX Need to see if the reservation has been released */ 8662 /* if so do we need to generate UA? */ 8663 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8664 lun->flags &= ~CTL_LUN_PR_RESERVED; 8665 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8666 8667 if ((lun->res_type == SPR_TYPE_WR_EX_RO 8668 || lun->res_type == SPR_TYPE_EX_AC_RO) 8669 && lun->pr_key_count) { 8670 /* 8671 * If the reservation is a registrants 8672 * only type we need to generate a UA 8673 * for other registered inits. The 8674 * sense code should be RESERVATIONS 8675 * RELEASED 8676 */ 8677 8678 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8679 if (lun->pr_keys[i + 8680 softc->persis_offset] == 0) 8681 continue; 8682 8683 lun->pending_ua[i] |= 8684 CTL_UA_RES_RELEASE; 8685 } 8686 } 8687 lun->res_type = 0; 8688 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8689 if (lun->pr_key_count==0) { 8690 lun->flags &= ~CTL_LUN_PR_RESERVED; 8691 lun->res_type = 0; 8692 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8693 } 8694 } 8695 lun->PRGeneration++; 8696 break; 8697 8698 case CTL_PR_RESERVE: 8699 lun->flags |= CTL_LUN_PR_RESERVED; 8700 lun->res_type = msg->pr.pr_info.res_type; 8701 lun->pr_res_idx = msg->pr.pr_info.residx; 8702 8703 break; 8704 8705 case CTL_PR_RELEASE: 8706 /* 8707 * if this isn't an exclusive access res generate UA for all 8708 * other registrants. 8709 */ 8710 if (lun->res_type != SPR_TYPE_EX_AC 8711 && lun->res_type != SPR_TYPE_WR_EX) { 8712 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8713 if (lun->pr_keys[i + softc->persis_offset] != 0) 8714 lun->pending_ua[i] |= 8715 CTL_UA_RES_RELEASE; 8716 } 8717 8718 lun->flags &= ~CTL_LUN_PR_RESERVED; 8719 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8720 lun->res_type = 0; 8721 break; 8722 8723 case CTL_PR_PREEMPT: 8724 ctl_pro_preempt_other(lun, msg); 8725 break; 8726 case CTL_PR_CLEAR: 8727 lun->flags &= ~CTL_LUN_PR_RESERVED; 8728 lun->res_type = 0; 8729 lun->pr_key_count = 0; 8730 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8731 8732 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) { 8733 if (lun->pr_keys[i] == 0) 8734 continue; 8735 lun->pr_keys[i] = 0; 8736 ctl_set_res_ua(lun, i, CTL_UA_REG_PREEMPT); 8737 } 8738 lun->PRGeneration++; 8739 break; 8740 } 8741 8742 mtx_unlock(&lun->lun_lock); 8743} 8744 8745int 8746ctl_read_write(struct ctl_scsiio *ctsio) 8747{ 8748 struct ctl_lun *lun; 8749 struct ctl_lba_len_flags *lbalen; 8750 uint64_t lba; 8751 uint32_t num_blocks; 8752 int flags, retval; 8753 int isread; 8754 8755 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8756 8757 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8758 8759 flags = 0; 8760 retval = CTL_RETVAL_COMPLETE; 8761 8762 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8763 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8764 switch (ctsio->cdb[0]) { 8765 case READ_6: 8766 case WRITE_6: { 8767 struct scsi_rw_6 *cdb; 8768 8769 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8770 8771 lba = scsi_3btoul(cdb->addr); 8772 /* only 5 bits are valid in the most significant address byte */ 8773 lba &= 0x1fffff; 8774 num_blocks = cdb->length; 8775 /* 8776 * This is correct according to SBC-2. 8777 */ 8778 if (num_blocks == 0) 8779 num_blocks = 256; 8780 break; 8781 } 8782 case READ_10: 8783 case WRITE_10: { 8784 struct scsi_rw_10 *cdb; 8785 8786 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8787 if (cdb->byte2 & SRW10_FUA) 8788 flags |= CTL_LLF_FUA; 8789 if (cdb->byte2 & SRW10_DPO) 8790 flags |= CTL_LLF_DPO; 8791 lba = scsi_4btoul(cdb->addr); 8792 num_blocks = scsi_2btoul(cdb->length); 8793 break; 8794 } 8795 case WRITE_VERIFY_10: { 8796 struct scsi_write_verify_10 *cdb; 8797 8798 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8799 flags |= CTL_LLF_FUA; 8800 if (cdb->byte2 & SWV_DPO) 8801 flags |= CTL_LLF_DPO; 8802 lba = scsi_4btoul(cdb->addr); 8803 num_blocks = scsi_2btoul(cdb->length); 8804 break; 8805 } 8806 case READ_12: 8807 case WRITE_12: { 8808 struct scsi_rw_12 *cdb; 8809 8810 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8811 if (cdb->byte2 & SRW12_FUA) 8812 flags |= CTL_LLF_FUA; 8813 if (cdb->byte2 & SRW12_DPO) 8814 flags |= CTL_LLF_DPO; 8815 lba = scsi_4btoul(cdb->addr); 8816 num_blocks = scsi_4btoul(cdb->length); 8817 break; 8818 } 8819 case WRITE_VERIFY_12: { 8820 struct scsi_write_verify_12 *cdb; 8821 8822 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8823 flags |= CTL_LLF_FUA; 8824 if (cdb->byte2 & SWV_DPO) 8825 flags |= CTL_LLF_DPO; 8826 lba = scsi_4btoul(cdb->addr); 8827 num_blocks = scsi_4btoul(cdb->length); 8828 break; 8829 } 8830 case READ_16: 8831 case WRITE_16: { 8832 struct scsi_rw_16 *cdb; 8833 8834 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8835 if (cdb->byte2 & SRW12_FUA) 8836 flags |= CTL_LLF_FUA; 8837 if (cdb->byte2 & SRW12_DPO) 8838 flags |= CTL_LLF_DPO; 8839 lba = scsi_8btou64(cdb->addr); 8840 num_blocks = scsi_4btoul(cdb->length); 8841 break; 8842 } 8843 case WRITE_ATOMIC_16: { 8844 struct scsi_rw_16 *cdb; 8845 8846 if (lun->be_lun->atomicblock == 0) { 8847 ctl_set_invalid_opcode(ctsio); 8848 ctl_done((union ctl_io *)ctsio); 8849 return (CTL_RETVAL_COMPLETE); 8850 } 8851 8852 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8853 if (cdb->byte2 & SRW12_FUA) 8854 flags |= CTL_LLF_FUA; 8855 if (cdb->byte2 & SRW12_DPO) 8856 flags |= CTL_LLF_DPO; 8857 lba = scsi_8btou64(cdb->addr); 8858 num_blocks = scsi_4btoul(cdb->length); 8859 if (num_blocks > lun->be_lun->atomicblock) { 8860 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8861 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8862 /*bit*/ 0); 8863 ctl_done((union ctl_io *)ctsio); 8864 return (CTL_RETVAL_COMPLETE); 8865 } 8866 break; 8867 } 8868 case WRITE_VERIFY_16: { 8869 struct scsi_write_verify_16 *cdb; 8870 8871 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8872 flags |= CTL_LLF_FUA; 8873 if (cdb->byte2 & SWV_DPO) 8874 flags |= CTL_LLF_DPO; 8875 lba = scsi_8btou64(cdb->addr); 8876 num_blocks = scsi_4btoul(cdb->length); 8877 break; 8878 } 8879 default: 8880 /* 8881 * We got a command we don't support. This shouldn't 8882 * happen, commands should be filtered out above us. 8883 */ 8884 ctl_set_invalid_opcode(ctsio); 8885 ctl_done((union ctl_io *)ctsio); 8886 8887 return (CTL_RETVAL_COMPLETE); 8888 break; /* NOTREACHED */ 8889 } 8890 8891 /* 8892 * The first check is to make sure we're in bounds, the second 8893 * check is to catch wrap-around problems. If the lba + num blocks 8894 * is less than the lba, then we've wrapped around and the block 8895 * range is invalid anyway. 8896 */ 8897 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8898 || ((lba + num_blocks) < lba)) { 8899 ctl_set_lba_out_of_range(ctsio); 8900 ctl_done((union ctl_io *)ctsio); 8901 return (CTL_RETVAL_COMPLETE); 8902 } 8903 8904 /* 8905 * According to SBC-3, a transfer length of 0 is not an error. 8906 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8907 * translates to 256 blocks for those commands. 8908 */ 8909 if (num_blocks == 0) { 8910 ctl_set_success(ctsio); 8911 ctl_done((union ctl_io *)ctsio); 8912 return (CTL_RETVAL_COMPLETE); 8913 } 8914 8915 /* Set FUA and/or DPO if caches are disabled. */ 8916 if (isread) { 8917 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8918 SCP_RCD) != 0) 8919 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8920 } else { 8921 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8922 SCP_WCE) == 0) 8923 flags |= CTL_LLF_FUA; 8924 } 8925 8926 lbalen = (struct ctl_lba_len_flags *) 8927 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8928 lbalen->lba = lba; 8929 lbalen->len = num_blocks; 8930 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8931 8932 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8933 ctsio->kern_rel_offset = 0; 8934 8935 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8936 8937 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8938 8939 return (retval); 8940} 8941 8942static int 8943ctl_cnw_cont(union ctl_io *io) 8944{ 8945 struct ctl_scsiio *ctsio; 8946 struct ctl_lun *lun; 8947 struct ctl_lba_len_flags *lbalen; 8948 int retval; 8949 8950 ctsio = &io->scsiio; 8951 ctsio->io_hdr.status = CTL_STATUS_NONE; 8952 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8953 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8954 lbalen = (struct ctl_lba_len_flags *) 8955 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8956 lbalen->flags &= ~CTL_LLF_COMPARE; 8957 lbalen->flags |= CTL_LLF_WRITE; 8958 8959 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8960 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8961 return (retval); 8962} 8963 8964int 8965ctl_cnw(struct ctl_scsiio *ctsio) 8966{ 8967 struct ctl_lun *lun; 8968 struct ctl_lba_len_flags *lbalen; 8969 uint64_t lba; 8970 uint32_t num_blocks; 8971 int flags, retval; 8972 8973 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8974 8975 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8976 8977 flags = 0; 8978 retval = CTL_RETVAL_COMPLETE; 8979 8980 switch (ctsio->cdb[0]) { 8981 case COMPARE_AND_WRITE: { 8982 struct scsi_compare_and_write *cdb; 8983 8984 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8985 if (cdb->byte2 & SRW10_FUA) 8986 flags |= CTL_LLF_FUA; 8987 if (cdb->byte2 & SRW10_DPO) 8988 flags |= CTL_LLF_DPO; 8989 lba = scsi_8btou64(cdb->addr); 8990 num_blocks = cdb->length; 8991 break; 8992 } 8993 default: 8994 /* 8995 * We got a command we don't support. This shouldn't 8996 * happen, commands should be filtered out above us. 8997 */ 8998 ctl_set_invalid_opcode(ctsio); 8999 ctl_done((union ctl_io *)ctsio); 9000 9001 return (CTL_RETVAL_COMPLETE); 9002 break; /* NOTREACHED */ 9003 } 9004 9005 /* 9006 * The first check is to make sure we're in bounds, the second 9007 * check is to catch wrap-around problems. If the lba + num blocks 9008 * is less than the lba, then we've wrapped around and the block 9009 * range is invalid anyway. 9010 */ 9011 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9012 || ((lba + num_blocks) < lba)) { 9013 ctl_set_lba_out_of_range(ctsio); 9014 ctl_done((union ctl_io *)ctsio); 9015 return (CTL_RETVAL_COMPLETE); 9016 } 9017 9018 /* 9019 * According to SBC-3, a transfer length of 0 is not an error. 9020 */ 9021 if (num_blocks == 0) { 9022 ctl_set_success(ctsio); 9023 ctl_done((union ctl_io *)ctsio); 9024 return (CTL_RETVAL_COMPLETE); 9025 } 9026 9027 /* Set FUA if write cache is disabled. */ 9028 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 9029 SCP_WCE) == 0) 9030 flags |= CTL_LLF_FUA; 9031 9032 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 9033 ctsio->kern_rel_offset = 0; 9034 9035 /* 9036 * Set the IO_CONT flag, so that if this I/O gets passed to 9037 * ctl_data_submit_done(), it'll get passed back to 9038 * ctl_ctl_cnw_cont() for further processing. 9039 */ 9040 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 9041 ctsio->io_cont = ctl_cnw_cont; 9042 9043 lbalen = (struct ctl_lba_len_flags *) 9044 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9045 lbalen->lba = lba; 9046 lbalen->len = num_blocks; 9047 lbalen->flags = CTL_LLF_COMPARE | flags; 9048 9049 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 9050 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9051 return (retval); 9052} 9053 9054int 9055ctl_verify(struct ctl_scsiio *ctsio) 9056{ 9057 struct ctl_lun *lun; 9058 struct ctl_lba_len_flags *lbalen; 9059 uint64_t lba; 9060 uint32_t num_blocks; 9061 int bytchk, flags; 9062 int retval; 9063 9064 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9065 9066 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 9067 9068 bytchk = 0; 9069 flags = CTL_LLF_FUA; 9070 retval = CTL_RETVAL_COMPLETE; 9071 9072 switch (ctsio->cdb[0]) { 9073 case VERIFY_10: { 9074 struct scsi_verify_10 *cdb; 9075 9076 cdb = (struct scsi_verify_10 *)ctsio->cdb; 9077 if (cdb->byte2 & SVFY_BYTCHK) 9078 bytchk = 1; 9079 if (cdb->byte2 & SVFY_DPO) 9080 flags |= CTL_LLF_DPO; 9081 lba = scsi_4btoul(cdb->addr); 9082 num_blocks = scsi_2btoul(cdb->length); 9083 break; 9084 } 9085 case VERIFY_12: { 9086 struct scsi_verify_12 *cdb; 9087 9088 cdb = (struct scsi_verify_12 *)ctsio->cdb; 9089 if (cdb->byte2 & SVFY_BYTCHK) 9090 bytchk = 1; 9091 if (cdb->byte2 & SVFY_DPO) 9092 flags |= CTL_LLF_DPO; 9093 lba = scsi_4btoul(cdb->addr); 9094 num_blocks = scsi_4btoul(cdb->length); 9095 break; 9096 } 9097 case VERIFY_16: { 9098 struct scsi_rw_16 *cdb; 9099 9100 cdb = (struct scsi_rw_16 *)ctsio->cdb; 9101 if (cdb->byte2 & SVFY_BYTCHK) 9102 bytchk = 1; 9103 if (cdb->byte2 & SVFY_DPO) 9104 flags |= CTL_LLF_DPO; 9105 lba = scsi_8btou64(cdb->addr); 9106 num_blocks = scsi_4btoul(cdb->length); 9107 break; 9108 } 9109 default: 9110 /* 9111 * We got a command we don't support. This shouldn't 9112 * happen, commands should be filtered out above us. 9113 */ 9114 ctl_set_invalid_opcode(ctsio); 9115 ctl_done((union ctl_io *)ctsio); 9116 return (CTL_RETVAL_COMPLETE); 9117 } 9118 9119 /* 9120 * The first check is to make sure we're in bounds, the second 9121 * check is to catch wrap-around problems. If the lba + num blocks 9122 * is less than the lba, then we've wrapped around and the block 9123 * range is invalid anyway. 9124 */ 9125 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 9126 || ((lba + num_blocks) < lba)) { 9127 ctl_set_lba_out_of_range(ctsio); 9128 ctl_done((union ctl_io *)ctsio); 9129 return (CTL_RETVAL_COMPLETE); 9130 } 9131 9132 /* 9133 * According to SBC-3, a transfer length of 0 is not an error. 9134 */ 9135 if (num_blocks == 0) { 9136 ctl_set_success(ctsio); 9137 ctl_done((union ctl_io *)ctsio); 9138 return (CTL_RETVAL_COMPLETE); 9139 } 9140 9141 lbalen = (struct ctl_lba_len_flags *) 9142 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 9143 lbalen->lba = lba; 9144 lbalen->len = num_blocks; 9145 if (bytchk) { 9146 lbalen->flags = CTL_LLF_COMPARE | flags; 9147 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 9148 } else { 9149 lbalen->flags = CTL_LLF_VERIFY | flags; 9150 ctsio->kern_total_len = 0; 9151 } 9152 ctsio->kern_rel_offset = 0; 9153 9154 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 9155 retval = lun->backend->data_submit((union ctl_io *)ctsio); 9156 return (retval); 9157} 9158 9159int 9160ctl_report_luns(struct ctl_scsiio *ctsio) 9161{ 9162 struct scsi_report_luns *cdb; 9163 struct scsi_report_luns_data *lun_data; 9164 struct ctl_lun *lun, *request_lun; 9165 int num_luns, retval; 9166 uint32_t alloc_len, lun_datalen; 9167 int num_filled, well_known; 9168 uint32_t initidx, targ_lun_id, lun_id; 9169 9170 retval = CTL_RETVAL_COMPLETE; 9171 well_known = 0; 9172 9173 cdb = (struct scsi_report_luns *)ctsio->cdb; 9174 9175 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 9176 9177 mtx_lock(&control_softc->ctl_lock); 9178 num_luns = control_softc->num_luns; 9179 mtx_unlock(&control_softc->ctl_lock); 9180 9181 switch (cdb->select_report) { 9182 case RPL_REPORT_DEFAULT: 9183 case RPL_REPORT_ALL: 9184 break; 9185 case RPL_REPORT_WELLKNOWN: 9186 well_known = 1; 9187 num_luns = 0; 9188 break; 9189 default: 9190 ctl_set_invalid_field(ctsio, 9191 /*sks_valid*/ 1, 9192 /*command*/ 1, 9193 /*field*/ 2, 9194 /*bit_valid*/ 0, 9195 /*bit*/ 0); 9196 ctl_done((union ctl_io *)ctsio); 9197 return (retval); 9198 break; /* NOTREACHED */ 9199 } 9200 9201 alloc_len = scsi_4btoul(cdb->length); 9202 /* 9203 * The initiator has to allocate at least 16 bytes for this request, 9204 * so he can at least get the header and the first LUN. Otherwise 9205 * we reject the request (per SPC-3 rev 14, section 6.21). 9206 */ 9207 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9208 sizeof(struct scsi_report_luns_lundata))) { 9209 ctl_set_invalid_field(ctsio, 9210 /*sks_valid*/ 1, 9211 /*command*/ 1, 9212 /*field*/ 6, 9213 /*bit_valid*/ 0, 9214 /*bit*/ 0); 9215 ctl_done((union ctl_io *)ctsio); 9216 return (retval); 9217 } 9218 9219 request_lun = (struct ctl_lun *) 9220 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9221 9222 lun_datalen = sizeof(*lun_data) + 9223 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9224 9225 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9226 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9227 ctsio->kern_sg_entries = 0; 9228 9229 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9230 9231 mtx_lock(&control_softc->ctl_lock); 9232 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9233 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id); 9234 if (lun_id >= CTL_MAX_LUNS) 9235 continue; 9236 lun = control_softc->ctl_luns[lun_id]; 9237 if (lun == NULL) 9238 continue; 9239 9240 if (targ_lun_id <= 0xff) { 9241 /* 9242 * Peripheral addressing method, bus number 0. 9243 */ 9244 lun_data->luns[num_filled].lundata[0] = 9245 RPL_LUNDATA_ATYP_PERIPH; 9246 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9247 num_filled++; 9248 } else if (targ_lun_id <= 0x3fff) { 9249 /* 9250 * Flat addressing method. 9251 */ 9252 lun_data->luns[num_filled].lundata[0] = 9253 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9254 lun_data->luns[num_filled].lundata[1] = 9255 (targ_lun_id & 0xff); 9256 num_filled++; 9257 } else if (targ_lun_id <= 0xffffff) { 9258 /* 9259 * Extended flat addressing method. 9260 */ 9261 lun_data->luns[num_filled].lundata[0] = 9262 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9263 scsi_ulto3b(targ_lun_id, 9264 &lun_data->luns[num_filled].lundata[1]); 9265 num_filled++; 9266 } else { 9267 printf("ctl_report_luns: bogus LUN number %jd, " 9268 "skipping\n", (intmax_t)targ_lun_id); 9269 } 9270 /* 9271 * According to SPC-3, rev 14 section 6.21: 9272 * 9273 * "The execution of a REPORT LUNS command to any valid and 9274 * installed logical unit shall clear the REPORTED LUNS DATA 9275 * HAS CHANGED unit attention condition for all logical 9276 * units of that target with respect to the requesting 9277 * initiator. A valid and installed logical unit is one 9278 * having a PERIPHERAL QUALIFIER of 000b in the standard 9279 * INQUIRY data (see 6.4.2)." 9280 * 9281 * If request_lun is NULL, the LUN this report luns command 9282 * was issued to is either disabled or doesn't exist. In that 9283 * case, we shouldn't clear any pending lun change unit 9284 * attention. 9285 */ 9286 if (request_lun != NULL) { 9287 mtx_lock(&lun->lun_lock); 9288 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE; 9289 mtx_unlock(&lun->lun_lock); 9290 } 9291 } 9292 mtx_unlock(&control_softc->ctl_lock); 9293 9294 /* 9295 * It's quite possible that we've returned fewer LUNs than we allocated 9296 * space for. Trim it. 9297 */ 9298 lun_datalen = sizeof(*lun_data) + 9299 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9300 9301 if (lun_datalen < alloc_len) { 9302 ctsio->residual = alloc_len - lun_datalen; 9303 ctsio->kern_data_len = lun_datalen; 9304 ctsio->kern_total_len = lun_datalen; 9305 } else { 9306 ctsio->residual = 0; 9307 ctsio->kern_data_len = alloc_len; 9308 ctsio->kern_total_len = alloc_len; 9309 } 9310 ctsio->kern_data_resid = 0; 9311 ctsio->kern_rel_offset = 0; 9312 ctsio->kern_sg_entries = 0; 9313 9314 /* 9315 * We set this to the actual data length, regardless of how much 9316 * space we actually have to return results. If the user looks at 9317 * this value, he'll know whether or not he allocated enough space 9318 * and reissue the command if necessary. We don't support well 9319 * known logical units, so if the user asks for that, return none. 9320 */ 9321 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9322 9323 /* 9324 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9325 * this request. 9326 */ 9327 ctl_set_success(ctsio); 9328 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9329 ctsio->be_move_done = ctl_config_move_done; 9330 ctl_datamove((union ctl_io *)ctsio); 9331 return (retval); 9332} 9333 9334int 9335ctl_request_sense(struct ctl_scsiio *ctsio) 9336{ 9337 struct scsi_request_sense *cdb; 9338 struct scsi_sense_data *sense_ptr; 9339 struct ctl_lun *lun; 9340 uint32_t initidx; 9341 int have_error; 9342 scsi_sense_data_type sense_format; 9343 9344 cdb = (struct scsi_request_sense *)ctsio->cdb; 9345 9346 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9347 9348 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9349 9350 /* 9351 * Determine which sense format the user wants. 9352 */ 9353 if (cdb->byte2 & SRS_DESC) 9354 sense_format = SSD_TYPE_DESC; 9355 else 9356 sense_format = SSD_TYPE_FIXED; 9357 9358 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9359 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9360 ctsio->kern_sg_entries = 0; 9361 9362 /* 9363 * struct scsi_sense_data, which is currently set to 256 bytes, is 9364 * larger than the largest allowed value for the length field in the 9365 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9366 */ 9367 ctsio->residual = 0; 9368 ctsio->kern_data_len = cdb->length; 9369 ctsio->kern_total_len = cdb->length; 9370 9371 ctsio->kern_data_resid = 0; 9372 ctsio->kern_rel_offset = 0; 9373 ctsio->kern_sg_entries = 0; 9374 9375 /* 9376 * If we don't have a LUN, we don't have any pending sense. 9377 */ 9378 if (lun == NULL) 9379 goto no_sense; 9380 9381 have_error = 0; 9382 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9383 /* 9384 * Check for pending sense, and then for pending unit attentions. 9385 * Pending sense gets returned first, then pending unit attentions. 9386 */ 9387 mtx_lock(&lun->lun_lock); 9388#ifdef CTL_WITH_CA 9389 if (ctl_is_set(lun->have_ca, initidx)) { 9390 scsi_sense_data_type stored_format; 9391 9392 /* 9393 * Check to see which sense format was used for the stored 9394 * sense data. 9395 */ 9396 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9397 9398 /* 9399 * If the user requested a different sense format than the 9400 * one we stored, then we need to convert it to the other 9401 * format. If we're going from descriptor to fixed format 9402 * sense data, we may lose things in translation, depending 9403 * on what options were used. 9404 * 9405 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9406 * for some reason we'll just copy it out as-is. 9407 */ 9408 if ((stored_format == SSD_TYPE_FIXED) 9409 && (sense_format == SSD_TYPE_DESC)) 9410 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9411 &lun->pending_sense[initidx], 9412 (struct scsi_sense_data_desc *)sense_ptr); 9413 else if ((stored_format == SSD_TYPE_DESC) 9414 && (sense_format == SSD_TYPE_FIXED)) 9415 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9416 &lun->pending_sense[initidx], 9417 (struct scsi_sense_data_fixed *)sense_ptr); 9418 else 9419 memcpy(sense_ptr, &lun->pending_sense[initidx], 9420 ctl_min(sizeof(*sense_ptr), 9421 sizeof(lun->pending_sense[initidx]))); 9422 9423 ctl_clear_mask(lun->have_ca, initidx); 9424 have_error = 1; 9425 } else 9426#endif 9427 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 9428 ctl_ua_type ua_type; 9429 9430 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 9431 sense_ptr, sense_format); 9432 if (ua_type != CTL_UA_NONE) 9433 have_error = 1; 9434 } 9435 mtx_unlock(&lun->lun_lock); 9436 9437 /* 9438 * We already have a pending error, return it. 9439 */ 9440 if (have_error != 0) { 9441 /* 9442 * We report the SCSI status as OK, since the status of the 9443 * request sense command itself is OK. 9444 * We report 0 for the sense length, because we aren't doing 9445 * autosense in this case. We're reporting sense as 9446 * parameter data. 9447 */ 9448 ctl_set_success(ctsio); 9449 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9450 ctsio->be_move_done = ctl_config_move_done; 9451 ctl_datamove((union ctl_io *)ctsio); 9452 return (CTL_RETVAL_COMPLETE); 9453 } 9454 9455no_sense: 9456 9457 /* 9458 * No sense information to report, so we report that everything is 9459 * okay. 9460 */ 9461 ctl_set_sense_data(sense_ptr, 9462 lun, 9463 sense_format, 9464 /*current_error*/ 1, 9465 /*sense_key*/ SSD_KEY_NO_SENSE, 9466 /*asc*/ 0x00, 9467 /*ascq*/ 0x00, 9468 SSD_ELEM_NONE); 9469 9470 /* 9471 * We report 0 for the sense length, because we aren't doing 9472 * autosense in this case. We're reporting sense as parameter data. 9473 */ 9474 ctl_set_success(ctsio); 9475 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9476 ctsio->be_move_done = ctl_config_move_done; 9477 ctl_datamove((union ctl_io *)ctsio); 9478 return (CTL_RETVAL_COMPLETE); 9479} 9480 9481int 9482ctl_tur(struct ctl_scsiio *ctsio) 9483{ 9484 9485 CTL_DEBUG_PRINT(("ctl_tur\n")); 9486 9487 ctl_set_success(ctsio); 9488 ctl_done((union ctl_io *)ctsio); 9489 9490 return (CTL_RETVAL_COMPLETE); 9491} 9492 9493#ifdef notyet 9494static int 9495ctl_cmddt_inquiry(struct ctl_scsiio *ctsio) 9496{ 9497 9498} 9499#endif 9500 9501static int 9502ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9503{ 9504 struct scsi_vpd_supported_pages *pages; 9505 int sup_page_size; 9506 struct ctl_lun *lun; 9507 9508 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9509 9510 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9511 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9512 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9513 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9514 ctsio->kern_sg_entries = 0; 9515 9516 if (sup_page_size < alloc_len) { 9517 ctsio->residual = alloc_len - sup_page_size; 9518 ctsio->kern_data_len = sup_page_size; 9519 ctsio->kern_total_len = sup_page_size; 9520 } else { 9521 ctsio->residual = 0; 9522 ctsio->kern_data_len = alloc_len; 9523 ctsio->kern_total_len = alloc_len; 9524 } 9525 ctsio->kern_data_resid = 0; 9526 ctsio->kern_rel_offset = 0; 9527 ctsio->kern_sg_entries = 0; 9528 9529 /* 9530 * The control device is always connected. The disk device, on the 9531 * other hand, may not be online all the time. Need to change this 9532 * to figure out whether the disk device is actually online or not. 9533 */ 9534 if (lun != NULL) 9535 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9536 lun->be_lun->lun_type; 9537 else 9538 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9539 9540 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES; 9541 /* Supported VPD pages */ 9542 pages->page_list[0] = SVPD_SUPPORTED_PAGES; 9543 /* Serial Number */ 9544 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER; 9545 /* Device Identification */ 9546 pages->page_list[2] = SVPD_DEVICE_ID; 9547 /* Extended INQUIRY Data */ 9548 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA; 9549 /* Mode Page Policy */ 9550 pages->page_list[4] = SVPD_MODE_PAGE_POLICY; 9551 /* SCSI Ports */ 9552 pages->page_list[5] = SVPD_SCSI_PORTS; 9553 /* Third-party Copy */ 9554 pages->page_list[6] = SVPD_SCSI_TPC; 9555 /* Block limits */ 9556 pages->page_list[7] = SVPD_BLOCK_LIMITS; 9557 /* Block Device Characteristics */ 9558 pages->page_list[8] = SVPD_BDC; 9559 /* Logical Block Provisioning */ 9560 pages->page_list[9] = SVPD_LBP; 9561 9562 ctl_set_success(ctsio); 9563 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9564 ctsio->be_move_done = ctl_config_move_done; 9565 ctl_datamove((union ctl_io *)ctsio); 9566 return (CTL_RETVAL_COMPLETE); 9567} 9568 9569static int 9570ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9571{ 9572 struct scsi_vpd_unit_serial_number *sn_ptr; 9573 struct ctl_lun *lun; 9574 int data_len; 9575 9576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9577 9578 data_len = 4 + CTL_SN_LEN; 9579 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9580 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9581 if (data_len < alloc_len) { 9582 ctsio->residual = alloc_len - data_len; 9583 ctsio->kern_data_len = data_len; 9584 ctsio->kern_total_len = data_len; 9585 } else { 9586 ctsio->residual = 0; 9587 ctsio->kern_data_len = alloc_len; 9588 ctsio->kern_total_len = alloc_len; 9589 } 9590 ctsio->kern_data_resid = 0; 9591 ctsio->kern_rel_offset = 0; 9592 ctsio->kern_sg_entries = 0; 9593 9594 /* 9595 * The control device is always connected. The disk device, on the 9596 * other hand, may not be online all the time. Need to change this 9597 * to figure out whether the disk device is actually online or not. 9598 */ 9599 if (lun != NULL) 9600 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9601 lun->be_lun->lun_type; 9602 else 9603 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9604 9605 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9606 sn_ptr->length = CTL_SN_LEN; 9607 /* 9608 * If we don't have a LUN, we just leave the serial number as 9609 * all spaces. 9610 */ 9611 if (lun != NULL) { 9612 strncpy((char *)sn_ptr->serial_num, 9613 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9614 } else 9615 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9616 9617 ctl_set_success(ctsio); 9618 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9619 ctsio->be_move_done = ctl_config_move_done; 9620 ctl_datamove((union ctl_io *)ctsio); 9621 return (CTL_RETVAL_COMPLETE); 9622} 9623 9624 9625static int 9626ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9627{ 9628 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9629 struct ctl_lun *lun; 9630 int data_len; 9631 9632 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9633 9634 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9635 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9636 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9637 ctsio->kern_sg_entries = 0; 9638 9639 if (data_len < alloc_len) { 9640 ctsio->residual = alloc_len - data_len; 9641 ctsio->kern_data_len = data_len; 9642 ctsio->kern_total_len = data_len; 9643 } else { 9644 ctsio->residual = 0; 9645 ctsio->kern_data_len = alloc_len; 9646 ctsio->kern_total_len = alloc_len; 9647 } 9648 ctsio->kern_data_resid = 0; 9649 ctsio->kern_rel_offset = 0; 9650 ctsio->kern_sg_entries = 0; 9651 9652 /* 9653 * The control device is always connected. The disk device, on the 9654 * other hand, may not be online all the time. 9655 */ 9656 if (lun != NULL) 9657 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9658 lun->be_lun->lun_type; 9659 else 9660 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9661 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9662 eid_ptr->page_length = data_len - 4; 9663 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9664 eid_ptr->flags3 = SVPD_EID_V_SUP; 9665 9666 ctl_set_success(ctsio); 9667 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9668 ctsio->be_move_done = ctl_config_move_done; 9669 ctl_datamove((union ctl_io *)ctsio); 9670 return (CTL_RETVAL_COMPLETE); 9671} 9672 9673static int 9674ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9675{ 9676 struct scsi_vpd_mode_page_policy *mpp_ptr; 9677 struct ctl_lun *lun; 9678 int data_len; 9679 9680 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9681 9682 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9683 sizeof(struct scsi_vpd_mode_page_policy_descr); 9684 9685 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9686 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9687 ctsio->kern_sg_entries = 0; 9688 9689 if (data_len < alloc_len) { 9690 ctsio->residual = alloc_len - data_len; 9691 ctsio->kern_data_len = data_len; 9692 ctsio->kern_total_len = data_len; 9693 } else { 9694 ctsio->residual = 0; 9695 ctsio->kern_data_len = alloc_len; 9696 ctsio->kern_total_len = alloc_len; 9697 } 9698 ctsio->kern_data_resid = 0; 9699 ctsio->kern_rel_offset = 0; 9700 ctsio->kern_sg_entries = 0; 9701 9702 /* 9703 * The control device is always connected. The disk device, on the 9704 * other hand, may not be online all the time. 9705 */ 9706 if (lun != NULL) 9707 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9708 lun->be_lun->lun_type; 9709 else 9710 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9711 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9712 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9713 mpp_ptr->descr[0].page_code = 0x3f; 9714 mpp_ptr->descr[0].subpage_code = 0xff; 9715 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9716 9717 ctl_set_success(ctsio); 9718 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9719 ctsio->be_move_done = ctl_config_move_done; 9720 ctl_datamove((union ctl_io *)ctsio); 9721 return (CTL_RETVAL_COMPLETE); 9722} 9723 9724static int 9725ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9726{ 9727 struct scsi_vpd_device_id *devid_ptr; 9728 struct scsi_vpd_id_descriptor *desc; 9729 struct ctl_softc *ctl_softc; 9730 struct ctl_lun *lun; 9731 struct ctl_port *port; 9732 int data_len; 9733 uint8_t proto; 9734 9735 ctl_softc = control_softc; 9736 9737 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]; 9738 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9739 9740 data_len = sizeof(struct scsi_vpd_device_id) + 9741 sizeof(struct scsi_vpd_id_descriptor) + 9742 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9743 sizeof(struct scsi_vpd_id_descriptor) + 9744 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9745 if (lun && lun->lun_devid) 9746 data_len += lun->lun_devid->len; 9747 if (port->port_devid) 9748 data_len += port->port_devid->len; 9749 if (port->target_devid) 9750 data_len += port->target_devid->len; 9751 9752 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9753 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9754 ctsio->kern_sg_entries = 0; 9755 9756 if (data_len < alloc_len) { 9757 ctsio->residual = alloc_len - data_len; 9758 ctsio->kern_data_len = data_len; 9759 ctsio->kern_total_len = data_len; 9760 } else { 9761 ctsio->residual = 0; 9762 ctsio->kern_data_len = alloc_len; 9763 ctsio->kern_total_len = alloc_len; 9764 } 9765 ctsio->kern_data_resid = 0; 9766 ctsio->kern_rel_offset = 0; 9767 ctsio->kern_sg_entries = 0; 9768 9769 /* 9770 * The control device is always connected. The disk device, on the 9771 * other hand, may not be online all the time. 9772 */ 9773 if (lun != NULL) 9774 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9775 lun->be_lun->lun_type; 9776 else 9777 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9778 devid_ptr->page_code = SVPD_DEVICE_ID; 9779 scsi_ulto2b(data_len - 4, devid_ptr->length); 9780 9781 if (port->port_type == CTL_PORT_FC) 9782 proto = SCSI_PROTO_FC << 4; 9783 else if (port->port_type == CTL_PORT_ISCSI) 9784 proto = SCSI_PROTO_ISCSI << 4; 9785 else 9786 proto = SCSI_PROTO_SPI << 4; 9787 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9788 9789 /* 9790 * We're using a LUN association here. i.e., this device ID is a 9791 * per-LUN identifier. 9792 */ 9793 if (lun && lun->lun_devid) { 9794 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9795 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9796 lun->lun_devid->len); 9797 } 9798 9799 /* 9800 * This is for the WWPN which is a port association. 9801 */ 9802 if (port->port_devid) { 9803 memcpy(desc, port->port_devid->data, port->port_devid->len); 9804 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9805 port->port_devid->len); 9806 } 9807 9808 /* 9809 * This is for the Relative Target Port(type 4h) identifier 9810 */ 9811 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9812 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9813 SVPD_ID_TYPE_RELTARG; 9814 desc->length = 4; 9815 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9816 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9817 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9818 9819 /* 9820 * This is for the Target Port Group(type 5h) identifier 9821 */ 9822 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9823 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9824 SVPD_ID_TYPE_TPORTGRP; 9825 desc->length = 4; 9826 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1, 9827 &desc->identifier[2]); 9828 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9829 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9830 9831 /* 9832 * This is for the Target identifier 9833 */ 9834 if (port->target_devid) { 9835 memcpy(desc, port->target_devid->data, port->target_devid->len); 9836 } 9837 9838 ctl_set_success(ctsio); 9839 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9840 ctsio->be_move_done = ctl_config_move_done; 9841 ctl_datamove((union ctl_io *)ctsio); 9842 return (CTL_RETVAL_COMPLETE); 9843} 9844 9845static int 9846ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9847{ 9848 struct ctl_softc *softc = control_softc; 9849 struct scsi_vpd_scsi_ports *sp; 9850 struct scsi_vpd_port_designation *pd; 9851 struct scsi_vpd_port_designation_cont *pdc; 9852 struct ctl_lun *lun; 9853 struct ctl_port *port; 9854 int data_len, num_target_ports, iid_len, id_len, g, pg, p; 9855 int num_target_port_groups; 9856 9857 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9858 9859 if (softc->is_single) 9860 num_target_port_groups = 1; 9861 else 9862 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 9863 num_target_ports = 0; 9864 iid_len = 0; 9865 id_len = 0; 9866 mtx_lock(&softc->ctl_lock); 9867 STAILQ_FOREACH(port, &softc->port_list, links) { 9868 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9869 continue; 9870 if (lun != NULL && 9871 ctl_map_lun_back(port->targ_port, lun->lun) >= 9872 CTL_MAX_LUNS) 9873 continue; 9874 num_target_ports++; 9875 if (port->init_devid) 9876 iid_len += port->init_devid->len; 9877 if (port->port_devid) 9878 id_len += port->port_devid->len; 9879 } 9880 mtx_unlock(&softc->ctl_lock); 9881 9882 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups * 9883 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9884 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9885 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9886 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9887 ctsio->kern_sg_entries = 0; 9888 9889 if (data_len < alloc_len) { 9890 ctsio->residual = alloc_len - data_len; 9891 ctsio->kern_data_len = data_len; 9892 ctsio->kern_total_len = data_len; 9893 } else { 9894 ctsio->residual = 0; 9895 ctsio->kern_data_len = alloc_len; 9896 ctsio->kern_total_len = alloc_len; 9897 } 9898 ctsio->kern_data_resid = 0; 9899 ctsio->kern_rel_offset = 0; 9900 ctsio->kern_sg_entries = 0; 9901 9902 /* 9903 * The control device is always connected. The disk device, on the 9904 * other hand, may not be online all the time. Need to change this 9905 * to figure out whether the disk device is actually online or not. 9906 */ 9907 if (lun != NULL) 9908 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9909 lun->be_lun->lun_type; 9910 else 9911 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9912 9913 sp->page_code = SVPD_SCSI_PORTS; 9914 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9915 sp->page_length); 9916 pd = &sp->design[0]; 9917 9918 mtx_lock(&softc->ctl_lock); 9919 pg = softc->port_offset / CTL_MAX_PORTS; 9920 for (g = 0; g < num_target_port_groups; g++) { 9921 STAILQ_FOREACH(port, &softc->port_list, links) { 9922 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9923 continue; 9924 if (lun != NULL && 9925 ctl_map_lun_back(port->targ_port, lun->lun) >= 9926 CTL_MAX_LUNS) 9927 continue; 9928 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS; 9929 scsi_ulto2b(p, pd->relative_port_id); 9930 if (port->init_devid && g == pg) { 9931 iid_len = port->init_devid->len; 9932 memcpy(pd->initiator_transportid, 9933 port->init_devid->data, port->init_devid->len); 9934 } else 9935 iid_len = 0; 9936 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9937 pdc = (struct scsi_vpd_port_designation_cont *) 9938 (&pd->initiator_transportid[iid_len]); 9939 if (port->port_devid && g == pg) { 9940 id_len = port->port_devid->len; 9941 memcpy(pdc->target_port_descriptors, 9942 port->port_devid->data, port->port_devid->len); 9943 } else 9944 id_len = 0; 9945 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9946 pd = (struct scsi_vpd_port_designation *) 9947 ((uint8_t *)pdc->target_port_descriptors + id_len); 9948 } 9949 } 9950 mtx_unlock(&softc->ctl_lock); 9951 9952 ctl_set_success(ctsio); 9953 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9954 ctsio->be_move_done = ctl_config_move_done; 9955 ctl_datamove((union ctl_io *)ctsio); 9956 return (CTL_RETVAL_COMPLETE); 9957} 9958 9959static int 9960ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9961{ 9962 struct scsi_vpd_block_limits *bl_ptr; 9963 struct ctl_lun *lun; 9964 int bs; 9965 9966 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9967 9968 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9969 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9970 ctsio->kern_sg_entries = 0; 9971 9972 if (sizeof(*bl_ptr) < alloc_len) { 9973 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9974 ctsio->kern_data_len = sizeof(*bl_ptr); 9975 ctsio->kern_total_len = sizeof(*bl_ptr); 9976 } else { 9977 ctsio->residual = 0; 9978 ctsio->kern_data_len = alloc_len; 9979 ctsio->kern_total_len = alloc_len; 9980 } 9981 ctsio->kern_data_resid = 0; 9982 ctsio->kern_rel_offset = 0; 9983 ctsio->kern_sg_entries = 0; 9984 9985 /* 9986 * The control device is always connected. The disk device, on the 9987 * other hand, may not be online all the time. Need to change this 9988 * to figure out whether the disk device is actually online or not. 9989 */ 9990 if (lun != NULL) 9991 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9992 lun->be_lun->lun_type; 9993 else 9994 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9995 9996 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9997 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9998 bl_ptr->max_cmp_write_len = 0xff; 9999 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 10000 if (lun != NULL) { 10001 bs = lun->be_lun->blocksize; 10002 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len); 10003 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10004 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 10005 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 10006 if (lun->be_lun->pblockexp != 0) { 10007 scsi_ulto4b((1 << lun->be_lun->pblockexp), 10008 bl_ptr->opt_unmap_grain); 10009 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff, 10010 bl_ptr->unmap_grain_align); 10011 } 10012 } 10013 scsi_ulto4b(lun->be_lun->atomicblock, 10014 bl_ptr->max_atomic_transfer_length); 10015 scsi_ulto4b(0, bl_ptr->atomic_alignment); 10016 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 10017 } 10018 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 10019 10020 ctl_set_success(ctsio); 10021 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10022 ctsio->be_move_done = ctl_config_move_done; 10023 ctl_datamove((union ctl_io *)ctsio); 10024 return (CTL_RETVAL_COMPLETE); 10025} 10026 10027static int 10028ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 10029{ 10030 struct scsi_vpd_block_device_characteristics *bdc_ptr; 10031 struct ctl_lun *lun; 10032 const char *value; 10033 u_int i; 10034 10035 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10036 10037 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 10038 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 10039 ctsio->kern_sg_entries = 0; 10040 10041 if (sizeof(*bdc_ptr) < alloc_len) { 10042 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 10043 ctsio->kern_data_len = sizeof(*bdc_ptr); 10044 ctsio->kern_total_len = sizeof(*bdc_ptr); 10045 } else { 10046 ctsio->residual = 0; 10047 ctsio->kern_data_len = alloc_len; 10048 ctsio->kern_total_len = alloc_len; 10049 } 10050 ctsio->kern_data_resid = 0; 10051 ctsio->kern_rel_offset = 0; 10052 ctsio->kern_sg_entries = 0; 10053 10054 /* 10055 * The control device is always connected. The disk device, on the 10056 * other hand, may not be online all the time. Need to change this 10057 * to figure out whether the disk device is actually online or not. 10058 */ 10059 if (lun != NULL) 10060 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10061 lun->be_lun->lun_type; 10062 else 10063 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10064 bdc_ptr->page_code = SVPD_BDC; 10065 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 10066 if (lun != NULL && 10067 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 10068 i = strtol(value, NULL, 0); 10069 else 10070 i = CTL_DEFAULT_ROTATION_RATE; 10071 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 10072 if (lun != NULL && 10073 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 10074 i = strtol(value, NULL, 0); 10075 else 10076 i = 0; 10077 bdc_ptr->wab_wac_ff = (i & 0x0f); 10078 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 10079 10080 ctl_set_success(ctsio); 10081 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10082 ctsio->be_move_done = ctl_config_move_done; 10083 ctl_datamove((union ctl_io *)ctsio); 10084 return (CTL_RETVAL_COMPLETE); 10085} 10086 10087static int 10088ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 10089{ 10090 struct scsi_vpd_logical_block_prov *lbp_ptr; 10091 struct ctl_lun *lun; 10092 10093 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10094 10095 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 10096 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 10097 ctsio->kern_sg_entries = 0; 10098 10099 if (sizeof(*lbp_ptr) < alloc_len) { 10100 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 10101 ctsio->kern_data_len = sizeof(*lbp_ptr); 10102 ctsio->kern_total_len = sizeof(*lbp_ptr); 10103 } else { 10104 ctsio->residual = 0; 10105 ctsio->kern_data_len = alloc_len; 10106 ctsio->kern_total_len = alloc_len; 10107 } 10108 ctsio->kern_data_resid = 0; 10109 ctsio->kern_rel_offset = 0; 10110 ctsio->kern_sg_entries = 0; 10111 10112 /* 10113 * The control device is always connected. The disk device, on the 10114 * other hand, may not be online all the time. Need to change this 10115 * to figure out whether the disk device is actually online or not. 10116 */ 10117 if (lun != NULL) 10118 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10119 lun->be_lun->lun_type; 10120 else 10121 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10122 10123 lbp_ptr->page_code = SVPD_LBP; 10124 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 10125 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 10126 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 10127 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 10128 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 10129 lbp_ptr->prov_type = SVPD_LBP_THIN; 10130 } 10131 10132 ctl_set_success(ctsio); 10133 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10134 ctsio->be_move_done = ctl_config_move_done; 10135 ctl_datamove((union ctl_io *)ctsio); 10136 return (CTL_RETVAL_COMPLETE); 10137} 10138 10139static int 10140ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 10141{ 10142 struct scsi_inquiry *cdb; 10143 int alloc_len, retval; 10144 10145 cdb = (struct scsi_inquiry *)ctsio->cdb; 10146 10147 retval = CTL_RETVAL_COMPLETE; 10148 10149 alloc_len = scsi_2btoul(cdb->length); 10150 10151 switch (cdb->page_code) { 10152 case SVPD_SUPPORTED_PAGES: 10153 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10154 break; 10155 case SVPD_UNIT_SERIAL_NUMBER: 10156 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10157 break; 10158 case SVPD_DEVICE_ID: 10159 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10160 break; 10161 case SVPD_EXTENDED_INQUIRY_DATA: 10162 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10163 break; 10164 case SVPD_MODE_PAGE_POLICY: 10165 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10166 break; 10167 case SVPD_SCSI_PORTS: 10168 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10169 break; 10170 case SVPD_SCSI_TPC: 10171 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10172 break; 10173 case SVPD_BLOCK_LIMITS: 10174 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10175 break; 10176 case SVPD_BDC: 10177 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10178 break; 10179 case SVPD_LBP: 10180 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10181 break; 10182 default: 10183 ctl_set_invalid_field(ctsio, 10184 /*sks_valid*/ 1, 10185 /*command*/ 1, 10186 /*field*/ 2, 10187 /*bit_valid*/ 0, 10188 /*bit*/ 0); 10189 ctl_done((union ctl_io *)ctsio); 10190 retval = CTL_RETVAL_COMPLETE; 10191 break; 10192 } 10193 10194 return (retval); 10195} 10196 10197static int 10198ctl_inquiry_std(struct ctl_scsiio *ctsio) 10199{ 10200 struct scsi_inquiry_data *inq_ptr; 10201 struct scsi_inquiry *cdb; 10202 struct ctl_softc *ctl_softc; 10203 struct ctl_lun *lun; 10204 char *val; 10205 uint32_t alloc_len, data_len; 10206 ctl_port_type port_type; 10207 10208 ctl_softc = control_softc; 10209 10210 /* 10211 * Figure out whether we're talking to a Fibre Channel port or not. 10212 * We treat the ioctl front end, and any SCSI adapters, as packetized 10213 * SCSI front ends. 10214 */ 10215 port_type = ctl_softc->ctl_ports[ 10216 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type; 10217 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10218 port_type = CTL_PORT_SCSI; 10219 10220 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10221 cdb = (struct scsi_inquiry *)ctsio->cdb; 10222 alloc_len = scsi_2btoul(cdb->length); 10223 10224 /* 10225 * We malloc the full inquiry data size here and fill it 10226 * in. If the user only asks for less, we'll give him 10227 * that much. 10228 */ 10229 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10230 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10231 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10232 ctsio->kern_sg_entries = 0; 10233 ctsio->kern_data_resid = 0; 10234 ctsio->kern_rel_offset = 0; 10235 10236 if (data_len < alloc_len) { 10237 ctsio->residual = alloc_len - data_len; 10238 ctsio->kern_data_len = data_len; 10239 ctsio->kern_total_len = data_len; 10240 } else { 10241 ctsio->residual = 0; 10242 ctsio->kern_data_len = alloc_len; 10243 ctsio->kern_total_len = alloc_len; 10244 } 10245 10246 /* 10247 * If we have a LUN configured, report it as connected. Otherwise, 10248 * report that it is offline or no device is supported, depending 10249 * on the value of inquiry_pq_no_lun. 10250 * 10251 * According to the spec (SPC-4 r34), the peripheral qualifier 10252 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario: 10253 * 10254 * "A peripheral device having the specified peripheral device type 10255 * is not connected to this logical unit. However, the device 10256 * server is capable of supporting the specified peripheral device 10257 * type on this logical unit." 10258 * 10259 * According to the same spec, the peripheral qualifier 10260 * SID_QUAL_BAD_LU (011b) is used in this scenario: 10261 * 10262 * "The device server is not capable of supporting a peripheral 10263 * device on this logical unit. For this peripheral qualifier the 10264 * peripheral device type shall be set to 1Fh. All other peripheral 10265 * device type values are reserved for this peripheral qualifier." 10266 * 10267 * Given the text, it would seem that we probably want to report that 10268 * the LUN is offline here. There is no LUN connected, but we can 10269 * support a LUN at the given LUN number. 10270 * 10271 * In the real world, though, it sounds like things are a little 10272 * different: 10273 * 10274 * - Linux, when presented with a LUN with the offline peripheral 10275 * qualifier, will create an sg driver instance for it. So when 10276 * you attach it to CTL, you wind up with a ton of sg driver 10277 * instances. (One for every LUN that Linux bothered to probe.) 10278 * Linux does this despite the fact that it issues a REPORT LUNs 10279 * to LUN 0 to get the inventory of supported LUNs. 10280 * 10281 * - There is other anecdotal evidence (from Emulex folks) about 10282 * arrays that use the offline peripheral qualifier for LUNs that 10283 * are on the "passive" path in an active/passive array. 10284 * 10285 * So the solution is provide a hopefully reasonable default 10286 * (return bad/no LUN) and allow the user to change the behavior 10287 * with a tunable/sysctl variable. 10288 */ 10289 if (lun != NULL) 10290 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10291 lun->be_lun->lun_type; 10292 else if (ctl_softc->inquiry_pq_no_lun == 0) 10293 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 10294 else 10295 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10296 10297 /* RMB in byte 2 is 0 */ 10298 inq_ptr->version = SCSI_REV_SPC4; 10299 10300 /* 10301 * According to SAM-3, even if a device only supports a single 10302 * level of LUN addressing, it should still set the HISUP bit: 10303 * 10304 * 4.9.1 Logical unit numbers overview 10305 * 10306 * All logical unit number formats described in this standard are 10307 * hierarchical in structure even when only a single level in that 10308 * hierarchy is used. The HISUP bit shall be set to one in the 10309 * standard INQUIRY data (see SPC-2) when any logical unit number 10310 * format described in this standard is used. Non-hierarchical 10311 * formats are outside the scope of this standard. 10312 * 10313 * Therefore we set the HiSup bit here. 10314 * 10315 * The reponse format is 2, per SPC-3. 10316 */ 10317 inq_ptr->response_format = SID_HiSup | 2; 10318 10319 inq_ptr->additional_length = data_len - 10320 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10321 CTL_DEBUG_PRINT(("additional_length = %d\n", 10322 inq_ptr->additional_length)); 10323 10324 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10325 /* 16 bit addressing */ 10326 if (port_type == CTL_PORT_SCSI) 10327 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10328 /* XXX set the SID_MultiP bit here if we're actually going to 10329 respond on multiple ports */ 10330 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10331 10332 /* 16 bit data bus, synchronous transfers */ 10333 if (port_type == CTL_PORT_SCSI) 10334 inq_ptr->flags = SID_WBus16 | SID_Sync; 10335 /* 10336 * XXX KDM do we want to support tagged queueing on the control 10337 * device at all? 10338 */ 10339 if ((lun == NULL) 10340 || (lun->be_lun->lun_type != T_PROCESSOR)) 10341 inq_ptr->flags |= SID_CmdQue; 10342 /* 10343 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10344 * We have 8 bytes for the vendor name, and 16 bytes for the device 10345 * name and 4 bytes for the revision. 10346 */ 10347 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10348 "vendor")) == NULL) { 10349 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10350 } else { 10351 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10352 strncpy(inq_ptr->vendor, val, 10353 min(sizeof(inq_ptr->vendor), strlen(val))); 10354 } 10355 if (lun == NULL) { 10356 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10357 sizeof(inq_ptr->product)); 10358 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10359 switch (lun->be_lun->lun_type) { 10360 case T_DIRECT: 10361 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10362 sizeof(inq_ptr->product)); 10363 break; 10364 case T_PROCESSOR: 10365 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10366 sizeof(inq_ptr->product)); 10367 break; 10368 default: 10369 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10370 sizeof(inq_ptr->product)); 10371 break; 10372 } 10373 } else { 10374 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10375 strncpy(inq_ptr->product, val, 10376 min(sizeof(inq_ptr->product), strlen(val))); 10377 } 10378 10379 /* 10380 * XXX make this a macro somewhere so it automatically gets 10381 * incremented when we make changes. 10382 */ 10383 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10384 "revision")) == NULL) { 10385 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10386 } else { 10387 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10388 strncpy(inq_ptr->revision, val, 10389 min(sizeof(inq_ptr->revision), strlen(val))); 10390 } 10391 10392 /* 10393 * For parallel SCSI, we support double transition and single 10394 * transition clocking. We also support QAS (Quick Arbitration 10395 * and Selection) and Information Unit transfers on both the 10396 * control and array devices. 10397 */ 10398 if (port_type == CTL_PORT_SCSI) 10399 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10400 SID_SPI_IUS; 10401 10402 /* SAM-5 (no version claimed) */ 10403 scsi_ulto2b(0x00A0, inq_ptr->version1); 10404 /* SPC-4 (no version claimed) */ 10405 scsi_ulto2b(0x0460, inq_ptr->version2); 10406 if (port_type == CTL_PORT_FC) { 10407 /* FCP-2 ANSI INCITS.350:2003 */ 10408 scsi_ulto2b(0x0917, inq_ptr->version3); 10409 } else if (port_type == CTL_PORT_SCSI) { 10410 /* SPI-4 ANSI INCITS.362:200x */ 10411 scsi_ulto2b(0x0B56, inq_ptr->version3); 10412 } else if (port_type == CTL_PORT_ISCSI) { 10413 /* iSCSI (no version claimed) */ 10414 scsi_ulto2b(0x0960, inq_ptr->version3); 10415 } else if (port_type == CTL_PORT_SAS) { 10416 /* SAS (no version claimed) */ 10417 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10418 } 10419 10420 if (lun == NULL) { 10421 /* SBC-4 (no version claimed) */ 10422 scsi_ulto2b(0x0600, inq_ptr->version4); 10423 } else { 10424 switch (lun->be_lun->lun_type) { 10425 case T_DIRECT: 10426 /* SBC-4 (no version claimed) */ 10427 scsi_ulto2b(0x0600, inq_ptr->version4); 10428 break; 10429 case T_PROCESSOR: 10430 default: 10431 break; 10432 } 10433 } 10434 10435 ctl_set_success(ctsio); 10436 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10437 ctsio->be_move_done = ctl_config_move_done; 10438 ctl_datamove((union ctl_io *)ctsio); 10439 return (CTL_RETVAL_COMPLETE); 10440} 10441 10442int 10443ctl_inquiry(struct ctl_scsiio *ctsio) 10444{ 10445 struct scsi_inquiry *cdb; 10446 int retval; 10447 10448 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10449 10450 cdb = (struct scsi_inquiry *)ctsio->cdb; 10451 if (cdb->byte2 & SI_EVPD) 10452 retval = ctl_inquiry_evpd(ctsio); 10453 else if (cdb->page_code == 0) 10454 retval = ctl_inquiry_std(ctsio); 10455 else { 10456 ctl_set_invalid_field(ctsio, 10457 /*sks_valid*/ 1, 10458 /*command*/ 1, 10459 /*field*/ 2, 10460 /*bit_valid*/ 0, 10461 /*bit*/ 0); 10462 ctl_done((union ctl_io *)ctsio); 10463 return (CTL_RETVAL_COMPLETE); 10464 } 10465 10466 return (retval); 10467} 10468 10469/* 10470 * For known CDB types, parse the LBA and length. 10471 */ 10472static int 10473ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10474{ 10475 if (io->io_hdr.io_type != CTL_IO_SCSI) 10476 return (1); 10477 10478 switch (io->scsiio.cdb[0]) { 10479 case COMPARE_AND_WRITE: { 10480 struct scsi_compare_and_write *cdb; 10481 10482 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10483 10484 *lba = scsi_8btou64(cdb->addr); 10485 *len = cdb->length; 10486 break; 10487 } 10488 case READ_6: 10489 case WRITE_6: { 10490 struct scsi_rw_6 *cdb; 10491 10492 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10493 10494 *lba = scsi_3btoul(cdb->addr); 10495 /* only 5 bits are valid in the most significant address byte */ 10496 *lba &= 0x1fffff; 10497 *len = cdb->length; 10498 break; 10499 } 10500 case READ_10: 10501 case WRITE_10: { 10502 struct scsi_rw_10 *cdb; 10503 10504 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10505 10506 *lba = scsi_4btoul(cdb->addr); 10507 *len = scsi_2btoul(cdb->length); 10508 break; 10509 } 10510 case WRITE_VERIFY_10: { 10511 struct scsi_write_verify_10 *cdb; 10512 10513 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10514 10515 *lba = scsi_4btoul(cdb->addr); 10516 *len = scsi_2btoul(cdb->length); 10517 break; 10518 } 10519 case READ_12: 10520 case WRITE_12: { 10521 struct scsi_rw_12 *cdb; 10522 10523 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10524 10525 *lba = scsi_4btoul(cdb->addr); 10526 *len = scsi_4btoul(cdb->length); 10527 break; 10528 } 10529 case WRITE_VERIFY_12: { 10530 struct scsi_write_verify_12 *cdb; 10531 10532 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10533 10534 *lba = scsi_4btoul(cdb->addr); 10535 *len = scsi_4btoul(cdb->length); 10536 break; 10537 } 10538 case READ_16: 10539 case WRITE_16: 10540 case WRITE_ATOMIC_16: { 10541 struct scsi_rw_16 *cdb; 10542 10543 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10544 10545 *lba = scsi_8btou64(cdb->addr); 10546 *len = scsi_4btoul(cdb->length); 10547 break; 10548 } 10549 case WRITE_VERIFY_16: { 10550 struct scsi_write_verify_16 *cdb; 10551 10552 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10553 10554 *lba = scsi_8btou64(cdb->addr); 10555 *len = scsi_4btoul(cdb->length); 10556 break; 10557 } 10558 case WRITE_SAME_10: { 10559 struct scsi_write_same_10 *cdb; 10560 10561 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10562 10563 *lba = scsi_4btoul(cdb->addr); 10564 *len = scsi_2btoul(cdb->length); 10565 break; 10566 } 10567 case WRITE_SAME_16: { 10568 struct scsi_write_same_16 *cdb; 10569 10570 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10571 10572 *lba = scsi_8btou64(cdb->addr); 10573 *len = scsi_4btoul(cdb->length); 10574 break; 10575 } 10576 case VERIFY_10: { 10577 struct scsi_verify_10 *cdb; 10578 10579 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10580 10581 *lba = scsi_4btoul(cdb->addr); 10582 *len = scsi_2btoul(cdb->length); 10583 break; 10584 } 10585 case VERIFY_12: { 10586 struct scsi_verify_12 *cdb; 10587 10588 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10589 10590 *lba = scsi_4btoul(cdb->addr); 10591 *len = scsi_4btoul(cdb->length); 10592 break; 10593 } 10594 case VERIFY_16: { 10595 struct scsi_verify_16 *cdb; 10596 10597 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10598 10599 *lba = scsi_8btou64(cdb->addr); 10600 *len = scsi_4btoul(cdb->length); 10601 break; 10602 } 10603 case UNMAP: { 10604 *lba = 0; 10605 *len = UINT64_MAX; 10606 break; 10607 } 10608 default: 10609 return (1); 10610 break; /* NOTREACHED */ 10611 } 10612 10613 return (0); 10614} 10615 10616static ctl_action 10617ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2) 10618{ 10619 uint64_t endlba1, endlba2; 10620 10621 endlba1 = lba1 + len1 - 1; 10622 endlba2 = lba2 + len2 - 1; 10623 10624 if ((endlba1 < lba2) 10625 || (endlba2 < lba1)) 10626 return (CTL_ACTION_PASS); 10627 else 10628 return (CTL_ACTION_BLOCK); 10629} 10630 10631static int 10632ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10633{ 10634 struct ctl_ptr_len_flags *ptrlen; 10635 struct scsi_unmap_desc *buf, *end, *range; 10636 uint64_t lba; 10637 uint32_t len; 10638 10639 /* If not UNMAP -- go other way. */ 10640 if (io->io_hdr.io_type != CTL_IO_SCSI || 10641 io->scsiio.cdb[0] != UNMAP) 10642 return (CTL_ACTION_ERROR); 10643 10644 /* If UNMAP without data -- block and wait for data. */ 10645 ptrlen = (struct ctl_ptr_len_flags *) 10646 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10647 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10648 ptrlen->ptr == NULL) 10649 return (CTL_ACTION_BLOCK); 10650 10651 /* UNMAP with data -- check for collision. */ 10652 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10653 end = buf + ptrlen->len / sizeof(*buf); 10654 for (range = buf; range < end; range++) { 10655 lba = scsi_8btou64(range->lba); 10656 len = scsi_4btoul(range->length); 10657 if ((lba < lba2 + len2) && (lba + len > lba2)) 10658 return (CTL_ACTION_BLOCK); 10659 } 10660 return (CTL_ACTION_PASS); 10661} 10662 10663static ctl_action 10664ctl_extent_check(union ctl_io *io1, union ctl_io *io2) 10665{ 10666 uint64_t lba1, lba2; 10667 uint64_t len1, len2; 10668 int retval; 10669 10670 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10671 return (CTL_ACTION_ERROR); 10672 10673 retval = ctl_extent_check_unmap(io2, lba1, len1); 10674 if (retval != CTL_ACTION_ERROR) 10675 return (retval); 10676 10677 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10678 return (CTL_ACTION_ERROR); 10679 10680 return (ctl_extent_check_lba(lba1, len1, lba2, len2)); 10681} 10682 10683static ctl_action 10684ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10685 union ctl_io *ooa_io) 10686{ 10687 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10688 ctl_serialize_action *serialize_row; 10689 10690 /* 10691 * The initiator attempted multiple untagged commands at the same 10692 * time. Can't do that. 10693 */ 10694 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10695 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10696 && ((pending_io->io_hdr.nexus.targ_port == 10697 ooa_io->io_hdr.nexus.targ_port) 10698 && (pending_io->io_hdr.nexus.initid.id == 10699 ooa_io->io_hdr.nexus.initid.id)) 10700 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10701 return (CTL_ACTION_OVERLAP); 10702 10703 /* 10704 * The initiator attempted to send multiple tagged commands with 10705 * the same ID. (It's fine if different initiators have the same 10706 * tag ID.) 10707 * 10708 * Even if all of those conditions are true, we don't kill the I/O 10709 * if the command ahead of us has been aborted. We won't end up 10710 * sending it to the FETD, and it's perfectly legal to resend a 10711 * command with the same tag number as long as the previous 10712 * instance of this tag number has been aborted somehow. 10713 */ 10714 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10715 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10716 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10717 && ((pending_io->io_hdr.nexus.targ_port == 10718 ooa_io->io_hdr.nexus.targ_port) 10719 && (pending_io->io_hdr.nexus.initid.id == 10720 ooa_io->io_hdr.nexus.initid.id)) 10721 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0)) 10722 return (CTL_ACTION_OVERLAP_TAG); 10723 10724 /* 10725 * If we get a head of queue tag, SAM-3 says that we should 10726 * immediately execute it. 10727 * 10728 * What happens if this command would normally block for some other 10729 * reason? e.g. a request sense with a head of queue tag 10730 * immediately after a write. Normally that would block, but this 10731 * will result in its getting executed immediately... 10732 * 10733 * We currently return "pass" instead of "skip", so we'll end up 10734 * going through the rest of the queue to check for overlapped tags. 10735 * 10736 * XXX KDM check for other types of blockage first?? 10737 */ 10738 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10739 return (CTL_ACTION_PASS); 10740 10741 /* 10742 * Ordered tags have to block until all items ahead of them 10743 * have completed. If we get called with an ordered tag, we always 10744 * block, if something else is ahead of us in the queue. 10745 */ 10746 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10747 return (CTL_ACTION_BLOCK); 10748 10749 /* 10750 * Simple tags get blocked until all head of queue and ordered tags 10751 * ahead of them have completed. I'm lumping untagged commands in 10752 * with simple tags here. XXX KDM is that the right thing to do? 10753 */ 10754 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10755 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 10756 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10757 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 10758 return (CTL_ACTION_BLOCK); 10759 10760 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 10761 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 10762 10763 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 10764 10765 switch (serialize_row[pending_entry->seridx]) { 10766 case CTL_SER_BLOCK: 10767 return (CTL_ACTION_BLOCK); 10768 case CTL_SER_EXTENT: 10769 return (ctl_extent_check(pending_io, ooa_io)); 10770 case CTL_SER_EXTENTOPT: 10771 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10772 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10773 return (ctl_extent_check(pending_io, ooa_io)); 10774 /* FALLTHROUGH */ 10775 case CTL_SER_PASS: 10776 return (CTL_ACTION_PASS); 10777 case CTL_SER_BLOCKOPT: 10778 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 10779 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 10780 return (CTL_ACTION_BLOCK); 10781 return (CTL_ACTION_PASS); 10782 case CTL_SER_SKIP: 10783 return (CTL_ACTION_SKIP); 10784 default: 10785 panic("invalid serialization value %d", 10786 serialize_row[pending_entry->seridx]); 10787 } 10788 10789 return (CTL_ACTION_ERROR); 10790} 10791 10792/* 10793 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 10794 * Assumptions: 10795 * - pending_io is generally either incoming, or on the blocked queue 10796 * - starting I/O is the I/O we want to start the check with. 10797 */ 10798static ctl_action 10799ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 10800 union ctl_io *starting_io) 10801{ 10802 union ctl_io *ooa_io; 10803 ctl_action action; 10804 10805 mtx_assert(&lun->lun_lock, MA_OWNED); 10806 10807 /* 10808 * Run back along the OOA queue, starting with the current 10809 * blocked I/O and going through every I/O before it on the 10810 * queue. If starting_io is NULL, we'll just end up returning 10811 * CTL_ACTION_PASS. 10812 */ 10813 for (ooa_io = starting_io; ooa_io != NULL; 10814 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 10815 ooa_links)){ 10816 10817 /* 10818 * This routine just checks to see whether 10819 * cur_blocked is blocked by ooa_io, which is ahead 10820 * of it in the queue. It doesn't queue/dequeue 10821 * cur_blocked. 10822 */ 10823 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 10824 switch (action) { 10825 case CTL_ACTION_BLOCK: 10826 case CTL_ACTION_OVERLAP: 10827 case CTL_ACTION_OVERLAP_TAG: 10828 case CTL_ACTION_SKIP: 10829 case CTL_ACTION_ERROR: 10830 return (action); 10831 break; /* NOTREACHED */ 10832 case CTL_ACTION_PASS: 10833 break; 10834 default: 10835 panic("invalid action %d", action); 10836 break; /* NOTREACHED */ 10837 } 10838 } 10839 10840 return (CTL_ACTION_PASS); 10841} 10842 10843/* 10844 * Assumptions: 10845 * - An I/O has just completed, and has been removed from the per-LUN OOA 10846 * queue, so some items on the blocked queue may now be unblocked. 10847 */ 10848static int 10849ctl_check_blocked(struct ctl_lun *lun) 10850{ 10851 union ctl_io *cur_blocked, *next_blocked; 10852 10853 mtx_assert(&lun->lun_lock, MA_OWNED); 10854 10855 /* 10856 * Run forward from the head of the blocked queue, checking each 10857 * entry against the I/Os prior to it on the OOA queue to see if 10858 * there is still any blockage. 10859 * 10860 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 10861 * with our removing a variable on it while it is traversing the 10862 * list. 10863 */ 10864 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 10865 cur_blocked != NULL; cur_blocked = next_blocked) { 10866 union ctl_io *prev_ooa; 10867 ctl_action action; 10868 10869 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 10870 blocked_links); 10871 10872 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 10873 ctl_ooaq, ooa_links); 10874 10875 /* 10876 * If cur_blocked happens to be the first item in the OOA 10877 * queue now, prev_ooa will be NULL, and the action 10878 * returned will just be CTL_ACTION_PASS. 10879 */ 10880 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 10881 10882 switch (action) { 10883 case CTL_ACTION_BLOCK: 10884 /* Nothing to do here, still blocked */ 10885 break; 10886 case CTL_ACTION_OVERLAP: 10887 case CTL_ACTION_OVERLAP_TAG: 10888 /* 10889 * This shouldn't happen! In theory we've already 10890 * checked this command for overlap... 10891 */ 10892 break; 10893 case CTL_ACTION_PASS: 10894 case CTL_ACTION_SKIP: { 10895 struct ctl_softc *softc; 10896 const struct ctl_cmd_entry *entry; 10897 int isc_retval; 10898 10899 /* 10900 * The skip case shouldn't happen, this transaction 10901 * should have never made it onto the blocked queue. 10902 */ 10903 /* 10904 * This I/O is no longer blocked, we can remove it 10905 * from the blocked queue. Since this is a TAILQ 10906 * (doubly linked list), we can do O(1) removals 10907 * from any place on the list. 10908 */ 10909 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 10910 blocked_links); 10911 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 10912 10913 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){ 10914 /* 10915 * Need to send IO back to original side to 10916 * run 10917 */ 10918 union ctl_ha_msg msg_info; 10919 10920 msg_info.hdr.original_sc = 10921 cur_blocked->io_hdr.original_sc; 10922 msg_info.hdr.serializing_sc = cur_blocked; 10923 msg_info.hdr.msg_type = CTL_MSG_R2R; 10924 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 10925 &msg_info, sizeof(msg_info), 0)) > 10926 CTL_HA_STATUS_SUCCESS) { 10927 printf("CTL:Check Blocked error from " 10928 "ctl_ha_msg_send %d\n", 10929 isc_retval); 10930 } 10931 break; 10932 } 10933 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 10934 softc = control_softc; 10935 10936 /* 10937 * Check this I/O for LUN state changes that may 10938 * have happened while this command was blocked. 10939 * The LUN state may have been changed by a command 10940 * ahead of us in the queue, so we need to re-check 10941 * for any states that can be caused by SCSI 10942 * commands. 10943 */ 10944 if (ctl_scsiio_lun_check(softc, lun, entry, 10945 &cur_blocked->scsiio) == 0) { 10946 cur_blocked->io_hdr.flags |= 10947 CTL_FLAG_IS_WAS_ON_RTR; 10948 ctl_enqueue_rtr(cur_blocked); 10949 } else 10950 ctl_done(cur_blocked); 10951 break; 10952 } 10953 default: 10954 /* 10955 * This probably shouldn't happen -- we shouldn't 10956 * get CTL_ACTION_ERROR, or anything else. 10957 */ 10958 break; 10959 } 10960 } 10961 10962 return (CTL_RETVAL_COMPLETE); 10963} 10964 10965/* 10966 * This routine (with one exception) checks LUN flags that can be set by 10967 * commands ahead of us in the OOA queue. These flags have to be checked 10968 * when a command initially comes in, and when we pull a command off the 10969 * blocked queue and are preparing to execute it. The reason we have to 10970 * check these flags for commands on the blocked queue is that the LUN 10971 * state may have been changed by a command ahead of us while we're on the 10972 * blocked queue. 10973 * 10974 * Ordering is somewhat important with these checks, so please pay 10975 * careful attention to the placement of any new checks. 10976 */ 10977static int 10978ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 10979 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 10980{ 10981 int retval; 10982 uint32_t residx; 10983 10984 retval = 0; 10985 10986 mtx_assert(&lun->lun_lock, MA_OWNED); 10987 10988 /* 10989 * If this shelf is a secondary shelf controller, we have to reject 10990 * any media access commands. 10991 */ 10992 if ((ctl_softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 && 10993 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) { 10994 ctl_set_lun_standby(ctsio); 10995 retval = 1; 10996 goto bailout; 10997 } 10998 10999 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11000 if (lun->flags & CTL_LUN_READONLY) { 11001 ctl_set_sense(ctsio, /*current_error*/ 1, 11002 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11003 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE); 11004 retval = 1; 11005 goto bailout; 11006 } 11007 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11008 .eca_and_aen & SCP_SWP) != 0) { 11009 ctl_set_sense(ctsio, /*current_error*/ 1, 11010 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11011 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11012 retval = 1; 11013 goto bailout; 11014 } 11015 } 11016 11017 /* 11018 * Check for a reservation conflict. If this command isn't allowed 11019 * even on reserved LUNs, and if this initiator isn't the one who 11020 * reserved us, reject the command with a reservation conflict. 11021 */ 11022 residx = ctl_get_resindex(&ctsio->io_hdr.nexus); 11023 if ((lun->flags & CTL_LUN_RESERVED) 11024 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11025 if (lun->res_idx != residx) { 11026 ctl_set_reservation_conflict(ctsio); 11027 retval = 1; 11028 goto bailout; 11029 } 11030 } 11031 11032 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11033 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11034 /* No reservation or command is allowed. */; 11035 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11036 (lun->res_type == SPR_TYPE_WR_EX || 11037 lun->res_type == SPR_TYPE_WR_EX_RO || 11038 lun->res_type == SPR_TYPE_WR_EX_AR)) { 11039 /* The command is allowed for Write Exclusive resv. */; 11040 } else { 11041 /* 11042 * if we aren't registered or it's a res holder type 11043 * reservation and this isn't the res holder then set a 11044 * conflict. 11045 */ 11046 if (lun->pr_keys[residx] == 0 11047 || (residx != lun->pr_res_idx && lun->res_type < 4)) { 11048 ctl_set_reservation_conflict(ctsio); 11049 retval = 1; 11050 goto bailout; 11051 } 11052 11053 } 11054 11055 if ((lun->flags & CTL_LUN_OFFLINE) 11056 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) { 11057 ctl_set_lun_not_ready(ctsio); 11058 retval = 1; 11059 goto bailout; 11060 } 11061 11062 /* 11063 * If the LUN is stopped, see if this particular command is allowed 11064 * for a stopped lun. Otherwise, reject it with 0x04,0x02. 11065 */ 11066 if ((lun->flags & CTL_LUN_STOPPED) 11067 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) { 11068 /* "Logical unit not ready, initializing cmd. required" */ 11069 ctl_set_lun_stopped(ctsio); 11070 retval = 1; 11071 goto bailout; 11072 } 11073 11074 if ((lun->flags & CTL_LUN_INOPERABLE) 11075 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) { 11076 /* "Medium format corrupted" */ 11077 ctl_set_medium_format_corrupted(ctsio); 11078 retval = 1; 11079 goto bailout; 11080 } 11081 11082bailout: 11083 return (retval); 11084 11085} 11086 11087static void 11088ctl_failover_io(union ctl_io *io, int have_lock) 11089{ 11090 ctl_set_busy(&io->scsiio); 11091 ctl_done(io); 11092} 11093 11094static void 11095ctl_failover(void) 11096{ 11097 struct ctl_lun *lun; 11098 struct ctl_softc *ctl_softc; 11099 union ctl_io *next_io, *pending_io; 11100 union ctl_io *io; 11101 int lun_idx; 11102 int i; 11103 11104 ctl_softc = control_softc; 11105 11106 mtx_lock(&ctl_softc->ctl_lock); 11107 /* 11108 * Remove any cmds from the other SC from the rtr queue. These 11109 * will obviously only be for LUNs for which we're the primary. 11110 * We can't send status or get/send data for these commands. 11111 * Since they haven't been executed yet, we can just remove them. 11112 * We'll either abort them or delete them below, depending on 11113 * which HA mode we're in. 11114 */ 11115#ifdef notyet 11116 mtx_lock(&ctl_softc->queue_lock); 11117 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue); 11118 io != NULL; io = next_io) { 11119 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links); 11120 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11121 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr, 11122 ctl_io_hdr, links); 11123 } 11124 mtx_unlock(&ctl_softc->queue_lock); 11125#endif 11126 11127 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) { 11128 lun = ctl_softc->ctl_luns[lun_idx]; 11129 if (lun==NULL) 11130 continue; 11131 11132 /* 11133 * Processor LUNs are primary on both sides. 11134 * XXX will this always be true? 11135 */ 11136 if (lun->be_lun->lun_type == T_PROCESSOR) 11137 continue; 11138 11139 if ((lun->flags & CTL_LUN_PRIMARY_SC) 11140 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11141 printf("FAILOVER: primary lun %d\n", lun_idx); 11142 /* 11143 * Remove all commands from the other SC. First from the 11144 * blocked queue then from the ooa queue. Once we have 11145 * removed them. Call ctl_check_blocked to see if there 11146 * is anything that can run. 11147 */ 11148 for (io = (union ctl_io *)TAILQ_FIRST( 11149 &lun->blocked_queue); io != NULL; io = next_io) { 11150 11151 next_io = (union ctl_io *)TAILQ_NEXT( 11152 &io->io_hdr, blocked_links); 11153 11154 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11155 TAILQ_REMOVE(&lun->blocked_queue, 11156 &io->io_hdr,blocked_links); 11157 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11158 TAILQ_REMOVE(&lun->ooa_queue, 11159 &io->io_hdr, ooa_links); 11160 11161 ctl_free_io(io); 11162 } 11163 } 11164 11165 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11166 io != NULL; io = next_io) { 11167 11168 next_io = (union ctl_io *)TAILQ_NEXT( 11169 &io->io_hdr, ooa_links); 11170 11171 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) { 11172 11173 TAILQ_REMOVE(&lun->ooa_queue, 11174 &io->io_hdr, 11175 ooa_links); 11176 11177 ctl_free_io(io); 11178 } 11179 } 11180 ctl_check_blocked(lun); 11181 } else if ((lun->flags & CTL_LUN_PRIMARY_SC) 11182 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11183 11184 printf("FAILOVER: primary lun %d\n", lun_idx); 11185 /* 11186 * Abort all commands from the other SC. We can't 11187 * send status back for them now. These should get 11188 * cleaned up when they are completed or come out 11189 * for a datamove operation. 11190 */ 11191 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); 11192 io != NULL; io = next_io) { 11193 next_io = (union ctl_io *)TAILQ_NEXT( 11194 &io->io_hdr, ooa_links); 11195 11196 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 11197 io->io_hdr.flags |= CTL_FLAG_ABORT; 11198 } 11199 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11200 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) { 11201 11202 printf("FAILOVER: secondary lun %d\n", lun_idx); 11203 11204 lun->flags |= CTL_LUN_PRIMARY_SC; 11205 11206 /* 11207 * We send all I/O that was sent to this controller 11208 * and redirected to the other side back with 11209 * busy status, and have the initiator retry it. 11210 * Figuring out how much data has been transferred, 11211 * etc. and picking up where we left off would be 11212 * very tricky. 11213 * 11214 * XXX KDM need to remove I/O from the blocked 11215 * queue as well! 11216 */ 11217 for (pending_io = (union ctl_io *)TAILQ_FIRST( 11218 &lun->ooa_queue); pending_io != NULL; 11219 pending_io = next_io) { 11220 11221 next_io = (union ctl_io *)TAILQ_NEXT( 11222 &pending_io->io_hdr, ooa_links); 11223 11224 pending_io->io_hdr.flags &= 11225 ~CTL_FLAG_SENT_2OTHER_SC; 11226 11227 if (pending_io->io_hdr.flags & 11228 CTL_FLAG_IO_ACTIVE) { 11229 pending_io->io_hdr.flags |= 11230 CTL_FLAG_FAILOVER; 11231 } else { 11232 ctl_set_busy(&pending_io->scsiio); 11233 ctl_done(pending_io); 11234 } 11235 } 11236 11237 /* 11238 * Build Unit Attention 11239 */ 11240 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11241 lun->pending_ua[i] |= 11242 CTL_UA_ASYM_ACC_CHANGE; 11243 } 11244 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0) 11245 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) { 11246 printf("FAILOVER: secondary lun %d\n", lun_idx); 11247 /* 11248 * if the first io on the OOA is not on the RtR queue 11249 * add it. 11250 */ 11251 lun->flags |= CTL_LUN_PRIMARY_SC; 11252 11253 pending_io = (union ctl_io *)TAILQ_FIRST( 11254 &lun->ooa_queue); 11255 if (pending_io==NULL) { 11256 printf("Nothing on OOA queue\n"); 11257 continue; 11258 } 11259 11260 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11261 if ((pending_io->io_hdr.flags & 11262 CTL_FLAG_IS_WAS_ON_RTR) == 0) { 11263 pending_io->io_hdr.flags |= 11264 CTL_FLAG_IS_WAS_ON_RTR; 11265 ctl_enqueue_rtr(pending_io); 11266 } 11267#if 0 11268 else 11269 { 11270 printf("Tag 0x%04x is running\n", 11271 pending_io->scsiio.tag_num); 11272 } 11273#endif 11274 11275 next_io = (union ctl_io *)TAILQ_NEXT( 11276 &pending_io->io_hdr, ooa_links); 11277 for (pending_io=next_io; pending_io != NULL; 11278 pending_io = next_io) { 11279 pending_io->io_hdr.flags &= 11280 ~CTL_FLAG_SENT_2OTHER_SC; 11281 next_io = (union ctl_io *)TAILQ_NEXT( 11282 &pending_io->io_hdr, ooa_links); 11283 if (pending_io->io_hdr.flags & 11284 CTL_FLAG_IS_WAS_ON_RTR) { 11285#if 0 11286 printf("Tag 0x%04x is running\n", 11287 pending_io->scsiio.tag_num); 11288#endif 11289 continue; 11290 } 11291 11292 switch (ctl_check_ooa(lun, pending_io, 11293 (union ctl_io *)TAILQ_PREV( 11294 &pending_io->io_hdr, ctl_ooaq, 11295 ooa_links))) { 11296 11297 case CTL_ACTION_BLOCK: 11298 TAILQ_INSERT_TAIL(&lun->blocked_queue, 11299 &pending_io->io_hdr, 11300 blocked_links); 11301 pending_io->io_hdr.flags |= 11302 CTL_FLAG_BLOCKED; 11303 break; 11304 case CTL_ACTION_PASS: 11305 case CTL_ACTION_SKIP: 11306 pending_io->io_hdr.flags |= 11307 CTL_FLAG_IS_WAS_ON_RTR; 11308 ctl_enqueue_rtr(pending_io); 11309 break; 11310 case CTL_ACTION_OVERLAP: 11311 ctl_set_overlapped_cmd( 11312 (struct ctl_scsiio *)pending_io); 11313 ctl_done(pending_io); 11314 break; 11315 case CTL_ACTION_OVERLAP_TAG: 11316 ctl_set_overlapped_tag( 11317 (struct ctl_scsiio *)pending_io, 11318 pending_io->scsiio.tag_num & 0xff); 11319 ctl_done(pending_io); 11320 break; 11321 case CTL_ACTION_ERROR: 11322 default: 11323 ctl_set_internal_failure( 11324 (struct ctl_scsiio *)pending_io, 11325 0, // sks_valid 11326 0); //retry count 11327 ctl_done(pending_io); 11328 break; 11329 } 11330 } 11331 11332 /* 11333 * Build Unit Attention 11334 */ 11335 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11336 lun->pending_ua[i] |= 11337 CTL_UA_ASYM_ACC_CHANGE; 11338 } 11339 } else { 11340 panic("Unhandled HA mode failover, LUN flags = %#x, " 11341 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode); 11342 } 11343 } 11344 ctl_pause_rtr = 0; 11345 mtx_unlock(&ctl_softc->ctl_lock); 11346} 11347 11348static int 11349ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio) 11350{ 11351 struct ctl_lun *lun; 11352 const struct ctl_cmd_entry *entry; 11353 uint32_t initidx, targ_lun; 11354 int retval; 11355 11356 retval = 0; 11357 11358 lun = NULL; 11359 11360 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11361 if ((targ_lun < CTL_MAX_LUNS) 11362 && ((lun = ctl_softc->ctl_luns[targ_lun]) != NULL)) { 11363 /* 11364 * If the LUN is invalid, pretend that it doesn't exist. 11365 * It will go away as soon as all pending I/O has been 11366 * completed. 11367 */ 11368 mtx_lock(&lun->lun_lock); 11369 if (lun->flags & CTL_LUN_DISABLED) { 11370 mtx_unlock(&lun->lun_lock); 11371 lun = NULL; 11372 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11373 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11374 } else { 11375 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11376 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11377 lun->be_lun; 11378 if (lun->be_lun->lun_type == T_PROCESSOR) { 11379 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV; 11380 } 11381 11382 /* 11383 * Every I/O goes into the OOA queue for a 11384 * particular LUN, and stays there until completion. 11385 */ 11386 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11387 ooa_links); 11388 } 11389 } else { 11390 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11391 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11392 } 11393 11394 /* Get command entry and return error if it is unsuppotyed. */ 11395 entry = ctl_validate_command(ctsio); 11396 if (entry == NULL) { 11397 if (lun) 11398 mtx_unlock(&lun->lun_lock); 11399 return (retval); 11400 } 11401 11402 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11403 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11404 11405 /* 11406 * Check to see whether we can send this command to LUNs that don't 11407 * exist. This should pretty much only be the case for inquiry 11408 * and request sense. Further checks, below, really require having 11409 * a LUN, so we can't really check the command anymore. Just put 11410 * it on the rtr queue. 11411 */ 11412 if (lun == NULL) { 11413 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) { 11414 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11415 ctl_enqueue_rtr((union ctl_io *)ctsio); 11416 return (retval); 11417 } 11418 11419 ctl_set_unsupported_lun(ctsio); 11420 ctl_done((union ctl_io *)ctsio); 11421 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11422 return (retval); 11423 } else { 11424 /* 11425 * Make sure we support this particular command on this LUN. 11426 * e.g., we don't support writes to the control LUN. 11427 */ 11428 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11429 mtx_unlock(&lun->lun_lock); 11430 ctl_set_invalid_opcode(ctsio); 11431 ctl_done((union ctl_io *)ctsio); 11432 return (retval); 11433 } 11434 } 11435 11436 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11437 11438#ifdef CTL_WITH_CA 11439 /* 11440 * If we've got a request sense, it'll clear the contingent 11441 * allegiance condition. Otherwise, if we have a CA condition for 11442 * this initiator, clear it, because it sent down a command other 11443 * than request sense. 11444 */ 11445 if ((ctsio->cdb[0] != REQUEST_SENSE) 11446 && (ctl_is_set(lun->have_ca, initidx))) 11447 ctl_clear_mask(lun->have_ca, initidx); 11448#endif 11449 11450 /* 11451 * If the command has this flag set, it handles its own unit 11452 * attention reporting, we shouldn't do anything. Otherwise we 11453 * check for any pending unit attentions, and send them back to the 11454 * initiator. We only do this when a command initially comes in, 11455 * not when we pull it off the blocked queue. 11456 * 11457 * According to SAM-3, section 5.3.2, the order that things get 11458 * presented back to the host is basically unit attentions caused 11459 * by some sort of reset event, busy status, reservation conflicts 11460 * or task set full, and finally any other status. 11461 * 11462 * One issue here is that some of the unit attentions we report 11463 * don't fall into the "reset" category (e.g. "reported luns data 11464 * has changed"). So reporting it here, before the reservation 11465 * check, may be technically wrong. I guess the only thing to do 11466 * would be to check for and report the reset events here, and then 11467 * check for the other unit attention types after we check for a 11468 * reservation conflict. 11469 * 11470 * XXX KDM need to fix this 11471 */ 11472 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11473 ctl_ua_type ua_type; 11474 11475 if (lun->pending_ua[initidx] != CTL_UA_NONE) { 11476 scsi_sense_data_type sense_format; 11477 11478 if (lun != NULL) 11479 sense_format = (lun->flags & 11480 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC : 11481 SSD_TYPE_FIXED; 11482 else 11483 sense_format = SSD_TYPE_FIXED; 11484 11485 ua_type = ctl_build_ua(&lun->pending_ua[initidx], 11486 &ctsio->sense_data, sense_format); 11487 if (ua_type != CTL_UA_NONE) { 11488 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11489 ctsio->io_hdr.status = CTL_SCSI_ERROR | 11490 CTL_AUTOSENSE; 11491 ctsio->sense_len = SSD_FULL_SIZE; 11492 mtx_unlock(&lun->lun_lock); 11493 ctl_done((union ctl_io *)ctsio); 11494 return (retval); 11495 } 11496 } 11497 } 11498 11499 11500 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) { 11501 mtx_unlock(&lun->lun_lock); 11502 ctl_done((union ctl_io *)ctsio); 11503 return (retval); 11504 } 11505 11506 /* 11507 * XXX CHD this is where we want to send IO to other side if 11508 * this LUN is secondary on this SC. We will need to make a copy 11509 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11510 * the copy we send as FROM_OTHER. 11511 * We also need to stuff the address of the original IO so we can 11512 * find it easily. Something similar will need be done on the other 11513 * side so when we are done we can find the copy. 11514 */ 11515 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11516 union ctl_ha_msg msg_info; 11517 int isc_retval; 11518 11519 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11520 11521 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11522 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11523#if 0 11524 printf("1. ctsio %p\n", ctsio); 11525#endif 11526 msg_info.hdr.serializing_sc = NULL; 11527 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11528 msg_info.scsi.tag_num = ctsio->tag_num; 11529 msg_info.scsi.tag_type = ctsio->tag_type; 11530 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11531 11532 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11533 11534 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11535 (void *)&msg_info, sizeof(msg_info), 0)) > 11536 CTL_HA_STATUS_SUCCESS) { 11537 printf("CTL:precheck, ctl_ha_msg_send returned %d\n", 11538 isc_retval); 11539 printf("CTL:opcode is %x\n", ctsio->cdb[0]); 11540 } else { 11541#if 0 11542 printf("CTL:Precheck sent msg, opcode is %x\n",opcode); 11543#endif 11544 } 11545 11546 /* 11547 * XXX KDM this I/O is off the incoming queue, but hasn't 11548 * been inserted on any other queue. We may need to come 11549 * up with a holding queue while we wait for serialization 11550 * so that we have an idea of what we're waiting for from 11551 * the other side. 11552 */ 11553 mtx_unlock(&lun->lun_lock); 11554 return (retval); 11555 } 11556 11557 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11558 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11559 ctl_ooaq, ooa_links))) { 11560 case CTL_ACTION_BLOCK: 11561 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11562 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11563 blocked_links); 11564 mtx_unlock(&lun->lun_lock); 11565 return (retval); 11566 case CTL_ACTION_PASS: 11567 case CTL_ACTION_SKIP: 11568 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11569 mtx_unlock(&lun->lun_lock); 11570 ctl_enqueue_rtr((union ctl_io *)ctsio); 11571 break; 11572 case CTL_ACTION_OVERLAP: 11573 mtx_unlock(&lun->lun_lock); 11574 ctl_set_overlapped_cmd(ctsio); 11575 ctl_done((union ctl_io *)ctsio); 11576 break; 11577 case CTL_ACTION_OVERLAP_TAG: 11578 mtx_unlock(&lun->lun_lock); 11579 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11580 ctl_done((union ctl_io *)ctsio); 11581 break; 11582 case CTL_ACTION_ERROR: 11583 default: 11584 mtx_unlock(&lun->lun_lock); 11585 ctl_set_internal_failure(ctsio, 11586 /*sks_valid*/ 0, 11587 /*retry_count*/ 0); 11588 ctl_done((union ctl_io *)ctsio); 11589 break; 11590 } 11591 return (retval); 11592} 11593 11594const struct ctl_cmd_entry * 11595ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11596{ 11597 const struct ctl_cmd_entry *entry; 11598 int service_action; 11599 11600 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11601 if (sa) 11602 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11603 if (entry->flags & CTL_CMD_FLAG_SA5) { 11604 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11605 entry = &((const struct ctl_cmd_entry *) 11606 entry->execute)[service_action]; 11607 } 11608 return (entry); 11609} 11610 11611const struct ctl_cmd_entry * 11612ctl_validate_command(struct ctl_scsiio *ctsio) 11613{ 11614 const struct ctl_cmd_entry *entry; 11615 int i, sa; 11616 uint8_t diff; 11617 11618 entry = ctl_get_cmd_entry(ctsio, &sa); 11619 if (entry->execute == NULL) { 11620 if (sa) 11621 ctl_set_invalid_field(ctsio, 11622 /*sks_valid*/ 1, 11623 /*command*/ 1, 11624 /*field*/ 1, 11625 /*bit_valid*/ 1, 11626 /*bit*/ 4); 11627 else 11628 ctl_set_invalid_opcode(ctsio); 11629 ctl_done((union ctl_io *)ctsio); 11630 return (NULL); 11631 } 11632 KASSERT(entry->length > 0, 11633 ("Not defined length for command 0x%02x/0x%02x", 11634 ctsio->cdb[0], ctsio->cdb[1])); 11635 for (i = 1; i < entry->length; i++) { 11636 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11637 if (diff == 0) 11638 continue; 11639 ctl_set_invalid_field(ctsio, 11640 /*sks_valid*/ 1, 11641 /*command*/ 1, 11642 /*field*/ i, 11643 /*bit_valid*/ 1, 11644 /*bit*/ fls(diff) - 1); 11645 ctl_done((union ctl_io *)ctsio); 11646 return (NULL); 11647 } 11648 return (entry); 11649} 11650 11651static int 11652ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11653{ 11654 11655 switch (lun_type) { 11656 case T_PROCESSOR: 11657 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) && 11658 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11659 return (0); 11660 break; 11661 case T_DIRECT: 11662 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) && 11663 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0)) 11664 return (0); 11665 break; 11666 default: 11667 return (0); 11668 } 11669 return (1); 11670} 11671 11672static int 11673ctl_scsiio(struct ctl_scsiio *ctsio) 11674{ 11675 int retval; 11676 const struct ctl_cmd_entry *entry; 11677 11678 retval = CTL_RETVAL_COMPLETE; 11679 11680 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11681 11682 entry = ctl_get_cmd_entry(ctsio, NULL); 11683 11684 /* 11685 * If this I/O has been aborted, just send it straight to 11686 * ctl_done() without executing it. 11687 */ 11688 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11689 ctl_done((union ctl_io *)ctsio); 11690 goto bailout; 11691 } 11692 11693 /* 11694 * All the checks should have been handled by ctl_scsiio_precheck(). 11695 * We should be clear now to just execute the I/O. 11696 */ 11697 retval = entry->execute(ctsio); 11698 11699bailout: 11700 return (retval); 11701} 11702 11703/* 11704 * Since we only implement one target right now, a bus reset simply resets 11705 * our single target. 11706 */ 11707static int 11708ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io) 11709{ 11710 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET)); 11711} 11712 11713static int 11714ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 11715 ctl_ua_type ua_type) 11716{ 11717 struct ctl_lun *lun; 11718 int retval; 11719 11720 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11721 union ctl_ha_msg msg_info; 11722 11723 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11724 msg_info.hdr.nexus = io->io_hdr.nexus; 11725 if (ua_type==CTL_UA_TARG_RESET) 11726 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11727 else 11728 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11729 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11730 msg_info.hdr.original_sc = NULL; 11731 msg_info.hdr.serializing_sc = NULL; 11732 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11733 (void *)&msg_info, sizeof(msg_info), 0)) { 11734 } 11735 } 11736 retval = 0; 11737 11738 mtx_lock(&ctl_softc->ctl_lock); 11739 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) 11740 retval += ctl_lun_reset(lun, io, ua_type); 11741 mtx_unlock(&ctl_softc->ctl_lock); 11742 11743 return (retval); 11744} 11745 11746/* 11747 * The LUN should always be set. The I/O is optional, and is used to 11748 * distinguish between I/Os sent by this initiator, and by other 11749 * initiators. We set unit attention for initiators other than this one. 11750 * SAM-3 is vague on this point. It does say that a unit attention should 11751 * be established for other initiators when a LUN is reset (see section 11752 * 5.7.3), but it doesn't specifically say that the unit attention should 11753 * be established for this particular initiator when a LUN is reset. Here 11754 * is the relevant text, from SAM-3 rev 8: 11755 * 11756 * 5.7.2 When a SCSI initiator port aborts its own tasks 11757 * 11758 * When a SCSI initiator port causes its own task(s) to be aborted, no 11759 * notification that the task(s) have been aborted shall be returned to 11760 * the SCSI initiator port other than the completion response for the 11761 * command or task management function action that caused the task(s) to 11762 * be aborted and notification(s) associated with related effects of the 11763 * action (e.g., a reset unit attention condition). 11764 * 11765 * XXX KDM for now, we're setting unit attention for all initiators. 11766 */ 11767static int 11768ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11769{ 11770 union ctl_io *xio; 11771#if 0 11772 uint32_t initidx; 11773#endif 11774 int i; 11775 11776 mtx_lock(&lun->lun_lock); 11777 /* 11778 * Run through the OOA queue and abort each I/O. 11779 */ 11780#if 0 11781 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11782#endif 11783 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11784 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11785 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11786 } 11787 11788 /* 11789 * This version sets unit attention for every 11790 */ 11791#if 0 11792 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11793 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11794 if (initidx == i) 11795 continue; 11796 lun->pending_ua[i] |= ua_type; 11797 } 11798#endif 11799 11800 /* 11801 * A reset (any kind, really) clears reservations established with 11802 * RESERVE/RELEASE. It does not clear reservations established 11803 * with PERSISTENT RESERVE OUT, but we don't support that at the 11804 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11805 * reservations made with the RESERVE/RELEASE commands, because 11806 * those commands are obsolete in SPC-3. 11807 */ 11808 lun->flags &= ~CTL_LUN_RESERVED; 11809 11810 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 11811#ifdef CTL_WITH_CA 11812 ctl_clear_mask(lun->have_ca, i); 11813#endif 11814 lun->pending_ua[i] |= ua_type; 11815 } 11816 mtx_unlock(&lun->lun_lock); 11817 11818 return (0); 11819} 11820 11821static void 11822ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11823 int other_sc) 11824{ 11825 union ctl_io *xio; 11826 11827 mtx_assert(&lun->lun_lock, MA_OWNED); 11828 11829 /* 11830 * Run through the OOA queue and attempt to find the given I/O. 11831 * The target port, initiator ID, tag type and tag number have to 11832 * match the values that we got from the initiator. If we have an 11833 * untagged command to abort, simply abort the first untagged command 11834 * we come to. We only allow one untagged command at a time of course. 11835 */ 11836 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11837 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11838 11839 if ((targ_port == UINT32_MAX || 11840 targ_port == xio->io_hdr.nexus.targ_port) && 11841 (init_id == UINT32_MAX || 11842 init_id == xio->io_hdr.nexus.initid.id)) { 11843 if (targ_port != xio->io_hdr.nexus.targ_port || 11844 init_id != xio->io_hdr.nexus.initid.id) 11845 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11846 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11847 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11848 union ctl_ha_msg msg_info; 11849 11850 msg_info.hdr.nexus = xio->io_hdr.nexus; 11851 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11852 msg_info.task.tag_num = xio->scsiio.tag_num; 11853 msg_info.task.tag_type = xio->scsiio.tag_type; 11854 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11855 msg_info.hdr.original_sc = NULL; 11856 msg_info.hdr.serializing_sc = NULL; 11857 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 11858 (void *)&msg_info, sizeof(msg_info), 0); 11859 } 11860 } 11861 } 11862} 11863 11864static int 11865ctl_abort_task_set(union ctl_io *io) 11866{ 11867 struct ctl_softc *softc = control_softc; 11868 struct ctl_lun *lun; 11869 uint32_t targ_lun; 11870 11871 /* 11872 * Look up the LUN. 11873 */ 11874 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11875 mtx_lock(&softc->ctl_lock); 11876 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL)) 11877 lun = softc->ctl_luns[targ_lun]; 11878 else { 11879 mtx_unlock(&softc->ctl_lock); 11880 return (1); 11881 } 11882 11883 mtx_lock(&lun->lun_lock); 11884 mtx_unlock(&softc->ctl_lock); 11885 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11886 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11887 io->io_hdr.nexus.initid.id, 11888 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11889 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11890 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11891 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11892 } 11893 mtx_unlock(&lun->lun_lock); 11894 return (0); 11895} 11896 11897static int 11898ctl_i_t_nexus_reset(union ctl_io *io) 11899{ 11900 struct ctl_softc *softc = control_softc; 11901 struct ctl_lun *lun; 11902 uint32_t initidx, residx; 11903 11904 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11905 residx = ctl_get_resindex(&io->io_hdr.nexus); 11906 mtx_lock(&softc->ctl_lock); 11907 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11908 mtx_lock(&lun->lun_lock); 11909 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11910 io->io_hdr.nexus.initid.id, 11911 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11912#ifdef CTL_WITH_CA 11913 ctl_clear_mask(lun->have_ca, initidx); 11914#endif 11915 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 11916 lun->flags &= ~CTL_LUN_RESERVED; 11917 lun->pending_ua[initidx] |= CTL_UA_I_T_NEXUS_LOSS; 11918 mtx_unlock(&lun->lun_lock); 11919 } 11920 mtx_unlock(&softc->ctl_lock); 11921 return (0); 11922} 11923 11924static int 11925ctl_abort_task(union ctl_io *io) 11926{ 11927 union ctl_io *xio; 11928 struct ctl_lun *lun; 11929 struct ctl_softc *ctl_softc; 11930#if 0 11931 struct sbuf sb; 11932 char printbuf[128]; 11933#endif 11934 int found; 11935 uint32_t targ_lun; 11936 11937 ctl_softc = control_softc; 11938 found = 0; 11939 11940 /* 11941 * Look up the LUN. 11942 */ 11943 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11944 mtx_lock(&ctl_softc->ctl_lock); 11945 if ((targ_lun < CTL_MAX_LUNS) 11946 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 11947 lun = ctl_softc->ctl_luns[targ_lun]; 11948 else { 11949 mtx_unlock(&ctl_softc->ctl_lock); 11950 return (1); 11951 } 11952 11953#if 0 11954 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 11955 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 11956#endif 11957 11958 mtx_lock(&lun->lun_lock); 11959 mtx_unlock(&ctl_softc->ctl_lock); 11960 /* 11961 * Run through the OOA queue and attempt to find the given I/O. 11962 * The target port, initiator ID, tag type and tag number have to 11963 * match the values that we got from the initiator. If we have an 11964 * untagged command to abort, simply abort the first untagged command 11965 * we come to. We only allow one untagged command at a time of course. 11966 */ 11967#if 0 11968 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) { 11969#endif 11970 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11971 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11972#if 0 11973 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 11974 11975 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 11976 lun->lun, xio->scsiio.tag_num, 11977 xio->scsiio.tag_type, 11978 (xio->io_hdr.blocked_links.tqe_prev 11979 == NULL) ? "" : " BLOCKED", 11980 (xio->io_hdr.flags & 11981 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 11982 (xio->io_hdr.flags & 11983 CTL_FLAG_ABORT) ? " ABORT" : "", 11984 (xio->io_hdr.flags & 11985 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 11986 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 11987 sbuf_finish(&sb); 11988 printf("%s\n", sbuf_data(&sb)); 11989#endif 11990 11991 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port) 11992 && (xio->io_hdr.nexus.initid.id == 11993 io->io_hdr.nexus.initid.id)) { 11994 /* 11995 * If the abort says that the task is untagged, the 11996 * task in the queue must be untagged. Otherwise, 11997 * we just check to see whether the tag numbers 11998 * match. This is because the QLogic firmware 11999 * doesn't pass back the tag type in an abort 12000 * request. 12001 */ 12002#if 0 12003 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12004 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12005 || (xio->scsiio.tag_num == io->taskio.tag_num)) { 12006#endif 12007 /* 12008 * XXX KDM we've got problems with FC, because it 12009 * doesn't send down a tag type with aborts. So we 12010 * can only really go by the tag number... 12011 * This may cause problems with parallel SCSI. 12012 * Need to figure that out!! 12013 */ 12014 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12015 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12016 found = 1; 12017 if ((io->io_hdr.flags & 12018 CTL_FLAG_FROM_OTHER_SC) == 0 && 12019 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12020 union ctl_ha_msg msg_info; 12021 12022 io->io_hdr.flags |= 12023 CTL_FLAG_SENT_2OTHER_SC; 12024 msg_info.hdr.nexus = io->io_hdr.nexus; 12025 msg_info.task.task_action = 12026 CTL_TASK_ABORT_TASK; 12027 msg_info.task.tag_num = 12028 io->taskio.tag_num; 12029 msg_info.task.tag_type = 12030 io->taskio.tag_type; 12031 msg_info.hdr.msg_type = 12032 CTL_MSG_MANAGE_TASKS; 12033 msg_info.hdr.original_sc = NULL; 12034 msg_info.hdr.serializing_sc = NULL; 12035#if 0 12036 printf("Sent Abort to other side\n"); 12037#endif 12038 if (CTL_HA_STATUS_SUCCESS != 12039 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12040 (void *)&msg_info, 12041 sizeof(msg_info), 0)) { 12042 } 12043 } 12044#if 0 12045 printf("ctl_abort_task: found I/O to abort\n"); 12046#endif 12047 break; 12048 } 12049 } 12050 } 12051 mtx_unlock(&lun->lun_lock); 12052 12053 if (found == 0) { 12054 /* 12055 * This isn't really an error. It's entirely possible for 12056 * the abort and command completion to cross on the wire. 12057 * This is more of an informative/diagnostic error. 12058 */ 12059#if 0 12060 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12061 "%d:%d:%d:%d tag %d type %d\n", 12062 io->io_hdr.nexus.initid.id, 12063 io->io_hdr.nexus.targ_port, 12064 io->io_hdr.nexus.targ_target.id, 12065 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12066 io->taskio.tag_type); 12067#endif 12068 } 12069 return (0); 12070} 12071 12072static void 12073ctl_run_task(union ctl_io *io) 12074{ 12075 struct ctl_softc *ctl_softc = control_softc; 12076 int retval = 1; 12077 const char *task_desc; 12078 12079 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12080 12081 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12082 ("ctl_run_task: Unextected io_type %d\n", 12083 io->io_hdr.io_type)); 12084 12085 task_desc = ctl_scsi_task_string(&io->taskio); 12086 if (task_desc != NULL) { 12087#ifdef NEEDTOPORT 12088 csevent_log(CSC_CTL | CSC_SHELF_SW | 12089 CTL_TASK_REPORT, 12090 csevent_LogType_Trace, 12091 csevent_Severity_Information, 12092 csevent_AlertLevel_Green, 12093 csevent_FRU_Firmware, 12094 csevent_FRU_Unknown, 12095 "CTL: received task: %s",task_desc); 12096#endif 12097 } else { 12098#ifdef NEEDTOPORT 12099 csevent_log(CSC_CTL | CSC_SHELF_SW | 12100 CTL_TASK_REPORT, 12101 csevent_LogType_Trace, 12102 csevent_Severity_Information, 12103 csevent_AlertLevel_Green, 12104 csevent_FRU_Firmware, 12105 csevent_FRU_Unknown, 12106 "CTL: received unknown task " 12107 "type: %d (%#x)", 12108 io->taskio.task_action, 12109 io->taskio.task_action); 12110#endif 12111 } 12112 switch (io->taskio.task_action) { 12113 case CTL_TASK_ABORT_TASK: 12114 retval = ctl_abort_task(io); 12115 break; 12116 case CTL_TASK_ABORT_TASK_SET: 12117 case CTL_TASK_CLEAR_TASK_SET: 12118 retval = ctl_abort_task_set(io); 12119 break; 12120 case CTL_TASK_CLEAR_ACA: 12121 break; 12122 case CTL_TASK_I_T_NEXUS_RESET: 12123 retval = ctl_i_t_nexus_reset(io); 12124 break; 12125 case CTL_TASK_LUN_RESET: { 12126 struct ctl_lun *lun; 12127 uint32_t targ_lun; 12128 12129 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12130 mtx_lock(&ctl_softc->ctl_lock); 12131 if ((targ_lun < CTL_MAX_LUNS) 12132 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 12133 lun = ctl_softc->ctl_luns[targ_lun]; 12134 else { 12135 mtx_unlock(&ctl_softc->ctl_lock); 12136 retval = 1; 12137 break; 12138 } 12139 12140 if (!(io->io_hdr.flags & 12141 CTL_FLAG_FROM_OTHER_SC)) { 12142 union ctl_ha_msg msg_info; 12143 12144 io->io_hdr.flags |= 12145 CTL_FLAG_SENT_2OTHER_SC; 12146 msg_info.hdr.msg_type = 12147 CTL_MSG_MANAGE_TASKS; 12148 msg_info.hdr.nexus = io->io_hdr.nexus; 12149 msg_info.task.task_action = 12150 CTL_TASK_LUN_RESET; 12151 msg_info.hdr.original_sc = NULL; 12152 msg_info.hdr.serializing_sc = NULL; 12153 if (CTL_HA_STATUS_SUCCESS != 12154 ctl_ha_msg_send(CTL_HA_CHAN_CTL, 12155 (void *)&msg_info, 12156 sizeof(msg_info), 0)) { 12157 } 12158 } 12159 12160 retval = ctl_lun_reset(lun, io, 12161 CTL_UA_LUN_RESET); 12162 mtx_unlock(&ctl_softc->ctl_lock); 12163 break; 12164 } 12165 case CTL_TASK_TARGET_RESET: 12166 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET); 12167 break; 12168 case CTL_TASK_BUS_RESET: 12169 retval = ctl_bus_reset(ctl_softc, io); 12170 break; 12171 case CTL_TASK_PORT_LOGIN: 12172 break; 12173 case CTL_TASK_PORT_LOGOUT: 12174 break; 12175 default: 12176 printf("ctl_run_task: got unknown task management event %d\n", 12177 io->taskio.task_action); 12178 break; 12179 } 12180 if (retval == 0) 12181 io->io_hdr.status = CTL_SUCCESS; 12182 else 12183 io->io_hdr.status = CTL_ERROR; 12184 ctl_done(io); 12185} 12186 12187/* 12188 * For HA operation. Handle commands that come in from the other 12189 * controller. 12190 */ 12191static void 12192ctl_handle_isc(union ctl_io *io) 12193{ 12194 int free_io; 12195 struct ctl_lun *lun; 12196 struct ctl_softc *ctl_softc; 12197 uint32_t targ_lun; 12198 12199 ctl_softc = control_softc; 12200 12201 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12202 lun = ctl_softc->ctl_luns[targ_lun]; 12203 12204 switch (io->io_hdr.msg_type) { 12205 case CTL_MSG_SERIALIZE: 12206 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12207 break; 12208 case CTL_MSG_R2R: { 12209 const struct ctl_cmd_entry *entry; 12210 12211 /* 12212 * This is only used in SER_ONLY mode. 12213 */ 12214 free_io = 0; 12215 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12216 mtx_lock(&lun->lun_lock); 12217 if (ctl_scsiio_lun_check(ctl_softc, lun, 12218 entry, (struct ctl_scsiio *)io) != 0) { 12219 mtx_unlock(&lun->lun_lock); 12220 ctl_done(io); 12221 break; 12222 } 12223 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12224 mtx_unlock(&lun->lun_lock); 12225 ctl_enqueue_rtr(io); 12226 break; 12227 } 12228 case CTL_MSG_FINISH_IO: 12229 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 12230 free_io = 0; 12231 ctl_done(io); 12232 } else { 12233 free_io = 1; 12234 mtx_lock(&lun->lun_lock); 12235 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12236 ooa_links); 12237 ctl_check_blocked(lun); 12238 mtx_unlock(&lun->lun_lock); 12239 } 12240 break; 12241 case CTL_MSG_PERS_ACTION: 12242 ctl_hndl_per_res_out_on_other_sc( 12243 (union ctl_ha_msg *)&io->presio.pr_msg); 12244 free_io = 1; 12245 break; 12246 case CTL_MSG_BAD_JUJU: 12247 free_io = 0; 12248 ctl_done(io); 12249 break; 12250 case CTL_MSG_DATAMOVE: 12251 /* Only used in XFER mode */ 12252 free_io = 0; 12253 ctl_datamove_remote(io); 12254 break; 12255 case CTL_MSG_DATAMOVE_DONE: 12256 /* Only used in XFER mode */ 12257 free_io = 0; 12258 io->scsiio.be_move_done(io); 12259 break; 12260 default: 12261 free_io = 1; 12262 printf("%s: Invalid message type %d\n", 12263 __func__, io->io_hdr.msg_type); 12264 break; 12265 } 12266 if (free_io) 12267 ctl_free_io(io); 12268 12269} 12270 12271 12272/* 12273 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12274 * there is no match. 12275 */ 12276static ctl_lun_error_pattern 12277ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12278{ 12279 const struct ctl_cmd_entry *entry; 12280 ctl_lun_error_pattern filtered_pattern, pattern; 12281 12282 pattern = desc->error_pattern; 12283 12284 /* 12285 * XXX KDM we need more data passed into this function to match a 12286 * custom pattern, and we actually need to implement custom pattern 12287 * matching. 12288 */ 12289 if (pattern & CTL_LUN_PAT_CMD) 12290 return (CTL_LUN_PAT_CMD); 12291 12292 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12293 return (CTL_LUN_PAT_ANY); 12294 12295 entry = ctl_get_cmd_entry(ctsio, NULL); 12296 12297 filtered_pattern = entry->pattern & pattern; 12298 12299 /* 12300 * If the user requested specific flags in the pattern (e.g. 12301 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12302 * flags. 12303 * 12304 * If the user did not specify any flags, it doesn't matter whether 12305 * or not the command supports the flags. 12306 */ 12307 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12308 (pattern & ~CTL_LUN_PAT_MASK)) 12309 return (CTL_LUN_PAT_NONE); 12310 12311 /* 12312 * If the user asked for a range check, see if the requested LBA 12313 * range overlaps with this command's LBA range. 12314 */ 12315 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12316 uint64_t lba1; 12317 uint64_t len1; 12318 ctl_action action; 12319 int retval; 12320 12321 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12322 if (retval != 0) 12323 return (CTL_LUN_PAT_NONE); 12324 12325 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12326 desc->lba_range.len); 12327 /* 12328 * A "pass" means that the LBA ranges don't overlap, so 12329 * this doesn't match the user's range criteria. 12330 */ 12331 if (action == CTL_ACTION_PASS) 12332 return (CTL_LUN_PAT_NONE); 12333 } 12334 12335 return (filtered_pattern); 12336} 12337 12338static void 12339ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12340{ 12341 struct ctl_error_desc *desc, *desc2; 12342 12343 mtx_assert(&lun->lun_lock, MA_OWNED); 12344 12345 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12346 ctl_lun_error_pattern pattern; 12347 /* 12348 * Check to see whether this particular command matches 12349 * the pattern in the descriptor. 12350 */ 12351 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12352 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12353 continue; 12354 12355 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12356 case CTL_LUN_INJ_ABORTED: 12357 ctl_set_aborted(&io->scsiio); 12358 break; 12359 case CTL_LUN_INJ_MEDIUM_ERR: 12360 ctl_set_medium_error(&io->scsiio); 12361 break; 12362 case CTL_LUN_INJ_UA: 12363 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12364 * OCCURRED */ 12365 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12366 break; 12367 case CTL_LUN_INJ_CUSTOM: 12368 /* 12369 * We're assuming the user knows what he is doing. 12370 * Just copy the sense information without doing 12371 * checks. 12372 */ 12373 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12374 ctl_min(sizeof(desc->custom_sense), 12375 sizeof(io->scsiio.sense_data))); 12376 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12377 io->scsiio.sense_len = SSD_FULL_SIZE; 12378 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12379 break; 12380 case CTL_LUN_INJ_NONE: 12381 default: 12382 /* 12383 * If this is an error injection type we don't know 12384 * about, clear the continuous flag (if it is set) 12385 * so it will get deleted below. 12386 */ 12387 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12388 break; 12389 } 12390 /* 12391 * By default, each error injection action is a one-shot 12392 */ 12393 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12394 continue; 12395 12396 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12397 12398 free(desc, M_CTL); 12399 } 12400} 12401 12402#ifdef CTL_IO_DELAY 12403static void 12404ctl_datamove_timer_wakeup(void *arg) 12405{ 12406 union ctl_io *io; 12407 12408 io = (union ctl_io *)arg; 12409 12410 ctl_datamove(io); 12411} 12412#endif /* CTL_IO_DELAY */ 12413 12414void 12415ctl_datamove(union ctl_io *io) 12416{ 12417 void (*fe_datamove)(union ctl_io *io); 12418 12419 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12420 12421 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12422 12423#ifdef CTL_TIME_IO 12424 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12425 char str[256]; 12426 char path_str[64]; 12427 struct sbuf sb; 12428 12429 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12430 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12431 12432 sbuf_cat(&sb, path_str); 12433 switch (io->io_hdr.io_type) { 12434 case CTL_IO_SCSI: 12435 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12436 sbuf_printf(&sb, "\n"); 12437 sbuf_cat(&sb, path_str); 12438 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12439 io->scsiio.tag_num, io->scsiio.tag_type); 12440 break; 12441 case CTL_IO_TASK: 12442 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12443 "Tag Type: %d\n", io->taskio.task_action, 12444 io->taskio.tag_num, io->taskio.tag_type); 12445 break; 12446 default: 12447 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12448 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12449 break; 12450 } 12451 sbuf_cat(&sb, path_str); 12452 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12453 (intmax_t)time_uptime - io->io_hdr.start_time); 12454 sbuf_finish(&sb); 12455 printf("%s", sbuf_data(&sb)); 12456 } 12457#endif /* CTL_TIME_IO */ 12458 12459#ifdef CTL_IO_DELAY 12460 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12461 struct ctl_lun *lun; 12462 12463 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12464 12465 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12466 } else { 12467 struct ctl_lun *lun; 12468 12469 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12470 if ((lun != NULL) 12471 && (lun->delay_info.datamove_delay > 0)) { 12472 struct callout *callout; 12473 12474 callout = (struct callout *)&io->io_hdr.timer_bytes; 12475 callout_init(callout, /*mpsafe*/ 1); 12476 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12477 callout_reset(callout, 12478 lun->delay_info.datamove_delay * hz, 12479 ctl_datamove_timer_wakeup, io); 12480 if (lun->delay_info.datamove_type == 12481 CTL_DELAY_TYPE_ONESHOT) 12482 lun->delay_info.datamove_delay = 0; 12483 return; 12484 } 12485 } 12486#endif 12487 12488 /* 12489 * This command has been aborted. Set the port status, so we fail 12490 * the data move. 12491 */ 12492 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12493 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n", 12494 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id, 12495 io->io_hdr.nexus.targ_port, 12496 (uintmax_t)io->io_hdr.nexus.targ_target.id, 12497 io->io_hdr.nexus.targ_lun); 12498 io->io_hdr.port_status = 31337; 12499 /* 12500 * Note that the backend, in this case, will get the 12501 * callback in its context. In other cases it may get 12502 * called in the frontend's interrupt thread context. 12503 */ 12504 io->scsiio.be_move_done(io); 12505 return; 12506 } 12507 12508 /* Don't confuse frontend with zero length data move. */ 12509 if (io->scsiio.kern_data_len == 0) { 12510 io->scsiio.be_move_done(io); 12511 return; 12512 } 12513 12514 /* 12515 * If we're in XFER mode and this I/O is from the other shelf 12516 * controller, we need to send the DMA to the other side to 12517 * actually transfer the data to/from the host. In serialize only 12518 * mode the transfer happens below CTL and ctl_datamove() is only 12519 * called on the machine that originally received the I/O. 12520 */ 12521 if ((control_softc->ha_mode == CTL_HA_MODE_XFER) 12522 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12523 union ctl_ha_msg msg; 12524 uint32_t sg_entries_sent; 12525 int do_sg_copy; 12526 int i; 12527 12528 memset(&msg, 0, sizeof(msg)); 12529 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 12530 msg.hdr.original_sc = io->io_hdr.original_sc; 12531 msg.hdr.serializing_sc = io; 12532 msg.hdr.nexus = io->io_hdr.nexus; 12533 msg.dt.flags = io->io_hdr.flags; 12534 /* 12535 * We convert everything into a S/G list here. We can't 12536 * pass by reference, only by value between controllers. 12537 * So we can't pass a pointer to the S/G list, only as many 12538 * S/G entries as we can fit in here. If it's possible for 12539 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 12540 * then we need to break this up into multiple transfers. 12541 */ 12542 if (io->scsiio.kern_sg_entries == 0) { 12543 msg.dt.kern_sg_entries = 1; 12544 /* 12545 * If this is in cached memory, flush the cache 12546 * before we send the DMA request to the other 12547 * controller. We want to do this in either the 12548 * read or the write case. The read case is 12549 * straightforward. In the write case, we want to 12550 * make sure nothing is in the local cache that 12551 * could overwrite the DMAed data. 12552 */ 12553 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12554 /* 12555 * XXX KDM use bus_dmamap_sync() here. 12556 */ 12557 } 12558 12559 /* 12560 * Convert to a physical address if this is a 12561 * virtual address. 12562 */ 12563 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 12564 msg.dt.sg_list[0].addr = 12565 io->scsiio.kern_data_ptr; 12566 } else { 12567 /* 12568 * XXX KDM use busdma here! 12569 */ 12570#if 0 12571 msg.dt.sg_list[0].addr = (void *) 12572 vtophys(io->scsiio.kern_data_ptr); 12573#endif 12574 } 12575 12576 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 12577 do_sg_copy = 0; 12578 } else { 12579 struct ctl_sg_entry *sgl; 12580 12581 do_sg_copy = 1; 12582 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 12583 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 12584 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 12585 /* 12586 * XXX KDM use bus_dmamap_sync() here. 12587 */ 12588 } 12589 } 12590 12591 msg.dt.kern_data_len = io->scsiio.kern_data_len; 12592 msg.dt.kern_total_len = io->scsiio.kern_total_len; 12593 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 12594 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 12595 msg.dt.sg_sequence = 0; 12596 12597 /* 12598 * Loop until we've sent all of the S/G entries. On the 12599 * other end, we'll recompose these S/G entries into one 12600 * contiguous list before passing it to the 12601 */ 12602 for (sg_entries_sent = 0; sg_entries_sent < 12603 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) { 12604 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/ 12605 sizeof(msg.dt.sg_list[0])), 12606 msg.dt.kern_sg_entries - sg_entries_sent); 12607 12608 if (do_sg_copy != 0) { 12609 struct ctl_sg_entry *sgl; 12610 int j; 12611 12612 sgl = (struct ctl_sg_entry *) 12613 io->scsiio.kern_data_ptr; 12614 /* 12615 * If this is in cached memory, flush the cache 12616 * before we send the DMA request to the other 12617 * controller. We want to do this in either 12618 * the * read or the write case. The read 12619 * case is straightforward. In the write 12620 * case, we want to make sure nothing is 12621 * in the local cache that could overwrite 12622 * the DMAed data. 12623 */ 12624 12625 for (i = sg_entries_sent, j = 0; 12626 i < msg.dt.cur_sg_entries; i++, j++) { 12627 if ((io->io_hdr.flags & 12628 CTL_FLAG_NO_DATASYNC) == 0) { 12629 /* 12630 * XXX KDM use bus_dmamap_sync() 12631 */ 12632 } 12633 if ((io->io_hdr.flags & 12634 CTL_FLAG_BUS_ADDR) == 0) { 12635 /* 12636 * XXX KDM use busdma. 12637 */ 12638#if 0 12639 msg.dt.sg_list[j].addr =(void *) 12640 vtophys(sgl[i].addr); 12641#endif 12642 } else { 12643 msg.dt.sg_list[j].addr = 12644 sgl[i].addr; 12645 } 12646 msg.dt.sg_list[j].len = sgl[i].len; 12647 } 12648 } 12649 12650 sg_entries_sent += msg.dt.cur_sg_entries; 12651 if (sg_entries_sent >= msg.dt.kern_sg_entries) 12652 msg.dt.sg_last = 1; 12653 else 12654 msg.dt.sg_last = 0; 12655 12656 /* 12657 * XXX KDM drop and reacquire the lock here? 12658 */ 12659 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12660 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 12661 /* 12662 * XXX do something here. 12663 */ 12664 } 12665 12666 msg.dt.sent_sg_entries = sg_entries_sent; 12667 } 12668 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12669 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) 12670 ctl_failover_io(io, /*have_lock*/ 0); 12671 12672 } else { 12673 12674 /* 12675 * Lookup the fe_datamove() function for this particular 12676 * front end. 12677 */ 12678 fe_datamove = 12679 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12680 12681 fe_datamove(io); 12682 } 12683} 12684 12685static void 12686ctl_send_datamove_done(union ctl_io *io, int have_lock) 12687{ 12688 union ctl_ha_msg msg; 12689 int isc_status; 12690 12691 memset(&msg, 0, sizeof(msg)); 12692 12693 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12694 msg.hdr.original_sc = io; 12695 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12696 msg.hdr.nexus = io->io_hdr.nexus; 12697 msg.hdr.status = io->io_hdr.status; 12698 msg.scsi.tag_num = io->scsiio.tag_num; 12699 msg.scsi.tag_type = io->scsiio.tag_type; 12700 msg.scsi.scsi_status = io->scsiio.scsi_status; 12701 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12702 sizeof(io->scsiio.sense_data)); 12703 msg.scsi.sense_len = io->scsiio.sense_len; 12704 msg.scsi.sense_residual = io->scsiio.sense_residual; 12705 msg.scsi.fetd_status = io->io_hdr.port_status; 12706 msg.scsi.residual = io->scsiio.residual; 12707 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12708 12709 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12710 ctl_failover_io(io, /*have_lock*/ have_lock); 12711 return; 12712 } 12713 12714 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0); 12715 if (isc_status > CTL_HA_STATUS_SUCCESS) { 12716 /* XXX do something if this fails */ 12717 } 12718 12719} 12720 12721/* 12722 * The DMA to the remote side is done, now we need to tell the other side 12723 * we're done so it can continue with its data movement. 12724 */ 12725static void 12726ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12727{ 12728 union ctl_io *io; 12729 12730 io = rq->context; 12731 12732 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12733 printf("%s: ISC DMA write failed with error %d", __func__, 12734 rq->ret); 12735 ctl_set_internal_failure(&io->scsiio, 12736 /*sks_valid*/ 1, 12737 /*retry_count*/ rq->ret); 12738 } 12739 12740 ctl_dt_req_free(rq); 12741 12742 /* 12743 * In this case, we had to malloc the memory locally. Free it. 12744 */ 12745 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12746 int i; 12747 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12748 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12749 } 12750 /* 12751 * The data is in local and remote memory, so now we need to send 12752 * status (good or back) back to the other side. 12753 */ 12754 ctl_send_datamove_done(io, /*have_lock*/ 0); 12755} 12756 12757/* 12758 * We've moved the data from the host/controller into local memory. Now we 12759 * need to push it over to the remote controller's memory. 12760 */ 12761static int 12762ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12763{ 12764 int retval; 12765 12766 retval = 0; 12767 12768 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12769 ctl_datamove_remote_write_cb); 12770 12771 return (retval); 12772} 12773 12774static void 12775ctl_datamove_remote_write(union ctl_io *io) 12776{ 12777 int retval; 12778 void (*fe_datamove)(union ctl_io *io); 12779 12780 /* 12781 * - Get the data from the host/HBA into local memory. 12782 * - DMA memory from the local controller to the remote controller. 12783 * - Send status back to the remote controller. 12784 */ 12785 12786 retval = ctl_datamove_remote_sgl_setup(io); 12787 if (retval != 0) 12788 return; 12789 12790 /* Switch the pointer over so the FETD knows what to do */ 12791 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12792 12793 /* 12794 * Use a custom move done callback, since we need to send completion 12795 * back to the other controller, not to the backend on this side. 12796 */ 12797 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12798 12799 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12800 12801 fe_datamove(io); 12802 12803 return; 12804 12805} 12806 12807static int 12808ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12809{ 12810#if 0 12811 char str[256]; 12812 char path_str[64]; 12813 struct sbuf sb; 12814#endif 12815 12816 /* 12817 * In this case, we had to malloc the memory locally. Free it. 12818 */ 12819 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) { 12820 int i; 12821 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12822 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12823 } 12824 12825#if 0 12826 scsi_path_string(io, path_str, sizeof(path_str)); 12827 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12828 sbuf_cat(&sb, path_str); 12829 scsi_command_string(&io->scsiio, NULL, &sb); 12830 sbuf_printf(&sb, "\n"); 12831 sbuf_cat(&sb, path_str); 12832 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12833 io->scsiio.tag_num, io->scsiio.tag_type); 12834 sbuf_cat(&sb, path_str); 12835 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12836 io->io_hdr.flags, io->io_hdr.status); 12837 sbuf_finish(&sb); 12838 printk("%s", sbuf_data(&sb)); 12839#endif 12840 12841 12842 /* 12843 * The read is done, now we need to send status (good or bad) back 12844 * to the other side. 12845 */ 12846 ctl_send_datamove_done(io, /*have_lock*/ 0); 12847 12848 return (0); 12849} 12850 12851static void 12852ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12853{ 12854 union ctl_io *io; 12855 void (*fe_datamove)(union ctl_io *io); 12856 12857 io = rq->context; 12858 12859 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12860 printf("%s: ISC DMA read failed with error %d", __func__, 12861 rq->ret); 12862 ctl_set_internal_failure(&io->scsiio, 12863 /*sks_valid*/ 1, 12864 /*retry_count*/ rq->ret); 12865 } 12866 12867 ctl_dt_req_free(rq); 12868 12869 /* Switch the pointer over so the FETD knows what to do */ 12870 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12871 12872 /* 12873 * Use a custom move done callback, since we need to send completion 12874 * back to the other controller, not to the backend on this side. 12875 */ 12876 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12877 12878 /* XXX KDM add checks like the ones in ctl_datamove? */ 12879 12880 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove; 12881 12882 fe_datamove(io); 12883} 12884 12885static int 12886ctl_datamove_remote_sgl_setup(union ctl_io *io) 12887{ 12888 struct ctl_sg_entry *local_sglist, *remote_sglist; 12889 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist; 12890 struct ctl_softc *softc; 12891 int retval; 12892 int i; 12893 12894 retval = 0; 12895 softc = control_softc; 12896 12897 local_sglist = io->io_hdr.local_sglist; 12898 local_dma_sglist = io->io_hdr.local_dma_sglist; 12899 remote_sglist = io->io_hdr.remote_sglist; 12900 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 12901 12902 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) { 12903 for (i = 0; i < io->scsiio.kern_sg_entries; i++) { 12904 local_sglist[i].len = remote_sglist[i].len; 12905 12906 /* 12907 * XXX Detect the situation where the RS-level I/O 12908 * redirector on the other side has already read the 12909 * data off of the AOR RS on this side, and 12910 * transferred it to remote (mirror) memory on the 12911 * other side. Since we already have the data in 12912 * memory here, we just need to use it. 12913 * 12914 * XXX KDM this can probably be removed once we 12915 * get the cache device code in and take the 12916 * current AOR implementation out. 12917 */ 12918#ifdef NEEDTOPORT 12919 if ((remote_sglist[i].addr >= 12920 (void *)vtophys(softc->mirr->addr)) 12921 && (remote_sglist[i].addr < 12922 ((void *)vtophys(softc->mirr->addr) + 12923 CacheMirrorOffset))) { 12924 local_sglist[i].addr = remote_sglist[i].addr - 12925 CacheMirrorOffset; 12926 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 12927 CTL_FLAG_DATA_IN) 12928 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE; 12929 } else { 12930 local_sglist[i].addr = remote_sglist[i].addr + 12931 CacheMirrorOffset; 12932 } 12933#endif 12934#if 0 12935 printf("%s: local %p, remote %p, len %d\n", 12936 __func__, local_sglist[i].addr, 12937 remote_sglist[i].addr, local_sglist[i].len); 12938#endif 12939 } 12940 } else { 12941 uint32_t len_to_go; 12942 12943 /* 12944 * In this case, we don't have automatically allocated 12945 * memory for this I/O on this controller. This typically 12946 * happens with internal CTL I/O -- e.g. inquiry, mode 12947 * sense, etc. Anything coming from RAIDCore will have 12948 * a mirror area available. 12949 */ 12950 len_to_go = io->scsiio.kern_data_len; 12951 12952 /* 12953 * Clear the no datasync flag, we have to use malloced 12954 * buffers. 12955 */ 12956 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC; 12957 12958 /* 12959 * The difficult thing here is that the size of the various 12960 * S/G segments may be different than the size from the 12961 * remote controller. That'll make it harder when DMAing 12962 * the data back to the other side. 12963 */ 12964 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) / 12965 sizeof(io->io_hdr.remote_sglist[0])) && 12966 (len_to_go > 0); i++) { 12967 local_sglist[i].len = ctl_min(len_to_go, 131072); 12968 CTL_SIZE_8B(local_dma_sglist[i].len, 12969 local_sglist[i].len); 12970 local_sglist[i].addr = 12971 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK); 12972 12973 local_dma_sglist[i].addr = local_sglist[i].addr; 12974 12975 if (local_sglist[i].addr == NULL) { 12976 int j; 12977 12978 printf("malloc failed for %zd bytes!", 12979 local_dma_sglist[i].len); 12980 for (j = 0; j < i; j++) { 12981 free(local_sglist[j].addr, M_CTL); 12982 } 12983 ctl_set_internal_failure(&io->scsiio, 12984 /*sks_valid*/ 1, 12985 /*retry_count*/ 4857); 12986 retval = 1; 12987 goto bailout_error; 12988 12989 } 12990 /* XXX KDM do we need a sync here? */ 12991 12992 len_to_go -= local_sglist[i].len; 12993 } 12994 /* 12995 * Reset the number of S/G entries accordingly. The 12996 * original number of S/G entries is available in 12997 * rem_sg_entries. 12998 */ 12999 io->scsiio.kern_sg_entries = i; 13000 13001#if 0 13002 printf("%s: kern_sg_entries = %d\n", __func__, 13003 io->scsiio.kern_sg_entries); 13004 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13005 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i, 13006 local_sglist[i].addr, local_sglist[i].len, 13007 local_dma_sglist[i].len); 13008#endif 13009 } 13010 13011 13012 return (retval); 13013 13014bailout_error: 13015 13016 ctl_send_datamove_done(io, /*have_lock*/ 0); 13017 13018 return (retval); 13019} 13020 13021static int 13022ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 13023 ctl_ha_dt_cb callback) 13024{ 13025 struct ctl_ha_dt_req *rq; 13026 struct ctl_sg_entry *remote_sglist, *local_sglist; 13027 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist; 13028 uint32_t local_used, remote_used, total_used; 13029 int retval; 13030 int i, j; 13031 13032 retval = 0; 13033 13034 rq = ctl_dt_req_alloc(); 13035 13036 /* 13037 * If we failed to allocate the request, and if the DMA didn't fail 13038 * anyway, set busy status. This is just a resource allocation 13039 * failure. 13040 */ 13041 if ((rq == NULL) 13042 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE)) 13043 ctl_set_busy(&io->scsiio); 13044 13045 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) { 13046 13047 if (rq != NULL) 13048 ctl_dt_req_free(rq); 13049 13050 /* 13051 * The data move failed. We need to return status back 13052 * to the other controller. No point in trying to DMA 13053 * data to the remote controller. 13054 */ 13055 13056 ctl_send_datamove_done(io, /*have_lock*/ 0); 13057 13058 retval = 1; 13059 13060 goto bailout; 13061 } 13062 13063 local_sglist = io->io_hdr.local_sglist; 13064 local_dma_sglist = io->io_hdr.local_dma_sglist; 13065 remote_sglist = io->io_hdr.remote_sglist; 13066 remote_dma_sglist = io->io_hdr.remote_dma_sglist; 13067 local_used = 0; 13068 remote_used = 0; 13069 total_used = 0; 13070 13071 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) { 13072 rq->ret = CTL_HA_STATUS_SUCCESS; 13073 rq->context = io; 13074 callback(rq); 13075 goto bailout; 13076 } 13077 13078 /* 13079 * Pull/push the data over the wire from/to the other controller. 13080 * This takes into account the possibility that the local and 13081 * remote sglists may not be identical in terms of the size of 13082 * the elements and the number of elements. 13083 * 13084 * One fundamental assumption here is that the length allocated for 13085 * both the local and remote sglists is identical. Otherwise, we've 13086 * essentially got a coding error of some sort. 13087 */ 13088 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 13089 int isc_ret; 13090 uint32_t cur_len, dma_length; 13091 uint8_t *tmp_ptr; 13092 13093 rq->id = CTL_HA_DATA_CTL; 13094 rq->command = command; 13095 rq->context = io; 13096 13097 /* 13098 * Both pointers should be aligned. But it is possible 13099 * that the allocation length is not. They should both 13100 * also have enough slack left over at the end, though, 13101 * to round up to the next 8 byte boundary. 13102 */ 13103 cur_len = ctl_min(local_sglist[i].len - local_used, 13104 remote_sglist[j].len - remote_used); 13105 13106 /* 13107 * In this case, we have a size issue and need to decrease 13108 * the size, except in the case where we actually have less 13109 * than 8 bytes left. In that case, we need to increase 13110 * the DMA length to get the last bit. 13111 */ 13112 if ((cur_len & 0x7) != 0) { 13113 if (cur_len > 0x7) { 13114 cur_len = cur_len - (cur_len & 0x7); 13115 dma_length = cur_len; 13116 } else { 13117 CTL_SIZE_8B(dma_length, cur_len); 13118 } 13119 13120 } else 13121 dma_length = cur_len; 13122 13123 /* 13124 * If we had to allocate memory for this I/O, instead of using 13125 * the non-cached mirror memory, we'll need to flush the cache 13126 * before trying to DMA to the other controller. 13127 * 13128 * We could end up doing this multiple times for the same 13129 * segment if we have a larger local segment than remote 13130 * segment. That shouldn't be an issue. 13131 */ 13132 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) { 13133 /* 13134 * XXX KDM use bus_dmamap_sync() here. 13135 */ 13136 } 13137 13138 rq->size = dma_length; 13139 13140 tmp_ptr = (uint8_t *)local_sglist[i].addr; 13141 tmp_ptr += local_used; 13142 13143 /* Use physical addresses when talking to ISC hardware */ 13144 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 13145 /* XXX KDM use busdma */ 13146#if 0 13147 rq->local = vtophys(tmp_ptr); 13148#endif 13149 } else 13150 rq->local = tmp_ptr; 13151 13152 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 13153 tmp_ptr += remote_used; 13154 rq->remote = tmp_ptr; 13155 13156 rq->callback = NULL; 13157 13158 local_used += cur_len; 13159 if (local_used >= local_sglist[i].len) { 13160 i++; 13161 local_used = 0; 13162 } 13163 13164 remote_used += cur_len; 13165 if (remote_used >= remote_sglist[j].len) { 13166 j++; 13167 remote_used = 0; 13168 } 13169 total_used += cur_len; 13170 13171 if (total_used >= io->scsiio.kern_data_len) 13172 rq->callback = callback; 13173 13174 if ((rq->size & 0x7) != 0) { 13175 printf("%s: warning: size %d is not on 8b boundary\n", 13176 __func__, rq->size); 13177 } 13178 if (((uintptr_t)rq->local & 0x7) != 0) { 13179 printf("%s: warning: local %p not on 8b boundary\n", 13180 __func__, rq->local); 13181 } 13182 if (((uintptr_t)rq->remote & 0x7) != 0) { 13183 printf("%s: warning: remote %p not on 8b boundary\n", 13184 __func__, rq->local); 13185 } 13186#if 0 13187 printf("%s: %s: local %#x remote %#x size %d\n", __func__, 13188 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 13189 rq->local, rq->remote, rq->size); 13190#endif 13191 13192 isc_ret = ctl_dt_single(rq); 13193 if (isc_ret == CTL_HA_STATUS_WAIT) 13194 continue; 13195 13196 if (isc_ret == CTL_HA_STATUS_DISCONNECT) { 13197 rq->ret = CTL_HA_STATUS_SUCCESS; 13198 } else { 13199 rq->ret = isc_ret; 13200 } 13201 callback(rq); 13202 goto bailout; 13203 } 13204 13205bailout: 13206 return (retval); 13207 13208} 13209 13210static void 13211ctl_datamove_remote_read(union ctl_io *io) 13212{ 13213 int retval; 13214 int i; 13215 13216 /* 13217 * This will send an error to the other controller in the case of a 13218 * failure. 13219 */ 13220 retval = ctl_datamove_remote_sgl_setup(io); 13221 if (retval != 0) 13222 return; 13223 13224 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13225 ctl_datamove_remote_read_cb); 13226 if ((retval != 0) 13227 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) { 13228 /* 13229 * Make sure we free memory if there was an error.. The 13230 * ctl_datamove_remote_xfer() function will send the 13231 * datamove done message, or call the callback with an 13232 * error if there is a problem. 13233 */ 13234 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13235 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13236 } 13237 13238 return; 13239} 13240 13241/* 13242 * Process a datamove request from the other controller. This is used for 13243 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13244 * first. Once that is complete, the data gets DMAed into the remote 13245 * controller's memory. For reads, we DMA from the remote controller's 13246 * memory into our memory first, and then move it out to the FETD. 13247 */ 13248static void 13249ctl_datamove_remote(union ctl_io *io) 13250{ 13251 struct ctl_softc *softc; 13252 13253 softc = control_softc; 13254 13255 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 13256 13257 /* 13258 * Note that we look for an aborted I/O here, but don't do some of 13259 * the other checks that ctl_datamove() normally does. 13260 * We don't need to run the datamove delay code, since that should 13261 * have been done if need be on the other controller. 13262 */ 13263 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13264 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__, 13265 io->scsiio.tag_num, io->io_hdr.nexus.initid.id, 13266 io->io_hdr.nexus.targ_port, 13267 io->io_hdr.nexus.targ_target.id, 13268 io->io_hdr.nexus.targ_lun); 13269 io->io_hdr.port_status = 31338; 13270 ctl_send_datamove_done(io, /*have_lock*/ 0); 13271 return; 13272 } 13273 13274 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) { 13275 ctl_datamove_remote_write(io); 13276 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){ 13277 ctl_datamove_remote_read(io); 13278 } else { 13279 union ctl_ha_msg msg; 13280 struct scsi_sense_data *sense; 13281 uint8_t sks[3]; 13282 int retry_count; 13283 13284 memset(&msg, 0, sizeof(msg)); 13285 13286 msg.hdr.msg_type = CTL_MSG_BAD_JUJU; 13287 msg.hdr.status = CTL_SCSI_ERROR; 13288 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND; 13289 13290 retry_count = 4243; 13291 13292 sense = &msg.scsi.sense_data; 13293 sks[0] = SSD_SCS_VALID; 13294 sks[1] = (retry_count >> 8) & 0xff; 13295 sks[2] = retry_count & 0xff; 13296 13297 /* "Internal target failure" */ 13298 scsi_set_sense_data(sense, 13299 /*sense_format*/ SSD_TYPE_NONE, 13300 /*current_error*/ 1, 13301 /*sense_key*/ SSD_KEY_HARDWARE_ERROR, 13302 /*asc*/ 0x44, 13303 /*ascq*/ 0x00, 13304 /*type*/ SSD_ELEM_SKS, 13305 /*size*/ sizeof(sks), 13306 /*data*/ sks, 13307 SSD_ELEM_NONE); 13308 13309 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 13310 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13311 ctl_failover_io(io, /*have_lock*/ 1); 13312 return; 13313 } 13314 13315 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) > 13316 CTL_HA_STATUS_SUCCESS) { 13317 /* XXX KDM what to do if this fails? */ 13318 } 13319 return; 13320 } 13321 13322} 13323 13324static int 13325ctl_process_done(union ctl_io *io) 13326{ 13327 struct ctl_lun *lun; 13328 struct ctl_softc *ctl_softc = control_softc; 13329 void (*fe_done)(union ctl_io *io); 13330 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port); 13331 13332 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13333 13334 fe_done = 13335 control_softc->ctl_ports[targ_port]->fe_done; 13336 13337#ifdef CTL_TIME_IO 13338 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13339 char str[256]; 13340 char path_str[64]; 13341 struct sbuf sb; 13342 13343 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13344 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13345 13346 sbuf_cat(&sb, path_str); 13347 switch (io->io_hdr.io_type) { 13348 case CTL_IO_SCSI: 13349 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13350 sbuf_printf(&sb, "\n"); 13351 sbuf_cat(&sb, path_str); 13352 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13353 io->scsiio.tag_num, io->scsiio.tag_type); 13354 break; 13355 case CTL_IO_TASK: 13356 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13357 "Tag Type: %d\n", io->taskio.task_action, 13358 io->taskio.tag_num, io->taskio.tag_type); 13359 break; 13360 default: 13361 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13362 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13363 break; 13364 } 13365 sbuf_cat(&sb, path_str); 13366 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13367 (intmax_t)time_uptime - io->io_hdr.start_time); 13368 sbuf_finish(&sb); 13369 printf("%s", sbuf_data(&sb)); 13370 } 13371#endif /* CTL_TIME_IO */ 13372 13373 switch (io->io_hdr.io_type) { 13374 case CTL_IO_SCSI: 13375 break; 13376 case CTL_IO_TASK: 13377 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO)) 13378 ctl_io_error_print(io, NULL); 13379 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) 13380 ctl_free_io(io); 13381 else 13382 fe_done(io); 13383 return (CTL_RETVAL_COMPLETE); 13384 default: 13385 panic("ctl_process_done: invalid io type %d\n", 13386 io->io_hdr.io_type); 13387 break; /* NOTREACHED */ 13388 } 13389 13390 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13391 if (lun == NULL) { 13392 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13393 io->io_hdr.nexus.targ_mapped_lun)); 13394 goto bailout; 13395 } 13396 13397 mtx_lock(&lun->lun_lock); 13398 13399 /* 13400 * Check to see if we have any errors to inject here. We only 13401 * inject errors for commands that don't already have errors set. 13402 */ 13403 if ((STAILQ_FIRST(&lun->error_list) != NULL) && 13404 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13405 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13406 ctl_inject_error(lun, io); 13407 13408 /* 13409 * XXX KDM how do we treat commands that aren't completed 13410 * successfully? 13411 * 13412 * XXX KDM should we also track I/O latency? 13413 */ 13414 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13415 io->io_hdr.io_type == CTL_IO_SCSI) { 13416#ifdef CTL_TIME_IO 13417 struct bintime cur_bt; 13418#endif 13419 int type; 13420 13421 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13422 CTL_FLAG_DATA_IN) 13423 type = CTL_STATS_READ; 13424 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13425 CTL_FLAG_DATA_OUT) 13426 type = CTL_STATS_WRITE; 13427 else 13428 type = CTL_STATS_NO_IO; 13429 13430 lun->stats.ports[targ_port].bytes[type] += 13431 io->scsiio.kern_total_len; 13432 lun->stats.ports[targ_port].operations[type]++; 13433#ifdef CTL_TIME_IO 13434 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13435 &io->io_hdr.dma_bt); 13436 lun->stats.ports[targ_port].num_dmas[type] += 13437 io->io_hdr.num_dmas; 13438 getbintime(&cur_bt); 13439 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13440 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13441#endif 13442 } 13443 13444 /* 13445 * Remove this from the OOA queue. 13446 */ 13447 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13448 13449 /* 13450 * Run through the blocked queue on this LUN and see if anything 13451 * has become unblocked, now that this transaction is done. 13452 */ 13453 ctl_check_blocked(lun); 13454 13455 /* 13456 * If the LUN has been invalidated, free it if there is nothing 13457 * left on its OOA queue. 13458 */ 13459 if ((lun->flags & CTL_LUN_INVALID) 13460 && TAILQ_EMPTY(&lun->ooa_queue)) { 13461 mtx_unlock(&lun->lun_lock); 13462 mtx_lock(&ctl_softc->ctl_lock); 13463 ctl_free_lun(lun); 13464 mtx_unlock(&ctl_softc->ctl_lock); 13465 } else 13466 mtx_unlock(&lun->lun_lock); 13467 13468bailout: 13469 13470 /* 13471 * If this command has been aborted, make sure we set the status 13472 * properly. The FETD is responsible for freeing the I/O and doing 13473 * whatever it needs to do to clean up its state. 13474 */ 13475 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13476 ctl_set_task_aborted(&io->scsiio); 13477 13478 /* 13479 * If enabled, print command error status. 13480 * We don't print UAs unless debugging was enabled explicitly. 13481 */ 13482 do { 13483 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) 13484 break; 13485 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0) 13486 break; 13487 if ((ctl_debug & CTL_DEBUG_INFO) == 0 && 13488 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) && 13489 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) { 13490 int error_code, sense_key, asc, ascq; 13491 13492 scsi_extract_sense_len(&io->scsiio.sense_data, 13493 io->scsiio.sense_len, &error_code, &sense_key, 13494 &asc, &ascq, /*show_errors*/ 0); 13495 if (sense_key == SSD_KEY_UNIT_ATTENTION) 13496 break; 13497 } 13498 13499 ctl_io_error_print(io, NULL); 13500 } while (0); 13501 13502 /* 13503 * Tell the FETD or the other shelf controller we're done with this 13504 * command. Note that only SCSI commands get to this point. Task 13505 * management commands are completed above. 13506 * 13507 * We only send status to the other controller if we're in XFER 13508 * mode. In SER_ONLY mode, the I/O is done on the controller that 13509 * received the I/O (from CTL's perspective), and so the status is 13510 * generated there. 13511 * 13512 * XXX KDM if we hold the lock here, we could cause a deadlock 13513 * if the frontend comes back in in this context to queue 13514 * something. 13515 */ 13516 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER) 13517 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 13518 union ctl_ha_msg msg; 13519 13520 memset(&msg, 0, sizeof(msg)); 13521 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13522 msg.hdr.original_sc = io->io_hdr.original_sc; 13523 msg.hdr.nexus = io->io_hdr.nexus; 13524 msg.hdr.status = io->io_hdr.status; 13525 msg.scsi.scsi_status = io->scsiio.scsi_status; 13526 msg.scsi.tag_num = io->scsiio.tag_num; 13527 msg.scsi.tag_type = io->scsiio.tag_type; 13528 msg.scsi.sense_len = io->scsiio.sense_len; 13529 msg.scsi.sense_residual = io->scsiio.sense_residual; 13530 msg.scsi.residual = io->scsiio.residual; 13531 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 13532 sizeof(io->scsiio.sense_data)); 13533 /* 13534 * We copy this whether or not this is an I/O-related 13535 * command. Otherwise, we'd have to go and check to see 13536 * whether it's a read/write command, and it really isn't 13537 * worth it. 13538 */ 13539 memcpy(&msg.scsi.lbalen, 13540 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, 13541 sizeof(msg.scsi.lbalen)); 13542 13543 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13544 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) { 13545 /* XXX do something here */ 13546 } 13547 13548 ctl_free_io(io); 13549 } else 13550 fe_done(io); 13551 13552 return (CTL_RETVAL_COMPLETE); 13553} 13554 13555#ifdef CTL_WITH_CA 13556/* 13557 * Front end should call this if it doesn't do autosense. When the request 13558 * sense comes back in from the initiator, we'll dequeue this and send it. 13559 */ 13560int 13561ctl_queue_sense(union ctl_io *io) 13562{ 13563 struct ctl_lun *lun; 13564 struct ctl_softc *ctl_softc; 13565 uint32_t initidx, targ_lun; 13566 13567 ctl_softc = control_softc; 13568 13569 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13570 13571 /* 13572 * LUN lookup will likely move to the ctl_work_thread() once we 13573 * have our new queueing infrastructure (that doesn't put things on 13574 * a per-LUN queue initially). That is so that we can handle 13575 * things like an INQUIRY to a LUN that we don't have enabled. We 13576 * can't deal with that right now. 13577 */ 13578 mtx_lock(&ctl_softc->ctl_lock); 13579 13580 /* 13581 * If we don't have a LUN for this, just toss the sense 13582 * information. 13583 */ 13584 targ_lun = io->io_hdr.nexus.targ_lun; 13585 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun); 13586 if ((targ_lun < CTL_MAX_LUNS) 13587 && (ctl_softc->ctl_luns[targ_lun] != NULL)) 13588 lun = ctl_softc->ctl_luns[targ_lun]; 13589 else 13590 goto bailout; 13591 13592 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13593 13594 mtx_lock(&lun->lun_lock); 13595 /* 13596 * Already have CA set for this LUN...toss the sense information. 13597 */ 13598 if (ctl_is_set(lun->have_ca, initidx)) { 13599 mtx_unlock(&lun->lun_lock); 13600 goto bailout; 13601 } 13602 13603 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13604 ctl_min(sizeof(lun->pending_sense[initidx]), 13605 sizeof(io->scsiio.sense_data))); 13606 ctl_set_mask(lun->have_ca, initidx); 13607 mtx_unlock(&lun->lun_lock); 13608 13609bailout: 13610 mtx_unlock(&ctl_softc->ctl_lock); 13611 13612 ctl_free_io(io); 13613 13614 return (CTL_RETVAL_COMPLETE); 13615} 13616#endif 13617 13618/* 13619 * Primary command inlet from frontend ports. All SCSI and task I/O 13620 * requests must go through this function. 13621 */ 13622int 13623ctl_queue(union ctl_io *io) 13624{ 13625 struct ctl_softc *ctl_softc; 13626 13627 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13628 13629 ctl_softc = control_softc; 13630 13631#ifdef CTL_TIME_IO 13632 io->io_hdr.start_time = time_uptime; 13633 getbintime(&io->io_hdr.start_bt); 13634#endif /* CTL_TIME_IO */ 13635 13636 /* Map FE-specific LUN ID into global one. */ 13637 io->io_hdr.nexus.targ_mapped_lun = 13638 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun); 13639 13640 switch (io->io_hdr.io_type) { 13641 case CTL_IO_SCSI: 13642 case CTL_IO_TASK: 13643 if (ctl_debug & CTL_DEBUG_CDB) 13644 ctl_io_print(io); 13645 ctl_enqueue_incoming(io); 13646 break; 13647 default: 13648 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13649 return (EINVAL); 13650 } 13651 13652 return (CTL_RETVAL_COMPLETE); 13653} 13654 13655#ifdef CTL_IO_DELAY 13656static void 13657ctl_done_timer_wakeup(void *arg) 13658{ 13659 union ctl_io *io; 13660 13661 io = (union ctl_io *)arg; 13662 ctl_done(io); 13663} 13664#endif /* CTL_IO_DELAY */ 13665 13666void 13667ctl_done(union ctl_io *io) 13668{ 13669 struct ctl_softc *ctl_softc; 13670 13671 ctl_softc = control_softc; 13672 13673 /* 13674 * Enable this to catch duplicate completion issues. 13675 */ 13676#if 0 13677 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13678 printf("%s: type %d msg %d cdb %x iptl: " 13679 "%d:%d:%d:%d tag 0x%04x " 13680 "flag %#x status %x\n", 13681 __func__, 13682 io->io_hdr.io_type, 13683 io->io_hdr.msg_type, 13684 io->scsiio.cdb[0], 13685 io->io_hdr.nexus.initid.id, 13686 io->io_hdr.nexus.targ_port, 13687 io->io_hdr.nexus.targ_target.id, 13688 io->io_hdr.nexus.targ_lun, 13689 (io->io_hdr.io_type == 13690 CTL_IO_TASK) ? 13691 io->taskio.tag_num : 13692 io->scsiio.tag_num, 13693 io->io_hdr.flags, 13694 io->io_hdr.status); 13695 } else 13696 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13697#endif 13698 13699 /* 13700 * This is an internal copy of an I/O, and should not go through 13701 * the normal done processing logic. 13702 */ 13703 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13704 return; 13705 13706 /* 13707 * We need to send a msg to the serializing shelf to finish the IO 13708 * as well. We don't send a finish message to the other shelf if 13709 * this is a task management command. Task management commands 13710 * aren't serialized in the OOA queue, but rather just executed on 13711 * both shelf controllers for commands that originated on that 13712 * controller. 13713 */ 13714 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC) 13715 && (io->io_hdr.io_type != CTL_IO_TASK)) { 13716 union ctl_ha_msg msg_io; 13717 13718 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO; 13719 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc; 13720 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io, 13721 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) { 13722 } 13723 /* continue on to finish IO */ 13724 } 13725#ifdef CTL_IO_DELAY 13726 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13727 struct ctl_lun *lun; 13728 13729 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13730 13731 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13732 } else { 13733 struct ctl_lun *lun; 13734 13735 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13736 13737 if ((lun != NULL) 13738 && (lun->delay_info.done_delay > 0)) { 13739 struct callout *callout; 13740 13741 callout = (struct callout *)&io->io_hdr.timer_bytes; 13742 callout_init(callout, /*mpsafe*/ 1); 13743 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13744 callout_reset(callout, 13745 lun->delay_info.done_delay * hz, 13746 ctl_done_timer_wakeup, io); 13747 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13748 lun->delay_info.done_delay = 0; 13749 return; 13750 } 13751 } 13752#endif /* CTL_IO_DELAY */ 13753 13754 ctl_enqueue_done(io); 13755} 13756 13757int 13758ctl_isc(struct ctl_scsiio *ctsio) 13759{ 13760 struct ctl_lun *lun; 13761 int retval; 13762 13763 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13764 13765 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0])); 13766 13767 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n")); 13768 13769 retval = lun->backend->data_submit((union ctl_io *)ctsio); 13770 13771 return (retval); 13772} 13773 13774 13775static void 13776ctl_work_thread(void *arg) 13777{ 13778 struct ctl_thread *thr = (struct ctl_thread *)arg; 13779 struct ctl_softc *softc = thr->ctl_softc; 13780 union ctl_io *io; 13781 int retval; 13782 13783 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13784 13785 for (;;) { 13786 retval = 0; 13787 13788 /* 13789 * We handle the queues in this order: 13790 * - ISC 13791 * - done queue (to free up resources, unblock other commands) 13792 * - RtR queue 13793 * - incoming queue 13794 * 13795 * If those queues are empty, we break out of the loop and 13796 * go to sleep. 13797 */ 13798 mtx_lock(&thr->queue_lock); 13799 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13800 if (io != NULL) { 13801 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13802 mtx_unlock(&thr->queue_lock); 13803 ctl_handle_isc(io); 13804 continue; 13805 } 13806 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13807 if (io != NULL) { 13808 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13809 /* clear any blocked commands, call fe_done */ 13810 mtx_unlock(&thr->queue_lock); 13811 retval = ctl_process_done(io); 13812 continue; 13813 } 13814 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13815 if (io != NULL) { 13816 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13817 mtx_unlock(&thr->queue_lock); 13818 if (io->io_hdr.io_type == CTL_IO_TASK) 13819 ctl_run_task(io); 13820 else 13821 ctl_scsiio_precheck(softc, &io->scsiio); 13822 continue; 13823 } 13824 if (!ctl_pause_rtr) { 13825 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13826 if (io != NULL) { 13827 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13828 mtx_unlock(&thr->queue_lock); 13829 retval = ctl_scsiio(&io->scsiio); 13830 if (retval != CTL_RETVAL_COMPLETE) 13831 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13832 continue; 13833 } 13834 } 13835 13836 /* Sleep until we have something to do. */ 13837 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13838 } 13839} 13840 13841static void 13842ctl_lun_thread(void *arg) 13843{ 13844 struct ctl_softc *softc = (struct ctl_softc *)arg; 13845 struct ctl_be_lun *be_lun; 13846 int retval; 13847 13848 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13849 13850 for (;;) { 13851 retval = 0; 13852 mtx_lock(&softc->ctl_lock); 13853 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13854 if (be_lun != NULL) { 13855 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13856 mtx_unlock(&softc->ctl_lock); 13857 ctl_create_lun(be_lun); 13858 continue; 13859 } 13860 13861 /* Sleep until we have something to do. */ 13862 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13863 PDROP | PRIBIO, "-", 0); 13864 } 13865} 13866 13867static void 13868ctl_thresh_thread(void *arg) 13869{ 13870 struct ctl_softc *softc = (struct ctl_softc *)arg; 13871 struct ctl_lun *lun; 13872 struct ctl_be_lun *be_lun; 13873 struct scsi_da_rw_recovery_page *rwpage; 13874 struct ctl_logical_block_provisioning_page *page; 13875 const char *attr; 13876 uint64_t thres, val; 13877 int i, e; 13878 13879 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13880 13881 for (;;) { 13882 mtx_lock(&softc->ctl_lock); 13883 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13884 be_lun = lun->be_lun; 13885 if ((lun->flags & CTL_LUN_DISABLED) || 13886 (lun->flags & CTL_LUN_OFFLINE) || 13887 (be_lun->flags & CTL_LUN_FLAG_UNMAP) == 0 || 13888 lun->backend->lun_attr == NULL) 13889 continue; 13890 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13891 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13892 continue; 13893 e = 0; 13894 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13895 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13896 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13897 continue; 13898 thres = scsi_4btoul(page->descr[i].count); 13899 thres <<= CTL_LBP_EXPONENT; 13900 switch (page->descr[i].resource) { 13901 case 0x01: 13902 attr = "blocksavail"; 13903 break; 13904 case 0x02: 13905 attr = "blocksused"; 13906 break; 13907 case 0xf1: 13908 attr = "poolblocksavail"; 13909 break; 13910 case 0xf2: 13911 attr = "poolblocksused"; 13912 break; 13913 default: 13914 continue; 13915 } 13916 mtx_unlock(&softc->ctl_lock); // XXX 13917 val = lun->backend->lun_attr( 13918 lun->be_lun->be_lun, attr); 13919 mtx_lock(&softc->ctl_lock); 13920 if (val == UINT64_MAX) 13921 continue; 13922 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13923 == SLBPPD_ARMING_INC) 13924 e |= (val >= thres); 13925 else 13926 e |= (val <= thres); 13927 } 13928 mtx_lock(&lun->lun_lock); 13929 if (e) { 13930 if (lun->lasttpt == 0 || 13931 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13932 lun->lasttpt = time_uptime; 13933 for (i = 0; i < CTL_MAX_INITIATORS; i++) 13934 lun->pending_ua[i] |= 13935 CTL_UA_THIN_PROV_THRES; 13936 } 13937 } else { 13938 lun->lasttpt = 0; 13939 for (i = 0; i < CTL_MAX_INITIATORS; i++) 13940 lun->pending_ua[i] &= ~CTL_UA_THIN_PROV_THRES; 13941 } 13942 mtx_unlock(&lun->lun_lock); 13943 } 13944 mtx_unlock(&softc->ctl_lock); 13945 pause("-", CTL_LBP_PERIOD * hz); 13946 } 13947} 13948 13949static void 13950ctl_enqueue_incoming(union ctl_io *io) 13951{ 13952 struct ctl_softc *softc = control_softc; 13953 struct ctl_thread *thr; 13954 u_int idx; 13955 13956 idx = (io->io_hdr.nexus.targ_port * 127 + 13957 io->io_hdr.nexus.initid.id) % worker_threads; 13958 thr = &softc->threads[idx]; 13959 mtx_lock(&thr->queue_lock); 13960 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13961 mtx_unlock(&thr->queue_lock); 13962 wakeup(thr); 13963} 13964 13965static void 13966ctl_enqueue_rtr(union ctl_io *io) 13967{ 13968 struct ctl_softc *softc = control_softc; 13969 struct ctl_thread *thr; 13970 13971 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13972 mtx_lock(&thr->queue_lock); 13973 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13974 mtx_unlock(&thr->queue_lock); 13975 wakeup(thr); 13976} 13977 13978static void 13979ctl_enqueue_done(union ctl_io *io) 13980{ 13981 struct ctl_softc *softc = control_softc; 13982 struct ctl_thread *thr; 13983 13984 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13985 mtx_lock(&thr->queue_lock); 13986 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13987 mtx_unlock(&thr->queue_lock); 13988 wakeup(thr); 13989} 13990 13991static void 13992ctl_enqueue_isc(union ctl_io *io) 13993{ 13994 struct ctl_softc *softc = control_softc; 13995 struct ctl_thread *thr; 13996 13997 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13998 mtx_lock(&thr->queue_lock); 13999 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 14000 mtx_unlock(&thr->queue_lock); 14001 wakeup(thr); 14002} 14003 14004/* Initialization and failover */ 14005 14006void 14007ctl_init_isc_msg(void) 14008{ 14009 printf("CTL: Still calling this thing\n"); 14010} 14011 14012/* 14013 * Init component 14014 * Initializes component into configuration defined by bootMode 14015 * (see hasc-sv.c) 14016 * returns hasc_Status: 14017 * OK 14018 * ERROR - fatal error 14019 */ 14020static ctl_ha_comp_status 14021ctl_isc_init(struct ctl_ha_component *c) 14022{ 14023 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14024 14025 c->status = ret; 14026 return ret; 14027} 14028 14029/* Start component 14030 * Starts component in state requested. If component starts successfully, 14031 * it must set its own state to the requestrd state 14032 * When requested state is HASC_STATE_HA, the component may refine it 14033 * by adding _SLAVE or _MASTER flags. 14034 * Currently allowed state transitions are: 14035 * UNKNOWN->HA - initial startup 14036 * UNKNOWN->SINGLE - initial startup when no parter detected 14037 * HA->SINGLE - failover 14038 * returns ctl_ha_comp_status: 14039 * OK - component successfully started in requested state 14040 * FAILED - could not start the requested state, failover may 14041 * be possible 14042 * ERROR - fatal error detected, no future startup possible 14043 */ 14044static ctl_ha_comp_status 14045ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state) 14046{ 14047 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK; 14048 14049 printf("%s: go\n", __func__); 14050 14051 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap) 14052 if (c->state == CTL_HA_STATE_UNKNOWN ) { 14053 control_softc->is_single = 0; 14054 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 14055 != CTL_HA_STATUS_SUCCESS) { 14056 printf("ctl_isc_start: ctl_ha_msg_create failed.\n"); 14057 ret = CTL_HA_COMP_STATUS_ERROR; 14058 } 14059 } else if (CTL_HA_STATE_IS_HA(c->state) 14060 && CTL_HA_STATE_IS_SINGLE(state)){ 14061 // HA->SINGLE transition 14062 ctl_failover(); 14063 control_softc->is_single = 1; 14064 } else { 14065 printf("ctl_isc_start:Invalid state transition %X->%X\n", 14066 c->state, state); 14067 ret = CTL_HA_COMP_STATUS_ERROR; 14068 } 14069 if (CTL_HA_STATE_IS_SINGLE(state)) 14070 control_softc->is_single = 1; 14071 14072 c->state = state; 14073 c->status = ret; 14074 return ret; 14075} 14076 14077/* 14078 * Quiesce component 14079 * The component must clear any error conditions (set status to OK) and 14080 * prepare itself to another Start call 14081 * returns ctl_ha_comp_status: 14082 * OK 14083 * ERROR 14084 */ 14085static ctl_ha_comp_status 14086ctl_isc_quiesce(struct ctl_ha_component *c) 14087{ 14088 int ret = CTL_HA_COMP_STATUS_OK; 14089 14090 ctl_pause_rtr = 1; 14091 c->status = ret; 14092 return ret; 14093} 14094 14095struct ctl_ha_component ctl_ha_component_ctlisc = 14096{ 14097 .name = "CTL ISC", 14098 .state = CTL_HA_STATE_UNKNOWN, 14099 .init = ctl_isc_init, 14100 .start = ctl_isc_start, 14101 .quiesce = ctl_isc_quiesce 14102}; 14103 14104/* 14105 * vim: ts=8 14106 */ 14107