ctl.c revision 290137
1/*- 2 * Copyright (c) 2003-2009 Silicon Graphics International Corp. 3 * Copyright (c) 2012 The FreeBSD Foundation 4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org> 5 * All rights reserved. 6 * 7 * Portions of this software were developed by Edward Tomasz Napierala 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification. 16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 17 * substantially similar to the "NO WARRANTY" disclaimer below 18 * ("Disclaimer") and any redistribution must be conditioned upon 19 * including a substantially similar Disclaimer requirement for further 20 * binary redistribution. 21 * 22 * NO WARRANTY 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 * POSSIBILITY OF SUCH DAMAGES. 34 * 35 * $Id$ 36 */ 37/* 38 * CAM Target Layer, a SCSI device emulation subsystem. 39 * 40 * Author: Ken Merry <ken@FreeBSD.org> 41 */ 42 43#define _CTL_C 44 45#include <sys/cdefs.h> 46__FBSDID("$FreeBSD: stable/10/sys/cam/ctl/ctl.c 290137 2015-10-29 09:08:04Z mav $"); 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/ctype.h> 51#include <sys/kernel.h> 52#include <sys/types.h> 53#include <sys/kthread.h> 54#include <sys/bio.h> 55#include <sys/fcntl.h> 56#include <sys/lock.h> 57#include <sys/module.h> 58#include <sys/mutex.h> 59#include <sys/condvar.h> 60#include <sys/malloc.h> 61#include <sys/conf.h> 62#include <sys/ioccom.h> 63#include <sys/queue.h> 64#include <sys/sbuf.h> 65#include <sys/smp.h> 66#include <sys/endian.h> 67#include <sys/sysctl.h> 68#include <vm/uma.h> 69 70#include <cam/cam.h> 71#include <cam/scsi/scsi_all.h> 72#include <cam/scsi/scsi_cd.h> 73#include <cam/scsi/scsi_da.h> 74#include <cam/ctl/ctl_io.h> 75#include <cam/ctl/ctl.h> 76#include <cam/ctl/ctl_frontend.h> 77#include <cam/ctl/ctl_util.h> 78#include <cam/ctl/ctl_backend.h> 79#include <cam/ctl/ctl_ioctl.h> 80#include <cam/ctl/ctl_ha.h> 81#include <cam/ctl/ctl_private.h> 82#include <cam/ctl/ctl_debug.h> 83#include <cam/ctl/ctl_scsi_all.h> 84#include <cam/ctl/ctl_error.h> 85 86struct ctl_softc *control_softc = NULL; 87 88/* 89 * Template mode pages. 90 */ 91 92/* 93 * Note that these are default values only. The actual values will be 94 * filled in when the user does a mode sense. 95 */ 96const static struct copan_debugconf_subpage debugconf_page_default = { 97 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 98 DBGCNF_SUBPAGE_CODE, /* subpage */ 99 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 100 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 101 DBGCNF_VERSION, /* page_version */ 102 {CTL_TIME_IO_DEFAULT_SECS>>8, 103 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */ 104}; 105 106const static struct copan_debugconf_subpage debugconf_page_changeable = { 107 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */ 108 DBGCNF_SUBPAGE_CODE, /* subpage */ 109 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8, 110 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */ 111 0, /* page_version */ 112 {0xff,0xff}, /* ctl_time_io_secs */ 113}; 114 115const static struct scsi_da_rw_recovery_page rw_er_page_default = { 116 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 117 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 118 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE, 119 /*read_retry_count*/0, 120 /*correction_span*/0, 121 /*head_offset_count*/0, 122 /*data_strobe_offset_cnt*/0, 123 /*byte8*/SMS_RWER_LBPERE, 124 /*write_retry_count*/0, 125 /*reserved2*/0, 126 /*recovery_time_limit*/{0, 0}, 127}; 128 129const static struct scsi_da_rw_recovery_page rw_er_page_changeable = { 130 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE, 131 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2, 132 /*byte3*/0, 133 /*read_retry_count*/0, 134 /*correction_span*/0, 135 /*head_offset_count*/0, 136 /*data_strobe_offset_cnt*/0, 137 /*byte8*/0, 138 /*write_retry_count*/0, 139 /*reserved2*/0, 140 /*recovery_time_limit*/{0, 0}, 141}; 142 143const static struct scsi_format_page format_page_default = { 144 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 145 /*page_length*/sizeof(struct scsi_format_page) - 2, 146 /*tracks_per_zone*/ {0, 0}, 147 /*alt_sectors_per_zone*/ {0, 0}, 148 /*alt_tracks_per_zone*/ {0, 0}, 149 /*alt_tracks_per_lun*/ {0, 0}, 150 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff, 151 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff}, 152 /*bytes_per_sector*/ {0, 0}, 153 /*interleave*/ {0, 0}, 154 /*track_skew*/ {0, 0}, 155 /*cylinder_skew*/ {0, 0}, 156 /*flags*/ SFP_HSEC, 157 /*reserved*/ {0, 0, 0} 158}; 159 160const static struct scsi_format_page format_page_changeable = { 161 /*page_code*/SMS_FORMAT_DEVICE_PAGE, 162 /*page_length*/sizeof(struct scsi_format_page) - 2, 163 /*tracks_per_zone*/ {0, 0}, 164 /*alt_sectors_per_zone*/ {0, 0}, 165 /*alt_tracks_per_zone*/ {0, 0}, 166 /*alt_tracks_per_lun*/ {0, 0}, 167 /*sectors_per_track*/ {0, 0}, 168 /*bytes_per_sector*/ {0, 0}, 169 /*interleave*/ {0, 0}, 170 /*track_skew*/ {0, 0}, 171 /*cylinder_skew*/ {0, 0}, 172 /*flags*/ 0, 173 /*reserved*/ {0, 0, 0} 174}; 175 176const static struct scsi_rigid_disk_page rigid_disk_page_default = { 177 /*page_code*/SMS_RIGID_DISK_PAGE, 178 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 179 /*cylinders*/ {0, 0, 0}, 180 /*heads*/ CTL_DEFAULT_HEADS, 181 /*start_write_precomp*/ {0, 0, 0}, 182 /*start_reduced_current*/ {0, 0, 0}, 183 /*step_rate*/ {0, 0}, 184 /*landing_zone_cylinder*/ {0, 0, 0}, 185 /*rpl*/ SRDP_RPL_DISABLED, 186 /*rotational_offset*/ 0, 187 /*reserved1*/ 0, 188 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff, 189 CTL_DEFAULT_ROTATION_RATE & 0xff}, 190 /*reserved2*/ {0, 0} 191}; 192 193const static struct scsi_rigid_disk_page rigid_disk_page_changeable = { 194 /*page_code*/SMS_RIGID_DISK_PAGE, 195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2, 196 /*cylinders*/ {0, 0, 0}, 197 /*heads*/ 0, 198 /*start_write_precomp*/ {0, 0, 0}, 199 /*start_reduced_current*/ {0, 0, 0}, 200 /*step_rate*/ {0, 0}, 201 /*landing_zone_cylinder*/ {0, 0, 0}, 202 /*rpl*/ 0, 203 /*rotational_offset*/ 0, 204 /*reserved1*/ 0, 205 /*rotation_rate*/ {0, 0}, 206 /*reserved2*/ {0, 0} 207}; 208 209const static struct scsi_caching_page caching_page_default = { 210 /*page_code*/SMS_CACHING_PAGE, 211 /*page_length*/sizeof(struct scsi_caching_page) - 2, 212 /*flags1*/ SCP_DISC | SCP_WCE, 213 /*ret_priority*/ 0, 214 /*disable_pf_transfer_len*/ {0xff, 0xff}, 215 /*min_prefetch*/ {0, 0}, 216 /*max_prefetch*/ {0xff, 0xff}, 217 /*max_pf_ceiling*/ {0xff, 0xff}, 218 /*flags2*/ 0, 219 /*cache_segments*/ 0, 220 /*cache_seg_size*/ {0, 0}, 221 /*reserved*/ 0, 222 /*non_cache_seg_size*/ {0, 0, 0} 223}; 224 225const static struct scsi_caching_page caching_page_changeable = { 226 /*page_code*/SMS_CACHING_PAGE, 227 /*page_length*/sizeof(struct scsi_caching_page) - 2, 228 /*flags1*/ SCP_WCE | SCP_RCD, 229 /*ret_priority*/ 0, 230 /*disable_pf_transfer_len*/ {0, 0}, 231 /*min_prefetch*/ {0, 0}, 232 /*max_prefetch*/ {0, 0}, 233 /*max_pf_ceiling*/ {0, 0}, 234 /*flags2*/ 0, 235 /*cache_segments*/ 0, 236 /*cache_seg_size*/ {0, 0}, 237 /*reserved*/ 0, 238 /*non_cache_seg_size*/ {0, 0, 0} 239}; 240 241const static struct scsi_control_page control_page_default = { 242 /*page_code*/SMS_CONTROL_MODE_PAGE, 243 /*page_length*/sizeof(struct scsi_control_page) - 2, 244 /*rlec*/0, 245 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED, 246 /*eca_and_aen*/0, 247 /*flags4*/SCP_TAS, 248 /*aen_holdoff_period*/{0, 0}, 249 /*busy_timeout_period*/{0, 0}, 250 /*extended_selftest_completion_time*/{0, 0} 251}; 252 253const static struct scsi_control_page control_page_changeable = { 254 /*page_code*/SMS_CONTROL_MODE_PAGE, 255 /*page_length*/sizeof(struct scsi_control_page) - 2, 256 /*rlec*/SCP_DSENSE, 257 /*queue_flags*/SCP_QUEUE_ALG_MASK, 258 /*eca_and_aen*/SCP_SWP, 259 /*flags4*/0, 260 /*aen_holdoff_period*/{0, 0}, 261 /*busy_timeout_period*/{0, 0}, 262 /*extended_selftest_completion_time*/{0, 0} 263}; 264 265#define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4) 266 267const static struct scsi_control_ext_page control_ext_page_default = { 268 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 269 /*subpage_code*/0x01, 270 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 271 /*flags*/0, 272 /*prio*/0, 273 /*max_sense*/0 274}; 275 276const static struct scsi_control_ext_page control_ext_page_changeable = { 277 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF, 278 /*subpage_code*/0x01, 279 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN}, 280 /*flags*/0, 281 /*prio*/0, 282 /*max_sense*/0 283}; 284 285const static struct scsi_info_exceptions_page ie_page_default = { 286 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 287 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 288 /*info_flags*/SIEP_FLAGS_DEXCPT, 289 /*mrie*/0, 290 /*interval_timer*/{0, 0, 0, 0}, 291 /*report_count*/{0, 0, 0, 0} 292}; 293 294const static struct scsi_info_exceptions_page ie_page_changeable = { 295 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE, 296 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2, 297 /*info_flags*/0, 298 /*mrie*/0, 299 /*interval_timer*/{0, 0, 0, 0}, 300 /*report_count*/{0, 0, 0, 0} 301}; 302 303#define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4) 304 305const static struct ctl_logical_block_provisioning_page lbp_page_default = {{ 306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 307 /*subpage_code*/0x02, 308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 309 /*flags*/0, 310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 311 /*descr*/{}}, 312 {{/*flags*/0, 313 /*resource*/0x01, 314 /*reserved*/{0, 0}, 315 /*count*/{0, 0, 0, 0}}, 316 {/*flags*/0, 317 /*resource*/0x02, 318 /*reserved*/{0, 0}, 319 /*count*/{0, 0, 0, 0}}, 320 {/*flags*/0, 321 /*resource*/0xf1, 322 /*reserved*/{0, 0}, 323 /*count*/{0, 0, 0, 0}}, 324 {/*flags*/0, 325 /*resource*/0xf2, 326 /*reserved*/{0, 0}, 327 /*count*/{0, 0, 0, 0}} 328 } 329}; 330 331const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{ 332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF, 333 /*subpage_code*/0x02, 334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN}, 335 /*flags*/0, 336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 337 /*descr*/{}}, 338 {{/*flags*/0, 339 /*resource*/0, 340 /*reserved*/{0, 0}, 341 /*count*/{0, 0, 0, 0}}, 342 {/*flags*/0, 343 /*resource*/0, 344 /*reserved*/{0, 0}, 345 /*count*/{0, 0, 0, 0}}, 346 {/*flags*/0, 347 /*resource*/0, 348 /*reserved*/{0, 0}, 349 /*count*/{0, 0, 0, 0}}, 350 {/*flags*/0, 351 /*resource*/0, 352 /*reserved*/{0, 0}, 353 /*count*/{0, 0, 0, 0}} 354 } 355}; 356 357const static struct scsi_cddvd_capabilities_page cddvd_page_default = { 358 /*page_code*/SMS_CDDVD_CAPS_PAGE, 359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 360 /*caps1*/0x3f, 361 /*caps2*/0x00, 362 /*caps3*/0xf0, 363 /*caps4*/0x00, 364 /*caps5*/0x29, 365 /*caps6*/0x00, 366 /*obsolete*/{0, 0}, 367 /*nvol_levels*/{0, 0}, 368 /*buffer_size*/{8, 0}, 369 /*obsolete2*/{0, 0}, 370 /*reserved*/0, 371 /*digital*/0, 372 /*obsolete3*/0, 373 /*copy_management*/0, 374 /*reserved2*/0, 375 /*rotation_control*/0, 376 /*cur_write_speed*/0, 377 /*num_speed_descr*/0, 378}; 379 380const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = { 381 /*page_code*/SMS_CDDVD_CAPS_PAGE, 382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2, 383 /*caps1*/0, 384 /*caps2*/0, 385 /*caps3*/0, 386 /*caps4*/0, 387 /*caps5*/0, 388 /*caps6*/0, 389 /*obsolete*/{0, 0}, 390 /*nvol_levels*/{0, 0}, 391 /*buffer_size*/{0, 0}, 392 /*obsolete2*/{0, 0}, 393 /*reserved*/0, 394 /*digital*/0, 395 /*obsolete3*/0, 396 /*copy_management*/0, 397 /*reserved2*/0, 398 /*rotation_control*/0, 399 /*cur_write_speed*/0, 400 /*num_speed_descr*/0, 401}; 402 403SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer"); 404static int worker_threads = -1; 405TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads); 406SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN, 407 &worker_threads, 1, "Number of worker threads"); 408static int ctl_debug = CTL_DEBUG_NONE; 409TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug); 410SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN, 411 &ctl_debug, 0, "Enabled debug flags"); 412 413/* 414 * Supported pages (0x00), Serial number (0x80), Device ID (0x83), 415 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87), 416 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0), 417 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2) 418 */ 419#define SCSI_EVPD_NUM_SUPPORTED_PAGES 10 420 421static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event, 422 int param); 423static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest); 424static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest); 425static int ctl_init(void); 426void ctl_shutdown(void); 427static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td); 428static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td); 429static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio); 430static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 431 struct ctl_ooa *ooa_hdr, 432 struct ctl_ooa_entry *kern_entries); 433static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 434 struct thread *td); 435static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun, 436 struct ctl_be_lun *be_lun); 437static int ctl_free_lun(struct ctl_lun *lun); 438static void ctl_create_lun(struct ctl_be_lun *be_lun); 439static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr); 440 441static int ctl_do_mode_select(union ctl_io *io); 442static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, 443 uint64_t res_key, uint64_t sa_res_key, 444 uint8_t type, uint32_t residx, 445 struct ctl_scsiio *ctsio, 446 struct scsi_per_res_out *cdb, 447 struct scsi_per_res_out_parms* param); 448static void ctl_pro_preempt_other(struct ctl_lun *lun, 449 union ctl_ha_msg *msg); 450static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg); 451static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len); 452static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len); 453static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len); 454static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len); 455static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len); 456static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, 457 int alloc_len); 458static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, 459 int alloc_len); 460static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len); 461static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len); 462static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio); 463static int ctl_inquiry_std(struct ctl_scsiio *ctsio); 464static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len); 465static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2, 466 bool seq); 467static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2); 468static ctl_action ctl_check_for_blockage(struct ctl_lun *lun, 469 union ctl_io *pending_io, union ctl_io *ooa_io); 470static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 471 union ctl_io *starting_io); 472static int ctl_check_blocked(struct ctl_lun *lun); 473static int ctl_scsiio_lun_check(struct ctl_lun *lun, 474 const struct ctl_cmd_entry *entry, 475 struct ctl_scsiio *ctsio); 476static void ctl_failover_lun(union ctl_io *io); 477static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc, 478 struct ctl_scsiio *ctsio); 479static int ctl_scsiio(struct ctl_scsiio *ctsio); 480 481static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 482static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io, 483 ctl_ua_type ua_type); 484static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, 485 ctl_ua_type ua_type); 486static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io); 487static int ctl_abort_task(union ctl_io *io); 488static int ctl_abort_task_set(union ctl_io *io); 489static int ctl_query_task(union ctl_io *io, int task_set); 490static int ctl_i_t_nexus_reset(union ctl_io *io); 491static int ctl_query_async_event(union ctl_io *io); 492static void ctl_run_task(union ctl_io *io); 493#ifdef CTL_IO_DELAY 494static void ctl_datamove_timer_wakeup(void *arg); 495static void ctl_done_timer_wakeup(void *arg); 496#endif /* CTL_IO_DELAY */ 497 498static void ctl_send_datamove_done(union ctl_io *io, int have_lock); 499static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq); 500static int ctl_datamove_remote_dm_write_cb(union ctl_io *io); 501static void ctl_datamove_remote_write(union ctl_io *io); 502static int ctl_datamove_remote_dm_read_cb(union ctl_io *io); 503static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq); 504static int ctl_datamove_remote_sgl_setup(union ctl_io *io); 505static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 506 ctl_ha_dt_cb callback); 507static void ctl_datamove_remote_read(union ctl_io *io); 508static void ctl_datamove_remote(union ctl_io *io); 509static void ctl_process_done(union ctl_io *io); 510static void ctl_lun_thread(void *arg); 511static void ctl_thresh_thread(void *arg); 512static void ctl_work_thread(void *arg); 513static void ctl_enqueue_incoming(union ctl_io *io); 514static void ctl_enqueue_rtr(union ctl_io *io); 515static void ctl_enqueue_done(union ctl_io *io); 516static void ctl_enqueue_isc(union ctl_io *io); 517static const struct ctl_cmd_entry * 518 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa); 519static const struct ctl_cmd_entry * 520 ctl_validate_command(struct ctl_scsiio *ctsio); 521static int ctl_cmd_applicable(uint8_t lun_type, 522 const struct ctl_cmd_entry *entry); 523 524static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx); 525static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx); 526static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx); 527static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key); 528 529/* 530 * Load the serialization table. This isn't very pretty, but is probably 531 * the easiest way to do it. 532 */ 533#include "ctl_ser_table.c" 534 535/* 536 * We only need to define open, close and ioctl routines for this driver. 537 */ 538static struct cdevsw ctl_cdevsw = { 539 .d_version = D_VERSION, 540 .d_flags = 0, 541 .d_open = ctl_open, 542 .d_close = ctl_close, 543 .d_ioctl = ctl_ioctl, 544 .d_name = "ctl", 545}; 546 547 548MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL"); 549 550static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *); 551 552static moduledata_t ctl_moduledata = { 553 "ctl", 554 ctl_module_event_handler, 555 NULL 556}; 557 558DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD); 559MODULE_VERSION(ctl, 1); 560 561static struct ctl_frontend ha_frontend = 562{ 563 .name = "ha", 564}; 565 566static void 567ctl_ha_datamove(union ctl_io *io) 568{ 569 struct ctl_lun *lun; 570 struct ctl_sg_entry *sgl; 571 union ctl_ha_msg msg; 572 uint32_t sg_entries_sent; 573 int do_sg_copy, i, j; 574 575 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 576 memset(&msg.dt, 0, sizeof(msg.dt)); 577 msg.hdr.msg_type = CTL_MSG_DATAMOVE; 578 msg.hdr.original_sc = io->io_hdr.original_sc; 579 msg.hdr.serializing_sc = io; 580 msg.hdr.nexus = io->io_hdr.nexus; 581 msg.hdr.status = io->io_hdr.status; 582 msg.dt.flags = io->io_hdr.flags; 583 584 /* 585 * We convert everything into a S/G list here. We can't 586 * pass by reference, only by value between controllers. 587 * So we can't pass a pointer to the S/G list, only as many 588 * S/G entries as we can fit in here. If it's possible for 589 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries, 590 * then we need to break this up into multiple transfers. 591 */ 592 if (io->scsiio.kern_sg_entries == 0) { 593 msg.dt.kern_sg_entries = 1; 594#if 0 595 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 596 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 597 } else { 598 /* XXX KDM use busdma here! */ 599 msg.dt.sg_list[0].addr = 600 (void *)vtophys(io->scsiio.kern_data_ptr); 601 } 602#else 603 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 604 ("HA does not support BUS_ADDR")); 605 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr; 606#endif 607 msg.dt.sg_list[0].len = io->scsiio.kern_data_len; 608 do_sg_copy = 0; 609 } else { 610 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries; 611 do_sg_copy = 1; 612 } 613 614 msg.dt.kern_data_len = io->scsiio.kern_data_len; 615 msg.dt.kern_total_len = io->scsiio.kern_total_len; 616 msg.dt.kern_data_resid = io->scsiio.kern_data_resid; 617 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset; 618 msg.dt.sg_sequence = 0; 619 620 /* 621 * Loop until we've sent all of the S/G entries. On the 622 * other end, we'll recompose these S/G entries into one 623 * contiguous list before processing. 624 */ 625 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries; 626 msg.dt.sg_sequence++) { 627 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) / 628 sizeof(msg.dt.sg_list[0])), 629 msg.dt.kern_sg_entries - sg_entries_sent); 630 if (do_sg_copy != 0) { 631 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr; 632 for (i = sg_entries_sent, j = 0; 633 i < msg.dt.cur_sg_entries; i++, j++) { 634#if 0 635 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) { 636 msg.dt.sg_list[j].addr = sgl[i].addr; 637 } else { 638 /* XXX KDM use busdma here! */ 639 msg.dt.sg_list[j].addr = 640 (void *)vtophys(sgl[i].addr); 641 } 642#else 643 KASSERT((io->io_hdr.flags & 644 CTL_FLAG_BUS_ADDR) == 0, 645 ("HA does not support BUS_ADDR")); 646 msg.dt.sg_list[j].addr = sgl[i].addr; 647#endif 648 msg.dt.sg_list[j].len = sgl[i].len; 649 } 650 } 651 652 sg_entries_sent += msg.dt.cur_sg_entries; 653 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries); 654 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 655 sizeof(msg.dt) - sizeof(msg.dt.sg_list) + 656 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries, 657 M_WAITOK) > CTL_HA_STATUS_SUCCESS) { 658 io->io_hdr.port_status = 31341; 659 io->scsiio.be_move_done(io); 660 return; 661 } 662 msg.dt.sent_sg_entries = sg_entries_sent; 663 } 664 665 /* 666 * Officially handover the request from us to peer. 667 * If failover has just happened, then we must return error. 668 * If failover happen just after, then it is not our problem. 669 */ 670 if (lun) 671 mtx_lock(&lun->lun_lock); 672 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 673 if (lun) 674 mtx_unlock(&lun->lun_lock); 675 io->io_hdr.port_status = 31342; 676 io->scsiio.be_move_done(io); 677 return; 678 } 679 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 680 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG; 681 if (lun) 682 mtx_unlock(&lun->lun_lock); 683} 684 685static void 686ctl_ha_done(union ctl_io *io) 687{ 688 union ctl_ha_msg msg; 689 690 if (io->io_hdr.io_type == CTL_IO_SCSI) { 691 memset(&msg, 0, sizeof(msg)); 692 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 693 msg.hdr.original_sc = io->io_hdr.original_sc; 694 msg.hdr.nexus = io->io_hdr.nexus; 695 msg.hdr.status = io->io_hdr.status; 696 msg.scsi.scsi_status = io->scsiio.scsi_status; 697 msg.scsi.tag_num = io->scsiio.tag_num; 698 msg.scsi.tag_type = io->scsiio.tag_type; 699 msg.scsi.sense_len = io->scsiio.sense_len; 700 msg.scsi.sense_residual = io->scsiio.sense_residual; 701 msg.scsi.residual = io->scsiio.residual; 702 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 703 io->scsiio.sense_len); 704 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 705 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 706 msg.scsi.sense_len, M_WAITOK); 707 } 708 ctl_free_io(io); 709} 710 711static void 712ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc, 713 union ctl_ha_msg *msg_info) 714{ 715 struct ctl_scsiio *ctsio; 716 717 if (msg_info->hdr.original_sc == NULL) { 718 printf("%s: original_sc == NULL!\n", __func__); 719 /* XXX KDM now what? */ 720 return; 721 } 722 723 ctsio = &msg_info->hdr.original_sc->scsiio; 724 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 725 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 726 ctsio->io_hdr.status = msg_info->hdr.status; 727 ctsio->scsi_status = msg_info->scsi.scsi_status; 728 ctsio->sense_len = msg_info->scsi.sense_len; 729 ctsio->sense_residual = msg_info->scsi.sense_residual; 730 ctsio->residual = msg_info->scsi.residual; 731 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data, 732 msg_info->scsi.sense_len); 733 ctl_enqueue_isc((union ctl_io *)ctsio); 734} 735 736static void 737ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc, 738 union ctl_ha_msg *msg_info) 739{ 740 struct ctl_scsiio *ctsio; 741 742 if (msg_info->hdr.serializing_sc == NULL) { 743 printf("%s: serializing_sc == NULL!\n", __func__); 744 /* XXX KDM now what? */ 745 return; 746 } 747 748 ctsio = &msg_info->hdr.serializing_sc->scsiio; 749 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO; 750 ctl_enqueue_isc((union ctl_io *)ctsio); 751} 752 753void 754ctl_isc_announce_lun(struct ctl_lun *lun) 755{ 756 struct ctl_softc *softc = lun->ctl_softc; 757 union ctl_ha_msg *msg; 758 struct ctl_ha_msg_lun_pr_key pr_key; 759 int i, k; 760 761 if (softc->ha_link != CTL_HA_LINK_ONLINE) 762 return; 763 mtx_lock(&lun->lun_lock); 764 i = sizeof(msg->lun); 765 if (lun->lun_devid) 766 i += lun->lun_devid->len; 767 i += sizeof(pr_key) * lun->pr_key_count; 768alloc: 769 mtx_unlock(&lun->lun_lock); 770 msg = malloc(i, M_CTL, M_WAITOK); 771 mtx_lock(&lun->lun_lock); 772 k = sizeof(msg->lun); 773 if (lun->lun_devid) 774 k += lun->lun_devid->len; 775 k += sizeof(pr_key) * lun->pr_key_count; 776 if (i < k) { 777 free(msg, M_CTL); 778 i = k; 779 goto alloc; 780 } 781 bzero(&msg->lun, sizeof(msg->lun)); 782 msg->hdr.msg_type = CTL_MSG_LUN_SYNC; 783 msg->hdr.nexus.targ_lun = lun->lun; 784 msg->hdr.nexus.targ_mapped_lun = lun->lun; 785 msg->lun.flags = lun->flags; 786 msg->lun.pr_generation = lun->pr_generation; 787 msg->lun.pr_res_idx = lun->pr_res_idx; 788 msg->lun.pr_res_type = lun->pr_res_type; 789 msg->lun.pr_key_count = lun->pr_key_count; 790 i = 0; 791 if (lun->lun_devid) { 792 msg->lun.lun_devid_len = lun->lun_devid->len; 793 memcpy(&msg->lun.data[i], lun->lun_devid->data, 794 msg->lun.lun_devid_len); 795 i += msg->lun.lun_devid_len; 796 } 797 for (k = 0; k < CTL_MAX_INITIATORS; k++) { 798 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0) 799 continue; 800 pr_key.pr_iid = k; 801 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key)); 802 i += sizeof(pr_key); 803 } 804 mtx_unlock(&lun->lun_lock); 805 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 806 M_WAITOK); 807 free(msg, M_CTL); 808 809 if (lun->flags & CTL_LUN_PRIMARY_SC) { 810 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 811 ctl_isc_announce_mode(lun, -1, 812 lun->mode_pages.index[i].page_code & SMPH_PC_MASK, 813 lun->mode_pages.index[i].subpage); 814 } 815 } 816} 817 818void 819ctl_isc_announce_port(struct ctl_port *port) 820{ 821 struct ctl_softc *softc = port->ctl_softc; 822 union ctl_ha_msg *msg; 823 int i; 824 825 if (port->targ_port < softc->port_min || 826 port->targ_port >= softc->port_max || 827 softc->ha_link != CTL_HA_LINK_ONLINE) 828 return; 829 i = sizeof(msg->port) + strlen(port->port_name) + 1; 830 if (port->lun_map) 831 i += sizeof(uint32_t) * CTL_MAX_LUNS; 832 if (port->port_devid) 833 i += port->port_devid->len; 834 if (port->target_devid) 835 i += port->target_devid->len; 836 if (port->init_devid) 837 i += port->init_devid->len; 838 msg = malloc(i, M_CTL, M_WAITOK); 839 bzero(&msg->port, sizeof(msg->port)); 840 msg->hdr.msg_type = CTL_MSG_PORT_SYNC; 841 msg->hdr.nexus.targ_port = port->targ_port; 842 msg->port.port_type = port->port_type; 843 msg->port.physical_port = port->physical_port; 844 msg->port.virtual_port = port->virtual_port; 845 msg->port.status = port->status; 846 i = 0; 847 msg->port.name_len = sprintf(&msg->port.data[i], 848 "%d:%s", softc->ha_id, port->port_name) + 1; 849 i += msg->port.name_len; 850 if (port->lun_map) { 851 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS; 852 memcpy(&msg->port.data[i], port->lun_map, 853 msg->port.lun_map_len); 854 i += msg->port.lun_map_len; 855 } 856 if (port->port_devid) { 857 msg->port.port_devid_len = port->port_devid->len; 858 memcpy(&msg->port.data[i], port->port_devid->data, 859 msg->port.port_devid_len); 860 i += msg->port.port_devid_len; 861 } 862 if (port->target_devid) { 863 msg->port.target_devid_len = port->target_devid->len; 864 memcpy(&msg->port.data[i], port->target_devid->data, 865 msg->port.target_devid_len); 866 i += msg->port.target_devid_len; 867 } 868 if (port->init_devid) { 869 msg->port.init_devid_len = port->init_devid->len; 870 memcpy(&msg->port.data[i], port->init_devid->data, 871 msg->port.init_devid_len); 872 i += msg->port.init_devid_len; 873 } 874 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i, 875 M_WAITOK); 876 free(msg, M_CTL); 877} 878 879void 880ctl_isc_announce_iid(struct ctl_port *port, int iid) 881{ 882 struct ctl_softc *softc = port->ctl_softc; 883 union ctl_ha_msg *msg; 884 int i, l; 885 886 if (port->targ_port < softc->port_min || 887 port->targ_port >= softc->port_max || 888 softc->ha_link != CTL_HA_LINK_ONLINE) 889 return; 890 mtx_lock(&softc->ctl_lock); 891 i = sizeof(msg->iid); 892 l = 0; 893 if (port->wwpn_iid[iid].name) 894 l = strlen(port->wwpn_iid[iid].name) + 1; 895 i += l; 896 msg = malloc(i, M_CTL, M_NOWAIT); 897 if (msg == NULL) { 898 mtx_unlock(&softc->ctl_lock); 899 return; 900 } 901 bzero(&msg->iid, sizeof(msg->iid)); 902 msg->hdr.msg_type = CTL_MSG_IID_SYNC; 903 msg->hdr.nexus.targ_port = port->targ_port; 904 msg->hdr.nexus.initid = iid; 905 msg->iid.in_use = port->wwpn_iid[iid].in_use; 906 msg->iid.name_len = l; 907 msg->iid.wwpn = port->wwpn_iid[iid].wwpn; 908 if (port->wwpn_iid[iid].name) 909 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l); 910 mtx_unlock(&softc->ctl_lock); 911 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT); 912 free(msg, M_CTL); 913} 914 915void 916ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx, 917 uint8_t page, uint8_t subpage) 918{ 919 struct ctl_softc *softc = lun->ctl_softc; 920 union ctl_ha_msg msg; 921 int i; 922 923 if (softc->ha_link != CTL_HA_LINK_ONLINE) 924 return; 925 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 926 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 927 page && lun->mode_pages.index[i].subpage == subpage) 928 break; 929 } 930 if (i == CTL_NUM_MODE_PAGES) 931 return; 932 933 /* Don't try to replicate pages not present on this device. */ 934 if (lun->mode_pages.index[i].page_data == NULL) 935 return; 936 937 bzero(&msg.mode, sizeof(msg.mode)); 938 msg.hdr.msg_type = CTL_MSG_MODE_SYNC; 939 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT; 940 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT; 941 msg.hdr.nexus.targ_lun = lun->lun; 942 msg.hdr.nexus.targ_mapped_lun = lun->lun; 943 msg.mode.page_code = page; 944 msg.mode.subpage = subpage; 945 msg.mode.page_len = lun->mode_pages.index[i].page_len; 946 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data, 947 msg.mode.page_len); 948 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode), 949 M_WAITOK); 950} 951 952static void 953ctl_isc_ha_link_up(struct ctl_softc *softc) 954{ 955 struct ctl_port *port; 956 struct ctl_lun *lun; 957 union ctl_ha_msg msg; 958 int i; 959 960 /* Announce this node parameters to peer for validation. */ 961 msg.login.msg_type = CTL_MSG_LOGIN; 962 msg.login.version = CTL_HA_VERSION; 963 msg.login.ha_mode = softc->ha_mode; 964 msg.login.ha_id = softc->ha_id; 965 msg.login.max_luns = CTL_MAX_LUNS; 966 msg.login.max_ports = CTL_MAX_PORTS; 967 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT; 968 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login), 969 M_WAITOK); 970 971 STAILQ_FOREACH(port, &softc->port_list, links) { 972 ctl_isc_announce_port(port); 973 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 974 if (port->wwpn_iid[i].in_use) 975 ctl_isc_announce_iid(port, i); 976 } 977 } 978 STAILQ_FOREACH(lun, &softc->lun_list, links) 979 ctl_isc_announce_lun(lun); 980} 981 982static void 983ctl_isc_ha_link_down(struct ctl_softc *softc) 984{ 985 struct ctl_port *port; 986 struct ctl_lun *lun; 987 union ctl_io *io; 988 int i; 989 990 mtx_lock(&softc->ctl_lock); 991 STAILQ_FOREACH(lun, &softc->lun_list, links) { 992 mtx_lock(&lun->lun_lock); 993 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) { 994 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 995 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 996 } 997 mtx_unlock(&lun->lun_lock); 998 999 mtx_unlock(&softc->ctl_lock); 1000 io = ctl_alloc_io(softc->othersc_pool); 1001 mtx_lock(&softc->ctl_lock); 1002 ctl_zero_io(io); 1003 io->io_hdr.msg_type = CTL_MSG_FAILOVER; 1004 io->io_hdr.nexus.targ_mapped_lun = lun->lun; 1005 ctl_enqueue_isc(io); 1006 } 1007 1008 STAILQ_FOREACH(port, &softc->port_list, links) { 1009 if (port->targ_port >= softc->port_min && 1010 port->targ_port < softc->port_max) 1011 continue; 1012 port->status &= ~CTL_PORT_STATUS_ONLINE; 1013 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1014 port->wwpn_iid[i].in_use = 0; 1015 free(port->wwpn_iid[i].name, M_CTL); 1016 port->wwpn_iid[i].name = NULL; 1017 } 1018 } 1019 mtx_unlock(&softc->ctl_lock); 1020} 1021 1022static void 1023ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1024{ 1025 struct ctl_lun *lun; 1026 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus); 1027 1028 mtx_lock(&softc->ctl_lock); 1029 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS && 1030 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) { 1031 mtx_lock(&lun->lun_lock); 1032 mtx_unlock(&softc->ctl_lock); 1033 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && 1034 msg->ua.ua_set) 1035 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8); 1036 if (msg->ua.ua_all) { 1037 if (msg->ua.ua_set) 1038 ctl_est_ua_all(lun, iid, msg->ua.ua_type); 1039 else 1040 ctl_clr_ua_all(lun, iid, msg->ua.ua_type); 1041 } else { 1042 if (msg->ua.ua_set) 1043 ctl_est_ua(lun, iid, msg->ua.ua_type); 1044 else 1045 ctl_clr_ua(lun, iid, msg->ua.ua_type); 1046 } 1047 mtx_unlock(&lun->lun_lock); 1048 } else 1049 mtx_unlock(&softc->ctl_lock); 1050} 1051 1052static void 1053ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1054{ 1055 struct ctl_lun *lun; 1056 struct ctl_ha_msg_lun_pr_key pr_key; 1057 int i, k; 1058 ctl_lun_flags oflags; 1059 uint32_t targ_lun; 1060 1061 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1062 mtx_lock(&softc->ctl_lock); 1063 if ((targ_lun >= CTL_MAX_LUNS) || 1064 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1065 mtx_unlock(&softc->ctl_lock); 1066 return; 1067 } 1068 mtx_lock(&lun->lun_lock); 1069 mtx_unlock(&softc->ctl_lock); 1070 if (lun->flags & CTL_LUN_DISABLED) { 1071 mtx_unlock(&lun->lun_lock); 1072 return; 1073 } 1074 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0; 1075 if (msg->lun.lun_devid_len != i || (i > 0 && 1076 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) { 1077 mtx_unlock(&lun->lun_lock); 1078 printf("%s: Received conflicting HA LUN %d\n", 1079 __func__, msg->hdr.nexus.targ_lun); 1080 return; 1081 } else { 1082 /* Record whether peer is primary. */ 1083 oflags = lun->flags; 1084 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) && 1085 (msg->lun.flags & CTL_LUN_DISABLED) == 0) 1086 lun->flags |= CTL_LUN_PEER_SC_PRIMARY; 1087 else 1088 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY; 1089 if (oflags != lun->flags) 1090 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 1091 1092 /* If peer is primary and we are not -- use data */ 1093 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 1094 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) { 1095 lun->pr_generation = msg->lun.pr_generation; 1096 lun->pr_res_idx = msg->lun.pr_res_idx; 1097 lun->pr_res_type = msg->lun.pr_res_type; 1098 lun->pr_key_count = msg->lun.pr_key_count; 1099 for (k = 0; k < CTL_MAX_INITIATORS; k++) 1100 ctl_clr_prkey(lun, k); 1101 for (k = 0; k < msg->lun.pr_key_count; k++) { 1102 memcpy(&pr_key, &msg->lun.data[i], 1103 sizeof(pr_key)); 1104 ctl_alloc_prkey(lun, pr_key.pr_iid); 1105 ctl_set_prkey(lun, pr_key.pr_iid, 1106 pr_key.pr_key); 1107 i += sizeof(pr_key); 1108 } 1109 } 1110 1111 mtx_unlock(&lun->lun_lock); 1112 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n", 1113 __func__, msg->hdr.nexus.targ_lun, 1114 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ? 1115 "primary" : "secondary")); 1116 1117 /* If we are primary but peer doesn't know -- notify */ 1118 if ((lun->flags & CTL_LUN_PRIMARY_SC) && 1119 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0) 1120 ctl_isc_announce_lun(lun); 1121 } 1122} 1123 1124static void 1125ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1126{ 1127 struct ctl_port *port; 1128 struct ctl_lun *lun; 1129 int i, new; 1130 1131 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1132 if (port == NULL) { 1133 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__, 1134 msg->hdr.nexus.targ_port)); 1135 new = 1; 1136 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO); 1137 port->frontend = &ha_frontend; 1138 port->targ_port = msg->hdr.nexus.targ_port; 1139 port->fe_datamove = ctl_ha_datamove; 1140 port->fe_done = ctl_ha_done; 1141 } else if (port->frontend == &ha_frontend) { 1142 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__, 1143 msg->hdr.nexus.targ_port)); 1144 new = 0; 1145 } else { 1146 printf("%s: Received conflicting HA port %d\n", 1147 __func__, msg->hdr.nexus.targ_port); 1148 return; 1149 } 1150 port->port_type = msg->port.port_type; 1151 port->physical_port = msg->port.physical_port; 1152 port->virtual_port = msg->port.virtual_port; 1153 port->status = msg->port.status; 1154 i = 0; 1155 free(port->port_name, M_CTL); 1156 port->port_name = strndup(&msg->port.data[i], msg->port.name_len, 1157 M_CTL); 1158 i += msg->port.name_len; 1159 if (msg->port.lun_map_len != 0) { 1160 if (port->lun_map == NULL) 1161 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 1162 M_CTL, M_WAITOK); 1163 memcpy(port->lun_map, &msg->port.data[i], 1164 sizeof(uint32_t) * CTL_MAX_LUNS); 1165 i += msg->port.lun_map_len; 1166 } else { 1167 free(port->lun_map, M_CTL); 1168 port->lun_map = NULL; 1169 } 1170 if (msg->port.port_devid_len != 0) { 1171 if (port->port_devid == NULL || 1172 port->port_devid->len != msg->port.port_devid_len) { 1173 free(port->port_devid, M_CTL); 1174 port->port_devid = malloc(sizeof(struct ctl_devid) + 1175 msg->port.port_devid_len, M_CTL, M_WAITOK); 1176 } 1177 memcpy(port->port_devid->data, &msg->port.data[i], 1178 msg->port.port_devid_len); 1179 port->port_devid->len = msg->port.port_devid_len; 1180 i += msg->port.port_devid_len; 1181 } else { 1182 free(port->port_devid, M_CTL); 1183 port->port_devid = NULL; 1184 } 1185 if (msg->port.target_devid_len != 0) { 1186 if (port->target_devid == NULL || 1187 port->target_devid->len != msg->port.target_devid_len) { 1188 free(port->target_devid, M_CTL); 1189 port->target_devid = malloc(sizeof(struct ctl_devid) + 1190 msg->port.target_devid_len, M_CTL, M_WAITOK); 1191 } 1192 memcpy(port->target_devid->data, &msg->port.data[i], 1193 msg->port.target_devid_len); 1194 port->target_devid->len = msg->port.target_devid_len; 1195 i += msg->port.target_devid_len; 1196 } else { 1197 free(port->target_devid, M_CTL); 1198 port->target_devid = NULL; 1199 } 1200 if (msg->port.init_devid_len != 0) { 1201 if (port->init_devid == NULL || 1202 port->init_devid->len != msg->port.init_devid_len) { 1203 free(port->init_devid, M_CTL); 1204 port->init_devid = malloc(sizeof(struct ctl_devid) + 1205 msg->port.init_devid_len, M_CTL, M_WAITOK); 1206 } 1207 memcpy(port->init_devid->data, &msg->port.data[i], 1208 msg->port.init_devid_len); 1209 port->init_devid->len = msg->port.init_devid_len; 1210 i += msg->port.init_devid_len; 1211 } else { 1212 free(port->init_devid, M_CTL); 1213 port->init_devid = NULL; 1214 } 1215 if (new) { 1216 if (ctl_port_register(port) != 0) { 1217 printf("%s: ctl_port_register() failed with error\n", 1218 __func__); 1219 } 1220 } 1221 mtx_lock(&softc->ctl_lock); 1222 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1223 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 1224 continue; 1225 mtx_lock(&lun->lun_lock); 1226 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE); 1227 mtx_unlock(&lun->lun_lock); 1228 } 1229 mtx_unlock(&softc->ctl_lock); 1230} 1231 1232static void 1233ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1234{ 1235 struct ctl_port *port; 1236 int iid; 1237 1238 port = softc->ctl_ports[msg->hdr.nexus.targ_port]; 1239 if (port == NULL) { 1240 printf("%s: Received IID for unknown port %d\n", 1241 __func__, msg->hdr.nexus.targ_port); 1242 return; 1243 } 1244 iid = msg->hdr.nexus.initid; 1245 port->wwpn_iid[iid].in_use = msg->iid.in_use; 1246 port->wwpn_iid[iid].wwpn = msg->iid.wwpn; 1247 free(port->wwpn_iid[iid].name, M_CTL); 1248 if (msg->iid.name_len) { 1249 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0], 1250 msg->iid.name_len, M_CTL); 1251 } else 1252 port->wwpn_iid[iid].name = NULL; 1253} 1254 1255static void 1256ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1257{ 1258 1259 if (msg->login.version != CTL_HA_VERSION) { 1260 printf("CTL HA peers have different versions %d != %d\n", 1261 msg->login.version, CTL_HA_VERSION); 1262 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1263 return; 1264 } 1265 if (msg->login.ha_mode != softc->ha_mode) { 1266 printf("CTL HA peers have different ha_mode %d != %d\n", 1267 msg->login.ha_mode, softc->ha_mode); 1268 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1269 return; 1270 } 1271 if (msg->login.ha_id == softc->ha_id) { 1272 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id); 1273 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1274 return; 1275 } 1276 if (msg->login.max_luns != CTL_MAX_LUNS || 1277 msg->login.max_ports != CTL_MAX_PORTS || 1278 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) { 1279 printf("CTL HA peers have different limits\n"); 1280 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1281 return; 1282 } 1283} 1284 1285static void 1286ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len) 1287{ 1288 struct ctl_lun *lun; 1289 int i; 1290 uint32_t initidx, targ_lun; 1291 1292 targ_lun = msg->hdr.nexus.targ_mapped_lun; 1293 mtx_lock(&softc->ctl_lock); 1294 if ((targ_lun >= CTL_MAX_LUNS) || 1295 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 1296 mtx_unlock(&softc->ctl_lock); 1297 return; 1298 } 1299 mtx_lock(&lun->lun_lock); 1300 mtx_unlock(&softc->ctl_lock); 1301 if (lun->flags & CTL_LUN_DISABLED) { 1302 mtx_unlock(&lun->lun_lock); 1303 return; 1304 } 1305 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 1306 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) == 1307 msg->mode.page_code && 1308 lun->mode_pages.index[i].subpage == msg->mode.subpage) 1309 break; 1310 } 1311 if (i == CTL_NUM_MODE_PAGES) { 1312 mtx_unlock(&lun->lun_lock); 1313 return; 1314 } 1315 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data, 1316 lun->mode_pages.index[i].page_len); 1317 initidx = ctl_get_initindex(&msg->hdr.nexus); 1318 if (initidx != -1) 1319 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 1320 mtx_unlock(&lun->lun_lock); 1321} 1322 1323/* 1324 * ISC (Inter Shelf Communication) event handler. Events from the HA 1325 * subsystem come in here. 1326 */ 1327static void 1328ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param) 1329{ 1330 struct ctl_softc *softc = control_softc; 1331 union ctl_io *io; 1332 struct ctl_prio *presio; 1333 ctl_ha_status isc_status; 1334 1335 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event)); 1336 if (event == CTL_HA_EVT_MSG_RECV) { 1337 union ctl_ha_msg *msg, msgbuf; 1338 1339 if (param > sizeof(msgbuf)) 1340 msg = malloc(param, M_CTL, M_WAITOK); 1341 else 1342 msg = &msgbuf; 1343 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param, 1344 M_WAITOK); 1345 if (isc_status != CTL_HA_STATUS_SUCCESS) { 1346 printf("%s: Error receiving message: %d\n", 1347 __func__, isc_status); 1348 if (msg != &msgbuf) 1349 free(msg, M_CTL); 1350 return; 1351 } 1352 1353 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type)); 1354 switch (msg->hdr.msg_type) { 1355 case CTL_MSG_SERIALIZE: 1356 io = ctl_alloc_io(softc->othersc_pool); 1357 ctl_zero_io(io); 1358 // populate ctsio from msg 1359 io->io_hdr.io_type = CTL_IO_SCSI; 1360 io->io_hdr.msg_type = CTL_MSG_SERIALIZE; 1361 io->io_hdr.original_sc = msg->hdr.original_sc; 1362 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC | 1363 CTL_FLAG_IO_ACTIVE; 1364 /* 1365 * If we're in serialization-only mode, we don't 1366 * want to go through full done processing. Thus 1367 * the COPY flag. 1368 * 1369 * XXX KDM add another flag that is more specific. 1370 */ 1371 if (softc->ha_mode != CTL_HA_MODE_XFER) 1372 io->io_hdr.flags |= CTL_FLAG_INT_COPY; 1373 io->io_hdr.nexus = msg->hdr.nexus; 1374#if 0 1375 printf("port %u, iid %u, lun %u\n", 1376 io->io_hdr.nexus.targ_port, 1377 io->io_hdr.nexus.initid, 1378 io->io_hdr.nexus.targ_lun); 1379#endif 1380 io->scsiio.tag_num = msg->scsi.tag_num; 1381 io->scsiio.tag_type = msg->scsi.tag_type; 1382#ifdef CTL_TIME_IO 1383 io->io_hdr.start_time = time_uptime; 1384 getbinuptime(&io->io_hdr.start_bt); 1385#endif /* CTL_TIME_IO */ 1386 io->scsiio.cdb_len = msg->scsi.cdb_len; 1387 memcpy(io->scsiio.cdb, msg->scsi.cdb, 1388 CTL_MAX_CDBLEN); 1389 if (softc->ha_mode == CTL_HA_MODE_XFER) { 1390 const struct ctl_cmd_entry *entry; 1391 1392 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 1393 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 1394 io->io_hdr.flags |= 1395 entry->flags & CTL_FLAG_DATA_MASK; 1396 } 1397 ctl_enqueue_isc(io); 1398 break; 1399 1400 /* Performed on the Originating SC, XFER mode only */ 1401 case CTL_MSG_DATAMOVE: { 1402 struct ctl_sg_entry *sgl; 1403 int i, j; 1404 1405 io = msg->hdr.original_sc; 1406 if (io == NULL) { 1407 printf("%s: original_sc == NULL!\n", __func__); 1408 /* XXX KDM do something here */ 1409 break; 1410 } 1411 io->io_hdr.msg_type = CTL_MSG_DATAMOVE; 1412 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1413 /* 1414 * Keep track of this, we need to send it back over 1415 * when the datamove is complete. 1416 */ 1417 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1418 if (msg->hdr.status == CTL_SUCCESS) 1419 io->io_hdr.status = msg->hdr.status; 1420 1421 if (msg->dt.sg_sequence == 0) { 1422#ifdef CTL_TIME_IO 1423 getbinuptime(&io->io_hdr.dma_start_bt); 1424#endif 1425 i = msg->dt.kern_sg_entries + 1426 msg->dt.kern_data_len / 1427 CTL_HA_DATAMOVE_SEGMENT + 1; 1428 sgl = malloc(sizeof(*sgl) * i, M_CTL, 1429 M_WAITOK | M_ZERO); 1430 io->io_hdr.remote_sglist = sgl; 1431 io->io_hdr.local_sglist = 1432 &sgl[msg->dt.kern_sg_entries]; 1433 1434 io->scsiio.kern_data_ptr = (uint8_t *)sgl; 1435 1436 io->scsiio.kern_sg_entries = 1437 msg->dt.kern_sg_entries; 1438 io->scsiio.rem_sg_entries = 1439 msg->dt.kern_sg_entries; 1440 io->scsiio.kern_data_len = 1441 msg->dt.kern_data_len; 1442 io->scsiio.kern_total_len = 1443 msg->dt.kern_total_len; 1444 io->scsiio.kern_data_resid = 1445 msg->dt.kern_data_resid; 1446 io->scsiio.kern_rel_offset = 1447 msg->dt.kern_rel_offset; 1448 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR; 1449 io->io_hdr.flags |= msg->dt.flags & 1450 CTL_FLAG_BUS_ADDR; 1451 } else 1452 sgl = (struct ctl_sg_entry *) 1453 io->scsiio.kern_data_ptr; 1454 1455 for (i = msg->dt.sent_sg_entries, j = 0; 1456 i < (msg->dt.sent_sg_entries + 1457 msg->dt.cur_sg_entries); i++, j++) { 1458 sgl[i].addr = msg->dt.sg_list[j].addr; 1459 sgl[i].len = msg->dt.sg_list[j].len; 1460 1461#if 0 1462 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n", 1463 __func__, sgl[i].addr, sgl[i].len, j, i); 1464#endif 1465 } 1466 1467 /* 1468 * If this is the last piece of the I/O, we've got 1469 * the full S/G list. Queue processing in the thread. 1470 * Otherwise wait for the next piece. 1471 */ 1472 if (msg->dt.sg_last != 0) 1473 ctl_enqueue_isc(io); 1474 break; 1475 } 1476 /* Performed on the Serializing (primary) SC, XFER mode only */ 1477 case CTL_MSG_DATAMOVE_DONE: { 1478 if (msg->hdr.serializing_sc == NULL) { 1479 printf("%s: serializing_sc == NULL!\n", 1480 __func__); 1481 /* XXX KDM now what? */ 1482 break; 1483 } 1484 /* 1485 * We grab the sense information here in case 1486 * there was a failure, so we can return status 1487 * back to the initiator. 1488 */ 1489 io = msg->hdr.serializing_sc; 1490 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 1491 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG; 1492 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1493 io->io_hdr.port_status = msg->scsi.fetd_status; 1494 io->scsiio.residual = msg->scsi.residual; 1495 if (msg->hdr.status != CTL_STATUS_NONE) { 1496 io->io_hdr.status = msg->hdr.status; 1497 io->scsiio.scsi_status = msg->scsi.scsi_status; 1498 io->scsiio.sense_len = msg->scsi.sense_len; 1499 io->scsiio.sense_residual =msg->scsi.sense_residual; 1500 memcpy(&io->scsiio.sense_data, 1501 &msg->scsi.sense_data, 1502 msg->scsi.sense_len); 1503 if (msg->hdr.status == CTL_SUCCESS) 1504 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT; 1505 } 1506 ctl_enqueue_isc(io); 1507 break; 1508 } 1509 1510 /* Preformed on Originating SC, SER_ONLY mode */ 1511 case CTL_MSG_R2R: 1512 io = msg->hdr.original_sc; 1513 if (io == NULL) { 1514 printf("%s: original_sc == NULL!\n", 1515 __func__); 1516 break; 1517 } 1518 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1519 io->io_hdr.msg_type = CTL_MSG_R2R; 1520 io->io_hdr.serializing_sc = msg->hdr.serializing_sc; 1521 ctl_enqueue_isc(io); 1522 break; 1523 1524 /* 1525 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY 1526 * mode. 1527 * Performed on the Originating (i.e. secondary) SC in XFER 1528 * mode 1529 */ 1530 case CTL_MSG_FINISH_IO: 1531 if (softc->ha_mode == CTL_HA_MODE_XFER) 1532 ctl_isc_handler_finish_xfer(softc, msg); 1533 else 1534 ctl_isc_handler_finish_ser_only(softc, msg); 1535 break; 1536 1537 /* Preformed on Originating SC */ 1538 case CTL_MSG_BAD_JUJU: 1539 io = msg->hdr.original_sc; 1540 if (io == NULL) { 1541 printf("%s: Bad JUJU!, original_sc is NULL!\n", 1542 __func__); 1543 break; 1544 } 1545 ctl_copy_sense_data(msg, io); 1546 /* 1547 * IO should have already been cleaned up on other 1548 * SC so clear this flag so we won't send a message 1549 * back to finish the IO there. 1550 */ 1551 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC; 1552 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE; 1553 1554 /* io = msg->hdr.serializing_sc; */ 1555 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU; 1556 ctl_enqueue_isc(io); 1557 break; 1558 1559 /* Handle resets sent from the other side */ 1560 case CTL_MSG_MANAGE_TASKS: { 1561 struct ctl_taskio *taskio; 1562 taskio = (struct ctl_taskio *)ctl_alloc_io( 1563 softc->othersc_pool); 1564 ctl_zero_io((union ctl_io *)taskio); 1565 taskio->io_hdr.io_type = CTL_IO_TASK; 1566 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1567 taskio->io_hdr.nexus = msg->hdr.nexus; 1568 taskio->task_action = msg->task.task_action; 1569 taskio->tag_num = msg->task.tag_num; 1570 taskio->tag_type = msg->task.tag_type; 1571#ifdef CTL_TIME_IO 1572 taskio->io_hdr.start_time = time_uptime; 1573 getbinuptime(&taskio->io_hdr.start_bt); 1574#endif /* CTL_TIME_IO */ 1575 ctl_run_task((union ctl_io *)taskio); 1576 break; 1577 } 1578 /* Persistent Reserve action which needs attention */ 1579 case CTL_MSG_PERS_ACTION: 1580 presio = (struct ctl_prio *)ctl_alloc_io( 1581 softc->othersc_pool); 1582 ctl_zero_io((union ctl_io *)presio); 1583 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION; 1584 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC; 1585 presio->io_hdr.nexus = msg->hdr.nexus; 1586 presio->pr_msg = msg->pr; 1587 ctl_enqueue_isc((union ctl_io *)presio); 1588 break; 1589 case CTL_MSG_UA: 1590 ctl_isc_ua(softc, msg, param); 1591 break; 1592 case CTL_MSG_PORT_SYNC: 1593 ctl_isc_port_sync(softc, msg, param); 1594 break; 1595 case CTL_MSG_LUN_SYNC: 1596 ctl_isc_lun_sync(softc, msg, param); 1597 break; 1598 case CTL_MSG_IID_SYNC: 1599 ctl_isc_iid_sync(softc, msg, param); 1600 break; 1601 case CTL_MSG_LOGIN: 1602 ctl_isc_login(softc, msg, param); 1603 break; 1604 case CTL_MSG_MODE_SYNC: 1605 ctl_isc_mode_sync(softc, msg, param); 1606 break; 1607 default: 1608 printf("Received HA message of unknown type %d\n", 1609 msg->hdr.msg_type); 1610 ctl_ha_msg_abort(CTL_HA_CHAN_CTL); 1611 break; 1612 } 1613 if (msg != &msgbuf) 1614 free(msg, M_CTL); 1615 } else if (event == CTL_HA_EVT_LINK_CHANGE) { 1616 printf("CTL: HA link status changed from %d to %d\n", 1617 softc->ha_link, param); 1618 if (param == softc->ha_link) 1619 return; 1620 if (softc->ha_link == CTL_HA_LINK_ONLINE) { 1621 softc->ha_link = param; 1622 ctl_isc_ha_link_down(softc); 1623 } else { 1624 softc->ha_link = param; 1625 if (softc->ha_link == CTL_HA_LINK_ONLINE) 1626 ctl_isc_ha_link_up(softc); 1627 } 1628 return; 1629 } else { 1630 printf("ctl_isc_event_handler: Unknown event %d\n", event); 1631 return; 1632 } 1633} 1634 1635static void 1636ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest) 1637{ 1638 1639 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data, 1640 src->scsi.sense_len); 1641 dest->scsiio.scsi_status = src->scsi.scsi_status; 1642 dest->scsiio.sense_len = src->scsi.sense_len; 1643 dest->io_hdr.status = src->hdr.status; 1644} 1645 1646static void 1647ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest) 1648{ 1649 1650 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data, 1651 src->scsiio.sense_len); 1652 dest->scsi.scsi_status = src->scsiio.scsi_status; 1653 dest->scsi.sense_len = src->scsiio.sense_len; 1654 dest->hdr.status = src->io_hdr.status; 1655} 1656 1657void 1658ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1659{ 1660 struct ctl_softc *softc = lun->ctl_softc; 1661 ctl_ua_type *pu; 1662 1663 if (initidx < softc->init_min || initidx >= softc->init_max) 1664 return; 1665 mtx_assert(&lun->lun_lock, MA_OWNED); 1666 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1667 if (pu == NULL) 1668 return; 1669 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua; 1670} 1671 1672void 1673ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua) 1674{ 1675 int i; 1676 1677 mtx_assert(&lun->lun_lock, MA_OWNED); 1678 if (lun->pending_ua[port] == NULL) 1679 return; 1680 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 1681 if (port * CTL_MAX_INIT_PER_PORT + i == except) 1682 continue; 1683 lun->pending_ua[port][i] |= ua; 1684 } 1685} 1686 1687void 1688ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1689{ 1690 struct ctl_softc *softc = lun->ctl_softc; 1691 int i; 1692 1693 mtx_assert(&lun->lun_lock, MA_OWNED); 1694 for (i = softc->port_min; i < softc->port_max; i++) 1695 ctl_est_ua_port(lun, i, except, ua); 1696} 1697 1698void 1699ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua) 1700{ 1701 struct ctl_softc *softc = lun->ctl_softc; 1702 ctl_ua_type *pu; 1703 1704 if (initidx < softc->init_min || initidx >= softc->init_max) 1705 return; 1706 mtx_assert(&lun->lun_lock, MA_OWNED); 1707 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT]; 1708 if (pu == NULL) 1709 return; 1710 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua; 1711} 1712 1713void 1714ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua) 1715{ 1716 struct ctl_softc *softc = lun->ctl_softc; 1717 int i, j; 1718 1719 mtx_assert(&lun->lun_lock, MA_OWNED); 1720 for (i = softc->port_min; i < softc->port_max; i++) { 1721 if (lun->pending_ua[i] == NULL) 1722 continue; 1723 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 1724 if (i * CTL_MAX_INIT_PER_PORT + j == except) 1725 continue; 1726 lun->pending_ua[i][j] &= ~ua; 1727 } 1728 } 1729} 1730 1731void 1732ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx, 1733 ctl_ua_type ua_type) 1734{ 1735 struct ctl_lun *lun; 1736 1737 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED); 1738 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) { 1739 mtx_lock(&lun->lun_lock); 1740 ctl_clr_ua(lun, initidx, ua_type); 1741 mtx_unlock(&lun->lun_lock); 1742 } 1743} 1744 1745static int 1746ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS) 1747{ 1748 struct ctl_softc *softc = (struct ctl_softc *)arg1; 1749 struct ctl_lun *lun; 1750 struct ctl_lun_req ireq; 1751 int error, value; 1752 1753 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1; 1754 error = sysctl_handle_int(oidp, &value, 0, req); 1755 if ((error != 0) || (req->newptr == NULL)) 1756 return (error); 1757 1758 mtx_lock(&softc->ctl_lock); 1759 if (value == 0) 1760 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1761 else 1762 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF; 1763 STAILQ_FOREACH(lun, &softc->lun_list, links) { 1764 mtx_unlock(&softc->ctl_lock); 1765 bzero(&ireq, sizeof(ireq)); 1766 ireq.reqtype = CTL_LUNREQ_MODIFY; 1767 ireq.reqdata.modify.lun_id = lun->lun; 1768 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0, 1769 curthread); 1770 if (ireq.status != CTL_LUN_OK) { 1771 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n", 1772 __func__, ireq.status, ireq.error_str); 1773 } 1774 mtx_lock(&softc->ctl_lock); 1775 } 1776 mtx_unlock(&softc->ctl_lock); 1777 return (0); 1778} 1779 1780static int 1781ctl_init(void) 1782{ 1783 struct ctl_softc *softc; 1784 void *other_pool; 1785 int i, error; 1786 1787 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF, 1788 M_WAITOK | M_ZERO); 1789 1790 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, 1791 "cam/ctl"); 1792 softc->dev->si_drv1 = softc; 1793 1794 sysctl_ctx_init(&softc->sysctl_ctx); 1795 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, 1796 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl", 1797 CTLFLAG_RD, 0, "CAM Target Layer"); 1798 1799 if (softc->sysctl_tree == NULL) { 1800 printf("%s: unable to allocate sysctl tree\n", __func__); 1801 destroy_dev(softc->dev); 1802 free(control_softc, M_DEVBUF); 1803 control_softc = NULL; 1804 return (ENOMEM); 1805 } 1806 1807 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF); 1808 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io), 1809 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1810 softc->flags = 0; 1811 1812 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1813 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0, 1814 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)"); 1815 1816 /* 1817 * In Copan's HA scheme, the "master" and "slave" roles are 1818 * figured out through the slot the controller is in. Although it 1819 * is an active/active system, someone has to be in charge. 1820 */ 1821 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1822 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0, 1823 "HA head ID (0 - no HA)"); 1824 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) { 1825 softc->flags |= CTL_FLAG_ACTIVE_SHELF; 1826 softc->is_single = 1; 1827 softc->port_cnt = CTL_MAX_PORTS; 1828 softc->port_min = 0; 1829 } else { 1830 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS; 1831 softc->port_min = (softc->ha_id - 1) * softc->port_cnt; 1832 } 1833 softc->port_max = softc->port_min + softc->port_cnt; 1834 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT; 1835 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT; 1836 1837 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree), 1838 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0, 1839 "HA link state (0 - offline, 1 - unknown, 2 - online)"); 1840 1841 STAILQ_INIT(&softc->lun_list); 1842 STAILQ_INIT(&softc->pending_lun_queue); 1843 STAILQ_INIT(&softc->fe_list); 1844 STAILQ_INIT(&softc->port_list); 1845 STAILQ_INIT(&softc->be_list); 1846 ctl_tpc_init(softc); 1847 1848 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC, 1849 &other_pool) != 0) 1850 { 1851 printf("ctl: can't allocate %d entry other SC pool, " 1852 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC); 1853 return (ENOMEM); 1854 } 1855 softc->othersc_pool = other_pool; 1856 1857 if (worker_threads <= 0) 1858 worker_threads = max(1, mp_ncpus / 4); 1859 if (worker_threads > CTL_MAX_THREADS) 1860 worker_threads = CTL_MAX_THREADS; 1861 1862 for (i = 0; i < worker_threads; i++) { 1863 struct ctl_thread *thr = &softc->threads[i]; 1864 1865 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF); 1866 thr->ctl_softc = softc; 1867 STAILQ_INIT(&thr->incoming_queue); 1868 STAILQ_INIT(&thr->rtr_queue); 1869 STAILQ_INIT(&thr->done_queue); 1870 STAILQ_INIT(&thr->isc_queue); 1871 1872 error = kproc_kthread_add(ctl_work_thread, thr, 1873 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i); 1874 if (error != 0) { 1875 printf("error creating CTL work thread!\n"); 1876 ctl_pool_free(other_pool); 1877 return (error); 1878 } 1879 } 1880 error = kproc_kthread_add(ctl_lun_thread, softc, 1881 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun"); 1882 if (error != 0) { 1883 printf("error creating CTL lun thread!\n"); 1884 ctl_pool_free(other_pool); 1885 return (error); 1886 } 1887 error = kproc_kthread_add(ctl_thresh_thread, softc, 1888 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh"); 1889 if (error != 0) { 1890 printf("error creating CTL threshold thread!\n"); 1891 ctl_pool_free(other_pool); 1892 return (error); 1893 } 1894 1895 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree), 1896 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN, 1897 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head"); 1898 1899 if (softc->is_single == 0) { 1900 ctl_frontend_register(&ha_frontend); 1901 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) { 1902 printf("ctl_init: ctl_ha_msg_init failed.\n"); 1903 softc->is_single = 1; 1904 } else 1905 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler) 1906 != CTL_HA_STATUS_SUCCESS) { 1907 printf("ctl_init: ctl_ha_msg_register failed.\n"); 1908 softc->is_single = 1; 1909 } 1910 } 1911 return (0); 1912} 1913 1914void 1915ctl_shutdown(void) 1916{ 1917 struct ctl_softc *softc = control_softc; 1918 struct ctl_lun *lun, *next_lun; 1919 1920 if (softc->is_single == 0) { 1921 ctl_ha_msg_shutdown(softc); 1922 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) 1923 != CTL_HA_STATUS_SUCCESS) 1924 printf("%s: ctl_ha_msg_deregister failed.\n", __func__); 1925 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS) 1926 printf("%s: ctl_ha_msg_destroy failed.\n", __func__); 1927 ctl_frontend_deregister(&ha_frontend); 1928 } 1929 1930 mtx_lock(&softc->ctl_lock); 1931 1932 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun) 1933 ctl_free_lun(lun); 1934 1935 mtx_unlock(&softc->ctl_lock); 1936 1937#if 0 1938 ctl_shutdown_thread(softc->work_thread); 1939 mtx_destroy(&softc->queue_lock); 1940#endif 1941 1942 ctl_tpc_shutdown(softc); 1943 uma_zdestroy(softc->io_zone); 1944 mtx_destroy(&softc->ctl_lock); 1945 1946 destroy_dev(softc->dev); 1947 1948 sysctl_ctx_free(&softc->sysctl_ctx); 1949 1950 free(control_softc, M_DEVBUF); 1951 control_softc = NULL; 1952} 1953 1954static int 1955ctl_module_event_handler(module_t mod, int what, void *arg) 1956{ 1957 1958 switch (what) { 1959 case MOD_LOAD: 1960 return (ctl_init()); 1961 case MOD_UNLOAD: 1962 return (EBUSY); 1963 default: 1964 return (EOPNOTSUPP); 1965 } 1966} 1967 1968/* 1969 * XXX KDM should we do some access checks here? Bump a reference count to 1970 * prevent a CTL module from being unloaded while someone has it open? 1971 */ 1972static int 1973ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td) 1974{ 1975 return (0); 1976} 1977 1978static int 1979ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td) 1980{ 1981 return (0); 1982} 1983 1984/* 1985 * Remove an initiator by port number and initiator ID. 1986 * Returns 0 for success, -1 for failure. 1987 */ 1988int 1989ctl_remove_initiator(struct ctl_port *port, int iid) 1990{ 1991 struct ctl_softc *softc = port->ctl_softc; 1992 1993 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 1994 1995 if (iid > CTL_MAX_INIT_PER_PORT) { 1996 printf("%s: initiator ID %u > maximun %u!\n", 1997 __func__, iid, CTL_MAX_INIT_PER_PORT); 1998 return (-1); 1999 } 2000 2001 mtx_lock(&softc->ctl_lock); 2002 port->wwpn_iid[iid].in_use--; 2003 port->wwpn_iid[iid].last_use = time_uptime; 2004 mtx_unlock(&softc->ctl_lock); 2005 ctl_isc_announce_iid(port, iid); 2006 2007 return (0); 2008} 2009 2010/* 2011 * Add an initiator to the initiator map. 2012 * Returns iid for success, < 0 for failure. 2013 */ 2014int 2015ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name) 2016{ 2017 struct ctl_softc *softc = port->ctl_softc; 2018 time_t best_time; 2019 int i, best; 2020 2021 mtx_assert(&softc->ctl_lock, MA_NOTOWNED); 2022 2023 if (iid >= CTL_MAX_INIT_PER_PORT) { 2024 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n", 2025 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT); 2026 free(name, M_CTL); 2027 return (-1); 2028 } 2029 2030 mtx_lock(&softc->ctl_lock); 2031 2032 if (iid < 0 && (wwpn != 0 || name != NULL)) { 2033 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2034 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) { 2035 iid = i; 2036 break; 2037 } 2038 if (name != NULL && port->wwpn_iid[i].name != NULL && 2039 strcmp(name, port->wwpn_iid[i].name) == 0) { 2040 iid = i; 2041 break; 2042 } 2043 } 2044 } 2045 2046 if (iid < 0) { 2047 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2048 if (port->wwpn_iid[i].in_use == 0 && 2049 port->wwpn_iid[i].wwpn == 0 && 2050 port->wwpn_iid[i].name == NULL) { 2051 iid = i; 2052 break; 2053 } 2054 } 2055 } 2056 2057 if (iid < 0) { 2058 best = -1; 2059 best_time = INT32_MAX; 2060 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) { 2061 if (port->wwpn_iid[i].in_use == 0) { 2062 if (port->wwpn_iid[i].last_use < best_time) { 2063 best = i; 2064 best_time = port->wwpn_iid[i].last_use; 2065 } 2066 } 2067 } 2068 iid = best; 2069 } 2070 2071 if (iid < 0) { 2072 mtx_unlock(&softc->ctl_lock); 2073 free(name, M_CTL); 2074 return (-2); 2075 } 2076 2077 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) { 2078 /* 2079 * This is not an error yet. 2080 */ 2081 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) { 2082#if 0 2083 printf("%s: port %d iid %u WWPN %#jx arrived" 2084 " again\n", __func__, port->targ_port, 2085 iid, (uintmax_t)wwpn); 2086#endif 2087 goto take; 2088 } 2089 if (name != NULL && port->wwpn_iid[iid].name != NULL && 2090 strcmp(name, port->wwpn_iid[iid].name) == 0) { 2091#if 0 2092 printf("%s: port %d iid %u name '%s' arrived" 2093 " again\n", __func__, port->targ_port, 2094 iid, name); 2095#endif 2096 goto take; 2097 } 2098 2099 /* 2100 * This is an error, but what do we do about it? The 2101 * driver is telling us we have a new WWPN for this 2102 * initiator ID, so we pretty much need to use it. 2103 */ 2104 printf("%s: port %d iid %u WWPN %#jx '%s' arrived," 2105 " but WWPN %#jx '%s' is still at that address\n", 2106 __func__, port->targ_port, iid, wwpn, name, 2107 (uintmax_t)port->wwpn_iid[iid].wwpn, 2108 port->wwpn_iid[iid].name); 2109 2110 /* 2111 * XXX KDM clear have_ca and ua_pending on each LUN for 2112 * this initiator. 2113 */ 2114 } 2115take: 2116 free(port->wwpn_iid[iid].name, M_CTL); 2117 port->wwpn_iid[iid].name = name; 2118 port->wwpn_iid[iid].wwpn = wwpn; 2119 port->wwpn_iid[iid].in_use++; 2120 mtx_unlock(&softc->ctl_lock); 2121 ctl_isc_announce_iid(port, iid); 2122 2123 return (iid); 2124} 2125 2126static int 2127ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf) 2128{ 2129 int len; 2130 2131 switch (port->port_type) { 2132 case CTL_PORT_FC: 2133 { 2134 struct scsi_transportid_fcp *id = 2135 (struct scsi_transportid_fcp *)buf; 2136 if (port->wwpn_iid[iid].wwpn == 0) 2137 return (0); 2138 memset(id, 0, sizeof(*id)); 2139 id->format_protocol = SCSI_PROTO_FC; 2140 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name); 2141 return (sizeof(*id)); 2142 } 2143 case CTL_PORT_ISCSI: 2144 { 2145 struct scsi_transportid_iscsi_port *id = 2146 (struct scsi_transportid_iscsi_port *)buf; 2147 if (port->wwpn_iid[iid].name == NULL) 2148 return (0); 2149 memset(id, 0, 256); 2150 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT | 2151 SCSI_PROTO_ISCSI; 2152 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1; 2153 len = roundup2(min(len, 252), 4); 2154 scsi_ulto2b(len, id->additional_length); 2155 return (sizeof(*id) + len); 2156 } 2157 case CTL_PORT_SAS: 2158 { 2159 struct scsi_transportid_sas *id = 2160 (struct scsi_transportid_sas *)buf; 2161 if (port->wwpn_iid[iid].wwpn == 0) 2162 return (0); 2163 memset(id, 0, sizeof(*id)); 2164 id->format_protocol = SCSI_PROTO_SAS; 2165 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address); 2166 return (sizeof(*id)); 2167 } 2168 default: 2169 { 2170 struct scsi_transportid_spi *id = 2171 (struct scsi_transportid_spi *)buf; 2172 memset(id, 0, sizeof(*id)); 2173 id->format_protocol = SCSI_PROTO_SPI; 2174 scsi_ulto2b(iid, id->scsi_addr); 2175 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id); 2176 return (sizeof(*id)); 2177 } 2178 } 2179} 2180 2181/* 2182 * Serialize a command that went down the "wrong" side, and so was sent to 2183 * this controller for execution. The logic is a little different than the 2184 * standard case in ctl_scsiio_precheck(). Errors in this case need to get 2185 * sent back to the other side, but in the success case, we execute the 2186 * command on this side (XFER mode) or tell the other side to execute it 2187 * (SER_ONLY mode). 2188 */ 2189static int 2190ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio) 2191{ 2192 struct ctl_softc *softc = control_softc; 2193 union ctl_ha_msg msg_info; 2194 struct ctl_port *port; 2195 struct ctl_lun *lun; 2196 const struct ctl_cmd_entry *entry; 2197 int retval = 0; 2198 uint32_t targ_lun; 2199 2200 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 2201 mtx_lock(&softc->ctl_lock); 2202 2203 /* Make sure that we know about this port. */ 2204 port = ctl_io_port(&ctsio->io_hdr); 2205 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) { 2206 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2207 /*retry_count*/ 1); 2208 goto badjuju; 2209 } 2210 2211 /* Make sure that we know about this LUN. */ 2212 if ((targ_lun < CTL_MAX_LUNS) && 2213 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 2214 mtx_lock(&lun->lun_lock); 2215 mtx_unlock(&softc->ctl_lock); 2216 /* 2217 * If the LUN is invalid, pretend that it doesn't exist. 2218 * It will go away as soon as all pending I/O has been 2219 * completed. 2220 */ 2221 if (lun->flags & CTL_LUN_DISABLED) { 2222 mtx_unlock(&lun->lun_lock); 2223 lun = NULL; 2224 } 2225 } else { 2226 mtx_unlock(&softc->ctl_lock); 2227 lun = NULL; 2228 } 2229 if (lun == NULL) { 2230 /* 2231 * The other node would not send this request to us unless 2232 * received announce that we are primary node for this LUN. 2233 * If this LUN does not exist now, it is probably result of 2234 * a race, so respond to initiator in the most opaque way. 2235 */ 2236 ctl_set_busy(ctsio); 2237 goto badjuju; 2238 } 2239 2240 entry = ctl_get_cmd_entry(ctsio, NULL); 2241 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 2242 mtx_unlock(&lun->lun_lock); 2243 goto badjuju; 2244 } 2245 2246 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 2247 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun; 2248 2249 /* 2250 * Every I/O goes into the OOA queue for a 2251 * particular LUN, and stays there until completion. 2252 */ 2253#ifdef CTL_TIME_IO 2254 if (TAILQ_EMPTY(&lun->ooa_queue)) 2255 lun->idle_time += getsbinuptime() - lun->last_busy; 2256#endif 2257 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2258 2259 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 2260 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, 2261 ooa_links))) { 2262 case CTL_ACTION_BLOCK: 2263 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 2264 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 2265 blocked_links); 2266 mtx_unlock(&lun->lun_lock); 2267 break; 2268 case CTL_ACTION_PASS: 2269 case CTL_ACTION_SKIP: 2270 if (softc->ha_mode == CTL_HA_MODE_XFER) { 2271 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 2272 ctl_enqueue_rtr((union ctl_io *)ctsio); 2273 mtx_unlock(&lun->lun_lock); 2274 } else { 2275 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 2276 mtx_unlock(&lun->lun_lock); 2277 2278 /* send msg back to other side */ 2279 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2280 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio; 2281 msg_info.hdr.msg_type = CTL_MSG_R2R; 2282 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2283 sizeof(msg_info.hdr), M_WAITOK); 2284 } 2285 break; 2286 case CTL_ACTION_OVERLAP: 2287 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2288 mtx_unlock(&lun->lun_lock); 2289 ctl_set_overlapped_cmd(ctsio); 2290 goto badjuju; 2291 case CTL_ACTION_OVERLAP_TAG: 2292 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2293 mtx_unlock(&lun->lun_lock); 2294 ctl_set_overlapped_tag(ctsio, ctsio->tag_num); 2295 goto badjuju; 2296 case CTL_ACTION_ERROR: 2297 default: 2298 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links); 2299 mtx_unlock(&lun->lun_lock); 2300 2301 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0, 2302 /*retry_count*/ 0); 2303badjuju: 2304 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info); 2305 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc; 2306 msg_info.hdr.serializing_sc = NULL; 2307 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU; 2308 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 2309 sizeof(msg_info.scsi), M_WAITOK); 2310 retval = 1; 2311 break; 2312 } 2313 return (retval); 2314} 2315 2316/* 2317 * Returns 0 for success, errno for failure. 2318 */ 2319static void 2320ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num, 2321 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries) 2322{ 2323 union ctl_io *io; 2324 2325 mtx_lock(&lun->lun_lock); 2326 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL); 2327 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, 2328 ooa_links)) { 2329 struct ctl_ooa_entry *entry; 2330 2331 /* 2332 * If we've got more than we can fit, just count the 2333 * remaining entries. 2334 */ 2335 if (*cur_fill_num >= ooa_hdr->alloc_num) 2336 continue; 2337 2338 entry = &kern_entries[*cur_fill_num]; 2339 2340 entry->tag_num = io->scsiio.tag_num; 2341 entry->lun_num = lun->lun; 2342#ifdef CTL_TIME_IO 2343 entry->start_bt = io->io_hdr.start_bt; 2344#endif 2345 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len); 2346 entry->cdb_len = io->scsiio.cdb_len; 2347 if (io->io_hdr.flags & CTL_FLAG_BLOCKED) 2348 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED; 2349 2350 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG) 2351 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA; 2352 2353 if (io->io_hdr.flags & CTL_FLAG_ABORT) 2354 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT; 2355 2356 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR) 2357 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR; 2358 2359 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED) 2360 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED; 2361 } 2362 mtx_unlock(&lun->lun_lock); 2363} 2364 2365static void * 2366ctl_copyin_alloc(void *user_addr, int len, char *error_str, 2367 size_t error_str_len) 2368{ 2369 void *kptr; 2370 2371 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO); 2372 2373 if (copyin(user_addr, kptr, len) != 0) { 2374 snprintf(error_str, error_str_len, "Error copying %d bytes " 2375 "from user address %p to kernel address %p", len, 2376 user_addr, kptr); 2377 free(kptr, M_CTL); 2378 return (NULL); 2379 } 2380 2381 return (kptr); 2382} 2383 2384static void 2385ctl_free_args(int num_args, struct ctl_be_arg *args) 2386{ 2387 int i; 2388 2389 if (args == NULL) 2390 return; 2391 2392 for (i = 0; i < num_args; i++) { 2393 free(args[i].kname, M_CTL); 2394 free(args[i].kvalue, M_CTL); 2395 } 2396 2397 free(args, M_CTL); 2398} 2399 2400static struct ctl_be_arg * 2401ctl_copyin_args(int num_args, struct ctl_be_arg *uargs, 2402 char *error_str, size_t error_str_len) 2403{ 2404 struct ctl_be_arg *args; 2405 int i; 2406 2407 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args), 2408 error_str, error_str_len); 2409 2410 if (args == NULL) 2411 goto bailout; 2412 2413 for (i = 0; i < num_args; i++) { 2414 args[i].kname = NULL; 2415 args[i].kvalue = NULL; 2416 } 2417 2418 for (i = 0; i < num_args; i++) { 2419 uint8_t *tmpptr; 2420 2421 args[i].kname = ctl_copyin_alloc(args[i].name, 2422 args[i].namelen, error_str, error_str_len); 2423 if (args[i].kname == NULL) 2424 goto bailout; 2425 2426 if (args[i].kname[args[i].namelen - 1] != '\0') { 2427 snprintf(error_str, error_str_len, "Argument %d " 2428 "name is not NUL-terminated", i); 2429 goto bailout; 2430 } 2431 2432 if (args[i].flags & CTL_BEARG_RD) { 2433 tmpptr = ctl_copyin_alloc(args[i].value, 2434 args[i].vallen, error_str, error_str_len); 2435 if (tmpptr == NULL) 2436 goto bailout; 2437 if ((args[i].flags & CTL_BEARG_ASCII) 2438 && (tmpptr[args[i].vallen - 1] != '\0')) { 2439 snprintf(error_str, error_str_len, "Argument " 2440 "%d value is not NUL-terminated", i); 2441 goto bailout; 2442 } 2443 args[i].kvalue = tmpptr; 2444 } else { 2445 args[i].kvalue = malloc(args[i].vallen, 2446 M_CTL, M_WAITOK | M_ZERO); 2447 } 2448 } 2449 2450 return (args); 2451bailout: 2452 2453 ctl_free_args(num_args, args); 2454 2455 return (NULL); 2456} 2457 2458static void 2459ctl_copyout_args(int num_args, struct ctl_be_arg *args) 2460{ 2461 int i; 2462 2463 for (i = 0; i < num_args; i++) { 2464 if (args[i].flags & CTL_BEARG_WR) 2465 copyout(args[i].kvalue, args[i].value, args[i].vallen); 2466 } 2467} 2468 2469/* 2470 * Escape characters that are illegal or not recommended in XML. 2471 */ 2472int 2473ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size) 2474{ 2475 char *end = str + size; 2476 int retval; 2477 2478 retval = 0; 2479 2480 for (; *str && str < end; str++) { 2481 switch (*str) { 2482 case '&': 2483 retval = sbuf_printf(sb, "&"); 2484 break; 2485 case '>': 2486 retval = sbuf_printf(sb, ">"); 2487 break; 2488 case '<': 2489 retval = sbuf_printf(sb, "<"); 2490 break; 2491 default: 2492 retval = sbuf_putc(sb, *str); 2493 break; 2494 } 2495 2496 if (retval != 0) 2497 break; 2498 2499 } 2500 2501 return (retval); 2502} 2503 2504static void 2505ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb) 2506{ 2507 struct scsi_vpd_id_descriptor *desc; 2508 int i; 2509 2510 if (id == NULL || id->len < 4) 2511 return; 2512 desc = (struct scsi_vpd_id_descriptor *)id->data; 2513 switch (desc->id_type & SVPD_ID_TYPE_MASK) { 2514 case SVPD_ID_TYPE_T10: 2515 sbuf_printf(sb, "t10."); 2516 break; 2517 case SVPD_ID_TYPE_EUI64: 2518 sbuf_printf(sb, "eui."); 2519 break; 2520 case SVPD_ID_TYPE_NAA: 2521 sbuf_printf(sb, "naa."); 2522 break; 2523 case SVPD_ID_TYPE_SCSI_NAME: 2524 break; 2525 } 2526 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) { 2527 case SVPD_ID_CODESET_BINARY: 2528 for (i = 0; i < desc->length; i++) 2529 sbuf_printf(sb, "%02x", desc->identifier[i]); 2530 break; 2531 case SVPD_ID_CODESET_ASCII: 2532 sbuf_printf(sb, "%.*s", (int)desc->length, 2533 (char *)desc->identifier); 2534 break; 2535 case SVPD_ID_CODESET_UTF8: 2536 sbuf_printf(sb, "%s", (char *)desc->identifier); 2537 break; 2538 } 2539} 2540 2541static int 2542ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, 2543 struct thread *td) 2544{ 2545 struct ctl_softc *softc = dev->si_drv1; 2546 struct ctl_lun *lun; 2547 int retval; 2548 2549 retval = 0; 2550 2551 switch (cmd) { 2552 case CTL_IO: 2553 retval = ctl_ioctl_io(dev, cmd, addr, flag, td); 2554 break; 2555 case CTL_ENABLE_PORT: 2556 case CTL_DISABLE_PORT: 2557 case CTL_SET_PORT_WWNS: { 2558 struct ctl_port *port; 2559 struct ctl_port_entry *entry; 2560 2561 entry = (struct ctl_port_entry *)addr; 2562 2563 mtx_lock(&softc->ctl_lock); 2564 STAILQ_FOREACH(port, &softc->port_list, links) { 2565 int action, done; 2566 2567 if (port->targ_port < softc->port_min || 2568 port->targ_port >= softc->port_max) 2569 continue; 2570 2571 action = 0; 2572 done = 0; 2573 if ((entry->port_type == CTL_PORT_NONE) 2574 && (entry->targ_port == port->targ_port)) { 2575 /* 2576 * If the user only wants to enable or 2577 * disable or set WWNs on a specific port, 2578 * do the operation and we're done. 2579 */ 2580 action = 1; 2581 done = 1; 2582 } else if (entry->port_type & port->port_type) { 2583 /* 2584 * Compare the user's type mask with the 2585 * particular frontend type to see if we 2586 * have a match. 2587 */ 2588 action = 1; 2589 done = 0; 2590 2591 /* 2592 * Make sure the user isn't trying to set 2593 * WWNs on multiple ports at the same time. 2594 */ 2595 if (cmd == CTL_SET_PORT_WWNS) { 2596 printf("%s: Can't set WWNs on " 2597 "multiple ports\n", __func__); 2598 retval = EINVAL; 2599 break; 2600 } 2601 } 2602 if (action == 0) 2603 continue; 2604 2605 /* 2606 * XXX KDM we have to drop the lock here, because 2607 * the online/offline operations can potentially 2608 * block. We need to reference count the frontends 2609 * so they can't go away, 2610 */ 2611 if (cmd == CTL_ENABLE_PORT) { 2612 mtx_unlock(&softc->ctl_lock); 2613 ctl_port_online(port); 2614 mtx_lock(&softc->ctl_lock); 2615 } else if (cmd == CTL_DISABLE_PORT) { 2616 mtx_unlock(&softc->ctl_lock); 2617 ctl_port_offline(port); 2618 mtx_lock(&softc->ctl_lock); 2619 } else if (cmd == CTL_SET_PORT_WWNS) { 2620 ctl_port_set_wwns(port, 2621 (entry->flags & CTL_PORT_WWNN_VALID) ? 2622 1 : 0, entry->wwnn, 2623 (entry->flags & CTL_PORT_WWPN_VALID) ? 2624 1 : 0, entry->wwpn); 2625 } 2626 if (done != 0) 2627 break; 2628 } 2629 mtx_unlock(&softc->ctl_lock); 2630 break; 2631 } 2632 case CTL_GET_OOA: { 2633 struct ctl_ooa *ooa_hdr; 2634 struct ctl_ooa_entry *entries; 2635 uint32_t cur_fill_num; 2636 2637 ooa_hdr = (struct ctl_ooa *)addr; 2638 2639 if ((ooa_hdr->alloc_len == 0) 2640 || (ooa_hdr->alloc_num == 0)) { 2641 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u " 2642 "must be non-zero\n", __func__, 2643 ooa_hdr->alloc_len, ooa_hdr->alloc_num); 2644 retval = EINVAL; 2645 break; 2646 } 2647 2648 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num * 2649 sizeof(struct ctl_ooa_entry))) { 2650 printf("%s: CTL_GET_OOA: alloc len %u must be alloc " 2651 "num %d * sizeof(struct ctl_ooa_entry) %zd\n", 2652 __func__, ooa_hdr->alloc_len, 2653 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry)); 2654 retval = EINVAL; 2655 break; 2656 } 2657 2658 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO); 2659 if (entries == NULL) { 2660 printf("%s: could not allocate %d bytes for OOA " 2661 "dump\n", __func__, ooa_hdr->alloc_len); 2662 retval = ENOMEM; 2663 break; 2664 } 2665 2666 mtx_lock(&softc->ctl_lock); 2667 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0) 2668 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS) 2669 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) { 2670 mtx_unlock(&softc->ctl_lock); 2671 free(entries, M_CTL); 2672 printf("%s: CTL_GET_OOA: invalid LUN %ju\n", 2673 __func__, (uintmax_t)ooa_hdr->lun_num); 2674 retval = EINVAL; 2675 break; 2676 } 2677 2678 cur_fill_num = 0; 2679 2680 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) { 2681 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2682 ctl_ioctl_fill_ooa(lun, &cur_fill_num, 2683 ooa_hdr, entries); 2684 } 2685 } else { 2686 lun = softc->ctl_luns[ooa_hdr->lun_num]; 2687 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr, 2688 entries); 2689 } 2690 mtx_unlock(&softc->ctl_lock); 2691 2692 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num); 2693 ooa_hdr->fill_len = ooa_hdr->fill_num * 2694 sizeof(struct ctl_ooa_entry); 2695 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len); 2696 if (retval != 0) { 2697 printf("%s: error copying out %d bytes for OOA dump\n", 2698 __func__, ooa_hdr->fill_len); 2699 } 2700 2701 getbinuptime(&ooa_hdr->cur_bt); 2702 2703 if (cur_fill_num > ooa_hdr->alloc_num) { 2704 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num; 2705 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE; 2706 } else { 2707 ooa_hdr->dropped_num = 0; 2708 ooa_hdr->status = CTL_OOA_OK; 2709 } 2710 2711 free(entries, M_CTL); 2712 break; 2713 } 2714 case CTL_DELAY_IO: { 2715 struct ctl_io_delay_info *delay_info; 2716 2717 delay_info = (struct ctl_io_delay_info *)addr; 2718 2719#ifdef CTL_IO_DELAY 2720 mtx_lock(&softc->ctl_lock); 2721 2722 if ((delay_info->lun_id >= CTL_MAX_LUNS) 2723 || (softc->ctl_luns[delay_info->lun_id] == NULL)) { 2724 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN; 2725 } else { 2726 lun = softc->ctl_luns[delay_info->lun_id]; 2727 mtx_lock(&lun->lun_lock); 2728 2729 delay_info->status = CTL_DELAY_STATUS_OK; 2730 2731 switch (delay_info->delay_type) { 2732 case CTL_DELAY_TYPE_CONT: 2733 break; 2734 case CTL_DELAY_TYPE_ONESHOT: 2735 break; 2736 default: 2737 delay_info->status = 2738 CTL_DELAY_STATUS_INVALID_TYPE; 2739 break; 2740 } 2741 2742 switch (delay_info->delay_loc) { 2743 case CTL_DELAY_LOC_DATAMOVE: 2744 lun->delay_info.datamove_type = 2745 delay_info->delay_type; 2746 lun->delay_info.datamove_delay = 2747 delay_info->delay_secs; 2748 break; 2749 case CTL_DELAY_LOC_DONE: 2750 lun->delay_info.done_type = 2751 delay_info->delay_type; 2752 lun->delay_info.done_delay = 2753 delay_info->delay_secs; 2754 break; 2755 default: 2756 delay_info->status = 2757 CTL_DELAY_STATUS_INVALID_LOC; 2758 break; 2759 } 2760 mtx_unlock(&lun->lun_lock); 2761 } 2762 2763 mtx_unlock(&softc->ctl_lock); 2764#else 2765 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED; 2766#endif /* CTL_IO_DELAY */ 2767 break; 2768 } 2769 case CTL_GETSTATS: { 2770 struct ctl_stats *stats; 2771 int i; 2772 2773 stats = (struct ctl_stats *)addr; 2774 2775 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) > 2776 stats->alloc_len) { 2777 stats->status = CTL_SS_NEED_MORE_SPACE; 2778 stats->num_luns = softc->num_luns; 2779 break; 2780 } 2781 /* 2782 * XXX KDM no locking here. If the LUN list changes, 2783 * things can blow up. 2784 */ 2785 i = 0; 2786 STAILQ_FOREACH(lun, &softc->lun_list, links) { 2787 retval = copyout(&lun->stats, &stats->lun_stats[i++], 2788 sizeof(lun->stats)); 2789 if (retval != 0) 2790 break; 2791 } 2792 stats->num_luns = softc->num_luns; 2793 stats->fill_len = sizeof(struct ctl_lun_io_stats) * 2794 softc->num_luns; 2795 stats->status = CTL_SS_OK; 2796#ifdef CTL_TIME_IO 2797 stats->flags = CTL_STATS_FLAG_TIME_VALID; 2798#else 2799 stats->flags = CTL_STATS_FLAG_NONE; 2800#endif 2801 getnanouptime(&stats->timestamp); 2802 break; 2803 } 2804 case CTL_ERROR_INJECT: { 2805 struct ctl_error_desc *err_desc, *new_err_desc; 2806 2807 err_desc = (struct ctl_error_desc *)addr; 2808 2809 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL, 2810 M_WAITOK | M_ZERO); 2811 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc)); 2812 2813 mtx_lock(&softc->ctl_lock); 2814 lun = softc->ctl_luns[err_desc->lun_id]; 2815 if (lun == NULL) { 2816 mtx_unlock(&softc->ctl_lock); 2817 free(new_err_desc, M_CTL); 2818 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n", 2819 __func__, (uintmax_t)err_desc->lun_id); 2820 retval = EINVAL; 2821 break; 2822 } 2823 mtx_lock(&lun->lun_lock); 2824 mtx_unlock(&softc->ctl_lock); 2825 2826 /* 2827 * We could do some checking here to verify the validity 2828 * of the request, but given the complexity of error 2829 * injection requests, the checking logic would be fairly 2830 * complex. 2831 * 2832 * For now, if the request is invalid, it just won't get 2833 * executed and might get deleted. 2834 */ 2835 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links); 2836 2837 /* 2838 * XXX KDM check to make sure the serial number is unique, 2839 * in case we somehow manage to wrap. That shouldn't 2840 * happen for a very long time, but it's the right thing to 2841 * do. 2842 */ 2843 new_err_desc->serial = lun->error_serial; 2844 err_desc->serial = lun->error_serial; 2845 lun->error_serial++; 2846 2847 mtx_unlock(&lun->lun_lock); 2848 break; 2849 } 2850 case CTL_ERROR_INJECT_DELETE: { 2851 struct ctl_error_desc *delete_desc, *desc, *desc2; 2852 int delete_done; 2853 2854 delete_desc = (struct ctl_error_desc *)addr; 2855 delete_done = 0; 2856 2857 mtx_lock(&softc->ctl_lock); 2858 lun = softc->ctl_luns[delete_desc->lun_id]; 2859 if (lun == NULL) { 2860 mtx_unlock(&softc->ctl_lock); 2861 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n", 2862 __func__, (uintmax_t)delete_desc->lun_id); 2863 retval = EINVAL; 2864 break; 2865 } 2866 mtx_lock(&lun->lun_lock); 2867 mtx_unlock(&softc->ctl_lock); 2868 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 2869 if (desc->serial != delete_desc->serial) 2870 continue; 2871 2872 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, 2873 links); 2874 free(desc, M_CTL); 2875 delete_done = 1; 2876 } 2877 mtx_unlock(&lun->lun_lock); 2878 if (delete_done == 0) { 2879 printf("%s: CTL_ERROR_INJECT_DELETE: can't find " 2880 "error serial %ju on LUN %u\n", __func__, 2881 delete_desc->serial, delete_desc->lun_id); 2882 retval = EINVAL; 2883 break; 2884 } 2885 break; 2886 } 2887 case CTL_DUMP_STRUCTS: { 2888 int i, j, k; 2889 struct ctl_port *port; 2890 struct ctl_frontend *fe; 2891 2892 mtx_lock(&softc->ctl_lock); 2893 printf("CTL Persistent Reservation information start:\n"); 2894 for (i = 0; i < CTL_MAX_LUNS; i++) { 2895 lun = softc->ctl_luns[i]; 2896 2897 if ((lun == NULL) 2898 || ((lun->flags & CTL_LUN_DISABLED) != 0)) 2899 continue; 2900 2901 for (j = 0; j < CTL_MAX_PORTS; j++) { 2902 if (lun->pr_keys[j] == NULL) 2903 continue; 2904 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){ 2905 if (lun->pr_keys[j][k] == 0) 2906 continue; 2907 printf(" LUN %d port %d iid %d key " 2908 "%#jx\n", i, j, k, 2909 (uintmax_t)lun->pr_keys[j][k]); 2910 } 2911 } 2912 } 2913 printf("CTL Persistent Reservation information end\n"); 2914 printf("CTL Ports:\n"); 2915 STAILQ_FOREACH(port, &softc->port_list, links) { 2916 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN " 2917 "%#jx WWPN %#jx\n", port->targ_port, port->port_name, 2918 port->frontend->name, port->port_type, 2919 port->physical_port, port->virtual_port, 2920 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn); 2921 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 2922 if (port->wwpn_iid[j].in_use == 0 && 2923 port->wwpn_iid[j].wwpn == 0 && 2924 port->wwpn_iid[j].name == NULL) 2925 continue; 2926 2927 printf(" iid %u use %d WWPN %#jx '%s'\n", 2928 j, port->wwpn_iid[j].in_use, 2929 (uintmax_t)port->wwpn_iid[j].wwpn, 2930 port->wwpn_iid[j].name); 2931 } 2932 } 2933 printf("CTL Port information end\n"); 2934 mtx_unlock(&softc->ctl_lock); 2935 /* 2936 * XXX KDM calling this without a lock. We'd likely want 2937 * to drop the lock before calling the frontend's dump 2938 * routine anyway. 2939 */ 2940 printf("CTL Frontends:\n"); 2941 STAILQ_FOREACH(fe, &softc->fe_list, links) { 2942 printf(" Frontend '%s'\n", fe->name); 2943 if (fe->fe_dump != NULL) 2944 fe->fe_dump(); 2945 } 2946 printf("CTL Frontend information end\n"); 2947 break; 2948 } 2949 case CTL_LUN_REQ: { 2950 struct ctl_lun_req *lun_req; 2951 struct ctl_backend_driver *backend; 2952 2953 lun_req = (struct ctl_lun_req *)addr; 2954 2955 backend = ctl_backend_find(lun_req->backend); 2956 if (backend == NULL) { 2957 lun_req->status = CTL_LUN_ERROR; 2958 snprintf(lun_req->error_str, 2959 sizeof(lun_req->error_str), 2960 "Backend \"%s\" not found.", 2961 lun_req->backend); 2962 break; 2963 } 2964 if (lun_req->num_be_args > 0) { 2965 lun_req->kern_be_args = ctl_copyin_args( 2966 lun_req->num_be_args, 2967 lun_req->be_args, 2968 lun_req->error_str, 2969 sizeof(lun_req->error_str)); 2970 if (lun_req->kern_be_args == NULL) { 2971 lun_req->status = CTL_LUN_ERROR; 2972 break; 2973 } 2974 } 2975 2976 retval = backend->ioctl(dev, cmd, addr, flag, td); 2977 2978 if (lun_req->num_be_args > 0) { 2979 ctl_copyout_args(lun_req->num_be_args, 2980 lun_req->kern_be_args); 2981 ctl_free_args(lun_req->num_be_args, 2982 lun_req->kern_be_args); 2983 } 2984 break; 2985 } 2986 case CTL_LUN_LIST: { 2987 struct sbuf *sb; 2988 struct ctl_lun_list *list; 2989 struct ctl_option *opt; 2990 2991 list = (struct ctl_lun_list *)addr; 2992 2993 /* 2994 * Allocate a fixed length sbuf here, based on the length 2995 * of the user's buffer. We could allocate an auto-extending 2996 * buffer, and then tell the user how much larger our 2997 * amount of data is than his buffer, but that presents 2998 * some problems: 2999 * 3000 * 1. The sbuf(9) routines use a blocking malloc, and so 3001 * we can't hold a lock while calling them with an 3002 * auto-extending buffer. 3003 * 3004 * 2. There is not currently a LUN reference counting 3005 * mechanism, outside of outstanding transactions on 3006 * the LUN's OOA queue. So a LUN could go away on us 3007 * while we're getting the LUN number, backend-specific 3008 * information, etc. Thus, given the way things 3009 * currently work, we need to hold the CTL lock while 3010 * grabbing LUN information. 3011 * 3012 * So, from the user's standpoint, the best thing to do is 3013 * allocate what he thinks is a reasonable buffer length, 3014 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error, 3015 * double the buffer length and try again. (And repeat 3016 * that until he succeeds.) 3017 */ 3018 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3019 if (sb == NULL) { 3020 list->status = CTL_LUN_LIST_ERROR; 3021 snprintf(list->error_str, sizeof(list->error_str), 3022 "Unable to allocate %d bytes for LUN list", 3023 list->alloc_len); 3024 break; 3025 } 3026 3027 sbuf_printf(sb, "<ctllunlist>\n"); 3028 3029 mtx_lock(&softc->ctl_lock); 3030 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3031 mtx_lock(&lun->lun_lock); 3032 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n", 3033 (uintmax_t)lun->lun); 3034 3035 /* 3036 * Bail out as soon as we see that we've overfilled 3037 * the buffer. 3038 */ 3039 if (retval != 0) 3040 break; 3041 3042 retval = sbuf_printf(sb, "\t<backend_type>%s" 3043 "</backend_type>\n", 3044 (lun->backend == NULL) ? "none" : 3045 lun->backend->name); 3046 3047 if (retval != 0) 3048 break; 3049 3050 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n", 3051 lun->be_lun->lun_type); 3052 3053 if (retval != 0) 3054 break; 3055 3056 if (lun->backend == NULL) { 3057 retval = sbuf_printf(sb, "</lun>\n"); 3058 if (retval != 0) 3059 break; 3060 continue; 3061 } 3062 3063 retval = sbuf_printf(sb, "\t<size>%ju</size>\n", 3064 (lun->be_lun->maxlba > 0) ? 3065 lun->be_lun->maxlba + 1 : 0); 3066 3067 if (retval != 0) 3068 break; 3069 3070 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n", 3071 lun->be_lun->blocksize); 3072 3073 if (retval != 0) 3074 break; 3075 3076 retval = sbuf_printf(sb, "\t<serial_number>"); 3077 3078 if (retval != 0) 3079 break; 3080 3081 retval = ctl_sbuf_printf_esc(sb, 3082 lun->be_lun->serial_num, 3083 sizeof(lun->be_lun->serial_num)); 3084 3085 if (retval != 0) 3086 break; 3087 3088 retval = sbuf_printf(sb, "</serial_number>\n"); 3089 3090 if (retval != 0) 3091 break; 3092 3093 retval = sbuf_printf(sb, "\t<device_id>"); 3094 3095 if (retval != 0) 3096 break; 3097 3098 retval = ctl_sbuf_printf_esc(sb, 3099 lun->be_lun->device_id, 3100 sizeof(lun->be_lun->device_id)); 3101 3102 if (retval != 0) 3103 break; 3104 3105 retval = sbuf_printf(sb, "</device_id>\n"); 3106 3107 if (retval != 0) 3108 break; 3109 3110 if (lun->backend->lun_info != NULL) { 3111 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb); 3112 if (retval != 0) 3113 break; 3114 } 3115 STAILQ_FOREACH(opt, &lun->be_lun->options, links) { 3116 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3117 opt->name, opt->value, opt->name); 3118 if (retval != 0) 3119 break; 3120 } 3121 3122 retval = sbuf_printf(sb, "</lun>\n"); 3123 3124 if (retval != 0) 3125 break; 3126 mtx_unlock(&lun->lun_lock); 3127 } 3128 if (lun != NULL) 3129 mtx_unlock(&lun->lun_lock); 3130 mtx_unlock(&softc->ctl_lock); 3131 3132 if ((retval != 0) 3133 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) { 3134 retval = 0; 3135 sbuf_delete(sb); 3136 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3137 snprintf(list->error_str, sizeof(list->error_str), 3138 "Out of space, %d bytes is too small", 3139 list->alloc_len); 3140 break; 3141 } 3142 3143 sbuf_finish(sb); 3144 3145 retval = copyout(sbuf_data(sb), list->lun_xml, 3146 sbuf_len(sb) + 1); 3147 3148 list->fill_len = sbuf_len(sb) + 1; 3149 list->status = CTL_LUN_LIST_OK; 3150 sbuf_delete(sb); 3151 break; 3152 } 3153 case CTL_ISCSI: { 3154 struct ctl_iscsi *ci; 3155 struct ctl_frontend *fe; 3156 3157 ci = (struct ctl_iscsi *)addr; 3158 3159 fe = ctl_frontend_find("iscsi"); 3160 if (fe == NULL) { 3161 ci->status = CTL_ISCSI_ERROR; 3162 snprintf(ci->error_str, sizeof(ci->error_str), 3163 "Frontend \"iscsi\" not found."); 3164 break; 3165 } 3166 3167 retval = fe->ioctl(dev, cmd, addr, flag, td); 3168 break; 3169 } 3170 case CTL_PORT_REQ: { 3171 struct ctl_req *req; 3172 struct ctl_frontend *fe; 3173 3174 req = (struct ctl_req *)addr; 3175 3176 fe = ctl_frontend_find(req->driver); 3177 if (fe == NULL) { 3178 req->status = CTL_LUN_ERROR; 3179 snprintf(req->error_str, sizeof(req->error_str), 3180 "Frontend \"%s\" not found.", req->driver); 3181 break; 3182 } 3183 if (req->num_args > 0) { 3184 req->kern_args = ctl_copyin_args(req->num_args, 3185 req->args, req->error_str, sizeof(req->error_str)); 3186 if (req->kern_args == NULL) { 3187 req->status = CTL_LUN_ERROR; 3188 break; 3189 } 3190 } 3191 3192 if (fe->ioctl) 3193 retval = fe->ioctl(dev, cmd, addr, flag, td); 3194 else 3195 retval = ENODEV; 3196 3197 if (req->num_args > 0) { 3198 ctl_copyout_args(req->num_args, req->kern_args); 3199 ctl_free_args(req->num_args, req->kern_args); 3200 } 3201 break; 3202 } 3203 case CTL_PORT_LIST: { 3204 struct sbuf *sb; 3205 struct ctl_port *port; 3206 struct ctl_lun_list *list; 3207 struct ctl_option *opt; 3208 int j; 3209 uint32_t plun; 3210 3211 list = (struct ctl_lun_list *)addr; 3212 3213 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN); 3214 if (sb == NULL) { 3215 list->status = CTL_LUN_LIST_ERROR; 3216 snprintf(list->error_str, sizeof(list->error_str), 3217 "Unable to allocate %d bytes for LUN list", 3218 list->alloc_len); 3219 break; 3220 } 3221 3222 sbuf_printf(sb, "<ctlportlist>\n"); 3223 3224 mtx_lock(&softc->ctl_lock); 3225 STAILQ_FOREACH(port, &softc->port_list, links) { 3226 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n", 3227 (uintmax_t)port->targ_port); 3228 3229 /* 3230 * Bail out as soon as we see that we've overfilled 3231 * the buffer. 3232 */ 3233 if (retval != 0) 3234 break; 3235 3236 retval = sbuf_printf(sb, "\t<frontend_type>%s" 3237 "</frontend_type>\n", port->frontend->name); 3238 if (retval != 0) 3239 break; 3240 3241 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n", 3242 port->port_type); 3243 if (retval != 0) 3244 break; 3245 3246 retval = sbuf_printf(sb, "\t<online>%s</online>\n", 3247 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO"); 3248 if (retval != 0) 3249 break; 3250 3251 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n", 3252 port->port_name); 3253 if (retval != 0) 3254 break; 3255 3256 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n", 3257 port->physical_port); 3258 if (retval != 0) 3259 break; 3260 3261 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n", 3262 port->virtual_port); 3263 if (retval != 0) 3264 break; 3265 3266 if (port->target_devid != NULL) { 3267 sbuf_printf(sb, "\t<target>"); 3268 ctl_id_sbuf(port->target_devid, sb); 3269 sbuf_printf(sb, "</target>\n"); 3270 } 3271 3272 if (port->port_devid != NULL) { 3273 sbuf_printf(sb, "\t<port>"); 3274 ctl_id_sbuf(port->port_devid, sb); 3275 sbuf_printf(sb, "</port>\n"); 3276 } 3277 3278 if (port->port_info != NULL) { 3279 retval = port->port_info(port->onoff_arg, sb); 3280 if (retval != 0) 3281 break; 3282 } 3283 STAILQ_FOREACH(opt, &port->options, links) { 3284 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n", 3285 opt->name, opt->value, opt->name); 3286 if (retval != 0) 3287 break; 3288 } 3289 3290 if (port->lun_map != NULL) { 3291 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n"); 3292 for (j = 0; j < CTL_MAX_LUNS; j++) { 3293 plun = ctl_lun_map_from_port(port, j); 3294 if (plun >= CTL_MAX_LUNS) 3295 continue; 3296 sbuf_printf(sb, 3297 "\t<lun id=\"%u\">%u</lun>\n", 3298 j, plun); 3299 } 3300 } 3301 3302 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) { 3303 if (port->wwpn_iid[j].in_use == 0 || 3304 (port->wwpn_iid[j].wwpn == 0 && 3305 port->wwpn_iid[j].name == NULL)) 3306 continue; 3307 3308 if (port->wwpn_iid[j].name != NULL) 3309 retval = sbuf_printf(sb, 3310 "\t<initiator id=\"%u\">%s</initiator>\n", 3311 j, port->wwpn_iid[j].name); 3312 else 3313 retval = sbuf_printf(sb, 3314 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n", 3315 j, port->wwpn_iid[j].wwpn); 3316 if (retval != 0) 3317 break; 3318 } 3319 if (retval != 0) 3320 break; 3321 3322 retval = sbuf_printf(sb, "</targ_port>\n"); 3323 if (retval != 0) 3324 break; 3325 } 3326 mtx_unlock(&softc->ctl_lock); 3327 3328 if ((retval != 0) 3329 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) { 3330 retval = 0; 3331 sbuf_delete(sb); 3332 list->status = CTL_LUN_LIST_NEED_MORE_SPACE; 3333 snprintf(list->error_str, sizeof(list->error_str), 3334 "Out of space, %d bytes is too small", 3335 list->alloc_len); 3336 break; 3337 } 3338 3339 sbuf_finish(sb); 3340 3341 retval = copyout(sbuf_data(sb), list->lun_xml, 3342 sbuf_len(sb) + 1); 3343 3344 list->fill_len = sbuf_len(sb) + 1; 3345 list->status = CTL_LUN_LIST_OK; 3346 sbuf_delete(sb); 3347 break; 3348 } 3349 case CTL_LUN_MAP: { 3350 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr; 3351 struct ctl_port *port; 3352 3353 mtx_lock(&softc->ctl_lock); 3354 if (lm->port < softc->port_min || 3355 lm->port >= softc->port_max || 3356 (port = softc->ctl_ports[lm->port]) == NULL) { 3357 mtx_unlock(&softc->ctl_lock); 3358 return (ENXIO); 3359 } 3360 if (port->status & CTL_PORT_STATUS_ONLINE) { 3361 STAILQ_FOREACH(lun, &softc->lun_list, links) { 3362 if (ctl_lun_map_to_port(port, lun->lun) >= 3363 CTL_MAX_LUNS) 3364 continue; 3365 mtx_lock(&lun->lun_lock); 3366 ctl_est_ua_port(lun, lm->port, -1, 3367 CTL_UA_LUN_CHANGE); 3368 mtx_unlock(&lun->lun_lock); 3369 } 3370 } 3371 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps 3372 if (lm->plun < CTL_MAX_LUNS) { 3373 if (lm->lun == UINT32_MAX) 3374 retval = ctl_lun_map_unset(port, lm->plun); 3375 else if (lm->lun < CTL_MAX_LUNS && 3376 softc->ctl_luns[lm->lun] != NULL) 3377 retval = ctl_lun_map_set(port, lm->plun, lm->lun); 3378 else 3379 return (ENXIO); 3380 } else if (lm->plun == UINT32_MAX) { 3381 if (lm->lun == UINT32_MAX) 3382 retval = ctl_lun_map_deinit(port); 3383 else 3384 retval = ctl_lun_map_init(port); 3385 } else 3386 return (ENXIO); 3387 if (port->status & CTL_PORT_STATUS_ONLINE) 3388 ctl_isc_announce_port(port); 3389 break; 3390 } 3391 default: { 3392 /* XXX KDM should we fix this? */ 3393#if 0 3394 struct ctl_backend_driver *backend; 3395 unsigned int type; 3396 int found; 3397 3398 found = 0; 3399 3400 /* 3401 * We encode the backend type as the ioctl type for backend 3402 * ioctls. So parse it out here, and then search for a 3403 * backend of this type. 3404 */ 3405 type = _IOC_TYPE(cmd); 3406 3407 STAILQ_FOREACH(backend, &softc->be_list, links) { 3408 if (backend->type == type) { 3409 found = 1; 3410 break; 3411 } 3412 } 3413 if (found == 0) { 3414 printf("ctl: unknown ioctl command %#lx or backend " 3415 "%d\n", cmd, type); 3416 retval = EINVAL; 3417 break; 3418 } 3419 retval = backend->ioctl(dev, cmd, addr, flag, td); 3420#endif 3421 retval = ENOTTY; 3422 break; 3423 } 3424 } 3425 return (retval); 3426} 3427 3428uint32_t 3429ctl_get_initindex(struct ctl_nexus *nexus) 3430{ 3431 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT)); 3432} 3433 3434int 3435ctl_lun_map_init(struct ctl_port *port) 3436{ 3437 struct ctl_softc *softc = port->ctl_softc; 3438 struct ctl_lun *lun; 3439 uint32_t i; 3440 3441 if (port->lun_map == NULL) 3442 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS, 3443 M_CTL, M_NOWAIT); 3444 if (port->lun_map == NULL) 3445 return (ENOMEM); 3446 for (i = 0; i < CTL_MAX_LUNS; i++) 3447 port->lun_map[i] = UINT32_MAX; 3448 if (port->status & CTL_PORT_STATUS_ONLINE) { 3449 if (port->lun_disable != NULL) { 3450 STAILQ_FOREACH(lun, &softc->lun_list, links) 3451 port->lun_disable(port->targ_lun_arg, lun->lun); 3452 } 3453 ctl_isc_announce_port(port); 3454 } 3455 return (0); 3456} 3457 3458int 3459ctl_lun_map_deinit(struct ctl_port *port) 3460{ 3461 struct ctl_softc *softc = port->ctl_softc; 3462 struct ctl_lun *lun; 3463 3464 if (port->lun_map == NULL) 3465 return (0); 3466 free(port->lun_map, M_CTL); 3467 port->lun_map = NULL; 3468 if (port->status & CTL_PORT_STATUS_ONLINE) { 3469 if (port->lun_enable != NULL) { 3470 STAILQ_FOREACH(lun, &softc->lun_list, links) 3471 port->lun_enable(port->targ_lun_arg, lun->lun); 3472 } 3473 ctl_isc_announce_port(port); 3474 } 3475 return (0); 3476} 3477 3478int 3479ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun) 3480{ 3481 int status; 3482 uint32_t old; 3483 3484 if (port->lun_map == NULL) { 3485 status = ctl_lun_map_init(port); 3486 if (status != 0) 3487 return (status); 3488 } 3489 old = port->lun_map[plun]; 3490 port->lun_map[plun] = glun; 3491 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) { 3492 if (port->lun_enable != NULL) 3493 port->lun_enable(port->targ_lun_arg, plun); 3494 ctl_isc_announce_port(port); 3495 } 3496 return (0); 3497} 3498 3499int 3500ctl_lun_map_unset(struct ctl_port *port, uint32_t plun) 3501{ 3502 uint32_t old; 3503 3504 if (port->lun_map == NULL) 3505 return (0); 3506 old = port->lun_map[plun]; 3507 port->lun_map[plun] = UINT32_MAX; 3508 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) { 3509 if (port->lun_disable != NULL) 3510 port->lun_disable(port->targ_lun_arg, plun); 3511 ctl_isc_announce_port(port); 3512 } 3513 return (0); 3514} 3515 3516uint32_t 3517ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id) 3518{ 3519 3520 if (port == NULL) 3521 return (UINT32_MAX); 3522 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS) 3523 return (lun_id); 3524 return (port->lun_map[lun_id]); 3525} 3526 3527uint32_t 3528ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id) 3529{ 3530 uint32_t i; 3531 3532 if (port == NULL) 3533 return (UINT32_MAX); 3534 if (port->lun_map == NULL) 3535 return (lun_id); 3536 for (i = 0; i < CTL_MAX_LUNS; i++) { 3537 if (port->lun_map[i] == lun_id) 3538 return (i); 3539 } 3540 return (UINT32_MAX); 3541} 3542 3543static struct ctl_port * 3544ctl_io_port(struct ctl_io_hdr *io_hdr) 3545{ 3546 3547 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]); 3548} 3549 3550int 3551ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last) 3552{ 3553 int i; 3554 3555 for (i = first; i < last; i++) { 3556 if ((mask[i / 32] & (1 << (i % 32))) == 0) 3557 return (i); 3558 } 3559 return (-1); 3560} 3561 3562int 3563ctl_set_mask(uint32_t *mask, uint32_t bit) 3564{ 3565 uint32_t chunk, piece; 3566 3567 chunk = bit >> 5; 3568 piece = bit % (sizeof(uint32_t) * 8); 3569 3570 if ((mask[chunk] & (1 << piece)) != 0) 3571 return (-1); 3572 else 3573 mask[chunk] |= (1 << piece); 3574 3575 return (0); 3576} 3577 3578int 3579ctl_clear_mask(uint32_t *mask, uint32_t bit) 3580{ 3581 uint32_t chunk, piece; 3582 3583 chunk = bit >> 5; 3584 piece = bit % (sizeof(uint32_t) * 8); 3585 3586 if ((mask[chunk] & (1 << piece)) == 0) 3587 return (-1); 3588 else 3589 mask[chunk] &= ~(1 << piece); 3590 3591 return (0); 3592} 3593 3594int 3595ctl_is_set(uint32_t *mask, uint32_t bit) 3596{ 3597 uint32_t chunk, piece; 3598 3599 chunk = bit >> 5; 3600 piece = bit % (sizeof(uint32_t) * 8); 3601 3602 if ((mask[chunk] & (1 << piece)) == 0) 3603 return (0); 3604 else 3605 return (1); 3606} 3607 3608static uint64_t 3609ctl_get_prkey(struct ctl_lun *lun, uint32_t residx) 3610{ 3611 uint64_t *t; 3612 3613 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3614 if (t == NULL) 3615 return (0); 3616 return (t[residx % CTL_MAX_INIT_PER_PORT]); 3617} 3618 3619static void 3620ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx) 3621{ 3622 uint64_t *t; 3623 3624 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3625 if (t == NULL) 3626 return; 3627 t[residx % CTL_MAX_INIT_PER_PORT] = 0; 3628} 3629 3630static void 3631ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx) 3632{ 3633 uint64_t *p; 3634 u_int i; 3635 3636 i = residx/CTL_MAX_INIT_PER_PORT; 3637 if (lun->pr_keys[i] != NULL) 3638 return; 3639 mtx_unlock(&lun->lun_lock); 3640 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL, 3641 M_WAITOK | M_ZERO); 3642 mtx_lock(&lun->lun_lock); 3643 if (lun->pr_keys[i] == NULL) 3644 lun->pr_keys[i] = p; 3645 else 3646 free(p, M_CTL); 3647} 3648 3649static void 3650ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key) 3651{ 3652 uint64_t *t; 3653 3654 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT]; 3655 KASSERT(t != NULL, ("prkey %d is not allocated", residx)); 3656 t[residx % CTL_MAX_INIT_PER_PORT] = key; 3657} 3658 3659/* 3660 * ctl_softc, pool_name, total_ctl_io are passed in. 3661 * npool is passed out. 3662 */ 3663int 3664ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name, 3665 uint32_t total_ctl_io, void **npool) 3666{ 3667#ifdef IO_POOLS 3668 struct ctl_io_pool *pool; 3669 3670 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL, 3671 M_NOWAIT | M_ZERO); 3672 if (pool == NULL) 3673 return (ENOMEM); 3674 3675 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name); 3676 pool->ctl_softc = ctl_softc; 3677 pool->zone = uma_zsecond_create(pool->name, NULL, 3678 NULL, NULL, NULL, ctl_softc->io_zone); 3679 /* uma_prealloc(pool->zone, total_ctl_io); */ 3680 3681 *npool = pool; 3682#else 3683 *npool = ctl_softc->io_zone; 3684#endif 3685 return (0); 3686} 3687 3688void 3689ctl_pool_free(struct ctl_io_pool *pool) 3690{ 3691 3692 if (pool == NULL) 3693 return; 3694 3695#ifdef IO_POOLS 3696 uma_zdestroy(pool->zone); 3697 free(pool, M_CTL); 3698#endif 3699} 3700 3701union ctl_io * 3702ctl_alloc_io(void *pool_ref) 3703{ 3704 union ctl_io *io; 3705#ifdef IO_POOLS 3706 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3707 3708 io = uma_zalloc(pool->zone, M_WAITOK); 3709#else 3710 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK); 3711#endif 3712 if (io != NULL) 3713 io->io_hdr.pool = pool_ref; 3714 return (io); 3715} 3716 3717union ctl_io * 3718ctl_alloc_io_nowait(void *pool_ref) 3719{ 3720 union ctl_io *io; 3721#ifdef IO_POOLS 3722 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref; 3723 3724 io = uma_zalloc(pool->zone, M_NOWAIT); 3725#else 3726 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT); 3727#endif 3728 if (io != NULL) 3729 io->io_hdr.pool = pool_ref; 3730 return (io); 3731} 3732 3733void 3734ctl_free_io(union ctl_io *io) 3735{ 3736#ifdef IO_POOLS 3737 struct ctl_io_pool *pool; 3738#endif 3739 3740 if (io == NULL) 3741 return; 3742 3743#ifdef IO_POOLS 3744 pool = (struct ctl_io_pool *)io->io_hdr.pool; 3745 uma_zfree(pool->zone, io); 3746#else 3747 uma_zfree((uma_zone_t)io->io_hdr.pool, io); 3748#endif 3749} 3750 3751void 3752ctl_zero_io(union ctl_io *io) 3753{ 3754 void *pool_ref; 3755 3756 if (io == NULL) 3757 return; 3758 3759 /* 3760 * May need to preserve linked list pointers at some point too. 3761 */ 3762 pool_ref = io->io_hdr.pool; 3763 memset(io, 0, sizeof(*io)); 3764 io->io_hdr.pool = pool_ref; 3765} 3766 3767int 3768ctl_expand_number(const char *buf, uint64_t *num) 3769{ 3770 char *endptr; 3771 uint64_t number; 3772 unsigned shift; 3773 3774 number = strtoq(buf, &endptr, 0); 3775 3776 switch (tolower((unsigned char)*endptr)) { 3777 case 'e': 3778 shift = 60; 3779 break; 3780 case 'p': 3781 shift = 50; 3782 break; 3783 case 't': 3784 shift = 40; 3785 break; 3786 case 'g': 3787 shift = 30; 3788 break; 3789 case 'm': 3790 shift = 20; 3791 break; 3792 case 'k': 3793 shift = 10; 3794 break; 3795 case 'b': 3796 case '\0': /* No unit. */ 3797 *num = number; 3798 return (0); 3799 default: 3800 /* Unrecognized unit. */ 3801 return (-1); 3802 } 3803 3804 if ((number << shift) >> shift != number) { 3805 /* Overflow */ 3806 return (-1); 3807 } 3808 *num = number << shift; 3809 return (0); 3810} 3811 3812 3813/* 3814 * This routine could be used in the future to load default and/or saved 3815 * mode page parameters for a particuar lun. 3816 */ 3817static int 3818ctl_init_page_index(struct ctl_lun *lun) 3819{ 3820 int i; 3821 struct ctl_page_index *page_index; 3822 const char *value; 3823 uint64_t ival; 3824 3825 memcpy(&lun->mode_pages.index, page_index_template, 3826 sizeof(page_index_template)); 3827 3828 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 3829 3830 page_index = &lun->mode_pages.index[i]; 3831 if (lun->be_lun->lun_type == T_DIRECT && 3832 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 3833 continue; 3834 if (lun->be_lun->lun_type == T_PROCESSOR && 3835 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 3836 continue; 3837 if (lun->be_lun->lun_type == T_CDROM && 3838 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 3839 continue; 3840 3841 switch (page_index->page_code & SMPH_PC_MASK) { 3842 case SMS_RW_ERROR_RECOVERY_PAGE: { 3843 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3844 panic("subpage is incorrect!"); 3845 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT], 3846 &rw_er_page_default, 3847 sizeof(rw_er_page_default)); 3848 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE], 3849 &rw_er_page_changeable, 3850 sizeof(rw_er_page_changeable)); 3851 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT], 3852 &rw_er_page_default, 3853 sizeof(rw_er_page_default)); 3854 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED], 3855 &rw_er_page_default, 3856 sizeof(rw_er_page_default)); 3857 page_index->page_data = 3858 (uint8_t *)lun->mode_pages.rw_er_page; 3859 break; 3860 } 3861 case SMS_FORMAT_DEVICE_PAGE: { 3862 struct scsi_format_page *format_page; 3863 3864 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3865 panic("subpage is incorrect!"); 3866 3867 /* 3868 * Sectors per track are set above. Bytes per 3869 * sector need to be set here on a per-LUN basis. 3870 */ 3871 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT], 3872 &format_page_default, 3873 sizeof(format_page_default)); 3874 memcpy(&lun->mode_pages.format_page[ 3875 CTL_PAGE_CHANGEABLE], &format_page_changeable, 3876 sizeof(format_page_changeable)); 3877 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT], 3878 &format_page_default, 3879 sizeof(format_page_default)); 3880 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED], 3881 &format_page_default, 3882 sizeof(format_page_default)); 3883 3884 format_page = &lun->mode_pages.format_page[ 3885 CTL_PAGE_CURRENT]; 3886 scsi_ulto2b(lun->be_lun->blocksize, 3887 format_page->bytes_per_sector); 3888 3889 format_page = &lun->mode_pages.format_page[ 3890 CTL_PAGE_DEFAULT]; 3891 scsi_ulto2b(lun->be_lun->blocksize, 3892 format_page->bytes_per_sector); 3893 3894 format_page = &lun->mode_pages.format_page[ 3895 CTL_PAGE_SAVED]; 3896 scsi_ulto2b(lun->be_lun->blocksize, 3897 format_page->bytes_per_sector); 3898 3899 page_index->page_data = 3900 (uint8_t *)lun->mode_pages.format_page; 3901 break; 3902 } 3903 case SMS_RIGID_DISK_PAGE: { 3904 struct scsi_rigid_disk_page *rigid_disk_page; 3905 uint32_t sectors_per_cylinder; 3906 uint64_t cylinders; 3907#ifndef __XSCALE__ 3908 int shift; 3909#endif /* !__XSCALE__ */ 3910 3911 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3912 panic("invalid subpage value %d", 3913 page_index->subpage); 3914 3915 /* 3916 * Rotation rate and sectors per track are set 3917 * above. We calculate the cylinders here based on 3918 * capacity. Due to the number of heads and 3919 * sectors per track we're using, smaller arrays 3920 * may turn out to have 0 cylinders. Linux and 3921 * FreeBSD don't pay attention to these mode pages 3922 * to figure out capacity, but Solaris does. It 3923 * seems to deal with 0 cylinders just fine, and 3924 * works out a fake geometry based on the capacity. 3925 */ 3926 memcpy(&lun->mode_pages.rigid_disk_page[ 3927 CTL_PAGE_DEFAULT], &rigid_disk_page_default, 3928 sizeof(rigid_disk_page_default)); 3929 memcpy(&lun->mode_pages.rigid_disk_page[ 3930 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable, 3931 sizeof(rigid_disk_page_changeable)); 3932 3933 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK * 3934 CTL_DEFAULT_HEADS; 3935 3936 /* 3937 * The divide method here will be more accurate, 3938 * probably, but results in floating point being 3939 * used in the kernel on i386 (__udivdi3()). On the 3940 * XScale, though, __udivdi3() is implemented in 3941 * software. 3942 * 3943 * The shift method for cylinder calculation is 3944 * accurate if sectors_per_cylinder is a power of 3945 * 2. Otherwise it might be slightly off -- you 3946 * might have a bit of a truncation problem. 3947 */ 3948#ifdef __XSCALE__ 3949 cylinders = (lun->be_lun->maxlba + 1) / 3950 sectors_per_cylinder; 3951#else 3952 for (shift = 31; shift > 0; shift--) { 3953 if (sectors_per_cylinder & (1 << shift)) 3954 break; 3955 } 3956 cylinders = (lun->be_lun->maxlba + 1) >> shift; 3957#endif 3958 3959 /* 3960 * We've basically got 3 bytes, or 24 bits for the 3961 * cylinder size in the mode page. If we're over, 3962 * just round down to 2^24. 3963 */ 3964 if (cylinders > 0xffffff) 3965 cylinders = 0xffffff; 3966 3967 rigid_disk_page = &lun->mode_pages.rigid_disk_page[ 3968 CTL_PAGE_DEFAULT]; 3969 scsi_ulto3b(cylinders, rigid_disk_page->cylinders); 3970 3971 if ((value = ctl_get_opt(&lun->be_lun->options, 3972 "rpm")) != NULL) { 3973 scsi_ulto2b(strtol(value, NULL, 0), 3974 rigid_disk_page->rotation_rate); 3975 } 3976 3977 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT], 3978 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3979 sizeof(rigid_disk_page_default)); 3980 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED], 3981 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT], 3982 sizeof(rigid_disk_page_default)); 3983 3984 page_index->page_data = 3985 (uint8_t *)lun->mode_pages.rigid_disk_page; 3986 break; 3987 } 3988 case SMS_CACHING_PAGE: { 3989 struct scsi_caching_page *caching_page; 3990 3991 if (page_index->subpage != SMS_SUBPAGE_PAGE_0) 3992 panic("invalid subpage value %d", 3993 page_index->subpage); 3994 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT], 3995 &caching_page_default, 3996 sizeof(caching_page_default)); 3997 memcpy(&lun->mode_pages.caching_page[ 3998 CTL_PAGE_CHANGEABLE], &caching_page_changeable, 3999 sizeof(caching_page_changeable)); 4000 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4001 &caching_page_default, 4002 sizeof(caching_page_default)); 4003 caching_page = &lun->mode_pages.caching_page[ 4004 CTL_PAGE_SAVED]; 4005 value = ctl_get_opt(&lun->be_lun->options, "writecache"); 4006 if (value != NULL && strcmp(value, "off") == 0) 4007 caching_page->flags1 &= ~SCP_WCE; 4008 value = ctl_get_opt(&lun->be_lun->options, "readcache"); 4009 if (value != NULL && strcmp(value, "off") == 0) 4010 caching_page->flags1 |= SCP_RCD; 4011 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT], 4012 &lun->mode_pages.caching_page[CTL_PAGE_SAVED], 4013 sizeof(caching_page_default)); 4014 page_index->page_data = 4015 (uint8_t *)lun->mode_pages.caching_page; 4016 break; 4017 } 4018 case SMS_CONTROL_MODE_PAGE: { 4019 switch (page_index->subpage) { 4020 case SMS_SUBPAGE_PAGE_0: { 4021 struct scsi_control_page *control_page; 4022 4023 memcpy(&lun->mode_pages.control_page[ 4024 CTL_PAGE_DEFAULT], 4025 &control_page_default, 4026 sizeof(control_page_default)); 4027 memcpy(&lun->mode_pages.control_page[ 4028 CTL_PAGE_CHANGEABLE], 4029 &control_page_changeable, 4030 sizeof(control_page_changeable)); 4031 memcpy(&lun->mode_pages.control_page[ 4032 CTL_PAGE_SAVED], 4033 &control_page_default, 4034 sizeof(control_page_default)); 4035 control_page = &lun->mode_pages.control_page[ 4036 CTL_PAGE_SAVED]; 4037 value = ctl_get_opt(&lun->be_lun->options, 4038 "reordering"); 4039 if (value != NULL && 4040 strcmp(value, "unrestricted") == 0) { 4041 control_page->queue_flags &= 4042 ~SCP_QUEUE_ALG_MASK; 4043 control_page->queue_flags |= 4044 SCP_QUEUE_ALG_UNRESTRICTED; 4045 } 4046 memcpy(&lun->mode_pages.control_page[ 4047 CTL_PAGE_CURRENT], 4048 &lun->mode_pages.control_page[ 4049 CTL_PAGE_SAVED], 4050 sizeof(control_page_default)); 4051 page_index->page_data = 4052 (uint8_t *)lun->mode_pages.control_page; 4053 break; 4054 } 4055 case 0x01: 4056 memcpy(&lun->mode_pages.control_ext_page[ 4057 CTL_PAGE_DEFAULT], 4058 &control_ext_page_default, 4059 sizeof(control_ext_page_default)); 4060 memcpy(&lun->mode_pages.control_ext_page[ 4061 CTL_PAGE_CHANGEABLE], 4062 &control_ext_page_changeable, 4063 sizeof(control_ext_page_changeable)); 4064 memcpy(&lun->mode_pages.control_ext_page[ 4065 CTL_PAGE_SAVED], 4066 &control_ext_page_default, 4067 sizeof(control_ext_page_default)); 4068 memcpy(&lun->mode_pages.control_ext_page[ 4069 CTL_PAGE_CURRENT], 4070 &lun->mode_pages.control_ext_page[ 4071 CTL_PAGE_SAVED], 4072 sizeof(control_ext_page_default)); 4073 page_index->page_data = 4074 (uint8_t *)lun->mode_pages.control_ext_page; 4075 break; 4076 } 4077 break; 4078 } 4079 case SMS_INFO_EXCEPTIONS_PAGE: { 4080 switch (page_index->subpage) { 4081 case SMS_SUBPAGE_PAGE_0: 4082 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT], 4083 &ie_page_default, 4084 sizeof(ie_page_default)); 4085 memcpy(&lun->mode_pages.ie_page[ 4086 CTL_PAGE_CHANGEABLE], &ie_page_changeable, 4087 sizeof(ie_page_changeable)); 4088 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT], 4089 &ie_page_default, 4090 sizeof(ie_page_default)); 4091 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED], 4092 &ie_page_default, 4093 sizeof(ie_page_default)); 4094 page_index->page_data = 4095 (uint8_t *)lun->mode_pages.ie_page; 4096 break; 4097 case 0x02: { 4098 struct ctl_logical_block_provisioning_page *page; 4099 4100 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT], 4101 &lbp_page_default, 4102 sizeof(lbp_page_default)); 4103 memcpy(&lun->mode_pages.lbp_page[ 4104 CTL_PAGE_CHANGEABLE], &lbp_page_changeable, 4105 sizeof(lbp_page_changeable)); 4106 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4107 &lbp_page_default, 4108 sizeof(lbp_page_default)); 4109 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED]; 4110 value = ctl_get_opt(&lun->be_lun->options, 4111 "avail-threshold"); 4112 if (value != NULL && 4113 ctl_expand_number(value, &ival) == 0) { 4114 page->descr[0].flags |= SLBPPD_ENABLED | 4115 SLBPPD_ARMING_DEC; 4116 if (lun->be_lun->blocksize) 4117 ival /= lun->be_lun->blocksize; 4118 else 4119 ival /= 512; 4120 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4121 page->descr[0].count); 4122 } 4123 value = ctl_get_opt(&lun->be_lun->options, 4124 "used-threshold"); 4125 if (value != NULL && 4126 ctl_expand_number(value, &ival) == 0) { 4127 page->descr[1].flags |= SLBPPD_ENABLED | 4128 SLBPPD_ARMING_INC; 4129 if (lun->be_lun->blocksize) 4130 ival /= lun->be_lun->blocksize; 4131 else 4132 ival /= 512; 4133 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4134 page->descr[1].count); 4135 } 4136 value = ctl_get_opt(&lun->be_lun->options, 4137 "pool-avail-threshold"); 4138 if (value != NULL && 4139 ctl_expand_number(value, &ival) == 0) { 4140 page->descr[2].flags |= SLBPPD_ENABLED | 4141 SLBPPD_ARMING_DEC; 4142 if (lun->be_lun->blocksize) 4143 ival /= lun->be_lun->blocksize; 4144 else 4145 ival /= 512; 4146 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4147 page->descr[2].count); 4148 } 4149 value = ctl_get_opt(&lun->be_lun->options, 4150 "pool-used-threshold"); 4151 if (value != NULL && 4152 ctl_expand_number(value, &ival) == 0) { 4153 page->descr[3].flags |= SLBPPD_ENABLED | 4154 SLBPPD_ARMING_INC; 4155 if (lun->be_lun->blocksize) 4156 ival /= lun->be_lun->blocksize; 4157 else 4158 ival /= 512; 4159 scsi_ulto4b(ival >> CTL_LBP_EXPONENT, 4160 page->descr[3].count); 4161 } 4162 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT], 4163 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED], 4164 sizeof(lbp_page_default)); 4165 page_index->page_data = 4166 (uint8_t *)lun->mode_pages.lbp_page; 4167 }} 4168 break; 4169 } 4170 case SMS_CDDVD_CAPS_PAGE:{ 4171 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT], 4172 &cddvd_page_default, 4173 sizeof(cddvd_page_default)); 4174 memcpy(&lun->mode_pages.cddvd_page[ 4175 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable, 4176 sizeof(cddvd_page_changeable)); 4177 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4178 &cddvd_page_default, 4179 sizeof(cddvd_page_default)); 4180 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT], 4181 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED], 4182 sizeof(cddvd_page_default)); 4183 page_index->page_data = 4184 (uint8_t *)lun->mode_pages.cddvd_page; 4185 break; 4186 } 4187 case SMS_VENDOR_SPECIFIC_PAGE:{ 4188 switch (page_index->subpage) { 4189 case DBGCNF_SUBPAGE_CODE: { 4190 memcpy(&lun->mode_pages.debugconf_subpage[ 4191 CTL_PAGE_CURRENT], 4192 &debugconf_page_default, 4193 sizeof(debugconf_page_default)); 4194 memcpy(&lun->mode_pages.debugconf_subpage[ 4195 CTL_PAGE_CHANGEABLE], 4196 &debugconf_page_changeable, 4197 sizeof(debugconf_page_changeable)); 4198 memcpy(&lun->mode_pages.debugconf_subpage[ 4199 CTL_PAGE_DEFAULT], 4200 &debugconf_page_default, 4201 sizeof(debugconf_page_default)); 4202 memcpy(&lun->mode_pages.debugconf_subpage[ 4203 CTL_PAGE_SAVED], 4204 &debugconf_page_default, 4205 sizeof(debugconf_page_default)); 4206 page_index->page_data = 4207 (uint8_t *)lun->mode_pages.debugconf_subpage; 4208 break; 4209 } 4210 default: 4211 panic("invalid subpage value %d", 4212 page_index->subpage); 4213 break; 4214 } 4215 break; 4216 } 4217 default: 4218 panic("invalid page value %d", 4219 page_index->page_code & SMPH_PC_MASK); 4220 break; 4221 } 4222 } 4223 4224 return (CTL_RETVAL_COMPLETE); 4225} 4226 4227static int 4228ctl_init_log_page_index(struct ctl_lun *lun) 4229{ 4230 struct ctl_page_index *page_index; 4231 int i, j, k, prev; 4232 4233 memcpy(&lun->log_pages.index, log_page_index_template, 4234 sizeof(log_page_index_template)); 4235 4236 prev = -1; 4237 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) { 4238 4239 page_index = &lun->log_pages.index[i]; 4240 if (lun->be_lun->lun_type == T_DIRECT && 4241 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 4242 continue; 4243 if (lun->be_lun->lun_type == T_PROCESSOR && 4244 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 4245 continue; 4246 if (lun->be_lun->lun_type == T_CDROM && 4247 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 4248 continue; 4249 4250 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING && 4251 lun->backend->lun_attr == NULL) 4252 continue; 4253 4254 if (page_index->page_code != prev) { 4255 lun->log_pages.pages_page[j] = page_index->page_code; 4256 prev = page_index->page_code; 4257 j++; 4258 } 4259 lun->log_pages.subpages_page[k*2] = page_index->page_code; 4260 lun->log_pages.subpages_page[k*2+1] = page_index->subpage; 4261 k++; 4262 } 4263 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0]; 4264 lun->log_pages.index[0].page_len = j; 4265 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0]; 4266 lun->log_pages.index[1].page_len = k * 2; 4267 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0]; 4268 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS; 4269 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page; 4270 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page); 4271 4272 return (CTL_RETVAL_COMPLETE); 4273} 4274 4275static int 4276hex2bin(const char *str, uint8_t *buf, int buf_size) 4277{ 4278 int i; 4279 u_char c; 4280 4281 memset(buf, 0, buf_size); 4282 while (isspace(str[0])) 4283 str++; 4284 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 4285 str += 2; 4286 buf_size *= 2; 4287 for (i = 0; str[i] != 0 && i < buf_size; i++) { 4288 c = str[i]; 4289 if (isdigit(c)) 4290 c -= '0'; 4291 else if (isalpha(c)) 4292 c -= isupper(c) ? 'A' - 10 : 'a' - 10; 4293 else 4294 break; 4295 if (c >= 16) 4296 break; 4297 if ((i & 1) == 0) 4298 buf[i / 2] |= (c << 4); 4299 else 4300 buf[i / 2] |= c; 4301 } 4302 return ((i + 1) / 2); 4303} 4304 4305/* 4306 * LUN allocation. 4307 * 4308 * Requirements: 4309 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he 4310 * wants us to allocate the LUN and he can block. 4311 * - ctl_softc is always set 4312 * - be_lun is set if the LUN has a backend (needed for disk LUNs) 4313 * 4314 * Returns 0 for success, non-zero (errno) for failure. 4315 */ 4316static int 4317ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun, 4318 struct ctl_be_lun *const be_lun) 4319{ 4320 struct ctl_lun *nlun, *lun; 4321 struct scsi_vpd_id_descriptor *desc; 4322 struct scsi_vpd_id_t10 *t10id; 4323 const char *eui, *naa, *scsiname, *vendor, *value; 4324 int lun_number, i, lun_malloced; 4325 int devidlen, idlen1, idlen2 = 0, len; 4326 4327 if (be_lun == NULL) 4328 return (EINVAL); 4329 4330 /* 4331 * We currently only support Direct Access or Processor LUN types. 4332 */ 4333 switch (be_lun->lun_type) { 4334 case T_DIRECT: 4335 case T_PROCESSOR: 4336 case T_CDROM: 4337 break; 4338 case T_SEQUENTIAL: 4339 case T_CHANGER: 4340 default: 4341 be_lun->lun_config_status(be_lun->be_lun, 4342 CTL_LUN_CONFIG_FAILURE); 4343 break; 4344 } 4345 if (ctl_lun == NULL) { 4346 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK); 4347 lun_malloced = 1; 4348 } else { 4349 lun_malloced = 0; 4350 lun = ctl_lun; 4351 } 4352 4353 memset(lun, 0, sizeof(*lun)); 4354 if (lun_malloced) 4355 lun->flags = CTL_LUN_MALLOCED; 4356 4357 /* Generate LUN ID. */ 4358 devidlen = max(CTL_DEVID_MIN_LEN, 4359 strnlen(be_lun->device_id, CTL_DEVID_LEN)); 4360 idlen1 = sizeof(*t10id) + devidlen; 4361 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1; 4362 scsiname = ctl_get_opt(&be_lun->options, "scsiname"); 4363 if (scsiname != NULL) { 4364 idlen2 = roundup2(strlen(scsiname) + 1, 4); 4365 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2; 4366 } 4367 eui = ctl_get_opt(&be_lun->options, "eui"); 4368 if (eui != NULL) { 4369 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4370 } 4371 naa = ctl_get_opt(&be_lun->options, "naa"); 4372 if (naa != NULL) { 4373 len += sizeof(struct scsi_vpd_id_descriptor) + 16; 4374 } 4375 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len, 4376 M_CTL, M_WAITOK | M_ZERO); 4377 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data; 4378 desc->proto_codeset = SVPD_ID_CODESET_ASCII; 4379 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10; 4380 desc->length = idlen1; 4381 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0]; 4382 memset(t10id->vendor, ' ', sizeof(t10id->vendor)); 4383 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) { 4384 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor)); 4385 } else { 4386 strncpy(t10id->vendor, vendor, 4387 min(sizeof(t10id->vendor), strlen(vendor))); 4388 } 4389 strncpy((char *)t10id->vendor_spec_id, 4390 (char *)be_lun->device_id, devidlen); 4391 if (scsiname != NULL) { 4392 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4393 desc->length); 4394 desc->proto_codeset = SVPD_ID_CODESET_UTF8; 4395 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4396 SVPD_ID_TYPE_SCSI_NAME; 4397 desc->length = idlen2; 4398 strlcpy(desc->identifier, scsiname, idlen2); 4399 } 4400 if (eui != NULL) { 4401 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4402 desc->length); 4403 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4404 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4405 SVPD_ID_TYPE_EUI64; 4406 desc->length = hex2bin(eui, desc->identifier, 16); 4407 desc->length = desc->length > 12 ? 16 : 4408 (desc->length > 8 ? 12 : 8); 4409 len -= 16 - desc->length; 4410 } 4411 if (naa != NULL) { 4412 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 4413 desc->length); 4414 desc->proto_codeset = SVPD_ID_CODESET_BINARY; 4415 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | 4416 SVPD_ID_TYPE_NAA; 4417 desc->length = hex2bin(naa, desc->identifier, 16); 4418 desc->length = desc->length > 8 ? 16 : 8; 4419 len -= 16 - desc->length; 4420 } 4421 lun->lun_devid->len = len; 4422 4423 mtx_lock(&ctl_softc->ctl_lock); 4424 /* 4425 * See if the caller requested a particular LUN number. If so, see 4426 * if it is available. Otherwise, allocate the first available LUN. 4427 */ 4428 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) { 4429 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) 4430 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) { 4431 mtx_unlock(&ctl_softc->ctl_lock); 4432 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) { 4433 printf("ctl: requested LUN ID %d is higher " 4434 "than CTL_MAX_LUNS - 1 (%d)\n", 4435 be_lun->req_lun_id, CTL_MAX_LUNS - 1); 4436 } else { 4437 /* 4438 * XXX KDM return an error, or just assign 4439 * another LUN ID in this case?? 4440 */ 4441 printf("ctl: requested LUN ID %d is already " 4442 "in use\n", be_lun->req_lun_id); 4443 } 4444 if (lun->flags & CTL_LUN_MALLOCED) 4445 free(lun, M_CTL); 4446 be_lun->lun_config_status(be_lun->be_lun, 4447 CTL_LUN_CONFIG_FAILURE); 4448 return (ENOSPC); 4449 } 4450 lun_number = be_lun->req_lun_id; 4451 } else { 4452 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS); 4453 if (lun_number == -1) { 4454 mtx_unlock(&ctl_softc->ctl_lock); 4455 printf("ctl: can't allocate LUN, out of LUNs\n"); 4456 if (lun->flags & CTL_LUN_MALLOCED) 4457 free(lun, M_CTL); 4458 be_lun->lun_config_status(be_lun->be_lun, 4459 CTL_LUN_CONFIG_FAILURE); 4460 return (ENOSPC); 4461 } 4462 } 4463 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number); 4464 4465 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF); 4466 lun->lun = lun_number; 4467 lun->be_lun = be_lun; 4468 /* 4469 * The processor LUN is always enabled. Disk LUNs come on line 4470 * disabled, and must be enabled by the backend. 4471 */ 4472 lun->flags |= CTL_LUN_DISABLED; 4473 lun->backend = be_lun->be; 4474 be_lun->ctl_lun = lun; 4475 be_lun->lun_id = lun_number; 4476 atomic_add_int(&be_lun->be->num_luns, 1); 4477 if (be_lun->flags & CTL_LUN_FLAG_EJECTED) 4478 lun->flags |= CTL_LUN_EJECTED; 4479 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA) 4480 lun->flags |= CTL_LUN_NO_MEDIA; 4481 if (be_lun->flags & CTL_LUN_FLAG_STOPPED) 4482 lun->flags |= CTL_LUN_STOPPED; 4483 4484 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY) 4485 lun->flags |= CTL_LUN_PRIMARY_SC; 4486 4487 value = ctl_get_opt(&be_lun->options, "removable"); 4488 if (value != NULL) { 4489 if (strcmp(value, "on") == 0) 4490 lun->flags |= CTL_LUN_REMOVABLE; 4491 } else if (be_lun->lun_type == T_CDROM) 4492 lun->flags |= CTL_LUN_REMOVABLE; 4493 4494 lun->ctl_softc = ctl_softc; 4495#ifdef CTL_TIME_IO 4496 lun->last_busy = getsbinuptime(); 4497#endif 4498 TAILQ_INIT(&lun->ooa_queue); 4499 TAILQ_INIT(&lun->blocked_queue); 4500 STAILQ_INIT(&lun->error_list); 4501 ctl_tpc_lun_init(lun); 4502 4503 /* 4504 * Initialize the mode and log page index. 4505 */ 4506 ctl_init_page_index(lun); 4507 ctl_init_log_page_index(lun); 4508 4509 /* 4510 * Now, before we insert this lun on the lun list, set the lun 4511 * inventory changed UA for all other luns. 4512 */ 4513 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) { 4514 mtx_lock(&nlun->lun_lock); 4515 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4516 mtx_unlock(&nlun->lun_lock); 4517 } 4518 4519 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links); 4520 4521 ctl_softc->ctl_luns[lun_number] = lun; 4522 4523 ctl_softc->num_luns++; 4524 4525 /* Setup statistics gathering */ 4526 lun->stats.device_type = be_lun->lun_type; 4527 lun->stats.lun_number = lun_number; 4528 lun->stats.blocksize = be_lun->blocksize; 4529 if (be_lun->blocksize == 0) 4530 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE; 4531 for (i = 0;i < CTL_MAX_PORTS;i++) 4532 lun->stats.ports[i].targ_port = i; 4533 4534 mtx_unlock(&ctl_softc->ctl_lock); 4535 4536 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK); 4537 return (0); 4538} 4539 4540/* 4541 * Delete a LUN. 4542 * Assumptions: 4543 * - LUN has already been marked invalid and any pending I/O has been taken 4544 * care of. 4545 */ 4546static int 4547ctl_free_lun(struct ctl_lun *lun) 4548{ 4549 struct ctl_softc *softc; 4550 struct ctl_lun *nlun; 4551 int i; 4552 4553 softc = lun->ctl_softc; 4554 4555 mtx_assert(&softc->ctl_lock, MA_OWNED); 4556 4557 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links); 4558 4559 ctl_clear_mask(softc->ctl_lun_mask, lun->lun); 4560 4561 softc->ctl_luns[lun->lun] = NULL; 4562 4563 if (!TAILQ_EMPTY(&lun->ooa_queue)) 4564 panic("Freeing a LUN %p with outstanding I/O!!\n", lun); 4565 4566 softc->num_luns--; 4567 4568 /* 4569 * Tell the backend to free resources, if this LUN has a backend. 4570 */ 4571 atomic_subtract_int(&lun->be_lun->be->num_luns, 1); 4572 lun->be_lun->lun_shutdown(lun->be_lun->be_lun); 4573 4574 ctl_tpc_lun_shutdown(lun); 4575 mtx_destroy(&lun->lun_lock); 4576 free(lun->lun_devid, M_CTL); 4577 for (i = 0; i < CTL_MAX_PORTS; i++) 4578 free(lun->pending_ua[i], M_CTL); 4579 for (i = 0; i < CTL_MAX_PORTS; i++) 4580 free(lun->pr_keys[i], M_CTL); 4581 free(lun->write_buffer, M_CTL); 4582 if (lun->flags & CTL_LUN_MALLOCED) 4583 free(lun, M_CTL); 4584 4585 STAILQ_FOREACH(nlun, &softc->lun_list, links) { 4586 mtx_lock(&nlun->lun_lock); 4587 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE); 4588 mtx_unlock(&nlun->lun_lock); 4589 } 4590 4591 return (0); 4592} 4593 4594static void 4595ctl_create_lun(struct ctl_be_lun *be_lun) 4596{ 4597 4598 /* 4599 * ctl_alloc_lun() should handle all potential failure cases. 4600 */ 4601 ctl_alloc_lun(control_softc, NULL, be_lun); 4602} 4603 4604int 4605ctl_add_lun(struct ctl_be_lun *be_lun) 4606{ 4607 struct ctl_softc *softc = control_softc; 4608 4609 mtx_lock(&softc->ctl_lock); 4610 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links); 4611 mtx_unlock(&softc->ctl_lock); 4612 wakeup(&softc->pending_lun_queue); 4613 4614 return (0); 4615} 4616 4617int 4618ctl_enable_lun(struct ctl_be_lun *be_lun) 4619{ 4620 struct ctl_softc *softc; 4621 struct ctl_port *port, *nport; 4622 struct ctl_lun *lun; 4623 int retval; 4624 4625 lun = (struct ctl_lun *)be_lun->ctl_lun; 4626 softc = lun->ctl_softc; 4627 4628 mtx_lock(&softc->ctl_lock); 4629 mtx_lock(&lun->lun_lock); 4630 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4631 /* 4632 * eh? Why did we get called if the LUN is already 4633 * enabled? 4634 */ 4635 mtx_unlock(&lun->lun_lock); 4636 mtx_unlock(&softc->ctl_lock); 4637 return (0); 4638 } 4639 lun->flags &= ~CTL_LUN_DISABLED; 4640 mtx_unlock(&lun->lun_lock); 4641 4642 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) { 4643 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4644 port->lun_map != NULL || port->lun_enable == NULL) 4645 continue; 4646 4647 /* 4648 * Drop the lock while we call the FETD's enable routine. 4649 * This can lead to a callback into CTL (at least in the 4650 * case of the internal initiator frontend. 4651 */ 4652 mtx_unlock(&softc->ctl_lock); 4653 retval = port->lun_enable(port->targ_lun_arg, lun->lun); 4654 mtx_lock(&softc->ctl_lock); 4655 if (retval != 0) { 4656 printf("%s: FETD %s port %d returned error " 4657 "%d for lun_enable on lun %jd\n", 4658 __func__, port->port_name, port->targ_port, 4659 retval, (intmax_t)lun->lun); 4660 } 4661 } 4662 4663 mtx_unlock(&softc->ctl_lock); 4664 ctl_isc_announce_lun(lun); 4665 4666 return (0); 4667} 4668 4669int 4670ctl_disable_lun(struct ctl_be_lun *be_lun) 4671{ 4672 struct ctl_softc *softc; 4673 struct ctl_port *port; 4674 struct ctl_lun *lun; 4675 int retval; 4676 4677 lun = (struct ctl_lun *)be_lun->ctl_lun; 4678 softc = lun->ctl_softc; 4679 4680 mtx_lock(&softc->ctl_lock); 4681 mtx_lock(&lun->lun_lock); 4682 if (lun->flags & CTL_LUN_DISABLED) { 4683 mtx_unlock(&lun->lun_lock); 4684 mtx_unlock(&softc->ctl_lock); 4685 return (0); 4686 } 4687 lun->flags |= CTL_LUN_DISABLED; 4688 mtx_unlock(&lun->lun_lock); 4689 4690 STAILQ_FOREACH(port, &softc->port_list, links) { 4691 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 || 4692 port->lun_map != NULL || port->lun_disable == NULL) 4693 continue; 4694 4695 /* 4696 * Drop the lock before we call the frontend's disable 4697 * routine, to avoid lock order reversals. 4698 * 4699 * XXX KDM what happens if the frontend list changes while 4700 * we're traversing it? It's unlikely, but should be handled. 4701 */ 4702 mtx_unlock(&softc->ctl_lock); 4703 retval = port->lun_disable(port->targ_lun_arg, lun->lun); 4704 mtx_lock(&softc->ctl_lock); 4705 if (retval != 0) { 4706 printf("%s: FETD %s port %d returned error " 4707 "%d for lun_disable on lun %jd\n", 4708 __func__, port->port_name, port->targ_port, 4709 retval, (intmax_t)lun->lun); 4710 } 4711 } 4712 4713 mtx_unlock(&softc->ctl_lock); 4714 ctl_isc_announce_lun(lun); 4715 4716 return (0); 4717} 4718 4719int 4720ctl_start_lun(struct ctl_be_lun *be_lun) 4721{ 4722 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4723 4724 mtx_lock(&lun->lun_lock); 4725 lun->flags &= ~CTL_LUN_STOPPED; 4726 mtx_unlock(&lun->lun_lock); 4727 return (0); 4728} 4729 4730int 4731ctl_stop_lun(struct ctl_be_lun *be_lun) 4732{ 4733 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4734 4735 mtx_lock(&lun->lun_lock); 4736 lun->flags |= CTL_LUN_STOPPED; 4737 mtx_unlock(&lun->lun_lock); 4738 return (0); 4739} 4740 4741int 4742ctl_lun_no_media(struct ctl_be_lun *be_lun) 4743{ 4744 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4745 4746 mtx_lock(&lun->lun_lock); 4747 lun->flags |= CTL_LUN_NO_MEDIA; 4748 mtx_unlock(&lun->lun_lock); 4749 return (0); 4750} 4751 4752int 4753ctl_lun_has_media(struct ctl_be_lun *be_lun) 4754{ 4755 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4756 union ctl_ha_msg msg; 4757 4758 mtx_lock(&lun->lun_lock); 4759 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED); 4760 if (lun->flags & CTL_LUN_REMOVABLE) 4761 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE); 4762 mtx_unlock(&lun->lun_lock); 4763 if ((lun->flags & CTL_LUN_REMOVABLE) && 4764 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4765 bzero(&msg.ua, sizeof(msg.ua)); 4766 msg.hdr.msg_type = CTL_MSG_UA; 4767 msg.hdr.nexus.initid = -1; 4768 msg.hdr.nexus.targ_port = -1; 4769 msg.hdr.nexus.targ_lun = lun->lun; 4770 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4771 msg.ua.ua_all = 1; 4772 msg.ua.ua_set = 1; 4773 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE; 4774 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4775 M_WAITOK); 4776 } 4777 return (0); 4778} 4779 4780int 4781ctl_lun_ejected(struct ctl_be_lun *be_lun) 4782{ 4783 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4784 4785 mtx_lock(&lun->lun_lock); 4786 lun->flags |= CTL_LUN_EJECTED; 4787 mtx_unlock(&lun->lun_lock); 4788 return (0); 4789} 4790 4791int 4792ctl_lun_primary(struct ctl_be_lun *be_lun) 4793{ 4794 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4795 4796 mtx_lock(&lun->lun_lock); 4797 lun->flags |= CTL_LUN_PRIMARY_SC; 4798 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4799 mtx_unlock(&lun->lun_lock); 4800 ctl_isc_announce_lun(lun); 4801 return (0); 4802} 4803 4804int 4805ctl_lun_secondary(struct ctl_be_lun *be_lun) 4806{ 4807 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4808 4809 mtx_lock(&lun->lun_lock); 4810 lun->flags &= ~CTL_LUN_PRIMARY_SC; 4811 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE); 4812 mtx_unlock(&lun->lun_lock); 4813 ctl_isc_announce_lun(lun); 4814 return (0); 4815} 4816 4817int 4818ctl_invalidate_lun(struct ctl_be_lun *be_lun) 4819{ 4820 struct ctl_softc *softc; 4821 struct ctl_lun *lun; 4822 4823 lun = (struct ctl_lun *)be_lun->ctl_lun; 4824 softc = lun->ctl_softc; 4825 4826 mtx_lock(&lun->lun_lock); 4827 4828 /* 4829 * The LUN needs to be disabled before it can be marked invalid. 4830 */ 4831 if ((lun->flags & CTL_LUN_DISABLED) == 0) { 4832 mtx_unlock(&lun->lun_lock); 4833 return (-1); 4834 } 4835 /* 4836 * Mark the LUN invalid. 4837 */ 4838 lun->flags |= CTL_LUN_INVALID; 4839 4840 /* 4841 * If there is nothing in the OOA queue, go ahead and free the LUN. 4842 * If we have something in the OOA queue, we'll free it when the 4843 * last I/O completes. 4844 */ 4845 if (TAILQ_EMPTY(&lun->ooa_queue)) { 4846 mtx_unlock(&lun->lun_lock); 4847 mtx_lock(&softc->ctl_lock); 4848 ctl_free_lun(lun); 4849 mtx_unlock(&softc->ctl_lock); 4850 } else 4851 mtx_unlock(&lun->lun_lock); 4852 4853 return (0); 4854} 4855 4856void 4857ctl_lun_capacity_changed(struct ctl_be_lun *be_lun) 4858{ 4859 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun; 4860 union ctl_ha_msg msg; 4861 4862 mtx_lock(&lun->lun_lock); 4863 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE); 4864 mtx_unlock(&lun->lun_lock); 4865 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 4866 /* Send msg to other side. */ 4867 bzero(&msg.ua, sizeof(msg.ua)); 4868 msg.hdr.msg_type = CTL_MSG_UA; 4869 msg.hdr.nexus.initid = -1; 4870 msg.hdr.nexus.targ_port = -1; 4871 msg.hdr.nexus.targ_lun = lun->lun; 4872 msg.hdr.nexus.targ_mapped_lun = lun->lun; 4873 msg.ua.ua_all = 1; 4874 msg.ua.ua_set = 1; 4875 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE; 4876 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua), 4877 M_WAITOK); 4878 } 4879} 4880 4881/* 4882 * Backend "memory move is complete" callback for requests that never 4883 * make it down to say RAIDCore's configuration code. 4884 */ 4885int 4886ctl_config_move_done(union ctl_io *io) 4887{ 4888 int retval; 4889 4890 CTL_DEBUG_PRINT(("ctl_config_move_done\n")); 4891 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI, 4892 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type)); 4893 4894 if ((io->io_hdr.port_status != 0) && 4895 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4896 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4897 /* 4898 * For hardware error sense keys, the sense key 4899 * specific value is defined to be a retry count, 4900 * but we use it to pass back an internal FETD 4901 * error code. XXX KDM Hopefully the FETD is only 4902 * using 16 bits for an error code, since that's 4903 * all the space we have in the sks field. 4904 */ 4905 ctl_set_internal_failure(&io->scsiio, 4906 /*sks_valid*/ 1, 4907 /*retry_count*/ 4908 io->io_hdr.port_status); 4909 } 4910 4911 if (ctl_debug & CTL_DEBUG_CDB_DATA) 4912 ctl_data_print(io); 4913 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) || 4914 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 4915 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) || 4916 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) { 4917 /* 4918 * XXX KDM just assuming a single pointer here, and not a 4919 * S/G list. If we start using S/G lists for config data, 4920 * we'll need to know how to clean them up here as well. 4921 */ 4922 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 4923 free(io->scsiio.kern_data_ptr, M_CTL); 4924 ctl_done(io); 4925 retval = CTL_RETVAL_COMPLETE; 4926 } else { 4927 /* 4928 * XXX KDM now we need to continue data movement. Some 4929 * options: 4930 * - call ctl_scsiio() again? We don't do this for data 4931 * writes, because for those at least we know ahead of 4932 * time where the write will go and how long it is. For 4933 * config writes, though, that information is largely 4934 * contained within the write itself, thus we need to 4935 * parse out the data again. 4936 * 4937 * - Call some other function once the data is in? 4938 */ 4939 4940 /* 4941 * XXX KDM call ctl_scsiio() again for now, and check flag 4942 * bits to see whether we're allocated or not. 4943 */ 4944 retval = ctl_scsiio(&io->scsiio); 4945 } 4946 return (retval); 4947} 4948 4949/* 4950 * This gets called by a backend driver when it is done with a 4951 * data_submit method. 4952 */ 4953void 4954ctl_data_submit_done(union ctl_io *io) 4955{ 4956 /* 4957 * If the IO_CONT flag is set, we need to call the supplied 4958 * function to continue processing the I/O, instead of completing 4959 * the I/O just yet. 4960 * 4961 * If there is an error, though, we don't want to keep processing. 4962 * Instead, just send status back to the initiator. 4963 */ 4964 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4965 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4966 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4967 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4968 io->scsiio.io_cont(io); 4969 return; 4970 } 4971 ctl_done(io); 4972} 4973 4974/* 4975 * This gets called by a backend driver when it is done with a 4976 * configuration write. 4977 */ 4978void 4979ctl_config_write_done(union ctl_io *io) 4980{ 4981 uint8_t *buf; 4982 4983 /* 4984 * If the IO_CONT flag is set, we need to call the supplied 4985 * function to continue processing the I/O, instead of completing 4986 * the I/O just yet. 4987 * 4988 * If there is an error, though, we don't want to keep processing. 4989 * Instead, just send status back to the initiator. 4990 */ 4991 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) && 4992 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 && 4993 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE || 4994 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) { 4995 io->scsiio.io_cont(io); 4996 return; 4997 } 4998 /* 4999 * Since a configuration write can be done for commands that actually 5000 * have data allocated, like write buffer, and commands that have 5001 * no data, like start/stop unit, we need to check here. 5002 */ 5003 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5004 buf = io->scsiio.kern_data_ptr; 5005 else 5006 buf = NULL; 5007 ctl_done(io); 5008 if (buf) 5009 free(buf, M_CTL); 5010} 5011 5012void 5013ctl_config_read_done(union ctl_io *io) 5014{ 5015 uint8_t *buf; 5016 5017 /* 5018 * If there is some error -- we are done, skip data transfer. 5019 */ 5020 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 || 5021 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 5022 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) { 5023 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED) 5024 buf = io->scsiio.kern_data_ptr; 5025 else 5026 buf = NULL; 5027 ctl_done(io); 5028 if (buf) 5029 free(buf, M_CTL); 5030 return; 5031 } 5032 5033 /* 5034 * If the IO_CONT flag is set, we need to call the supplied 5035 * function to continue processing the I/O, instead of completing 5036 * the I/O just yet. 5037 */ 5038 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) { 5039 io->scsiio.io_cont(io); 5040 return; 5041 } 5042 5043 ctl_datamove(io); 5044} 5045 5046/* 5047 * SCSI release command. 5048 */ 5049int 5050ctl_scsi_release(struct ctl_scsiio *ctsio) 5051{ 5052 struct ctl_lun *lun; 5053 uint32_t residx; 5054 5055 CTL_DEBUG_PRINT(("ctl_scsi_release\n")); 5056 5057 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5058 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5059 5060 /* 5061 * XXX KDM right now, we only support LUN reservation. We don't 5062 * support 3rd party reservations, or extent reservations, which 5063 * might actually need the parameter list. If we've gotten this 5064 * far, we've got a LUN reservation. Anything else got kicked out 5065 * above. So, according to SPC, ignore the length. 5066 */ 5067 5068 mtx_lock(&lun->lun_lock); 5069 5070 /* 5071 * According to SPC, it is not an error for an intiator to attempt 5072 * to release a reservation on a LUN that isn't reserved, or that 5073 * is reserved by another initiator. The reservation can only be 5074 * released, though, by the initiator who made it or by one of 5075 * several reset type events. 5076 */ 5077 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx)) 5078 lun->flags &= ~CTL_LUN_RESERVED; 5079 5080 mtx_unlock(&lun->lun_lock); 5081 5082 ctl_set_success(ctsio); 5083 ctl_done((union ctl_io *)ctsio); 5084 return (CTL_RETVAL_COMPLETE); 5085} 5086 5087int 5088ctl_scsi_reserve(struct ctl_scsiio *ctsio) 5089{ 5090 struct ctl_lun *lun; 5091 uint32_t residx; 5092 5093 CTL_DEBUG_PRINT(("ctl_reserve\n")); 5094 5095 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5096 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5097 5098 /* 5099 * XXX KDM right now, we only support LUN reservation. We don't 5100 * support 3rd party reservations, or extent reservations, which 5101 * might actually need the parameter list. If we've gotten this 5102 * far, we've got a LUN reservation. Anything else got kicked out 5103 * above. So, according to SPC, ignore the length. 5104 */ 5105 5106 mtx_lock(&lun->lun_lock); 5107 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) { 5108 ctl_set_reservation_conflict(ctsio); 5109 goto bailout; 5110 } 5111 5112 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */ 5113 if (lun->flags & CTL_LUN_PR_RESERVED) { 5114 ctl_set_success(ctsio); 5115 goto bailout; 5116 } 5117 5118 lun->flags |= CTL_LUN_RESERVED; 5119 lun->res_idx = residx; 5120 ctl_set_success(ctsio); 5121 5122bailout: 5123 mtx_unlock(&lun->lun_lock); 5124 ctl_done((union ctl_io *)ctsio); 5125 return (CTL_RETVAL_COMPLETE); 5126} 5127 5128int 5129ctl_start_stop(struct ctl_scsiio *ctsio) 5130{ 5131 struct scsi_start_stop_unit *cdb; 5132 struct ctl_lun *lun; 5133 int retval; 5134 5135 CTL_DEBUG_PRINT(("ctl_start_stop\n")); 5136 5137 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5138 cdb = (struct scsi_start_stop_unit *)ctsio->cdb; 5139 5140 if ((cdb->how & SSS_PC_MASK) == 0) { 5141 if ((lun->flags & CTL_LUN_PR_RESERVED) && 5142 (cdb->how & SSS_START) == 0) { 5143 uint32_t residx; 5144 5145 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5146 if (ctl_get_prkey(lun, residx) == 0 || 5147 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) { 5148 5149 ctl_set_reservation_conflict(ctsio); 5150 ctl_done((union ctl_io *)ctsio); 5151 return (CTL_RETVAL_COMPLETE); 5152 } 5153 } 5154 5155 if ((cdb->how & SSS_LOEJ) && 5156 (lun->flags & CTL_LUN_REMOVABLE) == 0) { 5157 ctl_set_invalid_field(ctsio, 5158 /*sks_valid*/ 1, 5159 /*command*/ 1, 5160 /*field*/ 4, 5161 /*bit_valid*/ 1, 5162 /*bit*/ 1); 5163 ctl_done((union ctl_io *)ctsio); 5164 return (CTL_RETVAL_COMPLETE); 5165 } 5166 5167 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) && 5168 lun->prevent_count > 0) { 5169 /* "Medium removal prevented" */ 5170 ctl_set_sense(ctsio, /*current_error*/ 1, 5171 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ? 5172 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST, 5173 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE); 5174 ctl_done((union ctl_io *)ctsio); 5175 return (CTL_RETVAL_COMPLETE); 5176 } 5177 } 5178 5179 retval = lun->backend->config_write((union ctl_io *)ctsio); 5180 return (retval); 5181} 5182 5183int 5184ctl_prevent_allow(struct ctl_scsiio *ctsio) 5185{ 5186 struct ctl_lun *lun; 5187 struct scsi_prevent *cdb; 5188 int retval; 5189 uint32_t initidx; 5190 5191 CTL_DEBUG_PRINT(("ctl_prevent_allow\n")); 5192 5193 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5194 cdb = (struct scsi_prevent *)ctsio->cdb; 5195 5196 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) { 5197 ctl_set_invalid_opcode(ctsio); 5198 ctl_done((union ctl_io *)ctsio); 5199 return (CTL_RETVAL_COMPLETE); 5200 } 5201 5202 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5203 mtx_lock(&lun->lun_lock); 5204 if ((cdb->how & PR_PREVENT) && 5205 ctl_is_set(lun->prevent, initidx) == 0) { 5206 ctl_set_mask(lun->prevent, initidx); 5207 lun->prevent_count++; 5208 } else if ((cdb->how & PR_PREVENT) == 0 && 5209 ctl_is_set(lun->prevent, initidx)) { 5210 ctl_clear_mask(lun->prevent, initidx); 5211 lun->prevent_count--; 5212 } 5213 mtx_unlock(&lun->lun_lock); 5214 retval = lun->backend->config_write((union ctl_io *)ctsio); 5215 return (retval); 5216} 5217 5218/* 5219 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but 5220 * we don't really do anything with the LBA and length fields if the user 5221 * passes them in. Instead we'll just flush out the cache for the entire 5222 * LUN. 5223 */ 5224int 5225ctl_sync_cache(struct ctl_scsiio *ctsio) 5226{ 5227 struct ctl_lun *lun; 5228 struct ctl_softc *softc; 5229 struct ctl_lba_len_flags *lbalen; 5230 uint64_t starting_lba; 5231 uint32_t block_count; 5232 int retval; 5233 uint8_t byte2; 5234 5235 CTL_DEBUG_PRINT(("ctl_sync_cache\n")); 5236 5237 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5238 softc = lun->ctl_softc; 5239 retval = 0; 5240 5241 switch (ctsio->cdb[0]) { 5242 case SYNCHRONIZE_CACHE: { 5243 struct scsi_sync_cache *cdb; 5244 cdb = (struct scsi_sync_cache *)ctsio->cdb; 5245 5246 starting_lba = scsi_4btoul(cdb->begin_lba); 5247 block_count = scsi_2btoul(cdb->lb_count); 5248 byte2 = cdb->byte2; 5249 break; 5250 } 5251 case SYNCHRONIZE_CACHE_16: { 5252 struct scsi_sync_cache_16 *cdb; 5253 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb; 5254 5255 starting_lba = scsi_8btou64(cdb->begin_lba); 5256 block_count = scsi_4btoul(cdb->lb_count); 5257 byte2 = cdb->byte2; 5258 break; 5259 } 5260 default: 5261 ctl_set_invalid_opcode(ctsio); 5262 ctl_done((union ctl_io *)ctsio); 5263 goto bailout; 5264 break; /* NOTREACHED */ 5265 } 5266 5267 /* 5268 * We check the LBA and length, but don't do anything with them. 5269 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to 5270 * get flushed. This check will just help satisfy anyone who wants 5271 * to see an error for an out of range LBA. 5272 */ 5273 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) { 5274 ctl_set_lba_out_of_range(ctsio); 5275 ctl_done((union ctl_io *)ctsio); 5276 goto bailout; 5277 } 5278 5279 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5280 lbalen->lba = starting_lba; 5281 lbalen->len = block_count; 5282 lbalen->flags = byte2; 5283 retval = lun->backend->config_write((union ctl_io *)ctsio); 5284 5285bailout: 5286 return (retval); 5287} 5288 5289int 5290ctl_format(struct ctl_scsiio *ctsio) 5291{ 5292 struct scsi_format *cdb; 5293 struct ctl_lun *lun; 5294 int length, defect_list_len; 5295 5296 CTL_DEBUG_PRINT(("ctl_format\n")); 5297 5298 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5299 5300 cdb = (struct scsi_format *)ctsio->cdb; 5301 5302 length = 0; 5303 if (cdb->byte2 & SF_FMTDATA) { 5304 if (cdb->byte2 & SF_LONGLIST) 5305 length = sizeof(struct scsi_format_header_long); 5306 else 5307 length = sizeof(struct scsi_format_header_short); 5308 } 5309 5310 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) 5311 && (length > 0)) { 5312 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK); 5313 ctsio->kern_data_len = length; 5314 ctsio->kern_total_len = length; 5315 ctsio->kern_data_resid = 0; 5316 ctsio->kern_rel_offset = 0; 5317 ctsio->kern_sg_entries = 0; 5318 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5319 ctsio->be_move_done = ctl_config_move_done; 5320 ctl_datamove((union ctl_io *)ctsio); 5321 5322 return (CTL_RETVAL_COMPLETE); 5323 } 5324 5325 defect_list_len = 0; 5326 5327 if (cdb->byte2 & SF_FMTDATA) { 5328 if (cdb->byte2 & SF_LONGLIST) { 5329 struct scsi_format_header_long *header; 5330 5331 header = (struct scsi_format_header_long *) 5332 ctsio->kern_data_ptr; 5333 5334 defect_list_len = scsi_4btoul(header->defect_list_len); 5335 if (defect_list_len != 0) { 5336 ctl_set_invalid_field(ctsio, 5337 /*sks_valid*/ 1, 5338 /*command*/ 0, 5339 /*field*/ 2, 5340 /*bit_valid*/ 0, 5341 /*bit*/ 0); 5342 goto bailout; 5343 } 5344 } else { 5345 struct scsi_format_header_short *header; 5346 5347 header = (struct scsi_format_header_short *) 5348 ctsio->kern_data_ptr; 5349 5350 defect_list_len = scsi_2btoul(header->defect_list_len); 5351 if (defect_list_len != 0) { 5352 ctl_set_invalid_field(ctsio, 5353 /*sks_valid*/ 1, 5354 /*command*/ 0, 5355 /*field*/ 2, 5356 /*bit_valid*/ 0, 5357 /*bit*/ 0); 5358 goto bailout; 5359 } 5360 } 5361 } 5362 5363 ctl_set_success(ctsio); 5364bailout: 5365 5366 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5367 free(ctsio->kern_data_ptr, M_CTL); 5368 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5369 } 5370 5371 ctl_done((union ctl_io *)ctsio); 5372 return (CTL_RETVAL_COMPLETE); 5373} 5374 5375int 5376ctl_read_buffer(struct ctl_scsiio *ctsio) 5377{ 5378 struct ctl_lun *lun; 5379 uint64_t buffer_offset; 5380 uint32_t len; 5381 uint8_t byte2; 5382 static uint8_t descr[4]; 5383 static uint8_t echo_descr[4] = { 0 }; 5384 5385 CTL_DEBUG_PRINT(("ctl_read_buffer\n")); 5386 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5387 switch (ctsio->cdb[0]) { 5388 case READ_BUFFER: { 5389 struct scsi_read_buffer *cdb; 5390 5391 cdb = (struct scsi_read_buffer *)ctsio->cdb; 5392 buffer_offset = scsi_3btoul(cdb->offset); 5393 len = scsi_3btoul(cdb->length); 5394 byte2 = cdb->byte2; 5395 break; 5396 } 5397 case READ_BUFFER_16: { 5398 struct scsi_read_buffer_16 *cdb; 5399 5400 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb; 5401 buffer_offset = scsi_8btou64(cdb->offset); 5402 len = scsi_4btoul(cdb->length); 5403 byte2 = cdb->byte2; 5404 break; 5405 } 5406 default: /* This shouldn't happen. */ 5407 ctl_set_invalid_opcode(ctsio); 5408 ctl_done((union ctl_io *)ctsio); 5409 return (CTL_RETVAL_COMPLETE); 5410 } 5411 5412 if ((byte2 & RWB_MODE) != RWB_MODE_DATA && 5413 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR && 5414 (byte2 & RWB_MODE) != RWB_MODE_DESCR) { 5415 ctl_set_invalid_field(ctsio, 5416 /*sks_valid*/ 1, 5417 /*command*/ 1, 5418 /*field*/ 1, 5419 /*bit_valid*/ 1, 5420 /*bit*/ 4); 5421 ctl_done((union ctl_io *)ctsio); 5422 return (CTL_RETVAL_COMPLETE); 5423 } 5424 5425 if (buffer_offset > CTL_WRITE_BUFFER_SIZE || 5426 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5427 ctl_set_invalid_field(ctsio, 5428 /*sks_valid*/ 1, 5429 /*command*/ 1, 5430 /*field*/ 6, 5431 /*bit_valid*/ 0, 5432 /*bit*/ 0); 5433 ctl_done((union ctl_io *)ctsio); 5434 return (CTL_RETVAL_COMPLETE); 5435 } 5436 5437 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) { 5438 descr[0] = 0; 5439 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]); 5440 ctsio->kern_data_ptr = descr; 5441 len = min(len, sizeof(descr)); 5442 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) { 5443 ctsio->kern_data_ptr = echo_descr; 5444 len = min(len, sizeof(echo_descr)); 5445 } else { 5446 if (lun->write_buffer == NULL) { 5447 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5448 M_CTL, M_WAITOK); 5449 } 5450 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5451 } 5452 ctsio->kern_data_len = len; 5453 ctsio->kern_total_len = len; 5454 ctsio->kern_data_resid = 0; 5455 ctsio->kern_rel_offset = 0; 5456 ctsio->kern_sg_entries = 0; 5457 ctl_set_success(ctsio); 5458 ctsio->be_move_done = ctl_config_move_done; 5459 ctl_datamove((union ctl_io *)ctsio); 5460 return (CTL_RETVAL_COMPLETE); 5461} 5462 5463int 5464ctl_write_buffer(struct ctl_scsiio *ctsio) 5465{ 5466 struct scsi_write_buffer *cdb; 5467 struct ctl_lun *lun; 5468 int buffer_offset, len; 5469 5470 CTL_DEBUG_PRINT(("ctl_write_buffer\n")); 5471 5472 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5473 cdb = (struct scsi_write_buffer *)ctsio->cdb; 5474 5475 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) { 5476 ctl_set_invalid_field(ctsio, 5477 /*sks_valid*/ 1, 5478 /*command*/ 1, 5479 /*field*/ 1, 5480 /*bit_valid*/ 1, 5481 /*bit*/ 4); 5482 ctl_done((union ctl_io *)ctsio); 5483 return (CTL_RETVAL_COMPLETE); 5484 } 5485 5486 len = scsi_3btoul(cdb->length); 5487 buffer_offset = scsi_3btoul(cdb->offset); 5488 5489 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) { 5490 ctl_set_invalid_field(ctsio, 5491 /*sks_valid*/ 1, 5492 /*command*/ 1, 5493 /*field*/ 6, 5494 /*bit_valid*/ 0, 5495 /*bit*/ 0); 5496 ctl_done((union ctl_io *)ctsio); 5497 return (CTL_RETVAL_COMPLETE); 5498 } 5499 5500 /* 5501 * If we've got a kernel request that hasn't been malloced yet, 5502 * malloc it and tell the caller the data buffer is here. 5503 */ 5504 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5505 if (lun->write_buffer == NULL) { 5506 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE, 5507 M_CTL, M_WAITOK); 5508 } 5509 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset; 5510 ctsio->kern_data_len = len; 5511 ctsio->kern_total_len = len; 5512 ctsio->kern_data_resid = 0; 5513 ctsio->kern_rel_offset = 0; 5514 ctsio->kern_sg_entries = 0; 5515 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5516 ctsio->be_move_done = ctl_config_move_done; 5517 ctl_datamove((union ctl_io *)ctsio); 5518 5519 return (CTL_RETVAL_COMPLETE); 5520 } 5521 5522 ctl_set_success(ctsio); 5523 ctl_done((union ctl_io *)ctsio); 5524 return (CTL_RETVAL_COMPLETE); 5525} 5526 5527int 5528ctl_write_same(struct ctl_scsiio *ctsio) 5529{ 5530 struct ctl_lun *lun; 5531 struct ctl_lba_len_flags *lbalen; 5532 uint64_t lba; 5533 uint32_t num_blocks; 5534 int len, retval; 5535 uint8_t byte2; 5536 5537 CTL_DEBUG_PRINT(("ctl_write_same\n")); 5538 5539 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5540 5541 switch (ctsio->cdb[0]) { 5542 case WRITE_SAME_10: { 5543 struct scsi_write_same_10 *cdb; 5544 5545 cdb = (struct scsi_write_same_10 *)ctsio->cdb; 5546 5547 lba = scsi_4btoul(cdb->addr); 5548 num_blocks = scsi_2btoul(cdb->length); 5549 byte2 = cdb->byte2; 5550 break; 5551 } 5552 case WRITE_SAME_16: { 5553 struct scsi_write_same_16 *cdb; 5554 5555 cdb = (struct scsi_write_same_16 *)ctsio->cdb; 5556 5557 lba = scsi_8btou64(cdb->addr); 5558 num_blocks = scsi_4btoul(cdb->length); 5559 byte2 = cdb->byte2; 5560 break; 5561 } 5562 default: 5563 /* 5564 * We got a command we don't support. This shouldn't 5565 * happen, commands should be filtered out above us. 5566 */ 5567 ctl_set_invalid_opcode(ctsio); 5568 ctl_done((union ctl_io *)ctsio); 5569 5570 return (CTL_RETVAL_COMPLETE); 5571 break; /* NOTREACHED */ 5572 } 5573 5574 /* ANCHOR flag can be used only together with UNMAP */ 5575 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) { 5576 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 5577 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 5578 ctl_done((union ctl_io *)ctsio); 5579 return (CTL_RETVAL_COMPLETE); 5580 } 5581 5582 /* 5583 * The first check is to make sure we're in bounds, the second 5584 * check is to catch wrap-around problems. If the lba + num blocks 5585 * is less than the lba, then we've wrapped around and the block 5586 * range is invalid anyway. 5587 */ 5588 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5589 || ((lba + num_blocks) < lba)) { 5590 ctl_set_lba_out_of_range(ctsio); 5591 ctl_done((union ctl_io *)ctsio); 5592 return (CTL_RETVAL_COMPLETE); 5593 } 5594 5595 /* Zero number of blocks means "to the last logical block" */ 5596 if (num_blocks == 0) { 5597 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) { 5598 ctl_set_invalid_field(ctsio, 5599 /*sks_valid*/ 0, 5600 /*command*/ 1, 5601 /*field*/ 0, 5602 /*bit_valid*/ 0, 5603 /*bit*/ 0); 5604 ctl_done((union ctl_io *)ctsio); 5605 return (CTL_RETVAL_COMPLETE); 5606 } 5607 num_blocks = (lun->be_lun->maxlba + 1) - lba; 5608 } 5609 5610 len = lun->be_lun->blocksize; 5611 5612 /* 5613 * If we've got a kernel request that hasn't been malloced yet, 5614 * malloc it and tell the caller the data buffer is here. 5615 */ 5616 if ((byte2 & SWS_NDOB) == 0 && 5617 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5618 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5619 ctsio->kern_data_len = len; 5620 ctsio->kern_total_len = len; 5621 ctsio->kern_data_resid = 0; 5622 ctsio->kern_rel_offset = 0; 5623 ctsio->kern_sg_entries = 0; 5624 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5625 ctsio->be_move_done = ctl_config_move_done; 5626 ctl_datamove((union ctl_io *)ctsio); 5627 5628 return (CTL_RETVAL_COMPLETE); 5629 } 5630 5631 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5632 lbalen->lba = lba; 5633 lbalen->len = num_blocks; 5634 lbalen->flags = byte2; 5635 retval = lun->backend->config_write((union ctl_io *)ctsio); 5636 5637 return (retval); 5638} 5639 5640int 5641ctl_unmap(struct ctl_scsiio *ctsio) 5642{ 5643 struct ctl_lun *lun; 5644 struct scsi_unmap *cdb; 5645 struct ctl_ptr_len_flags *ptrlen; 5646 struct scsi_unmap_header *hdr; 5647 struct scsi_unmap_desc *buf, *end, *endnz, *range; 5648 uint64_t lba; 5649 uint32_t num_blocks; 5650 int len, retval; 5651 uint8_t byte2; 5652 5653 CTL_DEBUG_PRINT(("ctl_unmap\n")); 5654 5655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5656 cdb = (struct scsi_unmap *)ctsio->cdb; 5657 5658 len = scsi_2btoul(cdb->length); 5659 byte2 = cdb->byte2; 5660 5661 /* 5662 * If we've got a kernel request that hasn't been malloced yet, 5663 * malloc it and tell the caller the data buffer is here. 5664 */ 5665 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 5666 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);; 5667 ctsio->kern_data_len = len; 5668 ctsio->kern_total_len = len; 5669 ctsio->kern_data_resid = 0; 5670 ctsio->kern_rel_offset = 0; 5671 ctsio->kern_sg_entries = 0; 5672 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 5673 ctsio->be_move_done = ctl_config_move_done; 5674 ctl_datamove((union ctl_io *)ctsio); 5675 5676 return (CTL_RETVAL_COMPLETE); 5677 } 5678 5679 len = ctsio->kern_total_len - ctsio->kern_data_resid; 5680 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr; 5681 if (len < sizeof (*hdr) || 5682 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) || 5683 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) || 5684 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) { 5685 ctl_set_invalid_field(ctsio, 5686 /*sks_valid*/ 0, 5687 /*command*/ 0, 5688 /*field*/ 0, 5689 /*bit_valid*/ 0, 5690 /*bit*/ 0); 5691 goto done; 5692 } 5693 len = scsi_2btoul(hdr->desc_length); 5694 buf = (struct scsi_unmap_desc *)(hdr + 1); 5695 end = buf + len / sizeof(*buf); 5696 5697 endnz = buf; 5698 for (range = buf; range < end; range++) { 5699 lba = scsi_8btou64(range->lba); 5700 num_blocks = scsi_4btoul(range->length); 5701 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 5702 || ((lba + num_blocks) < lba)) { 5703 ctl_set_lba_out_of_range(ctsio); 5704 ctl_done((union ctl_io *)ctsio); 5705 return (CTL_RETVAL_COMPLETE); 5706 } 5707 if (num_blocks != 0) 5708 endnz = range + 1; 5709 } 5710 5711 /* 5712 * Block backend can not handle zero last range. 5713 * Filter it out and return if there is nothing left. 5714 */ 5715 len = (uint8_t *)endnz - (uint8_t *)buf; 5716 if (len == 0) { 5717 ctl_set_success(ctsio); 5718 goto done; 5719 } 5720 5721 mtx_lock(&lun->lun_lock); 5722 ptrlen = (struct ctl_ptr_len_flags *) 5723 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 5724 ptrlen->ptr = (void *)buf; 5725 ptrlen->len = len; 5726 ptrlen->flags = byte2; 5727 ctl_check_blocked(lun); 5728 mtx_unlock(&lun->lun_lock); 5729 5730 retval = lun->backend->config_write((union ctl_io *)ctsio); 5731 return (retval); 5732 5733done: 5734 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) { 5735 free(ctsio->kern_data_ptr, M_CTL); 5736 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED; 5737 } 5738 ctl_done((union ctl_io *)ctsio); 5739 return (CTL_RETVAL_COMPLETE); 5740} 5741 5742/* 5743 * Note that this function currently doesn't actually do anything inside 5744 * CTL to enforce things if the DQue bit is turned on. 5745 * 5746 * Also note that this function can't be used in the default case, because 5747 * the DQue bit isn't set in the changeable mask for the control mode page 5748 * anyway. This is just here as an example for how to implement a page 5749 * handler, and a placeholder in case we want to allow the user to turn 5750 * tagged queueing on and off. 5751 * 5752 * The D_SENSE bit handling is functional, however, and will turn 5753 * descriptor sense on and off for a given LUN. 5754 */ 5755int 5756ctl_control_page_handler(struct ctl_scsiio *ctsio, 5757 struct ctl_page_index *page_index, uint8_t *page_ptr) 5758{ 5759 struct scsi_control_page *current_cp, *saved_cp, *user_cp; 5760 struct ctl_lun *lun; 5761 int set_ua; 5762 uint32_t initidx; 5763 5764 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5765 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5766 set_ua = 0; 5767 5768 user_cp = (struct scsi_control_page *)page_ptr; 5769 current_cp = (struct scsi_control_page *) 5770 (page_index->page_data + (page_index->page_len * 5771 CTL_PAGE_CURRENT)); 5772 saved_cp = (struct scsi_control_page *) 5773 (page_index->page_data + (page_index->page_len * 5774 CTL_PAGE_SAVED)); 5775 5776 mtx_lock(&lun->lun_lock); 5777 if (((current_cp->rlec & SCP_DSENSE) == 0) 5778 && ((user_cp->rlec & SCP_DSENSE) != 0)) { 5779 /* 5780 * Descriptor sense is currently turned off and the user 5781 * wants to turn it on. 5782 */ 5783 current_cp->rlec |= SCP_DSENSE; 5784 saved_cp->rlec |= SCP_DSENSE; 5785 lun->flags |= CTL_LUN_SENSE_DESC; 5786 set_ua = 1; 5787 } else if (((current_cp->rlec & SCP_DSENSE) != 0) 5788 && ((user_cp->rlec & SCP_DSENSE) == 0)) { 5789 /* 5790 * Descriptor sense is currently turned on, and the user 5791 * wants to turn it off. 5792 */ 5793 current_cp->rlec &= ~SCP_DSENSE; 5794 saved_cp->rlec &= ~SCP_DSENSE; 5795 lun->flags &= ~CTL_LUN_SENSE_DESC; 5796 set_ua = 1; 5797 } 5798 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) != 5799 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) { 5800 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5801 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5802 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK; 5803 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK; 5804 set_ua = 1; 5805 } 5806 if ((current_cp->eca_and_aen & SCP_SWP) != 5807 (user_cp->eca_and_aen & SCP_SWP)) { 5808 current_cp->eca_and_aen &= ~SCP_SWP; 5809 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5810 saved_cp->eca_and_aen &= ~SCP_SWP; 5811 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP; 5812 set_ua = 1; 5813 } 5814 if (set_ua != 0) 5815 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5816 mtx_unlock(&lun->lun_lock); 5817 if (set_ua) { 5818 ctl_isc_announce_mode(lun, 5819 ctl_get_initindex(&ctsio->io_hdr.nexus), 5820 page_index->page_code, page_index->subpage); 5821 } 5822 return (0); 5823} 5824 5825int 5826ctl_caching_sp_handler(struct ctl_scsiio *ctsio, 5827 struct ctl_page_index *page_index, uint8_t *page_ptr) 5828{ 5829 struct scsi_caching_page *current_cp, *saved_cp, *user_cp; 5830 struct ctl_lun *lun; 5831 int set_ua; 5832 uint32_t initidx; 5833 5834 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5835 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 5836 set_ua = 0; 5837 5838 user_cp = (struct scsi_caching_page *)page_ptr; 5839 current_cp = (struct scsi_caching_page *) 5840 (page_index->page_data + (page_index->page_len * 5841 CTL_PAGE_CURRENT)); 5842 saved_cp = (struct scsi_caching_page *) 5843 (page_index->page_data + (page_index->page_len * 5844 CTL_PAGE_SAVED)); 5845 5846 mtx_lock(&lun->lun_lock); 5847 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) != 5848 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) { 5849 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5850 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5851 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD); 5852 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD); 5853 set_ua = 1; 5854 } 5855 if (set_ua != 0) 5856 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE); 5857 mtx_unlock(&lun->lun_lock); 5858 if (set_ua) { 5859 ctl_isc_announce_mode(lun, 5860 ctl_get_initindex(&ctsio->io_hdr.nexus), 5861 page_index->page_code, page_index->subpage); 5862 } 5863 return (0); 5864} 5865 5866int 5867ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio, 5868 struct ctl_page_index *page_index, 5869 uint8_t *page_ptr) 5870{ 5871 uint8_t *c; 5872 int i; 5873 5874 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs; 5875 ctl_time_io_secs = 5876 (c[0] << 8) | 5877 (c[1] << 0) | 5878 0; 5879 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs)); 5880 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs); 5881 printf("page data:"); 5882 for (i=0; i<8; i++) 5883 printf(" %.2x",page_ptr[i]); 5884 printf("\n"); 5885 return (0); 5886} 5887 5888int 5889ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio, 5890 struct ctl_page_index *page_index, 5891 int pc) 5892{ 5893 struct copan_debugconf_subpage *page; 5894 5895 page = (struct copan_debugconf_subpage *)page_index->page_data + 5896 (page_index->page_len * pc); 5897 5898 switch (pc) { 5899 case SMS_PAGE_CTRL_CHANGEABLE >> 6: 5900 case SMS_PAGE_CTRL_DEFAULT >> 6: 5901 case SMS_PAGE_CTRL_SAVED >> 6: 5902 /* 5903 * We don't update the changable or default bits for this page. 5904 */ 5905 break; 5906 case SMS_PAGE_CTRL_CURRENT >> 6: 5907 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8; 5908 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0; 5909 break; 5910 default: 5911 break; 5912 } 5913 return (0); 5914} 5915 5916 5917static int 5918ctl_do_mode_select(union ctl_io *io) 5919{ 5920 struct scsi_mode_page_header *page_header; 5921 struct ctl_page_index *page_index; 5922 struct ctl_scsiio *ctsio; 5923 int page_len, page_len_offset, page_len_size; 5924 union ctl_modepage_info *modepage_info; 5925 struct ctl_lun *lun; 5926 int *len_left, *len_used; 5927 int retval, i; 5928 5929 ctsio = &io->scsiio; 5930 page_index = NULL; 5931 page_len = 0; 5932 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 5933 5934 modepage_info = (union ctl_modepage_info *) 5935 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 5936 len_left = &modepage_info->header.len_left; 5937 len_used = &modepage_info->header.len_used; 5938 5939do_next_page: 5940 5941 page_header = (struct scsi_mode_page_header *) 5942 (ctsio->kern_data_ptr + *len_used); 5943 5944 if (*len_left == 0) { 5945 free(ctsio->kern_data_ptr, M_CTL); 5946 ctl_set_success(ctsio); 5947 ctl_done((union ctl_io *)ctsio); 5948 return (CTL_RETVAL_COMPLETE); 5949 } else if (*len_left < sizeof(struct scsi_mode_page_header)) { 5950 5951 free(ctsio->kern_data_ptr, M_CTL); 5952 ctl_set_param_len_error(ctsio); 5953 ctl_done((union ctl_io *)ctsio); 5954 return (CTL_RETVAL_COMPLETE); 5955 5956 } else if ((page_header->page_code & SMPH_SPF) 5957 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) { 5958 5959 free(ctsio->kern_data_ptr, M_CTL); 5960 ctl_set_param_len_error(ctsio); 5961 ctl_done((union ctl_io *)ctsio); 5962 return (CTL_RETVAL_COMPLETE); 5963 } 5964 5965 5966 /* 5967 * XXX KDM should we do something with the block descriptor? 5968 */ 5969 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 5970 page_index = &lun->mode_pages.index[i]; 5971 if (lun->be_lun->lun_type == T_DIRECT && 5972 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 5973 continue; 5974 if (lun->be_lun->lun_type == T_PROCESSOR && 5975 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 5976 continue; 5977 if (lun->be_lun->lun_type == T_CDROM && 5978 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 5979 continue; 5980 5981 if ((page_index->page_code & SMPH_PC_MASK) != 5982 (page_header->page_code & SMPH_PC_MASK)) 5983 continue; 5984 5985 /* 5986 * If neither page has a subpage code, then we've got a 5987 * match. 5988 */ 5989 if (((page_index->page_code & SMPH_SPF) == 0) 5990 && ((page_header->page_code & SMPH_SPF) == 0)) { 5991 page_len = page_header->page_length; 5992 break; 5993 } 5994 5995 /* 5996 * If both pages have subpages, then the subpage numbers 5997 * have to match. 5998 */ 5999 if ((page_index->page_code & SMPH_SPF) 6000 && (page_header->page_code & SMPH_SPF)) { 6001 struct scsi_mode_page_header_sp *sph; 6002 6003 sph = (struct scsi_mode_page_header_sp *)page_header; 6004 if (page_index->subpage == sph->subpage) { 6005 page_len = scsi_2btoul(sph->page_length); 6006 break; 6007 } 6008 } 6009 } 6010 6011 /* 6012 * If we couldn't find the page, or if we don't have a mode select 6013 * handler for it, send back an error to the user. 6014 */ 6015 if ((i >= CTL_NUM_MODE_PAGES) 6016 || (page_index->select_handler == NULL)) { 6017 ctl_set_invalid_field(ctsio, 6018 /*sks_valid*/ 1, 6019 /*command*/ 0, 6020 /*field*/ *len_used, 6021 /*bit_valid*/ 0, 6022 /*bit*/ 0); 6023 free(ctsio->kern_data_ptr, M_CTL); 6024 ctl_done((union ctl_io *)ctsio); 6025 return (CTL_RETVAL_COMPLETE); 6026 } 6027 6028 if (page_index->page_code & SMPH_SPF) { 6029 page_len_offset = 2; 6030 page_len_size = 2; 6031 } else { 6032 page_len_size = 1; 6033 page_len_offset = 1; 6034 } 6035 6036 /* 6037 * If the length the initiator gives us isn't the one we specify in 6038 * the mode page header, or if they didn't specify enough data in 6039 * the CDB to avoid truncating this page, kick out the request. 6040 */ 6041 if ((page_len != (page_index->page_len - page_len_offset - 6042 page_len_size)) 6043 || (*len_left < page_index->page_len)) { 6044 6045 6046 ctl_set_invalid_field(ctsio, 6047 /*sks_valid*/ 1, 6048 /*command*/ 0, 6049 /*field*/ *len_used + page_len_offset, 6050 /*bit_valid*/ 0, 6051 /*bit*/ 0); 6052 free(ctsio->kern_data_ptr, M_CTL); 6053 ctl_done((union ctl_io *)ctsio); 6054 return (CTL_RETVAL_COMPLETE); 6055 } 6056 6057 /* 6058 * Run through the mode page, checking to make sure that the bits 6059 * the user changed are actually legal for him to change. 6060 */ 6061 for (i = 0; i < page_index->page_len; i++) { 6062 uint8_t *user_byte, *change_mask, *current_byte; 6063 int bad_bit; 6064 int j; 6065 6066 user_byte = (uint8_t *)page_header + i; 6067 change_mask = page_index->page_data + 6068 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i; 6069 current_byte = page_index->page_data + 6070 (page_index->page_len * CTL_PAGE_CURRENT) + i; 6071 6072 /* 6073 * Check to see whether the user set any bits in this byte 6074 * that he is not allowed to set. 6075 */ 6076 if ((*user_byte & ~(*change_mask)) == 6077 (*current_byte & ~(*change_mask))) 6078 continue; 6079 6080 /* 6081 * Go through bit by bit to determine which one is illegal. 6082 */ 6083 bad_bit = 0; 6084 for (j = 7; j >= 0; j--) { 6085 if ((((1 << i) & ~(*change_mask)) & *user_byte) != 6086 (((1 << i) & ~(*change_mask)) & *current_byte)) { 6087 bad_bit = i; 6088 break; 6089 } 6090 } 6091 ctl_set_invalid_field(ctsio, 6092 /*sks_valid*/ 1, 6093 /*command*/ 0, 6094 /*field*/ *len_used + i, 6095 /*bit_valid*/ 1, 6096 /*bit*/ bad_bit); 6097 free(ctsio->kern_data_ptr, M_CTL); 6098 ctl_done((union ctl_io *)ctsio); 6099 return (CTL_RETVAL_COMPLETE); 6100 } 6101 6102 /* 6103 * Decrement these before we call the page handler, since we may 6104 * end up getting called back one way or another before the handler 6105 * returns to this context. 6106 */ 6107 *len_left -= page_index->page_len; 6108 *len_used += page_index->page_len; 6109 6110 retval = page_index->select_handler(ctsio, page_index, 6111 (uint8_t *)page_header); 6112 6113 /* 6114 * If the page handler returns CTL_RETVAL_QUEUED, then we need to 6115 * wait until this queued command completes to finish processing 6116 * the mode page. If it returns anything other than 6117 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have 6118 * already set the sense information, freed the data pointer, and 6119 * completed the io for us. 6120 */ 6121 if (retval != CTL_RETVAL_COMPLETE) 6122 goto bailout_no_done; 6123 6124 /* 6125 * If the initiator sent us more than one page, parse the next one. 6126 */ 6127 if (*len_left > 0) 6128 goto do_next_page; 6129 6130 ctl_set_success(ctsio); 6131 free(ctsio->kern_data_ptr, M_CTL); 6132 ctl_done((union ctl_io *)ctsio); 6133 6134bailout_no_done: 6135 6136 return (CTL_RETVAL_COMPLETE); 6137 6138} 6139 6140int 6141ctl_mode_select(struct ctl_scsiio *ctsio) 6142{ 6143 int param_len, pf, sp; 6144 int header_size, bd_len; 6145 union ctl_modepage_info *modepage_info; 6146 6147 switch (ctsio->cdb[0]) { 6148 case MODE_SELECT_6: { 6149 struct scsi_mode_select_6 *cdb; 6150 6151 cdb = (struct scsi_mode_select_6 *)ctsio->cdb; 6152 6153 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6154 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6155 param_len = cdb->length; 6156 header_size = sizeof(struct scsi_mode_header_6); 6157 break; 6158 } 6159 case MODE_SELECT_10: { 6160 struct scsi_mode_select_10 *cdb; 6161 6162 cdb = (struct scsi_mode_select_10 *)ctsio->cdb; 6163 6164 pf = (cdb->byte2 & SMS_PF) ? 1 : 0; 6165 sp = (cdb->byte2 & SMS_SP) ? 1 : 0; 6166 param_len = scsi_2btoul(cdb->length); 6167 header_size = sizeof(struct scsi_mode_header_10); 6168 break; 6169 } 6170 default: 6171 ctl_set_invalid_opcode(ctsio); 6172 ctl_done((union ctl_io *)ctsio); 6173 return (CTL_RETVAL_COMPLETE); 6174 } 6175 6176 /* 6177 * From SPC-3: 6178 * "A parameter list length of zero indicates that the Data-Out Buffer 6179 * shall be empty. This condition shall not be considered as an error." 6180 */ 6181 if (param_len == 0) { 6182 ctl_set_success(ctsio); 6183 ctl_done((union ctl_io *)ctsio); 6184 return (CTL_RETVAL_COMPLETE); 6185 } 6186 6187 /* 6188 * Since we'll hit this the first time through, prior to 6189 * allocation, we don't need to free a data buffer here. 6190 */ 6191 if (param_len < header_size) { 6192 ctl_set_param_len_error(ctsio); 6193 ctl_done((union ctl_io *)ctsio); 6194 return (CTL_RETVAL_COMPLETE); 6195 } 6196 6197 /* 6198 * Allocate the data buffer and grab the user's data. In theory, 6199 * we shouldn't have to sanity check the parameter list length here 6200 * because the maximum size is 64K. We should be able to malloc 6201 * that much without too many problems. 6202 */ 6203 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 6204 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 6205 ctsio->kern_data_len = param_len; 6206 ctsio->kern_total_len = param_len; 6207 ctsio->kern_data_resid = 0; 6208 ctsio->kern_rel_offset = 0; 6209 ctsio->kern_sg_entries = 0; 6210 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6211 ctsio->be_move_done = ctl_config_move_done; 6212 ctl_datamove((union ctl_io *)ctsio); 6213 6214 return (CTL_RETVAL_COMPLETE); 6215 } 6216 6217 switch (ctsio->cdb[0]) { 6218 case MODE_SELECT_6: { 6219 struct scsi_mode_header_6 *mh6; 6220 6221 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr; 6222 bd_len = mh6->blk_desc_len; 6223 break; 6224 } 6225 case MODE_SELECT_10: { 6226 struct scsi_mode_header_10 *mh10; 6227 6228 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr; 6229 bd_len = scsi_2btoul(mh10->blk_desc_len); 6230 break; 6231 } 6232 default: 6233 panic("Invalid CDB type %#x", ctsio->cdb[0]); 6234 break; 6235 } 6236 6237 if (param_len < (header_size + bd_len)) { 6238 free(ctsio->kern_data_ptr, M_CTL); 6239 ctl_set_param_len_error(ctsio); 6240 ctl_done((union ctl_io *)ctsio); 6241 return (CTL_RETVAL_COMPLETE); 6242 } 6243 6244 /* 6245 * Set the IO_CONT flag, so that if this I/O gets passed to 6246 * ctl_config_write_done(), it'll get passed back to 6247 * ctl_do_mode_select() for further processing, or completion if 6248 * we're all done. 6249 */ 6250 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 6251 ctsio->io_cont = ctl_do_mode_select; 6252 6253 modepage_info = (union ctl_modepage_info *) 6254 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes; 6255 memset(modepage_info, 0, sizeof(*modepage_info)); 6256 modepage_info->header.len_left = param_len - header_size - bd_len; 6257 modepage_info->header.len_used = header_size + bd_len; 6258 6259 return (ctl_do_mode_select((union ctl_io *)ctsio)); 6260} 6261 6262int 6263ctl_mode_sense(struct ctl_scsiio *ctsio) 6264{ 6265 struct ctl_lun *lun; 6266 int pc, page_code, dbd, llba, subpage; 6267 int alloc_len, page_len, header_len, total_len; 6268 struct scsi_mode_block_descr *block_desc; 6269 struct ctl_page_index *page_index; 6270 6271 dbd = 0; 6272 llba = 0; 6273 block_desc = NULL; 6274 6275 CTL_DEBUG_PRINT(("ctl_mode_sense\n")); 6276 6277 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6278 switch (ctsio->cdb[0]) { 6279 case MODE_SENSE_6: { 6280 struct scsi_mode_sense_6 *cdb; 6281 6282 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb; 6283 6284 header_len = sizeof(struct scsi_mode_hdr_6); 6285 if (cdb->byte2 & SMS_DBD) 6286 dbd = 1; 6287 else 6288 header_len += sizeof(struct scsi_mode_block_descr); 6289 6290 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6291 page_code = cdb->page & SMS_PAGE_CODE; 6292 subpage = cdb->subpage; 6293 alloc_len = cdb->length; 6294 break; 6295 } 6296 case MODE_SENSE_10: { 6297 struct scsi_mode_sense_10 *cdb; 6298 6299 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb; 6300 6301 header_len = sizeof(struct scsi_mode_hdr_10); 6302 6303 if (cdb->byte2 & SMS_DBD) 6304 dbd = 1; 6305 else 6306 header_len += sizeof(struct scsi_mode_block_descr); 6307 if (cdb->byte2 & SMS10_LLBAA) 6308 llba = 1; 6309 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6; 6310 page_code = cdb->page & SMS_PAGE_CODE; 6311 subpage = cdb->subpage; 6312 alloc_len = scsi_2btoul(cdb->length); 6313 break; 6314 } 6315 default: 6316 ctl_set_invalid_opcode(ctsio); 6317 ctl_done((union ctl_io *)ctsio); 6318 return (CTL_RETVAL_COMPLETE); 6319 break; /* NOTREACHED */ 6320 } 6321 6322 /* 6323 * We have to make a first pass through to calculate the size of 6324 * the pages that match the user's query. Then we allocate enough 6325 * memory to hold it, and actually copy the data into the buffer. 6326 */ 6327 switch (page_code) { 6328 case SMS_ALL_PAGES_PAGE: { 6329 int i; 6330 6331 page_len = 0; 6332 6333 /* 6334 * At the moment, values other than 0 and 0xff here are 6335 * reserved according to SPC-3. 6336 */ 6337 if ((subpage != SMS_SUBPAGE_PAGE_0) 6338 && (subpage != SMS_SUBPAGE_ALL)) { 6339 ctl_set_invalid_field(ctsio, 6340 /*sks_valid*/ 1, 6341 /*command*/ 1, 6342 /*field*/ 3, 6343 /*bit_valid*/ 0, 6344 /*bit*/ 0); 6345 ctl_done((union ctl_io *)ctsio); 6346 return (CTL_RETVAL_COMPLETE); 6347 } 6348 6349 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6350 page_index = &lun->mode_pages.index[i]; 6351 6352 /* Make sure the page is supported for this dev type */ 6353 if (lun->be_lun->lun_type == T_DIRECT && 6354 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6355 continue; 6356 if (lun->be_lun->lun_type == T_PROCESSOR && 6357 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6358 continue; 6359 if (lun->be_lun->lun_type == T_CDROM && 6360 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6361 continue; 6362 6363 /* 6364 * We don't use this subpage if the user didn't 6365 * request all subpages. 6366 */ 6367 if ((page_index->subpage != 0) 6368 && (subpage == SMS_SUBPAGE_PAGE_0)) 6369 continue; 6370 6371#if 0 6372 printf("found page %#x len %d\n", 6373 page_index->page_code & SMPH_PC_MASK, 6374 page_index->page_len); 6375#endif 6376 page_len += page_index->page_len; 6377 } 6378 break; 6379 } 6380 default: { 6381 int i; 6382 6383 page_len = 0; 6384 6385 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6386 page_index = &lun->mode_pages.index[i]; 6387 6388 /* Make sure the page is supported for this dev type */ 6389 if (lun->be_lun->lun_type == T_DIRECT && 6390 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6391 continue; 6392 if (lun->be_lun->lun_type == T_PROCESSOR && 6393 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6394 continue; 6395 if (lun->be_lun->lun_type == T_CDROM && 6396 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6397 continue; 6398 6399 /* Look for the right page code */ 6400 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6401 continue; 6402 6403 /* Look for the right subpage or the subpage wildcard*/ 6404 if ((page_index->subpage != subpage) 6405 && (subpage != SMS_SUBPAGE_ALL)) 6406 continue; 6407 6408#if 0 6409 printf("found page %#x len %d\n", 6410 page_index->page_code & SMPH_PC_MASK, 6411 page_index->page_len); 6412#endif 6413 6414 page_len += page_index->page_len; 6415 } 6416 6417 if (page_len == 0) { 6418 ctl_set_invalid_field(ctsio, 6419 /*sks_valid*/ 1, 6420 /*command*/ 1, 6421 /*field*/ 2, 6422 /*bit_valid*/ 1, 6423 /*bit*/ 5); 6424 ctl_done((union ctl_io *)ctsio); 6425 return (CTL_RETVAL_COMPLETE); 6426 } 6427 break; 6428 } 6429 } 6430 6431 total_len = header_len + page_len; 6432#if 0 6433 printf("header_len = %d, page_len = %d, total_len = %d\n", 6434 header_len, page_len, total_len); 6435#endif 6436 6437 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6438 ctsio->kern_sg_entries = 0; 6439 ctsio->kern_data_resid = 0; 6440 ctsio->kern_rel_offset = 0; 6441 if (total_len < alloc_len) { 6442 ctsio->residual = alloc_len - total_len; 6443 ctsio->kern_data_len = total_len; 6444 ctsio->kern_total_len = total_len; 6445 } else { 6446 ctsio->residual = 0; 6447 ctsio->kern_data_len = alloc_len; 6448 ctsio->kern_total_len = alloc_len; 6449 } 6450 6451 switch (ctsio->cdb[0]) { 6452 case MODE_SENSE_6: { 6453 struct scsi_mode_hdr_6 *header; 6454 6455 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr; 6456 6457 header->datalen = MIN(total_len - 1, 254); 6458 if (lun->be_lun->lun_type == T_DIRECT) { 6459 header->dev_specific = 0x10; /* DPOFUA */ 6460 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6461 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6462 .eca_and_aen & SCP_SWP) != 0) 6463 header->dev_specific |= 0x80; /* WP */ 6464 } 6465 if (dbd) 6466 header->block_descr_len = 0; 6467 else 6468 header->block_descr_len = 6469 sizeof(struct scsi_mode_block_descr); 6470 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6471 break; 6472 } 6473 case MODE_SENSE_10: { 6474 struct scsi_mode_hdr_10 *header; 6475 int datalen; 6476 6477 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr; 6478 6479 datalen = MIN(total_len - 2, 65533); 6480 scsi_ulto2b(datalen, header->datalen); 6481 if (lun->be_lun->lun_type == T_DIRECT) { 6482 header->dev_specific = 0x10; /* DPOFUA */ 6483 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) || 6484 (lun->mode_pages.control_page[CTL_PAGE_CURRENT] 6485 .eca_and_aen & SCP_SWP) != 0) 6486 header->dev_specific |= 0x80; /* WP */ 6487 } 6488 if (dbd) 6489 scsi_ulto2b(0, header->block_descr_len); 6490 else 6491 scsi_ulto2b(sizeof(struct scsi_mode_block_descr), 6492 header->block_descr_len); 6493 block_desc = (struct scsi_mode_block_descr *)&header[1]; 6494 break; 6495 } 6496 default: 6497 panic("invalid CDB type %#x", ctsio->cdb[0]); 6498 break; /* NOTREACHED */ 6499 } 6500 6501 /* 6502 * If we've got a disk, use its blocksize in the block 6503 * descriptor. Otherwise, just set it to 0. 6504 */ 6505 if (dbd == 0) { 6506 if (lun->be_lun->lun_type == T_DIRECT) 6507 scsi_ulto3b(lun->be_lun->blocksize, 6508 block_desc->block_len); 6509 else 6510 scsi_ulto3b(0, block_desc->block_len); 6511 } 6512 6513 switch (page_code) { 6514 case SMS_ALL_PAGES_PAGE: { 6515 int i, data_used; 6516 6517 data_used = header_len; 6518 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6519 struct ctl_page_index *page_index; 6520 6521 page_index = &lun->mode_pages.index[i]; 6522 if (lun->be_lun->lun_type == T_DIRECT && 6523 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6524 continue; 6525 if (lun->be_lun->lun_type == T_PROCESSOR && 6526 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6527 continue; 6528 if (lun->be_lun->lun_type == T_CDROM && 6529 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6530 continue; 6531 6532 /* 6533 * We don't use this subpage if the user didn't 6534 * request all subpages. We already checked (above) 6535 * to make sure the user only specified a subpage 6536 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case. 6537 */ 6538 if ((page_index->subpage != 0) 6539 && (subpage == SMS_SUBPAGE_PAGE_0)) 6540 continue; 6541 6542 /* 6543 * Call the handler, if it exists, to update the 6544 * page to the latest values. 6545 */ 6546 if (page_index->sense_handler != NULL) 6547 page_index->sense_handler(ctsio, page_index,pc); 6548 6549 memcpy(ctsio->kern_data_ptr + data_used, 6550 page_index->page_data + 6551 (page_index->page_len * pc), 6552 page_index->page_len); 6553 data_used += page_index->page_len; 6554 } 6555 break; 6556 } 6557 default: { 6558 int i, data_used; 6559 6560 data_used = header_len; 6561 6562 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) { 6563 struct ctl_page_index *page_index; 6564 6565 page_index = &lun->mode_pages.index[i]; 6566 6567 /* Look for the right page code */ 6568 if ((page_index->page_code & SMPH_PC_MASK) != page_code) 6569 continue; 6570 6571 /* Look for the right subpage or the subpage wildcard*/ 6572 if ((page_index->subpage != subpage) 6573 && (subpage != SMS_SUBPAGE_ALL)) 6574 continue; 6575 6576 /* Make sure the page is supported for this dev type */ 6577 if (lun->be_lun->lun_type == T_DIRECT && 6578 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0) 6579 continue; 6580 if (lun->be_lun->lun_type == T_PROCESSOR && 6581 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0) 6582 continue; 6583 if (lun->be_lun->lun_type == T_CDROM && 6584 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0) 6585 continue; 6586 6587 /* 6588 * Call the handler, if it exists, to update the 6589 * page to the latest values. 6590 */ 6591 if (page_index->sense_handler != NULL) 6592 page_index->sense_handler(ctsio, page_index,pc); 6593 6594 memcpy(ctsio->kern_data_ptr + data_used, 6595 page_index->page_data + 6596 (page_index->page_len * pc), 6597 page_index->page_len); 6598 data_used += page_index->page_len; 6599 } 6600 break; 6601 } 6602 } 6603 6604 ctl_set_success(ctsio); 6605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6606 ctsio->be_move_done = ctl_config_move_done; 6607 ctl_datamove((union ctl_io *)ctsio); 6608 return (CTL_RETVAL_COMPLETE); 6609} 6610 6611int 6612ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio, 6613 struct ctl_page_index *page_index, 6614 int pc) 6615{ 6616 struct ctl_lun *lun; 6617 struct scsi_log_param_header *phdr; 6618 uint8_t *data; 6619 uint64_t val; 6620 6621 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6622 data = page_index->page_data; 6623 6624 if (lun->backend->lun_attr != NULL && 6625 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail")) 6626 != UINT64_MAX) { 6627 phdr = (struct scsi_log_param_header *)data; 6628 scsi_ulto2b(0x0001, phdr->param_code); 6629 phdr->param_control = SLP_LBIN | SLP_LP; 6630 phdr->param_len = 8; 6631 data = (uint8_t *)(phdr + 1); 6632 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6633 data[4] = 0x02; /* per-pool */ 6634 data += phdr->param_len; 6635 } 6636 6637 if (lun->backend->lun_attr != NULL && 6638 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused")) 6639 != UINT64_MAX) { 6640 phdr = (struct scsi_log_param_header *)data; 6641 scsi_ulto2b(0x0002, phdr->param_code); 6642 phdr->param_control = SLP_LBIN | SLP_LP; 6643 phdr->param_len = 8; 6644 data = (uint8_t *)(phdr + 1); 6645 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6646 data[4] = 0x01; /* per-LUN */ 6647 data += phdr->param_len; 6648 } 6649 6650 if (lun->backend->lun_attr != NULL && 6651 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail")) 6652 != UINT64_MAX) { 6653 phdr = (struct scsi_log_param_header *)data; 6654 scsi_ulto2b(0x00f1, phdr->param_code); 6655 phdr->param_control = SLP_LBIN | SLP_LP; 6656 phdr->param_len = 8; 6657 data = (uint8_t *)(phdr + 1); 6658 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6659 data[4] = 0x02; /* per-pool */ 6660 data += phdr->param_len; 6661 } 6662 6663 if (lun->backend->lun_attr != NULL && 6664 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused")) 6665 != UINT64_MAX) { 6666 phdr = (struct scsi_log_param_header *)data; 6667 scsi_ulto2b(0x00f2, phdr->param_code); 6668 phdr->param_control = SLP_LBIN | SLP_LP; 6669 phdr->param_len = 8; 6670 data = (uint8_t *)(phdr + 1); 6671 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data); 6672 data[4] = 0x02; /* per-pool */ 6673 data += phdr->param_len; 6674 } 6675 6676 page_index->page_len = data - page_index->page_data; 6677 return (0); 6678} 6679 6680int 6681ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio, 6682 struct ctl_page_index *page_index, 6683 int pc) 6684{ 6685 struct ctl_lun *lun; 6686 struct stat_page *data; 6687 uint64_t rn, wn, rb, wb; 6688 struct bintime rt, wt; 6689 int i; 6690 6691 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6692 data = (struct stat_page *)page_index->page_data; 6693 6694 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code); 6695 data->sap.hdr.param_control = SLP_LBIN; 6696 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) - 6697 sizeof(struct scsi_log_param_header); 6698 rn = wn = rb = wb = 0; 6699 bintime_clear(&rt); 6700 bintime_clear(&wt); 6701 for (i = 0; i < CTL_MAX_PORTS; i++) { 6702 rn += lun->stats.ports[i].operations[CTL_STATS_READ]; 6703 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE]; 6704 rb += lun->stats.ports[i].bytes[CTL_STATS_READ]; 6705 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE]; 6706 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]); 6707 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]); 6708 } 6709 scsi_u64to8b(rn, data->sap.read_num); 6710 scsi_u64to8b(wn, data->sap.write_num); 6711 if (lun->stats.blocksize > 0) { 6712 scsi_u64to8b(wb / lun->stats.blocksize, 6713 data->sap.recvieved_lba); 6714 scsi_u64to8b(rb / lun->stats.blocksize, 6715 data->sap.transmitted_lba); 6716 } 6717 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000), 6718 data->sap.read_int); 6719 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000), 6720 data->sap.write_int); 6721 scsi_u64to8b(0, data->sap.weighted_num); 6722 scsi_u64to8b(0, data->sap.weighted_int); 6723 scsi_ulto2b(SLP_IT, data->it.hdr.param_code); 6724 data->it.hdr.param_control = SLP_LBIN; 6725 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) - 6726 sizeof(struct scsi_log_param_header); 6727#ifdef CTL_TIME_IO 6728 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int); 6729#endif 6730 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code); 6731 data->it.hdr.param_control = SLP_LBIN; 6732 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) - 6733 sizeof(struct scsi_log_param_header); 6734 scsi_ulto4b(3, data->ti.exponent); 6735 scsi_ulto4b(1, data->ti.integer); 6736 6737 page_index->page_len = sizeof(*data); 6738 return (0); 6739} 6740 6741int 6742ctl_log_sense(struct ctl_scsiio *ctsio) 6743{ 6744 struct ctl_lun *lun; 6745 int i, pc, page_code, subpage; 6746 int alloc_len, total_len; 6747 struct ctl_page_index *page_index; 6748 struct scsi_log_sense *cdb; 6749 struct scsi_log_header *header; 6750 6751 CTL_DEBUG_PRINT(("ctl_log_sense\n")); 6752 6753 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6754 cdb = (struct scsi_log_sense *)ctsio->cdb; 6755 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6; 6756 page_code = cdb->page & SLS_PAGE_CODE; 6757 subpage = cdb->subpage; 6758 alloc_len = scsi_2btoul(cdb->length); 6759 6760 page_index = NULL; 6761 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) { 6762 page_index = &lun->log_pages.index[i]; 6763 6764 /* Look for the right page code */ 6765 if ((page_index->page_code & SL_PAGE_CODE) != page_code) 6766 continue; 6767 6768 /* Look for the right subpage or the subpage wildcard*/ 6769 if (page_index->subpage != subpage) 6770 continue; 6771 6772 break; 6773 } 6774 if (i >= CTL_NUM_LOG_PAGES) { 6775 ctl_set_invalid_field(ctsio, 6776 /*sks_valid*/ 1, 6777 /*command*/ 1, 6778 /*field*/ 2, 6779 /*bit_valid*/ 0, 6780 /*bit*/ 0); 6781 ctl_done((union ctl_io *)ctsio); 6782 return (CTL_RETVAL_COMPLETE); 6783 } 6784 6785 total_len = sizeof(struct scsi_log_header) + page_index->page_len; 6786 6787 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6788 ctsio->kern_sg_entries = 0; 6789 ctsio->kern_data_resid = 0; 6790 ctsio->kern_rel_offset = 0; 6791 if (total_len < alloc_len) { 6792 ctsio->residual = alloc_len - total_len; 6793 ctsio->kern_data_len = total_len; 6794 ctsio->kern_total_len = total_len; 6795 } else { 6796 ctsio->residual = 0; 6797 ctsio->kern_data_len = alloc_len; 6798 ctsio->kern_total_len = alloc_len; 6799 } 6800 6801 header = (struct scsi_log_header *)ctsio->kern_data_ptr; 6802 header->page = page_index->page_code; 6803 if (page_index->subpage) { 6804 header->page |= SL_SPF; 6805 header->subpage = page_index->subpage; 6806 } 6807 scsi_ulto2b(page_index->page_len, header->datalen); 6808 6809 /* 6810 * Call the handler, if it exists, to update the 6811 * page to the latest values. 6812 */ 6813 if (page_index->sense_handler != NULL) 6814 page_index->sense_handler(ctsio, page_index, pc); 6815 6816 memcpy(header + 1, page_index->page_data, page_index->page_len); 6817 6818 ctl_set_success(ctsio); 6819 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6820 ctsio->be_move_done = ctl_config_move_done; 6821 ctl_datamove((union ctl_io *)ctsio); 6822 return (CTL_RETVAL_COMPLETE); 6823} 6824 6825int 6826ctl_read_capacity(struct ctl_scsiio *ctsio) 6827{ 6828 struct scsi_read_capacity *cdb; 6829 struct scsi_read_capacity_data *data; 6830 struct ctl_lun *lun; 6831 uint32_t lba; 6832 6833 CTL_DEBUG_PRINT(("ctl_read_capacity\n")); 6834 6835 cdb = (struct scsi_read_capacity *)ctsio->cdb; 6836 6837 lba = scsi_4btoul(cdb->addr); 6838 if (((cdb->pmi & SRC_PMI) == 0) 6839 && (lba != 0)) { 6840 ctl_set_invalid_field(/*ctsio*/ ctsio, 6841 /*sks_valid*/ 1, 6842 /*command*/ 1, 6843 /*field*/ 2, 6844 /*bit_valid*/ 0, 6845 /*bit*/ 0); 6846 ctl_done((union ctl_io *)ctsio); 6847 return (CTL_RETVAL_COMPLETE); 6848 } 6849 6850 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6851 6852 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6853 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr; 6854 ctsio->residual = 0; 6855 ctsio->kern_data_len = sizeof(*data); 6856 ctsio->kern_total_len = sizeof(*data); 6857 ctsio->kern_data_resid = 0; 6858 ctsio->kern_rel_offset = 0; 6859 ctsio->kern_sg_entries = 0; 6860 6861 /* 6862 * If the maximum LBA is greater than 0xfffffffe, the user must 6863 * issue a SERVICE ACTION IN (16) command, with the read capacity 6864 * serivce action set. 6865 */ 6866 if (lun->be_lun->maxlba > 0xfffffffe) 6867 scsi_ulto4b(0xffffffff, data->addr); 6868 else 6869 scsi_ulto4b(lun->be_lun->maxlba, data->addr); 6870 6871 /* 6872 * XXX KDM this may not be 512 bytes... 6873 */ 6874 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6875 6876 ctl_set_success(ctsio); 6877 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6878 ctsio->be_move_done = ctl_config_move_done; 6879 ctl_datamove((union ctl_io *)ctsio); 6880 return (CTL_RETVAL_COMPLETE); 6881} 6882 6883int 6884ctl_read_capacity_16(struct ctl_scsiio *ctsio) 6885{ 6886 struct scsi_read_capacity_16 *cdb; 6887 struct scsi_read_capacity_data_long *data; 6888 struct ctl_lun *lun; 6889 uint64_t lba; 6890 uint32_t alloc_len; 6891 6892 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n")); 6893 6894 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb; 6895 6896 alloc_len = scsi_4btoul(cdb->alloc_len); 6897 lba = scsi_8btou64(cdb->addr); 6898 6899 if ((cdb->reladr & SRC16_PMI) 6900 && (lba != 0)) { 6901 ctl_set_invalid_field(/*ctsio*/ ctsio, 6902 /*sks_valid*/ 1, 6903 /*command*/ 1, 6904 /*field*/ 2, 6905 /*bit_valid*/ 0, 6906 /*bit*/ 0); 6907 ctl_done((union ctl_io *)ctsio); 6908 return (CTL_RETVAL_COMPLETE); 6909 } 6910 6911 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6912 6913 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO); 6914 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr; 6915 6916 if (sizeof(*data) < alloc_len) { 6917 ctsio->residual = alloc_len - sizeof(*data); 6918 ctsio->kern_data_len = sizeof(*data); 6919 ctsio->kern_total_len = sizeof(*data); 6920 } else { 6921 ctsio->residual = 0; 6922 ctsio->kern_data_len = alloc_len; 6923 ctsio->kern_total_len = alloc_len; 6924 } 6925 ctsio->kern_data_resid = 0; 6926 ctsio->kern_rel_offset = 0; 6927 ctsio->kern_sg_entries = 0; 6928 6929 scsi_u64to8b(lun->be_lun->maxlba, data->addr); 6930 /* XXX KDM this may not be 512 bytes... */ 6931 scsi_ulto4b(lun->be_lun->blocksize, data->length); 6932 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE; 6933 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp); 6934 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) 6935 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ; 6936 6937 ctl_set_success(ctsio); 6938 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6939 ctsio->be_move_done = ctl_config_move_done; 6940 ctl_datamove((union ctl_io *)ctsio); 6941 return (CTL_RETVAL_COMPLETE); 6942} 6943 6944int 6945ctl_get_lba_status(struct ctl_scsiio *ctsio) 6946{ 6947 struct scsi_get_lba_status *cdb; 6948 struct scsi_get_lba_status_data *data; 6949 struct ctl_lun *lun; 6950 struct ctl_lba_len_flags *lbalen; 6951 uint64_t lba; 6952 uint32_t alloc_len, total_len; 6953 int retval; 6954 6955 CTL_DEBUG_PRINT(("ctl_get_lba_status\n")); 6956 6957 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 6958 cdb = (struct scsi_get_lba_status *)ctsio->cdb; 6959 lba = scsi_8btou64(cdb->addr); 6960 alloc_len = scsi_4btoul(cdb->alloc_len); 6961 6962 if (lba > lun->be_lun->maxlba) { 6963 ctl_set_lba_out_of_range(ctsio); 6964 ctl_done((union ctl_io *)ctsio); 6965 return (CTL_RETVAL_COMPLETE); 6966 } 6967 6968 total_len = sizeof(*data) + sizeof(data->descr[0]); 6969 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 6970 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr; 6971 6972 if (total_len < alloc_len) { 6973 ctsio->residual = alloc_len - total_len; 6974 ctsio->kern_data_len = total_len; 6975 ctsio->kern_total_len = total_len; 6976 } else { 6977 ctsio->residual = 0; 6978 ctsio->kern_data_len = alloc_len; 6979 ctsio->kern_total_len = alloc_len; 6980 } 6981 ctsio->kern_data_resid = 0; 6982 ctsio->kern_rel_offset = 0; 6983 ctsio->kern_sg_entries = 0; 6984 6985 /* Fill dummy data in case backend can't tell anything. */ 6986 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length); 6987 scsi_u64to8b(lba, data->descr[0].addr); 6988 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba), 6989 data->descr[0].length); 6990 data->descr[0].status = 0; /* Mapped or unknown. */ 6991 6992 ctl_set_success(ctsio); 6993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 6994 ctsio->be_move_done = ctl_config_move_done; 6995 6996 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 6997 lbalen->lba = lba; 6998 lbalen->len = total_len; 6999 lbalen->flags = 0; 7000 retval = lun->backend->config_read((union ctl_io *)ctsio); 7001 return (CTL_RETVAL_COMPLETE); 7002} 7003 7004int 7005ctl_read_defect(struct ctl_scsiio *ctsio) 7006{ 7007 struct scsi_read_defect_data_10 *ccb10; 7008 struct scsi_read_defect_data_12 *ccb12; 7009 struct scsi_read_defect_data_hdr_10 *data10; 7010 struct scsi_read_defect_data_hdr_12 *data12; 7011 uint32_t alloc_len, data_len; 7012 uint8_t format; 7013 7014 CTL_DEBUG_PRINT(("ctl_read_defect\n")); 7015 7016 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7017 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb; 7018 format = ccb10->format; 7019 alloc_len = scsi_2btoul(ccb10->alloc_length); 7020 data_len = sizeof(*data10); 7021 } else { 7022 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb; 7023 format = ccb12->format; 7024 alloc_len = scsi_4btoul(ccb12->alloc_length); 7025 data_len = sizeof(*data12); 7026 } 7027 if (alloc_len == 0) { 7028 ctl_set_success(ctsio); 7029 ctl_done((union ctl_io *)ctsio); 7030 return (CTL_RETVAL_COMPLETE); 7031 } 7032 7033 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 7034 if (data_len < alloc_len) { 7035 ctsio->residual = alloc_len - data_len; 7036 ctsio->kern_data_len = data_len; 7037 ctsio->kern_total_len = data_len; 7038 } else { 7039 ctsio->residual = 0; 7040 ctsio->kern_data_len = alloc_len; 7041 ctsio->kern_total_len = alloc_len; 7042 } 7043 ctsio->kern_data_resid = 0; 7044 ctsio->kern_rel_offset = 0; 7045 ctsio->kern_sg_entries = 0; 7046 7047 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) { 7048 data10 = (struct scsi_read_defect_data_hdr_10 *) 7049 ctsio->kern_data_ptr; 7050 data10->format = format; 7051 scsi_ulto2b(0, data10->length); 7052 } else { 7053 data12 = (struct scsi_read_defect_data_hdr_12 *) 7054 ctsio->kern_data_ptr; 7055 data12->format = format; 7056 scsi_ulto2b(0, data12->generation); 7057 scsi_ulto4b(0, data12->length); 7058 } 7059 7060 ctl_set_success(ctsio); 7061 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7062 ctsio->be_move_done = ctl_config_move_done; 7063 ctl_datamove((union ctl_io *)ctsio); 7064 return (CTL_RETVAL_COMPLETE); 7065} 7066 7067int 7068ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio) 7069{ 7070 struct scsi_maintenance_in *cdb; 7071 int retval; 7072 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os; 7073 int num_target_port_groups, num_target_ports; 7074 struct ctl_lun *lun; 7075 struct ctl_softc *softc; 7076 struct ctl_port *port; 7077 struct scsi_target_group_data *rtg_ptr; 7078 struct scsi_target_group_data_extended *rtg_ext_ptr; 7079 struct scsi_target_port_group_descriptor *tpg_desc; 7080 7081 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n")); 7082 7083 cdb = (struct scsi_maintenance_in *)ctsio->cdb; 7084 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7085 softc = lun->ctl_softc; 7086 7087 retval = CTL_RETVAL_COMPLETE; 7088 7089 switch (cdb->byte2 & STG_PDF_MASK) { 7090 case STG_PDF_LENGTH: 7091 ext = 0; 7092 break; 7093 case STG_PDF_EXTENDED: 7094 ext = 1; 7095 break; 7096 default: 7097 ctl_set_invalid_field(/*ctsio*/ ctsio, 7098 /*sks_valid*/ 1, 7099 /*command*/ 1, 7100 /*field*/ 2, 7101 /*bit_valid*/ 1, 7102 /*bit*/ 5); 7103 ctl_done((union ctl_io *)ctsio); 7104 return(retval); 7105 } 7106 7107 if (softc->is_single) 7108 num_target_port_groups = 1; 7109 else 7110 num_target_port_groups = NUM_TARGET_PORT_GROUPS; 7111 num_target_ports = 0; 7112 mtx_lock(&softc->ctl_lock); 7113 STAILQ_FOREACH(port, &softc->port_list, links) { 7114 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7115 continue; 7116 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7117 continue; 7118 num_target_ports++; 7119 } 7120 mtx_unlock(&softc->ctl_lock); 7121 7122 if (ext) 7123 total_len = sizeof(struct scsi_target_group_data_extended); 7124 else 7125 total_len = sizeof(struct scsi_target_group_data); 7126 total_len += sizeof(struct scsi_target_port_group_descriptor) * 7127 num_target_port_groups + 7128 sizeof(struct scsi_target_port_descriptor) * num_target_ports; 7129 7130 alloc_len = scsi_4btoul(cdb->length); 7131 7132 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7133 7134 ctsio->kern_sg_entries = 0; 7135 7136 if (total_len < alloc_len) { 7137 ctsio->residual = alloc_len - total_len; 7138 ctsio->kern_data_len = total_len; 7139 ctsio->kern_total_len = total_len; 7140 } else { 7141 ctsio->residual = 0; 7142 ctsio->kern_data_len = alloc_len; 7143 ctsio->kern_total_len = alloc_len; 7144 } 7145 ctsio->kern_data_resid = 0; 7146 ctsio->kern_rel_offset = 0; 7147 7148 if (ext) { 7149 rtg_ext_ptr = (struct scsi_target_group_data_extended *) 7150 ctsio->kern_data_ptr; 7151 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length); 7152 rtg_ext_ptr->format_type = 0x10; 7153 rtg_ext_ptr->implicit_transition_time = 0; 7154 tpg_desc = &rtg_ext_ptr->groups[0]; 7155 } else { 7156 rtg_ptr = (struct scsi_target_group_data *) 7157 ctsio->kern_data_ptr; 7158 scsi_ulto4b(total_len - 4, rtg_ptr->length); 7159 tpg_desc = &rtg_ptr->groups[0]; 7160 } 7161 7162 mtx_lock(&softc->ctl_lock); 7163 pg = softc->port_min / softc->port_cnt; 7164 if (softc->ha_link == CTL_HA_LINK_OFFLINE) 7165 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE; 7166 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) 7167 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING; 7168 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) 7169 gs = TPG_ASYMMETRIC_ACCESS_STANDBY; 7170 else 7171 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED; 7172 if (lun->flags & CTL_LUN_PRIMARY_SC) { 7173 os = gs; 7174 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7175 } else 7176 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED; 7177 for (g = 0; g < num_target_port_groups; g++) { 7178 tpg_desc->pref_state = (g == pg) ? gs : os; 7179 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP | 7180 TPG_U_SUP | TPG_T_SUP; 7181 scsi_ulto2b(g + 1, tpg_desc->target_port_group); 7182 tpg_desc->status = TPG_IMPLICIT; 7183 pc = 0; 7184 STAILQ_FOREACH(port, &softc->port_list, links) { 7185 if (port->targ_port < g * softc->port_cnt || 7186 port->targ_port >= (g + 1) * softc->port_cnt) 7187 continue; 7188 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 7189 continue; 7190 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 7191 continue; 7192 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc]. 7193 relative_target_port_identifier); 7194 pc++; 7195 } 7196 tpg_desc->target_port_count = pc; 7197 tpg_desc = (struct scsi_target_port_group_descriptor *) 7198 &tpg_desc->descriptors[pc]; 7199 } 7200 mtx_unlock(&softc->ctl_lock); 7201 7202 ctl_set_success(ctsio); 7203 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7204 ctsio->be_move_done = ctl_config_move_done; 7205 ctl_datamove((union ctl_io *)ctsio); 7206 return(retval); 7207} 7208 7209int 7210ctl_report_supported_opcodes(struct ctl_scsiio *ctsio) 7211{ 7212 struct ctl_lun *lun; 7213 struct scsi_report_supported_opcodes *cdb; 7214 const struct ctl_cmd_entry *entry, *sentry; 7215 struct scsi_report_supported_opcodes_all *all; 7216 struct scsi_report_supported_opcodes_descr *descr; 7217 struct scsi_report_supported_opcodes_one *one; 7218 int retval; 7219 int alloc_len, total_len; 7220 int opcode, service_action, i, j, num; 7221 7222 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n")); 7223 7224 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb; 7225 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7226 7227 retval = CTL_RETVAL_COMPLETE; 7228 7229 opcode = cdb->requested_opcode; 7230 service_action = scsi_2btoul(cdb->requested_service_action); 7231 switch (cdb->options & RSO_OPTIONS_MASK) { 7232 case RSO_OPTIONS_ALL: 7233 num = 0; 7234 for (i = 0; i < 256; i++) { 7235 entry = &ctl_cmd_table[i]; 7236 if (entry->flags & CTL_CMD_FLAG_SA5) { 7237 for (j = 0; j < 32; j++) { 7238 sentry = &((const struct ctl_cmd_entry *) 7239 entry->execute)[j]; 7240 if (ctl_cmd_applicable( 7241 lun->be_lun->lun_type, sentry)) 7242 num++; 7243 } 7244 } else { 7245 if (ctl_cmd_applicable(lun->be_lun->lun_type, 7246 entry)) 7247 num++; 7248 } 7249 } 7250 total_len = sizeof(struct scsi_report_supported_opcodes_all) + 7251 num * sizeof(struct scsi_report_supported_opcodes_descr); 7252 break; 7253 case RSO_OPTIONS_OC: 7254 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) { 7255 ctl_set_invalid_field(/*ctsio*/ ctsio, 7256 /*sks_valid*/ 1, 7257 /*command*/ 1, 7258 /*field*/ 2, 7259 /*bit_valid*/ 1, 7260 /*bit*/ 2); 7261 ctl_done((union ctl_io *)ctsio); 7262 return (CTL_RETVAL_COMPLETE); 7263 } 7264 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7265 break; 7266 case RSO_OPTIONS_OC_SA: 7267 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 || 7268 service_action >= 32) { 7269 ctl_set_invalid_field(/*ctsio*/ ctsio, 7270 /*sks_valid*/ 1, 7271 /*command*/ 1, 7272 /*field*/ 2, 7273 /*bit_valid*/ 1, 7274 /*bit*/ 2); 7275 ctl_done((union ctl_io *)ctsio); 7276 return (CTL_RETVAL_COMPLETE); 7277 } 7278 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32; 7279 break; 7280 default: 7281 ctl_set_invalid_field(/*ctsio*/ ctsio, 7282 /*sks_valid*/ 1, 7283 /*command*/ 1, 7284 /*field*/ 2, 7285 /*bit_valid*/ 1, 7286 /*bit*/ 2); 7287 ctl_done((union ctl_io *)ctsio); 7288 return (CTL_RETVAL_COMPLETE); 7289 } 7290 7291 alloc_len = scsi_4btoul(cdb->length); 7292 7293 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7294 7295 ctsio->kern_sg_entries = 0; 7296 7297 if (total_len < alloc_len) { 7298 ctsio->residual = alloc_len - total_len; 7299 ctsio->kern_data_len = total_len; 7300 ctsio->kern_total_len = total_len; 7301 } else { 7302 ctsio->residual = 0; 7303 ctsio->kern_data_len = alloc_len; 7304 ctsio->kern_total_len = alloc_len; 7305 } 7306 ctsio->kern_data_resid = 0; 7307 ctsio->kern_rel_offset = 0; 7308 7309 switch (cdb->options & RSO_OPTIONS_MASK) { 7310 case RSO_OPTIONS_ALL: 7311 all = (struct scsi_report_supported_opcodes_all *) 7312 ctsio->kern_data_ptr; 7313 num = 0; 7314 for (i = 0; i < 256; i++) { 7315 entry = &ctl_cmd_table[i]; 7316 if (entry->flags & CTL_CMD_FLAG_SA5) { 7317 for (j = 0; j < 32; j++) { 7318 sentry = &((const struct ctl_cmd_entry *) 7319 entry->execute)[j]; 7320 if (!ctl_cmd_applicable( 7321 lun->be_lun->lun_type, sentry)) 7322 continue; 7323 descr = &all->descr[num++]; 7324 descr->opcode = i; 7325 scsi_ulto2b(j, descr->service_action); 7326 descr->flags = RSO_SERVACTV; 7327 scsi_ulto2b(sentry->length, 7328 descr->cdb_length); 7329 } 7330 } else { 7331 if (!ctl_cmd_applicable(lun->be_lun->lun_type, 7332 entry)) 7333 continue; 7334 descr = &all->descr[num++]; 7335 descr->opcode = i; 7336 scsi_ulto2b(0, descr->service_action); 7337 descr->flags = 0; 7338 scsi_ulto2b(entry->length, descr->cdb_length); 7339 } 7340 } 7341 scsi_ulto4b( 7342 num * sizeof(struct scsi_report_supported_opcodes_descr), 7343 all->length); 7344 break; 7345 case RSO_OPTIONS_OC: 7346 one = (struct scsi_report_supported_opcodes_one *) 7347 ctsio->kern_data_ptr; 7348 entry = &ctl_cmd_table[opcode]; 7349 goto fill_one; 7350 case RSO_OPTIONS_OC_SA: 7351 one = (struct scsi_report_supported_opcodes_one *) 7352 ctsio->kern_data_ptr; 7353 entry = &ctl_cmd_table[opcode]; 7354 entry = &((const struct ctl_cmd_entry *) 7355 entry->execute)[service_action]; 7356fill_one: 7357 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 7358 one->support = 3; 7359 scsi_ulto2b(entry->length, one->cdb_length); 7360 one->cdb_usage[0] = opcode; 7361 memcpy(&one->cdb_usage[1], entry->usage, 7362 entry->length - 1); 7363 } else 7364 one->support = 1; 7365 break; 7366 } 7367 7368 ctl_set_success(ctsio); 7369 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7370 ctsio->be_move_done = ctl_config_move_done; 7371 ctl_datamove((union ctl_io *)ctsio); 7372 return(retval); 7373} 7374 7375int 7376ctl_report_supported_tmf(struct ctl_scsiio *ctsio) 7377{ 7378 struct scsi_report_supported_tmf *cdb; 7379 struct scsi_report_supported_tmf_data *data; 7380 int retval; 7381 int alloc_len, total_len; 7382 7383 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n")); 7384 7385 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb; 7386 7387 retval = CTL_RETVAL_COMPLETE; 7388 7389 total_len = sizeof(struct scsi_report_supported_tmf_data); 7390 alloc_len = scsi_4btoul(cdb->length); 7391 7392 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7393 7394 ctsio->kern_sg_entries = 0; 7395 7396 if (total_len < alloc_len) { 7397 ctsio->residual = alloc_len - total_len; 7398 ctsio->kern_data_len = total_len; 7399 ctsio->kern_total_len = total_len; 7400 } else { 7401 ctsio->residual = 0; 7402 ctsio->kern_data_len = alloc_len; 7403 ctsio->kern_total_len = alloc_len; 7404 } 7405 ctsio->kern_data_resid = 0; 7406 ctsio->kern_rel_offset = 0; 7407 7408 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr; 7409 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS | 7410 RST_TRS; 7411 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS; 7412 7413 ctl_set_success(ctsio); 7414 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7415 ctsio->be_move_done = ctl_config_move_done; 7416 ctl_datamove((union ctl_io *)ctsio); 7417 return (retval); 7418} 7419 7420int 7421ctl_report_timestamp(struct ctl_scsiio *ctsio) 7422{ 7423 struct scsi_report_timestamp *cdb; 7424 struct scsi_report_timestamp_data *data; 7425 struct timeval tv; 7426 int64_t timestamp; 7427 int retval; 7428 int alloc_len, total_len; 7429 7430 CTL_DEBUG_PRINT(("ctl_report_timestamp\n")); 7431 7432 cdb = (struct scsi_report_timestamp *)ctsio->cdb; 7433 7434 retval = CTL_RETVAL_COMPLETE; 7435 7436 total_len = sizeof(struct scsi_report_timestamp_data); 7437 alloc_len = scsi_4btoul(cdb->length); 7438 7439 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7440 7441 ctsio->kern_sg_entries = 0; 7442 7443 if (total_len < alloc_len) { 7444 ctsio->residual = alloc_len - total_len; 7445 ctsio->kern_data_len = total_len; 7446 ctsio->kern_total_len = total_len; 7447 } else { 7448 ctsio->residual = 0; 7449 ctsio->kern_data_len = alloc_len; 7450 ctsio->kern_total_len = alloc_len; 7451 } 7452 ctsio->kern_data_resid = 0; 7453 ctsio->kern_rel_offset = 0; 7454 7455 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr; 7456 scsi_ulto2b(sizeof(*data) - 2, data->length); 7457 data->origin = RTS_ORIG_OUTSIDE; 7458 getmicrotime(&tv); 7459 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000; 7460 scsi_ulto4b(timestamp >> 16, data->timestamp); 7461 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]); 7462 7463 ctl_set_success(ctsio); 7464 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7465 ctsio->be_move_done = ctl_config_move_done; 7466 ctl_datamove((union ctl_io *)ctsio); 7467 return (retval); 7468} 7469 7470int 7471ctl_persistent_reserve_in(struct ctl_scsiio *ctsio) 7472{ 7473 struct scsi_per_res_in *cdb; 7474 int alloc_len, total_len = 0; 7475 /* struct scsi_per_res_in_rsrv in_data; */ 7476 struct ctl_lun *lun; 7477 struct ctl_softc *softc; 7478 uint64_t key; 7479 7480 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n")); 7481 7482 cdb = (struct scsi_per_res_in *)ctsio->cdb; 7483 7484 alloc_len = scsi_2btoul(cdb->length); 7485 7486 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 7487 softc = lun->ctl_softc; 7488 7489retry: 7490 mtx_lock(&lun->lun_lock); 7491 switch (cdb->action) { 7492 case SPRI_RK: /* read keys */ 7493 total_len = sizeof(struct scsi_per_res_in_keys) + 7494 lun->pr_key_count * 7495 sizeof(struct scsi_per_res_key); 7496 break; 7497 case SPRI_RR: /* read reservation */ 7498 if (lun->flags & CTL_LUN_PR_RESERVED) 7499 total_len = sizeof(struct scsi_per_res_in_rsrv); 7500 else 7501 total_len = sizeof(struct scsi_per_res_in_header); 7502 break; 7503 case SPRI_RC: /* report capabilities */ 7504 total_len = sizeof(struct scsi_per_res_cap); 7505 break; 7506 case SPRI_RS: /* read full status */ 7507 total_len = sizeof(struct scsi_per_res_in_header) + 7508 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7509 lun->pr_key_count; 7510 break; 7511 default: 7512 panic("Invalid PR type %x", cdb->action); 7513 } 7514 mtx_unlock(&lun->lun_lock); 7515 7516 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO); 7517 7518 if (total_len < alloc_len) { 7519 ctsio->residual = alloc_len - total_len; 7520 ctsio->kern_data_len = total_len; 7521 ctsio->kern_total_len = total_len; 7522 } else { 7523 ctsio->residual = 0; 7524 ctsio->kern_data_len = alloc_len; 7525 ctsio->kern_total_len = alloc_len; 7526 } 7527 7528 ctsio->kern_data_resid = 0; 7529 ctsio->kern_rel_offset = 0; 7530 ctsio->kern_sg_entries = 0; 7531 7532 mtx_lock(&lun->lun_lock); 7533 switch (cdb->action) { 7534 case SPRI_RK: { // read keys 7535 struct scsi_per_res_in_keys *res_keys; 7536 int i, key_count; 7537 7538 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr; 7539 7540 /* 7541 * We had to drop the lock to allocate our buffer, which 7542 * leaves time for someone to come in with another 7543 * persistent reservation. (That is unlikely, though, 7544 * since this should be the only persistent reservation 7545 * command active right now.) 7546 */ 7547 if (total_len != (sizeof(struct scsi_per_res_in_keys) + 7548 (lun->pr_key_count * 7549 sizeof(struct scsi_per_res_key)))){ 7550 mtx_unlock(&lun->lun_lock); 7551 free(ctsio->kern_data_ptr, M_CTL); 7552 printf("%s: reservation length changed, retrying\n", 7553 __func__); 7554 goto retry; 7555 } 7556 7557 scsi_ulto4b(lun->pr_generation, res_keys->header.generation); 7558 7559 scsi_ulto4b(sizeof(struct scsi_per_res_key) * 7560 lun->pr_key_count, res_keys->header.length); 7561 7562 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) { 7563 if ((key = ctl_get_prkey(lun, i)) == 0) 7564 continue; 7565 7566 /* 7567 * We used lun->pr_key_count to calculate the 7568 * size to allocate. If it turns out the number of 7569 * initiators with the registered flag set is 7570 * larger than that (i.e. they haven't been kept in 7571 * sync), we've got a problem. 7572 */ 7573 if (key_count >= lun->pr_key_count) { 7574 key_count++; 7575 continue; 7576 } 7577 scsi_u64to8b(key, res_keys->keys[key_count].key); 7578 key_count++; 7579 } 7580 break; 7581 } 7582 case SPRI_RR: { // read reservation 7583 struct scsi_per_res_in_rsrv *res; 7584 int tmp_len, header_only; 7585 7586 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr; 7587 7588 scsi_ulto4b(lun->pr_generation, res->header.generation); 7589 7590 if (lun->flags & CTL_LUN_PR_RESERVED) 7591 { 7592 tmp_len = sizeof(struct scsi_per_res_in_rsrv); 7593 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data), 7594 res->header.length); 7595 header_only = 0; 7596 } else { 7597 tmp_len = sizeof(struct scsi_per_res_in_header); 7598 scsi_ulto4b(0, res->header.length); 7599 header_only = 1; 7600 } 7601 7602 /* 7603 * We had to drop the lock to allocate our buffer, which 7604 * leaves time for someone to come in with another 7605 * persistent reservation. (That is unlikely, though, 7606 * since this should be the only persistent reservation 7607 * command active right now.) 7608 */ 7609 if (tmp_len != total_len) { 7610 mtx_unlock(&lun->lun_lock); 7611 free(ctsio->kern_data_ptr, M_CTL); 7612 printf("%s: reservation status changed, retrying\n", 7613 __func__); 7614 goto retry; 7615 } 7616 7617 /* 7618 * No reservation held, so we're done. 7619 */ 7620 if (header_only != 0) 7621 break; 7622 7623 /* 7624 * If the registration is an All Registrants type, the key 7625 * is 0, since it doesn't really matter. 7626 */ 7627 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 7628 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx), 7629 res->data.reservation); 7630 } 7631 res->data.scopetype = lun->pr_res_type; 7632 break; 7633 } 7634 case SPRI_RC: //report capabilities 7635 { 7636 struct scsi_per_res_cap *res_cap; 7637 uint16_t type_mask; 7638 7639 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr; 7640 scsi_ulto2b(sizeof(*res_cap), res_cap->length); 7641 res_cap->flags1 = SPRI_CRH; 7642 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5; 7643 type_mask = SPRI_TM_WR_EX_AR | 7644 SPRI_TM_EX_AC_RO | 7645 SPRI_TM_WR_EX_RO | 7646 SPRI_TM_EX_AC | 7647 SPRI_TM_WR_EX | 7648 SPRI_TM_EX_AC_AR; 7649 scsi_ulto2b(type_mask, res_cap->type_mask); 7650 break; 7651 } 7652 case SPRI_RS: { // read full status 7653 struct scsi_per_res_in_full *res_status; 7654 struct scsi_per_res_in_full_desc *res_desc; 7655 struct ctl_port *port; 7656 int i, len; 7657 7658 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr; 7659 7660 /* 7661 * We had to drop the lock to allocate our buffer, which 7662 * leaves time for someone to come in with another 7663 * persistent reservation. (That is unlikely, though, 7664 * since this should be the only persistent reservation 7665 * command active right now.) 7666 */ 7667 if (total_len < (sizeof(struct scsi_per_res_in_header) + 7668 (sizeof(struct scsi_per_res_in_full_desc) + 256) * 7669 lun->pr_key_count)){ 7670 mtx_unlock(&lun->lun_lock); 7671 free(ctsio->kern_data_ptr, M_CTL); 7672 printf("%s: reservation length changed, retrying\n", 7673 __func__); 7674 goto retry; 7675 } 7676 7677 scsi_ulto4b(lun->pr_generation, res_status->header.generation); 7678 7679 res_desc = &res_status->desc[0]; 7680 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7681 if ((key = ctl_get_prkey(lun, i)) == 0) 7682 continue; 7683 7684 scsi_u64to8b(key, res_desc->res_key.key); 7685 if ((lun->flags & CTL_LUN_PR_RESERVED) && 7686 (lun->pr_res_idx == i || 7687 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) { 7688 res_desc->flags = SPRI_FULL_R_HOLDER; 7689 res_desc->scopetype = lun->pr_res_type; 7690 } 7691 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT, 7692 res_desc->rel_trgt_port_id); 7693 len = 0; 7694 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT]; 7695 if (port != NULL) 7696 len = ctl_create_iid(port, 7697 i % CTL_MAX_INIT_PER_PORT, 7698 res_desc->transport_id); 7699 scsi_ulto4b(len, res_desc->additional_length); 7700 res_desc = (struct scsi_per_res_in_full_desc *) 7701 &res_desc->transport_id[len]; 7702 } 7703 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0], 7704 res_status->header.length); 7705 break; 7706 } 7707 default: 7708 /* 7709 * This is a bug, because we just checked for this above, 7710 * and should have returned an error. 7711 */ 7712 panic("Invalid PR type %x", cdb->action); 7713 break; /* NOTREACHED */ 7714 } 7715 mtx_unlock(&lun->lun_lock); 7716 7717 ctl_set_success(ctsio); 7718 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 7719 ctsio->be_move_done = ctl_config_move_done; 7720 ctl_datamove((union ctl_io *)ctsio); 7721 return (CTL_RETVAL_COMPLETE); 7722} 7723 7724/* 7725 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if 7726 * it should return. 7727 */ 7728static int 7729ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key, 7730 uint64_t sa_res_key, uint8_t type, uint32_t residx, 7731 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb, 7732 struct scsi_per_res_out_parms* param) 7733{ 7734 union ctl_ha_msg persis_io; 7735 int i; 7736 7737 mtx_lock(&lun->lun_lock); 7738 if (sa_res_key == 0) { 7739 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 7740 /* validate scope and type */ 7741 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7742 SPR_LU_SCOPE) { 7743 mtx_unlock(&lun->lun_lock); 7744 ctl_set_invalid_field(/*ctsio*/ ctsio, 7745 /*sks_valid*/ 1, 7746 /*command*/ 1, 7747 /*field*/ 2, 7748 /*bit_valid*/ 1, 7749 /*bit*/ 4); 7750 ctl_done((union ctl_io *)ctsio); 7751 return (1); 7752 } 7753 7754 if (type>8 || type==2 || type==4 || type==0) { 7755 mtx_unlock(&lun->lun_lock); 7756 ctl_set_invalid_field(/*ctsio*/ ctsio, 7757 /*sks_valid*/ 1, 7758 /*command*/ 1, 7759 /*field*/ 2, 7760 /*bit_valid*/ 1, 7761 /*bit*/ 0); 7762 ctl_done((union ctl_io *)ctsio); 7763 return (1); 7764 } 7765 7766 /* 7767 * Unregister everybody else and build UA for 7768 * them 7769 */ 7770 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7771 if (i == residx || ctl_get_prkey(lun, i) == 0) 7772 continue; 7773 7774 ctl_clr_prkey(lun, i); 7775 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7776 } 7777 lun->pr_key_count = 1; 7778 lun->pr_res_type = type; 7779 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7780 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7781 lun->pr_res_idx = residx; 7782 lun->pr_generation++; 7783 mtx_unlock(&lun->lun_lock); 7784 7785 /* send msg to other side */ 7786 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7787 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7788 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7789 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7790 persis_io.pr.pr_info.res_type = type; 7791 memcpy(persis_io.pr.pr_info.sa_res_key, 7792 param->serv_act_res_key, 7793 sizeof(param->serv_act_res_key)); 7794 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7795 sizeof(persis_io.pr), M_WAITOK); 7796 } else { 7797 /* not all registrants */ 7798 mtx_unlock(&lun->lun_lock); 7799 free(ctsio->kern_data_ptr, M_CTL); 7800 ctl_set_invalid_field(ctsio, 7801 /*sks_valid*/ 1, 7802 /*command*/ 0, 7803 /*field*/ 8, 7804 /*bit_valid*/ 0, 7805 /*bit*/ 0); 7806 ctl_done((union ctl_io *)ctsio); 7807 return (1); 7808 } 7809 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7810 || !(lun->flags & CTL_LUN_PR_RESERVED)) { 7811 int found = 0; 7812 7813 if (res_key == sa_res_key) { 7814 /* special case */ 7815 /* 7816 * The spec implies this is not good but doesn't 7817 * say what to do. There are two choices either 7818 * generate a res conflict or check condition 7819 * with illegal field in parameter data. Since 7820 * that is what is done when the sa_res_key is 7821 * zero I'll take that approach since this has 7822 * to do with the sa_res_key. 7823 */ 7824 mtx_unlock(&lun->lun_lock); 7825 free(ctsio->kern_data_ptr, M_CTL); 7826 ctl_set_invalid_field(ctsio, 7827 /*sks_valid*/ 1, 7828 /*command*/ 0, 7829 /*field*/ 8, 7830 /*bit_valid*/ 0, 7831 /*bit*/ 0); 7832 ctl_done((union ctl_io *)ctsio); 7833 return (1); 7834 } 7835 7836 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7837 if (ctl_get_prkey(lun, i) != sa_res_key) 7838 continue; 7839 7840 found = 1; 7841 ctl_clr_prkey(lun, i); 7842 lun->pr_key_count--; 7843 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7844 } 7845 if (!found) { 7846 mtx_unlock(&lun->lun_lock); 7847 free(ctsio->kern_data_ptr, M_CTL); 7848 ctl_set_reservation_conflict(ctsio); 7849 ctl_done((union ctl_io *)ctsio); 7850 return (CTL_RETVAL_COMPLETE); 7851 } 7852 lun->pr_generation++; 7853 mtx_unlock(&lun->lun_lock); 7854 7855 /* send msg to other side */ 7856 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7857 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7858 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7859 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7860 persis_io.pr.pr_info.res_type = type; 7861 memcpy(persis_io.pr.pr_info.sa_res_key, 7862 param->serv_act_res_key, 7863 sizeof(param->serv_act_res_key)); 7864 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7865 sizeof(persis_io.pr), M_WAITOK); 7866 } else { 7867 /* Reserved but not all registrants */ 7868 /* sa_res_key is res holder */ 7869 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) { 7870 /* validate scope and type */ 7871 if ((cdb->scope_type & SPR_SCOPE_MASK) != 7872 SPR_LU_SCOPE) { 7873 mtx_unlock(&lun->lun_lock); 7874 ctl_set_invalid_field(/*ctsio*/ ctsio, 7875 /*sks_valid*/ 1, 7876 /*command*/ 1, 7877 /*field*/ 2, 7878 /*bit_valid*/ 1, 7879 /*bit*/ 4); 7880 ctl_done((union ctl_io *)ctsio); 7881 return (1); 7882 } 7883 7884 if (type>8 || type==2 || type==4 || type==0) { 7885 mtx_unlock(&lun->lun_lock); 7886 ctl_set_invalid_field(/*ctsio*/ ctsio, 7887 /*sks_valid*/ 1, 7888 /*command*/ 1, 7889 /*field*/ 2, 7890 /*bit_valid*/ 1, 7891 /*bit*/ 0); 7892 ctl_done((union ctl_io *)ctsio); 7893 return (1); 7894 } 7895 7896 /* 7897 * Do the following: 7898 * if sa_res_key != res_key remove all 7899 * registrants w/sa_res_key and generate UA 7900 * for these registrants(Registrations 7901 * Preempted) if it wasn't an exclusive 7902 * reservation generate UA(Reservations 7903 * Preempted) for all other registered nexuses 7904 * if the type has changed. Establish the new 7905 * reservation and holder. If res_key and 7906 * sa_res_key are the same do the above 7907 * except don't unregister the res holder. 7908 */ 7909 7910 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 7911 if (i == residx || ctl_get_prkey(lun, i) == 0) 7912 continue; 7913 7914 if (sa_res_key == ctl_get_prkey(lun, i)) { 7915 ctl_clr_prkey(lun, i); 7916 lun->pr_key_count--; 7917 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7918 } else if (type != lun->pr_res_type && 7919 (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 7920 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 7921 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 7922 } 7923 } 7924 lun->pr_res_type = type; 7925 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 7926 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 7927 lun->pr_res_idx = residx; 7928 else 7929 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 7930 lun->pr_generation++; 7931 mtx_unlock(&lun->lun_lock); 7932 7933 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7934 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7935 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7936 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7937 persis_io.pr.pr_info.res_type = type; 7938 memcpy(persis_io.pr.pr_info.sa_res_key, 7939 param->serv_act_res_key, 7940 sizeof(param->serv_act_res_key)); 7941 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7942 sizeof(persis_io.pr), M_WAITOK); 7943 } else { 7944 /* 7945 * sa_res_key is not the res holder just 7946 * remove registrants 7947 */ 7948 int found=0; 7949 7950 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 7951 if (sa_res_key != ctl_get_prkey(lun, i)) 7952 continue; 7953 7954 found = 1; 7955 ctl_clr_prkey(lun, i); 7956 lun->pr_key_count--; 7957 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 7958 } 7959 7960 if (!found) { 7961 mtx_unlock(&lun->lun_lock); 7962 free(ctsio->kern_data_ptr, M_CTL); 7963 ctl_set_reservation_conflict(ctsio); 7964 ctl_done((union ctl_io *)ctsio); 7965 return (1); 7966 } 7967 lun->pr_generation++; 7968 mtx_unlock(&lun->lun_lock); 7969 7970 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 7971 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 7972 persis_io.pr.pr_info.action = CTL_PR_PREEMPT; 7973 persis_io.pr.pr_info.residx = lun->pr_res_idx; 7974 persis_io.pr.pr_info.res_type = type; 7975 memcpy(persis_io.pr.pr_info.sa_res_key, 7976 param->serv_act_res_key, 7977 sizeof(param->serv_act_res_key)); 7978 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 7979 sizeof(persis_io.pr), M_WAITOK); 7980 } 7981 } 7982 return (0); 7983} 7984 7985static void 7986ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg) 7987{ 7988 uint64_t sa_res_key; 7989 int i; 7990 7991 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key); 7992 7993 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS 7994 || lun->pr_res_idx == CTL_PR_NO_RESERVATION 7995 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) { 7996 if (sa_res_key == 0) { 7997 /* 7998 * Unregister everybody else and build UA for 7999 * them 8000 */ 8001 for(i = 0; i < CTL_MAX_INITIATORS; i++) { 8002 if (i == msg->pr.pr_info.residx || 8003 ctl_get_prkey(lun, i) == 0) 8004 continue; 8005 8006 ctl_clr_prkey(lun, i); 8007 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8008 } 8009 8010 lun->pr_key_count = 1; 8011 lun->pr_res_type = msg->pr.pr_info.res_type; 8012 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8013 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8014 lun->pr_res_idx = msg->pr.pr_info.residx; 8015 } else { 8016 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8017 if (sa_res_key == ctl_get_prkey(lun, i)) 8018 continue; 8019 8020 ctl_clr_prkey(lun, i); 8021 lun->pr_key_count--; 8022 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8023 } 8024 } 8025 } else { 8026 for (i = 0; i < CTL_MAX_INITIATORS; i++) { 8027 if (i == msg->pr.pr_info.residx || 8028 ctl_get_prkey(lun, i) == 0) 8029 continue; 8030 8031 if (sa_res_key == ctl_get_prkey(lun, i)) { 8032 ctl_clr_prkey(lun, i); 8033 lun->pr_key_count--; 8034 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8035 } else if (msg->pr.pr_info.res_type != lun->pr_res_type 8036 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8037 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) { 8038 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8039 } 8040 } 8041 lun->pr_res_type = msg->pr.pr_info.res_type; 8042 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR && 8043 lun->pr_res_type != SPR_TYPE_EX_AC_AR) 8044 lun->pr_res_idx = msg->pr.pr_info.residx; 8045 else 8046 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8047 } 8048 lun->pr_generation++; 8049 8050} 8051 8052 8053int 8054ctl_persistent_reserve_out(struct ctl_scsiio *ctsio) 8055{ 8056 int retval; 8057 u_int32_t param_len; 8058 struct scsi_per_res_out *cdb; 8059 struct ctl_lun *lun; 8060 struct scsi_per_res_out_parms* param; 8061 struct ctl_softc *softc; 8062 uint32_t residx; 8063 uint64_t res_key, sa_res_key, key; 8064 uint8_t type; 8065 union ctl_ha_msg persis_io; 8066 int i; 8067 8068 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n")); 8069 8070 retval = CTL_RETVAL_COMPLETE; 8071 8072 cdb = (struct scsi_per_res_out *)ctsio->cdb; 8073 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8074 softc = lun->ctl_softc; 8075 8076 /* 8077 * We only support whole-LUN scope. The scope & type are ignored for 8078 * register, register and ignore existing key and clear. 8079 * We sometimes ignore scope and type on preempts too!! 8080 * Verify reservation type here as well. 8081 */ 8082 type = cdb->scope_type & SPR_TYPE_MASK; 8083 if ((cdb->action == SPRO_RESERVE) 8084 || (cdb->action == SPRO_RELEASE)) { 8085 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) { 8086 ctl_set_invalid_field(/*ctsio*/ ctsio, 8087 /*sks_valid*/ 1, 8088 /*command*/ 1, 8089 /*field*/ 2, 8090 /*bit_valid*/ 1, 8091 /*bit*/ 4); 8092 ctl_done((union ctl_io *)ctsio); 8093 return (CTL_RETVAL_COMPLETE); 8094 } 8095 8096 if (type>8 || type==2 || type==4 || type==0) { 8097 ctl_set_invalid_field(/*ctsio*/ ctsio, 8098 /*sks_valid*/ 1, 8099 /*command*/ 1, 8100 /*field*/ 2, 8101 /*bit_valid*/ 1, 8102 /*bit*/ 0); 8103 ctl_done((union ctl_io *)ctsio); 8104 return (CTL_RETVAL_COMPLETE); 8105 } 8106 } 8107 8108 param_len = scsi_4btoul(cdb->length); 8109 8110 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) { 8111 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK); 8112 ctsio->kern_data_len = param_len; 8113 ctsio->kern_total_len = param_len; 8114 ctsio->kern_data_resid = 0; 8115 ctsio->kern_rel_offset = 0; 8116 ctsio->kern_sg_entries = 0; 8117 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 8118 ctsio->be_move_done = ctl_config_move_done; 8119 ctl_datamove((union ctl_io *)ctsio); 8120 8121 return (CTL_RETVAL_COMPLETE); 8122 } 8123 8124 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr; 8125 8126 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 8127 res_key = scsi_8btou64(param->res_key.key); 8128 sa_res_key = scsi_8btou64(param->serv_act_res_key); 8129 8130 /* 8131 * Validate the reservation key here except for SPRO_REG_IGNO 8132 * This must be done for all other service actions 8133 */ 8134 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) { 8135 mtx_lock(&lun->lun_lock); 8136 if ((key = ctl_get_prkey(lun, residx)) != 0) { 8137 if (res_key != key) { 8138 /* 8139 * The current key passed in doesn't match 8140 * the one the initiator previously 8141 * registered. 8142 */ 8143 mtx_unlock(&lun->lun_lock); 8144 free(ctsio->kern_data_ptr, M_CTL); 8145 ctl_set_reservation_conflict(ctsio); 8146 ctl_done((union ctl_io *)ctsio); 8147 return (CTL_RETVAL_COMPLETE); 8148 } 8149 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) { 8150 /* 8151 * We are not registered 8152 */ 8153 mtx_unlock(&lun->lun_lock); 8154 free(ctsio->kern_data_ptr, M_CTL); 8155 ctl_set_reservation_conflict(ctsio); 8156 ctl_done((union ctl_io *)ctsio); 8157 return (CTL_RETVAL_COMPLETE); 8158 } else if (res_key != 0) { 8159 /* 8160 * We are not registered and trying to register but 8161 * the register key isn't zero. 8162 */ 8163 mtx_unlock(&lun->lun_lock); 8164 free(ctsio->kern_data_ptr, M_CTL); 8165 ctl_set_reservation_conflict(ctsio); 8166 ctl_done((union ctl_io *)ctsio); 8167 return (CTL_RETVAL_COMPLETE); 8168 } 8169 mtx_unlock(&lun->lun_lock); 8170 } 8171 8172 switch (cdb->action & SPRO_ACTION_MASK) { 8173 case SPRO_REGISTER: 8174 case SPRO_REG_IGNO: { 8175 8176#if 0 8177 printf("Registration received\n"); 8178#endif 8179 8180 /* 8181 * We don't support any of these options, as we report in 8182 * the read capabilities request (see 8183 * ctl_persistent_reserve_in(), above). 8184 */ 8185 if ((param->flags & SPR_SPEC_I_PT) 8186 || (param->flags & SPR_ALL_TG_PT) 8187 || (param->flags & SPR_APTPL)) { 8188 int bit_ptr; 8189 8190 if (param->flags & SPR_APTPL) 8191 bit_ptr = 0; 8192 else if (param->flags & SPR_ALL_TG_PT) 8193 bit_ptr = 2; 8194 else /* SPR_SPEC_I_PT */ 8195 bit_ptr = 3; 8196 8197 free(ctsio->kern_data_ptr, M_CTL); 8198 ctl_set_invalid_field(ctsio, 8199 /*sks_valid*/ 1, 8200 /*command*/ 0, 8201 /*field*/ 20, 8202 /*bit_valid*/ 1, 8203 /*bit*/ bit_ptr); 8204 ctl_done((union ctl_io *)ctsio); 8205 return (CTL_RETVAL_COMPLETE); 8206 } 8207 8208 mtx_lock(&lun->lun_lock); 8209 8210 /* 8211 * The initiator wants to clear the 8212 * key/unregister. 8213 */ 8214 if (sa_res_key == 0) { 8215 if ((res_key == 0 8216 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER) 8217 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO 8218 && ctl_get_prkey(lun, residx) == 0)) { 8219 mtx_unlock(&lun->lun_lock); 8220 goto done; 8221 } 8222 8223 ctl_clr_prkey(lun, residx); 8224 lun->pr_key_count--; 8225 8226 if (residx == lun->pr_res_idx) { 8227 lun->flags &= ~CTL_LUN_PR_RESERVED; 8228 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8229 8230 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8231 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8232 lun->pr_key_count) { 8233 /* 8234 * If the reservation is a registrants 8235 * only type we need to generate a UA 8236 * for other registered inits. The 8237 * sense code should be RESERVATIONS 8238 * RELEASED 8239 */ 8240 8241 for (i = softc->init_min; i < softc->init_max; i++){ 8242 if (ctl_get_prkey(lun, i) == 0) 8243 continue; 8244 ctl_est_ua(lun, i, 8245 CTL_UA_RES_RELEASE); 8246 } 8247 } 8248 lun->pr_res_type = 0; 8249 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8250 if (lun->pr_key_count==0) { 8251 lun->flags &= ~CTL_LUN_PR_RESERVED; 8252 lun->pr_res_type = 0; 8253 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8254 } 8255 } 8256 lun->pr_generation++; 8257 mtx_unlock(&lun->lun_lock); 8258 8259 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8260 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8261 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY; 8262 persis_io.pr.pr_info.residx = residx; 8263 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8264 sizeof(persis_io.pr), M_WAITOK); 8265 } else /* sa_res_key != 0 */ { 8266 8267 /* 8268 * If we aren't registered currently then increment 8269 * the key count and set the registered flag. 8270 */ 8271 ctl_alloc_prkey(lun, residx); 8272 if (ctl_get_prkey(lun, residx) == 0) 8273 lun->pr_key_count++; 8274 ctl_set_prkey(lun, residx, sa_res_key); 8275 lun->pr_generation++; 8276 mtx_unlock(&lun->lun_lock); 8277 8278 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8279 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8280 persis_io.pr.pr_info.action = CTL_PR_REG_KEY; 8281 persis_io.pr.pr_info.residx = residx; 8282 memcpy(persis_io.pr.pr_info.sa_res_key, 8283 param->serv_act_res_key, 8284 sizeof(param->serv_act_res_key)); 8285 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8286 sizeof(persis_io.pr), M_WAITOK); 8287 } 8288 8289 break; 8290 } 8291 case SPRO_RESERVE: 8292#if 0 8293 printf("Reserve executed type %d\n", type); 8294#endif 8295 mtx_lock(&lun->lun_lock); 8296 if (lun->flags & CTL_LUN_PR_RESERVED) { 8297 /* 8298 * if this isn't the reservation holder and it's 8299 * not a "all registrants" type or if the type is 8300 * different then we have a conflict 8301 */ 8302 if ((lun->pr_res_idx != residx 8303 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) 8304 || lun->pr_res_type != type) { 8305 mtx_unlock(&lun->lun_lock); 8306 free(ctsio->kern_data_ptr, M_CTL); 8307 ctl_set_reservation_conflict(ctsio); 8308 ctl_done((union ctl_io *)ctsio); 8309 return (CTL_RETVAL_COMPLETE); 8310 } 8311 mtx_unlock(&lun->lun_lock); 8312 } else /* create a reservation */ { 8313 /* 8314 * If it's not an "all registrants" type record 8315 * reservation holder 8316 */ 8317 if (type != SPR_TYPE_WR_EX_AR 8318 && type != SPR_TYPE_EX_AC_AR) 8319 lun->pr_res_idx = residx; /* Res holder */ 8320 else 8321 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS; 8322 8323 lun->flags |= CTL_LUN_PR_RESERVED; 8324 lun->pr_res_type = type; 8325 8326 mtx_unlock(&lun->lun_lock); 8327 8328 /* send msg to other side */ 8329 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8330 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8331 persis_io.pr.pr_info.action = CTL_PR_RESERVE; 8332 persis_io.pr.pr_info.residx = lun->pr_res_idx; 8333 persis_io.pr.pr_info.res_type = type; 8334 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8335 sizeof(persis_io.pr), M_WAITOK); 8336 } 8337 break; 8338 8339 case SPRO_RELEASE: 8340 mtx_lock(&lun->lun_lock); 8341 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) { 8342 /* No reservation exists return good status */ 8343 mtx_unlock(&lun->lun_lock); 8344 goto done; 8345 } 8346 /* 8347 * Is this nexus a reservation holder? 8348 */ 8349 if (lun->pr_res_idx != residx 8350 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) { 8351 /* 8352 * not a res holder return good status but 8353 * do nothing 8354 */ 8355 mtx_unlock(&lun->lun_lock); 8356 goto done; 8357 } 8358 8359 if (lun->pr_res_type != type) { 8360 mtx_unlock(&lun->lun_lock); 8361 free(ctsio->kern_data_ptr, M_CTL); 8362 ctl_set_illegal_pr_release(ctsio); 8363 ctl_done((union ctl_io *)ctsio); 8364 return (CTL_RETVAL_COMPLETE); 8365 } 8366 8367 /* okay to release */ 8368 lun->flags &= ~CTL_LUN_PR_RESERVED; 8369 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8370 lun->pr_res_type = 0; 8371 8372 /* 8373 * if this isn't an exclusive access 8374 * res generate UA for all other 8375 * registrants. 8376 */ 8377 if (type != SPR_TYPE_EX_AC 8378 && type != SPR_TYPE_WR_EX) { 8379 for (i = softc->init_min; i < softc->init_max; i++) { 8380 if (i == residx || ctl_get_prkey(lun, i) == 0) 8381 continue; 8382 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8383 } 8384 } 8385 mtx_unlock(&lun->lun_lock); 8386 8387 /* Send msg to other side */ 8388 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8389 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8390 persis_io.pr.pr_info.action = CTL_PR_RELEASE; 8391 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8392 sizeof(persis_io.pr), M_WAITOK); 8393 break; 8394 8395 case SPRO_CLEAR: 8396 /* send msg to other side */ 8397 8398 mtx_lock(&lun->lun_lock); 8399 lun->flags &= ~CTL_LUN_PR_RESERVED; 8400 lun->pr_res_type = 0; 8401 lun->pr_key_count = 0; 8402 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8403 8404 ctl_clr_prkey(lun, residx); 8405 for (i = 0; i < CTL_MAX_INITIATORS; i++) 8406 if (ctl_get_prkey(lun, i) != 0) { 8407 ctl_clr_prkey(lun, i); 8408 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8409 } 8410 lun->pr_generation++; 8411 mtx_unlock(&lun->lun_lock); 8412 8413 persis_io.hdr.nexus = ctsio->io_hdr.nexus; 8414 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION; 8415 persis_io.pr.pr_info.action = CTL_PR_CLEAR; 8416 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io, 8417 sizeof(persis_io.pr), M_WAITOK); 8418 break; 8419 8420 case SPRO_PREEMPT: 8421 case SPRO_PRE_ABO: { 8422 int nretval; 8423 8424 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type, 8425 residx, ctsio, cdb, param); 8426 if (nretval != 0) 8427 return (CTL_RETVAL_COMPLETE); 8428 break; 8429 } 8430 default: 8431 panic("Invalid PR type %x", cdb->action); 8432 } 8433 8434done: 8435 free(ctsio->kern_data_ptr, M_CTL); 8436 ctl_set_success(ctsio); 8437 ctl_done((union ctl_io *)ctsio); 8438 8439 return (retval); 8440} 8441 8442/* 8443 * This routine is for handling a message from the other SC pertaining to 8444 * persistent reserve out. All the error checking will have been done 8445 * so only perorming the action need be done here to keep the two 8446 * in sync. 8447 */ 8448static void 8449ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg) 8450{ 8451 struct ctl_softc *softc = control_softc; 8452 struct ctl_lun *lun; 8453 int i; 8454 uint32_t residx, targ_lun; 8455 8456 targ_lun = msg->hdr.nexus.targ_mapped_lun; 8457 mtx_lock(&softc->ctl_lock); 8458 if ((targ_lun >= CTL_MAX_LUNS) || 8459 ((lun = softc->ctl_luns[targ_lun]) == NULL)) { 8460 mtx_unlock(&softc->ctl_lock); 8461 return; 8462 } 8463 mtx_lock(&lun->lun_lock); 8464 mtx_unlock(&softc->ctl_lock); 8465 if (lun->flags & CTL_LUN_DISABLED) { 8466 mtx_unlock(&lun->lun_lock); 8467 return; 8468 } 8469 residx = ctl_get_initindex(&msg->hdr.nexus); 8470 switch(msg->pr.pr_info.action) { 8471 case CTL_PR_REG_KEY: 8472 ctl_alloc_prkey(lun, msg->pr.pr_info.residx); 8473 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0) 8474 lun->pr_key_count++; 8475 ctl_set_prkey(lun, msg->pr.pr_info.residx, 8476 scsi_8btou64(msg->pr.pr_info.sa_res_key)); 8477 lun->pr_generation++; 8478 break; 8479 8480 case CTL_PR_UNREG_KEY: 8481 ctl_clr_prkey(lun, msg->pr.pr_info.residx); 8482 lun->pr_key_count--; 8483 8484 /* XXX Need to see if the reservation has been released */ 8485 /* if so do we need to generate UA? */ 8486 if (msg->pr.pr_info.residx == lun->pr_res_idx) { 8487 lun->flags &= ~CTL_LUN_PR_RESERVED; 8488 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8489 8490 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO || 8491 lun->pr_res_type == SPR_TYPE_EX_AC_RO) && 8492 lun->pr_key_count) { 8493 /* 8494 * If the reservation is a registrants 8495 * only type we need to generate a UA 8496 * for other registered inits. The 8497 * sense code should be RESERVATIONS 8498 * RELEASED 8499 */ 8500 8501 for (i = softc->init_min; i < softc->init_max; i++) { 8502 if (ctl_get_prkey(lun, i) == 0) 8503 continue; 8504 8505 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8506 } 8507 } 8508 lun->pr_res_type = 0; 8509 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) { 8510 if (lun->pr_key_count==0) { 8511 lun->flags &= ~CTL_LUN_PR_RESERVED; 8512 lun->pr_res_type = 0; 8513 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8514 } 8515 } 8516 lun->pr_generation++; 8517 break; 8518 8519 case CTL_PR_RESERVE: 8520 lun->flags |= CTL_LUN_PR_RESERVED; 8521 lun->pr_res_type = msg->pr.pr_info.res_type; 8522 lun->pr_res_idx = msg->pr.pr_info.residx; 8523 8524 break; 8525 8526 case CTL_PR_RELEASE: 8527 /* 8528 * if this isn't an exclusive access res generate UA for all 8529 * other registrants. 8530 */ 8531 if (lun->pr_res_type != SPR_TYPE_EX_AC && 8532 lun->pr_res_type != SPR_TYPE_WR_EX) { 8533 for (i = softc->init_min; i < softc->init_max; i++) 8534 if (i == residx || ctl_get_prkey(lun, i) == 0) 8535 continue; 8536 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE); 8537 } 8538 8539 lun->flags &= ~CTL_LUN_PR_RESERVED; 8540 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8541 lun->pr_res_type = 0; 8542 break; 8543 8544 case CTL_PR_PREEMPT: 8545 ctl_pro_preempt_other(lun, msg); 8546 break; 8547 case CTL_PR_CLEAR: 8548 lun->flags &= ~CTL_LUN_PR_RESERVED; 8549 lun->pr_res_type = 0; 8550 lun->pr_key_count = 0; 8551 lun->pr_res_idx = CTL_PR_NO_RESERVATION; 8552 8553 for (i=0; i < CTL_MAX_INITIATORS; i++) { 8554 if (ctl_get_prkey(lun, i) == 0) 8555 continue; 8556 ctl_clr_prkey(lun, i); 8557 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT); 8558 } 8559 lun->pr_generation++; 8560 break; 8561 } 8562 8563 mtx_unlock(&lun->lun_lock); 8564} 8565 8566int 8567ctl_read_write(struct ctl_scsiio *ctsio) 8568{ 8569 struct ctl_lun *lun; 8570 struct ctl_lba_len_flags *lbalen; 8571 uint64_t lba; 8572 uint32_t num_blocks; 8573 int flags, retval; 8574 int isread; 8575 8576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8577 8578 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0])); 8579 8580 flags = 0; 8581 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10 8582 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16; 8583 switch (ctsio->cdb[0]) { 8584 case READ_6: 8585 case WRITE_6: { 8586 struct scsi_rw_6 *cdb; 8587 8588 cdb = (struct scsi_rw_6 *)ctsio->cdb; 8589 8590 lba = scsi_3btoul(cdb->addr); 8591 /* only 5 bits are valid in the most significant address byte */ 8592 lba &= 0x1fffff; 8593 num_blocks = cdb->length; 8594 /* 8595 * This is correct according to SBC-2. 8596 */ 8597 if (num_blocks == 0) 8598 num_blocks = 256; 8599 break; 8600 } 8601 case READ_10: 8602 case WRITE_10: { 8603 struct scsi_rw_10 *cdb; 8604 8605 cdb = (struct scsi_rw_10 *)ctsio->cdb; 8606 if (cdb->byte2 & SRW10_FUA) 8607 flags |= CTL_LLF_FUA; 8608 if (cdb->byte2 & SRW10_DPO) 8609 flags |= CTL_LLF_DPO; 8610 lba = scsi_4btoul(cdb->addr); 8611 num_blocks = scsi_2btoul(cdb->length); 8612 break; 8613 } 8614 case WRITE_VERIFY_10: { 8615 struct scsi_write_verify_10 *cdb; 8616 8617 cdb = (struct scsi_write_verify_10 *)ctsio->cdb; 8618 flags |= CTL_LLF_FUA; 8619 if (cdb->byte2 & SWV_DPO) 8620 flags |= CTL_LLF_DPO; 8621 lba = scsi_4btoul(cdb->addr); 8622 num_blocks = scsi_2btoul(cdb->length); 8623 break; 8624 } 8625 case READ_12: 8626 case WRITE_12: { 8627 struct scsi_rw_12 *cdb; 8628 8629 cdb = (struct scsi_rw_12 *)ctsio->cdb; 8630 if (cdb->byte2 & SRW12_FUA) 8631 flags |= CTL_LLF_FUA; 8632 if (cdb->byte2 & SRW12_DPO) 8633 flags |= CTL_LLF_DPO; 8634 lba = scsi_4btoul(cdb->addr); 8635 num_blocks = scsi_4btoul(cdb->length); 8636 break; 8637 } 8638 case WRITE_VERIFY_12: { 8639 struct scsi_write_verify_12 *cdb; 8640 8641 cdb = (struct scsi_write_verify_12 *)ctsio->cdb; 8642 flags |= CTL_LLF_FUA; 8643 if (cdb->byte2 & SWV_DPO) 8644 flags |= CTL_LLF_DPO; 8645 lba = scsi_4btoul(cdb->addr); 8646 num_blocks = scsi_4btoul(cdb->length); 8647 break; 8648 } 8649 case READ_16: 8650 case WRITE_16: { 8651 struct scsi_rw_16 *cdb; 8652 8653 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8654 if (cdb->byte2 & SRW12_FUA) 8655 flags |= CTL_LLF_FUA; 8656 if (cdb->byte2 & SRW12_DPO) 8657 flags |= CTL_LLF_DPO; 8658 lba = scsi_8btou64(cdb->addr); 8659 num_blocks = scsi_4btoul(cdb->length); 8660 break; 8661 } 8662 case WRITE_ATOMIC_16: { 8663 struct scsi_write_atomic_16 *cdb; 8664 8665 if (lun->be_lun->atomicblock == 0) { 8666 ctl_set_invalid_opcode(ctsio); 8667 ctl_done((union ctl_io *)ctsio); 8668 return (CTL_RETVAL_COMPLETE); 8669 } 8670 8671 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb; 8672 if (cdb->byte2 & SRW12_FUA) 8673 flags |= CTL_LLF_FUA; 8674 if (cdb->byte2 & SRW12_DPO) 8675 flags |= CTL_LLF_DPO; 8676 lba = scsi_8btou64(cdb->addr); 8677 num_blocks = scsi_2btoul(cdb->length); 8678 if (num_blocks > lun->be_lun->atomicblock) { 8679 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, 8680 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0, 8681 /*bit*/ 0); 8682 ctl_done((union ctl_io *)ctsio); 8683 return (CTL_RETVAL_COMPLETE); 8684 } 8685 break; 8686 } 8687 case WRITE_VERIFY_16: { 8688 struct scsi_write_verify_16 *cdb; 8689 8690 cdb = (struct scsi_write_verify_16 *)ctsio->cdb; 8691 flags |= CTL_LLF_FUA; 8692 if (cdb->byte2 & SWV_DPO) 8693 flags |= CTL_LLF_DPO; 8694 lba = scsi_8btou64(cdb->addr); 8695 num_blocks = scsi_4btoul(cdb->length); 8696 break; 8697 } 8698 default: 8699 /* 8700 * We got a command we don't support. This shouldn't 8701 * happen, commands should be filtered out above us. 8702 */ 8703 ctl_set_invalid_opcode(ctsio); 8704 ctl_done((union ctl_io *)ctsio); 8705 8706 return (CTL_RETVAL_COMPLETE); 8707 break; /* NOTREACHED */ 8708 } 8709 8710 /* 8711 * The first check is to make sure we're in bounds, the second 8712 * check is to catch wrap-around problems. If the lba + num blocks 8713 * is less than the lba, then we've wrapped around and the block 8714 * range is invalid anyway. 8715 */ 8716 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8717 || ((lba + num_blocks) < lba)) { 8718 ctl_set_lba_out_of_range(ctsio); 8719 ctl_done((union ctl_io *)ctsio); 8720 return (CTL_RETVAL_COMPLETE); 8721 } 8722 8723 /* 8724 * According to SBC-3, a transfer length of 0 is not an error. 8725 * Note that this cannot happen with WRITE(6) or READ(6), since 0 8726 * translates to 256 blocks for those commands. 8727 */ 8728 if (num_blocks == 0) { 8729 ctl_set_success(ctsio); 8730 ctl_done((union ctl_io *)ctsio); 8731 return (CTL_RETVAL_COMPLETE); 8732 } 8733 8734 /* Set FUA and/or DPO if caches are disabled. */ 8735 if (isread) { 8736 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8737 SCP_RCD) != 0) 8738 flags |= CTL_LLF_FUA | CTL_LLF_DPO; 8739 } else { 8740 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8741 SCP_WCE) == 0) 8742 flags |= CTL_LLF_FUA; 8743 } 8744 8745 lbalen = (struct ctl_lba_len_flags *) 8746 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8747 lbalen->lba = lba; 8748 lbalen->len = num_blocks; 8749 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags; 8750 8751 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8752 ctsio->kern_rel_offset = 0; 8753 8754 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n")); 8755 8756 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8757 return (retval); 8758} 8759 8760static int 8761ctl_cnw_cont(union ctl_io *io) 8762{ 8763 struct ctl_scsiio *ctsio; 8764 struct ctl_lun *lun; 8765 struct ctl_lba_len_flags *lbalen; 8766 int retval; 8767 8768 ctsio = &io->scsiio; 8769 ctsio->io_hdr.status = CTL_STATUS_NONE; 8770 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT; 8771 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8772 lbalen = (struct ctl_lba_len_flags *) 8773 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8774 lbalen->flags &= ~CTL_LLF_COMPARE; 8775 lbalen->flags |= CTL_LLF_WRITE; 8776 8777 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n")); 8778 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8779 return (retval); 8780} 8781 8782int 8783ctl_cnw(struct ctl_scsiio *ctsio) 8784{ 8785 struct ctl_lun *lun; 8786 struct ctl_lba_len_flags *lbalen; 8787 uint64_t lba; 8788 uint32_t num_blocks; 8789 int flags, retval; 8790 8791 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8792 8793 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0])); 8794 8795 flags = 0; 8796 switch (ctsio->cdb[0]) { 8797 case COMPARE_AND_WRITE: { 8798 struct scsi_compare_and_write *cdb; 8799 8800 cdb = (struct scsi_compare_and_write *)ctsio->cdb; 8801 if (cdb->byte2 & SRW10_FUA) 8802 flags |= CTL_LLF_FUA; 8803 if (cdb->byte2 & SRW10_DPO) 8804 flags |= CTL_LLF_DPO; 8805 lba = scsi_8btou64(cdb->addr); 8806 num_blocks = cdb->length; 8807 break; 8808 } 8809 default: 8810 /* 8811 * We got a command we don't support. This shouldn't 8812 * happen, commands should be filtered out above us. 8813 */ 8814 ctl_set_invalid_opcode(ctsio); 8815 ctl_done((union ctl_io *)ctsio); 8816 8817 return (CTL_RETVAL_COMPLETE); 8818 break; /* NOTREACHED */ 8819 } 8820 8821 /* 8822 * The first check is to make sure we're in bounds, the second 8823 * check is to catch wrap-around problems. If the lba + num blocks 8824 * is less than the lba, then we've wrapped around and the block 8825 * range is invalid anyway. 8826 */ 8827 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8828 || ((lba + num_blocks) < lba)) { 8829 ctl_set_lba_out_of_range(ctsio); 8830 ctl_done((union ctl_io *)ctsio); 8831 return (CTL_RETVAL_COMPLETE); 8832 } 8833 8834 /* 8835 * According to SBC-3, a transfer length of 0 is not an error. 8836 */ 8837 if (num_blocks == 0) { 8838 ctl_set_success(ctsio); 8839 ctl_done((union ctl_io *)ctsio); 8840 return (CTL_RETVAL_COMPLETE); 8841 } 8842 8843 /* Set FUA if write cache is disabled. */ 8844 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 & 8845 SCP_WCE) == 0) 8846 flags |= CTL_LLF_FUA; 8847 8848 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize; 8849 ctsio->kern_rel_offset = 0; 8850 8851 /* 8852 * Set the IO_CONT flag, so that if this I/O gets passed to 8853 * ctl_data_submit_done(), it'll get passed back to 8854 * ctl_ctl_cnw_cont() for further processing. 8855 */ 8856 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT; 8857 ctsio->io_cont = ctl_cnw_cont; 8858 8859 lbalen = (struct ctl_lba_len_flags *) 8860 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8861 lbalen->lba = lba; 8862 lbalen->len = num_blocks; 8863 lbalen->flags = CTL_LLF_COMPARE | flags; 8864 8865 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n")); 8866 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8867 return (retval); 8868} 8869 8870int 8871ctl_verify(struct ctl_scsiio *ctsio) 8872{ 8873 struct ctl_lun *lun; 8874 struct ctl_lba_len_flags *lbalen; 8875 uint64_t lba; 8876 uint32_t num_blocks; 8877 int bytchk, flags; 8878 int retval; 8879 8880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 8881 8882 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0])); 8883 8884 bytchk = 0; 8885 flags = CTL_LLF_FUA; 8886 switch (ctsio->cdb[0]) { 8887 case VERIFY_10: { 8888 struct scsi_verify_10 *cdb; 8889 8890 cdb = (struct scsi_verify_10 *)ctsio->cdb; 8891 if (cdb->byte2 & SVFY_BYTCHK) 8892 bytchk = 1; 8893 if (cdb->byte2 & SVFY_DPO) 8894 flags |= CTL_LLF_DPO; 8895 lba = scsi_4btoul(cdb->addr); 8896 num_blocks = scsi_2btoul(cdb->length); 8897 break; 8898 } 8899 case VERIFY_12: { 8900 struct scsi_verify_12 *cdb; 8901 8902 cdb = (struct scsi_verify_12 *)ctsio->cdb; 8903 if (cdb->byte2 & SVFY_BYTCHK) 8904 bytchk = 1; 8905 if (cdb->byte2 & SVFY_DPO) 8906 flags |= CTL_LLF_DPO; 8907 lba = scsi_4btoul(cdb->addr); 8908 num_blocks = scsi_4btoul(cdb->length); 8909 break; 8910 } 8911 case VERIFY_16: { 8912 struct scsi_rw_16 *cdb; 8913 8914 cdb = (struct scsi_rw_16 *)ctsio->cdb; 8915 if (cdb->byte2 & SVFY_BYTCHK) 8916 bytchk = 1; 8917 if (cdb->byte2 & SVFY_DPO) 8918 flags |= CTL_LLF_DPO; 8919 lba = scsi_8btou64(cdb->addr); 8920 num_blocks = scsi_4btoul(cdb->length); 8921 break; 8922 } 8923 default: 8924 /* 8925 * We got a command we don't support. This shouldn't 8926 * happen, commands should be filtered out above us. 8927 */ 8928 ctl_set_invalid_opcode(ctsio); 8929 ctl_done((union ctl_io *)ctsio); 8930 return (CTL_RETVAL_COMPLETE); 8931 } 8932 8933 /* 8934 * The first check is to make sure we're in bounds, the second 8935 * check is to catch wrap-around problems. If the lba + num blocks 8936 * is less than the lba, then we've wrapped around and the block 8937 * range is invalid anyway. 8938 */ 8939 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1)) 8940 || ((lba + num_blocks) < lba)) { 8941 ctl_set_lba_out_of_range(ctsio); 8942 ctl_done((union ctl_io *)ctsio); 8943 return (CTL_RETVAL_COMPLETE); 8944 } 8945 8946 /* 8947 * According to SBC-3, a transfer length of 0 is not an error. 8948 */ 8949 if (num_blocks == 0) { 8950 ctl_set_success(ctsio); 8951 ctl_done((union ctl_io *)ctsio); 8952 return (CTL_RETVAL_COMPLETE); 8953 } 8954 8955 lbalen = (struct ctl_lba_len_flags *) 8956 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 8957 lbalen->lba = lba; 8958 lbalen->len = num_blocks; 8959 if (bytchk) { 8960 lbalen->flags = CTL_LLF_COMPARE | flags; 8961 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize; 8962 } else { 8963 lbalen->flags = CTL_LLF_VERIFY | flags; 8964 ctsio->kern_total_len = 0; 8965 } 8966 ctsio->kern_rel_offset = 0; 8967 8968 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n")); 8969 retval = lun->backend->data_submit((union ctl_io *)ctsio); 8970 return (retval); 8971} 8972 8973int 8974ctl_report_luns(struct ctl_scsiio *ctsio) 8975{ 8976 struct ctl_softc *softc; 8977 struct scsi_report_luns *cdb; 8978 struct scsi_report_luns_data *lun_data; 8979 struct ctl_lun *lun, *request_lun; 8980 struct ctl_port *port; 8981 int num_luns, retval; 8982 uint32_t alloc_len, lun_datalen; 8983 int num_filled; 8984 uint32_t initidx, targ_lun_id, lun_id; 8985 8986 retval = CTL_RETVAL_COMPLETE; 8987 cdb = (struct scsi_report_luns *)ctsio->cdb; 8988 port = ctl_io_port(&ctsio->io_hdr); 8989 softc = port->ctl_softc; 8990 8991 CTL_DEBUG_PRINT(("ctl_report_luns\n")); 8992 8993 mtx_lock(&softc->ctl_lock); 8994 num_luns = 0; 8995 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) { 8996 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS) 8997 num_luns++; 8998 } 8999 mtx_unlock(&softc->ctl_lock); 9000 9001 switch (cdb->select_report) { 9002 case RPL_REPORT_DEFAULT: 9003 case RPL_REPORT_ALL: 9004 case RPL_REPORT_NONSUBSID: 9005 break; 9006 case RPL_REPORT_WELLKNOWN: 9007 case RPL_REPORT_ADMIN: 9008 case RPL_REPORT_CONGLOM: 9009 num_luns = 0; 9010 break; 9011 default: 9012 ctl_set_invalid_field(ctsio, 9013 /*sks_valid*/ 1, 9014 /*command*/ 1, 9015 /*field*/ 2, 9016 /*bit_valid*/ 0, 9017 /*bit*/ 0); 9018 ctl_done((union ctl_io *)ctsio); 9019 return (retval); 9020 break; /* NOTREACHED */ 9021 } 9022 9023 alloc_len = scsi_4btoul(cdb->length); 9024 /* 9025 * The initiator has to allocate at least 16 bytes for this request, 9026 * so he can at least get the header and the first LUN. Otherwise 9027 * we reject the request (per SPC-3 rev 14, section 6.21). 9028 */ 9029 if (alloc_len < (sizeof(struct scsi_report_luns_data) + 9030 sizeof(struct scsi_report_luns_lundata))) { 9031 ctl_set_invalid_field(ctsio, 9032 /*sks_valid*/ 1, 9033 /*command*/ 1, 9034 /*field*/ 6, 9035 /*bit_valid*/ 0, 9036 /*bit*/ 0); 9037 ctl_done((union ctl_io *)ctsio); 9038 return (retval); 9039 } 9040 9041 request_lun = (struct ctl_lun *) 9042 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9043 9044 lun_datalen = sizeof(*lun_data) + 9045 (num_luns * sizeof(struct scsi_report_luns_lundata)); 9046 9047 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO); 9048 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr; 9049 ctsio->kern_sg_entries = 0; 9050 9051 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9052 9053 mtx_lock(&softc->ctl_lock); 9054 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) { 9055 lun_id = ctl_lun_map_from_port(port, targ_lun_id); 9056 if (lun_id >= CTL_MAX_LUNS) 9057 continue; 9058 lun = softc->ctl_luns[lun_id]; 9059 if (lun == NULL) 9060 continue; 9061 9062 if (targ_lun_id <= 0xff) { 9063 /* 9064 * Peripheral addressing method, bus number 0. 9065 */ 9066 lun_data->luns[num_filled].lundata[0] = 9067 RPL_LUNDATA_ATYP_PERIPH; 9068 lun_data->luns[num_filled].lundata[1] = targ_lun_id; 9069 num_filled++; 9070 } else if (targ_lun_id <= 0x3fff) { 9071 /* 9072 * Flat addressing method. 9073 */ 9074 lun_data->luns[num_filled].lundata[0] = 9075 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8); 9076 lun_data->luns[num_filled].lundata[1] = 9077 (targ_lun_id & 0xff); 9078 num_filled++; 9079 } else if (targ_lun_id <= 0xffffff) { 9080 /* 9081 * Extended flat addressing method. 9082 */ 9083 lun_data->luns[num_filled].lundata[0] = 9084 RPL_LUNDATA_ATYP_EXTLUN | 0x12; 9085 scsi_ulto3b(targ_lun_id, 9086 &lun_data->luns[num_filled].lundata[1]); 9087 num_filled++; 9088 } else { 9089 printf("ctl_report_luns: bogus LUN number %jd, " 9090 "skipping\n", (intmax_t)targ_lun_id); 9091 } 9092 /* 9093 * According to SPC-3, rev 14 section 6.21: 9094 * 9095 * "The execution of a REPORT LUNS command to any valid and 9096 * installed logical unit shall clear the REPORTED LUNS DATA 9097 * HAS CHANGED unit attention condition for all logical 9098 * units of that target with respect to the requesting 9099 * initiator. A valid and installed logical unit is one 9100 * having a PERIPHERAL QUALIFIER of 000b in the standard 9101 * INQUIRY data (see 6.4.2)." 9102 * 9103 * If request_lun is NULL, the LUN this report luns command 9104 * was issued to is either disabled or doesn't exist. In that 9105 * case, we shouldn't clear any pending lun change unit 9106 * attention. 9107 */ 9108 if (request_lun != NULL) { 9109 mtx_lock(&lun->lun_lock); 9110 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE); 9111 mtx_unlock(&lun->lun_lock); 9112 } 9113 } 9114 mtx_unlock(&softc->ctl_lock); 9115 9116 /* 9117 * It's quite possible that we've returned fewer LUNs than we allocated 9118 * space for. Trim it. 9119 */ 9120 lun_datalen = sizeof(*lun_data) + 9121 (num_filled * sizeof(struct scsi_report_luns_lundata)); 9122 9123 if (lun_datalen < alloc_len) { 9124 ctsio->residual = alloc_len - lun_datalen; 9125 ctsio->kern_data_len = lun_datalen; 9126 ctsio->kern_total_len = lun_datalen; 9127 } else { 9128 ctsio->residual = 0; 9129 ctsio->kern_data_len = alloc_len; 9130 ctsio->kern_total_len = alloc_len; 9131 } 9132 ctsio->kern_data_resid = 0; 9133 ctsio->kern_rel_offset = 0; 9134 ctsio->kern_sg_entries = 0; 9135 9136 /* 9137 * We set this to the actual data length, regardless of how much 9138 * space we actually have to return results. If the user looks at 9139 * this value, he'll know whether or not he allocated enough space 9140 * and reissue the command if necessary. We don't support well 9141 * known logical units, so if the user asks for that, return none. 9142 */ 9143 scsi_ulto4b(lun_datalen - 8, lun_data->length); 9144 9145 /* 9146 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy 9147 * this request. 9148 */ 9149 ctl_set_success(ctsio); 9150 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9151 ctsio->be_move_done = ctl_config_move_done; 9152 ctl_datamove((union ctl_io *)ctsio); 9153 return (retval); 9154} 9155 9156int 9157ctl_request_sense(struct ctl_scsiio *ctsio) 9158{ 9159 struct scsi_request_sense *cdb; 9160 struct scsi_sense_data *sense_ptr; 9161 struct ctl_softc *ctl_softc; 9162 struct ctl_lun *lun; 9163 uint32_t initidx; 9164 int have_error; 9165 scsi_sense_data_type sense_format; 9166 ctl_ua_type ua_type; 9167 9168 cdb = (struct scsi_request_sense *)ctsio->cdb; 9169 9170 ctl_softc = control_softc; 9171 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9172 9173 CTL_DEBUG_PRINT(("ctl_request_sense\n")); 9174 9175 /* 9176 * Determine which sense format the user wants. 9177 */ 9178 if (cdb->byte2 & SRS_DESC) 9179 sense_format = SSD_TYPE_DESC; 9180 else 9181 sense_format = SSD_TYPE_FIXED; 9182 9183 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK); 9184 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr; 9185 ctsio->kern_sg_entries = 0; 9186 9187 /* 9188 * struct scsi_sense_data, which is currently set to 256 bytes, is 9189 * larger than the largest allowed value for the length field in the 9190 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4. 9191 */ 9192 ctsio->residual = 0; 9193 ctsio->kern_data_len = cdb->length; 9194 ctsio->kern_total_len = cdb->length; 9195 9196 ctsio->kern_data_resid = 0; 9197 ctsio->kern_rel_offset = 0; 9198 ctsio->kern_sg_entries = 0; 9199 9200 /* 9201 * If we don't have a LUN, we don't have any pending sense. 9202 */ 9203 if (lun == NULL) 9204 goto no_sense; 9205 9206 have_error = 0; 9207 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 9208 /* 9209 * Check for pending sense, and then for pending unit attentions. 9210 * Pending sense gets returned first, then pending unit attentions. 9211 */ 9212 mtx_lock(&lun->lun_lock); 9213#ifdef CTL_WITH_CA 9214 if (ctl_is_set(lun->have_ca, initidx)) { 9215 scsi_sense_data_type stored_format; 9216 9217 /* 9218 * Check to see which sense format was used for the stored 9219 * sense data. 9220 */ 9221 stored_format = scsi_sense_type(&lun->pending_sense[initidx]); 9222 9223 /* 9224 * If the user requested a different sense format than the 9225 * one we stored, then we need to convert it to the other 9226 * format. If we're going from descriptor to fixed format 9227 * sense data, we may lose things in translation, depending 9228 * on what options were used. 9229 * 9230 * If the stored format is SSD_TYPE_NONE (i.e. invalid), 9231 * for some reason we'll just copy it out as-is. 9232 */ 9233 if ((stored_format == SSD_TYPE_FIXED) 9234 && (sense_format == SSD_TYPE_DESC)) 9235 ctl_sense_to_desc((struct scsi_sense_data_fixed *) 9236 &lun->pending_sense[initidx], 9237 (struct scsi_sense_data_desc *)sense_ptr); 9238 else if ((stored_format == SSD_TYPE_DESC) 9239 && (sense_format == SSD_TYPE_FIXED)) 9240 ctl_sense_to_fixed((struct scsi_sense_data_desc *) 9241 &lun->pending_sense[initidx], 9242 (struct scsi_sense_data_fixed *)sense_ptr); 9243 else 9244 memcpy(sense_ptr, &lun->pending_sense[initidx], 9245 MIN(sizeof(*sense_ptr), 9246 sizeof(lun->pending_sense[initidx]))); 9247 9248 ctl_clear_mask(lun->have_ca, initidx); 9249 have_error = 1; 9250 } else 9251#endif 9252 { 9253 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format); 9254 if (ua_type != CTL_UA_NONE) 9255 have_error = 1; 9256 if (ua_type == CTL_UA_LUN_CHANGE) { 9257 mtx_unlock(&lun->lun_lock); 9258 mtx_lock(&ctl_softc->ctl_lock); 9259 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type); 9260 mtx_unlock(&ctl_softc->ctl_lock); 9261 mtx_lock(&lun->lun_lock); 9262 } 9263 9264 } 9265 mtx_unlock(&lun->lun_lock); 9266 9267 /* 9268 * We already have a pending error, return it. 9269 */ 9270 if (have_error != 0) { 9271 /* 9272 * We report the SCSI status as OK, since the status of the 9273 * request sense command itself is OK. 9274 * We report 0 for the sense length, because we aren't doing 9275 * autosense in this case. We're reporting sense as 9276 * parameter data. 9277 */ 9278 ctl_set_success(ctsio); 9279 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9280 ctsio->be_move_done = ctl_config_move_done; 9281 ctl_datamove((union ctl_io *)ctsio); 9282 return (CTL_RETVAL_COMPLETE); 9283 } 9284 9285no_sense: 9286 9287 /* 9288 * No sense information to report, so we report that everything is 9289 * okay. 9290 */ 9291 ctl_set_sense_data(sense_ptr, 9292 lun, 9293 sense_format, 9294 /*current_error*/ 1, 9295 /*sense_key*/ SSD_KEY_NO_SENSE, 9296 /*asc*/ 0x00, 9297 /*ascq*/ 0x00, 9298 SSD_ELEM_NONE); 9299 9300 /* 9301 * We report 0 for the sense length, because we aren't doing 9302 * autosense in this case. We're reporting sense as parameter data. 9303 */ 9304 ctl_set_success(ctsio); 9305 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9306 ctsio->be_move_done = ctl_config_move_done; 9307 ctl_datamove((union ctl_io *)ctsio); 9308 return (CTL_RETVAL_COMPLETE); 9309} 9310 9311int 9312ctl_tur(struct ctl_scsiio *ctsio) 9313{ 9314 9315 CTL_DEBUG_PRINT(("ctl_tur\n")); 9316 9317 ctl_set_success(ctsio); 9318 ctl_done((union ctl_io *)ctsio); 9319 9320 return (CTL_RETVAL_COMPLETE); 9321} 9322 9323/* 9324 * SCSI VPD page 0x00, the Supported VPD Pages page. 9325 */ 9326static int 9327ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len) 9328{ 9329 struct scsi_vpd_supported_pages *pages; 9330 int sup_page_size; 9331 struct ctl_lun *lun; 9332 int p; 9333 9334 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9335 9336 sup_page_size = sizeof(struct scsi_vpd_supported_pages) * 9337 SCSI_EVPD_NUM_SUPPORTED_PAGES; 9338 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO); 9339 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr; 9340 ctsio->kern_sg_entries = 0; 9341 9342 if (sup_page_size < alloc_len) { 9343 ctsio->residual = alloc_len - sup_page_size; 9344 ctsio->kern_data_len = sup_page_size; 9345 ctsio->kern_total_len = sup_page_size; 9346 } else { 9347 ctsio->residual = 0; 9348 ctsio->kern_data_len = alloc_len; 9349 ctsio->kern_total_len = alloc_len; 9350 } 9351 ctsio->kern_data_resid = 0; 9352 ctsio->kern_rel_offset = 0; 9353 ctsio->kern_sg_entries = 0; 9354 9355 /* 9356 * The control device is always connected. The disk device, on the 9357 * other hand, may not be online all the time. Need to change this 9358 * to figure out whether the disk device is actually online or not. 9359 */ 9360 if (lun != NULL) 9361 pages->device = (SID_QUAL_LU_CONNECTED << 5) | 9362 lun->be_lun->lun_type; 9363 else 9364 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9365 9366 p = 0; 9367 /* Supported VPD pages */ 9368 pages->page_list[p++] = SVPD_SUPPORTED_PAGES; 9369 /* Serial Number */ 9370 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER; 9371 /* Device Identification */ 9372 pages->page_list[p++] = SVPD_DEVICE_ID; 9373 /* Extended INQUIRY Data */ 9374 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA; 9375 /* Mode Page Policy */ 9376 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY; 9377 /* SCSI Ports */ 9378 pages->page_list[p++] = SVPD_SCSI_PORTS; 9379 /* Third-party Copy */ 9380 pages->page_list[p++] = SVPD_SCSI_TPC; 9381 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) { 9382 /* Block limits */ 9383 pages->page_list[p++] = SVPD_BLOCK_LIMITS; 9384 /* Block Device Characteristics */ 9385 pages->page_list[p++] = SVPD_BDC; 9386 /* Logical Block Provisioning */ 9387 pages->page_list[p++] = SVPD_LBP; 9388 } 9389 pages->length = p; 9390 9391 ctl_set_success(ctsio); 9392 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9393 ctsio->be_move_done = ctl_config_move_done; 9394 ctl_datamove((union ctl_io *)ctsio); 9395 return (CTL_RETVAL_COMPLETE); 9396} 9397 9398/* 9399 * SCSI VPD page 0x80, the Unit Serial Number page. 9400 */ 9401static int 9402ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len) 9403{ 9404 struct scsi_vpd_unit_serial_number *sn_ptr; 9405 struct ctl_lun *lun; 9406 int data_len; 9407 9408 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9409 9410 data_len = 4 + CTL_SN_LEN; 9411 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9412 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr; 9413 if (data_len < alloc_len) { 9414 ctsio->residual = alloc_len - data_len; 9415 ctsio->kern_data_len = data_len; 9416 ctsio->kern_total_len = data_len; 9417 } else { 9418 ctsio->residual = 0; 9419 ctsio->kern_data_len = alloc_len; 9420 ctsio->kern_total_len = alloc_len; 9421 } 9422 ctsio->kern_data_resid = 0; 9423 ctsio->kern_rel_offset = 0; 9424 ctsio->kern_sg_entries = 0; 9425 9426 /* 9427 * The control device is always connected. The disk device, on the 9428 * other hand, may not be online all the time. Need to change this 9429 * to figure out whether the disk device is actually online or not. 9430 */ 9431 if (lun != NULL) 9432 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9433 lun->be_lun->lun_type; 9434 else 9435 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9436 9437 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER; 9438 sn_ptr->length = CTL_SN_LEN; 9439 /* 9440 * If we don't have a LUN, we just leave the serial number as 9441 * all spaces. 9442 */ 9443 if (lun != NULL) { 9444 strncpy((char *)sn_ptr->serial_num, 9445 (char *)lun->be_lun->serial_num, CTL_SN_LEN); 9446 } else 9447 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN); 9448 9449 ctl_set_success(ctsio); 9450 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9451 ctsio->be_move_done = ctl_config_move_done; 9452 ctl_datamove((union ctl_io *)ctsio); 9453 return (CTL_RETVAL_COMPLETE); 9454} 9455 9456 9457/* 9458 * SCSI VPD page 0x86, the Extended INQUIRY Data page. 9459 */ 9460static int 9461ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len) 9462{ 9463 struct scsi_vpd_extended_inquiry_data *eid_ptr; 9464 struct ctl_lun *lun; 9465 int data_len; 9466 9467 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9468 9469 data_len = sizeof(struct scsi_vpd_extended_inquiry_data); 9470 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9471 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr; 9472 ctsio->kern_sg_entries = 0; 9473 9474 if (data_len < alloc_len) { 9475 ctsio->residual = alloc_len - data_len; 9476 ctsio->kern_data_len = data_len; 9477 ctsio->kern_total_len = data_len; 9478 } else { 9479 ctsio->residual = 0; 9480 ctsio->kern_data_len = alloc_len; 9481 ctsio->kern_total_len = alloc_len; 9482 } 9483 ctsio->kern_data_resid = 0; 9484 ctsio->kern_rel_offset = 0; 9485 ctsio->kern_sg_entries = 0; 9486 9487 /* 9488 * The control device is always connected. The disk device, on the 9489 * other hand, may not be online all the time. 9490 */ 9491 if (lun != NULL) 9492 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9493 lun->be_lun->lun_type; 9494 else 9495 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9496 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA; 9497 scsi_ulto2b(data_len - 4, eid_ptr->page_length); 9498 /* 9499 * We support head of queue, ordered and simple tags. 9500 */ 9501 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP; 9502 /* 9503 * Volatile cache supported. 9504 */ 9505 eid_ptr->flags3 = SVPD_EID_V_SUP; 9506 9507 /* 9508 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit 9509 * attention for a particular IT nexus on all LUNs once we report 9510 * it to that nexus once. This bit is required as of SPC-4. 9511 */ 9512 eid_ptr->flags4 = SVPD_EID_LUICLT; 9513 9514 /* 9515 * XXX KDM in order to correctly answer this, we would need 9516 * information from the SIM to determine how much sense data it 9517 * can send. So this would really be a path inquiry field, most 9518 * likely. This can be set to a maximum of 252 according to SPC-4, 9519 * but the hardware may or may not be able to support that much. 9520 * 0 just means that the maximum sense data length is not reported. 9521 */ 9522 eid_ptr->max_sense_length = 0; 9523 9524 ctl_set_success(ctsio); 9525 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9526 ctsio->be_move_done = ctl_config_move_done; 9527 ctl_datamove((union ctl_io *)ctsio); 9528 return (CTL_RETVAL_COMPLETE); 9529} 9530 9531static int 9532ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len) 9533{ 9534 struct scsi_vpd_mode_page_policy *mpp_ptr; 9535 struct ctl_lun *lun; 9536 int data_len; 9537 9538 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9539 9540 data_len = sizeof(struct scsi_vpd_mode_page_policy) + 9541 sizeof(struct scsi_vpd_mode_page_policy_descr); 9542 9543 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9544 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr; 9545 ctsio->kern_sg_entries = 0; 9546 9547 if (data_len < alloc_len) { 9548 ctsio->residual = alloc_len - data_len; 9549 ctsio->kern_data_len = data_len; 9550 ctsio->kern_total_len = data_len; 9551 } else { 9552 ctsio->residual = 0; 9553 ctsio->kern_data_len = alloc_len; 9554 ctsio->kern_total_len = alloc_len; 9555 } 9556 ctsio->kern_data_resid = 0; 9557 ctsio->kern_rel_offset = 0; 9558 ctsio->kern_sg_entries = 0; 9559 9560 /* 9561 * The control device is always connected. The disk device, on the 9562 * other hand, may not be online all the time. 9563 */ 9564 if (lun != NULL) 9565 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9566 lun->be_lun->lun_type; 9567 else 9568 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9569 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY; 9570 scsi_ulto2b(data_len - 4, mpp_ptr->page_length); 9571 mpp_ptr->descr[0].page_code = 0x3f; 9572 mpp_ptr->descr[0].subpage_code = 0xff; 9573 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED; 9574 9575 ctl_set_success(ctsio); 9576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9577 ctsio->be_move_done = ctl_config_move_done; 9578 ctl_datamove((union ctl_io *)ctsio); 9579 return (CTL_RETVAL_COMPLETE); 9580} 9581 9582/* 9583 * SCSI VPD page 0x83, the Device Identification page. 9584 */ 9585static int 9586ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len) 9587{ 9588 struct scsi_vpd_device_id *devid_ptr; 9589 struct scsi_vpd_id_descriptor *desc; 9590 struct ctl_softc *softc; 9591 struct ctl_lun *lun; 9592 struct ctl_port *port; 9593 int data_len; 9594 uint8_t proto; 9595 9596 softc = control_softc; 9597 9598 port = ctl_io_port(&ctsio->io_hdr); 9599 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9600 9601 data_len = sizeof(struct scsi_vpd_device_id) + 9602 sizeof(struct scsi_vpd_id_descriptor) + 9603 sizeof(struct scsi_vpd_id_rel_trgt_port_id) + 9604 sizeof(struct scsi_vpd_id_descriptor) + 9605 sizeof(struct scsi_vpd_id_trgt_port_grp_id); 9606 if (lun && lun->lun_devid) 9607 data_len += lun->lun_devid->len; 9608 if (port && port->port_devid) 9609 data_len += port->port_devid->len; 9610 if (port && port->target_devid) 9611 data_len += port->target_devid->len; 9612 9613 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9614 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr; 9615 ctsio->kern_sg_entries = 0; 9616 9617 if (data_len < alloc_len) { 9618 ctsio->residual = alloc_len - data_len; 9619 ctsio->kern_data_len = data_len; 9620 ctsio->kern_total_len = data_len; 9621 } else { 9622 ctsio->residual = 0; 9623 ctsio->kern_data_len = alloc_len; 9624 ctsio->kern_total_len = alloc_len; 9625 } 9626 ctsio->kern_data_resid = 0; 9627 ctsio->kern_rel_offset = 0; 9628 ctsio->kern_sg_entries = 0; 9629 9630 /* 9631 * The control device is always connected. The disk device, on the 9632 * other hand, may not be online all the time. 9633 */ 9634 if (lun != NULL) 9635 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9636 lun->be_lun->lun_type; 9637 else 9638 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9639 devid_ptr->page_code = SVPD_DEVICE_ID; 9640 scsi_ulto2b(data_len - 4, devid_ptr->length); 9641 9642 if (port && port->port_type == CTL_PORT_FC) 9643 proto = SCSI_PROTO_FC << 4; 9644 else if (port && port->port_type == CTL_PORT_ISCSI) 9645 proto = SCSI_PROTO_ISCSI << 4; 9646 else 9647 proto = SCSI_PROTO_SPI << 4; 9648 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list; 9649 9650 /* 9651 * We're using a LUN association here. i.e., this device ID is a 9652 * per-LUN identifier. 9653 */ 9654 if (lun && lun->lun_devid) { 9655 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len); 9656 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9657 lun->lun_devid->len); 9658 } 9659 9660 /* 9661 * This is for the WWPN which is a port association. 9662 */ 9663 if (port && port->port_devid) { 9664 memcpy(desc, port->port_devid->data, port->port_devid->len); 9665 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc + 9666 port->port_devid->len); 9667 } 9668 9669 /* 9670 * This is for the Relative Target Port(type 4h) identifier 9671 */ 9672 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9673 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9674 SVPD_ID_TYPE_RELTARG; 9675 desc->length = 4; 9676 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]); 9677 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9678 sizeof(struct scsi_vpd_id_rel_trgt_port_id)); 9679 9680 /* 9681 * This is for the Target Port Group(type 5h) identifier 9682 */ 9683 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY; 9684 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | 9685 SVPD_ID_TYPE_TPORTGRP; 9686 desc->length = 4; 9687 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1, 9688 &desc->identifier[2]); 9689 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] + 9690 sizeof(struct scsi_vpd_id_trgt_port_grp_id)); 9691 9692 /* 9693 * This is for the Target identifier 9694 */ 9695 if (port && port->target_devid) { 9696 memcpy(desc, port->target_devid->data, port->target_devid->len); 9697 } 9698 9699 ctl_set_success(ctsio); 9700 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9701 ctsio->be_move_done = ctl_config_move_done; 9702 ctl_datamove((union ctl_io *)ctsio); 9703 return (CTL_RETVAL_COMPLETE); 9704} 9705 9706static int 9707ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len) 9708{ 9709 struct ctl_softc *softc = control_softc; 9710 struct scsi_vpd_scsi_ports *sp; 9711 struct scsi_vpd_port_designation *pd; 9712 struct scsi_vpd_port_designation_cont *pdc; 9713 struct ctl_lun *lun; 9714 struct ctl_port *port; 9715 int data_len, num_target_ports, iid_len, id_len; 9716 9717 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9718 9719 num_target_ports = 0; 9720 iid_len = 0; 9721 id_len = 0; 9722 mtx_lock(&softc->ctl_lock); 9723 STAILQ_FOREACH(port, &softc->port_list, links) { 9724 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9725 continue; 9726 if (lun != NULL && 9727 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9728 continue; 9729 num_target_ports++; 9730 if (port->init_devid) 9731 iid_len += port->init_devid->len; 9732 if (port->port_devid) 9733 id_len += port->port_devid->len; 9734 } 9735 mtx_unlock(&softc->ctl_lock); 9736 9737 data_len = sizeof(struct scsi_vpd_scsi_ports) + 9738 num_target_ports * (sizeof(struct scsi_vpd_port_designation) + 9739 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len; 9740 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 9741 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr; 9742 ctsio->kern_sg_entries = 0; 9743 9744 if (data_len < alloc_len) { 9745 ctsio->residual = alloc_len - data_len; 9746 ctsio->kern_data_len = data_len; 9747 ctsio->kern_total_len = data_len; 9748 } else { 9749 ctsio->residual = 0; 9750 ctsio->kern_data_len = alloc_len; 9751 ctsio->kern_total_len = alloc_len; 9752 } 9753 ctsio->kern_data_resid = 0; 9754 ctsio->kern_rel_offset = 0; 9755 ctsio->kern_sg_entries = 0; 9756 9757 /* 9758 * The control device is always connected. The disk device, on the 9759 * other hand, may not be online all the time. Need to change this 9760 * to figure out whether the disk device is actually online or not. 9761 */ 9762 if (lun != NULL) 9763 sp->device = (SID_QUAL_LU_CONNECTED << 5) | 9764 lun->be_lun->lun_type; 9765 else 9766 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9767 9768 sp->page_code = SVPD_SCSI_PORTS; 9769 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports), 9770 sp->page_length); 9771 pd = &sp->design[0]; 9772 9773 mtx_lock(&softc->ctl_lock); 9774 STAILQ_FOREACH(port, &softc->port_list, links) { 9775 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0) 9776 continue; 9777 if (lun != NULL && 9778 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 9779 continue; 9780 scsi_ulto2b(port->targ_port, pd->relative_port_id); 9781 if (port->init_devid) { 9782 iid_len = port->init_devid->len; 9783 memcpy(pd->initiator_transportid, 9784 port->init_devid->data, port->init_devid->len); 9785 } else 9786 iid_len = 0; 9787 scsi_ulto2b(iid_len, pd->initiator_transportid_length); 9788 pdc = (struct scsi_vpd_port_designation_cont *) 9789 (&pd->initiator_transportid[iid_len]); 9790 if (port->port_devid) { 9791 id_len = port->port_devid->len; 9792 memcpy(pdc->target_port_descriptors, 9793 port->port_devid->data, port->port_devid->len); 9794 } else 9795 id_len = 0; 9796 scsi_ulto2b(id_len, pdc->target_port_descriptors_length); 9797 pd = (struct scsi_vpd_port_designation *) 9798 ((uint8_t *)pdc->target_port_descriptors + id_len); 9799 } 9800 mtx_unlock(&softc->ctl_lock); 9801 9802 ctl_set_success(ctsio); 9803 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9804 ctsio->be_move_done = ctl_config_move_done; 9805 ctl_datamove((union ctl_io *)ctsio); 9806 return (CTL_RETVAL_COMPLETE); 9807} 9808 9809static int 9810ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len) 9811{ 9812 struct scsi_vpd_block_limits *bl_ptr; 9813 struct ctl_lun *lun; 9814 9815 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9816 9817 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO); 9818 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr; 9819 ctsio->kern_sg_entries = 0; 9820 9821 if (sizeof(*bl_ptr) < alloc_len) { 9822 ctsio->residual = alloc_len - sizeof(*bl_ptr); 9823 ctsio->kern_data_len = sizeof(*bl_ptr); 9824 ctsio->kern_total_len = sizeof(*bl_ptr); 9825 } else { 9826 ctsio->residual = 0; 9827 ctsio->kern_data_len = alloc_len; 9828 ctsio->kern_total_len = alloc_len; 9829 } 9830 ctsio->kern_data_resid = 0; 9831 ctsio->kern_rel_offset = 0; 9832 ctsio->kern_sg_entries = 0; 9833 9834 /* 9835 * The control device is always connected. The disk device, on the 9836 * other hand, may not be online all the time. Need to change this 9837 * to figure out whether the disk device is actually online or not. 9838 */ 9839 if (lun != NULL) 9840 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9841 lun->be_lun->lun_type; 9842 else 9843 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9844 9845 bl_ptr->page_code = SVPD_BLOCK_LIMITS; 9846 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length); 9847 bl_ptr->max_cmp_write_len = 0xff; 9848 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len); 9849 if (lun != NULL) { 9850 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len); 9851 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9852 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt); 9853 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt); 9854 if (lun->be_lun->ublockexp != 0) { 9855 scsi_ulto4b((1 << lun->be_lun->ublockexp), 9856 bl_ptr->opt_unmap_grain); 9857 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff, 9858 bl_ptr->unmap_grain_align); 9859 } 9860 } 9861 scsi_ulto4b(lun->be_lun->atomicblock, 9862 bl_ptr->max_atomic_transfer_length); 9863 scsi_ulto4b(0, bl_ptr->atomic_alignment); 9864 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity); 9865 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary); 9866 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size); 9867 } 9868 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length); 9869 9870 ctl_set_success(ctsio); 9871 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9872 ctsio->be_move_done = ctl_config_move_done; 9873 ctl_datamove((union ctl_io *)ctsio); 9874 return (CTL_RETVAL_COMPLETE); 9875} 9876 9877static int 9878ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len) 9879{ 9880 struct scsi_vpd_block_device_characteristics *bdc_ptr; 9881 struct ctl_lun *lun; 9882 const char *value; 9883 u_int i; 9884 9885 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9886 9887 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO); 9888 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr; 9889 ctsio->kern_sg_entries = 0; 9890 9891 if (sizeof(*bdc_ptr) < alloc_len) { 9892 ctsio->residual = alloc_len - sizeof(*bdc_ptr); 9893 ctsio->kern_data_len = sizeof(*bdc_ptr); 9894 ctsio->kern_total_len = sizeof(*bdc_ptr); 9895 } else { 9896 ctsio->residual = 0; 9897 ctsio->kern_data_len = alloc_len; 9898 ctsio->kern_total_len = alloc_len; 9899 } 9900 ctsio->kern_data_resid = 0; 9901 ctsio->kern_rel_offset = 0; 9902 ctsio->kern_sg_entries = 0; 9903 9904 /* 9905 * The control device is always connected. The disk device, on the 9906 * other hand, may not be online all the time. Need to change this 9907 * to figure out whether the disk device is actually online or not. 9908 */ 9909 if (lun != NULL) 9910 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9911 lun->be_lun->lun_type; 9912 else 9913 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9914 bdc_ptr->page_code = SVPD_BDC; 9915 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length); 9916 if (lun != NULL && 9917 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL) 9918 i = strtol(value, NULL, 0); 9919 else 9920 i = CTL_DEFAULT_ROTATION_RATE; 9921 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate); 9922 if (lun != NULL && 9923 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL) 9924 i = strtol(value, NULL, 0); 9925 else 9926 i = 0; 9927 bdc_ptr->wab_wac_ff = (i & 0x0f); 9928 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS; 9929 9930 ctl_set_success(ctsio); 9931 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9932 ctsio->be_move_done = ctl_config_move_done; 9933 ctl_datamove((union ctl_io *)ctsio); 9934 return (CTL_RETVAL_COMPLETE); 9935} 9936 9937static int 9938ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len) 9939{ 9940 struct scsi_vpd_logical_block_prov *lbp_ptr; 9941 struct ctl_lun *lun; 9942 9943 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 9944 9945 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO); 9946 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr; 9947 ctsio->kern_sg_entries = 0; 9948 9949 if (sizeof(*lbp_ptr) < alloc_len) { 9950 ctsio->residual = alloc_len - sizeof(*lbp_ptr); 9951 ctsio->kern_data_len = sizeof(*lbp_ptr); 9952 ctsio->kern_total_len = sizeof(*lbp_ptr); 9953 } else { 9954 ctsio->residual = 0; 9955 ctsio->kern_data_len = alloc_len; 9956 ctsio->kern_total_len = alloc_len; 9957 } 9958 ctsio->kern_data_resid = 0; 9959 ctsio->kern_rel_offset = 0; 9960 ctsio->kern_sg_entries = 0; 9961 9962 /* 9963 * The control device is always connected. The disk device, on the 9964 * other hand, may not be online all the time. Need to change this 9965 * to figure out whether the disk device is actually online or not. 9966 */ 9967 if (lun != NULL) 9968 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 9969 lun->be_lun->lun_type; 9970 else 9971 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT; 9972 9973 lbp_ptr->page_code = SVPD_LBP; 9974 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length); 9975 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT; 9976 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) { 9977 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | 9978 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP; 9979 lbp_ptr->prov_type = SVPD_LBP_THIN; 9980 } 9981 9982 ctl_set_success(ctsio); 9983 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 9984 ctsio->be_move_done = ctl_config_move_done; 9985 ctl_datamove((union ctl_io *)ctsio); 9986 return (CTL_RETVAL_COMPLETE); 9987} 9988 9989/* 9990 * INQUIRY with the EVPD bit set. 9991 */ 9992static int 9993ctl_inquiry_evpd(struct ctl_scsiio *ctsio) 9994{ 9995 struct ctl_lun *lun; 9996 struct scsi_inquiry *cdb; 9997 int alloc_len, retval; 9998 9999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10000 cdb = (struct scsi_inquiry *)ctsio->cdb; 10001 alloc_len = scsi_2btoul(cdb->length); 10002 10003 switch (cdb->page_code) { 10004 case SVPD_SUPPORTED_PAGES: 10005 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len); 10006 break; 10007 case SVPD_UNIT_SERIAL_NUMBER: 10008 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len); 10009 break; 10010 case SVPD_DEVICE_ID: 10011 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len); 10012 break; 10013 case SVPD_EXTENDED_INQUIRY_DATA: 10014 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len); 10015 break; 10016 case SVPD_MODE_PAGE_POLICY: 10017 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len); 10018 break; 10019 case SVPD_SCSI_PORTS: 10020 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len); 10021 break; 10022 case SVPD_SCSI_TPC: 10023 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len); 10024 break; 10025 case SVPD_BLOCK_LIMITS: 10026 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10027 goto err; 10028 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len); 10029 break; 10030 case SVPD_BDC: 10031 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10032 goto err; 10033 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len); 10034 break; 10035 case SVPD_LBP: 10036 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT) 10037 goto err; 10038 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len); 10039 break; 10040 default: 10041err: 10042 ctl_set_invalid_field(ctsio, 10043 /*sks_valid*/ 1, 10044 /*command*/ 1, 10045 /*field*/ 2, 10046 /*bit_valid*/ 0, 10047 /*bit*/ 0); 10048 ctl_done((union ctl_io *)ctsio); 10049 retval = CTL_RETVAL_COMPLETE; 10050 break; 10051 } 10052 10053 return (retval); 10054} 10055 10056/* 10057 * Standard INQUIRY data. 10058 */ 10059static int 10060ctl_inquiry_std(struct ctl_scsiio *ctsio) 10061{ 10062 struct scsi_inquiry_data *inq_ptr; 10063 struct scsi_inquiry *cdb; 10064 struct ctl_softc *softc = control_softc; 10065 struct ctl_port *port; 10066 struct ctl_lun *lun; 10067 char *val; 10068 uint32_t alloc_len, data_len; 10069 ctl_port_type port_type; 10070 10071 port = ctl_io_port(&ctsio->io_hdr); 10072 port_type = port->port_type; 10073 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL) 10074 port_type = CTL_PORT_SCSI; 10075 10076 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10077 cdb = (struct scsi_inquiry *)ctsio->cdb; 10078 alloc_len = scsi_2btoul(cdb->length); 10079 10080 /* 10081 * We malloc the full inquiry data size here and fill it 10082 * in. If the user only asks for less, we'll give him 10083 * that much. 10084 */ 10085 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1); 10086 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10087 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr; 10088 ctsio->kern_sg_entries = 0; 10089 ctsio->kern_data_resid = 0; 10090 ctsio->kern_rel_offset = 0; 10091 10092 if (data_len < alloc_len) { 10093 ctsio->residual = alloc_len - data_len; 10094 ctsio->kern_data_len = data_len; 10095 ctsio->kern_total_len = data_len; 10096 } else { 10097 ctsio->residual = 0; 10098 ctsio->kern_data_len = alloc_len; 10099 ctsio->kern_total_len = alloc_len; 10100 } 10101 10102 if (lun != NULL) { 10103 if ((lun->flags & CTL_LUN_PRIMARY_SC) || 10104 softc->ha_link >= CTL_HA_LINK_UNKNOWN) { 10105 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) | 10106 lun->be_lun->lun_type; 10107 } else { 10108 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | 10109 lun->be_lun->lun_type; 10110 } 10111 if (lun->flags & CTL_LUN_REMOVABLE) 10112 inq_ptr->dev_qual2 |= SID_RMB; 10113 } else 10114 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE; 10115 10116 /* RMB in byte 2 is 0 */ 10117 inq_ptr->version = SCSI_REV_SPC4; 10118 10119 /* 10120 * According to SAM-3, even if a device only supports a single 10121 * level of LUN addressing, it should still set the HISUP bit: 10122 * 10123 * 4.9.1 Logical unit numbers overview 10124 * 10125 * All logical unit number formats described in this standard are 10126 * hierarchical in structure even when only a single level in that 10127 * hierarchy is used. The HISUP bit shall be set to one in the 10128 * standard INQUIRY data (see SPC-2) when any logical unit number 10129 * format described in this standard is used. Non-hierarchical 10130 * formats are outside the scope of this standard. 10131 * 10132 * Therefore we set the HiSup bit here. 10133 * 10134 * The reponse format is 2, per SPC-3. 10135 */ 10136 inq_ptr->response_format = SID_HiSup | 2; 10137 10138 inq_ptr->additional_length = data_len - 10139 (offsetof(struct scsi_inquiry_data, additional_length) + 1); 10140 CTL_DEBUG_PRINT(("additional_length = %d\n", 10141 inq_ptr->additional_length)); 10142 10143 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT; 10144 if (port_type == CTL_PORT_SCSI) 10145 inq_ptr->spc2_flags = SPC2_SID_ADDR16; 10146 inq_ptr->spc2_flags |= SPC2_SID_MultiP; 10147 inq_ptr->flags = SID_CmdQue; 10148 if (port_type == CTL_PORT_SCSI) 10149 inq_ptr->flags |= SID_WBus16 | SID_Sync; 10150 10151 /* 10152 * Per SPC-3, unused bytes in ASCII strings are filled with spaces. 10153 * We have 8 bytes for the vendor name, and 16 bytes for the device 10154 * name and 4 bytes for the revision. 10155 */ 10156 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10157 "vendor")) == NULL) { 10158 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor)); 10159 } else { 10160 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor)); 10161 strncpy(inq_ptr->vendor, val, 10162 min(sizeof(inq_ptr->vendor), strlen(val))); 10163 } 10164 if (lun == NULL) { 10165 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10166 sizeof(inq_ptr->product)); 10167 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) { 10168 switch (lun->be_lun->lun_type) { 10169 case T_DIRECT: 10170 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT, 10171 sizeof(inq_ptr->product)); 10172 break; 10173 case T_PROCESSOR: 10174 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT, 10175 sizeof(inq_ptr->product)); 10176 break; 10177 case T_CDROM: 10178 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT, 10179 sizeof(inq_ptr->product)); 10180 break; 10181 default: 10182 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT, 10183 sizeof(inq_ptr->product)); 10184 break; 10185 } 10186 } else { 10187 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product)); 10188 strncpy(inq_ptr->product, val, 10189 min(sizeof(inq_ptr->product), strlen(val))); 10190 } 10191 10192 /* 10193 * XXX make this a macro somewhere so it automatically gets 10194 * incremented when we make changes. 10195 */ 10196 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options, 10197 "revision")) == NULL) { 10198 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision)); 10199 } else { 10200 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision)); 10201 strncpy(inq_ptr->revision, val, 10202 min(sizeof(inq_ptr->revision), strlen(val))); 10203 } 10204 10205 /* 10206 * For parallel SCSI, we support double transition and single 10207 * transition clocking. We also support QAS (Quick Arbitration 10208 * and Selection) and Information Unit transfers on both the 10209 * control and array devices. 10210 */ 10211 if (port_type == CTL_PORT_SCSI) 10212 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS | 10213 SID_SPI_IUS; 10214 10215 /* SAM-5 (no version claimed) */ 10216 scsi_ulto2b(0x00A0, inq_ptr->version1); 10217 /* SPC-4 (no version claimed) */ 10218 scsi_ulto2b(0x0460, inq_ptr->version2); 10219 if (port_type == CTL_PORT_FC) { 10220 /* FCP-2 ANSI INCITS.350:2003 */ 10221 scsi_ulto2b(0x0917, inq_ptr->version3); 10222 } else if (port_type == CTL_PORT_SCSI) { 10223 /* SPI-4 ANSI INCITS.362:200x */ 10224 scsi_ulto2b(0x0B56, inq_ptr->version3); 10225 } else if (port_type == CTL_PORT_ISCSI) { 10226 /* iSCSI (no version claimed) */ 10227 scsi_ulto2b(0x0960, inq_ptr->version3); 10228 } else if (port_type == CTL_PORT_SAS) { 10229 /* SAS (no version claimed) */ 10230 scsi_ulto2b(0x0BE0, inq_ptr->version3); 10231 } 10232 10233 if (lun == NULL) { 10234 /* SBC-4 (no version claimed) */ 10235 scsi_ulto2b(0x0600, inq_ptr->version4); 10236 } else { 10237 switch (lun->be_lun->lun_type) { 10238 case T_DIRECT: 10239 /* SBC-4 (no version claimed) */ 10240 scsi_ulto2b(0x0600, inq_ptr->version4); 10241 break; 10242 case T_PROCESSOR: 10243 break; 10244 case T_CDROM: 10245 /* MMC-6 (no version claimed) */ 10246 scsi_ulto2b(0x04E0, inq_ptr->version4); 10247 break; 10248 default: 10249 break; 10250 } 10251 } 10252 10253 ctl_set_success(ctsio); 10254 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10255 ctsio->be_move_done = ctl_config_move_done; 10256 ctl_datamove((union ctl_io *)ctsio); 10257 return (CTL_RETVAL_COMPLETE); 10258} 10259 10260int 10261ctl_inquiry(struct ctl_scsiio *ctsio) 10262{ 10263 struct scsi_inquiry *cdb; 10264 int retval; 10265 10266 CTL_DEBUG_PRINT(("ctl_inquiry\n")); 10267 10268 cdb = (struct scsi_inquiry *)ctsio->cdb; 10269 if (cdb->byte2 & SI_EVPD) 10270 retval = ctl_inquiry_evpd(ctsio); 10271 else if (cdb->page_code == 0) 10272 retval = ctl_inquiry_std(ctsio); 10273 else { 10274 ctl_set_invalid_field(ctsio, 10275 /*sks_valid*/ 1, 10276 /*command*/ 1, 10277 /*field*/ 2, 10278 /*bit_valid*/ 0, 10279 /*bit*/ 0); 10280 ctl_done((union ctl_io *)ctsio); 10281 return (CTL_RETVAL_COMPLETE); 10282 } 10283 10284 return (retval); 10285} 10286 10287int 10288ctl_get_config(struct ctl_scsiio *ctsio) 10289{ 10290 struct scsi_get_config_header *hdr; 10291 struct scsi_get_config_feature *feature; 10292 struct scsi_get_config *cdb; 10293 struct ctl_lun *lun; 10294 uint32_t alloc_len, data_len; 10295 int rt, starting; 10296 10297 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10298 cdb = (struct scsi_get_config *)ctsio->cdb; 10299 rt = (cdb->rt & SGC_RT_MASK); 10300 starting = scsi_2btoul(cdb->starting_feature); 10301 alloc_len = scsi_2btoul(cdb->length); 10302 10303 data_len = sizeof(struct scsi_get_config_header) + 10304 sizeof(struct scsi_get_config_feature) + 8 + 10305 sizeof(struct scsi_get_config_feature) + 8 + 10306 sizeof(struct scsi_get_config_feature) + 4 + 10307 sizeof(struct scsi_get_config_feature) + 4 + 10308 sizeof(struct scsi_get_config_feature) + 8 + 10309 sizeof(struct scsi_get_config_feature) + 10310 sizeof(struct scsi_get_config_feature) + 4 + 10311 sizeof(struct scsi_get_config_feature) + 4 + 10312 sizeof(struct scsi_get_config_feature) + 4 + 10313 sizeof(struct scsi_get_config_feature) + 4 + 10314 sizeof(struct scsi_get_config_feature) + 4 + 10315 sizeof(struct scsi_get_config_feature) + 4; 10316 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10317 ctsio->kern_sg_entries = 0; 10318 ctsio->kern_data_resid = 0; 10319 ctsio->kern_rel_offset = 0; 10320 10321 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr; 10322 if (lun->flags & CTL_LUN_NO_MEDIA) 10323 scsi_ulto2b(0x0000, hdr->current_profile); 10324 else 10325 scsi_ulto2b(0x0010, hdr->current_profile); 10326 feature = (struct scsi_get_config_feature *)(hdr + 1); 10327 10328 if (starting > 0x003b) 10329 goto done; 10330 if (starting > 0x003a) 10331 goto f3b; 10332 if (starting > 0x002b) 10333 goto f3a; 10334 if (starting > 0x002a) 10335 goto f2b; 10336 if (starting > 0x001f) 10337 goto f2a; 10338 if (starting > 0x001e) 10339 goto f1f; 10340 if (starting > 0x001d) 10341 goto f1e; 10342 if (starting > 0x0010) 10343 goto f1d; 10344 if (starting > 0x0003) 10345 goto f10; 10346 if (starting > 0x0002) 10347 goto f3; 10348 if (starting > 0x0001) 10349 goto f2; 10350 if (starting > 0x0000) 10351 goto f1; 10352 10353 /* Profile List */ 10354 scsi_ulto2b(0x0000, feature->feature_code); 10355 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT; 10356 feature->add_length = 8; 10357 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */ 10358 feature->feature_data[2] = 0x00; 10359 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */ 10360 feature->feature_data[6] = 0x01; 10361 feature = (struct scsi_get_config_feature *) 10362 &feature->feature_data[feature->add_length]; 10363 10364f1: /* Core */ 10365 scsi_ulto2b(0x0001, feature->feature_code); 10366 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10367 feature->add_length = 8; 10368 scsi_ulto4b(0x00000000, &feature->feature_data[0]); 10369 feature->feature_data[4] = 0x03; 10370 feature = (struct scsi_get_config_feature *) 10371 &feature->feature_data[feature->add_length]; 10372 10373f2: /* Morphing */ 10374 scsi_ulto2b(0x0002, feature->feature_code); 10375 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10376 feature->add_length = 4; 10377 feature->feature_data[0] = 0x02; 10378 feature = (struct scsi_get_config_feature *) 10379 &feature->feature_data[feature->add_length]; 10380 10381f3: /* Removable Medium */ 10382 scsi_ulto2b(0x0003, feature->feature_code); 10383 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT; 10384 feature->add_length = 4; 10385 feature->feature_data[0] = 0x39; 10386 feature = (struct scsi_get_config_feature *) 10387 &feature->feature_data[feature->add_length]; 10388 10389 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA)) 10390 goto done; 10391 10392f10: /* Random Read */ 10393 scsi_ulto2b(0x0010, feature->feature_code); 10394 feature->flags = 0x00; 10395 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10396 feature->flags |= SGC_F_CURRENT; 10397 feature->add_length = 8; 10398 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]); 10399 scsi_ulto2b(1, &feature->feature_data[4]); 10400 feature->feature_data[6] = 0x00; 10401 feature = (struct scsi_get_config_feature *) 10402 &feature->feature_data[feature->add_length]; 10403 10404f1d: /* Multi-Read */ 10405 scsi_ulto2b(0x001D, feature->feature_code); 10406 feature->flags = 0x00; 10407 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10408 feature->flags |= SGC_F_CURRENT; 10409 feature->add_length = 0; 10410 feature = (struct scsi_get_config_feature *) 10411 &feature->feature_data[feature->add_length]; 10412 10413f1e: /* CD Read */ 10414 scsi_ulto2b(0x001E, feature->feature_code); 10415 feature->flags = 0x00; 10416 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10417 feature->flags |= SGC_F_CURRENT; 10418 feature->add_length = 4; 10419 feature->feature_data[0] = 0x00; 10420 feature = (struct scsi_get_config_feature *) 10421 &feature->feature_data[feature->add_length]; 10422 10423f1f: /* DVD Read */ 10424 scsi_ulto2b(0x001F, feature->feature_code); 10425 feature->flags = 0x08; 10426 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10427 feature->flags |= SGC_F_CURRENT; 10428 feature->add_length = 4; 10429 feature->feature_data[0] = 0x01; 10430 feature->feature_data[2] = 0x03; 10431 feature = (struct scsi_get_config_feature *) 10432 &feature->feature_data[feature->add_length]; 10433 10434f2a: /* DVD+RW */ 10435 scsi_ulto2b(0x002A, feature->feature_code); 10436 feature->flags = 0x04; 10437 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10438 feature->flags |= SGC_F_CURRENT; 10439 feature->add_length = 4; 10440 feature->feature_data[0] = 0x00; 10441 feature->feature_data[1] = 0x00; 10442 feature = (struct scsi_get_config_feature *) 10443 &feature->feature_data[feature->add_length]; 10444 10445f2b: /* DVD+R */ 10446 scsi_ulto2b(0x002B, feature->feature_code); 10447 feature->flags = 0x00; 10448 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10449 feature->flags |= SGC_F_CURRENT; 10450 feature->add_length = 4; 10451 feature->feature_data[0] = 0x00; 10452 feature = (struct scsi_get_config_feature *) 10453 &feature->feature_data[feature->add_length]; 10454 10455f3a: /* DVD+RW Dual Layer */ 10456 scsi_ulto2b(0x003A, feature->feature_code); 10457 feature->flags = 0x00; 10458 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10459 feature->flags |= SGC_F_CURRENT; 10460 feature->add_length = 4; 10461 feature->feature_data[0] = 0x00; 10462 feature->feature_data[1] = 0x00; 10463 feature = (struct scsi_get_config_feature *) 10464 &feature->feature_data[feature->add_length]; 10465 10466f3b: /* DVD+R Dual Layer */ 10467 scsi_ulto2b(0x003B, feature->feature_code); 10468 feature->flags = 0x00; 10469 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0) 10470 feature->flags |= SGC_F_CURRENT; 10471 feature->add_length = 4; 10472 feature->feature_data[0] = 0x00; 10473 feature = (struct scsi_get_config_feature *) 10474 &feature->feature_data[feature->add_length]; 10475 10476done: 10477 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10478 if (rt == SGC_RT_SPECIFIC && data_len > 4) { 10479 feature = (struct scsi_get_config_feature *)(hdr + 1); 10480 if (scsi_2btoul(feature->feature_code) == starting) 10481 feature = (struct scsi_get_config_feature *) 10482 &feature->feature_data[feature->add_length]; 10483 data_len = (uint8_t *)feature - (uint8_t *)hdr; 10484 } 10485 scsi_ulto4b(data_len - 4, hdr->data_length); 10486 if (data_len < alloc_len) { 10487 ctsio->residual = alloc_len - data_len; 10488 ctsio->kern_data_len = data_len; 10489 ctsio->kern_total_len = data_len; 10490 } else { 10491 ctsio->residual = 0; 10492 ctsio->kern_data_len = alloc_len; 10493 ctsio->kern_total_len = alloc_len; 10494 } 10495 10496 ctl_set_success(ctsio); 10497 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10498 ctsio->be_move_done = ctl_config_move_done; 10499 ctl_datamove((union ctl_io *)ctsio); 10500 return (CTL_RETVAL_COMPLETE); 10501} 10502 10503int 10504ctl_get_event_status(struct ctl_scsiio *ctsio) 10505{ 10506 struct scsi_get_event_status_header *hdr; 10507 struct scsi_get_event_status *cdb; 10508 struct ctl_lun *lun; 10509 uint32_t alloc_len, data_len; 10510 int notif_class; 10511 10512 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10513 cdb = (struct scsi_get_event_status *)ctsio->cdb; 10514 if ((cdb->byte2 & SGESN_POLLED) == 0) { 10515 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1, 10516 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0); 10517 ctl_done((union ctl_io *)ctsio); 10518 return (CTL_RETVAL_COMPLETE); 10519 } 10520 notif_class = cdb->notif_class; 10521 alloc_len = scsi_2btoul(cdb->length); 10522 10523 data_len = sizeof(struct scsi_get_event_status_header); 10524 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10525 ctsio->kern_sg_entries = 0; 10526 ctsio->kern_data_resid = 0; 10527 ctsio->kern_rel_offset = 0; 10528 10529 if (data_len < alloc_len) { 10530 ctsio->residual = alloc_len - data_len; 10531 ctsio->kern_data_len = data_len; 10532 ctsio->kern_total_len = data_len; 10533 } else { 10534 ctsio->residual = 0; 10535 ctsio->kern_data_len = alloc_len; 10536 ctsio->kern_total_len = alloc_len; 10537 } 10538 10539 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr; 10540 scsi_ulto2b(0, hdr->descr_length); 10541 hdr->nea_class = SGESN_NEA; 10542 hdr->supported_class = 0; 10543 10544 ctl_set_success(ctsio); 10545 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10546 ctsio->be_move_done = ctl_config_move_done; 10547 ctl_datamove((union ctl_io *)ctsio); 10548 return (CTL_RETVAL_COMPLETE); 10549} 10550 10551int 10552ctl_mechanism_status(struct ctl_scsiio *ctsio) 10553{ 10554 struct scsi_mechanism_status_header *hdr; 10555 struct scsi_mechanism_status *cdb; 10556 struct ctl_lun *lun; 10557 uint32_t alloc_len, data_len; 10558 10559 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10560 cdb = (struct scsi_mechanism_status *)ctsio->cdb; 10561 alloc_len = scsi_2btoul(cdb->length); 10562 10563 data_len = sizeof(struct scsi_mechanism_status_header); 10564 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10565 ctsio->kern_sg_entries = 0; 10566 ctsio->kern_data_resid = 0; 10567 ctsio->kern_rel_offset = 0; 10568 10569 if (data_len < alloc_len) { 10570 ctsio->residual = alloc_len - data_len; 10571 ctsio->kern_data_len = data_len; 10572 ctsio->kern_total_len = data_len; 10573 } else { 10574 ctsio->residual = 0; 10575 ctsio->kern_data_len = alloc_len; 10576 ctsio->kern_total_len = alloc_len; 10577 } 10578 10579 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr; 10580 hdr->state1 = 0x00; 10581 hdr->state2 = 0xe0; 10582 scsi_ulto3b(0, hdr->lba); 10583 hdr->slots_num = 0; 10584 scsi_ulto2b(0, hdr->slots_length); 10585 10586 ctl_set_success(ctsio); 10587 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10588 ctsio->be_move_done = ctl_config_move_done; 10589 ctl_datamove((union ctl_io *)ctsio); 10590 return (CTL_RETVAL_COMPLETE); 10591} 10592 10593static void 10594ctl_ultomsf(uint32_t lba, uint8_t *buf) 10595{ 10596 10597 lba += 150; 10598 buf[0] = 0; 10599 buf[1] = bin2bcd((lba / 75) / 60); 10600 buf[2] = bin2bcd((lba / 75) % 60); 10601 buf[3] = bin2bcd(lba % 75); 10602} 10603 10604int 10605ctl_read_toc(struct ctl_scsiio *ctsio) 10606{ 10607 struct scsi_read_toc_hdr *hdr; 10608 struct scsi_read_toc_type01_descr *descr; 10609 struct scsi_read_toc *cdb; 10610 struct ctl_lun *lun; 10611 uint32_t alloc_len, data_len; 10612 int format, msf; 10613 10614 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 10615 cdb = (struct scsi_read_toc *)ctsio->cdb; 10616 msf = (cdb->byte2 & CD_MSF) != 0; 10617 format = cdb->format; 10618 alloc_len = scsi_2btoul(cdb->data_len); 10619 10620 data_len = sizeof(struct scsi_read_toc_hdr); 10621 if (format == 0) 10622 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr); 10623 else 10624 data_len += sizeof(struct scsi_read_toc_type01_descr); 10625 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO); 10626 ctsio->kern_sg_entries = 0; 10627 ctsio->kern_data_resid = 0; 10628 ctsio->kern_rel_offset = 0; 10629 10630 if (data_len < alloc_len) { 10631 ctsio->residual = alloc_len - data_len; 10632 ctsio->kern_data_len = data_len; 10633 ctsio->kern_total_len = data_len; 10634 } else { 10635 ctsio->residual = 0; 10636 ctsio->kern_data_len = alloc_len; 10637 ctsio->kern_total_len = alloc_len; 10638 } 10639 10640 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr; 10641 if (format == 0) { 10642 scsi_ulto2b(0x12, hdr->data_length); 10643 hdr->first = 1; 10644 hdr->last = 1; 10645 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10646 descr->addr_ctl = 0x14; 10647 descr->track_number = 1; 10648 if (msf) 10649 ctl_ultomsf(0, descr->track_start); 10650 else 10651 scsi_ulto4b(0, descr->track_start); 10652 descr++; 10653 descr->addr_ctl = 0x14; 10654 descr->track_number = 0xaa; 10655 if (msf) 10656 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start); 10657 else 10658 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start); 10659 } else { 10660 scsi_ulto2b(0x0a, hdr->data_length); 10661 hdr->first = 1; 10662 hdr->last = 1; 10663 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1); 10664 descr->addr_ctl = 0x14; 10665 descr->track_number = 1; 10666 if (msf) 10667 ctl_ultomsf(0, descr->track_start); 10668 else 10669 scsi_ulto4b(0, descr->track_start); 10670 } 10671 10672 ctl_set_success(ctsio); 10673 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED; 10674 ctsio->be_move_done = ctl_config_move_done; 10675 ctl_datamove((union ctl_io *)ctsio); 10676 return (CTL_RETVAL_COMPLETE); 10677} 10678 10679/* 10680 * For known CDB types, parse the LBA and length. 10681 */ 10682static int 10683ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len) 10684{ 10685 if (io->io_hdr.io_type != CTL_IO_SCSI) 10686 return (1); 10687 10688 switch (io->scsiio.cdb[0]) { 10689 case COMPARE_AND_WRITE: { 10690 struct scsi_compare_and_write *cdb; 10691 10692 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb; 10693 10694 *lba = scsi_8btou64(cdb->addr); 10695 *len = cdb->length; 10696 break; 10697 } 10698 case READ_6: 10699 case WRITE_6: { 10700 struct scsi_rw_6 *cdb; 10701 10702 cdb = (struct scsi_rw_6 *)io->scsiio.cdb; 10703 10704 *lba = scsi_3btoul(cdb->addr); 10705 /* only 5 bits are valid in the most significant address byte */ 10706 *lba &= 0x1fffff; 10707 *len = cdb->length; 10708 break; 10709 } 10710 case READ_10: 10711 case WRITE_10: { 10712 struct scsi_rw_10 *cdb; 10713 10714 cdb = (struct scsi_rw_10 *)io->scsiio.cdb; 10715 10716 *lba = scsi_4btoul(cdb->addr); 10717 *len = scsi_2btoul(cdb->length); 10718 break; 10719 } 10720 case WRITE_VERIFY_10: { 10721 struct scsi_write_verify_10 *cdb; 10722 10723 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb; 10724 10725 *lba = scsi_4btoul(cdb->addr); 10726 *len = scsi_2btoul(cdb->length); 10727 break; 10728 } 10729 case READ_12: 10730 case WRITE_12: { 10731 struct scsi_rw_12 *cdb; 10732 10733 cdb = (struct scsi_rw_12 *)io->scsiio.cdb; 10734 10735 *lba = scsi_4btoul(cdb->addr); 10736 *len = scsi_4btoul(cdb->length); 10737 break; 10738 } 10739 case WRITE_VERIFY_12: { 10740 struct scsi_write_verify_12 *cdb; 10741 10742 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb; 10743 10744 *lba = scsi_4btoul(cdb->addr); 10745 *len = scsi_4btoul(cdb->length); 10746 break; 10747 } 10748 case READ_16: 10749 case WRITE_16: { 10750 struct scsi_rw_16 *cdb; 10751 10752 cdb = (struct scsi_rw_16 *)io->scsiio.cdb; 10753 10754 *lba = scsi_8btou64(cdb->addr); 10755 *len = scsi_4btoul(cdb->length); 10756 break; 10757 } 10758 case WRITE_ATOMIC_16: { 10759 struct scsi_write_atomic_16 *cdb; 10760 10761 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb; 10762 10763 *lba = scsi_8btou64(cdb->addr); 10764 *len = scsi_2btoul(cdb->length); 10765 break; 10766 } 10767 case WRITE_VERIFY_16: { 10768 struct scsi_write_verify_16 *cdb; 10769 10770 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb; 10771 10772 *lba = scsi_8btou64(cdb->addr); 10773 *len = scsi_4btoul(cdb->length); 10774 break; 10775 } 10776 case WRITE_SAME_10: { 10777 struct scsi_write_same_10 *cdb; 10778 10779 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb; 10780 10781 *lba = scsi_4btoul(cdb->addr); 10782 *len = scsi_2btoul(cdb->length); 10783 break; 10784 } 10785 case WRITE_SAME_16: { 10786 struct scsi_write_same_16 *cdb; 10787 10788 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb; 10789 10790 *lba = scsi_8btou64(cdb->addr); 10791 *len = scsi_4btoul(cdb->length); 10792 break; 10793 } 10794 case VERIFY_10: { 10795 struct scsi_verify_10 *cdb; 10796 10797 cdb = (struct scsi_verify_10 *)io->scsiio.cdb; 10798 10799 *lba = scsi_4btoul(cdb->addr); 10800 *len = scsi_2btoul(cdb->length); 10801 break; 10802 } 10803 case VERIFY_12: { 10804 struct scsi_verify_12 *cdb; 10805 10806 cdb = (struct scsi_verify_12 *)io->scsiio.cdb; 10807 10808 *lba = scsi_4btoul(cdb->addr); 10809 *len = scsi_4btoul(cdb->length); 10810 break; 10811 } 10812 case VERIFY_16: { 10813 struct scsi_verify_16 *cdb; 10814 10815 cdb = (struct scsi_verify_16 *)io->scsiio.cdb; 10816 10817 *lba = scsi_8btou64(cdb->addr); 10818 *len = scsi_4btoul(cdb->length); 10819 break; 10820 } 10821 case UNMAP: { 10822 *lba = 0; 10823 *len = UINT64_MAX; 10824 break; 10825 } 10826 case SERVICE_ACTION_IN: { /* GET LBA STATUS */ 10827 struct scsi_get_lba_status *cdb; 10828 10829 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb; 10830 *lba = scsi_8btou64(cdb->addr); 10831 *len = UINT32_MAX; 10832 break; 10833 } 10834 default: 10835 return (1); 10836 break; /* NOTREACHED */ 10837 } 10838 10839 return (0); 10840} 10841 10842static ctl_action 10843ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2, 10844 bool seq) 10845{ 10846 uint64_t endlba1, endlba2; 10847 10848 endlba1 = lba1 + len1 - (seq ? 0 : 1); 10849 endlba2 = lba2 + len2 - 1; 10850 10851 if ((endlba1 < lba2) || (endlba2 < lba1)) 10852 return (CTL_ACTION_PASS); 10853 else 10854 return (CTL_ACTION_BLOCK); 10855} 10856 10857static int 10858ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2) 10859{ 10860 struct ctl_ptr_len_flags *ptrlen; 10861 struct scsi_unmap_desc *buf, *end, *range; 10862 uint64_t lba; 10863 uint32_t len; 10864 10865 /* If not UNMAP -- go other way. */ 10866 if (io->io_hdr.io_type != CTL_IO_SCSI || 10867 io->scsiio.cdb[0] != UNMAP) 10868 return (CTL_ACTION_ERROR); 10869 10870 /* If UNMAP without data -- block and wait for data. */ 10871 ptrlen = (struct ctl_ptr_len_flags *) 10872 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN]; 10873 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 || 10874 ptrlen->ptr == NULL) 10875 return (CTL_ACTION_BLOCK); 10876 10877 /* UNMAP with data -- check for collision. */ 10878 buf = (struct scsi_unmap_desc *)ptrlen->ptr; 10879 end = buf + ptrlen->len / sizeof(*buf); 10880 for (range = buf; range < end; range++) { 10881 lba = scsi_8btou64(range->lba); 10882 len = scsi_4btoul(range->length); 10883 if ((lba < lba2 + len2) && (lba + len > lba2)) 10884 return (CTL_ACTION_BLOCK); 10885 } 10886 return (CTL_ACTION_PASS); 10887} 10888 10889static ctl_action 10890ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq) 10891{ 10892 uint64_t lba1, lba2; 10893 uint64_t len1, len2; 10894 int retval; 10895 10896 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10897 return (CTL_ACTION_ERROR); 10898 10899 retval = ctl_extent_check_unmap(io1, lba2, len2); 10900 if (retval != CTL_ACTION_ERROR) 10901 return (retval); 10902 10903 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10904 return (CTL_ACTION_ERROR); 10905 10906 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10907 seq = FALSE; 10908 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq)); 10909} 10910 10911static ctl_action 10912ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2) 10913{ 10914 uint64_t lba1, lba2; 10915 uint64_t len1, len2; 10916 10917 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE) 10918 return (CTL_ACTION_PASS); 10919 if (ctl_get_lba_len(io1, &lba1, &len1) != 0) 10920 return (CTL_ACTION_ERROR); 10921 if (ctl_get_lba_len(io2, &lba2, &len2) != 0) 10922 return (CTL_ACTION_ERROR); 10923 10924 if (lba1 + len1 == lba2) 10925 return (CTL_ACTION_BLOCK); 10926 return (CTL_ACTION_PASS); 10927} 10928 10929static ctl_action 10930ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io, 10931 union ctl_io *ooa_io) 10932{ 10933 const struct ctl_cmd_entry *pending_entry, *ooa_entry; 10934 const ctl_serialize_action *serialize_row; 10935 10936 /* 10937 * The initiator attempted multiple untagged commands at the same 10938 * time. Can't do that. 10939 */ 10940 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10941 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 10942 && ((pending_io->io_hdr.nexus.targ_port == 10943 ooa_io->io_hdr.nexus.targ_port) 10944 && (pending_io->io_hdr.nexus.initid == 10945 ooa_io->io_hdr.nexus.initid)) 10946 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10947 CTL_FLAG_STATUS_SENT)) == 0)) 10948 return (CTL_ACTION_OVERLAP); 10949 10950 /* 10951 * The initiator attempted to send multiple tagged commands with 10952 * the same ID. (It's fine if different initiators have the same 10953 * tag ID.) 10954 * 10955 * Even if all of those conditions are true, we don't kill the I/O 10956 * if the command ahead of us has been aborted. We won't end up 10957 * sending it to the FETD, and it's perfectly legal to resend a 10958 * command with the same tag number as long as the previous 10959 * instance of this tag number has been aborted somehow. 10960 */ 10961 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10962 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED) 10963 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num) 10964 && ((pending_io->io_hdr.nexus.targ_port == 10965 ooa_io->io_hdr.nexus.targ_port) 10966 && (pending_io->io_hdr.nexus.initid == 10967 ooa_io->io_hdr.nexus.initid)) 10968 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT | 10969 CTL_FLAG_STATUS_SENT)) == 0)) 10970 return (CTL_ACTION_OVERLAP_TAG); 10971 10972 /* 10973 * If we get a head of queue tag, SAM-3 says that we should 10974 * immediately execute it. 10975 * 10976 * What happens if this command would normally block for some other 10977 * reason? e.g. a request sense with a head of queue tag 10978 * immediately after a write. Normally that would block, but this 10979 * will result in its getting executed immediately... 10980 * 10981 * We currently return "pass" instead of "skip", so we'll end up 10982 * going through the rest of the queue to check for overlapped tags. 10983 * 10984 * XXX KDM check for other types of blockage first?? 10985 */ 10986 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 10987 return (CTL_ACTION_PASS); 10988 10989 /* 10990 * Ordered tags have to block until all items ahead of them 10991 * have completed. If we get called with an ordered tag, we always 10992 * block, if something else is ahead of us in the queue. 10993 */ 10994 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED) 10995 return (CTL_ACTION_BLOCK); 10996 10997 /* 10998 * Simple tags get blocked until all head of queue and ordered tags 10999 * ahead of them have completed. I'm lumping untagged commands in 11000 * with simple tags here. XXX KDM is that the right thing to do? 11001 */ 11002 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED) 11003 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE)) 11004 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE) 11005 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED))) 11006 return (CTL_ACTION_BLOCK); 11007 11008 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL); 11009 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL); 11010 11011 serialize_row = ctl_serialize_table[ooa_entry->seridx]; 11012 11013 switch (serialize_row[pending_entry->seridx]) { 11014 case CTL_SER_BLOCK: 11015 return (CTL_ACTION_BLOCK); 11016 case CTL_SER_EXTENT: 11017 return (ctl_extent_check(ooa_io, pending_io, 11018 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11019 case CTL_SER_EXTENTOPT: 11020 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11021 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11022 return (ctl_extent_check(ooa_io, pending_io, 11023 (lun->be_lun && 11024 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON))); 11025 return (CTL_ACTION_PASS); 11026 case CTL_SER_EXTENTSEQ: 11027 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF) 11028 return (ctl_extent_check_seq(ooa_io, pending_io)); 11029 return (CTL_ACTION_PASS); 11030 case CTL_SER_PASS: 11031 return (CTL_ACTION_PASS); 11032 case CTL_SER_BLOCKOPT: 11033 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags 11034 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED) 11035 return (CTL_ACTION_BLOCK); 11036 return (CTL_ACTION_PASS); 11037 case CTL_SER_SKIP: 11038 return (CTL_ACTION_SKIP); 11039 default: 11040 panic("invalid serialization value %d", 11041 serialize_row[pending_entry->seridx]); 11042 } 11043 11044 return (CTL_ACTION_ERROR); 11045} 11046 11047/* 11048 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue. 11049 * Assumptions: 11050 * - pending_io is generally either incoming, or on the blocked queue 11051 * - starting I/O is the I/O we want to start the check with. 11052 */ 11053static ctl_action 11054ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io, 11055 union ctl_io *starting_io) 11056{ 11057 union ctl_io *ooa_io; 11058 ctl_action action; 11059 11060 mtx_assert(&lun->lun_lock, MA_OWNED); 11061 11062 /* 11063 * Run back along the OOA queue, starting with the current 11064 * blocked I/O and going through every I/O before it on the 11065 * queue. If starting_io is NULL, we'll just end up returning 11066 * CTL_ACTION_PASS. 11067 */ 11068 for (ooa_io = starting_io; ooa_io != NULL; 11069 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq, 11070 ooa_links)){ 11071 11072 /* 11073 * This routine just checks to see whether 11074 * cur_blocked is blocked by ooa_io, which is ahead 11075 * of it in the queue. It doesn't queue/dequeue 11076 * cur_blocked. 11077 */ 11078 action = ctl_check_for_blockage(lun, pending_io, ooa_io); 11079 switch (action) { 11080 case CTL_ACTION_BLOCK: 11081 case CTL_ACTION_OVERLAP: 11082 case CTL_ACTION_OVERLAP_TAG: 11083 case CTL_ACTION_SKIP: 11084 case CTL_ACTION_ERROR: 11085 return (action); 11086 break; /* NOTREACHED */ 11087 case CTL_ACTION_PASS: 11088 break; 11089 default: 11090 panic("invalid action %d", action); 11091 break; /* NOTREACHED */ 11092 } 11093 } 11094 11095 return (CTL_ACTION_PASS); 11096} 11097 11098/* 11099 * Assumptions: 11100 * - An I/O has just completed, and has been removed from the per-LUN OOA 11101 * queue, so some items on the blocked queue may now be unblocked. 11102 */ 11103static int 11104ctl_check_blocked(struct ctl_lun *lun) 11105{ 11106 struct ctl_softc *softc = lun->ctl_softc; 11107 union ctl_io *cur_blocked, *next_blocked; 11108 11109 mtx_assert(&lun->lun_lock, MA_OWNED); 11110 11111 /* 11112 * Run forward from the head of the blocked queue, checking each 11113 * entry against the I/Os prior to it on the OOA queue to see if 11114 * there is still any blockage. 11115 * 11116 * We cannot use the TAILQ_FOREACH() macro, because it can't deal 11117 * with our removing a variable on it while it is traversing the 11118 * list. 11119 */ 11120 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); 11121 cur_blocked != NULL; cur_blocked = next_blocked) { 11122 union ctl_io *prev_ooa; 11123 ctl_action action; 11124 11125 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr, 11126 blocked_links); 11127 11128 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr, 11129 ctl_ooaq, ooa_links); 11130 11131 /* 11132 * If cur_blocked happens to be the first item in the OOA 11133 * queue now, prev_ooa will be NULL, and the action 11134 * returned will just be CTL_ACTION_PASS. 11135 */ 11136 action = ctl_check_ooa(lun, cur_blocked, prev_ooa); 11137 11138 switch (action) { 11139 case CTL_ACTION_BLOCK: 11140 /* Nothing to do here, still blocked */ 11141 break; 11142 case CTL_ACTION_OVERLAP: 11143 case CTL_ACTION_OVERLAP_TAG: 11144 /* 11145 * This shouldn't happen! In theory we've already 11146 * checked this command for overlap... 11147 */ 11148 break; 11149 case CTL_ACTION_PASS: 11150 case CTL_ACTION_SKIP: { 11151 const struct ctl_cmd_entry *entry; 11152 11153 /* 11154 * The skip case shouldn't happen, this transaction 11155 * should have never made it onto the blocked queue. 11156 */ 11157 /* 11158 * This I/O is no longer blocked, we can remove it 11159 * from the blocked queue. Since this is a TAILQ 11160 * (doubly linked list), we can do O(1) removals 11161 * from any place on the list. 11162 */ 11163 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr, 11164 blocked_links); 11165 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED; 11166 11167 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 11168 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){ 11169 /* 11170 * Need to send IO back to original side to 11171 * run 11172 */ 11173 union ctl_ha_msg msg_info; 11174 11175 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11176 msg_info.hdr.original_sc = 11177 cur_blocked->io_hdr.original_sc; 11178 msg_info.hdr.serializing_sc = cur_blocked; 11179 msg_info.hdr.msg_type = CTL_MSG_R2R; 11180 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11181 sizeof(msg_info.hdr), M_NOWAIT); 11182 break; 11183 } 11184 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL); 11185 11186 /* 11187 * Check this I/O for LUN state changes that may 11188 * have happened while this command was blocked. 11189 * The LUN state may have been changed by a command 11190 * ahead of us in the queue, so we need to re-check 11191 * for any states that can be caused by SCSI 11192 * commands. 11193 */ 11194 if (ctl_scsiio_lun_check(lun, entry, 11195 &cur_blocked->scsiio) == 0) { 11196 cur_blocked->io_hdr.flags |= 11197 CTL_FLAG_IS_WAS_ON_RTR; 11198 ctl_enqueue_rtr(cur_blocked); 11199 } else 11200 ctl_done(cur_blocked); 11201 break; 11202 } 11203 default: 11204 /* 11205 * This probably shouldn't happen -- we shouldn't 11206 * get CTL_ACTION_ERROR, or anything else. 11207 */ 11208 break; 11209 } 11210 } 11211 11212 return (CTL_RETVAL_COMPLETE); 11213} 11214 11215/* 11216 * This routine (with one exception) checks LUN flags that can be set by 11217 * commands ahead of us in the OOA queue. These flags have to be checked 11218 * when a command initially comes in, and when we pull a command off the 11219 * blocked queue and are preparing to execute it. The reason we have to 11220 * check these flags for commands on the blocked queue is that the LUN 11221 * state may have been changed by a command ahead of us while we're on the 11222 * blocked queue. 11223 * 11224 * Ordering is somewhat important with these checks, so please pay 11225 * careful attention to the placement of any new checks. 11226 */ 11227static int 11228ctl_scsiio_lun_check(struct ctl_lun *lun, 11229 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio) 11230{ 11231 struct ctl_softc *softc = lun->ctl_softc; 11232 int retval; 11233 uint32_t residx; 11234 11235 retval = 0; 11236 11237 mtx_assert(&lun->lun_lock, MA_OWNED); 11238 11239 /* 11240 * If this shelf is a secondary shelf controller, we may have to 11241 * reject some commands disallowed by HA mode and link state. 11242 */ 11243 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) { 11244 if (softc->ha_link == CTL_HA_LINK_OFFLINE && 11245 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11246 ctl_set_lun_unavail(ctsio); 11247 retval = 1; 11248 goto bailout; 11249 } 11250 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 && 11251 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) { 11252 ctl_set_lun_transit(ctsio); 11253 retval = 1; 11254 goto bailout; 11255 } 11256 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY && 11257 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) { 11258 ctl_set_lun_standby(ctsio); 11259 retval = 1; 11260 goto bailout; 11261 } 11262 11263 /* The rest of checks are only done on executing side */ 11264 if (softc->ha_mode == CTL_HA_MODE_XFER) 11265 goto bailout; 11266 } 11267 11268 if (entry->pattern & CTL_LUN_PAT_WRITE) { 11269 if (lun->be_lun && 11270 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) { 11271 ctl_set_hw_write_protected(ctsio); 11272 retval = 1; 11273 goto bailout; 11274 } 11275 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT] 11276 .eca_and_aen & SCP_SWP) != 0) { 11277 ctl_set_sense(ctsio, /*current_error*/ 1, 11278 /*sense_key*/ SSD_KEY_DATA_PROTECT, 11279 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE); 11280 retval = 1; 11281 goto bailout; 11282 } 11283 } 11284 11285 /* 11286 * Check for a reservation conflict. If this command isn't allowed 11287 * even on reserved LUNs, and if this initiator isn't the one who 11288 * reserved us, reject the command with a reservation conflict. 11289 */ 11290 residx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11291 if ((lun->flags & CTL_LUN_RESERVED) 11292 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) { 11293 if (lun->res_idx != residx) { 11294 ctl_set_reservation_conflict(ctsio); 11295 retval = 1; 11296 goto bailout; 11297 } 11298 } 11299 11300 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 || 11301 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) { 11302 /* No reservation or command is allowed. */; 11303 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) && 11304 (lun->pr_res_type == SPR_TYPE_WR_EX || 11305 lun->pr_res_type == SPR_TYPE_WR_EX_RO || 11306 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) { 11307 /* The command is allowed for Write Exclusive resv. */; 11308 } else { 11309 /* 11310 * if we aren't registered or it's a res holder type 11311 * reservation and this isn't the res holder then set a 11312 * conflict. 11313 */ 11314 if (ctl_get_prkey(lun, residx) == 0 || 11315 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) { 11316 ctl_set_reservation_conflict(ctsio); 11317 retval = 1; 11318 goto bailout; 11319 } 11320 } 11321 11322 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) { 11323 if (lun->flags & CTL_LUN_EJECTED) 11324 ctl_set_lun_ejected(ctsio); 11325 else if (lun->flags & CTL_LUN_NO_MEDIA) { 11326 if (lun->flags & CTL_LUN_REMOVABLE) 11327 ctl_set_lun_no_media(ctsio); 11328 else 11329 ctl_set_lun_int_reqd(ctsio); 11330 } else if (lun->flags & CTL_LUN_STOPPED) 11331 ctl_set_lun_stopped(ctsio); 11332 else 11333 goto bailout; 11334 retval = 1; 11335 goto bailout; 11336 } 11337 11338bailout: 11339 return (retval); 11340} 11341 11342static void 11343ctl_failover_io(union ctl_io *io, int have_lock) 11344{ 11345 ctl_set_busy(&io->scsiio); 11346 ctl_done(io); 11347} 11348 11349static void 11350ctl_failover_lun(union ctl_io *rio) 11351{ 11352 struct ctl_softc *softc = control_softc; 11353 struct ctl_lun *lun; 11354 struct ctl_io_hdr *io, *next_io; 11355 uint32_t targ_lun; 11356 11357 targ_lun = rio->io_hdr.nexus.targ_mapped_lun; 11358 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun)); 11359 11360 /* Find and lock the LUN. */ 11361 mtx_lock(&softc->ctl_lock); 11362 if ((targ_lun < CTL_MAX_LUNS) && 11363 ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11364 mtx_lock(&lun->lun_lock); 11365 mtx_unlock(&softc->ctl_lock); 11366 if (lun->flags & CTL_LUN_DISABLED) { 11367 mtx_unlock(&lun->lun_lock); 11368 return; 11369 } 11370 } else { 11371 mtx_unlock(&softc->ctl_lock); 11372 return; 11373 } 11374 11375 if (softc->ha_mode == CTL_HA_MODE_XFER) { 11376 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11377 /* We are master */ 11378 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11379 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11380 io->flags |= CTL_FLAG_ABORT; 11381 io->flags |= CTL_FLAG_FAILOVER; 11382 } else { /* This can be only due to DATAMOVE */ 11383 io->msg_type = CTL_MSG_DATAMOVE_DONE; 11384 io->flags &= ~CTL_FLAG_DMA_INPROG; 11385 io->flags |= CTL_FLAG_IO_ACTIVE; 11386 io->port_status = 31340; 11387 ctl_enqueue_isc((union ctl_io *)io); 11388 } 11389 } 11390 /* We are slave */ 11391 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11392 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11393 if (io->flags & CTL_FLAG_IO_ACTIVE) { 11394 io->flags |= CTL_FLAG_FAILOVER; 11395 } else { 11396 ctl_set_busy(&((union ctl_io *)io)-> 11397 scsiio); 11398 ctl_done((union ctl_io *)io); 11399 } 11400 } 11401 } 11402 } else { /* SERIALIZE modes */ 11403 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links, 11404 next_io) { 11405 /* We are master */ 11406 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11407 TAILQ_REMOVE(&lun->blocked_queue, io, 11408 blocked_links); 11409 io->flags &= ~CTL_FLAG_BLOCKED; 11410 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11411 ctl_free_io((union ctl_io *)io); 11412 } 11413 } 11414 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) { 11415 /* We are master */ 11416 if (io->flags & CTL_FLAG_FROM_OTHER_SC) { 11417 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links); 11418 ctl_free_io((union ctl_io *)io); 11419 } 11420 /* We are slave */ 11421 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) { 11422 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC; 11423 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) { 11424 ctl_set_busy(&((union ctl_io *)io)-> 11425 scsiio); 11426 ctl_done((union ctl_io *)io); 11427 } 11428 } 11429 } 11430 ctl_check_blocked(lun); 11431 } 11432 mtx_unlock(&lun->lun_lock); 11433} 11434 11435static int 11436ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio) 11437{ 11438 struct ctl_lun *lun; 11439 const struct ctl_cmd_entry *entry; 11440 uint32_t initidx, targ_lun; 11441 int retval; 11442 11443 retval = 0; 11444 11445 lun = NULL; 11446 11447 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun; 11448 if ((targ_lun < CTL_MAX_LUNS) 11449 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) { 11450 /* 11451 * If the LUN is invalid, pretend that it doesn't exist. 11452 * It will go away as soon as all pending I/O has been 11453 * completed. 11454 */ 11455 mtx_lock(&lun->lun_lock); 11456 if (lun->flags & CTL_LUN_DISABLED) { 11457 mtx_unlock(&lun->lun_lock); 11458 lun = NULL; 11459 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11460 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11461 } else { 11462 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun; 11463 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = 11464 lun->be_lun; 11465 11466 /* 11467 * Every I/O goes into the OOA queue for a 11468 * particular LUN, and stays there until completion. 11469 */ 11470#ifdef CTL_TIME_IO 11471 if (TAILQ_EMPTY(&lun->ooa_queue)) { 11472 lun->idle_time += getsbinuptime() - 11473 lun->last_busy; 11474 } 11475#endif 11476 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, 11477 ooa_links); 11478 } 11479 } else { 11480 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL; 11481 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL; 11482 } 11483 11484 /* Get command entry and return error if it is unsuppotyed. */ 11485 entry = ctl_validate_command(ctsio); 11486 if (entry == NULL) { 11487 if (lun) 11488 mtx_unlock(&lun->lun_lock); 11489 return (retval); 11490 } 11491 11492 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK; 11493 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK; 11494 11495 /* 11496 * Check to see whether we can send this command to LUNs that don't 11497 * exist. This should pretty much only be the case for inquiry 11498 * and request sense. Further checks, below, really require having 11499 * a LUN, so we can't really check the command anymore. Just put 11500 * it on the rtr queue. 11501 */ 11502 if (lun == NULL) { 11503 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) { 11504 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11505 ctl_enqueue_rtr((union ctl_io *)ctsio); 11506 return (retval); 11507 } 11508 11509 ctl_set_unsupported_lun(ctsio); 11510 ctl_done((union ctl_io *)ctsio); 11511 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n")); 11512 return (retval); 11513 } else { 11514 /* 11515 * Make sure we support this particular command on this LUN. 11516 * e.g., we don't support writes to the control LUN. 11517 */ 11518 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) { 11519 mtx_unlock(&lun->lun_lock); 11520 ctl_set_invalid_opcode(ctsio); 11521 ctl_done((union ctl_io *)ctsio); 11522 return (retval); 11523 } 11524 } 11525 11526 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus); 11527 11528#ifdef CTL_WITH_CA 11529 /* 11530 * If we've got a request sense, it'll clear the contingent 11531 * allegiance condition. Otherwise, if we have a CA condition for 11532 * this initiator, clear it, because it sent down a command other 11533 * than request sense. 11534 */ 11535 if ((ctsio->cdb[0] != REQUEST_SENSE) 11536 && (ctl_is_set(lun->have_ca, initidx))) 11537 ctl_clear_mask(lun->have_ca, initidx); 11538#endif 11539 11540 /* 11541 * If the command has this flag set, it handles its own unit 11542 * attention reporting, we shouldn't do anything. Otherwise we 11543 * check for any pending unit attentions, and send them back to the 11544 * initiator. We only do this when a command initially comes in, 11545 * not when we pull it off the blocked queue. 11546 * 11547 * According to SAM-3, section 5.3.2, the order that things get 11548 * presented back to the host is basically unit attentions caused 11549 * by some sort of reset event, busy status, reservation conflicts 11550 * or task set full, and finally any other status. 11551 * 11552 * One issue here is that some of the unit attentions we report 11553 * don't fall into the "reset" category (e.g. "reported luns data 11554 * has changed"). So reporting it here, before the reservation 11555 * check, may be technically wrong. I guess the only thing to do 11556 * would be to check for and report the reset events here, and then 11557 * check for the other unit attention types after we check for a 11558 * reservation conflict. 11559 * 11560 * XXX KDM need to fix this 11561 */ 11562 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) { 11563 ctl_ua_type ua_type; 11564 11565 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data, 11566 SSD_TYPE_NONE); 11567 if (ua_type != CTL_UA_NONE) { 11568 mtx_unlock(&lun->lun_lock); 11569 ctsio->scsi_status = SCSI_STATUS_CHECK_COND; 11570 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 11571 ctsio->sense_len = SSD_FULL_SIZE; 11572 ctl_done((union ctl_io *)ctsio); 11573 return (retval); 11574 } 11575 } 11576 11577 11578 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) { 11579 mtx_unlock(&lun->lun_lock); 11580 ctl_done((union ctl_io *)ctsio); 11581 return (retval); 11582 } 11583 11584 /* 11585 * XXX CHD this is where we want to send IO to other side if 11586 * this LUN is secondary on this SC. We will need to make a copy 11587 * of the IO and flag the IO on this side as SENT_2OTHER and the flag 11588 * the copy we send as FROM_OTHER. 11589 * We also need to stuff the address of the original IO so we can 11590 * find it easily. Something similar will need be done on the other 11591 * side so when we are done we can find the copy. 11592 */ 11593 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 11594 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 && 11595 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) { 11596 union ctl_ha_msg msg_info; 11597 int isc_retval; 11598 11599 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC; 11600 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 11601 mtx_unlock(&lun->lun_lock); 11602 11603 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE; 11604 msg_info.hdr.original_sc = (union ctl_io *)ctsio; 11605 msg_info.hdr.serializing_sc = NULL; 11606 msg_info.hdr.nexus = ctsio->io_hdr.nexus; 11607 msg_info.scsi.tag_num = ctsio->tag_num; 11608 msg_info.scsi.tag_type = ctsio->tag_type; 11609 msg_info.scsi.cdb_len = ctsio->cdb_len; 11610 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN); 11611 11612 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11613 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data), 11614 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) { 11615 ctl_set_busy(ctsio); 11616 ctl_done((union ctl_io *)ctsio); 11617 return (retval); 11618 } 11619 return (retval); 11620 } 11621 11622 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, 11623 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, 11624 ctl_ooaq, ooa_links))) { 11625 case CTL_ACTION_BLOCK: 11626 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED; 11627 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr, 11628 blocked_links); 11629 mtx_unlock(&lun->lun_lock); 11630 return (retval); 11631 case CTL_ACTION_PASS: 11632 case CTL_ACTION_SKIP: 11633 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 11634 mtx_unlock(&lun->lun_lock); 11635 ctl_enqueue_rtr((union ctl_io *)ctsio); 11636 break; 11637 case CTL_ACTION_OVERLAP: 11638 mtx_unlock(&lun->lun_lock); 11639 ctl_set_overlapped_cmd(ctsio); 11640 ctl_done((union ctl_io *)ctsio); 11641 break; 11642 case CTL_ACTION_OVERLAP_TAG: 11643 mtx_unlock(&lun->lun_lock); 11644 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff); 11645 ctl_done((union ctl_io *)ctsio); 11646 break; 11647 case CTL_ACTION_ERROR: 11648 default: 11649 mtx_unlock(&lun->lun_lock); 11650 ctl_set_internal_failure(ctsio, 11651 /*sks_valid*/ 0, 11652 /*retry_count*/ 0); 11653 ctl_done((union ctl_io *)ctsio); 11654 break; 11655 } 11656 return (retval); 11657} 11658 11659const struct ctl_cmd_entry * 11660ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa) 11661{ 11662 const struct ctl_cmd_entry *entry; 11663 int service_action; 11664 11665 entry = &ctl_cmd_table[ctsio->cdb[0]]; 11666 if (sa) 11667 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0); 11668 if (entry->flags & CTL_CMD_FLAG_SA5) { 11669 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK; 11670 entry = &((const struct ctl_cmd_entry *) 11671 entry->execute)[service_action]; 11672 } 11673 return (entry); 11674} 11675 11676const struct ctl_cmd_entry * 11677ctl_validate_command(struct ctl_scsiio *ctsio) 11678{ 11679 const struct ctl_cmd_entry *entry; 11680 int i, sa; 11681 uint8_t diff; 11682 11683 entry = ctl_get_cmd_entry(ctsio, &sa); 11684 if (entry->execute == NULL) { 11685 if (sa) 11686 ctl_set_invalid_field(ctsio, 11687 /*sks_valid*/ 1, 11688 /*command*/ 1, 11689 /*field*/ 1, 11690 /*bit_valid*/ 1, 11691 /*bit*/ 4); 11692 else 11693 ctl_set_invalid_opcode(ctsio); 11694 ctl_done((union ctl_io *)ctsio); 11695 return (NULL); 11696 } 11697 KASSERT(entry->length > 0, 11698 ("Not defined length for command 0x%02x/0x%02x", 11699 ctsio->cdb[0], ctsio->cdb[1])); 11700 for (i = 1; i < entry->length; i++) { 11701 diff = ctsio->cdb[i] & ~entry->usage[i - 1]; 11702 if (diff == 0) 11703 continue; 11704 ctl_set_invalid_field(ctsio, 11705 /*sks_valid*/ 1, 11706 /*command*/ 1, 11707 /*field*/ i, 11708 /*bit_valid*/ 1, 11709 /*bit*/ fls(diff) - 1); 11710 ctl_done((union ctl_io *)ctsio); 11711 return (NULL); 11712 } 11713 return (entry); 11714} 11715 11716static int 11717ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry) 11718{ 11719 11720 switch (lun_type) { 11721 case T_DIRECT: 11722 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0) 11723 return (0); 11724 break; 11725 case T_PROCESSOR: 11726 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) 11727 return (0); 11728 break; 11729 case T_CDROM: 11730 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0) 11731 return (0); 11732 break; 11733 default: 11734 return (0); 11735 } 11736 return (1); 11737} 11738 11739static int 11740ctl_scsiio(struct ctl_scsiio *ctsio) 11741{ 11742 int retval; 11743 const struct ctl_cmd_entry *entry; 11744 11745 retval = CTL_RETVAL_COMPLETE; 11746 11747 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0])); 11748 11749 entry = ctl_get_cmd_entry(ctsio, NULL); 11750 11751 /* 11752 * If this I/O has been aborted, just send it straight to 11753 * ctl_done() without executing it. 11754 */ 11755 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) { 11756 ctl_done((union ctl_io *)ctsio); 11757 goto bailout; 11758 } 11759 11760 /* 11761 * All the checks should have been handled by ctl_scsiio_precheck(). 11762 * We should be clear now to just execute the I/O. 11763 */ 11764 retval = entry->execute(ctsio); 11765 11766bailout: 11767 return (retval); 11768} 11769 11770/* 11771 * Since we only implement one target right now, a bus reset simply resets 11772 * our single target. 11773 */ 11774static int 11775ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io) 11776{ 11777 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET)); 11778} 11779 11780static int 11781ctl_target_reset(struct ctl_softc *softc, union ctl_io *io, 11782 ctl_ua_type ua_type) 11783{ 11784 struct ctl_port *port; 11785 struct ctl_lun *lun; 11786 int retval; 11787 11788 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 11789 union ctl_ha_msg msg_info; 11790 11791 msg_info.hdr.nexus = io->io_hdr.nexus; 11792 if (ua_type==CTL_UA_TARG_RESET) 11793 msg_info.task.task_action = CTL_TASK_TARGET_RESET; 11794 else 11795 msg_info.task.task_action = CTL_TASK_BUS_RESET; 11796 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11797 msg_info.hdr.original_sc = NULL; 11798 msg_info.hdr.serializing_sc = NULL; 11799 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11800 sizeof(msg_info.task), M_WAITOK); 11801 } 11802 retval = 0; 11803 11804 mtx_lock(&softc->ctl_lock); 11805 port = ctl_io_port(&io->io_hdr); 11806 STAILQ_FOREACH(lun, &softc->lun_list, links) { 11807 if (port != NULL && 11808 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS) 11809 continue; 11810 retval += ctl_do_lun_reset(lun, io, ua_type); 11811 } 11812 mtx_unlock(&softc->ctl_lock); 11813 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11814 return (retval); 11815} 11816 11817/* 11818 * The LUN should always be set. The I/O is optional, and is used to 11819 * distinguish between I/Os sent by this initiator, and by other 11820 * initiators. We set unit attention for initiators other than this one. 11821 * SAM-3 is vague on this point. It does say that a unit attention should 11822 * be established for other initiators when a LUN is reset (see section 11823 * 5.7.3), but it doesn't specifically say that the unit attention should 11824 * be established for this particular initiator when a LUN is reset. Here 11825 * is the relevant text, from SAM-3 rev 8: 11826 * 11827 * 5.7.2 When a SCSI initiator port aborts its own tasks 11828 * 11829 * When a SCSI initiator port causes its own task(s) to be aborted, no 11830 * notification that the task(s) have been aborted shall be returned to 11831 * the SCSI initiator port other than the completion response for the 11832 * command or task management function action that caused the task(s) to 11833 * be aborted and notification(s) associated with related effects of the 11834 * action (e.g., a reset unit attention condition). 11835 * 11836 * XXX KDM for now, we're setting unit attention for all initiators. 11837 */ 11838static int 11839ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type) 11840{ 11841 union ctl_io *xio; 11842#if 0 11843 uint32_t initidx; 11844#endif 11845 int i; 11846 11847 mtx_lock(&lun->lun_lock); 11848 /* 11849 * Run through the OOA queue and abort each I/O. 11850 */ 11851 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11852 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11853 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS; 11854 } 11855 11856 /* 11857 * This version sets unit attention for every 11858 */ 11859#if 0 11860 initidx = ctl_get_initindex(&io->io_hdr.nexus); 11861 ctl_est_ua_all(lun, initidx, ua_type); 11862#else 11863 ctl_est_ua_all(lun, -1, ua_type); 11864#endif 11865 11866 /* 11867 * A reset (any kind, really) clears reservations established with 11868 * RESERVE/RELEASE. It does not clear reservations established 11869 * with PERSISTENT RESERVE OUT, but we don't support that at the 11870 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address 11871 * reservations made with the RESERVE/RELEASE commands, because 11872 * those commands are obsolete in SPC-3. 11873 */ 11874 lun->flags &= ~CTL_LUN_RESERVED; 11875 11876#ifdef CTL_WITH_CA 11877 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11878 ctl_clear_mask(lun->have_ca, i); 11879#endif 11880 lun->prevent_count = 0; 11881 for (i = 0; i < CTL_MAX_INITIATORS; i++) 11882 ctl_clear_mask(lun->prevent, i); 11883 mtx_unlock(&lun->lun_lock); 11884 11885 return (0); 11886} 11887 11888static int 11889ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io) 11890{ 11891 struct ctl_lun *lun; 11892 uint32_t targ_lun; 11893 int retval; 11894 11895 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11896 mtx_lock(&softc->ctl_lock); 11897 if ((targ_lun >= CTL_MAX_LUNS) || 11898 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11899 mtx_unlock(&softc->ctl_lock); 11900 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11901 return (1); 11902 } 11903 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET); 11904 mtx_unlock(&softc->ctl_lock); 11905 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11906 11907 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) { 11908 union ctl_ha_msg msg_info; 11909 11910 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11911 msg_info.hdr.nexus = io->io_hdr.nexus; 11912 msg_info.task.task_action = CTL_TASK_LUN_RESET; 11913 msg_info.hdr.original_sc = NULL; 11914 msg_info.hdr.serializing_sc = NULL; 11915 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11916 sizeof(msg_info.task), M_WAITOK); 11917 } 11918 return (retval); 11919} 11920 11921static void 11922ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id, 11923 int other_sc) 11924{ 11925 union ctl_io *xio; 11926 11927 mtx_assert(&lun->lun_lock, MA_OWNED); 11928 11929 /* 11930 * Run through the OOA queue and attempt to find the given I/O. 11931 * The target port, initiator ID, tag type and tag number have to 11932 * match the values that we got from the initiator. If we have an 11933 * untagged command to abort, simply abort the first untagged command 11934 * we come to. We only allow one untagged command at a time of course. 11935 */ 11936 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 11937 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 11938 11939 if ((targ_port == UINT32_MAX || 11940 targ_port == xio->io_hdr.nexus.targ_port) && 11941 (init_id == UINT32_MAX || 11942 init_id == xio->io_hdr.nexus.initid)) { 11943 if (targ_port != xio->io_hdr.nexus.targ_port || 11944 init_id != xio->io_hdr.nexus.initid) 11945 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS; 11946 xio->io_hdr.flags |= CTL_FLAG_ABORT; 11947 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) { 11948 union ctl_ha_msg msg_info; 11949 11950 msg_info.hdr.nexus = xio->io_hdr.nexus; 11951 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 11952 msg_info.task.tag_num = xio->scsiio.tag_num; 11953 msg_info.task.tag_type = xio->scsiio.tag_type; 11954 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 11955 msg_info.hdr.original_sc = NULL; 11956 msg_info.hdr.serializing_sc = NULL; 11957 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 11958 sizeof(msg_info.task), M_NOWAIT); 11959 } 11960 } 11961 } 11962} 11963 11964static int 11965ctl_abort_task_set(union ctl_io *io) 11966{ 11967 struct ctl_softc *softc = control_softc; 11968 struct ctl_lun *lun; 11969 uint32_t targ_lun; 11970 11971 /* 11972 * Look up the LUN. 11973 */ 11974 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 11975 mtx_lock(&softc->ctl_lock); 11976 if ((targ_lun >= CTL_MAX_LUNS) || 11977 (lun = softc->ctl_luns[targ_lun]) == NULL) { 11978 mtx_unlock(&softc->ctl_lock); 11979 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 11980 return (1); 11981 } 11982 11983 mtx_lock(&lun->lun_lock); 11984 mtx_unlock(&softc->ctl_lock); 11985 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) { 11986 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 11987 io->io_hdr.nexus.initid, 11988 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11989 } else { /* CTL_TASK_CLEAR_TASK_SET */ 11990 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX, 11991 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0); 11992 } 11993 mtx_unlock(&lun->lun_lock); 11994 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 11995 return (0); 11996} 11997 11998static int 11999ctl_i_t_nexus_reset(union ctl_io *io) 12000{ 12001 struct ctl_softc *softc = control_softc; 12002 struct ctl_lun *lun; 12003 uint32_t initidx; 12004 12005 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) { 12006 union ctl_ha_msg msg_info; 12007 12008 msg_info.hdr.nexus = io->io_hdr.nexus; 12009 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET; 12010 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12011 msg_info.hdr.original_sc = NULL; 12012 msg_info.hdr.serializing_sc = NULL; 12013 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12014 sizeof(msg_info.task), M_WAITOK); 12015 } 12016 12017 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12018 mtx_lock(&softc->ctl_lock); 12019 STAILQ_FOREACH(lun, &softc->lun_list, links) { 12020 mtx_lock(&lun->lun_lock); 12021 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port, 12022 io->io_hdr.nexus.initid, 1); 12023#ifdef CTL_WITH_CA 12024 ctl_clear_mask(lun->have_ca, initidx); 12025#endif 12026 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx)) 12027 lun->flags &= ~CTL_LUN_RESERVED; 12028 if (ctl_is_set(lun->prevent, initidx)) { 12029 ctl_clear_mask(lun->prevent, initidx); 12030 lun->prevent_count--; 12031 } 12032 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS); 12033 mtx_unlock(&lun->lun_lock); 12034 } 12035 mtx_unlock(&softc->ctl_lock); 12036 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12037 return (0); 12038} 12039 12040static int 12041ctl_abort_task(union ctl_io *io) 12042{ 12043 union ctl_io *xio; 12044 struct ctl_lun *lun; 12045 struct ctl_softc *softc; 12046#if 0 12047 struct sbuf sb; 12048 char printbuf[128]; 12049#endif 12050 int found; 12051 uint32_t targ_lun; 12052 12053 softc = control_softc; 12054 found = 0; 12055 12056 /* 12057 * Look up the LUN. 12058 */ 12059 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12060 mtx_lock(&softc->ctl_lock); 12061 if ((targ_lun >= CTL_MAX_LUNS) || 12062 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12063 mtx_unlock(&softc->ctl_lock); 12064 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12065 return (1); 12066 } 12067 12068#if 0 12069 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n", 12070 lun->lun, io->taskio.tag_num, io->taskio.tag_type); 12071#endif 12072 12073 mtx_lock(&lun->lun_lock); 12074 mtx_unlock(&softc->ctl_lock); 12075 /* 12076 * Run through the OOA queue and attempt to find the given I/O. 12077 * The target port, initiator ID, tag type and tag number have to 12078 * match the values that we got from the initiator. If we have an 12079 * untagged command to abort, simply abort the first untagged command 12080 * we come to. We only allow one untagged command at a time of course. 12081 */ 12082 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12083 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12084#if 0 12085 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN); 12086 12087 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ", 12088 lun->lun, xio->scsiio.tag_num, 12089 xio->scsiio.tag_type, 12090 (xio->io_hdr.blocked_links.tqe_prev 12091 == NULL) ? "" : " BLOCKED", 12092 (xio->io_hdr.flags & 12093 CTL_FLAG_DMA_INPROG) ? " DMA" : "", 12094 (xio->io_hdr.flags & 12095 CTL_FLAG_ABORT) ? " ABORT" : "", 12096 (xio->io_hdr.flags & 12097 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : "")); 12098 ctl_scsi_command_string(&xio->scsiio, NULL, &sb); 12099 sbuf_finish(&sb); 12100 printf("%s\n", sbuf_data(&sb)); 12101#endif 12102 12103 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12104 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12105 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12106 continue; 12107 12108 /* 12109 * If the abort says that the task is untagged, the 12110 * task in the queue must be untagged. Otherwise, 12111 * we just check to see whether the tag numbers 12112 * match. This is because the QLogic firmware 12113 * doesn't pass back the tag type in an abort 12114 * request. 12115 */ 12116#if 0 12117 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED) 12118 && (io->taskio.tag_type == CTL_TAG_UNTAGGED)) 12119 || (xio->scsiio.tag_num == io->taskio.tag_num)) 12120#endif 12121 /* 12122 * XXX KDM we've got problems with FC, because it 12123 * doesn't send down a tag type with aborts. So we 12124 * can only really go by the tag number... 12125 * This may cause problems with parallel SCSI. 12126 * Need to figure that out!! 12127 */ 12128 if (xio->scsiio.tag_num == io->taskio.tag_num) { 12129 xio->io_hdr.flags |= CTL_FLAG_ABORT; 12130 found = 1; 12131 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 && 12132 !(lun->flags & CTL_LUN_PRIMARY_SC)) { 12133 union ctl_ha_msg msg_info; 12134 12135 msg_info.hdr.nexus = io->io_hdr.nexus; 12136 msg_info.task.task_action = CTL_TASK_ABORT_TASK; 12137 msg_info.task.tag_num = io->taskio.tag_num; 12138 msg_info.task.tag_type = io->taskio.tag_type; 12139 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS; 12140 msg_info.hdr.original_sc = NULL; 12141 msg_info.hdr.serializing_sc = NULL; 12142#if 0 12143 printf("Sent Abort to other side\n"); 12144#endif 12145 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info, 12146 sizeof(msg_info.task), M_NOWAIT); 12147 } 12148#if 0 12149 printf("ctl_abort_task: found I/O to abort\n"); 12150#endif 12151 } 12152 } 12153 mtx_unlock(&lun->lun_lock); 12154 12155 if (found == 0) { 12156 /* 12157 * This isn't really an error. It's entirely possible for 12158 * the abort and command completion to cross on the wire. 12159 * This is more of an informative/diagnostic error. 12160 */ 12161#if 0 12162 printf("ctl_abort_task: ABORT sent for nonexistent I/O: " 12163 "%u:%u:%u tag %d type %d\n", 12164 io->io_hdr.nexus.initid, 12165 io->io_hdr.nexus.targ_port, 12166 io->io_hdr.nexus.targ_lun, io->taskio.tag_num, 12167 io->taskio.tag_type); 12168#endif 12169 } 12170 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12171 return (0); 12172} 12173 12174static int 12175ctl_query_task(union ctl_io *io, int task_set) 12176{ 12177 union ctl_io *xio; 12178 struct ctl_lun *lun; 12179 struct ctl_softc *softc; 12180 int found = 0; 12181 uint32_t targ_lun; 12182 12183 softc = control_softc; 12184 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12185 mtx_lock(&softc->ctl_lock); 12186 if ((targ_lun >= CTL_MAX_LUNS) || 12187 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12188 mtx_unlock(&softc->ctl_lock); 12189 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12190 return (1); 12191 } 12192 mtx_lock(&lun->lun_lock); 12193 mtx_unlock(&softc->ctl_lock); 12194 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL; 12195 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) { 12196 12197 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port) 12198 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid) 12199 || (xio->io_hdr.flags & CTL_FLAG_ABORT)) 12200 continue; 12201 12202 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) { 12203 found = 1; 12204 break; 12205 } 12206 } 12207 mtx_unlock(&lun->lun_lock); 12208 if (found) 12209 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12210 else 12211 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12212 return (0); 12213} 12214 12215static int 12216ctl_query_async_event(union ctl_io *io) 12217{ 12218 struct ctl_lun *lun; 12219 struct ctl_softc *softc; 12220 ctl_ua_type ua; 12221 uint32_t targ_lun, initidx; 12222 12223 softc = control_softc; 12224 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12225 mtx_lock(&softc->ctl_lock); 12226 if ((targ_lun >= CTL_MAX_LUNS) || 12227 (lun = softc->ctl_luns[targ_lun]) == NULL) { 12228 mtx_unlock(&softc->ctl_lock); 12229 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST; 12230 return (1); 12231 } 12232 mtx_lock(&lun->lun_lock); 12233 mtx_unlock(&softc->ctl_lock); 12234 initidx = ctl_get_initindex(&io->io_hdr.nexus); 12235 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp); 12236 mtx_unlock(&lun->lun_lock); 12237 if (ua != CTL_UA_NONE) 12238 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED; 12239 else 12240 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE; 12241 return (0); 12242} 12243 12244static void 12245ctl_run_task(union ctl_io *io) 12246{ 12247 struct ctl_softc *softc = control_softc; 12248 int retval = 1; 12249 12250 CTL_DEBUG_PRINT(("ctl_run_task\n")); 12251 KASSERT(io->io_hdr.io_type == CTL_IO_TASK, 12252 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type)); 12253 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED; 12254 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp)); 12255 switch (io->taskio.task_action) { 12256 case CTL_TASK_ABORT_TASK: 12257 retval = ctl_abort_task(io); 12258 break; 12259 case CTL_TASK_ABORT_TASK_SET: 12260 case CTL_TASK_CLEAR_TASK_SET: 12261 retval = ctl_abort_task_set(io); 12262 break; 12263 case CTL_TASK_CLEAR_ACA: 12264 break; 12265 case CTL_TASK_I_T_NEXUS_RESET: 12266 retval = ctl_i_t_nexus_reset(io); 12267 break; 12268 case CTL_TASK_LUN_RESET: 12269 retval = ctl_lun_reset(softc, io); 12270 break; 12271 case CTL_TASK_TARGET_RESET: 12272 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET); 12273 break; 12274 case CTL_TASK_BUS_RESET: 12275 retval = ctl_bus_reset(softc, io); 12276 break; 12277 case CTL_TASK_PORT_LOGIN: 12278 break; 12279 case CTL_TASK_PORT_LOGOUT: 12280 break; 12281 case CTL_TASK_QUERY_TASK: 12282 retval = ctl_query_task(io, 0); 12283 break; 12284 case CTL_TASK_QUERY_TASK_SET: 12285 retval = ctl_query_task(io, 1); 12286 break; 12287 case CTL_TASK_QUERY_ASYNC_EVENT: 12288 retval = ctl_query_async_event(io); 12289 break; 12290 default: 12291 printf("%s: got unknown task management event %d\n", 12292 __func__, io->taskio.task_action); 12293 break; 12294 } 12295 if (retval == 0) 12296 io->io_hdr.status = CTL_SUCCESS; 12297 else 12298 io->io_hdr.status = CTL_ERROR; 12299 ctl_done(io); 12300} 12301 12302/* 12303 * For HA operation. Handle commands that come in from the other 12304 * controller. 12305 */ 12306static void 12307ctl_handle_isc(union ctl_io *io) 12308{ 12309 int free_io; 12310 struct ctl_lun *lun; 12311 struct ctl_softc *softc = control_softc; 12312 uint32_t targ_lun; 12313 12314 targ_lun = io->io_hdr.nexus.targ_mapped_lun; 12315 lun = softc->ctl_luns[targ_lun]; 12316 12317 switch (io->io_hdr.msg_type) { 12318 case CTL_MSG_SERIALIZE: 12319 free_io = ctl_serialize_other_sc_cmd(&io->scsiio); 12320 break; 12321 case CTL_MSG_R2R: { 12322 const struct ctl_cmd_entry *entry; 12323 12324 /* 12325 * This is only used in SER_ONLY mode. 12326 */ 12327 free_io = 0; 12328 entry = ctl_get_cmd_entry(&io->scsiio, NULL); 12329 mtx_lock(&lun->lun_lock); 12330 if (ctl_scsiio_lun_check(lun, 12331 entry, (struct ctl_scsiio *)io) != 0) { 12332 mtx_unlock(&lun->lun_lock); 12333 ctl_done(io); 12334 break; 12335 } 12336 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR; 12337 mtx_unlock(&lun->lun_lock); 12338 ctl_enqueue_rtr(io); 12339 break; 12340 } 12341 case CTL_MSG_FINISH_IO: 12342 if (softc->ha_mode == CTL_HA_MODE_XFER) { 12343 free_io = 0; 12344 ctl_done(io); 12345 } else { 12346 free_io = 1; 12347 mtx_lock(&lun->lun_lock); 12348 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, 12349 ooa_links); 12350 ctl_check_blocked(lun); 12351 mtx_unlock(&lun->lun_lock); 12352 } 12353 break; 12354 case CTL_MSG_PERS_ACTION: 12355 ctl_hndl_per_res_out_on_other_sc( 12356 (union ctl_ha_msg *)&io->presio.pr_msg); 12357 free_io = 1; 12358 break; 12359 case CTL_MSG_BAD_JUJU: 12360 free_io = 0; 12361 ctl_done(io); 12362 break; 12363 case CTL_MSG_DATAMOVE: 12364 /* Only used in XFER mode */ 12365 free_io = 0; 12366 ctl_datamove_remote(io); 12367 break; 12368 case CTL_MSG_DATAMOVE_DONE: 12369 /* Only used in XFER mode */ 12370 free_io = 0; 12371 io->scsiio.be_move_done(io); 12372 break; 12373 case CTL_MSG_FAILOVER: 12374 ctl_failover_lun(io); 12375 free_io = 1; 12376 break; 12377 default: 12378 free_io = 1; 12379 printf("%s: Invalid message type %d\n", 12380 __func__, io->io_hdr.msg_type); 12381 break; 12382 } 12383 if (free_io) 12384 ctl_free_io(io); 12385 12386} 12387 12388 12389/* 12390 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if 12391 * there is no match. 12392 */ 12393static ctl_lun_error_pattern 12394ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc) 12395{ 12396 const struct ctl_cmd_entry *entry; 12397 ctl_lun_error_pattern filtered_pattern, pattern; 12398 12399 pattern = desc->error_pattern; 12400 12401 /* 12402 * XXX KDM we need more data passed into this function to match a 12403 * custom pattern, and we actually need to implement custom pattern 12404 * matching. 12405 */ 12406 if (pattern & CTL_LUN_PAT_CMD) 12407 return (CTL_LUN_PAT_CMD); 12408 12409 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY) 12410 return (CTL_LUN_PAT_ANY); 12411 12412 entry = ctl_get_cmd_entry(ctsio, NULL); 12413 12414 filtered_pattern = entry->pattern & pattern; 12415 12416 /* 12417 * If the user requested specific flags in the pattern (e.g. 12418 * CTL_LUN_PAT_RANGE), make sure the command supports all of those 12419 * flags. 12420 * 12421 * If the user did not specify any flags, it doesn't matter whether 12422 * or not the command supports the flags. 12423 */ 12424 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) != 12425 (pattern & ~CTL_LUN_PAT_MASK)) 12426 return (CTL_LUN_PAT_NONE); 12427 12428 /* 12429 * If the user asked for a range check, see if the requested LBA 12430 * range overlaps with this command's LBA range. 12431 */ 12432 if (filtered_pattern & CTL_LUN_PAT_RANGE) { 12433 uint64_t lba1; 12434 uint64_t len1; 12435 ctl_action action; 12436 int retval; 12437 12438 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1); 12439 if (retval != 0) 12440 return (CTL_LUN_PAT_NONE); 12441 12442 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba, 12443 desc->lba_range.len, FALSE); 12444 /* 12445 * A "pass" means that the LBA ranges don't overlap, so 12446 * this doesn't match the user's range criteria. 12447 */ 12448 if (action == CTL_ACTION_PASS) 12449 return (CTL_LUN_PAT_NONE); 12450 } 12451 12452 return (filtered_pattern); 12453} 12454 12455static void 12456ctl_inject_error(struct ctl_lun *lun, union ctl_io *io) 12457{ 12458 struct ctl_error_desc *desc, *desc2; 12459 12460 mtx_assert(&lun->lun_lock, MA_OWNED); 12461 12462 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) { 12463 ctl_lun_error_pattern pattern; 12464 /* 12465 * Check to see whether this particular command matches 12466 * the pattern in the descriptor. 12467 */ 12468 pattern = ctl_cmd_pattern_match(&io->scsiio, desc); 12469 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE) 12470 continue; 12471 12472 switch (desc->lun_error & CTL_LUN_INJ_TYPE) { 12473 case CTL_LUN_INJ_ABORTED: 12474 ctl_set_aborted(&io->scsiio); 12475 break; 12476 case CTL_LUN_INJ_MEDIUM_ERR: 12477 ctl_set_medium_error(&io->scsiio, 12478 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) != 12479 CTL_FLAG_DATA_OUT); 12480 break; 12481 case CTL_LUN_INJ_UA: 12482 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET 12483 * OCCURRED */ 12484 ctl_set_ua(&io->scsiio, 0x29, 0x00); 12485 break; 12486 case CTL_LUN_INJ_CUSTOM: 12487 /* 12488 * We're assuming the user knows what he is doing. 12489 * Just copy the sense information without doing 12490 * checks. 12491 */ 12492 bcopy(&desc->custom_sense, &io->scsiio.sense_data, 12493 MIN(sizeof(desc->custom_sense), 12494 sizeof(io->scsiio.sense_data))); 12495 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND; 12496 io->scsiio.sense_len = SSD_FULL_SIZE; 12497 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE; 12498 break; 12499 case CTL_LUN_INJ_NONE: 12500 default: 12501 /* 12502 * If this is an error injection type we don't know 12503 * about, clear the continuous flag (if it is set) 12504 * so it will get deleted below. 12505 */ 12506 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS; 12507 break; 12508 } 12509 /* 12510 * By default, each error injection action is a one-shot 12511 */ 12512 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS) 12513 continue; 12514 12515 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links); 12516 12517 free(desc, M_CTL); 12518 } 12519} 12520 12521#ifdef CTL_IO_DELAY 12522static void 12523ctl_datamove_timer_wakeup(void *arg) 12524{ 12525 union ctl_io *io; 12526 12527 io = (union ctl_io *)arg; 12528 12529 ctl_datamove(io); 12530} 12531#endif /* CTL_IO_DELAY */ 12532 12533void 12534ctl_datamove(union ctl_io *io) 12535{ 12536 struct ctl_lun *lun; 12537 void (*fe_datamove)(union ctl_io *io); 12538 12539 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 12540 12541 CTL_DEBUG_PRINT(("ctl_datamove\n")); 12542 12543 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 12544#ifdef CTL_TIME_IO 12545 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 12546 char str[256]; 12547 char path_str[64]; 12548 struct sbuf sb; 12549 12550 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 12551 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12552 12553 sbuf_cat(&sb, path_str); 12554 switch (io->io_hdr.io_type) { 12555 case CTL_IO_SCSI: 12556 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 12557 sbuf_printf(&sb, "\n"); 12558 sbuf_cat(&sb, path_str); 12559 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12560 io->scsiio.tag_num, io->scsiio.tag_type); 12561 break; 12562 case CTL_IO_TASK: 12563 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 12564 "Tag Type: %d\n", io->taskio.task_action, 12565 io->taskio.tag_num, io->taskio.tag_type); 12566 break; 12567 default: 12568 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12569 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 12570 break; 12571 } 12572 sbuf_cat(&sb, path_str); 12573 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n", 12574 (intmax_t)time_uptime - io->io_hdr.start_time); 12575 sbuf_finish(&sb); 12576 printf("%s", sbuf_data(&sb)); 12577 } 12578#endif /* CTL_TIME_IO */ 12579 12580#ifdef CTL_IO_DELAY 12581 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 12582 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 12583 } else { 12584 if ((lun != NULL) 12585 && (lun->delay_info.datamove_delay > 0)) { 12586 12587 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 12588 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 12589 callout_reset(&io->io_hdr.delay_callout, 12590 lun->delay_info.datamove_delay * hz, 12591 ctl_datamove_timer_wakeup, io); 12592 if (lun->delay_info.datamove_type == 12593 CTL_DELAY_TYPE_ONESHOT) 12594 lun->delay_info.datamove_delay = 0; 12595 return; 12596 } 12597 } 12598#endif 12599 12600 /* 12601 * This command has been aborted. Set the port status, so we fail 12602 * the data move. 12603 */ 12604 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 12605 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n", 12606 io->scsiio.tag_num, io->io_hdr.nexus.initid, 12607 io->io_hdr.nexus.targ_port, 12608 io->io_hdr.nexus.targ_lun); 12609 io->io_hdr.port_status = 31337; 12610 /* 12611 * Note that the backend, in this case, will get the 12612 * callback in its context. In other cases it may get 12613 * called in the frontend's interrupt thread context. 12614 */ 12615 io->scsiio.be_move_done(io); 12616 return; 12617 } 12618 12619 /* Don't confuse frontend with zero length data move. */ 12620 if (io->scsiio.kern_data_len == 0) { 12621 io->scsiio.be_move_done(io); 12622 return; 12623 } 12624 12625 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12626 fe_datamove(io); 12627} 12628 12629static void 12630ctl_send_datamove_done(union ctl_io *io, int have_lock) 12631{ 12632 union ctl_ha_msg msg; 12633#ifdef CTL_TIME_IO 12634 struct bintime cur_bt; 12635#endif 12636 12637 memset(&msg, 0, sizeof(msg)); 12638 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE; 12639 msg.hdr.original_sc = io; 12640 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 12641 msg.hdr.nexus = io->io_hdr.nexus; 12642 msg.hdr.status = io->io_hdr.status; 12643 msg.scsi.tag_num = io->scsiio.tag_num; 12644 msg.scsi.tag_type = io->scsiio.tag_type; 12645 msg.scsi.scsi_status = io->scsiio.scsi_status; 12646 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data, 12647 io->scsiio.sense_len); 12648 msg.scsi.sense_len = io->scsiio.sense_len; 12649 msg.scsi.sense_residual = io->scsiio.sense_residual; 12650 msg.scsi.fetd_status = io->io_hdr.port_status; 12651 msg.scsi.residual = io->scsiio.residual; 12652 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE; 12653 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 12654 ctl_failover_io(io, /*have_lock*/ have_lock); 12655 return; 12656 } 12657 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 12658 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) + 12659 msg.scsi.sense_len, M_WAITOK); 12660 12661#ifdef CTL_TIME_IO 12662 getbinuptime(&cur_bt); 12663 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt); 12664 bintime_add(&io->io_hdr.dma_bt, &cur_bt); 12665#endif 12666 io->io_hdr.num_dmas++; 12667} 12668 12669/* 12670 * The DMA to the remote side is done, now we need to tell the other side 12671 * we're done so it can continue with its data movement. 12672 */ 12673static void 12674ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq) 12675{ 12676 union ctl_io *io; 12677 int i; 12678 12679 io = rq->context; 12680 12681 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12682 printf("%s: ISC DMA write failed with error %d", __func__, 12683 rq->ret); 12684 ctl_set_internal_failure(&io->scsiio, 12685 /*sks_valid*/ 1, 12686 /*retry_count*/ rq->ret); 12687 } 12688 12689 ctl_dt_req_free(rq); 12690 12691 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12692 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12693 free(io->io_hdr.remote_sglist, M_CTL); 12694 io->io_hdr.remote_sglist = NULL; 12695 io->io_hdr.local_sglist = NULL; 12696 12697 /* 12698 * The data is in local and remote memory, so now we need to send 12699 * status (good or back) back to the other side. 12700 */ 12701 ctl_send_datamove_done(io, /*have_lock*/ 0); 12702} 12703 12704/* 12705 * We've moved the data from the host/controller into local memory. Now we 12706 * need to push it over to the remote controller's memory. 12707 */ 12708static int 12709ctl_datamove_remote_dm_write_cb(union ctl_io *io) 12710{ 12711 int retval; 12712 12713 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE, 12714 ctl_datamove_remote_write_cb); 12715 return (retval); 12716} 12717 12718static void 12719ctl_datamove_remote_write(union ctl_io *io) 12720{ 12721 int retval; 12722 void (*fe_datamove)(union ctl_io *io); 12723 12724 /* 12725 * - Get the data from the host/HBA into local memory. 12726 * - DMA memory from the local controller to the remote controller. 12727 * - Send status back to the remote controller. 12728 */ 12729 12730 retval = ctl_datamove_remote_sgl_setup(io); 12731 if (retval != 0) 12732 return; 12733 12734 /* Switch the pointer over so the FETD knows what to do */ 12735 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12736 12737 /* 12738 * Use a custom move done callback, since we need to send completion 12739 * back to the other controller, not to the backend on this side. 12740 */ 12741 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb; 12742 12743 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12744 fe_datamove(io); 12745} 12746 12747static int 12748ctl_datamove_remote_dm_read_cb(union ctl_io *io) 12749{ 12750#if 0 12751 char str[256]; 12752 char path_str[64]; 12753 struct sbuf sb; 12754#endif 12755 int i; 12756 12757 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12758 free(io->io_hdr.local_sglist[i].addr, M_CTL); 12759 free(io->io_hdr.remote_sglist, M_CTL); 12760 io->io_hdr.remote_sglist = NULL; 12761 io->io_hdr.local_sglist = NULL; 12762 12763#if 0 12764 scsi_path_string(io, path_str, sizeof(path_str)); 12765 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 12766 sbuf_cat(&sb, path_str); 12767 scsi_command_string(&io->scsiio, NULL, &sb); 12768 sbuf_printf(&sb, "\n"); 12769 sbuf_cat(&sb, path_str); 12770 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 12771 io->scsiio.tag_num, io->scsiio.tag_type); 12772 sbuf_cat(&sb, path_str); 12773 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__, 12774 io->io_hdr.flags, io->io_hdr.status); 12775 sbuf_finish(&sb); 12776 printk("%s", sbuf_data(&sb)); 12777#endif 12778 12779 12780 /* 12781 * The read is done, now we need to send status (good or bad) back 12782 * to the other side. 12783 */ 12784 ctl_send_datamove_done(io, /*have_lock*/ 0); 12785 12786 return (0); 12787} 12788 12789static void 12790ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq) 12791{ 12792 union ctl_io *io; 12793 void (*fe_datamove)(union ctl_io *io); 12794 12795 io = rq->context; 12796 12797 if (rq->ret != CTL_HA_STATUS_SUCCESS) { 12798 printf("%s: ISC DMA read failed with error %d\n", __func__, 12799 rq->ret); 12800 ctl_set_internal_failure(&io->scsiio, 12801 /*sks_valid*/ 1, 12802 /*retry_count*/ rq->ret); 12803 } 12804 12805 ctl_dt_req_free(rq); 12806 12807 /* Switch the pointer over so the FETD knows what to do */ 12808 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist; 12809 12810 /* 12811 * Use a custom move done callback, since we need to send completion 12812 * back to the other controller, not to the backend on this side. 12813 */ 12814 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb; 12815 12816 /* XXX KDM add checks like the ones in ctl_datamove? */ 12817 12818 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove; 12819 fe_datamove(io); 12820} 12821 12822static int 12823ctl_datamove_remote_sgl_setup(union ctl_io *io) 12824{ 12825 struct ctl_sg_entry *local_sglist; 12826 uint32_t len_to_go; 12827 int retval; 12828 int i; 12829 12830 retval = 0; 12831 local_sglist = io->io_hdr.local_sglist; 12832 len_to_go = io->scsiio.kern_data_len; 12833 12834 /* 12835 * The difficult thing here is that the size of the various 12836 * S/G segments may be different than the size from the 12837 * remote controller. That'll make it harder when DMAing 12838 * the data back to the other side. 12839 */ 12840 for (i = 0; len_to_go > 0; i++) { 12841 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT); 12842 local_sglist[i].addr = 12843 malloc(local_sglist[i].len, M_CTL, M_WAITOK); 12844 12845 len_to_go -= local_sglist[i].len; 12846 } 12847 /* 12848 * Reset the number of S/G entries accordingly. The original 12849 * number of S/G entries is available in rem_sg_entries. 12850 */ 12851 io->scsiio.kern_sg_entries = i; 12852 12853#if 0 12854 printf("%s: kern_sg_entries = %d\n", __func__, 12855 io->scsiio.kern_sg_entries); 12856 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 12857 printf("%s: sg[%d] = %p, %lu\n", __func__, i, 12858 local_sglist[i].addr, local_sglist[i].len); 12859#endif 12860 12861 return (retval); 12862} 12863 12864static int 12865ctl_datamove_remote_xfer(union ctl_io *io, unsigned command, 12866 ctl_ha_dt_cb callback) 12867{ 12868 struct ctl_ha_dt_req *rq; 12869 struct ctl_sg_entry *remote_sglist, *local_sglist; 12870 uint32_t local_used, remote_used, total_used; 12871 int i, j, isc_ret; 12872 12873 rq = ctl_dt_req_alloc(); 12874 12875 /* 12876 * If we failed to allocate the request, and if the DMA didn't fail 12877 * anyway, set busy status. This is just a resource allocation 12878 * failure. 12879 */ 12880 if ((rq == NULL) 12881 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12882 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) 12883 ctl_set_busy(&io->scsiio); 12884 12885 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE && 12886 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) { 12887 12888 if (rq != NULL) 12889 ctl_dt_req_free(rq); 12890 12891 /* 12892 * The data move failed. We need to return status back 12893 * to the other controller. No point in trying to DMA 12894 * data to the remote controller. 12895 */ 12896 12897 ctl_send_datamove_done(io, /*have_lock*/ 0); 12898 12899 return (1); 12900 } 12901 12902 local_sglist = io->io_hdr.local_sglist; 12903 remote_sglist = io->io_hdr.remote_sglist; 12904 local_used = 0; 12905 remote_used = 0; 12906 total_used = 0; 12907 12908 /* 12909 * Pull/push the data over the wire from/to the other controller. 12910 * This takes into account the possibility that the local and 12911 * remote sglists may not be identical in terms of the size of 12912 * the elements and the number of elements. 12913 * 12914 * One fundamental assumption here is that the length allocated for 12915 * both the local and remote sglists is identical. Otherwise, we've 12916 * essentially got a coding error of some sort. 12917 */ 12918 isc_ret = CTL_HA_STATUS_SUCCESS; 12919 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) { 12920 uint32_t cur_len; 12921 uint8_t *tmp_ptr; 12922 12923 rq->command = command; 12924 rq->context = io; 12925 12926 /* 12927 * Both pointers should be aligned. But it is possible 12928 * that the allocation length is not. They should both 12929 * also have enough slack left over at the end, though, 12930 * to round up to the next 8 byte boundary. 12931 */ 12932 cur_len = MIN(local_sglist[i].len - local_used, 12933 remote_sglist[j].len - remote_used); 12934 rq->size = cur_len; 12935 12936 tmp_ptr = (uint8_t *)local_sglist[i].addr; 12937 tmp_ptr += local_used; 12938 12939#if 0 12940 /* Use physical addresses when talking to ISC hardware */ 12941 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) { 12942 /* XXX KDM use busdma */ 12943 rq->local = vtophys(tmp_ptr); 12944 } else 12945 rq->local = tmp_ptr; 12946#else 12947 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0, 12948 ("HA does not support BUS_ADDR")); 12949 rq->local = tmp_ptr; 12950#endif 12951 12952 tmp_ptr = (uint8_t *)remote_sglist[j].addr; 12953 tmp_ptr += remote_used; 12954 rq->remote = tmp_ptr; 12955 12956 rq->callback = NULL; 12957 12958 local_used += cur_len; 12959 if (local_used >= local_sglist[i].len) { 12960 i++; 12961 local_used = 0; 12962 } 12963 12964 remote_used += cur_len; 12965 if (remote_used >= remote_sglist[j].len) { 12966 j++; 12967 remote_used = 0; 12968 } 12969 total_used += cur_len; 12970 12971 if (total_used >= io->scsiio.kern_data_len) 12972 rq->callback = callback; 12973 12974#if 0 12975 printf("%s: %s: local %p remote %p size %d\n", __func__, 12976 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ", 12977 rq->local, rq->remote, rq->size); 12978#endif 12979 12980 isc_ret = ctl_dt_single(rq); 12981 if (isc_ret > CTL_HA_STATUS_SUCCESS) 12982 break; 12983 } 12984 if (isc_ret != CTL_HA_STATUS_WAIT) { 12985 rq->ret = isc_ret; 12986 callback(rq); 12987 } 12988 12989 return (0); 12990} 12991 12992static void 12993ctl_datamove_remote_read(union ctl_io *io) 12994{ 12995 int retval; 12996 int i; 12997 12998 /* 12999 * This will send an error to the other controller in the case of a 13000 * failure. 13001 */ 13002 retval = ctl_datamove_remote_sgl_setup(io); 13003 if (retval != 0) 13004 return; 13005 13006 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ, 13007 ctl_datamove_remote_read_cb); 13008 if (retval != 0) { 13009 /* 13010 * Make sure we free memory if there was an error.. The 13011 * ctl_datamove_remote_xfer() function will send the 13012 * datamove done message, or call the callback with an 13013 * error if there is a problem. 13014 */ 13015 for (i = 0; i < io->scsiio.kern_sg_entries; i++) 13016 free(io->io_hdr.local_sglist[i].addr, M_CTL); 13017 free(io->io_hdr.remote_sglist, M_CTL); 13018 io->io_hdr.remote_sglist = NULL; 13019 io->io_hdr.local_sglist = NULL; 13020 } 13021} 13022 13023/* 13024 * Process a datamove request from the other controller. This is used for 13025 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory 13026 * first. Once that is complete, the data gets DMAed into the remote 13027 * controller's memory. For reads, we DMA from the remote controller's 13028 * memory into our memory first, and then move it out to the FETD. 13029 */ 13030static void 13031ctl_datamove_remote(union ctl_io *io) 13032{ 13033 13034 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED); 13035 13036 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) { 13037 ctl_failover_io(io, /*have_lock*/ 0); 13038 return; 13039 } 13040 13041 /* 13042 * Note that we look for an aborted I/O here, but don't do some of 13043 * the other checks that ctl_datamove() normally does. 13044 * We don't need to run the datamove delay code, since that should 13045 * have been done if need be on the other controller. 13046 */ 13047 if (io->io_hdr.flags & CTL_FLAG_ABORT) { 13048 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__, 13049 io->scsiio.tag_num, io->io_hdr.nexus.initid, 13050 io->io_hdr.nexus.targ_port, 13051 io->io_hdr.nexus.targ_lun); 13052 io->io_hdr.port_status = 31338; 13053 ctl_send_datamove_done(io, /*have_lock*/ 0); 13054 return; 13055 } 13056 13057 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) 13058 ctl_datamove_remote_write(io); 13059 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) 13060 ctl_datamove_remote_read(io); 13061 else { 13062 io->io_hdr.port_status = 31339; 13063 ctl_send_datamove_done(io, /*have_lock*/ 0); 13064 } 13065} 13066 13067static void 13068ctl_process_done(union ctl_io *io) 13069{ 13070 struct ctl_lun *lun; 13071 struct ctl_softc *softc = control_softc; 13072 void (*fe_done)(union ctl_io *io); 13073 union ctl_ha_msg msg; 13074 uint32_t targ_port = io->io_hdr.nexus.targ_port; 13075 13076 CTL_DEBUG_PRINT(("ctl_process_done\n")); 13077 fe_done = softc->ctl_ports[targ_port]->fe_done; 13078 13079#ifdef CTL_TIME_IO 13080 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) { 13081 char str[256]; 13082 char path_str[64]; 13083 struct sbuf sb; 13084 13085 ctl_scsi_path_string(io, path_str, sizeof(path_str)); 13086 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN); 13087 13088 sbuf_cat(&sb, path_str); 13089 switch (io->io_hdr.io_type) { 13090 case CTL_IO_SCSI: 13091 ctl_scsi_command_string(&io->scsiio, NULL, &sb); 13092 sbuf_printf(&sb, "\n"); 13093 sbuf_cat(&sb, path_str); 13094 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n", 13095 io->scsiio.tag_num, io->scsiio.tag_type); 13096 break; 13097 case CTL_IO_TASK: 13098 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, " 13099 "Tag Type: %d\n", io->taskio.task_action, 13100 io->taskio.tag_num, io->taskio.tag_type); 13101 break; 13102 default: 13103 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13104 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type); 13105 break; 13106 } 13107 sbuf_cat(&sb, path_str); 13108 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n", 13109 (intmax_t)time_uptime - io->io_hdr.start_time); 13110 sbuf_finish(&sb); 13111 printf("%s", sbuf_data(&sb)); 13112 } 13113#endif /* CTL_TIME_IO */ 13114 13115 switch (io->io_hdr.io_type) { 13116 case CTL_IO_SCSI: 13117 break; 13118 case CTL_IO_TASK: 13119 if (ctl_debug & CTL_DEBUG_INFO) 13120 ctl_io_error_print(io, NULL); 13121 fe_done(io); 13122 return; 13123 default: 13124 panic("ctl_process_done: invalid io type %d\n", 13125 io->io_hdr.io_type); 13126 break; /* NOTREACHED */ 13127 } 13128 13129 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13130 if (lun == NULL) { 13131 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n", 13132 io->io_hdr.nexus.targ_mapped_lun)); 13133 goto bailout; 13134 } 13135 13136 mtx_lock(&lun->lun_lock); 13137 13138 /* 13139 * Check to see if we have any errors to inject here. We only 13140 * inject errors for commands that don't already have errors set. 13141 */ 13142 if (!STAILQ_EMPTY(&lun->error_list) && 13143 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) && 13144 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0)) 13145 ctl_inject_error(lun, io); 13146 13147 /* 13148 * XXX KDM how do we treat commands that aren't completed 13149 * successfully? 13150 * 13151 * XXX KDM should we also track I/O latency? 13152 */ 13153 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS && 13154 io->io_hdr.io_type == CTL_IO_SCSI) { 13155#ifdef CTL_TIME_IO 13156 struct bintime cur_bt; 13157#endif 13158 int type; 13159 13160 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13161 CTL_FLAG_DATA_IN) 13162 type = CTL_STATS_READ; 13163 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == 13164 CTL_FLAG_DATA_OUT) 13165 type = CTL_STATS_WRITE; 13166 else 13167 type = CTL_STATS_NO_IO; 13168 13169 lun->stats.ports[targ_port].bytes[type] += 13170 io->scsiio.kern_total_len; 13171 lun->stats.ports[targ_port].operations[type]++; 13172#ifdef CTL_TIME_IO 13173 bintime_add(&lun->stats.ports[targ_port].dma_time[type], 13174 &io->io_hdr.dma_bt); 13175 getbinuptime(&cur_bt); 13176 bintime_sub(&cur_bt, &io->io_hdr.start_bt); 13177 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt); 13178#endif 13179 lun->stats.ports[targ_port].num_dmas[type] += 13180 io->io_hdr.num_dmas; 13181 } 13182 13183 /* 13184 * Remove this from the OOA queue. 13185 */ 13186 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links); 13187#ifdef CTL_TIME_IO 13188 if (TAILQ_EMPTY(&lun->ooa_queue)) 13189 lun->last_busy = getsbinuptime(); 13190#endif 13191 13192 /* 13193 * Run through the blocked queue on this LUN and see if anything 13194 * has become unblocked, now that this transaction is done. 13195 */ 13196 ctl_check_blocked(lun); 13197 13198 /* 13199 * If the LUN has been invalidated, free it if there is nothing 13200 * left on its OOA queue. 13201 */ 13202 if ((lun->flags & CTL_LUN_INVALID) 13203 && TAILQ_EMPTY(&lun->ooa_queue)) { 13204 mtx_unlock(&lun->lun_lock); 13205 mtx_lock(&softc->ctl_lock); 13206 ctl_free_lun(lun); 13207 mtx_unlock(&softc->ctl_lock); 13208 } else 13209 mtx_unlock(&lun->lun_lock); 13210 13211bailout: 13212 13213 /* 13214 * If this command has been aborted, make sure we set the status 13215 * properly. The FETD is responsible for freeing the I/O and doing 13216 * whatever it needs to do to clean up its state. 13217 */ 13218 if (io->io_hdr.flags & CTL_FLAG_ABORT) 13219 ctl_set_task_aborted(&io->scsiio); 13220 13221 /* 13222 * If enabled, print command error status. 13223 */ 13224 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS && 13225 (ctl_debug & CTL_DEBUG_INFO) != 0) 13226 ctl_io_error_print(io, NULL); 13227 13228 /* 13229 * Tell the FETD or the other shelf controller we're done with this 13230 * command. Note that only SCSI commands get to this point. Task 13231 * management commands are completed above. 13232 */ 13233 if ((softc->ha_mode != CTL_HA_MODE_XFER) && 13234 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) { 13235 memset(&msg, 0, sizeof(msg)); 13236 msg.hdr.msg_type = CTL_MSG_FINISH_IO; 13237 msg.hdr.serializing_sc = io->io_hdr.serializing_sc; 13238 msg.hdr.nexus = io->io_hdr.nexus; 13239 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13240 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data), 13241 M_WAITOK); 13242 } 13243 13244 fe_done(io); 13245} 13246 13247#ifdef CTL_WITH_CA 13248/* 13249 * Front end should call this if it doesn't do autosense. When the request 13250 * sense comes back in from the initiator, we'll dequeue this and send it. 13251 */ 13252int 13253ctl_queue_sense(union ctl_io *io) 13254{ 13255 struct ctl_lun *lun; 13256 struct ctl_port *port; 13257 struct ctl_softc *softc; 13258 uint32_t initidx, targ_lun; 13259 13260 softc = control_softc; 13261 13262 CTL_DEBUG_PRINT(("ctl_queue_sense\n")); 13263 13264 /* 13265 * LUN lookup will likely move to the ctl_work_thread() once we 13266 * have our new queueing infrastructure (that doesn't put things on 13267 * a per-LUN queue initially). That is so that we can handle 13268 * things like an INQUIRY to a LUN that we don't have enabled. We 13269 * can't deal with that right now. 13270 */ 13271 mtx_lock(&softc->ctl_lock); 13272 13273 /* 13274 * If we don't have a LUN for this, just toss the sense 13275 * information. 13276 */ 13277 port = ctl_io_port(&ctsio->io_hdr); 13278 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13279 if ((targ_lun < CTL_MAX_LUNS) 13280 && (softc->ctl_luns[targ_lun] != NULL)) 13281 lun = softc->ctl_luns[targ_lun]; 13282 else 13283 goto bailout; 13284 13285 initidx = ctl_get_initindex(&io->io_hdr.nexus); 13286 13287 mtx_lock(&lun->lun_lock); 13288 /* 13289 * Already have CA set for this LUN...toss the sense information. 13290 */ 13291 if (ctl_is_set(lun->have_ca, initidx)) { 13292 mtx_unlock(&lun->lun_lock); 13293 goto bailout; 13294 } 13295 13296 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data, 13297 MIN(sizeof(lun->pending_sense[initidx]), 13298 sizeof(io->scsiio.sense_data))); 13299 ctl_set_mask(lun->have_ca, initidx); 13300 mtx_unlock(&lun->lun_lock); 13301 13302bailout: 13303 mtx_unlock(&softc->ctl_lock); 13304 13305 ctl_free_io(io); 13306 13307 return (CTL_RETVAL_COMPLETE); 13308} 13309#endif 13310 13311/* 13312 * Primary command inlet from frontend ports. All SCSI and task I/O 13313 * requests must go through this function. 13314 */ 13315int 13316ctl_queue(union ctl_io *io) 13317{ 13318 struct ctl_port *port; 13319 13320 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0])); 13321 13322#ifdef CTL_TIME_IO 13323 io->io_hdr.start_time = time_uptime; 13324 getbinuptime(&io->io_hdr.start_bt); 13325#endif /* CTL_TIME_IO */ 13326 13327 /* Map FE-specific LUN ID into global one. */ 13328 port = ctl_io_port(&io->io_hdr); 13329 io->io_hdr.nexus.targ_mapped_lun = 13330 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun); 13331 13332 switch (io->io_hdr.io_type) { 13333 case CTL_IO_SCSI: 13334 case CTL_IO_TASK: 13335 if (ctl_debug & CTL_DEBUG_CDB) 13336 ctl_io_print(io); 13337 ctl_enqueue_incoming(io); 13338 break; 13339 default: 13340 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type); 13341 return (EINVAL); 13342 } 13343 13344 return (CTL_RETVAL_COMPLETE); 13345} 13346 13347#ifdef CTL_IO_DELAY 13348static void 13349ctl_done_timer_wakeup(void *arg) 13350{ 13351 union ctl_io *io; 13352 13353 io = (union ctl_io *)arg; 13354 ctl_done(io); 13355} 13356#endif /* CTL_IO_DELAY */ 13357 13358void 13359ctl_serseq_done(union ctl_io *io) 13360{ 13361 struct ctl_lun *lun; 13362 13363 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13364 if (lun->be_lun == NULL || 13365 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF) 13366 return; 13367 mtx_lock(&lun->lun_lock); 13368 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE; 13369 ctl_check_blocked(lun); 13370 mtx_unlock(&lun->lun_lock); 13371} 13372 13373void 13374ctl_done(union ctl_io *io) 13375{ 13376 13377 /* 13378 * Enable this to catch duplicate completion issues. 13379 */ 13380#if 0 13381 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) { 13382 printf("%s: type %d msg %d cdb %x iptl: " 13383 "%u:%u:%u tag 0x%04x " 13384 "flag %#x status %x\n", 13385 __func__, 13386 io->io_hdr.io_type, 13387 io->io_hdr.msg_type, 13388 io->scsiio.cdb[0], 13389 io->io_hdr.nexus.initid, 13390 io->io_hdr.nexus.targ_port, 13391 io->io_hdr.nexus.targ_lun, 13392 (io->io_hdr.io_type == 13393 CTL_IO_TASK) ? 13394 io->taskio.tag_num : 13395 io->scsiio.tag_num, 13396 io->io_hdr.flags, 13397 io->io_hdr.status); 13398 } else 13399 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE; 13400#endif 13401 13402 /* 13403 * This is an internal copy of an I/O, and should not go through 13404 * the normal done processing logic. 13405 */ 13406 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) 13407 return; 13408 13409#ifdef CTL_IO_DELAY 13410 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) { 13411 struct ctl_lun *lun; 13412 13413 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13414 13415 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE; 13416 } else { 13417 struct ctl_lun *lun; 13418 13419 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr; 13420 13421 if ((lun != NULL) 13422 && (lun->delay_info.done_delay > 0)) { 13423 13424 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1); 13425 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE; 13426 callout_reset(&io->io_hdr.delay_callout, 13427 lun->delay_info.done_delay * hz, 13428 ctl_done_timer_wakeup, io); 13429 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT) 13430 lun->delay_info.done_delay = 0; 13431 return; 13432 } 13433 } 13434#endif /* CTL_IO_DELAY */ 13435 13436 ctl_enqueue_done(io); 13437} 13438 13439static void 13440ctl_work_thread(void *arg) 13441{ 13442 struct ctl_thread *thr = (struct ctl_thread *)arg; 13443 struct ctl_softc *softc = thr->ctl_softc; 13444 union ctl_io *io; 13445 int retval; 13446 13447 CTL_DEBUG_PRINT(("ctl_work_thread starting\n")); 13448 13449 for (;;) { 13450 /* 13451 * We handle the queues in this order: 13452 * - ISC 13453 * - done queue (to free up resources, unblock other commands) 13454 * - RtR queue 13455 * - incoming queue 13456 * 13457 * If those queues are empty, we break out of the loop and 13458 * go to sleep. 13459 */ 13460 mtx_lock(&thr->queue_lock); 13461 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue); 13462 if (io != NULL) { 13463 STAILQ_REMOVE_HEAD(&thr->isc_queue, links); 13464 mtx_unlock(&thr->queue_lock); 13465 ctl_handle_isc(io); 13466 continue; 13467 } 13468 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue); 13469 if (io != NULL) { 13470 STAILQ_REMOVE_HEAD(&thr->done_queue, links); 13471 /* clear any blocked commands, call fe_done */ 13472 mtx_unlock(&thr->queue_lock); 13473 ctl_process_done(io); 13474 continue; 13475 } 13476 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue); 13477 if (io != NULL) { 13478 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links); 13479 mtx_unlock(&thr->queue_lock); 13480 if (io->io_hdr.io_type == CTL_IO_TASK) 13481 ctl_run_task(io); 13482 else 13483 ctl_scsiio_precheck(softc, &io->scsiio); 13484 continue; 13485 } 13486 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue); 13487 if (io != NULL) { 13488 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links); 13489 mtx_unlock(&thr->queue_lock); 13490 retval = ctl_scsiio(&io->scsiio); 13491 if (retval != CTL_RETVAL_COMPLETE) 13492 CTL_DEBUG_PRINT(("ctl_scsiio failed\n")); 13493 continue; 13494 } 13495 13496 /* Sleep until we have something to do. */ 13497 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0); 13498 } 13499} 13500 13501static void 13502ctl_lun_thread(void *arg) 13503{ 13504 struct ctl_softc *softc = (struct ctl_softc *)arg; 13505 struct ctl_be_lun *be_lun; 13506 13507 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n")); 13508 13509 for (;;) { 13510 mtx_lock(&softc->ctl_lock); 13511 be_lun = STAILQ_FIRST(&softc->pending_lun_queue); 13512 if (be_lun != NULL) { 13513 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links); 13514 mtx_unlock(&softc->ctl_lock); 13515 ctl_create_lun(be_lun); 13516 continue; 13517 } 13518 13519 /* Sleep until we have something to do. */ 13520 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock, 13521 PDROP | PRIBIO, "-", 0); 13522 } 13523} 13524 13525static void 13526ctl_thresh_thread(void *arg) 13527{ 13528 struct ctl_softc *softc = (struct ctl_softc *)arg; 13529 struct ctl_lun *lun; 13530 struct scsi_da_rw_recovery_page *rwpage; 13531 struct ctl_logical_block_provisioning_page *page; 13532 const char *attr; 13533 union ctl_ha_msg msg; 13534 uint64_t thres, val; 13535 int i, e, set; 13536 13537 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n")); 13538 13539 for (;;) { 13540 mtx_lock(&softc->ctl_lock); 13541 STAILQ_FOREACH(lun, &softc->lun_list, links) { 13542 if ((lun->flags & CTL_LUN_DISABLED) || 13543 (lun->flags & CTL_LUN_NO_MEDIA) || 13544 lun->backend->lun_attr == NULL) 13545 continue; 13546 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 && 13547 softc->ha_mode == CTL_HA_MODE_XFER) 13548 continue; 13549 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT]; 13550 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0) 13551 continue; 13552 e = 0; 13553 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT]; 13554 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) { 13555 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0) 13556 continue; 13557 thres = scsi_4btoul(page->descr[i].count); 13558 thres <<= CTL_LBP_EXPONENT; 13559 switch (page->descr[i].resource) { 13560 case 0x01: 13561 attr = "blocksavail"; 13562 break; 13563 case 0x02: 13564 attr = "blocksused"; 13565 break; 13566 case 0xf1: 13567 attr = "poolblocksavail"; 13568 break; 13569 case 0xf2: 13570 attr = "poolblocksused"; 13571 break; 13572 default: 13573 continue; 13574 } 13575 mtx_unlock(&softc->ctl_lock); // XXX 13576 val = lun->backend->lun_attr( 13577 lun->be_lun->be_lun, attr); 13578 mtx_lock(&softc->ctl_lock); 13579 if (val == UINT64_MAX) 13580 continue; 13581 if ((page->descr[i].flags & SLBPPD_ARMING_MASK) 13582 == SLBPPD_ARMING_INC) 13583 e = (val >= thres); 13584 else 13585 e = (val <= thres); 13586 if (e) 13587 break; 13588 } 13589 mtx_lock(&lun->lun_lock); 13590 if (e) { 13591 scsi_u64to8b((uint8_t *)&page->descr[i] - 13592 (uint8_t *)page, lun->ua_tpt_info); 13593 if (lun->lasttpt == 0 || 13594 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) { 13595 lun->lasttpt = time_uptime; 13596 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13597 set = 1; 13598 } else 13599 set = 0; 13600 } else { 13601 lun->lasttpt = 0; 13602 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES); 13603 set = -1; 13604 } 13605 mtx_unlock(&lun->lun_lock); 13606 if (set != 0 && 13607 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) { 13608 /* Send msg to other side. */ 13609 bzero(&msg.ua, sizeof(msg.ua)); 13610 msg.hdr.msg_type = CTL_MSG_UA; 13611 msg.hdr.nexus.initid = -1; 13612 msg.hdr.nexus.targ_port = -1; 13613 msg.hdr.nexus.targ_lun = lun->lun; 13614 msg.hdr.nexus.targ_mapped_lun = lun->lun; 13615 msg.ua.ua_all = 1; 13616 msg.ua.ua_set = (set > 0); 13617 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES; 13618 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8); 13619 mtx_unlock(&softc->ctl_lock); // XXX 13620 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, 13621 sizeof(msg.ua), M_WAITOK); 13622 mtx_lock(&softc->ctl_lock); 13623 } 13624 } 13625 mtx_unlock(&softc->ctl_lock); 13626 pause("-", CTL_LBP_PERIOD * hz); 13627 } 13628} 13629 13630static void 13631ctl_enqueue_incoming(union ctl_io *io) 13632{ 13633 struct ctl_softc *softc = control_softc; 13634 struct ctl_thread *thr; 13635 u_int idx; 13636 13637 idx = (io->io_hdr.nexus.targ_port * 127 + 13638 io->io_hdr.nexus.initid) % worker_threads; 13639 thr = &softc->threads[idx]; 13640 mtx_lock(&thr->queue_lock); 13641 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links); 13642 mtx_unlock(&thr->queue_lock); 13643 wakeup(thr); 13644} 13645 13646static void 13647ctl_enqueue_rtr(union ctl_io *io) 13648{ 13649 struct ctl_softc *softc = control_softc; 13650 struct ctl_thread *thr; 13651 13652 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13653 mtx_lock(&thr->queue_lock); 13654 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links); 13655 mtx_unlock(&thr->queue_lock); 13656 wakeup(thr); 13657} 13658 13659static void 13660ctl_enqueue_done(union ctl_io *io) 13661{ 13662 struct ctl_softc *softc = control_softc; 13663 struct ctl_thread *thr; 13664 13665 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13666 mtx_lock(&thr->queue_lock); 13667 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links); 13668 mtx_unlock(&thr->queue_lock); 13669 wakeup(thr); 13670} 13671 13672static void 13673ctl_enqueue_isc(union ctl_io *io) 13674{ 13675 struct ctl_softc *softc = control_softc; 13676 struct ctl_thread *thr; 13677 13678 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads]; 13679 mtx_lock(&thr->queue_lock); 13680 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links); 13681 mtx_unlock(&thr->queue_lock); 13682 wakeup(thr); 13683} 13684 13685/* 13686 * vim: ts=8 13687 */ 13688